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

©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.