X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-distributed-datastore%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fdatastore%2Fconfig%2FConfigurationImpl.java;h=d553c66dbe69d072b93617b38937d8e213395e61;hp=e705a142f2cead1f0672fd66611d716a96a1f803;hb=b34452ce75563e360ae1d02a9f2aa6223d6208c3;hpb=ffc46de334c8a903844b9f4aff73dc68b2401659 diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/config/ConfigurationImpl.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/config/ConfigurationImpl.java index e705a142f2..d553c66dbe 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/config/ConfigurationImpl.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/config/ConfigurationImpl.java @@ -8,191 +8,110 @@ 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.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.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 org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategy; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategyFactory; public class ConfigurationImpl implements Configuration { - - private final List moduleShards; - - private final List modules; - - private static final Logger - LOG = LoggerFactory.getLogger(DistributedDataStore.class); + private volatile Map moduleConfigMap; // Look up maps to speed things up - // key = memberName, value = list of shardNames - private final Map> memberShardNames = new HashMap<>(); - - // key = shardName, value = list of replicaNames (replicaNames are the same as memberNames) - private final Map> shardReplicaNames = new HashMap<>(); - - private final ListMultimap moduleNameToShardName; - private final Map moduleNameToStrategy; - private final Map namespaceToModuleName; - private final Set allShardNames; - - public ConfigurationImpl(final String moduleShardsConfigPath, + private volatile Map namespaceToModuleName; + private volatile Set allShardNames; - final String modulesConfigPath){ - - 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); - } - - 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); - } - - this.moduleShards = readModuleShards(moduleShardsConfig); - this.modules = readModules(modulesConfig); - - this.allShardNames = createAllShardNames(moduleShards); - this.moduleNameToShardName = createModuleNameToShardName(moduleShards); - this.moduleNameToStrategy = createModuleNameToStrategy(modules); - this.namespaceToModuleName = createNamespaceToModuleName(modules); + public ConfigurationImpl(final String moduleShardsConfigPath, final String modulesConfigPath) { + this(new FileModuleShardConfigProvider(moduleShardsConfigPath, modulesConfigPath)); } - private static Set createAllShardNames(Iterable moduleShards) { - final com.google.common.collect.ImmutableSet.Builder b = ImmutableSet.builder(); - for(ModuleShard ms : moduleShards){ - for(Shard s : ms.getShards()) { - b.add(s.getName()); - } - } - return b.build(); - } + public ConfigurationImpl(final ModuleShardConfigProvider provider) { + this.moduleConfigMap = ImmutableMap.copyOf(provider.retrieveModuleConfigs(this)); - private static Map createModuleNameToStrategy(Iterable modules) { - final com.google.common.collect.ImmutableMap.Builder b = ImmutableMap.builder(); - for (Module m : modules) { - b.put(m.getName(), m.getShardStrategy()); - } - return b.build(); + this.allShardNames = createAllShardNames(moduleConfigMap.values()); + this.namespaceToModuleName = createNamespaceToModuleName(moduleConfigMap.values()); } - private static Map createNamespaceToModuleName(Iterable modules) { - final com.google.common.collect.ImmutableMap.Builder b = ImmutableMap.builder(); - for (Module m : modules) { - b.put(m.getNameSpace(), m.getName()); + private static Set createAllShardNames(Iterable moduleConfigs) { + final ImmutableSet.Builder builder = ImmutableSet.builder(); + for(ModuleConfig moduleConfig : moduleConfigs) { + builder.addAll(moduleConfig.getShardNames()); } - return b.build(); - } - private static ListMultimap createModuleNameToShardName(Iterable moduleShards) { - final com.google.common.collect.ImmutableListMultimap.Builder b = ImmutableListMultimap.builder(); + return builder.build(); + } - for (ModuleShard m : moduleShards) { - for (Shard s : m.getShards()) { - b.put(m.getModuleName(), s.getName()); + private static Map createNamespaceToModuleName(Iterable moduleConfigs) { + final ImmutableMap.Builder 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 getMemberShardNames(final String memberName){ - + @Override + public Collection getMemberShardNames(final String memberName){ Preconditions.checkNotNull(memberName, "memberName should not be null"); - if(memberShardNames.containsKey(memberName)){ - return memberShardNames.get(memberName); - } - List 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 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 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 getShardNamesFromModuleName(final String moduleName) { + public String getShardNameForModule(final String moduleName) { Preconditions.checkNotNull(moduleName, "moduleName should not be null"); - return moduleNameToShardName.get(moduleName); - } - @Override public List getMembersFromShardName(final String shardName) { + ModuleConfig moduleConfig = moduleConfigMap.get(moduleName); + Collection shardConfigs = moduleConfig != null ? moduleConfig.getShardConfigs() : + Collections.emptySet(); + return !shardConfigs.isEmpty() ? shardConfigs.iterator().next().getName(): null; + } + @Override + public Collection getMembersFromShardName(final String shardName) { Preconditions.checkNotNull(shardName, "shardName should not be null"); - if(shardReplicaNames.containsKey(shardName)){ - return shardReplicaNames.get(shardName); - } - - for(ModuleShard ms : moduleShards){ - for(Shard s : ms.getShards()) { - if(s.getName().equals(shardName)){ - List replicas = s.getReplicas(); - shardReplicaNames.put(shardName, replicas); - return replicas; - } + for(ModuleConfig moduleConfig: moduleConfigMap.values()) { + ShardConfig shardConfig = moduleConfig.getShardConfig(shardName); + if(shardConfig != null) { + return shardConfig.getReplicas(); } } - shardReplicaNames.put(shardName, Collections.emptyList()); + return Collections.emptyList(); } @@ -204,129 +123,38 @@ public class ConfigurationImpl implements Configuration { @Override public Collection getUniqueMemberNamesForAllShards() { Set allNames = new HashSet<>(); - for(String shardName: allShardNames) { + for(String shardName: getAllShardNames()) { allNames.addAll(getMembersFromShardName(shardName)); } return allNames; } - private List readModules(final Config modulesConfig) { - List modulesConfigObjectList = - modulesConfig.getObjectList("modules"); - - final Builder 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 readModuleShards(final Config moduleShardsConfig) { - List moduleShardsConfigObjectList = - moduleShardsConfig.getObjectList("module-shards"); - - final Builder b = ImmutableList.builder(); - for(ConfigObject moduleShardConfigObject : moduleShardsConfigObjectList){ - - String moduleName = moduleShardConfigObject.get("name").unwrapped().toString(); - - List shardsConfigObjectList = - moduleShardConfigObject.toConfig().getObjectList("shards"); - - List shards = new ArrayList<>(); - - for(ConfigObject shard : shardsConfigObjectList){ - String shardName = shard.get("name").unwrapped().toString(); - List replicas = shard.toConfig().getStringList("replicas"); - shards.add(new Shard(shardName, replicas)); - } - - b.add(new ModuleShard(moduleName, shards)); - } - - return b.build(); - } + @Override + public synchronized void addModuleShardConfiguration(ModuleShardConfiguration config) { + Preconditions.checkNotNull(config, "ModuleShardConfiguration should not be null"); - private static class ModuleShard { - private final String moduleName; - private final List shards; + ModuleConfig moduleConfig = new ModuleConfig(config.getModuleName()); + moduleConfig.setNameSpace(config.getNamespace().toASCIIString()); + moduleConfig.setShardStrategy(createShardStrategy(config.getModuleName(), config.getShardStrategyName())); - public ModuleShard(final String moduleName, final List shards) { - this.moduleName = moduleName; - this.shards = shards; - } + moduleConfig.addShardConfig(config.getShardName(), ImmutableSet.copyOf(config.getShardMemberNames())); - public String getModuleName() { - return moduleName; - } + moduleConfigMap = ImmutableMap.builder().putAll(moduleConfigMap). + put(config.getModuleName(), moduleConfig).build(); - public List getShards() { - return shards; - } + namespaceToModuleName = ImmutableMap.builder().putAll(namespaceToModuleName). + put(moduleConfig.getNameSpace(), moduleConfig.getName()).build(); + allShardNames = ImmutableSet.builder().addAll(allShardNames).add(config.getShardName()).build(); } - private static class Shard { - private final String name; - private final List replicas; - - Shard(final String name, final List replicas) { - this.name = name; - this.replicas = replicas; - } - - public String getName() { - return name; - } - - public List getReplicas() { - return replicas; - } + private ShardStrategy createShardStrategy(String moduleName, String shardStrategyName) { + return ShardStrategyFactory.newShardStrategyInstance(moduleName, shardStrategyName, this); } - private class Module { - - private final String name; - private final String nameSpace; - private final ShardStrategy shardStrategy; - - 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(); - } - } - - public String getName() { - return name; - } - - public String getNameSpace() { - return nameSpace; - } - - public ShardStrategy getShardStrategy() { - return shardStrategy; - } - } - - - 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(); - } + @Override + public boolean isShardConfigured(String shardName) { + Preconditions.checkNotNull(shardName, "shardName should not be null"); + return allShardNames.contains(shardName); } }