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);
}
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();
}
}
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"))));
}
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();
}
}
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();
}
}
Aggregations