Skip to content

Commit bac59c0

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 2d08f28 commit bac59c0

File tree

2 files changed

+68
-11
lines changed

2 files changed

+68
-11
lines changed

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

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -438,9 +438,7 @@ public RedisClusterConnection getClusterConnection() {
438438

439439
RedisClusterClient clusterClient = (RedisClusterClient) client;
440440

441-
StatefulRedisClusterConnection<byte[], byte[]> sharedConnection = getShareNativeConnection()
442-
? (StatefulRedisClusterConnection<byte[], byte[]>) getOrCreateSharedConnection().getConnection()
443-
: null;
441+
StatefulRedisClusterConnection<byte[], byte[]> sharedConnection = getSharedClusterConnection();
444442

445443
LettuceClusterTopologyProvider topologyProvider = new LettuceClusterTopologyProvider(clusterClient);
446444
return doCreateLettuceClusterConnection(sharedConnection, connectionProvider, topologyProvider,
@@ -540,7 +538,12 @@ public void initConnection() {
540538

541539
resetConnection();
542540

543-
getSharedConnection();
541+
if (isClusterAware()) {
542+
getSharedClusterConnection();
543+
} else {
544+
getSharedConnection();
545+
}
546+
544547
getSharedReactiveConnection();
545548
}
546549

@@ -1081,12 +1084,27 @@ public boolean isClusterAware() {
10811084
}
10821085

10831086
/**
1084-
* @return the shared connection using {@literal byte} array encoding for imperative API use. {@literal null} if
1085-
* {@link #getShareNativeConnection() connection sharing} is disabled.
1087+
* @return the shared connection using {@literal byte[]} encoding for imperative API use. {@literal null} if
1088+
* {@link #getShareNativeConnection() connection sharing} is disabled or when connected to Redis Cluster.
10861089
*/
10871090
@Nullable
10881091
protected StatefulRedisConnection<byte[], byte[]> getSharedConnection() {
1089-
return shareNativeConnection ? (StatefulRedisConnection) getOrCreateSharedConnection().getConnection() : null;
1092+
return shareNativeConnection && !isClusterAware()
1093+
? (StatefulRedisConnection) getOrCreateSharedConnection().getConnection()
1094+
: null;
1095+
}
1096+
1097+
/**
1098+
* @return the shared cluster connection using {@literal byte[]} encoding for imperative API use. {@literal null} if
1099+
* {@link #getShareNativeConnection() connection sharing} is disabled or when connected to Redis
1100+
* Standalone/Sentinel/Master-Replica.
1101+
* @since 2.5.7
1102+
*/
1103+
@Nullable
1104+
protected StatefulRedisClusterConnection<byte[], byte[]> getSharedClusterConnection() {
1105+
return shareNativeConnection && isClusterAware()
1106+
? (StatefulRedisClusterConnection) getOrCreateSharedConnection().getConnection()
1107+
: null;
10901108
}
10911109

10921110
/**

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();
@@ -558,4 +557,44 @@ void getClientNameShouldEqualWithFactorySetting() {
558557

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

0 commit comments

Comments
 (0)