generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 18
Improving Retries based on Iceberg Integration Feedback #321
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
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d50b832
improve retries
fuatbasik efadf68
Fix return type of IOSupplier
fuatbasik 6175c97
add javadoc changes too
fuatbasik 374ef48
Add new test and update description to call out retries are additive
fuatbasik 59c4044
Address feedback
fuatbasik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 158 additions & 0 deletions
158
...ain/java/software/amazon/s3/analyticsaccelerator/util/retry/DefaultRetryStrategyImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"). | ||
| * You may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package software.amazon.s3.analyticsaccelerator.util.retry; | ||
|
|
||
| import dev.failsafe.Failsafe; | ||
| import dev.failsafe.FailsafeException; | ||
| import dev.failsafe.FailsafeExecutor; | ||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import java.util.stream.Collectors; | ||
| import software.amazon.s3.analyticsaccelerator.common.Preconditions; | ||
|
|
||
| /** | ||
| * Retry strategy implementation for seekable input stream operations. Uses Failsafe library to | ||
| * execute operations with configurable retry policies. | ||
| * | ||
| * <p>This strategy will be additive to readTimeout and readRetryCount set on PhysicalIO | ||
| * configuration. | ||
| */ | ||
| public class DefaultRetryStrategyImpl implements RetryStrategy { | ||
| private final List<RetryPolicy> retryPolicies; | ||
| FailsafeExecutor<Object> failsafeExecutor; | ||
|
|
||
| /** Creates a retry strategy with no retry policies (no retries). */ | ||
| public DefaultRetryStrategyImpl() { | ||
| this.retryPolicies = new ArrayList<>(); | ||
| this.failsafeExecutor = Failsafe.none(); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a retry strategy with one or more retry policies. | ||
| * | ||
| * @param outerPolicy the primary retry policy (required) | ||
| * @param policies additional retry policies (optional) | ||
| */ | ||
| @SuppressWarnings("varargs") | ||
| public DefaultRetryStrategyImpl(RetryPolicy outerPolicy, RetryPolicy... policies) { | ||
| Preconditions.checkNotNull(outerPolicy); | ||
| this.retryPolicies = new ArrayList<>(); | ||
| this.retryPolicies.add(outerPolicy); | ||
| if (policies != null && policies.length > 0) { | ||
| this.retryPolicies.addAll(Arrays.asList(policies)); | ||
| } | ||
| this.failsafeExecutor = Failsafe.with(getDelegates()); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a retry strategy with a list of retry policies. | ||
| * | ||
| * @param policies the list of retry policies to apply | ||
| */ | ||
| public DefaultRetryStrategyImpl(List<RetryPolicy> policies) { | ||
| Preconditions.checkNotNull(policies); | ||
| this.retryPolicies = new ArrayList<>(); | ||
| this.retryPolicies.addAll(policies); | ||
| this.failsafeExecutor = Failsafe.with(getDelegates()); | ||
| } | ||
|
|
||
| /** | ||
| * Executes a runnable operation with retry logic. | ||
| * | ||
| * @param runnable the operation to execute | ||
| * @throws IOException if the operation fails after all retries | ||
| */ | ||
| @Override | ||
| public void execute(IORunnable runnable) throws IOException { | ||
| try { | ||
| this.failsafeExecutor.run(runnable::apply); | ||
| } catch (Exception ex) { | ||
| throw handleExceptionAfterRetry(ex); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Executes a supplier operation with retry logic. | ||
| * | ||
| * @param <T> return type of the supplier | ||
| * @param supplier the operation that returns a byte array | ||
| * @return the result of the supplier operation | ||
| * @throws IOException if the operation fails after all retries | ||
| */ | ||
| @Override | ||
| public <T> T get(IOSupplier<T> supplier) throws IOException { | ||
| try { | ||
| return this.failsafeExecutor.get(supplier::apply); | ||
| } catch (Exception ex) { | ||
| throw handleExceptionAfterRetry(ex); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public RetryStrategy amend(RetryPolicy policy) { | ||
| Preconditions.checkNotNull(policy); | ||
| this.failsafeExecutor = this.failsafeExecutor.compose(policy.getDelegate()); | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public RetryStrategy merge(RetryStrategy strategy) { | ||
| Preconditions.checkNotNull(strategy); | ||
| for (RetryPolicy policy : strategy.getRetryPolicies()) { | ||
| this.failsafeExecutor = this.failsafeExecutor.compose(policy.getDelegate()); | ||
| } | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public List<RetryPolicy> getRetryPolicies() { | ||
| return this.retryPolicies; | ||
| } | ||
|
|
||
| /** | ||
| * Converts retry policies to their Failsafe delegate policies. | ||
| * | ||
| * @return list of Failsafe policies | ||
| */ | ||
| private List<dev.failsafe.Policy<Object>> getDelegates() { | ||
| return this.retryPolicies.stream().map(RetryPolicy::getDelegate).collect(Collectors.toList()); | ||
| } | ||
|
|
||
| /** | ||
| * Handles exceptions after retry attempts are exhausted. | ||
| * | ||
| * @param e the exception that occurred | ||
| * @return an IOException to throw | ||
| */ | ||
| private IOException handleExceptionAfterRetry(Exception e) { | ||
| IOException toThrow = new IOException("Failed to execute operation with retries", e); | ||
|
|
||
| if (e instanceof FailsafeException) { | ||
| Optional<Throwable> cause = Optional.ofNullable(e.getCause()); | ||
| if (cause.isPresent()) { | ||
| if (cause.get() instanceof IOException) { | ||
| return (IOException) cause.get(); | ||
| } else { | ||
| toThrow = new IOException("Failed to execute operation with retries", cause.get()); | ||
| } | ||
| } | ||
| } | ||
| return toThrow; | ||
| } | ||
| } |
29 changes: 29 additions & 0 deletions
29
common/src/main/java/software/amazon/s3/analyticsaccelerator/util/retry/IORunnable.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"). | ||
| * You may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package software.amazon.s3.analyticsaccelerator.util.retry; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| /** A functional interface that mimics {@link Runnable}, but allows IOException to be thrown. */ | ||
| public interface IORunnable { | ||
| /** | ||
| * Functional representation of the code that takes no parameters and returns no value. The code | ||
| * is allowed to throw any exception. | ||
| * | ||
| * @throws IOException on error condition. | ||
| */ | ||
| void apply() throws IOException; | ||
| } | ||
35 changes: 35 additions & 0 deletions
35
common/src/main/java/software/amazon/s3/analyticsaccelerator/util/retry/IOSupplier.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"). | ||
| * You may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package software.amazon.s3.analyticsaccelerator.util.retry; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| /** | ||
| * A function that mimics {@link java.util.function.Supplier}, but allows IOException to be thrown | ||
| * and returns T. | ||
| */ | ||
| @FunctionalInterface | ||
| public interface IOSupplier<T> { | ||
|
|
||
| /** | ||
| * Functional representation of the code that takes no parameters and returns a value of type | ||
| * {@link T}. The code is allowed to throw any exception. | ||
| * | ||
| * @return a value of type {@link T}. | ||
| * @throws IOException on error condition. | ||
| */ | ||
| T apply() throws IOException; | ||
| } |
48 changes: 48 additions & 0 deletions
48
common/src/main/java/software/amazon/s3/analyticsaccelerator/util/retry/RetryPolicy.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"). | ||
| * You may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package software.amazon.s3.analyticsaccelerator.util.retry; | ||
|
|
||
| /** | ||
| * A retry policy interface that wraps the Failsafe retry policy for byte array operations. Provides | ||
| * factory methods to create retry policies with default or custom configurations. | ||
| */ | ||
| public interface RetryPolicy { | ||
|
|
||
| /** | ||
| * Creates a new retry policy builder. | ||
| * | ||
| * @return a new RetryPolicyBuilder instance | ||
| */ | ||
| static RetryPolicyBuilder builder() { | ||
| return new RetryPolicyBuilder(); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a retry policy with default settings. | ||
| * | ||
| * @return a RetryPolicy with default configuration | ||
| */ | ||
| static RetryPolicy ofDefaults() { | ||
| return RetryPolicy.builder().build(); | ||
| } | ||
|
|
||
| /** | ||
| * Gets the underlying Failsafe retry policy delegate. | ||
| * | ||
| * @return the Failsafe RetryPolicy for byte arrays | ||
| */ | ||
| dev.failsafe.RetryPolicy<Object> getDelegate(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldn't this be byte[]? Whenever I see Object as a type I get worried. |
||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.