use of org.apache.ratis.client.RaftClient in project incubator-ratis by apache.
the class RaftAsyncTests method testAsyncRequestSemaphore.
@Test
public void testAsyncRequestSemaphore() throws InterruptedException, IOException {
LOG.info("Running testAsyncRequestSemaphore");
CLUSTER cluster = getFactory().newCluster(NUM_SERVERS, properties);
Assert.assertNull(cluster.getLeader());
cluster.start();
waitForLeader(cluster);
int numMessages = RaftClientConfigKeys.Async.maxOutstandingRequests(properties);
CompletableFuture[] futures = new CompletableFuture[numMessages + 1];
final RaftTestUtil.SimpleMessage[] messages = RaftTestUtil.SimpleMessage.create(numMessages);
final RaftClient client = cluster.createClient();
// Set blockTransaction flag so that transaction blocks
for (RaftServerProxy server : cluster.getServers()) {
((SimpleStateMachine4Testing) server.getStateMachine()).setBlockTransaction(true);
}
// Send numMessages which are blocked and do not release the client semaphore permits
AtomicInteger blockedRequestsCount = new AtomicInteger();
for (int i = 0; i < numMessages; i++) {
blockedRequestsCount.getAndIncrement();
futures[i] = client.sendAsync(messages[i]);
blockedRequestsCount.decrementAndGet();
}
Assert.assertTrue(blockedRequestsCount.get() == 0);
ExecutorService threadPool = Executors.newFixedThreadPool(1);
futures[numMessages] = CompletableFuture.supplyAsync(() -> {
blockedRequestsCount.incrementAndGet();
client.sendAsync(new RaftTestUtil.SimpleMessage("n1"));
blockedRequestsCount.decrementAndGet();
return null;
}, threadPool);
// Allow the last msg to be sent
while (blockedRequestsCount.get() != 1) {
Thread.sleep(1000);
}
Assert.assertTrue(blockedRequestsCount.get() == 1);
// Since all semaphore permits are acquired the last message sent is in queue
RaftClientTestUtil.assertAsyncRequestSemaphore(client, 0, 1);
// Unset the blockTransaction flag so that semaphore permits can be released
for (RaftServerProxy server : cluster.getServers()) {
((SimpleStateMachine4Testing) server.getStateMachine()).setBlockTransaction(false);
}
for (int i = 0; i <= numMessages; i++) {
futures[i].join();
}
Assert.assertTrue(blockedRequestsCount.get() == 0);
cluster.shutdown();
}
use of org.apache.ratis.client.RaftClient in project incubator-ratis by apache.
the class RaftExceptionBaseTest method testGroupMismatchException.
@Test
public void testGroupMismatchException() throws Exception {
final RaftGroup clusterGroup = cluster.getGroup();
Assert.assertEquals(NUM_PEERS, clusterGroup.getPeers().size());
final RaftGroup anotherGroup = new RaftGroup(RaftGroupId.randomId(), clusterGroup.getPeers());
Assert.assertNotEquals(clusterGroup.getGroupId(), anotherGroup.getGroupId());
// Create client using another group
try (RaftClient client = cluster.createClient(anotherGroup)) {
testFailureCase("send(..) with client group being different from the server group", () -> client.send(Message.EMPTY), GroupMismatchException.class);
testFailureCase("sendReadOnly(..) with client group being different from the server group", () -> client.sendReadOnly(Message.EMPTY), GroupMismatchException.class);
testFailureCase("setConfiguration(..) with client group being different from the server group", () -> client.setConfiguration(RaftPeer.emptyArray()), GroupMismatchException.class);
testFailureCase("reinitialize(..) with client group being different from the server group", () -> client.reinitialize(anotherGroup, clusterGroup.getPeers().iterator().next().getId()), GroupMismatchException.class);
}
}
use of org.apache.ratis.client.RaftClient in project incubator-ratis by apache.
the class RaftExceptionBaseTest method testHandleNotLeaderException.
private void testHandleNotLeaderException(boolean killNewLeader) throws Exception {
RaftTestUtil.waitForLeader(cluster);
final RaftPeerId leaderId = cluster.getLeader().getId();
final RaftClient client = cluster.createClient(leaderId);
RaftClientReply reply = client.send(new SimpleMessage("m1"));
Assert.assertTrue(reply.isSuccess());
// enforce leader change
RaftPeerId newLeader = RaftTestUtil.changeLeader(cluster, leaderId);
if (killNewLeader) {
// kill the new leader
cluster.killServer(newLeader);
}
RaftClientRpc rpc = client.getClientRpc();
reply = null;
for (int i = 0; reply == null && i < 10; i++) {
try {
reply = rpc.sendRequest(cluster.newRaftClientRequest(ClientId.randomId(), leaderId, new SimpleMessage("m2")));
} catch (IOException ignored) {
Thread.sleep(1000);
}
}
Assert.assertNotNull(reply);
Assert.assertFalse(reply.isSuccess());
final NotLeaderException nle = reply.getNotLeaderException();
Objects.requireNonNull(nle);
Assert.assertEquals(newLeader, nle.getSuggestedLeader().getId());
reply = client.send(new SimpleMessage("m3"));
Assert.assertTrue(reply.isSuccess());
client.close();
}
use of org.apache.ratis.client.RaftClient in project incubator-ratis by apache.
the class TestRaftLogMetrics method testFlushMetric.
@Test
public void testFlushMetric() throws Exception {
int numMsg = 2;
final RaftTestUtil.SimpleMessage[] messages = RaftTestUtil.SimpleMessage.create(numMsg);
try (final RaftClient client = cluster.createClient()) {
for (RaftTestUtil.SimpleMessage message : messages) {
client.send(message);
}
}
for (RaftServerProxy rsp : cluster.getServers()) {
String flushTimeMetric = getLogFlushTimeMetric(rsp.getId().toString());
Timer tm = RatisMetricsRegistry.getRegistry().getTimers().get(flushTimeMetric);
Assert.assertNotNull(tm);
// Number of log entries expected = numMsg + 1 entry for start-log-segment
int numExpectedLogEntries = numMsg + 1;
Assert.assertEquals(numExpectedLogEntries, tm.getCount());
Assert.assertTrue(tm.getMeanRate() > 0);
// Test jmx
ObjectName oname = new ObjectName("metrics", "name", flushTimeMetric);
Assert.assertEquals(numExpectedLogEntries, ((Long) ManagementFactory.getPlatformMBeanServer().getAttribute(oname, "Count")).intValue());
}
}
use of org.apache.ratis.client.RaftClient in project incubator-ratis by apache.
the class TestRestartRaftPeer method restartFollower.
@Test
public void restartFollower() throws Exception {
cluster.start();
RaftTestUtil.waitForLeader(cluster);
final RaftPeerId leaderId = cluster.getLeader().getId();
final RaftClient client = cluster.createClient(leaderId);
// write some messages
final byte[] content = new byte[1024];
Arrays.fill(content, (byte) 1);
final SimpleMessage message = new SimpleMessage(new String(content));
for (int i = 0; i < 10; i++) {
Assert.assertTrue(client.send(message).isSuccess());
}
// restart a follower
RaftPeerId followerId = cluster.getFollowers().get(0).getId();
LOG.info("Restart follower {}", followerId);
cluster.restartServer(followerId, false);
// write some more messages
for (int i = 0; i < 10; i++) {
Assert.assertTrue(client.send(message).isSuccess());
}
client.close();
// make sure the restarted follower can catchup
boolean catchup = false;
long lastAppliedIndex = 0;
for (int i = 0; i < 10 && !catchup; i++) {
Thread.sleep(500);
lastAppliedIndex = cluster.getServer(followerId).getImpl().getState().getLastAppliedIndex();
catchup = lastAppliedIndex >= 20;
}
Assert.assertTrue("lastAppliedIndex=" + lastAppliedIndex, catchup);
// make sure the restarted peer's log segments is correct
cluster.restartServer(followerId, false);
Assert.assertTrue(cluster.getServer(followerId).getImpl().getState().getLog().getLastEntryTermIndex().getIndex() >= 20);
}
Aggregations