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