6412231171db7bdc68cb980541f1a81138dad9be
[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 boolean canReuseInstance(AbstractDomConcurrentDataBrokerModule oldModule) {
39         return true;
40     }
41
42     @Override
43     public AutoCloseable createInstance() {
44         //Initializing Operational DOM DataStore defaulting to InMemoryDOMDataStore if one is not configured
45         DOMStore operStore =  getOperationalDataStoreDependency();
46         if(operStore == null){
47            //we will default to InMemoryDOMDataStore creation
48           operStore = InMemoryDOMDataStoreFactory.create("DOM-OPER", getSchemaServiceDependency());
49         }
50
51         DOMStore configStore = getConfigDataStoreDependency();
52         if(configStore == null){
53            //we will default to InMemoryDOMDataStore creation
54            configStore = InMemoryDOMDataStoreFactory.create("DOM-CFG", getSchemaServiceDependency());
55         }
56
57         final Map<LogicalDatastoreType, DOMStore> datastores = new EnumMap<>(LogicalDatastoreType.class);
58         datastores.put(LogicalDatastoreType.OPERATIONAL, operStore);
59         datastores.put(LogicalDatastoreType.CONFIGURATION, configStore);
60
61         /*
62          * We use an executor for commit ListenableFuture callbacks that favors reusing available
63          * threads over creating new threads at the expense of execution time. The assumption is
64          * that most ListenableFuture callbacks won't execute a lot of business logic where we want
65          * it to run quicker - many callbacks will likely just handle error conditions and do
66          * nothing on success. The executor queue capacity is bounded and, if the capacity is
67          * reached, subsequent submitted tasks will block the caller.
68          */
69         ExecutorService listenableFutureExecutor = SpecialExecutors.newBlockingBoundedCachedThreadPool(
70                 getMaxDataBrokerFutureCallbackPoolSize(), getMaxDataBrokerFutureCallbackQueueSize(),
71                 "CommitFutures");
72
73         final List<AbstractMXBean> mBeans = Lists.newArrayList();
74
75         final DurationStatisticsTracker commitStatsTracker;
76         final ConcurrentDOMDataBroker cdb = new ConcurrentDOMDataBroker(datastores, listenableFutureExecutor);
77         commitStatsTracker = cdb.getCommitStatsTracker();
78
79         if(commitStatsTracker != null) {
80             final CommitStatsMXBeanImpl commitStatsMXBean = new CommitStatsMXBeanImpl(
81                     commitStatsTracker, JMX_BEAN_TYPE);
82             commitStatsMXBean.registerMBean();
83             mBeans.add(commitStatsMXBean);
84         }
85
86         final AbstractMXBean commitFutureStatsMXBean =
87                 ThreadExecutorStatsMXBeanImpl.create(listenableFutureExecutor,
88                         "CommitFutureExecutorStats", JMX_BEAN_TYPE, null);
89         if(commitFutureStatsMXBean != null) {
90             mBeans.add(commitFutureStatsMXBean);
91         }
92
93         cdb.setCloseable(new AutoCloseable() {
94             @Override
95             public void close() {
96                 for(AbstractMXBean mBean: mBeans) {
97                     mBean.unregisterMBean();
98                 }
99             }
100         });
101
102         return cdb;
103     }
104 }