Bug 2187: EOS shard recovery after AddShardReplica
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / entityownership / DistributedEntityOwnershipService.java
index f782fc24147d0830965de2fe335e3ca603e5a29f..0277271020c2bd91b21a12f6481e2b8d8082624c 100644 (file)
@@ -21,6 +21,7 @@ import java.util.Collection;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 import java.util.concurrent.TimeUnit;
+import javax.annotation.Nonnull;
 import org.opendaylight.controller.cluster.datastore.DistributedDataStore;
 import org.opendaylight.controller.cluster.datastore.config.Configuration;
 import org.opendaylight.controller.cluster.datastore.config.ModuleShardConfiguration;
@@ -62,12 +63,14 @@ public class DistributedEntityOwnershipService implements EntityOwnershipService
     private static final Timeout MESSAGE_TIMEOUT = new Timeout(1, TimeUnit.MINUTES);
 
     private final DistributedDataStore datastore;
+    private final EntityOwnerSelectionStrategyConfig strategyConfig;
     private final ConcurrentMap<Entity, Entity> registeredEntities = new ConcurrentHashMap<>();
     private volatile ActorRef localEntityOwnershipShard;
     private volatile DataTree localEntityOwnershipShardDataTree;
 
-    public DistributedEntityOwnershipService(DistributedDataStore datastore) {
-        this.datastore = datastore;
+    public DistributedEntityOwnershipService(DistributedDataStore datastore, EntityOwnerSelectionStrategyConfig strategyConfig) {
+        this.datastore = Preconditions.checkNotNull(datastore);
+        this.strategyConfig = Preconditions.checkNotNull(strategyConfig);
     }
 
     public void start() {
@@ -86,7 +89,7 @@ public class DistributedEntityOwnershipService implements EntityOwnershipService
             @Override
             public void onComplete(Throwable failure, Object response) {
                 if(failure != null) {
-                    LOG.error("Failed to create {} shard", ENTITY_OWNERSHIP_SHARD_NAME);
+                    LOG.error("Failed to create {} shard", ENTITY_OWNERSHIP_SHARD_NAME, failure);
                 } else {
                     LOG.info("Successfully created {} shard", ENTITY_OWNERSHIP_SHARD_NAME);
                 }
@@ -190,6 +193,11 @@ public class DistributedEntityOwnershipService implements EntityOwnershipService
         return Optional.of(new EntityOwnershipState(isOwner, hasOwner));
     }
 
+    @Override
+    public boolean isCandidateRegistered(@Nonnull Entity entity) {
+        return registeredEntities.get(entity) != null;
+    }
+
     private DataTree getLocalEntityOwnershipShardDataTree() {
         if(localEntityOwnershipShardDataTree == null) {
             try {
@@ -220,7 +228,7 @@ public class DistributedEntityOwnershipService implements EntityOwnershipService
 
     protected EntityOwnershipShard.Builder newShardBuilder() {
         return EntityOwnershipShard.newBuilder().localMemberName(datastore.getActorContext().getCurrentMemberName())
-                .ownerSelectionStrategyConfig(EntityOwnerSelectionStrategyConfig.newBuilder().build());
+                .ownerSelectionStrategyConfig(this.strategyConfig);
     }
 
     @VisibleForTesting