X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;ds=sidebyside;f=opendaylight%2Fmd-sal%2Fsal-akka-raft%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fraft%2FRaftActorServerConfigurationSupport.java;h=29641cb00e441d5dedc5e7cccccd69bb0b0e6d3a;hb=d0f46920468c8e4b67c68bd9058572b2d10d75f1;hp=b3506ed8d04299305e889eb99e45e6fc0fa44c8f;hpb=364229dd715facec8ef8c73d6c60546c5f38b103;p=controller.git
diff --git a/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/RaftActorServerConfigurationSupport.java b/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/RaftActorServerConfigurationSupport.java
index b3506ed8d0..29641cb00e 100644
--- a/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/RaftActorServerConfigurationSupport.java
+++ b/opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/RaftActorServerConfigurationSupport.java
@@ -7,10 +7,11 @@
*/
package org.opendaylight.controller.cluster.raft;
+import static java.util.Objects.requireNonNull;
+
import akka.actor.ActorRef;
import akka.actor.ActorSelection;
import akka.actor.Cancellable;
-import com.google.common.base.Preconditions;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collection;
@@ -19,8 +20,7 @@ import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.UUID;
-import javax.annotation.Nullable;
-import org.opendaylight.controller.cluster.raft.ServerConfigurationPayload.ServerInfo;
+import org.eclipse.jdt.annotation.Nullable;
import org.opendaylight.controller.cluster.raft.base.messages.ApplyState;
import org.opendaylight.controller.cluster.raft.base.messages.SnapshotComplete;
import org.opendaylight.controller.cluster.raft.base.messages.TimeoutNow;
@@ -34,6 +34,8 @@ import org.opendaylight.controller.cluster.raft.messages.ServerChangeReply;
import org.opendaylight.controller.cluster.raft.messages.ServerChangeStatus;
import org.opendaylight.controller.cluster.raft.messages.ServerRemoved;
import org.opendaylight.controller.cluster.raft.messages.UnInitializedFollowerSnapshotReply;
+import org.opendaylight.controller.cluster.raft.persisted.ServerConfigurationPayload;
+import org.opendaylight.controller.cluster.raft.persisted.ServerInfo;
import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
import org.opendaylight.yangtools.concepts.Identifier;
import org.opendaylight.yangtools.util.AbstractUUIDIdentifier;
@@ -49,6 +51,7 @@ import scala.concurrent.duration.FiniteDuration;
class RaftActorServerConfigurationSupport {
private static final Logger LOG = LoggerFactory.getLogger(RaftActorServerConfigurationSupport.class);
+ @SuppressWarnings("checkstyle:MemberName")
private final OperationState IDLE = new Idle();
private final RaftActor raftActor;
@@ -59,19 +62,19 @@ class RaftActorServerConfigurationSupport {
private OperationState currentOperationState = IDLE;
- RaftActorServerConfigurationSupport(RaftActor raftActor) {
+ RaftActorServerConfigurationSupport(final RaftActor raftActor) {
this.raftActor = raftActor;
this.raftContext = raftActor.getRaftActorContext();
}
- boolean handleMessage(Object message, ActorRef sender) {
- if(message instanceof AddServer) {
+ boolean handleMessage(final Object message, final ActorRef sender) {
+ if (message instanceof AddServer) {
onAddServer((AddServer) message, sender);
return true;
- } else if(message instanceof RemoveServer) {
+ } else if (message instanceof RemoveServer) {
onRemoveServer((RemoveServer) message, sender);
return true;
- } else if(message instanceof ChangeServersVotingStatus) {
+ } else if (message instanceof ChangeServersVotingStatus) {
onChangeServersVotingStatus((ChangeServersVotingStatus) message, sender);
return true;
} else if (message instanceof ServerOperationTimeout) {
@@ -80,9 +83,9 @@ class RaftActorServerConfigurationSupport {
} else if (message instanceof UnInitializedFollowerSnapshotReply) {
currentOperationState.onUnInitializedFollowerSnapshotReply((UnInitializedFollowerSnapshotReply) message);
return true;
- } else if(message instanceof ApplyState) {
+ } else if (message instanceof ApplyState) {
return onApplyState((ApplyState) message);
- } else if(message instanceof SnapshotComplete) {
+ } else if (message instanceof SnapshotComplete) {
currentOperationState.onSnapshotComplete();
return false;
} else {
@@ -90,11 +93,11 @@ class RaftActorServerConfigurationSupport {
}
}
- void onNewLeader(String leaderId) {
+ void onNewLeader(final String leaderId) {
currentOperationState.onNewLeader(leaderId);
}
- private void onChangeServersVotingStatus(ChangeServersVotingStatus message, ActorRef sender) {
+ private void onChangeServersVotingStatus(final ChangeServersVotingStatus message, final ActorRef sender) {
LOG.debug("{}: onChangeServersVotingStatus: {}, state: {}", raftContext.getId(), message,
currentOperationState);
@@ -110,23 +113,23 @@ class RaftActorServerConfigurationSupport {
// Therefore, if the local server is currently non-voting and is to be changed to voting and there is
// no current leader, we will try to elect a leader using the new server config in order to replicate
// the change and progress.
- boolean localServerChangingToVoting = Boolean.TRUE.equals(message.
- getServerVotingStatusMap().get(raftActor.getRaftActorContext().getId()));
+ boolean localServerChangingToVoting = Boolean.TRUE.equals(message
+ .getServerVotingStatusMap().get(raftActor.getRaftActorContext().getId()));
boolean hasNoLeader = raftActor.getLeaderId() == null;
- if(localServerChangingToVoting && !raftContext.isVotingMember() && hasNoLeader) {
+ if (localServerChangingToVoting && !raftContext.isVotingMember() && hasNoLeader) {
currentOperationState.onNewOperation(new ChangeServersVotingStatusContext(message, sender, true));
} else {
onNewOperation(new ChangeServersVotingStatusContext(message, sender, false));
}
}
- private void onRemoveServer(RemoveServer removeServer, ActorRef sender) {
+ private void onRemoveServer(final RemoveServer removeServer, final ActorRef sender) {
LOG.debug("{}: onRemoveServer: {}, state: {}", raftContext.getId(), removeServer, currentOperationState);
boolean isSelf = removeServer.getServerId().equals(raftContext.getId());
- if(isSelf && !raftContext.hasFollowers()) {
+ if (isSelf && !raftContext.hasFollowers()) {
sender.tell(new RemoveServerReply(ServerChangeStatus.NOT_SUPPORTED, raftActor.getLeaderId()),
raftActor.getSelf());
- } else if(!isSelf && !raftContext.getPeerIds().contains(removeServer.getServerId())) {
+ } else if (!isSelf && !raftContext.getPeerIds().contains(removeServer.getServerId())) {
sender.tell(new RemoveServerReply(ServerChangeStatus.DOES_NOT_EXIST, raftActor.getLeaderId()),
raftActor.getSelf());
} else {
@@ -136,9 +139,9 @@ class RaftActorServerConfigurationSupport {
}
}
- private boolean onApplyState(ApplyState applyState) {
+ private boolean onApplyState(final ApplyState applyState) {
Payload data = applyState.getReplicatedLogEntry().getData();
- if(data instanceof ServerConfigurationPayload) {
+ if (data instanceof ServerConfigurationPayload) {
currentOperationState.onApplyState(applyState);
return true;
}
@@ -147,7 +150,7 @@ class RaftActorServerConfigurationSupport {
}
/**
- * The algorithm for AddServer is as follows:
+ * Add a server. The algorithm for AddServer is as follows:
*
* - Add the new server as a peer.
* - Add the new follower to the leader.
@@ -167,13 +170,13 @@ class RaftActorServerConfigurationSupport {
* - Respond to caller with TIMEOUT.
*
*/
- private void onAddServer(AddServer addServer, ActorRef sender) {
+ private void onAddServer(final AddServer addServer, final ActorRef sender) {
LOG.debug("{}: onAddServer: {}, state: {}", raftContext.getId(), addServer, currentOperationState);
onNewOperation(new AddServerContext(addServer, sender));
}
- private void onNewOperation(ServerOperationContext> operationContext) {
+ private void onNewOperation(final ServerOperationContext> operationContext) {
if (raftActor.isLeader()) {
currentOperationState.onNewOperation(operationContext);
} else {
@@ -200,7 +203,7 @@ class RaftActorServerConfigurationSupport {
* Abstract base class for a server operation FSM state. Handles common behavior for all states.
*/
private abstract class OperationState {
- void onNewOperation(ServerOperationContext> operationContext) {
+ void onNewOperation(final ServerOperationContext> operationContext) {
// We're currently processing another operation so queue it to be processed later.
LOG.debug("{}: Server operation already in progress - queueing {}", raftContext.getId(),
@@ -209,15 +212,15 @@ class RaftActorServerConfigurationSupport {
pendingOperationsQueue.add(operationContext);
}
- void onServerOperationTimeout(ServerOperationTimeout timeout) {
+ void onServerOperationTimeout(final ServerOperationTimeout timeout) {
LOG.debug("onServerOperationTimeout should not be called in state {}", this);
}
- void onUnInitializedFollowerSnapshotReply(UnInitializedFollowerSnapshotReply reply) {
+ void onUnInitializedFollowerSnapshotReply(final UnInitializedFollowerSnapshotReply reply) {
LOG.debug("onUnInitializedFollowerSnapshotReply was called in state {}", this);
}
- void onApplyState(ApplyState applyState) {
+ void onApplyState(final ApplyState applyState) {
LOG.debug("onApplyState was called in state {}", this);
}
@@ -225,17 +228,18 @@ class RaftActorServerConfigurationSupport {
}
- void onNewLeader(String newLeader) {
+ void onNewLeader(final String newLeader) {
}
- protected void persistNewServerConfiguration(ServerOperationContext> operationContext){
+ protected void persistNewServerConfiguration(final ServerOperationContext> operationContext) {
raftContext.setDynamicServerConfigurationInUse();
ServerConfigurationPayload payload = raftContext.getPeerServerInfo(
operationContext.includeSelfInNewConfiguration(raftActor));
LOG.debug("{}: New server configuration : {}", raftContext.getId(), payload.getServerConfig());
- raftActor.persistData(operationContext.getClientRequestor(), operationContext.getContextId(), payload);
+ raftActor.persistData(operationContext.getClientRequestor(), operationContext.getContextId(),
+ payload, false);
currentOperationState = new Persisting(operationContext, newTimer(new ServerOperationTimeout(
operationContext.getLoggingContext())));
@@ -243,8 +247,9 @@ class RaftActorServerConfigurationSupport {
sendReply(operationContext, ServerChangeStatus.OK);
}
- protected void operationComplete(ServerOperationContext> operationContext, @Nullable ServerChangeStatus replyStatus) {
- if(replyStatus != null) {
+ protected void operationComplete(final ServerOperationContext> operationContext,
+ final @Nullable ServerChangeStatus replyStatus) {
+ if (replyStatus != null) {
sendReply(operationContext, replyStatus);
}
@@ -257,23 +262,24 @@ class RaftActorServerConfigurationSupport {
currentOperationState = IDLE;
ServerOperationContext> nextOperation = pendingOperationsQueue.poll();
- if(nextOperation != null) {
+ if (nextOperation != null) {
RaftActorServerConfigurationSupport.this.onNewOperation(nextOperation);
}
}
- protected void sendReply(ServerOperationContext> operationContext, ServerChangeStatus status) {
- LOG.debug("{}: Returning {} for operation {}", raftContext.getId(), status, operationContext.getOperation());
+ protected void sendReply(final ServerOperationContext> operationContext, final ServerChangeStatus status) {
+ LOG.debug("{}: Returning {} for operation {}", raftContext.getId(), status,
+ operationContext.getOperation());
operationContext.getClientRequestor().tell(operationContext.newReply(status, raftActor.getLeaderId()),
raftActor.self());
}
- Cancellable newTimer(Object message) {
+ Cancellable newTimer(final Object message) {
return newTimer(raftContext.getConfigParams().getElectionTimeOutInterval().$times(2), message);
}
- Cancellable newTimer(FiniteDuration timeout, Object message) {
+ Cancellable newTimer(final FiniteDuration timeout, final Object message) {
return raftContext.getActorSystem().scheduler().scheduleOnce(
timeout, raftContext.getActor(), message,
raftContext.getActorSystem().dispatcher(), raftContext.getActor());
@@ -290,12 +296,12 @@ class RaftActorServerConfigurationSupport {
*/
private final class Idle extends OperationState {
@Override
- public void onNewOperation(ServerOperationContext> operationContext) {
+ public void onNewOperation(final ServerOperationContext> operationContext) {
operationContext.newInitialOperationState(RaftActorServerConfigurationSupport.this).initiate();
}
@Override
- public void onApplyState(ApplyState applyState) {
+ public void onApplyState(final ApplyState applyState) {
// Noop - we override b/c ApplyState is called normally for followers in the idle state.
}
}
@@ -308,16 +314,16 @@ class RaftActorServerConfigurationSupport {
private final Cancellable timer;
private boolean timedOut = false;
- Persisting(ServerOperationContext> operationContext, Cancellable timer) {
+ Persisting(final ServerOperationContext> operationContext, final Cancellable timer) {
this.operationContext = operationContext;
this.timer = timer;
}
@Override
- public void onApplyState(ApplyState applyState) {
+ public void onApplyState(final ApplyState applyState) {
// Sanity check - we could get an ApplyState from a previous operation that timed out so make
// sure it's meant for us.
- if(operationContext.getContextId().equals(applyState.getIdentifier())) {
+ if (operationContext.getContextId().equals(applyState.getIdentifier())) {
LOG.info("{}: {} has been successfully replicated to a majority of followers", raftContext.getId(),
applyState.getReplicatedLogEntry().getData());
@@ -327,7 +333,7 @@ class RaftActorServerConfigurationSupport {
}
@Override
- public void onServerOperationTimeout(ServerOperationTimeout timeout) {
+ public void onServerOperationTimeout(final ServerOperationTimeout timeout) {
LOG.warn("{}: Timeout occured while replicating the new server configuration for {}", raftContext.getId(),
timeout.getLoggingContext());
@@ -335,18 +341,18 @@ class RaftActorServerConfigurationSupport {
// Fail any pending operations
ServerOperationContext> nextOperation = pendingOperationsQueue.poll();
- while(nextOperation != null) {
+ while (nextOperation != null) {
sendReply(nextOperation, ServerChangeStatus.PRIOR_REQUEST_CONSENSUS_TIMEOUT);
nextOperation = pendingOperationsQueue.poll();
}
}
@Override
- public void onNewOperation(ServerOperationContext> operationContext) {
- if(timedOut) {
- sendReply(operationContext, ServerChangeStatus.PRIOR_REQUEST_CONSENSUS_TIMEOUT);
+ public void onNewOperation(final ServerOperationContext> newOperationContext) {
+ if (timedOut) {
+ sendReply(newOperationContext, ServerChangeStatus.PRIOR_REQUEST_CONSENSUS_TIMEOUT);
} else {
- super.onNewOperation(operationContext);
+ super.onNewOperation(newOperationContext);
}
}
}
@@ -357,7 +363,7 @@ class RaftActorServerConfigurationSupport {
private abstract class AddServerState extends OperationState {
private final AddServerContext addServerContext;
- AddServerState(AddServerContext addServerContext) {
+ AddServerState(final AddServerContext addServerContext) {
this.addServerContext = addServerContext;
}
@@ -369,7 +375,7 @@ class RaftActorServerConfigurationSupport {
return newTimer(new ServerOperationTimeout(addServerContext.getOperation().getNewServerId()));
}
- void handleInstallSnapshotTimeout(ServerOperationTimeout timeout) {
+ void handleInstallSnapshotTimeout(final ServerOperationTimeout timeout) {
String serverId = timeout.getLoggingContext();
LOG.debug("{}: handleInstallSnapshotTimeout for new server {}", raftContext.getId(), serverId);
@@ -378,12 +384,13 @@ class RaftActorServerConfigurationSupport {
raftContext.removePeer(serverId);
boolean isLeader = raftActor.isLeader();
- if(isLeader) {
+ if (isLeader) {
AbstractLeader leader = (AbstractLeader) raftActor.getCurrentBehavior();
leader.removeFollower(serverId);
}
- operationComplete(getAddServerContext(), isLeader ? ServerChangeStatus.TIMEOUT : ServerChangeStatus.NO_LEADER);
+ operationComplete(getAddServerContext(), isLeader ? ServerChangeStatus.TIMEOUT
+ : ServerChangeStatus.NO_LEADER);
}
}
@@ -393,18 +400,18 @@ class RaftActorServerConfigurationSupport {
* snapshot capture, if necessary.
*/
private final class InitialAddServerState extends AddServerState implements InitialOperationState {
- InitialAddServerState(AddServerContext addServerContext) {
+ InitialAddServerState(final AddServerContext addServerContext) {
super(addServerContext);
}
@Override
public void initiate() {
- AbstractLeader leader = (AbstractLeader) raftActor.getCurrentBehavior();
+ final AbstractLeader leader = (AbstractLeader) raftActor.getCurrentBehavior();
AddServer addServer = getAddServerContext().getOperation();
LOG.debug("{}: Initiating {}", raftContext.getId(), addServer);
- if(raftContext.getPeerInfo(addServer.getNewServerId()) != null) {
+ if (raftContext.getPeerInfo(addServer.getNewServerId()) != null) {
operationComplete(getAddServerContext(), ServerChangeStatus.ALREADY_EXISTS);
return;
}
@@ -415,10 +422,10 @@ class RaftActorServerConfigurationSupport {
leader.addFollower(addServer.getNewServerId());
- if(votingState == VotingState.VOTING_NOT_INITIALIZED){
+ if (votingState == VotingState.VOTING_NOT_INITIALIZED) {
// schedule the install snapshot timeout timer
Cancellable installSnapshotTimer = newInstallSnapshotTimer();
- if(leader.initiateCaptureSnapshot(addServer.getNewServerId())) {
+ if (leader.initiateCaptureSnapshot(addServer.getNewServerId())) {
LOG.debug("{}: Initiating capture snapshot for new server {}", raftContext.getId(),
addServer.getNewServerId());
@@ -445,13 +452,13 @@ class RaftActorServerConfigurationSupport {
private final class InstallingSnapshot extends AddServerState {
private final Cancellable installSnapshotTimer;
- InstallingSnapshot(AddServerContext addServerContext, Cancellable installSnapshotTimer) {
+ InstallingSnapshot(final AddServerContext addServerContext, final Cancellable installSnapshotTimer) {
super(addServerContext);
- this.installSnapshotTimer = Preconditions.checkNotNull(installSnapshotTimer);
+ this.installSnapshotTimer = requireNonNull(installSnapshotTimer);
}
@Override
- public void onServerOperationTimeout(ServerOperationTimeout timeout) {
+ public void onServerOperationTimeout(final ServerOperationTimeout timeout) {
handleInstallSnapshotTimeout(timeout);
LOG.warn("{}: Timeout occured for new server {} while installing snapshot", raftContext.getId(),
@@ -459,14 +466,14 @@ class RaftActorServerConfigurationSupport {
}
@Override
- public void onUnInitializedFollowerSnapshotReply(UnInitializedFollowerSnapshotReply reply) {
+ public void onUnInitializedFollowerSnapshotReply(final UnInitializedFollowerSnapshotReply reply) {
LOG.debug("{}: onUnInitializedFollowerSnapshotReply: {}", raftContext.getId(), reply);
String followerId = reply.getFollowerId();
// Sanity check to guard against receiving an UnInitializedFollowerSnapshotReply from a prior
// add server operation that timed out.
- if(getAddServerContext().getOperation().getNewServerId().equals(followerId) && raftActor.isLeader()) {
+ if (getAddServerContext().getOperation().getNewServerId().equals(followerId) && raftActor.isLeader()) {
AbstractLeader leader = (AbstractLeader) raftActor.getCurrentBehavior();
raftContext.getPeerInfo(followerId).setVotingState(VotingState.VOTING);
leader.updateMinReplicaCount();
@@ -489,22 +496,22 @@ class RaftActorServerConfigurationSupport {
private final class WaitingForPriorSnapshotComplete extends AddServerState {
private final Cancellable snapshotTimer;
- WaitingForPriorSnapshotComplete(AddServerContext addServerContext, Cancellable snapshotTimer) {
+ WaitingForPriorSnapshotComplete(final AddServerContext addServerContext, final Cancellable snapshotTimer) {
super(addServerContext);
- this.snapshotTimer = Preconditions.checkNotNull(snapshotTimer);
+ this.snapshotTimer = requireNonNull(snapshotTimer);
}
@Override
public void onSnapshotComplete() {
LOG.debug("{}: onSnapshotComplete", raftContext.getId());
- if(!raftActor.isLeader()) {
+ if (!raftActor.isLeader()) {
LOG.debug("{}: No longer the leader", raftContext.getId());
return;
}
AbstractLeader leader = (AbstractLeader) raftActor.getCurrentBehavior();
- if(leader.initiateCaptureSnapshot(getAddServerContext().getOperation().getNewServerId())) {
+ if (leader.initiateCaptureSnapshot(getAddServerContext().getOperation().getNewServerId())) {
LOG.debug("{}: Initiating capture snapshot for new server {}", raftContext.getId(),
getAddServerContext().getOperation().getNewServerId());
@@ -516,7 +523,7 @@ class RaftActorServerConfigurationSupport {
}
@Override
- public void onServerOperationTimeout(ServerOperationTimeout timeout) {
+ public void onServerOperationTimeout(final ServerOperationTimeout timeout) {
handleInstallSnapshotTimeout(timeout);
LOG.warn("{}: Timeout occured for new server {} while waiting for prior snapshot to complete",
@@ -524,7 +531,8 @@ class RaftActorServerConfigurationSupport {
}
}
- private static final class ServerOperationContextIdentifier extends AbstractUUIDIdentifier {
+ private static final class ServerOperationContextIdentifier
+ extends AbstractUUIDIdentifier {
private static final long serialVersionUID = 1L;
ServerOperationContextIdentifier() {
@@ -537,12 +545,12 @@ class RaftActorServerConfigurationSupport {
*
* @param the operation type
*/
- private static abstract class ServerOperationContext {
+ private abstract static class ServerOperationContext {
private final T operation;
private final ActorRef clientRequestor;
private final Identifier contextId;
- ServerOperationContext(T operation, ActorRef clientRequestor){
+ ServerOperationContext(final T operation, final ActorRef clientRequestor) {
this.operation = operation;
this.clientRequestor = clientRequestor;
contextId = new ServerOperationContextIdentifier();
@@ -560,10 +568,10 @@ class RaftActorServerConfigurationSupport {
return clientRequestor;
}
- void operationComplete(RaftActor raftActor, boolean succeeded) {
+ void operationComplete(final RaftActor raftActor, final boolean succeeded) {
}
- boolean includeSelfInNewConfiguration(RaftActor raftActor) {
+ boolean includeSelfInNewConfiguration(final RaftActor raftActor) {
return true;
}
@@ -578,17 +586,17 @@ class RaftActorServerConfigurationSupport {
* Stores context information for an AddServer operation.
*/
private static class AddServerContext extends ServerOperationContext {
- AddServerContext(AddServer addServer, ActorRef clientRequestor) {
+ AddServerContext(final AddServer addServer, final ActorRef clientRequestor) {
super(addServer, clientRequestor);
}
@Override
- Object newReply(ServerChangeStatus status, String leaderId) {
+ Object newReply(final ServerChangeStatus status, final String leaderId) {
return new AddServerReply(status, leaderId);
}
@Override
- InitialOperationState newInitialOperationState(RaftActorServerConfigurationSupport support) {
+ InitialOperationState newInitialOperationState(final RaftActorServerConfigurationSupport support) {
return support.new InitialAddServerState(this);
}
@@ -601,8 +609,8 @@ class RaftActorServerConfigurationSupport {
private abstract class RemoveServerState extends OperationState {
private final RemoveServerContext removeServerContext;
- protected RemoveServerState(RemoveServerContext removeServerContext) {
- this.removeServerContext = Preconditions.checkNotNull(removeServerContext);
+ protected RemoveServerState(final RemoveServerContext removeServerContext) {
+ this.removeServerContext = requireNonNull(removeServerContext);
}
@@ -611,9 +619,9 @@ class RaftActorServerConfigurationSupport {
}
}
- private final class InitialRemoveServerState extends RemoveServerState implements InitialOperationState{
+ private final class InitialRemoveServerState extends RemoveServerState implements InitialOperationState {
- protected InitialRemoveServerState(RemoveServerContext removeServerContext) {
+ protected InitialRemoveServerState(final RemoveServerContext removeServerContext) {
super(removeServerContext);
}
@@ -621,7 +629,9 @@ class RaftActorServerConfigurationSupport {
public void initiate() {
String serverId = getRemoveServerContext().getOperation().getServerId();
raftContext.removePeer(serverId);
- ((AbstractLeader)raftActor.getCurrentBehavior()).removeFollower(serverId);
+ AbstractLeader leader = (AbstractLeader)raftActor.getCurrentBehavior();
+ leader.removeFollower(serverId);
+ leader.updateMinReplicaCount();
persistNewServerConfiguration(getRemoveServerContext());
}
@@ -630,30 +640,31 @@ class RaftActorServerConfigurationSupport {
private static class RemoveServerContext extends ServerOperationContext {
private final String peerAddress;
- RemoveServerContext(RemoveServer operation, String peerAddress, ActorRef clientRequestor) {
+ RemoveServerContext(final RemoveServer operation, final String peerAddress, final ActorRef clientRequestor) {
super(operation, clientRequestor);
this.peerAddress = peerAddress;
}
@Override
- Object newReply(ServerChangeStatus status, String leaderId) {
+ Object newReply(final ServerChangeStatus status, final String leaderId) {
return new RemoveServerReply(status, leaderId);
}
@Override
- InitialOperationState newInitialOperationState(RaftActorServerConfigurationSupport support) {
+ InitialOperationState newInitialOperationState(final RaftActorServerConfigurationSupport support) {
return support.new InitialRemoveServerState(this);
}
@Override
- void operationComplete(RaftActor raftActor, boolean succeeded) {
- if(peerAddress != null) {
- raftActor.context().actorSelection(peerAddress).tell(new ServerRemoved(getOperation().getServerId()), raftActor.getSelf());
+ void operationComplete(final RaftActor raftActor, final boolean succeeded) {
+ if (peerAddress != null) {
+ raftActor.context().actorSelection(peerAddress).tell(
+ new ServerRemoved(getOperation().getServerId()), raftActor.getSelf());
}
}
@Override
- boolean includeSelfInNewConfiguration(RaftActor raftActor) {
+ boolean includeSelfInNewConfiguration(final RaftActor raftActor) {
return !getOperation().getServerId().equals(raftActor.getId());
}
@@ -666,31 +677,33 @@ class RaftActorServerConfigurationSupport {
private static class ChangeServersVotingStatusContext extends ServerOperationContext {
private final boolean tryToElectLeader;
- ChangeServersVotingStatusContext(ChangeServersVotingStatus convertMessage, ActorRef clientRequestor,
- boolean tryToElectLeader) {
+ ChangeServersVotingStatusContext(final ChangeServersVotingStatus convertMessage, final ActorRef clientRequestor,
+ final boolean tryToElectLeader) {
super(convertMessage, clientRequestor);
this.tryToElectLeader = tryToElectLeader;
}
@Override
- InitialOperationState newInitialOperationState(RaftActorServerConfigurationSupport support) {
+ InitialOperationState newInitialOperationState(final RaftActorServerConfigurationSupport support) {
return support.new ChangeServersVotingStatusState(this, tryToElectLeader);
}
@Override
- Object newReply(ServerChangeStatus status, String leaderId) {
+ Object newReply(final ServerChangeStatus status, final String leaderId) {
return new ServerChangeReply(status, leaderId);
}
@Override
- void operationComplete(final RaftActor raftActor, boolean succeeded) {
+ void operationComplete(final RaftActor raftActor, final boolean succeeded) {
// If this leader changed to non-voting we need to step down as leader so we'll try to transfer
// leadership.
- boolean localServerChangedToNonVoting = Boolean.FALSE.equals(getOperation().
- getServerVotingStatusMap().get(raftActor.getRaftActorContext().getId()));
+ boolean localServerChangedToNonVoting = Boolean.FALSE.equals(getOperation()
+ .getServerVotingStatusMap().get(raftActor.getRaftActorContext().getId()));
if (succeeded && localServerChangedToNonVoting) {
LOG.debug("Leader changed to non-voting - trying leadership transfer");
raftActor.becomeNonVoting();
+ } else if (raftActor.isLeader()) {
+ raftActor.onVotingStateChangeComplete();
}
}
@@ -704,8 +717,8 @@ class RaftActorServerConfigurationSupport {
private final ChangeServersVotingStatusContext changeVotingStatusContext;
private final boolean tryToElectLeader;
- ChangeServersVotingStatusState(ChangeServersVotingStatusContext changeVotingStatusContext,
- boolean tryToElectLeader) {
+ ChangeServersVotingStatusState(final ChangeServersVotingStatusContext changeVotingStatusContext,
+ final boolean tryToElectLeader) {
this.changeVotingStatusContext = changeVotingStatusContext;
this.tryToElectLeader = tryToElectLeader;
}
@@ -714,9 +727,9 @@ class RaftActorServerConfigurationSupport {
public void initiate() {
LOG.debug("Initiating ChangeServersVotingStatusState");
- if(tryToElectLeader) {
+ if (tryToElectLeader) {
initiateLocalLeaderElection();
- } else if(updateLocalPeerInfo()) {
+ } else if (updateLocalPeerInfo()) {
persistNewServerConfiguration(changeVotingStatusContext);
}
}
@@ -725,7 +738,7 @@ class RaftActorServerConfigurationSupport {
LOG.debug("{}: Sending local ElectionTimeout to start leader election", raftContext.getId());
ServerConfigurationPayload previousServerConfig = raftContext.getPeerServerInfo(true);
- if(!updateLocalPeerInfo()) {
+ if (!updateLocalPeerInfo()) {
return;
}
@@ -739,20 +752,20 @@ class RaftActorServerConfigurationSupport {
// Check if new voting state would leave us with no voting members.
boolean atLeastOneVoting = false;
- for(ServerInfo info: newServerInfoList) {
- if(info.isVoting()) {
+ for (ServerInfo info: newServerInfoList) {
+ if (info.isVoting()) {
atLeastOneVoting = true;
break;
}
}
- if(!atLeastOneVoting) {
+ if (!atLeastOneVoting) {
operationComplete(changeVotingStatusContext, ServerChangeStatus.INVALID_REQUEST);
return false;
}
raftContext.updatePeerIds(new ServerConfigurationPayload(newServerInfoList));
- if(raftActor.getCurrentBehavior() instanceof AbstractLeader) {
+ if (raftActor.getCurrentBehavior() instanceof AbstractLeader) {
AbstractLeader leader = (AbstractLeader) raftActor.getCurrentBehavior();
leader.updateMinReplicaCount();
}
@@ -761,15 +774,17 @@ class RaftActorServerConfigurationSupport {
}
private List newServerInfoList() {
- Map serverVotingStatusMap = changeVotingStatusContext.getOperation().getServerVotingStatusMap();
+ Map serverVotingStatusMap = changeVotingStatusContext.getOperation()
+ .getServerVotingStatusMap();
List newServerInfoList = new ArrayList<>();
- for(String peerId: raftContext.getPeerIds()) {
- newServerInfoList.add(new ServerInfo(peerId, serverVotingStatusMap.containsKey(peerId) ?
- serverVotingStatusMap.get(peerId) : raftContext.getPeerInfo(peerId).isVoting()));
+ for (String peerId: raftContext.getPeerIds()) {
+ newServerInfoList.add(new ServerInfo(peerId, serverVotingStatusMap.containsKey(peerId)
+ ? serverVotingStatusMap.get(peerId) : raftContext.getPeerInfo(peerId).isVoting()));
}
newServerInfoList.add(new ServerInfo(raftContext.getId(), serverVotingStatusMap.containsKey(
- raftContext.getId()) ? serverVotingStatusMap.get(raftContext.getId()) : raftContext.isVotingMember()));
+ raftContext.getId()) ? serverVotingStatusMap.get(raftContext.getId())
+ : raftContext.isVotingMember()));
return newServerInfoList;
}
@@ -780,8 +795,8 @@ class RaftActorServerConfigurationSupport {
private final ChangeServersVotingStatusContext operationContext;
private final Cancellable timer;
- WaitingForLeaderElected(ChangeServersVotingStatusContext operationContext,
- ServerConfigurationPayload previousServerConfig) {
+ WaitingForLeaderElected(final ChangeServersVotingStatusContext operationContext,
+ final ServerConfigurationPayload previousServerConfig) {
this.operationContext = operationContext;
this.previousServerConfig = previousServerConfig;
@@ -790,8 +805,8 @@ class RaftActorServerConfigurationSupport {
}
@Override
- void onNewLeader(String newLeader) {
- if(newLeader == null) {
+ void onNewLeader(final String newLeader) {
+ if (newLeader == null) {
return;
}
@@ -799,7 +814,7 @@ class RaftActorServerConfigurationSupport {
timer.cancel();
- if(raftActor.isLeader()) {
+ if (raftActor.isLeader()) {
persistNewServerConfiguration(operationContext);
} else {
// Edge case - some other node became leader so forward the operation.
@@ -814,7 +829,7 @@ class RaftActorServerConfigurationSupport {
}
@Override
- void onServerOperationTimeout(ServerOperationTimeout timeout) {
+ void onServerOperationTimeout(final ServerOperationTimeout timeout) {
LOG.warn("{}: Leader election timed out - cannot apply operation {}",
raftContext.getId(), timeout.getLoggingContext());
@@ -837,20 +852,20 @@ class RaftActorServerConfigurationSupport {
// tried yet.
Map serverVotingStatusMap = operationContext.getOperation().getServerVotingStatusMap();
ActorSelection forwardToPeerActor = null;
- for(Map.Entry e: serverVotingStatusMap.entrySet()) {
+ for (Map.Entry e: serverVotingStatusMap.entrySet()) {
Boolean isVoting = e.getValue();
String serverId = e.getKey();
PeerInfo peerInfo = raftContext.getPeerInfo(serverId);
- if(isVoting && peerInfo != null && !peerInfo.isVoting() && !serversVisited.contains(serverId)) {
+ if (isVoting && peerInfo != null && !peerInfo.isVoting() && !serversVisited.contains(serverId)) {
ActorSelection actor = raftContext.getPeerActorSelection(serverId);
- if(actor != null) {
+ if (actor != null) {
forwardToPeerActor = actor;
break;
}
}
}
- if(forwardToPeerActor != null) {
+ if (forwardToPeerActor != null) {
LOG.debug("{}: Found server {} to forward to", raftContext.getId(), forwardToPeerActor);
forwardToPeerActor.tell(new ChangeServersVotingStatus(serverVotingStatusMap, serversVisited),
@@ -865,8 +880,8 @@ class RaftActorServerConfigurationSupport {
static class ServerOperationTimeout {
private final String loggingContext;
- ServerOperationTimeout(String loggingContext){
- this.loggingContext = Preconditions.checkNotNull(loggingContext, "loggingContext should not be null");
+ ServerOperationTimeout(final String loggingContext) {
+ this.loggingContext = requireNonNull(loggingContext, "loggingContext should not be null");
}
String getLoggingContext() {