in ratis-logservice/src/main/java/org/apache/ratis/logservice/server/LogServiceRaftLogReader.java [102:164]
private void loadNext() throws RaftLogIOException, InvalidProtocolBufferException {
// Clear the old "current" record
currentRecord = null;
if (LOG.isTraceEnabled()) {
LOG.trace("Loading next value: raftIndex={}, recordId={}, proto='{}', offset={}",
currentRaftIndex, currentRecordId,
currentLogEntry == null ? "null" : TextFormat.shortDebugString(currentLogEntry),
currentLogEntryOffset);
}
// Continue iterating over the current entry.
if (currentLogEntry != null) {
assert currentLogEntryOffset != -1;
currentLogEntryOffset++;
// We have an element to read from our current entry
if (currentLogEntryOffset < currentLogEntry.getDataCount()) {
currentRecord = currentLogEntry.getData(currentLogEntryOffset);
return;
}
// We don't have an element in our current entry so null it out.
currentLogEntry = null;
currentLogEntryOffset = -1;
// Also, increment to the next element in the RaftLog
currentRaftIndex++;
}
// Make sure we don't read off the end of the Raft log
for (; currentRaftIndex <= raftLog.getLastCommittedIndex(); currentRaftIndex++) {
try {
LogEntryProto entry = raftLog.get(currentRaftIndex);
if (LOG.isTraceEnabled()) {
LOG.trace("Raft Index: {} Entry: {}", currentRaftIndex,
TextFormat.shortDebugString(entry));
}
if (entry == null || entry.hasConfigurationEntry()) {
continue;
}
LogServiceRequestProto logServiceProto =
LogServiceRequestProto.parseFrom(entry.getStateMachineLogEntry().getLogData());
// TODO is it possible to get LogService messages that aren't appends?
if (RequestCase.APPENDREQUEST != logServiceProto.getRequestCase()) {
continue;
}
currentLogEntry = logServiceProto.getAppendRequest();
currentLogEntryOffset = 0;
if (currentLogEntry.getDataCount() > 0) {
currentRecord = currentLogEntry.getData(currentLogEntryOffset);
return;
}
currentLogEntry = null;
currentLogEntryOffset = -1;
} catch (RaftLogIOException e) {
LOG.error("Caught exception reading from RaftLog", e);
throw e;
} catch (InvalidProtocolBufferException e) {
LOG.error("Caught exception reading LogService protobuf from RaftLog", e);
throw e;
}
}
// If we make it here, we've read off the end of the RaftLog.
}