Search in sources :

Example 61 with LogEntryProto

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"));
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) TermIndex(org.apache.ratis.server.protocol.TermIndex) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Aggregations

LogEntryProto (org.apache.ratis.proto.RaftProtos.LogEntryProto)61 Test (org.junit.Test)22 BaseTest (org.apache.ratis.BaseTest)20 SimpleOperation (org.apache.ratis.RaftTestUtil.SimpleOperation)15 CompletableFuture (java.util.concurrent.CompletableFuture)14 File (java.io.File)13 IOException (java.io.IOException)13 StateMachineLogEntryProto (org.apache.ratis.proto.RaftProtos.StateMachineLogEntryProto)12 RaftLog (org.apache.ratis.server.raftlog.RaftLog)12 RaftStorage (org.apache.ratis.server.storage.RaftStorage)11 ArrayList (java.util.ArrayList)10 RaftPeerId (org.apache.ratis.protocol.RaftPeerId)10 TermIndex (org.apache.ratis.server.protocol.TermIndex)9 LogEntryHeader (org.apache.ratis.server.raftlog.LogEntryHeader)7 RaftClient (org.apache.ratis.client.RaftClient)6 RaftProperties (org.apache.ratis.conf.RaftProperties)6 RaftGroupId (org.apache.ratis.protocol.RaftGroupId)6 RaftGroupMemberId (org.apache.ratis.protocol.RaftGroupMemberId)6 RaftServer (org.apache.ratis.server.RaftServer)6 RandomAccessFile (java.io.RandomAccessFile)5