05726e1d1e175a5f895b17e9c8b2a3539d97a0ed
[controller.git] / opendaylight / md-sal / sal-dom-broker-config / src / main / java / org / opendaylight / controller / config / yang / md / sal / dom / impl / DomInmemoryDataBrokerModule.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.config.yang.md.sal.dom.impl;
9
10 import com.google.common.collect.Lists;
11 import java.util.EnumMap;
12 import java.util.List;
13 import java.util.Map;
14 import java.util.concurrent.ExecutorService;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitDeadlockException;
17 import org.opendaylight.controller.md.sal.common.util.jmx.AbstractMXBean;
18 import org.opendaylight.controller.md.sal.common.util.jmx.ThreadExecutorStatsMXBeanImpl;
19 import org.opendaylight.controller.md.sal.dom.broker.impl.SerializedDOMDataBroker;
20 import org.opendaylight.controller.md.sal.dom.broker.impl.jmx.CommitStatsMXBeanImpl;
21 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStoreFactory;
22 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
23 import org.opendaylight.yangtools.util.DurationStatisticsTracker;
24 import org.opendaylight.yangtools.util.concurrent.DeadlockDetectingListeningExecutorService;
25 import org.opendaylight.yangtools.util.concurrent.SpecialExecutors;
26
27 /**
28  * Deprecated.
29  *
30  * @deprecated Replaced by the clustered data store.
31  */
32 @Deprecated
33 public final class DomInmemoryDataBrokerModule extends
34         org.opendaylight.controller.config.yang.md.sal.dom.impl.AbstractDomInmemoryDataBrokerModule {
35
36     private static final String JMX_BEAN_TYPE = "DOMDataBroker";
37
38     public DomInmemoryDataBrokerModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier,
39             final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
40         super(identifier, dependencyResolver);
41     }
42
43     public DomInmemoryDataBrokerModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier,
44             final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver,
45             final DomInmemoryDataBrokerModule oldModule, final java.lang.AutoCloseable oldInstance) {
46
47         super(identifier, dependencyResolver, oldModule, oldInstance);
48     }
49
50     @Override
51     protected void customValidation() {
52         // Add custom validation for module attributes here.
53     }
54
55     @Override
56     public java.lang.AutoCloseable createInstance() {
57         //Initializing Operational DOM DataStore defaulting to InMemoryDOMDataStore if one is not configured
58         DOMStore operStore =  getOperationalDataStoreDependency();
59         if (operStore == null) {
60             // we will default to InMemoryDOMDataStore creation
61             operStore = InMemoryDOMDataStoreFactory.create("DOM-OPER", getSchemaServiceDependency());
62         }
63
64         DOMStore configStore = getConfigDataStoreDependency();
65         if (configStore == null) {
66             // we will default to InMemoryDOMDataStore creation
67             configStore = InMemoryDOMDataStoreFactory.create("DOM-CFG", getSchemaServiceDependency());
68         }
69
70         final Map<LogicalDatastoreType, DOMStore> datastores = new EnumMap<>(LogicalDatastoreType.class);
71         datastores.put(LogicalDatastoreType.OPERATIONAL, operStore);
72         datastores.put(LogicalDatastoreType.CONFIGURATION, configStore);
73
74         /*
75          * We use an executor for commit ListenableFuture callbacks that favors reusing available
76          * threads over creating new threads at the expense of execution time. The assumption is
77          * that most ListenableFuture callbacks won't execute a lot of business logic where we want
78          * it to run quicker - many callbacks will likely just handle error conditions and do
79          * nothing on success. The executor queue capacity is bounded and, if the capacity is
80          * reached, subsequent submitted tasks will block the caller.
81          */
82         ExecutorService listenableFutureExecutor = SpecialExecutors.newBlockingBoundedCachedThreadPool(
83                 getMaxDataBrokerFutureCallbackPoolSize(), getMaxDataBrokerFutureCallbackQueueSize(),
84                 "CommitFutures", SerializedDOMDataBroker.class);
85
86         final List<AbstractMXBean> mBeans = Lists.newArrayList();
87         final DurationStatisticsTracker commitStatsTracker;
88
89         /*
90          * We use a single-threaded executor for commits with a bounded queue capacity. If the
91          * queue capacity is reached, subsequent commit tasks will be rejected and the commits will
92          * fail. This is done to relieve back pressure. This should be an extreme scenario - either
93          * there's deadlock(s) somewhere and the controller is unstable or some rogue component is
94          * continuously hammering commits too fast or the controller is just over-capacity for the
95          * system it's running on.
96          */
97         ExecutorService commitExecutor = SpecialExecutors.newBoundedSingleThreadExecutor(
98             getMaxDataBrokerCommitQueueSize(), "WriteTxCommit", SerializedDOMDataBroker.class);
99
100         SerializedDOMDataBroker sdb = new SerializedDOMDataBroker(datastores,
101             new DeadlockDetectingListeningExecutorService(commitExecutor,
102                 TransactionCommitDeadlockException.DEADLOCK_EXCEPTION_SUPPLIER,
103                 listenableFutureExecutor));
104         commitStatsTracker = sdb.getCommitStatsTracker();
105
106         final AbstractMXBean commitExecutorStatsMXBean =
107                 ThreadExecutorStatsMXBeanImpl.create(commitExecutor, "CommitExecutorStats",
108                     JMX_BEAN_TYPE, null);
109         if (commitExecutorStatsMXBean != null) {
110             mBeans.add(commitExecutorStatsMXBean);
111         }
112
113         if (commitStatsTracker != null) {
114             final CommitStatsMXBeanImpl commitStatsMXBean = new CommitStatsMXBeanImpl(
115                     commitStatsTracker, JMX_BEAN_TYPE);
116             commitStatsMXBean.registerMBean();
117             mBeans.add(commitStatsMXBean);
118         }
119
120         final AbstractMXBean commitFutureStatsMXBean =
121                 ThreadExecutorStatsMXBeanImpl.create(listenableFutureExecutor,
122                         "CommitFutureExecutorStats", JMX_BEAN_TYPE, null);
123         if (commitFutureStatsMXBean != null) {
124             mBeans.add(commitFutureStatsMXBean);
125         }
126
127         sdb.setCloseable(() -> mBeans.forEach(AbstractMXBean::unregisterMBean));
128
129         return sdb;
130     }
131 }