Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
}
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 {

Copy link
Copy Markdown
Contributor

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?

Copy link
Copy Markdown
Contributor Author

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 the ServiceLoader javadoc, this can throw ServiceConfigurationError if 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. Wrapping next() alone, not in a wider RuntimeException catch, keeps any other failure from next() visible.

The second block wraps provider.warmUp() and catches RuntimeException thrown by the provider's own code. We contain that one provider and still run the rest.

We intentionally contain failures from next() and warmUp() only. hasNext() stays in the loop condition, so if it throws we let it propagate rather than swallow it.

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);
}
}
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();
}
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();
}
}
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();
}
}
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);
}
}
Loading