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