X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-akka-raft%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fraft%2FSnapshotManager.java;h=cffd4222220041e5bf9a8ccf7cb2ea7ebff996a1;hb=1b7014bab4524e9915daa682b89f850cbead3c6c;hp=26d8c0af084a5233437896539df7d5db6f439ad8;hpb=5aa58404a8ee1ad053742780439823309360a3a1;p=controller.git 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 26d8c0af08..cffd422222 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 @@ -15,6 +15,7 @@ import java.util.List; import org.opendaylight.controller.cluster.raft.base.messages.ApplySnapshot; import org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshot; import org.opendaylight.controller.cluster.raft.base.messages.SendInstallSnapshot; +import org.opendaylight.controller.cluster.raft.base.messages.SnapshotComplete; import org.opendaylight.controller.cluster.raft.behaviors.RaftActorBehavior; import org.slf4j.Logger; @@ -115,6 +116,37 @@ public class SnapshotManager implements SnapshotState { return context.getId(); } + public CaptureSnapshot newCaptureSnapshot(ReplicatedLogEntry lastLogEntry, long replicatedToAllIndex, + boolean installSnapshotInitiated) { + TermInformationReader lastAppliedTermInfoReader = + lastAppliedTermInformationReader.init(context.getReplicatedLog(), context.getLastApplied(), + lastLogEntry, hasFollowers()); + + long lastAppliedIndex = lastAppliedTermInfoReader.getIndex(); + long lastAppliedTerm = lastAppliedTermInfoReader.getTerm(); + + TermInformationReader replicatedToAllTermInfoReader = + replicatedToAllTermInformationReader.init(context.getReplicatedLog(), replicatedToAllIndex); + + long newReplicatedToAllIndex = replicatedToAllTermInfoReader.getIndex(); + long newReplicatedToAllTerm = replicatedToAllTermInfoReader.getTerm(); + + List unAppliedEntries = context.getReplicatedLog().getFrom(lastAppliedIndex + 1); + + 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); + } + + return new CaptureSnapshot(lastLogEntryIndex, lastLogEntryTerm, lastAppliedIndex, lastAppliedTerm, + newReplicatedToAllIndex, newReplicatedToAllTerm, unAppliedEntries, installSnapshotInitiated); + } + private class AbstractSnapshotState implements SnapshotState { @Override @@ -199,36 +231,7 @@ public class SnapshotManager implements SnapshotState { } private boolean capture(ReplicatedLogEntry lastLogEntry, long replicatedToAllIndex, String targetFollower) { - TermInformationReader lastAppliedTermInfoReader = - lastAppliedTermInformationReader.init(context.getReplicatedLog(), context.getLastApplied(), - lastLogEntry, hasFollowers()); - - long lastAppliedIndex = lastAppliedTermInfoReader.getIndex(); - long lastAppliedTerm = lastAppliedTermInfoReader.getTerm(); - - TermInformationReader replicatedToAllTermInfoReader = - replicatedToAllTermInformationReader.init(context.getReplicatedLog(), replicatedToAllIndex); - - long newReplicatedToAllIndex = replicatedToAllTermInfoReader.getIndex(); - long newReplicatedToAllTerm = replicatedToAllTermInfoReader.getTerm(); - - // send a CaptureSnapshot to self to make the expensive operation async. - - List unAppliedEntries = context.getReplicatedLog().getFrom(lastAppliedIndex + 1); - - long lastLogEntryIndex = lastAppliedIndex; - long lastLogEntryTerm = lastAppliedTerm; - if(lastLogEntry != null) { - lastLogEntryIndex = lastLogEntry.getIndex(); - lastLogEntryTerm = lastLogEntry.getTerm(); - } else { - LOG.warn("Capturing Snapshot : lastLogEntry is null. Using lastAppliedIndex {} and lastAppliedTerm {} instead.", - lastAppliedIndex, lastAppliedTerm); - } - - captureSnapshot = new CaptureSnapshot(lastLogEntryIndex, - lastLogEntryTerm, lastAppliedIndex, lastAppliedTerm, - newReplicatedToAllIndex, newReplicatedToAllTerm, unAppliedEntries, targetFollower != null); + captureSnapshot = newCaptureSnapshot(lastLogEntry, replicatedToAllIndex, targetFollower != null); if(captureSnapshot.isInstallSnapshotInitiated()) { LOG.info("{}: Initiating snapshot capture {} to install on {}", @@ -239,7 +242,7 @@ public class SnapshotManager implements SnapshotState { lastSequenceNumber = context.getPersistenceProvider().getLastSequenceNumber(); - LOG.debug("lastSequenceNumber prior to capture: {}", lastSequenceNumber); + LOG.debug("{}: lastSequenceNumber prior to capture: {}", persistenceId(), lastSequenceNumber); SnapshotManager.this.currentState = CREATING; @@ -300,7 +303,7 @@ public class SnapshotManager implements SnapshotState { captureSnapshot.getLastIndex(), captureSnapshot.getLastTerm(), captureSnapshot.getLastAppliedIndex(), captureSnapshot.getLastAppliedTerm(), context.getTermInformation().getCurrentTerm(), - context.getTermInformation().getVotedFor()); + context.getTermInformation().getVotedFor(), context.getPeerServerInfo(true)); context.getPersistenceProvider().saveSnapshot(snapshot); @@ -379,34 +382,36 @@ public class SnapshotManager implements SnapshotState { @Override public void commit(long sequenceNumber, RaftActorBehavior currentBehavior) { - LOG.debug("Snapshot success sequence number: {}", sequenceNumber); + LOG.debug("{}: Snapshot success - sequence number: {}", persistenceId(), sequenceNumber); if(applySnapshot != null) { try { Snapshot snapshot = applySnapshot.getSnapshot(); - applySnapshotProcedure.apply(snapshot.getState()); //clears the followers log, sets the snapshot index to ensure adjusted-index works context.setReplicatedLog(ReplicatedLogImpl.newInstance(snapshot, context, currentBehavior)); context.setLastApplied(snapshot.getLastAppliedIndex()); context.setCommitIndex(snapshot.getLastAppliedIndex()); + context.getTermInformation().update(snapshot.getElectionTerm(), snapshot.getElectionVotedFor()); + + if(snapshot.getState().length > 0 ) { + applySnapshotProcedure.apply(snapshot.getState()); + } applySnapshot.getCallback().onSuccess(); } catch (Exception e) { - LOG.error("Error applying snapshot", e); + LOG.error("{}: Error applying snapshot", context.getId(), e); } } else { context.getReplicatedLog().snapshotCommit(); } context.getPersistenceProvider().deleteSnapshots(new SnapshotSelectionCriteria( - sequenceNumber - context.getConfigParams().getSnapshotBatchCount(), 43200000)); + sequenceNumber - context.getConfigParams().getSnapshotBatchCount(), Long.MAX_VALUE)); context.getPersistenceProvider().deleteMessages(lastSequenceNumber); - lastSequenceNumber = -1; - applySnapshot = null; - SnapshotManager.this.currentState = IDLE; + snapshotComplete(); } @Override @@ -424,9 +429,15 @@ public class SnapshotManager implements SnapshotState { applySnapshot.getCallback().onFailure(); } + snapshotComplete(); + } + + private void snapshotComplete() { lastSequenceNumber = -1; applySnapshot = null; SnapshotManager.this.currentState = IDLE; + + context.getActor().tell(SnapshotComplete.INSTANCE, context.getActor()); } @Override