Fix reading of EntityOwnerSelectionStrategy 92/33492/4
authorMoiz Raja <moraja@cisco.com>
Thu, 7 Jan 2016 22:07:02 +0000 (14:07 -0800)
committerGerrit Code Review <gerrit@opendaylight.org>
Thu, 4 Feb 2016 18:49:10 +0000 (18:49 +0000)
1. The pid used for reading a config admin file should not have hyphens
   so replaced them with dots
2. The config admin returns properties that are not from the file
   so we need a way to ignore them. I specifically look for the
   a prefix of "entity.type." and ignore the other properties

Change-Id: I26a66176583ec39cbdb78fec749022429218e005
Signed-off-by: Moiz Raja <moraja@cisco.com>
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/entityownership/EntityOwnershipShard.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/entityownership/selectionstrategy/EntityOwnerSelectionStrategyConfigReader.java
opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/entityownership/selectionstrategy/EntityOwnerSelectionStrategyConfigReaderTest.java

index 74cc6717dcb44b330d38e211a899690a39b0e316..89b7518a0326f5d1ae7044bf938e9ea7a5b20da1 100644 (file)
@@ -139,6 +139,8 @@ class EntityOwnershipShard extends Shard {
     }
 
     private void onSelectOwner(SelectOwner selectOwner) {
+        LOG.debug("{}: onSelectOwner: {}", persistenceId(), selectOwner);
+
         String currentOwner = getCurrentOwner(selectOwner.getEntityPath());
         if(Strings.isNullOrEmpty(currentOwner)) {
             String entityType = EntityOwnersModel.entityTypeFromEntityPath(selectOwner.getEntityPath());
@@ -325,6 +327,8 @@ class EntityOwnershipShard extends Shard {
 
         String currentOwner = getCurrentOwner(message.getEntityPath());
         EntityOwnerSelectionStrategy strategy = getEntityOwnerElectionStrategy(message.getEntityPath());
+
+        LOG.debug("{}: Using strategy {} to select owner", persistenceId(), strategy);
         if(Strings.isNullOrEmpty(currentOwner)){
             if(strategy.getSelectionDelayInMillis() == 0L) {
                 String entityType = EntityOwnersModel.entityTypeFromEntityPath(message.getEntityPath());
@@ -443,6 +447,9 @@ class EntityOwnershipShard extends Shard {
         if(lastScheduledTask != null && !lastScheduledTask.isCancelled()){
             lastScheduledTask.cancel();
         }
+
+        LOG.debug("{}: Scheduling owner selection after {} ms", persistenceId(), strategy.getSelectionDelayInMillis());
+
         lastScheduledTask = context().system().scheduler().scheduleOnce(
                 FiniteDuration.apply(strategy.getSelectionDelayInMillis(), TimeUnit.MILLISECONDS)
                 , self(), new SelectOwner(entityPath, allCandidates, strategy)
index b6092b2e00cc39334ab8475424fc42976ee3c4c2..cc9ff63aba74063812f664285d2d706285811946 100644 (file)
@@ -21,9 +21,11 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 public class EntityOwnerSelectionStrategyConfigReader {
-    public static final String CONFIG_ID = "org.opendaylight.controller.cluster.entity-owner-selection-strategies";
+    public static final String CONFIG_ID = "org.opendaylight.controller.cluster.entity.owner.selection.strategies";
 
     private static final Logger LOG = LoggerFactory.getLogger(EntityOwnerSelectionStrategyConfigReader.class);
+    private static final String ENTITY_TYPE_PREFIX = "entity.type.";
+
     private final BundleContext bundleContext;
     private final EntityOwnerSelectionStrategyConfig config;
 
@@ -51,7 +53,7 @@ public class EntityOwnerSelectionStrategyConfigReader {
                     String key = keys.nextElement();
                     String strategyProps = (String) properties.get(key);
                     String[] strategyClassAndDelay = strategyProps.split(",");
-                    if(strategyClassAndDelay.length >= 1) {
+                    if(key.startsWith(ENTITY_TYPE_PREFIX)) {
                         @SuppressWarnings("unchecked")
                         Class<? extends EntityOwnerSelectionStrategy> aClass
                         = (Class<? extends EntityOwnerSelectionStrategy>) getClass().getClassLoader().loadClass(strategyClassAndDelay[0]);
@@ -59,9 +61,14 @@ public class EntityOwnerSelectionStrategyConfigReader {
                         if(strategyClassAndDelay.length > 1){
                             delay = Long.parseLong(strategyClassAndDelay[1]);
                         }
-                        builder.addStrategy(key, aClass, delay);
+                        String entityType = key.substring(key.lastIndexOf(".") + 1);
+                        builder.addStrategy(entityType, aClass, delay);
+                    } else {
+                        LOG.debug("Ignoring non-conforming property key : {}, value : {}", key, strategyProps);
                     }
                 }
+            } else {
+                LOG.error("Could not read strategy configuration file, will use default configuration");
             }
         } catch(Exception e){
             LOG.warn("Failed to read selection strategy configuration file. All configuration will be ignored.", e);
index 252e7b7cb18182ce21a41d901fd0a6ceada68100..61613c77613f0f21703c02e0509449a4440ebe4b 100644 (file)
@@ -49,7 +49,7 @@ public class EntityOwnerSelectionStrategyConfigReaderTest {
     @Test
     public void testReadStrategies(){
         Hashtable<String, Object> props = new Hashtable<>();
-        props.put("test", "org.opendaylight.controller.cluster.datastore.entityownership.selectionstrategy.LastCandidateSelectionStrategy,100");
+        props.put("entity.type.test", "org.opendaylight.controller.cluster.datastore.entityownership.selectionstrategy.LastCandidateSelectionStrategy,100");
 
         doReturn(props).when(mockConfig).getProperties();
 
@@ -92,7 +92,7 @@ public class EntityOwnerSelectionStrategyConfigReaderTest {
     @Test
     public void testReadStrategiesInvalidDelay(){
         Hashtable<String, Object> props = new Hashtable<>();
-        props.put("test", "org.opendaylight.controller.cluster.datastore.entityownership.selectionstrategy.LastCandidateSelectionStrategy,foo");
+        props.put("entity.type.test", "org.opendaylight.controller.cluster.datastore.entityownership.selectionstrategy.LastCandidateSelectionStrategy,foo");
 
         doReturn(props).when(mockConfig).getProperties();
 
@@ -104,7 +104,7 @@ public class EntityOwnerSelectionStrategyConfigReaderTest {
     @Test
     public void testReadStrategiesInvalidClassType(){
         Hashtable<String, Object> props = new Hashtable<>();
-        props.put("test", "String,100");
+        props.put("entity.type.test", "String,100");
 
         doReturn(props).when(mockConfig).getProperties();
 
@@ -116,8 +116,8 @@ public class EntityOwnerSelectionStrategyConfigReaderTest {
     @Test
     public void testReadStrategiesMissingDelay(){
         Hashtable<String, Object> props = new Hashtable<>();
-        props.put("test", "org.opendaylight.controller.cluster.datastore.entityownership.selectionstrategy.LastCandidateSelectionStrategy,100");
-        props.put("test1", "org.opendaylight.controller.cluster.datastore.entityownership.selectionstrategy.LastCandidateSelectionStrategy");
+        props.put("entity.type.test", "org.opendaylight.controller.cluster.datastore.entityownership.selectionstrategy.LastCandidateSelectionStrategy,100");
+        props.put("entity.type.test1", "org.opendaylight.controller.cluster.datastore.entityownership.selectionstrategy.LastCandidateSelectionStrategy");
 
         doReturn(props).when(mockConfig).getProperties();