本文将继续介绍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<IndicesSegmentResponse> 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": {
}
}
}
}
]
}
}
}
}
返回结果字段说明如下:
- _0
段的名称,表示第一个段。 - generation
在需要编写新段时基本上递增的生成数。段名是从这个生成号派生出来的。 - num_docs
存储在此段中的未删除文档的数量。 - deleted_docs
存储在此段中的已删除文档的数量。如果这个数大于0,那么当这个段合并时,空间就会被回收。 - size_in_bytes
段使用的磁盘空间量,以字节为单位。 - memory_in_bytes
段存储在内存中的字节数,如果-1表示elasticsearch无法计算。 - committed
段是否已在磁盘上同步(是否已经提交到磁盘)。 - search
是否可搜索,如果为false,表示段已提交到磁盘,但还没有被refresh,故暂时不可用来搜索。 - version
底层使用的lucene版本。 - compound
段是否存储在复合文件中。当为true时,这意味着Lucene将该段中的所有文件合并为一个文件,以便保存文件描述符。 - 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<IndicesShardStoresResponse> responseFuture = client.admin().indices().shardStores(request);
IndicesShardStoresResponse response = responseFuture.get();
ImmutableOpenMap<String, ImmutableOpenIntMap<List<IndicesShardStoresResponse.StoreStatus>>> 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<List<IndicesShardStoresResponse.StoreStatus>> 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<IndicesShardStoresResponse.StoreStatus> 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索引监控就介绍到这里了,该系列后续文章将开始关注集群相关的内容。
见文如面,我是威哥,热衷于成体系剖析JAVA主流中间件,关注公众号『中间件兴趣圈』,回复专栏可获取成体系专栏导航,回复资料可以获取笔者的学习思维导图。