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
9 package org.opendaylight.controller.cluster.datastore.config;
11 import com.google.common.base.Preconditions;
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 javax.annotation.Nonnull;
25 import javax.annotation.Nullable;
26 import org.opendaylight.controller.cluster.access.concepts.MemberName;
27 import org.opendaylight.controller.cluster.datastore.shardstrategy.PrefixShardStrategy;
28 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategy;
29 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategyFactory;
30 import org.opendaylight.controller.cluster.datastore.utils.ClusterUtils;
31 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
34 // TODO clean this up once we get rid of module based configuration, prefix one should be alot simpler
35 public class ConfigurationImpl implements Configuration {
36 private volatile Map<String, ModuleConfig> moduleConfigMap;
38 // TODO should this be initialized with something? on restart we should restore the shards from configuration?
39 private volatile Map<DOMDataTreeIdentifier, PrefixShardConfiguration> prefixConfigMap = Collections.emptyMap();
41 // Look up maps to speed things up
43 private volatile Map<String, String> namespaceToModuleName;
44 private volatile Set<String> allShardNames;
46 public ConfigurationImpl(final String moduleShardsConfigPath, final String modulesConfigPath) {
47 this(new FileModuleShardConfigProvider(moduleShardsConfigPath, modulesConfigPath));
50 public ConfigurationImpl(final ModuleShardConfigProvider provider) {
51 ImmutableMap.Builder<String, ModuleConfig> mapBuilder = ImmutableMap.builder();
52 for (Map.Entry<String, ModuleConfig.Builder> e: provider.retrieveModuleConfigs(this).entrySet()) {
53 mapBuilder.put(e.getKey(), e.getValue().build());
56 this.moduleConfigMap = mapBuilder.build();
58 this.allShardNames = createAllShardNames(moduleConfigMap.values());
59 this.namespaceToModuleName = createNamespaceToModuleName(moduleConfigMap.values());
62 private static Set<String> createAllShardNames(Iterable<ModuleConfig> moduleConfigs) {
63 final ImmutableSet.Builder<String> builder = ImmutableSet.builder();
64 for (ModuleConfig moduleConfig : moduleConfigs) {
65 builder.addAll(moduleConfig.getShardNames());
68 return builder.build();
71 private static Map<String, String> createNamespaceToModuleName(Iterable<ModuleConfig> moduleConfigs) {
72 final ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
73 for (ModuleConfig moduleConfig : moduleConfigs) {
74 if (moduleConfig.getNamespace() != null) {
75 builder.put(moduleConfig.getNamespace(), moduleConfig.getName());
79 return builder.build();
83 public Collection<String> getMemberShardNames(final MemberName memberName) {
84 Preconditions.checkNotNull(memberName, "memberName should not be null");
86 List<String> shards = new ArrayList<>();
87 for (ModuleConfig moduleConfig: moduleConfigMap.values()) {
88 for (ShardConfig shardConfig: moduleConfig.getShardConfigs()) {
89 if (shardConfig.getReplicas().contains(memberName)) {
90 shards.add(shardConfig.getName());
99 public String getModuleNameFromNameSpace(final String nameSpace) {
100 Preconditions.checkNotNull(nameSpace, "nameSpace should not be null");
102 return namespaceToModuleName.get(nameSpace);
106 public ShardStrategy getStrategyForModule(String moduleName) {
107 Preconditions.checkNotNull(moduleName, "moduleName should not be null");
109 ModuleConfig moduleConfig = moduleConfigMap.get(moduleName);
110 return moduleConfig != null ? moduleConfig.getShardStrategy() : null;
114 public String getShardNameForModule(final String moduleName) {
115 Preconditions.checkNotNull(moduleName, "moduleName should not be null");
117 ModuleConfig moduleConfig = moduleConfigMap.get(moduleName);
118 Collection<ShardConfig> shardConfigs = moduleConfig != null ? moduleConfig.getShardConfigs() :
119 Collections.<ShardConfig>emptySet();
120 return !shardConfigs.isEmpty() ? shardConfigs.iterator().next().getName() : null;
125 public String getShardNameForPrefix(@Nonnull final DOMDataTreeIdentifier prefix) {
126 Preconditions.checkNotNull(prefix, "prefix should not be null");
128 Entry<DOMDataTreeIdentifier, PrefixShardConfiguration> bestMatchEntry =
130 new DOMDataTreeIdentifier(prefix.getDatastoreType(), YangInstanceIdentifier.EMPTY), null);
132 for (Entry<DOMDataTreeIdentifier, PrefixShardConfiguration> entry : prefixConfigMap.entrySet()) {
133 if (entry.getKey().contains(prefix) && entry.getKey().getRootIdentifier().getPathArguments().size()
134 > bestMatchEntry.getKey().getRootIdentifier().getPathArguments().size()) {
135 bestMatchEntry = entry;
139 //TODO we really should have mapping based on prefix instead of Strings
140 return ClusterUtils.getCleanShardName(bestMatchEntry.getKey().getRootIdentifier());
144 public Collection<MemberName> getMembersFromShardName(final String shardName) {
145 Preconditions.checkNotNull(shardName, "shardName should not be null");
147 for (ModuleConfig moduleConfig: moduleConfigMap.values()) {
148 ShardConfig shardConfig = moduleConfig.getShardConfig(shardName);
149 if (shardConfig != null) {
150 return shardConfig.getReplicas();
154 for (final PrefixShardConfiguration prefixConfig : prefixConfigMap.values()) {
155 if (shardName.equals(ClusterUtils.getCleanShardName(prefixConfig.getPrefix().getRootIdentifier()))) {
156 return prefixConfig.getShardMemberNames();
160 return Collections.emptyList();
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(@Nonnull 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(@Nonnull 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 Preconditions.checkNotNull(shardName, "shardName should not be null");
238 return allShardNames.contains(shardName);
242 public void addMemberReplicaForShard(String shardName, MemberName newMemberName) {
243 Preconditions.checkNotNull(shardName, "shardName should not be null");
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 Preconditions.checkNotNull(shardName, "shardName should not be null");
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(@Nonnull 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);