2 * Copyright (c) 2015 Brocade Communications Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.raft;
10 import akka.actor.ActorRef;
11 import akka.actor.ActorSelection;
12 import akka.actor.Cancellable;
13 import com.google.common.base.Preconditions;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.LinkedList;
17 import java.util.List;
18 import java.util.Queue;
19 import java.util.UUID;
20 import java.util.concurrent.TimeUnit;
21 import javax.annotation.Nullable;
22 import org.opendaylight.controller.cluster.raft.ServerConfigurationPayload.ServerInfo;
23 import org.opendaylight.controller.cluster.raft.base.messages.ApplyState;
24 import org.opendaylight.controller.cluster.raft.base.messages.SnapshotComplete;
25 import org.opendaylight.controller.cluster.raft.behaviors.AbstractLeader;
26 import org.opendaylight.controller.cluster.raft.messages.AddServer;
27 import org.opendaylight.controller.cluster.raft.messages.AddServerReply;
28 import org.opendaylight.controller.cluster.raft.messages.FollowerCatchUpTimeout;
29 import org.opendaylight.controller.cluster.raft.messages.ServerChangeStatus;
30 import org.opendaylight.controller.cluster.raft.messages.UnInitializedFollowerSnapshotReply;
31 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import scala.concurrent.duration.FiniteDuration;
37 * Handles server configuration related messages for a RaftActor.
39 * @author Thomas Pantelis
41 class RaftActorServerConfigurationSupport {
42 private static final Logger LOG = LoggerFactory.getLogger(RaftActorServerConfigurationSupport.class);
44 private final OperationState IDLE = new Idle();
46 private final RaftActorContext raftContext;
48 private final Queue<ServerOperationContext<?>> pendingOperationsQueue = new LinkedList<>();
50 private OperationState currentOperationState = IDLE;
52 RaftActorServerConfigurationSupport(RaftActorContext context) {
53 this.raftContext = context;
56 boolean handleMessage(Object message, RaftActor raftActor, ActorRef sender) {
57 if(message instanceof AddServer) {
58 onAddServer((AddServer)message, raftActor, sender);
60 } else if (message instanceof FollowerCatchUpTimeout) {
61 currentOperationState.onFollowerCatchupTimeout(raftActor, (FollowerCatchUpTimeout)message);
63 } else if (message instanceof UnInitializedFollowerSnapshotReply) {
64 currentOperationState.onUnInitializedFollowerSnapshotReply(raftActor,
65 (UnInitializedFollowerSnapshotReply)message);
67 } else if(message instanceof ApplyState) {
68 return onApplyState((ApplyState) message, raftActor);
69 } else if(message instanceof SnapshotComplete) {
70 currentOperationState.onSnapshotComplete(raftActor);
77 private boolean onApplyState(ApplyState applyState, RaftActor raftActor) {
78 Payload data = applyState.getReplicatedLogEntry().getData();
79 if(data instanceof ServerConfigurationPayload) {
80 currentOperationState.onApplyState(raftActor, applyState);
88 * The algorithm for AddServer is as follows:
90 * <li>Add the new server as a peer.</li>
91 * <li>Add the new follower to the leader.</li>
92 * <li>If new server should be voting member</li>
94 * <li>Initialize FollowerState to VOTING_NOT_INITIALIZED.</li>
95 * <li>Initiate install snapshot to the new follower.</li>
96 * <li>When install snapshot complete, mark the follower as VOTING and re-calculate majority vote count.</li>
98 * <li>Persist and replicate ServerConfigurationPayload with the new server list.</li>
99 * <li>On replication consensus, respond to caller with OK.</li>
101 * If the install snapshot times out after a period of 2 * election time out
103 * <li>Remove the new server as a peer.</li>
104 * <li>Remove the new follower from the leader.</li>
105 * <li>Respond to caller with TIMEOUT.</li>
108 private void onAddServer(AddServer addServer, RaftActor raftActor, ActorRef sender) {
109 LOG.debug("{}: onAddServer: {}", raftContext.getId(), addServer);
111 onNewOperation(raftActor, new AddServerContext(addServer, sender));
114 private void onNewOperation(RaftActor raftActor, ServerOperationContext<?> operationContext) {
115 if (raftActor.isLeader()) {
116 currentOperationState.onNewOperation(raftActor, operationContext);
118 ActorSelection leader = raftActor.getLeader();
119 if (leader != null) {
120 LOG.debug("{}: Not leader - forwarding to leader {}", raftContext.getId(), leader);
121 leader.forward(operationContext.getOperation(), raftActor.getContext());
123 LOG.debug("{}: No leader - returning NO_LEADER reply", raftContext.getId());
124 operationContext.getClientRequestor().tell(operationContext.newReply(
125 ServerChangeStatus.NO_LEADER, null), raftActor.self());
131 * Interface for a server operation FSM state.
133 private interface OperationState {
134 void onNewOperation(RaftActor raftActor, ServerOperationContext<?> operationContext);
136 void onFollowerCatchupTimeout(RaftActor raftActor, FollowerCatchUpTimeout followerTimeout);
138 void onUnInitializedFollowerSnapshotReply(RaftActor raftActor, UnInitializedFollowerSnapshotReply reply);
140 void onApplyState(RaftActor raftActor, ApplyState applyState);
142 void onSnapshotComplete(RaftActor raftActor);
146 * Interface for the initial state for a server operation.
148 private interface InitialOperationState {
149 void initiate(RaftActor raftActor);
153 * Abstract base class for server operation FSM state. Handles common behavior for all states.
155 private abstract class AbstractOperationState implements OperationState {
157 public void onNewOperation(RaftActor raftActor, ServerOperationContext<?> operationContext) {
158 // We're currently processing another operation so queue it to be processed later.
160 LOG.debug("{}: Server operation already in progress - queueing {}", raftContext.getId(),
161 operationContext.getOperation());
163 pendingOperationsQueue.add(operationContext);
167 public void onFollowerCatchupTimeout(RaftActor raftActor, FollowerCatchUpTimeout followerTimeout) {
168 LOG.debug("onFollowerCatchupTimeout should not be called in state {}", this);
172 public void onUnInitializedFollowerSnapshotReply(RaftActor raftActor, UnInitializedFollowerSnapshotReply reply) {
173 LOG.debug("onUnInitializedFollowerSnapshotReply was called in state {}", this);
177 public void onApplyState(RaftActor raftActor, ApplyState applyState) {
178 LOG.debug("onApplyState was called in state {}", this);
182 public void onSnapshotComplete(RaftActor raftActor) {
185 protected void persistNewServerConfiguration(RaftActor raftActor, ServerOperationContext<?> operationContext){
186 Collection<PeerInfo> peers = raftContext.getPeers();
187 List<ServerInfo> newConfig = new ArrayList<>(peers.size() + 1);
188 for(PeerInfo peer: peers) {
189 newConfig.add(new ServerInfo(peer.getId(), peer.isVoting()));
192 newConfig.add(new ServerInfo(raftContext.getId(), true));
194 LOG.debug("{}: Persisting new server configuration : {}", raftContext.getId(), newConfig);
196 ServerConfigurationPayload payload = new ServerConfigurationPayload(newConfig);
198 raftActor.persistData(operationContext.getClientRequestor(), operationContext.getContextId(), payload);
200 currentOperationState = new Persisting(operationContext);
202 sendReply(raftActor, operationContext, ServerChangeStatus.OK);
205 protected void operationComplete(RaftActor raftActor, ServerOperationContext<?> operationContext,
206 @Nullable ServerChangeStatus replyStatus) {
207 if(replyStatus != null) {
208 sendReply(raftActor, operationContext, replyStatus);
211 currentOperationState = IDLE;
213 ServerOperationContext<?> nextOperation = pendingOperationsQueue.poll();
214 if(nextOperation != null) {
215 RaftActorServerConfigurationSupport.this.onNewOperation(raftActor, nextOperation);
219 private void sendReply(RaftActor raftActor, ServerOperationContext<?> operationContext,
220 ServerChangeStatus status) {
221 LOG.debug("{}: Returning {} for operation {}", raftContext.getId(), status, operationContext.getOperation());
223 operationContext.getClientRequestor().tell(operationContext.newReply(status, raftActor.getLeaderId()),
228 public String toString() {
229 return getClass().getSimpleName();
234 * The state when no server operation is in progress. It immediately initiates new server operations.
236 private class Idle extends AbstractOperationState {
238 public void onNewOperation(RaftActor raftActor, ServerOperationContext<?> operationContext) {
239 operationContext.newInitialOperationState(RaftActorServerConfigurationSupport.this).initiate(raftActor);
243 public void onApplyState(RaftActor raftActor, ApplyState applyState) {
244 // Noop - we override b/c ApplyState is called normally for followers in the idle state.
249 * The state when a new server configuration is being persisted and replicated.
251 private class Persisting extends AbstractOperationState {
252 private final ServerOperationContext<?> operationContext;
254 Persisting(ServerOperationContext<?> operationContext) {
255 this.operationContext = operationContext;
259 public void onApplyState(RaftActor raftActor, ApplyState applyState) {
260 // Sanity check - we could get an ApplyState from a previous operation that timed out so make
261 // sure it's meant for us.
262 if(operationContext.getContextId().equals(applyState.getIdentifier())) {
263 LOG.info("{}: {} has been successfully replicated to a majority of followers",
264 applyState.getReplicatedLogEntry().getData());
266 operationComplete(raftActor, operationContext, null);
272 * Abstract base class for an AddServer operation state.
274 private abstract class AddServerState extends AbstractOperationState {
275 private final AddServerContext addServerContext;
277 AddServerState(AddServerContext addServerContext) {
278 this.addServerContext = addServerContext;
281 AddServerContext getAddServerContext() {
282 return addServerContext;
285 Cancellable newInstallSnapshotTimer(RaftActor raftActor) {
286 return raftContext.getActorSystem().scheduler().scheduleOnce(
287 new FiniteDuration(((raftContext.getConfigParams().getElectionTimeOutInterval().toMillis()) * 2),
288 TimeUnit.MILLISECONDS), raftContext.getActor(),
289 new FollowerCatchUpTimeout(addServerContext.getOperation().getNewServerId()),
290 raftContext.getActorSystem().dispatcher(), raftContext.getActor());
293 void handleOnFollowerCatchupTimeout(RaftActor raftActor, FollowerCatchUpTimeout followerTimeout) {
294 String serverId = followerTimeout.getNewServerId();
296 LOG.debug("{}: onFollowerCatchupTimeout for new server {}", raftContext.getId(), serverId);
299 raftContext.removePeer(serverId);
301 boolean isLeader = raftActor.isLeader();
303 AbstractLeader leader = (AbstractLeader) raftActor.getCurrentBehavior();
304 leader.removeFollower(serverId);
307 operationComplete(raftActor, getAddServerContext(),
308 isLeader ? ServerChangeStatus.TIMEOUT : ServerChangeStatus.NO_LEADER);
313 * The initial state for the AddServer operation. It adds the new follower as a peer and initiates
314 * snapshot capture, if necessary.
316 private class InitialAddServerState extends AddServerState implements InitialOperationState {
317 InitialAddServerState(AddServerContext addServerContext) {
318 super(addServerContext);
322 public void initiate(RaftActor raftActor) {
323 AbstractLeader leader = (AbstractLeader) raftActor.getCurrentBehavior();
324 AddServer addServer = getAddServerContext().getOperation();
326 LOG.debug("{}: Initiating {}", raftContext.getId(), addServer);
328 if(raftContext.getPeerInfo(addServer.getNewServerId()) != null) {
329 operationComplete(raftActor, getAddServerContext(), ServerChangeStatus.ALREADY_EXISTS);
333 VotingState votingState = addServer.isVotingMember() ? VotingState.VOTING_NOT_INITIALIZED :
334 VotingState.NON_VOTING;
335 raftContext.addToPeers(addServer.getNewServerId(), addServer.getNewServerAddress(), votingState);
337 leader.addFollower(addServer.getNewServerId());
339 if(votingState == VotingState.VOTING_NOT_INITIALIZED){
340 // schedule the install snapshot timeout timer
341 Cancellable installSnapshotTimer = newInstallSnapshotTimer(raftActor);
342 if(leader.initiateCaptureSnapshot(addServer.getNewServerId())) {
343 LOG.debug("{}: Initiating capture snapshot for new server {}", raftContext.getId(),
344 addServer.getNewServerId());
346 currentOperationState = new InstallingSnapshot(getAddServerContext(), installSnapshotTimer);
348 LOG.debug("{}: Snapshot already in progress - waiting for completion", raftContext.getId());
350 currentOperationState = new WaitingForPriorSnapshotComplete(getAddServerContext(),
351 installSnapshotTimer);
354 LOG.debug("{}: New follower is non-voting - directly persisting new server configuration",
355 raftContext.getId());
357 persistNewServerConfiguration(raftActor, getAddServerContext());
363 * The AddServer operation state for when the catch-up snapshot is being installed. It handles successful
366 private class InstallingSnapshot extends AddServerState {
367 private final Cancellable installSnapshotTimer;
369 InstallingSnapshot(AddServerContext addServerContext, Cancellable installSnapshotTimer) {
370 super(addServerContext);
371 this.installSnapshotTimer = Preconditions.checkNotNull(installSnapshotTimer);
375 public void onFollowerCatchupTimeout(RaftActor raftActor, FollowerCatchUpTimeout followerTimeout) {
376 handleOnFollowerCatchupTimeout(raftActor, followerTimeout);
378 LOG.warn("{}: Timeout occured for new server {} while installing snapshot", raftContext.getId(),
379 followerTimeout.getNewServerId());
383 public void onUnInitializedFollowerSnapshotReply(RaftActor raftActor, UnInitializedFollowerSnapshotReply reply) {
384 LOG.debug("{}: onUnInitializedFollowerSnapshotReply: {}", raftContext.getId(), reply);
386 String followerId = reply.getFollowerId();
388 // Sanity check to guard against receiving an UnInitializedFollowerSnapshotReply from a prior
389 // add server operation that timed out.
390 if(getAddServerContext().getOperation().getNewServerId().equals(followerId) && raftActor.isLeader()) {
391 AbstractLeader leader = (AbstractLeader) raftActor.getCurrentBehavior();
392 raftContext.getPeerInfo(followerId).setVotingState(VotingState.VOTING);
393 leader.updateMinReplicaCount();
395 persistNewServerConfiguration(raftActor, getAddServerContext());
397 installSnapshotTimer.cancel();
399 LOG.debug("{}: Dropping UnInitializedFollowerSnapshotReply for server {}: {}",
400 raftContext.getId(), followerId,
401 !raftActor.isLeader() ? "not leader" : "server Id doesn't match");
407 * The AddServer operation state for when there is a snapshot already in progress. When the current
408 * snapshot completes, it initiates an install snapshot.
410 private class WaitingForPriorSnapshotComplete extends AddServerState {
411 private final Cancellable snapshotTimer;
413 WaitingForPriorSnapshotComplete(AddServerContext addServerContext, Cancellable snapshotTimer) {
414 super(addServerContext);
415 this.snapshotTimer = Preconditions.checkNotNull(snapshotTimer);
419 public void onSnapshotComplete(RaftActor raftActor) {
420 LOG.debug("{}: onSnapshotComplete", raftContext.getId());
422 if(!raftActor.isLeader()) {
423 LOG.debug("{}: No longer the leader", raftContext.getId());
427 AbstractLeader leader = (AbstractLeader) raftActor.getCurrentBehavior();
428 if(leader.initiateCaptureSnapshot(getAddServerContext().getOperation().getNewServerId())) {
429 LOG.debug("{}: Initiating capture snapshot for new server {}", raftContext.getId(),
430 getAddServerContext().getOperation().getNewServerId());
432 currentOperationState = new InstallingSnapshot(getAddServerContext(),
433 newInstallSnapshotTimer(raftActor));
435 snapshotTimer.cancel();
440 public void onFollowerCatchupTimeout(RaftActor raftActor, FollowerCatchUpTimeout followerTimeout) {
441 handleOnFollowerCatchupTimeout(raftActor, followerTimeout);
443 LOG.warn("{}: Timeout occured for new server {} while waiting for prior snapshot to complete",
444 raftContext.getId(), followerTimeout.getNewServerId());
449 * Stores context information for a server operation.
451 * @param <T> the operation type
453 private static abstract class ServerOperationContext<T> {
454 private final T operation;
455 private final ActorRef clientRequestor;
456 private final String contextId;
458 ServerOperationContext(T operation, ActorRef clientRequestor){
459 this.operation = operation;
460 this.clientRequestor = clientRequestor;
461 contextId = UUID.randomUUID().toString();
464 String getContextId() {
472 ActorRef getClientRequestor() {
473 return clientRequestor;
476 abstract Object newReply(ServerChangeStatus status, String leaderId);
478 abstract InitialOperationState newInitialOperationState(RaftActorServerConfigurationSupport support);
482 * Stores context information for an AddServer operation.
484 private static class AddServerContext extends ServerOperationContext<AddServer> {
485 AddServerContext(AddServer addServer, ActorRef clientRequestor) {
486 super(addServer, clientRequestor);
490 Object newReply(ServerChangeStatus status, String leaderId) {
491 return new AddServerReply(status, leaderId);
495 InitialOperationState newInitialOperationState(RaftActorServerConfigurationSupport support) {
496 return support.new InitialAddServerState(this);