62da307979a1a6372601e9a857e8a0cd4bd4bd3a
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / config / yang / config / concurrent_data_broker / DomConcurrentDataBrokerModule.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.config.concurrent_data_broker;
9
10 import com.google.common.collect.Lists;
11 import java.util.EnumMap;
12 import java.util.List;
13 import java.util.Map;
14 import java.util.concurrent.ExecutorService;
15 import org.opendaylight.controller.cluster.datastore.ConcurrentDOMDataBroker;
16 import org.opendaylight.controller.config.api.DependencyResolver;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.controller.md.sal.common.util.jmx.AbstractMXBean;
19 import org.opendaylight.controller.md.sal.common.util.jmx.ThreadExecutorStatsMXBeanImpl;
20 import org.opendaylight.controller.md.sal.dom.broker.impl.jmx.CommitStatsMXBeanImpl;
21 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStoreFactory;
22 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
23 import org.opendaylight.yangtools.util.DurationStatisticsTracker;
24 import org.opendaylight.yangtools.util.concurrent.SpecialExecutors;
25
26 public class DomConcurrentDataBrokerModule extends AbstractDomConcurrentDataBrokerModule {
27     private static final String JMX_BEAN_TYPE = "DOMDataBroker";
28
29     public DomConcurrentDataBrokerModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier, final DependencyResolver dependencyResolver) {
30         super(identifier, dependencyResolver);
31     }
32
33     public DomConcurrentDataBrokerModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier, final DependencyResolver dependencyResolver, final DomConcurrentDataBrokerModule oldModule, final AutoCloseable oldInstance) {
34         super(identifier, dependencyResolver, oldModule, oldInstance);
35     }
36
37     @Override
38     public AutoCloseable createInstance() {
39         //Initializing Operational DOM DataStore defaulting to InMemoryDOMDataStore if one is not configured
40         DOMStore operStore =  getOperationalDataStoreDependency();
41         if(operStore == null){
42            //we will default to InMemoryDOMDataStore creation
43           operStore = InMemoryDOMDataStoreFactory.create("DOM-OPER", getSchemaServiceDependency());
44         }
45
46         DOMStore configStore = getConfigDataStoreDependency();
47         if(configStore == null){
48            //we will default to InMemoryDOMDataStore creation
49            configStore = InMemoryDOMDataStoreFactory.create("DOM-CFG", getSchemaServiceDependency());
50         }
51
52         final Map<LogicalDatastoreType, DOMStore> datastores = new EnumMap<>(LogicalDatastoreType.class);
53         datastores.put(LogicalDatastoreType.OPERATIONAL, operStore);
54         datastores.put(LogicalDatastoreType.CONFIGURATION, configStore);
55
56         /*
57          * We use an executor for commit ListenableFuture callbacks that favors reusing available
58          * threads over creating new threads at the expense of execution time. The assumption is
59          * that most ListenableFuture callbacks won't execute a lot of business logic where we want
60          * it to run quicker - many callbacks will likely just handle error conditions and do
61          * nothing on success. The executor queue capacity is bounded and, if the capacity is
62          * reached, subsequent submitted tasks will block the caller.
63          */
64         ExecutorService listenableFutureExecutor = SpecialExecutors.newBlockingBoundedCachedThreadPool(
65                 getMaxDataBrokerFutureCallbackPoolSize(), getMaxDataBrokerFutureCallbackQueueSize(),
66                 "CommitFutures");
67
68         final List<AbstractMXBean> mBeans = Lists.newArrayList();
69
70         final DurationStatisticsTracker commitStatsTracker;
71         final ConcurrentDOMDataBroker cdb = new ConcurrentDOMDataBroker(datastores, listenableFutureExecutor);
72         commitStatsTracker = cdb.getCommitStatsTracker();
73
74         if(commitStatsTracker != null) {
75             final CommitStatsMXBeanImpl commitStatsMXBean = new CommitStatsMXBeanImpl(
76                     commitStatsTracker, JMX_BEAN_TYPE);
77             commitStatsMXBean.registerMBean();
78             mBeans.add(commitStatsMXBean);
79         }
80
81         final AbstractMXBean commitFutureStatsMXBean =
82                 ThreadExecutorStatsMXBeanImpl.create(listenableFutureExecutor,
83                         "CommitFutureExecutorStats", JMX_BEAN_TYPE, null);
84         if(commitFutureStatsMXBean != null) {
85             mBeans.add(commitFutureStatsMXBean);
86         }
87
88         cdb.setCloseable(new AutoCloseable() {
89             @Override
90             public void close() {
91                 for(AbstractMXBean mBean: mBeans) {
92                     mBean.unregisterMBean();
93                 }
94             }
95         });
96
97         return cdb;
98     }
99 }