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%2FRaftActor.java;h=ae8b6fe8e3aadc23a33cf57531a43ff9cefed8d4;hb=refs%2Fchanges%2F25%2F9825%2F9;hp=70b85b4627dc707b0223f8a7bdae37dfab1441f1;hpb=5a9287bb6ddaaa8805939b3b3301f648c03785f4;p=controller.git diff --git a/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/RaftActor.java b/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/RaftActor.java index 70b85b4627..ae8b6fe8e3 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/RaftActor.java +++ b/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/RaftActor.java @@ -148,6 +148,7 @@ public abstract class RaftActor extends UntypedPersistentActor { replicatedLog.lastIndex(), replicatedLog.snapshotIndex, replicatedLog.snapshotTerm, replicatedLog.size()); currentBehavior = switchBehavior(RaftState.Follower); + onStateChanged(); } } @@ -206,7 +207,11 @@ public abstract class RaftActor extends UntypedPersistentActor { RaftState state = currentBehavior.handleMessage(getSender(), message); + RaftActorBehavior oldBehavior = currentBehavior; currentBehavior = switchBehavior(state); + if(oldBehavior != currentBehavior){ + onStateChanged(); + } } } @@ -271,13 +276,41 @@ public abstract class RaftActor extends UntypedPersistentActor { String peerAddress = context.getPeerAddress(leaderId); LOG.debug("getLeader leaderId = " + leaderId + " peerAddress = " + peerAddress); + + if(peerAddress == null){ + return null; + } return context.actorSelection(peerAddress); } + /** + * + * @return the current leader's id + */ + protected String getLeaderId(){ + return currentBehavior.getLeaderId(); + } + protected RaftState getRaftState() { return currentBehavior.state(); } + protected ReplicatedLogEntry getLastLogEntry() { + return replicatedLog.last(); + } + + protected Long getCurrentTerm(){ + return context.getTermInformation().getCurrentTerm(); + } + + protected Long getCommitIndex(){ + return context.getCommitIndex(); + } + + protected Long getLastApplied(){ + return context.getLastApplied(); + } + /** * setPeerAddress sets the address of a known peer at a later time. *

@@ -341,6 +374,13 @@ public abstract class RaftActor extends UntypedPersistentActor { */ protected abstract void applySnapshot(Object snapshot); + /** + * This method will be called by the RaftActor when the state of the + * RaftActor changes. The derived actor can then use methods like + * isLeader or getLeader to do something useful + */ + protected abstract void onStateChanged(); + private RaftActorBehavior switchBehavior(RaftState state) { if (currentBehavior != null) { if (currentBehavior.state() == state) { @@ -367,6 +407,9 @@ public abstract class RaftActor extends UntypedPersistentActor { } else { behavior = new Leader(context); } + + + return behavior; } @@ -575,7 +618,7 @@ public abstract class RaftActor extends UntypedPersistentActor { } @Override public void update(long currentTerm, String votedFor) { - LOG.info("Set currentTerm={}, votedFor={}", currentTerm, votedFor); + LOG.debug("Set currentTerm={}, votedFor={}", currentTerm, votedFor); this.currentTerm = currentTerm; this.votedFor = votedFor;