8000 [automatic failover] Make MultiDbConfig.DatabaseConfig.Builder agnostic to call order by atakavci · Pull Request #4456 · redis/jedis · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 20 additions & 24 deletions src/main/java/redis/clients/jedis/MultiDbConfig.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -871,11 +871,14 @@ public static class DatabaseConfig {
*/
private float weight = 1.0f;

/** Health check enabled. Default: true */
private boolean healthCheckEnabled = true;

/**
* Strategy supplier for creating health check instances for this database. Default is
* PingStrategy.DEFAULT.
*/
private StrategySupplier healthCheckStrategySupplier;
private StrategySupplier healthCheckStrategySupplier = PingStrategy.DEFAULT;

/**
* Constructs a DatabaseConfig with basic endpoint and client configuration.
Expand All @@ -893,24 +896,6 @@ public DatabaseConfig(Endpoint endpoint, JedisClientConfig clientConfig) {
this.jedisClientConfig = clientConfig;
}

/**
* Constructs a DatabaseConfig with endpoint, client, and connection pool configuration.
* <p>
* This constructor allows specification of connection pool settings in addition to basic
* endpoint configuration. Default weight of 1.0f and PingStrategy for health checks are used.
* </p>
* @param endpoint the Redis endpoint (host and port)
* @param clientConfig the Jedis client configuration
* @param connectionPoolConfig the connection pool configuration
* @throws IllegalArgumentException if endpoint or clientConfig is null
*/
public DatabaseConfig(Endpoint endpoint, JedisClientConfig clientConfig,
GenericObjectPoolConfig<Connection> connectionPoolConfig) {
this.endpoint = endpoint;
this.jedisClientConfig = clientConfig;
this.connectionPoolConfig = connectionPoolConfig;
}

/**
* Private constructor used by the Builder to create configured instances.
* @param builder the builder containing configuration values
Expand All @@ -920,6 +905,7 @@ private DatabaseConfig(Builder builder) {
this.jedisClientConfig = builder.jedisClientConfig;
this.connectionPoolConfig = builder.connectionPoolConfig;
this.weight = builder.weight;
this.healthCheckEnabled = builder.healthCheckEnabled;
this.healthCheckStrategySupplier = builder.healthCheckStrategySupplier;
}

Expand Down Expand Up @@ -982,9 +968,20 @@ public float getWeight() {
* @see redis.clients.jedis.mcf.HealthCheckStrategy
*/
public StrategySupplier getHealthCheckStrategySupplier() {
if (!healthCheckEnabled) {
return null;
}
return healthCheckStrategySupplier;
}

/**
* Returns whether health checks are enabled for this database.
* @return true if health checks are enabled, false otherwise
*/
public boolean isHealthCheckEnabled() {
return healthCheckEnabled;
}

/**
* Builder class for creating DatabaseConfig instances with fluent configuration API.
* <p>
Expand Down Expand Up @@ -1014,6 +1011,9 @@ public static class Builder {
/** Weight for database selection priority. Default: 1.0f */
private float weight = 1.0f;

/** Health check enabled. Default: true */
private boolean healthCheckEnabled = true;

/** Health check strategy supplier. Default: PingStrategy.DEFAULT */
private StrategySupplier healthCheckStrategySupplier = PingStrategy.DEFAULT;

Expand Down Expand Up @@ -1130,11 +1130,7 @@ public Builder healthCheckStrategy(HealthCheckStrategy healthCheckStrategy) {
* @return this builder instance for method chaining
*/
public Builder healthCheckEnabled(boolean healthCheckEnabled) {
if (!healthCheckEnabled) {
this.healthCheckStrategySupplier = null;
} else if (healthCheckStrategySupplier == null) {
this.healthCheckStrategySupplier = PingStrategy.DEFAULT;
}
this.healthCheckEnabled = healthCheckEnabled;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,15 @@ public void testConnectionPoolConfigApplied() {
poolConfig.setMaxIdle(4);
poolConfig.setMinIdle(1);
DatabaseConfig[] databaseConfigs = new DatabaseConfig[2];
databaseConfigs[0] = new DatabaseConfig(endpointStandalone0.getHostAndPort(),
endpointStandalone0.getClientConfigBuilder().build(), poolConfig);
databaseConfigs[1] = new DatabaseConfig(endpointStandalone1.getHostAndPort(),
endpointStandalone0.getClientConfigBuilder().build(), poolConfig);

databaseConfigs[0] = DatabaseConfig
.builder(endpointStandalone0.getHostAndPort(),
endpointStandalone0.getClientConfigBuilder().build())
.connectionPoolConfig(poolConfig).build();
databaseConfigs[1] = DatabaseConfig
.builder(endpointStandalone1.getHostAndPort(),
endpointStandalone1.getClientConfigBuilder().build())
.connectionPoolConfig(poolConfig).build();
try (MultiDbConnectionProvider customProvider = new MultiDbConnectionProvider(
new MultiDbConfig.Builder(databaseConfigs).build())) {
MultiDbConnectionProvider.Database activeDatabase = customProvider.getDatabase();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.slf4j.LoggerFactory;

import redis.clients.jedis.*;
import redis.clients.jedis.MultiDbConfig.DatabaseConfig;
import redis.clients.jedis.exceptions.JedisAccessControlException;
import redis.clients.jedis.exceptions.JedisConnectionException;
import redis.clients.jedis.mcf.DatabaseSwitchEvent;
Expand Down Expand Up @@ -63,7 +64,7 @@ public static void prepareEndpoints() {
private List<MultiDbConfig.DatabaseConfig> getDatabaseConfigs(
JedisClientConfig clientConfig, HostAndPort... hostPorts) {
return Arrays.stream(hostPorts)
.map(hp -> new MultiDbConfig.DatabaseConfig(hp, clientConfig))
.map(hp -> DatabaseConfig.builder(hp, clientConfig).healthCheckEnabled(false).build())
.collect(Collectors.toList());
}

Expand Down
Loading
0