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

@@ -18,6 +18,7 @@ import io.floci.aws.services.dynamodb.NativeDynamoDbTableService.RestoreOverride
import io.floci.aws.services.dynamodb.NativeDynamoDbTableService.TableSettings;
import io.floci.aws.services.dynamodb.NativeDynamoDbTableService.Throughput;
import io.floci.aws.services.dynamodb.NativeDynamoDbTableService.UpdateTableRequest;
import io.floci.aws.services.dynamodb.NativeDynamoDbTableService.WarmThroughput;
import io.floci.aws.services.dynamodb.model.AttributeDefinition;
import io.floci.aws.services.dynamodb.model.ConditionalCheckFailedException;
import io.floci.aws.services.dynamodb.model.ContributorInsights;
@@ -83,9 +84,6 @@ public class NativeDynamoDbJsonHandler {
List.of("OLD_IMAGE", "KEYS_ONLY", "NEW_AND_OLD_IMAGES", "NEW_IMAGE");
private static final List<String> ATTRIBUTE_TYPES = List.of("B", "N", "S");
private static final List<String> PROJECTION_TYPES = List.of("ALL", "INCLUDE", "KEYS_ONLY");
// The warm throughput a new on-demand table or index starts with.
private static final long ON_DEMAND_WARM_READ_UNITS = 12000;
private static final long ON_DEMAND_WARM_WRITE_UNITS = 4000;
private final DynamoDbService dynamoDbService;
private final NativeDynamoDbTableService tableService;
@@ -199,7 +197,14 @@ public class NativeDynamoDbJsonHandler {
TableDefinition table = createTableFromSpec(request, "", region, "ACTIVE");
ObjectNode tableNode = tableToNode(table);
tableNode.remove("WarmThroughput");
reportRequestedWarmThroughput(tableNode, request);
JsonNode requestedGsis = request.path("GlobalSecondaryIndexes");
if (tableNode.has("GlobalSecondaryIndexes")) {
int position = 0;
for (JsonNode gsiNode : tableNode.get("GlobalSecondaryIndexes")) {
reportRequestedWarmThroughput((ObjectNode) gsiNode, requestedGsis.path(position++));
}
}
ObjectNode response = objectMapper.createObjectNode();
response.set("TableDescription", tableNode);
return Response.ok(response).build();
@@ -271,6 +276,9 @@ public class NativeDynamoDbJsonHandler {
gsi.setOnDemandMaxWriteRequestUnits(gsiOnDemand.get("MaxWriteRequestUnits").asInt());
}
}
if (memberPrefix.isEmpty()) {
setWarmThroughput(gsi, parseWarmThroughput(gsiNode.path("WarmThroughput")));
}
gsis.add(gsi);
}
}
@@ -321,7 +329,9 @@ public class NativeDynamoDbJsonHandler {
}
return tableService.createTable(new CreateTableRequest(tableName, keySchema, attrDefs,
throughput, gsis, lsis, vectorIndexes, tags, parseTableSettings(request)), initialStatus, region);
throughput, gsis, lsis, vectorIndexes, tags, parseTableSettings(request),
memberPrefix.isEmpty() ? parseWarmThroughput(request.path("WarmThroughput")) : null),
initialStatus, region);
}
/**
@@ -2020,6 +2030,7 @@ public class NativeDynamoDbJsonHandler {
newGsi.setOnDemandMaxWriteRequestUnits(newGsiOnDemand.get("MaxWriteRequestUnits").asInt());
}
}
setWarmThroughput(newGsi, parseWarmThroughput(createNode.path("WarmThroughput")));
gsiCreates.add(newGsi);
}
JsonNode deleteNode = update.path("Delete");
@@ -2034,7 +2045,8 @@ public class NativeDynamoDbJsonHandler {
requireIndexName(updateNode,
"globalSecondaryIndexUpdates." + updatePosition + ".member.update"),
gsiPt.isObject() ? parseThroughput(gsiPt) : null,
parseOnDemandThroughput(updateNode.path("OnDemandThroughput"))));
parseOnDemandThroughput(updateNode.path("OnDemandThroughput")),
parseWarmThroughput(updateNode.path("WarmThroughput"))));
}
}
}
@@ -2065,6 +2077,11 @@ public class NativeDynamoDbJsonHandler {
"Cannot delete and update the same index: " + indexName, 400);
}
if (currentTable.findGsi(indexName).isEmpty()) {
if (gsiUpdate.warmThroughput() != null && gsiUpdate.provisionedThroughput() == null
&& gsiUpdate.onDemandThroughput() == null) {
// AWS answers a warm-only update of a missing index with InternalFailure.
throw new AwsException("InternalFailure", null, 500);
}
throw DynamoDbService.missingGsi(indexName, currentTable.getTableName());
}
}
@@ -2104,10 +2121,13 @@ public class NativeDynamoDbJsonHandler {
TableDefinition table = tableService.updateTable(new UpdateTableRequest(tableName,
throughput, newAttrDefs, gsiCreates, gsiDeletes, gsiThroughputUpdates, vectorCreates, vectorDeletes,
addRegions, removeRegions, updateRegions, parseTableSettings(request)), region);
addRegions, removeRegions, updateRegions, parseTableSettings(request),
parseWarmThroughput(request.path("WarmThroughput"))), region);
ObjectNode tableNode = tableToNode(table);
markWarmThroughputUpdating(tableNode, request);
ObjectNode response = objectMapper.createObjectNode();
response.set("TableDescription", tableToNode(table));
response.set("TableDescription", tableNode);
return Response.ok(response).build();
}
@@ -4085,17 +4105,72 @@ public class NativeDynamoDbJsonHandler {
return node;
}
/**
* CreateTable describes a table or index's warm throughput only when the request gave one, and
* then only the members it gave, still UPDATING.
*/
private void reportRequestedWarmThroughput(ObjectNode described, JsonNode requested) {
described.remove("WarmThroughput");
WarmThroughput warm = parseWarmThroughput(requested.path("WarmThroughput"));
if (warm == null || warm.isEmpty()) {
return;
}
ObjectNode reported = described.putObject("WarmThroughput");
if (warm.readUnitsPerSecond() != null) {
reported.put("ReadUnitsPerSecond", warm.readUnitsPerSecond());
}
if (warm.writeUnitsPerSecond() != null) {
reported.put("WriteUnitsPerSecond", warm.writeUnitsPerSecond());
}
reported.put("Status", "UPDATING");
}
/** UpdateTable reports UPDATING only for the table and indexes whose request carried a WarmThroughput. */
private static void markWarmThroughputUpdating(ObjectNode described, JsonNode request) {
if (request.path("WarmThroughput").isObject()) {
((ObjectNode) described.get("WarmThroughput")).put("Status", "UPDATING");
}
Set<String> updatedIndexes = new HashSet<>();
for (JsonNode indexUpdate : request.path("GlobalSecondaryIndexUpdates")) {
for (String action : List.of("Create", "Update")) {
JsonNode node = indexUpdate.path(action);
WarmThroughput warm = parseWarmThroughput(node.path("WarmThroughput"));
if (warm != null && !warm.isEmpty()) {
updatedIndexes.add(node.path("IndexName").asText());
}
}
}
for (JsonNode gsiNode : described.path("GlobalSecondaryIndexes")) {
if (updatedIndexes.contains(gsiNode.path("IndexName").asText())) {
((ObjectNode) gsiNode.get("WarmThroughput")).put("Status", "UPDATING");
}
}
}
private static WarmThroughput parseWarmThroughput(JsonNode warm) {
if (!warm.isObject()) {
return null;
}
return new WarmThroughput(
warm.hasNonNull("ReadUnitsPerSecond") ? warm.get("ReadUnitsPerSecond").asLong() : null,
warm.hasNonNull("WriteUnitsPerSecond") ? warm.get("WriteUnitsPerSecond").asLong() : null);
}
private static void setWarmThroughput(GlobalSecondaryIndex gsi, WarmThroughput warm) {
if (warm != null) {
gsi.setWarmReadUnitsPerSecond(warm.readUnitsPerSecond());
gsi.setWarmWriteUnitsPerSecond(warm.writeUnitsPerSecond());
}
}
/**
* AWS reports a provisioned index's warm throughput as its provisioned capacity, and an on-demand
* one's as the values a new on-demand index starts with. An index is ACTIVE as soon as it exists here.
*/
private ObjectNode gsiWarmThroughput(TableDefinition table, GlobalSecondaryIndex gsi) {
boolean onDemand = "PAY_PER_REQUEST".equals(table.getBillingMode());
ObjectNode warmThroughput = objectMapper.createObjectNode();
warmThroughput.put("ReadUnitsPerSecond",
onDemand ? ON_DEMAND_WARM_READ_UNITS : gsi.getProvisionedThroughput().getReadCapacityUnits());
warmThroughput.put("WriteUnitsPerSecond",
onDemand ? ON_DEMAND_WARM_WRITE_UNITS : gsi.getProvisionedThroughput().getWriteCapacityUnits());
warmThroughput.put("ReadUnitsPerSecond", table.gsiWarmReadUnits(gsi));
warmThroughput.put("WriteUnitsPerSecond", table.gsiWarmWriteUnits(gsi));
warmThroughput.put("Status", "ACTIVE");
return warmThroughput;
}
@@ -40,6 +40,8 @@ public class NativeDynamoDbTableService {
private static final String SSE_TYPE_KMS = "KMS";
// The order AWS prints the enum constraint in.
static final List<String> SSE_TYPES = List.of("AES256", "KMS");
private static final String CURRENT_WARM_REASON =
"current WarmThroughput, decreasing WarmThroughput is not supported";
// A null field or throughput record means "not specified in the request". Collections and the
// settings record must not be null.
@@ -47,8 +49,15 @@ public class NativeDynamoDbTableService {
record OnDemandThroughput(Integer maxReadRequestUnits, Integer maxWriteRequestUnits) {}
// Both members null is a request that sent WarmThroughput as an empty object.
record WarmThroughput(Long readUnitsPerSecond, Long writeUnitsPerSecond) {
boolean isEmpty() {
return readUnitsPerSecond == null && writeUnitsPerSecond == null;
}
}
record GsiThroughputUpdate(String indexName, Throughput provisionedThroughput,
OnDemandThroughput onDemandThroughput) {}
OnDemandThroughput onDemandThroughput, WarmThroughput warmThroughput) {}
record TableSettings(String billingMode, Boolean deletionProtectionEnabled, String tableClass,
OnDemandThroughput onDemandThroughput,
@@ -60,7 +69,7 @@ public class NativeDynamoDbTableService {
List<GlobalSecondaryIndex> globalSecondaryIndexes,
List<LocalSecondaryIndex> localSecondaryIndexes,
List<DynamoDbService.VectorIndexCreate> vectorIndexes,
Map<String, String> tags, TableSettings settings) {}
Map<String, String> tags, TableSettings settings, WarmThroughput warmThroughput) {}
record UpdateTableRequest(String tableName, Throughput provisionedThroughput,
List<AttributeDefinition> attributeDefinitions,
@@ -68,7 +77,7 @@ public class NativeDynamoDbTableService {
List<GsiThroughputUpdate> gsiUpdates,
List<DynamoDbService.VectorIndexCreate> vectorCreates, List<String> vectorDeletes,
List<String> replicaAdds, List<String> replicaRemoves, List<String> replicaUpdates,
TableSettings settings) {}
TableSettings settings, WarmThroughput warmThroughput) {}
/**
* What one Enable or Disable call applied. The values are the ones the call itself wrote, not a
@@ -135,6 +144,8 @@ public class NativeDynamoDbTableService {
+ "disabled, UpdateViewType should not be specified", 400);
}
requireCreateWarmThroughput(request, "PAY_PER_REQUEST".equals(billingMode));
TableDefinition table = dynamoDbService.createTable(tableName, request.keySchema(),
request.attributeDefinitions(),
throughput != null ? throughput.readCapacityUnits() : null,
@@ -155,6 +166,8 @@ public class NativeDynamoDbTableService {
applyOnDemandThroughput(table, settings.onDemandThroughput());
applyWarmThroughput(table, request.warmThroughput());
dynamoDbService.putCreationTags(table, request.tags());
if (Boolean.TRUE.equals(settings.streamEnabled())) {
@@ -221,7 +234,7 @@ public class NativeDynamoDbTableService {
sseEnabled, sseType, kmsMasterKeyId);
dynamoDbService.requireRestoreTarget(targetTableName, region);
return createTable(new CreateTableRequest(targetTableName, new ArrayList<>(source.getKeySchema()),
new ArrayList<>(attributeDefinitions), throughput, gsis, lsis, vectorIndexes, Map.of(), settings),
new ArrayList<>(attributeDefinitions), throughput, gsis, lsis, vectorIndexes, Map.of(), settings, null),
"CREATING", region);
}
@@ -432,6 +445,8 @@ public class NativeDynamoDbTableService {
validateSseType(sseType);
}
requireUpdateWarmThroughput(request, region);
Throughput throughput = request.provisionedThroughput();
TableDefinition table = dynamoDbService.updateTable(tableName,
throughput != null ? throughput.readCapacityUnits() : null,
@@ -451,6 +466,7 @@ public class NativeDynamoDbTableService {
gsi.getProvisionedThroughput().setWriteCapacityUnits(gsiThroughput.writeCapacityUnits());
}
}
applyWarmThroughput(gsi, update.warmThroughput());
OnDemandThroughput gsiOnDemand = update.onDemandThroughput();
if (gsiOnDemand != null) {
if (gsiOnDemand.maxReadRequestUnits() != null) {
@@ -473,6 +489,8 @@ public class NativeDynamoDbTableService {
applyOnDemandThroughput(table, settings.onDemandThroughput());
applyWarmThroughput(table, request.warmThroughput());
if (settings.streamEnabled() != null) {
if (settings.streamEnabled()) {
enableStream(table, settings.streamViewType(), region);
@@ -633,6 +651,114 @@ public class NativeDynamoDbTableService {
table.setStreamViewType(viewType);
}
private static void requireCreateWarmThroughput(CreateTableRequest request, boolean onDemand) {
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);
}
for (GlobalSecondaryIndex gsi : request.globalSecondaryIndexes()) {
requireCreateWarmThroughput("index " + gsi.getIndexName(), gsi.getWarmReadUnitsPerSecond(),
gsi.getWarmWriteUnitsPerSecond(), onDemand, gsi.getProvisionedThroughput().getReadCapacityUnits(),
gsi.getProvisionedThroughput().getWriteCapacityUnits());
}
}
private static void requireCreateWarmThroughput(String target, Long read, Long write, boolean onDemand,
Long readCapacity, Long writeCapacity) {
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");
}
}
private void requireUpdateWarmThroughput(UpdateTableRequest request, String region) {
WarmThroughput tableWarm = request.warmThroughput();
boolean indexWarm = request.gsiUpdates().stream().anyMatch(u -> u.warmThroughput() != null)
|| request.gsiCreates().stream().anyMatch(g -> g.getWarmReadUnitsPerSecond() != null
|| g.getWarmWriteUnitsPerSecond() != null);
if (tableWarm == null && !indexWarm) {
return;
}
TableDefinition table = dynamoDbService.describeTable(request.tableName(), region);
if (tableWarm != null) {
if (tableWarm.isEmpty()) {
// AWS answers an empty table WarmThroughput with InternalFailure.
throw new AwsException("InternalFailure", null, 500);
}
requireWarmThroughputAtLeast("table", tableWarm.readUnitsPerSecond(), tableWarm.writeUnitsPerSecond(),
table.getWarmReadUnitsPerSecond(), table.getWarmWriteUnitsPerSecond(),
CURRENT_WARM_REASON, CURRENT_WARM_REASON);
}
for (GsiThroughputUpdate update : request.gsiUpdates()) {
WarmThroughput warm = update.warmThroughput();
if (warm == null) {
continue;
}
if (warm.isEmpty()) {
throw new AwsException("ValidationException",
"One or more parameter values were invalid: WarmThroughput must have at least one of "
+ "ReadUnitsPerSecond or WriteUnitsPerSecond specified for index: " + update.indexName(), 400);
}
GlobalSecondaryIndex gsi = table.findGsi(update.indexName()).orElseThrow();
requireWarmThroughputAtLeast("index " + update.indexName(), warm.readUnitsPerSecond(),
warm.writeUnitsPerSecond(), table.gsiWarmReadUnits(gsi), table.gsiWarmWriteUnits(gsi),
CURRENT_WARM_REASON, CURRENT_WARM_REASON);
}
boolean onDemand = "PAY_PER_REQUEST".equals(table.getBillingMode());
for (GlobalSecondaryIndex gsi : request.gsiCreates()) {
requireCreateWarmThroughput("index " + gsi.getIndexName(), gsi.getWarmReadUnitsPerSecond(),
gsi.getWarmWriteUnitsPerSecond(), onDemand, gsi.getProvisionedThroughput().getReadCapacityUnits(),
gsi.getProvisionedThroughput().getWriteCapacityUnits());
}
}
private static void requireWarmThroughputAtLeast(String target, Long read, Long write, long minimumRead,
long minimumWrite, String readReason, String writeReason) {
if (read != null && read < minimumRead) {
throw lowWarmThroughput("ReadUnitsPerSecond", target, readReason);
}
if (write != null && write < minimumWrite) {
throw lowWarmThroughput("WriteUnitsPerSecond", target, writeReason);
}
}
private static AwsException lowWarmThroughput(String member, String target, String reason) {
return new AwsException("ValidationException", "One or more parameter values were invalid: Requested "
+ member + " for WarmThroughput for " + target + " is lower than " + reason, 400);
}
private static void applyWarmThroughput(TableDefinition table, WarmThroughput warm) {
if (warm == null) {
return;
}
if (warm.readUnitsPerSecond() != null && table.getWarmReadUnitsPerSecond() < warm.readUnitsPerSecond()) {
table.setWarmReadUnitsPerSecond(warm.readUnitsPerSecond());
}
if (warm.writeUnitsPerSecond() != null && table.getWarmWriteUnitsPerSecond() < warm.writeUnitsPerSecond()) {
table.setWarmWriteUnitsPerSecond(warm.writeUnitsPerSecond());
}
}
private static void applyWarmThroughput(GlobalSecondaryIndex gsi, WarmThroughput warm) {
if (warm == null) {
return;
}
if (warm.readUnitsPerSecond() != null) {
gsi.setWarmReadUnitsPerSecond(warm.readUnitsPerSecond());
}
if (warm.writeUnitsPerSecond() != null) {
gsi.setWarmWriteUnitsPerSecond(warm.writeUnitsPerSecond());
}
}
private static void applyOnDemandThroughput(TableDefinition table, OnDemandThroughput onDemand) {
if (onDemand == null) {
return;
@@ -21,6 +21,9 @@ public class GlobalSecondaryIndex {
private List<String> nonKeyAttributes;
private Integer onDemandMaxReadRequestUnits;
private Integer onDemandMaxWriteRequestUnits;
// Null until a request sets the index's warm throughput.
private Long warmReadUnitsPerSecond;
private Long warmWriteUnitsPerSecond;
// Null until the first UpdateContributorInsights call on this index.
private ContributorInsights contributorInsights;
@@ -74,6 +77,12 @@ public class GlobalSecondaryIndex {
public Integer getOnDemandMaxWriteRequestUnits() { return onDemandMaxWriteRequestUnits; }
public void setOnDemandMaxWriteRequestUnits(Integer v) { this.onDemandMaxWriteRequestUnits = v; }
public Long getWarmReadUnitsPerSecond() { return warmReadUnitsPerSecond; }
public void setWarmReadUnitsPerSecond(Long v) { this.warmReadUnitsPerSecond = v; }
public Long getWarmWriteUnitsPerSecond() { return warmWriteUnitsPerSecond; }
public void setWarmWriteUnitsPerSecond(Long v) { this.warmWriteUnitsPerSecond = v; }
public ContributorInsights getContributorInsights() { return contributorInsights; }
public void setContributorInsights(ContributorInsights contributorInsights) {
this.contributorInsights = contributorInsights;
@@ -126,6 +126,22 @@ public class TableDefinition {
return wcu > 0 ? wcu : 5;
}
/** The warm read units an index reports: its baseline, or a higher value the index was given. */
public long gsiWarmReadUnits(GlobalSecondaryIndex gsi) {
long baseline = "PAY_PER_REQUEST".equals(billingMode)
? defaultWarmReadUnits(billingMode, null) : gsi.getProvisionedThroughput().getReadCapacityUnits();
Long stored = gsi.getWarmReadUnitsPerSecond();
return stored != null && stored > baseline ? stored : baseline;
}
/** The warm write units an index reports: its baseline, or a higher value the index was given. */
public long gsiWarmWriteUnits(GlobalSecondaryIndex gsi) {
long baseline = "PAY_PER_REQUEST".equals(billingMode)
? defaultWarmWriteUnits(billingMode, null) : gsi.getProvisionedThroughput().getWriteCapacityUnits();
Long stored = gsi.getWarmWriteUnitsPerSecond();
return stored != null && stored > baseline ? stored : baseline;
}
public String getTableName() { return tableName; }
public void setTableName(String tableName) { this.tableName = tableName; }