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