Search in sources :

Example 1 with LogEntryProto

use of org.apache.ratis.proto.RaftProtos.LogEntryProto in project incubator-ratis by apache.

the class TestLogSegment method checkLogSegment.

static void checkLogSegment(LogSegment segment, long start, long end, boolean isOpen, long totalSize, long term) throws Exception {
    Assert.assertEquals(start, segment.getStartIndex());
    Assert.assertEquals(end, segment.getEndIndex());
    Assert.assertEquals(isOpen, segment.isOpen());
    Assert.assertEquals(totalSize, segment.getTotalFileSize());
    long offset = SegmentedRaftLogFormat.getHeaderLength();
    for (long i = start; i <= end; i++) {
        LogSegment.LogRecord record = segment.getLogRecord(i);
        final TermIndex ti = record.getTermIndex();
        Assert.assertEquals(i, ti.getIndex());
        Assert.assertEquals(term, ti.getTerm());
        Assert.assertEquals(offset, record.getOffset());
        LogEntryProto entry = segment.getEntryFromCache(ti);
        if (entry == null) {
            entry = segment.loadCache(record);
        }
        offset += getEntrySize(entry, LogSegment.Op.WRITE_CACHE_WITHOUT_STATE_MACHINE_CACHE);
    }
}
Also used : LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) StateMachineLogEntryProto(org.apache.ratis.proto.RaftProtos.StateMachineLogEntryProto) TermIndex(org.apache.ratis.server.protocol.TermIndex)

Example 2 with LogEntryProto

use of org.apache.ratis.proto.RaftProtos.LogEntryProto in project incubator-ratis by apache.

the class TestSegmentedRaftLog method testAppendEntryAfterPurge.

@Test
public void testAppendEntryAfterPurge() throws Exception {
    List<SegmentRange> ranges = prepareRanges(0, 5, 200, 0);
    List<LogEntryProto> entries = prepareLogEntries(ranges, null);
    long desiredSnapshotIndex = entries.size() - 2;
    final LongSupplier getSnapshotIndexFromStateMachine = new LongSupplier() {

        private boolean firstCall = true;

        @Override
        public long getAsLong() {
            long index = firstCall ? -1 : desiredSnapshotIndex;
            firstCall = !firstCall;
            return index;
        }
    };
    try (SegmentedRaftLog raftLog = newSegmentedRaftLog(getSnapshotIndexFromStateMachine)) {
        raftLog.open(RaftLog.INVALID_LOG_INDEX, null);
        entries.subList(0, entries.size() - 1).stream().map(raftLog::appendEntry).forEach(CompletableFuture::join);
        raftLog.onSnapshotInstalled(desiredSnapshotIndex);
        // Try appending last entry after snapshot + purge.
        CompletableFuture<Long> appendEntryFuture = raftLog.appendEntry(entries.get(entries.size() - 1));
        assertTrue(desiredSnapshotIndex + 1 == appendEntryFuture.get());
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) LongSupplier(java.util.function.LongSupplier) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Example 3 with LogEntryProto

use of org.apache.ratis.proto.RaftProtos.LogEntryProto in project incubator-ratis by apache.

the class TestSegmentedRaftLog method checkEntries.

private void checkEntries(RaftLog raftLog, List<LogEntryProto> expected, int offset, int size) throws IOException {
    if (size > 0) {
        for (int i = offset; i < size + offset; i++) {
            LogEntryProto entry = raftLog.get(expected.get(i).getIndex());
            Assert.assertEquals(expected.get(i), entry);
        }
        final LogEntryHeader[] termIndices = raftLog.getEntries(expected.get(offset).getIndex(), expected.get(offset + size - 1).getIndex() + 1);
        LogEntryProto[] entriesFromLog = Arrays.stream(termIndices).map(ti -> {
            try {
                return raftLog.get(ti.getIndex());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }).toArray(LogEntryProto[]::new);
        LogEntryProto[] expectedArray = expected.subList(offset, offset + size).stream().toArray(LogEntryProto[]::new);
        Assert.assertArrayEquals(expectedArray, entriesFromLog);
    }
}
Also used : LogProtoUtils(org.apache.ratis.server.raftlog.LogProtoUtils) Arrays(java.util.Arrays) RetryCache(org.apache.ratis.server.RetryCache) TermIndex(org.apache.ratis.server.protocol.TermIndex) LogEntryHeader(org.apache.ratis.server.raftlog.LogEntryHeader) LongSupplier(java.util.function.LongSupplier) RaftGroupMemberId(org.apache.ratis.protocol.RaftGroupMemberId) RaftLog(org.apache.ratis.server.raftlog.RaftLog) RetryCacheTestUtil(org.apache.ratis.server.impl.RetryCacheTestUtil) CompletableFuture(java.util.concurrent.CompletableFuture) Supplier(java.util.function.Supplier) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) RaftGroupId(org.apache.ratis.protocol.RaftGroupId) TimeoutIOException(org.apache.ratis.protocol.exceptions.TimeoutIOException) RaftLogMetricsBase(org.apache.ratis.server.metrics.RaftLogMetricsBase) Log4jUtils(org.apache.ratis.util.Log4jUtils) Level(org.apache.log4j.Level) After(org.junit.After) SimpleStateMachine4Testing(org.apache.ratis.statemachine.SimpleStateMachine4Testing) RaftStorage(org.apache.ratis.server.storage.RaftStorage) JavaUtils(org.apache.ratis.util.JavaUtils) SizeInBytes(org.apache.ratis.util.SizeInBytes) Before(org.junit.Before) StateMachine(org.apache.ratis.statemachine.StateMachine) LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftServerConfigKeys(org.apache.ratis.server.RaftServerConfigKeys) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) BaseTest(org.apache.ratis.BaseTest) RatisMetricRegistry(org.apache.ratis.metrics.RatisMetricRegistry) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) FileUtils(org.apache.ratis.util.FileUtils) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) BaseStateMachine(org.apache.ratis.statemachine.impl.BaseStateMachine) RaftStorageTestUtils(org.apache.ratis.server.storage.RaftStorageTestUtils) RaftProperties(org.apache.ratis.conf.RaftProperties) LifeCycle(org.apache.ratis.util.LifeCycle) Timer(com.codahale.metrics.Timer) SimpleOperation(org.apache.ratis.RaftTestUtil.SimpleOperation) Assert(org.junit.Assert) Collections(java.util.Collections) TimeDuration(org.apache.ratis.util.TimeDuration) LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) LogEntryHeader(org.apache.ratis.server.raftlog.LogEntryHeader) TimeoutIOException(org.apache.ratis.protocol.exceptions.TimeoutIOException) IOException(java.io.IOException)

Example 4 with LogEntryProto

use of org.apache.ratis.proto.RaftProtos.LogEntryProto in project incubator-ratis by apache.

the class TestSegmentedRaftLog method purgeAndVerify.

private void purgeAndVerify(int startTerm, int endTerm, int segmentSize, int purgeGap, long purgeIndex, long expectedIndex) throws Exception {
    List<SegmentRange> ranges = prepareRanges(startTerm, endTerm, segmentSize, 0);
    List<LogEntryProto> entries = prepareLogEntries(ranges, null);
    final RaftProperties p = new RaftProperties();
    RaftServerConfigKeys.Log.setPurgeGap(p, purgeGap);
    try (SegmentedRaftLog raftLog = newSegmentedRaftLog(storage, p)) {
        raftLog.open(RaftLog.INVALID_LOG_INDEX, null);
        entries.stream().map(raftLog::appendEntry).forEach(CompletableFuture::join);
        final CompletableFuture<Long> f = raftLog.purge(purgeIndex);
        final Long purged = f.get();
        LOG.info("purgeIndex = {}, purged = {}", purgeIndex, purged);
        Assert.assertEquals(expectedIndex, raftLog.getRaftLogCache().getStartIndex());
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) RaftProperties(org.apache.ratis.conf.RaftProperties)

Example 5 with LogEntryProto

use of org.apache.ratis.proto.RaftProtos.LogEntryProto in project incubator-ratis by apache.

the class TestSegmentedRaftLog method testLoadLogSegments.

@Test
public void testLoadLogSegments() throws Exception {
    // first generate log files
    List<SegmentRange> ranges = prepareRanges(0, 5, 100, 0);
    LogEntryProto[] entries = prepareLog(ranges);
    // create RaftLog object and load log file
    try (SegmentedRaftLog raftLog = newSegmentedRaftLog()) {
        raftLog.open(RaftLog.INVALID_LOG_INDEX, null);
        // check if log entries are loaded correctly
        for (LogEntryProto e : entries) {
            LogEntryProto entry = raftLog.get(e.getIndex());
            Assert.assertEquals(e, entry);
        }
        final LogEntryHeader[] termIndices = raftLog.getEntries(0, 500);
        LogEntryProto[] entriesFromLog = Arrays.stream(termIndices).map(ti -> {
            try {
                return raftLog.get(ti.getIndex());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }).toArray(LogEntryProto[]::new);
        Assert.assertArrayEquals(entries, entriesFromLog);
        Assert.assertEquals(entries[entries.length - 1], getLastEntry(raftLog));
        final RatisMetricRegistry metricRegistryForLogWorker = RaftLogMetricsBase.getLogWorkerMetricRegistry(memberId);
        Timer raftLogSegmentLoadLatencyTimer = metricRegistryForLogWorker.timer("segmentLoadLatency");
        assertTrue(raftLogSegmentLoadLatencyTimer.getMeanRate() > 0);
        Timer raftLogReadLatencyTimer = metricRegistryForLogWorker.timer("readEntryLatency");
        assertTrue(raftLogReadLatencyTimer.getMeanRate() > 0);
    }
}
Also used : LogProtoUtils(org.apache.ratis.server.raftlog.LogProtoUtils) Arrays(java.util.Arrays) RetryCache(org.apache.ratis.server.RetryCache) TermIndex(org.apache.ratis.server.protocol.TermIndex) LogEntryHeader(org.apache.ratis.server.raftlog.LogEntryHeader) LongSupplier(java.util.function.LongSupplier) RaftGroupMemberId(org.apache.ratis.protocol.RaftGroupMemberId) RaftLog(org.apache.ratis.server.raftlog.RaftLog) RetryCacheTestUtil(org.apache.ratis.server.impl.RetryCacheTestUtil) CompletableFuture(java.util.concurrent.CompletableFuture) Supplier(java.util.function.Supplier) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) RaftGroupId(org.apache.ratis.protocol.RaftGroupId) TimeoutIOException(org.apache.ratis.protocol.exceptions.TimeoutIOException) RaftLogMetricsBase(org.apache.ratis.server.metrics.RaftLogMetricsBase) Log4jUtils(org.apache.ratis.util.Log4jUtils) Level(org.apache.log4j.Level) After(org.junit.After) SimpleStateMachine4Testing(org.apache.ratis.statemachine.SimpleStateMachine4Testing) RaftStorage(org.apache.ratis.server.storage.RaftStorage) JavaUtils(org.apache.ratis.util.JavaUtils) SizeInBytes(org.apache.ratis.util.SizeInBytes) Before(org.junit.Before) StateMachine(org.apache.ratis.statemachine.StateMachine) LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftServerConfigKeys(org.apache.ratis.server.RaftServerConfigKeys) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) BaseTest(org.apache.ratis.BaseTest) RatisMetricRegistry(org.apache.ratis.metrics.RatisMetricRegistry) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) FileUtils(org.apache.ratis.util.FileUtils) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) BaseStateMachine(org.apache.ratis.statemachine.impl.BaseStateMachine) RaftStorageTestUtils(org.apache.ratis.server.storage.RaftStorageTestUtils) RaftProperties(org.apache.ratis.conf.RaftProperties) LifeCycle(org.apache.ratis.util.LifeCycle) Timer(com.codahale.metrics.Timer) SimpleOperation(org.apache.ratis.RaftTestUtil.SimpleOperation) Assert(org.junit.Assert) Collections(java.util.Collections) TimeDuration(org.apache.ratis.util.TimeDuration) LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) LogEntryHeader(org.apache.ratis.server.raftlog.LogEntryHeader) Timer(com.codahale.metrics.Timer) TimeoutIOException(org.apache.ratis.protocol.exceptions.TimeoutIOException) IOException(java.io.IOException) RatisMetricRegistry(org.apache.ratis.metrics.RatisMetricRegistry) 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