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