Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

The implementation of health api of ES Learning Notes

Shulou Source: shulou.com Published: 2022-06-02 19:10:01 10月03日 Update

Use health api to view the health of the es cluster. The usage of health api is as follows:

Curl 'http://localhost:9200/_cluster/health'

The return value of health api has a core field status, and status has three values: green, yellow, and red. It represents the three states of the cluster: both the master shard and the replica have been assigned, the master shard has been assigned and the replica shard has not been assigned, and neither the master shard nor the replica has been assigned.

In other words, the core of health api's concern is the high availability of data.

Look at the implementation of health api:

The request is routed to the master node and then reads the routing_table. Exe in clusterState.

Judge the state of each part of the index based on routing_table

Public ClusterShardHealth (int shardId, final IndexShardRoutingTable shardRoutingTable) {this.shardId = shardId; for (ShardRouting shardRouting: shardRoutingTable) {if (shardRouting.active ()) {activeShards++; if (shardRouting.relocating ()) {/ / the shard is relocating, the one it is relocating to will be in initializing state, so we don't count it relocatingShards++ } if (shardRouting.primary ()) {primaryActive = true;}} else if (shardRouting.initializing ()) {initializingShards++;} else if (shardRouting.unassigned ()) {unassignedShards++ }} if (primaryActive) {if (activeShards = = shardRoutingTable.size ()) {status = ClusterHealthStatus.GREEN;} else {status = ClusterHealthStatus.YELLOW;}} else {status = ClusterHealthStatus.RED;}}

Based on fragmentation, determine the status of the index

Public ClusterIndexHealth (IndexMetaData indexMetaData, IndexRoutingTable indexRoutingTable) {this.index = indexMetaData.getIndex (); this.numberOfShards = indexMetaData.getNumberOfShards (); this.numberOfReplicas = indexMetaData.getNumberOfReplicas (); this.validationFailures = indexRoutingTable.validate (indexMetaData); for (IndexShardRoutingTable shardRoutingTable: indexRoutingTable) {int shardId = shardRoutingTable.shardId (). Id (); shards.put (shardId, new ClusterShardHealth (shardId, shardRoutingTable)) } / / update the index status status = ClusterHealthStatus.GREEN; for (ClusterShardHealth shardHealth: shards.values ()) {if (shardHealth.isPrimaryActive ()) {activePrimaryShards++;} activeShards + = shardHealth.getActiveShards (); relocatingShards + = shardHealth.getRelocatingShards (); initializingShards + = shardHealth.getInitializingShards (); unassignedShards + = shardHealth.getUnassignedShards () If (shardHealth.getStatus () = = ClusterHealthStatus.RED) {status = ClusterHealthStatus.RED;} else if (shardHealth.getStatus () = = ClusterHealthStatus.YELLOW & & status! = ClusterHealthStatus.RED) {/ / do not override an existing red status = ClusterHealthStatus.YELLOW;}} if (! validationFailures.isEmpty ()) {status = ClusterHealthStatus.RED } else if (shards.isEmpty ()) {/ / might be since none has been created yet (two phase index creation) status = ClusterHealthStatus.RED;}}

The status of the cluster is determined based on the index.

Public ClusterStateHealth (ClusterState clusterState, String [] concreteIndices) {RoutingTableValidation validation = clusterState.routingTable (). Validate (clusterState.metaData ()); validationFailures = validation.failures (); numberOfNodes = clusterState.nodes (). Size (); numberOfDataNodes = clusterState.nodes (). DataNodes (). Size (); for (String index: concreteIndices) {IndexRoutingTable indexRoutingTable = clusterState.routingTable (). Index (index) IndexMetaData indexMetaData = clusterState.metaData () .index (index); if (indexRoutingTable = = null) {continue;} ClusterIndexHealth indexHealth = new ClusterIndexHealth (indexMetaData, indexRoutingTable); indices.put (indexHealth.getIndex (), indexHealth);} status = ClusterHealthStatus.GREEN For (ClusterIndexHealth indexHealth: indices.values ()) {activePrimaryShards + = indexHealth.getActivePrimaryShards (); activeShards + = indexHealth.getActiveShards (); relocatingShards + = indexHealth.getRelocatingShards (); initializingShards + = indexHealth.getInitializingShards (); unassignedShards + = indexHealth.getUnassignedShards (); if (indexHealth.getStatus () = = ClusterHealthStatus.RED) {status = ClusterHealthStatus.RED } else if (indexHealth.getStatus () = = ClusterHealthStatus.YELLOW & & status! = ClusterHealthStatus.RED) {status = ClusterHealthStatus.YELLOW;}} if (! validationFailures.isEmpty ()) {status = ClusterHealthStatus.RED;} else if (clusterState.blocks (). HasGlobalBlock (RestStatus.SERVICE_UNAVAILABLE)) {status = ClusterHealthStatus.RED } / / shortcut on green if (status.equals (ClusterHealthStatus.GREEN)) {this.activeShardsPercent = 100;} else {List shardRoutings = clusterState.getRoutingTable (). AllShards (); int activeShardCount = 0; int totalShardCount = 0; for (ShardRouting shardRouting: shardRoutings) {if (shardRouting.active ()) activeShardCount++ TotalShardCount++;} this.activeShardsPercent = ((double) activeShardCount) / totalShardCount) * 100;}}

To understand health api, you need to understand clusterState. Fortunately, these are read-only messages, which are not difficult to understand.

Tags: Status replica index cluster allocation core health that is representatives information fields data nodes routing all notes learning Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Apple OPPO Reno Docker Microsoft