Authored by hugufei

优化ip2PrimaryShard工具接口

... ... @@ -59,35 +59,31 @@ public class ToolsController {
return results;
}
@RequestMapping(value = "/ip2IndexInfo")
@RequestMapping(value = "/ip2PrimaryShard")
@ResponseBody
public Map<String, Object> nodeAndShardInfo() {
IElasticsearchClient client = yohoIndexService.getElasticsearchClient(ISearchConstants.INDEX_NAME_PRODUCT_INDEX);
ClusterState clusterState = client.getClusterStateResponse().getState();
DiscoveryNodes discoveryNodes = clusterState.nodes();
Map<String, List<Map<String, Object>>> ip2IndexInfo = new HashMap<>();
Map<String,List<String>> ip2PrimaryShards = new HashMap<>();
Set<String> allIps = new HashSet<>();
Set<String> hasPrimaryIps = new HashSet<>();
for (ShardRouting shard : clusterState.routingTable().allShards()) {
String indexName = shard.getIndexName();
String hostAddress = discoveryNodes.get(shard.currentNodeId()).getHostAddress();
boolean isPrimary = shard.primary();
if(isPrimary){
hasPrimaryIps.add(hostAddress);
}
allIps.add(hostAddress);
List<Map<String, Object>> indexInfoList = ip2IndexInfo.computeIfAbsent(hostAddress,a->new ArrayList<>());
JSONObject indexInfo = new JSONObject();
indexInfo.put("index_name", indexName);
indexInfo.put("node_id", shard.currentNodeId());
indexInfo.put("is_primary", isPrimary);
indexInfo.put("host_address", hostAddress);
indexInfoList.add(indexInfo);
if(!isPrimary){
allIps.add(hostAddress);
continue;
}
hasPrimaryIps.add(hostAddress);
List<String> primaryShardList = ip2PrimaryShards.computeIfAbsent(hostAddress,a->new ArrayList<>());
primaryShardList.add(indexName);
}
Map<String, Object> result = new HashMap<>();
result.put("all",ip2IndexInfo);
result.put("ip2PrimaryShards",ip2PrimaryShards);
result.put("hasPrimaryIps", StringUtils.join(hasPrimaryIps,","));
allIps.removeAll(hasPrimaryIps);
result.put("noPrimaryIps", StringUtils.join(allIps,","));
... ...