Search in sources :

Example 1 with ByteArray

use of com.alipay.sofa.jraft.rhea.util.ByteArray in project sofa-jraft by sofastack.

the class DefaultMetadataStore method getClusterInfo.

@Override
public Cluster getClusterInfo(final long clusterId) {
    final Set<Long> storeIds = getClusterIndex(clusterId);
    if (storeIds == null) {
        return null;
    }
    final List<byte[]> storeKeys = Lists.newArrayList();
    for (final Long storeId : storeIds) {
        final String storeInfoKey = MetadataKeyHelper.getStoreInfoKey(clusterId, storeId);
        storeKeys.add(BytesUtil.writeUtf8(storeInfoKey));
    }
    final Map<ByteArray, byte[]> storeInfoBytes = this.rheaKVStore.bMultiGet(storeKeys);
    final List<Store> stores = Lists.newArrayListWithCapacity(storeInfoBytes.size());
    for (final byte[] storeBytes : storeInfoBytes.values()) {
        final Store store = this.serializer.readObject(storeBytes, Store.class);
        stores.add(store);
    }
    return new Cluster(clusterId, stores);
}
Also used : ByteArray(com.alipay.sofa.jraft.rhea.util.ByteArray) RheaKVStore(com.alipay.sofa.jraft.rhea.client.RheaKVStore) Store(com.alipay.sofa.jraft.rhea.metadata.Store) Cluster(com.alipay.sofa.jraft.rhea.metadata.Cluster)

Example 2 with ByteArray

use of com.alipay.sofa.jraft.rhea.util.ByteArray in project sofa-jraft by sofastack.

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(com.alipay.sofa.jraft.rhea.util.ByteArray)

Example 3 with ByteArray

use of com.alipay.sofa.jraft.rhea.util.ByteArray 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"))));
}
Also used : ArrayList(java.util.ArrayList) ByteArray(com.alipay.sofa.jraft.rhea.util.ByteArray) CASEntry(com.alipay.sofa.jraft.rhea.storage.CASEntry)

Example 4 with ByteArray

use of com.alipay.sofa.jraft.rhea.util.ByteArray in project sofa-jraft by sofastack.

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(com.alipay.sofa.jraft.rhea.util.ByteArray) Map(java.util.Map) EnumMap(java.util.EnumMap) RocksDBException(org.rocksdb.RocksDBException) StorageException(com.alipay.sofa.jraft.rhea.errors.StorageException) IOException(java.io.IOException) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) DistributedLock(com.alipay.sofa.jraft.rhea.util.concurrent.DistributedLock) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Lock(java.util.concurrent.locks.Lock)

Example 5 with ByteArray

use of com.alipay.sofa.jraft.rhea.util.ByteArray in project sofa-jraft by sofastack.

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(com.alipay.sofa.jraft.rhea.util.concurrent.DistributedLock) Timer(com.codahale.metrics.Timer) ByteArray(com.alipay.sofa.jraft.rhea.util.ByteArray)

Aggregations

ByteArray (com.alipay.sofa.jraft.rhea.util.ByteArray)9 DistributedLock (com.alipay.sofa.jraft.rhea.util.concurrent.DistributedLock)5 Timer (com.codahale.metrics.Timer)4 Map (java.util.Map)4 CASEntry (com.alipay.sofa.jraft.rhea.storage.CASEntry)3 Endpoint (com.alipay.sofa.jraft.util.Endpoint)3 RouteTable (com.alipay.sofa.jraft.RouteTable)2 Status (com.alipay.sofa.jraft.Status)2 PeerId (com.alipay.sofa.jraft.entity.PeerId)2 DescriberManager (com.alipay.sofa.jraft.rhea.DescriberManager)2 FollowerStateListener (com.alipay.sofa.jraft.rhea.FollowerStateListener)2 JRaftHelper (com.alipay.sofa.jraft.rhea.JRaftHelper)2 LeaderStateListener (com.alipay.sofa.jraft.rhea.LeaderStateListener)2 RegionEngine (com.alipay.sofa.jraft.rhea.RegionEngine)2 StateListener (com.alipay.sofa.jraft.rhea.StateListener)2 StateListenerContainer (com.alipay.sofa.jraft.rhea.StateListenerContainer)2 StoreEngine (com.alipay.sofa.jraft.rhea.StoreEngine)2 FailoverClosure (com.alipay.sofa.jraft.rhea.client.failover.FailoverClosure)2 ListRetryCallable (com.alipay.sofa.jraft.rhea.client.failover.ListRetryCallable)2 RetryCallable (com.alipay.sofa.jraft.rhea.client.failover.RetryCallable)2