2 * Copyright (c) 2013 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.config.manager.impl;
10 import java.util.ArrayList;
11 import java.util.Collection;
12 import java.util.Collections;
13 import java.util.HashMap;
14 import java.util.Iterator;
15 import java.util.LinkedList;
16 import java.util.List;
18 import java.util.Map.Entry;
21 import javax.annotation.concurrent.GuardedBy;
22 import javax.annotation.concurrent.NotThreadSafe;
23 import javax.annotation.concurrent.ThreadSafe;
24 import javax.management.InstanceAlreadyExistsException;
25 import javax.management.InstanceNotFoundException;
26 import javax.management.MBeanServer;
27 import javax.management.MBeanServerFactory;
28 import javax.management.ObjectName;
30 import org.opendaylight.controller.config.api.ConflictingVersionException;
31 import org.opendaylight.controller.config.api.ModuleIdentifier;
32 import org.opendaylight.controller.config.api.RuntimeBeanRegistratorAwareModule;
33 import org.opendaylight.controller.config.api.ValidationException;
34 import org.opendaylight.controller.config.api.jmx.CommitStatus;
35 import org.opendaylight.controller.config.api.jmx.ConfigRegistryMXBean;
36 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
37 import org.opendaylight.controller.config.manager.impl.dynamicmbean.DynamicReadableWrapper;
38 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HierarchicalConfigMBeanFactoriesHolder;
39 import org.opendaylight.controller.config.manager.impl.factoriesresolver.ModuleFactoriesResolver;
40 import org.opendaylight.controller.config.manager.impl.jmx.BaseJMXRegistrator;
41 import org.opendaylight.controller.config.manager.impl.jmx.ModuleJMXRegistrator;
42 import org.opendaylight.controller.config.manager.impl.jmx.RootRuntimeBeanRegistratorImpl;
43 import org.opendaylight.controller.config.manager.impl.jmx.TransactionJMXRegistrator;
44 import org.opendaylight.controller.config.manager.impl.osgi.BeanToOsgiServiceManager;
45 import org.opendaylight.controller.config.manager.impl.osgi.BeanToOsgiServiceManager.OsgiRegistration;
46 import org.opendaylight.controller.config.manager.impl.util.LookupBeansUtil;
47 import org.opendaylight.controller.config.spi.Module;
48 import org.opendaylight.controller.config.spi.ModuleFactory;
49 import org.osgi.framework.BundleContext;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
54 * Singleton that is responsible for creating and committing Config
55 * Transactions. It is registered in Platform MBean Server.
58 public class ConfigRegistryImpl implements AutoCloseable, ConfigRegistryImplMXBean {
59 private static final Logger logger = LoggerFactory.getLogger(ConfigRegistryImpl.class);
61 private final ModuleFactoriesResolver resolver;
62 private final MBeanServer configMBeanServer;
64 private long version = 0;
66 private long versionCounter = 0;
69 * Contains current configuration in form of {moduleName:{instanceName,read
70 * only module}} for copying state to new transaction. Each running module
71 * is present just once, no matter how many interfaces it exposes.
74 private final ConfigHolder currentConfig = new ConfigHolder();
77 * Will return true unless there was a transaction that succeeded during
78 * validation but failed in second phase of commit. In this case the server
79 * is unstable and its state is undefined.
82 private boolean isHealthy = true;
85 * Holds Map<transactionName, transactionController> and purges it each time
86 * its content is requested.
89 private final TransactionsHolder transactionsHolder = new TransactionsHolder();
91 private final BaseJMXRegistrator baseJMXRegistrator;
93 private final BeanToOsgiServiceManager beanToOsgiServiceManager;
95 // internal jmx server for read only beans
96 private final MBeanServer registryMBeanServer;
97 // internal jmx server shared by all transactions
98 private final MBeanServer transactionsMBeanServer;
101 private List<ModuleFactory> lastListOfFactories = Collections.emptyList();
104 public ConfigRegistryImpl(ModuleFactoriesResolver resolver,
105 BundleContext bundleContext, MBeanServer configMBeanServer) {
106 this(resolver, bundleContext, configMBeanServer,
107 new BaseJMXRegistrator(configMBeanServer));
111 public ConfigRegistryImpl(ModuleFactoriesResolver resolver,
112 BundleContext bundleContext, MBeanServer configMBeanServer,
113 BaseJMXRegistrator baseJMXRegistrator) {
114 this.resolver = resolver;
115 this.beanToOsgiServiceManager = new BeanToOsgiServiceManager(
117 this.configMBeanServer = configMBeanServer;
118 this.baseJMXRegistrator = baseJMXRegistrator;
119 this.registryMBeanServer = MBeanServerFactory
120 .createMBeanServer("ConfigRegistry" + configMBeanServer.getDefaultDomain());
121 this.transactionsMBeanServer = MBeanServerFactory
122 .createMBeanServer("ConfigTransactions" + configMBeanServer.getDefaultDomain());
126 * Create new {@link ConfigTransactionControllerImpl }
129 public synchronized ObjectName beginConfig() {
130 return beginConfigInternal().getControllerObjectName();
133 private synchronized ConfigTransactionControllerInternal beginConfigInternal() {
135 String transactionName = "ConfigTransaction-" + version + "-" + versionCounter;
136 TransactionJMXRegistrator transactionRegistrator = baseJMXRegistrator
137 .createTransactionJMXRegistrator(transactionName);
138 List<ModuleFactory> allCurrentFactories = Collections.unmodifiableList(resolver.getAllFactories());
139 ConfigTransactionControllerInternal transactionController = new ConfigTransactionControllerImpl(
140 transactionName, transactionRegistrator, version,
141 versionCounter, allCurrentFactories, transactionsMBeanServer, configMBeanServer);
143 transactionRegistrator.registerMBean(transactionController, transactionController.getControllerObjectName());
144 } catch (InstanceAlreadyExistsException e) {
145 throw new IllegalStateException(e);
148 transactionController.copyExistingModulesAndProcessFactoryDiff(currentConfig.getEntries(), lastListOfFactories);
150 transactionsHolder.add(transactionName, transactionController);
151 return transactionController;
158 public synchronized CommitStatus commitConfig(ObjectName transactionControllerON)
159 throws ConflictingVersionException, ValidationException {
160 final String transactionName = ObjectNameUtil
161 .getTransactionName(transactionControllerON);
162 logger.info("About to commit {}. Current parentVersion: {}, versionCounter {}", transactionName, version, versionCounter);
164 // find ConfigTransactionController
165 Map<String, ConfigTransactionControllerInternal> transactions = transactionsHolder.getCurrentTransactions();
166 ConfigTransactionControllerInternal configTransactionController = transactions.get(transactionName);
167 if (configTransactionController == null) {
168 throw new IllegalArgumentException(String.format(
169 "Transaction with name '%s' not found", transactionName));
171 // check optimistic lock
172 if (version != configTransactionController.getParentVersion()) {
173 throw new ConflictingVersionException(
175 "Optimistic lock failed. Expected parent version %d, was %d",
177 configTransactionController.getParentVersion()));
179 // optimistic lock ok
181 CommitInfo commitInfo = configTransactionController.validateBeforeCommitAndLockTransaction();
182 lastListOfFactories = Collections.unmodifiableList(configTransactionController.getCurrentlyRegisteredFactories());
183 // non recoverable from here:
185 return secondPhaseCommit(
186 configTransactionController, commitInfo);
187 } catch (Throwable t) { // some libs throw Errors: e.g.
188 // javax.xml.ws.spi.FactoryFinder$ConfigurationError
190 logger.error("Configuration Transaction failed on 2PC, server is unhealthy", t);
191 if (t instanceof RuntimeException) {
192 throw (RuntimeException) t;
193 } else if (t instanceof Error) {
196 throw new RuntimeException(t);
201 private CommitStatus secondPhaseCommit(ConfigTransactionControllerInternal configTransactionController,
202 CommitInfo commitInfo) {
204 // close instances which were destroyed by the user, including
205 // (hopefully) runtime beans
206 for (DestroyedModule toBeDestroyed : commitInfo
207 .getDestroyedFromPreviousTransactions()) {
208 toBeDestroyed.close(); // closes instance (which should close
209 // runtime jmx registrator),
210 // also closes osgi registration and ModuleJMXRegistrator
212 currentConfig.remove(toBeDestroyed.getIdentifier());
215 // set RuntimeBeanRegistrators on beans implementing
216 // RuntimeBeanRegistratorAwareModule, each module
217 // should have exactly one runtime jmx registrator.
218 Map<ModuleIdentifier, RootRuntimeBeanRegistratorImpl> runtimeRegistrators = new HashMap<>();
219 for (ModuleInternalTransactionalInfo entry : commitInfo.getCommitted()
221 RootRuntimeBeanRegistratorImpl runtimeBeanRegistrator;
222 if (entry.hasOldModule() == false) {
223 runtimeBeanRegistrator = baseJMXRegistrator
224 .createRuntimeBeanRegistrator(entry.getName());
226 // reuse old JMX registrator
227 runtimeBeanRegistrator = entry.getOldInternalInfo()
228 .getRuntimeBeanRegistrator();
230 // set runtime jmx registrator if required
231 Module module = entry.getModule();
232 if (module instanceof RuntimeBeanRegistratorAwareModule) {
233 ((RuntimeBeanRegistratorAwareModule) module)
234 .setRuntimeBeanRegistrator(runtimeBeanRegistrator);
236 // save it to info so it is accessible afterwards
237 runtimeRegistrators.put(entry.getName(), runtimeBeanRegistrator);
240 // can register runtime beans
241 List<ModuleIdentifier> orderedModuleIdentifiers = configTransactionController
242 .secondPhaseCommit();
244 // copy configuration to read only mode
245 List<ObjectName> newInstances = new LinkedList<>();
246 List<ObjectName> reusedInstances = new LinkedList<>();
247 List<ObjectName> recreatedInstances = new LinkedList<>();
249 Map<Module, ModuleInternalInfo> newConfigEntries = new HashMap<>();
252 for (ModuleIdentifier moduleIdentifier : orderedModuleIdentifiers) {
253 ModuleInternalTransactionalInfo entry = commitInfo.getCommitted()
254 .get(moduleIdentifier);
256 throw new NullPointerException("Module not found "
258 Module module = entry.getModule();
259 ObjectName primaryReadOnlyON = ObjectNameUtil
260 .createReadOnlyModuleON(moduleIdentifier);
262 // determine if current instance was recreated or reused or is new
264 // rules for closing resources:
265 // osgi registration - will be (re)created every time, so it needs
267 // module jmx registration - will be (re)created every time, needs
269 // runtime jmx registration - should be taken care of by module
271 // instance - is closed only if it was destroyed
272 ModuleJMXRegistrator newModuleJMXRegistrator = baseJMXRegistrator
273 .createModuleJMXRegistrator();
275 if (entry.hasOldModule()) {
276 ModuleInternalInfo oldInternalInfo = entry.getOldInternalInfo();
277 DynamicReadableWrapper oldReadableConfigBean = oldInternalInfo
278 .getReadableModule();
279 currentConfig.remove(entry.getName());
281 // test if old instance == new instance
282 if (oldReadableConfigBean.getInstance().equals(
283 module.getInstance())) {
284 // reused old instance:
285 // wrap in readable dynamic mbean
286 reusedInstances.add(primaryReadOnlyON);
288 // recreated instance:
289 // it is responsibility of module to call the old instance -
290 // we just need to unregister configbean
291 recreatedInstances.add(primaryReadOnlyON);
293 // close old osgi registration in any case
294 oldInternalInfo.getOsgiRegistration().close();
295 // close old module jmx registrator
296 oldInternalInfo.getModuleJMXRegistrator().close();
299 // wrap in readable dynamic mbean
300 newInstances.add(primaryReadOnlyON);
303 DynamicReadableWrapper newReadableConfigBean = new DynamicReadableWrapper(
304 module, module.getInstance(), moduleIdentifier,
305 registryMBeanServer, configMBeanServer);
309 newModuleJMXRegistrator.registerMBean(newReadableConfigBean,
311 } catch (InstanceAlreadyExistsException e) {
312 throw new IllegalStateException(e);
316 OsgiRegistration osgiRegistration = beanToOsgiServiceManager
317 .registerToOsgi(module.getClass(),
318 newReadableConfigBean.getInstance(),
321 RootRuntimeBeanRegistratorImpl runtimeBeanRegistrator = runtimeRegistrators
322 .get(entry.getName());
323 ModuleInternalInfo newInfo = new ModuleInternalInfo(
324 entry.getName(), newReadableConfigBean, osgiRegistration,
325 runtimeBeanRegistrator, newModuleJMXRegistrator,
328 newConfigEntries.put(module, newInfo);
331 currentConfig.addAll(newConfigEntries.values());
334 version = configTransactionController.getVersion();
335 return new CommitStatus(newInstances, reusedInstances,
343 public synchronized List<ObjectName> getOpenConfigs() {
344 Map<String, ConfigTransactionControllerInternal> transactions = transactionsHolder
345 .getCurrentTransactions();
346 List<ObjectName> result = new ArrayList<>(transactions.size());
347 for (ConfigTransactionControllerInternal configTransactionController : transactions
349 result.add(configTransactionController.getControllerObjectName());
355 * Abort open transactions and unregister read only modules. Since this
356 * class is not responsible for registering itself under
357 * {@link ConfigRegistryMXBean#OBJECT_NAME}, it will not unregister itself
361 public synchronized void close() {
362 // abort transactions
363 Map<String, ConfigTransactionControllerInternal> transactions = transactionsHolder
364 .getCurrentTransactions();
365 for (ConfigTransactionControllerInternal configTransactionController : transactions
368 configTransactionController.abortConfig();
369 } catch (RuntimeException e) {
370 logger.warn("Ignoring exception while aborting {}",
371 configTransactionController, e);
375 // destroy all live objects one after another in order of the dependency
377 List<DestroyedModule> destroyedModules = currentConfig
378 .getModulesToBeDestroyed();
379 for (DestroyedModule destroyedModule : destroyedModules) {
380 destroyedModule.close();
382 // unregister MBeans that failed to unregister properly
383 baseJMXRegistrator.close();
384 // remove jmx servers
385 MBeanServerFactory.releaseMBeanServer(registryMBeanServer);
386 MBeanServerFactory.releaseMBeanServer(transactionsMBeanServer);
394 public long getVersion() {
402 public Set<String> getAvailableModuleNames() {
403 return new HierarchicalConfigMBeanFactoriesHolder(
404 resolver.getAllFactories()).getModuleNames();
411 public boolean isHealthy() {
421 public Set<ObjectName> lookupConfigBeans() {
422 return lookupConfigBeans("*", "*");
429 public Set<ObjectName> lookupConfigBeans(String moduleName) {
430 return lookupConfigBeans(moduleName, "*");
437 public ObjectName lookupConfigBean(String moduleName, String instanceName)
438 throws InstanceNotFoundException {
439 return LookupBeansUtil.lookupConfigBean(this, moduleName, instanceName);
446 public Set<ObjectName> lookupConfigBeans(String moduleName,
447 String instanceName) {
448 ObjectName namePattern = ObjectNameUtil.createModulePattern(moduleName,
450 return baseJMXRegistrator.queryNames(namePattern, null);
457 public Set<ObjectName> lookupRuntimeBeans() {
458 return lookupRuntimeBeans("*", "*");
465 public Set<ObjectName> lookupRuntimeBeans(String moduleName,
466 String instanceName) {
467 if (moduleName == null)
469 if (instanceName == null)
471 ObjectName namePattern = ObjectNameUtil.createRuntimeBeanPattern(
472 moduleName, instanceName);
473 return baseJMXRegistrator.queryNames(namePattern, null);
479 * Holds currently running modules
483 private final Map<ModuleIdentifier, ModuleInternalInfo> currentConfig = new HashMap<>();
486 * Add all modules to the internal map. Also add service instance to OSGi
489 public void addAll(Collection<ModuleInternalInfo> configInfos) {
490 if (currentConfig.size() > 0) {
491 throw new IllegalStateException(
492 "Error - some config entries were not removed: "
495 for (ModuleInternalInfo configInfo : configInfos) {
500 private void add(ModuleInternalInfo configInfo) {
501 ModuleInternalInfo oldValue = currentConfig.put(configInfo.getName(),
503 if (oldValue != null) {
504 throw new IllegalStateException(
505 "Cannot overwrite module with same name:"
506 + configInfo.getName() + ":" + configInfo);
511 * Remove entry from current config.
513 public void remove(ModuleIdentifier name) {
514 ModuleInternalInfo removed = currentConfig.remove(name);
515 if (removed == null) {
516 throw new IllegalStateException(
517 "Cannot remove from ConfigHolder - name not found:" + name);
521 public Collection<ModuleInternalInfo> getEntries() {
522 return currentConfig.values();
525 public List<DestroyedModule> getModulesToBeDestroyed() {
526 List<DestroyedModule> result = new ArrayList<>();
527 for (ModuleInternalInfo moduleInternalInfo : getEntries()) {
528 result.add(moduleInternalInfo.toDestroyedModule());
530 Collections.sort(result);
536 * Holds Map<transactionName, transactionController> and purges it each time its
537 * content is requested.
540 class TransactionsHolder {
542 * This map keeps transaction names and
543 * {@link ConfigTransactionControllerInternal} instances, because platform
544 * MBeanServer transforms mbeans into another representation. Map is cleaned
545 * every time current transactions are requested.
548 @GuardedBy("ConfigRegistryImpl.this")
549 private final Map<String /* transactionName */, ConfigTransactionControllerInternal> transactions = new HashMap<>();
552 * Can only be called from within synchronized method.
554 public void add(String transactionName,
555 ConfigTransactionControllerInternal transactionController) {
556 Object oldValue = transactions.put(transactionName,
557 transactionController);
558 if (oldValue != null) {
559 throw new IllegalStateException(
560 "Error: two transactions with same name");
565 * Purges closed transactions from transactions map. Can only be called from
566 * within synchronized method. Calling this method more than once within the
567 * method can modify the resulting map that was obtained in previous calls.
569 * @return current view on transactions map.
571 public Map<String, ConfigTransactionControllerInternal> getCurrentTransactions() {
572 // first, remove closed transaction
573 for (Iterator<Entry<String, ConfigTransactionControllerInternal>> it = transactions
574 .entrySet().iterator(); it.hasNext();) {
575 Entry<String, ConfigTransactionControllerInternal> entry = it
577 if (entry.getValue().isClosed()) {
581 return Collections.unmodifiableMap(transactions);