use of org.apache.ratis.proto.RaftProtos.LogEntryProto in project incubator-ratis by apache.
the class SimpleStateMachine4Testing method loadSnapshot.
private synchronized long loadSnapshot(SingleFileSnapshotInfo snapshot) throws IOException {
if (snapshot == null || !snapshot.getFile().getPath().toFile().exists()) {
LOG.info("The snapshot file {} does not exist", snapshot == null ? null : snapshot.getFile());
return RaftLog.INVALID_LOG_INDEX;
} else {
LOG.info("Loading snapshot {}", snapshot);
final long endIndex = snapshot.getIndex();
try (SegmentedRaftLogInputStream in = new SegmentedRaftLogInputStream(snapshot.getFile().getPath().toFile(), 0, endIndex, false)) {
LogEntryProto entry;
while ((entry = in.nextEntry()) != null) {
put(entry);
updateLastAppliedTermIndex(entry.getTerm(), entry.getIndex());
}
}
// The end index is greater than last entry in indexMap as it also
// includes the configuration and metadata entries
Preconditions.assertTrue(!indexMap.isEmpty() && endIndex >= indexMap.lastKey(), "endIndex=%s, indexMap=%s", endIndex, indexMap);
this.endIndexLastCkpt = endIndex;
setLastAppliedTermIndex(snapshot.getTermIndex());
this.storage.loadLatestSnapshot();
return endIndex;
}
}
use of org.apache.ratis.proto.RaftProtos.LogEntryProto 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.proto.RaftProtos.LogEntryProto in project incubator-ratis by apache.
the class RaftTestUtil method assertLogEntries.
static void assertLogEntries(List<LogEntryProto> entries, long expectedTerm, SimpleMessage... expectedMessages) {
long logIndex = 0;
Assert.assertEquals(expectedMessages.length, entries.size());
for (int i = 0; i < expectedMessages.length; i++) {
final LogEntryProto e = entries.get(i);
Assert.assertTrue(e.getTerm() >= expectedTerm);
if (e.getTerm() > expectedTerm) {
expectedTerm = e.getTerm();
}
Assert.assertTrue(e.getIndex() > logIndex);
logIndex = e.getIndex();
Assert.assertArrayEquals(expectedMessages[i].getContent().toByteArray(), e.getStateMachineLogEntry().getLogData().toByteArray());
}
}
use of org.apache.ratis.proto.RaftProtos.LogEntryProto in project incubator-ratis by apache.
the class OutputStreamBaseTest method checkLog.
private void checkLog(RaftLog raftLog, long expectedCommittedIndex, Supplier<byte[]> s) throws IOException {
long committedIndex = raftLog.getLastCommittedIndex();
Assert.assertTrue(committedIndex >= expectedCommittedIndex);
// check the log content
final LogEntryHeader[] entries = raftLog.getEntries(0, Long.MAX_VALUE);
int count = 0;
for (LogEntryHeader entry : entries) {
LogEntryProto log = raftLog.get(entry.getIndex());
if (!log.hasStateMachineLogEntry()) {
continue;
}
byte[] logData = log.getStateMachineLogEntry().getLogData().toByteArray();
byte[] expected = s.get();
final String message = "log " + entry + " " + log.getLogEntryBodyCase() + " " + StringUtils.bytes2HexString(logData) + ", expected=" + StringUtils.bytes2HexString(expected);
Assert.assertArrayEquals(message, expected, logData);
count++;
}
Assert.assertEquals(expectedCommittedIndex, count);
}
use of org.apache.ratis.proto.RaftProtos.LogEntryProto in project incubator-ratis by apache.
the class LogAppenderTests method runTest.
void runTest(CLUSTER cluster) throws Exception {
final int numMsgs = 10;
final int numClients = 5;
final RaftPeerId leaderId = RaftTestUtil.waitForLeader(cluster).getId();
List<RaftClient> clients = new ArrayList<>();
try {
List<Sender> senders = new ArrayList<>();
// start several clients and write concurrently
final CountDownLatch latch = new CountDownLatch(1);
for (int i = 0; i < numClients; i++) {
RaftClient client = cluster.createClient(leaderId);
clients.add(client);
senders.add(new Sender(client, numMsgs, latch));
}
senders.forEach(Thread::start);
latch.countDown();
for (Sender s : senders) {
s.join();
final Exception e = s.exception.get();
if (e != null) {
throw e;
}
Assert.assertTrue(s.succeed.get());
}
} finally {
for (int i = 0; i < clients.size(); i++) {
try {
clients.get(i).close();
} catch (Exception ignored) {
LOG.warn("{} is ignored", JavaUtils.getClassSimpleName(ignored.getClass()), ignored);
}
}
}
final RaftServer.Division leader = cluster.getLeader();
final RaftLog leaderLog = cluster.getLeader().getRaftLog();
final EnumMap<LogEntryBodyCase, AtomicLong> counts = RaftTestUtil.countEntries(leaderLog);
LOG.info("counts = " + counts);
Assert.assertEquals(6 * numMsgs * numClients, counts.get(LogEntryBodyCase.STATEMACHINELOGENTRY).get());
final LogEntryProto last = RaftTestUtil.getLastEntry(LogEntryBodyCase.STATEMACHINELOGENTRY, leaderLog);
LOG.info("last = {}", LogProtoUtils.toLogEntryString(last));
Assert.assertNotNull(last);
Assert.assertTrue(last.getIndex() <= leader.getInfo().getLastAppliedIndex());
}
Aggregations