blob: 9b91a1aba98e16ad23cc0c0daf821072fd42c72f [file] [log] [blame]
Thomas Vachuskab0317c62015-04-08 23:58:58 -07001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
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 */
16package org.onlab.util;
17
Jian Li66865682016-03-02 11:43:09 -080018import com.codahale.metrics.Timer;
Murat Parlakisikdc17f7b2016-01-26 12:08:35 -080019import org.onlab.metrics.MetricsComponent;
20import org.onlab.metrics.MetricsFeature;
21import org.onlab.metrics.MetricsService;
22
Thomas Vachuskab0317c62015-04-08 23:58:58 -070023import java.util.Collection;
24import java.util.List;
25import java.util.concurrent.Callable;
26import java.util.concurrent.ExecutionException;
27import java.util.concurrent.ExecutorService;
28import java.util.concurrent.Future;
29import java.util.concurrent.TimeUnit;
30import java.util.concurrent.TimeoutException;
Murat Parlakisikdc17f7b2016-01-26 12:08:35 -080031
Jian Li66865682016-03-02 11:43:09 -080032import static org.slf4j.LoggerFactory.getLogger;
Murat Parlakisikdc17f7b2016-01-26 12:08:35 -080033
Thomas Vachuskab0317c62015-04-08 23:58:58 -070034
35/**
36 * Executor service wrapper for shared executors with safeguards on shutdown
37 * to prevent inadvertent shutdown.
38 */
39class SharedExecutorService implements ExecutorService {
40
41 private static final String NOT_ALLOWED = "Shutdown of shared executor is not allowed";
42
43 private ExecutorService executor;
44
Murat Parlakisikdc17f7b2016-01-26 12:08:35 -080045 private MetricsService metricsService = null;
46
47 private MetricsComponent executorMetrics;
48 private Timer queueMetrics = null;
49 private Timer delayMetrics = null;
50
51
Thomas Vachuskab0317c62015-04-08 23:58:58 -070052 /**
53 * Creates a wrapper for the given executor service.
54 *
55 * @param executor executor service to wrap
56 */
57 SharedExecutorService(ExecutorService executor) {
58 this.executor = executor;
59 }
60
61 /**
62 * Returns the backing executor service.
63 *
64 * @return backing executor service
65 */
66 ExecutorService backingExecutor() {
67 return executor;
68 }
69
70 /**
71 * Swaps the backing executor with a new one and shuts down the old one.
72 *
73 * @param executor new executor service
74 */
75 void setBackingExecutor(ExecutorService executor) {
76 ExecutorService oldExecutor = this.executor;
77 this.executor = executor;
78 oldExecutor.shutdown();
79 }
80
Murat Parlakisikdc17f7b2016-01-26 12:08:35 -080081
Thomas Vachuskab0317c62015-04-08 23:58:58 -070082 @Override
83 public void shutdown() {
84 throw new UnsupportedOperationException(NOT_ALLOWED);
85 }
86
87 @Override
88 public List<Runnable> shutdownNow() {
89 throw new UnsupportedOperationException(NOT_ALLOWED);
90 }
91
92 @Override
93 public boolean isShutdown() {
94 return executor.isShutdown();
95 }
96
97 @Override
98 public boolean isTerminated() {
99 return executor.isTerminated();
100 }
101
102 @Override
103 public boolean awaitTermination(long timeout, TimeUnit unit)
104 throws InterruptedException {
105 return executor.awaitTermination(timeout, unit);
106 }
107
108 @Override
109 public <T> Future<T> submit(Callable<T> task) {
Murat Parlakisikdc17f7b2016-01-26 12:08:35 -0800110 Counter taskCounter = new Counter();
111 taskCounter.reset();
112 return executor.submit(() -> {
113 T t = null;
114 long queueWaitTime = (long) taskCounter.duration();
Jian Li66865682016-03-02 11:43:09 -0800115 Class className;
Murat Parlakisikdc17f7b2016-01-26 12:08:35 -0800116 if (task instanceof CallableExtended) {
Jian Li66865682016-03-02 11:43:09 -0800117 className = ((CallableExtended) task).getRunnable().getClass();
Murat Parlakisikdc17f7b2016-01-26 12:08:35 -0800118 } else {
Jian Li66865682016-03-02 11:43:09 -0800119 className = task.getClass();
Murat Parlakisikdc17f7b2016-01-26 12:08:35 -0800120 }
121 if (queueMetrics != null) {
122 queueMetrics.update(queueWaitTime, TimeUnit.SECONDS);
123 }
124 taskCounter.reset();
125 try {
126 t = task.call();
Jian Li66865682016-03-02 11:43:09 -0800127 } catch (Exception e) {
128 getLogger(className).error("Uncaught exception on " + className, e);
129 }
Murat Parlakisikdc17f7b2016-01-26 12:08:35 -0800130 long taskwaittime = (long) taskCounter.duration();
131 if (delayMetrics != null) {
132 delayMetrics.update(taskwaittime, TimeUnit.SECONDS);
133 }
134 return t;
135 }
136 );
Thomas Vachuskab0317c62015-04-08 23:58:58 -0700137 }
138
139 @Override
140 public <T> Future<T> submit(Runnable task, T result) {
141 return executor.submit(task, result);
142 }
143
144 @Override
145 public Future<?> submit(Runnable task) {
146 return executor.submit(task);
147 }
148
149 @Override
150 public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
151 throws InterruptedException {
152 return executor.invokeAll(tasks);
153 }
154
155 @Override
156 public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks,
157 long timeout, TimeUnit unit)
158 throws InterruptedException {
159 return executor.invokeAll(tasks, timeout, unit);
160 }
161
162 @Override
163 public <T> T invokeAny(Collection<? extends Callable<T>> tasks)
164 throws InterruptedException, ExecutionException {
165 return executor.invokeAny(tasks);
166 }
167
168 @Override
169 public <T> T invokeAny(Collection<? extends Callable<T>> tasks,
170 long timeout, TimeUnit unit)
171 throws InterruptedException, ExecutionException, TimeoutException {
HIGUCHI Yuta1651e982015-09-04 13:45:08 -0700172 return executor.invokeAny(tasks, timeout, unit);
Thomas Vachuskab0317c62015-04-08 23:58:58 -0700173 }
174
175 @Override
176 public void execute(Runnable command) {
177 executor.execute(command);
178 }
179
Murat Parlakisikdc17f7b2016-01-26 12:08:35 -0800180 public void setCalculatePoolPerformance(boolean calculatePoolPerformance, MetricsService metricsSrv) {
181 this.metricsService = metricsSrv;
182 if (calculatePoolPerformance) {
183 if (metricsService != null) {
184 executorMetrics = metricsService.registerComponent("SharedExecutor");
185 MetricsFeature mf = executorMetrics.registerFeature("*");
186 queueMetrics = metricsService.createTimer(executorMetrics, mf, "Queue");
187 delayMetrics = metricsService.createTimer(executorMetrics, mf, "Delay");
188 }
189 } else {
190 metricsService = null;
191 queueMetrics = null;
192 delayMetrics = null;
193 }
194 }
195
196 /**
197 * CallableExtended class is used to get Runnable Object
198 * from Callable Object.
199 *
200 */
201 class CallableExtended implements Callable {
202
203 private Runnable runnable;
204
205 /**
206 * Wrapper for Callable object .
207 * @param runnable Runnable object
208 */
209 public CallableExtended(Runnable runnable) {
210 this.runnable = runnable;
211 }
212 public Runnable getRunnable() {
213 return runnable;
214 }
215
216 @Override
217 public Object call() throws Exception {
218 runnable.run();
219 return null;
220 }
221 }
222
Thomas Vachuskab0317c62015-04-08 23:58:58 -0700223}