BUG-865: TransactionCommitDeadlockException safety
[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.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;
21
22 /**
23 *
24 */
25 public final class DomInmemoryDataBrokerModule extends
26         org.opendaylight.controller.config.yang.md.sal.dom.impl.AbstractDomInmemoryDataBrokerModule {
27
28     private static final String JMX_BEAN_TYPE = "DOMDataBroker";
29
30     public DomInmemoryDataBrokerModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier,
31             final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
32         super(identifier, dependencyResolver);
33     }
34
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) {
38
39         super(identifier, dependencyResolver, oldModule, oldInstance);
40     }
41
42     @Override
43     protected void customValidation() {
44         // Add custom validation for module attributes here.
45     }
46
47     @Override
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());
54         }
55
56         DOMStore configStore = getConfigDataStoreDependency();
57         if(configStore == null){
58            //we will default to InMemoryDOMDataStore creation
59            configStore = InMemoryDOMDataStoreFactory.create("DOM-CFG", getSchemaServiceDependency());
60         }
61         ImmutableMap<LogicalDatastoreType, DOMStore> datastores = ImmutableMap
62                 .<LogicalDatastoreType, DOMStore> builder().put(LogicalDatastoreType.OPERATIONAL, operStore)
63                 .put(LogicalDatastoreType.CONFIGURATION, configStore).build();
64
65         /*
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.
72          */
73         ExecutorService commitExecutor = SpecialExecutors.newBoundedSingleThreadExecutor(
74                 getMaxDataBrokerCommitQueueSize(), "WriteTxCommit");
75
76         /*
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.
83          */
84         ExecutorService listenableFutureExecutor = SpecialExecutors.newBlockingBoundedCachedThreadPool(
85                 getMaxDataBrokerFutureCallbackPoolSize(), getMaxDataBrokerFutureCallbackQueueSize(),
86                 "CommitFutures");
87
88         DOMDataBrokerImpl newDataBroker = new DOMDataBrokerImpl(datastores,
89                 new DeadlockDetectingListeningExecutorService(commitExecutor,
90                     TransactionCommitDeadlockException.DEADLOCK_EXCEPTION_SUPPLIER,
91                     listenableFutureExecutor));
92
93         final CommitStatsMXBeanImpl commitStatsMXBean = new CommitStatsMXBeanImpl(
94                 newDataBroker.getCommitStatsTracker(), JMX_BEAN_TYPE);
95         commitStatsMXBean.registerMBean();
96
97         final ThreadExecutorStatsMXBeanImpl commitExecutorStatsMXBean =
98                 new ThreadExecutorStatsMXBeanImpl(commitExecutor, "CommitExecutorStats",
99                         JMX_BEAN_TYPE, null);
100         commitExecutorStatsMXBean.registerMBean();
101
102         final ThreadExecutorStatsMXBeanImpl commitFutureStatsMXBean =
103                 new ThreadExecutorStatsMXBeanImpl(listenableFutureExecutor,
104                         "CommitFutureExecutorStats", JMX_BEAN_TYPE, null);
105         commitFutureStatsMXBean.registerMBean();
106
107         newDataBroker.setCloseable(new AutoCloseable() {
108             @Override
109             public void close() {
110                 commitStatsMXBean.unregisterMBean();
111                 commitExecutorStatsMXBean.unregisterMBean();
112                 commitFutureStatsMXBean.unregisterMBean();
113             }
114         });
115
116         return newDataBroker;
117     }
118 }