Light, fluffy, and always free - The AWS Local Emulator alternative
fix(dynamodb): recompute UpdateTable AttributeDefinitions (#4046)
TATSUNO “Taz” Yasuhiro60fd65b5b63c
diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.javaindex 41603c4220..926519d114 100644--- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java+++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java@@ -1888,13 +1888,16 @@ public class DynamoDbService implements ResourceProvider {}}- Set<String> knownAttrs = table.getAttributeDefinitions().stream()- .map(AttributeDefinition::getAttributeName)- .collect(Collectors.toSet());- if (newAttrDefs != null) newAttrDefs.forEach(ad -> knownAttrs.add(ad.getAttributeName()));+ // A new index's key attributes must all appear in the request's own AttributeDefinitions.+ // The definitions already stored on the table do not satisfy this, even for an attribute+ // the table key uses.+ Set<String> requestAttrs = new HashSet<>();+ if (newAttrDefs != null) {+ newAttrDefs.forEach(ad -> requestAttrs.add(ad.getAttributeName()));+ }for (GlobalSecondaryIndex newGsi : gsiCreates) {for (KeySchemaElement k : newGsi.getKeySchema()) {- if (!knownAttrs.contains(k.getAttributeName())) {+ if (!requestAttrs.contains(k.getAttributeName())) {throw new AwsException("ValidationException","Attribute: " + k.getAttributeName() + " is not defined in AttributeDefinitions", 400);}@@ -1935,12 +1938,42 @@ public class DynamoDbService implements ResourceProvider {}}}+ pruneUnusedAttributeDefinitions(table);tableStore.put(storageKey, table);LOG.infov("Updated table: {0} in region {1}", canonicalTableName, region);return table;}+ /**+ * UpdateTable keeps only the attribute definitions a key schema still uses. A definition sent+ * with the request but used by no key is dropped in the same response, and deleting an index+ * drops the definitions only that index used. CreateTable rejects an unused definition instead.+ * AWS documents neither rule. Both are observed behaviour.+ */+ private void pruneUnusedAttributeDefinitions(TableDefinition table) {+ Set<String> used = new HashSet<>();+ for (KeySchemaElement k : table.getKeySchema()) {+ used.add(k.getAttributeName());+ }+ for (LocalSecondaryIndex lsi : table.getLocalSecondaryIndexes()) {+ for (KeySchemaElement k : lsi.getKeySchema()) {+ used.add(k.getAttributeName());+ }+ }+ for (GlobalSecondaryIndex gsi : table.getGlobalSecondaryIndexes()) {+ for (KeySchemaElement k : gsi.getKeySchema()) {+ used.add(k.getAttributeName());+ }+ }+ List<AttributeDefinition> kept = table.getAttributeDefinitions().stream()+ .filter(ad -> used.contains(ad.getAttributeName()))+ .collect(Collectors.toCollection(ArrayList::new));+ if (kept.size() != table.getAttributeDefinitions().size()) {+ table.setAttributeDefinitions(kept);+ }+ }+// --- Global-table replicas ---/**diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbJsonHandlerTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbJsonHandlerTest.javaindex 4c35d41f0b..707c714f4e 100644--- a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbJsonHandlerTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbJsonHandlerTest.java@@ -18,6 +18,8 @@ import org.junit.jupiter.api.Test;import org.junit.jupiter.api.io.TempDir;import java.nio.file.Path;+import java.util.ArrayList;+import java.util.Comparator;import java.util.HashMap;import java.util.HashSet;import java.util.List;@@ -759,6 +761,147 @@ class DynamoDbJsonHandlerTest {+ "ProjectionType is ALL, but NonKeyAttributes is specified", ex.getMessage());}+ @Test+ void updateTableDropsADefinitionNoKeyUses() throws Exception {+ handler.handle("CreateTable", json("""+ {+ "TableName": "Drops",+ "KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}],+ "AttributeDefinitions": [{"AttributeName": "pk", "AttributeType": "S"}],+ "BillingMode": "PAY_PER_REQUEST"+ }+ """), "eu-west-1");++ Response response = handler.handle("UpdateTable", json("""+ {+ "TableName": "Drops",+ "AttributeDefinitions": [+ {"AttributeName": "g1", "AttributeType": "S"},+ {"AttributeName": "extraUnused", "AttributeType": "S"}+ ],+ "GlobalSecondaryIndexUpdates": [{"Create": {+ "IndexName": "gsi1",+ "KeySchema": [{"AttributeName": "g1", "KeyType": "HASH"}],+ "Projection": {"ProjectionType": "KEYS_ONLY"}+ }}]+ }+ """), "eu-west-1");++ JsonNode body = mapper.convertValue(response.getEntity(), JsonNode.class);+ assertEquals(List.of("g1", "pk"), sortedAttributeNames(body.get("TableDescription")));+ }++ @Test+ void updateTableDeletingAGsiPrunesOnlyThatIndexKeyAttribute() throws Exception {+ handler.handle("CreateTable", json("""+ {+ "TableName": "Prunes",+ "KeySchema": [+ {"AttributeName": "pk", "KeyType": "HASH"},+ {"AttributeName": "sk", "KeyType": "RANGE"}+ ],+ "AttributeDefinitions": [+ {"AttributeName": "pk", "AttributeType": "S"},+ {"AttributeName": "sk", "AttributeType": "S"},+ {"AttributeName": "gsiSk", "AttributeType": "S"}+ ],+ "BillingMode": "PAY_PER_REQUEST",+ "GlobalSecondaryIndexes": [{+ "IndexName": "gsi1",+ "KeySchema": [+ {"AttributeName": "pk", "KeyType": "HASH"},+ {"AttributeName": "gsiSk", "KeyType": "RANGE"}+ ],+ "Projection": {"ProjectionType": "KEYS_ONLY"}+ }]+ }+ """), "eu-west-1");++ Response response = handler.handle("UpdateTable", json("""+ {+ "TableName": "Prunes",+ "GlobalSecondaryIndexUpdates": [{"Delete": {"IndexName": "gsi1"}}]+ }+ """), "eu-west-1");++ JsonNode body = mapper.convertValue(response.getEntity(), JsonNode.class);+ assertEquals(List.of("pk", "sk"), sortedAttributeNames(body.get("TableDescription")));+ }++ @Test+ void updateTableDeletingAGsiKeepsTheAttributesAnLsiStillUses() throws Exception {+ handler.handle("CreateTable", json("""+ {+ "TableName": "SharedKeys",+ "KeySchema": [+ {"AttributeName": "pk", "KeyType": "HASH"},+ {"AttributeName": "sk", "KeyType": "RANGE"}+ ],+ "AttributeDefinitions": [+ {"AttributeName": "pk", "AttributeType": "S"},+ {"AttributeName": "sk", "AttributeType": "S"},+ {"AttributeName": "lsiSk", "AttributeType": "N"},+ {"AttributeName": "gsiSk", "AttributeType": "S"}+ ],+ "BillingMode": "PAY_PER_REQUEST",+ "LocalSecondaryIndexes": [{+ "IndexName": "lsi1",+ "KeySchema": [+ {"AttributeName": "pk", "KeyType": "HASH"},+ {"AttributeName": "lsiSk", "KeyType": "RANGE"}+ ],+ "Projection": {"ProjectionType": "KEYS_ONLY"}+ }],+ "GlobalSecondaryIndexes": [{+ "IndexName": "gsi1",+ "KeySchema": [+ {"AttributeName": "pk", "KeyType": "HASH"},+ {"AttributeName": "gsiSk", "KeyType": "RANGE"}+ ],+ "Projection": {"ProjectionType": "KEYS_ONLY"}+ }]+ }+ """), "eu-west-1");++ Response response = handler.handle("UpdateTable", json("""+ {+ "TableName": "SharedKeys",+ "GlobalSecondaryIndexUpdates": [{"Delete": {"IndexName": "gsi1"}}]+ }+ """), "eu-west-1");++ JsonNode body = mapper.convertValue(response.getEntity(), JsonNode.class);+ assertEquals(List.of("lsiSk", "pk", "sk"), sortedAttributeNames(body.get("TableDescription")));+ }++ @Test+ void updateTableRejectsAnIndexKeyOnlyTheStoredDefinitionsCarry() {+ createUsersTable("eu-west-1");+ AwsException ex = expectValidationException("UpdateTable", json("""+ {+ "TableName": "Users",+ "AttributeDefinitions": [{"AttributeName": "g3", "AttributeType": "S"}],+ "GlobalSecondaryIndexUpdates": [{"Create": {+ "IndexName": "gsiSharedPk",+ "KeySchema": [+ {"AttributeName": "userId", "KeyType": "HASH"},+ {"AttributeName": "g3", "KeyType": "RANGE"}+ ],+ "Projection": {"ProjectionType": "KEYS_ONLY"}+ }}]+ }+ """));+ assertEquals("Attribute: userId is not defined in AttributeDefinitions", ex.getMessage());+ }++ private List<String> sortedAttributeNames(JsonNode tableDescription) {+ List<String> names = new ArrayList<>();+ tableDescription.path("AttributeDefinitions")+ .forEach(definition -> names.add(definition.path("AttributeName").asText()));+ names.sort(Comparator.naturalOrder());+ return names;+ }+// Checked against real DynamoDB: the projection is validated before the table lookup.@Testvoid updateTableValidatesGsiProjectionBeforeTableLookup() {