use of org.apache.ratis.protocol.RaftPeerId in project incubator-ratis by apache.
the class RaftBasicTests method runTestOldLeaderNotCommit.
void runTestOldLeaderNotCommit(CLUSTER cluster) throws Exception {
final RaftPeerId leaderId = waitForLeader(cluster).getId();
final List<RaftServer.Division> followers = cluster.getFollowers();
final RaftServer.Division followerToCommit = followers.get(0);
try {
for (int i = 1; i < NUM_SERVERS - 1; i++) {
cluster.killServer(followers.get(i).getId());
}
} catch (IndexOutOfBoundsException e) {
throw new org.junit.AssumptionViolatedException("The assumption is follower.size() = NUM_SERVERS - 1, " + "actual NUM_SERVERS is " + NUM_SERVERS + ", and actual follower.size() is " + followers.size(), e);
}
SimpleMessage[] messages = SimpleMessage.create(1);
RaftTestUtil.sendMessageInNewThread(cluster, leaderId, messages);
Thread.sleep(cluster.getTimeoutMax().toLong(TimeUnit.MILLISECONDS) + 100);
RaftTestUtil.logEntriesContains(followerToCommit.getRaftLog(), messages);
cluster.killServer(leaderId);
cluster.killServer(followerToCommit.getId());
for (int i = 1; i < NUM_SERVERS - 1; i++) {
cluster.restartServer(followers.get(i).getId(), false);
}
waitForLeader(cluster);
Thread.sleep(cluster.getTimeoutMax().toLong(TimeUnit.MILLISECONDS) + 100);
final Predicate<LogEntryProto> predicate = l -> l.getTerm() != 1;
cluster.getServerAliveStream().map(RaftServer.Division::getRaftLog).forEach(log -> RaftTestUtil.checkLogEntries(log, messages, predicate));
}
use of org.apache.ratis.protocol.RaftPeerId in project incubator-ratis by apache.
the class RaftTestUtil method changeLeader.
static RaftPeerId changeLeader(MiniRaftCluster cluster, RaftPeerId oldLeader, Function<String, Exception> constructor) throws Exception {
final String name = JavaUtils.getCallerStackTraceElement().getMethodName() + "-changeLeader";
cluster.setBlockRequestsFrom(oldLeader.toString(), true);
try {
return JavaUtils.attemptRepeatedly(() -> {
final RaftPeerId newLeader = waitForLeader(cluster).getId();
if (newLeader.equals(oldLeader)) {
throw constructor.apply("Failed to change leader: newLeader == oldLeader == " + oldLeader);
}
LOG.info("Changed leader from " + oldLeader + " to " + newLeader);
return newLeader;
}, 20, BaseTest.HUNDRED_MILLIS, name, LOG);
} finally {
cluster.setBlockRequestsFrom(oldLeader.toString(), false);
}
}
use of org.apache.ratis.protocol.RaftPeerId in project incubator-ratis by apache.
the class RetryCacheTests method runTestRetryOnNewLeader.
void runTestRetryOnNewLeader(CLUSTER cluster) throws Exception {
RaftTestUtil.waitForLeader(cluster);
final RaftPeerId leaderId = cluster.getLeaderAndSendFirstMessage(false).getId();
try (final RaftClient client = cluster.createClient(leaderId)) {
RaftClientRpc rpc = client.getClientRpc();
final long callId = 999;
RaftClientRequest r = cluster.newRaftClientRequest(client.getId(), leaderId, callId, new SimpleMessage("message"));
assertReply(rpc.sendRequest(r), client, callId);
final long oldLastApplied = cluster.getLeader().getInfo().getLastAppliedIndex();
// trigger the reconfiguration, make sure the original leader is kicked out
PeerChanges change = cluster.addNewPeers(2, true);
RaftPeer[] allPeers = cluster.removePeers(2, true, asList(change.newPeers)).allPeersInNewConf;
// trigger setConfiguration
cluster.setConfiguration(allPeers);
final RaftPeerId newLeaderId = JavaUtils.attemptRepeatedly(() -> {
final RaftPeerId id = RaftTestUtil.waitForLeader(cluster).getId();
Assert.assertNotEquals(leaderId, id);
return id;
}, 10, TimeDuration.valueOf(100, TimeUnit.MILLISECONDS), "wait for a leader different than " + leaderId, LOG);
Assert.assertNotEquals(leaderId, newLeaderId);
// same clientId and callId in the request
r = cluster.newRaftClientRequest(client.getId(), newLeaderId, callId, new SimpleMessage("message"));
rpc.addRaftPeers(Arrays.asList(change.newPeers));
for (int i = 0; i < 10; i++) {
try {
assertReply(rpc.sendRequest(r), client, callId);
LOG.info("successfully sent out the retry request_" + i);
} catch (Exception e) {
LOG.info("hit exception while retrying the same request: " + r, e);
}
Thread.sleep(100);
}
// check the new leader and make sure the retry did not get committed
Assert.assertEquals(0, count(cluster.getLeader().getRaftLog(), oldLastApplied + 1));
}
}
use of org.apache.ratis.protocol.RaftPeerId in project incubator-ratis by apache.
the class TestLeaderElectionMetrics method setUp.
@BeforeClass
public static void setUp() {
RaftGroupId raftGroupId = RaftGroupId.randomId();
RaftPeerId raftPeerId = RaftPeerId.valueOf("TestId");
RaftGroupMemberId raftGroupMemberId = RaftGroupMemberId.valueOf(raftPeerId, raftGroupId);
leaderElectionMetrics = LeaderElectionMetrics.getLeaderElectionMetrics(raftGroupMemberId, () -> 1000L);
ratisMetricRegistry = leaderElectionMetrics.getRegistry();
}
use of org.apache.ratis.protocol.RaftPeerId in project incubator-ratis by apache.
the class LeaderElectionTests method testChangeLeader.
@Test
public void testChangeLeader() throws Exception {
SegmentedRaftLogTestUtils.setRaftLogWorkerLogLevel(Level.TRACE);
LOG.info("Running testChangeLeader");
final MiniRaftCluster cluster = newCluster(3);
cluster.start();
RaftPeerId leader = RaftTestUtil.waitForLeader(cluster).getId();
for (int i = 0; i < 10; i++) {
leader = RaftTestUtil.changeLeader(cluster, leader, IllegalStateException::new);
ExitUtils.assertNotTerminated();
}
SegmentedRaftLogTestUtils.setRaftLogWorkerLogLevel(Level.INFO);
cluster.shutdown();
}
Aggregations