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