/
PooledConnectionProviderImpl.java
438 lines (388 loc) · 18.3 KB
/
PooledConnectionProviderImpl.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
/*
* Copyright 2016 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package io.reactivex.netty.client.pool;
import io.reactivex.netty.channel.Connection;
import io.reactivex.netty.client.ClientConnectionToChannelBridge.PooledConnectionReleaseEvent;
import io.reactivex.netty.client.HostConnector;
import io.reactivex.netty.client.events.ClientEventListener;
import io.reactivex.netty.events.Clock;
import io.reactivex.netty.events.EventPublisher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import rx.Observable;
import rx.Observable.OnSubscribe;
import rx.Observable.Operator;
import rx.Subscriber;
import rx.Subscription;
import rx.functions.Action0;
import rx.functions.Action1;
import rx.functions.Actions;
import rx.functions.Func1;
import static io.reactivex.netty.events.EventAttributeKeys.*;
import static java.util.concurrent.TimeUnit.*;
/**
* An implementation of {@link PooledConnectionProvider} that pools connections.
*
* Following are the key parameters:
*
* <ul>
<li>{@link PoolLimitDeterminationStrategy}: A strategy to determine whether a new physical connection should be
created as part of the user request.</li>
<li>{@link PoolConfig#getIdleConnectionsCleanupTimer()}: The schedule for cleaning up idle connections in the pool.</li>
<li>{@link PoolConfig#getMaxIdleTimeMillis()}: Maximum time a connection can be idle in this pool.</li>
</ul>
*
* @param <W> Type of object that is written to the client using this factory.
* @param <R> Type of object that is read from the the client using this factory.
*/
public final class PooledConnectionProviderImpl<W, R> extends PooledConnectionProvider<W, R> {
private static final Logger logger = LoggerFactory.getLogger(PooledConnectionProviderImpl.class);
private final Subscription idleConnCleanupSubscription;
private final IdleConnectionsHolder<W, R> idleConnectionsHolder;
private final PoolLimitDeterminationStrategy limitDeterminationStrategy;
private final long maxIdleTimeMillis;
private final HostConnector<W, R> hostConnector;
private volatile boolean isShutdown;
public PooledConnectionProviderImpl(PoolConfig<W, R> poolConfig, HostConnector<W, R> hostConnector) {
this.hostConnector = hostConnector;
idleConnectionsHolder = poolConfig.getIdleConnectionsHolder();
limitDeterminationStrategy = poolConfig.getPoolLimitDeterminationStrategy();
maxIdleTimeMillis = poolConfig.getMaxIdleTimeMillis();
// In case, there is no cleanup required, this observable should never give a tick.
idleConnCleanupSubscription = poolConfig.getIdleConnCleanupTicker()
.doOnError(LogErrorAction.INSTANCE)
.retry() // Retry when there is an error in timer.
.concatMap(new IdleConnectionCleanupTask())
.doOnError(new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
logger.error("Ignoring error cleaning up idle connections.",
throwable);
}
})
.retry()
.subscribe();
hostConnector.getHost()
.getCloseNotifier()
.doOnTerminate(new Action0() {
@Override
public void call() {
isShutdown = true;
idleConnCleanupSubscription.unsubscribe();
}
})
.onErrorResumeNext(new Func1<Throwable, Observable<Void>>() {
@Override
public Observable<Void> call(Throwable throwable) {
logger.error("Error listening to Host close notifications. Shutting down the pool.",
throwable);
return Observable.empty();
}
})
.subscribe(Actions.empty());
}
@Override
public Observable<Connection<R, W>> newConnectionRequest() {
return Observable.create(new OnSubscribe<Connection<R, W>>() {
@Override
public void call(Subscriber<? super Connection<R, W>> subscriber) {
if (isShutdown) {
subscriber.onError(new IllegalStateException("Connection provider is shutdown."));
}
idleConnectionsHolder.pollThisEventLoopConnections()
.concatWith(connectIfAllowed())
.filter(new Func1<PooledConnection<R, W>, Boolean>() {
@Override
public Boolean call(PooledConnection<R, W> c) {
boolean isUsable = c.isUsable();
if (!isUsable) {
discardNow(c);
}
return isUsable;
}
})
.take(1)
.lift(new ReuseSubscriberLinker())
.lift(new ConnectMetricsOperator())
.unsafeSubscribe(subscriber);
}
});
}
@Override
public Observable<Void> release(final PooledConnection<?, ?> connection) {
@SuppressWarnings("unchecked")
final PooledConnection<R, W> c = (PooledConnection<R, W>) connection;
return Observable.create(new OnSubscribe<Void>() {
@Override
public void call(Subscriber<? super Void> subscriber) {
if (null == c) {
subscriber.onCompleted();
} else {
/**
* Executing the release on the eventloop to avoid race-conditions between code cleaning up
* connection in the pipeline and the connecting being released to the pool.
*/
c.unsafeNettyChannel()
.eventLoop()
.submit(new ReleaseTask(c, subscriber));
}
}
});
}
@Override
public Observable<Void> discard(final PooledConnection<?, ?> connection) {
return connection.discard().doOnSubscribe(new Action0() {
@Override
public void call() {
EventPublisher eventPublisher = connection.unsafeNettyChannel().attr(EVENT_PUBLISHER).get();
if (eventPublisher.publishingEnabled()) {
ClientEventListener eventListener = connection.unsafeNettyChannel()
.attr(CLIENT_EVENT_LISTENER).get();
eventListener.onPooledConnectionEviction();
}
limitDeterminationStrategy.releasePermit();/*Since, an idle connection took a permit*/
}
});
}
private Observable<PooledConnection<R, W>> connectIfAllowed() {
return Observable.create(new OnSubscribe<PooledConnection<R, W>>() {
@Override
public void call(Subscriber<? super PooledConnection<R, W>> subscriber) {
final long startTimeNanos = Clock.newStartTimeNanos();
if (limitDeterminationStrategy.acquireCreationPermit(startTimeNanos, NANOSECONDS)) {
Observable<Connection<R, W>> newConnObsv = hostConnector.getConnectionProvider()
.newConnectionRequest();
newConnObsv.map(new Func1<Connection<R, W>, PooledConnection<R, W>>() {
@Override
public PooledConnection<R, W> call(Connection<R, W> connection) {
return PooledConnection.create(PooledConnectionProviderImpl.this,
maxIdleTimeMillis, connection);
}
}).doOnError(new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
limitDeterminationStrategy.releasePermit(); /*Before connect we acquired.*/
}
}).unsafeSubscribe(subscriber);
} else {
idleConnectionsHolder.poll()
.switchIfEmpty(Observable.<PooledConnection<R, W>>error(
new PoolExhaustedException("Client connection pool exhausted.")))
.unsafeSubscribe(subscriber);
}
}
});
}
private void discardNow(PooledConnection<R, W> toDiscard) {
discard(toDiscard).subscribe(Actions.empty(), new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
logger.error("Error discarding connection.", throwable);
}
});
}
private static class LogErrorAction implements Action1<Throwable> {
public static final LogErrorAction INSTANCE = new LogErrorAction();
@Override
public void call(Throwable throwable) {
logger.error("Error from idle connection cleanup timer. This will be retried.", throwable);
}
}
private class IdleConnectionCleanupTask implements Func1<Long, Observable<Void>> {
@Override
public Observable<Void> call(Long aLong) {
return idleConnectionsHolder.peek()
.map(new Func1<PooledConnection<R, W>, Void>() {
@Override
public Void call(PooledConnection<R, W> connection) {
if (!connection.isUsable()) {
idleConnectionsHolder.remove(connection);
discardNow(connection);
}
return null;
}
}).ignoreElements();
}
}
private class ReleaseTask implements Runnable {
private final PooledConnection<R, W> connection;
private final Subscriber<? super Void> subscriber;
private final long releaseStartTimeNanos;
private final EventPublisher eventPublisher;
private final ClientEventListener eventListener;
private ReleaseTask(PooledConnection<R, W> connection, Subscriber<? super Void> subscriber) {
this.connection = connection;
this.subscriber = subscriber;
releaseStartTimeNanos = Clock.newStartTimeNanos();
eventPublisher = connection.unsafeNettyChannel().attr(EVENT_PUBLISHER).get();
eventListener = connection.unsafeNettyChannel().attr(CLIENT_EVENT_LISTENER).get();
}
@Override
public void run() {
try {
connection.unsafeNettyChannel().pipeline().fireUserEventTriggered(PooledConnectionReleaseEvent.INSTANCE);
if (eventPublisher.publishingEnabled()) {
eventListener.onPoolReleaseStart();
}
if (isShutdown || !connection.isUsable()) {
discardNow(connection);
} else {
idleConnectionsHolder.add(connection);
}
if (eventPublisher.publishingEnabled()) {
eventListener.onPoolReleaseSuccess(Clock.onEndNanos(releaseStartTimeNanos), NANOSECONDS);
}
subscriber.onCompleted();
} catch (Throwable throwable) {
if (eventPublisher.publishingEnabled()) {
eventListener.onPoolReleaseFailed(Clock.onEndNanos(releaseStartTimeNanos), NANOSECONDS, throwable);
}
subscriber.onError(throwable);
}
}
}
private class ConnectMetricsOperator implements Operator<Connection<R, W>, PooledConnection<R, W>> {
@Override
public Subscriber<? super PooledConnection<R, W>> call(final Subscriber<? super Connection<R, W>> o) {
final long startTimeNanos = Clock.newStartTimeNanos();
return new Subscriber<PooledConnection<R, W>>(o) {
private volatile boolean publishingEnabled;
private volatile ClientEventListener eventListener;
@Override
public void onCompleted() {
if (publishingEnabled) {
eventListener.onPoolAcquireStart();
eventListener.onPoolAcquireSuccess(Clock.onEndNanos(startTimeNanos), NANOSECONDS);
}
o.onCompleted();
}
@Override
public void onError(Throwable e) {
if (publishingEnabled) {
/*Error means no connection was received, as it always every gets at most one connection*/
eventListener.onPoolAcquireStart();
eventListener.onPoolAcquireFailed(Clock.onEndNanos(startTimeNanos), NANOSECONDS, e);
}
o.onError(e);
}
@Override
public void onNext(PooledConnection<R, W> c) {
EventPublisher eventPublisher = c.unsafeNettyChannel().attr(EVENT_PUBLISHER).get();
if (eventPublisher.publishingEnabled()) {
publishingEnabled = true;
eventListener = c.unsafeNettyChannel().attr(CLIENT_EVENT_LISTENER).get();
}
o.onNext(c);
}
};
}
}
private boolean isEventPublishingEnabled() {
return hostConnector.getEventPublisher().publishingEnabled();
}
private class ReuseSubscriberLinker implements Operator<PooledConnection<R, W>, PooledConnection<R, W>> {
private ScalarAsyncSubscriber<R, W> onReuseSubscriber;
@Override
public Subscriber<? super PooledConnection<R, W>> call(final Subscriber<? super PooledConnection<R, W>> o) {
return new Subscriber<PooledConnection<R, W>>(o) {
@Override
public void onCompleted() {
/*This subscriber is not invoked by different threads, so don't need sychronization*/
if (null != onReuseSubscriber) {
onReuseSubscriber.onCompleted();
} else {
o.onCompleted();
}
}
@Override
public void onError(Throwable e) {
/*This subscriber is not invoked by different threads, so don't need sychronization*/
if (null != onReuseSubscriber) {
onReuseSubscriber.onError(e);
} else {
o.onError(e);
}
}
@Override
public void onNext(PooledConnection<R, W> c) {
if (c.isReused()) {
EventPublisher eventPublisher = c.unsafeNettyChannel().attr(EVENT_PUBLISHER).get();
if (eventPublisher.publishingEnabled()) {
ClientEventListener eventListener = c.unsafeNettyChannel()
.attr(CLIENT_EVENT_LISTENER).get();
eventListener.onPooledConnectionReuse();
}
onReuseSubscriber = new ScalarAsyncSubscriber<>(o);
c.reuse(onReuseSubscriber); /*Reuse will on next to the subscriber*/
} else {
o.onNext(c);
}
}
};
}
}
private static class ScalarAsyncSubscriber<R, W> extends Subscriber<PooledConnection<R, W>> {
private boolean terminated; /*Guarded by this*/
private Throwable error; /*Guarded by this*/
private boolean onNextArrived; /*Guarded by this*/
private final Subscriber<? super PooledConnection<R, W>> delegate;
private ScalarAsyncSubscriber(Subscriber<? super PooledConnection<R, W>> delegate) {
this.delegate = delegate;
}
@Override
public void onCompleted() {
boolean _onNextArrived;
synchronized (this) {
_onNextArrived = onNextArrived;
}
terminated = true;
if (_onNextArrived) {
delegate.onCompleted();
}
}
@Override
public void onError(Throwable e) {
boolean _onNextArrived;
synchronized (this) {
_onNextArrived = onNextArrived;
}
terminated = true;
error = e;
if (_onNextArrived) {
delegate.onError(e);
}
}
@Override
public void onNext(PooledConnection<R, W> conn) {
boolean _terminated;
Throwable _error;
synchronized (this) {
onNextArrived = true;
_terminated = terminated;
_error = error;
delegate.onNext(conn);
}
if (_terminated) {
if (null != error) {
delegate.onError(_error);
} else {
delegate.onCompleted();
}
}
}
}
}