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 static java.util.Objects.requireNonNull;
12 import com.google.common.collect.ImmutableMap;
13 import com.google.common.collect.ImmutableSet;
14 import java.util.AbstractMap.SimpleEntry;
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.Collections;
18 import java.util.HashMap;
19 import java.util.HashSet;
20 import java.util.List;
22 import java.util.Map.Entry;
24 import org.opendaylight.controller.cluster.access.concepts.MemberName;
25 import org.opendaylight.controller.cluster.datastore.shardstrategy.PrefixShardStrategy;
26 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategy;
27 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategyFactory;
28 import org.opendaylight.controller.cluster.datastore.utils.ClusterUtils;
29 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
32 // TODO clean this up once we get rid of module based configuration, prefix one should be alot simpler
33 public class ConfigurationImpl implements Configuration {
34 private volatile Map<String, ModuleConfig> moduleConfigMap;
36 // TODO should this be initialized with something? on restart we should restore the shards from configuration?
37 private volatile Map<DOMDataTreeIdentifier, PrefixShardConfiguration> prefixConfigMap = Collections.emptyMap();
39 // Look up maps to speed things up
41 private volatile Map<String, String> namespaceToModuleName;
42 private volatile Set<String> allShardNames;
44 public ConfigurationImpl(final String moduleShardsConfigPath, final String modulesConfigPath) {
45 this(new FileModuleShardConfigProvider(moduleShardsConfigPath, modulesConfigPath));
48 public ConfigurationImpl(final ModuleShardConfigProvider provider) {
49 ImmutableMap.Builder<String, ModuleConfig> mapBuilder = ImmutableMap.builder();
50 for (Map.Entry<String, ModuleConfig.Builder> e: provider.retrieveModuleConfigs(this).entrySet()) {
51 mapBuilder.put(e.getKey(), e.getValue().build());
54 this.moduleConfigMap = mapBuilder.build();
56 this.allShardNames = createAllShardNames(moduleConfigMap.values());
57 this.namespaceToModuleName = createNamespaceToModuleName(moduleConfigMap.values());
60 private static Set<String> createAllShardNames(final Iterable<ModuleConfig> moduleConfigs) {
61 final ImmutableSet.Builder<String> builder = ImmutableSet.builder();
62 for (ModuleConfig moduleConfig : moduleConfigs) {
63 builder.addAll(moduleConfig.getShardNames());
66 return builder.build();
69 private static Map<String, String> createNamespaceToModuleName(final Iterable<ModuleConfig> moduleConfigs) {
70 final ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
71 for (ModuleConfig moduleConfig : moduleConfigs) {
72 if (moduleConfig.getNamespace() != null) {
73 builder.put(moduleConfig.getNamespace(), moduleConfig.getName());
77 return builder.build();
81 public Collection<String> getMemberShardNames(final MemberName memberName) {
82 requireNonNull(memberName, "memberName should not be null");
84 List<String> shards = new ArrayList<>();
85 for (ModuleConfig moduleConfig: moduleConfigMap.values()) {
86 for (ShardConfig shardConfig: moduleConfig.getShardConfigs()) {
87 if (shardConfig.getReplicas().contains(memberName)) {
88 shards.add(shardConfig.getName());
97 public String getModuleNameFromNameSpace(final String nameSpace) {
98 requireNonNull(nameSpace, "nameSpace should not be null");
100 return namespaceToModuleName.get(nameSpace);
104 public ShardStrategy getStrategyForModule(final String moduleName) {
105 requireNonNull(moduleName, "moduleName should not be null");
107 ModuleConfig moduleConfig = moduleConfigMap.get(moduleName);
108 return moduleConfig != null ? moduleConfig.getShardStrategy() : null;
112 public String getShardNameForModule(final String moduleName) {
113 requireNonNull(moduleName, "moduleName should not be null");
115 ModuleConfig moduleConfig = moduleConfigMap.get(moduleName);
116 Collection<ShardConfig> shardConfigs = moduleConfig != null ? moduleConfig.getShardConfigs() :
117 Collections.<ShardConfig>emptySet();
118 return !shardConfigs.isEmpty() ? shardConfigs.iterator().next().getName() : null;
122 public String getShardNameForPrefix(final DOMDataTreeIdentifier prefix) {
123 requireNonNull(prefix, "prefix should not be null");
125 Entry<DOMDataTreeIdentifier, PrefixShardConfiguration> bestMatchEntry = new SimpleEntry<>(
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 requireNonNull(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(final ModuleShardConfiguration config) {
180 requireNonNull(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 addPrefixConfig(requireNonNull(config, "PrefixShardConfiguration cannot be null"));
197 allShardNames = ImmutableSet.<String>builder().addAll(allShardNames)
198 .add(ClusterUtils.getCleanShardName(config.getPrefix().getRootIdentifier())).build();
202 public void removePrefixShardConfiguration(final DOMDataTreeIdentifier prefix) {
203 removePrefixConfig(requireNonNull(prefix, "Prefix cannot be null"));
205 final HashSet<String> temp = new HashSet<>(allShardNames);
206 temp.remove(ClusterUtils.getCleanShardName(prefix.getRootIdentifier()));
208 allShardNames = ImmutableSet.copyOf(temp);
212 public Map<DOMDataTreeIdentifier, PrefixShardConfiguration> getAllPrefixShardConfigurations() {
213 return ImmutableMap.copyOf(prefixConfigMap);
216 private void addPrefixConfig(final PrefixShardConfiguration config) {
217 final Map<DOMDataTreeIdentifier, PrefixShardConfiguration> newPrefixConfigMap = new HashMap<>(prefixConfigMap);
218 newPrefixConfigMap.put(config.getPrefix(), config);
219 prefixConfigMap = ImmutableMap.copyOf(newPrefixConfigMap);
222 private void removePrefixConfig(final DOMDataTreeIdentifier prefix) {
223 final Map<DOMDataTreeIdentifier, PrefixShardConfiguration> newPrefixConfigMap = new HashMap<>(prefixConfigMap);
224 newPrefixConfigMap.remove(prefix);
225 prefixConfigMap = ImmutableMap.copyOf(newPrefixConfigMap);
228 private ShardStrategy createShardStrategy(final String moduleName, final String shardStrategyName) {
229 return ShardStrategyFactory.newShardStrategyInstance(moduleName, shardStrategyName, this);
233 public boolean isShardConfigured(final String shardName) {
234 checkNotNullShardName(shardName);
235 return allShardNames.contains(shardName);
239 public void addMemberReplicaForShard(final String shardName, final MemberName newMemberName) {
240 checkNotNullShardName(shardName);
241 requireNonNull(newMemberName, "MemberName should not be null");
243 for (ModuleConfig moduleConfig: moduleConfigMap.values()) {
244 ShardConfig shardConfig = moduleConfig.getShardConfig(shardName);
245 if (shardConfig != null) {
246 Set<MemberName> replicas = new HashSet<>(shardConfig.getReplicas());
247 replicas.add(newMemberName);
248 updateModuleConfigMap(ModuleConfig.builder(moduleConfig).shardConfig(shardName, replicas).build());
255 public void removeMemberReplicaForShard(final String shardName, final MemberName newMemberName) {
256 checkNotNullShardName(shardName);
257 requireNonNull(newMemberName, "MemberName should not be null");
259 for (ModuleConfig moduleConfig: moduleConfigMap.values()) {
260 ShardConfig shardConfig = moduleConfig.getShardConfig(shardName);
261 if (shardConfig != null) {
262 Set<MemberName> replicas = new HashSet<>(shardConfig.getReplicas());
263 replicas.remove(newMemberName);
264 updateModuleConfigMap(ModuleConfig.builder(moduleConfig).shardConfig(shardName, replicas).build());
271 public ShardStrategy getStrategyForPrefix(final DOMDataTreeIdentifier prefix) {
272 requireNonNull(prefix, "Prefix cannot be null");
273 // FIXME using prefix tables like in mdsal will be better
274 Entry<DOMDataTreeIdentifier, PrefixShardConfiguration> bestMatchEntry = new SimpleEntry<>(
275 new DOMDataTreeIdentifier(prefix.getDatastoreType(), YangInstanceIdentifier.EMPTY), null);
277 for (Entry<DOMDataTreeIdentifier, PrefixShardConfiguration> entry : prefixConfigMap.entrySet()) {
278 if (entry.getKey().contains(prefix) && entry.getKey().getRootIdentifier().getPathArguments().size()
279 > bestMatchEntry.getKey().getRootIdentifier().getPathArguments().size()) {
280 bestMatchEntry = entry;
284 if (bestMatchEntry.getValue() == null) {
287 return new PrefixShardStrategy(ClusterUtils
288 .getCleanShardName(bestMatchEntry.getKey().getRootIdentifier()),
289 bestMatchEntry.getKey().getRootIdentifier());
292 private void updateModuleConfigMap(final ModuleConfig moduleConfig) {
293 final Map<String, ModuleConfig> newModuleConfigMap = new HashMap<>(moduleConfigMap);
294 newModuleConfigMap.put(moduleConfig.getName(), moduleConfig);
295 moduleConfigMap = ImmutableMap.copyOf(newModuleConfigMap);