Search in sources :

Example 6 with LogEntryProto

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

the class ServerRestartTests method runTestRestartCommitIndex.

void runTestRestartCommitIndex(MiniRaftCluster cluster) throws Exception {
    final SimpleMessage[] messages = SimpleMessage.create(100);
    final List<CompletableFuture<Void>> futures = new ArrayList<>(messages.length);
    for (int i = 0; i < messages.length; i++) {
        final CompletableFuture<Void> f = new CompletableFuture<>();
        futures.add(f);
        final SimpleMessage m = messages[i];
        new Thread(() -> {
            try (final RaftClient client = cluster.createClient()) {
                Assert.assertTrue(client.io().send(m).isSuccess());
            } catch (IOException e) {
                throw new IllegalStateException("Failed to send " + m, e);
            }
            f.complete(null);
        }).start();
    }
    JavaUtils.allOf(futures).get();
    final List<RaftPeerId> ids = new ArrayList<>();
    final RaftServer.Division leader = cluster.getLeader();
    final RaftLog leaderLog = leader.getRaftLog();
    final RaftPeerId leaderId = leader.getId();
    ids.add(leaderId);
    RaftTestUtil.getStateMachineLogEntries(leaderLog);
    // check that the last metadata entry is written to the log
    JavaUtils.attempt(() -> assertLastLogEntry(leader), 20, HUNDRED_MILLIS, "leader last metadata entry", LOG);
    final long lastIndex = leaderLog.getLastEntryTermIndex().getIndex();
    LOG.info("{}: leader lastIndex={}", leaderId, lastIndex);
    final LogEntryProto lastEntry = leaderLog.get(lastIndex);
    LOG.info("{}: leader lastEntry entry[{}] = {}", leaderId, lastIndex, LogProtoUtils.toLogEntryString(lastEntry));
    final long loggedCommitIndex = lastEntry.getMetadataEntry().getCommitIndex();
    final LogEntryProto lastCommittedEntry = leaderLog.get(loggedCommitIndex);
    LOG.info("{}: leader lastCommittedEntry = entry[{}] = {}", leaderId, loggedCommitIndex, LogProtoUtils.toLogEntryString(lastCommittedEntry));
    final SimpleStateMachine4Testing leaderStateMachine = SimpleStateMachine4Testing.get(leader);
    final TermIndex lastAppliedTermIndex = leaderStateMachine.getLastAppliedTermIndex();
    LOG.info("{}: leader lastAppliedTermIndex = {}", leaderId, lastAppliedTermIndex);
    // check follower logs
    for (RaftServer.Division s : cluster.iterateDivisions()) {
        if (!s.getId().equals(leaderId)) {
            ids.add(s.getId());
            JavaUtils.attempt(() -> RaftTestUtil.assertSameLog(leaderLog, s.getRaftLog()), 10, HUNDRED_MILLIS, "assertRaftLog-" + s.getId(), LOG);
        }
    }
    // take snapshot and truncate last (metadata) entry
    leaderStateMachine.takeSnapshot();
    leaderLog.truncate(lastIndex);
    // kill all servers
    ids.forEach(cluster::killServer);
    // Restart and kill servers one by one so that they won't talk to each other.
    for (RaftPeerId id : ids) {
        cluster.restartServer(id, false);
        final RaftServer.Division server = cluster.getDivision(id);
        final RaftLog raftLog = server.getRaftLog();
        JavaUtils.attemptRepeatedly(() -> {
            Assert.assertTrue(raftLog.getLastCommittedIndex() >= loggedCommitIndex);
            return null;
        }, 10, HUNDRED_MILLIS, id + "(commitIndex >= loggedCommitIndex)", LOG);
        JavaUtils.attemptRepeatedly(() -> {
            Assert.assertTrue(server.getInfo().getLastAppliedIndex() >= loggedCommitIndex);
            return null;
        }, 10, HUNDRED_MILLIS, id + "(lastAppliedIndex >= loggedCommitIndex)", LOG);
        LOG.info("{}: commitIndex={}, lastAppliedIndex={}", id, raftLog.getLastCommittedIndex(), server.getInfo().getLastAppliedIndex());
        cluster.killServer(id);
    }
}
Also used : SimpleStateMachine4Testing(org.apache.ratis.statemachine.SimpleStateMachine4Testing) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) ArrayList(java.util.ArrayList) RaftLogIOException(org.apache.ratis.server.raftlog.RaftLogIOException) IOException(java.io.IOException) TermIndex(org.apache.ratis.server.protocol.TermIndex) CompletableFuture(java.util.concurrent.CompletableFuture) LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftClient(org.apache.ratis.client.RaftClient) RaftLog(org.apache.ratis.server.raftlog.RaftLog) TestSegmentedRaftLog(org.apache.ratis.server.raftlog.segmented.TestSegmentedRaftLog)

Example 7 with LogEntryProto

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

the class ServerRestartTests method assertLastLogEntry.

static void assertLastLogEntry(RaftServer.Division server) throws RaftLogIOException {
    final RaftLog raftLog = server.getRaftLog();
    final long lastIndex = raftLog.getLastEntryTermIndex().getIndex();
    final LogEntryProto lastEntry = raftLog.get(lastIndex);
    Assert.assertTrue(lastEntry.hasMetadataEntry());
    final long loggedCommitIndex = lastEntry.getMetadataEntry().getCommitIndex();
    final LogEntryProto lastCommittedEntry = raftLog.get(loggedCommitIndex);
    Assert.assertTrue(lastCommittedEntry.hasStateMachineLogEntry());
    final SimpleStateMachine4Testing leaderStateMachine = SimpleStateMachine4Testing.get(server);
    final TermIndex lastAppliedTermIndex = leaderStateMachine.getLastAppliedTermIndex();
    Assert.assertEquals(lastCommittedEntry.getTerm(), lastAppliedTermIndex.getTerm());
    Assert.assertTrue(lastCommittedEntry.getIndex() <= lastAppliedTermIndex.getIndex());
}
Also used : SimpleStateMachine4Testing(org.apache.ratis.statemachine.SimpleStateMachine4Testing) LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) RaftLog(org.apache.ratis.server.raftlog.RaftLog) TestSegmentedRaftLog(org.apache.ratis.server.raftlog.segmented.TestSegmentedRaftLog) TermIndex(org.apache.ratis.server.protocol.TermIndex)

Example 8 with LogEntryProto

use of org.apache.ratis.proto.RaftProtos.LogEntryProto 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 9 with LogEntryProto

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

the class TestCacheEviction method generateEntries.

private LogEntryProto[] generateEntries(List<SegmentRange> slist) {
    List<LogEntryProto> eList = new ArrayList<>();
    for (SegmentRange range : slist) {
        for (long index = range.start; index <= range.end; index++) {
            SimpleOperation m = new SimpleOperation(new String(new byte[1024]));
            eList.add(LogProtoUtils.toLogEntryProto(m.getLogEntryContent(), range.term, index));
        }
    }
    return eList.toArray(new LogEntryProto[eList.size()]);
}
Also used : SegmentRange(org.apache.ratis.server.raftlog.segmented.TestSegmentedRaftLog.SegmentRange) LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) ArrayList(java.util.ArrayList) SimpleOperation(org.apache.ratis.RaftTestUtil.SimpleOperation)

Example 10 with LogEntryProto

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

the class TestRaftLogReadWrite method testReadWriteLog.

/**
 * Test basic functionality: write several log entries, then read
 */
@Test
public void testReadWriteLog() throws IOException {
    final RaftStorage storage = RaftStorageTestUtils.newRaftStorage(storageDir);
    final File openSegment = LogSegmentStartEnd.valueOf(0).getFile(storage);
    long size = SegmentedRaftLogFormat.getHeaderLength();
    final LogEntryProto[] entries = new LogEntryProto[100];
    try (SegmentedRaftLogOutputStream out = new SegmentedRaftLogOutputStream(openSegment, false, segmentMaxSize, preallocatedSize, ByteBuffer.allocateDirect(bufferSize))) {
        size += writeMessages(entries, out);
    } finally {
        storage.close();
    }
    Assert.assertEquals(size, openSegment.length());
    final LogEntryProto[] readEntries = readLog(openSegment, 0, RaftLog.INVALID_LOG_INDEX, true);
    Assert.assertArrayEquals(entries, readEntries);
}
Also used : LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) RaftStorage(org.apache.ratis.server.storage.RaftStorage) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) 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