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.config.yang.md.sal.dom.impl;
10 import java.util.EnumMap;
12 import java.util.concurrent.ExecutorService;
13 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
14 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitDeadlockException;
15 import org.opendaylight.controller.md.sal.common.util.jmx.AbstractMXBean;
16 import org.opendaylight.controller.md.sal.common.util.jmx.ThreadExecutorStatsMXBeanImpl;
17 import org.opendaylight.controller.md.sal.dom.broker.impl.DOMDataBrokerImpl;
18 import org.opendaylight.controller.md.sal.dom.broker.impl.jmx.CommitStatsMXBeanImpl;
19 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStoreFactory;
20 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
21 import org.opendaylight.yangtools.util.concurrent.DeadlockDetectingListeningExecutorService;
22 import org.opendaylight.yangtools.util.concurrent.SpecialExecutors;
27 public final class DomInmemoryDataBrokerModule extends
28 org.opendaylight.controller.config.yang.md.sal.dom.impl.AbstractDomInmemoryDataBrokerModule {
30 private static final String JMX_BEAN_TYPE = "DOMDataBroker";
32 public DomInmemoryDataBrokerModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier,
33 final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
34 super(identifier, dependencyResolver);
37 public DomInmemoryDataBrokerModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier,
38 final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver,
39 final DomInmemoryDataBrokerModule oldModule, final java.lang.AutoCloseable oldInstance) {
41 super(identifier, dependencyResolver, oldModule, oldInstance);
45 protected void customValidation() {
46 // Add custom validation for module attributes here.
50 public java.lang.AutoCloseable createInstance() {
51 //Initializing Operational DOM DataStore defaulting to InMemoryDOMDataStore if one is not configured
52 DOMStore operStore = getOperationalDataStoreDependency();
53 if(operStore == null){
54 //we will default to InMemoryDOMDataStore creation
55 operStore = InMemoryDOMDataStoreFactory.create("DOM-OPER", getSchemaServiceDependency());
58 DOMStore configStore = getConfigDataStoreDependency();
59 if(configStore == null){
60 //we will default to InMemoryDOMDataStore creation
61 configStore = InMemoryDOMDataStoreFactory.create("DOM-CFG", getSchemaServiceDependency());
64 final Map<LogicalDatastoreType, DOMStore> datastores = new EnumMap<>(LogicalDatastoreType.class);
65 datastores.put(LogicalDatastoreType.OPERATIONAL, operStore);
66 datastores.put(LogicalDatastoreType.CONFIGURATION, configStore);
69 * We use a single-threaded executor for commits with a bounded queue capacity. If the
70 * queue capacity is reached, subsequent commit tasks will be rejected and the commits will
71 * fail. This is done to relieve back pressure. This should be an extreme scenario - either
72 * there's deadlock(s) somewhere and the controller is unstable or some rogue component is
73 * continuously hammering commits too fast or the controller is just over-capacity for the
74 * system it's running on.
76 ExecutorService commitExecutor = SpecialExecutors.newBoundedSingleThreadExecutor(
77 getMaxDataBrokerCommitQueueSize(), "WriteTxCommit");
80 * We use an executor for commit ListenableFuture callbacks that favors reusing available
81 * threads over creating new threads at the expense of execution time. The assumption is
82 * that most ListenableFuture callbacks won't execute a lot of business logic where we want
83 * it to run quicker - many callbacks will likely just handle error conditions and do
84 * nothing on success. The executor queue capacity is bounded and, if the capacity is
85 * reached, subsequent submitted tasks will block the caller.
87 ExecutorService listenableFutureExecutor = SpecialExecutors.newBlockingBoundedCachedThreadPool(
88 getMaxDataBrokerFutureCallbackPoolSize(), getMaxDataBrokerFutureCallbackQueueSize(),
91 DOMDataBrokerImpl newDataBroker = new DOMDataBrokerImpl(datastores,
92 new DeadlockDetectingListeningExecutorService(commitExecutor,
93 TransactionCommitDeadlockException.DEADLOCK_EXCEPTION_SUPPLIER,
94 listenableFutureExecutor));
96 final CommitStatsMXBeanImpl commitStatsMXBean = new CommitStatsMXBeanImpl(
97 newDataBroker.getCommitStatsTracker(), JMX_BEAN_TYPE);
98 commitStatsMXBean.registerMBean();
100 final AbstractMXBean commitExecutorStatsMXBean =
101 ThreadExecutorStatsMXBeanImpl.create(commitExecutor, "CommitExecutorStats",
102 JMX_BEAN_TYPE, null);
103 final AbstractMXBean commitFutureStatsMXBean =
104 ThreadExecutorStatsMXBeanImpl.create(listenableFutureExecutor,
105 "CommitFutureExecutorStats", JMX_BEAN_TYPE, null);
107 newDataBroker.setCloseable(new AutoCloseable() {
109 public void close() {
110 commitStatsMXBean.unregisterMBean();
111 if (commitExecutorStatsMXBean != null) {
112 commitExecutorStatsMXBean.unregisterMBean();
114 if (commitFutureStatsMXBean != null) {
115 commitFutureStatsMXBean.unregisterMBean();
120 return newDataBroker;