Search in sources :

Example 1 with ClusterStatsManager

use of io.dingodb.server.coordinator.meta.ClusterStatsManager in project dingo by dingodb.

the class RegionHeartbeatHandler method balance.

private Instruction balance(Region region, RegionStats regionStats) {
    final long clusterId = 0;
    final String storeId = regionStats.getLeader().getStoreId();
    final ClusterStatsManager clusterStatsManager = ClusterStatsManager.getInstance(clusterId);
    clusterStatsManager.addOrUpdateLeader(storeId, region.getId());
    // check if the modelWorker
    final Pair<Set<String>, Integer> modelWorkers = clusterStatsManager.findModelWorkerStores(1);
    final Set<String> modelWorkerStoreIds = modelWorkers.getKey();
    final int modelWorkerLeaders = modelWorkers.getValue();
    if (!modelWorkerStoreIds.contains(storeId)) {
        return null;
    }
    log.info("[Cluster] model worker stores is: {}, it has {} leaders.", modelWorkerStoreIds, modelWorkerLeaders);
    final List<Peer> peers = region.getPeers();
    if (peers == null) {
        return null;
    }
    final List<Endpoint> endpoints = Lists.transform(peers, Peer::getEndpoint);
    final Map<String, Endpoint> storeIds = rowStoreMetaAdaptor.storeLocation();
    // find lazyWorkers
    final List<Pair<String, Integer>> lazyWorkers = clusterStatsManager.findLazyWorkerStores(storeIds.keySet());
    if (lazyWorkers.isEmpty()) {
        return null;
    }
    for (int i = lazyWorkers.size() - 1; i >= 0; i--) {
        final Pair<String, Integer> worker = lazyWorkers.get(i);
        if (modelWorkerLeaders - worker.getValue() <= 1) {
            // no need to transfer
            lazyWorkers.remove(i);
        }
    }
    if (lazyWorkers.isEmpty()) {
        return null;
    }
    final Pair<String, Integer> laziestWorker = tryToFindLaziestWorker(lazyWorkers);
    if (laziestWorker == null) {
        return null;
    }
    final String lazyWorkerStoreId = laziestWorker.getKey();
    log.info("[Cluster: {}], lazy worker store is: {}, it has {} leaders.", clusterId, lazyWorkerStoreId, laziestWorker.getValue());
    final Instruction.TransferLeader transferLeader = new Instruction.TransferLeader();
    transferLeader.setMoveToStoreId(lazyWorkerStoreId);
    transferLeader.setMoveToEndpoint(storeIds.get(lazyWorkerStoreId));
    final Instruction instruction = new Instruction();
    instruction.setRegion(region.copy());
    instruction.setTransferLeader(transferLeader);
    log.info("[Cluster: {}], send 'instruction.transferLeader': {} to region: {}.", clusterId, instruction, region);
    return instruction;
}
Also used : ClusterStatsManager(io.dingodb.server.coordinator.meta.ClusterStatsManager) Set(java.util.Set) Peer(io.dingodb.store.row.metadata.Peer) Instruction(io.dingodb.store.row.metadata.Instruction) Endpoint(io.dingodb.raft.util.Endpoint) Endpoint(io.dingodb.raft.util.Endpoint) Pair(io.dingodb.store.row.util.Pair)

Aggregations

Endpoint (io.dingodb.raft.util.Endpoint)1 ClusterStatsManager (io.dingodb.server.coordinator.meta.ClusterStatsManager)1 Instruction (io.dingodb.store.row.metadata.Instruction)1 Peer (io.dingodb.store.row.metadata.Peer)1 Pair (io.dingodb.store.row.util.Pair)1 Set (java.util.Set)1