mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(dynamodb): add missing ConsumedCapacity to transaction responses (#4151)
* fix(dynamodb): add missing ConsumedCapacity to transaction responses * fix(dynamodb): wait for an in-flight same-token transaction and free the token when it fails * fix(dynamodb): reject an invalid ReturnConsumedCapacity on transactions before they run
This commit is contained in:
1 parent
f5c63ff398
commit
5d9986aa6c
5 files changed
+516
-94
No files matched your search
+205
-1
@@ -3,6 +3,7 @@ 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 io.restassured.response.ValidatableResponse;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.MethodOrderer;
|
||||
@@ -20,7 +21,8 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
* 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.
|
||||
* the write changes. Only transactions report a read/write split, and they charge the
|
||||
* table twice the normal rate.
|
||||
*/
|
||||
@QuarkusTest
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
@@ -439,6 +441,208 @@ class DynamoDbConsumedCapacityIntegrationTest {
|
||||
.when().post("/").then().statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(14)
|
||||
void transactWriteChargesTheTableTwiceAndEachIndexOnce() {
|
||||
transactWrite("""
|
||||
{
|
||||
"TransactItems": [{"Put": {"TableName": "%s",
|
||||
"Item": {"pk": {"S": "cc-tx"}, "gsiPk": {"S": "t1"}, "gsi2Pk": {"S": "t2"}}}}],
|
||||
"ReturnConsumedCapacity": "INDEXES"
|
||||
}
|
||||
""".formatted(TABLE))
|
||||
.body("ConsumedCapacity", hasSize(1))
|
||||
.body("ConsumedCapacity[0].TableName", equalTo(TABLE))
|
||||
.body("ConsumedCapacity[0].CapacityUnits", equalTo(4.0f))
|
||||
.body("ConsumedCapacity[0].WriteCapacityUnits", equalTo(4.0f))
|
||||
.body("ConsumedCapacity[0].ReadCapacityUnits", nullValue())
|
||||
.body("ConsumedCapacity[0].Table.CapacityUnits", equalTo(2.0f))
|
||||
.body("ConsumedCapacity[0].Table.WriteCapacityUnits", equalTo(2.0f))
|
||||
.body("ConsumedCapacity[0].GlobalSecondaryIndexes.gsi1.WriteCapacityUnits", equalTo(1.0f))
|
||||
.body("ConsumedCapacity[0].GlobalSecondaryIndexes.gsi2.WriteCapacityUnits", equalTo(1.0f));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(15)
|
||||
void transactConditionCheckIsChargedAsAWriteOfTheCheckedItem() {
|
||||
putItem("""
|
||||
{"pk": {"S": "cc-tx-checked"}, "filler": {"S": "%s"}}
|
||||
""".formatted("x".repeat(5000)));
|
||||
transactWrite("""
|
||||
{
|
||||
"TransactItems": [
|
||||
{"Put": {"TableName": "%1$s", "Item": {"pk": {"S": "cc-tx-put"}}}},
|
||||
{"ConditionCheck": {"TableName": "%1$s", "Key": {"pk": {"S": "cc-tx-checked"}},
|
||||
"ConditionExpression": "attribute_exists(pk)"}}
|
||||
],
|
||||
"ReturnConsumedCapacity": "TOTAL"
|
||||
}
|
||||
""".formatted(TABLE))
|
||||
.body("ConsumedCapacity", hasSize(1))
|
||||
.body("ConsumedCapacity[0].CapacityUnits", equalTo(12.0f))
|
||||
.body("ConsumedCapacity[0].WriteCapacityUnits", equalTo(12.0f))
|
||||
.body("ConsumedCapacity[0].Table", nullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(16)
|
||||
void transactReplayReportsAReadSizedOnTheFirstCall() {
|
||||
String body = """
|
||||
{
|
||||
"ClientRequestToken": "cc-tx-replay",
|
||||
"TransactItems": [{"Put": {"TableName": "%s",
|
||||
"Item": {"pk": {"S": "cc-tx-replay"}, "gsiPk": {"S": "r"}, "filler": {"S": "%s"}}}}],
|
||||
"ReturnConsumedCapacity": "INDEXES"
|
||||
}
|
||||
""".formatted(TABLE, "x".repeat(1536));
|
||||
transactWrite(body)
|
||||
.body("ConsumedCapacity[0].WriteCapacityUnits", equalTo(6.0f))
|
||||
.body("ConsumedCapacity[0].Table.WriteCapacityUnits", equalTo(4.0f))
|
||||
.body("ConsumedCapacity[0].GlobalSecondaryIndexes.gsi1.WriteCapacityUnits", equalTo(2.0f))
|
||||
.body("ConsumedCapacity[0].ReadCapacityUnits", nullValue());
|
||||
putItem("""
|
||||
{"pk": {"S": "cc-tx-replay"}, "filler": {"S": "%s"}}
|
||||
""".formatted("x".repeat(5000)));
|
||||
transactWrite(body)
|
||||
.body("ConsumedCapacity[0].CapacityUnits", equalTo(2.0f))
|
||||
.body("ConsumedCapacity[0].ReadCapacityUnits", equalTo(2.0f))
|
||||
.body("ConsumedCapacity[0].WriteCapacityUnits", nullValue())
|
||||
.body("ConsumedCapacity[0].Table.ReadCapacityUnits", equalTo(2.0f))
|
||||
.body("ConsumedCapacity[0].GlobalSecondaryIndexes", nullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(17)
|
||||
void transactGetChargesTheWholeItemAndAMissingItem() {
|
||||
given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810.TransactGetItems")
|
||||
.contentType(CT)
|
||||
.body("""
|
||||
{
|
||||
"TransactItems": [
|
||||
{"Get": {"TableName": "%1$s", "Key": {"pk": {"S": "cc-tx-checked"}},
|
||||
"ProjectionExpression": "pk"}},
|
||||
{"Get": {"TableName": "%1$s", "Key": {"pk": {"S": "cc-tx-missing"}}}}
|
||||
],
|
||||
"ReturnConsumedCapacity": "INDEXES"
|
||||
}
|
||||
""".formatted(TABLE))
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("ConsumedCapacity", hasSize(1))
|
||||
.body("ConsumedCapacity[0].CapacityUnits", equalTo(6.0f))
|
||||
.body("ConsumedCapacity[0].ReadCapacityUnits", equalTo(6.0f))
|
||||
.body("ConsumedCapacity[0].WriteCapacityUnits", nullValue())
|
||||
.body("ConsumedCapacity[0].Table.ReadCapacityUnits", equalTo(6.0f));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(18)
|
||||
void executeTransactionIsChargedLikeATransaction() {
|
||||
putItem("""
|
||||
{"pk": {"S": "cc-et"}, "data": {"S": "x"}}
|
||||
""");
|
||||
String update = """
|
||||
{
|
||||
"ClientRequestToken": "cc-et-tx",
|
||||
"TransactStatements": [{"Statement": "UPDATE \\"%s\\" SET data = 'y' WHERE pk = 'cc-et'"}],
|
||||
"ReturnConsumedCapacity": "INDEXES"
|
||||
}
|
||||
""".formatted(TABLE);
|
||||
send("ExecuteTransaction", update)
|
||||
.body("ConsumedCapacity[0].WriteCapacityUnits", equalTo(2.0f))
|
||||
.body("ConsumedCapacity[0].Table.WriteCapacityUnits", equalTo(2.0f))
|
||||
.body("ConsumedCapacity[0].ReadCapacityUnits", nullValue());
|
||||
send("ExecuteTransaction", update)
|
||||
.body("ConsumedCapacity[0].ReadCapacityUnits", equalTo(2.0f))
|
||||
.body("ConsumedCapacity[0].WriteCapacityUnits", nullValue());
|
||||
send("ExecuteTransaction", """
|
||||
{
|
||||
"TransactStatements": [
|
||||
{"Statement": "SELECT * FROM \\"%1$s\\" WHERE pk = 'cc-et'"},
|
||||
{"Statement": "SELECT * FROM \\"%1$s\\" WHERE pk = 'cc-et-missing'"}
|
||||
],
|
||||
"ReturnConsumedCapacity": "TOTAL"
|
||||
}
|
||||
""".formatted(TABLE))
|
||||
.body("ConsumedCapacity", hasSize(1))
|
||||
.body("ConsumedCapacity[0].CapacityUnits", equalTo(4.0f))
|
||||
.body("ConsumedCapacity[0].ReadCapacityUnits", equalTo(4.0f));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(19)
|
||||
void transactionsRejectAnInvalidReturnConsumedCapacityBeforeWriting() {
|
||||
String invalid = "Value 'INVALID' at 'returnConsumedCapacity' failed to satisfy constraint: "
|
||||
+ "Member must satisfy enum value set: [INDEXES, TOTAL, NONE]";
|
||||
rejected("TransactWriteItems", """
|
||||
{
|
||||
"TransactItems": [{"Put": {"TableName": "%s", "Item": {"pk": {"S": "cc-rcc"}}}}],
|
||||
"ReturnConsumedCapacity": "INVALID"
|
||||
}
|
||||
""".formatted(TABLE))
|
||||
.body("message", equalTo("1 validation error detected: " + invalid));
|
||||
rejected("ExecuteTransaction", """
|
||||
{
|
||||
"TransactStatements": [{"Statement": "INSERT INTO \\"%s\\" VALUE {'pk': 'cc-rcc-et'}"}],
|
||||
"ReturnConsumedCapacity": "INVALID"
|
||||
}
|
||||
""".formatted(TABLE))
|
||||
.body("message", equalTo("1 validation error detected: " + invalid));
|
||||
rejected("TransactGetItems", """
|
||||
{
|
||||
"TransactItems": [{"Get": {"TableName": "%s", "Key": {"pk": {"S": "cc-get"}}}}],
|
||||
"ReturnConsumedCapacity": "INVALID"
|
||||
}
|
||||
""".formatted(TABLE))
|
||||
.body("message", equalTo("1 validation error detected: " + invalid));
|
||||
rejected("TransactWriteItems", """
|
||||
{"TransactItems": [], "ReturnConsumedCapacity": "INVALID"}
|
||||
""")
|
||||
.body("message", equalTo("2 validation errors detected: Value '[]' at 'transactItems' failed to "
|
||||
+ "satisfy constraint: Member must have length greater than or equal to 1; " + invalid));
|
||||
|
||||
send("TransactGetItems", """
|
||||
{
|
||||
"TransactItems": [
|
||||
{"Get": {"TableName": "%1$s", "Key": {"pk": {"S": "cc-rcc"}}}},
|
||||
{"Get": {"TableName": "%1$s", "Key": {"pk": {"S": "cc-rcc-et"}}}}
|
||||
]
|
||||
}
|
||||
""".formatted(TABLE))
|
||||
.body("Responses[0].Item", nullValue())
|
||||
.body("Responses[1].Item", nullValue());
|
||||
}
|
||||
|
||||
private static ValidatableResponse rejected(String operation, String body) {
|
||||
return given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810." + operation)
|
||||
.contentType(CT)
|
||||
.body(body)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("ValidationException"));
|
||||
}
|
||||
|
||||
private static ValidatableResponse transactWrite(String body) {
|
||||
return send("TransactWriteItems", body);
|
||||
}
|
||||
|
||||
private static ValidatableResponse send(String operation, String body) {
|
||||
return given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810." + operation)
|
||||
.contentType(CT)
|
||||
.body(body)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
private static void putItem(String itemJson) {
|
||||
given()
|
||||
.header("X-Amz-Target", "DynamoDB_20120810.PutItem")
|
||||
|
||||
@@ -30,11 +30,18 @@ import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import java.util.zip.GZIPOutputStream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
@@ -3740,6 +3747,70 @@ class DynamoDbServiceTest {
|
||||
assertEquals("2", stored.get("counter").get("N").asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void transactWriteItemsReplayDuringTheFirstCallWaitsForItsCapacity() throws Exception {
|
||||
@SuppressWarnings("unchecked")
|
||||
StorageBackend<String, Map<String, JsonNode>> itemStore = mock(StorageBackend.class);
|
||||
DynamoDbService svc = new DynamoDbService(
|
||||
new InMemoryStorage<>(), itemStore, new RegionResolver("us-east-1", "000000000000"));
|
||||
svc.createTable("Users",
|
||||
List.of(new KeySchemaElement("userId", "HASH")),
|
||||
List.of(new AttributeDefinition("userId", "S")),
|
||||
5L, 5L, "us-east-1");
|
||||
CountDownLatch committing = new CountDownLatch(1);
|
||||
CountDownLatch release = new CountDownLatch(1);
|
||||
doAnswer(invocation -> {
|
||||
committing.countDown();
|
||||
release.await();
|
||||
return null;
|
||||
}).when(itemStore).put(any(), any());
|
||||
JsonNode put = mapper.readTree("""
|
||||
{"Put":{"TableName":"Users","Item":{"userId":{"S":"u1"}}}}
|
||||
""");
|
||||
JsonNode rawRequest = mapper.readTree("""
|
||||
{"ClientRequestToken":"tok-wait","TransactItems":[{"Put":{"TableName":"Users","Item":{"userId":{"S":"u1"}}}}]}
|
||||
""");
|
||||
|
||||
ExecutorService executor = Executors.newFixedThreadPool(2);
|
||||
try {
|
||||
Future<DynamoDbService.TransactWriteResult> first = executor.submit(() ->
|
||||
svc.transactWriteItems(List.of(put), "us-east-1", "tok-wait", rawRequest));
|
||||
assertTrue(committing.await(5, TimeUnit.SECONDS));
|
||||
Future<DynamoDbService.TransactWriteResult> replay = executor.submit(() ->
|
||||
svc.transactWriteItems(List.of(put), "us-east-1", "tok-wait", rawRequest));
|
||||
assertThrows(TimeoutException.class, () -> replay.get(200, TimeUnit.MILLISECONDS));
|
||||
release.countDown();
|
||||
|
||||
assertFalse(first.get(5, TimeUnit.SECONDS).replayed());
|
||||
DynamoDbService.TransactWriteResult replayed = replay.get(5, TimeUnit.SECONDS);
|
||||
assertTrue(replayed.replayed());
|
||||
assertEquals(2.0, replayed.capacity().get("Users").table());
|
||||
} finally {
|
||||
release.countDown();
|
||||
executor.shutdownNow();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void transactWriteItemsCancelledCallDoesNotKeepItsToken() throws Exception {
|
||||
createUsersTable("us-east-1");
|
||||
JsonNode put = mapper.readTree("""
|
||||
{"Put":{"TableName":"Users","Item":{"userId":{"S":"u1"}},"ConditionExpression":"attribute_exists(userId)"}}
|
||||
""");
|
||||
ObjectNode rawRequest = mapper.createObjectNode();
|
||||
rawRequest.putArray("TransactItems").add(put);
|
||||
rawRequest.put("ClientRequestToken", "tok-cancel");
|
||||
|
||||
assertThrows(TransactionCanceledException.class, () ->
|
||||
service.transactWriteItems(List.of(put), "us-east-1", "tok-cancel", rawRequest));
|
||||
assertThrows(TransactionCanceledException.class, () ->
|
||||
service.transactWriteItems(List.of(put), "us-east-1", "tok-cancel", rawRequest));
|
||||
|
||||
service.putItem("Users", item("userId", "u1"), "us-east-1");
|
||||
assertFalse(service.transactWriteItems(List.of(put), "us-east-1", "tok-cancel", rawRequest).replayed());
|
||||
assertTrue(service.transactWriteItems(List.of(put), "us-east-1", "tok-cancel", rawRequest).replayed());
|
||||
}
|
||||
|
||||
@Test
|
||||
void batchWriteItem_flushesOncePerTable_notPerItem() {
|
||||
@SuppressWarnings("unchecked")
|
||||
|
||||
Reference in new issue
Block a user