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.ForwardingBlockingQueue;
11 import com.google.common.util.concurrent.ListeningExecutorService;
12 import com.google.common.util.concurrent.MoreExecutors;
13 import com.google.common.util.concurrent.ThreadFactoryBuilder;
14 import java.util.concurrent.BlockingQueue;
15 import java.util.concurrent.ExecutorService;
16 import java.util.concurrent.Executors;
17 import java.util.concurrent.LinkedBlockingQueue;
18 import java.util.concurrent.RejectedExecutionException;
19 import java.util.concurrent.RejectedExecutionHandler;
20 import java.util.concurrent.ThreadFactory;
21 import java.util.concurrent.ThreadPoolExecutor;
22 import java.util.concurrent.TimeUnit;
23 import javassist.ClassPool;
24 import org.apache.commons.lang3.StringUtils;
25 import org.opendaylight.controller.sal.binding.codegen.RuntimeCodeGenerator;
26 import org.opendaylight.controller.sal.binding.spi.NotificationInvokerFactory;
27 import org.opendaylight.yangtools.sal.binding.generator.util.JavassistUtils;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 public class SingletonHolder {
32 private static final Logger logger = LoggerFactory.getLogger(SingletonHolder.class);
34 public static final ClassPool CLASS_POOL = ClassPool.getDefault();
35 public static final JavassistUtils JAVASSIST = JavassistUtils.forClassPool(CLASS_POOL);
36 public static final org.opendaylight.controller.sal.binding.codegen.impl.DefaultRuntimeCodeGenerator RPC_GENERATOR_IMPL = new org.opendaylight.controller.sal.binding.codegen.impl.DefaultRuntimeCodeGenerator(
38 public static final RuntimeCodeGenerator RPC_GENERATOR = RPC_GENERATOR_IMPL;
39 public static final NotificationInvokerFactory INVOKER_FACTORY = RPC_GENERATOR_IMPL.getInvokerFactory();
41 public static final int CORE_NOTIFICATION_THREADS = 4;
42 public static final int MAX_NOTIFICATION_THREADS = 32;
43 // block caller thread after MAX_NOTIFICATION_THREADS + MAX_NOTIFICATION_QUEUE_SIZE pending notifications
44 public static final int MAX_NOTIFICATION_QUEUE_SIZE = 1000;
45 public static final int NOTIFICATION_THREAD_LIFE = 15;
46 private static final String NOTIFICATION_QUEUE_SIZE_PROPERTY = "mdsal.notificationqueue.size";
48 private static ListeningExecutorService NOTIFICATION_EXECUTOR = null;
49 private static ListeningExecutorService COMMIT_EXECUTOR = null;
50 private static ListeningExecutorService CHANGE_EVENT_EXECUTOR = null;
53 * @deprecated This method is only used from configuration modules and thus callers of it
54 * should use service injection to make the executor configurable.
57 public static synchronized ListeningExecutorService getDefaultNotificationExecutor() {
59 if (NOTIFICATION_EXECUTOR == null) {
60 int queueSize = MAX_NOTIFICATION_QUEUE_SIZE;
61 String queueValue = System.getProperty(NOTIFICATION_QUEUE_SIZE_PROPERTY);
62 if (StringUtils.isNotBlank(queueValue)) {
64 queueSize = Integer.parseInt(queueValue);
65 logger.trace("Queue size was set to {}", queueSize);
66 } catch (NumberFormatException e) {
67 logger.warn("Cannot parse {} as set by {}, using default {}", queueValue,
68 NOTIFICATION_QUEUE_SIZE_PROPERTY, queueSize);
72 // Overriding the queue:
73 // ThreadPoolExecutor would not create new threads if the queue is not full, thus adding
74 // occurs in RejectedExecutionHandler.
75 // This impl saturates threadpool first, then queue. When both are full caller will get blocked.
76 final BlockingQueue<Runnable> delegate = new LinkedBlockingQueue<>(queueSize);
77 final BlockingQueue<Runnable> queue = new ForwardingBlockingQueue<Runnable>() {
79 protected BlockingQueue<Runnable> delegate() {
84 public boolean offer(final Runnable r) {
85 // ThreadPoolExecutor will spawn a new thread after core size is reached only
86 // if the queue.offer returns false.
91 final ThreadFactory factory = new ThreadFactoryBuilder()
93 .setNameFormat("md-sal-binding-notification-%d")
96 final ThreadPoolExecutor executor = new ThreadPoolExecutor(CORE_NOTIFICATION_THREADS, MAX_NOTIFICATION_THREADS,
97 NOTIFICATION_THREAD_LIFE, TimeUnit.SECONDS, queue, factory,
98 new RejectedExecutionHandler() {
99 // if the max threads are met, then it will raise a rejectedExecution. We then push to the queue.
101 public void rejectedExecution(final Runnable r, final ThreadPoolExecutor executor) {
103 executor.getQueue().put(r);
104 } catch (InterruptedException e) {
105 throw new RejectedExecutionException("Interrupted while waiting on the queue", e);
110 NOTIFICATION_EXECUTOR = MoreExecutors.listeningDecorator(executor);
113 return NOTIFICATION_EXECUTOR;
117 * @deprecated This method is only used from configuration modules and thus callers of it
118 * should use service injection to make the executor configurable.
121 public static synchronized ListeningExecutorService getDefaultCommitExecutor() {
122 if (COMMIT_EXECUTOR == null) {
123 ThreadFactory factory = new ThreadFactoryBuilder().setDaemon(true).setNameFormat("md-sal-binding-commit-%d").build();
125 * FIXME: this used to be newCacheThreadPool(), but MD-SAL does not have transaction
126 * ordering guarantees, which means that using a concurrent threadpool results
127 * in application data being committed in random order, potentially resulting
128 * in inconsistent data being present. Once proper primitives are introduced,
129 * concurrency can be reintroduced.
131 ExecutorService executor = Executors.newSingleThreadExecutor(factory);
132 COMMIT_EXECUTOR = MoreExecutors.listeningDecorator(executor);
135 return COMMIT_EXECUTOR;
138 public static ExecutorService getDefaultChangeEventExecutor() {
139 if (CHANGE_EVENT_EXECUTOR == null) {
140 ThreadFactory factory = new ThreadFactoryBuilder().setDaemon(true).setNameFormat("md-sal-binding-change-%d").build();
142 * FIXME: this used to be newCacheThreadPool(), but MD-SAL does not have transaction
143 * ordering guarantees, which means that using a concurrent threadpool results
144 * in application data being committed in random order, potentially resulting
145 * in inconsistent data being present. Once proper primitives are introduced,
146 * concurrency can be reintroduced.
148 ExecutorService executor = Executors.newSingleThreadExecutor(factory);
149 CHANGE_EVENT_EXECUTOR = MoreExecutors.listeningDecorator(executor);
152 return CHANGE_EVENT_EXECUTOR;