Skip to content

Commit 6bf9f3b

Browse files
committed
Add configuration option to set warmupParallelism parameter for connection pool.
[#195] Signed-off-by: cty123 <ctychen2216@gmail.com>
1 parent 3ee034a commit 6bf9f3b

File tree

5 files changed

+53
-6
lines changed

5 files changed

+53
-6
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ Mono<Object> resultMono = Mono.usingWhen(pooledConnectionFactory.create(),
7272
| `registerJmx` | Whether to register the pool to JMX.
7373
| `validationDepth` | Validation depth used to validate an R2DBC connection. Defaults to `LOCAL`.
7474
| `validationQuery` | Query that will be executed just before a connection is given to you from the pool to validate that the connection to the database is still alive.
75+
| `warmupParallelism` | The concurrency level used when the allocator is subscribed to during the warmup phase. Default to `1`.
7576

7677
All other properties are driver-specific.
7778

src/main/java/io/r2dbc/pool/ConnectionPool.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,9 +276,15 @@ private InstrumentedPool<Connection> createConnectionPool(ConnectionPoolConfigur
276276
.idleResourceReuseMruOrder(); // MRU to support eviction of idle
277277

278278
if (maxSize == -1 || initialSize > 0) {
279-
builder.sizeBetween(Math.max(configuration.getMinIdle(), initialSize), maxSize == -1 ? Integer.MAX_VALUE : maxSize);
279+
builder.sizeBetween(
280+
Math.max(configuration.getMinIdle(), initialSize),
281+
maxSize == -1 ? Integer.MAX_VALUE : maxSize,
282+
configuration.getWarmupParallelism());
280283
} else {
281-
builder.sizeBetween(Math.max(configuration.getMinIdle(), initialSize), maxSize);
284+
builder.sizeBetween(
285+
Math.max(configuration.getMinIdle(), initialSize),
286+
maxSize,
287+
configuration.getWarmupParallelism());
282288
}
283289

284290
Duration backgroundEvictionInterval = configuration.getBackgroundEvictionInterval();

src/main/java/io/r2dbc/pool/ConnectionPoolConfiguration.java

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ public final class ConnectionPoolConfiguration {
5252
*/
5353
public static final Duration NO_TIMEOUT = Duration.ofMillis(-1);
5454

55+
/**
56+
* Constant indicating the default parallelism used during connection pool warmup.
57+
*/
58+
public static final int DEFAULT_WARMUP_PARALLELISM = 1;
59+
5560
@Nullable
5661
private final Scheduler allocatorSubscribeOn;
5762

@@ -99,12 +104,14 @@ public final class ConnectionPoolConfiguration {
99104
@Nullable
100105
private final String validationQuery;
101106

107+
private final int warmupParallelism;
108+
102109
private ConnectionPoolConfiguration(@Nullable Scheduler allocatorSubscribeOn, int acquireRetry, @Nullable Duration backgroundEvictionInterval, ConnectionFactory connectionFactory, Clock clock, Consumer<PoolBuilder<Connection, ?
103110
extends PoolConfig<? extends Connection>>> customizer, int initialSize, int maxSize, int minIdle, Duration maxAcquireTime, Duration maxCreateConnectionTime, Duration maxIdleTime,
104111
Duration maxLifeTime, Duration maxValidationTime, PoolMetricsRecorder metricsRecorder, @Nullable String name,
105112
@Nullable Function<? super Connection, ? extends Publisher<Void>> postAllocate,
106113
@Nullable Function<? super Connection, ? extends Publisher<Void>> preRelease, boolean registerJmx, ValidationDepth validationDepth,
107-
@Nullable String validationQuery) {
114+
@Nullable String validationQuery, int warmupParallelism) {
108115
this.allocatorSubscribeOn = allocatorSubscribeOn;
109116
this.acquireRetry = acquireRetry;
110117
this.connectionFactory = Assert.requireNonNull(connectionFactory, "ConnectionFactory must not be null");
@@ -126,6 +133,7 @@ private ConnectionPoolConfiguration(@Nullable Scheduler allocatorSubscribeOn, in
126133
this.validationDepth = validationDepth;
127134
this.validationQuery = validationQuery;
128135
this.backgroundEvictionInterval = backgroundEvictionInterval;
136+
this.warmupParallelism = warmupParallelism;
129137
}
130138

131139
/**
@@ -237,6 +245,10 @@ String getValidationQuery() {
237245
return this.validationQuery;
238246
}
239247

248+
int getWarmupParallelism() {
249+
return this.warmupParallelism;
250+
}
251+
240252
/**
241253
* A builder for {@link ConnectionPoolConfiguration} instances.
242254
* <p>
@@ -293,6 +305,8 @@ public static final class Builder {
293305

294306
private ValidationDepth validationDepth = ValidationDepth.LOCAL;
295307

308+
private Integer warmupParallelism = DEFAULT_WARMUP_PARALLELISM;
309+
296310
private Builder() {
297311
}
298312

@@ -583,6 +597,23 @@ public Builder validationQuery(String validationQuery) {
583597
return this;
584598
}
585599

600+
/**
601+
* Configure the concurrency level used when the allocator is subscribed to during the warmup phase.
602+
*
603+
* @param warmupParallelism Specifies the concurrency level used when the allocator is subscribed to during the warmup phase, if any.
604+
* During warmup, resources that can be pre-allocated will be created eagerly, but at most {@code warmupParallelism} resources are
605+
* subscribed to at the same time.
606+
* @return this {@link Builder}
607+
* @throws IllegalArgumentException if {@code warmupParallelism} is negative
608+
*/
609+
public Builder warmupParallelism(int warmupParallelism) {
610+
if (warmupParallelism < 0) {
611+
throw new IllegalArgumentException("warmupParallelism must not be negative");
612+
}
613+
this.warmupParallelism = warmupParallelism;
614+
return this;
615+
}
616+
586617
/**
587618
* Returns a configured {@link ConnectionPoolConfiguration}.
588619
*
@@ -596,7 +627,7 @@ public ConnectionPoolConfiguration build() {
596627
this.clock, this.customizer, this.initialSize, this.maxSize, this.minIdle,
597628
this.maxAcquireTime, this.maxCreateConnectionTime, this.maxIdleTime, this.maxLifeTime, this.maxValidationTime,
598629
this.metricsRecorder, this.name, this.postAllocate, this.preRelease, this.registerJmx,
599-
this.validationDepth, this.validationQuery
630+
this.validationDepth, this.validationQuery, this.warmupParallelism
600631
);
601632
}
602633

src/main/java/io/r2dbc/pool/PoolingConnectionFactoryProvider.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,11 @@ public class PoolingConnectionFactoryProvider implements ConnectionFactoryProvid
149149
*/
150150
public static final Option<ValidationDepth> VALIDATION_DEPTH = Option.valueOf("validationDepth");
151151

152+
/**
153+
* WarmupParallelism {@link Option}.
154+
*/
155+
public static final Option<Integer> WARMUP_PARALLELISM = Option.valueOf("warmupParallelism");
156+
152157
private static final String COLON = ":";
153158

154159
/**
@@ -210,6 +215,7 @@ static ConnectionPoolConfiguration buildConfiguration(ConnectionFactoryOptions c
210215
mapper.from(REGISTER_JMX).as(OptionMapper::toBoolean).to(builder::registerJmx);
211216
mapper.fromExact(VALIDATION_QUERY).to(builder::validationQuery);
212217
mapper.from(VALIDATION_DEPTH).as(validationDepth -> OptionMapper.toEnum(validationDepth, ValidationDepth.class)).to(builder::validationDepth);
218+
mapper.from(WARMUP_PARALLELISM).as(OptionMapper::toInteger).to(builder::warmupParallelism);
213219

214220
return builder.build();
215221
}

src/test/java/io/r2dbc/pool/ConnectionPoolConfigurationUnitTests.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ void configuration() {
4949
.maxSize(20)
5050
.name("bar")
5151
.registerJmx(true)
52+
.warmupParallelism(99)
5253
.build();
5354

5455
assertThat(configuration)
@@ -62,7 +63,8 @@ void configuration() {
6263
.hasFieldOrPropertyWithValue("initialSize", 2)
6364
.hasFieldOrPropertyWithValue("maxSize", 20)
6465
.hasFieldOrPropertyWithValue("name", "bar")
65-
.hasFieldOrPropertyWithValue("registerJmx", true);
66+
.hasFieldOrPropertyWithValue("registerJmx", true)
67+
.hasFieldOrPropertyWithValue("warmupParallelism", 99);
6668
}
6769

6870
@Test
@@ -82,7 +84,8 @@ void configurationDefaults() {
8284
.hasFieldOrPropertyWithValue("maxValidationTime", Duration.ofMillis(-1))
8385
.hasFieldOrPropertyWithValue("initialSize", 10)
8486
.hasFieldOrPropertyWithValue("maxSize", 10)
85-
.hasFieldOrPropertyWithValue("registerJmx", false);
87+
.hasFieldOrPropertyWithValue("registerJmx", false)
88+
.hasFieldOrPropertyWithValue("warmupParallelism", 1);
8689
}
8790

8891
@Test

0 commit comments

Comments
 (0)