- LOG.debug("Cold server configuration : {}", cOld.toString());
- /* get new server configuration list */
- List <String> cNew = new ArrayList<String>(cOld);
- cNew.add(followerId);
- LOG.debug("Cnew server configuration : {}", cNew.toString());
- // construct the peer list
- ServerConfigurationPayload servPayload = new ServerConfigurationPayload(cOld, cNew);
- /* TODO - persist new configuration - CHECK WHETHER USING getId below is correct */
- raftActor.persistData(sender, context.getId(), servPayload);
- }
-
- private void stopFollowerTimer() {
- if (followerTimeout != null && !followerTimeout.isCancelled()) {
- followerTimeout.cancel();
- }
- }
-
- private void onFollowerCatchupTimeout(RaftActor raftActor, ActorRef sender, String serverId){
-
- LOG.debug("onFollowerCatchupTimeout: {}", serverId);
- AbstractLeader leader = (AbstractLeader) raftActor.getCurrentBehavior();
- // cleanup
- context.removePeer(serverId);
- leader.removeFollower(serverId);
- LOG.warn("onFollowerCatchupTimeout - Timeout occured for server - {} while installing snapshot", serverId);
- respondToClient(raftActor,ServerChangeStatus.TIMEOUT);
- }
-
- private void respondToClient(RaftActor raftActor, ServerChangeStatus result){
-
- int size = followerInfoQueue.size();
-
- // remove the entry from the queue
- CatchupFollowerInfo fInfo = followerInfoQueue.remove();
- // get the sender
- ActorRef toClient = fInfo.getClientRequestor();
-
- toClient.tell(new AddServerReply(result, raftActor.getLeaderId()), raftActor.self());
- LOG.debug("Response returned is {} for server {} ", result, fInfo.getAddServer().getNewServerId());
- if(!followerInfoQueue.isEmpty()){
- processAddServer(raftActor);
- }
- }
-
- // mantain sender actorRef
- private class CatchupFollowerInfo {
- private final AddServer addServer;
+
+ @Override
+ public void initiate() {
+ AbstractLeader leader = (AbstractLeader) raftActor.getCurrentBehavior();
+ AddServer addServer = getAddServerContext().getOperation();
+
+ LOG.debug("{}: Initiating {}", raftContext.getId(), addServer);
+
+ if(raftContext.getPeerInfo(addServer.getNewServerId()) != null) {
+ operationComplete(getAddServerContext(), ServerChangeStatus.ALREADY_EXISTS);
+ return;
+ }
+
+ VotingState votingState = addServer.isVotingMember() ? VotingState.VOTING_NOT_INITIALIZED :
+ VotingState.NON_VOTING;
+ raftContext.addToPeers(addServer.getNewServerId(), addServer.getNewServerAddress(), votingState);
+
+ leader.addFollower(addServer.getNewServerId());
+
+ if(votingState == VotingState.VOTING_NOT_INITIALIZED){
+ // schedule the install snapshot timeout timer
+ Cancellable installSnapshotTimer = newInstallSnapshotTimer();
+ if(leader.initiateCaptureSnapshot(addServer.getNewServerId())) {
+ LOG.debug("{}: Initiating capture snapshot for new server {}", raftContext.getId(),
+ addServer.getNewServerId());
+
+ currentOperationState = new InstallingSnapshot(getAddServerContext(), installSnapshotTimer);
+ } else {
+ LOG.debug("{}: Snapshot already in progress - waiting for completion", raftContext.getId());
+
+ currentOperationState = new WaitingForPriorSnapshotComplete(getAddServerContext(),
+ installSnapshotTimer);
+ }
+ } else {
+ LOG.debug("{}: New follower is non-voting - directly persisting new server configuration",
+ raftContext.getId());
+
+ persistNewServerConfiguration(getAddServerContext());
+ }
+ }
+ }
+
+ /**
+ * The AddServer operation state for when the catch-up snapshot is being installed. It handles successful
+ * reply or timeout.
+ */
+ private class InstallingSnapshot extends AddServerState {
+ private final Cancellable installSnapshotTimer;
+
+ InstallingSnapshot(AddServerContext addServerContext, Cancellable installSnapshotTimer) {
+ super(addServerContext);
+ this.installSnapshotTimer = Preconditions.checkNotNull(installSnapshotTimer);
+ }
+
+ @Override
+ public void onServerOperationTimeout(ServerOperationTimeout timeout) {
+ handleInstallSnapshotTimeout(timeout);
+
+ LOG.warn("{}: Timeout occured for new server {} while installing snapshot", raftContext.getId(),
+ timeout.getServerId());
+ }
+
+ @Override
+ public void onUnInitializedFollowerSnapshotReply(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()) {
+ AbstractLeader leader = (AbstractLeader) raftActor.getCurrentBehavior();
+ raftContext.getPeerInfo(followerId).setVotingState(VotingState.VOTING);
+ leader.updateMinReplicaCount();
+
+ persistNewServerConfiguration(getAddServerContext());
+
+ installSnapshotTimer.cancel();
+ } else {
+ LOG.debug("{}: Dropping UnInitializedFollowerSnapshotReply for server {}: {}",
+ raftContext.getId(), followerId,
+ !raftActor.isLeader() ? "not leader" : "server Id doesn't match");
+ }
+ }
+ }
+
+ /**
+ * The AddServer operation state for when there is a snapshot already in progress. When the current
+ * snapshot completes, it initiates an install snapshot.
+ */
+ private class WaitingForPriorSnapshotComplete extends AddServerState {
+ private final Cancellable snapshotTimer;
+
+ WaitingForPriorSnapshotComplete(AddServerContext addServerContext, Cancellable snapshotTimer) {
+ super(addServerContext);
+ this.snapshotTimer = Preconditions.checkNotNull(snapshotTimer);
+ }
+
+ @Override
+ public void onSnapshotComplete() {
+ LOG.debug("{}: onSnapshotComplete", raftContext.getId());
+
+ if(!raftActor.isLeader()) {
+ LOG.debug("{}: No longer the leader", raftContext.getId());
+ return;
+ }
+
+ AbstractLeader leader = (AbstractLeader) raftActor.getCurrentBehavior();
+ if(leader.initiateCaptureSnapshot(getAddServerContext().getOperation().getNewServerId())) {
+ LOG.debug("{}: Initiating capture snapshot for new server {}", raftContext.getId(),
+ getAddServerContext().getOperation().getNewServerId());
+
+ currentOperationState = new InstallingSnapshot(getAddServerContext(),
+ newInstallSnapshotTimer());
+
+ snapshotTimer.cancel();
+ }
+ }
+
+ @Override
+ public void onServerOperationTimeout(ServerOperationTimeout timeout) {
+ handleInstallSnapshotTimeout(timeout);
+
+ LOG.warn("{}: Timeout occured for new server {} while waiting for prior snapshot to complete",
+ raftContext.getId(), timeout.getServerId());
+ }
+ }
+
+ /**
+ * Stores context information for a server operation.
+ *
+ * @param <T> the operation type
+ */
+ private static abstract class ServerOperationContext<T> {
+ private final T operation;