Skip to content

Commit f6546f9

Browse files
committed
Fix flaky tests in GoogleCloudStorageBlobStoreRepositoryTests, S3BlobStoreRepositoryTests, AzureBlobStoreRepositoryTests
Signed-off-by: kkewwei <kewei.11@bytedance.com> Signed-off-by: kkewwei <kkewwei@163.com>
1 parent 998ae73 commit f6546f9

File tree

4 files changed

+16
-14
lines changed

4 files changed

+16
-14
lines changed

plugins/repository-azure/src/internalClusterTest/java/org/opensearch/repositories/azure/AzureBlobStoreRepositoryTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ protected Map<String, HttpHandler> createHttpHandlers() {
9797

9898
@Override
9999
protected HttpHandler createErroneousHttpHandler(final HttpHandler delegate) {
100-
return new AzureErroneousHttpHandler(delegate, randomIntBetween(2, 3));
100+
return new AzureErroneousHttpHandler(delegate, randomDoubleBetween(0, 0.25, false));
101101
}
102102

103103
@Override
@@ -165,8 +165,8 @@ private static class AzureBlobStoreHttpHandler extends AzureHttpHandler implemen
165165
@SuppressForbidden(reason = "this test uses a HttpServer to emulate an Azure endpoint")
166166
private static class AzureErroneousHttpHandler extends ErroneousHttpHandler {
167167

168-
AzureErroneousHttpHandler(final HttpHandler delegate, final int maxErrorsPerRequest) {
169-
super(delegate, maxErrorsPerRequest);
168+
AzureErroneousHttpHandler(final HttpHandler delegate, final double maxErrorsPercentage) {
169+
super(delegate, maxErrorsPercentage);
170170
}
171171

172172
@Override

plugins/repository-gcs/src/internalClusterTest/java/org/opensearch/repositories/gcs/GoogleCloudStorageBlobStoreRepositoryTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ protected Map<String, HttpHandler> createHttpHandlers() {
112112

113113
@Override
114114
protected HttpHandler createErroneousHttpHandler(final HttpHandler delegate) {
115-
return new GoogleErroneousHttpHandler(delegate, randomIntBetween(2, 3));
115+
return new GoogleErroneousHttpHandler(delegate, randomDoubleBetween(0, 0.25, false));
116116
}
117117

118118
@Override
@@ -305,8 +305,8 @@ private static class GoogleCloudStorageBlobStoreHttpHandler extends GoogleCloudS
305305
@SuppressForbidden(reason = "this test uses a HttpServer to emulate a Google Cloud Storage endpoint")
306306
private static class GoogleErroneousHttpHandler extends ErroneousHttpHandler {
307307

308-
GoogleErroneousHttpHandler(final HttpHandler delegate, final int maxErrorsPerRequest) {
309-
super(delegate, maxErrorsPerRequest);
308+
GoogleErroneousHttpHandler(final HttpHandler delegate, final double maxErrorsPercentage) {
309+
super(delegate, maxErrorsPercentage);
310310
}
311311

312312
@Override

plugins/repository-s3/src/internalClusterTest/java/org/opensearch/repositories/s3/S3BlobStoreRepositoryTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ protected Map<String, HttpHandler> createHttpHandlers() {
137137

138138
@Override
139139
protected HttpHandler createErroneousHttpHandler(final HttpHandler delegate) {
140-
return new S3StatsCollectorHttpHandler(new S3ErroneousHttpHandler(delegate, randomIntBetween(2, 3)));
140+
return new S3StatsCollectorHttpHandler(new S3ErroneousHttpHandler(delegate, randomDoubleBetween(0, 0.25, false)));
141141
}
142142

143143
@Override
@@ -332,8 +332,8 @@ private void validateAuthHeader(HttpExchange exchange) {
332332
@SuppressForbidden(reason = "this test uses a HttpServer to emulate an S3 endpoint")
333333
private static class S3ErroneousHttpHandler extends ErroneousHttpHandler {
334334

335-
S3ErroneousHttpHandler(final HttpHandler delegate, final int maxErrorsPerRequest) {
336-
super(delegate, maxErrorsPerRequest);
335+
S3ErroneousHttpHandler(final HttpHandler delegate, final double maxErrorsPercentage) {
336+
super(delegate, maxErrorsPercentage);
337337
}
338338

339339
@Override

test/framework/src/main/java/org/opensearch/repositories/blobstore/OpenSearchMockAPIBasedRepositoryIntegTestCase.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -277,14 +277,15 @@ protected abstract static class ErroneousHttpHandler implements DelegatingHttpHa
277277
private final Map<String, AtomicInteger> requests;
278278

279279
private final HttpHandler delegate;
280-
private final int maxErrorsPerRequest;
280+
private final double maxErrorsPercentage;
281281

282282
@SuppressForbidden(reason = "this test uses a HttpServer to emulate a cloud-based storage service")
283-
protected ErroneousHttpHandler(final HttpHandler delegate, final int maxErrorsPerRequest) {
283+
protected ErroneousHttpHandler(final HttpHandler delegate, final double maxErrorsPercentage) {
284284
this.requests = new ConcurrentHashMap<>();
285285
this.delegate = delegate;
286-
this.maxErrorsPerRequest = maxErrorsPerRequest;
287-
assert maxErrorsPerRequest > 1;
286+
this.maxErrorsPercentage = maxErrorsPercentage;
287+
// We don't want to fail too often as it will cost too much time, which will lead to flaky tests
288+
assert maxErrorsPercentage >= 0 && maxErrorsPercentage <= 0.25;
288289
}
289290

290291
@Override
@@ -295,7 +296,8 @@ public void handle(final HttpExchange exchange) throws IOException {
295296

296297
final boolean canFailRequest = canFailRequest(exchange);
297298
final int count = requests.computeIfAbsent(requestId, req -> new AtomicInteger(0)).incrementAndGet();
298-
if (count >= maxErrorsPerRequest || canFailRequest == false) {
299+
// We should not fail more than 3 times as the default max retry count is 3 (see SdkDefaultRetrySetting.maxAttempts), the request will fail when retry count > 3.
300+
if (random().nextDouble() > maxErrorsPercentage || count >= 3 || canFailRequest == false) {
299301
requests.remove(requestId);
300302
delegate.handle(exchange);
301303
} else {

0 commit comments

Comments
 (0)