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