Merge "Skip from the surefire execution plugins that can step over guava feet"
[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     // block caller thread after MAX_NOTIFICATION_THREADS + MAX_NOTIFICATION_QUEUE_SIZE pending notifications
37     public static final int MAX_NOTIFICATION_QUEUE_SIZE = 10;
38     public static final int NOTIFICATION_THREAD_LIFE = 15;
39
40     private static ListeningExecutorService NOTIFICATION_EXECUTOR = null;
41     private static ListeningExecutorService COMMIT_EXECUTOR = null;
42     private static ListeningExecutorService CHANGE_EVENT_EXECUTOR = null;
43
44     /**
45      * @deprecated This method is only used from configuration modules and thus callers of it
46      *             should use service injection to make the executor configurable.
47      */
48     @Deprecated
49     public static synchronized final ListeningExecutorService getDefaultNotificationExecutor() {
50
51         if (NOTIFICATION_EXECUTOR == null) {
52             // Overriding the queue:
53             // ThreadPoolExecutor would not create new threads if the queue is not full, thus adding
54             // occurs in RejectedExecutionHandler.
55             // This impl saturates threadpool first, then queue. When both are full caller will get blocked.
56             BlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>(MAX_NOTIFICATION_QUEUE_SIZE) {
57                 @Override
58                 public boolean offer(Runnable r) {
59                     // ThreadPoolExecutor will spawn a new thread after core size is reached only if the queue.offer returns false.
60                     return false;
61                 }
62             };
63
64             ThreadFactory factory = new ThreadFactoryBuilder().setDaemon(true).setNameFormat("md-sal-binding-notification-%d").build();
65
66             ThreadPoolExecutor executor = new ThreadPoolExecutor(CORE_NOTIFICATION_THREADS, MAX_NOTIFICATION_THREADS,
67                     NOTIFICATION_THREAD_LIFE, TimeUnit.SECONDS, queue , factory,
68                     new RejectedExecutionHandler() {
69                         // if the max threads are met, then it will raise a rejectedExecution. We then push to the queue.
70                         @Override
71                         public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
72                             try {
73                                 executor.getQueue().put(r);
74                             } catch (InterruptedException e) {
75                                 Thread.currentThread().interrupt();// set interrupt flag after clearing
76                                 throw new IllegalStateException(e);
77                             }
78                         }
79                     });
80
81             NOTIFICATION_EXECUTOR = MoreExecutors.listeningDecorator(executor);
82         }
83
84         return NOTIFICATION_EXECUTOR;
85     }
86
87     /**
88      * @deprecated This method is only used from configuration modules and thus callers of it
89      *             should use service injection to make the executor configurable.
90      */
91     @Deprecated
92     public static synchronized final ListeningExecutorService getDefaultCommitExecutor() {
93         if (COMMIT_EXECUTOR == null) {
94             ThreadFactory factory = new ThreadFactoryBuilder().setDaemon(true).setNameFormat("md-sal-binding-commit-%d").build();
95             /*
96              * FIXME: this used to be newCacheThreadPool(), but MD-SAL does not have transaction
97              *        ordering guarantees, which means that using a concurrent threadpool results
98              *        in application data being committed in random order, potentially resulting
99              *        in inconsistent data being present. Once proper primitives are introduced,
100              *        concurrency can be reintroduced.
101              */
102             ExecutorService executor = Executors.newSingleThreadExecutor(factory);
103             COMMIT_EXECUTOR = MoreExecutors.listeningDecorator(executor);
104         }
105
106         return COMMIT_EXECUTOR;
107     }
108
109     public static ExecutorService getDefaultChangeEventExecutor() {
110         if (CHANGE_EVENT_EXECUTOR == null) {
111             ThreadFactory factory = new ThreadFactoryBuilder().setDaemon(true).setNameFormat("md-sal-binding-change-%d").build();
112             /*
113              * FIXME: this used to be newCacheThreadPool(), but MD-SAL does not have transaction
114              *        ordering guarantees, which means that using a concurrent threadpool results
115              *        in application data being committed in random order, potentially resulting
116              *        in inconsistent data being present. Once proper primitives are introduced,
117              *        concurrency can be reintroduced.
118              */
119             ExecutorService executor = Executors.newSingleThreadExecutor(factory);
120             CHANGE_EVENT_EXECUTOR  = MoreExecutors.listeningDecorator(executor);
121         }
122
123         return CHANGE_EVENT_EXECUTOR;
124     }
125 }