use of com.alipay.sofa.jraft.Closure in project sofa-jraft by sofastack.
the class UtilsTest method testRunClosureWithStatus.
@Test
public void testRunClosureWithStatus() throws Exception {
CountDownLatch latch = new CountDownLatch(1);
Utils.runClosureInThread(new Closure() {
@Override
public void run(Status status) {
assertFalse(status.isOk());
Assert.assertEquals(RaftError.EACCES.getNumber(), status.getCode());
assertEquals("test 99", status.getErrorMsg());
latch.countDown();
}
}, new Status(RaftError.EACCES, "test %d", 99));
latch.await();
}
use of com.alipay.sofa.jraft.Closure in project sofa-jraft by sofastack.
the class RocksKVStoreTest method snapshotTest.
public void snapshotTest() throws Exception {
final File backupDir = new File("backup");
if (backupDir.exists()) {
FileUtils.deleteDirectory(backupDir);
}
FileUtils.forceMkdir(backupDir);
for (int i = 0; i < 100000; i++) {
final String v = String.valueOf(i);
this.kvStore.put(makeKey(v), makeValue(v), null);
}
final Region region = new Region();
KVStoreSnapshotFile kvStoreSnapshotFile = KVStoreSnapshotFileFactory.getKVStoreSnapshotFile(this.kvStore);
final ExecutorService snapshotPool = StoreEngineHelper.createSnapshotExecutor(1, 2);
final TestSnapshotWriter snapshotWriter = new TestSnapshotWriter(backupDir.getAbsolutePath());
final CountDownLatch latch = new CountDownLatch(1);
final Closure done = status -> {
assertTrue(status.isOk());
latch.countDown();
};
kvStoreSnapshotFile.save(snapshotWriter, region, done, snapshotPool);
latch.await();
final LocalFileMeta meta = (LocalFileMeta) snapshotWriter.getFileMeta(SNAPSHOT_ARCHIVE);
assertNotNull(meta);
assertNotNull(get(makeKey("1")));
this.kvStore.put(makeKey("100001"), makeValue("100001"), null);
assertNotNull(get(makeKey("100001")));
this.kvStore.shutdown();
FileUtils.deleteDirectory(new File(this.tempPath));
FileUtils.forceMkdir(new File(this.tempPath));
this.kvStore = new RocksRawKVStore();
this.kvStore.init(this.dbOptions);
assertNull(get(makeKey("1")));
final TestSnapshotReader snapshotReader = new TestSnapshotReader(snapshotWriter.metaTable, backupDir.getAbsolutePath());
kvStoreSnapshotFile = KVStoreSnapshotFileFactory.getKVStoreSnapshotFile(this.kvStore);
final boolean ret = kvStoreSnapshotFile.load(snapshotReader, region);
assertTrue(ret);
for (int i = 0; i < 100000; i++) {
final String v = String.valueOf(i);
assertArrayEquals(makeValue(v), get(makeKey(v)));
}
// key[100001] is put after the snapshot, so key[100001] should not exist.
assertNull(get(makeKey("100001")));
FileUtils.deleteDirectory(backupDir);
ExecutorServiceHelper.shutdownAndAwaitTermination(snapshotPool);
}
use of com.alipay.sofa.jraft.Closure in project sofa-jraft by sofastack.
the class AtomicStateMachine method onApply.
@Override
public void onApply(final Iterator iter) {
while (iter.hasNext()) {
final Closure done = iter.done();
CommandType cmdType;
final ByteBuffer data = iter.getData();
Object cmd = null;
LeaderTaskClosure closure = null;
if (done != null) {
closure = (LeaderTaskClosure) done;
cmdType = closure.getCmdType();
cmd = closure.getCmd();
} else {
final byte b = data.get();
final byte[] cmdBytes = new byte[data.remaining()];
data.get(cmdBytes);
cmdType = CommandType.parseByte(b);
switch(cmdType) {
case GET:
cmd = CommandCodec.decodeCommand(cmdBytes, GetCommand.class);
break;
case SET:
cmd = CommandCodec.decodeCommand(cmdBytes, SetCommand.class);
break;
case CAS:
cmd = CommandCodec.decodeCommand(cmdBytes, CompareAndSetCommand.class);
break;
case INC:
cmd = CommandCodec.decodeCommand(cmdBytes, IncrementAndGetCommand.class);
break;
}
}
final String key = ((BaseRequestCommand) cmd).getKey();
final AtomicLong counter = getCounter(key, true);
Object response = null;
switch(cmdType) {
case GET:
response = new ValueCommand(counter.get());
break;
case SET:
final SetCommand setCmd = (SetCommand) cmd;
counter.set(setCmd.getValue());
response = new BooleanCommand(true);
break;
case CAS:
final CompareAndSetCommand casCmd = (CompareAndSetCommand) cmd;
response = new BooleanCommand(counter.compareAndSet(casCmd.getExpect(), casCmd.getNewValue()));
break;
case INC:
final IncrementAndGetCommand incCmd = (IncrementAndGetCommand) cmd;
final long ret = counter.addAndGet(incCmd.getDetal());
response = new ValueCommand(ret);
break;
}
if (closure != null) {
closure.setResponse(response);
closure.run(Status.OK());
}
iter.next();
}
}
use of com.alipay.sofa.jraft.Closure in project sofa-jraft by sofastack.
the class NodeImpl method afterShutdown.
private void afterShutdown() {
List<Closure> savedDoneList = null;
this.writeLock.lock();
try {
if (!this.shutdownContinuations.isEmpty()) {
savedDoneList = new ArrayList<>(this.shutdownContinuations);
}
if (this.logStorage != null) {
this.logStorage.shutdown();
}
this.state = State.STATE_SHUTDOWN;
} finally {
this.writeLock.unlock();
}
if (savedDoneList != null) {
for (final Closure closure : savedDoneList) {
Utils.runClosureInThread(closure);
}
}
}
use of com.alipay.sofa.jraft.Closure in project sofa-jraft by sofastack.
the class ClosureQueueImpl method clear.
@Override
public void clear() {
List<Closure> savedQueue;
this.lock.lock();
try {
this.firstIndex = 0;
savedQueue = this.queue;
this.queue = new LinkedList<>();
} finally {
this.lock.unlock();
}
final Status status = new Status(RaftError.EPERM, "Leader stepped down");
Utils.runInThread(() -> {
for (final Closure done : savedQueue) {
if (done != null) {
done.run(status);
}
}
});
}
Aggregations