use of com.alipay.sofa.jraft.rhea.storage.CASEntry in project sofa-jraft by sofastack.
the class RegionRouteTable method findRegionsByCASEntries.
/**
* Returns the list of regions to which the keys belongs.
*/
public Map<Region, List<CASEntry>> findRegionsByCASEntries(final List<CASEntry> casEntries) {
Requires.requireNonNull(casEntries, "casEntries");
final Map<Region, List<CASEntry>> regionMap = Maps.newHashMap();
final StampedLock stampedLock = this.stampedLock;
final long stamp = stampedLock.readLock();
try {
for (final CASEntry casEntry : casEntries) {
final Region region = findRegionByKeyWithoutLock(casEntry.getKey());
regionMap.computeIfAbsent(region, k -> Lists.newArrayList()).add(casEntry);
}
return regionMap;
} finally {
stampedLock.unlockRead(stamp);
}
}
use of com.alipay.sofa.jraft.rhea.storage.CASEntry in project sofa-jraft by sofastack.
the class AbstractRheaKVStoreTest method compareAndPutAllTest.
/**
* Test method: {@link RheaKVStore#compareAndPutAll(List)}
*/
public void compareAndPutAllTest(final RheaKVStore store) {
final List<CASEntry> entries = new ArrayList<>();
entries.add(new CASEntry(makeKey("k1"), null, makeValue("v1")));
entries.add(new CASEntry(makeKey("k2"), null, makeValue("v2")));
entries.add(new CASEntry(makeKey("k3"), null, makeValue("v3")));
boolean ret = store.bCompareAndPutAll(entries);
assertTrue(ret);
entries.clear();
entries.add(new CASEntry(makeKey("k1"), makeValue("v1"), makeValue("v11")));
entries.add(new CASEntry(makeKey("k2"), makeValue("v2"), makeValue("v22")));
ret = store.bCompareAndPutAll(entries);
assertTrue(ret);
entries.clear();
entries.add(new CASEntry(makeKey("k1"), makeValue("v11"), makeValue("v111")));
entries.add(new CASEntry(makeKey("k2"), makeValue("v22"), makeValue("v222")));
entries.add(new CASEntry(makeKey("k3"), makeValue("v33"), makeValue("v333")));
ret = store.bCompareAndPutAll(entries);
assertTrue(!ret);
final Map<ByteArray, byte[]> map = store.bMultiGet(Lists.newArrayList(makeKey("k1"), makeKey("k2"), makeKey("k3")));
assertArrayEquals(makeValue("v11"), map.get(ByteArray.wrap(makeKey("k1"))));
assertArrayEquals(makeValue("v22"), map.get(ByteArray.wrap(makeKey("k2"))));
assertArrayEquals(makeValue("v3"), map.get(ByteArray.wrap(makeKey("k3"))));
}
use of com.alipay.sofa.jraft.rhea.storage.CASEntry in project sofa-jraft by sofastack.
the class DefaultRheaKVStore method internalCompareAndPutAll.
private FutureGroup<Boolean> internalCompareAndPutAll(final List<CASEntry> entries, final int retriesLeft, final Throwable lastCause) {
final Map<Region, List<CASEntry>> regionMap = this.pdClient.findRegionsByCASEntries(entries, 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<CASEntry>> entry : regionMap.entrySet()) {
final Region region = entry.getKey();
final List<CASEntry> subEntries = entry.getValue();
final RetryCallable<Boolean> retryCallable = retryCause -> internalCompareAndPutAll(subEntries, retriesLeft - 1, retryCause);
final BoolFailoverFuture future = new BoolFailoverFuture(retriesLeft, retryCallable);
internalRegionCompareAndPutAll(region, subEntries, future, retriesLeft, lastError);
futures.add(future);
}
return new FutureGroup<>(futures);
}
use of com.alipay.sofa.jraft.rhea.storage.CASEntry in project sofa-jraft by sofastack.
the class DefaultRheaKVStore method internalRegionCompareAndPutAll.
private void internalRegionCompareAndPutAll(final Region region, final List<CASEntry> subEntries, final CompletableFuture<Boolean> future, final int retriesLeft, final Errors lastCause) {
final RegionEngine regionEngine = getRegionEngine(region.getId(), true);
final RetryRunner retryRunner = retryCause -> internalRegionCompareAndPutAll(region, subEntries, future, retriesLeft - 1, retryCause);
final FailoverClosure<Boolean> closure = new FailoverClosureImpl<>(future, false, retriesLeft, retryRunner);
if (regionEngine != null) {
if (ensureOnValidEpoch(region, regionEngine, closure)) {
final RawKVStore rawKVStore = getRawKVStore(regionEngine);
if (this.kvDispatcher == null) {
rawKVStore.compareAndPutAll(subEntries, closure);
} else {
this.kvDispatcher.execute(() -> rawKVStore.compareAndPutAll(subEntries, closure));
}
}
} else {
final CASAllRequest request = new CASAllRequest();
request.setCasEntries(subEntries);
request.setRegionId(region.getId());
request.setRegionEpoch(region.getRegionEpoch());
this.rheaKVRpcService.callAsyncWithRpc(request, closure, lastCause);
}
}
use of com.alipay.sofa.jraft.rhea.storage.CASEntry in project sofa-jraft by sofastack.
the class DefaultRegionKVService method handleCompareAndPutAll.
@Override
public void handleCompareAndPutAll(final CASAllRequest request, final RequestProcessClosure<BaseRequest, BaseResponse<?>> closure) {
final CASAllResponse response = new CASAllResponse();
response.setRegionId(getRegionId());
response.setRegionEpoch(getRegionEpoch());
try {
KVParameterRequires.requireSameEpoch(request, getRegionEpoch());
final List<CASEntry> casEntries = KVParameterRequires.requireNonEmpty(request.getCasEntries(), "casAll.casEntries");
this.rawKVStore.compareAndPutAll(casEntries, new BaseKVStoreClosure() {
@Override
public void run(final Status status) {
if (status.isOk()) {
response.setValue((Boolean) getData());
} else {
setFailure(request, response, status, getError());
}
closure.sendResponse(response);
}
});
} catch (final Throwable t) {
LOG.error("Failed to handle: {}, {}.", request, StackTraceUtil.stackTrace(t));
response.setError(Errors.forException(t));
closure.sendResponse(response);
}
}
Aggregations