use of org.apache.ratis.RaftTestUtil.SimpleMessage in project incubator-ratis by apache.
the class RaftExceptionBaseTest method sendMessage.
static void sendMessage(String message, RaftClient client) throws IOException {
final RaftClientReply reply = client.io().send(new SimpleMessage(message));
Assert.assertTrue(reply.isSuccess());
}
use of org.apache.ratis.RaftTestUtil.SimpleMessage in project incubator-ratis by apache.
the class LogAppenderTests method generateMsgs.
static SimpleMessage[] generateMsgs(int num) {
SimpleMessage[] msgs = new SimpleMessage[num * 6];
for (int i = 0; i < num; i++) {
for (int j = 0; j < 6; j++) {
byte[] bytes = new byte[1024 * (j + 1)];
Arrays.fill(bytes, (byte) (j + '0'));
msgs[i * 6 + j] = new SimpleMessage(new String(bytes));
}
}
return msgs;
}
use of org.apache.ratis.RaftTestUtil.SimpleMessage in project incubator-ratis by apache.
the class RaftBasicTests method runTestOldLeaderCommit.
void runTestOldLeaderCommit(CLUSTER cluster) throws Exception {
final RaftServer.Division leader = waitForLeader(cluster);
final RaftPeerId leaderId = leader.getId();
final long term = leader.getInfo().getCurrentTerm();
final List<RaftServer.Division> followers = cluster.getFollowers();
final List<RaftServer.Division> followersToSendLog = followers.subList(0, followers.size() / 2);
for (int i = followers.size() / 2; i < NUM_SERVERS - 1; i++) {
cluster.killServer(followers.get(i).getId());
}
SimpleMessage[] messages = SimpleMessage.create(1);
RaftTestUtil.sendMessageInNewThread(cluster, leaderId, messages);
Thread.sleep(cluster.getTimeoutMax().toLong(TimeUnit.MILLISECONDS) + 100);
for (RaftServer.Division followerToSendLog : followersToSendLog) {
RaftLog followerLog = followerToSendLog.getRaftLog();
Assert.assertTrue(RaftTestUtil.logEntriesContains(followerLog, messages));
}
LOG.info(String.format("killing old leader: %s", leaderId.toString()));
cluster.killServer(leaderId);
for (int i = followers.size() / 2; i < NUM_SERVERS - 1; i++) {
final RaftPeerId followerId = followers.get(i).getId();
LOG.info(String.format("restarting follower: %s", followerId));
cluster.restartServer(followerId, false);
}
Thread.sleep(cluster.getTimeoutMax().toLong(TimeUnit.MILLISECONDS) * 5);
// confirm the server with log is elected as new leader.
final RaftPeerId newLeaderId = waitForLeader(cluster).getId();
Set<RaftPeerId> followersToSendLogIds = followersToSendLog.stream().map(f -> f.getId()).collect(Collectors.toSet());
Assert.assertTrue(followersToSendLogIds.contains(newLeaderId));
cluster.getServerAliveStream().map(RaftServer.Division::getRaftLog).forEach(log -> RaftTestUtil.assertLogEntries(log, term, messages));
}
use of org.apache.ratis.RaftTestUtil.SimpleMessage in project incubator-ratis by apache.
the class RaftBasicTests method testRequestTimeout.
public static void testRequestTimeout(boolean async, MiniRaftCluster cluster, Logger LOG) throws Exception {
waitForLeader(cluster);
final Timestamp startTime = Timestamp.currentTime();
try (final RaftClient client = cluster.createClient()) {
// Get the next callId to be used by the client
final ClientInvocationId invocationId = RaftClientTestUtil.getClientInvocationId(client);
// Create an entry corresponding to the callId and clientId
// in each server's retry cache.
cluster.getServerAliveStream().forEach(raftServer -> RetryCacheTestUtil.getOrCreateEntry(raftServer, invocationId));
// The retry is successful when the retry cache entry for the corresponding callId and clientId expires.
if (async) {
CompletableFuture<RaftClientReply> replyFuture = client.async().send(new SimpleMessage("abc"));
replyFuture.get();
} else {
client.io().send(new SimpleMessage("abc"));
}
// Eventually the request would be accepted by the server
// when the retry cache entry is invalidated.
// The duration for which the client waits should be more than the retryCacheExpiryDuration.
final TimeDuration duration = startTime.elapsedTime();
TimeDuration retryCacheExpiryDuration = RaftServerConfigKeys.RetryCache.expiryTime(cluster.getProperties());
Assert.assertTrue(duration.compareTo(retryCacheExpiryDuration) >= 0);
}
}
use of org.apache.ratis.RaftTestUtil.SimpleMessage 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));
}
Aggregations