public RestApiResponse getHighLevelPartitions()

in hugegraph-pd/hg-pd-service/src/main/java/org/apache/hugegraph/pd/rest/PartitionAPI.java [60:180]


    public RestApiResponse getHighLevelPartitions() {
        // Information about multiple graphs under the partition
        Map<Integer, Map<String, GraphStats>> partitions2GraphsMap = new HashMap<>();
        Map<Integer, HighLevelPartition> resultPartitionsMap = new HashMap<>();
        // The keyCount of each partition is only taken from the leader
        Map<Integer, Long> partition2KeyCount = new HashMap<>();
        // The dataSize of each partition is only taken from the leader
        Map<Integer, Long> partition2DataSize = new HashMap<>();
        List<Metapb.Store> stores;
        Map<Long, Metapb.Store> storesMap = new HashMap<>();
        try {
            stores = pdRestService.getStores("");
        } catch (PDException e) {
            log.error("getStores error", e);
            return new RestApiResponse(null, e.getErrorCode(), e.getMessage());
        }
        for (Metapb.Store store : stores) {
            storesMap.put(store.getId(), store);
            List<Metapb.GraphStats> graphStatsList = store.getStats().getGraphStatsList();
            for (Metapb.GraphStats graphStats : graphStatsList) {
                // Obtaining Graph Information Saved by a Partition (Only from the Leader)
                if (Metapb.ShardRole.Leader != graphStats.getRole()) {
                    continue;
                }
                // Calculating the key count of partitions (indiscriminate graphs)
                partition2KeyCount.put(graphStats.getPartitionId(),
                                       partition2KeyCount.getOrDefault(graphStats.getPartitionId(),
                                                                       graphStats.getApproximateKeys()));
                // The dataSize of the partition is calculated by adding the size of the graph
                partition2DataSize.put(graphStats.getPartitionId(),
                                       partition2DataSize.getOrDefault(graphStats.getPartitionId(),
                                                                       0L)
                                       + graphStats.getApproximateSize());
                if (partitions2GraphsMap.get(graphStats.getPartitionId()) == null) {
                    partitions2GraphsMap.put(graphStats.getPartitionId(),
                                             new HashMap<String, GraphStats>());
                }
                Map<String, GraphStats> partitionGraphsMap =
                        partitions2GraphsMap.get(graphStats.getPartitionId());
                partitionGraphsMap.put(graphStats.getGraphName(), new GraphStats(graphStats));
            }
        }
        // Construct all the information that needs to be returned for the partition
        List<Metapb.Partition> partitionList = pdRestService.getPartitions("");
        for (Metapb.Partition partition : partitionList) {
            // Supplement the startKey and endKey of the partition image
            if (partitions2GraphsMap.get(partition.getId()) != null) {
                GraphStats graphStats =
                        partitions2GraphsMap.get(partition.getId()).get(partition.getGraphName());
                if (graphStats != null) {
                    graphStats.startKey = partition.getStartKey();
                    graphStats.endKey = partition.getEndKey();
                }
            }
            // Construct the overall information of the partition (regardless of the diagram)
            if ((resultPartitionsMap.get(partition.getId()) == null)
                && (!partition.getGraphName().endsWith("/s"))
            ) {
                Metapb.PartitionStats partitionStats;
                try {
                    partitionStats = pdRestService.getPartitionStats(partition.getGraphName(),
                                                                     partition.getId());
                } catch (PDException e) {
                    log.error("getPartitionStats error", e);
                    partitionStats = null;
                }
                // Initialize the partition information
                HighLevelPartition resultPartition =
                        new HighLevelPartition(partition, partitionStats);
                resultPartition.keyCount =
                        partition2KeyCount.getOrDefault(resultPartition.partitionId, 0L);
                resultPartition.dataSize =
                        partition2DataSize.getOrDefault(resultPartition.partitionId, 0L);
                for (ShardStats shard : resultPartition.shards) {
                    // Assign values to the address and partition information of the replica
                    shard.address = storesMap.get(shard.storeId).getAddress();
                    shard.partitionId = partition.getId();
                }
                if ((partitionStats != null) && (partitionStats.getLeader() != null)) {
                    long storeId = partitionStats.getLeader().getStoreId();
                    resultPartition.leaderAddress =
                            storesMap.get(storeId).getAddress();
                }
                resultPartitionsMap.put(partition.getId(), resultPartition);
            }
        }
        // Construct a list of graphs under the partitions to be returned, return only /g, and
        // sort by name
        for (Map.Entry<Integer, HighLevelPartition> entry : resultPartitionsMap.entrySet()) {
            Integer partitionId = entry.getKey();
            HighLevelPartition currentPartition = resultPartitionsMap.get(partitionId);
            Map<String, GraphStats> graphsMap = partitions2GraphsMap
                    .getOrDefault(partitionId,
                                  new HashMap<>()); // Avoid null pointer exceptions at the back
            ArrayList<GraphStats> graphsList = new ArrayList<>();
            for (Map.Entry<String, GraphStats> entry1 : graphsMap.entrySet()) {
                if (!entry1.getKey().endsWith("/g")) {
                    continue; // Only the graph of /g is kept
                }
                String graphName = entry1.getKey();
                GraphStats tmpGraph = graphsMap.get(graphName);
                final int postfixLength = 2;
                tmpGraph.graphName = tmpGraph.graphName.substring(0, tmpGraph.graphName.length() -
                                                                     postfixLength);
                graphsList.add(tmpGraph);
            }
            graphsList.sort((o1, o2) -> o1.graphName.compareTo(o2.graphName));
            currentPartition.graphs = graphsList;
        }
        List<HighLevelPartition> resultPartitionList = new ArrayList<>();
        if (!resultPartitionsMap.isEmpty()) {
            ArrayList<Integer> partitionids = new ArrayList(resultPartitionsMap.keySet());
            partitionids.sort((o1, o2) -> o1.intValue() - o2.intValue());
            for (Integer partitionId : partitionids) {
                resultPartitionList.add(resultPartitionsMap.get(partitionId));
            }
        }
        HashMap<String, Object> dataMap = new HashMap<>();
        dataMap.put("partitions", resultPartitionList);
        return new RestApiResponse(dataMap, Pdpb.ErrorType.OK, Pdpb.ErrorType.OK.name());
    }