use of com.alipay.sofa.jraft.Status in project sofa-jraft by sofastack.
the class RocksKVStoreTest method getAndPutTest.
/**
* Test method: {@link RocksRawKVStore#getAndPut(byte[], byte[], KVStoreClosure)}
*/
@Test
public void getAndPutTest() {
final byte[] key = makeKey("put_test");
TestClosure closure = new TestClosure();
this.kvStore.get(key, closure);
byte[] value = (byte[]) closure.getData();
assertNull(value);
value = makeValue("put_test_value");
KVStoreClosure kvStoreClosure = new BaseKVStoreClosure() {
@Override
public void run(Status status) {
assertEquals(status, Status.OK());
}
};
this.kvStore.getAndPut(key, value, kvStoreClosure);
assertNull(kvStoreClosure.getData());
byte[] newVal = makeValue("put_test_value_new");
this.kvStore.getAndPut(key, newVal, kvStoreClosure);
assertArrayEquals(value, (byte[]) kvStoreClosure.getData());
}
use of com.alipay.sofa.jraft.Status in project sofa-jraft by sofastack.
the class AbstractChaosTest method chaosSplittingTest.
@Test
public void chaosSplittingTest() {
final List<PeerId> peerIds = TestUtil.generatePeers(INITIAL_PEER_COUNT);
final CliOptions opts = new CliOptions();
opts.setTimeoutMs(30000);
final RheaKVCliService cliService = RheaKVServiceFactory.createAndInitRheaKVCliService(opts);
final long regionId = Constants.DEFAULT_REGION_ID;
final long newRegionId = 2;
final String groupId = JRaftHelper.getJRaftGroupId(ChaosTestCluster.CLUSTER_NAME, regionId);
final Configuration conf = new Configuration(peerIds);
ChaosTestCluster cluster = null;
for (int l = 0; l < RETRIES; l++) {
final ExecutorService executor = Executors.newCachedThreadPool(new NamedThreadFactory("chaos-splitting-test", true));
final List<Future<?>> allFutures = new CopyOnWriteArrayList<>();
try {
cluster = new ChaosTestCluster(peerIds, getStorageType(), isAllowBatching(), isOnlyLeaderRead());
cluster.start();
final RheaKVStore store = cluster.getLeaderStore();
// for least keys on split
for (int j = 0; j < LOOP_2; j++) {
store.bPut(j + "_split_", VALUE);
}
for (int i = 0; i < LOOP_1; i++) {
final int index = i;
final Future<?> f = executor.submit(() -> {
for (int j = 0; j < LOOP_2; j++) {
store.bPut(index + "_split_test_" + j, VALUE);
}
});
allFutures.add(f);
}
final Status st = cliService.rangeSplit(regionId, newRegionId, groupId, conf);
if (!st.isOk()) {
System.err.println("Status:" + st);
throw new RuntimeException(st.toString());
}
// wait for all writes finished
for (final Future<?> f : allFutures) {
f.get(30, TimeUnit.SECONDS);
}
break;
} catch (final Exception e) {
System.err.println("Fail to put data, try again...");
e.printStackTrace();
for (final Future<?> f : allFutures) {
f.cancel(true);
}
if (cluster != null) {
cluster.stopAll();
}
cluster = null;
} finally {
ExecutorServiceHelper.shutdownAndAwaitTermination(executor);
}
}
if (cluster == null) {
throw new RuntimeException("fail to put data, can not check data");
}
try {
chaosSplittingCheckData(cluster);
} finally {
cluster.stopAll();
}
}
use of com.alipay.sofa.jraft.Status in project sofa-jraft by sofastack.
the class LocalSnapshotCopierTest method testCancelByRemote.
@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testCancelByRemote() throws Exception {
final FutureImpl<Message> future = new FutureImpl<>();
final RpcRequests.GetFileRequest.Builder rb = RpcRequests.GetFileRequest.newBuilder().setReaderId(99).setFilename(Snapshot.JRAFT_SNAPSHOT_META_FILE).setCount(Integer.MAX_VALUE).setOffset(0).setReadPartly(true);
// mock get metadata
final ArgumentCaptor<RpcResponseClosure> argument = ArgumentCaptor.forClass(RpcResponseClosure.class);
Mockito.when(this.raftClientService.getFile(eq(new Endpoint("localhost", 8081)), eq(rb.build()), eq(this.copyOpts.getTimeoutMs()), argument.capture())).thenReturn(future);
this.copier.start();
Thread.sleep(500);
final RpcResponseClosure<RpcRequests.GetFileResponse> closure = argument.getValue();
closure.run(new Status(RaftError.ECANCELED, "test cancel"));
this.copier.join();
// start timer
final SnapshotReader reader = this.copier.getReader();
assertNull(reader);
Assert.assertEquals(RaftError.ECANCELED.getNumber(), this.copier.getCode());
Assert.assertEquals("test cancel", this.copier.getErrorMsg());
}
use of com.alipay.sofa.jraft.Status in project sofa-jraft by sofastack.
the class CopySessionTest method testOnRpcReturnedRetry.
@Test
public void testOnRpcReturnedRetry() throws Exception {
assertNull(this.session.getTimer());
assertNull(this.session.getRpcCall());
final ByteBufferCollector bufRef = ByteBufferCollector.allocate(0);
this.session.setDestBuf(bufRef);
final FutureImpl<Message> future = new FutureImpl<>();
final RpcRequests.GetFileRequest.Builder rb = RpcRequests.GetFileRequest.newBuilder().setReaderId(99).setFilename("data").setCount(Integer.MAX_VALUE).setOffset(0).setReadPartly(true);
Mockito.when(this.rpcService.getFile(this.address, rb.build(), this.copyOpts.getTimeoutMs(), session.getDone())).thenReturn(future);
this.session.onRpcReturned(new Status(RaftError.EINTR, "test"), null);
assertNotNull(this.session.getTimer());
Thread.sleep(this.copyOpts.getRetryIntervalMs() + 100);
assertNotNull(this.session.getRpcCall());
assertSame(future, this.session.getRpcCall());
assertNull(this.session.getTimer());
}
use of com.alipay.sofa.jraft.Status in project sofa-jraft by sofastack.
the class LogManagerTest method testAppendEntriesBeforeAppliedIndex.
@Test
public void testAppendEntriesBeforeAppliedIndex() throws Exception {
// Append 0-10
List<LogEntry> mockEntries = TestUtils.mockEntries(10);
for (int i = 0; i < 10; i++) {
mockEntries.get(i).getId().setTerm(1);
}
final CountDownLatch latch1 = new CountDownLatch(1);
this.logManager.appendEntries(new ArrayList<>(mockEntries), new LogManager.StableClosure() {
@Override
public void run(final Status status) {
assertTrue(status.isOk());
latch1.countDown();
}
});
latch1.await();
assertEquals(1, this.logManager.getFirstLogIndex());
assertEquals(10, this.logManager.getLastLogIndex());
// waiting for setDiskId()
Thread.sleep(200);
this.logManager.setAppliedId(new LogId(9, 1));
for (int i = 0; i < 10; i++) {
assertNull(this.logManager.getEntryFromMemory(i));
}
// append 1-10 again, already applied, returns OK.
final CountDownLatch latch2 = new CountDownLatch(1);
mockEntries = TestUtils.mockEntries(10);
mockEntries.remove(0);
this.logManager.appendEntries(new ArrayList<>(mockEntries), new LogManager.StableClosure() {
@Override
public void run(final Status status) {
assertTrue(status.isOk());
latch2.countDown();
}
});
latch2.await();
assertEquals(1, this.logManager.getFirstLogIndex());
assertEquals(10, this.logManager.getLastLogIndex());
}
Aggregations