BUG-2187: Add Server - Leader Implementation
[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 com.google.common.annotations.VisibleForTesting;
12 import com.google.common.base.Preconditions;
13 import com.google.common.base.Stopwatch;
14 import java.util.concurrent.TimeUnit;
15 import org.opendaylight.controller.cluster.raft.RaftActorContext;
16 import org.opendaylight.controller.cluster.raft.RaftState;
17 import org.opendaylight.controller.cluster.raft.base.messages.IsolatedLeaderCheck;
18
19 /**
20  * The behavior of a RaftActor when it is in the Leader state
21  * <p/>
22  * Leaders:
23  * <ul>
24  * <li> Upon election: send initial empty AppendEntries RPCs
25  * (heartbeat) to each server; repeat during idle periods to
26  * prevent election timeouts (§5.2)
27  * <li> If command received from client: append entry to local log,
28  * respond after entry applied to state machine (§5.3)
29  * <li> If last log index ≥ nextIndex for a follower: send
30  * AppendEntries RPC with log entries starting at nextIndex
31  * <ul>
32  * <li> If successful: update nextIndex and matchIndex for
33  * follower (§5.3)
34  * <li> If AppendEntries fails because of log inconsistency:
35  * decrement nextIndex and retry (§5.3)
36  * </ul>
37  * <li> If there exists an N such that N > commitIndex, a majority
38  * of matchIndex[i] ≥ N, and log[N].term == currentTerm:
39  * set commitIndex = N (§5.3, §5.4).
40  */
41 public class Leader extends AbstractLeader {
42     private static final IsolatedLeaderCheck ISOLATED_LEADER_CHECK = new IsolatedLeaderCheck();
43     private final Stopwatch isolatedLeaderCheck;
44
45     public Leader(RaftActorContext context) {
46         super(context);
47         isolatedLeaderCheck = Stopwatch.createStarted();
48     }
49
50     @Override public RaftActorBehavior handleMessage(ActorRef sender, Object originalMessage) {
51         Preconditions.checkNotNull(sender, "sender should not be null");
52
53         if (originalMessage instanceof IsolatedLeaderCheck) {
54             if (isLeaderIsolated()) {
55                 LOG.warn("{}: At least {} followers need to be active, Switching {} from Leader to IsolatedLeader",
56                         context.getId(), getMinIsolatedLeaderPeerCount(), leaderId);
57
58                 return internalSwitchBehavior(RaftState.IsolatedLeader);
59             }
60         }
61
62         return super.handleMessage(sender, originalMessage);
63     }
64
65     @Override
66     protected void beforeSendHeartbeat(){
67         if(isolatedLeaderCheck.elapsed(TimeUnit.MILLISECONDS) > context.getConfigParams().getIsolatedCheckIntervalInMillis()){
68             context.getActor().tell(ISOLATED_LEADER_CHECK, context.getActor());
69             isolatedLeaderCheck.reset().start();
70         }
71
72     }
73
74     @Override
75     public void close() throws Exception {
76         super.close();
77     }
78
79     @VisibleForTesting
80     void markFollowerActive(String followerId) {
81         getFollower(followerId).markFollowerActive();
82     }
83
84     @VisibleForTesting
85     void markFollowerInActive(String followerId) {
86         getFollower(followerId).markFollowerInActive();
87     }
88 }

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