diff --git a/src/main/java/io/floci/aws/services/dynamodb/DynamoDbService.java b/src/main/java/io/floci/aws/services/dynamodb/DynamoDbService.java index c333a3a59f..8a8734dcfe 100644 --- a/src/main/java/io/floci/aws/services/dynamodb/DynamoDbService.java +++ b/src/main/java/io/floci/aws/services/dynamodb/DynamoDbService.java @@ -38,6 +38,7 @@ import io.floci.aws.services.dynamodb.model.IncrementalExportSpecification; import io.floci.aws.services.dynamodb.model.KeySchemaElement; import io.floci.aws.services.dynamodb.model.LocalSecondaryIndex; import io.floci.aws.services.dynamodb.model.PointInTimeChange; +import io.floci.aws.services.dynamodb.model.ProvisionedThroughput; import io.floci.aws.services.dynamodb.model.StreamDescription; import io.floci.aws.services.dynamodb.model.TableBackup; import io.floci.aws.services.dynamodb.model.TableDefinition; @@ -648,6 +649,11 @@ public class DynamoDbService { table.getProvisionedThroughput().setReadCapacityUnits(readCapacity); table.getProvisionedThroughput().setWriteCapacityUnits(writeCapacity); } + // Track warm throughput: on-demand uses fixed values, provisioned uses the capacity. + ProvisionedThroughput tp = readCapacity != null && writeCapacity != null + ? new ProvisionedThroughput(readCapacity, writeCapacity) : null; + table.setWarmReadUnitsPerSecond(TableDefinition.defaultWarmReadUnits(billingMode, tp)); + table.setWarmWriteUnitsPerSecond(TableDefinition.defaultWarmWriteUnits(billingMode, tp)); if (gsis != null && !gsis.isEmpty()) { for (GlobalSecondaryIndex gsi : gsis) { @@ -2610,9 +2616,32 @@ public class DynamoDbService { if (readCapacity != null) { table.getProvisionedThroughput().setReadCapacityUnits(readCapacity); + if (readCapacity > table.getWarmReadUnitsPerSecond()) { + table.setWarmReadUnitsPerSecond(readCapacity); + } } if (writeCapacity != null) { table.getProvisionedThroughput().setWriteCapacityUnits(writeCapacity); + if (writeCapacity > table.getWarmWriteUnitsPerSecond()) { + table.setWarmWriteUnitsPerSecond(writeCapacity); + } + } + + // Handle billing mode switch. + if (billingMode != null && !billingMode.equals(table.getBillingMode())) { + table.setBillingMode(billingMode); + if ("PAY_PER_REQUEST".equals(billingMode)) { + table.getProvisionedThroughput().setReadCapacityUnits(0L); + table.getProvisionedThroughput().setWriteCapacityUnits(0L); + long wru = TableDefinition.defaultWarmReadUnits("PAY_PER_REQUEST", null); + long wwu = TableDefinition.defaultWarmWriteUnits("PAY_PER_REQUEST", null); + if (wru > table.getWarmReadUnitsPerSecond()) { + table.setWarmReadUnitsPerSecond(wru); + } + if (wwu > table.getWarmWriteUnitsPerSecond()) { + table.setWarmWriteUnitsPerSecond(wwu); + } + } } for (String indexName : gsiDeletes) { @@ -4141,7 +4170,7 @@ public class DynamoDbService { arr.add(value); } } - + private void setValueAtPath(ObjectNode item, String path, JsonNode value, JsonNode exprAttrNames) { List tokens = parsePath(path, exprAttrNames); if (tokens.isEmpty()) return; diff --git a/src/main/java/io/floci/aws/services/dynamodb/NativeDynamoDbJsonHandler.java b/src/main/java/io/floci/aws/services/dynamodb/NativeDynamoDbJsonHandler.java index a1bf4c1d34..d395936d6c 100644 --- a/src/main/java/io/floci/aws/services/dynamodb/NativeDynamoDbJsonHandler.java +++ b/src/main/java/io/floci/aws/services/dynamodb/NativeDynamoDbJsonHandler.java @@ -197,8 +197,10 @@ public class NativeDynamoDbJsonHandler { throwValidationErrors(createTableViolations(request)); TableDefinition table = createTableFromSpec(request, "", region, "ACTIVE"); + ObjectNode tableNode = tableToNode(table); + tableNode.remove("WarmThroughput"); ObjectNode response = objectMapper.createObjectNode(); - response.set("TableDescription", tableToNode(table)); + response.set("TableDescription", tableNode); return Response.ok(response).build(); } @@ -3924,8 +3926,8 @@ public class NativeDynamoDbJsonHandler { ObjectNode warmThroughput = objectMapper.createObjectNode(); warmThroughput.put("Status", "ACTIVE"); - warmThroughput.put("ReadUnitsPerSecond", 0); - warmThroughput.put("WriteUnitsPerSecond", 0); + warmThroughput.put("ReadUnitsPerSecond", table.getWarmReadUnitsPerSecond()); + warmThroughput.put("WriteUnitsPerSecond", table.getWarmWriteUnitsPerSecond()); node.set("WarmThroughput", warmThroughput); // Global-table replicas. In a single-process emulator every region is served by this same diff --git a/src/main/java/io/floci/aws/services/dynamodb/NativeDynamoDbTableService.java b/src/main/java/io/floci/aws/services/dynamodb/NativeDynamoDbTableService.java index 0777a18193..ec27295093 100644 --- a/src/main/java/io/floci/aws/services/dynamodb/NativeDynamoDbTableService.java +++ b/src/main/java/io/floci/aws/services/dynamodb/NativeDynamoDbTableService.java @@ -466,14 +466,6 @@ public class NativeDynamoDbTableService { table.setDeletionProtectionEnabled(settings.deletionProtectionEnabled()); } - String billingMode = settings.billingMode(); - if (billingMode != null) { - table.setBillingMode(billingMode); - if ("PAY_PER_REQUEST".equals(billingMode)) { - table.getProvisionedThroughput().setReadCapacityUnits(0L); - table.getProvisionedThroughput().setWriteCapacityUnits(0L); - } - } if (settings.tableClass() != null) { table.setTableClass(settings.tableClass()); diff --git a/src/main/java/io/floci/aws/services/dynamodb/model/TableDefinition.java b/src/main/java/io/floci/aws/services/dynamodb/model/TableDefinition.java index 789ab04d85..109f2422ba 100644 --- a/src/main/java/io/floci/aws/services/dynamodb/model/TableDefinition.java +++ b/src/main/java/io/floci/aws/services/dynamodb/model/TableDefinition.java @@ -28,6 +28,8 @@ public class TableDefinition { private long itemCount; private long tableSizeBytes; private ProvisionedThroughput provisionedThroughput; + private long warmReadUnitsPerSecond; + private long warmWriteUnitsPerSecond; private String tableArn; private Map tags; private List globalSecondaryIndexes; @@ -95,6 +97,8 @@ public class TableDefinition { this.tableArn = AwsArnUtils.Arn.of("dynamodb", region, accountId, "table/" + tableName).toString(); this.tableId = UUID.randomUUID().toString(); this.provisionedThroughput = new ProvisionedThroughput(5, 5); + this.warmReadUnitsPerSecond = 5; + this.warmWriteUnitsPerSecond = 5; this.tags = new HashMap<>(); this.globalSecondaryIndexes = new ArrayList<>(); this.localSecondaryIndexes = new ArrayList<>(); @@ -104,6 +108,24 @@ public class TableDefinition { this.replicaRegions = new ArrayList<>(); } + /** Baseline warm read units for a table given its billing mode and provisioned capacity. */ + public static long defaultWarmReadUnits(String billingMode, ProvisionedThroughput pt) { + if ("PAY_PER_REQUEST".equals(billingMode)) { + return 12000; + } + long rcu = pt != null ? pt.getReadCapacityUnits() : 0; + return rcu > 0 ? rcu : 5; + } + + /** Baseline warm write units for a table given its billing mode and provisioned capacity. */ + public static long defaultWarmWriteUnits(String billingMode, ProvisionedThroughput pt) { + if ("PAY_PER_REQUEST".equals(billingMode)) { + return 4000; + } + long wcu = pt != null ? pt.getWriteCapacityUnits() : 0; + return wcu > 0 ? wcu : 5; + } + public String getTableName() { return tableName; } public void setTableName(String tableName) { this.tableName = tableName; } @@ -128,6 +150,22 @@ public class TableDefinition { public ProvisionedThroughput getProvisionedThroughput() { return provisionedThroughput; } public void setProvisionedThroughput(ProvisionedThroughput provisionedThroughput) { this.provisionedThroughput = provisionedThroughput; } + public long getWarmReadUnitsPerSecond() { + if (warmReadUnitsPerSecond <= 0) { + return defaultWarmReadUnits(billingMode, provisionedThroughput); + } + return warmReadUnitsPerSecond; + } + public void setWarmReadUnitsPerSecond(long v) { this.warmReadUnitsPerSecond = v; } + + public long getWarmWriteUnitsPerSecond() { + if (warmWriteUnitsPerSecond <= 0) { + return defaultWarmWriteUnits(billingMode, provisionedThroughput); + } + return warmWriteUnitsPerSecond; + } + public void setWarmWriteUnitsPerSecond(long v) { this.warmWriteUnitsPerSecond = v; } + public String getTableArn() { return tableArn; } public void setTableArn(String tableArn) { this.tableArn = tableArn; } diff --git a/src/test/java/io/floci/aws/services/dynamodb/NativeDynamoDbJsonHandlerTest.java b/src/test/java/io/floci/aws/services/dynamodb/NativeDynamoDbJsonHandlerTest.java index 676fa8c0ed..746243ed67 100644 --- a/src/test/java/io/floci/aws/services/dynamodb/NativeDynamoDbJsonHandlerTest.java +++ b/src/test/java/io/floci/aws/services/dynamodb/NativeDynamoDbJsonHandlerTest.java @@ -88,7 +88,7 @@ class NativeDynamoDbJsonHandlerTest { return node; } - private JsonNode createRequest(String tableName, JsonNode key, String updateExpression, + private JsonNode createRequest(String tableName, JsonNode key, String updateExpression, JsonNode exprAttrNames, JsonNode exprAttrValues, String returnValues){ ObjectNode node = mapper.createObjectNode(); node.put("TableName", tableName); @@ -116,12 +116,12 @@ class NativeDynamoDbJsonHandlerTest { exprValues.put(":changeVal", attributeValue("S", "val2")); exprValues.put(":newVal", attributeValue("S", "newVal")); - JsonNode request = createRequest("Users", key, + JsonNode request = createRequest("Users", key, "SET changeAttr = :changeVal, newAttr = :newVal, REMOVE delAttr", null, exprValues, "UPDATED_NEW"); Response response = null; - + response = handler.handle("UpdateItem", request, "us-east-1"); assertNotNull(response); @@ -143,7 +143,7 @@ class NativeDynamoDbJsonHandlerTest { assertFalse(attr.has("sameAttr"), "Attributes should not have sameAttr"); } - + @Test void updateItemReturnValuesUpdatedNewOnNewItem() throws Exception { createUsersTable("us-east-1"); @@ -193,12 +193,12 @@ class NativeDynamoDbJsonHandlerTest { exprValues.put(":changeVal", attributeValue("S", "val2")); exprValues.put(":newVal", attributeValue("S", "newVal")); - JsonNode request = createRequest("Users", key, + JsonNode request = createRequest("Users", key, "SET changeAttr = :changeVal, newAttr = :newVal, REMOVE delAttr", null, exprValues, "UPDATED_OLD"); Response response = null; - + response = handler.handle("UpdateItem", request, "us-east-1"); assertNotNull(response); @@ -220,7 +220,7 @@ class NativeDynamoDbJsonHandlerTest { assertFalse(attr.has("sameAttr"), "Attributes should not have sameAttr"); } - + @Test void updateItemReturnValuesAllOld() throws Exception { createUsersTable("us-east-1"); @@ -233,12 +233,12 @@ class NativeDynamoDbJsonHandlerTest { exprValues.put(":changeVal", attributeValue("S", "val2")); exprValues.put(":newVal", attributeValue("S", "newVal")); - JsonNode request = createRequest("Users", key, + JsonNode request = createRequest("Users", key, "SET changeAttr = :changeVal, newAttr = :newVal, REMOVE delAttr", null, exprValues, "ALL_OLD"); Response response = null; - + response = handler.handle("UpdateItem", request, "us-east-1"); assertNotNull(response); @@ -262,7 +262,7 @@ class NativeDynamoDbJsonHandlerTest { assertTrue(attr.get("sameAttr").has("S"), "sameAttr should have S"); assertEquals("static", attr.get("sameAttr").get("S").asText()); } - + @Test void updateItemReturnValuesAllNew() throws Exception { createUsersTable("us-east-1"); @@ -275,12 +275,12 @@ class NativeDynamoDbJsonHandlerTest { exprValues.put(":changeVal", attributeValue("S", "val2")); exprValues.put(":newVal", attributeValue("S", "newVal")); - JsonNode request = createRequest("Users", key, + JsonNode request = createRequest("Users", key, "SET changeAttr = :changeVal, newAttr = :newVal, REMOVE delAttr", null, exprValues, "ALL_NEW"); Response response = null; - + response = handler.handle("UpdateItem", request, "us-east-1"); assertNotNull(response); @@ -304,7 +304,7 @@ class NativeDynamoDbJsonHandlerTest { assertTrue(attr.get("sameAttr").has("S"), "sameAttr should have S"); assertEquals("static", attr.get("sameAttr").get("S").asText()); } - + @Test void updateItemReturnValuesNone() throws Exception { createUsersTable("us-east-1"); @@ -317,12 +317,12 @@ class NativeDynamoDbJsonHandlerTest { exprValues.put(":changeVal", attributeValue("S", "val2")); exprValues.put(":newVal", attributeValue("S", "newVal")); - JsonNode request = createRequest("Users", key, + JsonNode request = createRequest("Users", key, "SET changeAttr = :changeVal, newAttr = :newVal, REMOVE delAttr", null, exprValues, "NONE"); Response response = null; - + response = handler.handle("UpdateItem", request, "us-east-1"); assertNotNull(response); @@ -611,6 +611,121 @@ class NativeDynamoDbJsonHandlerTest { assertEquals("ACTIVE", warmThroughput.get("Status").asText()); } + @Test + void describeTableReportsProvisionedTableCapacityAsWarmThroughput() throws Exception { + service.createTable("table-warm-provisioned", + List.of(new KeySchemaElement("id", "HASH")), + List.of(new AttributeDefinition("id", "S")), + 3L, 7L, List.of(), "eu-west-1"); + + JsonNode warmThroughput = warmThroughputOf(describedTable("table-warm-provisioned")); + + assertNotNull(warmThroughput); + assertEquals(3, warmThroughput.get("ReadUnitsPerSecond").asLong()); + assertEquals(7, warmThroughput.get("WriteUnitsPerSecond").asLong()); + assertEquals("ACTIVE", warmThroughput.get("Status").asText()); + } + + @Test + void describeTableReportsOnDemandTableWarmThroughput() throws Exception { + handler.handle("CreateTable", json(""" + { + "TableName": "table-warm-on-demand", + "KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}], + "AttributeDefinitions": [{"AttributeName": "pk", "AttributeType": "S"}], + "BillingMode": "PAY_PER_REQUEST" + } + """), "eu-west-1"); + + JsonNode warmThroughput = warmThroughputOf(describedTable("table-warm-on-demand")); + + assertNotNull(warmThroughput); + assertEquals(12000, warmThroughput.get("ReadUnitsPerSecond").asLong()); + assertEquals(4000, warmThroughput.get("WriteUnitsPerSecond").asLong()); + assertEquals("ACTIVE", warmThroughput.get("Status").asText()); + } + + @Test + void updateTableWalksWarmThroughputThroughCutRaiseAndOnDemandSwitch() throws Exception { + handler.handle("CreateTable", json(""" + { + "TableName": "table-warm-walk", + "KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}], + "AttributeDefinitions": [{"AttributeName": "pk", "AttributeType": "S"}], + "ProvisionedThroughput": {"ReadCapacityUnits": 10, "WriteCapacityUnits": 20} + } + """), "eu-west-1"); + + // cut: warm stays at the previous peak (10/20) + handler.handle("UpdateTable", json(""" + { + "TableName": "table-warm-walk", + "ProvisionedThroughput": {"ReadCapacityUnits": 5, "WriteCapacityUnits": 8} + } + """), "eu-west-1"); + JsonNode warm = warmThroughputOf(describedTable("table-warm-walk")); + assertEquals(10, warm.get("ReadUnitsPerSecond").asLong()); + assertEquals(20, warm.get("WriteUnitsPerSecond").asLong()); + + // raise: warm follows the new capacity + handler.handle("UpdateTable", json(""" + { + "TableName": "table-warm-walk", + "ProvisionedThroughput": {"ReadCapacityUnits": 15, "WriteCapacityUnits": 25} + } + """), "eu-west-1"); + warm = warmThroughputOf(describedTable("table-warm-walk")); + assertEquals(15, warm.get("ReadUnitsPerSecond").asLong()); + assertEquals(25, warm.get("WriteUnitsPerSecond").asLong()); + + // on-demand switch: warm only rises to the baseline, never drops the peak + handler.handle("UpdateTable", json(""" + { + "TableName": "table-warm-walk", + "BillingMode": "PAY_PER_REQUEST" + } + """), "eu-west-1"); + warm = warmThroughputOf(describedTable("table-warm-walk")); + assertEquals(12000, warm.get("ReadUnitsPerSecond").asLong()); + assertEquals(4000, warm.get("WriteUnitsPerSecond").asLong()); + } + + @Test + void describeTableFallsBackForLegacyZeroWarmThroughput() throws Exception { + service.createTable("table-warm-legacy", + List.of(new KeySchemaElement("id", "HASH")), + List.of(new AttributeDefinition("id", "S")), + 3L, 7L, List.of(), "eu-west-1"); + + // Simulate a persisted table from before warm throughput was tracked. + TableDefinition table = service.describeTable("table-warm-legacy", "eu-west-1"); + table.setWarmReadUnitsPerSecond(0); + table.setWarmWriteUnitsPerSecond(0); + service.persistTable("table-warm-legacy", table, "eu-west-1"); + + JsonNode warmThroughput = warmThroughputOf(describedTable("table-warm-legacy")); + + assertNotNull(warmThroughput); + assertEquals(3, warmThroughput.get("ReadUnitsPerSecond").asLong()); + assertEquals(7, warmThroughput.get("WriteUnitsPerSecond").asLong()); + } + + @Test + void createTableOmitsTableWarmThroughput() throws Exception { + Response response = handler.handle("CreateTable", json(""" + { + "TableName": "table-warm-create", + "KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}], + "AttributeDefinitions": [{"AttributeName": "pk", "AttributeType": "S"}], + "ProvisionedThroughput": {"ReadCapacityUnits": 2, "WriteCapacityUnits": 4} + } + """), "eu-west-1"); + assertEquals(200, response.getStatus()); + JsonNode body = mapper.convertValue(response.getEntity(), JsonNode.class); + assertFalse(body.get("TableDescription").has("WarmThroughput"), + "CreateTable must omit table-level WarmThroughput"); + } + @Test void describeTableReportsOnDemandGsiWarmThroughput() throws Exception { handler.handle("CreateTable", json(""" @@ -674,6 +789,14 @@ class NativeDynamoDbJsonHandlerTest { assertEquals("ACTIVE", describedWarmThroughput.get("Status").asText()); } + private JsonNode describedTable(String tableName) throws Exception { + Response response = handler.handle("DescribeTable", + mapper.createObjectNode().put("TableName", tableName), "eu-west-1"); + assertEquals(200, response.getStatus()); + JsonNode body = mapper.convertValue(response.getEntity(), JsonNode.class); + return body.get("Table"); + } + private JsonNode describedGsi(String tableName, String indexName) throws Exception { Response response = handler.handle("DescribeTable", mapper.createObjectNode().put("TableName", tableName), "eu-west-1");