Authored by hugufei

优化ip2PrimaryShard工具接口

@@ -59,35 +59,31 @@ public class ToolsController { @@ -59,35 +59,31 @@ public class ToolsController {
59 return results; 59 return results;
60 } 60 }
61 61
62 - @RequestMapping(value = "/ip2IndexInfo") 62 + @RequestMapping(value = "/ip2PrimaryShard")
63 @ResponseBody 63 @ResponseBody
64 public Map<String, Object> nodeAndShardInfo() { 64 public Map<String, Object> nodeAndShardInfo() {
65 IElasticsearchClient client = yohoIndexService.getElasticsearchClient(ISearchConstants.INDEX_NAME_PRODUCT_INDEX); 65 IElasticsearchClient client = yohoIndexService.getElasticsearchClient(ISearchConstants.INDEX_NAME_PRODUCT_INDEX);
66 ClusterState clusterState = client.getClusterStateResponse().getState(); 66 ClusterState clusterState = client.getClusterStateResponse().getState();
67 DiscoveryNodes discoveryNodes = clusterState.nodes(); 67 DiscoveryNodes discoveryNodes = clusterState.nodes();
68 68
69 - Map<String, List<Map<String, Object>>> ip2IndexInfo = new HashMap<>(); 69 + Map<String,List<String>> ip2PrimaryShards = new HashMap<>();
70 Set<String> allIps = new HashSet<>(); 70 Set<String> allIps = new HashSet<>();
71 Set<String> hasPrimaryIps = new HashSet<>(); 71 Set<String> hasPrimaryIps = new HashSet<>();
72 for (ShardRouting shard : clusterState.routingTable().allShards()) { 72 for (ShardRouting shard : clusterState.routingTable().allShards()) {
73 String indexName = shard.getIndexName(); 73 String indexName = shard.getIndexName();
74 String hostAddress = discoveryNodes.get(shard.currentNodeId()).getHostAddress(); 74 String hostAddress = discoveryNodes.get(shard.currentNodeId()).getHostAddress();
75 boolean isPrimary = shard.primary(); 75 boolean isPrimary = shard.primary();
76 - if(isPrimary){  
77 - hasPrimaryIps.add(hostAddress);  
78 - }  
79 allIps.add(hostAddress); 76 allIps.add(hostAddress);
80 -  
81 - List<Map<String, Object>> indexInfoList = ip2IndexInfo.computeIfAbsent(hostAddress,a->new ArrayList<>());  
82 - JSONObject indexInfo = new JSONObject();  
83 - indexInfo.put("index_name", indexName);  
84 - indexInfo.put("node_id", shard.currentNodeId());  
85 - indexInfo.put("is_primary", isPrimary);  
86 - indexInfo.put("host_address", hostAddress);  
87 - indexInfoList.add(indexInfo); 77 + if(!isPrimary){
  78 + allIps.add(hostAddress);
  79 + continue;
  80 + }
  81 + hasPrimaryIps.add(hostAddress);
  82 + List<String> primaryShardList = ip2PrimaryShards.computeIfAbsent(hostAddress,a->new ArrayList<>());
  83 + primaryShardList.add(indexName);
88 } 84 }
89 Map<String, Object> result = new HashMap<>(); 85 Map<String, Object> result = new HashMap<>();
90 - result.put("all",ip2IndexInfo); 86 + result.put("ip2PrimaryShards",ip2PrimaryShards);
91 result.put("hasPrimaryIps", StringUtils.join(hasPrimaryIps,",")); 87 result.put("hasPrimaryIps", StringUtils.join(hasPrimaryIps,","));
92 allIps.removeAll(hasPrimaryIps); 88 allIps.removeAll(hasPrimaryIps);
93 result.put("noPrimaryIps", StringUtils.join(allIps,",")); 89 result.put("noPrimaryIps", StringUtils.join(allIps,","));