fix(glue): preserve existing partitions on duplicate creation (#8100)

* fix(glue): preserve existing partitions on duplicate creation

Reject CreatePartition for values already stored in the table before
mutating partition metadata. Keep the existing BatchCreatePartition
error and allow explicit updates through UpdatePartition.

Port the independent duplicate check and regression from
AndreKurait/floci commit f6905c8ced6c21fd9aeca013beb4ac511e5c1469.
Add SDK coverage for single and batch creation, preserved metadata,
per-item batch errors, and subsequent explicit updates.

Signed-off-by: Andre Kurait <akurait@amazon.com>
Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>

* fix(glue): serialize concurrent partition creation

Synchronize single and batch creates so duplicate detection and writes share
one service critical section. Cover all four concurrent single/batch combinations,
preserving the first metadata and per-item batch errors.

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>

* test(glue): isolate catalog fixtures from Athena readers

Share the existing floci-duck resource lock with Athena and Firehose tests.
Their catalog setup must not read temporary Glue metadata whose S3 locations
are placeholders while the catalog test is running.

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>

---------

Signed-off-by: Andre Kurait <akurait@amazon.com>
Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>
Co-authored-by: Andre Kurait <akurait@amazon.com>
This commit is contained in:
Jackson PriceandAndre Kurait authored and GitHub committed 2026-11-12 09:19:57 -04:00
1 parent 17af8368b5
commit 08d7ca5b4d
4 files changed
+217 -3

No files matched your search

@@ -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");
@@ -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<BatchCreatePartitionError> batchCreatePartitions(
public synchronized List<BatchCreatePartitionError> batchCreatePartitions(
String databaseName,
String tableName,
List<Partition> partitions) {
@@ -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());
});
}
}
@@ -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<String, Partition> 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<List<GlueService.BatchCreatePartitionError>> 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<List<GlueService.BatchCreatePartitionError>> 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<String, Partition> {
private final AtomicBoolean pause = new AtomicBoolean(true);
private final CountDownLatch firstRead = new CountDownLatch(1);
private final CountDownLatch release = new CountDownLatch(1);
@Override
public Optional<Partition> get(String key) {
Optional<Partition> 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();