X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-distributed-datastore%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fdatastore%2FConfigurationImpl.java;h=1a0a5dd6591c9a653ae2079b938d6448e59c036f;hb=8de365b86ee7b65ee201166be85142b27ffd7295;hp=c8c82997dfc0c38bb551049d3a810f33c5cfb417;hpb=1e59825dbec7b354d76bd7efa6a61e4ad802c802;p=controller.git diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/ConfigurationImpl.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/ConfigurationImpl.java index c8c82997df..1a0a5dd659 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/ConfigurationImpl.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/ConfigurationImpl.java @@ -8,29 +8,85 @@ package org.opendaylight.controller.cluster.datastore; +import com.google.common.base.Optional; +import com.google.common.base.Preconditions; import com.typesafe.config.Config; import com.typesafe.config.ConfigFactory; import com.typesafe.config.ConfigObject; +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 java.io.File; import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.LinkedHashSet; import java.util.List; +import java.util.Map; +import java.util.Set; public class ConfigurationImpl implements Configuration { + private final List moduleShards = new ArrayList<>(); + private final List modules = new ArrayList<>(); + private static final Logger + LOG = LoggerFactory.getLogger(DistributedDataStore.class); + + // Look up maps to speed things up + + // key = memberName, value = list of shardNames + private Map> memberShardNames = new HashMap<>(); + + // key = shardName, value = list of replicaNames (replicaNames are the same as memberNames) + private Map> shardReplicaNames = new HashMap<>(); + public ConfigurationImpl(String moduleShardsConfigPath, + String modulesConfigPath){ - Config moduleShardsConfig = ConfigFactory.load(moduleShardsConfigPath); - Config modulesConfig = ConfigFactory.load(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); + } readModuleShards(moduleShardsConfig); readModules(modulesConfig); } - public List getMemberShardNames(String memberName){ + @Override public List getMemberShardNames(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){ @@ -42,10 +98,82 @@ public class ConfigurationImpl implements Configuration { } } } + + memberShardNames.put(memberName, shards); + return shards; } + @Override public Optional getModuleNameFromNameSpace(String nameSpace) { + + Preconditions.checkNotNull(nameSpace, "nameSpace should not be null"); + + for(Module m : modules){ + if(m.getNameSpace().equals(nameSpace)){ + return Optional.of(m.getName()); + } + } + return Optional.absent(); + } + + @Override public Map getModuleNameToShardStrategyMap() { + Map map = new HashMap<>(); + for(Module m : modules){ + map.put(m.getName(), m.getShardStrategy()); + } + return map; + } + + @Override public List getShardNamesFromModuleName(String moduleName) { + + Preconditions.checkNotNull(moduleName, "moduleName should not be null"); + + for(ModuleShard m : moduleShards){ + if(m.getModuleName().equals(moduleName)){ + List l = new ArrayList<>(); + for(Shard s : m.getShards()){ + l.add(s.getName()); + } + return l; + } + } + + return Collections.EMPTY_LIST; + } + + @Override public List getMembersFromShardName(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; + } + } + } + shardReplicaNames.put(shardName, Collections.EMPTY_LIST); + return Collections.EMPTY_LIST; + } + + @Override public Set getAllShardNames() { + Set shardNames = new LinkedHashSet<>(); + for(ModuleShard ms : moduleShards){ + for(Shard s : ms.getShards()) { + shardNames.add(s.getName()); + } + } + return shardNames; + } + + private void readModules(Config modulesConfig) { List modulesConfigObjectList = @@ -54,7 +182,7 @@ public class ConfigurationImpl implements Configuration { for(ConfigObject o : modulesConfigObjectList){ ConfigObjectWrapper w = new ConfigObjectWrapper(o); modules.add(new Module(w.stringValue("name"), w.stringValue( - "namespace"), w.stringValue("sharding-strategy"))); + "namespace"), w.stringValue("shard-strategy"))); } } @@ -82,7 +210,7 @@ public class ConfigurationImpl implements Configuration { } - public static class ModuleShard { + private class ModuleShard { private final String moduleName; private final List shards; @@ -100,7 +228,7 @@ public class ConfigurationImpl implements Configuration { } } - public static class Shard { + private class Shard { private final String name; private final List replicas; @@ -118,16 +246,20 @@ public class ConfigurationImpl implements Configuration { } } - public static class Module { + private class Module { private final String name; private final String nameSpace; - private final String shardingStrategy; + private final ShardStrategy shardStrategy; - Module(String name, String nameSpace, String shardingStrategy) { + Module(String name, String nameSpace, String shardStrategy) { this.name = name; this.nameSpace = nameSpace; - this.shardingStrategy = shardingStrategy; + if(ModuleShardStrategy.NAME.equals(shardStrategy)){ + this.shardStrategy = new ModuleShardStrategy(name, ConfigurationImpl.this); + } else { + this.shardStrategy = new DefaultShardStrategy(); + } } public String getName() { @@ -138,8 +270,8 @@ public class ConfigurationImpl implements Configuration { return nameSpace; } - public String getShardingStrategy() { - return shardingStrategy; + public ShardStrategy getShardStrategy() { + return shardStrategy; } }