Merge "Added LeaderElectionScenariosTest class"
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / behaviors / Leader.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 akka.actor.Cancellable;
12 import com.google.common.annotations.VisibleForTesting;
13 import com.google.common.base.Preconditions;
14 import org.opendaylight.controller.cluster.raft.RaftActorContext;
15 import org.opendaylight.controller.cluster.raft.base.messages.IsolatedLeaderCheck;
16 import scala.concurrent.duration.FiniteDuration;
17
18 /**
19  * The behavior of a RaftActor when it is in the Leader state
20  * <p/>
21  * Leaders:
22  * <ul>
23  * <li> Upon election: send initial empty AppendEntries RPCs
24  * (heartbeat) to each server; repeat during idle periods to
25  * prevent election timeouts (§5.2)
26  * <li> If command received from client: append entry to local log,
27  * respond after entry applied to state machine (§5.3)
28  * <li> If last log index ≥ nextIndex for a follower: send
29  * AppendEntries RPC with log entries starting at nextIndex
30  * <ul>
31  * <li> If successful: update nextIndex and matchIndex for
32  * follower (§5.3)
33  * <li> If AppendEntries fails because of log inconsistency:
34  * decrement nextIndex and retry (§5.3)
35  * </ul>
36  * <li> If there exists an N such that N > commitIndex, a majority
37  * of matchIndex[i] ≥ N, and log[N].term == currentTerm:
38  * set commitIndex = N (§5.3, §5.4).
39  */
40 public class Leader extends AbstractLeader {
41     private Cancellable installSnapshotSchedule = null;
42     private Cancellable isolatedLeaderCheckSchedule = null;
43
44     public Leader(RaftActorContext context) {
45         super(context);
46
47         scheduleIsolatedLeaderCheck(
48             new FiniteDuration(context.getConfigParams().getHeartBeatInterval().length() * 10,
49                 context.getConfigParams().getHeartBeatInterval().unit()));
50     }
51
52     @Override public RaftActorBehavior handleMessage(ActorRef sender, Object originalMessage) {
53         Preconditions.checkNotNull(sender, "sender should not be null");
54
55         if (originalMessage instanceof IsolatedLeaderCheck) {
56             if (isLeaderIsolated()) {
57                 LOG.info("{}: At least {} followers need to be active, Switching {} from Leader to IsolatedLeader",
58                         context.getId(), minIsolatedLeaderPeerCount, leaderId);
59                 return switchBehavior(new IsolatedLeader(context));
60             }
61         }
62
63         return super.handleMessage(sender, originalMessage);
64     }
65
66     protected void stopIsolatedLeaderCheckSchedule() {
67         if (isolatedLeaderCheckSchedule != null && !isolatedLeaderCheckSchedule.isCancelled()) {
68             isolatedLeaderCheckSchedule.cancel();
69         }
70     }
71
72     protected void scheduleIsolatedLeaderCheck(FiniteDuration isolatedCheckInterval) {
73         isolatedLeaderCheckSchedule = context.getActorSystem().scheduler().schedule(isolatedCheckInterval, isolatedCheckInterval,
74             context.getActor(), new IsolatedLeaderCheck(),
75             context.getActorSystem().dispatcher(), context.getActor());
76     }
77
78     @Override
79     public void close() throws Exception {
80         stopIsolatedLeaderCheckSchedule();
81         super.close();
82     }
83
84     @VisibleForTesting
85     void markFollowerActive(String followerId) {
86         getFollower(followerId).markFollowerActive();
87     }
88
89     @VisibleForTesting
90     void markFollowerInActive(String followerId) {
91         getFollower(followerId).markFollowerInActive();
92     }
93 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.