fix(dynamodb): refuse provisioned capacity above the account and table quotas (#8156)

CreateTable and UpdateTable answer LimitExceededException above the
quotas DescribeLimits reports: 40,000 capacity units for a table or for
one index alone, and 80,000 for the account. The account total adds
every provisioned table and index in the Region.
This commit is contained in:
TATSUNO “Taz” Yasuhiro authored and GitHub committed 2026-11-12 19:04:12 -04:00
1 parent c6361cab91
commit ef785e3269
7 files changed
+475 -12

No files matched your search

@@ -0,0 +1,159 @@
package io.floci.aws.services.dynamodb;
import io.floci.aws.core.common.AwsException;
import io.floci.aws.services.dynamodb.NativeDynamoDbTableService.GsiThroughputUpdate;
import io.floci.aws.services.dynamodb.NativeDynamoDbTableService.Throughput;
import io.floci.aws.services.dynamodb.model.GlobalSecondaryIndex;
import io.floci.aws.services.dynamodb.model.ProvisionedThroughput;
import io.floci.aws.services.dynamodb.model.TableDefinition;
import java.util.List;
/**
* The provisioned capacity quotas DescribeLimits reports, which CreateTable and UpdateTable
* enforce. On-demand tables are outside them.
*/
final class DynamoDbCapacityQuotas {
// The default account quotas, the same in every Region measured.
static final long ACCOUNT_MAX_CAPACITY_UNITS = 80_000;
static final long TABLE_MAX_CAPACITY_UNITS = 40_000;
private static final String READ = "ReadCapacityUnits";
private static final String WRITE = "WriteCapacityUnits";
private static final String SEE_GUIDE =
" Refer to the Amazon DynamoDB Developer Guide for current limits and how to request higher limits.";
private DynamoDbCapacityQuotas() {
}
/** Capacity units summed over a table and its global secondary indexes. */
record Capacity(long read, long write) {
static final Capacity NONE = new Capacity(0, 0);
static Capacity of(TableDefinition table) {
if ("PAY_PER_REQUEST".equals(table.getBillingMode())) {
return NONE;
}
ProvisionedThroughput throughput = table.getProvisionedThroughput();
Capacity total = new Capacity(throughput.getReadCapacityUnits(), throughput.getWriteCapacityUnits());
for (GlobalSecondaryIndex gsi : table.getGlobalSecondaryIndexes()) {
total = total.plus(of(gsi.getProvisionedThroughput()));
}
return total;
}
private static Capacity of(ProvisionedThroughput throughput) {
return new Capacity(throughput.getReadCapacityUnits(), throughput.getWriteCapacityUnits());
}
Capacity plus(Capacity other) {
return new Capacity(read + other.read, write + other.write);
}
}
/**
* Checks a provisioned table before CreateTable stores it.
*
* @param reserved what the account's other tables in the Region hold
*/
static void requireNewTableWithinQuotas(TableDefinition table, Capacity reserved, String region) {
Capacity requested = Capacity.of(table);
requireWithinAccountQuota(READ, reserved.read(), null, requested.read(), region);
requireWithinAccountQuota(WRITE, reserved.write(), null, requested.write(), region);
ProvisionedThroughput throughput = table.getProvisionedThroughput();
requireWithinTableQuota(throughput.getReadCapacityUnits(), throughput.getWriteCapacityUnits(), region);
for (GlobalSecondaryIndex gsi : table.getGlobalSecondaryIndexes()) {
requireWithinIndexQuota(gsi.getIndexName(), gsi.getProvisionedThroughput(), region);
}
}
/**
* Checks the indexes an UpdateTable creates and the table's new total against the account
* quota. The total is checked only when it grows.
*
* @param reserved what the account's tables in the Region hold, this table included
*/
static void requireUpdateWithinQuotas(TableDefinition table, Long read, Long write,
List<GlobalSecondaryIndex> creates, List<String> deletes,
List<GsiThroughputUpdate> updates, Capacity reserved, String region) {
Capacity current = Capacity.of(table);
ProvisionedThroughput tableThroughput = table.getProvisionedThroughput();
Capacity requested = new Capacity(read != null ? read : tableThroughput.getReadCapacityUnits(),
write != null ? write : tableThroughput.getWriteCapacityUnits());
for (GlobalSecondaryIndex gsi : table.getGlobalSecondaryIndexes()) {
if (!deletes.contains(gsi.getIndexName())) {
requested = requested.plus(updated(gsi, updates));
}
}
for (GlobalSecondaryIndex gsi : creates) {
requested = requested.plus(Capacity.of(gsi.getProvisionedThroughput()));
}
if (requested.read() > current.read()) {
requireWithinAccountQuota(READ, reserved.read(), current.read(), requested.read(), region);
}
if (requested.write() > current.write()) {
requireWithinAccountQuota(WRITE, reserved.write(), current.write(), requested.write(), region);
}
for (GlobalSecondaryIndex gsi : creates) {
requireWithinIndexQuota(gsi.getIndexName(), gsi.getProvisionedThroughput(), region);
}
}
private static Capacity updated(GlobalSecondaryIndex gsi, List<GsiThroughputUpdate> updates) {
ProvisionedThroughput throughput = gsi.getProvisionedThroughput();
long read = throughput.getReadCapacityUnits();
long write = throughput.getWriteCapacityUnits();
for (GsiThroughputUpdate update : updates) {
Throughput requested = update.provisionedThroughput();
if (requested != null && gsi.getIndexName().equals(update.indexName())) {
read = requested.readCapacityUnits() != null ? requested.readCapacityUnits() : read;
write = requested.writeCapacityUnits() != null ? requested.writeCapacityUnits() : write;
}
}
return new Capacity(read, write);
}
static void requireWithinTableQuota(Long read, Long write, String region) {
requireWithinPerTableMaximum(READ, "table", read, region);
requireWithinPerTableMaximum(WRITE, "table", write, region);
}
static void requireWithinIndexQuota(String indexName, Long read, Long write, String region) {
requireWithinPerTableMaximum(READ + " for index " + indexName, "index", read, region);
requireWithinPerTableMaximum(WRITE + " for index " + indexName, "index", write, region);
}
private static void requireWithinIndexQuota(String indexName, ProvisionedThroughput throughput, String region) {
requireWithinIndexQuota(indexName, throughput.getReadCapacityUnits(), throughput.getWriteCapacityUnits(),
region);
}
// The message names the per table maximum for an index too.
private static void requireWithinPerTableMaximum(String requested, String scope, Long units, String region) {
if (units != null && units > TABLE_MAX_CAPACITY_UNITS) {
throw new AwsException("LimitExceededException",
"The requested " + requested + ", " + units + ", is above the per " + scope
+ " maximum for the account in " + region + ". Per table maximum: "
+ TABLE_MAX_CAPACITY_UNITS + "." + SEE_GUIDE, 400);
}
}
/** @param current the table's own total on UpdateTable, null on CreateTable */
private static void requireWithinAccountQuota(String unit, long reserved, Long current, long requested,
String region) {
long others = current != null ? reserved - current : reserved;
if (others + requested <= ACCOUNT_MAX_CAPACITY_UNITS) {
return;
}
String currentForTable = current != null
? " Current " + unit + " provisioned for the table and indexes: " + current + "."
: "";
throw new AwsException("LimitExceededException",
"This request would have caused the " + unit + " limit to be exceeded for the account in "
+ region + ". Current " + unit + " reserved by the account: " + reserved + ". Limit: "
+ ACCOUNT_MAX_CAPACITY_UNITS + "." + currentForTable + " Requested: " + requested + "."
+ SEE_GUIDE, 400);
}
}
@@ -24,6 +24,8 @@ import io.floci.aws.core.storage.AccountAwareStorageBackend;
import io.floci.aws.core.storage.InMemoryStorage;
import io.floci.aws.core.storage.StorageBackend;
import io.floci.aws.core.storage.StorageFactory;
import io.floci.aws.services.dynamodb.DynamoDbCapacityQuotas.Capacity;
import io.floci.aws.services.dynamodb.NativeDynamoDbTableService.GsiThroughputUpdate;
import io.floci.aws.services.dynamodb.model.AttributeDefinition;
import io.floci.aws.services.dynamodb.model.ConditionalCheckFailedException;
import io.floci.aws.services.dynamodb.model.ContributorInsights;
@@ -131,6 +133,8 @@ public class DynamoDbService {
// relies on ReentrantLock's re-entrancy so the inner put/update/delete calls do
// not deadlock after the outer transaction already took each participant's lock.
private final ConcurrentHashMap<String, ConcurrentHashMap<String, ReentrantLock>> itemLocks = new ConcurrentHashMap<>();
// Held from an account quota check until the capacity it allowed is stored.
private final Object capacityLock = new Object();
// ClientRequestToken idempotency for TransactWriteItems. AWS retains tokens for
// ~10 minutes; floci uses the same window. The cache entry stores a hash of the
// request body so a replay with the same token but different parameters can be
@@ -685,7 +689,12 @@ public class DynamoDbService {
table.setVectorIndexes(created);
}
tableStore.put(storageKey, table);
synchronized (capacityLock) {
if (!"PAY_PER_REQUEST".equals(billingMode)) {
DynamoDbCapacityQuotas.requireNewTableWithinQuotas(table, reservedCapacity(region), region);
}
tableStore.put(storageKey, table);
}
itemsByTable.put(scopedItemsKey(storageKey), new ConcurrentSkipListMap<>());
LOG.infov("Created table: {0} in region {1}", tableName, region);
return table;
@@ -1041,6 +1050,19 @@ public class DynamoDbService {
}
}
Object capacityLock() {
return capacityLock;
}
private Capacity reservedCapacity(String region) {
String prefix = region + "::";
Capacity reserved = Capacity.NONE;
for (TableDefinition table : tableStore.scan(k -> k.startsWith(prefix))) {
reserved = reserved.plus(Capacity.of(table));
}
return reserved;
}
public List<String> listTables(String region) {
String prefix = region + "::";
return tableStore.scan(k -> k.startsWith(prefix)).stream()
@@ -2506,6 +2528,17 @@ public class DynamoDbService {
List<AttributeDefinition> newAttrDefs,
List<VectorIndexCreate> vectorCreates, List<String> vectorDeletes,
String billingMode, String region) {
return updateTable(tableName, readCapacity, writeCapacity, gsiCreates, gsiDeletes, List.of(),
newAttrDefs, vectorCreates, vectorDeletes, billingMode, region);
}
/** @param gsiUpdates the index throughput changes, which the caller applies after this returns */
public TableDefinition updateTable(String tableName, Long readCapacity, Long writeCapacity,
List<GlobalSecondaryIndex> gsiCreates, List<String> gsiDeletes,
List<GsiThroughputUpdate> gsiUpdates,
List<AttributeDefinition> newAttrDefs,
List<VectorIndexCreate> vectorCreates, List<String> vectorDeletes,
String billingMode, String region) {
String canonicalTableName = canonicalTableName(region, tableName);
String storageKey = regionKey(region, canonicalTableName);
TableDefinition table = tableStore.get(storageKey)
@@ -2521,6 +2554,7 @@ public class DynamoDbService {
throw new AwsException("ValidationException",
"The parameter 'ProvisionedThroughput.WriteCapacityUnits' must be greater than 0", 400);
}
DynamoDbCapacityQuotas.requireWithinTableQuota(readCapacity, writeCapacity, region);
if (readCapacity != null && writeCapacity != null
&& "PROVISIONED".equals(table.getBillingMode())
&& readCapacity.equals(table.getProvisionedThroughput().getReadCapacityUnits())
@@ -2566,9 +2600,13 @@ public class DynamoDbService {
if (newAttrDefs != null) {
knownAttrDefs.addAll(newAttrDefs);
}
String newBillingMode = billingMode != null ? billingMode : table.getBillingMode();
validateVectorIndexUpdates(table, vectorIndexCreates, vectorIndexDeletes,
gsiCreates.size() + gsiDeletes.size(), knownAttrDefs,
billingMode != null ? billingMode : table.getBillingMode());
gsiCreates.size() + gsiDeletes.size(), knownAttrDefs, newBillingMode);
if (!"PAY_PER_REQUEST".equals(newBillingMode)) {
DynamoDbCapacityQuotas.requireUpdateWithinQuotas(table, readCapacity, writeCapacity, gsiCreates,
gsiDeletes, gsiUpdates, reservedCapacity(region), region);
}
if (readCapacity != null) {
table.getProvisionedThroughput().setReadCapacityUnits(readCapacity);
@@ -172,17 +172,14 @@ public class NativeDynamoDbJsonHandler {
};
}
// The default account quotas, the same in every Region measured.
private static final long ACCOUNT_MAX_CAPACITY_UNITS = 80_000;
private static final long TABLE_MAX_CAPACITY_UNITS = 40_000;
private static final int ENDPOINT_CACHE_PERIOD_IN_MINUTES = 1440;
private Response handleDescribeLimits() {
ObjectNode response = objectMapper.createObjectNode();
response.put("AccountMaxReadCapacityUnits", ACCOUNT_MAX_CAPACITY_UNITS);
response.put("AccountMaxWriteCapacityUnits", ACCOUNT_MAX_CAPACITY_UNITS);
response.put("TableMaxReadCapacityUnits", TABLE_MAX_CAPACITY_UNITS);
response.put("TableMaxWriteCapacityUnits", TABLE_MAX_CAPACITY_UNITS);
response.put("AccountMaxReadCapacityUnits", DynamoDbCapacityQuotas.ACCOUNT_MAX_CAPACITY_UNITS);
response.put("AccountMaxWriteCapacityUnits", DynamoDbCapacityQuotas.ACCOUNT_MAX_CAPACITY_UNITS);
response.put("TableMaxReadCapacityUnits", DynamoDbCapacityQuotas.TABLE_MAX_CAPACITY_UNITS);
response.put("TableMaxWriteCapacityUnits", DynamoDbCapacityQuotas.TABLE_MAX_CAPACITY_UNITS);
return Response.ok(response).build();
}
@@ -2053,6 +2050,17 @@ public class NativeDynamoDbJsonHandler {
if (!gsiThroughputUpdates.isEmpty()) {
TableDefinition currentTable = dynamoDbService.describeTable(tableName, region);
if (throughput != null) {
DynamoDbCapacityQuotas.requireWithinTableQuota(
throughput.readCapacityUnits(), throughput.writeCapacityUnits(), region);
}
for (GsiThroughputUpdate gsiUpdate : gsiThroughputUpdates) {
Throughput gsiThroughput = gsiUpdate.provisionedThroughput();
if (gsiThroughput != null) {
DynamoDbCapacityQuotas.requireWithinIndexQuota(gsiUpdate.indexName(),
gsiThroughput.readCapacityUnits(), gsiThroughput.writeCapacityUnits(), region);
}
}
for (GsiThroughputUpdate gsiUpdate : gsiThroughputUpdates) {
String indexName = gsiUpdate.indexName();
if (gsiDeletes.contains(indexName)) {
@@ -404,6 +404,12 @@ public class NativeDynamoDbTableService {
* stay with the caller, which runs them while it parses so their error precedence holds.
*/
TableDefinition updateTable(UpdateTableRequest request, String region) {
synchronized (dynamoDbService.capacityLock()) {
return applyUpdate(request, region);
}
}
private TableDefinition applyUpdate(UpdateTableRequest request, String region) {
String tableName = request.tableName();
TableSettings settings = request.settings();
List<String> addRegions = request.replicaAdds();
@@ -430,7 +436,7 @@ public class NativeDynamoDbTableService {
TableDefinition table = dynamoDbService.updateTable(tableName,
throughput != null ? throughput.readCapacityUnits() : null,
throughput != null ? throughput.writeCapacityUnits() : null,
request.gsiCreates(), request.gsiDeletes(), request.attributeDefinitions(),
request.gsiCreates(), request.gsiDeletes(), request.gsiUpdates(), request.attributeDefinitions(),
request.vectorCreates(), request.vectorDeletes(),
settings.billingMode(), region);