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 return namespaceToModuleName.get(requireNonNull(nameSpace, "nameSpace should not be null"));
102 public ShardStrategy getStrategyForModule(final String moduleName) {
103 ModuleConfig moduleConfig = getModuleConfig(moduleName);
104 return moduleConfig != null ? moduleConfig.getShardStrategy() : null;
108 public String getShardNameForModule(final String moduleName) {
109 ModuleConfig moduleConfig = getModuleConfig(moduleName);
110 if (moduleConfig != null) {
111 Collection<ShardConfig> shardConfigs = moduleConfig.getShardConfigs();
112 if (!shardConfigs.isEmpty()) {
113 return shardConfigs.iterator().next().getName();
119 private ModuleConfig getModuleConfig(final String moduleName) {
120 return moduleConfigMap.get(requireNonNull(moduleName, "moduleName should not be null"));
124 public String getShardNameForPrefix(final DOMDataTreeIdentifier prefix) {
125 requireNonNull(prefix, "prefix should not be null");
127 Entry<DOMDataTreeIdentifier, PrefixShardConfiguration> bestMatchEntry = new SimpleEntry<>(
128 new DOMDataTreeIdentifier(prefix.getDatastoreType(), YangInstanceIdentifier.empty()), null);
130 for (Entry<DOMDataTreeIdentifier, PrefixShardConfiguration> entry : prefixConfigMap.entrySet()) {
131 if (entry.getKey().contains(prefix) && entry.getKey().getRootIdentifier().getPathArguments().size()
132 > bestMatchEntry.getKey().getRootIdentifier().getPathArguments().size()) {
133 bestMatchEntry = entry;
137 //TODO we really should have mapping based on prefix instead of Strings
138 return ClusterUtils.getCleanShardName(bestMatchEntry.getKey().getRootIdentifier());
142 public Collection<MemberName> getMembersFromShardName(final String shardName) {
143 checkNotNullShardName(shardName);
145 for (ModuleConfig moduleConfig: moduleConfigMap.values()) {
146 ShardConfig shardConfig = moduleConfig.getShardConfig(shardName);
147 if (shardConfig != null) {
148 return shardConfig.getReplicas();
152 for (final PrefixShardConfiguration prefixConfig : prefixConfigMap.values()) {
153 if (shardName.equals(ClusterUtils.getCleanShardName(prefixConfig.getPrefix().getRootIdentifier()))) {
154 return prefixConfig.getShardMemberNames();
158 return Collections.emptyList();
161 private static void checkNotNullShardName(final String shardName) {
162 requireNonNull(shardName, "shardName should not be null");
166 public Set<String> getAllShardNames() {
167 return allShardNames;
171 public Collection<MemberName> getUniqueMemberNamesForAllShards() {
172 Set<MemberName> allNames = new HashSet<>();
173 for (String shardName: getAllShardNames()) {
174 allNames.addAll(getMembersFromShardName(shardName));
181 public synchronized void addModuleShardConfiguration(final ModuleShardConfiguration config) {
182 requireNonNull(config, "ModuleShardConfiguration should not be null");
184 ModuleConfig moduleConfig = ModuleConfig.builder(config.getModuleName())
185 .nameSpace(config.getNamespace().toString())
186 .shardStrategy(createShardStrategy(config.getModuleName(), config.getShardStrategyName()))
187 .shardConfig(config.getShardName(), config.getShardMemberNames()).build();
189 updateModuleConfigMap(moduleConfig);
191 namespaceToModuleName = ImmutableMap.<String, String>builder().putAll(namespaceToModuleName)
192 .put(moduleConfig.getNamespace(), moduleConfig.getName()).build();
193 allShardNames = ImmutableSet.<String>builder().addAll(allShardNames).add(config.getShardName()).build();
197 public void addPrefixShardConfiguration(final PrefixShardConfiguration config) {
198 addPrefixConfig(requireNonNull(config, "PrefixShardConfiguration cannot be null"));
199 allShardNames = ImmutableSet.<String>builder().addAll(allShardNames)
200 .add(ClusterUtils.getCleanShardName(config.getPrefix().getRootIdentifier())).build();
204 public void removePrefixShardConfiguration(final DOMDataTreeIdentifier prefix) {
205 removePrefixConfig(requireNonNull(prefix, "Prefix cannot be null"));
207 final HashSet<String> temp = new HashSet<>(allShardNames);
208 temp.remove(ClusterUtils.getCleanShardName(prefix.getRootIdentifier()));
210 allShardNames = ImmutableSet.copyOf(temp);
214 public Map<DOMDataTreeIdentifier, PrefixShardConfiguration> getAllPrefixShardConfigurations() {
215 return ImmutableMap.copyOf(prefixConfigMap);
218 private void addPrefixConfig(final PrefixShardConfiguration config) {
219 final Map<DOMDataTreeIdentifier, PrefixShardConfiguration> newPrefixConfigMap = new HashMap<>(prefixConfigMap);
220 newPrefixConfigMap.put(config.getPrefix(), config);
221 prefixConfigMap = ImmutableMap.copyOf(newPrefixConfigMap);
224 private void removePrefixConfig(final DOMDataTreeIdentifier prefix) {
225 final Map<DOMDataTreeIdentifier, PrefixShardConfiguration> newPrefixConfigMap = new HashMap<>(prefixConfigMap);
226 newPrefixConfigMap.remove(prefix);
227 prefixConfigMap = ImmutableMap.copyOf(newPrefixConfigMap);
230 private ShardStrategy createShardStrategy(final String moduleName, final String shardStrategyName) {
231 return ShardStrategyFactory.newShardStrategyInstance(moduleName, shardStrategyName, this);
235 public boolean isShardConfigured(final String shardName) {
236 checkNotNullShardName(shardName);
237 return allShardNames.contains(shardName);
241 public void addMemberReplicaForShard(final String shardName, final MemberName newMemberName) {
242 checkNotNullShardName(shardName);
243 requireNonNull(newMemberName, "MemberName should not be null");
245 for (ModuleConfig moduleConfig: moduleConfigMap.values()) {
246 ShardConfig shardConfig = moduleConfig.getShardConfig(shardName);
247 if (shardConfig != null) {
248 Set<MemberName> replicas = new HashSet<>(shardConfig.getReplicas());
249 replicas.add(newMemberName);
250 updateModuleConfigMap(ModuleConfig.builder(moduleConfig).shardConfig(shardName, replicas).build());
257 public void removeMemberReplicaForShard(final String shardName, final MemberName newMemberName) {
258 checkNotNullShardName(shardName);
259 requireNonNull(newMemberName, "MemberName should not be null");
261 for (ModuleConfig moduleConfig: moduleConfigMap.values()) {
262 ShardConfig shardConfig = moduleConfig.getShardConfig(shardName);
263 if (shardConfig != null) {
264 Set<MemberName> replicas = new HashSet<>(shardConfig.getReplicas());
265 replicas.remove(newMemberName);
266 updateModuleConfigMap(ModuleConfig.builder(moduleConfig).shardConfig(shardName, replicas).build());
273 public ShardStrategy getStrategyForPrefix(final DOMDataTreeIdentifier prefix) {
274 requireNonNull(prefix, "Prefix cannot be null");
275 // FIXME using prefix tables like in mdsal will be better
276 Entry<DOMDataTreeIdentifier, PrefixShardConfiguration> bestMatchEntry = new SimpleEntry<>(
277 new DOMDataTreeIdentifier(prefix.getDatastoreType(), YangInstanceIdentifier.empty()), null);
279 for (Entry<DOMDataTreeIdentifier, PrefixShardConfiguration> entry : prefixConfigMap.entrySet()) {
280 if (entry.getKey().contains(prefix) && entry.getKey().getRootIdentifier().getPathArguments().size()
281 > bestMatchEntry.getKey().getRootIdentifier().getPathArguments().size()) {
282 bestMatchEntry = entry;
286 if (bestMatchEntry.getValue() == null) {
289 return new PrefixShardStrategy(ClusterUtils
290 .getCleanShardName(bestMatchEntry.getKey().getRootIdentifier()),
291 bestMatchEntry.getKey().getRootIdentifier());
294 private void updateModuleConfigMap(final ModuleConfig moduleConfig) {
295 final Map<String, ModuleConfig> newModuleConfigMap = new HashMap<>(moduleConfigMap);
296 newModuleConfigMap.put(moduleConfig.getName(), moduleConfig);
297 moduleConfigMap = ImmutableMap.copyOf(newModuleConfigMap);