2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.cluster.datastore.config;
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.ImmutableMap;
12 import com.google.common.collect.ImmutableSet;
13 import java.util.AbstractMap.SimpleEntry;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.HashMap;
18 import java.util.HashSet;
19 import java.util.List;
21 import java.util.Map.Entry;
23 import org.opendaylight.controller.cluster.access.concepts.MemberName;
24 import org.opendaylight.controller.cluster.datastore.shardstrategy.PrefixShardStrategy;
25 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategy;
26 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategyFactory;
27 import org.opendaylight.controller.cluster.datastore.utils.ClusterUtils;
28 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31 // TODO clean this up once we get rid of module based configuration, prefix one should be alot simpler
32 public class ConfigurationImpl implements Configuration {
33 private volatile Map<String, ModuleConfig> moduleConfigMap;
35 // TODO should this be initialized with something? on restart we should restore the shards from configuration?
36 private volatile Map<DOMDataTreeIdentifier, PrefixShardConfiguration> prefixConfigMap = Collections.emptyMap();
38 // Look up maps to speed things up
40 private volatile Map<String, String> namespaceToModuleName;
41 private volatile Set<String> allShardNames;
43 public ConfigurationImpl(final String moduleShardsConfigPath, final String modulesConfigPath) {
44 this(new FileModuleShardConfigProvider(moduleShardsConfigPath, modulesConfigPath));
47 public ConfigurationImpl(final ModuleShardConfigProvider provider) {
48 ImmutableMap.Builder<String, ModuleConfig> mapBuilder = ImmutableMap.builder();
49 for (Map.Entry<String, ModuleConfig.Builder> e: provider.retrieveModuleConfigs(this).entrySet()) {
50 mapBuilder.put(e.getKey(), e.getValue().build());
53 this.moduleConfigMap = mapBuilder.build();
55 this.allShardNames = createAllShardNames(moduleConfigMap.values());
56 this.namespaceToModuleName = createNamespaceToModuleName(moduleConfigMap.values());
59 private static Set<String> createAllShardNames(Iterable<ModuleConfig> moduleConfigs) {
60 final ImmutableSet.Builder<String> builder = ImmutableSet.builder();
61 for (ModuleConfig moduleConfig : moduleConfigs) {
62 builder.addAll(moduleConfig.getShardNames());
65 return builder.build();
68 private static Map<String, String> createNamespaceToModuleName(Iterable<ModuleConfig> moduleConfigs) {
69 final ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
70 for (ModuleConfig moduleConfig : moduleConfigs) {
71 if (moduleConfig.getNamespace() != null) {
72 builder.put(moduleConfig.getNamespace(), moduleConfig.getName());
76 return builder.build();
80 public Collection<String> getMemberShardNames(final MemberName memberName) {
81 Preconditions.checkNotNull(memberName, "memberName should not be null");
83 List<String> shards = new ArrayList<>();
84 for (ModuleConfig moduleConfig: moduleConfigMap.values()) {
85 for (ShardConfig shardConfig: moduleConfig.getShardConfigs()) {
86 if (shardConfig.getReplicas().contains(memberName)) {
87 shards.add(shardConfig.getName());
96 public String getModuleNameFromNameSpace(final String nameSpace) {
97 Preconditions.checkNotNull(nameSpace, "nameSpace should not be null");
99 return namespaceToModuleName.get(nameSpace);
103 public ShardStrategy getStrategyForModule(String moduleName) {
104 Preconditions.checkNotNull(moduleName, "moduleName should not be null");
106 ModuleConfig moduleConfig = moduleConfigMap.get(moduleName);
107 return moduleConfig != null ? moduleConfig.getShardStrategy() : null;
111 public String getShardNameForModule(final String moduleName) {
112 Preconditions.checkNotNull(moduleName, "moduleName should not be null");
114 ModuleConfig moduleConfig = moduleConfigMap.get(moduleName);
115 Collection<ShardConfig> shardConfigs = moduleConfig != null ? moduleConfig.getShardConfigs() :
116 Collections.<ShardConfig>emptySet();
117 return !shardConfigs.isEmpty() ? shardConfigs.iterator().next().getName() : null;
121 public String getShardNameForPrefix(final DOMDataTreeIdentifier prefix) {
122 Preconditions.checkNotNull(prefix, "prefix should not be null");
124 Entry<DOMDataTreeIdentifier, PrefixShardConfiguration> bestMatchEntry =
126 new DOMDataTreeIdentifier(prefix.getDatastoreType(), YangInstanceIdentifier.EMPTY), null);
128 for (Entry<DOMDataTreeIdentifier, PrefixShardConfiguration> entry : prefixConfigMap.entrySet()) {
129 if (entry.getKey().contains(prefix) && entry.getKey().getRootIdentifier().getPathArguments().size()
130 > bestMatchEntry.getKey().getRootIdentifier().getPathArguments().size()) {
131 bestMatchEntry = entry;
135 //TODO we really should have mapping based on prefix instead of Strings
136 return ClusterUtils.getCleanShardName(bestMatchEntry.getKey().getRootIdentifier());
140 public Collection<MemberName> getMembersFromShardName(final String shardName) {
141 checkNotNullShardName(shardName);
143 for (ModuleConfig moduleConfig: moduleConfigMap.values()) {
144 ShardConfig shardConfig = moduleConfig.getShardConfig(shardName);
145 if (shardConfig != null) {
146 return shardConfig.getReplicas();
150 for (final PrefixShardConfiguration prefixConfig : prefixConfigMap.values()) {
151 if (shardName.equals(ClusterUtils.getCleanShardName(prefixConfig.getPrefix().getRootIdentifier()))) {
152 return prefixConfig.getShardMemberNames();
156 return Collections.emptyList();
159 private static void checkNotNullShardName(final String shardName) {
160 Preconditions.checkNotNull(shardName, "shardName should not be null");
164 public Set<String> getAllShardNames() {
165 return allShardNames;
169 public Collection<MemberName> getUniqueMemberNamesForAllShards() {
170 Set<MemberName> allNames = new HashSet<>();
171 for (String shardName: getAllShardNames()) {
172 allNames.addAll(getMembersFromShardName(shardName));
179 public synchronized void addModuleShardConfiguration(ModuleShardConfiguration config) {
180 Preconditions.checkNotNull(config, "ModuleShardConfiguration should not be null");
182 ModuleConfig moduleConfig = ModuleConfig.builder(config.getModuleName())
183 .nameSpace(config.getNamespace().toASCIIString())
184 .shardStrategy(createShardStrategy(config.getModuleName(), config.getShardStrategyName()))
185 .shardConfig(config.getShardName(), config.getShardMemberNames()).build();
187 updateModuleConfigMap(moduleConfig);
189 namespaceToModuleName = ImmutableMap.<String, String>builder().putAll(namespaceToModuleName)
190 .put(moduleConfig.getNamespace(), moduleConfig.getName()).build();
191 allShardNames = ImmutableSet.<String>builder().addAll(allShardNames).add(config.getShardName()).build();
195 public void addPrefixShardConfiguration(final PrefixShardConfiguration config) {
196 Preconditions.checkNotNull(config, "PrefixShardConfiguration cannot be null");
197 addPrefixConfig(config);
198 allShardNames = ImmutableSet.<String>builder().addAll(allShardNames)
199 .add(ClusterUtils.getCleanShardName(config.getPrefix().getRootIdentifier())).build();
203 public void removePrefixShardConfiguration(final DOMDataTreeIdentifier prefix) {
204 Preconditions.checkNotNull(prefix, "Prefix cannot be null");
206 removePrefixConfig(prefix);
208 final HashSet<String> temp = new HashSet<>(allShardNames);
209 temp.remove(ClusterUtils.getCleanShardName(prefix.getRootIdentifier()));
211 allShardNames = ImmutableSet.copyOf(temp);
215 public Map<DOMDataTreeIdentifier, PrefixShardConfiguration> getAllPrefixShardConfigurations() {
216 return ImmutableMap.copyOf(prefixConfigMap);
219 private void addPrefixConfig(final PrefixShardConfiguration config) {
220 final Map<DOMDataTreeIdentifier, PrefixShardConfiguration> newPrefixConfigMap = new HashMap<>(prefixConfigMap);
221 newPrefixConfigMap.put(config.getPrefix(), config);
222 prefixConfigMap = ImmutableMap.copyOf(newPrefixConfigMap);
225 private void removePrefixConfig(final DOMDataTreeIdentifier prefix) {
226 final Map<DOMDataTreeIdentifier, PrefixShardConfiguration> newPrefixConfigMap = new HashMap<>(prefixConfigMap);
227 newPrefixConfigMap.remove(prefix);
228 prefixConfigMap = ImmutableMap.copyOf(newPrefixConfigMap);
231 private ShardStrategy createShardStrategy(String moduleName, String shardStrategyName) {
232 return ShardStrategyFactory.newShardStrategyInstance(moduleName, shardStrategyName, this);
236 public boolean isShardConfigured(String shardName) {
237 checkNotNullShardName(shardName);
238 return allShardNames.contains(shardName);
242 public void addMemberReplicaForShard(String shardName, MemberName newMemberName) {
243 checkNotNullShardName(shardName);
244 Preconditions.checkNotNull(newMemberName, "MemberName should not be null");
246 for (ModuleConfig moduleConfig: moduleConfigMap.values()) {
247 ShardConfig shardConfig = moduleConfig.getShardConfig(shardName);
248 if (shardConfig != null) {
249 Set<MemberName> replicas = new HashSet<>(shardConfig.getReplicas());
250 replicas.add(newMemberName);
251 updateModuleConfigMap(ModuleConfig.builder(moduleConfig).shardConfig(shardName, replicas).build());
258 public void removeMemberReplicaForShard(String shardName, MemberName newMemberName) {
259 checkNotNullShardName(shardName);
260 Preconditions.checkNotNull(newMemberName, "MemberName should not be null");
262 for (ModuleConfig moduleConfig: moduleConfigMap.values()) {
263 ShardConfig shardConfig = moduleConfig.getShardConfig(shardName);
264 if (shardConfig != null) {
265 Set<MemberName> replicas = new HashSet<>(shardConfig.getReplicas());
266 replicas.remove(newMemberName);
267 updateModuleConfigMap(ModuleConfig.builder(moduleConfig).shardConfig(shardName, replicas).build());
274 public ShardStrategy getStrategyForPrefix(final DOMDataTreeIdentifier prefix) {
275 Preconditions.checkNotNull(prefix, "Prefix cannot be null");
276 // FIXME using prefix tables like in mdsal will be better
277 Entry<DOMDataTreeIdentifier, PrefixShardConfiguration> bestMatchEntry =
279 new DOMDataTreeIdentifier(prefix.getDatastoreType(), YangInstanceIdentifier.EMPTY), null);
281 for (Entry<DOMDataTreeIdentifier, PrefixShardConfiguration> entry : prefixConfigMap.entrySet()) {
282 if (entry.getKey().contains(prefix) && entry.getKey().getRootIdentifier().getPathArguments().size()
283 > bestMatchEntry.getKey().getRootIdentifier().getPathArguments().size()) {
284 bestMatchEntry = entry;
288 if (bestMatchEntry.getValue() == null) {
291 return new PrefixShardStrategy(ClusterUtils
292 .getCleanShardName(bestMatchEntry.getKey().getRootIdentifier()),
293 bestMatchEntry.getKey().getRootIdentifier());
296 private void updateModuleConfigMap(final ModuleConfig moduleConfig) {
297 final Map<String, ModuleConfig> newModuleConfigMap = new HashMap<>(moduleConfigMap);
298 newModuleConfigMap.put(moduleConfig.getName(), moduleConfig);
299 moduleConfigMap = ImmutableMap.copyOf(newModuleConfigMap);