blob: e4f5e5b446a8f263903b9bbf4380b83b0254c4c2 [file] [log] [blame]
Murat Parlakisikaf1042d2015-03-14 01:08:29 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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
Thomas Vachuskab0317c62015-04-08 23:58:58 -070042 private static SharedExecutorService singleThreadExecutor =
43 new SharedExecutorService(
44 newSingleThreadExecutor(groupedThreads("onos/shared",
45 "onos-single-executor")));
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070046
Thomas Vachuskab0317c62015-04-08 23:58:58 -070047 private static SharedExecutorService poolThreadExecutor =
48 new SharedExecutorService(
49 newFixedThreadPool(DEFAULT_POOL_SIZE,
50 groupedThreads("onos/shared",
51 "onos-pool-executor-%d")));
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070052
Thomas Vachuskab0317c62015-04-08 23:58:58 -070053 private static SharedTimer sharedTimer = new SharedTimer();
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070054
Thomas Vachuska945e7b02015-03-18 14:23:53 -070055 // Ban public construction
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070056 private SharedExecutors() {
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070057 }
58
59 /**
Thomas Vachuska945e7b02015-03-18 14:23:53 -070060 * Returns the shared single thread executor.
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070061 *
62 * @return shared single thread executor
63 */
64 public static ExecutorService getSingleThreadExecutor() {
65 return singleThreadExecutor;
66 }
Thomas Vachuska945e7b02015-03-18 14:23:53 -070067
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070068 /**
Thomas Vachuska945e7b02015-03-18 14:23:53 -070069 * Returns the shared thread pool executor.
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070070 *
71 * @return shared executor pool
72 */
73 public static ExecutorService getPoolThreadExecutor() {
74 return poolThreadExecutor;
75 }
Thomas Vachuska945e7b02015-03-18 14:23:53 -070076
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070077 /**
Thomas Vachuska945e7b02015-03-18 14:23:53 -070078 * Returns the shared timer.
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070079 *
80 * @return shared timer
81 */
Thomas Vachuska945e7b02015-03-18 14:23:53 -070082 public static Timer getTimer() {
Murat Parlakisikaf1042d2015-03-14 01:08:29 -070083 return sharedTimer;
84 }
85
Murat Parlakisik553db172015-04-08 03:29:04 -070086 /**
87 * Sets the shared thread pool size.
Thomas Vachuskab0317c62015-04-08 23:58:58 -070088 *
89 * @param poolSize new pool size
Murat Parlakisik553db172015-04-08 03:29:04 -070090 */
91 public static void setPoolSize(int poolSize) {
Thomas Vachuskab0317c62015-04-08 23:58:58 -070092 checkArgument(poolSize > 0, "Shared pool size size must be greater than 0");
93 poolThreadExecutor.setBackingExecutor(
94 newFixedThreadPool(poolSize, groupedThreads("onos/shared",
95 "onos-pool-executor-%d")));
96 }
97
Thomas Vachuska0666f152016-08-05 12:03:54 -070098 /**
99 * Enables or disables calculation of the pool performance metrics. If
100 * the metrics service is not null metric collection will be enabled;
101 * otherwise it will be disabled.
102 *
103 * @param metricsService optional metric service
104 */
105 public static void setMetricsService(MetricsService metricsService) {
106 poolThreadExecutor.setMetricsService(metricsService);
Murat Parlakisikdc17f7b2016-01-26 12:08:35 -0800107 }
108
Thomas Vachuskab0317c62015-04-08 23:58:58 -0700109 /**
110 * Shuts down all shared timers and executors and therefore should be
111 * called only by the framework.
112 */
113 public static void shutdown() {
114 sharedTimer.shutdown();
115 singleThreadExecutor.backingExecutor().shutdown();
116 poolThreadExecutor.backingExecutor().shutdown();
117 }
118
119 // Timer extension which does not allow outside cancel method.
120 private static class SharedTimer extends Timer {
121
122 public SharedTimer() {
123 super("onos-shared-timer");
124 }
125
126 @Override
127 public void cancel() {
128 throw new UnsupportedOperationException("Cancel of shared timer is not allowed");
129 }
130
131 private void shutdown() {
132 super.cancel();
Murat Parlakisik553db172015-04-08 03:29:04 -0700133 }
134 }
Thomas Vachuskab0317c62015-04-08 23:58:58 -0700135
Murat Parlakisikaf1042d2015-03-14 01:08:29 -0700136}