Search in sources :

Example 1 with Pair

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

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

the class MemoryRawKVStore method doSnapshotSave.

void doSnapshotSave(final MemoryKVStoreSnapshotFile snapshotFile, final String snapshotPath, final Region region) throws Exception {
    final Timer.Context timeCtx = getTimeContext("SNAPSHOT_SAVE");
    try {
        final String tempPath = snapshotPath + "_temp";
        final File tempFile = new File(tempPath);
        FileUtils.deleteDirectory(tempFile);
        FileUtils.forceMkdir(tempFile);
        snapshotFile.writeToFile(tempPath, SEQUENCE_DB, new SequenceDB(subRangeMap(this.sequenceDB, region)));
        snapshotFile.writeToFile(tempPath, FENCING_KEY_DB, new FencingKeyDB(subRangeMap(this.fencingKeyDB, region)));
        snapshotFile.writeToFile(tempPath, LOCKER_DB, new LockerDB(subRangeMap(this.lockerDB, region)));
        final int size = this.opts.getKeysPerSegment();
        final List<Pair<byte[], byte[]>> segment = Lists.newArrayListWithCapacity(size);
        int index = 0;
        final byte[] realStartKey = BytesUtil.nullToEmpty(region.getStartKey());
        final byte[] endKey = region.getEndKey();
        final NavigableMap<byte[], byte[]> subMap;
        if (endKey == null) {
            subMap = this.defaultDB.tailMap(realStartKey);
        } else {
            subMap = this.defaultDB.subMap(realStartKey, endKey);
        }
        for (final Map.Entry<byte[], byte[]> entry : subMap.entrySet()) {
            segment.add(Pair.of(entry.getKey(), entry.getValue()));
            if (segment.size() >= size) {
                snapshotFile.writeToFile(tempPath, SEGMENT + index++, new Segment(segment));
                segment.clear();
            }
        }
        if (!segment.isEmpty()) {
            snapshotFile.writeToFile(tempPath, SEGMENT + index++, new Segment(segment));
            segment.clear();
        }
        snapshotFile.writeToFile(tempPath, TAIL_INDEX, new TailIndex(--index));
        final File destinationPath = new File(snapshotPath);
        FileUtils.deleteDirectory(destinationPath);
        FileUtils.moveDirectory(tempFile, destinationPath);
    } finally {
        timeCtx.stop();
    }
}
Also used : FencingKeyDB(com.alipay.sofa.jraft.rhea.storage.MemoryKVStoreSnapshotFile.FencingKeyDB) LockerDB(com.alipay.sofa.jraft.rhea.storage.MemoryKVStoreSnapshotFile.LockerDB) Segment(com.alipay.sofa.jraft.rhea.storage.MemoryKVStoreSnapshotFile.Segment) Timer(com.codahale.metrics.Timer) TailIndex(com.alipay.sofa.jraft.rhea.storage.MemoryKVStoreSnapshotFile.TailIndex) SequenceDB(com.alipay.sofa.jraft.rhea.storage.MemoryKVStoreSnapshotFile.SequenceDB) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentNavigableMap(java.util.concurrent.ConcurrentNavigableMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) NavigableMap(java.util.NavigableMap) ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) Pair(com.alipay.sofa.jraft.rhea.util.Pair)

Example 3 with Pair

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

the class HeartbeatSender method sendRegionHeartbeat.

private void sendRegionHeartbeat(final long nextDelay, final long lastTime, final boolean forceRefreshLeader) {
    final long now = System.currentTimeMillis();
    final RegionHeartbeatRequest request = new RegionHeartbeatRequest();
    request.setClusterId(this.storeEngine.getClusterId());
    request.setStoreId(this.storeEngine.getStoreId());
    request.setLeastKeysOnSplit(this.storeEngine.getStoreOpts().getLeastKeysOnSplit());
    final List<Long> regionIdList = this.storeEngine.getLeaderRegionIds();
    if (regionIdList.isEmpty()) {
        // So sad, there is no even a region leader :(
        final RegionHeartbeatTask nextTask = new RegionHeartbeatTask(nextDelay, now, false);
        this.heartbeatTimer.newTimeout(nextTask, nextTask.getNextDelay(), TimeUnit.SECONDS);
        if (LOG.isInfoEnabled()) {
            LOG.info("So sad, there is no even a region leader on [clusterId:{}, storeId: {}, endpoint:{}].", this.storeEngine.getClusterId(), this.storeEngine.getStoreId(), this.storeEngine.getSelfEndpoint());
        }
        return;
    }
    final List<Pair<Region, RegionStats>> regionStatsList = Lists.newArrayListWithCapacity(regionIdList.size());
    final TimeInterval timeInterval = new TimeInterval(lastTime, now);
    for (final Long regionId : regionIdList) {
        final Region region = this.pdClient.getRegionById(regionId);
        final RegionStats stats = this.statsCollector.collectRegionStats(region, timeInterval);
        if (stats == null) {
            continue;
        }
        regionStatsList.add(Pair.of(region, stats));
    }
    request.setRegionStatsList(regionStatsList);
    final HeartbeatClosure<List<Instruction>> closure = new HeartbeatClosure<List<Instruction>>() {

        @Override
        public void run(final Status status) {
            final boolean isOk = status.isOk();
            if (isOk) {
                final List<Instruction> instructions = getResult();
                if (instructions != null && !instructions.isEmpty()) {
                    instructionProcessor.process(instructions);
                }
            }
            final boolean forceRefresh = !isOk && ErrorsHelper.isInvalidPeer(getError());
            final RegionHeartbeatTask nextTask = new RegionHeartbeatTask(nextDelay, now, forceRefresh);
            heartbeatTimer.newTimeout(nextTask, nextTask.getNextDelay(), TimeUnit.SECONDS);
        }
    };
    final Endpoint endpoint = this.pdClient.getPdLeader(forceRefreshLeader, this.heartbeatRpcTimeoutMillis);
    callAsyncWithRpc(endpoint, request, closure);
}
Also used : Status(com.alipay.sofa.jraft.Status) TimeInterval(com.alipay.sofa.jraft.rhea.metadata.TimeInterval) 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) Region(com.alipay.sofa.jraft.rhea.metadata.Region) List(java.util.List) Pair(com.alipay.sofa.jraft.rhea.util.Pair)

Example 4 with Pair

use of com.alipay.sofa.jraft.rhea.util.Pair 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 5 with Pair

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

the class RegionLeaderBalanceHandler method tryToFindLaziestWorker.

private Pair<Long, Integer> tryToFindLaziestWorker(final long clusterId, final MetadataStore metadataStore, final List<Pair<Long, Integer>> lazyWorkers) {
    final List<Pair<Pair<Long, Integer>, StoreStats>> storeStatsList = Lists.newArrayList();
    for (final Pair<Long, Integer> worker : lazyWorkers) {
        final StoreStats stats = metadataStore.getStoreStats(clusterId, worker.getKey());
        if (stats != null) {
            // TODO check timeInterval
            storeStatsList.add(Pair.of(worker, stats));
        }
    }
    if (storeStatsList.isEmpty()) {
        return null;
    }
    if (storeStatsList.size() == 1) {
        return storeStatsList.get(0).getKey();
    }
    final Pair<Pair<Long, Integer>, StoreStats> min = Collections.min(storeStatsList, (o1, o2) -> {
        final StoreStats s1 = o1.getValue();
        final StoreStats s2 = o2.getValue();
        int val = Boolean.compare(s1.isBusy(), s2.isBusy());
        if (val != 0) {
            return val;
        }
        val = Integer.compare(s1.getRegionCount(), s2.getRegionCount());
        if (val != 0) {
            return val;
        }
        val = Long.compare(s1.getBytesWritten(), s2.getBytesWritten());
        if (val != 0) {
            return val;
        }
        val = Long.compare(s1.getBytesRead(), s2.getBytesRead());
        if (val != 0) {
            return val;
        }
        val = Long.compare(s1.getKeysWritten(), s2.getKeysWritten());
        if (val != 0) {
            return val;
        }
        val = Long.compare(s1.getKeysRead(), s2.getKeysRead());
        if (val != 0) {
            return val;
        }
        return Long.compare(-s1.getAvailable(), -s2.getAvailable());
    });
    return min.getKey();
}
Also used : StoreStats(com.alipay.sofa.jraft.rhea.metadata.StoreStats) Endpoint(com.alipay.sofa.jraft.util.Endpoint) Pair(com.alipay.sofa.jraft.rhea.util.Pair)

Aggregations

Pair (com.alipay.sofa.jraft.rhea.util.Pair)6 Region (com.alipay.sofa.jraft.rhea.metadata.Region)4 RegionStats (com.alipay.sofa.jraft.rhea.metadata.RegionStats)4 RegionHeartbeatRequest (com.alipay.sofa.jraft.rhea.cmd.pd.RegionHeartbeatRequest)3 Endpoint (com.alipay.sofa.jraft.util.Endpoint)3 MetadataStore (com.alipay.sofa.jraft.rhea.MetadataStore)2 Instruction (com.alipay.sofa.jraft.rhea.metadata.Instruction)2 TimeInterval (com.alipay.sofa.jraft.rhea.metadata.TimeInterval)2 Status (com.alipay.sofa.jraft.Status)1 ClusterStatsManager (com.alipay.sofa.jraft.rhea.ClusterStatsManager)1 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 FencingKeyDB (com.alipay.sofa.jraft.rhea.storage.MemoryKVStoreSnapshotFile.FencingKeyDB)1 LockerDB (com.alipay.sofa.jraft.rhea.storage.MemoryKVStoreSnapshotFile.LockerDB)1 Segment (com.alipay.sofa.jraft.rhea.storage.MemoryKVStoreSnapshotFile.Segment)1 SequenceDB (com.alipay.sofa.jraft.rhea.storage.MemoryKVStoreSnapshotFile.SequenceDB)1 TailIndex (com.alipay.sofa.jraft.rhea.storage.MemoryKVStoreSnapshotFile.TailIndex)1 Timer (com.codahale.metrics.Timer)1 File (java.io.File)1