Search in sources :

Example 1 with EntryWithData

use of org.apache.ratis.server.raftlog.RaftLog.EntryWithData 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)

Aggregations

LogEntryProto (org.apache.ratis.proto.RaftProtos.LogEntryProto)1 TermIndex (org.apache.ratis.server.protocol.TermIndex)1 EntryWithData (org.apache.ratis.server.raftlog.RaftLog.EntryWithData)1