feat(dynamodb): store WarmThroughput from CreateTable and UpdateTable (#9069)

* feat(dynamodb): store WarmThroughput from CreateTable and UpdateTable
This commit is contained in:
TATSUNO “Taz” Yasuhiro authored and GitHub committed 2026-11-19 22:12:08 -04:00
1 parent 1a888867ad
commit d37698ea71
7 files changed
+584 -22

No files matched your search

@@ -869,6 +869,340 @@ class NativeDynamoDbJsonHandlerTest {
assertEquals("ACTIVE", describedWarmThroughput.get("Status").asText());
}
private static final String ON_DEMAND_REASON = "initial throughput for OnDemand. See: https://docs.aws.amazon.com/"
+ "amazondynamodb/latest/developerguide/on-demand-capacity-mode.html#on-demand-capacity-mode-initial";
private static final String CURRENT_REASON = "current WarmThroughput, decreasing WarmThroughput is not supported";
private JsonNode warmTableRequest(String name, boolean onDemand, String tableWarm, String indexWarm) {
String capacity = onDemand ? "\"BillingMode\": \"PAY_PER_REQUEST\","
: "\"ProvisionedThroughput\": {\"ReadCapacityUnits\": 5, \"WriteCapacityUnits\": 6},";
String indexCapacity = onDemand ? ""
: "\"ProvisionedThroughput\": {\"ReadCapacityUnits\": 7, \"WriteCapacityUnits\": 8},";
return json("""
{
"TableName": "%s",
"KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}],
"AttributeDefinitions": [
{"AttributeName": "pk", "AttributeType": "S"},
{"AttributeName": "g", "AttributeType": "S"}
],
%s
%s
"GlobalSecondaryIndexes": [{
"IndexName": "gsi1",
"KeySchema": [{"AttributeName": "g", "KeyType": "HASH"}],
%s
%s
"Projection": {"ProjectionType": "ALL"}
}]
}
""".formatted(name, capacity,
tableWarm == null ? "" : "\"WarmThroughput\": " + tableWarm + ",",
indexCapacity, indexWarm == null ? "" : "\"WarmThroughput\": " + indexWarm + ","));
}
private JsonNode createWarmTable(String name, boolean onDemand, String tableWarm, String indexWarm)
throws Exception {
Response response = handler.handle("CreateTable", warmTableRequest(name, onDemand, tableWarm, indexWarm),
"eu-west-1");
assertEquals(200, response.getStatus());
return mapper.convertValue(response.getEntity(), JsonNode.class).get("TableDescription");
}
private AwsException warmFailure(String operation, JsonNode request) {
return assertThrows(AwsException.class, () -> handler.handle(operation, request, "eu-west-1"));
}
private JsonNode updateWarm(String body) throws Exception {
Response response = handler.handle("UpdateTable", json(body), "eu-west-1");
assertEquals(200, response.getStatus());
return mapper.convertValue(response.getEntity(), JsonNode.class).get("TableDescription");
}
private static void assertWarm(JsonNode warm, long read, long write, String status) {
assertEquals(read, warm.get("ReadUnitsPerSecond").asLong());
assertEquals(write, warm.get("WriteUnitsPerSecond").asLong());
assertEquals(status, warm.get("Status").asText());
}
@Test
void createTableEchoesRequestedWarmThroughputAsUpdating() throws Exception {
JsonNode description = createWarmTable("warm-create-echo", true,
"{\"ReadUnitsPerSecond\": 15000, \"WriteUnitsPerSecond\": 5000}",
"{\"ReadUnitsPerSecond\": 13000}");
assertWarm(description.get("WarmThroughput"), 15000, 5000, "UPDATING");
JsonNode indexWarm = description.get("GlobalSecondaryIndexes").get(0).get("WarmThroughput");
assertEquals(2, indexWarm.size());
assertEquals(13000, indexWarm.get("ReadUnitsPerSecond").asLong());
assertFalse(indexWarm.has("WriteUnitsPerSecond"));
assertEquals("UPDATING", indexWarm.get("Status").asText());
assertWarm(warmThroughputOf(describedTable("warm-create-echo")), 15000, 5000, "ACTIVE");
assertWarm(warmThroughputOf(describedGsi("warm-create-echo", "gsi1")), 13000, 4000, "ACTIVE");
}
@Test
void createTableWithOneWarmMemberKeepsTheOtherAtItsBaseline() throws Exception {
JsonNode description = createWarmTable("warm-create-one", true, "{\"ReadUnitsPerSecond\": 12000}", null);
JsonNode warm = description.get("WarmThroughput");
assertEquals(2, warm.size());
assertEquals("UPDATING", warm.get("Status").asText());
assertWarm(warmThroughputOf(describedTable("warm-create-one")), 12000, 4000, "ACTIVE");
}
@Test
void createTableWithoutWarmThroughputOmitsTableAndIndexWarmThroughput() throws Exception {
JsonNode description = createWarmTable("warm-create-none", false, null, null);
assertFalse(description.has("WarmThroughput"));
assertFalse(description.get("GlobalSecondaryIndexes").get(0).has("WarmThroughput"));
}
@Test
void createTableIgnoresEmptyWarmThroughput() throws Exception {
JsonNode description = createWarmTable("warm-create-empty", false, "{}", "{}");
assertFalse(description.has("WarmThroughput"));
assertFalse(description.get("GlobalSecondaryIndexes").get(0).has("WarmThroughput"));
assertWarm(warmThroughputOf(describedTable("warm-create-empty")), 5, 6, "ACTIVE");
}
@Test
void createTableRejectsWarmThroughputBelowOnDemandInitialThroughput() {
AwsException readEx = warmFailure("CreateTable", warmTableRequest("warm-low-od", true,
"{\"ReadUnitsPerSecond\": 11999, \"WriteUnitsPerSecond\": 1}", null));
assertEquals("ValidationException", readEx.getErrorCode());
assertEquals(400, readEx.getHttpStatus());
assertEquals("One or more parameter values were invalid: Requested ReadUnitsPerSecond for WarmThroughput "
+ "for table is lower than " + ON_DEMAND_REASON, readEx.getMessage());
AwsException writeEx = warmFailure("CreateTable", warmTableRequest("warm-low-od", true,
"{\"WriteUnitsPerSecond\": 0}", null));
assertEquals("One or more parameter values were invalid: Requested WriteUnitsPerSecond for WarmThroughput "
+ "for table is lower than " + ON_DEMAND_REASON, writeEx.getMessage());
assertThrows(AwsException.class, () -> describedTable("warm-low-od"));
}
@Test
void createTableRejectsIndexWarmThroughputBelowOnDemandInitialThroughput() {
AwsException ex = warmFailure("CreateTable", warmTableRequest("warm-low-od-index", true, null,
"{\"WriteUnitsPerSecond\": 3999}"));
assertEquals("One or more parameter values were invalid: Requested WriteUnitsPerSecond for WarmThroughput "
+ "for index gsi1 is lower than " + ON_DEMAND_REASON, ex.getMessage());
assertThrows(AwsException.class, () -> describedTable("warm-low-od-index"));
}
@Test
void createTableRejectsWarmThroughputBelowProvisionedCapacity() {
AwsException readEx = warmFailure("CreateTable", warmTableRequest("warm-low-prov", false,
"{\"ReadUnitsPerSecond\": 4, \"WriteUnitsPerSecond\": 1}", null));
assertEquals("One or more parameter values were invalid: Requested ReadUnitsPerSecond for WarmThroughput "
+ "for table is lower than ReadCapacityUnits of ProvisionedThroughput", readEx.getMessage());
AwsException writeEx = warmFailure("CreateTable", warmTableRequest("warm-low-prov", false,
"{\"ReadUnitsPerSecond\": 5, \"WriteUnitsPerSecond\": 5}", null));
assertEquals("One or more parameter values were invalid: Requested WriteUnitsPerSecond for WarmThroughput "
+ "for table is lower than WriteCapacityUnits of ProvisionedThroughput", writeEx.getMessage());
AwsException indexEx = warmFailure("CreateTable", warmTableRequest("warm-low-prov", false, null,
"{\"ReadUnitsPerSecond\": 6}"));
assertEquals("One or more parameter values were invalid: Requested ReadUnitsPerSecond for WarmThroughput "
+ "for index gsi1 is lower than ReadCapacityUnits of ProvisionedThroughput", indexEx.getMessage());
assertThrows(AwsException.class, () -> describedTable("warm-low-prov"));
}
@Test
void createTableChecksTheTableBeforeItsIndexes() {
AwsException ex = warmFailure("CreateTable", warmTableRequest("warm-order-create", true,
"{\"WriteUnitsPerSecond\": 1}", "{\"ReadUnitsPerSecond\": 1}"));
assertTrue(ex.getMessage().contains("WriteUnitsPerSecond for WarmThroughput for table"));
}
@Test
void updateTableRaisesTableWarmThroughputOnOnDemandAndProvisionedTables() throws Exception {
createWarmTable("warm-raise-od", true, null, null);
createWarmTable("warm-raise-prov", false, null, null);
JsonNode onDemand = updateWarm("""
{"TableName": "warm-raise-od", "WarmThroughput": {"ReadUnitsPerSecond": 20000}}
""");
JsonNode provisioned = updateWarm("""
{"TableName": "warm-raise-prov", "WarmThroughput": {"WriteUnitsPerSecond": 50}}
""");
assertWarm(onDemand.get("WarmThroughput"), 20000, 4000, "UPDATING");
assertWarm(provisioned.get("WarmThroughput"), 5, 50, "UPDATING");
assertWarm(warmThroughputOf(describedTable("warm-raise-od")), 20000, 4000, "ACTIVE");
assertWarm(warmThroughputOf(describedTable("warm-raise-prov")), 5, 50, "ACTIVE");
}
@Test
void updateTableMarksOnlyRequestedTargetsAsUpdating() throws Exception {
createWarmTable("warm-flags", true, null, null);
JsonNode indexOnly = updateWarm("""
{"TableName": "warm-flags", "GlobalSecondaryIndexUpdates": [
{"Update": {"IndexName": "gsi1", "WarmThroughput": {"ReadUnitsPerSecond": 13000}}}]}
""");
assertWarm(indexOnly.get("WarmThroughput"), 12000, 4000, "ACTIVE");
assertWarm(indexOnly.get("GlobalSecondaryIndexes").get(0).get("WarmThroughput"), 13000, 4000, "UPDATING");
assertWarm(warmThroughputOf(describedGsi("warm-flags", "gsi1")), 13000, 4000, "ACTIVE");
JsonNode tableOnly = updateWarm("""
{"TableName": "warm-flags", "WarmThroughput": {"WriteUnitsPerSecond": 4001}}
""");
assertWarm(tableOnly.get("WarmThroughput"), 12000, 4001, "UPDATING");
assertWarm(tableOnly.get("GlobalSecondaryIndexes").get(0).get("WarmThroughput"), 13000, 4000, "ACTIVE");
}
@Test
void updateTableAcceptsWarmThroughputEqualToTheCurrentValue() throws Exception {
createWarmTable("warm-equal", true, null, null);
JsonNode description = updateWarm("""
{"TableName": "warm-equal", "WarmThroughput": {"ReadUnitsPerSecond": 12000, "WriteUnitsPerSecond": 4000}}
""");
assertWarm(description.get("WarmThroughput"), 12000, 4000, "UPDATING");
}
@Test
void updateTableRejectsLoweringTableWarmThroughput() throws Exception {
createWarmTable("warm-lower", true, "{\"ReadUnitsPerSecond\": 15000, \"WriteUnitsPerSecond\": 6000}", null);
AwsException readEx = warmFailure("UpdateTable", json("""
{"TableName": "warm-lower", "WarmThroughput": {"ReadUnitsPerSecond": 14999}}
"""));
assertEquals("ValidationException", readEx.getErrorCode());
assertEquals("One or more parameter values were invalid: Requested ReadUnitsPerSecond for WarmThroughput "
+ "for table is lower than " + CURRENT_REASON, readEx.getMessage());
AwsException writeEx = warmFailure("UpdateTable", json("""
{"TableName": "warm-lower", "WarmThroughput": {"WriteUnitsPerSecond": 5999}}
"""));
assertEquals("One or more parameter values were invalid: Requested WriteUnitsPerSecond for WarmThroughput "
+ "for table is lower than " + CURRENT_REASON, writeEx.getMessage());
assertWarm(warmThroughputOf(describedTable("warm-lower")), 15000, 6000, "ACTIVE");
}
@Test
void updateTableRejectsLoweringIndexWarmThroughputAndChecksTableFirst() throws Exception {
createWarmTable("warm-lower-index", true, null, "{\"ReadUnitsPerSecond\": 14000}");
AwsException indexEx = warmFailure("UpdateTable", json("""
{"TableName": "warm-lower-index", "GlobalSecondaryIndexUpdates": [
{"Update": {"IndexName": "gsi1", "WarmThroughput": {"ReadUnitsPerSecond": 13999}}}]}
"""));
assertEquals("One or more parameter values were invalid: Requested ReadUnitsPerSecond for WarmThroughput "
+ "for index gsi1 is lower than " + CURRENT_REASON, indexEx.getMessage());
AwsException orderEx = warmFailure("UpdateTable", json("""
{"TableName": "warm-lower-index", "WarmThroughput": {"ReadUnitsPerSecond": 1},
"GlobalSecondaryIndexUpdates": [
{"Update": {"IndexName": "gsi1", "WarmThroughput": {"ReadUnitsPerSecond": 1}}}]}
"""));
assertTrue(orderEx.getMessage().contains("for table"));
}
@Test
void updateTableRejectsEmptyTableWarmThroughputWithInternalFailure() throws Exception {
createWarmTable("warm-empty-table", true, null, null);
AwsException ex = warmFailure("UpdateTable", json("""
{"TableName": "warm-empty-table", "WarmThroughput": {}}
"""));
assertEquals("InternalFailure", ex.getErrorCode());
assertEquals(500, ex.getHttpStatus());
}
@Test
void updateTableReportsAMissingTableBeforeAnyWarmThroughputCheck() {
AwsException ex = warmFailure("UpdateTable", json("""
{"TableName": "warm-no-such-table", "WarmThroughput": {}}
"""));
assertEquals("ResourceNotFoundException", ex.getErrorCode());
}
@Test
void updateTableRejectsEmptyIndexWarmThroughput() throws Exception {
createWarmTable("warm-empty-index", true, null, null);
AwsException ex = warmFailure("UpdateTable", json("""
{"TableName": "warm-empty-index", "GlobalSecondaryIndexUpdates": [
{"Update": {"IndexName": "gsi1", "WarmThroughput": {}}}]}
"""));
assertEquals("ValidationException", ex.getErrorCode());
assertEquals("One or more parameter values were invalid: WarmThroughput must have at least one of "
+ "ReadUnitsPerSecond or WriteUnitsPerSecond specified for index: gsi1", ex.getMessage());
}
@Test
void updateTableRejectsWarmOnlyUpdateOfAMissingIndexWithInternalFailure() throws Exception {
createWarmTable("warm-missing-index", true, null, null);
AwsException ex = warmFailure("UpdateTable", json("""
{"TableName": "warm-missing-index", "GlobalSecondaryIndexUpdates": [
{"Update": {"IndexName": "nope", "WarmThroughput": {"ReadUnitsPerSecond": 13000}}}]}
"""));
assertEquals("InternalFailure", ex.getErrorCode());
assertEquals(500, ex.getHttpStatus());
AwsException other = warmFailure("UpdateTable", json("""
{"TableName": "warm-missing-index", "GlobalSecondaryIndexUpdates": [
{"Update": {"IndexName": "nope", "OnDemandThroughput": {"MaxReadRequestUnits": 10}}}]}
"""));
assertEquals("ResourceNotFoundException", other.getErrorCode());
}
@Test
void updateTableCreatedIndexFollowsTheCreateTableWarmThroughputRules() throws Exception {
createWarmTable("warm-index-create", true, null, null);
String create = """
{"TableName": "warm-index-create",
"AttributeDefinitions": [{"AttributeName": "h", "AttributeType": "S"}],
"GlobalSecondaryIndexUpdates": [{"Create": {
"IndexName": "gsi2",
"KeySchema": [{"AttributeName": "h", "KeyType": "HASH"}],
"Projection": {"ProjectionType": "ALL"},
"WarmThroughput": %s}}]}
""";
AwsException ex = warmFailure("UpdateTable", json(create.formatted("{\"ReadUnitsPerSecond\": 100}")));
assertEquals("One or more parameter values were invalid: Requested ReadUnitsPerSecond for WarmThroughput "
+ "for index gsi2 is lower than " + ON_DEMAND_REASON, ex.getMessage());
JsonNode description = updateWarm(create.formatted("{\"ReadUnitsPerSecond\": 12500}"));
JsonNode created = description.get("GlobalSecondaryIndexes").get(1).get("WarmThroughput");
assertWarm(created, 12500, 4000, "UPDATING");
assertWarm(description.get("GlobalSecondaryIndexes").get(0).get("WarmThroughput"), 12000, 4000, "ACTIVE");
assertWarm(warmThroughputOf(describedGsi("warm-index-create", "gsi2")), 12500, 4000, "ACTIVE");
}
@Test
void updateTableIgnoresEmptyWarmThroughputOnACreatedIndex() throws Exception {
createWarmTable("warm-index-create-empty", true, null, null);
JsonNode description = updateWarm("""
{"TableName": "warm-index-create-empty",
"AttributeDefinitions": [{"AttributeName": "h", "AttributeType": "S"}],
"GlobalSecondaryIndexUpdates": [{"Create": {
"IndexName": "gsi2",
"KeySchema": [{"AttributeName": "h", "KeyType": "HASH"}],
"Projection": {"ProjectionType": "ALL"},
"WarmThroughput": {}}}]}
""");
assertWarm(description.get("GlobalSecondaryIndexes").get(1).get("WarmThroughput"), 12000, 4000, "ACTIVE");
}
private JsonNode describedTable(String tableName) throws Exception {
Response response = handler.handle("DescribeTable",
mapper.createObjectNode().put("TableName", tableName), "eu-west-1");
@@ -106,7 +106,7 @@ class NativeDynamoDbTableServiceTest {
return new CreateTableRequest(TABLE,
new ArrayList<>(List.of(new KeySchemaElement("id", "HASH"))),
new ArrayList<>(List.of(new AttributeDefinition("id", "S"))),
throughput, List.of(), List.of(), List.of(), Map.of(), settings);
throughput, List.of(), List.of(), List.of(), Map.of(), settings, null);
}
private static CreateTableRequest createRequestWithGsi(TableSettings settings) {
@@ -117,13 +117,13 @@ class NativeDynamoDbTableServiceTest {
return new CreateTableRequest(TABLE,
new ArrayList<>(List.of(new KeySchemaElement("id", "HASH"))),
new ArrayList<>(List.of(new AttributeDefinition("id", "S"), new AttributeDefinition("category", "S"))),
new Throughput(5L, 5L), new ArrayList<>(List.of(gsi)), List.of(), List.of(), Map.of(), settings);
new Throughput(5L, 5L), new ArrayList<>(List.of(gsi)), List.of(), List.of(), Map.of(), settings, null);
}
private static UpdateTableRequest updateRequest(List<GsiThroughputUpdate> gsiUpdates,
List<String> replicaUpdates, TableSettings settings) {
return new UpdateTableRequest(TABLE, null, List.of(), List.of(), List.of(), gsiUpdates,
List.of(), List.of(), List.of(), List.of(), replicaUpdates, settings);
List.of(), List.of(), List.of(), List.of(), replicaUpdates, settings, null);
}
private void assertValidationFailure(Executable call, String message) {
@@ -190,7 +190,7 @@ class NativeDynamoDbTableServiceTest {
CreateTableRequest request = new CreateTableRequest(TABLE,
new ArrayList<>(List.of(new KeySchemaElement("id", "HASH"))),
new ArrayList<>(List.of(new AttributeDefinition("id", "S"))),
null, List.of(), List.of(), List.of(), tags, settings);
null, List.of(), List.of(), List.of(), tags, settings, null);
TableDefinition created = tableService.createTable(request, "ACTIVE", REGION);
@@ -253,7 +253,7 @@ class NativeDynamoDbTableServiceTest {
TableSettings update = new TableSettings("PAY_PER_REQUEST", true, "STANDARD_INFREQUENT_ACCESS",
new OnDemandThroughput(100, 200), true, "NEW_IMAGE", false, null, null);
List<GsiThroughputUpdate> gsiUpdates = List.of(
new GsiThroughputUpdate(GSI, new Throughput(7L, null), new OnDemandThroughput(30, null)));
new GsiThroughputUpdate(GSI, new Throughput(7L, null), new OnDemandThroughput(30, null), null));
tableService.updateTable(updateRequest(gsiUpdates, List.of(), update), REGION);