Skip to content

[automatic failover] Improve failover resilience by evaluating circuit breaker before retries #4178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion formatter-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<plugin>
<groupId>net.revelc.code.formatter</groupId>
<artifactId>formatter-maven-plugin</artifactId>
<version>2.23.0</version>
<version>2.16.0</version>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so what is the issue with 2.23 ?

Copy link
Collaborator Author

@ggivo ggivo Jun 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@atakavci
2.23 requires Java 11.
There is a slight difference in the formatted code if we format the code using 2.23.0 vs 2.16.0 (used in the pom.xml with regular build).
To avoid the difference decided to align the versions.

<configuration>
<configFile>${project.basedir}/hbase-formatter.xml</configFile>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public <T> T executeCommand(CommandObject<T> commandObject) {
DecorateSupplier<T> supplier = Decorators
.ofSupplier(() -> this.handleExecuteCommand(commandObject, cluster));

supplier.withRetry(cluster.getRetry());
supplier.withCircuitBreaker(cluster.getCircuitBreaker());
supplier.withRetry(cluster.getRetry());
supplier.withFallback(provider.getFallbackExceptionList(),
e -> this.handleClusterFailover(commandObject, cluster.getCircuitBreaker()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,4 +272,62 @@ public void testManualFailoverInflightCommandsWithErrorsPropagateError() throws
assertThat(getNodeId(failoverClient.info("server")), equalTo(JEDIS2_ID));
}

}
/**
* Tests that the CircuitBreaker counts each command error separately, and not just after all
* retries are exhausted. This ensures that the circuit breaker opens based on the actual number
* of send commands with failures, and not based on the number of logical operations.
*/
@Test
public void testCircuitBreakerCountsEachConnectionErrorSeparately() throws IOException {
MultiClusterClientConfig failoverConfig = new MultiClusterClientConfig.Builder(
getClusterConfigs(
DefaultJedisClientConfig.builder()
.socketTimeoutMillis(RecommendedSettings.DEFAULT_TIMEOUT_MS)
.connectionTimeoutMillis(RecommendedSettings.DEFAULT_TIMEOUT_MS).build(),
endpoint1, endpoint2)).retryMaxAttempts(2).retryWaitDuration(1)
.circuitBreakerSlidingWindowType(COUNT_BASED).circuitBreakerSlidingWindowSize(3)
.circuitBreakerFailureRateThreshold(50) // 50% failure
// rate threshold
.circuitBreakerSlidingWindowMinCalls(3).build();

MultiClusterPooledConnectionProvider provider = new MultiClusterPooledConnectionProvider(
failoverConfig);
try (UnifiedJedis client = new UnifiedJedis(provider)) {
// Verify initial connection to first endpoint
assertThat(getNodeId(client.info("server")), equalTo(JEDIS1_ID));

// Disable first endpoint
redisProxy1.disable();

// First command should fail and OPEN the circuit breaker immediately
//
// If CB is applied after retries:
// - It would take 2 commands to OPEN CB (error is propagated for both commands)
// - Failover to the next Endpoint happens on the 3rd command
//
// If CB is applied before retries:
// - It should open after just 1 command with retries
// - CB is OPEN after the 2nd retry of the first command
// - Failover to the next Endpoint happens on the 2nd command
//
// This test verifies the second case by checking that:
// 1. CB opens after the first command (with retries)
// 2. The second command is routed to the second endpoint
// Command 1
assertThrows(JedisConnectionException.class, () -> client.info("server"));

// Circuit breaker should be open after just one command with retries
assertThat(provider.getCluster(1).getCircuitBreaker().getState(),
equalTo(CircuitBreaker.State.OPEN));

// Next command should be routed to the second endpoint
// Command 2
assertThat(getNodeId(client.info("server")), equalTo(JEDIS2_ID));

// Command 3
assertThat(getNodeId(client.info("server")), equalTo(JEDIS2_ID));

}
}

}
Loading