blob: cc99bcce3415f364fb825c8e7eb902b664cbb6e2 [file] [log] [blame]
Murat Parlakisikaf1042d2015-03-14 01:08:29 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Murat Parlakisikaf1042d2015-03-14 01:08:29 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onlab.util;
18
Murat Parlakisikdc17f7b2016-01-26 12:08:35 -080019import org.onlab.metrics.MetricsService;
20
Thomas Vachuska945e7b02015-03-18 14:23:53 -070021import java.util.Timer;
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070022import java.util.concurrent.ExecutorService;
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070023
Thomas Vachuskab0317c62015-04-08 23:58:58 -070024import static com.google.common.base.Preconditions.checkArgument;
Thomas Vachuska945e7b02015-03-18 14:23:53 -070025import static java.util.concurrent.Executors.newFixedThreadPool;
26import static java.util.concurrent.Executors.newSingleThreadExecutor;
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070027import static org.onlab.util.Tools.groupedThreads;
28
29/**
Thomas Vachuska945e7b02015-03-18 14:23:53 -070030 * Utility for managing a set of shared execution resources, such as a timer,
31 * single thread executor and thread pool executor for use by various parts of
32 * the platform or by applications.
33 * <p>
34 * Whenever possible, use of these shared resources is encouraged over creating
35 * separate ones.
36 * </p>
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070037 */
38public final class SharedExecutors {
39
Thomas Vachuskab0317c62015-04-08 23:58:58 -070040 public static final int DEFAULT_POOL_SIZE = 30;
Murat Parlakisik553db172015-04-08 03:29:04 -070041
Ray Milkey9bf3b2a82019-03-14 13:47:31 -070042 private static SharedExecutorService singleThreadExecutor;
43 private static SharedExecutorService poolThreadExecutor;
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070044
Ray Milkey9bf3b2a82019-03-14 13:47:31 -070045 private static SharedTimer sharedTimer;
Ray Milkey397d8d42019-03-22 09:05:02 -070046 private static final Object SHARED_TIMER_LOCK = new Object();
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070047
Thomas Vachuska945e7b02015-03-18 14:23:53 -070048 // Ban public construction
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070049 private SharedExecutors() {
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070050 }
51
52 /**
Thomas Vachuska945e7b02015-03-18 14:23:53 -070053 * Returns the shared single thread executor.
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070054 *
55 * @return shared single thread executor
56 */
57 public static ExecutorService getSingleThreadExecutor() {
Ray Milkey9bf3b2a82019-03-14 13:47:31 -070058 setup();
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070059 return singleThreadExecutor;
60 }
Thomas Vachuska945e7b02015-03-18 14:23:53 -070061
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070062 /**
Thomas Vachuska945e7b02015-03-18 14:23:53 -070063 * Returns the shared thread pool executor.
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070064 *
65 * @return shared executor pool
66 */
67 public static ExecutorService getPoolThreadExecutor() {
Ray Milkey9bf3b2a82019-03-14 13:47:31 -070068 setup();
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070069 return poolThreadExecutor;
70 }
Thomas Vachuska945e7b02015-03-18 14:23:53 -070071
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070072 /**
Thomas Vachuska945e7b02015-03-18 14:23:53 -070073 * Returns the shared timer.
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070074 *
75 * @return shared timer
76 */
Thomas Vachuska945e7b02015-03-18 14:23:53 -070077 public static Timer getTimer() {
Ray Milkey9bf3b2a82019-03-14 13:47:31 -070078 setup();
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070079 return sharedTimer;
80 }
81
Murat Parlakisik553db172015-04-08 03:29:04 -070082 /**
83 * Sets the shared thread pool size.
Thomas Vachuskab0317c62015-04-08 23:58:58 -070084 *
85 * @param poolSize new pool size
Murat Parlakisik553db172015-04-08 03:29:04 -070086 */
87 public static void setPoolSize(int poolSize) {
Thomas Vachuskab0317c62015-04-08 23:58:58 -070088 checkArgument(poolSize > 0, "Shared pool size size must be greater than 0");
89 poolThreadExecutor.setBackingExecutor(
90 newFixedThreadPool(poolSize, groupedThreads("onos/shared",
91 "onos-pool-executor-%d")));
92 }
93
Thomas Vachuska0666f152016-08-05 12:03:54 -070094 /**
95 * Enables or disables calculation of the pool performance metrics. If
96 * the metrics service is not null metric collection will be enabled;
97 * otherwise it will be disabled.
98 *
99 * @param metricsService optional metric service
100 */
101 public static void setMetricsService(MetricsService metricsService) {
102 poolThreadExecutor.setMetricsService(metricsService);
Murat Parlakisikdc17f7b2016-01-26 12:08:35 -0800103 }
104
Thomas Vachuskab0317c62015-04-08 23:58:58 -0700105 /**
106 * Shuts down all shared timers and executors and therefore should be
107 * called only by the framework.
108 */
109 public static void shutdown() {
Ray Milkey397d8d42019-03-22 09:05:02 -0700110 synchronized (SHARED_TIMER_LOCK) {
Carmelo Cascone4d5607e2021-01-28 18:25:59 -0800111 if (sharedTimer != null) {
112 sharedTimer.shutdown();
113 singleThreadExecutor.backingExecutor().shutdown();
114 poolThreadExecutor.backingExecutor().shutdown();
115 sharedTimer = null;
116 singleThreadExecutor = null;
117 poolThreadExecutor = null;
118 }
Ray Milkey397d8d42019-03-22 09:05:02 -0700119 }
Ray Milkey9bf3b2a82019-03-14 13:47:31 -0700120 }
121
Ray Milkey397d8d42019-03-22 09:05:02 -0700122 private static void setup() {
123 synchronized (SHARED_TIMER_LOCK) {
124 if (sharedTimer == null) {
125 sharedTimer = new SharedTimer();
Ray Milkey9bf3b2a82019-03-14 13:47:31 -0700126
Ray Milkey397d8d42019-03-22 09:05:02 -0700127 singleThreadExecutor =
128 new SharedExecutorService(
129 newSingleThreadExecutor(groupedThreads("onos/shared",
130 "onos-single-executor")));
Ray Milkey9bf3b2a82019-03-14 13:47:31 -0700131
Ray Milkey397d8d42019-03-22 09:05:02 -0700132 poolThreadExecutor =
133 new SharedExecutorService(
134 newFixedThreadPool(DEFAULT_POOL_SIZE,
135 groupedThreads("onos/shared",
136 "onos-pool-executor-%d")));
137 }
Ray Milkey9bf3b2a82019-03-14 13:47:31 -0700138 }
Thomas Vachuskab0317c62015-04-08 23:58:58 -0700139 }
140
141 // Timer extension which does not allow outside cancel method.
142 private static class SharedTimer extends Timer {
143
144 public SharedTimer() {
145 super("onos-shared-timer");
146 }
147
148 @Override
149 public void cancel() {
150 throw new UnsupportedOperationException("Cancel of shared timer is not allowed");
151 }
152
153 private void shutdown() {
154 super.cancel();
Murat Parlakisik553db172015-04-08 03:29:04 -0700155 }
156 }
Thomas Vachuskab0317c62015-04-08 23:58:58 -0700157
Murat Parlakisikaf1042d2015-03-14 01:08:29 -0700158}