blob: b2e921f4b69560bd4c8b2ad38d0bd5bc26ad41f7 [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;
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070046
Thomas Vachuska945e7b02015-03-18 14:23:53 -070047 // Ban public construction
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070048 private SharedExecutors() {
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070049 }
50
51 /**
Thomas Vachuska945e7b02015-03-18 14:23:53 -070052 * Returns the shared single thread executor.
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070053 *
54 * @return shared single thread executor
55 */
56 public static ExecutorService getSingleThreadExecutor() {
Ray Milkey9bf3b2a82019-03-14 13:47:31 -070057 setup();
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070058 return singleThreadExecutor;
59 }
Thomas Vachuska945e7b02015-03-18 14:23:53 -070060
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070061 /**
Thomas Vachuska945e7b02015-03-18 14:23:53 -070062 * Returns the shared thread pool executor.
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070063 *
64 * @return shared executor pool
65 */
66 public static ExecutorService getPoolThreadExecutor() {
Ray Milkey9bf3b2a82019-03-14 13:47:31 -070067 setup();
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070068 return poolThreadExecutor;
69 }
Thomas Vachuska945e7b02015-03-18 14:23:53 -070070
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070071 /**
Thomas Vachuska945e7b02015-03-18 14:23:53 -070072 * Returns the shared timer.
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070073 *
74 * @return shared timer
75 */
Thomas Vachuska945e7b02015-03-18 14:23:53 -070076 public static Timer getTimer() {
Ray Milkey9bf3b2a82019-03-14 13:47:31 -070077 setup();
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070078 return sharedTimer;
79 }
80
Murat Parlakisik553db172015-04-08 03:29:04 -070081 /**
82 * Sets the shared thread pool size.
Thomas Vachuskab0317c62015-04-08 23:58:58 -070083 *
84 * @param poolSize new pool size
Murat Parlakisik553db172015-04-08 03:29:04 -070085 */
86 public static void setPoolSize(int poolSize) {
Thomas Vachuskab0317c62015-04-08 23:58:58 -070087 checkArgument(poolSize > 0, "Shared pool size size must be greater than 0");
88 poolThreadExecutor.setBackingExecutor(
89 newFixedThreadPool(poolSize, groupedThreads("onos/shared",
90 "onos-pool-executor-%d")));
91 }
92
Thomas Vachuska0666f152016-08-05 12:03:54 -070093 /**
94 * Enables or disables calculation of the pool performance metrics. If
95 * the metrics service is not null metric collection will be enabled;
96 * otherwise it will be disabled.
97 *
98 * @param metricsService optional metric service
99 */
100 public static void setMetricsService(MetricsService metricsService) {
101 poolThreadExecutor.setMetricsService(metricsService);
Murat Parlakisikdc17f7b2016-01-26 12:08:35 -0800102 }
103
Thomas Vachuskab0317c62015-04-08 23:58:58 -0700104 /**
105 * Shuts down all shared timers and executors and therefore should be
106 * called only by the framework.
107 */
108 public static void shutdown() {
109 sharedTimer.shutdown();
110 singleThreadExecutor.backingExecutor().shutdown();
111 poolThreadExecutor.backingExecutor().shutdown();
Ray Milkey9bf3b2a82019-03-14 13:47:31 -0700112 sharedTimer = null;
113 singleThreadExecutor = null;
114 poolThreadExecutor = null;
115 }
116
117 private static synchronized void setup() {
118 if (sharedTimer == null) {
119 sharedTimer = new SharedTimer();
120
121 singleThreadExecutor =
122 new SharedExecutorService(
123 newSingleThreadExecutor(groupedThreads("onos/shared",
124 "onos-single-executor")));
125
126 poolThreadExecutor =
127 new SharedExecutorService(
128 newFixedThreadPool(DEFAULT_POOL_SIZE,
129 groupedThreads("onos/shared",
130 "onos-pool-executor-%d")));
131 }
Thomas Vachuskab0317c62015-04-08 23:58:58 -0700132 }
133
134 // Timer extension which does not allow outside cancel method.
135 private static class SharedTimer extends Timer {
136
137 public SharedTimer() {
138 super("onos-shared-timer");
139 }
140
141 @Override
142 public void cancel() {
143 throw new UnsupportedOperationException("Cancel of shared timer is not allowed");
144 }
145
146 private void shutdown() {
147 super.cancel();
Murat Parlakisik553db172015-04-08 03:29:04 -0700148 }
149 }
Thomas Vachuskab0317c62015-04-08 23:58:58 -0700150
Murat Parlakisikaf1042d2015-03-14 01:08:29 -0700151}