Skip to content

Commit 0fcea1e

Browse files
committed
Support of Jedis topology refresh period in cluster mode.
Jedis added support of topology refresh and providing a period on which jedis will refresh node topology of cluster. This was done from jedis version v5.1.0 https://github.com/redis/jedis/releases/tag/v5.1.0 In pr: redis/jedis#3596 Signed-off-by: Bharat Agarwal <[email protected]>
1 parent 8156530 commit 0fcea1e

File tree

4 files changed

+62
-3
lines changed

4 files changed

+62
-3
lines changed

src/main/java/org/springframework/data/redis/connection/jedis/DefaultJedisClientConfiguration.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
*
3131
* @author Mark Paluch
3232
* @author Christoph Strobl
33+
* @author Bharat Agarwal
3334
* @since 2.0
3435
*/
3536
class DefaultJedisClientConfiguration implements JedisClientConfiguration {
@@ -44,11 +45,13 @@ class DefaultJedisClientConfiguration implements JedisClientConfiguration {
4445
private final Optional<String> clientName;
4546
private final Duration readTimeout;
4647
private final Duration connectTimeout;
48+
private final Optional<Duration> topologyRefreshPeriod;
4749

4850
DefaultJedisClientConfiguration(@Nullable JedisClientConfigBuilderCustomizer customizer, boolean useSsl,
4951
@Nullable SSLSocketFactory sslSocketFactory, @Nullable SSLParameters sslParameters,
5052
@Nullable HostnameVerifier hostnameVerifier, boolean usePooling, @Nullable GenericObjectPoolConfig poolConfig,
51-
@Nullable String clientName, Duration readTimeout, Duration connectTimeout) {
53+
@Nullable String clientName, Duration readTimeout, Duration connectTimeout,
54+
@Nullable Duration topologyRefreshPeriod) {
5255

5356
this.customizer = Optional.ofNullable(customizer);
5457
this.useSsl = useSsl;
@@ -60,6 +63,7 @@ class DefaultJedisClientConfiguration implements JedisClientConfiguration {
6063
this.clientName = Optional.ofNullable(clientName);
6164
this.readTimeout = readTimeout;
6265
this.connectTimeout = connectTimeout;
66+
this.topologyRefreshPeriod = Optional.ofNullable(topologyRefreshPeriod);
6367
}
6468

6569
@Override
@@ -111,4 +115,8 @@ public Duration getReadTimeout() {
111115
public Duration getConnectTimeout() {
112116
return connectTimeout;
113117
}
118+
@Override
119+
public Optional<Duration> getTopologyRefreshPeriod() {
120+
return topologyRefreshPeriod;
121+
}
114122
}

src/main/java/org/springframework/data/redis/connection/jedis/JedisClientConfiguration.java

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,13 @@
4545
* <li>Optional client name</li>
4646
* <li>Connect {@link Duration timeout}</li>
4747
* <li>Read {@link Duration timeout}</li>
48+
* <li>Optional {@link Duration topologyRefreshPeriod}
4849
* </ul>
4950
*
5051
* @author Mark Paluch
5152
* @author Christoph Strobl
5253
* @author Chao Chang
54+
* @author Bharat Agarwal
5355
* @since 2.0
5456
* @see redis.clients.jedis.Jedis
5557
* @see org.springframework.data.redis.connection.RedisStandaloneConfiguration
@@ -112,6 +114,14 @@ public interface JedisClientConfiguration {
112114
*/
113115
Duration getReadTimeout();
114116

117+
/**
118+
* @return the optional {@link Duration} for Topology Refresh.
119+
* Applies only to Redis Cluster mode. Standalone and Sentinel mode of cluster enabled redis doesn't use or require this.
120+
* Default value is null i.e. Jedis doesn't do periodical topology refresh, else Jedis will refresh node topology every duration mentioned.
121+
*/
122+
123+
Optional<Duration> getTopologyRefreshPeriod();
124+
115125
/**
116126
* Creates a new {@link JedisClientConfigurationBuilder} to build {@link JedisClientConfiguration} to be used with the
117127
* jedis client.
@@ -137,6 +147,8 @@ static JedisClientConfigurationBuilder builder() {
137147
* <dd>2000 msec</dd>
138148
* <dt>Connect Timeout</dt>
139149
* <dd>2000 msec</dd>
150+
* <dt>Topology Refresh Period</dt>
151+
* <dd>null</dd>
140152
* </dl>
141153
*
142154
* @return a {@link JedisClientConfiguration} with defaults.
@@ -203,6 +215,22 @@ interface JedisClientConfigurationBuilder {
203215
*/
204216
JedisClientConfigurationBuilder connectTimeout(Duration connectTimeout);
205217

218+
219+
/**
220+
* Configure a topology refresh period
221+
* <p>
222+
* Topology Refresh Period is used by Jedis to refresh topology of nodes on periodically basis.
223+
* Default value is null i.e. doesn't do periodically refresh.
224+
* <p>
225+
* Applies only to Cluster mode redis. Standalone or Sentinal redis clusters doesn't have impact of this configuration.
226+
*
227+
* @param topologyRefreshPeriod
228+
* @return {@literal this} builder.
229+
*/
230+
JedisClientConfigurationBuilder topologyRefreshPeriod(Duration topologyRefreshPeriod);
231+
232+
233+
206234
/**
207235
* Build the {@link JedisClientConfiguration} with the configuration applied from this builder.
208236
*
@@ -296,6 +324,7 @@ class DefaultJedisClientConfigurationBuilder implements JedisClientConfiguration
296324
private @Nullable String clientName;
297325
private Duration readTimeout = Duration.ofMillis(Protocol.DEFAULT_TIMEOUT);
298326
private Duration connectTimeout = Duration.ofMillis(Protocol.DEFAULT_TIMEOUT);
327+
private @Nullable Duration topologyRefreshPeriod = null; // Maintaing default value as Jedis has to keep backward compatibility
299328

300329
private DefaultJedisClientConfigurationBuilder() {}
301330

@@ -390,11 +419,18 @@ public JedisClientConfigurationBuilder connectTimeout(Duration connectTimeout) {
390419
return this;
391420
}
392421

422+
423+
@Override
424+
public JedisClientConfigurationBuilder topologyRefreshPeriod(Duration topologyRefreshPeriod) {
425+
this.topologyRefreshPeriod = topologyRefreshPeriod;
426+
return this;
427+
}
428+
393429
@Override
394430
public JedisClientConfiguration build() {
395431

396432
return new DefaultJedisClientConfiguration(customizer, useSsl, sslSocketFactory, sslParameters, hostnameVerifier,
397-
usePooling, poolConfig, clientName, readTimeout, connectTimeout);
433+
usePooling, poolConfig, clientName, readTimeout, connectTimeout, topologyRefreshPeriod);
398434
}
399435
}
400436

src/main/java/org/springframework/data/redis/connection/jedis/JedisConnectionFactory.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
* @author Mark Paluch
9494
* @author Fu Jian
9595
* @author Ajith Kumar
96+
* @author Bharat Agarwal
9697
* @see JedisClientConfiguration
9798
* @see Jedis
9899
*/
@@ -846,7 +847,11 @@ protected JedisCluster createCluster(RedisClusterConfiguration clusterConfig,
846847

847848
int redirects = clusterConfig.getMaxRedirects() != null ? clusterConfig.getMaxRedirects() : 5;
848849

849-
return new JedisCluster(hostAndPort, this.clientConfig, redirects, poolConfig);
850+
// When maxTotalRetriesDuration passed, Jedis use this method to calcuate, to use topologyRefreshPeriod, Jedis constructor expect maxTotalRetriesDuration.
851+
Duration maxTotalRetriesDuration = Duration.ofMillis((long)this.clientConfig.getSocketTimeoutMillis() * (long)redirects);
852+
Duration topologyRefreshPeriod = this.clientConfiguration.getTopologyRefreshPeriod().orElse(null);
853+
854+
return new JedisCluster(hostAndPort, this.clientConfig, poolConfig, topologyRefreshPeriod, redirects, maxTotalRetriesDuration);
850855
}
851856

852857
@Override
@@ -1093,6 +1098,7 @@ static class MutableJedisClientConfiguration implements JedisClientConfiguration
10931098
private @Nullable String clientName;
10941099
private Duration readTimeout = Duration.ofMillis(Protocol.DEFAULT_TIMEOUT);
10951100
private Duration connectTimeout = Duration.ofMillis(Protocol.DEFAULT_TIMEOUT);
1101+
private @Nullable Duration topologyRefreshPeriod = null;
10961102

10971103
public static JedisClientConfiguration create(GenericObjectPoolConfig jedisPoolConfig) {
10981104

@@ -1186,5 +1192,10 @@ public Duration getConnectTimeout() {
11861192
public void setConnectTimeout(Duration connectTimeout) {
11871193
this.connectTimeout = connectTimeout;
11881194
}
1195+
1196+
@Override
1197+
public Optional<Duration> getTopologyRefreshPeriod() {
1198+
return Optional.empty();
1199+
}
11891200
}
11901201
}

src/test/java/org/springframework/data/redis/connection/jedis/JedisClientConfigurationUnitTests.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ void shouldCreateEmptyConfiguration() {
4949
assertThat(configuration.getPoolConfig()).isPresent();
5050
assertThat(configuration.getSslParameters()).isEmpty();
5151
assertThat(configuration.getSslSocketFactory()).isEmpty();
52+
assertThat(configuration.getTopologyRefreshPeriod()).isEmpty();
5253
}
5354

5455
@Test // DATAREDIS-574
@@ -64,6 +65,7 @@ void shouldConfigureAllProperties() throws NoSuchAlgorithmException {
6465
.sslParameters(sslParameters) //
6566
.sslSocketFactory(socketFactory).and() //
6667
.clientName("my-client") //
68+
.topologyRefreshPeriod(Duration.ofMillis(100)) // adding topologyRefreshPeriod
6769
.connectTimeout(Duration.ofMinutes(10)) //
6870
.readTimeout(Duration.ofHours(5)) //
6971
.usePooling().poolConfig(poolConfig) //
@@ -79,6 +81,8 @@ void shouldConfigureAllProperties() throws NoSuchAlgorithmException {
7981
assertThat(configuration.getReadTimeout()).isEqualTo(Duration.ofHours(5));
8082

8183
assertThat(configuration.getPoolConfig()).contains(poolConfig);
84+
85+
assertThat(configuration.getTopologyRefreshPeriod()).isEqualTo(Duration.ofMillis(100));
8286
}
8387

8488
enum MyHostnameVerifier implements HostnameVerifier {

0 commit comments

Comments
 (0)