Merge "BUG 652 leafref CCE & BUG 720 colons problem"
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / sal / binding / codegen / impl / SingletonHolder.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.sal.binding.codegen.impl;
9
10 import com.google.common.util.concurrent.ListeningExecutorService;
11 import com.google.common.util.concurrent.MoreExecutors;
12 import com.google.common.util.concurrent.ThreadFactoryBuilder;
13 import javassist.ClassPool;
14 import org.opendaylight.controller.sal.binding.codegen.RuntimeCodeGenerator;
15 import org.opendaylight.controller.sal.binding.spi.NotificationInvokerFactory;
16
17 import java.util.concurrent.BlockingQueue;
18 import java.util.concurrent.ExecutorService;
19 import java.util.concurrent.Executors;
20 import java.util.concurrent.LinkedBlockingQueue;
21 import java.util.concurrent.RejectedExecutionHandler;
22 import java.util.concurrent.ThreadFactory;
23 import java.util.concurrent.ThreadPoolExecutor;
24 import java.util.concurrent.TimeUnit;
25
26 public class SingletonHolder {
27
28     public static final ClassPool CLASS_POOL = ClassPool.getDefault();
29     public static final org.opendaylight.controller.sal.binding.codegen.impl.RuntimeCodeGenerator RPC_GENERATOR_IMPL = new org.opendaylight.controller.sal.binding.codegen.impl.RuntimeCodeGenerator(
30             CLASS_POOL);
31     public static final RuntimeCodeGenerator RPC_GENERATOR = RPC_GENERATOR_IMPL;
32     public static final NotificationInvokerFactory INVOKER_FACTORY = RPC_GENERATOR_IMPL.getInvokerFactory();
33
34     public static final int CORE_NOTIFICATION_THREADS = 4;
35     public static final int MAX_NOTIFICATION_THREADS = 32;
36     public static final int NOTIFICATION_THREAD_LIFE = 15;
37
38     private static ListeningExecutorService NOTIFICATION_EXECUTOR = null;
39     private static ListeningExecutorService COMMIT_EXECUTOR = null;
40     private static ListeningExecutorService CHANGE_EVENT_EXECUTOR = null;
41
42     /**
43      * @deprecated This method is only used from configuration modules and thus callers of it
44      *             should use service injection to make the executor configurable.
45      */
46     @Deprecated
47     public static synchronized final ListeningExecutorService getDefaultNotificationExecutor() {
48
49         if (NOTIFICATION_EXECUTOR == null) {
50             // Overriding the queue since we need an unbounded queue
51             // and threadpoolexecutor would not create new threads if the queue is not full
52             BlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>() {
53                 @Override
54                 public boolean offer(Runnable r) {
55                     if (size() <= 1) {
56                         // if the queue is empty (or has just 1), no need to rampup the threads
57                         return super.offer(r);
58                     } else {
59                         // if the queue is not empty, force the queue to return false.
60                         // threadpoolexecutor will spawn a new thread if the queue.offer returns false.
61                         return false;
62                     }
63                 }
64             };
65
66             ThreadFactory factory = new ThreadFactoryBuilder().setDaemon(true).setNameFormat("md-sal-binding-notification-%d").build();
67
68             ThreadPoolExecutor executor = new ThreadPoolExecutor(CORE_NOTIFICATION_THREADS, MAX_NOTIFICATION_THREADS,
69                     NOTIFICATION_THREAD_LIFE, TimeUnit.SECONDS, queue , factory,
70                     new RejectedExecutionHandler() {
71                         // if the max threads are met, then it will raise a rejectedExecution. We then push to the queue.
72                         @Override
73                         public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
74                             try {
75                                 executor.getQueue().put(r);
76                             } catch (InterruptedException e) {
77                                 e.printStackTrace();
78                             }
79                         }
80                     });
81
82             NOTIFICATION_EXECUTOR = MoreExecutors.listeningDecorator(executor);
83         }
84
85         return NOTIFICATION_EXECUTOR;
86     }
87
88     /**
89      * @deprecated This method is only used from configuration modules and thus callers of it
90      *             should use service injection to make the executor configurable.
91      */
92     @Deprecated
93     public static synchronized final ListeningExecutorService getDefaultCommitExecutor() {
94         if (COMMIT_EXECUTOR == null) {
95             ThreadFactory factory = new ThreadFactoryBuilder().setDaemon(true).setNameFormat("md-sal-binding-commit-%d").build();
96             /*
97              * FIXME: this used to be newCacheThreadPool(), but MD-SAL does not have transaction
98              *        ordering guarantees, which means that using a concurrent threadpool results
99              *        in application data being committed in random order, potentially resulting
100              *        in inconsistent data being present. Once proper primitives are introduced,
101              *        concurrency can be reintroduced.
102              */
103             ExecutorService executor = Executors.newSingleThreadExecutor(factory);
104             COMMIT_EXECUTOR = MoreExecutors.listeningDecorator(executor);
105         }
106
107         return COMMIT_EXECUTOR;
108     }
109
110     public static ExecutorService getDefaultChangeEventExecutor() {
111         if (CHANGE_EVENT_EXECUTOR == null) {
112             ThreadFactory factory = new ThreadFactoryBuilder().setDaemon(true).setNameFormat("md-sal-binding-change-%d").build();
113             /*
114              * FIXME: this used to be newCacheThreadPool(), but MD-SAL does not have transaction
115              *        ordering guarantees, which means that using a concurrent threadpool results
116              *        in application data being committed in random order, potentially resulting
117              *        in inconsistent data being present. Once proper primitives are introduced,
118              *        concurrency can be reintroduced.
119              */
120             ExecutorService executor = Executors.newSingleThreadExecutor(factory);
121             CHANGE_EVENT_EXECUTOR  = MoreExecutors.listeningDecorator(executor);
122         }
123
124         return CHANGE_EVENT_EXECUTOR;
125     }
126 }