use of com.alipay.sofa.jraft.rhea.metadata.Region in project sofa-jraft by sofastack.
the class DefaultMetadataStore method batchUpdateRegionStats.
@Override
public CompletableFuture<Boolean> batchUpdateRegionStats(final long clusterId, final List<Pair<Region, RegionStats>> regionStatsList) {
final List<KVEntry> entries = Lists.newArrayListWithCapacity(regionStatsList.size());
for (final Pair<Region, RegionStats> p : regionStatsList) {
final String key = MetadataKeyHelper.getRegionStatsKey(clusterId, p.getKey().getId());
final byte[] bytes = this.serializer.writeObject(p);
entries.add(new KVEntry(BytesUtil.writeUtf8(key), bytes));
}
return this.rheaKVStore.put(entries);
}
use of com.alipay.sofa.jraft.rhea.metadata.Region in project sofa-jraft by sofastack.
the class KVStateMachineTest method setup.
@Before
public void setup() throws IOException, InterruptedException {
final Region region = new Region();
region.setId(1);
final StoreEngine storeEngine = new MockStoreEngine();
final KVStoreStateMachine fsm = new KVStoreStateMachine(region, storeEngine);
final NodeOptions nodeOpts = new NodeOptions();
final Configuration conf = new Configuration();
conf.addPeer(PeerId.parsePeer("127.0.0.1:8081"));
nodeOpts.setInitialConf(conf);
nodeOpts.setFsm(fsm);
final String raftDataPath = "raft_st_test";
this.raftDataPath = new File(raftDataPath);
if (this.raftDataPath.exists()) {
FileUtils.forceDelete(this.raftDataPath);
}
FileUtils.forceMkdir(this.raftDataPath);
final Path logUri = Paths.get(raftDataPath, "log");
nodeOpts.setLogUri(logUri.toString());
final Path meteUri = Paths.get(raftDataPath, "meta");
nodeOpts.setRaftMetaUri(meteUri.toString());
final Path snapshotUri = Paths.get(raftDataPath, "snapshot");
nodeOpts.setSnapshotUri(snapshotUri.toString());
final Endpoint serverAddress = new Endpoint("127.0.0.1", 8081);
final PeerId serverId = new PeerId(serverAddress, 0);
this.raftGroupService = new RaftGroupService("st_test", serverId, nodeOpts, null, true);
final Node node = this.raftGroupService.start(false);
for (int i = 0; i < 100; i++) {
if (node.isLeader()) {
break;
}
Thread.sleep(100);
}
final RawKVStore rawKVStore = storeEngine.getRawKVStore();
this.raftRawKVStore = new RaftRawKVStore(node, rawKVStore, null);
}
use of com.alipay.sofa.jraft.rhea.metadata.Region in project sofa-jraft by sofastack.
the class DefaultRheaKVStore method internalDelete.
private FutureGroup<Boolean> internalDelete(final List<byte[]> keys, final int retriesLeft, final Throwable lastCause) {
final Map<Region, List<byte[]>> regionMap = this.pdClient.findRegionsByKeys(keys, ApiExceptionHelper.isInvalidEpoch(lastCause));
final List<CompletableFuture<Boolean>> futures = Lists.newArrayListWithCapacity(regionMap.size());
final Errors lastError = lastCause == null ? null : Errors.forException(lastCause);
for (final Map.Entry<Region, List<byte[]>> entry : regionMap.entrySet()) {
final Region region = entry.getKey();
final List<byte[]> subKeys = entry.getValue();
final RetryCallable<Boolean> retryCallable = retryCause -> internalDelete(subKeys, retriesLeft - 1, retryCause);
final BoolFailoverFuture future = new BoolFailoverFuture(retriesLeft, retryCallable);
internalRegionDelete(region, subKeys, future, retriesLeft, lastError);
futures.add(future);
}
return new FutureGroup<>(futures);
}
use of com.alipay.sofa.jraft.rhea.metadata.Region in project sofa-jraft by sofastack.
the class DefaultRheaKVStore method internalScan.
private FutureGroup<List<KVEntry>> internalScan(final byte[] startKey, final byte[] endKey, final boolean readOnlySafe, final boolean returnValue, final int retriesLeft, final Throwable lastCause) {
Requires.requireNonNull(startKey, "startKey");
final List<Region> regionList = this.pdClient.findRegionsByKeyRange(startKey, endKey, ApiExceptionHelper.isInvalidEpoch(lastCause));
final List<CompletableFuture<List<KVEntry>>> futures = Lists.newArrayListWithCapacity(regionList.size());
final Errors lastError = lastCause == null ? null : Errors.forException(lastCause);
for (final Region region : regionList) {
final byte[] regionStartKey = region.getStartKey();
final byte[] regionEndKey = region.getEndKey();
final byte[] subStartKey = regionStartKey == null ? startKey : BytesUtil.max(regionStartKey, startKey);
final byte[] subEndKey = regionEndKey == null ? endKey : (endKey == null ? regionEndKey : BytesUtil.min(regionEndKey, endKey));
final ListRetryCallable<KVEntry> retryCallable = retryCause -> internalScan(subStartKey, subEndKey, readOnlySafe, returnValue, retriesLeft - 1, retryCause);
final ListFailoverFuture<KVEntry> future = new ListFailoverFuture<>(retriesLeft, retryCallable);
internalRegionScan(region, subStartKey, subEndKey, false, readOnlySafe, returnValue, future, retriesLeft, lastError, this.onlyLeaderRead);
futures.add(future);
}
return new FutureGroup<>(futures);
}
use of com.alipay.sofa.jraft.rhea.metadata.Region in project sofa-jraft by sofastack.
the class DefaultRheaKVStore method internalRegionDeleteRange.
private void internalRegionDeleteRange(final Region region, final byte[] subStartKey, final byte[] subEndKey, final CompletableFuture<Boolean> future, final int retriesLeft, final Errors lastCause) {
final RegionEngine regionEngine = getRegionEngine(region.getId(), true);
final RetryRunner retryRunner = retryCause -> internalRegionDeleteRange(region, subStartKey, subEndKey, future, retriesLeft - 1, retryCause);
final FailoverClosure<Boolean> closure = new FailoverClosureImpl<>(future, false, retriesLeft, retryRunner);
if (regionEngine != null) {
if (ensureOnValidEpoch(region, regionEngine, closure)) {
getRawKVStore(regionEngine).deleteRange(subStartKey, subEndKey, closure);
}
} else {
final DeleteRangeRequest request = new DeleteRangeRequest();
request.setStartKey(subStartKey);
request.setEndKey(subEndKey);
request.setRegionId(region.getId());
request.setRegionEpoch(region.getRegionEpoch());
this.rheaKVRpcService.callAsyncWithRpc(request, closure, lastCause);
}
}
Aggregations