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