Search in sources :

Example 6 with KVEntry

use of com.alipay.sofa.jraft.rhea.storage.KVEntry 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);
}
Also used : KVEntry(com.alipay.sofa.jraft.rhea.storage.KVEntry) RegionStats(com.alipay.sofa.jraft.rhea.metadata.RegionStats) Region(com.alipay.sofa.jraft.rhea.metadata.Region)

Example 7 with KVEntry

use of com.alipay.sofa.jraft.rhea.storage.KVEntry in project sofa-jraft by sofastack.

the class AbstractRheaKVStoreTest method deleteListTest.

/**
 * Test method: {@link RheaKVStore#delete(List)}
 */
private void deleteListTest(RheaKVStore store) {
    List<KVEntry> entries1 = Lists.newArrayList();
    List<byte[]> keys1 = Lists.newArrayList();
    for (int i = 0; i < 3; i++) {
        byte[] key = makeKey("batch_del_test_key" + i);
        checkRegion(store, key, 1);
        entries1.add(new KVEntry(key, makeValue("batch_del_test_value" + i)));
        keys1.add(key);
    }
    store.bPut(entries1);
    store.bDelete(keys1);
    List<KVEntry> entries2 = Lists.newArrayList();
    List<byte[]> keys2 = Lists.newArrayList();
    for (int i = 0; i < 10; i++) {
        byte[] key = makeKey("g_batch_del_test_key" + i);
        checkRegion(store, key, 2);
        entries2.add(new KVEntry(key, makeValue("batch_del_test_value" + i)));
        keys2.add(key);
    }
    store.bPut(entries2);
    store.bDelete(keys2);
    List<KVEntry> foundList = store.bScan(makeKey("batch_del_test_key"), makeKey("batch_del_test_key" + 99));
    assertEquals(0, foundList.size());
    for (int i = 0; i < keys1.size(); i++) {
        byte[] value = store.bGet(keys1.get(i));
        assertNull(value);
    }
    foundList = store.bScan(makeKey("g_batch_del_test_key"), makeKey("g_batch_put_test_key" + 99));
    assertEquals(0, foundList.size());
    for (int i = 0; i < keys2.size(); i++) {
        byte[] value = store.bGet(keys2.get(i));
        assertNull(value);
    }
}
Also used : KVEntry(com.alipay.sofa.jraft.rhea.storage.KVEntry)

Example 8 with KVEntry

use of com.alipay.sofa.jraft.rhea.storage.KVEntry in project sofa-jraft by sofastack.

the class AbstractRheaKVStoreTest method deleteRangeTest.

/**
 * Test method: {@link RheaKVStore#deleteRange(byte[], byte[])}
 */
private void deleteRangeTest(RheaKVStore store) {
    // regions: 1 -> [null, g), 2 -> [g, null)
    List<KVEntry> entries = Lists.newArrayList();
    for (int i = 0; i < 10; i++) {
        byte[] key = makeKey("del_range_test" + i);
        checkRegion(store, key, 1);
        byte[] value = makeValue("del_range_test_value" + i);
        entries.add(new KVEntry(key, value));
    }
    for (int i = 0; i < 10; i++) {
        byte[] key = makeKey("t_del_range_test" + i);
        checkRegion(store, key, 2);
        byte[] value = makeValue("del_range_test_value" + i);
        entries.add(new KVEntry(key, value));
    }
    store.bPut(entries);
    // delete [del_range_test5, t_del_range_test8)
    // cover three region: 1 -> [null, g), 2 -> [g, t), 3 -> [t, null)
    store.bDeleteRange(makeKey("del_range_test5"), makeKey("t_del_range_test8"));
    List<KVEntry> entries2 = store.bScan(makeKey("del_range_test"), makeKey("t_del_range_test" + 99));
    assertEquals(entries.size() - 13, entries2.size());
    byte[] value = store.bGet(makeKey("del_range_test5"));
    assertNull(value);
    value = store.bGet(makeKey("del_range_test6"));
    assertNull(value);
    value = store.bGet(makeKey("del_range_test7"));
    assertNull(value);
    value = store.bGet(makeKey("t_del_range_test8"));
    assertNotNull(value);
}
Also used : KVEntry(com.alipay.sofa.jraft.rhea.storage.KVEntry)

Example 9 with KVEntry

use of com.alipay.sofa.jraft.rhea.storage.KVEntry in project sofa-jraft by sofastack.

the class AbstractRheaKVStoreTest method deleteTest.

/**
 * Test method: {@link RheaKVStore#delete(byte[])}
 */
private void deleteTest(RheaKVStore store) {
    // regions: 1 -> [null, g), 2 -> [g, null)
    List<KVEntry> entries = Lists.newArrayList();
    for (int i = 0; i < 10; i++) {
        byte[] key = makeKey("del_test" + i);
        checkRegion(store, key, 1);
        byte[] value = makeValue("del_test_value" + i);
        entries.add(new KVEntry(key, value));
    }
    store.bPut(entries);
    store.bDelete(makeKey("del_test5"));
    List<KVEntry> entries2 = store.bScan(makeKey("del_test"), makeKey("del_test" + 99));
    assertEquals(entries.size() - 1, entries2.size());
    byte[] value = store.bGet(makeKey("del_test5"));
    assertNull(value);
}
Also used : KVEntry(com.alipay.sofa.jraft.rhea.storage.KVEntry)

Example 10 with KVEntry

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

Aggregations

KVEntry (com.alipay.sofa.jraft.rhea.storage.KVEntry)36 List (java.util.List)17 Test (org.junit.Test)14 Status (com.alipay.sofa.jraft.Status)8 Region (com.alipay.sofa.jraft.rhea.metadata.Region)8 KVStoreClosure (com.alipay.sofa.jraft.rhea.storage.KVStoreClosure)8 RawKVStore (com.alipay.sofa.jraft.rhea.storage.RawKVStore)8 CompletableFuture (java.util.concurrent.CompletableFuture)8 RouteTable (com.alipay.sofa.jraft.RouteTable)6 PeerId (com.alipay.sofa.jraft.entity.PeerId)6 DescriberManager (com.alipay.sofa.jraft.rhea.DescriberManager)6 FollowerStateListener (com.alipay.sofa.jraft.rhea.FollowerStateListener)6 JRaftHelper (com.alipay.sofa.jraft.rhea.JRaftHelper)6 LeaderStateListener (com.alipay.sofa.jraft.rhea.LeaderStateListener)6 RegionEngine (com.alipay.sofa.jraft.rhea.RegionEngine)6 StateListener (com.alipay.sofa.jraft.rhea.StateListener)6 StateListenerContainer (com.alipay.sofa.jraft.rhea.StateListenerContainer)6 StoreEngine (com.alipay.sofa.jraft.rhea.StoreEngine)6 FailoverClosure (com.alipay.sofa.jraft.rhea.client.failover.FailoverClosure)6 ListRetryCallable (com.alipay.sofa.jraft.rhea.client.failover.ListRetryCallable)6