use of org.apache.ratis.proto.RaftProtos.LogEntryProto in project incubator-ratis by apache.
the class TestSegmentedRaftLog method testAppendEntry.
/**
* Append entry one by one and check if log state is correct.
*/
@Test
public void testAppendEntry() throws Exception {
List<SegmentRange> ranges = prepareRanges(0, 5, 200, 0);
List<LogEntryProto> entries = prepareLogEntries(ranges, null);
try (SegmentedRaftLog raftLog = newSegmentedRaftLog()) {
raftLog.open(RaftLog.INVALID_LOG_INDEX, null);
// append entries to the raftlog
entries.stream().map(raftLog::appendEntry).forEach(CompletableFuture::join);
}
try (SegmentedRaftLog raftLog = newSegmentedRaftLog()) {
raftLog.open(RaftLog.INVALID_LOG_INDEX, null);
// check if the raft log is correct
checkEntries(raftLog, entries, 0, entries.size());
}
try (SegmentedRaftLog raftLog = newSegmentedRaftLog()) {
raftLog.open(RaftLog.INVALID_LOG_INDEX, null);
TermIndex lastTermIndex = raftLog.getLastEntryTermIndex();
IllegalStateException ex = null;
try {
// append entry fails if append entry term is lower than log's last entry term
raftLog.appendEntry(LogEntryProto.newBuilder(entries.get(0)).setTerm(lastTermIndex.getTerm() - 1).setIndex(lastTermIndex.getIndex() + 1).build());
} catch (IllegalStateException e) {
ex = e;
}
assertTrue(ex.getMessage().contains("term less than RaftLog's last term"));
try {
// append entry fails if difference between append entry index and log's last entry index is greater than 1
raftLog.appendEntry(LogEntryProto.newBuilder(entries.get(0)).setTerm(lastTermIndex.getTerm()).setIndex(lastTermIndex.getIndex() + 2).build());
} catch (IllegalStateException e) {
ex = e;
}
assertTrue(ex.getMessage().contains("and RaftLog's last index " + lastTermIndex.getIndex() + " (or snapshot index " + raftLog.getSnapshotIndex() + ") is greater than 1"));
raftLog.onSnapshotInstalled(raftLog.getLastEntryTermIndex().getIndex());
try {
// append entry fails if there are no log entries && log's snapshotIndex + 1 < incoming log entry.
raftLog.appendEntry(LogEntryProto.newBuilder(entries.get(0)).setTerm(lastTermIndex.getTerm()).setIndex(lastTermIndex.getIndex() + 2).build());
} catch (IllegalStateException e) {
ex = e;
}
assertTrue(ex.getMessage().contains("Difference between entry index and RaftLog's latest snapshot " + "index -1 is greater than 1"));
}
}
Aggregations