Search in sources :

Example 1 with RetryCache

use of org.apache.ratis.server.RetryCache in project incubator-ratis by apache.

the class TestSegmentedRaftLog method testAppendEntriesWithInconsistency.

/**
 * Test append with inconsistent entries
 */
@Test
public void testAppendEntriesWithInconsistency() throws Exception {
    // prepare the log for truncation
    List<SegmentRange> ranges = prepareRanges(0, 5, 200, 0);
    List<LogEntryProto> entries = prepareLogEntries(ranges, null);
    final RetryCache retryCache = RetryCacheTestUtil.createRetryCache();
    try (SegmentedRaftLog raftLog = RetryCacheTestUtil.newSegmentedRaftLog(memberId, retryCache, storage, properties)) {
        raftLog.open(RaftLog.INVALID_LOG_INDEX, null);
        entries.forEach(entry -> RetryCacheTestUtil.createEntry(retryCache, entry));
        // append entries to the raftlog
        entries.stream().map(raftLog::appendEntry).forEach(CompletableFuture::join);
    }
    // append entries whose first 100 entries are the same with existing log,
    // and the next 100 are with different term
    SegmentRange r1 = new SegmentRange(550, 599, 2, false);
    SegmentRange r2 = new SegmentRange(600, 649, 3, false);
    SegmentRange r3 = new SegmentRange(650, 749, 10, false);
    List<LogEntryProto> newEntries = prepareLogEntries(Arrays.asList(r1, r2, r3), null);
    try (SegmentedRaftLog raftLog = RetryCacheTestUtil.newSegmentedRaftLog(memberId, retryCache, storage, properties)) {
        raftLog.open(RaftLog.INVALID_LOG_INDEX, null);
        LOG.info("newEntries[0] = {}", newEntries.get(0));
        final int last = newEntries.size() - 1;
        LOG.info("newEntries[{}] = {}", last, newEntries.get(last));
        raftLog.append(newEntries.toArray(new LogEntryProto[0])).forEach(CompletableFuture::join);
        checkFailedEntries(entries, 650, retryCache);
        checkEntries(raftLog, entries, 0, 650);
        checkEntries(raftLog, newEntries, 100, 100);
        Assert.assertEquals(newEntries.get(newEntries.size() - 1), getLastEntry(raftLog));
        Assert.assertEquals(newEntries.get(newEntries.size() - 1).getIndex(), raftLog.getFlushIndex());
    }
    // load the raftlog again and check
    try (SegmentedRaftLog raftLog = RetryCacheTestUtil.newSegmentedRaftLog(memberId, retryCache, storage, properties)) {
        raftLog.open(RaftLog.INVALID_LOG_INDEX, null);
        checkEntries(raftLog, entries, 0, 650);
        checkEntries(raftLog, newEntries, 100, 100);
        Assert.assertEquals(newEntries.get(newEntries.size() - 1), getLastEntry(raftLog));
        Assert.assertEquals(newEntries.get(newEntries.size() - 1).getIndex(), raftLog.getFlushIndex());
        SegmentedRaftLogCache cache = raftLog.getRaftLogCache();
        Assert.assertEquals(5, cache.getNumOfSegments());
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) RetryCache(org.apache.ratis.server.RetryCache) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Aggregations

CompletableFuture (java.util.concurrent.CompletableFuture)1 BaseTest (org.apache.ratis.BaseTest)1 LogEntryProto (org.apache.ratis.proto.RaftProtos.LogEntryProto)1 RetryCache (org.apache.ratis.server.RetryCache)1 Test (org.junit.Test)1