Fixup comparison formatting
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / behaviors / IsolatedLeader.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.cluster.raft.behaviors;
9
10 import akka.actor.ActorRef;
11 import org.eclipse.jdt.annotation.Nullable;
12 import org.opendaylight.controller.cluster.raft.RaftActorContext;
13 import org.opendaylight.controller.cluster.raft.RaftState;
14 import org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply;
15
16 /**
17  * Leader which is termed as isolated.
18  *
19  * <p>
20  * If the reply from the majority of the followers  is not received then the leader changes its behavior
21  * to IsolatedLeader. An isolated leader may have followers and they would continue to receive replicated messages.
22  *
23  * <p>
24  * A schedule is run, at an interval of (10 * Heartbeat-time-interval),  in the Leader
25  * to check if its isolated or not.
26  *
27  * <p>
28  * In the Isolated Leader , on every AppendEntriesReply, we aggressively check if the leader is isolated.
29  * If no, then the state is switched back to Leader.
30  */
31 public class IsolatedLeader extends AbstractLeader {
32     IsolatedLeader(RaftActorContext context, @Nullable AbstractLeader initializeFromLeader) {
33         super(context, RaftState.IsolatedLeader, initializeFromLeader);
34     }
35
36     public IsolatedLeader(RaftActorContext context) {
37         this(context, null);
38     }
39
40     // we received an Append Entries reply, we should switch the Behavior to Leader
41     @Override
42     protected RaftActorBehavior handleAppendEntriesReply(ActorRef sender,
43         AppendEntriesReply appendEntriesReply) {
44         RaftActorBehavior ret = super.handleAppendEntriesReply(sender, appendEntriesReply);
45
46         // it can happen that this isolated leader interacts with a new leader in the cluster and
47         // changes its state to Follower, hence we only need to switch to Leader if the state is still Isolated
48         if (ret.state() == RaftState.IsolatedLeader && !isLeaderIsolated()) {
49             log.info("IsolatedLeader {} switching from IsolatedLeader to Leader", getLeaderId());
50             return internalSwitchBehavior(new Leader(context, this));
51         }
52         return ret;
53     }
54 }