博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ES学习笔记之health api的实现
阅读量:7091 次
发布时间:2019-06-28

本文共 5617 字,大约阅读时间需要 18 分钟。

使用health api可以查看es集群的健康度。 health api的用法如下:

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

health api的返回值中有一个核心的字段statusstatus 有3种取值: green, yellow, red。分别代表集群的3种状态: 主分片和副本都已经分配,主分片已经分配副本分片没有,主分片和副本都都没有分配。

也就是说, health api关注的核心在于数据的高可用。

看health api的实现:

请求会路由到master节点,然后读取clusterState中的routing_table.

基于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;        }    }

基于分片,决定索引的状态

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;        }    }

基于索引,决定集群的状态。

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; } }

理解health api, 需要理解clusterState。 好在这些都是只读的信息,不难理解。

转载于:https://blog.51cto.com/sbp810050504/2384245

你可能感兴趣的文章
转: C# 根据当前时间获取,本周,本月,本季度等时间段 .Net中Exception
查看>>
Java语言基础相关问题
查看>>
spring boot集成swagger2
查看>>
写代码的三重境界
查看>>
hibernate中懒加载和急加载的区别,以及hibernate中get()方法和load()方法的区别
查看>>
最适合程序员编程的10款字体
查看>>
Hierarchyviewer定位Android图片资源的研究
查看>>
sqlserver关于时间的一些语句
查看>>
失效的URL访问限制(转)
查看>>
转:你应当如何学习C++(以及编程)(rev#1)
查看>>
加密解密算法与通讯安全(五)
查看>>
说说Java代理模式
查看>>
【原】centos系统命令部分不可用
查看>>
servlet 上传文件 参数中文乱码
查看>>
获取当前的日期时间 格式“yyyy-MM-dd HH:MM:SS”
查看>>
[STM32F429-DISCO-HAL]2.先学会点亮LED和使用LCD。。。
查看>>
POJ2349 Arctic Network(Prim)
查看>>
wp7、8丿____IValueConverter_(含参,值转换器)
查看>>
HBase 超详细介绍
查看>>
iOS 中二维码扫描
查看>>