use of com.alipay.sofa.jraft.rhea.client.FutureGroup in project sofa-jraft by sofastack.
the class AbstractChaosTest method chaosGetTest.
@Test
public void chaosGetTest() throws Exception {
ChaosTestCluster cluster = null;
PeerId p1 = null;
PeerId p2 = null;
for (int l = 0; l < RETRIES; l++) {
final ExecutorService executor = Executors.newCachedThreadPool(new NamedThreadFactory("chaos-test", true));
final List<CompletableFuture<Boolean>> allFutures = new CopyOnWriteArrayList<>();
try {
cluster = new ChaosTestCluster(TestUtil.generatePeers(INITIAL_PEER_COUNT), getStorageType(), isAllowBatching(), isOnlyLeaderRead());
cluster.start();
// Before writing data, remove a node (node1) and add it back later to verify that read consistency is guaranteed.
p1 = cluster.getRandomPeer();
cluster.removePeer(p1);
final RheaKVStore store = cluster.getLeaderStore();
// warm up
store.bGet("test_key");
for (int i = 0; i < LOOP_1; i++) {
final int index = i;
executor.execute(() -> {
for (int j = 0; j < LOOP_2; j++) {
allFutures.add(store.put("test_" + index + "_" + j, VALUE));
}
});
}
// In the process of writing data, remove one node (node2)
p2 = cluster.getRandomPeer();
cluster.removePeer(p2);
// Waiting for the write to be completed
CompletableFuture.allOf(allFutures.toArray(new CompletableFuture[0])).get(30, TimeUnit.SECONDS);
break;
} catch (final Exception e) {
System.err.println("Fail to put data, try again...");
e.printStackTrace();
new FutureGroup<>(allFutures).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 {
chaosGetCheckData(cluster, p2, p1);
} finally {
cluster.stopAll();
}
}
Aggregations