Skip to content

Commit

Permalink
add maxIdleTime parameter to mongodb client configuration
Browse files Browse the repository at this point in the history
Signed-off-by: Dominik Guggemos <dominik.guggemos@bosch.io>
  • Loading branch information
dguggemos committed Jul 8, 2022
1 parent fd600d4 commit 5fd6d33
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 4 deletions.
3 changes: 3 additions & 0 deletions internal/utils/config/src/main/resources/ditto-mongo.conf
Expand Up @@ -65,6 +65,9 @@ ditto.mongodb {
maxSize = 100
maxSize = ${?MONGO_DB_CONNECTION_POOL_SIZE}

maxIdleTime = -1s
maxIdleTime = ${?MONGO_DB_CONNECTION_POOL_IDLE_TIME}

maxWaitTime = 30s
maxWaitTime = ${?MONGO_DB_CONNECTION_POOL_WAIT_TIME}
}
Expand Down
Expand Up @@ -118,6 +118,15 @@ interface GeneralPropertiesStep {
*/
GeneralPropertiesStep connectionPoolMaxSize(int maxSize);

/**
* Sets the maximum amount of time a pooled connection is allowed to idle before closing the connection.
* Default is {@code 0}ms (no upper bound).
*
* @param maxConnectionIdleTime maximum allowed time for a connection to idle.
* @return this builder instance to allow method chaining.
*/
GeneralPropertiesStep connectionPoolMaxIdleTime(Duration maxConnectionIdleTime);

/**
* Sets the maximum time that a thread may wait for a connection to become available.
* Default is {@code 2} minutes.
Expand Down
Expand Up @@ -298,6 +298,9 @@ static GeneralPropertiesStep newInstance(final MongoDbConfig mongoDbConfig) {
final MongoDbConfig.ConnectionPoolConfig connectionPoolConfig = mongoDbConfig.getConnectionPoolConfig();
builder.connectionPoolMinSize(connectionPoolConfig.getMinSize());
builder.connectionPoolMaxSize(connectionPoolConfig.getMaxSize());
if (!connectionPoolConfig.getMaxIdleTime().isNegative()) {
builder.connectionPoolMaxIdleTime(connectionPoolConfig.getMaxIdleTime());
}
builder.connectionPoolMaxWaitTime(connectionPoolConfig.getMaxWaitTime());
builder.enableJmxListener(connectionPoolConfig.isJmxListenerEnabled());

Expand Down Expand Up @@ -360,6 +363,12 @@ public MongoClientWrapperBuilder connectionPoolMaxSize(final int maxSize) {
return this;
}

@Override
public MongoClientWrapperBuilder connectionPoolMaxIdleTime(final Duration maxConnectionIdleTime) {
mongoClientSettingsBuilder.applyToConnectionPoolSettings(builder -> builder.maxConnectionIdleTime(maxConnectionIdleTime.toMillis(), TimeUnit.MILLISECONDS));
return this;
}

@Override
public MongoClientWrapperBuilder connectionPoolMaxWaitTime(final Duration maxPoolWaitTime) {
checkNotNull(maxPoolWaitTime, "maxPoolWaitTime");
Expand Down
Expand Up @@ -32,12 +32,14 @@ public final class DefaultConnectionPoolConfig implements MongoDbConfig.Connecti

private final int minSize;
private final int maxSize;
private final Duration maxIdleTime;
private final Duration maxWaitTime;
private final boolean jmxListenerEnabled;

private DefaultConnectionPoolConfig(final ScopedConfig config) {
minSize = config.getNonNegativeIntOrThrow(ConnectionPoolConfigValue.MIN_SIZE);
maxSize = config.getNonNegativeIntOrThrow(ConnectionPoolConfigValue.MAX_SIZE);
maxIdleTime = config.getDuration(ConnectionPoolConfigValue.MAX_IDLE_TIME.getConfigPath());
maxWaitTime = config.getNonNegativeDurationOrThrow(ConnectionPoolConfigValue.MAX_WAIT_TIME);
jmxListenerEnabled = config.getBoolean(ConnectionPoolConfigValue.JMX_LISTENER_ENABLED.getConfigPath());
}
Expand All @@ -64,6 +66,11 @@ public int getMaxSize() {
return maxSize;
}

@Override
public Duration getMaxIdleTime() {
return maxIdleTime;
}

@Override
public Duration getMaxWaitTime() {
return maxWaitTime;
Expand All @@ -86,12 +93,13 @@ public boolean equals(final Object o) {
return minSize == that.minSize &&
maxSize == that.maxSize &&
jmxListenerEnabled == that.jmxListenerEnabled &&
Objects.equals(maxIdleTime, that.maxIdleTime) &&
Objects.equals(maxWaitTime, that.maxWaitTime);
}

@Override
public int hashCode() {
return Objects.hash(minSize, maxSize, maxWaitTime, jmxListenerEnabled);
return Objects.hash(minSize, maxSize, maxWaitTime, maxIdleTime, jmxListenerEnabled);
}

@Override
Expand All @@ -100,6 +108,7 @@ public String toString() {
"minSize=" + minSize +
", maxSize=" + maxSize +
", maxWaitTime=" + maxWaitTime +
", maxIdleTime=" + maxIdleTime +
", jmxListenerEnabled=" + jmxListenerEnabled +
"]";
}
Expand Down
Expand Up @@ -231,6 +231,13 @@ interface ConnectionPoolConfig {
*/
int getMaxSize();

/**
* Returns the maximum amount of time a pooled connection is allowed to idle before closing the connection.
*
* @return the maximum amount of time a pooled connection is allowed to idle.
*/
Duration getMaxIdleTime();

/**
* Returns the maximum time to wait for a connection to become available.
*
Expand Down Expand Up @@ -261,6 +268,12 @@ enum ConnectionPoolConfigValue implements KnownConfigValue {
*/
MAX_SIZE("maxSize", 100),

/**
* The maximum amount of time a pooled connection is allowed to idle before closing the connection.
* Set to negative value to ignore and use Mongo Client default or value provided with URI.
*/
MAX_IDLE_TIME("maxIdleTime", Duration.ofSeconds(-1)),

/**
* The maximum time to wait for a connection to become available.
*/
Expand Down
Expand Up @@ -59,9 +59,15 @@ public void testHashCodeAndEquals() {
public void underTestReturnsDefaultValuesWhenBaseConfigWasEmpty() {
final DefaultConnectionPoolConfig underTest = DefaultConnectionPoolConfig.of(ConfigFactory.empty());

softly.assertThat(underTest.getMinSize())
.as(MongoDbConfig.ConnectionPoolConfig.ConnectionPoolConfigValue.MIN_SIZE.getConfigPath())
.isEqualTo(MongoDbConfig.ConnectionPoolConfig.ConnectionPoolConfigValue.MIN_SIZE.getDefaultValue());
softly.assertThat(underTest.getMaxSize())
.as(MongoDbConfig.ConnectionPoolConfig.ConnectionPoolConfigValue.MAX_SIZE.getConfigPath())
.isEqualTo(MongoDbConfig.ConnectionPoolConfig.ConnectionPoolConfigValue.MAX_SIZE.getDefaultValue());
softly.assertThat(underTest.getMaxIdleTime())
.as(MongoDbConfig.ConnectionPoolConfig.ConnectionPoolConfigValue.MAX_IDLE_TIME.getConfigPath())
.isEqualTo(MongoDbConfig.ConnectionPoolConfig.ConnectionPoolConfigValue.MAX_IDLE_TIME.getDefaultValue());
softly.assertThat(underTest.getMaxWaitTime())
.as(MongoDbConfig.ConnectionPoolConfig.ConnectionPoolConfigValue.MAX_WAIT_TIME.getConfigPath())
.isEqualTo(MongoDbConfig.ConnectionPoolConfig.ConnectionPoolConfigValue.MAX_WAIT_TIME.getDefaultValue());
Expand All @@ -74,9 +80,15 @@ public void underTestReturnsDefaultValuesWhenBaseConfigWasEmpty() {
public void underTestReturnsValuesOfBaseConfig() {
final DefaultConnectionPoolConfig underTest = DefaultConnectionPoolConfig.of(connectionPoolTestConfig);

softly.assertThat(underTest.getMinSize())
.as(MongoDbConfig.ConnectionPoolConfig.ConnectionPoolConfigValue.MIN_SIZE.getConfigPath())
.isEqualTo(10);
softly.assertThat(underTest.getMaxSize())
.as(MongoDbConfig.ConnectionPoolConfig.ConnectionPoolConfigValue.MAX_SIZE.getConfigPath())
.isEqualTo(1_000);
softly.assertThat(underTest.getMaxIdleTime())
.as(MongoDbConfig.ConnectionPoolConfig.ConnectionPoolConfigValue.MAX_IDLE_TIME.getConfigPath())
.isEqualTo(Duration.ofMinutes(5L));
softly.assertThat(underTest.getMaxWaitTime())
.as(MongoDbConfig.ConnectionPoolConfig.ConnectionPoolConfigValue.MAX_WAIT_TIME.getConfigPath())
.isEqualTo(Duration.ofSeconds(42L));
Expand Down
Expand Up @@ -89,9 +89,21 @@ public void defaultMongodbConfigContainsExactlyValuesOfResourceConfigFile() {
softly.assertThat(optionsConfig.isSslEnabled()).isFalse();
});
softly.assertThat(underTest.getConnectionPoolConfig()).satisfies(connectionPoolConfig -> {
softly.assertThat(connectionPoolConfig.getMaxSize()).isEqualTo(1_000);
softly.assertThat(connectionPoolConfig.getMaxWaitTime()).isEqualTo(Duration.ofSeconds(42L));
softly.assertThat(connectionPoolConfig.isJmxListenerEnabled()).isTrue();
softly.assertThat(connectionPoolConfig.getMinSize())
.as(MongoDbConfig.ConnectionPoolConfig.ConnectionPoolConfigValue.MIN_SIZE.getConfigPath())
.isEqualTo(10);
softly.assertThat(connectionPoolConfig.getMaxSize())
.as(MongoDbConfig.ConnectionPoolConfig.ConnectionPoolConfigValue.MAX_SIZE.getConfigPath())
.isEqualTo(1_000);
softly.assertThat(connectionPoolConfig.getMaxIdleTime())
.as(MongoDbConfig.ConnectionPoolConfig.ConnectionPoolConfigValue.MAX_IDLE_TIME.getConfigPath())
.isEqualTo(Duration.ofMinutes(5L));
softly.assertThat(connectionPoolConfig.getMaxWaitTime())
.as(MongoDbConfig.ConnectionPoolConfig.ConnectionPoolConfigValue.MAX_WAIT_TIME.getConfigPath())
.isEqualTo(Duration.ofSeconds(42L));
softly.assertThat(connectionPoolConfig.isJmxListenerEnabled())
.as(MongoDbConfig.ConnectionPoolConfig.ConnectionPoolConfigValue.JMX_LISTENER_ENABLED.getConfigPath())
.isTrue();
});
softly.assertThat(underTest.getCircuitBreakerConfig()).satisfies(circuitBreakerConfig -> {
softly.assertThat(circuitBreakerConfig.getMaxFailures()).isEqualTo(23);
Expand Down
2 changes: 2 additions & 0 deletions internal/utils/persistence/src/test/resources/pool-test.conf
@@ -1,5 +1,7 @@
pool {
minSize = 10
maxSize = 1000
maxIdleTime = 5m
maxWaitTime = 42s
jmxListenerEnabled = true
}
3 changes: 3 additions & 0 deletions thingsearch/service/src/main/resources/search.conf
Expand Up @@ -15,6 +15,9 @@ ditto {
minSize = ${?MONGO_DB_CONNECTION_MIN_POOL_SIZE}
maxSize = 1000
maxSize = ${?MONGO_DB_CONNECTION_POOL_SIZE}
# 0 = no limit, negative value = ignore, use client default
maxIdleTime = -1s
maxIdleTime = ${?MONGO_DB_CONNECTION_POOL_IDLE_TIME}
maxWaitTime = 30s
maxWaitTime = ${?MONGO_DB_CONNECTION_POOL_WAIT_TIME}
jmxListenerEnabled = false
Expand Down
2 changes: 2 additions & 0 deletions thingsearch/service/src/test/resources/actors-test.conf
Expand Up @@ -14,6 +14,8 @@ ditto {
pool {
maxSize = 1000
maxSize = ${?MONGO_DB_CONNECTION_POOL_SIZE}
maxIdleTime = 1m
maxIdleTime = ${?MONGO_DB_CONNECTION_POOL_IDLE_TIME}
maxWaitTime = 30s
maxWaitTime = ${?MONGO_DB_CONNECTION_POOL_WAIT_TIME}
jmxListenerEnabled = false
Expand Down

0 comments on commit 5fd6d33

Please sign in to comment.