Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The implementation of health api of ES Learning Notes

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report