mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(dynamodb): calculate consumed capacity correctly (#3071)
* fix(dynamodb): bill reads on pre-filter size and honor ConsistentRead ConsumedCapacity for GetItem, Query and Scan now follows the DynamoDB read billing model. Units are half a unit per 4KB read, doubled for a strongly consistent read, with the half unit minimum. Query and Scan carry the pre-filter byte size of the read items out of the service, so a FilterExpression or a narrow projection no longer makes the read look cheaper than it is. Verified with the paritysuite dynamodb-conformance tier1 suite. * fix(dynamodb): charge secondary index writes in ConsumedCapacity PutItem, UpdateItem, DeleteItem and BatchWriteItem now report the index side of a write. Each GSI and LSI is charged by how the write changes what the index stores. One unit for an insert, an in place update or a delete, two units when an index key changes, and nothing when the stored view is unchanged or the item never touches the index. Under INDEXES the response carries the Table arm plus a per index map only for the indexes that were charged, and the top level total folds them in. BatchWriteItem reports one entry per table with summed arms. Verified with the paritysuite dynamodb-conformance tier1 suite. * test(dynamodb): pin the ConsumedCapacity billing model Mirror paritysuite dynamodb-conformance tier1 cases. * fix(dynamodb): size index reads on the projected view the index stores * test(dynamodb): pin BatchWriteItem ConsumedCapacity shape and per-table arms * fix(dynamodb): validate BatchWriteItem ReturnConsumedCapacity and report LSI reads under LocalSecondaryIndexes
This commit is contained in:
1 parent
5c8cff61b8
commit
97915d3668
5 files changed
+962
-26
No files matched your search
+481
@@ -0,0 +1,481 @@
|
||||
package io.github.hectorvent.floci.services.dynamodb;
|
||||
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.RestAssured;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.MethodOrderer;
|
||||
import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* Pins the ConsumedCapacity magnitudes to the DynamoDB billing model, as measured by
|
||||
* the paritysuite dynamodb-conformance tier1 suite. Reads cost half a unit per 4KB,
|
||||
* doubled for a strongly consistent read, and Query and Scan are sized on what was
|
||||
* read before the filter. Writes charge the table plus each index whose stored view
|
||||
* the write changes, and no operation reports a read/write split.
|
||||
*/
|
||||
@QuarkusTest
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
class DynamoDbConsumedCapacityIntegrationTest {
|
||||
|
||||
private static final String CT = "application/x-amz-json-1.0";
|
||||
private static final String TABLE = "CapacityParityTable";
|
||||
private static int testPort;
|
||||
|
||||
@BeforeAll
|
||||
static void configureRestAssured() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void createTable() {
|
||||
testPort = RestAssured.port;
|
||||
given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810.CreateTable")
|
||||
.contentType(CT)
|
||||
.body("""
|
||||
{
|
||||
"TableName": "%s",
|
||||
"KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}],
|
||||
"AttributeDefinitions": [
|
||||
{"AttributeName": "pk", "AttributeType": "S"},
|
||||
{"AttributeName": "gsiPk", "AttributeType": "S"},
|
||||
{"AttributeName": "gsi2Pk", "AttributeType": "S"}
|
||||
],
|
||||
"GlobalSecondaryIndexes": [{
|
||||
"IndexName": "gsi1",
|
||||
"KeySchema": [{"AttributeName": "gsiPk", "KeyType": "HASH"}],
|
||||
"Projection": {"ProjectionType": "ALL"}
|
||||
}, {
|
||||
"IndexName": "gsi2",
|
||||
"KeySchema": [{"AttributeName": "gsi2Pk", "KeyType": "HASH"}],
|
||||
"Projection": {"ProjectionType": "KEYS_ONLY"}
|
||||
}],
|
||||
"BillingMode": "PAY_PER_REQUEST"
|
||||
}
|
||||
""".formatted(TABLE))
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void strongConsistentGetItemCostsOneUnitWithNoSplit() {
|
||||
putItem("""
|
||||
{"pk": {"S": "cc-get"}, "data": {"S": "x"}}
|
||||
""");
|
||||
given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810.GetItem")
|
||||
.contentType(CT)
|
||||
.body("""
|
||||
{
|
||||
"TableName": "%s",
|
||||
"Key": {"pk": {"S": "cc-get"}},
|
||||
"ConsistentRead": true,
|
||||
"ReturnConsumedCapacity": "TOTAL"
|
||||
}
|
||||
""".formatted(TABLE))
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("ConsumedCapacity.CapacityUnits", equalTo(1.0f))
|
||||
.body("ConsumedCapacity.ReadCapacityUnits", nullValue())
|
||||
.body("ConsumedCapacity.WriteCapacityUnits", nullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void eventualGetItemCostsHalfAUnit() {
|
||||
given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810.GetItem")
|
||||
.contentType(CT)
|
||||
.body("""
|
||||
{
|
||||
"TableName": "%s",
|
||||
"Key": {"pk": {"S": "cc-get"}},
|
||||
"ReturnConsumedCapacity": "TOTAL"
|
||||
}
|
||||
""".formatted(TABLE))
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("ConsumedCapacity.CapacityUnits", equalTo(0.5f));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(4)
|
||||
void queryUnderIndexesReportsTableArmWithNoSplit() {
|
||||
given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810.Query")
|
||||
.contentType(CT)
|
||||
.body("""
|
||||
{
|
||||
"TableName": "%s",
|
||||
"KeyConditionExpression": "pk = :pk",
|
||||
"ExpressionAttributeValues": {":pk": {"S": "cc-get"}},
|
||||
"ConsistentRead": true,
|
||||
"ReturnConsumedCapacity": "INDEXES"
|
||||
}
|
||||
""".formatted(TABLE))
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("ConsumedCapacity.CapacityUnits", equalTo(1.0f))
|
||||
.body("ConsumedCapacity.Table.CapacityUnits", equalTo(1.0f))
|
||||
.body("ConsumedCapacity.Table.ReadCapacityUnits", nullValue())
|
||||
.body("ConsumedCapacity.ReadCapacityUnits", nullValue());
|
||||
}
|
||||
|
||||
/**
|
||||
* A Scan is billed on what it read, not on what survived the filter. Six 1KB
|
||||
* items make the unfiltered read cost more than the half-unit minimum, so a
|
||||
* post-filter or per-returned-item model would report a smaller figure for
|
||||
* the filter that matches nothing.
|
||||
*/
|
||||
@Test
|
||||
@Order(5)
|
||||
void scanWithFilterCostsWhatTheUnfilteredScanCosts() {
|
||||
for (var i = 0; i < 6; i++) {
|
||||
putItem("""
|
||||
{"pk": {"S": "cc-scan-%d"}, "keep": {"S": "no"}, "filler": {"S": "%s"}}
|
||||
""".formatted(i, "x".repeat(1000)));
|
||||
}
|
||||
float plain = scanUnits("""
|
||||
{"TableName": "%s", "ReturnConsumedCapacity": "TOTAL"}
|
||||
""".formatted(TABLE));
|
||||
float filtered = scanUnits("""
|
||||
{
|
||||
"TableName": "%s",
|
||||
"FilterExpression": "keep = :k",
|
||||
"ExpressionAttributeValues": {":k": {"S": "yes"}},
|
||||
"ReturnConsumedCapacity": "TOTAL"
|
||||
}
|
||||
""".formatted(TABLE));
|
||||
assertTrue(plain >= 1.0f, "six 1KB items must cost more than the half-unit minimum");
|
||||
assertEquals(plain, filtered, "a filter matching nothing must not change the cost");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
void putItemReportsTheGsiWriteBesideTheTableWrite() {
|
||||
given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810.PutItem")
|
||||
.contentType(CT)
|
||||
.body("""
|
||||
{
|
||||
"TableName": "%s",
|
||||
"Item": {"pk": {"S": "cc-idx"}, "gsiPk": {"S": "g1"}},
|
||||
"ReturnConsumedCapacity": "INDEXES"
|
||||
}
|
||||
""".formatted(TABLE))
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("ConsumedCapacity.CapacityUnits", equalTo(2.0f))
|
||||
.body("ConsumedCapacity.Table.CapacityUnits", equalTo(1.0f))
|
||||
.body("ConsumedCapacity.GlobalSecondaryIndexes.gsi1.CapacityUnits", equalTo(1.0f))
|
||||
.body("ConsumedCapacity.LocalSecondaryIndexes", nullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(7)
|
||||
void identicalOverwriteChargesNoIndexArms() {
|
||||
given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810.PutItem")
|
||||
.contentType(CT)
|
||||
.body("""
|
||||
{
|
||||
"TableName": "%s",
|
||||
"Item": {"pk": {"S": "cc-idx"}, "gsiPk": {"S": "g1"}},
|
||||
"ReturnConsumedCapacity": "INDEXES"
|
||||
}
|
||||
""".formatted(TABLE))
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("ConsumedCapacity.CapacityUnits", equalTo(1.0f))
|
||||
.body("ConsumedCapacity.Table.CapacityUnits", equalTo(1.0f))
|
||||
.body("ConsumedCapacity.GlobalSecondaryIndexes", nullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(8)
|
||||
void putItemUnderTotalFoldsTheIndexCostWithNoBreakdown() {
|
||||
given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810.PutItem")
|
||||
.contentType(CT)
|
||||
.body("""
|
||||
{
|
||||
"TableName": "%s",
|
||||
"Item": {"pk": {"S": "cc-idx-total"}, "gsiPk": {"S": "g2"}},
|
||||
"ReturnConsumedCapacity": "TOTAL"
|
||||
}
|
||||
""".formatted(TABLE))
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("ConsumedCapacity.CapacityUnits", equalTo(2.0f))
|
||||
.body("ConsumedCapacity.Table", nullValue())
|
||||
.body("ConsumedCapacity.GlobalSecondaryIndexes", nullValue());
|
||||
}
|
||||
|
||||
/**
|
||||
* A read served by a secondary index is billed on what the index stores, not on
|
||||
* the full base item. On real AWS a 20KB item behind a KEYS_ONLY GSI costs 0.5
|
||||
* units to query while the same item through an ALL projection carries its size
|
||||
* (measured us-east-1, 2026-09-05).
|
||||
*/
|
||||
@Test
|
||||
@Order(9)
|
||||
void indexReadsAreBilledOnTheProjectedView() {
|
||||
putItem("""
|
||||
{"pk": {"S": "cc-big"}, "gsiPk": {"S": "g-big"}, "gsi2Pk": {"S": "g-big"}, "filler": {"S": "%s"}}
|
||||
""".formatted("x".repeat(9000)));
|
||||
given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810.Query")
|
||||
.contentType(CT)
|
||||
.body("""
|
||||
{
|
||||
"TableName": "%s",
|
||||
"IndexName": "gsi2",
|
||||
"KeyConditionExpression": "gsi2Pk = :g",
|
||||
"ExpressionAttributeValues": {":g": {"S": "g-big"}},
|
||||
"ReturnConsumedCapacity": "TOTAL"
|
||||
}
|
||||
""".formatted(TABLE))
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("Count", equalTo(1))
|
||||
.body("ConsumedCapacity.CapacityUnits", equalTo(0.5f));
|
||||
given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810.Query")
|
||||
.contentType(CT)
|
||||
.body("""
|
||||
{
|
||||
"TableName": "%s",
|
||||
"IndexName": "gsi1",
|
||||
"KeyConditionExpression": "gsiPk = :g",
|
||||
"ExpressionAttributeValues": {":g": {"S": "g-big"}},
|
||||
"ReturnConsumedCapacity": "TOTAL"
|
||||
}
|
||||
""".formatted(TABLE))
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("Count", equalTo(1))
|
||||
.body("ConsumedCapacity.CapacityUnits", equalTo(1.5f));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(10)
|
||||
void batchWriteItemReportsPerTableEntryWithTouchedArms() {
|
||||
given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810.BatchWriteItem")
|
||||
.contentType(CT)
|
||||
.body("""
|
||||
{
|
||||
"RequestItems": {
|
||||
"%s": [
|
||||
{"PutRequest": {"Item": {"pk": {"S": "cc-bw-1"}, "gsiPk": {"S": "g-bw"}}}},
|
||||
{"PutRequest": {"Item": {"pk": {"S": "cc-bw-2"}}}}
|
||||
]
|
||||
},
|
||||
"ReturnConsumedCapacity": "INDEXES"
|
||||
}
|
||||
""".formatted(TABLE))
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("ConsumedCapacity.size()", equalTo(1))
|
||||
.body("ConsumedCapacity[0].TableName", equalTo(TABLE))
|
||||
.body("ConsumedCapacity[0].CapacityUnits", equalTo(3.0f))
|
||||
.body("ConsumedCapacity[0].Table.CapacityUnits", equalTo(2.0f))
|
||||
.body("ConsumedCapacity[0].GlobalSecondaryIndexes.gsi1.CapacityUnits", equalTo(1.0f))
|
||||
.body("ConsumedCapacity[0].LocalSecondaryIndexes", nullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(11)
|
||||
void batchWriteItemUnderTotalFoldsTheArmsWithNoBreakdown() {
|
||||
given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810.BatchWriteItem")
|
||||
.contentType(CT)
|
||||
.body("""
|
||||
{
|
||||
"RequestItems": {
|
||||
"%s": [
|
||||
{"PutRequest": {"Item": {"pk": {"S": "cc-bw-3"}, "gsiPk": {"S": "g-bw2"}}}},
|
||||
{"PutRequest": {"Item": {"pk": {"S": "cc-bw-4"}}}}
|
||||
]
|
||||
},
|
||||
"ReturnConsumedCapacity": "TOTAL"
|
||||
}
|
||||
""".formatted(TABLE))
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("ConsumedCapacity.size()", equalTo(1))
|
||||
.body("ConsumedCapacity[0].CapacityUnits", equalTo(3.0f))
|
||||
.body("ConsumedCapacity[0].Table", nullValue())
|
||||
.body("ConsumedCapacity[0].GlobalSecondaryIndexes", nullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(12)
|
||||
void batchWriteItemRejectsAnInvalidReturnConsumedCapacity() {
|
||||
given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810.BatchWriteItem")
|
||||
.contentType(CT)
|
||||
.body("""
|
||||
{
|
||||
"RequestItems": {
|
||||
"%s": [{"PutRequest": {"Item": {"pk": {"S": "cc-bw-bad"}}}}]
|
||||
},
|
||||
"ReturnConsumedCapacity": "BOGUS"
|
||||
}
|
||||
""".formatted(TABLE))
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("message", equalTo("1 validation error detected: Value 'BOGUS' at "
|
||||
+ "'returnConsumedCapacity' failed to satisfy constraint: "
|
||||
+ "Member must satisfy enum value set: [INDEXES, TOTAL, NONE]"));
|
||||
}
|
||||
|
||||
/**
|
||||
* A read served by an LSI reports its units under LocalSecondaryIndexes with a
|
||||
* zero Table entry, measured on real DynamoDB (us-east-1, 2026-09-05).
|
||||
*/
|
||||
@Test
|
||||
@Order(13)
|
||||
void lsiReadReportsItsUnitsUnderLocalSecondaryIndexes() {
|
||||
given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810.CreateTable")
|
||||
.contentType(CT)
|
||||
.body("""
|
||||
{
|
||||
"TableName": "CapacityLsiTable",
|
||||
"KeySchema": [
|
||||
{"AttributeName": "pk", "KeyType": "HASH"},
|
||||
{"AttributeName": "sk", "KeyType": "RANGE"}
|
||||
],
|
||||
"AttributeDefinitions": [
|
||||
{"AttributeName": "pk", "AttributeType": "S"},
|
||||
{"AttributeName": "sk", "AttributeType": "S"},
|
||||
{"AttributeName": "lsiSk", "AttributeType": "S"}
|
||||
],
|
||||
"LocalSecondaryIndexes": [{
|
||||
"IndexName": "lsi1",
|
||||
"KeySchema": [
|
||||
{"AttributeName": "pk", "KeyType": "HASH"},
|
||||
{"AttributeName": "lsiSk", "KeyType": "RANGE"}
|
||||
],
|
||||
"Projection": {"ProjectionType": "ALL"}
|
||||
}],
|
||||
"BillingMode": "PAY_PER_REQUEST"
|
||||
}
|
||||
""")
|
||||
.when().post("/").then().statusCode(200);
|
||||
given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810.PutItem")
|
||||
.contentType(CT)
|
||||
.body("""
|
||||
{
|
||||
"TableName": "CapacityLsiTable",
|
||||
"Item": {"pk": {"S": "p1"}, "sk": {"S": "1"}, "lsiSk": {"S": "L1"}}
|
||||
}
|
||||
""")
|
||||
.when().post("/").then().statusCode(200);
|
||||
given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810.Query")
|
||||
.contentType(CT)
|
||||
.body("""
|
||||
{
|
||||
"TableName": "CapacityLsiTable",
|
||||
"IndexName": "lsi1",
|
||||
"KeyConditionExpression": "pk = :p",
|
||||
"ExpressionAttributeValues": {":p": {"S": "p1"}},
|
||||
"ConsistentRead": true,
|
||||
"ReturnConsumedCapacity": "INDEXES"
|
||||
}
|
||||
""")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("ConsumedCapacity.CapacityUnits", equalTo(1.0f))
|
||||
.body("ConsumedCapacity.Table.CapacityUnits", equalTo(0.0f))
|
||||
.body("ConsumedCapacity.LocalSecondaryIndexes.lsi1.CapacityUnits", equalTo(1.0f))
|
||||
.body("ConsumedCapacity.GlobalSecondaryIndexes", nullValue());
|
||||
given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810.DeleteTable")
|
||||
.contentType(CT)
|
||||
.body("""
|
||||
{"TableName": "CapacityLsiTable"}
|
||||
""")
|
||||
.when().post("/").then().statusCode(200);
|
||||
}
|
||||
|
||||
private static void putItem(String itemJson) {
|
||||
given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810.PutItem")
|
||||
.contentType(CT)
|
||||
.body("""
|
||||
{"TableName": "%s", "Item": %s}
|
||||
""".formatted(TABLE, itemJson))
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
private static float scanUnits(String body) {
|
||||
return given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810.Scan")
|
||||
.contentType(CT)
|
||||
.body(body)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().path("ConsumedCapacity.CapacityUnits");
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void cleanup() {
|
||||
given()
|
||||
.port(testPort)
|
||||
.header("X-Amz-Target", "DynamoDB_20120810.DeleteTable")
|
||||
.contentType(CT)
|
||||
.body("""
|
||||
{"TableName": "%s"}
|
||||
""".formatted(TABLE))
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
}
|
||||
+189
@@ -0,0 +1,189 @@
|
||||
package io.github.hectorvent.floci.services.dynamodb;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.GlobalSecondaryIndex;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.KeySchemaElement;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.LocalSecondaryIndex;
|
||||
import io.github.hectorvent.floci.services.dynamodb.model.TableDefinition;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/**
|
||||
* Unit tests for the write ConsumedCapacity model. The table setup mirrors the
|
||||
* paritysuite dynamodb-conformance index write-capacity ladder: a GSI with an
|
||||
* INCLUDE projection as the projected/non-projected lever, and an ALL-projected
|
||||
* LSI. Every charge below is a value measured on real DynamoDB by that suite.
|
||||
*/
|
||||
class DynamoDbWriteCapacityTest {
|
||||
|
||||
private static final ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
private TableDefinition table;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
table = new TableDefinition();
|
||||
table.setKeySchema(List.of(
|
||||
new KeySchemaElement("pk", "HASH"),
|
||||
new KeySchemaElement("sk", "RANGE")));
|
||||
table.setGlobalSecondaryIndexes(List.of(new GlobalSecondaryIndex(
|
||||
"gsi-inc",
|
||||
List.of(new KeySchemaElement("gsiPk", "HASH")),
|
||||
null, "INCLUDE", List.of("proj"))));
|
||||
table.setLocalSecondaryIndexes(List.of(new LocalSecondaryIndex(
|
||||
"lsi1",
|
||||
List.of(new KeySchemaElement("pk", "HASH"),
|
||||
new KeySchemaElement("lsiSk", "RANGE")),
|
||||
null, "ALL")));
|
||||
}
|
||||
|
||||
private static ObjectNode item(String json) {
|
||||
try {
|
||||
return (ObjectNode) mapper.readTree(json);
|
||||
} catch (Exception e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private static ObjectNode fullItem() {
|
||||
return item("""
|
||||
{
|
||||
"pk": {"S": "p1"}, "sk": {"S": "1"},
|
||||
"gsiPk": {"S": "g1"}, "lsiSk": {"S": "L1"},
|
||||
"proj": {"S": "p"}, "other": {"S": "o"}
|
||||
}
|
||||
""");
|
||||
}
|
||||
|
||||
@Test
|
||||
void insertChargesTableAndEveryIndexTheItemEnters() {
|
||||
var cost = DynamoDbWriteCapacity.forWrite(table, null, fullItem());
|
||||
assertEquals(1.0, cost.table());
|
||||
assertEquals(Map.of("gsi-inc", 1.0), cost.gsi());
|
||||
assertEquals(Map.of("lsi1", 1.0), cost.lsi());
|
||||
assertEquals(3.0, cost.total());
|
||||
}
|
||||
|
||||
@Test
|
||||
void sparseInsertChargesNothingForTheIndexesItMisses() {
|
||||
var sparse = item("""
|
||||
{"pk": {"S": "p1"}, "sk": {"S": "1"}, "other": {"S": "o"}}
|
||||
""");
|
||||
var cost = DynamoDbWriteCapacity.forWrite(table, null, sparse);
|
||||
assertEquals(1.0, cost.total());
|
||||
assertTrue(cost.gsi().isEmpty(), "an item without gsiPk never enters the GSI");
|
||||
assertTrue(cost.lsi().isEmpty(), "an item without lsiSk never enters the LSI");
|
||||
}
|
||||
|
||||
@Test
|
||||
void identicalOverwriteChargesNoIndexWritesAtAll() {
|
||||
var cost = DynamoDbWriteCapacity.forWrite(table, fullItem(), fullItem());
|
||||
assertEquals(1.0, cost.table(), "the table write is still charged");
|
||||
assertTrue(cost.gsi().isEmpty());
|
||||
assertTrue(cost.lsi().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void nonProjectedAttributeChangeChargesNothingOnTheIncludeIndex() {
|
||||
var updated = fullItem();
|
||||
updated.set("other", item("""
|
||||
{"S": "o2"}
|
||||
"""));
|
||||
var cost = DynamoDbWriteCapacity.forWrite(table, fullItem(), updated);
|
||||
assertTrue(cost.gsi().isEmpty(), "other is outside the INCLUDE projection");
|
||||
assertEquals(Map.of("lsi1", 1.0), cost.lsi());
|
||||
assertEquals(2.0, cost.total());
|
||||
}
|
||||
|
||||
@Test
|
||||
void projectedAttributeChangeChargesOneIndexWrite() {
|
||||
var updated = fullItem();
|
||||
updated.set("proj", item("""
|
||||
{"S": "p2"}
|
||||
"""));
|
||||
var cost = DynamoDbWriteCapacity.forWrite(table, fullItem(), updated);
|
||||
assertEquals(Map.of("gsi-inc", 1.0), cost.gsi());
|
||||
assertEquals(Map.of("lsi1", 1.0), cost.lsi());
|
||||
assertEquals(3.0, cost.total());
|
||||
}
|
||||
|
||||
@Test
|
||||
void indexKeyChangeChargesDeletePlusInsert() {
|
||||
var moved = fullItem();
|
||||
moved.set("gsiPk", item("""
|
||||
{"S": "g-moved"}
|
||||
"""));
|
||||
var cost = DynamoDbWriteCapacity.forWrite(table, fullItem(), moved);
|
||||
assertEquals(Map.of("gsi-inc", 2.0), cost.gsi());
|
||||
assertEquals(Map.of("lsi1", 1.0), cost.lsi());
|
||||
assertEquals(4.0, cost.total());
|
||||
}
|
||||
|
||||
@Test
|
||||
void removingTheIndexKeyChargesDeleteOnly() {
|
||||
var removed = fullItem();
|
||||
removed.remove("gsiPk");
|
||||
var cost = DynamoDbWriteCapacity.forWrite(table, fullItem(), removed);
|
||||
assertEquals(Map.of("gsi-inc", 1.0), cost.gsi());
|
||||
assertEquals(Map.of("lsi1", 1.0), cost.lsi());
|
||||
assertEquals(3.0, cost.total());
|
||||
}
|
||||
|
||||
@Test
|
||||
void lsiKeyChangeWalksTheSameLadder() {
|
||||
var moved = fullItem();
|
||||
moved.set("lsiSk", item("""
|
||||
{"S": "L2"}
|
||||
"""));
|
||||
var cost = DynamoDbWriteCapacity.forWrite(table, fullItem(), moved);
|
||||
assertTrue(cost.gsi().isEmpty(), "lsiSk is outside the GSI projection, its view never changed");
|
||||
assertEquals(Map.of("lsi1", 2.0), cost.lsi());
|
||||
assertEquals(3.0, cost.total());
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteChargesOneWritePerIndexTheItemOccupied() {
|
||||
var cost = DynamoDbWriteCapacity.forWrite(table, fullItem(), null);
|
||||
assertEquals(1.0, cost.table());
|
||||
assertEquals(Map.of("gsi-inc", 1.0), cost.gsi());
|
||||
assertEquals(Map.of("lsi1", 1.0), cost.lsi());
|
||||
assertEquals(3.0, cost.total());
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteOfAMissingItemStillChargesOneTableUnit() {
|
||||
var cost = DynamoDbWriteCapacity.forWrite(table, null, null);
|
||||
assertEquals(1.0, cost.total());
|
||||
}
|
||||
|
||||
@Test
|
||||
void costPlusSumsTheTableAndMergesIndexArms() {
|
||||
var indexed = DynamoDbWriteCapacity.forWrite(table, null, fullItem());
|
||||
var sparse = DynamoDbWriteCapacity.forWrite(table, null, item("""
|
||||
{"pk": {"S": "p2"}, "sk": {"S": "1"}, "other": {"S": "o"}}
|
||||
"""));
|
||||
var sum = DynamoDbWriteCapacity.Cost.zero().plus(indexed).plus(sparse);
|
||||
assertEquals(2.0, sum.table());
|
||||
assertEquals(Map.of("gsi-inc", 1.0), sum.gsi());
|
||||
assertEquals(Map.of("lsi1", 1.0), sum.lsi());
|
||||
assertEquals(4.0, sum.total());
|
||||
}
|
||||
|
||||
@Test
|
||||
void tableUnitsFollowTheLargerImagePerKilobyte() {
|
||||
var big = item("""
|
||||
{"pk": {"S": "p1"}, "sk": {"S": "1"}}
|
||||
""");
|
||||
big.set("filler", item("""
|
||||
{"S": "%s"}
|
||||
""".formatted("x".repeat(2500))));
|
||||
var cost = DynamoDbWriteCapacity.forWrite(table, big, fullItem());
|
||||
assertEquals(3.0, cost.table(), "the larger of the two images rounds up per 1KB");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user