Search in sources :

Example 31 with LogEntryProto

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;
}
Also used : LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) LogAppender(org.apache.ratis.server.leader.LogAppender)

Example 32 with LogEntryProto

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;
}
Also used : LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) Message(org.apache.ratis.protocol.Message) Expression(org.apache.ratis.examples.arithmetic.expression.Expression) AutoCloseableLock(org.apache.ratis.util.AutoCloseableLock) RaftPeerRole(org.apache.ratis.proto.RaftProtos.RaftPeerRole)

Example 33 with LogEntryProto

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);
}
Also used : LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) EntryWithData(org.apache.ratis.server.raftlog.RaftLog.EntryWithData) TermIndex(org.apache.ratis.server.protocol.TermIndex)

Example 34 with LogEntryProto

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;
}
Also used : LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) Message(org.apache.ratis.protocol.Message) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 35 with LogEntryProto

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);
}
Also used : LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) LogRecord(org.apache.ratis.server.raftlog.segmented.LogSegment.LogRecord) AutoCloseableLock(org.apache.ratis.util.AutoCloseableLock)

Aggregations

LogEntryProto (org.apache.ratis.proto.RaftProtos.LogEntryProto)61 Test (org.junit.Test)22 BaseTest (org.apache.ratis.BaseTest)20 SimpleOperation (org.apache.ratis.RaftTestUtil.SimpleOperation)15 CompletableFuture (java.util.concurrent.CompletableFuture)14 File (java.io.File)13 IOException (java.io.IOException)13 StateMachineLogEntryProto (org.apache.ratis.proto.RaftProtos.StateMachineLogEntryProto)12 RaftLog (org.apache.ratis.server.raftlog.RaftLog)12 RaftStorage (org.apache.ratis.server.storage.RaftStorage)11 ArrayList (java.util.ArrayList)10 RaftPeerId (org.apache.ratis.protocol.RaftPeerId)10 TermIndex (org.apache.ratis.server.protocol.TermIndex)9 LogEntryHeader (org.apache.ratis.server.raftlog.LogEntryHeader)7 RaftClient (org.apache.ratis.client.RaftClient)6 RaftProperties (org.apache.ratis.conf.RaftProperties)6 RaftGroupId (org.apache.ratis.protocol.RaftGroupId)6 RaftGroupMemberId (org.apache.ratis.protocol.RaftGroupMemberId)6 RaftServer (org.apache.ratis.server.RaftServer)6 RandomAccessFile (java.io.RandomAccessFile)5