mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(glue): implement partition indexes (#3551)
* feat(glue): implement partition indexes
CreatePartitionIndex and DeletePartitionIndex were missing, and
GetPartitionIndexes was a stub that resolved the table and then always
returned an empty list. A client could create an index and read back
nothing, and Terraform's aws_glue_partition_index could not apply at all.
Behaviour captured against real Glue in us-west-2, on a table partitioned
by tenant/year/month/day:
- an index key must name a partition column, else InvalidInputException
"IndexKeys not a part of PartitionColumns. Verify the indexKeys : [x]"
- keys need not be a prefix of the partition keys, and need not be in
partition order: [year] and [month, tenant] are both accepted
- a repeated index name gives AlreadyExistsException "... already exists
with the same name."
- a repeated key list under a new name is refused too: "... already
exists with the same keys." The comparison is order sensitive, so
[year, month] and [month, year] are distinct indexes
- a fourth index gives ResourceNumberLimitExceededException "Partition
index limit exceeded. Maximum: 3", and the cap is evaluated before the
duplicate checks
- deleting an unknown index gives EntityNotFoundException "Index with the
given indexName : x does not exist."
- a read resolves each key to its name and type from the table's
partition keys, while a create carries names only
Indexes live in their own store keyed by the table key, so the existing
deleteTable cascade sweeps them and a recreated table starts clean.
One deliberate divergence: an index is stored ACTIVE immediately. Real
Glue creates asynchronously, reports CREATING while it backfills, and
refuses a concurrent create on the same table. Settling immediately keeps
tests free of polling, and a client that waits for ACTIVE (the Terraform
provider does) completes on its first read.
Verified end to end against a local floci: the SDK round-trips create,
read and delete, every error above matches the capture, and a real
aws_glue_partition_index applies and destroys cleanly.
* feat(glue): expose the partition index lifecycle
The first cut stored an index ACTIVE on create and removed it on delete,
treating the lifecycle as an internal detail. It is not: a caller reads
IndexStatus from GetPartitionIndexes and can branch on it, so collapsing
the states changed an AWS-visible contract.
Captured against real Glue in us-west-2:
- a new index reports CREATING for roughly 12 seconds, then ACTIVE
- a deleted index reports DELETING before it disappears
- while either is in progress, another create on the same table gives
ResourceNumberLimitExceededException "Index <name> is in <STATE>
state. Only 1 index can be created or deleted simultaneously per
table."
- an index that is still CREATING cannot be deleted: Glue reports
EntityNotFoundException "Index with the given indexName : <name> does
not exist." even while a read lists it as CREATING. Measured in
isolation, with nothing else in flight
The transitions are driven by reads rather than wall-clock time, so they
stay deterministic and tests need no sleeps: each GetPartitionIndexes
reports the current state and settles it, so the next read sees the
outcome. A client that polls converges on its second call.
FAILED is not emulated; it only arises from a backfill failure.
Verified with a real aws_glue_partition_index against a local floci:
apply polls CREATING to ACTIVE and completes, a refresh reports no drift,
and destroy passes through DELETING.
Also replaces var with explicit types and drops inline fully-qualified
names in the tests, per the AGENTS.md rule.
* docs(glue): correct two javadocs left behind by the index lifecycle
Both still said an index is stored ACTIVE immediately, which stopped being
true when createPartitionIndex started storing CREATING and letting
GetPartitionIndexes settle it.
* fix(glue): serialize partition index writes against their own reads
createPartitionIndex reads the stored indexes, checks the cap and the duplicate
rules, then writes, with nothing holding that together. Two concurrent creates
on a table at two indexes both saw room under the cap of three and both wrote,
leaving four. Nothing repairs that afterwards.
create, delete and the settling loop in getPartitionIndexes now carry the same
synchronized treatment updateTable already uses for its own check-then-write.
This commit is contained in:
1 parent
43a0d737e9
commit
38d1d0a8e4
8 files changed
+595
-7
No files matched your search
@@ -2,7 +2,9 @@ package io.github.hectorvent.floci.services.glue;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
@@ -13,6 +15,7 @@ import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
@@ -71,7 +74,7 @@ class GlueJsonHandlerTest {
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Object> body = (Map<String, Object>) response.getEntity();
|
||||
assertTrue(body.containsKey("PartitionIndexDescriptorList"));
|
||||
assertTrue(((java.util.List<?>) body.get("PartitionIndexDescriptorList")).isEmpty());
|
||||
assertTrue(((List<?>) body.get("PartitionIndexDescriptorList")).isEmpty());
|
||||
}
|
||||
|
||||
/** A missing table is reported as missing, not as a table that happens to have no indexes. */
|
||||
@@ -86,6 +89,241 @@ class GlueJsonHandlerTest {
|
||||
assertThrows(Exception.class, () -> handler.handle("GetPartitionIndexes", request, REGION));
|
||||
}
|
||||
|
||||
/** Creates a table whose partition keys are the given names, all typed {@code string}. */
|
||||
private void createPartitionedTable(String dbName, String tableName, String... partitionKeys) throws Exception {
|
||||
ObjectNode createDb = mapper.createObjectNode();
|
||||
createDb.putObject("DatabaseInput").put("Name", dbName);
|
||||
try {
|
||||
handler.handle("CreateDatabase", createDb, REGION);
|
||||
} catch (AwsException alreadyExists) {
|
||||
// The helper is called twice in the drop-and-recreate case; the database survives.
|
||||
}
|
||||
|
||||
ObjectNode createTable = mapper.createObjectNode();
|
||||
createTable.put("DatabaseName", dbName);
|
||||
ObjectNode tableInput = createTable.putObject("TableInput");
|
||||
tableInput.put("Name", tableName);
|
||||
ArrayNode keys = tableInput.putArray("PartitionKeys");
|
||||
for (String partitionKey : partitionKeys) {
|
||||
keys.addObject().put("Name", partitionKey).put("Type", "string");
|
||||
}
|
||||
assertEquals(200, handler.handle("CreateTable", createTable, REGION).getStatus());
|
||||
}
|
||||
|
||||
private Response createIndex(String dbName, String tableName, String indexName, String... keys) throws Exception {
|
||||
ObjectNode request = mapper.createObjectNode();
|
||||
request.put("DatabaseName", dbName);
|
||||
request.put("TableName", tableName);
|
||||
ObjectNode index = request.putObject("PartitionIndex");
|
||||
index.put("IndexName", indexName);
|
||||
ArrayNode keyArray = index.putArray("Keys");
|
||||
for (String key : keys) {
|
||||
keyArray.add(key);
|
||||
}
|
||||
return handler.handle("CreatePartitionIndex", request, REGION);
|
||||
}
|
||||
|
||||
private Response deleteIndex(String dbName, String tableName, String indexName) throws Exception {
|
||||
ObjectNode request = mapper.createObjectNode();
|
||||
request.put("DatabaseName", dbName);
|
||||
request.put("TableName", tableName);
|
||||
request.put("IndexName", indexName);
|
||||
return handler.handle("DeletePartitionIndex", request, REGION);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private List<Map<String, Object>> listIndexes(String dbName, String tableName) throws Exception {
|
||||
ObjectNode request = mapper.createObjectNode();
|
||||
request.put("DatabaseName", dbName);
|
||||
request.put("TableName", tableName);
|
||||
Response response = handler.handle("GetPartitionIndexes", request, REGION);
|
||||
assertEquals(200, response.getStatus());
|
||||
Map<String, Object> body = (Map<String, Object>) response.getEntity();
|
||||
return mapper.convertValue(
|
||||
body.get("PartitionIndexDescriptorList"), new TypeReference<List<Map<String, Object>>>() {});
|
||||
}
|
||||
|
||||
/** Reads once to settle any in-progress index, mirroring a client that polls to ACTIVE. */
|
||||
private void settleIndexes(String dbName, String tableName) throws Exception {
|
||||
listIndexes(dbName, tableName);
|
||||
}
|
||||
|
||||
private static String statusOf(List<Map<String, Object>> indexes, String indexName) {
|
||||
return indexes.stream()
|
||||
.filter(index -> indexName.equals(index.get("IndexName")))
|
||||
.map(index -> (String) index.get("IndexStatus"))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
// ── Partition indexes ──────────────────────────────────────────
|
||||
//
|
||||
// Behaviour below was captured against real Glue in us-west-2 on a table partitioned by
|
||||
// tenant/year/month/day, so the codes, messages and lifecycle states are AWS's own.
|
||||
|
||||
@Test
|
||||
void aNewIndexReportsCreatingAndThenBecomesActive() throws Exception {
|
||||
createPartitionedTable("idx_db", "events", "tenant", "year", "month", "day");
|
||||
assertEquals(200, createIndex("idx_db", "events", "by_tenant", "tenant").getStatus());
|
||||
|
||||
// Real Glue reports CREATING until the backfill finishes, then ACTIVE.
|
||||
List<Map<String, Object>> whileCreating = listIndexes("idx_db", "events");
|
||||
assertEquals("CREATING", statusOf(whileCreating, "by_tenant"));
|
||||
|
||||
List<Map<String, Object>> settled = listIndexes("idx_db", "events");
|
||||
assertEquals("ACTIVE", statusOf(settled, "by_tenant"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aReadResolvesEachKeyToItsNameAndType() throws Exception {
|
||||
createPartitionedTable("idx_db_keys_shape", "events", "tenant", "year");
|
||||
createIndex("idx_db_keys_shape", "events", "by_tenant", "tenant");
|
||||
|
||||
List<Map<String, Object>> indexes = listIndexes("idx_db_keys_shape", "events");
|
||||
assertEquals(1, indexes.size());
|
||||
assertEquals("by_tenant", indexes.get(0).get("IndexName"));
|
||||
@SuppressWarnings("unchecked")
|
||||
List<Map<String, Object>> keys = (List<Map<String, Object>>) indexes.get(0).get("Keys");
|
||||
// A request carries key names only; the read resolves each one's type from the table.
|
||||
assertEquals("tenant", keys.get(0).get("Name"));
|
||||
assertEquals("string", keys.get(0).get("Type"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aDeletedIndexReportsDeletingAndThenDisappears() throws Exception {
|
||||
createPartitionedTable("idx_db_del", "events", "tenant", "year");
|
||||
createIndex("idx_db_del", "events", "by_tenant", "tenant");
|
||||
settleIndexes("idx_db_del", "events");
|
||||
|
||||
assertEquals(200, deleteIndex("idx_db_del", "events", "by_tenant").getStatus());
|
||||
|
||||
List<Map<String, Object>> whileDeleting = listIndexes("idx_db_del", "events");
|
||||
assertEquals("DELETING", statusOf(whileDeleting, "by_tenant"));
|
||||
|
||||
assertTrue(listIndexes("idx_db_del", "events").isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aSecondIndexCannotBeCreatedWhileOneIsCreating() throws Exception {
|
||||
createPartitionedTable("idx_db_busy", "events", "tenant", "year");
|
||||
createIndex("idx_db_busy", "events", "first", "tenant");
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class,
|
||||
() -> createIndex("idx_db_busy", "events", "second", "year"));
|
||||
assertEquals("ResourceNumberLimitExceededException", ex.getErrorCode());
|
||||
assertTrue(ex.getMessage().contains("is in CREATING state"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anIndexCannotBeCreatedWhileAnotherIsDeleting() throws Exception {
|
||||
createPartitionedTable("idx_db_busy2", "events", "tenant", "year");
|
||||
createIndex("idx_db_busy2", "events", "first", "tenant");
|
||||
settleIndexes("idx_db_busy2", "events");
|
||||
deleteIndex("idx_db_busy2", "events", "first");
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class,
|
||||
() -> createIndex("idx_db_busy2", "events", "second", "year"));
|
||||
assertEquals("ResourceNumberLimitExceededException", ex.getErrorCode());
|
||||
assertTrue(ex.getMessage().contains("is in DELETING state"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anIndexStillCreatingCannotBeDeleted() throws Exception {
|
||||
createPartitionedTable("idx_db_earlydel", "events", "tenant");
|
||||
createIndex("idx_db_earlydel", "events", "by_tenant", "tenant");
|
||||
|
||||
// Measured in isolation: Glue reports it absent even while a read lists it as CREATING.
|
||||
AwsException ex = assertThrows(AwsException.class,
|
||||
() -> deleteIndex("idx_db_earlydel", "events", "by_tenant"));
|
||||
assertEquals("EntityNotFoundException", ex.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anIndexKeyMustBeOneOfTheTablesPartitionKeys() throws Exception {
|
||||
createPartitionedTable("idx_db_key", "events", "tenant", "year");
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class,
|
||||
() -> createIndex("idx_db_key", "events", "bad", "nosuchcolumn"));
|
||||
assertEquals("InvalidInputException", ex.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anIndexNameCannotBeReused() throws Exception {
|
||||
createPartitionedTable("idx_db_dup", "events", "tenant", "year");
|
||||
createIndex("idx_db_dup", "events", "dup", "tenant");
|
||||
settleIndexes("idx_db_dup", "events");
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class,
|
||||
() -> createIndex("idx_db_dup", "events", "dup", "year"));
|
||||
assertEquals("AlreadyExistsException", ex.getErrorCode());
|
||||
assertTrue(ex.getMessage().contains("same name"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aSecondIndexOverTheSameKeysIsRefusedEvenUnderANewName() throws Exception {
|
||||
createPartitionedTable("idx_db_keys", "events", "tenant", "year");
|
||||
createIndex("idx_db_keys", "events", "first", "tenant");
|
||||
settleIndexes("idx_db_keys", "events");
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class,
|
||||
() -> createIndex("idx_db_keys", "events", "second", "tenant"));
|
||||
assertEquals("AlreadyExistsException", ex.getErrorCode());
|
||||
assertTrue(ex.getMessage().contains("same keys"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void keyOrderDistinguishesTwoIndexes() throws Exception {
|
||||
createPartitionedTable("idx_db_order", "events", "tenant", "year", "month");
|
||||
|
||||
// [year, month] and [month, year] are different indexes on real Glue, not duplicates.
|
||||
assertEquals(200, createIndex("idx_db_order", "events", "ord1", "year", "month").getStatus());
|
||||
settleIndexes("idx_db_order", "events");
|
||||
assertEquals(200, createIndex("idx_db_order", "events", "ord2", "month", "year").getStatus());
|
||||
settleIndexes("idx_db_order", "events");
|
||||
assertEquals(2, listIndexes("idx_db_order", "events").size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aFourthIndexExceedsTheLimit() throws Exception {
|
||||
createPartitionedTable("idx_db_cap", "events", "tenant", "year", "month", "day");
|
||||
createIndex("idx_db_cap", "events", "i1", "tenant");
|
||||
settleIndexes("idx_db_cap", "events");
|
||||
createIndex("idx_db_cap", "events", "i2", "year");
|
||||
settleIndexes("idx_db_cap", "events");
|
||||
createIndex("idx_db_cap", "events", "i3", "month");
|
||||
settleIndexes("idx_db_cap", "events");
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class,
|
||||
() -> createIndex("idx_db_cap", "events", "i4", "day"));
|
||||
assertEquals("ResourceNumberLimitExceededException", ex.getErrorCode());
|
||||
assertTrue(ex.getMessage().contains("Maximum: 3"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void deletingAnUnknownIndexFails() throws Exception {
|
||||
createPartitionedTable("idx_db_missing", "events", "tenant");
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class,
|
||||
() -> deleteIndex("idx_db_missing", "events", "absent"));
|
||||
assertEquals("EntityNotFoundException", ex.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void droppingATableDropsItsIndexes() throws Exception {
|
||||
createPartitionedTable("idx_db_cascade", "events", "tenant");
|
||||
createIndex("idx_db_cascade", "events", "by_tenant", "tenant");
|
||||
settleIndexes("idx_db_cascade", "events");
|
||||
|
||||
ObjectNode dropTable = mapper.createObjectNode();
|
||||
dropTable.put("DatabaseName", "idx_db_cascade");
|
||||
dropTable.put("Name", "events");
|
||||
assertEquals(200, handler.handle("DeleteTable", dropTable, REGION).getStatus());
|
||||
|
||||
// Recreating the table must not resurrect the old index.
|
||||
createPartitionedTable("idx_db_cascade", "events", "tenant");
|
||||
assertTrue(listIndexes("idx_db_cascade", "events").isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createCrawlerWithScheduleSucceeds() throws Exception {
|
||||
ObjectNode request = mapper.createObjectNode();
|
||||
|
||||
@@ -15,6 +15,8 @@ import io.github.hectorvent.floci.services.glue.model.Job;
|
||||
import io.github.hectorvent.floci.services.glue.model.JobCommand;
|
||||
import io.github.hectorvent.floci.services.glue.model.JobUpdate;
|
||||
import io.github.hectorvent.floci.services.glue.model.Partition;
|
||||
import io.github.hectorvent.floci.services.glue.model.PartitionIndex;
|
||||
import io.github.hectorvent.floci.services.glue.model.PartitionIndexDescriptor;
|
||||
import io.github.hectorvent.floci.services.glue.model.S3Target;
|
||||
import io.github.hectorvent.floci.services.glue.model.SchemaReference;
|
||||
import io.github.hectorvent.floci.services.glue.model.StorageDescriptor;
|
||||
@@ -31,6 +33,8 @@ import java.time.Instant;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
@@ -76,6 +80,7 @@ class GlueServiceTest {
|
||||
tableVersionStore,
|
||||
columnStatisticsStore,
|
||||
partitionStore,
|
||||
new InMemoryStorage<String, PartitionIndexDescriptor>(),
|
||||
new InMemoryStorage<String, Map<String, Object>>(),
|
||||
new InMemoryStorage<String, UserDefinedFunction>(),
|
||||
new InMemoryStorage<String, Job>(),
|
||||
@@ -1258,4 +1263,52 @@ class GlueServiceTest {
|
||||
AwsException udfTagEx = assertThrows(AwsException.class, () -> glueService.tagResource(fakeUdfArn, Map.of("k", "v"), REGION));
|
||||
assertEquals("EntityNotFoundException", udfTagEx.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void concurrentPartitionIndexCreatesStopAtTheCap() throws Exception {
|
||||
Table table = new Table();
|
||||
table.setName("indexed");
|
||||
StorageDescriptor sd = new StorageDescriptor();
|
||||
sd.setColumns(java.util.List.of(new Column("a", "string")));
|
||||
table.setStorageDescriptor(sd);
|
||||
table.setPartitionKeys(java.util.List.of(new Column("a", "string"), new Column("b", "string")));
|
||||
glueService.createTable("db1", table);
|
||||
|
||||
// Two settled indexes leave exactly one slot under the cap of three.
|
||||
createSettledIndex("idx0", "a");
|
||||
createSettledIndex("idx1", "b");
|
||||
|
||||
int attempts = 4;
|
||||
CountDownLatch start = new CountDownLatch(1);
|
||||
CountDownLatch done = new CountDownLatch(attempts);
|
||||
for (int i = 0; i < attempts; i++) {
|
||||
String indexName = "race" + i;
|
||||
Thread.ofVirtual().start(() -> {
|
||||
try {
|
||||
start.await();
|
||||
PartitionIndex index = new PartitionIndex();
|
||||
index.setIndexName(indexName);
|
||||
index.setKeys(java.util.List.of("a", "b"));
|
||||
glueService.createPartitionIndex("db1", "indexed", index);
|
||||
} catch (AwsException | InterruptedException expected) {
|
||||
// Only the create that takes the last slot succeeds; the rest are rejected.
|
||||
} finally {
|
||||
done.countDown();
|
||||
}
|
||||
});
|
||||
}
|
||||
start.countDown();
|
||||
assertTrue(done.await(10, TimeUnit.SECONDS), "partition index creates did not finish");
|
||||
|
||||
assertEquals(3, glueService.getPartitionIndexes("db1", "indexed").size());
|
||||
}
|
||||
|
||||
private void createSettledIndex(String indexName, String key) {
|
||||
PartitionIndex index = new PartitionIndex();
|
||||
index.setIndexName(indexName);
|
||||
index.setKeys(java.util.List.of(key));
|
||||
glueService.createPartitionIndex("db1", "indexed", index);
|
||||
glueService.getPartitionIndexes("db1", "indexed");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user