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;
}
Aggregations