|
| 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