protected String executeZkCommand()

in ambari-infra-solr-client/src/main/java/org/apache/ambari/infra/solr/commands/DumpCollectionsCommand.java [57:130]


  protected String executeZkCommand(AmbariSolrCloudClient client, SolrZkClient zkClient, SolrZooKeeper solrZooKeeper) throws Exception {
    Map<String, SolrCollection> collectionMap = new HashMap<>();
    if (!this.collections.isEmpty()) {
      for (String collection : this.collections) {
        SolrCollection solrCollection = new SolrCollection();
        CloudSolrClient solrClient = client.getSolrCloudClient();
        if (client.isIncludeDocNumber()) {
          long numberOfDocs = getNumberOfDocs(solrClient, collection);
          solrCollection.setNumberOfDocs(numberOfDocs);
        }
        Collection<Slice> slices = getSlices(solrClient, collection);
        Integer numShards = slices.size();
        Map<String, SolrShard> solrShardMap = new HashMap<>();
        Map<String, List<String>> leaderHostCoreMap = new HashMap<>();
        Map<String, SolrCoreData> leaderCoreDataMap = new HashMap<>();
        Map<String, List<String>> leaderShardCoreMap = new HashMap<>();
        Map<String, String> leaderCoreHostMap = new HashMap<>();
        for (Slice slice : slices) {
          SolrShard solrShard = new SolrShard();
          solrShard.setName(slice.getName());
          solrShard.setState(slice.getState());
          Collection<Replica> replicas = slice.getReplicas();
          Map<String, Replica> replicaMap = new HashMap<>();
          leaderShardCoreMap.put(slice.getName(), new ArrayList<>());
          for (Replica replica : replicas) {
            replicaMap.put(replica.getName(), replica);
            Replica.State state = replica.getState();
            if (Replica.State.ACTIVE.equals(state)
              && replica.getProperties().get("leader") != null && "true".equals(replica.getProperties().get("leader"))) {
              String coreName = replica.getCoreName();
              String hostName = getHostFromNodeName(replica.getNodeName());
              if (leaderHostCoreMap.containsKey(hostName)) {
                List<String> coresList = leaderHostCoreMap.get(hostName);
                coresList.add(coreName);
              } else {
                List<String> coreList = new ArrayList<>();
                coreList.add(coreName);
                leaderHostCoreMap.put(hostName, coreList);
              }
              Map<String, String> properties = new HashMap<>();
              properties.put("name", coreName);
              properties.put("coreNodeName", replica.getName());
              properties.put("shard", slice.getName());
              properties.put("collection", collection);
              properties.put("numShards", numShards.toString());
              properties.put("replicaType", replica.getType().name());
              SolrCoreData solrCoreData = new SolrCoreData(replica.getName(), hostName, properties);
              leaderCoreDataMap.put(coreName, solrCoreData);
              leaderShardCoreMap.get(slice.getName()).add(coreName);
              leaderCoreHostMap.put(coreName, hostName);
            }
          }
          solrShard.setReplicas(replicaMap);
          solrShardMap.put(slice.getName(), solrShard);
        }
        solrCollection.setShards(solrShardMap);
        solrCollection.setLeaderHostCoreMap(leaderHostCoreMap);
        solrCollection.setLeaderSolrCoreDataMap(leaderCoreDataMap);
        solrCollection.setLeaderShardsMap(leaderShardCoreMap);
        solrCollection.setLeaderCoreHostMap(leaderCoreHostMap);
        solrCollection.setName(collection);
        collectionMap.put(collection, solrCollection);
      }
    }
    ObjectMapper objectMapper = new ObjectMapper();
    final ObjectWriter objectWriter = objectMapper
      .writerWithDefaultPrettyPrinter();
    File file = new File(client.getOutput());
    if (!file.exists()) {
      file.createNewFile();
    }
    objectWriter.writeValue(file, collectionMap);
    return objectWriter.writeValueAsString(collectionMap);
  }