package org.opendaylight.controller.cluster.datastore.config;
-import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
-import com.google.common.collect.ImmutableList;
-import com.google.common.collect.ImmutableList.Builder;
-import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
-import com.google.common.collect.ListMultimap;
-import com.typesafe.config.Config;
-import com.typesafe.config.ConfigFactory;
-import com.typesafe.config.ConfigObject;
-import java.io.File;
+import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
+import java.util.Map.Entry;
import java.util.Set;
-import org.opendaylight.controller.cluster.datastore.DistributedDataStore;
-import org.opendaylight.controller.cluster.datastore.shardstrategy.DefaultShardStrategy;
-import org.opendaylight.controller.cluster.datastore.shardstrategy.ModuleShardStrategy;
+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.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategyFactory;
+import org.opendaylight.controller.cluster.datastore.utils.ClusterUtils;
+import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
+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;
- private final List<ModuleShard> moduleShards;
-
- private final List<Module> modules;
-
- private static final Logger
- LOG = LoggerFactory.getLogger(DistributedDataStore.class);
+ // TODO should this be initialized with something? on restart we should restore the shards from configuration?
+ private volatile Map<DOMDataTreeIdentifier, PrefixShardConfiguration> prefixConfigMap = Collections.emptyMap();
// Look up maps to speed things up
- // key = memberName, value = list of shardNames
- private final Map<String, List<String>> memberShardNames = new HashMap<>();
-
- // key = shardName, value = list of replicaNames (replicaNames are the same as memberNames)
- private final Map<String, List<String>> shardReplicaNames = new HashMap<>();
-
- private final ListMultimap<String, String> moduleNameToShardName;
- private final Map<String, ShardStrategy> moduleNameToStrategy;
- private final Map<String, String> namespaceToModuleName;
- private final Set<String> allShardNames;
-
- public ConfigurationImpl(final String moduleShardsConfigPath,
-
- final String modulesConfigPath){
+ private volatile Map<String, String> namespaceToModuleName;
+ private volatile Set<String> allShardNames;
- Preconditions.checkNotNull(moduleShardsConfigPath, "moduleShardsConfigPath should not be null");
- Preconditions.checkNotNull(modulesConfigPath, "modulesConfigPath should not be null");
-
-
- File moduleShardsFile = new File("./configuration/initial/" + moduleShardsConfigPath);
- File modulesFile = new File("./configuration/initial/" + modulesConfigPath);
-
- Config moduleShardsConfig = null;
- if(moduleShardsFile.exists()) {
- LOG.info("module shards config file exists - reading config from it");
- moduleShardsConfig = ConfigFactory.parseFile(moduleShardsFile);
- } else {
- LOG.warn("module shards configuration read from resource");
- moduleShardsConfig = ConfigFactory.load(moduleShardsConfigPath);
- }
+ public ConfigurationImpl(final String moduleShardsConfigPath, final String modulesConfigPath) {
+ this(new FileModuleShardConfigProvider(moduleShardsConfigPath, modulesConfigPath));
+ }
- Config modulesConfig = null;
- if(modulesFile.exists()) {
- LOG.info("modules config file exists - reading config from it");
- modulesConfig = ConfigFactory.parseFile(modulesFile);
- } else {
- LOG.warn("modules configuration read from resource");
- modulesConfig = ConfigFactory.load(modulesConfigPath);
+ public ConfigurationImpl(final ModuleShardConfigProvider provider) {
+ 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.moduleShards = readModuleShards(moduleShardsConfig);
- this.modules = readModules(modulesConfig);
+ this.moduleConfigMap = mapBuilder.build();
- this.allShardNames = createAllShardNames(moduleShards);
- this.moduleNameToShardName = createModuleNameToShardName(moduleShards);
- this.moduleNameToStrategy = createModuleNameToStrategy(modules);
- this.namespaceToModuleName = createNamespaceToModuleName(modules);
+ this.allShardNames = createAllShardNames(moduleConfigMap.values());
+ this.namespaceToModuleName = createNamespaceToModuleName(moduleConfigMap.values());
}
- private static Set<String> createAllShardNames(Iterable<ModuleShard> moduleShards) {
- final com.google.common.collect.ImmutableSet.Builder<String> b = ImmutableSet.builder();
- for(ModuleShard ms : moduleShards){
- for(Shard s : ms.getShards()) {
- b.add(s.getName());
- }
+ private static Set<String> createAllShardNames(Iterable<ModuleConfig> moduleConfigs) {
+ final ImmutableSet.Builder<String> builder = ImmutableSet.builder();
+ for (ModuleConfig moduleConfig : moduleConfigs) {
+ builder.addAll(moduleConfig.getShardNames());
}
- return b.build();
- }
-
- private static Map<String, ShardStrategy> createModuleNameToStrategy(Iterable<Module> modules) {
- final com.google.common.collect.ImmutableMap.Builder<String, ShardStrategy> b = ImmutableMap.builder();
- for (Module m : modules) {
- b.put(m.getName(), m.getShardStrategy());
- }
- return b.build();
- }
- private static Map<String, String> createNamespaceToModuleName(Iterable<Module> modules) {
- final com.google.common.collect.ImmutableMap.Builder<String, String> b = ImmutableMap.builder();
- for (Module m : modules) {
- b.put(m.getNameSpace(), m.getName());
- }
- return b.build();
+ return builder.build();
}
- private static ListMultimap<String, String> createModuleNameToShardName(Iterable<ModuleShard> moduleShards) {
- final com.google.common.collect.ImmutableListMultimap.Builder<String, String> b = ImmutableListMultimap.builder();
-
- for (ModuleShard m : moduleShards) {
- for (Shard s : m.getShards()) {
- b.put(m.getModuleName(), s.getName());
+ 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());
}
}
- return b.build();
+ return builder.build();
}
- @Override public List<String> getMemberShardNames(final String memberName){
-
+ @Override
+ public Collection<String> getMemberShardNames(final MemberName memberName) {
Preconditions.checkNotNull(memberName, "memberName should not be null");
- if(memberShardNames.containsKey(memberName)){
- return memberShardNames.get(memberName);
- }
-
List<String> shards = new ArrayList<>();
- for(ModuleShard ms : moduleShards){
- for(Shard s : ms.getShards()){
- for(String m : s.getReplicas()){
- if(memberName.equals(m)){
- shards.add(s.getName());
- }
+ for (ModuleConfig moduleConfig: moduleConfigMap.values()) {
+ for (ShardConfig shardConfig: moduleConfig.getShardConfigs()) {
+ if (shardConfig.getReplicas().contains(memberName)) {
+ shards.add(shardConfig.getName());
}
}
}
- memberShardNames.put(memberName, shards);
-
return shards;
-
}
@Override
- public Optional<String> getModuleNameFromNameSpace(final String nameSpace) {
+ public String getModuleNameFromNameSpace(final String nameSpace) {
Preconditions.checkNotNull(nameSpace, "nameSpace should not be null");
- return Optional.fromNullable(namespaceToModuleName.get(nameSpace));
+
+ return namespaceToModuleName.get(nameSpace);
}
@Override
- public Map<String, ShardStrategy> getModuleNameToShardStrategyMap() {
- return moduleNameToStrategy;
+ public ShardStrategy getStrategyForModule(String moduleName) {
+ Preconditions.checkNotNull(moduleName, "moduleName should not be null");
+
+ ModuleConfig moduleConfig = moduleConfigMap.get(moduleName);
+ return moduleConfig != null ? moduleConfig.getShardStrategy() : null;
}
@Override
- public List<String> getShardNamesFromModuleName(final String moduleName) {
+ public String getShardNameForModule(final String moduleName) {
Preconditions.checkNotNull(moduleName, "moduleName should not be null");
- return moduleNameToShardName.get(moduleName);
+
+ ModuleConfig moduleConfig = moduleConfigMap.get(moduleName);
+ Collection<ShardConfig> shardConfigs = moduleConfig != null ? moduleConfig.getShardConfigs() :
+ Collections.<ShardConfig>emptySet();
+ return !shardConfigs.isEmpty() ? shardConfigs.iterator().next().getName() : null;
}
- @Override public List<String> getMembersFromShardName(final String shardName) {
+ @Nullable
+ @Override
+ public String getShardNameForPrefix(@Nonnull final DOMDataTreeIdentifier prefix) {
+ Preconditions.checkNotNull(prefix, "prefix should not be null");
+
+ Entry<DOMDataTreeIdentifier, PrefixShardConfiguration> bestMatchEntry =
+ new SimpleEntry<>(
+ new DOMDataTreeIdentifier(prefix.getDatastoreType(), YangInstanceIdentifier.EMPTY), null);
+
+ for (Entry<DOMDataTreeIdentifier, PrefixShardConfiguration> entry : prefixConfigMap.entrySet()) {
+ if (entry.getKey().contains(prefix) && entry.getKey().getRootIdentifier().getPathArguments().size()
+ > bestMatchEntry.getKey().getRootIdentifier().getPathArguments().size()) {
+ bestMatchEntry = entry;
+ }
+ }
+ //TODO we really should have mapping based on prefix instead of Strings
+ return ClusterUtils.getCleanShardName(bestMatchEntry.getKey().getRootIdentifier());
+ }
+
+ @Override
+ public Collection<MemberName> getMembersFromShardName(final String shardName) {
Preconditions.checkNotNull(shardName, "shardName should not be null");
- if(shardReplicaNames.containsKey(shardName)){
- return shardReplicaNames.get(shardName);
+ for (ModuleConfig moduleConfig: moduleConfigMap.values()) {
+ ShardConfig shardConfig = moduleConfig.getShardConfig(shardName);
+ if (shardConfig != null) {
+ return shardConfig.getReplicas();
+ }
}
- for(ModuleShard ms : moduleShards){
- for(Shard s : ms.getShards()) {
- if(s.getName().equals(shardName)){
- List<String> replicas = s.getReplicas();
- shardReplicaNames.put(shardName, replicas);
- return replicas;
- }
+ for (final PrefixShardConfiguration prefixConfig : prefixConfigMap.values()) {
+ if (shardName.equals(ClusterUtils.getCleanShardName(prefixConfig.getPrefix().getRootIdentifier()))) {
+ return prefixConfig.getShardMemberNames();
}
}
- shardReplicaNames.put(shardName, Collections.<String>emptyList());
+
return Collections.emptyList();
}
}
@Override
- public Collection<String> getUniqueMemberNamesForAllShards() {
- Set<String> allNames = new HashSet<>();
- for(String shardName: allShardNames) {
+ public Collection<MemberName> getUniqueMemberNamesForAllShards() {
+ Set<MemberName> allNames = new HashSet<>();
+ for (String shardName: getAllShardNames()) {
allNames.addAll(getMembersFromShardName(shardName));
}
return allNames;
}
- private List<Module> readModules(final Config modulesConfig) {
- List<? extends ConfigObject> modulesConfigObjectList =
- modulesConfig.getObjectList("modules");
-
- final Builder<Module> b = ImmutableList.builder();
- for(ConfigObject o : modulesConfigObjectList){
- ConfigObjectWrapper w = new ConfigObjectWrapper(o);
- b.add(new Module(w.stringValue("name"), w.stringValue(
- "namespace"), w.stringValue("shard-strategy")));
- }
-
- return b.build();
- }
-
- private static List<ModuleShard> readModuleShards(final Config moduleShardsConfig) {
- List<? extends ConfigObject> moduleShardsConfigObjectList =
- moduleShardsConfig.getObjectList("module-shards");
-
- final Builder<ModuleShard> b = ImmutableList.builder();
- for(ConfigObject moduleShardConfigObject : moduleShardsConfigObjectList){
-
- String moduleName = moduleShardConfigObject.get("name").unwrapped().toString();
-
- List<? extends ConfigObject> shardsConfigObjectList =
- moduleShardConfigObject.toConfig().getObjectList("shards");
+ @Override
+ public synchronized void addModuleShardConfiguration(ModuleShardConfiguration config) {
+ Preconditions.checkNotNull(config, "ModuleShardConfiguration should not be null");
- List<Shard> shards = new ArrayList<>();
+ ModuleConfig moduleConfig = ModuleConfig.builder(config.getModuleName())
+ .nameSpace(config.getNamespace().toASCIIString())
+ .shardStrategy(createShardStrategy(config.getModuleName(), config.getShardStrategyName()))
+ .shardConfig(config.getShardName(), config.getShardMemberNames()).build();
- for(ConfigObject shard : shardsConfigObjectList){
- String shardName = shard.get("name").unwrapped().toString();
- List<String> replicas = shard.toConfig().getStringList("replicas");
- shards.add(new Shard(shardName, replicas));
- }
+ updateModuleConfigMap(moduleConfig);
- b.add(new ModuleShard(moduleName, shards));
- }
+ namespaceToModuleName = ImmutableMap.<String, String>builder().putAll(namespaceToModuleName)
+ .put(moduleConfig.getNamespace(), moduleConfig.getName()).build();
+ allShardNames = ImmutableSet.<String>builder().addAll(allShardNames).add(config.getShardName()).build();
+ }
- return b.build();
+ @Override
+ public void addPrefixShardConfiguration(@Nonnull final PrefixShardConfiguration config) {
+ Preconditions.checkNotNull(config, "PrefixShardConfiguration cannot be null");
+ addPrefixConfig(config);
+ allShardNames = ImmutableSet.<String>builder().addAll(allShardNames)
+ .add(ClusterUtils.getCleanShardName(config.getPrefix().getRootIdentifier())).build();
}
- private static class ModuleShard {
- private final String moduleName;
- private final List<Shard> shards;
+ @Override
+ public void removePrefixShardConfiguration(@Nonnull final DOMDataTreeIdentifier prefix) {
+ Preconditions.checkNotNull(prefix, "Prefix cannot be null");
- public ModuleShard(final String moduleName, final List<Shard> shards) {
- this.moduleName = moduleName;
- this.shards = shards;
- }
+ removePrefixConfig(prefix);
- public String getModuleName() {
- return moduleName;
- }
+ final HashSet<String> temp = new HashSet<>(allShardNames);
+ temp.remove(ClusterUtils.getCleanShardName(prefix.getRootIdentifier()));
- public List<Shard> getShards() {
- return shards;
- }
+ allShardNames = ImmutableSet.copyOf(temp);
}
- private static class Shard {
- private final String name;
- private final List<String> replicas;
-
- Shard(final String name, final List<String> replicas) {
- this.name = name;
- this.replicas = replicas;
- }
+ @Override
+ public Map<DOMDataTreeIdentifier, PrefixShardConfiguration> getAllPrefixShardConfigurations() {
+ return ImmutableMap.copyOf(prefixConfigMap);
+ }
- public String getName() {
- return name;
- }
+ private void addPrefixConfig(final PrefixShardConfiguration config) {
+ final Map<DOMDataTreeIdentifier, PrefixShardConfiguration> newPrefixConfigMap = new HashMap<>(prefixConfigMap);
+ newPrefixConfigMap.put(config.getPrefix(), config);
+ prefixConfigMap = ImmutableMap.copyOf(newPrefixConfigMap);
+ }
- public List<String> getReplicas() {
- return replicas;
- }
+ private void removePrefixConfig(final DOMDataTreeIdentifier prefix) {
+ final Map<DOMDataTreeIdentifier, PrefixShardConfiguration> newPrefixConfigMap = new HashMap<>(prefixConfigMap);
+ newPrefixConfigMap.remove(prefix);
+ prefixConfigMap = ImmutableMap.copyOf(newPrefixConfigMap);
}
- private class Module {
+ private ShardStrategy createShardStrategy(String moduleName, String shardStrategyName) {
+ return ShardStrategyFactory.newShardStrategyInstance(moduleName, shardStrategyName, this);
+ }
- private final String name;
- private final String nameSpace;
- private final ShardStrategy shardStrategy;
+ @Override
+ public boolean isShardConfigured(String shardName) {
+ Preconditions.checkNotNull(shardName, "shardName should not be null");
+ return allShardNames.contains(shardName);
+ }
- Module(final String name, final String nameSpace, final String shardStrategy) {
- this.name = name;
- this.nameSpace = nameSpace;
- if(ModuleShardStrategy.NAME.equals(shardStrategy)){
- this.shardStrategy = new ModuleShardStrategy(name, ConfigurationImpl.this);
- } else {
- this.shardStrategy = DefaultShardStrategy.getInstance();
+ @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;
}
}
+ }
- public String getName() {
- return name;
+ @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;
+ }
}
+ }
- public String getNameSpace() {
- return nameSpace;
+ @Override
+ public ShardStrategy getStrategyForPrefix(@Nonnull final DOMDataTreeIdentifier prefix) {
+ Preconditions.checkNotNull(prefix, "Prefix cannot be null");
+ // FIXME using prefix tables like in mdsal will be better
+ Entry<DOMDataTreeIdentifier, PrefixShardConfiguration> bestMatchEntry =
+ new SimpleEntry<>(
+ new DOMDataTreeIdentifier(prefix.getDatastoreType(), YangInstanceIdentifier.EMPTY), null);
+
+ for (Entry<DOMDataTreeIdentifier, PrefixShardConfiguration> entry : prefixConfigMap.entrySet()) {
+ if (entry.getKey().contains(prefix) && entry.getKey().getRootIdentifier().getPathArguments().size()
+ > bestMatchEntry.getKey().getRootIdentifier().getPathArguments().size()) {
+ bestMatchEntry = entry;
+ }
}
- public ShardStrategy getShardStrategy() {
- return shardStrategy;
+ if (bestMatchEntry.getValue() == null) {
+ return null;
}
+ return new PrefixShardStrategy(ClusterUtils
+ .getCleanShardName(bestMatchEntry.getKey().getRootIdentifier()),
+ bestMatchEntry.getKey().getRootIdentifier());
}
-
- private static class ConfigObjectWrapper{
-
- private final ConfigObject configObject;
-
- ConfigObjectWrapper(final ConfigObject configObject){
- this.configObject = configObject;
- }
-
- public String stringValue(final String name){
- return configObject.get(name).unwrapped().toString();
- }
+ private void updateModuleConfigMap(final ModuleConfig moduleConfig) {
+ final Map<String, ModuleConfig> newModuleConfigMap = new HashMap<>(moduleConfigMap);
+ newModuleConfigMap.put(moduleConfig.getName(), moduleConfig);
+ moduleConfigMap = ImmutableMap.copyOf(newModuleConfigMap);
}
}