1 /*
2 * Copyright 2015 The Netty Project
3 *
4 * The Netty Project licenses this file to you under the Apache License,
5 * version 2.0 (the "License"); you may not use this file except in compliance
6 * with the License. You may obtain a copy of the License at:
7 *
8 * https://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, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations
14 * under the License.
15 */
16 package io.netty.channel.pool;
17
18 import static io.netty.util.internal.ObjectUtil.checkPositive;
19
20 import io.netty.bootstrap.Bootstrap;
21 import io.netty.channel.Channel;
22 import io.netty.util.concurrent.EventExecutor;
23 import io.netty.util.concurrent.Future;
24 import io.netty.util.concurrent.FutureListener;
25 import io.netty.util.concurrent.GlobalEventExecutor;
26 import io.netty.util.concurrent.Promise;
27 import io.netty.util.internal.ObjectUtil;
28
29 import java.nio.channels.ClosedChannelException;
30 import java.util.ArrayDeque;
31 import java.util.Queue;
32 import java.util.concurrent.atomic.AtomicInteger;
33 import java.util.concurrent.Callable;
34 import java.util.concurrent.ScheduledFuture;
35 import java.util.concurrent.TimeUnit;
36 import java.util.concurrent.TimeoutException;
37
38 /**
39 * {@link ChannelPool} implementation that takes another {@link ChannelPool} implementation and enforce a maximum
40 * number of concurrent connections.
41 */
42 public class FixedChannelPool extends SimpleChannelPool {
43
44 public enum AcquireTimeoutAction {
45 /**
46 * Create a new connection when the timeout is detected.
47 */
48 NEW,
49
50 /**
51 * Fail the {@link Future} of the acquire call with a {@link TimeoutException}.
52 */
53 FAIL
54 }
55
56 private final EventExecutor executor;
57 private final long acquireTimeoutNanos;
58 private final Runnable timeoutTask;
59
60 // There is no need to worry about synchronization as everything that modified the queue or counts is done
61 // by the above EventExecutor.
62 private final Queue<AcquireTask> pendingAcquireQueue = new ArrayDeque<AcquireTask>();
63 private final int maxConnections;
64 private final int maxPendingAcquires;
65 private final AtomicInteger acquiredChannelCount = new AtomicInteger();
66 private int pendingAcquireCount;
67 private boolean closed;
68
69 /**
70 * Creates a new instance using the {@link ChannelHealthChecker#ACTIVE}.
71 *
72 * @param bootstrap the {@link Bootstrap} that is used for connections
73 * @param handler the {@link ChannelPoolHandler} that will be notified for the different pool actions
74 * @param maxConnections the number of maximal active connections, once this is reached new tries to acquire
75 * a {@link Channel} will be delayed until a connection is returned to the pool again.
76 */
77 public FixedChannelPool(Bootstrap bootstrap,
78 ChannelPoolHandler handler, int maxConnections) {
79 this(bootstrap, handler, maxConnections, Integer.MAX_VALUE);
80 }
81
82 /**
83 * Creates a new instance using the {@link ChannelHealthChecker#ACTIVE}.
84 *
85 * @param bootstrap the {@link Bootstrap} that is used for connections
86 * @param handler the {@link ChannelPoolHandler} that will be notified for the different pool actions
87 * @param maxConnections the number of maximal active connections, once this is reached new tries to
88 * acquire a {@link Channel} will be delayed until a connection is returned to the
89 * pool again.
90 * @param maxPendingAcquires the maximum number of pending acquires. Once this is exceed acquire tries will
91 * be failed.
92 */
93 public FixedChannelPool(Bootstrap bootstrap,
94 ChannelPoolHandler handler, int maxConnections, int maxPendingAcquires) {
95 this(bootstrap, handler, ChannelHealthChecker.ACTIVE, null, -1, maxConnections, maxPendingAcquires);
96 }
97
98 /**
99 * Creates a new instance.
100 *
101 * @param bootstrap the {@link Bootstrap} that is used for connections
102 * @param handler the {@link ChannelPoolHandler} that will be notified for the different pool actions
103 * @param healthCheck the {@link ChannelHealthChecker} that will be used to check if a {@link Channel} is
104 * still healthy when obtain from the {@link ChannelPool}
105 * @param action the {@link AcquireTimeoutAction} to use or {@code null} if non should be used.
106 * In this case {@param acquireTimeoutMillis} must be {@code -1}.
107 * @param acquireTimeoutMillis the time (in milliseconds) after which an pending acquire must complete or
108 * the {@link AcquireTimeoutAction} takes place.
109 * @param maxConnections the number of maximal active connections, once this is reached new tries to
110 * acquire a {@link Channel} will be delayed until a connection is returned to the
111 * pool again.
112 * @param maxPendingAcquires the maximum number of pending acquires. Once this is exceed acquire tries will
113 * be failed.
114 */
115 public FixedChannelPool(Bootstrap bootstrap,
116 ChannelPoolHandler handler,
117 ChannelHealthChecker healthCheck, AcquireTimeoutAction action,
118 final long acquireTimeoutMillis,
119 int maxConnections, int maxPendingAcquires) {
120 this(bootstrap, handler, healthCheck, action, acquireTimeoutMillis, maxConnections, maxPendingAcquires, true);
121 }
122
123 /**
124 * Creates a new instance.
125 *
126 * @param bootstrap the {@link Bootstrap} that is used for connections
127 * @param handler the {@link ChannelPoolHandler} that will be notified for the different pool actions
128 * @param healthCheck the {@link ChannelHealthChecker} that will be used to check if a {@link Channel} is
129 * still healthy when obtain from the {@link ChannelPool}
130 * @param action the {@link AcquireTimeoutAction} to use or {@code null} if non should be used.
131 * In this case {@param acquireTimeoutMillis} must be {@code -1}.
132 * @param acquireTimeoutMillis the time (in milliseconds) after which an pending acquire must complete or
133 * the {@link AcquireTimeoutAction} takes place.
134 * @param maxConnections the number of maximal active connections, once this is reached new tries to
135 * acquire a {@link Channel} will be delayed until a connection is returned to the
136 * pool again.
137 * @param maxPendingAcquires the maximum number of pending acquires. Once this is exceed acquire tries will
138 * be failed.
139 * @param releaseHealthCheck will check channel health before offering back if this parameter set to
140 * {@code true}.
141 */
142 public FixedChannelPool(Bootstrap bootstrap,
143 ChannelPoolHandler handler,
144 ChannelHealthChecker healthCheck, AcquireTimeoutAction action,
145 final long acquireTimeoutMillis,
146 int maxConnections, int maxPendingAcquires, final boolean releaseHealthCheck) {
147 this(bootstrap, handler, healthCheck, action, acquireTimeoutMillis, maxConnections, maxPendingAcquires,
148 releaseHealthCheck, true);
149 }
150
151 /**
152 * Creates a new instance.
153 *
154 * @param bootstrap the {@link Bootstrap} that is used for connections
155 * @param handler the {@link ChannelPoolHandler} that will be notified for the different pool actions
156 * @param healthCheck the {@link ChannelHealthChecker} that will be used to check if a {@link Channel} is
157 * still healthy when obtain from the {@link ChannelPool}
158 * @param action the {@link AcquireTimeoutAction} to use or {@code null} if non should be used.
159 * In this case {@param acquireTimeoutMillis} must be {@code -1}.
160 * @param acquireTimeoutMillis the time (in milliseconds) after which an pending acquire must complete or
161 * the {@link AcquireTimeoutAction} takes place.
162 * @param maxConnections the number of maximal active connections, once this is reached new tries to
163 * acquire a {@link Channel} will be delayed until a connection is returned to the
164 * pool again.
165 * @param maxPendingAcquires the maximum number of pending acquires. Once this is exceed acquire tries will
166 * be failed.
167 * @param releaseHealthCheck will check channel health before offering back if this parameter set to
168 * {@code true}.
169 * @param lastRecentUsed {@code true} {@link Channel} selection will be LIFO, if {@code false} FIFO.
170 */
171 public FixedChannelPool(Bootstrap bootstrap,
172 ChannelPoolHandler handler,
173 ChannelHealthChecker healthCheck, AcquireTimeoutAction action,
174 final long acquireTimeoutMillis,
175 int maxConnections, int maxPendingAcquires,
176 boolean releaseHealthCheck, boolean lastRecentUsed) {
177 super(bootstrap, handler, healthCheck, releaseHealthCheck, lastRecentUsed);
178 checkPositive(maxConnections, "maxConnections");
179 checkPositive(maxPendingAcquires, "maxPendingAcquires");
180 if (action == null && acquireTimeoutMillis == -1) {
181 timeoutTask = null;
182 acquireTimeoutNanos = -1;
183 } else if (action == null && acquireTimeoutMillis != -1) {
184 throw new NullPointerException("action");
185 } else if (action != null && acquireTimeoutMillis < 0) {
186 throw new IllegalArgumentException("acquireTimeoutMillis: " + acquireTimeoutMillis + " (expected: >= 0)");
187 } else {
188 acquireTimeoutNanos = TimeUnit.MILLISECONDS.toNanos(acquireTimeoutMillis);
189 switch (action) {
190 case FAIL:
191 timeoutTask = new TimeoutTask() {
192 @Override
193 public void onTimeout(AcquireTask task) {
194 // Fail the promise as we timed out.
195 task.promise.setFailure(new AcquireTimeoutException());
196 }
197 };
198 break;
199 case NEW:
200 timeoutTask = new TimeoutTask() {
201 @Override
202 public void onTimeout(AcquireTask task) {
203 // Increment the acquire count and delegate to super to actually acquire a Channel which will
204 // create a new connection.
205 task.acquired();
206
207 FixedChannelPool.super.acquire(task.promise);
208 }
209 };
210 break;
211 default:
212 throw new Error("Unexpected AcquireTimeoutAction: " + action);
213 }
214 }
215 executor = bootstrap.config().group().next();
216 this.maxConnections = maxConnections;
217 this.maxPendingAcquires = maxPendingAcquires;
218 }
219
220 /** Returns the number of acquired channels that this pool thinks it has. */
221 public int acquiredChannelCount() {
222 return acquiredChannelCount.get();
223 }
224
225 @Override
226 public Future<Channel> acquire(final Promise<Channel> promise) {
227 try {
228 if (executor.inEventLoop()) {
229 acquire0(promise);
230 } else {
231 executor.execute(new Runnable() {
232 @Override
233 public void run() {
234 acquire0(promise);
235 }
236 });
237 }
238 } catch (Throwable cause) {
239 promise.tryFailure(cause);
240 }
241 return promise;
242 }
243
244 private void acquire0(final Promise<Channel> promise) {
245 try {
246 assert executor.inEventLoop();
247
248 if (closed) {
249 promise.setFailure(new IllegalStateException("FixedChannelPool was closed"));
250 return;
251 }
252 if (acquiredChannelCount.get() < maxConnections) {
253 assert acquiredChannelCount.get() >= 0;
254
255 // We need to create a new promise as we need to ensure the AcquireListener runs in the correct
256 // EventLoop
257 Promise<Channel> p = executor.newPromise();
258 AcquireListener l = new AcquireListener(promise);
259 l.acquired();
260 p.addListener(l);
261 super.acquire(p);
262 } else {
263 if (pendingAcquireCount >= maxPendingAcquires) {
264 tooManyOutstanding(promise);
265 } else {
266 AcquireTask task = new AcquireTask(promise);
267 if (pendingAcquireQueue.offer(task)) {
268 ++pendingAcquireCount;
269
270 if (timeoutTask != null) {
271 task.timeoutFuture = executor.schedule(timeoutTask, acquireTimeoutNanos,
272 TimeUnit.NANOSECONDS);
273 }
274 } else {
275 tooManyOutstanding(promise);
276 }
277 }
278
279 assert pendingAcquireCount > 0;
280 }
281 } catch (Throwable cause) {
282 promise.tryFailure(cause);
283 }
284 }
285
286 private void tooManyOutstanding(Promise<?> promise) {
287 promise.setFailure(new IllegalStateException("Too many outstanding acquire operations"));
288 }
289
290 @Override
291 public Future<Void> release(final Channel channel, final Promise<Void> promise) {
292 ObjectUtil.checkNotNull(promise, "promise");
293 final Promise<Void> p = executor.newPromise();
294 super.release(channel, p.addListener((FutureListener<Void>) future -> {
295 try {
296 assert executor.inEventLoop();
297
298 if (closed) {
299 // Since the pool is closed, we have no choice but to close the channel
300 channel.close();
301 promise.setFailure(new IllegalStateException("FixedChannelPool was closed"));
302 return;
303 }
304
305 if (future.isSuccess()) {
306 decrementAndRunTaskQueue();
307 promise.setSuccess(null);
308 } else {
309 Throwable cause = future.cause();
310 // Check if the exception was not because of we passed the Channel to the wrong pool.
311 // Other failures (for example an IllegalArgumentException thrown by the ChannelPoolHandler)
312 // must still give back the slot.
313 if (!(cause instanceof ChannelNotAcquiredFromPoolException)) {
314 decrementAndRunTaskQueue();
315 }
316 promise.setFailure(future.cause());
317 }
318 } catch (Throwable cause) {
319 promise.tryFailure(cause);
320 }
321 }));
322 return promise;
323 }
324
325 private void decrementAndRunTaskQueue() {
326 // We should never have a negative value.
327 int currentCount = acquiredChannelCount.decrementAndGet();
328 assert currentCount >= 0;
329
330 // Run the pending acquire tasks before notify the original promise so if the user would
331 // try to acquire again from the ChannelFutureListener and the pendingAcquireCount is >=
332 // maxPendingAcquires we may be able to run some pending tasks first and so allow to add
333 // more.
334 runTaskQueue();
335 }
336
337 private void runTaskQueue() {
338 while (acquiredChannelCount.get() < maxConnections) {
339 AcquireTask task = pendingAcquireQueue.poll();
340 if (task == null) {
341 break;
342 }
343
344 // Cancel the timeout if one was scheduled
345 ScheduledFuture<?> timeoutFuture = task.timeoutFuture;
346 if (timeoutFuture != null) {
347 timeoutFuture.cancel(false);
348 }
349
350 --pendingAcquireCount;
351 task.acquired();
352
353 super.acquire(task.promise);
354 }
355
356 // We should never have a negative value.
357 assert pendingAcquireCount >= 0;
358 assert acquiredChannelCount.get() >= 0;
359 }
360
361 // AcquireTask extends AcquireListener to reduce object creations and so GC pressure
362 private final class AcquireTask extends AcquireListener {
363 final Promise<Channel> promise;
364 final long expireNanoTime = System.nanoTime() + acquireTimeoutNanos;
365 ScheduledFuture<?> timeoutFuture;
366
367 AcquireTask(Promise<Channel> promise) {
368 super(promise);
369 // We need to create a new promise as we need to ensure the AcquireListener runs in the correct
370 // EventLoop.
371 this.promise = executor.<Channel>newPromise().addListener(this);
372 }
373 }
374
375 private abstract class TimeoutTask implements Runnable {
376 @Override
377 public final void run() {
378 assert executor.inEventLoop();
379 long nanoTime = System.nanoTime();
380 for (;;) {
381 AcquireTask task = pendingAcquireQueue.peek();
382 // Compare nanoTime as descripted in the javadocs of System.nanoTime()
383 //
384 // See https://docs.oracle.com/javase/7/docs/api/java/lang/System.html#nanoTime()
385 // See https://github.com/netty/netty/issues/3705
386 if (task == null || nanoTime - task.expireNanoTime < 0) {
387 break;
388 }
389 pendingAcquireQueue.remove();
390
391 --pendingAcquireCount;
392 onTimeout(task);
393 }
394 }
395
396 public abstract void onTimeout(AcquireTask task);
397 }
398
399 private class AcquireListener implements FutureListener<Channel> {
400 private final Promise<Channel> originalPromise;
401 protected boolean acquired;
402
403 AcquireListener(Promise<Channel> originalPromise) {
404 this.originalPromise = originalPromise;
405 }
406
407 @Override
408 public void operationComplete(Future<Channel> future) throws Exception {
409 try {
410 assert executor.inEventLoop();
411
412 if (closed) {
413 if (future.isSuccess()) {
414 // Since the pool is closed, we have no choice but to close the channel
415 future.getNow().close();
416 }
417 originalPromise.setFailure(new IllegalStateException("FixedChannelPool was closed"));
418 return;
419 }
420
421 if (future.isSuccess()) {
422 Channel channel = future.getNow();
423 if (!originalPromise.trySuccess(channel)) {
424 // Promise was completed in the meantime (like cancelled), just release the channel again.
425 release(channel);
426 }
427 } else {
428 if (acquired) {
429 decrementAndRunTaskQueue();
430 } else {
431 runTaskQueue();
432 }
433
434 originalPromise.setFailure(future.cause());
435 }
436 } catch (Throwable cause) {
437 originalPromise.tryFailure(cause);
438 }
439 }
440
441 public void acquired() {
442 if (acquired) {
443 return;
444 }
445 acquiredChannelCount.incrementAndGet();
446 acquired = true;
447 }
448 }
449
450 @Override
451 public void close() {
452 try {
453 closeAsync().await();
454 } catch (InterruptedException e) {
455 Thread.currentThread().interrupt();
456 throw new RuntimeException(e);
457 }
458 }
459
460 /**
461 * Closes the pool in an async manner.
462 *
463 * @return Future which represents completion of the close task
464 */
465 @Override
466 public Future<Void> closeAsync() {
467 if (executor.inEventLoop()) {
468 return close0();
469 } else {
470 final Promise<Void> closeComplete = executor.newPromise();
471 executor.execute(new Runnable() {
472 @Override
473 public void run() {
474 close0().addListener((FutureListener<Void>) f -> {
475 if (f.isSuccess()) {
476 closeComplete.setSuccess(null);
477 } else {
478 closeComplete.setFailure(f.cause());
479 }
480 });
481 }
482 });
483 return closeComplete;
484 }
485 }
486
487 private Future<Void> close0() {
488 assert executor.inEventLoop();
489
490 if (!closed) {
491 closed = true;
492 for (;;) {
493 AcquireTask task = pendingAcquireQueue.poll();
494 if (task == null) {
495 break;
496 }
497 ScheduledFuture<?> f = task.timeoutFuture;
498 if (f != null) {
499 f.cancel(false);
500 }
501 task.promise.setFailure(new ClosedChannelException());
502 }
503 acquiredChannelCount.set(0);
504 pendingAcquireCount = 0;
505
506 // Ensure we dispatch this on another Thread as close0 will be called from the EventExecutor and we need
507 // to ensure we will not block in a EventExecutor.
508 return GlobalEventExecutor.INSTANCE.submit(new Callable<Void>() {
509 @Override
510 public Void call() throws Exception {
511 FixedChannelPool.super.close();
512 return null;
513 }
514 });
515 }
516
517 return GlobalEventExecutor.INSTANCE.newSucceededFuture(null);
518 }
519
520 private static final class AcquireTimeoutException extends TimeoutException {
521
522 private AcquireTimeoutException() {
523 super("Acquire operation took longer then configured maximum time");
524 }
525
526 // Suppress a warning since the method doesn't need synchronization
527 @Override
528 public Throwable fillInStackTrace() {
529 return this;
530 }
531 }
532 }