使用健康api可以查看es集群的健康度。健康api的用法如下:
<代码> curl http://localhost: 9200/_cluster/健康的代码>
健康api的返回值中有一个核心的字段<代码>状态代码>,<代码> 代码>状态有3种取值:绿色,黄色,红色。分别代表集群的3种状态:主分片和副本都已经分配,主分片已经分配副本分片没有,主分片和副本都都没有分配。
也就是说,健康api关注的核心在于数据的高可用。
看健康api的实现:
请求会路由到主节点,然后读取clusterState中的<代码> routing_table> 代码。
基于<代码> routing_table> 代码,判断索引的各个分片状态
<代码>公共ClusterShardHealth (int shardId,最后IndexShardRoutingTable shardRoutingTable) { 这一点。shardId=shardId; (ShardRouting ShardRouting: shardRoutingTable) { 如果(shardRouting.active ()) { activeShards + +; 如果(shardRouting.relocating ()) {//碎片是迁移,迁往处于初始化状态,所以我们不计数 relocatingShards + +; } 如果(shardRouting.primary ()) { primaryActive=true; } }else if (shardRouting.initializing ()) { initializingShards + +; }else if (shardRouting.unassigned ()) { unassignedShards + +; } } 如果(primaryActive) { 如果(activeShards==shardRoutingTable.size ()) { 状态=ClusterHealthStatus.GREEN; 其他}{ 状态=ClusterHealthStatus.YELLOW; } 其他}{ 状态=ClusterHealthStatus.RED; } }代码>
基于分片,决定索引的状态
<代码>公共ClusterIndexHealth (IndexMetaData IndexMetaData, IndexRoutingTable IndexRoutingTable) { 这一点。指数=indexMetaData.getIndex (); 这一点。numberOfShards=indexMetaData.getNumberOfShards (); 这一点。numberOfReplicas=indexMetaData.getNumberOfReplicas (); 这一点。validationFailures=indexRoutingTable.validate (indexMetaData); (IndexShardRoutingTable shardRoutingTable: indexRoutingTable) { .id int shardId=shardRoutingTable.shardId () (); 碎片。把(shardId新ClusterShardHealth (shardId shardRoutingTable)); }//更新索引状态 状态=ClusterHealthStatus.GREEN; (ClusterShardHealth shardHealth: shards.values ()) { 如果(shardHealth.isPrimaryActive ()) { activePrimaryShards + +; } activeShards +=shardHealth.getActiveShards (); relocatingShards +=shardHealth.getRelocatingShards (); initializingShards +=shardHealth.getInitializingShards (); unassignedShards +=shardHealth.getUnassignedShards (); 如果(shardHealth.getStatus ()==ClusterHealthStatus.RED) { 状态=ClusterHealthStatus.RED; }else if (shardHealth.getStatus ()==ClusterHealthStatus。黄色的,,地位!=ClusterHealthStatus.RED) {//不覆盖现有的红色 状态=ClusterHealthStatus.YELLOW; } } 如果(! validationFailures.isEmpty ()) { 状态=ClusterHealthStatus.RED; }else if (shards.isEmpty()){//可能是迄今为止还没有创建以来(两个阶段创建索引) 状态=ClusterHealthStatus.RED; } }代码>
基于索引,决定集群的状态。
<代码>公共ClusterStateHealth (ClusterState ClusterState, String [] concreteIndices) { RoutingTableValidation验证=clusterState.routingTable () . validate (clusterState.metaData ()); validationFailures=validation.failures (); .size numberOfNodes=clusterState.nodes () (); .size .dataNodes numberOfDataNodes=clusterState.nodes () () (); (字符串索引:concreteIndices) { IndexRoutingTable IndexRoutingTable=clusterState.routingTable () .index(指数); IndexMetaData IndexMetaData=https://www.yisu.com/zixun/clusterState.metaData () .index(指数); 如果(indexRoutingTable==null) { 继续; } ClusterIndexHealth indexHealth=new ClusterIndexHealth (indexMetaData indexRoutingTable); indices.put (indexHealth.getIndex (), indexHealth); } 状态=ClusterHealthStatus.GREEN; (ClusterIndexHealth indexHealth: indices.values ()) { activePrimaryShards +=indexHealth.getActivePrimaryShards (); activeShards +=indexHealth.getActiveShards (); relocatingShards +=indexHealth.getRelocatingShards (); initializingShards +=indexHealth.getInitializingShards (); unassignedShards +=indexHealth.getUnassignedShards (); 如果(indexHealth.getStatus ()==ClusterHealthStatus.RED) { 状态=ClusterHealthStatus.RED; }else if (indexHealth.getStatus ()==ClusterHealthStatus。黄色& &状态!=ClusterHealthStatus.RED) { 状态=ClusterHealthStatus.YELLOW; } } 如果(! validationFailures.isEmpty ()) { 状态=ClusterHealthStatus.RED; }else if (clusterState.blocks () .hasGlobalBlock (RestStatus.SERVICE_UNAVAILABLE)) { 状态=ClusterHealthStatus.RED; }//快捷绿色 如果(status.equals (ClusterHealthStatus.GREEN)) { 这一点。activeShardsPercent=100; 其他}{ .allShards列表胚胎学习笔记之健康api的实现