Search in sources :

Example 1 with MetadataStore

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

the class RegionStatsValidator method readMessage.

@Override
public void readMessage(final HandlerContext ctx, final RegionPingEvent event) throws Exception {
    final MetadataStore metadataStore = event.getMetadataStore();
    final RegionHeartbeatRequest request = event.getMessage();
    final List<Pair<Region, RegionStats>> regionStatsList = request.getRegionStatsList();
    if (regionStatsList == null || regionStatsList.isEmpty()) {
        LOG.error("Empty [RegionStatsList] by event: {}.", event);
        throw Errors.INVALID_REGION_STATS.exception();
    }
    for (final Pair<Region, RegionStats> pair : regionStatsList) {
        final Region region = pair.getKey();
        if (region == null) {
            LOG.error("Empty [Region] by event: {}.", event);
            throw Errors.INVALID_REGION_STATS.exception();
        }
        final RegionEpoch regionEpoch = region.getRegionEpoch();
        if (regionEpoch == null) {
            LOG.error("Empty [RegionEpoch] by event: {}.", event);
            throw Errors.INVALID_REGION_STATS.exception();
        }
        final RegionStats regionStats = pair.getValue();
        if (regionStats == null) {
            LOG.error("Empty [RegionStats] by event: {}.", event);
            throw Errors.INVALID_REGION_STATS.exception();
        }
        final Pair<Region, RegionStats> currentRegionInfo = metadataStore.getRegionStats(request.getClusterId(), region);
        if (currentRegionInfo == null) {
            // new data
            return;
        }
        final Region currentRegion = currentRegionInfo.getKey();
        if (regionEpoch.compareTo(currentRegion.getRegionEpoch()) < 0) {
            LOG.error("The region epoch is out of date: {}.", event);
            throw Errors.REGION_HEARTBEAT_OUT_OF_DATE.exception();
        }
        final TimeInterval interval = regionStats.getInterval();
        if (interval == null) {
            LOG.error("Empty [TimeInterval] by event: {}.", event);
            throw Errors.INVALID_REGION_STATS.exception();
        }
        final TimeInterval currentInterval = currentRegionInfo.getValue().getInterval();
        if (interval.getEndTimestamp() < currentInterval.getEndTimestamp()) {
            LOG.error("The [TimeInterval] is out of date: {}.", event);
            throw Errors.REGION_HEARTBEAT_OUT_OF_DATE.exception();
        }
    }
}
Also used : MetadataStore(com.alipay.sofa.jraft.rhea.MetadataStore) RegionStats(com.alipay.sofa.jraft.rhea.metadata.RegionStats) TimeInterval(com.alipay.sofa.jraft.rhea.metadata.TimeInterval) Region(com.alipay.sofa.jraft.rhea.metadata.Region) RegionEpoch(com.alipay.sofa.jraft.rhea.metadata.RegionEpoch) RegionHeartbeatRequest(com.alipay.sofa.jraft.rhea.cmd.pd.RegionHeartbeatRequest) Pair(com.alipay.sofa.jraft.rhea.util.Pair)

Example 2 with MetadataStore

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

the class SplittingJudgeByApproximateKeysHandler 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 ClusterStatsManager clusterStatsManager = ClusterStatsManager.getInstance(clusterId);
    clusterStatsManager.addOrUpdateRegionStats(request.getRegionStatsList());
    final Set<Long> stores = metadataStore.unsafeGetStoreIds(clusterId);
    if (stores == null || stores.isEmpty()) {
        return;
    }
    if (clusterStatsManager.regionSize() >= stores.size()) {
        // one store one region is perfect
        return;
    }
    final Pair<Region, RegionStats> modelWorker = clusterStatsManager.findModelWorkerRegion();
    if (!isSplitNeeded(request, modelWorker)) {
        return;
    }
    LOG.info("[Cluster: {}] model worker region is: {}.", clusterId, modelWorker);
    final Long newRegionId = metadataStore.createRegionId(clusterId);
    final Instruction.RangeSplit rangeSplit = new Instruction.RangeSplit();
    rangeSplit.setNewRegionId(newRegionId);
    final Instruction instruction = new Instruction();
    instruction.setRegion(modelWorker.getKey().copy());
    instruction.setRangeSplit(rangeSplit);
    event.addInstruction(instruction);
}
Also used : MetadataStore(com.alipay.sofa.jraft.rhea.MetadataStore) ClusterStatsManager(com.alipay.sofa.jraft.rhea.ClusterStatsManager) RegionStats(com.alipay.sofa.jraft.rhea.metadata.RegionStats) Region(com.alipay.sofa.jraft.rhea.metadata.Region) Instruction(com.alipay.sofa.jraft.rhea.metadata.Instruction) RegionHeartbeatRequest(com.alipay.sofa.jraft.rhea.cmd.pd.RegionHeartbeatRequest)

Example 3 with MetadataStore

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

the class StoreStatsPersistenceHandler method readMessage.

@Override
public void readMessage(final HandlerContext ctx, final StorePingEvent event) throws Exception {
    final MetadataStore metadataStore = event.getMetadataStore();
    final StoreHeartbeatRequest request = event.getMessage();
    // sync
    metadataStore.updateStoreStats(request.getClusterId(), request.getStats()).get();
}
Also used : MetadataStore(com.alipay.sofa.jraft.rhea.MetadataStore) StoreHeartbeatRequest(com.alipay.sofa.jraft.rhea.cmd.pd.StoreHeartbeatRequest)

Example 4 with MetadataStore

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

the class StoreStatsValidator method readMessage.

@Override
public void readMessage(final HandlerContext ctx, final StorePingEvent event) throws Exception {
    final MetadataStore metadataStore = event.getMetadataStore();
    final StoreHeartbeatRequest request = event.getMessage();
    final StoreStats storeStats = request.getStats();
    if (storeStats == null) {
        LOG.error("Empty [StoreStats] by event: {}.", event);
        throw Errors.INVALID_STORE_STATS.exception();
    }
    final StoreStats currentStoreStats = metadataStore.getStoreStats(request.getClusterId(), storeStats.getStoreId());
    if (currentStoreStats == null) {
        // new data
        return;
    }
    final TimeInterval interval = storeStats.getInterval();
    if (interval == null) {
        LOG.error("Empty [TimeInterval] by event: {}.", event);
        throw Errors.INVALID_STORE_STATS.exception();
    }
    final TimeInterval currentInterval = currentStoreStats.getInterval();
    if (interval.getEndTimestamp() < currentInterval.getEndTimestamp()) {
        LOG.error("The [TimeInterval] is out of date: {}.", event);
        throw Errors.STORE_HEARTBEAT_OUT_OF_DATE.exception();
    }
}
Also used : MetadataStore(com.alipay.sofa.jraft.rhea.MetadataStore) StoreHeartbeatRequest(com.alipay.sofa.jraft.rhea.cmd.pd.StoreHeartbeatRequest) StoreStats(com.alipay.sofa.jraft.rhea.metadata.StoreStats) TimeInterval(com.alipay.sofa.jraft.rhea.metadata.TimeInterval)

Example 5 with MetadataStore

use of com.alipay.sofa.jraft.rhea.MetadataStore 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)

Aggregations

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