diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/GlueCatalogTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/GlueCatalogTest.java index 917053ae9b..c40a195ac9 100644 --- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/GlueCatalogTest.java +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/GlueCatalogTest.java @@ -4,6 +4,7 @@ import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.parallel.ResourceLock; import software.amazon.awssdk.services.glue.GlueClient; import software.amazon.awssdk.services.glue.model.BatchCreatePartitionRequest; import software.amazon.awssdk.services.glue.model.BatchCreatePartitionResponse; @@ -92,6 +93,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; @DisplayName("Glue catalog") +@ResourceLock("floci-duck") class GlueCatalogTest { private static final String DATABASE_NAME = TestFixtures.uniqueName("catalog_db"); 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 4fdd1680fd..317cb12454 100644 --- a/src/main/java/io/floci/aws/services/glue/GlueService.java +++ b/src/main/java/io/floci/aws/services/glue/GlueService.java @@ -869,10 +869,13 @@ public class GlueService { columnStatisticsStore.delete(columnStatisticsKey(databaseName, tableName, columnName)); } - public void createPartition(String databaseName, String tableName, Partition partition) { + public synchronized void createPartition(String databaseName, String tableName, Partition partition) { requireValidSortColumns(sortColumnViolations("partitionInput", partition.getStorageDescriptor())); Table table = getTable(databaseName, tableName); String key = partitionKey(databaseName, tableName, partition.getValues()); + if (partitionStore.get(key).isPresent()) { + throw new AwsException("AlreadyExistsException", "Partition already exists.", 400); + } partition.setDatabaseName(table.getDatabaseName()); partition.setTableName(table.getName()); if (partition.getCreationTime() == null) { @@ -881,7 +884,7 @@ public class GlueService { partitionStore.put(key, partition); } - public List batchCreatePartitions( + public synchronized List batchCreatePartitions( String databaseName, String tableName, List partitions) { diff --git a/src/test/java/io/floci/aws/services/glue/GlueCreatePartitionIntegrationTest.java b/src/test/java/io/floci/aws/services/glue/GlueCreatePartitionIntegrationTest.java new file mode 100644 index 0000000000..92cbe8ddc8 --- /dev/null +++ b/src/test/java/io/floci/aws/services/glue/GlueCreatePartitionIntegrationTest.java @@ -0,0 +1,79 @@ +package io.floci.aws.services.glue; + +import io.quarkus.test.junit.QuarkusTest; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; +import software.amazon.awssdk.services.glue.model.AlreadyExistsException; +import software.amazon.awssdk.services.glue.model.BatchCreatePartitionResponse; +import software.amazon.awssdk.services.glue.model.Partition; +import software.amazon.awssdk.services.glue.model.PartitionInput; + +import java.time.Instant; +import java.util.List; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +@QuarkusTest +class GlueCreatePartitionIntegrationTest { + + @ParameterizedTest + @ValueSource(booleans = {false, true}) + void duplicateCreatePreservesTheStoredPartition(boolean createdInBatch) { + GlueCatalogPaginationTestSupport.withCatalog("741648700013", "create_partition_", (client, database) -> { + client.createTable(request -> request.databaseName(database).tableInput(input -> input.name("events") + .partitionKeys(key -> key.name("year").type("string"), key -> key.name("month").type("string")) + .storageDescriptor(sd -> sd.columns(column -> column.name("id").type("string"))))); + PartitionInput original = PartitionInput.builder().values("2026", "11") + .parameters(Map.of("revision", "original")) + .lastAccessTime(Instant.parse("2026-11-01T00:00:00Z")) + .storageDescriptor(sd -> sd.location("s3://catalog-fixture/events/year=2026/month=11/") + .columns(column -> column.name("id").type("string"))).build(); + if (createdInBatch) { + assertTrue(client.batchCreatePartition(request -> request.databaseName(database).tableName("events") + .partitionInputList(original)).errors().isEmpty()); + } else { + client.createPartition(request -> request.databaseName(database).tableName("events") + .partitionInput(original)); + } + Partition stored = client.getPartition(request -> request.databaseName(database).tableName("events") + .partitionValues(original.values())).partition(); + assertNotNull(stored.creationTime()); + PartitionInput replacement = original.toBuilder().parameters(Map.of("revision", "replacement")) + .lastAccessTime(Instant.parse("2026-11-02T00:00:00Z")) + .storageDescriptor(sd -> sd.location("s3://catalog-fixture/replacement/") + .columns(column -> column.name("id").type("string"))).build(); + + AlreadyExistsException refused = assertThrows(AlreadyExistsException.class, + () -> client.createPartition(request -> request.databaseName(database).tableName("events") + .partitionInput(replacement))); + assertEquals(400, refused.statusCode()); + assertEquals(stored, client.getPartition(request -> request.databaseName(database).tableName("events") + .partitionValues(original.values())).partition()); + + PartitionInput nextMonth = original.toBuilder().values("2026", "12").build(); + BatchCreatePartitionResponse batch = client.batchCreatePartition(request -> request.databaseName(database) + .tableName("events").partitionInputList(replacement, nextMonth)); + assertEquals(1, batch.errors().size()); + assertEquals(original.values(), batch.errors().getFirst().partitionValues()); + assertEquals("AlreadyExistsException", batch.errors().getFirst().errorDetail().errorCode()); + assertEquals(stored, client.getPartition(request -> request.databaseName(database).tableName("events") + .partitionValues(original.values())).partition()); + assertEquals(List.of("2026", "12"), client.getPartition(request -> request.databaseName(database) + .tableName("events").partitionValues(nextMonth.values())).partition().values()); + assertEquals(2, client.getPartitions(request -> request.databaseName(database).tableName("events")) + .partitions().size()); + + client.updatePartition(request -> request.databaseName(database).tableName("events") + .partitionValueList(original.values()).partitionInput(replacement)); + Partition updated = client.getPartition(request -> request.databaseName(database).tableName("events") + .partitionValues(original.values())).partition(); + assertEquals(replacement.parameters(), updated.parameters()); + assertEquals(replacement.storageDescriptor().location(), updated.storageDescriptor().location()); + assertEquals(replacement.lastAccessTime(), updated.lastAccessTime()); + }); + } +} 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 f6b099c4bf..6d4c40d399 100644 --- a/src/test/java/io/floci/aws/services/glue/GlueServiceTest.java +++ b/src/test/java/io/floci/aws/services/glue/GlueServiceTest.java @@ -44,6 +44,8 @@ import io.floci.aws.services.kms.KmsService; import io.floci.aws.services.resourcegroupstagging.ResourceGroupsTaggingService; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; import java.nio.charset.StandardCharsets; import java.time.Instant; @@ -51,12 +53,21 @@ import java.util.Base64; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.concurrent.CountDownLatch; +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.AtomicBoolean; import java.util.stream.IntStream; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; @@ -94,6 +105,10 @@ class GlueServiceTest { @BeforeEach void setUp() { + setUp(new InMemoryStorage<>()); + } + + private void setUp(StorageBackend partitions) { RegionResolver regionResolver = new RegionResolver(REGION, ACCOUNT_ID); StorageFactory storageFactory = new InMemoryStorageFactory(); schemaRegistryService = spy(new GlueSchemaRegistryService(storageFactory, regionResolver)); @@ -102,7 +117,7 @@ class GlueServiceTest { tableStore = new InMemoryStorage<>(); tableVersionStore = new InMemoryStorage<>(); columnStatisticsStore = new InMemoryStorage<>(); - partitionStore = new InMemoryStorage<>(); + partitionStore = partitions; glueService = new GlueService( databaseStore, tableStore, @@ -548,6 +563,121 @@ class GlueServiceTest { assertTrue(partitionStore.scan(k -> true).isEmpty()); } + @Test + void createPartitionRefusesValuesThatAlreadyExist() { + Table table = new Table(); + table.setName("plain"); + table.setPartitionKeys(List.of(new Column("year", "string"))); + glueService.createTable("db1", table); + Partition first = new Partition(); + first.setValues(List.of("2026")); + first.setParameters(Map.of("kept", "yes")); + glueService.createPartition("db1", "plain", first); + + Partition second = new Partition(); + second.setValues(List.of("2026")); + second.setParameters(Map.of("kept", "no")); + AwsException refused = assertThrows(AwsException.class, + () -> glueService.createPartition("db1", "plain", second)); + + assertEquals("AlreadyExistsException", refused.getErrorCode()); + assertEquals(Map.of("kept", "yes"), + glueService.getPartition("db1", "plain", List.of("2026")).getParameters()); + } + + @ParameterizedTest + @CsvSource({"false, false", "false, true", "true, false", "true, true"}) + void concurrentPartitionCreatesPreserveTheFirstWrite(boolean firstInBatch, boolean duplicateInBatch) + throws Exception { + PausingPartitionStore store = new PausingPartitionStore(); + setUp(store); + Table table = new Table(); + table.setName("plain"); + table.setPartitionKeys(List.of(new Column("year", "string"))); + glueService.createTable("db1", table); + Partition first = new Partition(); + first.setValues(List.of("2026")); + first.setParameters(Map.of("revision", "first")); + Partition duplicate = new Partition(); + duplicate.setValues(first.getValues()); + duplicate.setParameters(Map.of("revision", "duplicate")); + Partition other = new Partition(); + other.setValues(List.of("2027")); + + ExecutorService pool = Executors.newFixedThreadPool(2); + try { + Future> original = pool.submit(() -> { + if (firstInBatch) { + return glueService.batchCreatePartitions("db1", "plain", List.of(first)); + } + glueService.createPartition("db1", "plain", first); + return List.of(); + }); + assertTrue(store.firstRead.await(5, TimeUnit.SECONDS), "first create did not read the partition"); + CountDownLatch duplicateStarted = new CountDownLatch(1); + Future> competing = pool.submit(() -> { + duplicateStarted.countDown(); + if (duplicateInBatch) { + return glueService.batchCreatePartitions("db1", "plain", List.of(duplicate, other)); + } + glueService.createPartition("db1", "plain", duplicate); + return List.of(); + }); + assertTrue(duplicateStarted.await(5, TimeUnit.SECONDS), "duplicate create did not start"); + try { + competing.get(500, TimeUnit.MILLISECONDS); + } catch (TimeoutException expected) { + // The duplicate waits while the first create holds its read open. + } catch (ExecutionException expected) { + // Check the request's result below after releasing the first create. + } + store.release.countDown(); + + assertEquals(List.of(), assertDoesNotThrow(() -> original.get(5, TimeUnit.SECONDS))); + if (duplicateInBatch) { + assertEquals(List.of(new GlueService.BatchCreatePartitionError(first.getValues(), + new GlueService.ErrorDetail("AlreadyExistsException", "Partition already exists."))), + assertDoesNotThrow(() -> competing.get(5, TimeUnit.SECONDS))); + assertEquals(other.getValues(), + glueService.getPartition("db1", "plain", other.getValues()).getValues()); + } else { + ExecutionException rejected = assertThrows(ExecutionException.class, + () -> competing.get(5, TimeUnit.SECONDS)); + AwsException failure = assertInstanceOf(AwsException.class, rejected.getCause()); + assertEquals("AlreadyExistsException", failure.getErrorCode()); + assertEquals(400, failure.getHttpStatus()); + } + assertEquals(first.getParameters(), + glueService.getPartition("db1", "plain", first.getValues()).getParameters()); + assertEquals(duplicateInBatch ? 2 : 1, glueService.getPartitions("db1", "plain").size()); + } finally { + store.release.countDown(); + pool.shutdownNow(); + assertTrue(pool.awaitTermination(5, TimeUnit.SECONDS), "partition creates did not stop"); + } + } + + private static final class PausingPartitionStore extends InMemoryStorage { + private final AtomicBoolean pause = new AtomicBoolean(true); + private final CountDownLatch firstRead = new CountDownLatch(1); + private final CountDownLatch release = new CountDownLatch(1); + + @Override + public Optional get(String key) { + Optional found = super.get(key); + if (pause.compareAndSet(true, false)) { + firstRead.countDown(); + try { + assertTrue(release.await(10, TimeUnit.SECONDS), "first partition read was not released"); + } catch (InterruptedException interrupted) { + Thread.currentThread().interrupt(); + throw new IllegalStateException("Interrupted while holding the first partition read", interrupted); + } + } + return found; + } + } + @Test void partitionsCanBeFetchedFilteredAndDeleted() { Table table = new Table();