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.ObjectNameUtil;
16 import org.opendaylight.controller.config.manager.impl.dynamicmbean.DynamicReadableWrapper;
17 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HierarchicalConfigMBeanFactoriesHolder;
18 import org.opendaylight.controller.config.manager.impl.factoriesresolver.ModuleFactoriesResolver;
19 import org.opendaylight.controller.config.manager.impl.jmx.BaseJMXRegistrator;
20 import org.opendaylight.controller.config.manager.impl.jmx.ModuleJMXRegistrator;
21 import org.opendaylight.controller.config.manager.impl.jmx.RootRuntimeBeanRegistratorImpl;
22 import org.opendaylight.controller.config.manager.impl.jmx.TransactionJMXRegistrator;
23 import org.opendaylight.controller.config.manager.impl.osgi.BeanToOsgiServiceManager;
24 import org.opendaylight.controller.config.manager.impl.osgi.BeanToOsgiServiceManager.OsgiRegistration;
25 import org.opendaylight.controller.config.manager.impl.util.LookupBeansUtil;
26 import org.opendaylight.controller.config.spi.Module;
27 import org.opendaylight.controller.config.spi.ModuleFactory;
28 import org.osgi.framework.BundleContext;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 import javax.annotation.concurrent.GuardedBy;
33 import javax.annotation.concurrent.NotThreadSafe;
34 import javax.annotation.concurrent.ThreadSafe;
35 import javax.management.*;
37 import java.util.Map.Entry;
40 * Singleton that is responsible for creating and committing Config
41 * Transactions. It is registered in Platform MBean Server.
44 public class ConfigRegistryImpl implements AutoCloseable, ConfigRegistryImplMXBean {
45 private static final Logger logger = LoggerFactory.getLogger(ConfigRegistryImpl.class);
47 private final ModuleFactoriesResolver resolver;
48 private final MBeanServer configMBeanServer;
51 private final BundleContext bundleContext;
54 private long version = 0;
56 private long versionCounter = 0;
59 * Contains current configuration in form of {moduleName:{instanceName,read
60 * only module}} for copying state to new transaction. Each running module
61 * is present just once, no matter how many interfaces it exposes.
64 private final ConfigHolder currentConfig = new ConfigHolder();
67 * Will return true unless there was a transaction that succeeded during
68 * validation but failed in second phase of commit. In this case the server
69 * is unstable and its state is undefined.
72 private boolean isHealthy = true;
75 * Holds Map<transactionName, transactionController> and purges it each time
76 * its content is requested.
79 private final TransactionsHolder transactionsHolder = new TransactionsHolder();
81 private final BaseJMXRegistrator baseJMXRegistrator;
83 private final BeanToOsgiServiceManager beanToOsgiServiceManager;
85 // internal jmx server for read only beans
86 private final MBeanServer registryMBeanServer;
87 // internal jmx server shared by all transactions
88 private final MBeanServer transactionsMBeanServer;
91 private List<ModuleFactory> lastListOfFactories = Collections.emptyList();
94 public ConfigRegistryImpl(ModuleFactoriesResolver resolver,
95 BundleContext bundleContext, MBeanServer configMBeanServer) {
96 this(resolver, bundleContext, configMBeanServer,
97 new BaseJMXRegistrator(configMBeanServer));
101 public ConfigRegistryImpl(ModuleFactoriesResolver resolver,
102 BundleContext bundleContext, MBeanServer configMBeanServer,
103 BaseJMXRegistrator baseJMXRegistrator) {
104 this.resolver = resolver;
105 this.beanToOsgiServiceManager = new BeanToOsgiServiceManager(
107 this.bundleContext = bundleContext;
108 this.configMBeanServer = configMBeanServer;
109 this.baseJMXRegistrator = baseJMXRegistrator;
110 this.registryMBeanServer = MBeanServerFactory
111 .createMBeanServer("ConfigRegistry" + configMBeanServer.getDefaultDomain());
112 this.transactionsMBeanServer = MBeanServerFactory
113 .createMBeanServer("ConfigTransactions" + configMBeanServer.getDefaultDomain());
117 * Create new {@link ConfigTransactionControllerImpl }
120 public synchronized ObjectName beginConfig() {
121 return beginConfigInternal().getControllerObjectName();
124 private synchronized ConfigTransactionControllerInternal beginConfigInternal() {
126 String transactionName = "ConfigTransaction-" + version + "-" + versionCounter;
127 TransactionJMXRegistrator transactionRegistrator = baseJMXRegistrator
128 .createTransactionJMXRegistrator(transactionName);
129 List<ModuleFactory> allCurrentFactories = Collections.unmodifiableList(resolver.getAllFactories());
130 ConfigTransactionControllerInternal transactionController = new ConfigTransactionControllerImpl(
131 transactionName, transactionRegistrator, version,
132 versionCounter, allCurrentFactories, transactionsMBeanServer, configMBeanServer, bundleContext);
134 transactionRegistrator.registerMBean(transactionController, transactionController.getControllerObjectName());
135 } catch (InstanceAlreadyExistsException e) {
136 throw new IllegalStateException(e);
139 transactionController.copyExistingModulesAndProcessFactoryDiff(currentConfig.getEntries(), lastListOfFactories);
141 transactionsHolder.add(transactionName, transactionController);
142 return transactionController;
149 public synchronized CommitStatus commitConfig(ObjectName transactionControllerON)
150 throws ConflictingVersionException, ValidationException {
151 final String transactionName = ObjectNameUtil
152 .getTransactionName(transactionControllerON);
153 logger.info("About to commit {}. Current parentVersion: {}, versionCounter {}", transactionName, version, versionCounter);
155 // find ConfigTransactionController
156 Map<String, ConfigTransactionControllerInternal> transactions = transactionsHolder.getCurrentTransactions();
157 ConfigTransactionControllerInternal configTransactionController = transactions.get(transactionName);
158 if (configTransactionController == null) {
159 throw new IllegalArgumentException(String.format(
160 "Transaction with name '%s' not found", transactionName));
162 // check optimistic lock
163 if (version != configTransactionController.getParentVersion()) {
164 throw new ConflictingVersionException(
166 "Optimistic lock failed. Expected parent version %d, was %d",
168 configTransactionController.getParentVersion()));
170 // optimistic lock ok
172 CommitInfo commitInfo = configTransactionController.validateBeforeCommitAndLockTransaction();
173 lastListOfFactories = Collections.unmodifiableList(configTransactionController.getCurrentlyRegisteredFactories());
174 // non recoverable from here:
176 return secondPhaseCommit(
177 configTransactionController, commitInfo);
178 } catch (Throwable t) { // some libs throw Errors: e.g.
179 // javax.xml.ws.spi.FactoryFinder$ConfigurationError
181 logger.error("Configuration Transaction failed on 2PC, server is unhealthy", t);
182 if (t instanceof RuntimeException) {
183 throw (RuntimeException) t;
184 } else if (t instanceof Error) {
187 throw new RuntimeException(t);
192 private CommitStatus secondPhaseCommit(ConfigTransactionControllerInternal configTransactionController,
193 CommitInfo commitInfo) {
195 // close instances which were destroyed by the user, including
196 // (hopefully) runtime beans
197 for (DestroyedModule toBeDestroyed : commitInfo
198 .getDestroyedFromPreviousTransactions()) {
199 toBeDestroyed.close(); // closes instance (which should close
200 // runtime jmx registrator),
201 // also closes osgi registration and ModuleJMXRegistrator
203 currentConfig.remove(toBeDestroyed.getIdentifier());
206 // set RuntimeBeanRegistrators on beans implementing
207 // RuntimeBeanRegistratorAwareModule, each module
208 // should have exactly one runtime jmx registrator.
209 Map<ModuleIdentifier, RootRuntimeBeanRegistratorImpl> runtimeRegistrators = new HashMap<>();
210 for (ModuleInternalTransactionalInfo entry : commitInfo.getCommitted()
212 RootRuntimeBeanRegistratorImpl runtimeBeanRegistrator;
213 if (entry.hasOldModule() == false) {
214 runtimeBeanRegistrator = baseJMXRegistrator
215 .createRuntimeBeanRegistrator(entry.getName());
217 // reuse old JMX registrator
218 runtimeBeanRegistrator = entry.getOldInternalInfo()
219 .getRuntimeBeanRegistrator();
221 // set runtime jmx registrator if required
222 Module module = entry.getModule();
223 if (module instanceof RuntimeBeanRegistratorAwareModule) {
224 ((RuntimeBeanRegistratorAwareModule) module)
225 .setRuntimeBeanRegistrator(runtimeBeanRegistrator);
227 // save it to info so it is accessible afterwards
228 runtimeRegistrators.put(entry.getName(), runtimeBeanRegistrator);
231 // can register runtime beans
232 List<ModuleIdentifier> orderedModuleIdentifiers = configTransactionController
233 .secondPhaseCommit();
235 // copy configuration to read only mode
236 List<ObjectName> newInstances = new LinkedList<>();
237 List<ObjectName> reusedInstances = new LinkedList<>();
238 List<ObjectName> recreatedInstances = new LinkedList<>();
240 Map<Module, ModuleInternalInfo> newConfigEntries = new HashMap<>();
243 for (ModuleIdentifier moduleIdentifier : orderedModuleIdentifiers) {
244 ModuleInternalTransactionalInfo entry = commitInfo.getCommitted()
245 .get(moduleIdentifier);
247 throw new NullPointerException("Module not found "
249 Module module = entry.getModule();
250 ObjectName primaryReadOnlyON = ObjectNameUtil
251 .createReadOnlyModuleON(moduleIdentifier);
253 // determine if current instance was recreated or reused or is new
255 // rules for closing resources:
256 // osgi registration - will be (re)created every time, so it needs
258 // module jmx registration - will be (re)created every time, needs
260 // runtime jmx registration - should be taken care of by module
262 // instance - is closed only if it was destroyed
263 ModuleJMXRegistrator newModuleJMXRegistrator = baseJMXRegistrator
264 .createModuleJMXRegistrator();
266 if (entry.hasOldModule()) {
267 ModuleInternalInfo oldInternalInfo = entry.getOldInternalInfo();
268 DynamicReadableWrapper oldReadableConfigBean = oldInternalInfo
269 .getReadableModule();
270 currentConfig.remove(entry.getName());
272 // test if old instance == new instance
273 if (oldReadableConfigBean.getInstance().equals(
274 module.getInstance())) {
275 // reused old instance:
276 // wrap in readable dynamic mbean
277 reusedInstances.add(primaryReadOnlyON);
279 // recreated instance:
280 // it is responsibility of module to call the old instance -
281 // we just need to unregister configbean
282 recreatedInstances.add(primaryReadOnlyON);
284 // close old osgi registration in any case
285 oldInternalInfo.getOsgiRegistration().close();
286 // close old module jmx registrator
287 oldInternalInfo.getModuleJMXRegistrator().close();
290 // wrap in readable dynamic mbean
291 newInstances.add(primaryReadOnlyON);
294 DynamicReadableWrapper newReadableConfigBean = new DynamicReadableWrapper(
295 module, module.getInstance(), moduleIdentifier,
296 registryMBeanServer, configMBeanServer);
300 newModuleJMXRegistrator.registerMBean(newReadableConfigBean,
302 } catch (InstanceAlreadyExistsException e) {
303 throw new IllegalStateException(e);
307 OsgiRegistration osgiRegistration = beanToOsgiServiceManager
308 .registerToOsgi(module.getClass(),
309 newReadableConfigBean.getInstance(),
312 RootRuntimeBeanRegistratorImpl runtimeBeanRegistrator = runtimeRegistrators
313 .get(entry.getName());
314 ModuleInternalInfo newInfo = new ModuleInternalInfo(
315 entry.getName(), newReadableConfigBean, osgiRegistration,
316 runtimeBeanRegistrator, newModuleJMXRegistrator,
319 newConfigEntries.put(module, newInfo);
322 currentConfig.addAll(newConfigEntries.values());
325 version = configTransactionController.getVersion();
326 return new CommitStatus(newInstances, reusedInstances,
334 public synchronized List<ObjectName> getOpenConfigs() {
335 Map<String, ConfigTransactionControllerInternal> transactions = transactionsHolder
336 .getCurrentTransactions();
337 List<ObjectName> result = new ArrayList<>(transactions.size());
338 for (ConfigTransactionControllerInternal configTransactionController : transactions
340 result.add(configTransactionController.getControllerObjectName());
346 * Abort open transactions and unregister read only modules. Since this
347 * class is not responsible for registering itself under
348 * {@link ConfigRegistryMXBean#OBJECT_NAME}, it will not unregister itself
352 public synchronized void close() {
353 // abort transactions
354 Map<String, ConfigTransactionControllerInternal> transactions = transactionsHolder
355 .getCurrentTransactions();
356 for (ConfigTransactionControllerInternal configTransactionController : transactions
359 configTransactionController.abortConfig();
360 } catch (RuntimeException e) {
361 logger.warn("Ignoring exception while aborting {}",
362 configTransactionController, e);
366 // destroy all live objects one after another in order of the dependency
368 List<DestroyedModule> destroyedModules = currentConfig
369 .getModulesToBeDestroyed();
370 for (DestroyedModule destroyedModule : destroyedModules) {
371 destroyedModule.close();
373 // unregister MBeans that failed to unregister properly
374 baseJMXRegistrator.close();
375 // remove jmx servers
376 MBeanServerFactory.releaseMBeanServer(registryMBeanServer);
377 MBeanServerFactory.releaseMBeanServer(transactionsMBeanServer);
385 public long getVersion() {
393 public Set<String> getAvailableModuleNames() {
394 return new HierarchicalConfigMBeanFactoriesHolder(
395 resolver.getAllFactories()).getModuleNames();
402 public boolean isHealthy() {
412 public Set<ObjectName> lookupConfigBeans() {
413 return lookupConfigBeans("*", "*");
420 public Set<ObjectName> lookupConfigBeans(String moduleName) {
421 return lookupConfigBeans(moduleName, "*");
428 public ObjectName lookupConfigBean(String moduleName, String instanceName)
429 throws InstanceNotFoundException {
430 return LookupBeansUtil.lookupConfigBean(this, moduleName, instanceName);
437 public Set<ObjectName> lookupConfigBeans(String moduleName,
438 String instanceName) {
439 ObjectName namePattern = ObjectNameUtil.createModulePattern(moduleName,
441 return baseJMXRegistrator.queryNames(namePattern, null);
448 public Set<ObjectName> lookupRuntimeBeans() {
449 return lookupRuntimeBeans("*", "*");
456 public Set<ObjectName> lookupRuntimeBeans(String moduleName,
457 String instanceName) {
458 if (moduleName == null)
460 if (instanceName == null)
462 ObjectName namePattern = ObjectNameUtil.createRuntimeBeanPattern(
463 moduleName, instanceName);
464 return baseJMXRegistrator.queryNames(namePattern, null);
470 * Holds currently running modules
474 private final Map<ModuleIdentifier, ModuleInternalInfo> currentConfig = new HashMap<>();
477 * Add all modules to the internal map. Also add service instance to OSGi
480 public void addAll(Collection<ModuleInternalInfo> configInfos) {
481 if (currentConfig.size() > 0) {
482 throw new IllegalStateException(
483 "Error - some config entries were not removed: "
486 for (ModuleInternalInfo configInfo : configInfos) {
491 private void add(ModuleInternalInfo configInfo) {
492 ModuleInternalInfo oldValue = currentConfig.put(configInfo.getName(),
494 if (oldValue != null) {
495 throw new IllegalStateException(
496 "Cannot overwrite module with same name:"
497 + configInfo.getName() + ":" + configInfo);
502 * Remove entry from current config.
504 public void remove(ModuleIdentifier name) {
505 ModuleInternalInfo removed = currentConfig.remove(name);
506 if (removed == null) {
507 throw new IllegalStateException(
508 "Cannot remove from ConfigHolder - name not found:" + name);
512 public Collection<ModuleInternalInfo> getEntries() {
513 return currentConfig.values();
516 public List<DestroyedModule> getModulesToBeDestroyed() {
517 List<DestroyedModule> result = new ArrayList<>();
518 for (ModuleInternalInfo moduleInternalInfo : getEntries()) {
519 result.add(moduleInternalInfo.toDestroyedModule());
521 Collections.sort(result);
527 * Holds Map<transactionName, transactionController> and purges it each time its
528 * content is requested.
531 class TransactionsHolder {
533 * This map keeps transaction names and
534 * {@link ConfigTransactionControllerInternal} instances, because platform
535 * MBeanServer transforms mbeans into another representation. Map is cleaned
536 * every time current transactions are requested.
539 @GuardedBy("ConfigRegistryImpl.this")
540 private final Map<String /* transactionName */, ConfigTransactionControllerInternal> transactions = new HashMap<>();
543 * Can only be called from within synchronized method.
545 public void add(String transactionName,
546 ConfigTransactionControllerInternal transactionController) {
547 Object oldValue = transactions.put(transactionName,
548 transactionController);
549 if (oldValue != null) {
550 throw new IllegalStateException(
551 "Error: two transactions with same name");
556 * Purges closed transactions from transactions map. Can only be called from
557 * within synchronized method. Calling this method more than once within the
558 * method can modify the resulting map that was obtained in previous calls.
560 * @return current view on transactions map.
562 public Map<String, ConfigTransactionControllerInternal> getCurrentTransactions() {
563 // first, remove closed transaction
564 for (Iterator<Entry<String, ConfigTransactionControllerInternal>> it = transactions
565 .entrySet().iterator(); it.hasNext();) {
566 Entry<String, ConfigTransactionControllerInternal> entry = it
568 if (entry.getValue().isClosed()) {
572 return Collections.unmodifiableMap(transactions);