Skip to content

Commit ce8999a

Browse files
committed
Avoid cast to StatefulRedisConnection upon eager LettuceConnectionFactory initialization.
We now no longer try to cast the Lettuce connection to StatefulRedisConnection when eagerly initializing the shared connection. Instead, we now introduced another method to obtain the cluster connection. Closes #2186
1 parent b2fb464 commit ce8999a

File tree

2 files changed

+69
-12
lines changed

2 files changed

+69
-12
lines changed

src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionFactory.java

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444

4545
import org.apache.commons.logging.Log;
4646
import org.apache.commons.logging.LogFactory;
47+
4748
import org.springframework.beans.factory.DisposableBean;
4849
import org.springframework.beans.factory.InitializingBean;
4950
import org.springframework.dao.DataAccessException;
@@ -56,7 +57,6 @@
5657
import org.springframework.data.redis.connection.RedisConfiguration.DomainSocketConfiguration;
5758
import org.springframework.data.redis.connection.RedisConfiguration.WithDatabaseIndex;
5859
import org.springframework.data.redis.connection.RedisConfiguration.WithPassword;
59-
import org.springframework.data.redis.connection.lettuce.LettuceConnection.*;
6060
import org.springframework.data.util.Optionals;
6161
import org.springframework.lang.Nullable;
6262
import org.springframework.util.Assert;
@@ -374,9 +374,7 @@ public RedisClusterConnection getClusterConnection() {
374374

375375
RedisClusterClient clusterClient = (RedisClusterClient) client;
376376

377-
StatefulRedisClusterConnection<byte[], byte[]> sharedConnection = getShareNativeConnection()
378-
? (StatefulRedisClusterConnection<byte[], byte[]>) getOrCreateSharedConnection().getConnection()
379-
: null;
377+
StatefulRedisClusterConnection<byte[], byte[]> sharedConnection = getSharedClusterConnection();
380378

381379
LettuceClusterTopologyProvider topologyProvider = new LettuceClusterTopologyProvider(clusterClient);
382380
return doCreateLettuceClusterConnection(sharedConnection, connectionProvider, topologyProvider,
@@ -472,7 +470,12 @@ public void initConnection() {
472470

473471
resetConnection();
474472

475-
getSharedConnection();
473+
if (isClusterAware()) {
474+
getSharedClusterConnection();
475+
} else {
476+
getSharedConnection();
477+
}
478+
476479
getSharedReactiveConnection();
477480
}
478481

@@ -971,12 +974,27 @@ public boolean isClusterAware() {
971974
}
972975

973976
/**
974-
* @return the shared connection using {@literal byte} array encoding for imperative API use. {@literal null} if
975-
* {@link #getShareNativeConnection() connection sharing} is disabled.
977+
* @return the shared connection using {@literal byte[]} encoding for imperative API use. {@literal null} if
978+
* {@link #getShareNativeConnection() connection sharing} is disabled or when connected to Redis Cluster.
976979
*/
977980
@Nullable
978981
protected StatefulRedisConnection<byte[], byte[]> getSharedConnection() {
979-
return shareNativeConnection ? (StatefulRedisConnection) getOrCreateSharedConnection().getConnection() : null;
982+
return shareNativeConnection && !isClusterAware()
983+
? (StatefulRedisConnection) getOrCreateSharedConnection().getConnection()
984+
: null;
985+
}
986+
987+
/**
988+
* @return the shared cluster connection using {@literal byte[]} encoding for imperative API use. {@literal null} if
989+
* {@link #getShareNativeConnection() connection sharing} is disabled or when connected to Redis
990+
* Standalone/Sentinel/Master-Replica.
991+
* @since 2.4.15
992+
*/
993+
@Nullable
994+
protected StatefulRedisClusterConnection<byte[], byte[]> getSharedClusterConnection() {
995+
return shareNativeConnection && isClusterAware()
996+
? (StatefulRedisClusterConnection) getOrCreateSharedConnection().getConnection()
997+
: null;
980998
}
981999

9821000
/**

src/test/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionFactoryTests.java

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import org.springframework.data.redis.connection.RedisStaticMasterReplicaConfiguration;
4949
import org.springframework.data.redis.connection.StringRedisConnection;
5050
import org.springframework.data.redis.connection.lettuce.extension.LettuceConnectionFactoryExtension;
51+
import org.springframework.data.redis.test.condition.EnabledOnRedisClusterAvailable;
5152
import org.springframework.data.redis.test.extension.LettuceTestClientResources;
5253

5354
/**
@@ -85,7 +86,6 @@ void tearDown() {
8586
factory.destroy();
8687
}
8788

88-
8989
@SuppressWarnings("rawtypes")
9090
@Test
9191
void testGetNewConnectionOnError() throws Exception {
@@ -462,9 +462,8 @@ void pubSubDoesNotSupportMasterReplicaConnections() {
462462

463463
RedisConnection connection = factory.getConnection();
464464

465-
assertThatThrownBy(() -> connection.pSubscribe((message, pattern) -> {
466-
}, "foo".getBytes())).isInstanceOf(RedisConnectionFailureException.class)
467-
.hasCauseInstanceOf(UnsupportedOperationException.class);
465+
assertThatThrownBy(() -> connection.pSubscribe((message, pattern) -> {}, "foo".getBytes()))
466+
.isInstanceOf(RedisConnectionFailureException.class).hasCauseInstanceOf(UnsupportedOperationException.class);
468467

469468
connection.close();
470469
factory.destroy();
@@ -559,4 +558,44 @@ void getClientNameShouldEqualWithFactorySetting() {
559558

560559
connection.close();
561560
}
561+
562+
@Test // GH-2186
563+
void shouldInitializeMasterReplicaConnectionsEagerly() {
564+
565+
LettuceClientConfiguration configuration = LettuceClientConfiguration.builder()
566+
.clientResources(LettuceTestClientResources.getSharedClientResources()).build();
567+
568+
RedisStaticMasterReplicaConfiguration elastiCache = new RedisStaticMasterReplicaConfiguration(
569+
SettingsUtils.getHost()).node(SettingsUtils.getHost(), SettingsUtils.getPort() + 1);
570+
571+
LettuceConnectionFactory factory = new LettuceConnectionFactory(elastiCache, configuration);
572+
factory.setEagerInitialization(true);
573+
factory.afterPropertiesSet();
574+
575+
assertThat(factory.getSharedConnection()).isNotNull();
576+
assertThat(factory.getSharedClusterConnection()).isNull();
577+
578+
factory.getConnection().close();
579+
factory.destroy();
580+
}
581+
582+
@Test // GH-2186
583+
@EnabledOnRedisClusterAvailable
584+
void shouldInitializeClusterConnectionsEagerly() {
585+
586+
LettuceClientConfiguration configuration = LettuceClientConfiguration.builder()
587+
.clientResources(LettuceTestClientResources.getSharedClientResources()).build();
588+
589+
LettuceConnectionFactory factory = new LettuceConnectionFactory(SettingsUtils.clusterConfiguration(),
590+
configuration);
591+
factory.setEagerInitialization(true);
592+
factory.afterPropertiesSet();
593+
594+
assertThat(factory.getSharedConnection()).isNull();
595+
assertThat(factory.getSharedClusterConnection()).isNotNull();
596+
597+
factory.getConnection().close();
598+
factory.destroy();
599+
}
600+
562601
}

0 commit comments

Comments
 (0)