- if (appendEntries.getEntries() != null && appendEntries.getEntries().size() > 0) {
-
- log.debug("{}: Number of entries to be appended = {}", logName(),
- appendEntries.getEntries().size());
-
- // 3. If an existing entry conflicts with a new one (same index
- // but different terms), delete the existing entry and all that
- // follow it (§5.3)
- int addEntriesFrom = 0;
- if (context.getReplicatedLog().size() > 0) {
-
- // Find the entry up until the one that is not in the follower's log
- for (int i = 0;i < appendEntries.getEntries().size(); i++, addEntriesFrom++) {
- ReplicatedLogEntry matchEntry = appendEntries.getEntries().get(i);
-
- if (!isLogEntryPresent(matchEntry.getIndex())) {
- // newEntry not found in the log
- break;
- }
-
- long existingEntryTerm = getLogEntryTerm(matchEntry.getIndex());
-
- log.debug("{}: matchEntry {} is present: existingEntryTerm: {}", logName(), matchEntry,
- existingEntryTerm);
-
- // existingEntryTerm == -1 means it's in the snapshot and not in the log. We don't know
- // what the term was so we'll assume it matches.
- if (existingEntryTerm == -1 || existingEntryTerm == matchEntry.getTerm()) {
- continue;
- }
-
- if (!context.getRaftPolicy().applyModificationToStateBeforeConsensus()) {
-
- log.info("{}: Removing entries from log starting at {}", logName(), matchEntry.getIndex());
-
- // Entries do not match so remove all subsequent entries
- if (!context.getReplicatedLog().removeFromAndPersist(matchEntry.getIndex())) {
- // Could not remove the entries - this means the matchEntry index must be in the
- // snapshot and not the log. In this case the prior entries are part of the state
- // so we must send back a reply to force a snapshot to completely re-sync the
- // follower's log and state.
-
- log.info("{}: Could not remove entries - sending reply to force snapshot", logName());
- updateInitialSyncStatus(appendEntries.getLeaderCommit(), appendEntries.getLeaderId());
- sender.tell(new AppendEntriesReply(context.getId(), currentTerm(), false, lastIndex,
- lastTerm(), context.getPayloadVersion(), true), actor());
- return this;
- }
-
- break;
- } else {
- updateInitialSyncStatus(appendEntries.getLeaderCommit(), appendEntries.getLeaderId());
- sender.tell(new AppendEntriesReply(context.getId(), currentTerm(), false, lastIndex,
- lastTerm(), context.getPayloadVersion(), true), actor());
- return this;
- }
- }
- }
-
- lastIndex = lastIndex();
- log.debug("{}: After cleanup, lastIndex: {}, entries to be added from: {}", logName(),
- lastIndex, addEntriesFrom);
-
- // When persistence successfully completes for each new log entry appended, we need to determine if we
- // should capture a snapshot to compact the persisted log. shouldCaptureSnapshot tracks whether or not
- // one of the log entries has exceeded the log size threshold whereby a snapshot should be taken. However
- // we don't initiate the snapshot at that log entry but rather after the last log entry has been persisted.
- // This is done because subsequent log entries after the one that tripped the threshold may have been
- // applied to the state already, as the persistence callback occurs async, and we want those entries
- // purged from the persisted log as well.
- final AtomicBoolean shouldCaptureSnapshot = new AtomicBoolean(false);
- final Procedure<ReplicatedLogEntry> appendAndPersistCallback = logEntry -> {
- final ReplicatedLogEntry lastEntryToAppend = appendEntries.getEntries().get(
- appendEntries.getEntries().size() - 1);
- if (shouldCaptureSnapshot.get() && logEntry == lastEntryToAppend) {
- context.getSnapshotManager().capture(context.getReplicatedLog().last(), getReplicatedToAllIndex());
- }
- };
-
- // 4. Append any new entries not already in the log
- for (int i = addEntriesFrom; i < appendEntries.getEntries().size(); i++) {
- ReplicatedLogEntry entry = appendEntries.getEntries().get(i);
-
- log.debug("{}: Append entry to log {}", logName(), entry.getData());
-
- context.getReplicatedLog().appendAndPersist(entry, appendAndPersistCallback, false);
-
- shouldCaptureSnapshot.compareAndSet(false,
- context.getReplicatedLog().shouldCaptureSnapshot(entry.getIndex()));
-
- if (entry.getData() instanceof ServerConfigurationPayload) {
- context.updatePeerIds((ServerConfigurationPayload)entry.getData());
- }
- }
-
- log.debug("{}: Log size is now {}", logName(), context.getReplicatedLog().size());