Search in sources :

Example 1 with Instruction

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;
    }
}
Also used : Endpoint(io.dingodb.raft.util.Endpoint) Region(io.dingodb.store.row.metadata.Region) RegionEngine(io.dingodb.store.row.RegionEngine) Instruction(io.dingodb.store.row.metadata.Instruction)

Example 2 with Instruction

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);
    }
}
Also used : Instruction(io.dingodb.store.row.metadata.Instruction)

Example 3 with 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;
    }
}
Also used : Status(io.dingodb.raft.Status) CompletableFuture(java.util.concurrent.CompletableFuture) BaseKVStoreClosure(io.dingodb.store.row.storage.BaseKVStoreClosure) Region(io.dingodb.store.row.metadata.Region) RegionEngine(io.dingodb.store.row.RegionEngine) Instruction(io.dingodb.store.row.metadata.Instruction)

Example 4 with Instruction

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;
}
Also used : RegionStats(io.dingodb.store.row.metadata.RegionStats) Region(io.dingodb.store.row.metadata.Region) Instruction(io.dingodb.store.row.metadata.Instruction)

Example 5 with 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;
}
Also used : ClusterStatsManager(io.dingodb.server.coordinator.meta.ClusterStatsManager) Set(java.util.Set) Peer(io.dingodb.store.row.metadata.Peer) Instruction(io.dingodb.store.row.metadata.Instruction) Endpoint(io.dingodb.raft.util.Endpoint) Endpoint(io.dingodb.raft.util.Endpoint) Pair(io.dingodb.store.row.util.Pair)

Aggregations

Instruction (io.dingodb.store.row.metadata.Instruction)5 Region (io.dingodb.store.row.metadata.Region)3 Endpoint (io.dingodb.raft.util.Endpoint)2 RegionEngine (io.dingodb.store.row.RegionEngine)2 Status (io.dingodb.raft.Status)1 ClusterStatsManager (io.dingodb.server.coordinator.meta.ClusterStatsManager)1 Peer (io.dingodb.store.row.metadata.Peer)1 RegionStats (io.dingodb.store.row.metadata.RegionStats)1 BaseKVStoreClosure (io.dingodb.store.row.storage.BaseKVStoreClosure)1 Pair (io.dingodb.store.row.util.Pair)1 Set (java.util.Set)1 CompletableFuture (java.util.concurrent.CompletableFuture)1