X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-akka-raft%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fraft%2FRaftActorServerConfigurationSupport.java;h=207642a7213e637fc8af6539ea9e989c0a75d43e;hb=refs%2Fchanges%2F03%2F29803%2F3;hp=258287a36ae1a95258b9eb2e258e8d5621c63399;hpb=8b3db9dc8b4ac15d0b8bc133966fd3521f4909da;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 258287a36a..207642a721 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 @@ -11,22 +11,21 @@ import akka.actor.ActorRef; import akka.actor.ActorSelection; import akka.actor.Cancellable; import com.google.common.base.Preconditions; -import java.util.ArrayList; -import java.util.Collection; import java.util.LinkedList; -import java.util.List; import java.util.Queue; import java.util.UUID; import java.util.concurrent.TimeUnit; import javax.annotation.Nullable; -import org.opendaylight.controller.cluster.raft.ServerConfigurationPayload.ServerInfo; import org.opendaylight.controller.cluster.raft.base.messages.ApplyState; import org.opendaylight.controller.cluster.raft.base.messages.SnapshotComplete; import org.opendaylight.controller.cluster.raft.behaviors.AbstractLeader; import org.opendaylight.controller.cluster.raft.messages.AddServer; import org.opendaylight.controller.cluster.raft.messages.AddServerReply; import org.opendaylight.controller.cluster.raft.messages.FollowerCatchUpTimeout; +import org.opendaylight.controller.cluster.raft.messages.RemoveServer; +import org.opendaylight.controller.cluster.raft.messages.RemoveServerReply; 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.protobuff.client.messages.Payload; import org.slf4j.Logger; @@ -55,14 +54,17 @@ class RaftActorServerConfigurationSupport { boolean handleMessage(Object message, RaftActor raftActor, ActorRef sender) { if(message instanceof AddServer) { - onAddServer((AddServer)message, raftActor, sender); + onAddServer((AddServer) message, raftActor, sender); + return true; + } else if(message instanceof RemoveServer) { + onRemoveServer((RemoveServer) message, raftActor, sender); return true; } else if (message instanceof FollowerCatchUpTimeout) { - currentOperationState.onFollowerCatchupTimeout(raftActor, (FollowerCatchUpTimeout)message); + currentOperationState.onFollowerCatchupTimeout(raftActor, (FollowerCatchUpTimeout) message); return true; } else if (message instanceof UnInitializedFollowerSnapshotReply) { currentOperationState.onUnInitializedFollowerSnapshotReply(raftActor, - (UnInitializedFollowerSnapshotReply)message); + (UnInitializedFollowerSnapshotReply) message); return true; } else if(message instanceof ApplyState) { return onApplyState((ApplyState) message, raftActor); @@ -74,6 +76,19 @@ class RaftActorServerConfigurationSupport { } } + private void onRemoveServer(RemoveServer removeServer, RaftActor raftActor, ActorRef sender) { + if(removeServer.getServerId().equals(raftActor.getLeaderId())){ + // Removing current leader is not supported yet + // TODO: To properly support current leader removal we need to first implement transfer of leadership + LOG.debug("Cannot remove {} replica because it is the Leader", removeServer.getServerId()); + sender.tell(new RemoveServerReply(ServerChangeStatus.NOT_SUPPORTED, raftActor.getLeaderId()), raftActor.getSelf()); + } else if(!raftContext.getPeerIds().contains(removeServer.getServerId())) { + sender.tell(new RemoveServerReply(ServerChangeStatus.DOES_NOT_EXIST, raftActor.getLeaderId()), raftActor.getSelf()); + } else { + onNewOperation(raftActor, new RemoveServerContext(removeServer, raftContext.getPeerAddress(removeServer.getServerId()), sender)); + } + } + private boolean onApplyState(ApplyState applyState, RaftActor raftActor) { Payload data = applyState.getReplicatedLogEntry().getData(); if(data instanceof ServerConfigurationPayload) { @@ -183,17 +198,9 @@ class RaftActorServerConfigurationSupport { } protected void persistNewServerConfiguration(RaftActor raftActor, ServerOperationContext operationContext){ - Collection peers = raftContext.getPeers(); - List newConfig = new ArrayList<>(peers.size() + 1); - for(PeerInfo peer: peers) { - newConfig.add(new ServerInfo(peer.getId(), peer.isVoting())); - } - - newConfig.add(new ServerInfo(raftContext.getId(), true)); - - LOG.debug("{}: Persisting new server configuration : {}", raftContext.getId(), newConfig); - - ServerConfigurationPayload payload = new ServerConfigurationPayload(newConfig); + raftContext.setDynamicServerConfigurationInUse(); + ServerConfigurationPayload payload = raftContext.getPeerServerInfo(); + LOG.debug("{}: New server configuration : {}", raftContext.getId(), payload.getServerConfig()); raftActor.persistData(operationContext.getClientRequestor(), operationContext.getContextId(), payload); @@ -208,6 +215,8 @@ class RaftActorServerConfigurationSupport { sendReply(raftActor, operationContext, replyStatus); } + operationContext.operationComplete(raftActor, replyStatus); + currentOperationState = IDLE; ServerOperationContext nextOperation = pendingOperationsQueue.poll(); @@ -260,7 +269,7 @@ class RaftActorServerConfigurationSupport { // 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())) { - LOG.info("{}: {} has been successfully replicated to a majority of followers", + LOG.info("{}: {} has been successfully replicated to a majority of followers", raftActor.getId(), applyState.getReplicatedLogEntry().getData()); operationComplete(raftActor, operationContext, null); @@ -307,6 +316,7 @@ class RaftActorServerConfigurationSupport { operationComplete(raftActor, getAddServerContext(), isLeader ? ServerChangeStatus.TIMEOUT : ServerChangeStatus.NO_LEADER); } + } /** @@ -476,6 +486,8 @@ class RaftActorServerConfigurationSupport { abstract Object newReply(ServerChangeStatus status, String leaderId); abstract InitialOperationState newInitialOperationState(RaftActorServerConfigurationSupport support); + + abstract void operationComplete(RaftActor raftActor, ServerChangeStatus serverChangeStatus); } /** @@ -495,5 +507,65 @@ class RaftActorServerConfigurationSupport { InitialOperationState newInitialOperationState(RaftActorServerConfigurationSupport support) { return support.new InitialAddServerState(this); } + + @Override + void operationComplete(RaftActor raftActor, ServerChangeStatus serverChangeStatus) { + + } + } + + private abstract class RemoveServerState extends AbstractOperationState { + private final RemoveServerContext removeServerContext; + + + protected RemoveServerState(RemoveServerContext removeServerContext) { + this.removeServerContext = Preconditions.checkNotNull(removeServerContext); + + } + + public RemoveServerContext getRemoveServerContext() { + return removeServerContext; + } + + } + + private class InitialRemoveServerState extends RemoveServerState implements InitialOperationState{ + + protected InitialRemoveServerState(RemoveServerContext removeServerContext) { + super(removeServerContext); + } + + @Override + public void initiate(RaftActor raftActor) { + raftContext.removePeer(getRemoveServerContext().getOperation().getServerId()); + persistNewServerConfiguration(raftActor, getRemoveServerContext()); + } + } + + private static class RemoveServerContext extends ServerOperationContext { + private final String peerAddress; + + RemoveServerContext(RemoveServer operation, String peerAddress, ActorRef clientRequestor) { + super(operation, clientRequestor); + this.peerAddress = peerAddress; + } + + @Override + Object newReply(ServerChangeStatus status, String leaderId) { + return new RemoveServerReply(status, leaderId); + } + + @Override + InitialOperationState newInitialOperationState(RaftActorServerConfigurationSupport support) { + return support.new InitialRemoveServerState(this); + } + + @Override + void operationComplete(RaftActor raftActor, ServerChangeStatus serverChangeStatus) { + if(peerAddress != null) { + raftActor.context().actorSelection(peerAddress).tell(new ServerRemoved(getOperation().getServerId()), raftActor.getSelf()); + } + } + } }