/**
* Performs a commit operation.
*/
private synchronized void replicate() {
final long prevIndex = sendIndex - 1;
final CopycatEntry prevEntry = log.getEntry(prevIndex);
// Create a list of up to ten entries to send to the follower.
// We can only send one snapshot entry in any given request. So, if any of
// the entries are snapshot entries, send all entries up to the snapshot and
// then send snapshot entries individually.
List<CopycatEntry> entries = new ArrayList<>(BATCH_SIZE);
long lastIndex = Math.min(sendIndex + BATCH_SIZE - 1, log.lastIndex());
for (long i = sendIndex; i <= lastIndex; i++) {
CopycatEntry entry = log.getEntry(i);
if (entry instanceof SnapshotEntry) {
if (entries.isEmpty()) {
doSync(prevIndex, prevEntry, Collections.singletonList(entry));
} else {
doSync(prevIndex, prevEntry, entries);