-
Notifications
You must be signed in to change notification settings - Fork 1k
feat(sdk-core): add SdkWarmUp.prime() for CRaC auto-priming #7056
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
Open
joviegas
wants to merge
3
commits into
feature/master/crac_auto_priming_support
Choose a base branch
from
joviegas/crac_warmup_orchestration_partA
base: feature/master/crac_auto_priming_support
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
72 changes: 72 additions & 0 deletions
72
core/sdk-core/src/main/java/software/amazon/awssdk/core/crac/SdkWarmUp.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,72 @@ | ||
| /* | ||
| * 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. | ||
| * A copy of the License is located at | ||
| * | ||
| * http://aws.amazon.com/apache2.0 | ||
| * | ||
| * or in the "license" file accompanying this file. This file 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.awssdk.core.crac; | ||
|
|
||
| import java.util.ServiceLoader; | ||
| import software.amazon.awssdk.annotations.SdkPublicApi; | ||
| import software.amazon.awssdk.annotations.ThreadSafe; | ||
| import software.amazon.awssdk.core.internal.crac.ClasspathWarmUpInvoker; | ||
|
|
||
| /** | ||
| * Entry point for warming up SDK service request paths before a Coordinated Restore at Checkpoint (CRaC) | ||
| * checkpoint. | ||
| * | ||
| * <p>{@link #prime()} discovers every {@link SdkWarmUpProvider} registered on the classpath through {@link | ||
| * ServiceLoader} (via the {@code META-INF/services/software.amazon.awssdk.core.crac.SdkWarmUpProvider} | ||
| * resource) and invokes {@link SdkWarmUpProvider#warmUp()} on each. | ||
| * | ||
| * <p>Behavior contract: | ||
| * <ul> | ||
| * <li><b>Idempotent:</b> {@code prime()} runs the warm-up at most once per JVM. Once a call completes | ||
| * successfully, later calls return immediately. If a call throws before completing, a later call retries. | ||
| * Concurrent callers block until the in-flight call finishes, then observe its result.</li> | ||
| * <li><b>Per-provider resilience:</b> a single provider that throws from {@code warmUp()}, or that fails | ||
| * to load, does not prevent the remaining providers from running.</li> | ||
| * <li><b>Safe when empty:</b> if no providers are registered, {@code prime()} is a no-op.</li> | ||
| * </ul> | ||
| * | ||
| * <p>Call this once during application initialization, before a CRaC checkpoint is taken. | ||
| */ | ||
| @ThreadSafe | ||
| @SdkPublicApi | ||
| public final class SdkWarmUp { | ||
|
|
||
| private static final Object PRIME_LOCK = new Object(); | ||
|
|
||
| private static volatile boolean primed = false; | ||
|
|
||
| private SdkWarmUp() { | ||
| } | ||
|
|
||
| /** | ||
| * Discovers every {@link SdkWarmUpProvider} on the classpath and invokes {@link SdkWarmUpProvider#warmUp()} | ||
| * on each, honoring the idempotency, per-provider resilience, and empty-classpath behavior described on | ||
| * this class. Safe to call concurrently. | ||
| */ | ||
| public static void prime() { | ||
| if (primed) { | ||
| return; | ||
| } | ||
| synchronized (PRIME_LOCK) { | ||
| if (primed) { | ||
| return; | ||
| } | ||
| // Set primed only after invokeAll() succeeds, so a failed run leaves primed false and a later call retries. | ||
| ClasspathWarmUpInvoker.create().invokeAll(); | ||
| primed = true; | ||
| } | ||
| } | ||
| } |
76 changes: 76 additions & 0 deletions
76
...-core/src/main/java/software/amazon/awssdk/core/internal/crac/ClasspathWarmUpInvoker.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,76 @@ | ||
| /* | ||
| * 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. | ||
| * A copy of the License is located at | ||
| * | ||
| * http://aws.amazon.com/apache2.0 | ||
| * | ||
| * or in the "license" file accompanying this file. This file 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.awssdk.core.internal.crac; | ||
|
|
||
| import java.util.Iterator; | ||
| import java.util.ServiceConfigurationError; | ||
| import java.util.ServiceLoader; | ||
| import software.amazon.awssdk.annotations.SdkInternalApi; | ||
| import software.amazon.awssdk.annotations.SdkTestInternalApi; | ||
| import software.amazon.awssdk.core.crac.SdkWarmUpProvider; | ||
| import software.amazon.awssdk.utils.Logger; | ||
|
|
||
| /** | ||
| * {@link WarmUpInvoker} implementation that uses {@link ServiceLoader} to find {@link SdkWarmUpProvider} | ||
| * implementations on the classpath and invokes {@code warmUp()} on every one of them. | ||
| */ | ||
| @SdkInternalApi | ||
| public final class ClasspathWarmUpInvoker implements WarmUpInvoker { | ||
|
|
||
| private static final Logger log = Logger.loggerFor(ClasspathWarmUpInvoker.class); | ||
|
|
||
| private final WarmUpServiceLoader serviceLoader; | ||
|
|
||
| @SdkTestInternalApi | ||
| ClasspathWarmUpInvoker(WarmUpServiceLoader serviceLoader) { | ||
| this.serviceLoader = serviceLoader; | ||
| } | ||
|
|
||
| @Override | ||
| public void invokeAll() { | ||
| Iterator<SdkWarmUpProvider> iterator = serviceLoader.loadProviders(); | ||
| boolean invokedAny = false; | ||
|
|
||
| while (iterator.hasNext()) { | ||
| SdkWarmUpProvider provider; | ||
| try { | ||
| provider = iterator.next(); | ||
| } catch (ServiceConfigurationError e) { | ||
| // next() has already advanced past the bad provider, so it is safe to continue to the next one. | ||
| log.warn(() -> "Skipping an SdkWarmUpProvider that could not be loaded.", e); | ||
| continue; | ||
| } | ||
|
|
||
| invokedAny = true; | ||
| try { | ||
| provider.warmUp(); | ||
| } catch (RuntimeException e) { | ||
| log.warn(() -> "An SdkWarmUpProvider failed during warmUp() and was skipped.", e); | ||
| } | ||
| } | ||
|
|
||
| if (!invokedAny) { | ||
| log.debug(() -> "No SdkWarmUpProvider implementations were discovered on the classpath."); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @return ClasspathWarmUpInvoker that discovers {@link SdkWarmUpProvider}s from the classpath. | ||
| */ | ||
| public static WarmUpInvoker create() { | ||
| return new ClasspathWarmUpInvoker(WarmUpServiceLoader.INSTANCE); | ||
| } | ||
| } | ||
34 changes: 34 additions & 0 deletions
34
core/sdk-core/src/main/java/software/amazon/awssdk/core/internal/crac/WarmUpInvoker.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,34 @@ | ||
| /* | ||
| * 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. | ||
| * A copy of the License is located at | ||
| * | ||
| * http://aws.amazon.com/apache2.0 | ||
| * | ||
| * or in the "license" file accompanying this file. This file 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.awssdk.core.internal.crac; | ||
|
|
||
| import software.amazon.awssdk.annotations.SdkInternalApi; | ||
| import software.amazon.awssdk.core.crac.SdkWarmUpProvider; | ||
|
|
||
| /** | ||
| * Discovers {@link SdkWarmUpProvider}s and invokes their warm-up behind the public {@code SdkWarmUp.prime()}. | ||
| * Mirrors the {@code SdkHttpServiceProvider} loader abstraction, except warm-up invokes every discovered | ||
| * provider rather than selecting one. | ||
| */ | ||
| @SdkInternalApi | ||
| public interface WarmUpInvoker { | ||
|
|
||
| /** | ||
| * Invokes {@link SdkWarmUpProvider#warmUp()} on every discovered provider, containing per-provider failures | ||
| * so one failing provider does not stop the others. | ||
| */ | ||
| void invokeAll(); | ||
| } |
36 changes: 36 additions & 0 deletions
36
...sdk-core/src/main/java/software/amazon/awssdk/core/internal/crac/WarmUpServiceLoader.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,36 @@ | ||
| /* | ||
| * 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. | ||
| * A copy of the License is located at | ||
| * | ||
| * http://aws.amazon.com/apache2.0 | ||
| * | ||
| * or in the "license" file accompanying this file. This file 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.awssdk.core.internal.crac; | ||
|
|
||
| import java.util.Iterator; | ||
| import java.util.ServiceLoader; | ||
| import software.amazon.awssdk.annotations.SdkInternalApi; | ||
| import software.amazon.awssdk.core.crac.SdkWarmUpProvider; | ||
| import software.amazon.awssdk.core.internal.util.ClassLoaderHelper; | ||
|
|
||
| /** | ||
| * Thin layer over {@link ServiceLoader} for {@link SdkWarmUpProvider}. | ||
| */ | ||
| @SdkInternalApi | ||
| class WarmUpServiceLoader { | ||
|
|
||
| public static final WarmUpServiceLoader INSTANCE = new WarmUpServiceLoader(); | ||
|
|
||
| Iterator<SdkWarmUpProvider> loadProviders() { | ||
| return ServiceLoader.load(SdkWarmUpProvider.class, | ||
| ClassLoaderHelper.classLoader(WarmUpServiceLoader.class)).iterator(); | ||
| } | ||
| } |
33 changes: 33 additions & 0 deletions
33
core/sdk-core/src/test/java/software/amazon/awssdk/core/crac/RegisteredWarmUpProvider.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,33 @@ | ||
| /* | ||
| * 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. | ||
| * A copy of the License is located at | ||
| * | ||
| * http://aws.amazon.com/apache2.0 | ||
| * | ||
| * or in the "license" file accompanying this file. This file 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.awssdk.core.crac; | ||
|
|
||
| import java.util.concurrent.atomic.AtomicInteger; | ||
|
|
||
| /** | ||
| * Test-only {@link SdkWarmUpProvider} registered in test-scoped {@code META-INF/services} so a real {@link | ||
| * java.util.ServiceLoader} discovers and instantiates it by name. {@code INVOCATIONS} is static because the loader | ||
| * builds its own instance. Must be public with a no-arg constructor for ServiceLoader. | ||
| */ | ||
| public final class RegisteredWarmUpProvider implements SdkWarmUpProvider { | ||
|
|
||
| public static final AtomicInteger INVOCATIONS = new AtomicInteger(); | ||
|
|
||
| @Override | ||
| public void warmUp() { | ||
| INVOCATIONS.incrementAndGet(); | ||
| } | ||
| } |
63 changes: 63 additions & 0 deletions
63
core/sdk-core/src/test/java/software/amazon/awssdk/core/crac/SdkWarmUpTest.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,63 @@ | ||
| /* | ||
| * 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. | ||
| * A copy of the License is located at | ||
| * | ||
| * http://aws.amazon.com/apache2.0 | ||
| * | ||
| * or in the "license" file accompanying this file. This file 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.awssdk.core.crac; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.concurrent.CountDownLatch; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| /** | ||
| * Tests the static {@link SdkWarmUp#prime()} entry point end to end through {@link java.util.ServiceLoader}, | ||
| * using a test-scoped {@code META-INF/services} registration of {@link RegisteredWarmUpProvider}. {@code prime()} | ||
| * runs at most once per JVM, so many concurrent calls must invoke the provider exactly once in total. | ||
| */ | ||
| class SdkWarmUpTest { | ||
|
|
||
| @Test | ||
| void prime_concurrentCalls_invokeRegisteredProviderExactlyOnce() throws InterruptedException { | ||
| RegisteredWarmUpProvider.INVOCATIONS.set(0); | ||
| int threadCount = 16; | ||
| CountDownLatch start = new CountDownLatch(1); | ||
| CountDownLatch done = new CountDownLatch(threadCount); | ||
| List<Thread> threads = new ArrayList<>(); | ||
|
|
||
| for (int i = 0; i < threadCount; i++) { | ||
| Thread thread = new Thread(() -> { | ||
| try { | ||
| start.await(); | ||
| SdkWarmUp.prime(); | ||
| } catch (InterruptedException e) { | ||
| Thread.currentThread().interrupt(); | ||
| } finally { | ||
| done.countDown(); | ||
| } | ||
| }); | ||
| threads.add(thread); | ||
| thread.start(); | ||
| } | ||
|
|
||
| start.countDown(); | ||
| done.await(); | ||
| for (Thread thread : threads) { | ||
| thread.join(); | ||
| } | ||
|
|
||
| assertThat(RegisteredWarmUpProvider.INVOCATIONS.get()).isEqualTo(1); | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason to have two seperate try catch blocks here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Each block catches a different failure.
The first block wraps
iterator.next(), which is where a provider is located and instantiated. Per theServiceLoaderjavadoc, this can throwServiceConfigurationErrorif a provider-configuration file violates the specified format, if it names a provider class that cannot be found and instantiated, or if the result is not assignable to the service type. We catch that, log it, and continue with the remaining providers, so one bad provider does not abort the rest. Wrappingnext()alone, not in a widerRuntimeExceptioncatch, keeps any other failure fromnext()visible.The second block wraps
provider.warmUp()and catchesRuntimeExceptionthrown by the provider's own code. We contain that one provider and still run the rest.We intentionally contain failures from
next()andwarmUp()only.hasNext()stays in the loop condition, so if it throws we let it propagate rather than swallow it.