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);
}
Aggregations