use of io.dingodb.store.row.metadata.Instruction in project dingo by dingodb.
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 String 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 io.dingodb.store.row.metadata.Instruction in project dingo by dingodb.
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 io.dingodb.store.row.metadata.Instruction in project dingo by dingodb.
the class InstructionProcessor method processSplit.
private boolean processSplit(final Instruction instruction) {
try {
final Instruction.RangeSplit rangeSplit = instruction.getRangeSplit();
if (rangeSplit == null) {
return false;
}
final String newRegionId = rangeSplit.getNewRegionId();
if (newRegionId == null) {
LOG.error("RangeSplit#newRegionId must not be null, {}.", instruction);
return false;
}
final Region region = instruction.getRegion();
final String 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 io.dingodb.store.row.metadata.Instruction in project dingo by dingodb.
the class RegionHeartbeatHandler method split.
private Instruction split(RegionHeartbeatRequest request) {
Region region = request.getRegion();
RegionStats regionStats = request.getRegionStats();
clusterStatsManager.addOrUpdateRegionStats(region, regionStats);
final Set<String> stores = rowStoreMetaAdaptor.storeLocation().keySet();
if (stores.isEmpty()) {
return null;
}
if (clusterStatsManager.regionSize() >= stores.size()) {
// one store one region is perfect
return null;
}
final Pair<Region, RegionStats> modelWorker = clusterStatsManager.findModelWorkerRegion();
if (!isSplitNeeded(request, modelWorker)) {
return null;
}
final String newRegionId = rowStoreMetaAdaptor.newRegionId().seqNo().toString();
final Instruction.RangeSplit rangeSplit = new Instruction.RangeSplit();
rangeSplit.setNewRegionId(newRegionId);
final Instruction instruction = new Instruction();
instruction.setRegion(modelWorker.getKey().copy());
instruction.setRangeSplit(rangeSplit);
return instruction;
}
use of io.dingodb.store.row.metadata.Instruction in project dingo by dingodb.
the class RegionHeartbeatHandler method balance.
private Instruction balance(Region region, RegionStats regionStats) {
final long clusterId = 0;
final String storeId = regionStats.getLeader().getStoreId();
final ClusterStatsManager clusterStatsManager = ClusterStatsManager.getInstance(clusterId);
clusterStatsManager.addOrUpdateLeader(storeId, region.getId());
// check if the modelWorker
final Pair<Set<String>, Integer> modelWorkers = clusterStatsManager.findModelWorkerStores(1);
final Set<String> modelWorkerStoreIds = modelWorkers.getKey();
final int modelWorkerLeaders = modelWorkers.getValue();
if (!modelWorkerStoreIds.contains(storeId)) {
return null;
}
log.info("[Cluster] model worker stores is: {}, it has {} leaders.", modelWorkerStoreIds, modelWorkerLeaders);
final List<Peer> peers = region.getPeers();
if (peers == null) {
return null;
}
final List<Endpoint> endpoints = Lists.transform(peers, Peer::getEndpoint);
final Map<String, Endpoint> storeIds = rowStoreMetaAdaptor.storeLocation();
// find lazyWorkers
final List<Pair<String, Integer>> lazyWorkers = clusterStatsManager.findLazyWorkerStores(storeIds.keySet());
if (lazyWorkers.isEmpty()) {
return null;
}
for (int i = lazyWorkers.size() - 1; i >= 0; i--) {
final Pair<String, Integer> worker = lazyWorkers.get(i);
if (modelWorkerLeaders - worker.getValue() <= 1) {
// no need to transfer
lazyWorkers.remove(i);
}
}
if (lazyWorkers.isEmpty()) {
return null;
}
final Pair<String, Integer> laziestWorker = tryToFindLaziestWorker(lazyWorkers);
if (laziestWorker == null) {
return null;
}
final String 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);
log.info("[Cluster: {}], send 'instruction.transferLeader': {} to region: {}.", clusterId, instruction, region);
return instruction;
}
Aggregations