本文将继续介绍elasticsearch索引监控之Indices segments与Indices Shard stores api。
1、Indices Segments
提供Lucene索引(分片级别)使用的segments(段信息)。
其对应的示例代码如下:
public static final void test_Indices_segments() {
TransportClient client = EsClient.getTransportClient();
try {
IndicesSegmentsRequest request = new IndicesSegmentsRequest();
request.indices("logs_write");
ActionFuture responseFuture = client.admin().indices().segments(request);
IndicesSegmentResponse response = responseFuture.get();
System.out.println(response);
} catch (Throwable e) {
e.printStackTrace();
} finally {
EsClient.close(client);
}
}
返回结果类似:
{
"_shards": ...
"indices": {
"test": {
"shards": {
"0": [
{
"routing": {
"state": "STARTED",
"primary": true,
"node": "zDC_RorJQCao9xf9pg3Fvw"
},
"num_committed_segments": 0,
"num_search_segments": 1,
"segments": {
"_0": {
"generation": 0,
"num_docs": 1,
"deleted_docs": 0,
"size_in_bytes": 3800,
"memory_in_bytes": 1410,
"committed": false,
"search": true,
"version": "7.0.0",
"compound": true,
"attributes": {
}
}
}
}
]
}
}
}
}
返回结果字段说明如下:
另外Indices Segments支持verbose默认,将输出一些调试信息,其返回结果如下:
{
"_0": {
"ram_tree": [
{
"description": "postings [PerFieldPostings(format=1)]",
"size_in_bytes": 2696,
"children": [
{
"description": "format 'Lucene50_0' ...",
"size_in_bytes": 2608,
"children" :[ ... ]
},
]
},
]
}
}
2、Indices Shard Stores
主要展示索引分片副本的存储信息。默认情况下,列表只存储至少有一个未分配副本的分片的信息。当集群健康状态为黄色时,将列出至少有一个未分配副本的分片的存储信息。当集群健康状态为红色时,这将列出具有未分配初选的碎片的存储信息。
对应的JAVA示例如下:
public static final void test_Indices_Shard_Stores() {
TransportClient client = EsClient.getTransportClient();
try {
IndicesShardStoresRequest request = new IndicesShardStoresRequest();
request.indices("logs_write");
ActionFuture responseFuture = client.admin().indices().shardStores(request);
IndicesShardStoresResponse response = responseFuture.get();
ImmutableOpenMap>> data = response.getStoreStatuses();
List indexList = new ArrayList();
for (Iterator it = data.keysIt(); it.hasNext(); ) {
String key = (String)it.next();
Map indexData = new HashMap();
indexList.add(indexData);
List indexShardList = new ArrayList();
indexData.put(key, indexShardList);
ImmutableOpenIntMap> value = data.get(key);
for(Iterator it2 = value.keysIt(); it2.hasNext(); ) {
Integer key2 = (Integer)it2.next();
Map shardData = new HashMap();
indexShardList.add(shardData);
List shardStoreStatusList = new ArrayList();
shardData.put(key2 + "", shardStoreStatusList);
List storeStatusList = value.get(key2);
for(IndicesShardStoresResponse.StoreStatus storeStatus : storeStatusList) {
Map storeStatusMap = new HashMap();
shardStoreStatusList.add(storeStatusMap);
storeStatusMap.put("allocationId", storeStatus.getAllocationId());
storeStatusMap.put("allocationStatus", storeStatus.getAllocationStatus().value());
Map discoveryNodeData = new HashMap();
storeStatusMap.put("discoveryNode", discoveryNodeData);
DiscoveryNode node = storeStatus.getNode();
discoveryNodeData.put("name", node.getName());
discoveryNodeData.put("name", node.getAddress());
discoveryNodeData.put("attributes", node.getAttributes());
discoveryNodeData.put("ephemeralId", node.getEphemeralId());
discoveryNodeData.put("hostAddress", node.getHostAddress());
discoveryNodeData.put("hostName", node.getHostName());
discoveryNodeData.put("id", node.getId());
discoveryNodeData.put("roles", node.getRoles());
}
}
}
System.out.println(FastJsonUtils.getBeanToJson(indexList));
} catch (Throwable e) {
e.printStackTrace();
} finally {
EsClient.close(client);
}
}
返回的结果为:
[
{
"logs-000002":[
{
0:[ // @1
{
"discoveryNode":{ // @2
"hostName":"127.0.0.1",
"roles":[
"MASTER",
"DATA",
"INGEST"
],
"name":{
"address":"127.0.0.1",
"fragment":true,
"port":9300
},
"attributes":{
"ml.machine_memory":"16964890624",
"ml.max_open_jobs":"20",
"xpack.installed":"true",
"ml.enabled":"true"
},
"hostAddress":"127.0.0.1",
"id":"ekEDWaVVRH-944BgEsfRLA",
"ephemeralId":"ox0CP9hhQOu1klZgNv7Ezw"
},
"allocationId":"KRw3BYPFTrK39HOYXzwXBA", // @3
"allocationStatus":"primary" // @4
}
]
}
//由于当前试验环境为单机模式,故省略其他分片信息
]
}
]
代码@1:分片编号
代码@2:分片所在的节点的信息,包含名称、角色、id、地址等信息。
代码@3:副本的分配ID。
代码@4:分配的状态,其值为primary、replica、unused。
ElasticSearch索引监控就介绍到这里了,该系列后续文章将开始关注集群相关的内容。
欢迎加笔者微信号(dingwpmz),加群探讨,笔者优质专栏目录:
1、源码分析RocketMQ专栏(40篇+)
2、源码分析Sentinel专栏(12篇+)
3、源码分析Dubbo专栏(28篇+)
4、源码分析Mybatis专栏
5、源码分析Netty专栏(18篇+)
6、源码分析JUC专栏
7、源码分析Elasticjob专栏
8、Elasticsearch专栏(20篇+)
9、源码分析MyCat专栏