-
Notifications
You must be signed in to change notification settings - Fork 36
/
TransportFilter.java
517 lines (455 loc) · 20.7 KB
/
TransportFilter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
/*
* Copyright (c) 2014, 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.glassfish.tyrus.container.jdk.client;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousChannelGroup;
import java.nio.channels.AsynchronousCloseException;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;
import java.nio.channels.UnresolvedAddressException;
import java.nio.channels.UnsupportedAddressTypeException;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Queue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.glassfish.tyrus.client.ThreadPoolConfig;
/**
* Writes and reads data to and from a socket. Only one {@link #write(java.nio.ByteBuffer,
* org.glassfish.tyrus.spi.CompletionHandler)} method call can be processed at a time. Only one {@link
* #_read(java.nio.ByteBuffer)} operation is supported at a time, another one is started only after the previous one
* has
* completed. Blocking in {@link #onRead(java.nio.ByteBuffer)} or {@link #onConnect()} method will result in data not
* being read from a socket until these methods have completed.
*
* @author Petr Janouch
*/
class TransportFilter extends Filter {
private static final Logger LOGGER = Logger.getLogger(TransportFilter.class.getName());
private static final int DEFAULT_CONNECTION_CLOSE_WAIT = 30;
private static final AtomicInteger openedConnections = new AtomicInteger(0);
private static final ScheduledExecutorService connectionCloseScheduler =
Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread thread = new Thread(r);
thread.setName("tyrus-jdk-container-idle-timeout");
thread.setDaemon(true);
return thread;
}
});
private static volatile AsynchronousChannelGroup channelGroup;
private static volatile ScheduledFuture<?> closeWaitTask;
/**
* {@link org.glassfish.tyrus.client.ThreadPoolConfig} current {@link #channelGroup} has been created with.
*/
private static volatile ThreadPoolConfig currentThreadPoolConfig;
/**
* Idle timeout that will be used when closing current {@link #channelGroup}
*/
private static volatile Integer currentContainerIdleTimeout;
private final int inputBufferSize;
private final ThreadPoolConfig threadPoolConfig;
private final Integer containerIdleTimeout;
private final InetAddress bindingAddress;
private volatile AsynchronousSocketChannel socketChannel;
/**
* Constructor.
* <p/>
* If the channel group is not active (all connections have been closed and the shutdown timeout is running) and a
* new transport is created with tread pool configuration different from the one of the current thread pool, the
* current thread pool will be shut down and a new one created with the new configuration.
*
* @param inputBufferSize size of buffer to be allocated for reading data from a socket.
* @param threadPoolConfig thread pool configuration used for creating thread pool.
* @param containerIdleTimeout idle time after which the shared thread pool will be destroyed. If {@code null}
* default value will be used. The default value is 30 seconds.
* @param bindingAddress local address to bind sockets ({@link #socketChannel}) when they are created.
* Binding is done only if not {@code null}.
*/
TransportFilter(int inputBufferSize,
ThreadPoolConfig threadPoolConfig,
Integer containerIdleTimeout,
InetAddress bindingAddress) {
super(null);
this.inputBufferSize = inputBufferSize;
this.threadPoolConfig = threadPoolConfig;
this.containerIdleTimeout = containerIdleTimeout;
this.bindingAddress = bindingAddress;
}
@Override
void write(ByteBuffer data, final org.glassfish.tyrus.spi.CompletionHandler<ByteBuffer> completionHandler) {
socketChannel.write(data, data, new CompletionHandler<Integer, ByteBuffer>() {
@Override
public void completed(Integer result, ByteBuffer buffer) {
if (buffer.hasRemaining()) {
write(buffer, completionHandler);
return;
}
completionHandler.completed(buffer);
}
@Override
public void failed(Throwable exc, ByteBuffer buffer) {
completionHandler.failed(exc);
}
});
}
@Override
synchronized void close() {
if (!socketChannel.isOpen()) {
return;
}
try {
socketChannel.close();
} catch (IOException e) {
LOGGER.log(Level.INFO, "Could not close a connection", e);
}
synchronized (TransportFilter.class) {
openedConnections.decrementAndGet();
if (openedConnections.get() == 0 && channelGroup != null) {
scheduleClose();
}
}
upstreamFilter = null;
}
@Override
void startSsl() {
onSslHandshakeCompleted();
}
@Override
public void handleConnect(SocketAddress serverAddress, Filter upstreamFilter) {
this.upstreamFilter = upstreamFilter;
try {
synchronized (TransportFilter.class) {
updateThreadPoolConfig();
initializeChannelGroup();
socketChannel = AsynchronousSocketChannel.open(channelGroup);
if (bindingAddress != null) {
socketChannel.bind(new InetSocketAddress(bindingAddress, 0));
}
openedConnections.incrementAndGet();
}
} catch (IOException e) {
onError(e);
return;
}
try {
socketChannel.connect(serverAddress, null, new CompletionHandler<Void, Void>() {
@Override
public void completed(Void result, Void nothing) {
final ByteBuffer inputBuffer = ByteBuffer.allocate(inputBufferSize);
onConnect();
_read(inputBuffer);
}
@Override
public void failed(Throwable exc, Void nothing) {
onError(exc);
try {
socketChannel.close();
} catch (IOException e) {
LOGGER.log(Level.FINE, "Could not close connection", exc.getMessage());
}
}
});
} catch (UnresolvedAddressException | UnsupportedAddressTypeException e) {
onError(e);
}
}
private void updateThreadPoolConfig() {
// the channel group is active, no change in configuration
if (openedConnections.get() != 0) {
return;
}
Integer closeWait = containerIdleTimeout == null ? DEFAULT_CONNECTION_CLOSE_WAIT : containerIdleTimeout;
// check if the new configuration is different from the one of the current container
if (!threadPoolConfig.equals(currentThreadPoolConfig) || !closeWait.equals(currentContainerIdleTimeout)) {
currentThreadPoolConfig = threadPoolConfig;
currentContainerIdleTimeout = closeWait;
if (channelGroup == null) {
// the channel group has not been initialized (this is a first client) - no need to shut it down
return;
}
closeWaitTask.cancel(true);
closeWaitTask = null;
channelGroup.shutdown();
channelGroup = null;
}
}
private void initializeChannelGroup() throws IOException {
if (closeWaitTask != null) {
closeWaitTask.cancel(true);
closeWaitTask = null;
}
if (channelGroup == null) {
ThreadFactory threadFactory = threadPoolConfig.getThreadFactory();
if (threadFactory == null) {
threadFactory = new TransportThreadFactory(threadPoolConfig);
}
ExecutorService executor;
if (threadPoolConfig.getQueue() != null) {
executor = new QueuingExecutor(threadPoolConfig.getCorePoolSize(), threadPoolConfig.getMaxPoolSize(),
threadPoolConfig.getKeepAliveTime(TimeUnit.MILLISECONDS),
TimeUnit.MILLISECONDS,
threadPoolConfig.getQueue(), false, threadFactory);
} else {
int taskQueueLimit = threadPoolConfig.getQueueLimit();
if (taskQueueLimit == -1) {
taskQueueLimit = Integer.MAX_VALUE;
}
executor = new QueuingExecutor(threadPoolConfig.getCorePoolSize(), threadPoolConfig.getMaxPoolSize(),
threadPoolConfig.getKeepAliveTime(TimeUnit.MILLISECONDS),
TimeUnit.MILLISECONDS, new
LinkedBlockingDeque<Runnable>(taskQueueLimit), true, threadFactory);
}
// Thread pool is owned by the channel group and will be shut down when channel group is shut down
channelGroup = AsynchronousChannelGroup.withCachedThreadPool(executor, threadPoolConfig.getCorePoolSize());
}
}
private void _read(final ByteBuffer inputBuffer) {
/**
* It must be checked that the channel has not been closed by {@link #close()} method.
*/
if (!socketChannel.isOpen()) {
return;
}
socketChannel.read(inputBuffer, null, new CompletionHandler<Integer, Void>() {
@Override
public void completed(Integer bytesRead, Void result) {
// connection closed by the server
if (bytesRead == -1) {
// close will set TransportFilter.this.upstreamFilter to null
Filter upstreamFilter = TransportFilter.this.upstreamFilter;
if (upstreamFilter != null) {
close();
upstreamFilter.onConnectionClosed();
}
return;
}
((Buffer) inputBuffer).flip();
onRead(inputBuffer);
inputBuffer.compact();
_read(inputBuffer);
}
@Override
public void failed(Throwable exc, Void result) {
/**
* Reading from the channel will fail if it is closing. In such cases {@link AsynchronousCloseException}
* is thrown. This should not be logged and no action undertaken.
*/
if (exc instanceof AsynchronousCloseException) {
return;
}
onError(exc);
}
});
}
private void scheduleClose() {
closeWaitTask = connectionCloseScheduler.schedule(new Runnable() {
@Override
public void run() {
synchronized (TransportFilter.class) {
if (closeWaitTask == null) {
return;
}
channelGroup.shutdown();
channelGroup = null;
closeWaitTask = null;
}
}
}, currentContainerIdleTimeout, TimeUnit.SECONDS);
}
/**
* A default thread factory that gets used if {@link org.glassfish.tyrus.client.ThreadPoolConfig#getThreadFactory()}
* is not specified.
*/
private static class TransportThreadFactory implements ThreadFactory {
private static final String THREAD_NAME_BASE = " tyrus-jdk-client-";
private static final AtomicInteger threadCounter = new AtomicInteger(0);
private final ThreadPoolConfig threadPoolConfig;
TransportThreadFactory(ThreadPoolConfig threadPoolConfig) {
this.threadPoolConfig = threadPoolConfig;
}
@Override
public Thread newThread(Runnable r) {
final Thread thread = new Thread(r);
thread.setName(THREAD_NAME_BASE + threadCounter.incrementAndGet());
thread.setPriority(threadPoolConfig.getPriority());
thread.setDaemon(threadPoolConfig.isDaemon());
try {
AccessController.doPrivileged(new PrivilegedAction<Void>() {
@Override
public Void run() {
if (threadPoolConfig.getInitialClassLoader() == null) {
thread.setContextClassLoader(this.getClass().getClassLoader());
} else {
thread.setContextClassLoader(threadPoolConfig.getInitialClassLoader());
}
return null;
}
});
} catch (Throwable t) {
// just log - client still can work without setting context class loader
LOGGER.log(Level.WARNING, "Cannot set thread context class loader.", t);
}
return thread;
}
}
/**
* A thread pool executor that prefers creating new worker threads over queueing tasks until the maximum poll size
* has been reached, after which it will start queueing tasks.
*/
private static class QueuingExecutor extends ThreadPoolExecutor {
private final Queue<Runnable> taskQueue;
private final boolean threadSafeQueue;
/**
* Constructor.
*
* @param threadSafeQueue indicates if {@link #taskQueue} is thread safe or not.
*/
QueuingExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, Queue<Runnable>
taskQueue, boolean threadSafeQueue, ThreadFactory threadFactory) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, new HandOffQueue(taskQueue, threadSafeQueue),
threadFactory);
this.taskQueue = taskQueue;
this.threadSafeQueue = threadSafeQueue;
}
/**
* Submit a task for execution, if the maximum thread limit has been reached and all the threads are occupied,
* enqueue the task. The task is not executed by the current thread, but by a thread from the thread pool.
*
* @param task to be executed.
*/
@Override
public void execute(Runnable task) {
try {
super.execute(task);
} catch (RejectedExecutionException e) {
/* execution has been rejected either because the executor has been shut down or all worker threads are
* busy - check the former one
*/
if (isShutdown()) {
throw new RejectedExecutionException("The thread pool executor has been shut down", e);
}
/* All threads are occupied, try enqueuing the task.
* Each worker thread checks the queue after it has finished executing its task.
*/
if (threadSafeQueue) {
if (!taskQueue.offer(task)) {
throw new RejectedExecutionException("A limit of Tyrus client thread pool queue has been "
+ "reached.", e);
}
} else {
synchronized (taskQueue) {
if (!taskQueue.offer(task)) {
throw new RejectedExecutionException("A limit of Tyrus client thread pool queue has been "
+ "reached.", e);
}
}
}
/**
* There is a small time interval between a worker thread checks {@link #taskQueue} and it starts to
* block waiting for a new tasks to be submitted (Ideally checking that the {@link #taskQueue} is
* empty and starting to block at the task hand off queue would be atomic). This can be detected by
* the situation when a thread submitting a new tasks has been rejected, but not all worker threads
* are active (However this does not indicate exclusively the problematic situation).
*/
if (getActiveCount() < getMaximumPoolSize()) {
/*
* There is no guarantee that the same tasks that has been enqueued above will be dequeued,
* but trying to execute one arbitrary task by everyone in this situation is enough to clear the
* queue.
*/
Runnable dequeuedTask;
if (threadSafeQueue) {
dequeuedTask = taskQueue.poll();
} else {
synchronized (taskQueue) {
dequeuedTask = taskQueue.poll();
}
}
// check if the task has not been consumed by a worker thread after all
if (dequeuedTask != null) {
execute(dequeuedTask);
}
}
}
}
/**
* Synchronous queue that tries to empty {@link #taskQueue} before it blocks waiting for new tasks to be
* submitted. It is passed to {@link ThreadPoolExecutor}, where it is used used to hand off tasks from
* task-submitting thread to worker threads.
*/
private static class HandOffQueue extends SynchronousQueue<Runnable> {
private static final long serialVersionUID = -1607064661828834847L;
private final Queue<Runnable> taskQueue;
private final boolean threadSafeQueue;
private HandOffQueue(Queue<Runnable> taskQueue, boolean threadSafeQueue) {
this.taskQueue = taskQueue;
this.threadSafeQueue = threadSafeQueue;
}
@Override
public Runnable take() throws InterruptedException {
// try to empty the task queue
Runnable task;
if (threadSafeQueue) {
task = taskQueue.poll();
} else {
synchronized (taskQueue) {
task = taskQueue.poll();
}
}
if (task != null) {
return task;
}
// block and wait for a task to be submitted
return super.take();
}
@Override
public Runnable poll(long timeout, TimeUnit unit) throws InterruptedException {
// try to empty the task queue
Runnable task;
if (threadSafeQueue) {
task = taskQueue.poll();
} else {
synchronized (taskQueue) {
task = taskQueue.poll();
}
}
if (task != null) {
return task;
}
// block and wait for a task to be submitted
return super.poll(timeout, unit);
}
}
}
}