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