use of org.apache.ratis.protocol.RaftClientReply in project incubator-ratis by apache.
the class TestGrpcMessageMetrics method sendMessages.
static void sendMessages(MiniRaftCluster cluster) throws Exception {
waitForLeader(cluster);
try (final RaftClient client = cluster.createClient()) {
CompletableFuture<RaftClientReply> replyFuture = client.async().send(new RaftTestUtil.SimpleMessage("abc"));
}
// Wait for commits to happen on leader
JavaUtils.attempt(() -> assertMessageCount(cluster.getLeader()), 100, HUNDRED_MILLIS, cluster.getLeader().getId() + "-assertMessageCount", null);
}
use of org.apache.ratis.protocol.RaftClientReply in project incubator-ratis by apache.
the class LeaderElectionTests method testTransferLeader.
@Test
public void testTransferLeader() throws Exception {
try (final MiniRaftCluster cluster = newCluster(3)) {
cluster.start();
final RaftServer.Division leader = waitForLeader(cluster);
try (RaftClient client = cluster.createClient(leader.getId())) {
client.io().send(new RaftTestUtil.SimpleMessage("message"));
List<RaftServer.Division> followers = cluster.getFollowers();
Assert.assertEquals(followers.size(), 2);
RaftServer.Division newLeader = followers.get(0);
List<RaftPeer> peers = cluster.getPeers();
List<RaftPeer> peersWithNewPriority = getPeersWithPriority(peers, newLeader.getPeer());
RaftClientReply reply = client.admin().setConfiguration(peersWithNewPriority.toArray(new RaftPeer[0]));
Assert.assertTrue(reply.isSuccess());
reply = client.admin().transferLeadership(newLeader.getId(), 20000);
assertTrue(reply.isSuccess());
final RaftServer.Division currLeader = waitForLeader(cluster);
assertTrue(newLeader.getId() == currLeader.getId());
reply = client.io().send(new RaftTestUtil.SimpleMessage("message"));
Assert.assertTrue(reply.getReplierId().equals(newLeader.getId().toString()));
Assert.assertTrue(reply.isSuccess());
}
cluster.shutdown();
}
}
use of org.apache.ratis.protocol.RaftClientReply in project incubator-ratis by apache.
the class LeaderElectionTests method runTestPauseResumeLeaderElection.
void runTestPauseResumeLeaderElection(CLUSTER cluster) throws IOException, InterruptedException {
final RaftClientReply pauseLeaderReply;
final RaftServer.Division leader = RaftTestUtil.waitForLeader(cluster);
final RaftPeerId leaderId = leader.getId();
final List<RaftServer.Division> followers = cluster.getFollowers();
Assert.assertTrue(followers.size() >= 1);
final RaftServerImpl f1 = (RaftServerImpl) followers.get(0);
try (final RaftClient client = cluster.createClient()) {
pauseLeaderReply = client.getLeaderElectionManagementApi(f1.getId()).pause();
Assert.assertTrue(pauseLeaderReply.isSuccess());
client.io().send(new RaftTestUtil.SimpleMessage("message"));
RaftServer.Division newLeader = followers.get(0);
List<RaftPeer> peers = cluster.getPeers();
List<RaftPeer> peersWithNewPriority = getPeersWithPriority(peers, newLeader.getPeer());
RaftClientReply reply = client.admin().setConfiguration(peersWithNewPriority.toArray(new RaftPeer[0]));
Assert.assertTrue(reply.isSuccess());
JavaUtils.attempt(() -> Assert.assertEquals(leaderId, leader.getId()), 20, HUNDRED_MILLIS, "check leader id", LOG);
final RaftClientReply resumeLeaderReply = client.getLeaderElectionManagementApi(f1.getId()).resume();
Assert.assertTrue(resumeLeaderReply.isSuccess());
JavaUtils.attempt(() -> Assert.assertEquals(f1.getId(), cluster.getLeader().getId()), 20, HUNDRED_MILLIS, "check new leader", LOG);
}
}
use of org.apache.ratis.protocol.RaftClientReply in project incubator-ratis by apache.
the class LeaderElectionTests method testPreVote.
@Test
public void testPreVote() {
try (final MiniRaftCluster cluster = newCluster(3)) {
cluster.start();
RaftServer.Division leader = waitForLeader(cluster);
try (RaftClient client = cluster.createClient(leader.getId())) {
client.io().send(new RaftTestUtil.SimpleMessage("message"));
final List<RaftServer.Division> followers = cluster.getFollowers();
assertEquals(followers.size(), 2);
RaftServer.Division follower = followers.get(0);
isolate(cluster, follower.getId());
// send message so that the isolated follower's log lag the others
RaftClientReply reply = client.io().send(new RaftTestUtil.SimpleMessage("message"));
Assert.assertTrue(reply.isSuccess());
final long savedTerm = leader.getInfo().getCurrentTerm();
LOG.info("Wait follower {} timeout and trigger pre-vote", follower.getId());
Thread.sleep(2000);
deIsolate(cluster, follower.getId());
Thread.sleep(2000);
// with pre-vote leader will not step down
RaftServer.Division newleader = waitForLeader(cluster);
assertNotNull(newleader);
assertEquals(newleader.getId(), leader.getId());
// with pre-vote, term will not change
assertEquals(savedTerm, leader.getInfo().getCurrentTerm());
reply = client.io().send(new RaftTestUtil.SimpleMessage("message"));
Assert.assertTrue(reply.isSuccess());
}
cluster.shutdown();
} catch (Exception e) {
fail(e.getMessage());
}
}
use of org.apache.ratis.protocol.RaftClientReply in project incubator-ratis by apache.
the class LeaderElectionTests method testDisconnectLeader.
protected void testDisconnectLeader() throws Exception {
try (final MiniRaftCluster cluster = newCluster(3)) {
cluster.start();
final RaftServer.Division leader = waitForLeader(cluster);
try (RaftClient client = cluster.createClient(leader.getId())) {
client.io().send(new RaftTestUtil.SimpleMessage("message"));
Thread.sleep(1000);
isolate(cluster, leader.getId());
RaftClientReply reply = client.io().send(new RaftTestUtil.SimpleMessage("message"));
Assert.assertNotEquals(reply.getReplierId(), leader.getId().toString());
Assert.assertTrue(reply.isSuccess());
} finally {
deIsolate(cluster, leader.getId());
}
cluster.shutdown();
}
}
Aggregations