class RaftActorServerConfigurationSupport {
private static final Logger LOG = LoggerFactory.getLogger(RaftActorServerConfigurationSupport.class);
+ @SuppressWarnings("checkstyle:MemberName")
private final OperationState IDLE = new Idle();
private final RaftActor raftActor;
}
boolean handleMessage(Object message, ActorRef sender) {
- if(message instanceof AddServer) {
+ 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) {
} 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 {
// 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) {
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 {
private boolean onApplyState(ApplyState applyState) {
Payload data = applyState.getReplicatedLogEntry().getData();
- if(data instanceof ServerConfigurationPayload) {
+ if (data instanceof ServerConfigurationPayload) {
currentOperationState.onApplyState(applyState);
return true;
}
void onNewLeader(String newLeader) {
}
- protected void persistNewServerConfiguration(ServerOperationContext<?> operationContext){
+ protected void persistNewServerConfiguration(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())));
sendReply(operationContext, ServerChangeStatus.OK);
}
- protected void operationComplete(ServerOperationContext<?> operationContext, @Nullable ServerChangeStatus replyStatus) {
- if(replyStatus != null) {
+ protected void operationComplete(ServerOperationContext<?> operationContext,
+ @Nullable ServerChangeStatus replyStatus) {
+ if (replyStatus != null) {
sendReply(operationContext, replyStatus);
}
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());
+ LOG.debug("{}: Returning {} for operation {}", raftContext.getId(), status,
+ operationContext.getOperation());
operationContext.getClientRequestor().tell(operationContext.newReply(status, raftActor.getLeaderId()),
raftActor.self());
public void onApplyState(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());
// 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<?> newOperationContext) {
- if(timedOut) {
+ if (timedOut) {
sendReply(newOperationContext, ServerChangeStatus.PRIOR_REQUEST_CONSENSUS_TIMEOUT);
} else {
super.onNewOperation(newOperationContext);
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);
}
}
@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;
}
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());
// 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();
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());
}
}
- private static final class ServerOperationContextIdentifier extends AbstractUUIDIdentifier<ServerOperationContextIdentifier> {
+ private static final class ServerOperationContextIdentifier
+ extends AbstractUUIDIdentifier<ServerOperationContextIdentifier> {
private static final long serialVersionUID = 1L;
ServerOperationContextIdentifier() {
private final ActorRef clientRequestor;
private final Identifier contextId;
- ServerOperationContext(T operation, ActorRef clientRequestor){
+ ServerOperationContext(T operation, ActorRef clientRequestor) {
this.operation = operation;
this.clientRequestor = clientRequestor;
contextId = new ServerOperationContextIdentifier();
}
}
- private final class InitialRemoveServerState extends RemoveServerState implements InitialOperationState{
+ private final class InitialRemoveServerState extends RemoveServerState implements InitialOperationState {
protected InitialRemoveServerState(RemoveServerContext removeServerContext) {
super(removeServerContext);
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());
}
@Override
void operationComplete(RaftActor raftActor, boolean succeeded) {
- if(peerAddress != null) {
- raftActor.context().actorSelection(peerAddress).tell(new ServerRemoved(getOperation().getServerId()), raftActor.getSelf());
+ if (peerAddress != null) {
+ raftActor.context().actorSelection(peerAddress).tell(
+ new ServerRemoved(getOperation().getServerId()), raftActor.getSelf());
}
}
void operationComplete(final RaftActor raftActor, 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();
public void initiate() {
LOG.debug("Initiating ChangeServersVotingStatusState");
- if(tryToElectLeader) {
+ if (tryToElectLeader) {
initiateLocalLeaderElection();
- } else if(updateLocalPeerInfo()) {
+ } else if (updateLocalPeerInfo()) {
persistNewServerConfiguration(changeVotingStatusContext);
}
}
LOG.debug("{}: Sending local ElectionTimeout to start leader election", raftContext.getId());
ServerConfigurationPayload previousServerConfig = raftContext.getPeerServerInfo(true);
- if(!updateLocalPeerInfo()) {
+ if (!updateLocalPeerInfo()) {
return;
}
// 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();
}
}
private List<ServerInfo> newServerInfoList() {
- Map<String, Boolean> serverVotingStatusMap = changeVotingStatusContext.getOperation().getServerVotingStatusMap();
+ Map<String, Boolean> serverVotingStatusMap = changeVotingStatusContext.getOperation()
+ .getServerVotingStatusMap();
List<ServerInfo> 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;
}
@Override
void onNewLeader(String newLeader) {
- if(newLeader == null) {
+ if (newLeader == null) {
return;
}
timer.cancel();
- if(raftActor.isLeader()) {
+ if (raftActor.isLeader()) {
persistNewServerConfiguration(operationContext);
} else {
// Edge case - some other node became leader so forward the operation.
// tried yet.
Map<String, Boolean> serverVotingStatusMap = operationContext.getOperation().getServerVotingStatusMap();
ActorSelection forwardToPeerActor = null;
- for(Map.Entry<String, Boolean> e: serverVotingStatusMap.entrySet()) {
+ for (Map.Entry<String, Boolean> 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),
static class ServerOperationTimeout {
private final String loggingContext;
- ServerOperationTimeout(String loggingContext){
- this.loggingContext = Preconditions.checkNotNull(loggingContext, "loggingContext should not be null");
+ ServerOperationTimeout(String loggingContext) {
+ this.loggingContext = Preconditions.checkNotNull(loggingContext, "loggingContext should not be null");
}
String getLoggingContext() {