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.Collections;
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 org.opendaylight.controller.cluster.raft.FollowerLogInformation.FollowerState;
22 import org.opendaylight.controller.cluster.raft.base.messages.ApplyState;
23 import org.opendaylight.controller.cluster.raft.behaviors.AbstractLeader;
24 import org.opendaylight.controller.cluster.raft.messages.AddServer;
25 import org.opendaylight.controller.cluster.raft.messages.AddServerReply;
26 import org.opendaylight.controller.cluster.raft.messages.FollowerCatchUpTimeout;
27 import org.opendaylight.controller.cluster.raft.messages.ServerChangeStatus;
28 import org.opendaylight.controller.cluster.raft.messages.UnInitializedFollowerSnapshotReply;
29 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import scala.concurrent.duration.FiniteDuration;
35 * Handles server configuration related messages for a RaftActor.
37 * @author Thomas Pantelis
39 class RaftActorServerConfigurationSupport {
40 private static final Logger LOG = LoggerFactory.getLogger(RaftActorServerConfigurationSupport.class);
42 private final OperationState IDLE = new Idle();
44 private final RaftActorContext raftContext;
46 private final Queue<ServerOperationContext<?>> pendingOperationsQueue = new LinkedList<>();
48 private OperationState currentOperationState = IDLE;
50 RaftActorServerConfigurationSupport(RaftActorContext context) {
51 this.raftContext = context;
54 boolean handleMessage(Object message, RaftActor raftActor, ActorRef sender) {
55 if(message instanceof AddServer) {
56 onAddServer((AddServer)message, raftActor, sender);
58 } else if (message instanceof FollowerCatchUpTimeout) {
59 currentOperationState.onFollowerCatchupTimeout(raftActor, (FollowerCatchUpTimeout)message);
61 } else if (message instanceof UnInitializedFollowerSnapshotReply) {
62 currentOperationState.onUnInitializedFollowerSnapshotReply(raftActor,
63 (UnInitializedFollowerSnapshotReply)message);
65 } else if(message instanceof ApplyState) {
66 return onApplyState((ApplyState) message, raftActor);
72 private boolean onApplyState(ApplyState applyState, RaftActor raftActor) {
73 Payload data = applyState.getReplicatedLogEntry().getData();
74 if(data instanceof ServerConfigurationPayload) {
75 currentOperationState.onApplyState(raftActor, applyState);
83 * The algorithm for AddServer is as follows:
85 * <li>Add the new server as a peer.</li>
86 * <li>Add the new follower to the leader.</li>
87 * <li>If new server should be voting member</li>
89 * <li>Initialize FollowerState to VOTING_NOT_INITIALIZED.</li>
90 * <li>Initiate install snapshot to the new follower.</li>
91 * <li>When install snapshot complete, mark the follower as VOTING and re-calculate majority vote count.</li>
93 * <li>Persist and replicate ServerConfigurationPayload with the new server list.</li>
94 * <li>On replication consensus, respond to caller with OK.</li>
96 * If the install snapshot times out after a period of 2 * election time out
98 * <li>Remove the new server as a peer.</li>
99 * <li>Remove the new follower from the leader.</li>
100 * <li>Respond to caller with TIMEOUT.</li>
103 private void onAddServer(AddServer addServer, RaftActor raftActor, ActorRef sender) {
104 LOG.debug("{}: onAddServer: {}", raftContext.getId(), addServer);
106 onNewOperation(raftActor, new AddServerContext(addServer, sender));
109 private void onNewOperation(RaftActor raftActor, ServerOperationContext<?> operationContext) {
110 if (raftActor.isLeader()) {
111 currentOperationState.onNewOperation(raftActor, operationContext);
113 ActorSelection leader = raftActor.getLeader();
114 if (leader != null) {
115 LOG.debug("{}: Not leader - forwarding to leader {}", raftContext.getId(), leader);
116 leader.forward(operationContext.getOperation(), raftActor.getContext());
118 LOG.debug("{}: No leader - returning NO_LEADER reply", raftContext.getId());
119 operationContext.getClientRequestor().tell(operationContext.newReply(
120 ServerChangeStatus.NO_LEADER, null), raftActor.self());
126 * Interface for a server operation FSM state.
128 private interface OperationState {
129 void onNewOperation(RaftActor raftActor, ServerOperationContext<?> operationContext);
131 void onFollowerCatchupTimeout(RaftActor raftActor, FollowerCatchUpTimeout followerTimeout);
133 void onUnInitializedFollowerSnapshotReply(RaftActor raftActor, UnInitializedFollowerSnapshotReply reply);
135 void onApplyState(RaftActor raftActor, ApplyState applyState);
139 * Interface for the initial state for a server operation.
141 private interface InitialOperationState {
142 void initiate(RaftActor raftActor);
146 * Abstract base class for server operation FSM state. Handles common behavior for all states.
148 private abstract class AbstractOperationState implements OperationState {
150 public void onNewOperation(RaftActor raftActor, ServerOperationContext<?> operationContext) {
151 // We're currently processing another operation so queue it to be processed later.
153 LOG.debug("{}: Server operation already in progress - queueing {}", raftContext.getId(),
154 operationContext.getOperation());
156 pendingOperationsQueue.add(operationContext);
160 public void onFollowerCatchupTimeout(RaftActor raftActor, FollowerCatchUpTimeout followerTimeout) {
161 LOG.debug("onFollowerCatchupTimeout should not be called in state {}", this);
165 public void onUnInitializedFollowerSnapshotReply(RaftActor raftActor, UnInitializedFollowerSnapshotReply reply) {
166 LOG.debug("onUnInitializedFollowerSnapshotReply was called in state {}", this);
170 public void onApplyState(RaftActor raftActor, ApplyState applyState) {
171 LOG.debug("onApplyState was called in state {}", this);
174 protected void persistNewServerConfiguration(RaftActor raftActor, ServerOperationContext<?> operationContext){
175 List <String> newConfig = new ArrayList<String>(raftContext.getPeerIds());
176 newConfig.add(raftContext.getId());
178 LOG.debug("{}: New server configuration : {}", raftContext.getId(), newConfig);
180 ServerConfigurationPayload payload = new ServerConfigurationPayload(newConfig, Collections.<String>emptyList());
182 raftActor.persistData(operationContext.getClientRequestor(), operationContext.getContextId(), payload);
184 currentOperationState = new Persisting(operationContext);
187 protected void operationComplete(RaftActor raftActor, ServerOperationContext<?> operationContext,
188 ServerChangeStatus status) {
190 LOG.debug("{}: Returning {} for operation {}", raftContext.getId(), status, operationContext.getOperation());
192 operationContext.getClientRequestor().tell(operationContext.newReply(status, raftActor.getLeaderId()),
195 currentOperationState = IDLE;
197 ServerOperationContext<?> nextOperation = pendingOperationsQueue.poll();
198 if(nextOperation != null) {
199 RaftActorServerConfigurationSupport.this.onNewOperation(raftActor, nextOperation);
204 public String toString() {
205 return getClass().getSimpleName();
210 * The state when no server operation is in progress. It immediately initiates new server operations.
212 private class Idle extends AbstractOperationState {
214 public void onNewOperation(RaftActor raftActor, ServerOperationContext<?> operationContext) {
215 operationContext.newInitialOperationState(RaftActorServerConfigurationSupport.this).initiate(raftActor);
219 public void onApplyState(RaftActor raftActor, ApplyState applyState) {
220 // Noop - we override b/c ApplyState is called normally for followers in the idle state.
225 * The state when a new server configuration is being persisted and replicated.
227 private class Persisting extends AbstractOperationState {
228 private final ServerOperationContext<?> operationContext;
230 Persisting(ServerOperationContext<?> operationContext) {
231 this.operationContext = operationContext;
235 public void onApplyState(RaftActor raftActor, ApplyState applyState) {
236 // Sanity check - we could get an ApplyState from a previous operation that timed out so make
237 // sure it's meant for us.
238 if(operationContext.getContextId().equals(applyState.getIdentifier())) {
239 LOG.info("{}: {} has been successfully replicated to a majority of followers",
240 applyState.getReplicatedLogEntry().getData());
242 operationComplete(raftActor, operationContext, ServerChangeStatus.OK);
248 * Abstract base class for an AddServer operation state.
250 private abstract class AddServerState extends AbstractOperationState {
251 private final AddServerContext addServerContext;
253 AddServerState(AddServerContext addServerContext) {
254 this.addServerContext = addServerContext;
257 AddServerContext getAddServerContext() {
258 return addServerContext;
263 * The initial state for the AddServer operation. It adds the new follower as a peer and initiates
264 * snapshot capture, if necessary.
266 private class InitialAddServerState extends AddServerState implements InitialOperationState {
267 InitialAddServerState(AddServerContext addServerContext) {
268 super(addServerContext);
272 public void initiate(RaftActor raftActor) {
273 AbstractLeader leader = (AbstractLeader) raftActor.getCurrentBehavior();
275 AddServer addServer = getAddServerContext().getOperation();
277 LOG.debug("{}: Initiating {}", raftContext.getId(), addServer);
279 raftContext.addToPeers(addServer.getNewServerId(), addServer.getNewServerAddress());
281 // if voting member - initialize to VOTING_NOT_INITIALIZED
282 FollowerState initialState = addServer.isVotingMember() ? FollowerState.VOTING_NOT_INITIALIZED :
283 FollowerState.NON_VOTING;
284 leader.addFollower(addServer.getNewServerId(), initialState);
286 if(initialState == FollowerState.VOTING_NOT_INITIALIZED){
287 LOG.debug("{}: Leader sending initiate capture snapshot to new follower {}", raftContext.getId(),
288 addServer.getNewServerId());
290 leader.initiateCaptureSnapshot(addServer.getNewServerId());
292 // schedule the install snapshot timeout timer
293 Cancellable installSnapshotTimer = raftContext.getActorSystem().scheduler().scheduleOnce(
294 new FiniteDuration(((raftContext.getConfigParams().getElectionTimeOutInterval().toMillis()) * 2),
295 TimeUnit.MILLISECONDS), raftContext.getActor(),
296 new FollowerCatchUpTimeout(addServer.getNewServerId()),
297 raftContext.getActorSystem().dispatcher(), raftContext.getActor());
299 currentOperationState = new InstallingSnapshot(getAddServerContext(), installSnapshotTimer);
301 LOG.debug("{}: New follower is non-voting - directly persisting new server configuration",
302 raftContext.getId());
304 persistNewServerConfiguration(raftActor, getAddServerContext());
310 * The AddServer operation state for when the catch-up snapshot is being installed. It handles successful
313 private class InstallingSnapshot extends AddServerState {
314 private final Cancellable installSnapshotTimer;
316 InstallingSnapshot(AddServerContext addServerContext, Cancellable installSnapshotTimer) {
317 super(addServerContext);
318 this.installSnapshotTimer = Preconditions.checkNotNull(installSnapshotTimer);
322 public void onFollowerCatchupTimeout(RaftActor raftActor, FollowerCatchUpTimeout followerTimeout) {
323 String serverId = followerTimeout.getNewServerId();
325 LOG.debug("{}: onFollowerCatchupTimeout: {}", raftContext.getId(), serverId);
327 AbstractLeader leader = (AbstractLeader) raftActor.getCurrentBehavior();
330 raftContext.removePeer(serverId);
331 leader.removeFollower(serverId);
333 LOG.warn("{}: Timeout occured for new server {} while installing snapshot", raftContext.getId(), serverId);
335 operationComplete(raftActor, getAddServerContext(), ServerChangeStatus.TIMEOUT);
339 public void onUnInitializedFollowerSnapshotReply(RaftActor raftActor, UnInitializedFollowerSnapshotReply reply) {
340 LOG.debug("{}: onUnInitializedFollowerSnapshotReply: {}", raftContext.getId(), reply);
342 String followerId = reply.getFollowerId();
344 // Sanity check to guard against receiving an UnInitializedFollowerSnapshotReply from a prior
345 // add server operation that timed out.
346 if(getAddServerContext().getOperation().getNewServerId().equals(followerId)) {
347 AbstractLeader leader = (AbstractLeader) raftActor.getCurrentBehavior();
348 FollowerLogInformation followerLogInformation = leader.getFollower(followerId);
350 installSnapshotTimer.cancel();
352 followerLogInformation.setFollowerState(FollowerState.VOTING);
353 leader.updateMinReplicaCountAndMinIsolatedLeaderPeerCount();
355 persistNewServerConfiguration(raftActor, getAddServerContext());
361 * Stores context information for a server operation.
363 * @param <T> the operation type
365 private static abstract class ServerOperationContext<T> {
366 private final T operation;
367 private final ActorRef clientRequestor;
368 private final String contextId;
370 ServerOperationContext(T operation, ActorRef clientRequestor){
371 this.operation = operation;
372 this.clientRequestor = clientRequestor;
373 contextId = UUID.randomUUID().toString();
376 String getContextId() {
384 ActorRef getClientRequestor() {
385 return clientRequestor;
388 abstract Object newReply(ServerChangeStatus status, String leaderId);
390 abstract InitialOperationState newInitialOperationState(RaftActorServerConfigurationSupport support);
394 * Stores context information for an AddServer operation.
396 private static class AddServerContext extends ServerOperationContext<AddServer> {
397 AddServerContext(AddServer addServer, ActorRef clientRequestor) {
398 super(addServer, clientRequestor);
402 Object newReply(ServerChangeStatus status, String leaderId) {
403 return new AddServerReply(status, leaderId);
407 InitialOperationState newInitialOperationState(RaftActorServerConfigurationSupport support) {
408 return support.new InitialAddServerState(this);