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
index 69b17ee3c48aeb5aae64dc8c4b02e36abdfce7a7..0841435785ebb70a30dc10acc24fa13c1d6ad3bf 100644 (file)
@@ -7,16 +7,17 @@
  */
 package org.opendaylight.controller.config.yang.md.sal.dom.impl;
 
-import java.util.concurrent.Executors;
-
+import com.google.common.collect.ImmutableMap;
+import java.util.concurrent.ExecutorService;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitDeadlockException;
+import org.opendaylight.controller.md.sal.common.util.jmx.ThreadExecutorStatsMXBeanImpl;
 import org.opendaylight.controller.md.sal.dom.broker.impl.DOMDataBrokerImpl;
-import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
+import org.opendaylight.controller.md.sal.dom.broker.impl.jmx.CommitStatsMXBeanImpl;
+import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStoreFactory;
 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
-
-import com.google.common.collect.ImmutableMap;
-import com.google.common.util.concurrent.ListeningExecutorService;
-import com.google.common.util.concurrent.MoreExecutors;
+import org.opendaylight.yangtools.util.concurrent.DeadlockDetectingListeningExecutorService;
+import org.opendaylight.yangtools.util.concurrent.SpecialExecutors;
 
 /**
 *
@@ -24,6 +25,8 @@ import com.google.common.util.concurrent.MoreExecutors;
 public final class DomInmemoryDataBrokerModule extends
         org.opendaylight.controller.config.yang.md.sal.dom.impl.AbstractDomInmemoryDataBrokerModule {
 
+    private static final String JMX_BEAN_TYPE = "DOMDataBroker";
+
     public DomInmemoryDataBrokerModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier,
             final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
         super(identifier, dependencyResolver);
@@ -43,28 +46,72 @@ public final class DomInmemoryDataBrokerModule extends
 
     @Override
     public java.lang.AutoCloseable createInstance() {
-        ListeningExecutorService storeExecutor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(2));
         //Initializing Operational DOM DataStore defaulting to InMemoryDOMDataStore if one is not configured
         DOMStore operStore =  getOperationalDataStoreDependency();
         if(operStore == null){
            //we will default to InMemoryDOMDataStore creation
-          operStore = new InMemoryDOMDataStore("DOM-OPER", storeExecutor);
-          //here we will register the SchemaContext listener
-          getSchemaServiceDependency().registerSchemaServiceListener((InMemoryDOMDataStore)operStore);
+          operStore = InMemoryDOMDataStoreFactory.create("DOM-OPER", getSchemaServiceDependency());
         }
 
         DOMStore configStore = getConfigDataStoreDependency();
         if(configStore == null){
            //we will default to InMemoryDOMDataStore creation
-           configStore = new InMemoryDOMDataStore("DOM-CFG", storeExecutor);
-          //here we will register the SchemaContext listener
-          getSchemaServiceDependency().registerSchemaServiceListener((InMemoryDOMDataStore)configStore);
+           configStore = InMemoryDOMDataStoreFactory.create("DOM-CFG", getSchemaServiceDependency());
         }
         ImmutableMap<LogicalDatastoreType, DOMStore> datastores = ImmutableMap
                 .<LogicalDatastoreType, DOMStore> builder().put(LogicalDatastoreType.OPERATIONAL, operStore)
                 .put(LogicalDatastoreType.CONFIGURATION, configStore).build();
 
-        DOMDataBrokerImpl newDataBroker = new DOMDataBrokerImpl(datastores, MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor()));
+        /*
+         * We use a single-threaded executor for commits with a bounded queue capacity. If the
+         * queue capacity is reached, subsequent commit tasks will be rejected and the commits will
+         * fail. This is done to relieve back pressure. This should be an extreme scenario - either
+         * there's deadlock(s) somewhere and the controller is unstable or some rogue component is
+         * continuously hammering commits too fast or the controller is just over-capacity for the
+         * system it's running on.
+         */
+        ExecutorService commitExecutor = SpecialExecutors.newBoundedSingleThreadExecutor(
+                getMaxDataBrokerCommitQueueSize(), "WriteTxCommit");
+
+        /*
+         * We use an executor for commit ListenableFuture callbacks that favors reusing available
+         * threads over creating new threads at the expense of execution time. The assumption is
+         * that most ListenableFuture callbacks won't execute a lot of business logic where we want
+         * it to run quicker - many callbacks will likely just handle error conditions and do
+         * nothing on success. The executor queue capacity is bounded and, if the capacity is
+         * reached, subsequent submitted tasks will block the caller.
+         */
+        ExecutorService listenableFutureExecutor = SpecialExecutors.newBlockingBoundedCachedThreadPool(
+                getMaxDataBrokerFutureCallbackPoolSize(), getMaxDataBrokerFutureCallbackQueueSize(),
+                "CommitFutures");
+
+        DOMDataBrokerImpl newDataBroker = new DOMDataBrokerImpl(datastores,
+                new DeadlockDetectingListeningExecutorService(commitExecutor,
+                    TransactionCommitDeadlockException.DEADLOCK_EXCEPTION_SUPPLIER,
+                    listenableFutureExecutor));
+
+        final CommitStatsMXBeanImpl commitStatsMXBean = new CommitStatsMXBeanImpl(
+                newDataBroker.getCommitStatsTracker(), JMX_BEAN_TYPE);
+        commitStatsMXBean.registerMBean();
+
+        final ThreadExecutorStatsMXBeanImpl commitExecutorStatsMXBean =
+                new ThreadExecutorStatsMXBeanImpl(commitExecutor, "CommitExecutorStats",
+                        JMX_BEAN_TYPE, null);
+        commitExecutorStatsMXBean.registerMBean();
+
+        final ThreadExecutorStatsMXBeanImpl commitFutureStatsMXBean =
+                new ThreadExecutorStatsMXBeanImpl(listenableFutureExecutor,
+                        "CommitFutureExecutorStats", JMX_BEAN_TYPE, null);
+        commitFutureStatsMXBean.registerMBean();
+
+        newDataBroker.setCloseable(new AutoCloseable() {
+            @Override
+            public void close() {
+                commitStatsMXBean.unregisterMBean();
+                commitExecutorStatsMXBean.unregisterMBean();
+                commitFutureStatsMXBean.unregisterMBean();
+            }
+        });
 
         return newDataBroker;
     }