658c129e2d6cf0d0924b897448e80be639169141
[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 javax.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  * <p/>
19  * If the reply from the majority of the followers  is not received then the leader changes its behavior
20  * to IsolatedLeader. An isolated leader may have followers and they would continue to receive replicated messages.
21  * <p/>
22  * A schedule is run, at an interval of (10 * Heartbeat-time-interval),  in the Leader
23  * to check if its isolated or not.
24  * <p/>
25  * In the Isolated Leader , on every AppendEntriesReply, we aggressively check if the leader is isolated.
26  * If no, then the state is switched back to Leader.
27  *
28  */
29 public class IsolatedLeader extends AbstractLeader {
30     IsolatedLeader(RaftActorContext context, @Nullable AbstractLeader initializeFromLeader) {
31         super(context, RaftState.IsolatedLeader, initializeFromLeader);
32     }
33
34     public IsolatedLeader(RaftActorContext context) {
35         this(context, null);
36     }
37
38     // we received an Append Entries reply, we should switch the Behavior to Leader
39     @Override
40     protected RaftActorBehavior handleAppendEntriesReply(ActorRef sender,
41         AppendEntriesReply appendEntriesReply) {
42         RaftActorBehavior ret = super.handleAppendEntriesReply(sender, appendEntriesReply);
43
44         // it can happen that this isolated leader interacts with a new leader in the cluster and
45         // changes its state to Follower, hence we only need to switch to Leader if the state is still Isolated
46         if (ret.state() == RaftState.IsolatedLeader && !isLeaderIsolated()) {
47             log.info("IsolatedLeader {} switching from IsolatedLeader to Leader", getLeaderId());
48             return internalSwitchBehavior(new Leader(context, this));
49         }
50         return ret;
51     }
52 }