Backport NPE fix in SnapshotManager 59/34359/2
authorTom Pantelis <tpanteli@brocade.com>
Tue, 9 Feb 2016 21:11:30 +0000 (16:11 -0500)
committerTom Pantelis <tpanteli@brocade.com>
Tue, 9 Feb 2016 21:19:42 +0000 (16:19 -0500)
Backported  https://git.opendaylight.org/gerrit/#/c/26132/ that fixed
an NPE in the SnapshotManager if there is no log entry from which to obtain
the last term/index for the snapshot. In this case it uses the last
applied term/index.

Change-Id: Iabd7f7467fa09a71576611e8261a3c985e62a97c
Signed-off-by: Tom Pantelis <tpanteli@brocade.com>
opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/SnapshotManager.java

index 970fb8fc717c2b6d918c2e815fb9ef2b323a39fb..16b4b2e498261847c833950514e04140623b7da2 100644 (file)
@@ -211,8 +211,18 @@ public class SnapshotManager implements SnapshotState {
 
             List<ReplicatedLogEntry> unAppliedEntries = context.getReplicatedLog().getFrom(lastAppliedIndex + 1);
 
-            captureSnapshot = new CaptureSnapshot(lastLogEntry.getIndex(),
-                    lastLogEntry.getTerm(), lastAppliedIndex, lastAppliedTerm,
+            long lastLogEntryIndex = lastAppliedIndex;
+            long lastLogEntryTerm = lastAppliedTerm;
+            if(lastLogEntry != null) {
+                lastLogEntryIndex = lastLogEntry.getIndex();
+                lastLogEntryTerm = lastLogEntry.getTerm();
+            } else {
+                LOG.debug("{}: Capturing Snapshot : lastLogEntry is null. Using lastAppliedIndex {} and lastAppliedTerm {} instead.",
+                        persistenceId(), lastAppliedIndex, lastAppliedTerm);
+            }
+
+            captureSnapshot = new CaptureSnapshot(lastLogEntryIndex,
+                    lastLogEntryTerm, lastAppliedIndex, lastAppliedTerm,
                     newReplicatedToAllIndex, newReplicatedToAllTerm, unAppliedEntries, targetFollower != null);
 
             if(captureSnapshot.isInstallSnapshotInitiated()) {