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 org.opendaylight.controller.config.api.ConflictingVersionException;
11 import org.opendaylight.controller.config.api.ModuleIdentifier;
12 import org.opendaylight.controller.config.api.RuntimeBeanRegistratorAwareModule;
13 import org.opendaylight.controller.config.api.ValidationException;
14 import org.opendaylight.controller.config.api.jmx.CommitStatus;
15 import org.opendaylight.controller.config.api.jmx.ConfigRegistryMXBean;
16 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
17 import org.opendaylight.controller.config.manager.impl.dynamicmbean.DynamicReadableWrapper;
18 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HierarchicalConfigMBeanFactoriesHolder;
19 import org.opendaylight.controller.config.manager.impl.factoriesresolver.ModuleFactoriesResolver;
20 import org.opendaylight.controller.config.manager.impl.jmx.BaseJMXRegistrator;
21 import org.opendaylight.controller.config.manager.impl.jmx.ModuleJMXRegistrator;
22 import org.opendaylight.controller.config.manager.impl.jmx.RootRuntimeBeanRegistratorImpl;
23 import org.opendaylight.controller.config.manager.impl.jmx.TransactionJMXRegistrator;
24 import org.opendaylight.controller.config.manager.impl.osgi.BeanToOsgiServiceManager;
25 import org.opendaylight.controller.config.manager.impl.osgi.BeanToOsgiServiceManager.OsgiRegistration;
26 import org.opendaylight.controller.config.manager.impl.util.LookupBeansUtil;
27 import org.opendaylight.controller.config.spi.Module;
28 import org.opendaylight.controller.config.spi.ModuleFactory;
29 import org.osgi.framework.BundleContext;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 import javax.annotation.concurrent.GuardedBy;
34 import javax.annotation.concurrent.NotThreadSafe;
35 import javax.annotation.concurrent.ThreadSafe;
36 import javax.management.*;
38 import java.util.Map.Entry;
41 * Singleton that is responsible for creating and committing Config
42 * Transactions. It is registered in Platform MBean Server.
45 public class ConfigRegistryImpl implements AutoCloseable, ConfigRegistryImplMXBean {
46 private static final Logger logger = LoggerFactory.getLogger(ConfigRegistryImpl.class);
48 private final ModuleFactoriesResolver resolver;
49 private final MBeanServer configMBeanServer;
52 private final BundleContext bundleContext;
55 private long version = 0;
57 private long versionCounter = 0;
60 * Contains current configuration in form of {moduleName:{instanceName,read
61 * only module}} for copying state to new transaction. Each running module
62 * is present just once, no matter how many interfaces it exposes.
65 private final ConfigHolder currentConfig = new ConfigHolder();
68 * Will return true unless there was a transaction that succeeded during
69 * validation but failed in second phase of commit. In this case the server
70 * is unstable and its state is undefined.
73 private boolean isHealthy = true;
76 * Holds Map<transactionName, transactionController> and purges it each time
77 * its content is requested.
80 private final TransactionsHolder transactionsHolder = new TransactionsHolder();
82 private final BaseJMXRegistrator baseJMXRegistrator;
84 private final BeanToOsgiServiceManager beanToOsgiServiceManager;
86 // internal jmx server for read only beans
87 private final MBeanServer registryMBeanServer;
88 // internal jmx server shared by all transactions
89 private final MBeanServer transactionsMBeanServer;
92 private List<ModuleFactory> lastListOfFactories = Collections.emptyList();
95 public ConfigRegistryImpl(ModuleFactoriesResolver resolver,
96 BundleContext bundleContext, MBeanServer configMBeanServer) {
97 this(resolver, bundleContext, configMBeanServer,
98 new BaseJMXRegistrator(configMBeanServer));
102 public ConfigRegistryImpl(ModuleFactoriesResolver resolver,
103 BundleContext bundleContext, MBeanServer configMBeanServer,
104 BaseJMXRegistrator baseJMXRegistrator) {
105 this.resolver = resolver;
106 this.beanToOsgiServiceManager = new BeanToOsgiServiceManager(
108 this.bundleContext = bundleContext;
109 this.configMBeanServer = configMBeanServer;
110 this.baseJMXRegistrator = baseJMXRegistrator;
111 this.registryMBeanServer = MBeanServerFactory
112 .createMBeanServer("ConfigRegistry" + configMBeanServer.getDefaultDomain());
113 this.transactionsMBeanServer = MBeanServerFactory
114 .createMBeanServer("ConfigTransactions" + configMBeanServer.getDefaultDomain());
118 * Create new {@link ConfigTransactionControllerImpl }
121 public synchronized ObjectName beginConfig() {
122 return beginConfigInternal().getControllerObjectName();
125 private synchronized ConfigTransactionControllerInternal beginConfigInternal() {
127 String transactionName = "ConfigTransaction-" + version + "-" + versionCounter;
128 TransactionJMXRegistrator transactionRegistrator = baseJMXRegistrator
129 .createTransactionJMXRegistrator(transactionName);
130 List<ModuleFactory> allCurrentFactories = Collections.unmodifiableList(resolver.getAllFactories());
131 ConfigTransactionControllerInternal transactionController = new ConfigTransactionControllerImpl(
132 transactionName, transactionRegistrator, version,
133 versionCounter, allCurrentFactories, transactionsMBeanServer, configMBeanServer, bundleContext);
135 transactionRegistrator.registerMBean(transactionController, transactionController.getControllerObjectName());
136 } catch (InstanceAlreadyExistsException e) {
137 throw new IllegalStateException(e);
140 transactionController.copyExistingModulesAndProcessFactoryDiff(currentConfig.getEntries(), lastListOfFactories);
142 transactionsHolder.add(transactionName, transactionController);
143 return transactionController;
150 public synchronized CommitStatus commitConfig(ObjectName transactionControllerON)
151 throws ConflictingVersionException, ValidationException {
152 final String transactionName = ObjectNameUtil
153 .getTransactionName(transactionControllerON);
154 logger.info("About to commit {}. Current parentVersion: {}, versionCounter {}", transactionName, version, versionCounter);
156 // find ConfigTransactionController
157 Map<String, ConfigTransactionControllerInternal> transactions = transactionsHolder.getCurrentTransactions();
158 ConfigTransactionControllerInternal configTransactionController = transactions.get(transactionName);
159 if (configTransactionController == null) {
160 throw new IllegalArgumentException(String.format(
161 "Transaction with name '%s' not found", transactionName));
163 // check optimistic lock
164 if (version != configTransactionController.getParentVersion()) {
165 throw new ConflictingVersionException(
167 "Optimistic lock failed. Expected parent version %d, was %d",
169 configTransactionController.getParentVersion()));
171 // optimistic lock ok
173 CommitInfo commitInfo = configTransactionController.validateBeforeCommitAndLockTransaction();
174 lastListOfFactories = Collections.unmodifiableList(configTransactionController.getCurrentlyRegisteredFactories());
175 // non recoverable from here:
177 return secondPhaseCommit(
178 configTransactionController, commitInfo);
179 } catch (Throwable t) { // some libs throw Errors: e.g.
180 // javax.xml.ws.spi.FactoryFinder$ConfigurationError
182 logger.error("Configuration Transaction failed on 2PC, server is unhealthy", t);
183 if (t instanceof RuntimeException) {
184 throw (RuntimeException) t;
185 } else if (t instanceof Error) {
188 throw new RuntimeException(t);
193 private CommitStatus secondPhaseCommit(ConfigTransactionControllerInternal configTransactionController,
194 CommitInfo commitInfo) {
196 // close instances which were destroyed by the user, including
197 // (hopefully) runtime beans
198 for (DestroyedModule toBeDestroyed : commitInfo
199 .getDestroyedFromPreviousTransactions()) {
200 toBeDestroyed.close(); // closes instance (which should close
201 // runtime jmx registrator),
202 // also closes osgi registration and ModuleJMXRegistrator
204 currentConfig.remove(toBeDestroyed.getIdentifier());
207 // set RuntimeBeanRegistrators on beans implementing
208 // RuntimeBeanRegistratorAwareModule, each module
209 // should have exactly one runtime jmx registrator.
210 Map<ModuleIdentifier, RootRuntimeBeanRegistratorImpl> runtimeRegistrators = new HashMap<>();
211 for (ModuleInternalTransactionalInfo entry : commitInfo.getCommitted()
213 RootRuntimeBeanRegistratorImpl runtimeBeanRegistrator;
214 if (entry.hasOldModule() == false) {
215 runtimeBeanRegistrator = baseJMXRegistrator
216 .createRuntimeBeanRegistrator(entry.getName());
218 // reuse old JMX registrator
219 runtimeBeanRegistrator = entry.getOldInternalInfo()
220 .getRuntimeBeanRegistrator();
222 // set runtime jmx registrator if required
223 Module module = entry.getModule();
224 if (module instanceof RuntimeBeanRegistratorAwareModule) {
225 ((RuntimeBeanRegistratorAwareModule) module)
226 .setRuntimeBeanRegistrator(runtimeBeanRegistrator);
228 // save it to info so it is accessible afterwards
229 runtimeRegistrators.put(entry.getName(), runtimeBeanRegistrator);
232 // can register runtime beans
233 List<ModuleIdentifier> orderedModuleIdentifiers = configTransactionController
234 .secondPhaseCommit();
236 // copy configuration to read only mode
237 List<ObjectName> newInstances = new LinkedList<>();
238 List<ObjectName> reusedInstances = new LinkedList<>();
239 List<ObjectName> recreatedInstances = new LinkedList<>();
241 Map<Module, ModuleInternalInfo> newConfigEntries = new HashMap<>();
244 for (ModuleIdentifier moduleIdentifier : orderedModuleIdentifiers) {
245 ModuleInternalTransactionalInfo entry = commitInfo.getCommitted()
246 .get(moduleIdentifier);
248 throw new NullPointerException("Module not found "
250 Module module = entry.getModule();
251 ObjectName primaryReadOnlyON = ObjectNameUtil
252 .createReadOnlyModuleON(moduleIdentifier);
254 // determine if current instance was recreated or reused or is new
256 // rules for closing resources:
257 // osgi registration - will be (re)created every time, so it needs
259 // module jmx registration - will be (re)created every time, needs
261 // runtime jmx registration - should be taken care of by module
263 // instance - is closed only if it was destroyed
264 ModuleJMXRegistrator newModuleJMXRegistrator = baseJMXRegistrator
265 .createModuleJMXRegistrator();
267 if (entry.hasOldModule()) {
268 ModuleInternalInfo oldInternalInfo = entry.getOldInternalInfo();
269 DynamicReadableWrapper oldReadableConfigBean = oldInternalInfo
270 .getReadableModule();
271 currentConfig.remove(entry.getName());
273 // test if old instance == new instance
274 if (oldReadableConfigBean.getInstance().equals(
275 module.getInstance())) {
276 // reused old instance:
277 // wrap in readable dynamic mbean
278 reusedInstances.add(primaryReadOnlyON);
280 // recreated instance:
281 // it is responsibility of module to call the old instance -
282 // we just need to unregister configbean
283 recreatedInstances.add(primaryReadOnlyON);
285 // close old osgi registration in any case
286 oldInternalInfo.getOsgiRegistration().close();
287 // close old module jmx registrator
288 oldInternalInfo.getModuleJMXRegistrator().close();
291 // wrap in readable dynamic mbean
292 newInstances.add(primaryReadOnlyON);
295 DynamicReadableWrapper newReadableConfigBean = new DynamicReadableWrapper(
296 module, module.getInstance(), moduleIdentifier,
297 registryMBeanServer, configMBeanServer);
301 newModuleJMXRegistrator.registerMBean(newReadableConfigBean,
303 } catch (InstanceAlreadyExistsException e) {
304 throw new IllegalStateException(e);
308 OsgiRegistration osgiRegistration = beanToOsgiServiceManager
309 .registerToOsgi(module.getClass(),
310 newReadableConfigBean.getInstance(),
313 RootRuntimeBeanRegistratorImpl runtimeBeanRegistrator = runtimeRegistrators
314 .get(entry.getName());
315 ModuleInternalInfo newInfo = new ModuleInternalInfo(
316 entry.getName(), newReadableConfigBean, osgiRegistration,
317 runtimeBeanRegistrator, newModuleJMXRegistrator,
320 newConfigEntries.put(module, newInfo);
323 currentConfig.addAll(newConfigEntries.values());
326 version = configTransactionController.getVersion();
327 return new CommitStatus(newInstances, reusedInstances,
335 public synchronized List<ObjectName> getOpenConfigs() {
336 Map<String, ConfigTransactionControllerInternal> transactions = transactionsHolder
337 .getCurrentTransactions();
338 List<ObjectName> result = new ArrayList<>(transactions.size());
339 for (ConfigTransactionControllerInternal configTransactionController : transactions
341 result.add(configTransactionController.getControllerObjectName());
347 * Abort open transactions and unregister read only modules. Since this
348 * class is not responsible for registering itself under
349 * {@link ConfigRegistryMXBean#OBJECT_NAME}, it will not unregister itself
353 public synchronized void close() {
354 // abort transactions
355 Map<String, ConfigTransactionControllerInternal> transactions = transactionsHolder
356 .getCurrentTransactions();
357 for (ConfigTransactionControllerInternal configTransactionController : transactions
360 configTransactionController.abortConfig();
361 } catch (RuntimeException e) {
362 logger.warn("Ignoring exception while aborting {}",
363 configTransactionController, e);
367 // destroy all live objects one after another in order of the dependency
369 List<DestroyedModule> destroyedModules = currentConfig
370 .getModulesToBeDestroyed();
371 for (DestroyedModule destroyedModule : destroyedModules) {
372 destroyedModule.close();
374 // unregister MBeans that failed to unregister properly
375 baseJMXRegistrator.close();
376 // remove jmx servers
377 MBeanServerFactory.releaseMBeanServer(registryMBeanServer);
378 MBeanServerFactory.releaseMBeanServer(transactionsMBeanServer);
386 public long getVersion() {
394 public Set<String> getAvailableModuleNames() {
395 return new HierarchicalConfigMBeanFactoriesHolder(
396 resolver.getAllFactories()).getModuleNames();
403 public boolean isHealthy() {
413 public Set<ObjectName> lookupConfigBeans() {
414 return lookupConfigBeans("*", "*");
421 public Set<ObjectName> lookupConfigBeans(String moduleName) {
422 return lookupConfigBeans(moduleName, "*");
429 public ObjectName lookupConfigBean(String moduleName, String instanceName)
430 throws InstanceNotFoundException {
431 return LookupBeansUtil.lookupConfigBean(this, moduleName, instanceName);
438 public Set<ObjectName> lookupConfigBeans(String moduleName,
439 String instanceName) {
440 ObjectName namePattern = ObjectNameUtil.createModulePattern(moduleName,
442 return baseJMXRegistrator.queryNames(namePattern, null);
449 public Set<ObjectName> lookupRuntimeBeans() {
450 return lookupRuntimeBeans("*", "*");
457 public Set<ObjectName> lookupRuntimeBeans(String moduleName,
458 String instanceName) {
459 if (moduleName == null)
461 if (instanceName == null)
463 ObjectName namePattern = ObjectNameUtil.createRuntimeBeanPattern(
464 moduleName, instanceName);
465 return baseJMXRegistrator.queryNames(namePattern, null);
471 * Holds currently running modules
475 private final Map<ModuleIdentifier, ModuleInternalInfo> currentConfig = new HashMap<>();
478 * Add all modules to the internal map. Also add service instance to OSGi
481 public void addAll(Collection<ModuleInternalInfo> configInfos) {
482 if (currentConfig.size() > 0) {
483 throw new IllegalStateException(
484 "Error - some config entries were not removed: "
487 for (ModuleInternalInfo configInfo : configInfos) {
492 private void add(ModuleInternalInfo configInfo) {
493 ModuleInternalInfo oldValue = currentConfig.put(configInfo.getName(),
495 if (oldValue != null) {
496 throw new IllegalStateException(
497 "Cannot overwrite module with same name:"
498 + configInfo.getName() + ":" + configInfo);
503 * Remove entry from current config.
505 public void remove(ModuleIdentifier name) {
506 ModuleInternalInfo removed = currentConfig.remove(name);
507 if (removed == null) {
508 throw new IllegalStateException(
509 "Cannot remove from ConfigHolder - name not found:" + name);
513 public Collection<ModuleInternalInfo> getEntries() {
514 return currentConfig.values();
517 public List<DestroyedModule> getModulesToBeDestroyed() {
518 List<DestroyedModule> result = new ArrayList<>();
519 for (ModuleInternalInfo moduleInternalInfo : getEntries()) {
520 result.add(moduleInternalInfo.toDestroyedModule());
522 Collections.sort(result);
528 * Holds Map<transactionName, transactionController> and purges it each time its
529 * content is requested.
532 class TransactionsHolder {
534 * This map keeps transaction names and
535 * {@link ConfigTransactionControllerInternal} instances, because platform
536 * MBeanServer transforms mbeans into another representation. Map is cleaned
537 * every time current transactions are requested.
540 @GuardedBy("ConfigRegistryImpl.this")
541 private final Map<String /* transactionName */, ConfigTransactionControllerInternal> transactions = new HashMap<>();
544 * Can only be called from within synchronized method.
546 public void add(String transactionName,
547 ConfigTransactionControllerInternal transactionController) {
548 Object oldValue = transactions.put(transactionName,
549 transactionController);
550 if (oldValue != null) {
551 throw new IllegalStateException(
552 "Error: two transactions with same name");
557 * Purges closed transactions from transactions map. Can only be called from
558 * within synchronized method. Calling this method more than once within the
559 * method can modify the resulting map that was obtained in previous calls.
561 * @return current view on transactions map.
563 public Map<String, ConfigTransactionControllerInternal> getCurrentTransactions() {
564 // first, remove closed transaction
565 for (Iterator<Entry<String, ConfigTransactionControllerInternal>> it = transactions
566 .entrySet().iterator(); it.hasNext();) {
567 Entry<String, ConfigTransactionControllerInternal> entry = it
569 if (entry.getValue().isClosed()) {
573 return Collections.unmodifiableMap(transactions);