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