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%2Fbehaviors%2FAbstractRaftActorBehavior.java;h=d17fba03d4ba58e77168602ffe388431b0a64a1a;hb=01c5a7cc52f8a438053d52ebb91e531493ca765a;hp=6ef4914477bee8fc2f7a50a78f66fdfd8be2f1d0;hpb=766df8b58cc1decd7b40e0adbf41d3657fc21f2c;p=controller.git diff --git a/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/behaviors/AbstractRaftActorBehavior.java b/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/behaviors/AbstractRaftActorBehavior.java index 6ef4914477..d17fba03d4 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/behaviors/AbstractRaftActorBehavior.java +++ b/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/behaviors/AbstractRaftActorBehavior.java @@ -252,6 +252,10 @@ public abstract class AbstractRaftActorBehavior implements RaftActorBehavior { } } + protected boolean canStartElection() { + return context.getRaftPolicy().automaticElectionsEnabled() && context.isVotingMember(); + } + /** * schedule a new election * @@ -260,12 +264,11 @@ public abstract class AbstractRaftActorBehavior implements RaftActorBehavior { protected void scheduleElection(FiniteDuration interval) { stopElection(); - // Schedule an election. When the scheduler triggers an ElectionTimeout - // message is sent to itself - electionCancel = - context.getActorSystem().scheduler().scheduleOnce(interval, - context.getActor(), ELECTION_TIMEOUT, - context.getActorSystem().dispatcher(), context.getActor()); + if(canStartElection()) { + // Schedule an election. When the scheduler triggers an ElectionTimeout message is sent to itself + electionCancel = context.getActorSystem().scheduler().scheduleOnce(interval, context.getActor(), + ELECTION_TIMEOUT,context.getActorSystem().dispatcher(), context.getActor()); + } } /** @@ -427,16 +430,27 @@ public abstract class AbstractRaftActorBehavior implements RaftActorBehavior { @Override public RaftActorBehavior switchBehavior(RaftActorBehavior behavior) { - LOG.info("{} :- Switching from behavior {} to {}", logName(), this.state(), behavior.state()); + return internalSwitchBehavior(behavior); + } + + protected RaftActorBehavior internalSwitchBehavior(RaftState newState) { + if(context.getRaftPolicy().automaticElectionsEnabled()){ + return internalSwitchBehavior(newState.createBehavior(context)); + } + return this; + } + + private RaftActorBehavior internalSwitchBehavior(RaftActorBehavior newBehavior) { + LOG.info("{} :- Switching from behavior {} to {}", logName(), this.state(), newBehavior.state()); try { close(); } catch (Exception e) { LOG.error("{}: Failed to close behavior : {}", logName(), this.state(), e); } - - return behavior; + return newBehavior; } + protected int getMajorityVoteCount(int numPeers) { // Votes are required from a majority of the peers including self. // The numMajority field therefore stores a calculated value @@ -478,5 +492,4 @@ public abstract class AbstractRaftActorBehavior implements RaftActorBehavior { protected String getId(){ return context.getId(); } - }