use of org.apache.ratis.protocol.RaftPeerId in project incubator-ratis by apache.
the class LeaderElectionTests method testLateServerStart.
@Test
public void testLateServerStart() throws Exception {
final int numServer = 3;
LOG.info("Running testLateServerStart");
final MiniRaftCluster cluster = newCluster(numServer);
cluster.initServers();
// start all except one servers
final Iterator<RaftServer> i = cluster.getServers().iterator();
for (int j = 1; j < numServer; j++) {
i.next().start();
}
final RaftServer.Division leader = waitForLeader(cluster);
final TimeDuration sleepTime = TimeDuration.valueOf(3, TimeUnit.SECONDS);
LOG.info("sleep " + sleepTime);
sleepTime.sleep();
// start the last server
final RaftServerProxy lastServer = (RaftServerProxy) i.next();
lastServer.start();
final RaftPeerId lastServerLeaderId = JavaUtils.attemptRepeatedly(() -> Optional.ofNullable(lastServer.getImpls().iterator().next().getState().getLeaderId()).orElseThrow(() -> new IllegalStateException("No leader yet")), 10, ONE_SECOND, "getLeaderId", LOG);
LOG.info(cluster.printServers());
Assert.assertEquals(leader.getId(), lastServerLeaderId);
}
use of org.apache.ratis.protocol.RaftPeerId 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.RaftPeerId in project incubator-ratis by apache.
the class PreAppendLeaderStepDownTest method runTestLeaderStepDownAsync.
void runTestLeaderStepDownAsync(CLUSTER cluster) throws IOException, InterruptedException {
RaftServer.Division leader = RaftTestUtil.waitForLeader(cluster);
RaftPeerId leaderId = leader.getId();
RaftServerImpl l = (RaftServerImpl) leader;
try (RaftClient client = cluster.createClient(leader.getId())) {
JavaUtils.attempt(() -> Assert.assertEquals(leaderId, leader.getId()), 20, ONE_SECOND, "check leader id", LOG);
RaftClientReply reply = client.admin().transferLeadership(null, 3000);
Assert.assertTrue(reply.isSuccess());
Assert.assertEquals(2, ((RaftServerImpl) leader).getRole().getCurrentRole().getNumber());
}
}
use of org.apache.ratis.protocol.RaftPeerId in project incubator-ratis by apache.
the class GroupManagementBaseTest method testGroupWithPriority.
@Test
public void testGroupWithPriority() throws Exception {
final MiniRaftCluster cluster = getCluster(0);
LOG.info("Start testMultiGroup" + cluster.printServers());
// Start server with null group
final List<RaftPeerId> ids = Arrays.stream(MiniRaftCluster.generateIds(3, 0)).map(RaftPeerId::valueOf).collect(Collectors.toList());
ids.forEach(id -> cluster.putNewServer(id, null, true));
LOG.info("putNewServer: " + cluster.printServers());
cluster.start();
// Make sure that there are no leaders.
TimeUnit.SECONDS.sleep(1);
LOG.info("start: " + cluster.printServers());
Assert.assertNull(cluster.getLeader());
// Add groups
List<RaftPeer> peers = cluster.getPeers();
Random r = new Random(1);
final int suggestedLeaderIndex = r.nextInt(peers.size());
List<RaftPeer> peersWithPriority = getPeersWithPriority(peers, peers.get(suggestedLeaderIndex));
final RaftGroup newGroup = RaftGroup.valueOf(RaftGroupId.randomId(), peersWithPriority);
LOG.info("add new group: " + newGroup);
try (final RaftClient client = cluster.createClient(newGroup)) {
// Before request, client try leader with the highest priority
Assert.assertTrue(client.getLeaderId() == peersWithPriority.get(suggestedLeaderIndex).getId());
for (RaftPeer p : newGroup.getPeers()) {
client.getGroupManagementApi(p.getId()).add(newGroup);
}
}
JavaUtils.attempt(() -> {
final RaftServer.Division leader = RaftTestUtil.waitForLeader(cluster, newGroup.getGroupId());
Assert.assertTrue(leader.getId() == peers.get(suggestedLeaderIndex).getId());
}, 10, TimeDuration.valueOf(1, TimeUnit.SECONDS), "testMultiGroupWithPriority", LOG);
String suggestedLeader = peers.get(suggestedLeaderIndex).getId().toString();
// isolate leader, then follower will trigger leader election.
// Because leader was isolated, so leader can not vote, and candidate wait timeout,
// then if candidate get majority, candidate can pass vote
BlockRequestHandlingInjection.getInstance().blockRequestor(suggestedLeader);
BlockRequestHandlingInjection.getInstance().blockReplier(suggestedLeader);
cluster.setBlockRequestsFrom(suggestedLeader, true);
JavaUtils.attempt(() -> {
final RaftServer.Division leader = RaftTestUtil.waitForLeader(cluster, newGroup.getGroupId());
Assert.assertTrue(leader.getId() != peers.get(suggestedLeaderIndex).getId());
}, 10, TimeDuration.valueOf(1, TimeUnit.SECONDS), "testMultiGroupWithPriority", LOG);
// when suggested leader rejoin cluster, it will catch up log first.
try (final RaftClient client = cluster.createClient(newGroup)) {
for (int i = 0; i < 10; i++) {
RaftClientReply reply = client.io().send(new RaftTestUtil.SimpleMessage("m" + i));
Assert.assertTrue(reply.isSuccess());
}
}
BlockRequestHandlingInjection.getInstance().unblockRequestor(suggestedLeader);
BlockRequestHandlingInjection.getInstance().unblockReplier(suggestedLeader);
cluster.setBlockRequestsFrom(suggestedLeader, false);
// suggested leader with highest priority rejoin cluster, then current leader will yield
// leadership to suggested leader when suggested leader catch up the log.
JavaUtils.attempt(() -> {
final RaftServer.Division leader = RaftTestUtil.waitForLeader(cluster, newGroup.getGroupId());
Assert.assertTrue(leader.getId() == peers.get(suggestedLeaderIndex).getId());
}, 10, TimeDuration.valueOf(1, TimeUnit.SECONDS), "testMultiGroupWithPriority", LOG);
// change the suggest leader
final int newSuggestedLeaderIndex = (suggestedLeaderIndex + 1) % peersWithPriority.size();
List<RaftPeer> peersWithNewPriority = getPeersWithPriority(peers, peers.get(newSuggestedLeaderIndex));
try (final RaftClient client = cluster.createClient(newGroup)) {
RaftClientReply reply = client.admin().setConfiguration(peersWithNewPriority.toArray(new RaftPeer[0]));
Assert.assertTrue(reply.isSuccess());
}
JavaUtils.attempt(() -> {
final RaftServer.Division leader = RaftTestUtil.waitForLeader(cluster, newGroup.getGroupId());
Assert.assertTrue(leader.getId() == peers.get(newSuggestedLeaderIndex).getId());
}, 10, TimeDuration.valueOf(1, TimeUnit.SECONDS), "testMultiGroupWithPriority", LOG);
cluster.killServer(peers.get(newSuggestedLeaderIndex).getId());
JavaUtils.attempt(() -> {
final RaftServer.Division leader = RaftTestUtil.waitForLeader(cluster, newGroup.getGroupId());
Assert.assertTrue(leader.getId() != peers.get(newSuggestedLeaderIndex).getId());
}, 10, TimeDuration.valueOf(1, TimeUnit.SECONDS), "testMultiGroupWithPriority", LOG);
cluster.shutdown();
}
use of org.apache.ratis.protocol.RaftPeerId in project incubator-ratis by apache.
the class GroupManagementBaseTest method testGroupAlreadyExists.
@Test
public void testGroupAlreadyExists() throws Exception {
final MiniRaftCluster cluster = getCluster(1);
cluster.start();
final RaftPeer peer = cluster.getPeers().get(0);
final RaftPeerId peerId = peer.getId();
final RaftGroup group = RaftGroup.valueOf(cluster.getGroupId(), peer);
try (final RaftClient client = cluster.createClient()) {
Assert.assertEquals(group, cluster.getDivision(peerId).getGroup());
try {
client.getGroupManagementApi(peer.getId()).add(group);
} catch (IOException ex) {
// HadoopRPC throws RemoteException, which makes it hard to check if
// the exception is instance of AlreadyExistsException
Assert.assertTrue(ex.toString().contains(AlreadyExistsException.class.getCanonicalName()));
}
Assert.assertEquals(group, cluster.getDivision(peerId).getGroup());
cluster.shutdown();
}
}
Aggregations