mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
test(ec2): clean up failed concurrent image workers (#7374)
This commit is contained in:
1 parent
25780c051e
commit
80eee041d5
3 files changed
+107
-35
No files matched your search
+11
-32
@@ -8,6 +8,7 @@ import io.github.hectorvent.floci.services.ec2.model.Image;
|
||||
import io.github.hectorvent.floci.services.ec2.model.Instance;
|
||||
import io.github.hectorvent.floci.services.ec2.model.NetworkInterface;
|
||||
import io.github.hectorvent.floci.services.ec2.model.Tag;
|
||||
import io.github.hectorvent.floci.testing.StartGate;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.specification.RequestSpecification;
|
||||
import jakarta.inject.Inject;
|
||||
@@ -20,11 +21,6 @@ import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
@@ -548,7 +544,7 @@ class Ec2DeregisterAndCopyImageIntegrationTest {
|
||||
for (int round = 0; round < RACE_ROUNDS; round++) {
|
||||
String name = uniqueName("floci-race-dst");
|
||||
AtomicInteger duplicates = new AtomicInteger();
|
||||
List<Image> created = runConcurrently(RACE_THREADS, () -> {
|
||||
StartGate.Results<Image> created = StartGate.runConcurrently(RACE_THREADS, index -> {
|
||||
try {
|
||||
return service.copyImage(WEST, EAST, source.getImageId(), name, null, false, List.of());
|
||||
} catch (AwsException e) {
|
||||
@@ -558,7 +554,8 @@ class Ec2DeregisterAndCopyImageIntegrationTest {
|
||||
}
|
||||
});
|
||||
|
||||
List<Image> winners = created.stream().filter(Objects::nonNull).toList();
|
||||
assertTrue(created.errors().isEmpty(), () -> "copy workers failed: " + created.errors());
|
||||
List<Image> winners = created.values().stream().filter(Objects::nonNull).toList();
|
||||
assertEquals(1, winners.size(),
|
||||
"exactly one copy may claim the name; round " + round + " produced " + winners.size());
|
||||
assertEquals(RACE_THREADS - 1, duplicates.get());
|
||||
@@ -581,7 +578,7 @@ class Ec2DeregisterAndCopyImageIntegrationTest {
|
||||
|
||||
AtomicInteger successes = new AtomicInteger();
|
||||
AtomicInteger unavailable = new AtomicInteger();
|
||||
runConcurrently(RACE_THREADS, () -> {
|
||||
StartGate.Results<Void> results = StartGate.runConcurrently(RACE_THREADS, index -> {
|
||||
try {
|
||||
service.deregisterImage(EAST, image.getImageId(), true);
|
||||
successes.incrementAndGet();
|
||||
@@ -592,6 +589,7 @@ class Ec2DeregisterAndCopyImageIntegrationTest {
|
||||
return null;
|
||||
});
|
||||
|
||||
assertTrue(results.errors().isEmpty(), () -> "deregistration workers failed: " + results.errors());
|
||||
assertEquals(1, successes.get(), "round " + round + " deregistered the same AMI twice");
|
||||
assertEquals(RACE_THREADS - 1, unavailable.get());
|
||||
assertTrue(service.describeImages(EAST, List.of(image.getImageId()), List.of()).isEmpty());
|
||||
@@ -606,12 +604,12 @@ class Ec2DeregisterAndCopyImageIntegrationTest {
|
||||
"x86_64", "/dev/xvda", null);
|
||||
String prefix = uniqueName("floci-fanout-dst");
|
||||
|
||||
AtomicInteger seq = new AtomicInteger();
|
||||
List<Image> copies = runConcurrently(24,
|
||||
() -> service.copyImage(WEST, EAST, source.getImageId(),
|
||||
prefix + "-" + seq.getAndIncrement(), null, false, List.of()));
|
||||
StartGate.Results<Image> copies = StartGate.runConcurrently(24,
|
||||
index -> service.copyImage(WEST, EAST, source.getImageId(),
|
||||
prefix + "-" + index, null, false, List.of()));
|
||||
|
||||
Set<String> ids = copies.stream().map(Image::getImageId).collect(Collectors.toSet());
|
||||
assertTrue(copies.errors().isEmpty(), () -> "copy workers failed: " + copies.errors());
|
||||
Set<String> ids = copies.values().stream().map(Image::getImageId).collect(Collectors.toSet());
|
||||
assertEquals(24, ids.size(), "every concurrent copy must get a distinct id");
|
||||
assertEquals(24, service.describeImages(WEST, List.of(), List.of("self")).stream()
|
||||
.filter(img -> img.getName().startsWith(prefix))
|
||||
@@ -636,25 +634,6 @@ class Ec2DeregisterAndCopyImageIntegrationTest {
|
||||
"x86_64", "/dev/xvda", null);
|
||||
}
|
||||
|
||||
private <T> List<T> runConcurrently(int threads, Callable<T> body) throws Exception {
|
||||
ExecutorService pool = Executors.newFixedThreadPool(threads);
|
||||
CountDownLatch start = new CountDownLatch(1);
|
||||
List<Future<T>> futures = new ArrayList<>();
|
||||
for (int i = 0; i < threads; i++) {
|
||||
futures.add(pool.submit(() -> {
|
||||
start.await();
|
||||
return body.call();
|
||||
}));
|
||||
}
|
||||
start.countDown();
|
||||
List<T> results = new ArrayList<>();
|
||||
for (Future<T> future : futures) {
|
||||
results.add(future.get(30, TimeUnit.SECONDS));
|
||||
}
|
||||
pool.shutdown();
|
||||
return results;
|
||||
}
|
||||
|
||||
/**
|
||||
* A DeregisterImage landing after a launch's first tombstone check must leave nothing behind.
|
||||
* The rejection used to arrive only once the launch had already persisted its root volume, its
|
||||
|
||||
@@ -33,6 +33,7 @@ public final class StartGate {
|
||||
CountDownLatch doneGate = new CountDownLatch(threadCount);
|
||||
Object[] values = new Object[threadCount];
|
||||
List<Throwable> errors = Collections.synchronizedList(new ArrayList<>());
|
||||
Throwable failure = null;
|
||||
try {
|
||||
for (int i = 0; i < threadCount; i++) {
|
||||
int index = i;
|
||||
@@ -50,10 +51,34 @@ public final class StartGate {
|
||||
startGate.countDown();
|
||||
assertTrue(doneGate.await(30, TimeUnit.SECONDS),
|
||||
"concurrent tasks did not complete within 30s");
|
||||
} catch (InterruptedException | RuntimeException | Error e) {
|
||||
failure = e;
|
||||
throw e;
|
||||
} finally {
|
||||
pool.shutdownNow();
|
||||
assertTrue(pool.awaitTermination(5, TimeUnit.SECONDS),
|
||||
"thread pool did not terminate");
|
||||
try {
|
||||
pool.shutdownNow();
|
||||
assertTrue(pool.awaitTermination(5, TimeUnit.SECONDS),
|
||||
"thread pool did not terminate");
|
||||
} catch (InterruptedException | RuntimeException | Error cleanupFailure) {
|
||||
if (failure == null) {
|
||||
failure = cleanupFailure;
|
||||
throw cleanupFailure;
|
||||
}
|
||||
failure.addSuppressed(cleanupFailure);
|
||||
if (cleanupFailure instanceof InterruptedException) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
} finally {
|
||||
if (failure != null) {
|
||||
synchronized (errors) {
|
||||
for (Throwable workerFailure : errors) {
|
||||
if (workerFailure != failure) {
|
||||
failure.addSuppressed(workerFailure);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@SuppressWarnings("unchecked")
|
||||
List<T> results = (List<T>) Arrays.asList(values);
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
package io.github.hectorvent.floci.testing;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.FutureTask;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class StartGateTest {
|
||||
|
||||
@Test
|
||||
void interruptedWaitRetainsWorkerFailure() throws Exception {
|
||||
AssertionError workerFailure = new AssertionError("worker failed during interrupted wait");
|
||||
CountDownLatch entered = new CountDownLatch(2);
|
||||
CountDownLatch release = new CountDownLatch(1);
|
||||
FutureTask<StartGate.Results<Integer>> operation = new FutureTask<>(
|
||||
() -> StartGate.runConcurrently(2, index -> {
|
||||
entered.countDown();
|
||||
if (index == 0) {
|
||||
throw workerFailure;
|
||||
}
|
||||
awaitRelease(release);
|
||||
return index;
|
||||
}));
|
||||
Thread caller = new Thread(operation, "start-gate-interrupted-caller");
|
||||
caller.start();
|
||||
try {
|
||||
assertTrue(entered.await(5, TimeUnit.SECONDS));
|
||||
caller.interrupt();
|
||||
ExecutionException failure = assertThrows(ExecutionException.class,
|
||||
() -> operation.get(10, TimeUnit.SECONDS));
|
||||
assertInstanceOf(InterruptedException.class, failure.getCause());
|
||||
assertTrue(Arrays.asList(failure.getCause().getSuppressed()).contains(workerFailure));
|
||||
} finally {
|
||||
release.countDown();
|
||||
caller.interrupt();
|
||||
caller.join(10_000);
|
||||
assertFalse(caller.isAlive());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void completedWorkersStillReturnValuesAndErrors() throws InterruptedException {
|
||||
AssertionError workerFailure = new AssertionError("completed failure");
|
||||
StartGate.Results<Integer> result = StartGate.runConcurrently(3, index -> {
|
||||
if (index == 1) {
|
||||
throw workerFailure;
|
||||
}
|
||||
return index;
|
||||
});
|
||||
assertEquals(Arrays.asList(0, null, 2), result.values());
|
||||
assertEquals(1, result.errors().size());
|
||||
assertSame(workerFailure, result.errors().getFirst());
|
||||
}
|
||||
|
||||
private static void awaitRelease(CountDownLatch release) {
|
||||
try {
|
||||
release.await();
|
||||
} catch (InterruptedException expected) {
|
||||
// StartGate must cancel blocked workers after timeout or caller interruption.
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user