use of io.dingodb.store.row.util.ByteArray in project dingo by dingodb.
the class DefaultDingoRowStore method internalMultiGet.
private FutureGroup<Map<ByteArray, byte[]>> internalMultiGet(final List<byte[]> keys, final boolean readOnlySafe, final int retriesLeft, final Throwable lastCause) {
final Map<Region, List<byte[]>> regionMap = this.pdClient.findRegionsByKeys(keys, ApiExceptionHelper.isInvalidEpoch(lastCause));
final List<CompletableFuture<Map<ByteArray, byte[]>>> 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<Map<ByteArray, byte[]>> retryCallable = retryCause -> internalMultiGet(subKeys, readOnlySafe, retriesLeft - 1, retryCause);
final MapFailoverFuture<ByteArray, byte[]> future = new MapFailoverFuture<>(retriesLeft, retryCallable);
internalRegionMultiGet(region, subKeys, readOnlySafe, future, retriesLeft, lastError, this.onlyLeaderRead);
futures.add(future);
}
return new FutureGroup<>(futures);
}
use of io.dingodb.store.row.util.ByteArray in project dingo by dingodb.
the class DefaultDingoRowStore method internalRegionMultiGet.
private void internalRegionMultiGet(final Region region, final List<byte[]> subKeys, final boolean readOnlySafe, final CompletableFuture<Map<ByteArray, byte[]>> future, final int retriesLeft, final Errors lastCause, final boolean requireLeader) {
final RegionEngine regionEngine = getRegionEngine(region.getId(), requireLeader);
// require leader on retry
final RetryRunner retryRunner = retryCause -> internalRegionMultiGet(region, subKeys, readOnlySafe, future, retriesLeft - 1, retryCause, true);
final FailoverClosure<Map<ByteArray, byte[]>> 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.multiGet(subKeys, readOnlySafe, closure);
} else {
this.kvDispatcher.execute(() -> rawKVStore.multiGet(subKeys, readOnlySafe, closure));
}
}
} else {
final MultiGetRequest request = new MultiGetRequest();
request.setKeys(subKeys);
request.setReadOnlySafe(readOnlySafe);
request.setRegionId(region.getId());
request.setRegionEpoch(region.getRegionEpoch());
this.dingoRowStoreRpcService.callAsyncWithRpc(request, closure, lastCause, requireLeader);
}
}
use of io.dingodb.store.row.util.ByteArray in project dingo by dingodb.
the class RocksRawKVStore method multiGet.
@Override
public void multiGet(final List<byte[]> keys, @SuppressWarnings("unused") final boolean readOnlySafe, final KVStoreClosure closure) {
final Timer.Context timeCtx = getTimeContext("MULTI_GET");
final Lock readLock = this.readWriteLock.readLock();
readLock.lock();
try {
final Map<byte[], byte[]> rawMap = this.db.multiGet(keys);
final Map<ByteArray, byte[]> resultMap = Maps.newHashMapWithExpectedSize(rawMap.size());
for (final Map.Entry<byte[], byte[]> entry : rawMap.entrySet()) {
resultMap.put(ByteArray.wrap(entry.getKey()), entry.getValue());
}
setSuccess(closure, resultMap);
} catch (final Exception e) {
LOG.error("Fail to [MULTI_GET], key size: [{}], {}.", keys.size(), StackTraceUtil.stackTrace(e));
setFailure(closure, "Fail to [MULTI_GET]");
} finally {
readLock.unlock();
timeCtx.stop();
}
}
use of io.dingodb.store.row.util.ByteArray in project dingo by dingodb.
the class MemoryRawKVStore method getSequence.
@Override
public void getSequence(final byte[] seqKey, final int step, final KVStoreClosure closure) {
final Timer.Context timeCtx = getTimeContext("GET_SEQUENCE");
try {
final ByteArray wrappedKey = ByteArray.wrap(seqKey);
Long startVal = this.sequenceDB.get(wrappedKey);
startVal = startVal == null ? 0 : startVal;
if (step < 0) {
// never get here
setFailure(closure, "Fail to [GET_SEQUENCE], step must >= 0");
return;
}
if (step == 0) {
setSuccess(closure, new Sequence(startVal, startVal));
return;
}
final long endVal = getSafeEndValueForSequence(startVal, step);
if (startVal != endVal) {
this.sequenceDB.put(wrappedKey, endVal);
}
setSuccess(closure, new Sequence(startVal, endVal));
} catch (final Exception e) {
LOG.error("Fail to [GET_SEQUENCE], [key = {}, step = {}], {}.", BytesUtil.toHex(seqKey), step, StackTraceUtil.stackTrace(e));
setCriticalError(closure, "Fail to [GET_SEQUENCE]", e);
} finally {
timeCtx.stop();
}
}
use of io.dingodb.store.row.util.ByteArray in project dingo by dingodb.
the class MemoryRawKVStore method releaseLockWith.
@Override
public void releaseLockWith(final byte[] key, final DistributedLock.Acquirer acquirer, final KVStoreClosure closure) {
final Timer.Context timeCtx = getTimeContext("RELEASE_LOCK");
try {
final ByteArray wrappedKey = ByteArray.wrap(key);
final DistributedLock.Owner prevOwner = this.lockerDB.get(wrappedKey);
final DistributedLock.Owner owner;
// noinspection ConstantConditions
do {
final DistributedLock.OwnerBuilder builder = DistributedLock.newOwnerBuilder();
if (prevOwner == null) {
LOG.warn("Lock not exist: {}.", acquirer);
owner = //
builder.id(acquirer.getId()).fencingToken(acquirer.getFencingToken()).acquires(0).success(true).build();
break;
}
if (prevOwner.isSameAcquirer(acquirer)) {
final long acquires = prevOwner.getAcquires() - 1;
owner = //
builder.id(prevOwner.getId()).deadlineMillis(prevOwner.getDeadlineMillis()).fencingToken(prevOwner.getFencingToken()).acquires(acquires).context(prevOwner.getContext()).success(true).build();
if (acquires <= 0) {
// real delete, goodbye ~
this.lockerDB.remove(wrappedKey);
} else {
// acquires--
this.lockerDB.put(wrappedKey, owner);
}
break;
}
// invalid acquirer, can't to release the lock
owner = //
builder.id(prevOwner.getId()).fencingToken(prevOwner.getFencingToken()).acquires(prevOwner.getAcquires()).context(prevOwner.getContext()).success(false).build();
LOG.warn("The lock owner is: [{}], [{}] could't release it.", prevOwner, acquirer);
} while (false);
setSuccess(closure, owner);
} catch (final Exception e) {
LOG.error("Fail to [RELEASE_LOCK], [{}], {}.", BytesUtil.toHex(key), StackTraceUtil.stackTrace(e));
setCriticalError(closure, "Fail to [RELEASE_LOCK]", e);
} finally {
timeCtx.stop();
}
}
Aggregations