Search in sources :

Example 1 with Instruction

use of com.alipay.sofa.jraft.rhea.metadata.Instruction 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 2 with Instruction

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

the class InstructionProcessor method processTransferLeader.

private boolean processTransferLeader(final Instruction instruction) {
    try {
        final Instruction.TransferLeader transferLeader = instruction.getTransferLeader();
        if (transferLeader == null) {
            return false;
        }
        final Endpoint toEndpoint = transferLeader.getMoveToEndpoint();
        if (toEndpoint == null) {
            LOG.error("TransferLeader#toEndpoint must not be null, {}.", instruction);
            return false;
        }
        final Region region = instruction.getRegion();
        final long regionId = region.getId();
        final RegionEngine engine = this.storeEngine.getRegionEngine(regionId);
        if (engine == null) {
            LOG.error("Could not found regionEngine, {}.", instruction);
            return false;
        }
        if (!region.equals(engine.getRegion())) {
            LOG.warn("Instruction [{}] is out of date.", instruction);
            return false;
        }
        return engine.transferLeadershipTo(toEndpoint);
    } catch (final Throwable t) {
        LOG.error("Caught an exception on #processTransferLeader: {}.", StackTraceUtil.stackTrace(t));
        return false;
    }
}
Also used : Endpoint(com.alipay.sofa.jraft.util.Endpoint) Region(com.alipay.sofa.jraft.rhea.metadata.Region) RegionEngine(com.alipay.sofa.jraft.rhea.RegionEngine) Instruction(com.alipay.sofa.jraft.rhea.metadata.Instruction)

Example 3 with Instruction

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

the class InstructionProcessor method process.

public void process(final List<Instruction> instructions) {
    LOG.info("Received instructions: {}.", instructions);
    for (final Instruction instruction : instructions) {
        if (!checkInstruction(instruction)) {
            continue;
        }
        processSplit(instruction);
        processTransferLeader(instruction);
    }
}
Also used : Instruction(com.alipay.sofa.jraft.rhea.metadata.Instruction)

Example 4 with Instruction

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

the class InstructionProcessor method processSplit.

private boolean processSplit(final Instruction instruction) {
    try {
        final Instruction.RangeSplit rangeSplit = instruction.getRangeSplit();
        if (rangeSplit == null) {
            return false;
        }
        final Long newRegionId = rangeSplit.getNewRegionId();
        if (newRegionId == null) {
            LOG.error("RangeSplit#newRegionId must not be null, {}.", instruction);
            return false;
        }
        final Region region = instruction.getRegion();
        final long regionId = region.getId();
        final RegionEngine engine = this.storeEngine.getRegionEngine(regionId);
        if (engine == null) {
            LOG.error("Could not found regionEngine, {}.", instruction);
            return false;
        }
        if (!region.equals(engine.getRegion())) {
            LOG.warn("Instruction [{}] is out of date.", instruction);
            return false;
        }
        final CompletableFuture<Status> future = new CompletableFuture<>();
        this.storeEngine.applySplit(regionId, newRegionId, new BaseKVStoreClosure() {

            @Override
            public void run(Status status) {
                future.complete(status);
            }
        });
        final Status status = future.get(20, TimeUnit.SECONDS);
        final boolean ret = status.isOk();
        if (ret) {
            LOG.info("Range-split succeeded, instruction: {}.", instruction);
        } else {
            LOG.warn("Range-split failed: {}, instruction: {}.", status, instruction);
        }
        return ret;
    } catch (final Throwable t) {
        LOG.error("Caught an exception on #processSplit: {}.", StackTraceUtil.stackTrace(t));
        return false;
    }
}
Also used : Status(com.alipay.sofa.jraft.Status) BaseKVStoreClosure(com.alipay.sofa.jraft.rhea.storage.BaseKVStoreClosure) Instruction(com.alipay.sofa.jraft.rhea.metadata.Instruction) CompletableFuture(java.util.concurrent.CompletableFuture) Region(com.alipay.sofa.jraft.rhea.metadata.Region) RegionEngine(com.alipay.sofa.jraft.rhea.RegionEngine)

Example 5 with Instruction

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

Aggregations

Instruction (com.alipay.sofa.jraft.rhea.metadata.Instruction)6 Region (com.alipay.sofa.jraft.rhea.metadata.Region)5 RegionHeartbeatRequest (com.alipay.sofa.jraft.rhea.cmd.pd.RegionHeartbeatRequest)3 RegionStats (com.alipay.sofa.jraft.rhea.metadata.RegionStats)3 Endpoint (com.alipay.sofa.jraft.util.Endpoint)3 Status (com.alipay.sofa.jraft.Status)2 ClusterStatsManager (com.alipay.sofa.jraft.rhea.ClusterStatsManager)2 MetadataStore (com.alipay.sofa.jraft.rhea.MetadataStore)2 RegionEngine (com.alipay.sofa.jraft.rhea.RegionEngine)2 Pair (com.alipay.sofa.jraft.rhea.util.Pair)2 Peer (com.alipay.sofa.jraft.rhea.metadata.Peer)1 TimeInterval (com.alipay.sofa.jraft.rhea.metadata.TimeInterval)1 BaseKVStoreClosure (com.alipay.sofa.jraft.rhea.storage.BaseKVStoreClosure)1 List (java.util.List)1 Set (java.util.Set)1 CompletableFuture (java.util.concurrent.CompletableFuture)1