use of org.apache.ratis.protocol.RaftClientReply 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.RaftClientReply 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.RaftClientReply in project incubator-ratis by apache.
the class RaftReconfigurationBaseTest method testLeaderNotReadyException.
/**
* Delay the commit of the leader placeholder log entry and see if the client
* can correctly receive and handle the LeaderNotReadyException.
*/
@Test
public void testLeaderNotReadyException() throws Exception {
LOG.info("Start testLeaderNotReadyException");
final MiniRaftCluster cluster = newCluster(1).initServers();
try {
// delay 1s for each logSync call
cluster.getServers().forEach(peer -> leaderPlaceHolderDelay.setDelayMs(peer.getId().toString(), 2000));
cluster.start();
AtomicBoolean caughtNotReady = new AtomicBoolean(false);
AtomicBoolean success = new AtomicBoolean(false);
final RaftPeerId leaderId = cluster.getPeers().iterator().next().getId();
new Thread(() -> {
try (final RaftClient client = cluster.createClient(leaderId)) {
final RaftClientRpc sender = client.getClientRpc();
final RaftClientRequest request = cluster.newRaftClientRequest(client.getId(), leaderId, new SimpleMessage("test"));
while (!success.get()) {
try {
final RaftClientReply reply = sender.sendRequest(request);
success.set(reply.isSuccess());
if (reply.getException() != null && reply.getException() instanceof LeaderNotReadyException) {
caughtNotReady.set(true);
}
} catch (IOException e) {
LOG.info("Hit other IOException", e);
}
if (!success.get()) {
try {
Thread.sleep(200);
} catch (InterruptedException ignored) {
Thread.currentThread().interrupt();
}
}
}
} catch (Exception ignored) {
LOG.warn("{} is ignored", JavaUtils.getClassSimpleName(ignored.getClass()), ignored);
}
}).start();
RaftTestUtil.waitForLeader(cluster);
for (int i = 0; !success.get() && i < 5; i++) {
Thread.sleep(1000);
}
Assert.assertTrue(success.get());
Assert.assertTrue(caughtNotReady.get());
} finally {
leaderPlaceHolderDelay.clear();
cluster.shutdown();
}
}
use of org.apache.ratis.protocol.RaftClientReply in project incubator-ratis by apache.
the class RaftReconfigurationBaseTest method runTestBootstrapReconf.
void runTestBootstrapReconf(int numNewPeer, boolean startNewPeer, CLUSTER cluster) throws Exception {
LOG.info("Originally {} peer(s), add {} more, startNewPeer={}", cluster.getNumServers(), numNewPeer, startNewPeer);
RaftTestUtil.waitForLeader(cluster);
final RaftPeerId leaderId = cluster.getLeader().getId();
try (final RaftClient client = cluster.createClient(leaderId)) {
// submit some msgs before reconf
for (int i = 0; i < STAGING_CATCHUP_GAP * 2; i++) {
RaftClientReply reply = client.io().send(new SimpleMessage("m" + i));
Assert.assertTrue(reply.isSuccess());
}
final PeerChanges c1 = cluster.addNewPeers(numNewPeer, startNewPeer);
LOG.info("Start changing the configuration: {}", asList(c1.allPeersInNewConf));
final AtomicReference<Boolean> success = new AtomicReference<>();
Thread clientThread = new Thread(() -> {
try {
RaftClientReply reply = client.admin().setConfiguration(c1.allPeersInNewConf);
success.set(reply.isSuccess());
} catch (IOException ioe) {
LOG.error("FAILED", ioe);
}
});
clientThread.start();
if (!startNewPeer) {
// Make sure that set configuration is run inside the thread
RaftTestUtil.waitFor(() -> clientThread.isAlive(), 300, 5000);
ONE_SECOND.sleep();
LOG.info("start new peer(s): {}", c1.newPeers);
for (RaftPeer p : c1.newPeers) {
cluster.restartServer(p.getId(), true);
}
}
RaftTestUtil.waitFor(() -> success.get() != null && success.get(), 300, 15000);
LOG.info(cluster.printServers());
final RaftLog leaderLog = cluster.getLeader().getRaftLog();
for (RaftPeer newPeer : c1.newPeers) {
final RaftServer.Division d = cluster.getDivision(newPeer.getId());
RaftTestUtil.waitFor(() -> leaderLog.getEntries(0, Long.MAX_VALUE).length == d.getRaftLog().getEntries(0, Long.MAX_VALUE).length, 300, 15000);
Assert.assertArrayEquals(leaderLog.getEntries(0, Long.MAX_VALUE), d.getRaftLog().getEntries(0, Long.MAX_VALUE));
}
}
}
use of org.apache.ratis.protocol.RaftClientReply in project incubator-ratis by apache.
the class RaftReconfigurationBaseTest method runTestNoChangeRequest.
void runTestNoChangeRequest(CLUSTER cluster) throws Exception {
final RaftServer.Division leader = RaftTestUtil.waitForLeader(cluster);
try (final RaftClient client = cluster.createClient(leader.getId())) {
client.io().send(new SimpleMessage("m"));
final RaftLog leaderLog = leader.getRaftLog();
final long committedIndex = leaderLog.getLastCommittedIndex();
final RaftConfiguration confBefore = cluster.getLeader().getRaftConf();
// no real configuration change in the request
final RaftClientReply reply = client.admin().setConfiguration(cluster.getPeers().toArray(RaftPeer.emptyArray()));
Assert.assertTrue(reply.isSuccess());
final long newCommittedIndex = leaderLog.getLastCommittedIndex();
for (long i = committedIndex + 1; i <= newCommittedIndex; i++) {
final LogEntryProto e = leaderLog.get(i);
Assert.assertTrue(e.hasMetadataEntry());
}
Assert.assertSame(confBefore, cluster.getLeader().getRaftConf());
}
}
Aggregations