Skip to content

Commit 4af3df1

Browse files
committed
[automatic failover]Use Endpoint interface instead HostAndPort in multi db (#4302)
[clean up] Use Endpoint interface where possible
1 parent 03387cf commit 4af3df1

File tree

3 files changed

+41
-46
lines changed

3 files changed

+41
-46
lines changed

src/main/java/redis/clients/jedis/MultiClusterClientConfig.java

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,6 @@ public static interface StrategySupplier {
279279
* <strong>Default:</strong> {@value #CIRCUIT_BREAKER_SLIDING_WINDOW_SIZE_DEFAULT}
280280
* </p>
281281
* @see #getCircuitBreakerSlidingWindowSize()
282-
* @see #circuitBreakerSlidingWindowType
283282
*/
284283
private int circuitBreakerSlidingWindowSize;
285284

@@ -687,10 +686,10 @@ public static Builder builder(List<ClusterConfig> clusterConfigs) {
687686
public static class ClusterConfig {
688687

689688
/** The Redis endpoint (host and port) for this cluster. */
690-
private HostAndPort hostAndPort;
689+
private final Endpoint endpoint;
691690

692691
/** Jedis client configuration containing connection settings and authentication. */
693-
private JedisClientConfig jedisClientConfig;
692+
private final JedisClientConfig jedisClientConfig;
694693

695694
/** Optional connection pool configuration for managing connections to this cluster. */
696695
private GenericObjectPoolConfig<Connection> connectionPoolConfig;
@@ -714,12 +713,12 @@ public static class ClusterConfig {
714713
* EchoStrategy for health checks. Use the {@link Builder} for more advanced configuration
715714
* options.
716715
* </p>
717-
* @param hostAndPort the Redis endpoint (host and port)
716+
* @param endpoint the Redis endpoint (host and port)
718717
* @param clientConfig the Jedis client configuration
719-
* @throws IllegalArgumentException if hostAndPort or clientConfig is null
718+
* @throws IllegalArgumentException if endpoint or clientConfig is null
720719
*/
721-
public ClusterConfig(HostAndPort hostAndPort, JedisClientConfig clientConfig) {
722-
this.hostAndPort = hostAndPort;
720+
public ClusterConfig(Endpoint endpoint, JedisClientConfig clientConfig) {
721+
this.endpoint = endpoint;
723722
this.jedisClientConfig = clientConfig;
724723
}
725724

@@ -729,14 +728,14 @@ public ClusterConfig(HostAndPort hostAndPort, JedisClientConfig clientConfig) {
729728
* This constructor allows specification of connection pool settings in addition to basic
730729
* endpoint configuration. Default weight of 1.0f and EchoStrategy for health checks are used.
731730
* </p>
732-
* @param hostAndPort the Redis endpoint (host and port)
731+
* @param endpoint the Redis endpoint (host and port)
733732
* @param clientConfig the Jedis client configuration
734733
* @param connectionPoolConfig the connection pool configuration
735-
* @throws IllegalArgumentException if hostAndPort or clientConfig is null
734+
* @throws IllegalArgumentException if endpoint or clientConfig is null
736735
*/
737-
public ClusterConfig(HostAndPort hostAndPort, JedisClientConfig clientConfig,
736+
public ClusterConfig(Endpoint endpoint, JedisClientConfig clientConfig,
738737
GenericObjectPoolConfig<Connection> connectionPoolConfig) {
739-
this.hostAndPort = hostAndPort;
738+
this.endpoint = endpoint;
740739
this.jedisClientConfig = clientConfig;
741740
this.connectionPoolConfig = connectionPoolConfig;
742741
}
@@ -746,7 +745,7 @@ public ClusterConfig(HostAndPort hostAndPort, JedisClientConfig clientConfig,
746745
* @param builder the builder containing configuration values
747746
*/
748747
private ClusterConfig(Builder builder) {
749-
this.hostAndPort = builder.hostAndPort;
748+
this.endpoint = builder.endpoint;
750749
this.jedisClientConfig = builder.jedisClientConfig;
751750
this.connectionPoolConfig = builder.connectionPoolConfig;
752751
this.weight = builder.weight;
@@ -757,20 +756,20 @@ private ClusterConfig(Builder builder) {
757756
* Returns the Redis endpoint (host and port) for this cluster.
758757
* @return the host and port information
759758
*/
760-
public HostAndPort getHostAndPort() {
761-
return hostAndPort;
759+
public Endpoint getEndpoint() {
760+
return endpoint;
762761
}
763762

764763
/**
765764
* Creates a new Builder instance for configuring a ClusterConfig.
766-
* @param hostAndPort the Redis endpoint (host and port)
765+
* @param endpoint the Redis endpoint (host and port)
767766
* @param clientConfig the Jedis client configuration
768767
* @return new Builder instance
769-
* @throws IllegalArgumentException if hostAndPort or clientConfig is null
768+
* @throws IllegalArgumentException if endpoint or clientConfig is null
770769
*/
771-
// TODO : Replace HostAndPort with Endpoint
772-
public static Builder builder(HostAndPort hostAndPort, JedisClientConfig clientConfig) {
773-
return new Builder(hostAndPort, clientConfig);
770+
771+
public static Builder builder(Endpoint endpoint, JedisClientConfig clientConfig) {
772+
return new Builder(endpoint, clientConfig);
774773
}
775774

776775
/**
@@ -833,7 +832,7 @@ public StrategySupplier getHealthCheckStrategySupplier() {
833832
*/
834833
public static class Builder {
835834
/** The Redis endpoint for this cluster configuration. */
836-
private HostAndPort hostAndPort;
835+
private Endpoint endpoint;
837836

838837
/** The Jedis client configuration. */
839838
private JedisClientConfig jedisClientConfig;
@@ -849,12 +848,12 @@ public static class Builder {
849848

850849
/**
851850
* Constructs a new Builder with required endpoint and client configuration.
852-
* @param hostAndPort the Redis endpoint (host and port)
851+
* @param endpoint the Redis endpoint (host and port)
853852
* @param clientConfig the Jedis client configuration
854-
* @throws IllegalArgumentException if hostAndPort or clientConfig is null
853+
* @throws IllegalArgumentException if endpoint or clientConfig is null
855854
*/
856-
public Builder(HostAndPort hostAndPort, JedisClientConfig clientConfig) {
857-
this.hostAndPort = hostAndPort;
855+
public Builder(Endpoint endpoint, JedisClientConfig clientConfig) {
856+
this.endpoint = endpoint;
858857
this.jedisClientConfig = clientConfig;
859858
}
860859

@@ -1104,12 +1103,8 @@ public Builder endpoint(ClusterConfig clusterConfig) {
11041103
* @return this builder
11051104
*/
11061105
public Builder endpoint(Endpoint endpoint, float weight, JedisClientConfig clientConfig) {
1107-
// Convert Endpoint to HostAndPort for ClusterConfig
1108-
// TODO : Refactor ClusterConfig to accept Endpoint directly
1109-
HostAndPort hostAndPort = (endpoint instanceof HostAndPort) ? (HostAndPort) endpoint
1110-
: new HostAndPort(endpoint.getHost(), endpoint.getPort());
11111106

1112-
ClusterConfig clusterConfig = ClusterConfig.builder(hostAndPort, clientConfig).weight(weight)
1107+
ClusterConfig clusterConfig = ClusterConfig.builder(endpoint, clientConfig).weight(weight)
11131108
.build();
11141109

11151110
this.clusterConfigs.add(clusterConfig);

src/main/java/redis/clients/jedis/MultiDbClient.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,7 @@ public void addEndpoint(ClusterConfig clusterConfig) {
153153
* @throws redis.clients.jedis.exceptions.JedisValidationException if the endpoint already exists
154154
*/
155155
public void addEndpoint(Endpoint endpoint, float weight, JedisClientConfig clientConfig) {
156-
// Convert Endpoint to HostAndPort for ClusterConfig
157-
HostAndPort hostAndPort = (endpoint instanceof HostAndPort) ? (HostAndPort) endpoint
158-
: new HostAndPort(endpoint.getHost(), endpoint.getPort());
159-
160-
ClusterConfig clusterConfig = ClusterConfig.builder(hostAndPort, clientConfig).weight(weight)
156+
ClusterConfig clusterConfig = ClusterConfig.builder(endpoint, clientConfig).weight(weight)
161157
.build();
162158

163159
getMultiClusterProvider().add(clusterConfig);

src/main/java/redis/clients/jedis/mcf/MultiClusterPooledConnectionProvider.java

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ public void add(ClusterConfig clusterConfig) {
206206
throw new JedisValidationException("ClusterConfig must not be null");
207207
}
208208

209-
Endpoint endpoint = clusterConfig.getHostAndPort();
209+
Endpoint endpoint = clusterConfig.getEndpoint();
210210
if (multiClusterMap.containsKey(endpoint)) {
211211
throw new JedisValidationException(
212212
"Endpoint " + endpoint + " already exists in the provider");
@@ -289,12 +289,12 @@ public void remove(Endpoint endpoint) {
289289
*/
290290
private void addClusterInternal(MultiClusterClientConfig multiClusterClientConfig,
291291
ClusterConfig config) {
292-
if (multiClusterMap.containsKey(config.getHostAndPort())) {
292+
if (multiClusterMap.containsKey(config.getEndpoint())) {
293293
throw new JedisValidationException(
294-
"Endpoint " + config.getHostAndPort() + " already exists in the provider");
294+
"Endpoint " + config.getEndpoint() + " already exists in the provider");
295295
}
296296

297-
String clusterId = "cluster:" + config.getHostAndPort();
297+
String clusterId = "cluster:" + config.getEndpoint();
298298

299299
Retry retry = RetryRegistry.of(retryConfig).retry(clusterId);
300300

@@ -312,25 +312,25 @@ private void addClusterInternal(MultiClusterClientConfig multiClusterClientConfi
312312
circuitBreakerEventPublisher.onSlowCallRateExceeded(event -> log.error(String.valueOf(event)));
313313

314314
TrackingConnectionPool pool = TrackingConnectionPool.builder()
315-
.hostAndPort(config.getHostAndPort()).clientConfig(config.getJedisClientConfig())
315+
.hostAndPort(hostPort(config.getEndpoint())).clientConfig(config.getJedisClientConfig())
316316
.poolConfig(config.getConnectionPoolConfig()).build();
317317

318318
Cluster cluster;
319319
StrategySupplier strategySupplier = config.getHealthCheckStrategySupplier();
320320
if (strategySupplier != null) {
321-
HealthCheckStrategy hcs = strategySupplier.get(config.getHostAndPort(),
321+
HealthCheckStrategy hcs = strategySupplier.get(hostPort(config.getEndpoint()),
322322
config.getJedisClientConfig());
323323
// Register listeners BEFORE adding clusters to avoid missing events
324-
healthStatusManager.registerListener(config.getHostAndPort(), this::onHealthStatusChange);
325-
HealthCheck hc = healthStatusManager.add(config.getHostAndPort(), hcs);
326-
cluster = new Cluster(config.getHostAndPort(), pool, retry, hc, circuitBreaker,
324+
healthStatusManager.registerListener(config.getEndpoint(), this::onHealthStatusChange);
325+
HealthCheck hc = healthStatusManager.add(config.getEndpoint(), hcs);
326+
cluster = new Cluster(config.getEndpoint(), pool, retry, hc, circuitBreaker,
327327
config.getWeight(), multiClusterClientConfig);
328328
} else {
329-
cluster = new Cluster(config.getHostAndPort(), pool, retry, circuitBreaker,
330-
config.getWeight(), multiClusterClientConfig);
329+
cluster = new Cluster(config.getEndpoint(), pool, retry, circuitBreaker, config.getWeight(),
330+
multiClusterClientConfig);
331331
}
332332

333-
multiClusterMap.put(config.getHostAndPort(), cluster);
333+
multiClusterMap.put(config.getEndpoint(), cluster);
334334

335335
// this is the place where we listen tracked errors and check if
336336
// thresholds are exceeded for the cluster
@@ -339,6 +339,10 @@ private void addClusterInternal(MultiClusterClientConfig multiClusterClientConfi
339339
});
340340
}
341341

342+
private HostAndPort hostPort(Endpoint endpoint) {
343+
return new HostAndPort(endpoint.getHost(), endpoint.getPort());
344+
}
345+
342346
/**
343347
* Handles health status changes for clusters. This method is called by the health status manager
344348
* when the health status of a cluster changes.

0 commit comments

Comments
 (0)