Bug 2187: AddServer: check if already exists 72/29572/3
authorTom Pantelis <tpanteli@brocade.com>
Tue, 10 Nov 2015 22:32:36 +0000 (17:32 -0500)
committerTom Pantelis <tpanteli@brocade.com>
Thu, 12 Nov 2015 08:44:19 +0000 (03:44 -0500)
On AddServer, if the new server already exists as a peer return
ALREADY_EXISTS status reply.

Change-Id: I3b324850e1f05fce72eced3b2ced52f1510973fe
Signed-off-by: Tom Pantelis <tpanteli@brocade.com>
opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/RaftActorServerConfigurationSupport.java
opendaylight/md-sal/sal-akka-raft/src/main/java/org/opendaylight/controller/cluster/raft/messages/ServerChangeStatus.java
opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/RaftActorServerConfigurationSupportTest.java

index 6c8bf14136d21a218181ffe02aa1cedfa3429de1..258287a36ae1a95258b9eb2e258e8d5621c63399 100644 (file)
@@ -321,11 +321,15 @@ class RaftActorServerConfigurationSupport {
         @Override
         public void initiate(RaftActor raftActor) {
             AbstractLeader leader = (AbstractLeader) raftActor.getCurrentBehavior();
-
             AddServer addServer = getAddServerContext().getOperation();
 
             LOG.debug("{}: Initiating {}", raftContext.getId(), addServer);
 
+            if(raftContext.getPeerInfo(addServer.getNewServerId()) != null) {
+                operationComplete(raftActor, getAddServerContext(), ServerChangeStatus.ALREADY_EXISTS);
+                return;
+            }
+
             VotingState votingState = addServer.isVotingMember() ? VotingState.VOTING_NOT_INITIALIZED :
                     VotingState.NON_VOTING;
             raftContext.addToPeers(addServer.getNewServerId(), addServer.getNewServerAddress(), votingState);
index b5caa1656c4f149a30f3d0e0054375ed595b2240..ca2f35cbf6a7a50542e22cc3e6fc336f17b8fb0e 100644 (file)
@@ -15,5 +15,6 @@ package org.opendaylight.controller.cluster.raft.messages;
 public enum ServerChangeStatus {
     OK,
     NO_LEADER,
-    TIMEOUT
+    TIMEOUT,
+    ALREADY_EXISTS
 }
index 3e04e4c1bb396000f3010b507cea6aecc33610c1..16acb410fb169702ee82cc7908f3e0b61abc5eb2 100644 (file)
@@ -600,6 +600,21 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest {
                 votingServer(NEW_SERVER_ID));
     }
 
+    @Test
+    public void testAddServerWithExistingServer() {
+        RaftActorContext initialActorContext = new MockRaftActorContext();
+
+        TestActorRef<MockLeaderRaftActor> leaderActor = actorFactory.createTestActor(
+                MockLeaderRaftActor.props(ImmutableMap.of(FOLLOWER_ID, followerActor.path().toString()),
+                        initialActorContext).withDispatcher(Dispatchers.DefaultDispatcherId()),
+                actorFactory.generateActorId(LEADER_ID));
+
+        leaderActor.tell(new AddServer(FOLLOWER_ID, followerActor.path().toString(), true), testKit.getRef());
+
+        AddServerReply addServerReply = testKit.expectMsgClass(JavaTestKit.duration("5 seconds"), AddServerReply.class);
+        assertEquals("getStatus", ServerChangeStatus.ALREADY_EXISTS, addServerReply.getStatus());
+    }
+
     @Test
     public void testAddServerForwardedToLeader() {
         DefaultConfigParamsImpl configParams = new DefaultConfigParamsImpl();
@@ -666,14 +681,8 @@ public class RaftActorServerConfigurationSupportTest extends AbstractActorTest {
         configParams.setElectionTimeoutFactor(100000);
         ElectionTermImpl termInfo = new ElectionTermImpl(NO_PERSISTENCE, id, LOG);
         termInfo.update(1, LEADER_ID);
-        RaftActorContext followerActorContext = new RaftActorContextImpl(actor, actor.underlyingActor().getContext(),
-                id, termInfo, -1, -1,
-                ImmutableMap.of(LEADER_ID, ""), configParams, NO_PERSISTENCE, LOG);
-        followerActorContext.setCommitIndex(-1);
-        followerActorContext.setLastApplied(-1);
-        followerActorContext.setReplicatedLog(new MockRaftActorContext.MockReplicatedLogBuilder().build());
-
-        return followerActorContext;
+        return new RaftActorContextImpl(actor, actor.underlyingActor().getContext(),
+                id, termInfo, -1, -1, ImmutableMap.of(LEADER_ID, ""), configParams, NO_PERSISTENCE, LOG);
     }
 
     static abstract class AbstractMockRaftActor extends MockRaftActor {