Skip to content

Commit 3cfde51

Browse files
onobcsnicoll
authored andcommitted
Add option to disable Redis Cluster dynamic sources refresh
This commit adds an option to enable/disable the DynamicRefreshSources setting on the Lettuce cluster toplogy refresh options. See gh-22571
1 parent f854a79 commit 3cfde51

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/LettuceConnectionConfiguration.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ private ClientOptions.Builder initializeClientOptionsBuilder() {
138138
if (refreshProperties.isAdaptive()) {
139139
refreshBuilder.enableAllAdaptiveRefreshTriggers();
140140
}
141+
if (refreshProperties.isDynamicSources() != null) {
142+
refreshBuilder.dynamicRefreshSources(refreshProperties.isDynamicSources());
143+
}
141144
return builder.topologyRefreshOptions(refreshBuilder.build());
142145
}
143146
return ClientOptions.builder();

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/RedisProperties.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,13 @@ public static class Refresh {
410410
*/
411411
private boolean adaptive;
412412

413+
/**
414+
* Whether discovered nodes should be used as the source for the cluster
415+
* topology. When set to false, only the initial seed nodes
416+
* will be used as sources for topology discovery.
417+
*/
418+
private Boolean dynamicSources;
419+
413420
public Duration getPeriod() {
414421
return this.period;
415422
}
@@ -426,6 +433,14 @@ public void setAdaptive(boolean adaptive) {
426433
this.adaptive = adaptive;
427434
}
428435

436+
public Boolean isDynamicSources() {
437+
return this.dynamicSources;
438+
}
439+
440+
public void setDynamicSources(Boolean dynamicSources) {
441+
this.dynamicSources = dynamicSources;
442+
}
443+
429444
}
430445

431446
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/redis/RedisAutoConfigurationTests.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import io.lettuce.core.ClientOptions;
2727
import io.lettuce.core.cluster.ClusterClientOptions;
28+
import io.lettuce.core.cluster.ClusterTopologyRefreshOptions;
2829
import io.lettuce.core.cluster.ClusterTopologyRefreshOptions.RefreshTrigger;
2930
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
3031
import org.junit.jupiter.api.Test;
@@ -312,6 +313,36 @@ void testRedisConfigurationWithClusterRefreshPeriodHasNoEffectWithNonClusteredCo
312313
ClientOptions.class, (options) -> assertThat(options.getClass()).isEqualTo(ClientOptions.class)));
313314
}
314315

316+
@Test
317+
void testRedisConfigurationWithClusterDynamicSourcesEnabled() {
318+
this.contextRunner
319+
.withPropertyValues("spring.redis.cluster.nodes=127.0.0.1:27379,127.0.0.1:27380",
320+
"spring.redis.lettuce.cluster.refresh.dynamic-sources=true")
321+
.run(assertClientOptions(ClusterClientOptions.class,
322+
(options) -> assertThat(options.getTopologyRefreshOptions().useDynamicRefreshSources())
323+
.isTrue()));
324+
}
325+
326+
@Test
327+
void testRedisConfigurationWithClusterDynamicSourcesDisabled() {
328+
this.contextRunner
329+
.withPropertyValues("spring.redis.cluster.nodes=127.0.0.1:27379,127.0.0.1:27380",
330+
"spring.redis.lettuce.cluster.refresh.dynamic-sources=false")
331+
.run(assertClientOptions(ClusterClientOptions.class,
332+
(options) -> assertThat(options.getTopologyRefreshOptions().useDynamicRefreshSources())
333+
.isFalse()));
334+
}
335+
336+
@Test
337+
void testRedisConfigurationWithClusterDynamicSourcesUnspecifiedUsesDefault() {
338+
this.contextRunner
339+
.withPropertyValues("spring.redis.cluster.nodes=127.0.0.1:27379,127.0.0.1:27380",
340+
"spring.redis.lettuce.cluster.refresh.dynamic-sources=")
341+
.run(assertClientOptions(ClusterClientOptions.class,
342+
(options) -> assertThat(options.getTopologyRefreshOptions().useDynamicRefreshSources())
343+
.isEqualTo(ClusterTopologyRefreshOptions.DEFAULT_DYNAMIC_REFRESH_SOURCES)));
344+
}
345+
315346
private <T extends ClientOptions> ContextConsumer<AssertableApplicationContext> assertClientOptions(
316347
Class<T> expectedType, Consumer<T> options) {
317348
return (context) -> {

0 commit comments

Comments
 (0)