-
Notifications
You must be signed in to change notification settings - Fork 628
/
PooledConnectionProvider.java
573 lines (507 loc) · 20 KB
/
PooledConnectionProvider.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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
/*
* Copyright (c) 2018-2022 VMware, Inc. or its affiliates, All Rights Reserved.
*
* 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
*
* https://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 reactor.netty.resources;
import io.netty.channel.Channel;
import io.netty.channel.EventLoop;
import io.netty.channel.EventLoopGroup;
import io.netty.resolver.AddressResolverGroup;
import org.reactivestreams.Publisher;
import reactor.core.CoreSubscriber;
import reactor.core.Disposable;
import reactor.core.publisher.Mono;
import reactor.core.publisher.MonoSink;
import reactor.core.scheduler.Schedulers;
import reactor.netty.Connection;
import reactor.netty.ConnectionObserver;
import reactor.netty.ReactorNetty;
import reactor.netty.transport.TransportConfig;
import reactor.netty.internal.util.MapUtils;
import reactor.pool.InstrumentedPool;
import reactor.pool.Pool;
import reactor.pool.PoolBuilder;
import reactor.pool.PoolConfig;
import reactor.pool.PooledRef;
import reactor.pool.PooledRefMetadata;
import reactor.pool.decorators.GracefulShutdownInstrumentedPool;
import reactor.pool.decorators.InstrumentedPoolDecorators;
import reactor.pool.introspection.SamplingAllocationStrategy;
import reactor.util.Logger;
import reactor.util.Loggers;
import reactor.util.Metrics;
import reactor.util.annotation.Nullable;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.time.Clock;
import java.time.Duration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import static reactor.netty.ReactorNetty.format;
import static reactor.netty.resources.ConnectionProvider.ConnectionPoolSpec.PENDING_ACQUIRE_MAX_COUNT_NOT_SPECIFIED;
/**
* Base {@link ConnectionProvider} implementation.
*
* @param <T> the poolable resource
* @author Violeta Georgieva
* @since 1.0.0
*/
public abstract class PooledConnectionProvider<T extends Connection> implements ConnectionProvider {
/**
* Context key used to propagate the caller event loop in the connection pool subscription.
*/
protected final static String CONTEXT_CALLER_EVENTLOOP = "callereventloop";
final PoolFactory<T> defaultPoolFactory;
final Map<SocketAddress, PoolFactory<T>> poolFactoryPerRemoteHost = new HashMap<>();
final ConcurrentMap<PoolKey, InstrumentedPool<T>> channelPools = new ConcurrentHashMap<>();
final Builder builder;
final String name;
final Duration inactivePoolDisposeInterval;
final Duration poolInactivity;
final Duration disposeTimeout;
final Map<SocketAddress, Integer> maxConnections = new HashMap<>();
Mono<Void> onDispose;
protected PooledConnectionProvider(Builder builder) {
this(builder, null);
}
// Used only for testing purposes
PooledConnectionProvider(Builder builder, @Nullable Clock clock) {
this.builder = builder;
this.name = builder.name;
this.inactivePoolDisposeInterval = builder.inactivePoolDisposeInterval;
this.poolInactivity = builder.poolInactivity;
this.disposeTimeout = builder.disposeTimeout;
this.defaultPoolFactory = new PoolFactory<>(builder, builder.disposeTimeout, clock);
for (Map.Entry<SocketAddress, ConnectionPoolSpec<?>> entry : builder.confPerRemoteHost.entrySet()) {
poolFactoryPerRemoteHost.put(entry.getKey(), new PoolFactory<>(entry.getValue(), builder.disposeTimeout));
maxConnections.put(entry.getKey(), entry.getValue().maxConnections);
}
this.onDispose = Mono.empty();
scheduleInactivePoolsDisposal();
}
@Override
public final Mono<? extends Connection> acquire(
TransportConfig config,
ConnectionObserver connectionObserver,
@Nullable Supplier<? extends SocketAddress> remote,
@Nullable AddressResolverGroup<?> resolverGroup) {
Objects.requireNonNull(config, "config");
Objects.requireNonNull(connectionObserver, "connectionObserver");
Objects.requireNonNull(remote, "remoteAddress");
Objects.requireNonNull(resolverGroup, "resolverGroup");
return Mono.create(sink -> {
SocketAddress remoteAddress = Objects.requireNonNull(remote.get(), "Remote Address supplier returned null");
PoolKey holder = new PoolKey(remoteAddress, config.channelHash());
PoolFactory<T> poolFactory = poolFactory(remoteAddress);
InstrumentedPool<T> pool = MapUtils.computeIfAbsent(channelPools, holder, poolKey -> {
if (log.isDebugEnabled()) {
log.debug("Creating a new [{}] client pool [{}] for [{}]", name, poolFactory, remoteAddress);
}
InstrumentedPool<T> newPool = createPool(config, poolFactory, remoteAddress, resolverGroup);
if (poolFactory.metricsEnabled || config.metricsRecorder() != null) {
// registrar is null when metrics are enabled on HttpClient level or
// with the `metrics(boolean metricsEnabled)` method on ConnectionProvider
String id = poolKey.hashCode() + "";
if (poolFactory.registrar != null) {
poolFactory.registrar.get().registerMetrics(name, id, remoteAddress,
new DelegatingConnectionPoolMetrics(newPool.metrics()));
}
else if (Metrics.isInstrumentationAvailable()) {
// work directly with the pool otherwise a weak reference is needed to ConnectionPoolMetrics
// we don't want to keep another map with weak references
registerDefaultMetrics(id, remoteAddress, newPool.metrics());
}
}
return newPool;
});
EventLoop eventLoop;
if (sink.contextView().hasKey(CONTEXT_CALLER_EVENTLOOP)) {
eventLoop = sink.contextView().get(CONTEXT_CALLER_EVENTLOOP);
}
else {
EventLoopGroup group = config.loopResources().onClient(config.isPreferNative());
if (group instanceof ColocatedEventLoopGroup) {
eventLoop = ((ColocatedEventLoopGroup) group).nextInternal();
}
else {
eventLoop = null;
}
}
Mono<PooledRef<T>> mono = pool.acquire(Duration.ofMillis(poolFactory.pendingAcquireTimeout));
if (eventLoop != null) {
mono = mono.contextWrite(ctx -> ctx.put(CONTEXT_CALLER_EVENTLOOP, eventLoop));
}
mono.subscribe(createDisposableAcquire(config, connectionObserver,
poolFactory.pendingAcquireTimeout, pool, sink));
});
}
@Override
public final Mono<Void> disposeLater() {
return Mono.defer(() -> {
List<Mono<Void>> pools;
pools = channelPools.entrySet()
.stream()
.map(e -> {
Pool<T> pool = e.getValue();
if (pool instanceof GracefulShutdownInstrumentedPool) {
return ((GracefulShutdownInstrumentedPool<T>) pool)
.disposeGracefully(disposeTimeout)
.onErrorResume(t -> {
log.error("Connection pool for [{}] didn't shut down gracefully", e.getKey(), t);
return Mono.empty();
});
}
return pool.disposeLater();
})
.collect(Collectors.toList());
if (pools.isEmpty()) {
return onDispose;
}
channelPools.clear();
return onDispose.and(Mono.when(pools));
});
}
@Override
public final void disposeWhen(SocketAddress address) {
List<Map.Entry<PoolKey, InstrumentedPool<T>>> toDispose;
toDispose = channelPools.entrySet()
.stream()
.filter(p -> compareAddresses(p.getKey().holder, address))
.collect(Collectors.toList());
toDispose.forEach(e -> {
if (channelPools.remove(e.getKey(), e.getValue())) {
if (log.isDebugEnabled()) {
log.debug("ConnectionProvider[name={}]: Disposing pool for [{}]", name, e.getKey().fqdn);
}
e.getValue().dispose();
}
});
}
@Override
public final boolean isDisposed() {
return channelPools.isEmpty() || channelPools.values()
.stream()
.allMatch(Disposable::isDisposed);
}
@Override
public int maxConnections() {
return defaultPoolFactory.maxConnections;
}
@Override
public Map<SocketAddress, Integer> maxConnectionsPerHost() {
return maxConnections;
}
@Override
public Builder mutate() {
return new Builder(builder);
}
@Override
public String name() {
return name;
}
public void onDispose(Mono<Void> disposeMono) {
onDispose = onDispose.and(disposeMono);
}
protected abstract CoreSubscriber<PooledRef<T>> createDisposableAcquire(
TransportConfig config,
ConnectionObserver connectionObserver,
long pendingAcquireTimeout,
InstrumentedPool<T> pool,
MonoSink<Connection> sink);
protected abstract InstrumentedPool<T> createPool(
TransportConfig config,
PoolFactory<T> poolFactory,
SocketAddress remoteAddress,
AddressResolverGroup<?> resolverGroup);
protected PoolFactory<T> poolFactory(SocketAddress remoteAddress) {
return poolFactoryPerRemoteHost.getOrDefault(remoteAddress, defaultPoolFactory);
}
protected void registerDefaultMetrics(String id, SocketAddress remoteAddress, InstrumentedPool.PoolMetrics metrics) {
MicrometerPooledConnectionProviderMeterRegistrar.INSTANCE.registerMetrics(name, id, remoteAddress, metrics);
}
final boolean compareAddresses(SocketAddress origin, SocketAddress target) {
if (origin.equals(target)) {
return true;
}
else if (origin instanceof InetSocketAddress && target instanceof InetSocketAddress) {
InetSocketAddress isaOrigin = (InetSocketAddress) origin;
InetSocketAddress isaTarget = (InetSocketAddress) target;
if (isaOrigin.getPort() == isaTarget.getPort()) {
InetAddress iaTarget = isaTarget.getAddress();
//CHECKSTYLE:OFF
// "Unnecessary parentheses around expression"
return (iaTarget != null && iaTarget.isAnyLocalAddress()) ||
Objects.equals(isaOrigin.getHostString(), isaTarget.getHostString());
//CHECKSTYLE:ON
}
}
return false;
}
protected static void logPoolState(Channel channel, InstrumentedPool<? extends Connection> pool, String msg) {
logPoolState(channel, pool, msg, null);
}
protected static void logPoolState(Channel channel, InstrumentedPool<? extends Connection> pool, String msg, @Nullable Throwable t) {
InstrumentedPool.PoolMetrics metrics = pool.metrics();
log.debug(format(channel, "{}, now: {} active connections, {} inactive connections and {} pending acquire requests."),
msg,
metrics.acquiredSize(),
metrics.idleSize(),
metrics.pendingAcquireSize(),
t == null ? "" : t);
}
final void scheduleInactivePoolsDisposal() {
if (!inactivePoolDisposeInterval.isZero()) {
Schedulers.parallel()
.schedule(this::disposeInactivePoolsInBackground, inactivePoolDisposeInterval.toMillis(), TimeUnit.MILLISECONDS);
}
}
final void disposeInactivePoolsInBackground() {
if (!channelPools.isEmpty()) {
List<Map.Entry<PoolKey, InstrumentedPool<T>>> toDispose;
toDispose = channelPools.entrySet()
.stream()
.filter(p -> p.getValue().metrics().isInactiveForMoreThan(poolInactivity))
.collect(Collectors.toList());
toDispose.forEach(e -> {
if (channelPools.remove(e.getKey(), e.getValue())) {
if (log.isDebugEnabled()) {
log.debug("ConnectionProvider[name={}]: Disposing inactive pool for [{}]", name, e.getKey().fqdn);
}
e.getValue().dispose();
}
});
}
scheduleInactivePoolsDisposal();
}
static final Logger log = Loggers.getLogger(PooledConnectionProvider.class);
protected static final class PoolFactory<T extends Connection> {
static final double DEFAULT_POOL_GET_PERMITS_SAMPLING_RATE;
static {
double getPermitsSamplingRate =
Double.parseDouble(System.getProperty(ReactorNetty.POOL_GET_PERMITS_SAMPLING_RATE, "0"));
if (getPermitsSamplingRate > 1d) {
DEFAULT_POOL_GET_PERMITS_SAMPLING_RATE = 0;
log.warn("Invalid configuration [" + ReactorNetty.POOL_GET_PERMITS_SAMPLING_RATE + "=" + getPermitsSamplingRate +
"], the value must be between 0d and 1d (percentage). SamplingAllocationStrategy in not enabled.");
}
else {
DEFAULT_POOL_GET_PERMITS_SAMPLING_RATE = getPermitsSamplingRate;
}
}
static final double DEFAULT_POOL_RETURN_PERMITS_SAMPLING_RATE;
static {
double returnPermitsSamplingRate =
Double.parseDouble(System.getProperty(ReactorNetty.POOL_RETURN_PERMITS_SAMPLING_RATE, "0"));
if (returnPermitsSamplingRate > 1d) {
DEFAULT_POOL_RETURN_PERMITS_SAMPLING_RATE = 0;
log.warn("Invalid configuration [" + ReactorNetty.POOL_RETURN_PERMITS_SAMPLING_RATE + "=" + returnPermitsSamplingRate +
"], the value must be between 0d and 1d (percentage). SamplingAllocationStrategy is enabled.");
}
else {
DEFAULT_POOL_RETURN_PERMITS_SAMPLING_RATE = returnPermitsSamplingRate;
}
}
final Duration evictionInterval;
final String leasingStrategy;
final int maxConnections;
final long maxIdleTime;
final long maxLifeTime;
final boolean metricsEnabled;
final int pendingAcquireMaxCount;
final long pendingAcquireTimeout;
final Supplier<? extends MeterRegistrar> registrar;
final Clock clock;
final Duration disposeTimeout;
final BiFunction<Runnable, Duration, Disposable> pendingAcquireTimer;
final AllocationStrategy<?> allocationStrategy;
PoolFactory(ConnectionPoolSpec<?> conf, Duration disposeTimeout) {
this(conf, disposeTimeout, null);
}
// Used only for testing purposes
PoolFactory(ConnectionPoolSpec<?> conf, Duration disposeTimeout, @Nullable Clock clock) {
this.evictionInterval = conf.evictionInterval;
this.leasingStrategy = conf.leasingStrategy;
this.maxConnections = conf.maxConnections;
this.maxIdleTime = conf.maxIdleTime != null ? conf.maxIdleTime.toMillis() : -1;
this.maxLifeTime = conf.maxLifeTime != null ? conf.maxLifeTime.toMillis() : -1;
this.metricsEnabled = conf.metricsEnabled;
this.pendingAcquireMaxCount = conf.pendingAcquireMaxCount == PENDING_ACQUIRE_MAX_COUNT_NOT_SPECIFIED ?
2 * conf.maxConnections : conf.pendingAcquireMaxCount;
this.pendingAcquireTimeout = conf.pendingAcquireTimeout.toMillis();
this.registrar = conf.registrar;
this.clock = clock;
this.disposeTimeout = disposeTimeout;
this.pendingAcquireTimer = conf.pendingAcquireTimer;
this.allocationStrategy = conf.allocationStrategy;
}
public InstrumentedPool<T> newPool(
Publisher<T> allocator,
@Nullable reactor.pool.AllocationStrategy allocationStrategy, // this is not used but kept for backwards compatibility
Function<T, Publisher<Void>> destroyHandler,
BiPredicate<T, PooledRefMetadata> evictionPredicate) {
if (disposeTimeout != null) {
return newPoolInternal(allocator, destroyHandler, evictionPredicate)
.buildPoolAndDecorateWith(InstrumentedPoolDecorators::gracefulShutdown);
}
return newPoolInternal(allocator, destroyHandler, evictionPredicate).buildPool();
}
public InstrumentedPool<T> newPool(
Publisher<T> allocator,
@Nullable reactor.pool.AllocationStrategy allocationStrategy, // this is not used but kept for backwards compatibility
Function<T, Publisher<Void>> destroyHandler,
BiPredicate<T, PooledRefMetadata> evictionPredicate,
Function<PoolConfig<T>, InstrumentedPool<T>> poolFactory) {
if (disposeTimeout != null) {
return newPoolInternal(allocator, destroyHandler, evictionPredicate)
.build(poolFactory.andThen(InstrumentedPoolDecorators::gracefulShutdown));
}
return newPoolInternal(allocator, destroyHandler, evictionPredicate).build(poolFactory);
}
PoolBuilder<T, PoolConfig<T>> newPoolInternal(
Publisher<T> allocator,
Function<T, Publisher<Void>> destroyHandler,
BiPredicate<T, PooledRefMetadata> evictionPredicate) {
PoolBuilder<T, PoolConfig<T>> poolBuilder =
PoolBuilder.from(allocator)
.destroyHandler(destroyHandler)
.evictionPredicate(evictionPredicate
//CHECKSTYLE:OFF
// "Unnecessary parentheses around expression"
.or((poolable, meta) -> (maxIdleTime != -1 && meta.idleTime() >= maxIdleTime)
|| (maxLifeTime != -1 && meta.lifeTime() >= maxLifeTime))) //CHECKSTYLE:ON
.maxPendingAcquire(pendingAcquireMaxCount)
.evictInBackground(evictionInterval);
if (DEFAULT_POOL_GET_PERMITS_SAMPLING_RATE > 0d && DEFAULT_POOL_GET_PERMITS_SAMPLING_RATE <= 1d
&& DEFAULT_POOL_RETURN_PERMITS_SAMPLING_RATE > 0d && DEFAULT_POOL_RETURN_PERMITS_SAMPLING_RATE <= 1d) {
poolBuilder = poolBuilder.allocationStrategy(SamplingAllocationStrategy.sizeBetweenWithSampling(
0,
maxConnections,
DEFAULT_POOL_GET_PERMITS_SAMPLING_RATE,
DEFAULT_POOL_RETURN_PERMITS_SAMPLING_RATE));
}
else {
if (allocationStrategy == null) {
poolBuilder = poolBuilder.sizeBetween(0, maxConnections);
}
else {
poolBuilder = poolBuilder.allocationStrategy(new DelegatingAllocationStrategy(allocationStrategy.copy()));
}
}
if (pendingAcquireTimer != null) {
poolBuilder = poolBuilder.pendingAcquireTimer(pendingAcquireTimer);
}
if (clock != null) {
poolBuilder = poolBuilder.clock(clock);
}
if (LEASING_STRATEGY_FIFO.equals(leasingStrategy)) {
poolBuilder = poolBuilder.idleResourceReuseLruOrder();
}
else {
poolBuilder = poolBuilder.idleResourceReuseMruOrder();
}
return poolBuilder;
}
@Nullable
public AllocationStrategy<?> allocationStrategy() {
return allocationStrategy;
}
public long maxIdleTime() {
return this.maxIdleTime;
}
public long maxLifeTime() {
return maxLifeTime;
}
@Override
public String toString() {
return "PoolFactory{" +
"evictionInterval=" + evictionInterval +
", leasingStrategy=" + leasingStrategy +
", maxConnections=" + maxConnections +
", maxIdleTime=" + maxIdleTime +
", maxLifeTime=" + maxLifeTime +
", metricsEnabled=" + metricsEnabled +
", pendingAcquireMaxCount=" + pendingAcquireMaxCount +
", pendingAcquireTimeout=" + pendingAcquireTimeout +
'}';
}
static final class DelegatingAllocationStrategy implements reactor.pool.AllocationStrategy {
final AllocationStrategy<?> delegate;
DelegatingAllocationStrategy(AllocationStrategy<?> delegate) {
this.delegate = delegate;
}
@Override
public int estimatePermitCount() {
return delegate.estimatePermitCount();
}
@Override
public int getPermits(int desired) {
return delegate.getPermits(desired);
}
@Override
public int permitGranted() {
return delegate.permitGranted();
}
@Override
public int permitMinimum() {
return delegate.permitMinimum();
}
@Override
public int permitMaximum() {
return delegate.permitMaximum();
}
@Override
public void returnPermits(int returned) {
delegate.returnPermits(returned);
}
}
}
static final class PoolKey {
final String fqdn;
final SocketAddress holder;
final int pipelineKey;
PoolKey(SocketAddress holder, int pipelineKey) {
this.fqdn = holder.toString();
this.holder = holder;
this.pipelineKey = pipelineKey;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
PoolKey poolKey = (PoolKey) o;
return Objects.equals(fqdn, poolKey.fqdn) &&
Objects.equals(holder, poolKey.holder) &&
pipelineKey == poolKey.pipelineKey;
}
@Override
public int hashCode() {
return Objects.hash(fqdn, holder, pipelineKey);
}
}
}