Search in sources :

Example 1 with LogEntryHeader

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

the class LeaderStateImpl method updateCommit.

private void updateCommit(LogEntryHeader[] entriesToCommit) {
    final long newCommitIndex = raftLog.getLastCommittedIndex();
    logMetadata(newCommitIndex);
    commitIndexChanged();
    boolean hasConfiguration = false;
    for (LogEntryHeader entry : entriesToCommit) {
        if (entry.getIndex() > newCommitIndex) {
            break;
        }
        hasConfiguration |= entry.getLogEntryBodyCase() == LogEntryBodyCase.CONFIGURATIONENTRY;
        raftLog.getRaftLogMetrics().onLogEntryCommitted(entry);
    }
    if (hasConfiguration) {
        checkAndUpdateConfiguration();
    }
}
Also used : LogEntryHeader(org.apache.ratis.server.raftlog.LogEntryHeader)

Example 2 with LogEntryHeader

use of org.apache.ratis.server.raftlog.LogEntryHeader 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 3 with LogEntryHeader

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

the class MemoryRaftLogTest method testEntryPerformTruncation.

@Test
public void testEntryPerformTruncation() throws Exception {
    final RaftProperties prop = new RaftProperties();
    prop.setClass(MiniRaftCluster.STATEMACHINE_CLASS_KEY, SimpleStateMachine4Testing.class, StateMachine.class);
    final RaftPeerId peerId = RaftPeerId.valueOf("s0");
    final RaftGroupId groupId = RaftGroupId.randomId();
    final RaftGroupMemberId memberId = RaftGroupMemberId.valueOf(peerId, groupId);
    MemoryRaftLog raftLog = new MemoryRaftLog(memberId, () -> -1, prop);
    raftLog.open(RaftLog.INVALID_LOG_INDEX, null);
    LogEntryProto[] entries1 = new LogEntryProto[2];
    entries1[0] = LogEntryProto.newBuilder().setIndex(0).setTerm(0).build();
    entries1[1] = LogEntryProto.newBuilder().setIndex(1).setTerm(0).build();
    raftLog.append(entries1).forEach(CompletableFuture::join);
    LogEntryProto[] entries2 = new LogEntryProto[1];
    entries2[0] = LogEntryProto.newBuilder().setIndex(0).setTerm(2).build();
    raftLog.append(entries2).forEach(CompletableFuture::join);
    final LogEntryHeader[] termIndices = raftLog.getEntries(0, 10);
    assertEquals(1, termIndices.length);
    assertEquals(entries2[0].getIndex(), termIndices[0].getIndex());
    assertEquals(entries2[0].getTerm(), termIndices[0].getTerm());
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) LogEntryHeader(org.apache.ratis.server.raftlog.LogEntryHeader) RaftProperties(org.apache.ratis.conf.RaftProperties) RaftGroupId(org.apache.ratis.protocol.RaftGroupId) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftGroupMemberId(org.apache.ratis.protocol.RaftGroupMemberId) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Example 4 with LogEntryHeader

use of org.apache.ratis.server.raftlog.LogEntryHeader 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)

Example 5 with LogEntryHeader

use of org.apache.ratis.server.raftlog.LogEntryHeader 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);
}
Also used : LogEntryHeader(org.apache.ratis.server.raftlog.LogEntryHeader) LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto)

Aggregations

LogEntryHeader (org.apache.ratis.server.raftlog.LogEntryHeader)9 LogEntryProto (org.apache.ratis.proto.RaftProtos.LogEntryProto)6 CompletableFuture (java.util.concurrent.CompletableFuture)5 Test (org.junit.Test)5 BaseTest (org.apache.ratis.BaseTest)4 RaftProperties (org.apache.ratis.conf.RaftProperties)4 RaftGroupId (org.apache.ratis.protocol.RaftGroupId)4 RaftGroupMemberId (org.apache.ratis.protocol.RaftGroupMemberId)4 RaftPeerId (org.apache.ratis.protocol.RaftPeerId)4 RaftLog (org.apache.ratis.server.raftlog.RaftLog)4 TimeUnit (java.util.concurrent.TimeUnit)3 SimpleStateMachine4Testing (org.apache.ratis.statemachine.SimpleStateMachine4Testing)3 StateMachine (org.apache.ratis.statemachine.StateMachine)3 JavaUtils (org.apache.ratis.util.JavaUtils)3 Timer (com.codahale.metrics.Timer)2 File (java.io.File)2 IOException (java.io.IOException)2 ByteBuffer (java.nio.ByteBuffer)2 StandardCharsets (java.nio.charset.StandardCharsets)2 ArrayList (java.util.ArrayList)2