Search in sources :

Example 6 with RegionStats

use of com.alipay.sofa.jraft.rhea.metadata.RegionStats in project sofa-jraft by sofastack.

the class RegionLeaderBalanceHandler method readMessage.

@Override
public void readMessage(final HandlerContext ctx, final RegionPingEvent event) throws Exception {
    if (event.isReady()) {
        return;
    }
    final MetadataStore metadataStore = event.getMetadataStore();
    final RegionHeartbeatRequest request = event.getMessage();
    final long clusterId = request.getClusterId();
    final long storeId = request.getStoreId();
    final ClusterStatsManager clusterStatsManager = ClusterStatsManager.getInstance(clusterId);
    final List<Pair<Region, RegionStats>> regionStatsList = request.getRegionStatsList();
    for (final Pair<Region, RegionStats> stats : regionStatsList) {
        final Region region = stats.getKey();
        clusterStatsManager.addOrUpdateLeader(storeId, region.getId());
    }
    // check if the modelWorker
    final Pair<Set<Long>, Integer> modelWorkers = clusterStatsManager.findModelWorkerStores(1);
    final Set<Long> modelWorkerStoreIds = modelWorkers.getKey();
    final int modelWorkerLeaders = modelWorkers.getValue();
    if (!modelWorkerStoreIds.contains(storeId)) {
        return;
    }
    LOG.info("[Cluster: {}] model worker stores is: {}, it has {} leaders.", clusterId, modelWorkerStoreIds, modelWorkerLeaders);
    for (final Pair<Region, RegionStats> pair : regionStatsList) {
        final Region region = pair.getKey();
        final List<Peer> peers = region.getPeers();
        if (peers == null) {
            continue;
        }
        final List<Endpoint> endpoints = Lists.transform(peers, Peer::getEndpoint);
        final Map<Long, Endpoint> storeIds = metadataStore.unsafeGetStoreIdsByEndpoints(clusterId, endpoints);
        // find lazyWorkers
        final List<Pair<Long, Integer>> lazyWorkers = clusterStatsManager.findLazyWorkerStores(storeIds.keySet());
        if (lazyWorkers.isEmpty()) {
            return;
        }
        for (int i = lazyWorkers.size() - 1; i >= 0; i--) {
            final Pair<Long, Integer> worker = lazyWorkers.get(i);
            if (modelWorkerLeaders - worker.getValue() <= 1) {
                // no need to transfer
                lazyWorkers.remove(i);
            }
        }
        if (lazyWorkers.isEmpty()) {
            continue;
        }
        final Pair<Long, Integer> laziestWorker = tryToFindLaziestWorker(clusterId, metadataStore, lazyWorkers);
        if (laziestWorker == null) {
            continue;
        }
        final Long 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);
        event.addInstruction(instruction);
        LOG.info("[Cluster: {}], send 'instruction.transferLeader': {} to region: {}.", clusterId, instruction, region);
        // Only do one thing at a time
        break;
    }
}
Also used : ClusterStatsManager(com.alipay.sofa.jraft.rhea.ClusterStatsManager) Set(java.util.Set) RegionStats(com.alipay.sofa.jraft.rhea.metadata.RegionStats) Instruction(com.alipay.sofa.jraft.rhea.metadata.Instruction) RegionHeartbeatRequest(com.alipay.sofa.jraft.rhea.cmd.pd.RegionHeartbeatRequest) Endpoint(com.alipay.sofa.jraft.util.Endpoint) Pair(com.alipay.sofa.jraft.rhea.util.Pair) Peer(com.alipay.sofa.jraft.rhea.metadata.Peer) Endpoint(com.alipay.sofa.jraft.util.Endpoint) MetadataStore(com.alipay.sofa.jraft.rhea.MetadataStore) Region(com.alipay.sofa.jraft.rhea.metadata.Region)

Example 7 with RegionStats

use of com.alipay.sofa.jraft.rhea.metadata.RegionStats in project sofa-jraft by sofastack.

the class SplittingJudgeByApproximateKeysHandler method isSplitNeeded.

private boolean isSplitNeeded(final RegionHeartbeatRequest request, final Pair<Region, RegionStats> modelWorker) {
    if (modelWorker == null) {
        return false;
    }
    final long modelApproximateKeys = modelWorker.getValue().getApproximateKeys();
    if (request.getLeastKeysOnSplit() > modelApproximateKeys) {
        return false;
    }
    final Region modelRegion = modelWorker.getKey();
    final List<Pair<Region, RegionStats>> regionStatsList = request.getRegionStatsList();
    for (final Pair<Region, RegionStats> p : regionStatsList) {
        if (modelRegion.equals(p.getKey())) {
            return true;
        }
    }
    return false;
}
Also used : RegionStats(com.alipay.sofa.jraft.rhea.metadata.RegionStats) Region(com.alipay.sofa.jraft.rhea.metadata.Region) Pair(com.alipay.sofa.jraft.rhea.util.Pair)

Aggregations

RegionStats (com.alipay.sofa.jraft.rhea.metadata.RegionStats)7 Region (com.alipay.sofa.jraft.rhea.metadata.Region)6 RegionHeartbeatRequest (com.alipay.sofa.jraft.rhea.cmd.pd.RegionHeartbeatRequest)4 Pair (com.alipay.sofa.jraft.rhea.util.Pair)4 MetadataStore (com.alipay.sofa.jraft.rhea.MetadataStore)3 Instruction (com.alipay.sofa.jraft.rhea.metadata.Instruction)3 ClusterStatsManager (com.alipay.sofa.jraft.rhea.ClusterStatsManager)2 Peer (com.alipay.sofa.jraft.rhea.metadata.Peer)2 TimeInterval (com.alipay.sofa.jraft.rhea.metadata.TimeInterval)2 Endpoint (com.alipay.sofa.jraft.util.Endpoint)2 Status (com.alipay.sofa.jraft.Status)1 RegionEpoch (com.alipay.sofa.jraft.rhea.metadata.RegionEpoch)1 KVEntry (com.alipay.sofa.jraft.rhea.storage.KVEntry)1 List (java.util.List)1 Set (java.util.Set)1