use of com.alipay.sofa.jraft.rhea.metadata.Region in project sofa-jraft by sofastack.
the class RocksKVStoreTest method snapshotTest.
public void snapshotTest() throws Exception {
final File backupDir = new File("backup");
if (backupDir.exists()) {
FileUtils.deleteDirectory(backupDir);
}
FileUtils.forceMkdir(backupDir);
for (int i = 0; i < 100000; i++) {
final String v = String.valueOf(i);
this.kvStore.put(makeKey(v), makeValue(v), null);
}
final Region region = new Region();
KVStoreSnapshotFile kvStoreSnapshotFile = KVStoreSnapshotFileFactory.getKVStoreSnapshotFile(this.kvStore);
final ExecutorService snapshotPool = StoreEngineHelper.createSnapshotExecutor(1, 2);
final TestSnapshotWriter snapshotWriter = new TestSnapshotWriter(backupDir.getAbsolutePath());
final CountDownLatch latch = new CountDownLatch(1);
final Closure done = status -> {
assertTrue(status.isOk());
latch.countDown();
};
kvStoreSnapshotFile.save(snapshotWriter, region, done, snapshotPool);
latch.await();
final LocalFileMeta meta = (LocalFileMeta) snapshotWriter.getFileMeta(SNAPSHOT_ARCHIVE);
assertNotNull(meta);
assertNotNull(get(makeKey("1")));
this.kvStore.put(makeKey("100001"), makeValue("100001"), null);
assertNotNull(get(makeKey("100001")));
this.kvStore.shutdown();
FileUtils.deleteDirectory(new File(this.tempPath));
FileUtils.forceMkdir(new File(this.tempPath));
this.kvStore = new RocksRawKVStore();
this.kvStore.init(this.dbOptions);
assertNull(get(makeKey("1")));
final TestSnapshotReader snapshotReader = new TestSnapshotReader(snapshotWriter.metaTable, backupDir.getAbsolutePath());
kvStoreSnapshotFile = KVStoreSnapshotFileFactory.getKVStoreSnapshotFile(this.kvStore);
final boolean ret = kvStoreSnapshotFile.load(snapshotReader, region);
assertTrue(ret);
for (int i = 0; i < 100000; i++) {
final String v = String.valueOf(i);
assertArrayEquals(makeValue(v), get(makeKey(v)));
}
// key[100001] is put after the snapshot, so key[100001] should not exist.
assertNull(get(makeKey("100001")));
FileUtils.deleteDirectory(backupDir);
ExecutorServiceHelper.shutdownAndAwaitTermination(snapshotPool);
}
use of com.alipay.sofa.jraft.rhea.metadata.Region 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;
}
}
use of com.alipay.sofa.jraft.rhea.metadata.Region in project sofa-jraft by sofastack.
the class SplittingJudgeByApproximateKeysHandler method isSplitNeeded.
private boolean isSplitNeeded(final RegionHeartbeatRequest request, final Pair<Region, RegionStats> modelWorker) {
if (modelWorker == null) {
return false;
}
final long modelApproximateKeys = modelWorker.getValue().getApproximateKeys();
if (request.getLeastKeysOnSplit() > modelApproximateKeys) {
return false;
}
final Region modelRegion = modelWorker.getKey();
final List<Pair<Region, RegionStats>> regionStatsList = request.getRegionStatsList();
for (final Pair<Region, RegionStats> p : regionStatsList) {
if (modelRegion.equals(p.getKey())) {
return true;
}
}
return false;
}
use of com.alipay.sofa.jraft.rhea.metadata.Region in project sofa-jraft by sofastack.
the class DefaultRheaKVStore method internalTryLockWith.
private void internalTryLockWith(final byte[] key, final boolean keepLease, final DistributedLock.Acquirer acquirer, final CompletableFuture<DistributedLock.Owner> future, final int retriesLeft, final Errors lastCause) {
final Region region = this.pdClient.findRegionByKey(key, ErrorsHelper.isInvalidEpoch(lastCause));
final RegionEngine regionEngine = getRegionEngine(region.getId(), true);
final RetryRunner retryRunner = retryCause -> internalTryLockWith(key, keepLease, acquirer, future, retriesLeft - 1, retryCause);
final FailoverClosure<DistributedLock.Owner> closure = new FailoverClosureImpl<>(future, retriesLeft, retryRunner);
if (regionEngine != null) {
if (ensureOnValidEpoch(region, regionEngine, closure)) {
getRawKVStore(regionEngine).tryLockWith(key, region.getStartKey(), keepLease, acquirer, closure);
}
} else {
final KeyLockRequest request = new KeyLockRequest();
request.setKey(key);
request.setKeepLease(keepLease);
request.setAcquirer(acquirer);
request.setRegionId(region.getId());
request.setRegionEpoch(region.getRegionEpoch());
this.rheaKVRpcService.callAsyncWithRpc(request, closure, lastCause);
}
}
use of com.alipay.sofa.jraft.rhea.metadata.Region in project sofa-jraft by sofastack.
the class DefaultRheaKVStore method internalCompareAndPut.
private void internalCompareAndPut(final byte[] key, final byte[] expect, final byte[] update, final CompletableFuture<Boolean> future, final int retriesLeft, final Errors lastCause) {
final Region region = this.pdClient.findRegionByKey(key, ErrorsHelper.isInvalidEpoch(lastCause));
final RegionEngine regionEngine = getRegionEngine(region.getId(), true);
final RetryRunner retryRunner = retryCause -> internalCompareAndPut(key, expect, update, future, retriesLeft - 1, retryCause);
final FailoverClosure<Boolean> closure = new FailoverClosureImpl<>(future, retriesLeft, retryRunner);
if (regionEngine != null) {
if (ensureOnValidEpoch(region, regionEngine, closure)) {
getRawKVStore(regionEngine).compareAndPut(key, expect, update, closure);
}
} else {
final CompareAndPutRequest request = new CompareAndPutRequest();
request.setKey(key);
request.setExpect(expect);
request.setUpdate(update);
request.setRegionId(region.getId());
request.setRegionEpoch(region.getRegionEpoch());
this.rheaKVRpcService.callAsyncWithRpc(request, closure, lastCause);
}
}
Aggregations