use of org.apache.ratis.protocol.RaftPeerId in project incubator-ratis by apache.
the class PeerConfiguration method hasMajority.
boolean hasMajority(Collection<RaftPeerId> others, RaftPeerId selfId) {
Preconditions.assertTrue(!others.contains(selfId));
int num = 0;
if (contains(selfId)) {
num++;
}
for (RaftPeerId other : others) {
if (contains(other)) {
num++;
}
if (num > size() / 2) {
return true;
}
}
return false;
}
use of org.apache.ratis.protocol.RaftPeerId in project incubator-ratis by apache.
the class RaftBasicTests method testOldLeaderNotCommit.
@Test
public void testOldLeaderNotCommit() throws Exception {
LOG.info("Running testOldLeaderNotCommit");
final MiniRaftCluster cluster = getCluster();
final RaftPeerId leaderId = waitForLeader(cluster).getId();
List<RaftServerImpl> followers = cluster.getFollowers();
final RaftServerImpl followerToCommit = followers.get(0);
for (int i = 1; i < NUM_SERVERS - 1; i++) {
RaftServerImpl follower = followers.get(i);
cluster.killServer(follower.getId());
}
SimpleMessage[] messages = SimpleMessage.create(1);
sendMessageInNewThread(cluster, messages);
Thread.sleep(cluster.getMaxTimeout() + 100);
logEntriesContains(followerToCommit.getState().getLog(), messages);
cluster.killServer(leaderId);
cluster.killServer(followerToCommit.getId());
for (int i = 1; i < NUM_SERVERS - 1; i++) {
RaftServerImpl follower = followers.get(i);
cluster.restartServer(follower.getId(), false);
}
waitForLeader(cluster);
Thread.sleep(cluster.getMaxTimeout() + 100);
final Predicate<LogEntryProto> predicate = l -> l.getTerm() != 1;
cluster.getServerAliveStream().map(s -> s.getState().getLog()).forEach(log -> RaftTestUtil.checkLogEntries(log, messages, predicate));
}
use of org.apache.ratis.protocol.RaftPeerId in project incubator-ratis by apache.
the class RaftBasicTests method testOldLeaderCommit.
@Test
public void testOldLeaderCommit() throws Exception {
LOG.info("Running testOldLeaderCommit");
final MiniRaftCluster cluster = getCluster();
final RaftServerImpl leader = waitForLeader(cluster);
final RaftPeerId leaderId = leader.getId();
final long term = leader.getState().getCurrentTerm();
List<RaftServerImpl> followers = cluster.getFollowers();
final RaftServerImpl followerToSendLog = followers.get(0);
for (int i = 1; i < NUM_SERVERS - 1; i++) {
RaftServerImpl follower = followers.get(i);
cluster.killServer(follower.getId());
}
SimpleMessage[] messages = SimpleMessage.create(1);
RaftTestUtil.sendMessageInNewThread(cluster, messages);
Thread.sleep(cluster.getMaxTimeout() + 100);
RaftLog followerLog = followerToSendLog.getState().getLog();
assertTrue(logEntriesContains(followerLog, messages));
LOG.info(String.format("killing old leader: %s", leaderId.toString()));
cluster.killServer(leaderId);
for (int i = 1; i < 3; i++) {
RaftServerImpl follower = followers.get(i);
LOG.info(String.format("restarting follower: %s", follower.getId().toString()));
cluster.restartServer(follower.getId(), false);
}
Thread.sleep(cluster.getMaxTimeout() * 5);
// confirm the server with log is elected as new leader.
final RaftPeerId newLeaderId = waitForLeader(cluster).getId();
Assert.assertEquals(followerToSendLog.getId(), newLeaderId);
cluster.getServerAliveStream().map(s -> s.getState().getLog()).forEach(log -> RaftTestUtil.assertLogEntries(log, false, term, messages));
LOG.info("terminating testOldLeaderCommit test");
}
use of org.apache.ratis.protocol.RaftPeerId in project incubator-ratis by apache.
the class RaftTestUtil method sendMessageInNewThread.
static void sendMessageInNewThread(MiniRaftCluster cluster, SimpleMessage... messages) {
RaftPeerId leaderId = cluster.getLeader().getId();
new Thread(() -> {
try (final RaftClient client = cluster.createClient(leaderId)) {
for (SimpleMessage mssg : messages) {
client.send(mssg);
}
} catch (Exception e) {
e.printStackTrace();
}
}).start();
}
use of org.apache.ratis.protocol.RaftPeerId in project incubator-ratis by apache.
the class RetryCacheTests method testRetryOnNewLeader.
/**
* Test retry while the leader changes to another peer
*/
@Test
public void testRetryOnNewLeader() throws Exception {
final MiniRaftCluster cluster = getCluster();
RaftTestUtil.waitForLeader(cluster);
final RaftPeerId leaderId = cluster.getLeaderAndSendFirstMessage().getId();
final RaftClient client = cluster.createClient(leaderId);
RaftClientRpc rpc = client.getClientRpc();
final long callId = 999;
final long seqNum = 111;
RaftClientRequest r = cluster.newRaftClientRequest(client.getId(), leaderId, callId, seqNum, new SimpleMessage("message"));
RaftClientReply reply = rpc.sendRequest(r);
Assert.assertEquals(callId, reply.getCallId());
Assert.assertTrue(reply.isSuccess());
long oldLastApplied = cluster.getLeader().getState().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);
RaftTestUtil.waitForLeader(cluster);
final RaftPeerId newLeaderId = cluster.getLeader().getId();
Assert.assertNotEquals(leaderId, newLeaderId);
// same clientId and callId in the request
r = cluster.newRaftClientRequest(client.getId(), newLeaderId, callId, seqNum, new SimpleMessage("message"));
for (int i = 0; i < 10; i++) {
try {
reply = rpc.sendRequest(r);
LOG.info("successfully sent out the retry request_" + i);
Assert.assertEquals(client.getId(), reply.getClientId());
Assert.assertEquals(callId, reply.getCallId());
Assert.assertTrue(reply.isSuccess());
} catch (Exception e) {
LOG.info("hit exception while retrying the same request: " + e);
}
Thread.sleep(100);
}
// check the new leader and make sure the retry did not get committed
Assert.assertEquals(oldLastApplied + 3, cluster.getLeader().getState().getLastAppliedIndex());
client.close();
}
Aggregations