Search in sources :

Example 1 with ByteArray

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);
}
Also used : Constants(io.dingodb.store.row.util.Constants) CASAllRequest(io.dingodb.store.row.cmd.store.CASAllRequest) LoggerFactory(org.slf4j.LoggerFactory) RouteTable(io.dingodb.raft.RouteTable) ListRetryCallable(io.dingodb.store.row.client.failover.ListRetryCallable) DeleteRangeRequest(io.dingodb.store.row.cmd.store.DeleteRangeRequest) Strings(io.dingodb.store.row.util.Strings) DistributedLock(io.dingodb.store.row.util.concurrent.DistributedLock) PlacementDriverClient(io.dingodb.store.row.client.pd.PlacementDriverClient) BoolFailoverFuture(io.dingodb.store.row.client.failover.impl.BoolFailoverFuture) Map(java.util.Map) RegionEngine(io.dingodb.store.row.RegionEngine) BatchPutRequest(io.dingodb.store.row.cmd.store.BatchPutRequest) ThreadFactory(java.util.concurrent.ThreadFactory) JRaftHelper(io.dingodb.store.row.JRaftHelper) ResetSequenceRequest(io.dingodb.store.row.cmd.store.ResetSequenceRequest) NodeExecutor(io.dingodb.store.row.storage.NodeExecutor) GetRequest(io.dingodb.store.row.cmd.store.GetRequest) Errors(io.dingodb.store.row.errors.Errors) FailoverClosure(io.dingodb.store.row.client.failover.FailoverClosure) FakePlacementDriverClient(io.dingodb.store.row.client.pd.FakePlacementDriverClient) StoreEngineOptions(io.dingodb.store.row.options.StoreEngineOptions) Status(io.dingodb.raft.Status) PutRequest(io.dingodb.store.row.cmd.store.PutRequest) Lists(io.dingodb.store.row.util.Lists) Utils(io.dingodb.raft.util.Utils) MergeRequest(io.dingodb.store.row.cmd.store.MergeRequest) MultiGetRequest(io.dingodb.store.row.cmd.store.MultiGetRequest) StackTraceUtil(io.dingodb.store.row.util.StackTraceUtil) StateListener(io.dingodb.store.row.StateListener) StoreEngine(io.dingodb.store.row.StoreEngine) PlacementDriverOptions(io.dingodb.store.row.options.PlacementDriverOptions) List(java.util.List) DescriberManager(io.dingodb.store.row.DescriberManager) RpcOptions(io.dingodb.store.row.options.RpcOptions) PutIfAbsentRequest(io.dingodb.store.row.cmd.store.PutIfAbsentRequest) KVMetrics(io.dingodb.store.row.metrics.KVMetrics) KVEntry(io.dingodb.store.row.storage.KVEntry) ZipStrategyManager(io.dingodb.store.row.storage.zip.ZipStrategyManager) WaitStrategyType(io.dingodb.store.row.util.concurrent.disruptor.WaitStrategyType) Endpoint(io.dingodb.raft.util.Endpoint) Histogram(com.codahale.metrics.Histogram) KeyLockRequest(io.dingodb.store.row.cmd.store.KeyLockRequest) KeyUnlockRequest(io.dingodb.store.row.cmd.store.KeyUnlockRequest) KVStoreClosure(io.dingodb.store.row.storage.KVStoreClosure) Requires(io.dingodb.raft.util.Requires) KVMetricNames(io.dingodb.store.row.metrics.KVMetricNames) DingoRowStoreOptions(io.dingodb.store.row.options.DingoRowStoreOptions) BatchDeleteRequest(io.dingodb.store.row.cmd.store.BatchDeleteRequest) FollowerStateListener(io.dingodb.store.row.FollowerStateListener) MapFailoverFuture(io.dingodb.store.row.client.failover.impl.MapFailoverFuture) CompletableFuture(java.util.concurrent.CompletableFuture) LogExceptionHandler(io.dingodb.raft.util.LogExceptionHandler) ApiExceptionHelper(io.dingodb.store.row.errors.ApiExceptionHelper) CompareAndPutRequest(io.dingodb.store.row.cmd.store.CompareAndPutRequest) GetSequenceRequest(io.dingodb.store.row.cmd.store.GetSequenceRequest) AffinityNamedThreadFactory(io.dingodb.store.row.util.concurrent.AffinityNamedThreadFactory) RetryRunner(io.dingodb.store.row.client.failover.RetryRunner) FailoverClosureImpl(io.dingodb.store.row.client.failover.impl.FailoverClosureImpl) Sequence(io.dingodb.store.row.storage.Sequence) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Dispatcher(io.dingodb.store.row.util.concurrent.disruptor.Dispatcher) RemotePlacementDriverClient(io.dingodb.store.row.client.pd.RemotePlacementDriverClient) DeleteRequest(io.dingodb.store.row.cmd.store.DeleteRequest) BatchingOptions(io.dingodb.store.row.options.BatchingOptions) EventHandler(com.lmax.disruptor.EventHandler) ContainsKeyRequest(io.dingodb.store.row.cmd.store.ContainsKeyRequest) KVIterator(io.dingodb.store.row.storage.KVIterator) ByteArray(io.dingodb.store.row.util.ByteArray) NodeExecuteRequest(io.dingodb.store.row.cmd.store.NodeExecuteRequest) RetryCallable(io.dingodb.store.row.client.failover.RetryCallable) Logger(org.slf4j.Logger) NamedThreadFactory(io.dingodb.raft.util.NamedThreadFactory) GetAndPutRequest(io.dingodb.store.row.cmd.store.GetAndPutRequest) DingoRowStoreRuntimeException(io.dingodb.store.row.errors.DingoRowStoreRuntimeException) RingBuffer(com.lmax.disruptor.RingBuffer) Region(io.dingodb.store.row.metadata.Region) ErrorsHelper(io.dingodb.store.row.errors.ErrorsHelper) ScanRequest(io.dingodb.store.row.cmd.store.ScanRequest) TaskDispatcher(io.dingodb.store.row.util.concurrent.disruptor.TaskDispatcher) TimeUnit(java.util.concurrent.TimeUnit) RawKVStore(io.dingodb.store.row.storage.RawKVStore) StateListenerContainer(io.dingodb.store.row.StateListenerContainer) LeaderStateListener(io.dingodb.store.row.LeaderStateListener) ListFailoverFuture(io.dingodb.store.row.client.failover.impl.ListFailoverFuture) PeerId(io.dingodb.raft.entity.PeerId) BytesUtil(io.dingodb.raft.util.BytesUtil) EventFactory(com.lmax.disruptor.EventFactory) Collections(java.util.Collections) Disruptor(com.lmax.disruptor.dsl.Disruptor) ExtSerializerSupports(io.dingodb.store.row.rpc.ExtSerializerSupports) CASEntry(io.dingodb.store.row.storage.CASEntry) MapFailoverFuture(io.dingodb.store.row.client.failover.impl.MapFailoverFuture) Errors(io.dingodb.store.row.errors.Errors) CompletableFuture(java.util.concurrent.CompletableFuture) Region(io.dingodb.store.row.metadata.Region) ByteArray(io.dingodb.store.row.util.ByteArray) List(java.util.List) Map(java.util.Map)

Example 2 with ByteArray

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);
    }
}
Also used : Constants(io.dingodb.store.row.util.Constants) CASAllRequest(io.dingodb.store.row.cmd.store.CASAllRequest) LoggerFactory(org.slf4j.LoggerFactory) RouteTable(io.dingodb.raft.RouteTable) ListRetryCallable(io.dingodb.store.row.client.failover.ListRetryCallable) DeleteRangeRequest(io.dingodb.store.row.cmd.store.DeleteRangeRequest) Strings(io.dingodb.store.row.util.Strings) DistributedLock(io.dingodb.store.row.util.concurrent.DistributedLock) PlacementDriverClient(io.dingodb.store.row.client.pd.PlacementDriverClient) BoolFailoverFuture(io.dingodb.store.row.client.failover.impl.BoolFailoverFuture) Map(java.util.Map) RegionEngine(io.dingodb.store.row.RegionEngine) BatchPutRequest(io.dingodb.store.row.cmd.store.BatchPutRequest) ThreadFactory(java.util.concurrent.ThreadFactory) JRaftHelper(io.dingodb.store.row.JRaftHelper) ResetSequenceRequest(io.dingodb.store.row.cmd.store.ResetSequenceRequest) NodeExecutor(io.dingodb.store.row.storage.NodeExecutor) GetRequest(io.dingodb.store.row.cmd.store.GetRequest) Errors(io.dingodb.store.row.errors.Errors) FailoverClosure(io.dingodb.store.row.client.failover.FailoverClosure) FakePlacementDriverClient(io.dingodb.store.row.client.pd.FakePlacementDriverClient) StoreEngineOptions(io.dingodb.store.row.options.StoreEngineOptions) Status(io.dingodb.raft.Status) PutRequest(io.dingodb.store.row.cmd.store.PutRequest) Lists(io.dingodb.store.row.util.Lists) Utils(io.dingodb.raft.util.Utils) MergeRequest(io.dingodb.store.row.cmd.store.MergeRequest) MultiGetRequest(io.dingodb.store.row.cmd.store.MultiGetRequest) StackTraceUtil(io.dingodb.store.row.util.StackTraceUtil) StateListener(io.dingodb.store.row.StateListener) StoreEngine(io.dingodb.store.row.StoreEngine) PlacementDriverOptions(io.dingodb.store.row.options.PlacementDriverOptions) List(java.util.List) DescriberManager(io.dingodb.store.row.DescriberManager) RpcOptions(io.dingodb.store.row.options.RpcOptions) PutIfAbsentRequest(io.dingodb.store.row.cmd.store.PutIfAbsentRequest) KVMetrics(io.dingodb.store.row.metrics.KVMetrics) KVEntry(io.dingodb.store.row.storage.KVEntry) ZipStrategyManager(io.dingodb.store.row.storage.zip.ZipStrategyManager) WaitStrategyType(io.dingodb.store.row.util.concurrent.disruptor.WaitStrategyType) Endpoint(io.dingodb.raft.util.Endpoint) Histogram(com.codahale.metrics.Histogram) KeyLockRequest(io.dingodb.store.row.cmd.store.KeyLockRequest) KeyUnlockRequest(io.dingodb.store.row.cmd.store.KeyUnlockRequest) KVStoreClosure(io.dingodb.store.row.storage.KVStoreClosure) Requires(io.dingodb.raft.util.Requires) KVMetricNames(io.dingodb.store.row.metrics.KVMetricNames) DingoRowStoreOptions(io.dingodb.store.row.options.DingoRowStoreOptions) BatchDeleteRequest(io.dingodb.store.row.cmd.store.BatchDeleteRequest) FollowerStateListener(io.dingodb.store.row.FollowerStateListener) MapFailoverFuture(io.dingodb.store.row.client.failover.impl.MapFailoverFuture) CompletableFuture(java.util.concurrent.CompletableFuture) LogExceptionHandler(io.dingodb.raft.util.LogExceptionHandler) ApiExceptionHelper(io.dingodb.store.row.errors.ApiExceptionHelper) CompareAndPutRequest(io.dingodb.store.row.cmd.store.CompareAndPutRequest) GetSequenceRequest(io.dingodb.store.row.cmd.store.GetSequenceRequest) AffinityNamedThreadFactory(io.dingodb.store.row.util.concurrent.AffinityNamedThreadFactory) RetryRunner(io.dingodb.store.row.client.failover.RetryRunner) FailoverClosureImpl(io.dingodb.store.row.client.failover.impl.FailoverClosureImpl) Sequence(io.dingodb.store.row.storage.Sequence) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Dispatcher(io.dingodb.store.row.util.concurrent.disruptor.Dispatcher) RemotePlacementDriverClient(io.dingodb.store.row.client.pd.RemotePlacementDriverClient) DeleteRequest(io.dingodb.store.row.cmd.store.DeleteRequest) BatchingOptions(io.dingodb.store.row.options.BatchingOptions) EventHandler(com.lmax.disruptor.EventHandler) ContainsKeyRequest(io.dingodb.store.row.cmd.store.ContainsKeyRequest) KVIterator(io.dingodb.store.row.storage.KVIterator) ByteArray(io.dingodb.store.row.util.ByteArray) NodeExecuteRequest(io.dingodb.store.row.cmd.store.NodeExecuteRequest) RetryCallable(io.dingodb.store.row.client.failover.RetryCallable) Logger(org.slf4j.Logger) NamedThreadFactory(io.dingodb.raft.util.NamedThreadFactory) GetAndPutRequest(io.dingodb.store.row.cmd.store.GetAndPutRequest) DingoRowStoreRuntimeException(io.dingodb.store.row.errors.DingoRowStoreRuntimeException) RingBuffer(com.lmax.disruptor.RingBuffer) Region(io.dingodb.store.row.metadata.Region) ErrorsHelper(io.dingodb.store.row.errors.ErrorsHelper) ScanRequest(io.dingodb.store.row.cmd.store.ScanRequest) TaskDispatcher(io.dingodb.store.row.util.concurrent.disruptor.TaskDispatcher) TimeUnit(java.util.concurrent.TimeUnit) RawKVStore(io.dingodb.store.row.storage.RawKVStore) StateListenerContainer(io.dingodb.store.row.StateListenerContainer) LeaderStateListener(io.dingodb.store.row.LeaderStateListener) ListFailoverFuture(io.dingodb.store.row.client.failover.impl.ListFailoverFuture) PeerId(io.dingodb.raft.entity.PeerId) BytesUtil(io.dingodb.raft.util.BytesUtil) EventFactory(com.lmax.disruptor.EventFactory) Collections(java.util.Collections) Disruptor(com.lmax.disruptor.dsl.Disruptor) ExtSerializerSupports(io.dingodb.store.row.rpc.ExtSerializerSupports) CASEntry(io.dingodb.store.row.storage.CASEntry) FailoverClosureImpl(io.dingodb.store.row.client.failover.impl.FailoverClosureImpl) RawKVStore(io.dingodb.store.row.storage.RawKVStore) RegionEngine(io.dingodb.store.row.RegionEngine) Map(java.util.Map) RetryRunner(io.dingodb.store.row.client.failover.RetryRunner) MultiGetRequest(io.dingodb.store.row.cmd.store.MultiGetRequest)

Example 3 with ByteArray

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();
    }
}
Also used : Timer(com.codahale.metrics.Timer) ByteArray(io.dingodb.store.row.util.ByteArray) Map(java.util.Map) EnumMap(java.util.EnumMap) RocksDBException(org.rocksdb.RocksDBException) StorageException(io.dingodb.store.row.errors.StorageException) IOException(java.io.IOException) DistributedLock(io.dingodb.store.row.util.concurrent.DistributedLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Lock(java.util.concurrent.locks.Lock)

Example 4 with ByteArray

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();
    }
}
Also used : Timer(com.codahale.metrics.Timer) ByteArray(io.dingodb.store.row.util.ByteArray)

Example 5 with ByteArray

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();
    }
}
Also used : DistributedLock(io.dingodb.store.row.util.concurrent.DistributedLock) Timer(com.codahale.metrics.Timer) ByteArray(io.dingodb.store.row.util.ByteArray)

Aggregations

ByteArray (io.dingodb.store.row.util.ByteArray)6 DistributedLock (io.dingodb.store.row.util.concurrent.DistributedLock)5 Timer (com.codahale.metrics.Timer)4 Map (java.util.Map)3 Histogram (com.codahale.metrics.Histogram)2 EventFactory (com.lmax.disruptor.EventFactory)2 EventHandler (com.lmax.disruptor.EventHandler)2 RingBuffer (com.lmax.disruptor.RingBuffer)2 Disruptor (com.lmax.disruptor.dsl.Disruptor)2 RouteTable (io.dingodb.raft.RouteTable)2 Status (io.dingodb.raft.Status)2 PeerId (io.dingodb.raft.entity.PeerId)2 BytesUtil (io.dingodb.raft.util.BytesUtil)2 Endpoint (io.dingodb.raft.util.Endpoint)2 LogExceptionHandler (io.dingodb.raft.util.LogExceptionHandler)2 NamedThreadFactory (io.dingodb.raft.util.NamedThreadFactory)2 Requires (io.dingodb.raft.util.Requires)2 Utils (io.dingodb.raft.util.Utils)2 DescriberManager (io.dingodb.store.row.DescriberManager)2 FollowerStateListener (io.dingodb.store.row.FollowerStateListener)2