diff --git a/src/main/java/io/floci/aws/services/glue/GlueService.java b/src/main/java/io/floci/aws/services/glue/GlueService.java index 8c312ba0d5..9945976ffa 100644 --- a/src/main/java/io/floci/aws/services/glue/GlueService.java +++ b/src/main/java/io/floci/aws/services/glue/GlueService.java @@ -445,7 +445,7 @@ public class GlueService { LOG.infov("Deleted Glue Database: {0}", name); } - public void createTable(String databaseName, Table table) { + public synchronized void createTable(String databaseName, Table table) { requireValidSortColumns(sortColumnViolations("tableInput", table.getStorageDescriptor())); Database database = getDatabase(databaseName); String key = tableKey(databaseName, table.getName()); diff --git a/src/test/java/io/floci/aws/services/appsync/AppSyncServiceTest.java b/src/test/java/io/floci/aws/services/appsync/AppSyncServiceTest.java index 8378e80a77..1dbfd12452 100644 --- a/src/test/java/io/floci/aws/services/appsync/AppSyncServiceTest.java +++ b/src/test/java/io/floci/aws/services/appsync/AppSyncServiceTest.java @@ -25,14 +25,9 @@ import java.time.ZoneOffset; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.concurrent.ExecutionException; -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.AtomicReference; -import static io.floci.aws.testing.ThreadStates.awaitBlockedOrDone; +import static io.floci.aws.testing.ParkingStoreAssertions.assertOperationWaitsForParkedStore; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -563,43 +558,6 @@ class AppSyncServiceTest { assertThrows(AwsException.class, () -> service.tagResource(null, Map.of())); } - /** - * Parks {@code first} in its next write to {@code store}, starts {@code second} on another - * thread, waits until it is blocked behind the first or done, then lets the first go and waits - * for both. The second can only be blocked if a lock covers the first's read, change and write; - * the callers assert the final state. - */ - private static void secondWriterRunsAroundTheFirst(ParkingStore store, Runnable first, Runnable second) - throws Exception { - secondCallRunsAroundTheFirst(store, store::parkNextWrite, first, second); - } - - /** As above, with the caller choosing which store call of the first one is parked. */ - private static void secondCallRunsAroundTheFirst(ParkingStore store, Runnable arm, Runnable first, - Runnable second) throws Exception { - arm.run(); - ExecutorService pool = Executors.newFixedThreadPool(2); - try { - Future firstWrite = pool.submit(first); - assertTrue(store.awaitParked(), "the first write must reach the store"); - AtomicReference secondThread = new AtomicReference<>(); - Future secondWrite = pool.submit(() -> { - secondThread.set(Thread.currentThread()); - second.run(); - }); - awaitBlockedOrDone(secondThread, secondWrite); - - store.release(); - firstWrite.get(2, TimeUnit.SECONDS); - secondWrite.get(2, TimeUnit.SECONDS); - } catch (ExecutionException e) { - throw new AssertionError(e.getCause()); - } finally { - store.release(); - pool.shutdownNow(); - } - } - private AppSyncService serviceWithApiStore(ParkingStore apiStore) { return newService(Clock.fixed(NOW, ZoneOffset.UTC), "http://localhost:4566", new AccountAwareStorageBackend<>(apiStore, null, "000000000000")); @@ -619,7 +577,8 @@ class AppSyncServiceTest { AppSyncService svc = serviceWithApiStore(store); GraphqlApi api = svc.createGraphqlApi(apiRequest("before"), "us-east-1"); - secondWriterRunsAroundTheFirst(store, + store.parkNextWrite(); + assertOperationWaitsForParkedStore(store, () -> svc.updateGraphqlApi(api.getApiId(), apiRequest("after"), "us-east-1"), () -> svc.tagResource(arnOf(api), Map.of("env", "test"))); @@ -634,7 +593,8 @@ class AppSyncServiceTest { GraphqlApi api = svc.createGraphqlApi(apiRequest("before"), "us-east-1"); svc.tagResource(arnOf(api), Map.of("drop", "1", "keep", "2")); - secondWriterRunsAroundTheFirst(store, + store.parkNextWrite(); + assertOperationWaitsForParkedStore(store, () -> svc.updateGraphqlApi(api.getApiId(), apiRequest("after"), "us-east-1"), () -> svc.untagResource(arnOf(api), List.of("drop"))); @@ -649,7 +609,8 @@ class AppSyncServiceTest { AppSyncService svc = serviceWithApiStore(store); GraphqlApi api = svc.createGraphqlApi(apiRequest("before"), "us-east-1"); - secondWriterRunsAroundTheFirst(store, + store.parkNextWrite(); + assertOperationWaitsForParkedStore(store, () -> svc.updateGraphqlApi(api.getApiId(), apiRequest("after"), "us-east-1"), () -> svc.putEnvironmentVariables(api.getApiId(), Map.of("TABLE_NAME", "my-table"))); @@ -666,7 +627,8 @@ class AppSyncServiceTest { ApiKey created = svc.createApiKey(api.getApiId(), Map.of()); long expires = NOW.plus(Duration.ofDays(30)).getEpochSecond(); - secondWriterRunsAroundTheFirst(keyStore, + keyStore.parkNextWrite(); + assertOperationWaitsForParkedStore(keyStore, () -> svc.updateApiKey(api.getApiId(), created.getId(), Map.of("description", "renewed")), () -> svc.updateApiKey(api.getApiId(), created.getId(), Map.of("expires", expires))); @@ -690,7 +652,8 @@ class AppSyncServiceTest { GraphqlApi api = svc.createGraphqlApi(apiRequest("kept"), "us-east-1"); AtomicReference deleteFailure = new AtomicReference<>(); - secondCallRunsAroundTheFirst(store, store::parkNextRead, + store.parkNextRead(); + assertOperationWaitsForParkedStore(store, () -> svc.startSchemaCreation(api.getApiId(), "type Query { a: String }"), () -> { try { diff --git a/src/test/java/io/floci/aws/services/glue/GlueServiceTest.java b/src/test/java/io/floci/aws/services/glue/GlueServiceTest.java index 77393757e3..ca078ef70f 100644 --- a/src/test/java/io/floci/aws/services/glue/GlueServiceTest.java +++ b/src/test/java/io/floci/aws/services/glue/GlueServiceTest.java @@ -68,10 +68,9 @@ import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicBoolean; -import java.util.concurrent.atomic.AtomicReference; import java.util.stream.IntStream; -import static io.floci.aws.testing.ThreadStates.awaitBlockedOrDone; +import static io.floci.aws.testing.ParkingStoreAssertions.assertOperationWaitsForParkedStore; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -126,12 +125,18 @@ class GlueServiceTest { private void setUp(StorageBackend partitions, StorageBackend functions) { + setUp(partitions, functions, new InMemoryStorage<>()); + } + + private void setUp(StorageBackend partitions, + StorageBackend functions, + StorageBackend tables) { RegionResolver regionResolver = new RegionResolver(REGION, ACCOUNT_ID); StorageFactory storageFactory = new InMemoryStorageFactory(); schemaRegistryService = spy(new GlueSchemaRegistryService(storageFactory, regionResolver)); kmsService = new KmsService(storageFactory, regionResolver); databaseStore = new InMemoryStorage<>(); - tableStore = new InMemoryStorage<>(); + tableStore = tables; tableVersionStore = new InMemoryStorage<>(); columnStatisticsStore = new InMemoryStorage<>(); partitionStore = partitions; @@ -2387,34 +2392,59 @@ class GlueServiceTest { glueService.createUserDefinedFunction("db1", function("db1", "fn")); } functions.parkNextWrite(); - ExecutorService executor = Executors.newFixedThreadPool(2); - try { - Future writer = executor.submit(() -> { - if (update) { - glueService.updateUserDefinedFunction("db1", "fn", function("db1", "fn")); - } else { - glueService.createUserDefinedFunction("db1", function("db1", "fn")); - } - }); - assertTrue(functions.awaitParked(), "function writer reached its publication point"); - AtomicReference deleteThread = new AtomicReference<>(); - Future deletion = executor.submit(() -> { - deleteThread.set(Thread.currentThread()); - glueService.deleteDatabase("db1"); - }); - awaitBlockedOrDone(deleteThread, deletion); - assertFalse(deletion.isDone(), "database deletion must wait for the in-flight function writer"); - functions.release(); - writer.get(5, TimeUnit.SECONDS); - deletion.get(5, TimeUnit.SECONDS); - glueService.createDatabase(new Database("db1")); - assertEquals("EntityNotFoundException", assertThrows(AwsException.class, - () -> glueService.getUserDefinedFunction("db1", "fn")).getErrorCode()); - } finally { - functions.release(); - executor.shutdownNow(); - assertTrue(executor.awaitTermination(5, TimeUnit.SECONDS)); + assertOperationWaitsForParkedStore(functions, () -> { + if (update) { + glueService.updateUserDefinedFunction("db1", "fn", function("db1", "fn")); + } else { + glueService.createUserDefinedFunction("db1", function("db1", "fn")); + } + }, () -> glueService.deleteDatabase("db1")); + + glueService.createDatabase(new Database("db1")); + assertEquals("EntityNotFoundException", assertThrows(AwsException.class, + () -> glueService.getUserDefinedFunction("db1", "fn")).getErrorCode()); + } + + @ParameterizedTest + @ValueSource(booleans = {false, true}) + void databaseDeletionWaitsForTablePublication(boolean update) throws Exception { + ParkingStore tables = new ParkingStore<>(); + setUp(new InMemoryStorage<>(), new InMemoryStorage<>(), tables); + if (update) { + glueService.createTable("db1", paginationTable("table")); } + tables.parkNextWrite(); + assertOperationWaitsForParkedStore(tables, () -> { + if (update) { + glueService.updateTable("db1", paginationTable("table"), null, false); + } else { + glueService.createTable("db1", paginationTable("table")); + } + }, () -> glueService.deleteDatabase("db1")); + + glueService.createDatabase(new Database("db1")); + assertTrue(glueService.getTables("db1").isEmpty()); + assertTrue(tableVersionStore.keys().isEmpty()); + glueService.createTable("db1", paginationTable("table")); + assertEquals("0", glueService.getTable("db1", "table").getVersionId()); + } + + @Test + void tableCreationWaitsForDatabaseDeletionAndThenRejectsTheMissingDatabase() throws Exception { + ParkingStore
tables = new ParkingStore<>(); + setUp(new InMemoryStorage<>(), new InMemoryStorage<>(), tables); + glueService.createTable("db1", paginationTable("existing")); + tables.parkNextRead(); + assertOperationWaitsForParkedStore(tables, () -> glueService.deleteDatabase("db1"), () -> { + AwsException refused = assertThrows(AwsException.class, + () -> glueService.createTable("db1", paginationTable("new"))); + assertEquals("EntityNotFoundException", refused.getErrorCode()); + }); + + glueService.createDatabase(new Database("db1")); + assertTrue(glueService.getTables("db1").isEmpty()); + glueService.createTable("db1", paginationTable("new")); + assertEquals("new", glueService.getTable("db1", "new").getName()); } private static UserDefinedFunction function(String database, String name) { diff --git a/src/test/java/io/floci/aws/testing/ParkingStoreAssertions.java b/src/test/java/io/floci/aws/testing/ParkingStoreAssertions.java new file mode 100644 index 0000000000..fcdf73115b --- /dev/null +++ b/src/test/java/io/floci/aws/testing/ParkingStoreAssertions.java @@ -0,0 +1,48 @@ +package io.floci.aws.testing; + +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import java.util.concurrent.atomic.AtomicReference; + +import static io.floci.aws.testing.ThreadStates.awaitBlockedOrDone; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** Assertions for operations that must serialize around an armed storage call. */ +public final class ParkingStoreAssertions { + + private ParkingStoreAssertions() {} + + /** + * Runs the first operation until it parks, asserts the second waits, then releases the store + * and waits for both results. The caller arms the store and checks the final resource state. + * Releases the store and shuts down the executor even when an assertion fails. + */ + public static void assertOperationWaitsForParkedStore(ParkingStore store, Runnable parkedOperation, + Runnable waitingOperation) + throws InterruptedException, ExecutionException, TimeoutException { + ExecutorService executor = Executors.newFixedThreadPool(2); + try { + Future parked = executor.submit(parkedOperation); + assertTrue(store.awaitParked(), "first operation reached the armed store"); + AtomicReference waitingThread = new AtomicReference<>(); + Future waiting = executor.submit(() -> { + waitingThread.set(Thread.currentThread()); + waitingOperation.run(); + }); + awaitBlockedOrDone(waitingThread, waiting); + assertFalse(waiting.isDone(), "second operation must wait for the parked operation"); + store.release(); + parked.get(5, TimeUnit.SECONDS); + waiting.get(5, TimeUnit.SECONDS); + } finally { + store.release(); + executor.shutdownNow(); + assertTrue(executor.awaitTermination(5, TimeUnit.SECONDS)); + } + } +}