2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.sal.binding.codegen.impl;
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;
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;
26 public class SingletonHolder {
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(
31 public static final RuntimeCodeGenerator RPC_GENERATOR = RPC_GENERATOR_IMPL;
32 public static final NotificationInvokerFactory INVOKER_FACTORY = RPC_GENERATOR_IMPL.getInvokerFactory();
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;
40 private static ListeningExecutorService NOTIFICATION_EXECUTOR = null;
41 private static ListeningExecutorService COMMIT_EXECUTOR = null;
42 private static ListeningExecutorService CHANGE_EVENT_EXECUTOR = null;
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.
49 public static synchronized final ListeningExecutorService getDefaultNotificationExecutor() {
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) {
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.
64 ThreadFactory factory = new ThreadFactoryBuilder().setDaemon(true).setNameFormat("md-sal-binding-notification-%d").build();
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.
71 public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
73 executor.getQueue().put(r);
74 } catch (InterruptedException e) {
75 Thread.currentThread().interrupt();// set interrupt flag after clearing
76 throw new IllegalStateException(e);
81 NOTIFICATION_EXECUTOR = MoreExecutors.listeningDecorator(executor);
84 return NOTIFICATION_EXECUTOR;
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.
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();
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.
102 ExecutorService executor = Executors.newSingleThreadExecutor(factory);
103 COMMIT_EXECUTOR = MoreExecutors.listeningDecorator(executor);
106 return COMMIT_EXECUTOR;
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();
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.
119 ExecutorService executor = Executors.newSingleThreadExecutor(factory);
120 CHANGE_EVENT_EXECUTOR = MoreExecutors.listeningDecorator(executor);
123 return CHANGE_EVENT_EXECUTOR;