// if the last rpc call was successful then try to send
// as many updates as we can, otherwise, if we're
// probing backwards, only send one entry as a probe, as
// soon as we have a successful call forwards will become
// true and we can catch up quickly
GetEntriesResult result = log.getEntriesFrom(nextIndex, forwards ? BATCH_SIZE : 1);
final AppendEntries request = AppendEntries.newBuilder()
.setTerm(log.currentTerm())
.setLeaderId(log.self().toString())
.setPrevLogIndex(result.lastLogIndex())
.setPrevLogTerm(result.lastLogTerm())
.setCommitIndex(log.commitIndex())
.addAllEntries(result.entries())
.build();
LOGGER.debug("Sending update to {} prevLogIndex: {}, prevLogTerm: {}, nr. of entries {}", remote, result.lastLogIndex(),
result.lastLogTerm(), result.entries().size());
final ListenableFuture<AppendEntriesResponse> response = client.appendEntries(remote, request);
final SettableFuture<AppendEntriesResponse> previousResponse = nextResponse;
Futures.addCallback(response, new FutureCallback<AppendEntriesResponse>() {
@Override
public void onSuccess(@Nullable AppendEntriesResponse result) {
waitingForResponse = false;
if (result != null) {
updateNextIndex(request, result);
if (result.getSuccess()) {
previousResponse.set(result);
}
}
}