use of org.apache.ratis.proto.RaftProtos.LogEntryProto in project incubator-ratis by apache.
the class LeaderStateImpl method start.
LogEntryProto start() {
// In the beginning of the new term, replicate a conf entry in order
// to finally commit entries in the previous term.
// Also this message can help identify the last committed index and the conf.
final LogEntryProto placeHolder = LogProtoUtils.toLogEntryProto(server.getRaftConf(), server.getState().getCurrentTerm(), raftLog.getNextIndex());
CodeInjectionForTesting.execute(APPEND_PLACEHOLDER, server.getId().toString(), null);
raftLog.append(placeHolder);
processor.start();
senders.forEach(LogAppender::start);
return placeHolder;
}
use of org.apache.ratis.proto.RaftProtos.LogEntryProto in project incubator-ratis by apache.
the class ArithmeticStateMachine method applyTransaction.
@Override
public CompletableFuture<Message> applyTransaction(TransactionContext trx) {
final LogEntryProto entry = trx.getLogEntry();
final AssignmentMessage assignment = new AssignmentMessage(entry.getStateMachineLogEntry().getLogData());
final long index = entry.getIndex();
final Double result;
try (AutoCloseableLock writeLock = writeLock()) {
result = assignment.evaluate(variables);
updateLastAppliedTermIndex(entry.getTerm(), index);
}
final Expression r = Expression.Utils.double2Expression(result);
final CompletableFuture<Message> f = CompletableFuture.completedFuture(Expression.Utils.toMessage(r));
final RaftPeerRole role = trx.getServerRole();
if (role == RaftPeerRole.LEADER) {
LOG.info("{}:{}-{}: {} = {}", role, getId(), index, assignment, r);
} else {
LOG.debug("{}:{}-{}: {} = {}", role, getId(), index, assignment, r);
}
if (LOG.isTraceEnabled()) {
LOG.trace("{}-{}: variables={}", getId(), index, variables);
}
return f;
}
use of org.apache.ratis.proto.RaftProtos.LogEntryProto in project incubator-ratis by apache.
the class LogAppenderBase method newAppendEntriesRequest.
@Override
public AppendEntriesRequestProto newAppendEntriesRequest(long callId, boolean heartbeat) throws RaftLogIOException {
final TermIndex previous = getPrevious(follower.getNextIndex());
final long snapshotIndex = follower.getSnapshotIndex();
final long heartbeatWaitTimeMs = getHeartbeatWaitTimeMs();
if (heartbeatWaitTimeMs <= 0L || heartbeat) {
// heartbeat
return leaderState.newAppendEntriesRequestProto(follower, Collections.emptyList(), previous, callId);
}
Preconditions.assertTrue(buffer.isEmpty(), () -> "buffer has " + buffer.getNumElements() + " elements.");
final long leaderNext = getRaftLog().getNextIndex();
final long followerNext = follower.getNextIndex();
final long halfMs = heartbeatWaitTimeMs / 2;
for (long next = followerNext; leaderNext > next && getHeartbeatWaitTimeMs() - halfMs > 0; ) {
if (!buffer.offer(getRaftLog().getEntryWithData(next++))) {
break;
}
}
if (buffer.isEmpty()) {
return null;
}
final List<LogEntryProto> protos = buffer.pollList(getHeartbeatWaitTimeMs(), EntryWithData::getEntry, (entry, time, exception) -> LOG.warn("Failed to get {} in {}: {}", entry, time, exception));
buffer.clear();
assertProtos(protos, followerNext, previous, snapshotIndex);
return leaderState.newAppendEntriesRequestProto(follower, protos, previous, callId);
}
use of org.apache.ratis.proto.RaftProtos.LogEntryProto in project incubator-ratis by apache.
the class StateMachineUpdater method applyLog.
private MemoizedSupplier<List<CompletableFuture<Message>>> applyLog() throws RaftLogIOException {
final MemoizedSupplier<List<CompletableFuture<Message>>> futures = MemoizedSupplier.valueOf(ArrayList::new);
final long committed = raftLog.getLastCommittedIndex();
for (long applied; (applied = getLastAppliedIndex()) < committed && state == State.RUNNING && !shouldStop(); ) {
final long nextIndex = applied + 1;
final LogEntryProto next = raftLog.get(nextIndex);
if (next != null) {
if (LOG.isTraceEnabled()) {
LOG.trace("{}: applying nextIndex={}, nextLog={}", this, nextIndex, LogProtoUtils.toLogEntryString(next));
} else {
LOG.debug("{}: applying nextIndex={}", this, nextIndex);
}
final CompletableFuture<Message> f = server.applyLogToStateMachine(next);
if (f != null) {
futures.get().add(f);
}
final long incremented = appliedIndex.incrementAndGet(debugIndexChange);
Preconditions.assertTrue(incremented == nextIndex);
} else {
LOG.debug("{}: logEntry {} is null. There may be snapshot to load. state:{}", this, nextIndex, state);
break;
}
}
return futures;
}
use of org.apache.ratis.proto.RaftProtos.LogEntryProto in project incubator-ratis by apache.
the class SegmentedRaftLog method get.
@Override
public LogEntryProto get(long index) throws RaftLogIOException {
checkLogState();
final LogSegment segment;
final LogRecord record;
try (AutoCloseableLock readLock = readLock()) {
segment = cache.getSegment(index);
if (segment == null) {
return null;
}
record = segment.getLogRecord(index);
if (record == null) {
return null;
}
final LogEntryProto entry = segment.getEntryFromCache(record.getTermIndex());
if (entry != null) {
getRaftLogMetrics().onRaftLogCacheHit();
return entry;
}
}
// the entry is not in the segment's cache. Load the cache without holding the lock.
getRaftLogMetrics().onRaftLogCacheMiss();
checkAndEvictCache();
return segment.loadCache(record);
}
Aggregations