Skip to content

Commit

Permalink
Allow to prepare the pool
Browse files Browse the repository at this point in the history
  • Loading branch information
goetas committed Dec 16, 2024
1 parent c83f5ac commit 3e7fe9d
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,33 @@
* @author Christoph Strobl
* @author Yanming Zhou
* @author Zhian Chen
* @author Asmir Mustafic
* @since 2.0
*/
class DefaultLettucePoolingClientConfiguration implements LettucePoolingClientConfiguration {

private final LettuceClientConfiguration clientConfiguration;
private final GenericObjectPoolConfig poolConfig;
private final boolean preparePool;

DefaultLettucePoolingClientConfiguration(LettuceClientConfiguration clientConfiguration,
GenericObjectPoolConfig poolConfig) {

this.clientConfiguration = clientConfiguration;
this.poolConfig = poolConfig;
this.preparePool = false;
}

DefaultLettucePoolingClientConfiguration(LettuceClientConfiguration clientConfiguration,
GenericObjectPoolConfig poolConfig, boolean preparePool) {
this.clientConfiguration = clientConfiguration;
this.poolConfig = poolConfig;
this.preparePool = preparePool;
}

@Override
public boolean preparePool() {
return preparePool;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@
* @author Mark Paluch
* @author Christoph Strobl
* @author Yanming Zhou
* @author Asmir Mustafic
* @since 2.0
*/
public interface LettucePoolingClientConfiguration extends LettuceClientConfiguration {

boolean preparePool();
/**
* @return the {@link GenericObjectPoolConfig}. Never {@literal null}.
*/
Expand Down Expand Up @@ -92,6 +94,7 @@ static LettucePoolingClientConfiguration defaultConfiguration() {
class LettucePoolingClientConfigurationBuilder extends LettuceClientConfigurationBuilder {

GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
boolean preparePool = false;

LettucePoolingClientConfigurationBuilder() {
super();
Expand Down Expand Up @@ -125,6 +128,12 @@ public LettucePoolingClientConfigurationBuilder commandTimeout(Duration timeout)
return this;
}

public LettucePoolingClientConfigurationBuilder preparePool(boolean preparePool) {

this.preparePool = preparePool;
return this;
}

@Override
public LettucePoolingClientConfigurationBuilder shutdownTimeout(Duration shutdownTimeout) {

Expand Down Expand Up @@ -173,7 +182,7 @@ public LettucePoolingClientConfigurationBuilder poolConfig(GenericObjectPoolConf

@Override
public LettucePoolingClientConfiguration build() {
return new DefaultLettucePoolingClientConfiguration(super.build(), poolConfig);
return new DefaultLettucePoolingClientConfiguration(super.build(), poolConfig, preparePool);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
*
* @author Mark Paluch
* @author Christoph Strobl
* @author Asmir Mustafic
* @since 2.0
* @see #getConnection(Class)
*/
Expand All @@ -63,6 +64,7 @@ class LettucePoolingConnectionProvider implements LettuceConnectionProvider, Red
private static final Log log = LogFactory.getLog(LettucePoolingConnectionProvider.class);

private final LettuceConnectionProvider connectionProvider;
private final LettucePoolingClientConfiguration clientConfiguration;
private final GenericObjectPoolConfig poolConfig;
private final Map<StatefulConnection<?, ?>, GenericObjectPool<StatefulConnection<?, ?>>> poolRef = new ConcurrentHashMap<>(
32);
Expand All @@ -81,6 +83,7 @@ class LettucePoolingConnectionProvider implements LettuceConnectionProvider, Red
Assert.notNull(connectionProvider, "ConnectionProvider must not be null");
Assert.notNull(clientConfiguration, "ClientConfiguration must not be null");

this.clientConfiguration = clientConfiguration;
this.connectionProvider = connectionProvider;
this.poolConfig = clientConfiguration.getPoolConfig();
this.asyncPoolConfig = CommonsPool2ConfigConverter.bounded(this.poolConfig);
Expand All @@ -90,8 +93,21 @@ class LettucePoolingConnectionProvider implements LettuceConnectionProvider, Red
public <T extends StatefulConnection<?, ?>> T getConnection(Class<T> connectionType) {

GenericObjectPool<StatefulConnection<?, ?>> pool = pools.computeIfAbsent(connectionType, poolType -> {
return ConnectionPoolSupport.createGenericObjectPool(() -> connectionProvider.getConnection(connectionType),
poolConfig, false);

GenericObjectPool<StatefulConnection<?, ?>> newPool = ConnectionPoolSupport.createGenericObjectPool(() ->
connectionProvider.getConnection(connectionType), poolConfig, false);

if (clientConfiguration.preparePool()) {

try {
newPool.preparePool();

} catch (Exception ex) {
throw new PoolException("Could not prepare the pool", ex);
}
}

return newPool;
});

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
* @author Christoph Strobl
* @author Longlong Zhao
* @author Zhian Chen
* @author Asmir Mustafic
*/
class LettucePoolingClientConfigurationUnitTests {

Expand All @@ -48,6 +49,7 @@ void shouldCreateEmptyConfiguration() {
assertThat(configuration.getPoolConfig()).isNotNull();
assertThat(configuration.isUseSsl()).isFalse();
assertThat(configuration.isVerifyPeer()).isTrue();
assertThat(configuration.preparePool()).isFalse();
assertThat(configuration.getVerifyMode().equals(SslVerifyMode.FULL));
assertThat(configuration.isStartTls()).isFalse();
assertThat(configuration.getClientOptions()).hasValueSatisfying(actual -> {
Expand All @@ -73,6 +75,7 @@ void shouldConfigureAllProperties() {
.disablePeerVerification() //
.startTls().and() //
.poolConfig(poolConfig) //
.preparePool(true)
.clientOptions(clientOptions) //
.clientResources(sharedClientResources) //
.commandTimeout(Duration.ofMinutes(5)) //
Expand All @@ -83,6 +86,7 @@ void shouldConfigureAllProperties() {
assertThat(configuration.getPoolConfig()).isEqualTo(poolConfig);
assertThat(configuration.isUseSsl()).isTrue();
assertThat(configuration.isVerifyPeer()).isFalse();
assertThat(configuration.preparePool()).isTrue();
assertThat(configuration.getVerifyMode().equals(SslVerifyMode.NONE));
assertThat(configuration.isStartTls()).isTrue();
assertThat(configuration.getClientOptions()).contains(clientOptions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,21 @@
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.async.RedisAsyncCommands;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration.LettucePoolingClientConfigurationBuilder;

/**
* Unit tests for {@link LettucePoolingConnectionProvider}.
*
* @author Mark Paluch
* @author Asmir Mustafic
*/
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
Expand Down Expand Up @@ -70,4 +73,32 @@ void shouldDiscardTransactionOnReleaseOnActiveTransaction() {

verify(commandsMock).discard();
}

@Test
void shouldPrepareThePool() {

GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
poolConfig.setMinIdle(5);
poolConfig.setMaxIdle(8);
poolConfig.setMaxTotal(10);

LettucePoolingClientConfiguration config = new LettucePoolingClientConfigurationBuilder()
.poolConfig(poolConfig)
.preparePool(true)
.build();

LettucePoolingConnectionProvider provider = new LettucePoolingConnectionProvider(connectionProviderMock, config);

provider.getConnection(StatefulRedisConnection.class);
verify(connectionProviderMock, times(5)).getConnection(any());
}

@Test
void shouldNotPrepareThePoolByDefault() {

LettucePoolingConnectionProvider provider = new LettucePoolingConnectionProvider(connectionProviderMock, config);

provider.getConnection(StatefulRedisConnection.class);
verify(connectionProviderMock, times(1)).getConnection(any());
}
}

0 comments on commit 3e7fe9d

Please sign in to comment.