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