use of org.apache.ratis.proto.RaftProtos.LogEntryProto in project incubator-ratis by apache.
the class SegmentedRaftLog method newServerLogMethods.
/**
* When the server is null, return the dummy instance of {@link ServerLogMethods}.
* Otherwise, the server is non-null, return the implementation using the given server.
*/
private ServerLogMethods newServerLogMethods(RaftServer.Division impl, Consumer<LogEntryProto> notifyTruncatedLogEntry) {
if (impl == null) {
return ServerLogMethods.DUMMY;
}
return new ServerLogMethods() {
@Override
public long[] getFollowerNextIndices() {
return impl.getInfo().getFollowerNextIndices();
}
@Override
public long getLastAppliedIndex() {
return impl.getInfo().getLastAppliedIndex();
}
@Override
public void notifyTruncatedLogEntry(TermIndex ti) {
try {
final LogEntryProto entry = get(ti.getIndex());
notifyTruncatedLogEntry.accept(entry);
} catch (RaftLogIOException e) {
LOG.error("{}: Failed to read log {}", getName(), ti, e);
}
}
};
}
use of org.apache.ratis.proto.RaftProtos.LogEntryProto in project incubator-ratis by apache.
the class LogSegment method getEntrySize.
static long getEntrySize(LogEntryProto entry, Op op) {
LogEntryProto e = entry;
if (op == Op.CHECK_SEGMENT_FILE_FULL) {
e = LogProtoUtils.removeStateMachineData(entry);
} else if (op == Op.LOAD_SEGMENT_FILE || op == Op.WRITE_CACHE_WITH_STATE_MACHINE_CACHE) {
Preconditions.assertTrue(entry == LogProtoUtils.removeStateMachineData(entry), () -> "Unexpected LogEntryProto with StateMachine data: op=" + op + ", entry=" + entry);
} else {
Preconditions.assertTrue(op == Op.WRITE_CACHE_WITHOUT_STATE_MACHINE_CACHE || op == Op.REMOVE_CACHE, () -> "Unexpected op " + op + ", entry=" + entry);
}
final int serialized = e.getSerializedSize();
return serialized + CodedOutputStream.computeUInt32SizeNoTag(serialized) + 4L;
}
use of org.apache.ratis.proto.RaftProtos.LogEntryProto in project incubator-ratis by apache.
the class SegmentedRaftLogInputStream method scanEditLog.
/**
* Find the last valid entry index in the stream.
* If there are invalid or corrupt entries in the middle of the stream,
* scanEditLog will skip over them.
*
* This reads through the stream but does not close it.
*
* @param maxIndexToScan Maximum entry index to try to scan. The scan returns
* after reading this or a higher index. The file
* portion beyond this index is potentially being
* updated.
*/
static LogValidation scanEditLog(SegmentedRaftLogInputStream in, long maxIndexToScan) {
long lastPos = 0;
long end = INVALID_LOG_INDEX;
long numValid = 0;
boolean hitError = false;
while (end < maxIndexToScan) {
long index;
lastPos = in.getPosition();
try {
if (hitError) {
LogEntryProto entry = in.nextEntry();
index = entry != null ? entry.getIndex() : INVALID_LOG_INDEX;
LOG.warn("After resync, position is " + in.getPosition());
} else {
index = in.scanNextEntry();
}
if (index == INVALID_LOG_INDEX) {
break;
} else {
hitError = false;
}
} catch (Exception e) {
LOG.warn("Caught exception after scanning through {} ops from {}" + " while determining its valid length. Position was " + lastPos, numValid, in, e);
hitError = true;
continue;
}
if (end == INVALID_LOG_INDEX || index > end) {
end = index;
}
numValid++;
}
return new LogValidation(lastPos, end, false);
}
use of org.apache.ratis.proto.RaftProtos.LogEntryProto in project incubator-ratis by apache.
the class SegmentedRaftLogInputStream method nextEntry.
public LogEntryProto nextEntry() throws IOException {
if (state.isUnopened()) {
try {
init();
} catch (Exception e) {
LOG.error("caught exception initializing " + this, e);
throw IOUtils.asIOException(e);
}
}
Preconditions.assertTrue(!state.isUnopened());
if (state.isOpened()) {
final LogEntryProto entry = reader.readEntry();
if (entry != null) {
long index = entry.getIndex();
if (!isOpen() && index >= endIndex) {
/*
* The end index may be derived from the segment recovery
* process. It is possible that we still have some uncleaned garbage
* in the end. We should skip them.
*/
long skipAmt = logFile.length() - reader.getPos();
if (skipAmt > 0) {
LOG.debug("skipping {} bytes at the end of log '{}': reached" + " entry {} out of {}", skipAmt, getName(), index, endIndex);
reader.skipFully(skipAmt);
}
}
}
return entry;
} else if (state.isClosed()) {
return null;
}
throw new IOException("Failed to get next entry from " + this, state.getThrowable());
}
use of org.apache.ratis.proto.RaftProtos.LogEntryProto in project incubator-ratis by apache.
the class RaftLogBase method appendImpl.
private long appendImpl(long term, TransactionContext operation) throws StateMachineException {
checkLogState();
try (AutoCloseableLock writeLock = writeLock()) {
final long nextIndex = getNextIndex();
// the SM wants to attach a logic depending on ordered execution in the log commit order.
try {
operation = operation.preAppendTransaction();
} catch (StateMachineException e) {
throw e;
} catch (IOException e) {
throw new StateMachineException(memberId, e);
}
// build the log entry after calling the StateMachine
final LogEntryProto e = operation.initLogEntry(term, nextIndex);
int entrySize = e.getSerializedSize();
if (entrySize > maxBufferSize) {
throw new StateMachineException(memberId, new RaftLogIOException("Log entry size " + entrySize + " exceeds the max buffer limit of " + maxBufferSize));
}
appendEntry(e);
return nextIndex;
}
}
Aggregations