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 9399301a73..aee955c7a8 100644 --- a/src/main/java/io/floci/aws/services/dynamodb/NativeDynamoDbTableService.java +++ b/src/main/java/io/floci/aws/services/dynamodb/NativeDynamoDbTableService.java @@ -150,7 +150,7 @@ public class NativeDynamoDbTableService { throughput != null ? throughput.writeCapacityUnits() : null, request.globalSecondaryIndexes(), request.localSecondaryIndexes(), request.vectorIndexes(), billingMode, region, - () -> requireCreateWarmThroughput(request, "PAY_PER_REQUEST".equals(billingMode))); + () -> requireCreateWarmThroughput(request, "PAY_PER_REQUEST".equals(billingMode), region)); table.setTableStatus(initialStatus); table.setDeletionProtectionEnabled(Boolean.TRUE.equals(settings.deletionProtectionEnabled())); @@ -675,35 +675,39 @@ public class NativeDynamoDbTableService { table.setStreamViewType(viewType); } - private static void requireCreateWarmThroughput(CreateTableRequest request, boolean onDemand) { + private static void requireCreateWarmThroughput(CreateTableRequest request, boolean onDemand, String region) { Throughput provisioned = request.provisionedThroughput(); WarmThroughput tableWarm = request.warmThroughput(); if (tableWarm != null) { requireCreateWarmThroughput("table", tableWarm.readUnitsPerSecond(), tableWarm.writeUnitsPerSecond(), onDemand, provisioned != null ? provisioned.readCapacityUnits() : null, - provisioned != null ? provisioned.writeCapacityUnits() : null); + provisioned != null ? provisioned.writeCapacityUnits() : null, region); } - requireNewIndexWarmThroughput(request.globalSecondaryIndexes(), onDemand); + requireNewIndexWarmThroughput(request.globalSecondaryIndexes(), onDemand, region); } - private static void requireNewIndexWarmThroughput(List indexes, boolean onDemand) { + private static void requireNewIndexWarmThroughput(List indexes, boolean onDemand, + String region) { for (GlobalSecondaryIndex gsi : indexes) { requireCreateWarmThroughput("index " + gsi.getIndexName(), gsi.getWarmReadUnitsPerSecond(), gsi.getWarmWriteUnitsPerSecond(), onDemand, gsi.getProvisionedThroughput().getReadCapacityUnits(), - gsi.getProvisionedThroughput().getWriteCapacityUnits()); + gsi.getProvisionedThroughput().getWriteCapacityUnits(), region); } } private static void requireCreateWarmThroughput(String target, Long read, Long write, boolean onDemand, - Long readCapacity, Long writeCapacity) { + Long readCapacity, Long writeCapacity, String region) { if (onDemand) { String reason = "initial throughput for OnDemand. See: https://docs.aws.amazon.com/amazondynamodb/" + "latest/developerguide/on-demand-capacity-mode.html#on-demand-capacity-mode-initial"; - requireWarmThroughputAtLeast(target, read, write, TableDefinition.defaultWarmReadUnits("PAY_PER_REQUEST", null), - TableDefinition.defaultWarmWriteUnits("PAY_PER_REQUEST", null), reason, reason); - } else if (readCapacity != null && writeCapacity != null) { - requireWarmThroughputAtLeast(target, read, write, readCapacity, writeCapacity, - "ReadCapacityUnits of ProvisionedThroughput", "WriteCapacityUnits of ProvisionedThroughput"); + requireWarmThroughputInRange(target, read, write, TableDefinition.defaultWarmReadUnits("PAY_PER_REQUEST", null), + TableDefinition.defaultWarmWriteUnits("PAY_PER_REQUEST", null), reason, reason, region); + } else { + requireWarmThroughputInRange(target, read, write, + readCapacity != null && writeCapacity != null ? readCapacity : Long.MIN_VALUE, + readCapacity != null && writeCapacity != null ? writeCapacity : Long.MIN_VALUE, + "ReadCapacityUnits of ProvisionedThroughput", "WriteCapacityUnits of ProvisionedThroughput", + region); } } @@ -721,9 +725,9 @@ public class NativeDynamoDbTableService { // AWS answers an empty table WarmThroughput with InternalFailure. throw new AwsException("InternalFailure", null, 500); } - requireWarmThroughputAtLeast("table", tableWarm.readUnitsPerSecond(), tableWarm.writeUnitsPerSecond(), + requireWarmThroughputInRange("table", tableWarm.readUnitsPerSecond(), tableWarm.writeUnitsPerSecond(), table.getWarmReadUnitsPerSecond(), table.getWarmWriteUnitsPerSecond(), - CURRENT_WARM_REASON, CURRENT_WARM_REASON); + CURRENT_WARM_REASON, CURRENT_WARM_REASON, region); } for (GsiThroughputUpdate update : request.gsiUpdates()) { WarmThroughput warm = update.warmThroughput(); @@ -736,21 +740,33 @@ public class NativeDynamoDbTableService { + "ReadUnitsPerSecond or WriteUnitsPerSecond specified for index: " + update.indexName(), 400); } GlobalSecondaryIndex gsi = table.findGsi(update.indexName()).orElseThrow(); - requireWarmThroughputAtLeast("index " + update.indexName(), warm.readUnitsPerSecond(), + requireWarmThroughputInRange("index " + update.indexName(), warm.readUnitsPerSecond(), warm.writeUnitsPerSecond(), table.gsiWarmReadUnits(gsi), table.gsiWarmWriteUnits(gsi), - CURRENT_WARM_REASON, CURRENT_WARM_REASON); + CURRENT_WARM_REASON, CURRENT_WARM_REASON, region); } - requireNewIndexWarmThroughput(request.gsiCreates(), "PAY_PER_REQUEST".equals(table.getBillingMode())); + requireNewIndexWarmThroughput(request.gsiCreates(), "PAY_PER_REQUEST".equals(table.getBillingMode()), + region); } - private static void requireWarmThroughputAtLeast(String target, Long read, Long write, long minimumRead, - long minimumWrite, String readReason, String writeReason) { + private static void requireWarmThroughputInRange(String target, Long read, Long write, long minimumRead, + long minimumWrite, String readReason, String writeReason, + String region) { if (read != null && read < minimumRead) { throw lowWarmThroughput("ReadUnitsPerSecond", target, readReason); } + requireWarmThroughputWithinQuota("Read", target, read, region); if (write != null && write < minimumWrite) { throw lowWarmThroughput("WriteUnitsPerSecond", target, writeReason); } + requireWarmThroughputWithinQuota("Write", target, write, region); + } + + private static void requireWarmThroughputWithinQuota(String kind, String target, Long units, String region) { + if (units != null && units > DynamoDbCapacityQuotas.TABLE_MAX_CAPACITY_UNITS) { + throw new AwsException("LimitExceededException", "Subscriber limit exceeded: Requested " + kind + + "UnitsPerSecond for WarmThroughput for " + target + " exceeds TableMax" + kind + + "CapacityUnits of the account in region " + region, 400); + } } private static AwsException lowWarmThroughput(String member, String target, String reason) { 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 c9481b27b1..f4042d9b82 100644 --- a/src/test/java/io/floci/aws/services/dynamodb/NativeDynamoDbJsonHandlerTest.java +++ b/src/test/java/io/floci/aws/services/dynamodb/NativeDynamoDbJsonHandlerTest.java @@ -1138,6 +1138,51 @@ class NativeDynamoDbJsonHandlerTest { assertEquals("ResourceInUseException", ex.getErrorCode()); } + @Test + void createTableRejectsWarmThroughputAboveThePerTableQuota() { + AwsException tableEx = warmFailure("CreateTable", + warmTableRequest("warm-quota-create", true, "{\"ReadUnitsPerSecond\": 40001}", null)); + assertEquals("LimitExceededException", tableEx.getErrorCode()); + assertEquals("Subscriber limit exceeded: Requested ReadUnitsPerSecond for WarmThroughput for table exceeds " + + "TableMaxReadCapacityUnits of the account in region eu-west-1", tableEx.getMessage()); + + AwsException indexEx = warmFailure("CreateTable", + warmTableRequest("warm-quota-create", false, null, "{\"WriteUnitsPerSecond\": 40001}")); + assertEquals("Subscriber limit exceeded: Requested WriteUnitsPerSecond for WarmThroughput for index gsi1 " + + "exceeds TableMaxWriteCapacityUnits of the account in region eu-west-1", indexEx.getMessage()); + + AwsException lowFirst = warmFailure("CreateTable", warmTableRequest("warm-quota-create", true, + "{\"ReadUnitsPerSecond\": 1, \"WriteUnitsPerSecond\": 40001}", null)); + assertEquals("ValidationException", lowFirst.getErrorCode()); + assertThrows(AwsException.class, () -> describedTable("warm-quota-create")); + } + + @Test + void updateTableRejectsWarmThroughputAboveThePerTableQuota() throws Exception { + createWarmTable("warm-quota-update", true, null, null); + + AwsException tableEx = warmFailure("UpdateTable", json(""" + {"TableName": "warm-quota-update", "WarmThroughput": {"ReadUnitsPerSecond": 40001, + "WriteUnitsPerSecond": 1}} + """)); + assertEquals("Subscriber limit exceeded: Requested ReadUnitsPerSecond for WarmThroughput for table exceeds " + + "TableMaxReadCapacityUnits of the account in region eu-west-1", tableEx.getMessage()); + + AwsException indexEx = warmFailure("UpdateTable", json(""" + {"TableName": "warm-quota-update", "GlobalSecondaryIndexUpdates": [ + {"Update": {"IndexName": "gsi1", "WarmThroughput": {"WriteUnitsPerSecond": 40001}}}]} + """)); + assertEquals("Subscriber limit exceeded: Requested WriteUnitsPerSecond for WarmThroughput for index gsi1 " + + "exceeds TableMaxWriteCapacityUnits of the account in region eu-west-1", indexEx.getMessage()); + + assertWarm(warmThroughputOf(describedTable("warm-quota-update")), 12000, 4000, "ACTIVE"); + updateWarm(""" + {"TableName": "warm-quota-update", "WarmThroughput": {"ReadUnitsPerSecond": 40000, + "WriteUnitsPerSecond": 40000}} + """); + assertWarm(warmThroughputOf(describedTable("warm-quota-update")), 40000, 40000, "ACTIVE"); + } + @Test void updateTableRejectsEmptyTableWarmThroughputWithInternalFailure() throws Exception { createWarmTable("warm-empty-table", true, null, null);