From: Tomas Cere Date: Tue, 6 Aug 2019 11:46:32 +0000 (+0200) Subject: Lost commit index when a snapshot is captured X-Git-Tag: release/magnesium~98 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=commitdiff_plain;h=aad11d48fcace37e3365388f387e16fa67257a25 Lost commit index when a snapshot is captured When we have no journals and a snapshot is recovered and immediately captured again, if there was no movement in the datastore we loose the lastIndex and term from the previous snapshot. We can safely reuse these from the previous snapshot when no entry is present in the ReplicatedLog. Change-Id: Iaeb71edc7ec865bec18dbcb436af76e592eea69d Signed-off-by: Tomas Cere Signed-off-by: Robert Varga --- diff --git a/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/SnapshotManager.java b/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/SnapshotManager.java index e6dc8bb367..7d9a1bbf5b 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/SnapshotManager.java +++ b/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/SnapshotManager.java @@ -170,14 +170,19 @@ public class SnapshotManager implements SnapshotState { List unAppliedEntries = context.getReplicatedLog().getFrom(lastAppliedIndex + 1); - long lastLogEntryIndex = lastAppliedIndex; - long lastLogEntryTerm = lastAppliedTerm; - if (lastLogEntry != null) { + final long lastLogEntryIndex; + final long lastLogEntryTerm; + if (lastLogEntry == null) { + // When we don't have journal present, for example two captureSnapshots executed right after another with no + // new journal we still want to preserve the index and term in the snapshot. + lastAppliedIndex = lastLogEntryIndex = context.getReplicatedLog().getSnapshotIndex(); + lastAppliedTerm = lastLogEntryTerm = context.getReplicatedLog().getSnapshotTerm(); + + log.debug("{}: Capturing Snapshot : lastLogEntry is null. Using snapshot values lastAppliedIndex {} and " + + "lastAppliedTerm {} instead.", persistenceId(), lastAppliedIndex, lastAppliedTerm); + } else { lastLogEntryIndex = lastLogEntry.getIndex(); lastLogEntryTerm = lastLogEntry.getTerm(); - } else { - log.debug("{}: Capturing Snapshot : lastLogEntry is null. Using lastAppliedIndex {} and " - + "lastAppliedTerm {} instead.", persistenceId(), lastAppliedIndex, lastAppliedTerm); } return new CaptureSnapshot(lastLogEntryIndex, lastLogEntryTerm, lastAppliedIndex, lastAppliedTerm, diff --git a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/SnapshotManagerTest.java b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/SnapshotManagerTest.java index 14b94d9fb7..88e9a613ca 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/SnapshotManagerTest.java +++ b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/SnapshotManagerTest.java @@ -194,12 +194,12 @@ public class SnapshotManagerTest extends AbstractActorTest { CaptureSnapshot captureSnapshot = snapshotManager.getCaptureSnapshot(); // LastIndex and LastTerm are picked up from the lastLogEntry - assertEquals(-1L, captureSnapshot.getLastIndex()); - assertEquals(-1L, captureSnapshot.getLastTerm()); + assertEquals(0, captureSnapshot.getLastIndex()); + assertEquals(0, captureSnapshot.getLastTerm()); // Since the actor does not have any followers (no peer addresses) lastApplied will be from lastLogEntry - assertEquals(-1L, captureSnapshot.getLastAppliedIndex()); - assertEquals(-1L, captureSnapshot.getLastAppliedTerm()); + assertEquals(0, captureSnapshot.getLastAppliedIndex()); + assertEquals(0, captureSnapshot.getLastAppliedTerm()); // assertEquals(-1L, captureSnapshot.getReplicatedToAllIndex());