Merge "Bug 1430: Off-load notifications from single commit thread"
[controller.git] / opendaylight / md-sal / sal-inmemory-datastore / src / main / java / org / opendaylight / controller / md / sal / dom / store / impl / InMemoryDOMDataStoreFactory.java
1 /*
2  * Copyright (c) 2014 Brocade Communications 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
9 package org.opendaylight.controller.md.sal.dom.store.impl;
10
11 import java.util.concurrent.ExecutorService;
12 import java.util.concurrent.Executors;
13
14 import javax.annotation.Nullable;
15
16 import org.opendaylight.controller.sal.core.api.model.SchemaService;
17 import org.opendaylight.yangtools.util.concurrent.SpecialExecutors;
18 import org.opendaylight.yangtools.util.PropertyUtils;
19 import com.google.common.util.concurrent.MoreExecutors;
20
21 /**
22  * A factory for creating InMemoryDOMDataStore instances.
23  *
24  * @author Thomas Pantelis
25  */
26 public final class InMemoryDOMDataStoreFactory {
27
28     private static final String DCL_EXECUTOR_MAX_QUEUE_SIZE_PROP =
29             "mdsal.datastore-dcl-notification-queue.size";
30     private static final int DEFAULT_DCL_EXECUTOR_MAX_QUEUE_SIZE = 1000;
31
32     private static final String DCL_EXECUTOR_MAX_POOL_SIZE_PROP =
33             "mdsal.datastore-dcl-notification-pool.size";
34     private static final int DEFAULT_DCL_EXECUTOR_MAX_POOL_SIZE = 20;
35
36     private InMemoryDOMDataStoreFactory() {
37     }
38
39     /**
40      * Creates an InMemoryDOMDataStore instance.
41      *
42      * @param name the name of the data store
43      * @param schemaService the SchemaService to which to register the data store.
44      * @return an InMemoryDOMDataStore instance
45      */
46     public static InMemoryDOMDataStore create(final String name,
47             @Nullable final SchemaService schemaService) {
48
49         // For DataChangeListener notifications we use an executor that provides the fastest
50         // task execution time to get higher throughput as DataChangeListeners typically provide
51         // much of the business logic for a data model. If the executor queue size limit is reached,
52         // subsequent submitted notifications will block the calling thread.
53
54         int dclExecutorMaxQueueSize = PropertyUtils.getIntSystemProperty(
55                 DCL_EXECUTOR_MAX_QUEUE_SIZE_PROP, DEFAULT_DCL_EXECUTOR_MAX_QUEUE_SIZE);
56         int dclExecutorMaxPoolSize = PropertyUtils.getIntSystemProperty(
57                 DCL_EXECUTOR_MAX_POOL_SIZE_PROP, DEFAULT_DCL_EXECUTOR_MAX_POOL_SIZE);
58
59         ExecutorService dataChangeListenerExecutor = SpecialExecutors.newBlockingBoundedFastThreadPool(
60                 dclExecutorMaxPoolSize, dclExecutorMaxQueueSize, name + "-DCL" );
61
62         InMemoryDOMDataStore dataStore = new InMemoryDOMDataStore(name,
63                 MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor()),
64                 dataChangeListenerExecutor);
65
66         if(schemaService != null) {
67             schemaService.registerSchemaContextListener(dataStore);
68         }
69
70         return dataStore;
71     }
72 }