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);
}
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;
}
}
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());
}
}
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();
}
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);
}
}
Aggregations