Search in sources :

Example 16 with TermIndex

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

the class RaftServerImpl method appendEntriesAsync.

@Override
public CompletableFuture<AppendEntriesReplyProto> appendEntriesAsync(AppendEntriesRequestProto r) throws IOException {
    // TODO avoid converting list to array
    final RaftRpcRequestProto request = r.getServerRequest();
    final LogEntryProto[] entries = r.getEntriesList().toArray(new LogEntryProto[r.getEntriesCount()]);
    final TermIndex previous = r.hasPreviousLog() ? ServerProtoUtils.toTermIndex(r.getPreviousLog()) : null;
    return appendEntriesAsync(RaftPeerId.valueOf(request.getRequestorId()), ProtoUtils.toRaftGroupId(request.getRaftGroupId()), r.getLeaderTerm(), previous, r.getLeaderCommit(), r.getInitializing(), r.getCommitInfosList(), entries);
}
Also used : TermIndex(org.apache.ratis.server.protocol.TermIndex)

Example 17 with TermIndex

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

the class SegmentedRaftLog method append.

@Override
public List<CompletableFuture<Long>> append(LogEntryProto... entries) {
    checkLogState();
    if (entries == null || entries.length == 0) {
        return Collections.emptyList();
    }
    try (AutoCloseableLock writeLock = writeLock()) {
        Iterator<TermIndex> iter = cache.iterator(entries[0].getIndex());
        int index = 0;
        long truncateIndex = -1;
        for (; iter.hasNext() && index < entries.length; index++) {
            TermIndex storedEntry = iter.next();
            Preconditions.assertTrue(storedEntry.getIndex() == entries[index].getIndex(), "The stored entry's index %s is not consistent with" + " the received entries[%s]'s index %s", storedEntry.getIndex(), index, entries[index].getIndex());
            if (storedEntry.getTerm() != entries[index].getTerm()) {
                // we should truncate from the storedEntry's index
                truncateIndex = storedEntry.getIndex();
                if (LOG.isTraceEnabled()) {
                    LOG.trace("{}: truncate to {}, index={}, ti={}, storedEntry={}, entries={}", server.getId(), truncateIndex, index, ServerProtoUtils.toTermIndex(entries[index]), storedEntry, ServerProtoUtils.toString(entries));
                }
                while (true) {
                    try {
                        final LogEntryProto entry = get(storedEntry.getIndex());
                        server.failClientRequest(entry);
                    } catch (RaftLogIOException e) {
                        LOG.error("Failed to read log " + storedEntry, e);
                    }
                    if (iter.hasNext()) {
                        storedEntry = iter.next();
                    } else {
                        break;
                    }
                }
                break;
            }
        }
        final List<CompletableFuture<Long>> futures;
        if (truncateIndex != -1) {
            futures = new ArrayList<>(entries.length - index + 1);
            futures.add(truncate(truncateIndex));
        } else {
            futures = new ArrayList<>(entries.length - index);
        }
        for (int i = index; i < entries.length; i++) {
            futures.add(appendEntry(entries[i]));
        }
        return futures;
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) LogEntryProto(org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto) AutoCloseableLock(org.apache.ratis.util.AutoCloseableLock) TermIndex(org.apache.ratis.server.protocol.TermIndex)

Example 18 with TermIndex

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

the class RaftTestUtil method assertLogEntries.

static void assertLogEntries(RaftLog log, boolean async, long expectedTerm, SimpleMessage... expectedMessages) {
    final TermIndex[] termIndices = log.getEntries(1, Long.MAX_VALUE);
    final List<LogEntryProto> entries = new ArrayList<>(expectedMessages.length);
    for (TermIndex ti : termIndices) {
        final LogEntryProto e;
        try {
            e = log.get(ti.getIndex());
        } catch (IOException exception) {
            throw new AssertionError("Failed to get log at " + ti, exception);
        }
        if (e.getLogEntryBodyCase() == LogEntryProto.LogEntryBodyCase.SMLOGENTRY) {
            entries.add(e);
        } else if (e.getLogEntryBodyCase() == LogEntryProto.LogEntryBodyCase.NOOP) {
            LOG.info("Found " + LogEntryProto.LogEntryBodyCase.NOOP + " at " + ti + ", ignoring it.");
        } else {
            throw new AssertionError("Unexpected LogEntryBodyCase " + e.getLogEntryBodyCase() + " at " + ti);
        }
    }
    long logIndex = 0;
    Assert.assertEquals(expectedMessages.length, entries.size());
    for (int i = 0; i < expectedMessages.length; i++) {
        final LogEntryProto e = entries.get(i);
        Assert.assertTrue(e.getTerm() >= expectedTerm);
        if (e.getTerm() > expectedTerm) {
            expectedTerm = e.getTerm();
        }
        if (!async) {
            Assert.assertTrue(e.getIndex() > logIndex);
        }
        logIndex = e.getIndex();
        Assert.assertArrayEquals(expectedMessages[i].getContent().toByteArray(), e.getSmLogEntry().getData().toByteArray());
    }
}
Also used : SMLogEntryProto(org.apache.ratis.shaded.proto.RaftProtos.SMLogEntryProto) LogEntryProto(org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto) IOException(java.io.IOException) TermIndex(org.apache.ratis.server.protocol.TermIndex)

Example 19 with TermIndex

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

the class ArithmeticStateMachine method load.

private long load(SingleFileSnapshotInfo snapshot, boolean reload) throws IOException {
    if (snapshot == null) {
        LOG.warn("The snapshot info is null.");
        return RaftServerConstants.INVALID_LOG_INDEX;
    }
    final File snapshotFile = snapshot.getFile().getPath().toFile();
    if (!snapshotFile.exists()) {
        LOG.warn("The snapshot file {} does not exist for snapshot {}", snapshotFile, snapshot);
        return RaftServerConstants.INVALID_LOG_INDEX;
    }
    final TermIndex last = SimpleStateMachineStorage.getTermIndexFromSnapshotFile(snapshotFile);
    try (final AutoCloseableLock writeLock = writeLock();
        final ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(snapshotFile)))) {
        if (reload) {
            reset();
        }
        setLastAppliedTermIndex(last);
        variables.putAll((Map<String, Double>) in.readObject());
    } catch (ClassNotFoundException e) {
        throw new IllegalStateException(e);
    }
    return last.getIndex();
}
Also used : AutoCloseableLock(org.apache.ratis.util.AutoCloseableLock) TermIndex(org.apache.ratis.server.protocol.TermIndex)

Example 20 with TermIndex

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

the class TestRaftStream method checkLog.

private void checkLog(RaftLog raftLog, long expectedCommittedIndex, Supplier<byte[]> s) throws IOException {
    long committedIndex = raftLog.getLastCommittedIndex();
    Assert.assertEquals(expectedCommittedIndex, committedIndex);
    // check the log content
    TermIndex[] entries = raftLog.getEntries(1, expectedCommittedIndex + 1);
    for (TermIndex entry : entries) {
        RaftProtos.LogEntryProto log = raftLog.get(entry.getIndex());
        byte[] logData = log.getSmLogEntry().getData().toByteArray();
        byte[] expected = s.get();
        LOG.info("log " + entry + " " + log.getLogEntryBodyCase() + " " + StringUtils.bytes2HexString(logData));
        Assert.assertEquals(expected.length, logData.length);
        Assert.assertArrayEquals(expected, logData);
    }
}
Also used : RaftProtos(org.apache.ratis.shaded.proto.RaftProtos) TermIndex(org.apache.ratis.server.protocol.TermIndex)

Aggregations

TermIndex (org.apache.ratis.server.protocol.TermIndex)25 IOException (java.io.IOException)8 File (java.io.File)7 AutoCloseableLock (org.apache.ratis.util.AutoCloseableLock)6 SnapshotInfo (org.apache.ratis.statemachine.SnapshotInfo)5 BaseTest (org.apache.ratis.BaseTest)4 LogEntryProto (org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto)4 Test (org.junit.Test)4 Timer (com.codahale.metrics.Timer)3 FileNotFoundException (java.io.FileNotFoundException)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 CompletionException (java.util.concurrent.CompletionException)3 MD5Hash (org.apache.ratis.io.MD5Hash)3 RaftServerImpl (org.apache.ratis.server.impl.RaftServerImpl)3 FileInfo (org.apache.ratis.server.storage.FileInfo)3 SingleFileSnapshotInfo (org.apache.ratis.statemachine.impl.SingleFileSnapshotInfo)3 AbortedException (alluxio.exception.status.AbortedException)2 AlluxioStatusException (alluxio.exception.status.AlluxioStatusException)2 NotFoundException (alluxio.exception.status.NotFoundException)2 FileOutputStream (java.io.FileOutputStream)2