Pass in EntityOwnerSelectionStrategyConfig when constructing EntityOwnershipShard 03/29403/5
authorMoiz Raja <moraja@cisco.com>
Sun, 11 Oct 2015 01:44:17 +0000 (18:44 -0700)
committerTom Pantelis <tpanteli@brocade.com>
Thu, 12 Nov 2015 20:58:56 +0000 (15:58 -0500)
Change-Id: I56c2f4f87c61e81b662cd0b30c60775389e9b9a3
Signed-off-by: Moiz Raja <moraja@cisco.com>
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/entityownership/DistributedEntityOwnershipService.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/entityownership/EntityOwnershipShard.java
opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/entityownership/DistributedEntityOwnershipServiceTest.java
opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/entityownership/EntityOwnershipShardTest.java

index a2619b3..f782fc2 100644 (file)
@@ -28,6 +28,7 @@ import org.opendaylight.controller.cluster.datastore.entityownership.messages.Re
 import org.opendaylight.controller.cluster.datastore.entityownership.messages.RegisterListenerLocal;
 import org.opendaylight.controller.cluster.datastore.entityownership.messages.UnregisterCandidateLocal;
 import org.opendaylight.controller.cluster.datastore.entityownership.messages.UnregisterListenerLocal;
+import org.opendaylight.controller.cluster.datastore.entityownership.selectionstrategy.EntityOwnerSelectionStrategyConfig;
 import org.opendaylight.controller.cluster.datastore.messages.CreateShard;
 import org.opendaylight.controller.cluster.datastore.messages.GetShardDataTree;
 import org.opendaylight.controller.cluster.datastore.shardstrategy.ModuleShardStrategy;
@@ -218,7 +219,8 @@ public class DistributedEntityOwnershipService implements EntityOwnershipService
     }
 
     protected EntityOwnershipShard.Builder newShardBuilder() {
-        return EntityOwnershipShard.newBuilder().localMemberName(datastore.getActorContext().getCurrentMemberName());
+        return EntityOwnershipShard.newBuilder().localMemberName(datastore.getActorContext().getCurrentMemberName())
+                .ownerSelectionStrategyConfig(EntityOwnerSelectionStrategyConfig.newBuilder().build());
     }
 
     @VisibleForTesting
index 2f3b51e..ba75128 100644 (file)
@@ -27,7 +27,6 @@ import akka.actor.ActorRef;
 import akka.actor.ActorSelection;
 import akka.actor.Cancellable;
 import akka.pattern.Patterns;
-import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
 import com.google.common.base.Strings;
@@ -79,8 +78,8 @@ class EntityOwnershipShard extends Shard {
     private final EntityOwnershipListenerSupport listenerSupport;
     private final Set<String> downPeerMemberNames = new HashSet<>();
     private final Map<String, String> peerIdToMemberNames = new HashMap<>();
-    private EntityOwnerSelectionStrategyConfig strategyConfig;
-    private Map<YangInstanceIdentifier, Cancellable> entityToScheduledOwnershipTask = new HashMap<>();
+    private final EntityOwnerSelectionStrategyConfig strategyConfig;
+    private final Map<YangInstanceIdentifier, Cancellable> entityToScheduledOwnershipTask = new HashMap<>();
 
     private static DatastoreContext noPersistenceDatastoreContext(DatastoreContext datastoreContext) {
         return DatastoreContext.newBuilderFrom(datastoreContext).persistent(false).build();
@@ -91,7 +90,7 @@ class EntityOwnershipShard extends Shard {
         this.localMemberName = builder.localMemberName;
         this.commitCoordinator = new EntityOwnershipShardCommitCoordinator(builder.localMemberName, LOG);
         this.listenerSupport = new EntityOwnershipListenerSupport(getContext(), persistenceId());
-        this.strategyConfig = EntityOwnerSelectionStrategyConfig.newBuilder().build();
+        this.strategyConfig = builder.ownerSelectionStrategyConfig;
 
         for(String peerId: getRaftActorContext().getPeerIds()) {
             ShardIdentifier shardId = ShardIdentifier.builder().fromShardIdString(peerId).build();
@@ -397,7 +396,7 @@ class EntityOwnershipShard extends Shard {
             public void onEntity(MapEntryNode entityTypeNode, MapEntryNode entityNode) {
                 Optional<DataContainerChild<? extends PathArgument, ?>> possibleOwner =
                         entityNode.getChild(ENTITY_OWNER_NODE_ID);
-                if(possibleOwner.isPresent() && owner.equals(possibleOwner.get().getValue().toString())) {
+                if (possibleOwner.isPresent() && owner.equals(possibleOwner.get().getValue().toString())) {
                     walker.onEntity(entityTypeNode, entityNode);
                 }
             }
@@ -491,21 +490,13 @@ class EntityOwnershipShard extends Shard {
         void onEntity(MapEntryNode entityTypeNode, MapEntryNode entityNode);
     }
 
-    @VisibleForTesting
-    void addEntityOwnerSelectionStrategy(String entityType,
-                                         Class<? extends EntityOwnerSelectionStrategy> clazz,
-                                         long delay){
-        EntityOwnerSelectionStrategyConfig config = EntityOwnerSelectionStrategyConfig.newBuilder()
-                .addStrategy(entityType, clazz, delay).build();
-        this.strategyConfig = config;
-    }
-
     public static Builder newBuilder() {
         return new Builder();
     }
 
     static class Builder extends Shard.AbstractBuilder<Builder, EntityOwnershipShard> {
         private String localMemberName;
+        private EntityOwnerSelectionStrategyConfig ownerSelectionStrategyConfig;
 
         protected Builder() {
             super(EntityOwnershipShard.class);
@@ -517,10 +508,17 @@ class EntityOwnershipShard extends Shard {
             return this;
         }
 
+        Builder ownerSelectionStrategyConfig(EntityOwnerSelectionStrategyConfig ownerSelectionStrategyConfig){
+            checkSealed();
+            this.ownerSelectionStrategyConfig = ownerSelectionStrategyConfig;
+            return this;
+        }
+
         @Override
         protected void verify() {
             super.verify();
             Preconditions.checkNotNull(localMemberName, "localMemberName should not be null");
+            Preconditions.checkNotNull(ownerSelectionStrategyConfig, "ownerSelectionStrategyConfig should not be null");
         }
     }
 }
index d38959d..17eb1f6 100644 (file)
@@ -47,6 +47,7 @@ import org.opendaylight.controller.cluster.datastore.entityownership.messages.Re
 import org.opendaylight.controller.cluster.datastore.entityownership.messages.RegisterListenerLocal;
 import org.opendaylight.controller.cluster.datastore.entityownership.messages.UnregisterCandidateLocal;
 import org.opendaylight.controller.cluster.datastore.entityownership.messages.UnregisterListenerLocal;
+import org.opendaylight.controller.cluster.datastore.entityownership.selectionstrategy.EntityOwnerSelectionStrategyConfig;
 import org.opendaylight.controller.cluster.datastore.messages.GetShardDataTree;
 import org.opendaylight.controller.cluster.datastore.utils.MockClusterWrapper;
 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
@@ -319,7 +320,8 @@ public class DistributedEntityOwnershipServiceTest extends AbstractEntityOwnersh
 
     static class TestShardBuilder extends EntityOwnershipShard.Builder {
         TestShardBuilder() {
-            localMemberName("member-1");
+            localMemberName("member-1").ownerSelectionStrategyConfig(
+                    EntityOwnerSelectionStrategyConfig.newBuilder().build());
         }
 
         private final AtomicReference<CountDownLatch> messageReceived = new AtomicReference<>();
@@ -329,6 +331,7 @@ public class DistributedEntityOwnershipServiceTest extends AbstractEntityOwnersh
 
         @Override
         public Props props() {
+            verify();
             return Props.create(TestEntityOwnershipShard.class,this, messageClass, messageReceived,
                     receivedMessage, dataTree);
         }
index 63ec7cf..eb64920 100644 (file)
@@ -48,6 +48,7 @@ import org.opendaylight.controller.cluster.datastore.entityownership.messages.Re
 import org.opendaylight.controller.cluster.datastore.entityownership.messages.RegisterListenerLocal;
 import org.opendaylight.controller.cluster.datastore.entityownership.messages.UnregisterCandidateLocal;
 import org.opendaylight.controller.cluster.datastore.entityownership.messages.UnregisterListenerLocal;
+import org.opendaylight.controller.cluster.datastore.entityownership.selectionstrategy.EntityOwnerSelectionStrategyConfig;
 import org.opendaylight.controller.cluster.datastore.entityownership.selectionstrategy.LastCandidateSelectionStrategy;
 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
 import org.opendaylight.controller.cluster.datastore.messages.BatchedModifications;
@@ -405,15 +406,15 @@ public class EntityOwnershipShardTest extends AbstractEntityOwnershipTest {
 
         TestActorRef<EntityOwnershipShard> peer1 = actorFactory.createTestActor(newShardProps(peerId1,
                 ImmutableMap.<String, String>builder().put(leaderId.toString(), ""). put(peerId2.toString(), "").build(),
-                        peerMemberName1).withDispatcher(Dispatchers.DefaultDispatcherId()), peerId1.toString());
+                        peerMemberName1, EntityOwnerSelectionStrategyConfig.newBuilder().build()).withDispatcher(Dispatchers.DefaultDispatcherId()), peerId1.toString());
 
         TestActorRef<EntityOwnershipShard> peer2 = actorFactory.createTestActor(newShardProps(peerId2,
                 ImmutableMap.<String, String>builder().put(leaderId.toString(), ""). put(peerId1.toString(), "").build(),
-                        peerMemberName2). withDispatcher(Dispatchers.DefaultDispatcherId()), peerId2.toString());
+                        peerMemberName2, EntityOwnerSelectionStrategyConfig.newBuilder().build()). withDispatcher(Dispatchers.DefaultDispatcherId()), peerId2.toString());
 
         TestActorRef<EntityOwnershipShard> leader = actorFactory.createTestActor(newShardProps(leaderId,
                 ImmutableMap.<String, String>builder().put(peerId1.toString(), peer1.path().toString()).
-                        put(peerId2.toString(), peer2.path().toString()).build(), LOCAL_MEMBER_NAME).
+                        put(peerId2.toString(), peer2.path().toString()).build(), LOCAL_MEMBER_NAME, EntityOwnerSelectionStrategyConfig.newBuilder().build()).
                 withDispatcher(Dispatchers.DefaultDispatcherId()), leaderId.toString());
         leader.tell(new ElectionTimeout(), leader);
 
@@ -498,7 +499,7 @@ public class EntityOwnershipShardTest extends AbstractEntityOwnershipTest {
 
         peer2 = actorFactory.createTestActor(newShardProps(peerId2,
                 ImmutableMap.<String, String>builder().put(leaderId.toString(), ""). put(peerId1.toString(), "").build(),
-                        peerMemberName2). withDispatcher(Dispatchers.DefaultDispatcherId()), peerId2.toString());
+                        peerMemberName2, EntityOwnerSelectionStrategyConfig.newBuilder().build()). withDispatcher(Dispatchers.DefaultDispatcherId()), peerId2.toString());
         leader.tell(new PeerUp(peerMemberName2, peerId2.toString()), ActorRef.noSender());
         // Send PeerUp again - should be noop
         leader.tell(new PeerUp(peerMemberName2, peerId2.toString()), ActorRef.noSender());
@@ -539,7 +540,7 @@ public class EntityOwnershipShardTest extends AbstractEntityOwnershipTest {
 
         peer1 = actorFactory.createTestActor(newShardProps(peerId1,
                 ImmutableMap.<String, String>builder().put(leaderId.toString(), ""). put(peerId2.toString(), "").build(),
-                        peerMemberName1).withDispatcher(Dispatchers.DefaultDispatcherId()), peerId1.toString());
+                        peerMemberName1, EntityOwnerSelectionStrategyConfig.newBuilder().build()).withDispatcher(Dispatchers.DefaultDispatcherId()), peerId1.toString());
         leader.tell(new PeerUp(peerMemberName1, peerId1.toString()), ActorRef.noSender());
 
         verifyOwner(leader, ENTITY_TYPE, ENTITY_ID4, "");
@@ -588,7 +589,7 @@ public class EntityOwnershipShardTest extends AbstractEntityOwnershipTest {
 
         TestActorRef<EntityOwnershipShard> leader = actorFactory.createTestActor(newShardProps(leaderId,
                 ImmutableMap.<String, String>builder().put(localId.toString(), shard.path().toString()).build(),
-                    LOCAL_MEMBER_NAME).withDispatcher(Dispatchers.DefaultDispatcherId()), leaderId.toString());
+                    LOCAL_MEMBER_NAME, EntityOwnerSelectionStrategyConfig.newBuilder().build()).withDispatcher(Dispatchers.DefaultDispatcherId()), leaderId.toString());
         leader.tell(new ElectionTimeout(), leader);
 
         kit.waitUntilLeader(leader);
@@ -803,14 +804,20 @@ public class EntityOwnershipShardTest extends AbstractEntityOwnershipTest {
         return newShardProps(Collections.<String,String>emptyMap());
     }
 
+    private Props newShardProps(EntityOwnerSelectionStrategyConfig strategyConfig) {
+        return newShardProps(newShardId(LOCAL_MEMBER_NAME), Collections.<String,String>emptyMap(),
+                LOCAL_MEMBER_NAME, strategyConfig);
+    }
+
     private Props newShardProps(Map<String,String> peers) {
-        return newShardProps(newShardId(LOCAL_MEMBER_NAME), peers, LOCAL_MEMBER_NAME);
+        return newShardProps(newShardId(LOCAL_MEMBER_NAME), peers, LOCAL_MEMBER_NAME, EntityOwnerSelectionStrategyConfig.newBuilder().build());
     }
 
-    private Props newShardProps(ShardIdentifier shardId, Map<String,String> peers, String memberName) {
+    private Props newShardProps(ShardIdentifier shardId, Map<String,String> peers, String memberName,
+                                EntityOwnerSelectionStrategyConfig config) {
         return EntityOwnershipShard.newBuilder().id(shardId).peerAddresses(peers).
                 datastoreContext(dataStoreContextBuilder.build()).schemaContext(SCHEMA_CONTEXT).
-                localMemberName(memberName).props().withDispatcher(Dispatchers.DefaultDispatcherId());
+                localMemberName(memberName).ownerSelectionStrategyConfig(config).props().withDispatcher(Dispatchers.DefaultDispatcherId());
     }
 
     private static ShardIdentifier newShardId(String memberName) {
@@ -877,8 +884,9 @@ public class EntityOwnershipShardTest extends AbstractEntityOwnershipTest {
     @Test
     public void testDelayedEntityOwnerSelection() throws Exception {
         ShardTestKit kit = new ShardTestKit(getSystem());
-        TestActorRef<EntityOwnershipShard> shard = actorFactory.createTestActor(newShardProps());
-        shard.underlyingActor().addEntityOwnerSelectionStrategy(ENTITY_TYPE, LastCandidateSelectionStrategy.class, 500);
+        EntityOwnerSelectionStrategyConfig.Builder builder
+                = EntityOwnerSelectionStrategyConfig.newBuilder().addStrategy(ENTITY_TYPE, LastCandidateSelectionStrategy.class, 500);
+        TestActorRef<EntityOwnershipShard> shard = actorFactory.createTestActor(newShardProps(builder.build()));
         kit.waitUntilLeader(shard);
 
         Entity entity = new Entity(ENTITY_TYPE, ENTITY_ID1);