Fix warnings and clean up javadocs in sal-akka-raft
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / behaviors / PreLeader.java
1 /*
2  * Copyright (c) 2016 Brocade Communications 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.base.messages.ApplyState;
14 import org.opendaylight.controller.cluster.raft.persisted.NoopPayload;
15
16 /**
17  * The behavior of a RaftActor when it is in the PreLeader state. This state performs all the duties of
18  * Leader with the added behavior of attempting to commit all uncommitted entries from the previous leader's
19  * term. Raft does not allow a leader to commit entries from a previous term by simply counting replicas -
20  * only entries from the leader's current term can be committed (§5.4.2). Rather then waiting for a client
21  * interaction to commit a new entry, the PreLeader state immediately appends a no-op entry (NoopPayload) to
22  * the log with the leader's current term. Once the no-op entry is committed, all prior entries are committed
23  * indirectly. Once all entries are committed, ie commitIndex matches the last log index, it switches to the
24  * normal Leader state.
25  * <p/>
26  * The use of a no-op entry in this manner is outlined in the last paragraph in §8 of the
27  * <a href="https://raft.github.io/raft.pdf">extended raft version</a>.
28  *
29  * @author Thomas Pantelis
30  */
31 public class PreLeader extends AbstractLeader {
32
33     public PreLeader(RaftActorContext context) {
34         super(context, RaftState.PreLeader);
35
36         context.getActor().tell(NoopPayload.INSTANCE, context.getActor());
37     }
38
39     @Override
40     public RaftActorBehavior handleMessage(ActorRef sender, Object message) {
41         if (message instanceof ApplyState) {
42             if (context.getLastApplied() >= context.getReplicatedLog().lastIndex()) {
43                 // We've applied all entries - we can switch to Leader.
44                 return internalSwitchBehavior(new Leader(context, this));
45             } else {
46                 return this;
47             }
48         } else {
49             return super.handleMessage(sender, message);
50         }
51     }
52 }