mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(compat): retry RedshiftTest's SDK calls past a brief connection-refused hiccup (#3216)
* fix(compat): retry RedshiftTest's SDK calls past a brief connection-refused hiccup github.com/floci-io/floci/issues/2789: testCreateCluster/testDeleteCluster intermittently fail native/sdk-test-java CI with Connection refused on PRs that never touch Redshift or networking. The AWS SDK's own 4 built-in attempts total under a second of backoff, and the failure persists past CI's "wait for floci to be ready" step reporting success - consistent with a brief port-publish/network-alias hiccup between the test runner and floci containers that self-resolves almost immediately, just not within a second. Wraps each SDK call in a 30-second retry loop that only catches SdkClientException (connectivity failures), so a genuine service-level error (e.g. ClusterAlreadyExists) still surfaces immediately instead of being retried into a timeout. Mirrors the retry pattern RedshiftOperationsTest already uses successfully for its JDBC connection in the same module. Verified against a local floci instance: testCreateCluster and testDeleteCluster pass. Could not reproduce the actual CI-only race locally (same as the issue reporter, who diagnosed it from CI logs only). * fix(compat): narrow RedshiftTest's retry to the diagnosed connection-refused failure pgermosen (review): catching the whole SdkClientException hierarchy also retries failures that can happen after floci already processed the request, e.g. a response reset mid-stream on createCluster/deleteCluster. Retrying those resubmits and gets back a real ClusterAlreadyExists/ ClusterNotFound instead of the connection-refused flake this PR targets, masking the original failure behind an unrelated one. Narrows the catch to only retry when a ConnectException (the pre-handshake "Connection refused" this was diagnosed from) is in the cause chain; any other SdkClientException now propagates immediately.
This commit is contained in:
1 parent
83b010e6c3
commit
974d357f88
1 file changed
+54
-10
+54
-10
@@ -5,6 +5,7 @@ import org.junit.jupiter.api.MethodOrderer;
|
||||
import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
import software.amazon.awssdk.core.exception.SdkClientException;
|
||||
import software.amazon.awssdk.services.redshift.RedshiftClient;
|
||||
import software.amazon.awssdk.services.redshift.model.Cluster;
|
||||
import software.amazon.awssdk.services.redshift.model.CreateClusterRequest;
|
||||
@@ -19,11 +20,16 @@ import software.amazon.awssdk.services.s3.model.GetObjectRequest;
|
||||
import software.amazon.awssdk.services.s3.model.ListObjectsV2Request;
|
||||
import software.amazon.awssdk.services.s3.model.ListObjectsV2Response;
|
||||
|
||||
import java.net.ConnectException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.Statement;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
@@ -35,23 +41,61 @@ public class RedshiftTest {
|
||||
return TestFixtures.redshiftClient();
|
||||
}
|
||||
|
||||
// github.com/floci-io/floci/issues/2789: this class's first request intermittently hits
|
||||
// "Connection refused" even after the AWS SDK's own 4 built-in attempts (whose backoff totals
|
||||
// under a second) and even after CI's "wait for floci to be ready" step reports success - a
|
||||
// brief port-publish/network-alias hiccup between the test runner and floci containers that
|
||||
// self-resolves almost immediately. Retrying the whole call, not just relying on the SDK's
|
||||
// built-in attempts, gives it long enough to clear.
|
||||
//
|
||||
// Only a pre-handshake ConnectException is worth retrying (review, PR #3216): a broader catch
|
||||
// of SdkClientException would also retry failures that can happen after floci already processed
|
||||
// the request, e.g. a response reset mid-stream on createCluster/deleteCluster - a retry there
|
||||
// resubmits and gets back a real ClusterAlreadyExists/ClusterNotFound, which masks the original
|
||||
// failure behind an unrelated one instead of fixing the flake.
|
||||
private static <T> T withRetry(Supplier<T> action) throws InterruptedException {
|
||||
Instant deadline = Instant.now().plus(Duration.ofSeconds(30));
|
||||
SdkClientException last = null;
|
||||
while (Instant.now().isBefore(deadline)) {
|
||||
try {
|
||||
return action.get();
|
||||
} catch (SdkClientException e) {
|
||||
if (!isConnectionRefused(e)) {
|
||||
throw e;
|
||||
}
|
||||
last = e;
|
||||
Thread.sleep(1000);
|
||||
}
|
||||
}
|
||||
throw last;
|
||||
}
|
||||
|
||||
private static boolean isConnectionRefused(Throwable e) {
|
||||
for (Throwable cause = e; cause != null; cause = cause.getCause()) {
|
||||
if (cause instanceof ConnectException) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
public void testCreateCluster() throws Exception {
|
||||
RedshiftClient client = getClient();
|
||||
CreateClusterResponse res = client.createCluster(CreateClusterRequest.builder()
|
||||
CreateClusterResponse res = withRetry(() -> client.createCluster(CreateClusterRequest.builder()
|
||||
.clusterIdentifier("test-cluster")
|
||||
.nodeType("dc2.large")
|
||||
.masterUsername("admin")
|
||||
.masterUserPassword("Password123")
|
||||
.build());
|
||||
|
||||
.build()));
|
||||
|
||||
assertEquals("test-cluster", res.cluster().clusterIdentifier());
|
||||
|
||||
DescribeClustersResponse describeRes = client.describeClusters(DescribeClustersRequest.builder()
|
||||
|
||||
DescribeClustersResponse describeRes = withRetry(() -> client.describeClusters(DescribeClustersRequest.builder()
|
||||
.clusterIdentifier("test-cluster")
|
||||
.build());
|
||||
|
||||
.build()));
|
||||
|
||||
Cluster cluster = describeRes.clusters().get(0);
|
||||
assertEquals("test-cluster", cluster.clusterIdentifier());
|
||||
// Terraform's AWS provider polls these two on create and validates them on read (issue #3098).
|
||||
@@ -108,11 +152,11 @@ public class RedshiftTest {
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
public void testDeleteCluster() {
|
||||
public void testDeleteCluster() throws Exception {
|
||||
RedshiftClient client = getClient();
|
||||
DeleteClusterResponse res = client.deleteCluster(DeleteClusterRequest.builder()
|
||||
DeleteClusterResponse res = withRetry(() -> client.deleteCluster(DeleteClusterRequest.builder()
|
||||
.clusterIdentifier("test-cluster")
|
||||
.build());
|
||||
.build()));
|
||||
assertEquals("test-cluster", res.cluster().clusterIdentifier());
|
||||
assertEquals("deleting", res.cluster().clusterStatus());
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user