BUG 2138: Introduce prefix based shards into ShardManager
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / config / ConfigurationImpl.java
index 2f1e88910e34f30bbabfe11e1fad7ea4bbba8656..59acdbdb01f8346232123e5952951a76b073a8c6 100644 (file)
@@ -11,19 +11,32 @@ package org.opendaylight.controller.cluster.datastore.config;
 import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableSet;
+import java.util.AbstractMap.SimpleEntry;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Map.Entry;
 import java.util.Set;
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+import org.opendaylight.controller.cluster.access.concepts.MemberName;
+import org.opendaylight.controller.cluster.datastore.shardstrategy.PrefixShardStrategy;
 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategy;
 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategyFactory;
+import org.opendaylight.controller.cluster.datastore.utils.ClusterUtils;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 
+// TODO clean this up once we get rid of module based configuration, prefix one should be alot simpler
 public class ConfigurationImpl implements Configuration {
     private volatile Map<String, ModuleConfig> moduleConfigMap;
 
+    // TODO should this be initialized with something? on restart we should restore the shards from configuration?
+    private volatile Map<YangInstanceIdentifier, PrefixShardConfiguration> prefixConfigMap = Collections.emptyMap();
+
     // Look up maps to speed things up
 
     private volatile Map<String, String> namespaceToModuleName;
@@ -34,7 +47,12 @@ public class ConfigurationImpl implements Configuration {
     }
 
     public ConfigurationImpl(final ModuleShardConfigProvider provider) {
-        this.moduleConfigMap = ImmutableMap.copyOf(provider.retrieveModuleConfigs(this));
+        ImmutableMap.Builder<String, ModuleConfig> mapBuilder = ImmutableMap.builder();
+        for (Map.Entry<String, ModuleConfig.Builder> e: provider.retrieveModuleConfigs(this).entrySet()) {
+            mapBuilder.put(e.getKey(), e.getValue().build());
+        }
+
+        this.moduleConfigMap = mapBuilder.build();
 
         this.allShardNames = createAllShardNames(moduleConfigMap.values());
         this.namespaceToModuleName = createNamespaceToModuleName(moduleConfigMap.values());
@@ -42,7 +60,7 @@ public class ConfigurationImpl implements Configuration {
 
     private static Set<String> createAllShardNames(Iterable<ModuleConfig> moduleConfigs) {
         final ImmutableSet.Builder<String> builder = ImmutableSet.builder();
-        for(ModuleConfig moduleConfig : moduleConfigs) {
+        for (ModuleConfig moduleConfig : moduleConfigs) {
             builder.addAll(moduleConfig.getShardNames());
         }
 
@@ -51,9 +69,9 @@ public class ConfigurationImpl implements Configuration {
 
     private static Map<String, String> createNamespaceToModuleName(Iterable<ModuleConfig> moduleConfigs) {
         final ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
-        for(ModuleConfig moduleConfig : moduleConfigs) {
-            if(moduleConfig.getNameSpace() != null) {
-                builder.put(moduleConfig.getNameSpace(), moduleConfig.getName());
+        for (ModuleConfig moduleConfig : moduleConfigs) {
+            if (moduleConfig.getNamespace() != null) {
+                builder.put(moduleConfig.getNamespace(), moduleConfig.getName());
             }
         }
 
@@ -61,13 +79,13 @@ public class ConfigurationImpl implements Configuration {
     }
 
     @Override
-    public Collection<String> getMemberShardNames(final String memberName){
+    public Collection<String> getMemberShardNames(final MemberName memberName) {
         Preconditions.checkNotNull(memberName, "memberName should not be null");
 
         List<String> shards = new ArrayList<>();
-        for(ModuleConfig moduleConfig: moduleConfigMap.values()) {
-            for(ShardConfig shardConfig: moduleConfig.getShardConfigs()) {
-                if(shardConfig.getReplicas().contains(memberName)) {
+        for (ModuleConfig moduleConfig: moduleConfigMap.values()) {
+            for (ShardConfig shardConfig: moduleConfig.getShardConfigs()) {
+                if (shardConfig.getReplicas().contains(memberName)) {
                     shards.add(shardConfig.getName());
                 }
             }
@@ -88,7 +106,7 @@ public class ConfigurationImpl implements Configuration {
         Preconditions.checkNotNull(moduleName, "moduleName should not be null");
 
         ModuleConfig moduleConfig = moduleConfigMap.get(moduleName);
-        return moduleConfig != null ? moduleConfig.getShardStrategy(): null;
+        return moduleConfig != null ? moduleConfig.getShardStrategy() : null;
     }
 
     @Override
@@ -98,20 +116,45 @@ public class ConfigurationImpl implements Configuration {
         ModuleConfig moduleConfig = moduleConfigMap.get(moduleName);
         Collection<ShardConfig> shardConfigs = moduleConfig != null ? moduleConfig.getShardConfigs() :
             Collections.<ShardConfig>emptySet();
-        return !shardConfigs.isEmpty() ? shardConfigs.iterator().next().getName(): null;
+        return !shardConfigs.isEmpty() ? shardConfigs.iterator().next().getName() : null;
     }
 
+    @Nullable
     @Override
-    public Collection<String> getMembersFromShardName(final String shardName) {
+    public String getShardNameForPrefix(@Nonnull final YangInstanceIdentifier prefix) {
+        Preconditions.checkNotNull(prefix, "prefix should not be null");
+
+        Entry<YangInstanceIdentifier, PrefixShardConfiguration> bestMatchEntry =
+                new SimpleEntry<>(YangInstanceIdentifier.EMPTY, null);
+
+        for (Entry<YangInstanceIdentifier, PrefixShardConfiguration> entry : prefixConfigMap.entrySet()) {
+            if (entry.getKey().contains(prefix) && entry.getKey().getPathArguments().size()
+                    > bestMatchEntry.getKey().getPathArguments().size()) {
+                bestMatchEntry = entry;
+            }
+        }
+
+        //TODO we really should have mapping based on prefix instead of Strings
+        return ClusterUtils.getCleanShardName(bestMatchEntry.getValue().getPrefix().getRootIdentifier());
+    }
+
+    @Override
+    public Collection<MemberName> getMembersFromShardName(final String shardName) {
         Preconditions.checkNotNull(shardName, "shardName should not be null");
 
-        for(ModuleConfig moduleConfig: moduleConfigMap.values()) {
+        for (ModuleConfig moduleConfig: moduleConfigMap.values()) {
             ShardConfig shardConfig = moduleConfig.getShardConfig(shardName);
-            if(shardConfig != null) {
+            if (shardConfig != null) {
                 return shardConfig.getReplicas();
             }
         }
 
+        for (final PrefixShardConfiguration prefixConfig : prefixConfigMap.values()) {
+            if (shardName.equals(ClusterUtils.getCleanShardName(prefixConfig.getPrefix().getRootIdentifier()))) {
+                return prefixConfig.getShardMemberNames();
+            }
+        }
+
         return Collections.emptyList();
     }
 
@@ -121,9 +164,9 @@ public class ConfigurationImpl implements Configuration {
     }
 
     @Override
-    public Collection<String> getUniqueMemberNamesForAllShards() {
-        Set<String> allNames = new HashSet<>();
-        for(String shardName: getAllShardNames()) {
+    public Collection<MemberName> getUniqueMemberNamesForAllShards() {
+        Set<MemberName> allNames = new HashSet<>();
+        for (String shardName: getAllShardNames()) {
             allNames.addAll(getMembersFromShardName(shardName));
         }
 
@@ -134,21 +177,98 @@ public class ConfigurationImpl implements Configuration {
     public synchronized void addModuleShardConfiguration(ModuleShardConfiguration config) {
         Preconditions.checkNotNull(config, "ModuleShardConfiguration should not be null");
 
-        ModuleConfig moduleConfig = new ModuleConfig(config.getModuleName());
-        moduleConfig.setNameSpace(config.getNamespace().toASCIIString());
-        moduleConfig.setShardStrategy(createShardStrategy(config.getModuleName(), config.getShardStrategyName()));
-
-        moduleConfig.addShardConfig(config.getShardName(), ImmutableSet.copyOf(config.getShardMemberNames()));
+        ModuleConfig moduleConfig = ModuleConfig.builder(config.getModuleName())
+                .nameSpace(config.getNamespace().toASCIIString())
+                .shardStrategy(createShardStrategy(config.getModuleName(), config.getShardStrategyName()))
+                .shardConfig(config.getShardName(), config.getShardMemberNames()).build();
 
-        moduleConfigMap = ImmutableMap.<String, ModuleConfig>builder().putAll(moduleConfigMap).
-                put(config.getModuleName(), moduleConfig).build();
+        updateModuleConfigMap(moduleConfig);
 
-        namespaceToModuleName = ImmutableMap.<String, String>builder().putAll(namespaceToModuleName).
-                put(moduleConfig.getNameSpace(), moduleConfig.getName()).build();
+        namespaceToModuleName = ImmutableMap.<String, String>builder().putAll(namespaceToModuleName)
+                .put(moduleConfig.getNamespace(), moduleConfig.getName()).build();
         allShardNames = ImmutableSet.<String>builder().addAll(allShardNames).add(config.getShardName()).build();
     }
 
+    @Override
+    public void addPrefixShardConfiguration(@Nonnull final PrefixShardConfiguration config) {
+        Preconditions.checkNotNull(config, "PrefixShardConfiguration cannot be null");
+        updatePrefixConfigMap(config);
+        allShardNames = ImmutableSet.<String>builder().addAll(allShardNames)
+                .add(ClusterUtils.getCleanShardName(config.getPrefix().getRootIdentifier())).build();
+    }
+
+    private void updatePrefixConfigMap(final PrefixShardConfiguration config) {
+        final Map<YangInstanceIdentifier, PrefixShardConfiguration> newPrefixConfigMap = new HashMap<>(prefixConfigMap);
+        newPrefixConfigMap.put(config.getPrefix().getRootIdentifier(), config);
+        prefixConfigMap = ImmutableMap.copyOf(newPrefixConfigMap);
+    }
+
     private ShardStrategy createShardStrategy(String moduleName, String shardStrategyName) {
         return ShardStrategyFactory.newShardStrategyInstance(moduleName, shardStrategyName, this);
     }
+
+    @Override
+    public boolean isShardConfigured(String shardName) {
+        Preconditions.checkNotNull(shardName, "shardName should not be null");
+        return allShardNames.contains(shardName);
+    }
+
+    @Override
+    public void addMemberReplicaForShard(String shardName, MemberName newMemberName) {
+        Preconditions.checkNotNull(shardName, "shardName should not be null");
+        Preconditions.checkNotNull(newMemberName, "MemberName should not be null");
+
+        for (ModuleConfig moduleConfig: moduleConfigMap.values()) {
+            ShardConfig shardConfig = moduleConfig.getShardConfig(shardName);
+            if (shardConfig != null) {
+                Set<MemberName> replicas = new HashSet<>(shardConfig.getReplicas());
+                replicas.add(newMemberName);
+                updateModuleConfigMap(ModuleConfig.builder(moduleConfig).shardConfig(shardName, replicas).build());
+                return;
+            }
+        }
+    }
+
+    @Override
+    public void removeMemberReplicaForShard(String shardName, MemberName newMemberName) {
+        Preconditions.checkNotNull(shardName, "shardName should not be null");
+        Preconditions.checkNotNull(newMemberName, "MemberName should not be null");
+
+        for (ModuleConfig moduleConfig: moduleConfigMap.values()) {
+            ShardConfig shardConfig = moduleConfig.getShardConfig(shardName);
+            if (shardConfig != null) {
+                Set<MemberName> replicas = new HashSet<>(shardConfig.getReplicas());
+                replicas.remove(newMemberName);
+                updateModuleConfigMap(ModuleConfig.builder(moduleConfig).shardConfig(shardName, replicas).build());
+                return;
+            }
+        }
+    }
+
+    @Override
+    public ShardStrategy getStrategyForPrefix(@Nonnull final YangInstanceIdentifier prefix) {
+        Preconditions.checkNotNull(prefix, "Prefix cannot be null");
+        // FIXME using prefix tables like in mdsal will be better
+        Entry<YangInstanceIdentifier, PrefixShardConfiguration> bestMatchEntry =
+                new SimpleEntry<>(YangInstanceIdentifier.EMPTY, null);
+
+        for (Entry<YangInstanceIdentifier, PrefixShardConfiguration> entry : prefixConfigMap.entrySet()) {
+            if (entry.getKey().contains(prefix) && entry.getKey().getPathArguments().size()
+                    > bestMatchEntry.getKey().getPathArguments().size()) {
+                bestMatchEntry = entry;
+            }
+        }
+
+        if (bestMatchEntry.getValue() == null) {
+            return null;
+        }
+        return new PrefixShardStrategy(
+                ClusterUtils.getCleanShardName(bestMatchEntry.getValue().getPrefix().getRootIdentifier()), this);
+    }
+
+    private void updateModuleConfigMap(final ModuleConfig moduleConfig) {
+        final Map<String, ModuleConfig> newModuleConfigMap = new HashMap<>(moduleConfigMap);
+        newModuleConfigMap.put(moduleConfig.getName(), moduleConfig);
+        moduleConfigMap = ImmutableMap.copyOf(newModuleConfigMap);
+    }
 }