public Map getQuota()

in hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/StoreNodeService.java [950:1038]


    public Map getQuota() throws PDException {
        List<Metapb.Graph> graphs = partitionService.getGraphs();
        String delimiter = String.valueOf(MetadataKeyHelper.DELIMITER);
        HashMap<String, Long> storages = new HashMap<>();
        for (Metapb.Graph g : graphs) {
            String graphName = g.getGraphName();
            String[] splits = graphName.split(delimiter);
            if (!graphName.endsWith("/g") || splits.length < 2) {
                continue;
            }
            String graphSpace = splits[0];
            storages.putIfAbsent(graphSpace, 0L);
            List<Metapb.Store> stores = getStores(graphName);
            long dataSize = 0;
            for (Metapb.Store store : stores) {
                List<Metapb.GraphStats> gss = store.getStats()
                                                   .getGraphStatsList();
                for (Metapb.GraphStats gs : gss) {
                    boolean nameEqual = graphName.equals(gs.getGraphName());
                    boolean roleEqual = Metapb.ShardRole.Leader.equals(
                            gs.getRole());
                    if (nameEqual && roleEqual) {
                        dataSize += gs.getApproximateSize();
                    }
                }
            }
            Long size = storages.get(graphSpace);
            size += dataSize;
            storages.put(graphSpace, size);

        }
        Metapb.GraphSpace.Builder spaceBuilder = Metapb.GraphSpace.newBuilder();
        HashMap<String, Boolean> limits = new HashMap<>();
        for (Map.Entry<String, Long> item : storages.entrySet()) {
            String spaceName = item.getKey();
            String value = kvService.get(graphSpaceConfPrefix + spaceName);
            if (!StringUtils.isEmpty(value)) {
                HashMap config = new Gson().fromJson(value, HashMap.class);
                Long size = item.getValue();
                int limit = ((Double) config.get("storage_limit")).intValue();
                long limitByLong = limit * 1024L * 1024L;
                try {
                    spaceBuilder.setName(spaceName).setStorageLimit(limitByLong).setUsedSize(size);
                    Metapb.GraphSpace graphSpace = spaceBuilder.build();
                    configService.setGraphSpace(graphSpace);
                } catch (Exception e) {
                    log.error("update graph space with error:", e);
                }
                // KB and GB * 1024L * 1024L
                if (size > limitByLong) {
                    limits.put(spaceName, true);
                    continue;
                }
            }
            limits.put(spaceName, false);

        }
        GraphState.Builder stateBuilder = GraphState.newBuilder()
                                                    .setMode(GraphMode.ReadOnly)
                                                    .setReason(
                                                            GraphModeReason.Quota);
        for (Metapb.Graph g : graphs) {
            String graphName = g.getGraphName();
            String[] splits = graphName.split(delimiter);
            if (!graphName.endsWith("/g") || splits.length < 2) {
                continue;
            }
            String graphSpace = splits[0];
            Metapb.GraphState gsOld = g.getGraphState();
            GraphMode gmOld = gsOld != null ? gsOld.getMode() : GraphMode.ReadWrite;
            GraphMode gmNew = limits.get(
                    graphSpace) ? GraphMode.ReadOnly : GraphMode.ReadWrite;
            if (gmOld == null || gmOld.getNumber() != gmNew.getNumber()) {
                stateBuilder.setMode(gmNew);
                if (gmNew.getNumber() == GraphMode.ReadOnly.getNumber()) {
                    stateBuilder.setReason(GraphModeReason.Quota);
                }
                GraphState gsNew = stateBuilder.build();
                Metapb.Graph newGraph = g.toBuilder().setGraphState(gsNew)
                                         .build();
                partitionService.updateGraph(newGraph);
                statusListeners.forEach(listener -> {
                    listener.onGraphChange(newGraph, gsOld, gsNew);
                });
            }
        }

        return limits;
    }