Skip to content

Commit 904e045

Browse files
committed
Try and simplify shouldRetry override
1 parent 004319f commit 904e045

File tree

3 files changed

+88
-75
lines changed

3 files changed

+88
-75
lines changed

modules/repository-gcs/src/main/java/org/elasticsearch/repositories/gcs/DelegatingStorageRetryStrategy.java

Lines changed: 0 additions & 65 deletions
This file was deleted.

modules/repository-gcs/src/main/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageService.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import com.google.api.client.http.HttpTransport;
1515
import com.google.api.client.http.javanet.NetHttpTransport;
1616
import com.google.api.client.util.SecurityUtils;
17+
import com.google.api.gax.retrying.ResultRetryAlgorithm;
1718
import com.google.auth.oauth2.GoogleCredentials;
1819
import com.google.auth.oauth2.ServiceAccountCredentials;
1920
import com.google.cloud.ServiceOptions;
@@ -43,7 +44,6 @@
4344
import java.net.UnknownHostException;
4445
import java.security.KeyStore;
4546
import java.util.Map;
46-
import java.util.concurrent.CancellationException;
4747
import java.util.stream.Collectors;
4848

4949
import static java.nio.charset.StandardCharsets.UTF_8;
@@ -258,17 +258,14 @@ StorageOptions createStorageOptions(
258258
}
259259

260260
protected StorageRetryStrategy getRetryStrategy() {
261-
return new DelegatingStorageRetryStrategy<>(
261+
return ShouldRetryDecorator.decorate(
262262
StorageRetryStrategy.getLegacyStorageRetryStrategy(),
263-
d -> new DelegatingStorageRetryStrategy.DelegatingResultRetryAlgorithm<>(d) {
264-
@Override
265-
public boolean shouldRetry(Throwable prevThrowable, Object prevResponse) throws CancellationException {
266-
// Retry in the event of an unknown host exception
267-
if (ExceptionsHelper.unwrap(prevThrowable, UnknownHostException.class) != null) {
268-
return true;
269-
}
270-
return delegate.shouldRetry(prevThrowable, prevResponse);
263+
(Throwable prevThrowable, Object prevResponse, ResultRetryAlgorithm<Object> delegate) -> {
264+
// Retry in the event of an unknown host exception
265+
if (ExceptionsHelper.unwrap(prevThrowable, UnknownHostException.class) != null) {
266+
return true;
271267
}
268+
return delegate.shouldRetry(prevThrowable, prevResponse);
272269
}
273270
);
274271
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.repositories.gcs;
11+
12+
import com.google.api.gax.retrying.ResultRetryAlgorithm;
13+
import com.google.api.gax.retrying.TimedAttemptSettings;
14+
import com.google.cloud.storage.StorageRetryStrategy;
15+
16+
import java.util.concurrent.CancellationException;
17+
18+
public class ShouldRetryDecorator<T> implements StorageRetryStrategy {
19+
20+
private final ResultRetryAlgorithm<T> idempotentRetryAlgorithm;
21+
private final ResultRetryAlgorithm<T> nonIdempotentRetryAlgorithm;
22+
23+
/**
24+
* Decorate the should-retry logic for the specified {@link StorageRetryStrategy}
25+
*
26+
* @param delegate The underling storage retry strategy
27+
* @param shouldRetryDecorator The decorated behaviour of {@link ResultRetryAlgorithm#shouldRetry(Throwable, Object)}
28+
* @return A decorated {@link StorageRetryStrategy}
29+
*/
30+
public static StorageRetryStrategy decorate(StorageRetryStrategy delegate, Decorator<?> shouldRetryDecorator) {
31+
return new ShouldRetryDecorator<>(delegate, shouldRetryDecorator);
32+
}
33+
34+
/**
35+
* The logic to use for {@link ResultRetryAlgorithm#shouldRetry(Throwable, Object)}
36+
*/
37+
public interface Decorator<R> {
38+
boolean shouldRetry(Throwable prevThrowable, R prevResponse, ResultRetryAlgorithm<R> delegate);
39+
}
40+
41+
/**
42+
* @param delegate The delegate {@link StorageRetryStrategy}
43+
* @param shouldRetryDecorator The function to call for shouldRetry for idempotent and non-idempotent requests
44+
*/
45+
@SuppressWarnings("unchecked")
46+
private ShouldRetryDecorator(StorageRetryStrategy delegate, Decorator<T> shouldRetryDecorator) {
47+
this.idempotentRetryAlgorithm = new DelegatingResultRetryAlgorithm<>(
48+
(ResultRetryAlgorithm<T>) delegate.getIdempotentHandler(),
49+
shouldRetryDecorator
50+
);
51+
this.nonIdempotentRetryAlgorithm = new DelegatingResultRetryAlgorithm<>(
52+
(ResultRetryAlgorithm<T>) delegate.getNonidempotentHandler(),
53+
shouldRetryDecorator
54+
);
55+
}
56+
57+
@Override
58+
public ResultRetryAlgorithm<?> getIdempotentHandler() {
59+
return idempotentRetryAlgorithm;
60+
}
61+
62+
@Override
63+
public ResultRetryAlgorithm<?> getNonidempotentHandler() {
64+
return nonIdempotentRetryAlgorithm;
65+
}
66+
67+
private record DelegatingResultRetryAlgorithm<R>(ResultRetryAlgorithm<R> delegate, Decorator<R> shouldRetryDecorator)
68+
implements
69+
ResultRetryAlgorithm<R> {
70+
71+
@Override
72+
public TimedAttemptSettings createNextAttempt(Throwable prevThrowable, R prevResponse, TimedAttemptSettings prevSettings) {
73+
return delegate.createNextAttempt(prevThrowable, prevResponse, prevSettings);
74+
}
75+
76+
@Override
77+
public boolean shouldRetry(Throwable prevThrowable, R prevResponse) throws CancellationException {
78+
return shouldRetryDecorator.shouldRetry(prevThrowable, prevResponse, delegate);
79+
}
80+
}
81+
}

0 commit comments

Comments
 (0)