fix(dynamodb): fix GSI query pagination infinite loop when items share sort key (#637)

When paginating a GSI query where multiple items have the same index sort key,
LastEvaluatedKey only contained the index keys (GSI1PK + GSI1SK), making it
impossible to distinguish between items on the same "page boundary". This caused
ExclusiveStartKey matching to never advance past that boundary, looping forever.

Fix: include the base-table PK and SK in LastEvaluatedKey for index queries,
and prefer base-table keys when matching ExclusiveStartKey (AWS SDK behaviour).

Closes #587
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-04-28 22:36:08 -05:00
1 parent 94d7f411f2
commit 77db7d24ef
2 files changed
+158 -4

No files matched your search

@@ -1,5 +1,7 @@
package io.github.hectorvent.floci.services.dynamodb;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.response.Response;
@@ -9,6 +11,10 @@ import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.zip.CRC32;
import static io.restassured.RestAssured.given;
@@ -1768,4 +1774,129 @@ given()
crc.update(bytes);
return crc.getValue();
}
@Test
@Order(33)
void gsiQueryPaginationWithSharedSortKey() throws Exception {
ObjectMapper mapper = new ObjectMapper();
String tableName = "gsi-pagination-test";
given()
.header("X-Amz-Target", "DynamoDB_20120810.CreateTable")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"KeySchema": [
{"AttributeName": "PK", "KeyType": "HASH"},
{"AttributeName": "SK", "KeyType": "RANGE"}
],
"AttributeDefinitions": [
{"AttributeName": "PK", "AttributeType": "S"},
{"AttributeName": "SK", "AttributeType": "S"},
{"AttributeName": "GSI1PK", "AttributeType": "S"},
{"AttributeName": "GSI1SK", "AttributeType": "S"}
],
"BillingMode": "PAY_PER_REQUEST",
"GlobalSecondaryIndexes": [{
"IndexName": "GSI1",
"KeySchema": [
{"AttributeName": "GSI1PK", "KeyType": "HASH"},
{"AttributeName": "GSI1SK", "KeyType": "RANGE"}
],
"Projection": {"ProjectionType": "ALL"}
}]
}
""".formatted(tableName))
.when().post("/")
.then().statusCode(200);
// 5 items all sharing (GSI1PK="ITEM", GSI1SK="SAME"), unique base-table PK/SK
for (String id : new String[]{"ITEM_a", "ITEM_b", "ITEM_c", "ITEM_d", "ITEM_e"}) {
given()
.header("X-Amz-Target", "DynamoDB_20120810.PutItem")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"Item": {
"PK": {"S": "%s"},
"SK": {"S": "DETAIL"},
"GSI1PK": {"S": "ITEM"},
"GSI1SK": {"S": "SAME"}
}
}
""".formatted(tableName, id))
.when().post("/")
.then().statusCode(200);
}
List<String> allCollected = new ArrayList<>();
Set<String> seenLeks = new HashSet<>();
JsonNode exclusiveStartKey = null;
int pages = 0;
do {
String body;
if (exclusiveStartKey == null) {
body = """
{
"TableName": "%s",
"IndexName": "GSI1",
"KeyConditionExpression": "GSI1PK = :pk",
"ExpressionAttributeValues": {":pk": {"S": "ITEM"}},
"Limit": 2
}
""".formatted(tableName);
} else {
body = """
{
"TableName": "%s",
"IndexName": "GSI1",
"KeyConditionExpression": "GSI1PK = :pk",
"ExpressionAttributeValues": {":pk": {"S": "ITEM"}},
"Limit": 2,
"ExclusiveStartKey": %s
}
""".formatted(tableName, mapper.writeValueAsString(exclusiveStartKey));
}
String responseBody = given()
.header("X-Amz-Target", "DynamoDB_20120810.Query")
.contentType(DYNAMODB_CONTENT_TYPE)
.body(body)
.when().post("/")
.then().statusCode(200).extract().body().asString();
JsonNode root = mapper.readTree(responseBody);
pages++;
for (JsonNode item : root.path("Items")) {
allCollected.add(item.path("PK").path("S").asText());
}
JsonNode lek = root.path("LastEvaluatedKey");
if (lek.isMissingNode() || lek.isNull()) {
exclusiveStartKey = null;
} else {
// LEK must contain all four keys
assertNotNull(lek.get("GSI1PK"), "LastEvaluatedKey missing GSI1PK");
assertNotNull(lek.get("GSI1SK"), "LastEvaluatedKey missing GSI1SK");
assertNotNull(lek.get("PK"), "LastEvaluatedKey missing PK");
assertNotNull(lek.get("SK"), "LastEvaluatedKey missing SK");
// LEK must be unique across pages — cursor must advance
String lekStr = lek.toString();
assertEquals(false, seenLeks.contains(lekStr),
"LastEvaluatedKey repeated — infinite pagination loop: " + lekStr);
seenLeks.add(lekStr);
exclusiveStartKey = lek;
}
} while (exclusiveStartKey != null && pages < 10);
// All 5 distinct items returned exactly once
assertEquals(5, allCollected.size(), "Expected 5 items total, got: " + allCollected);
assertEquals(Set.of("ITEM_a", "ITEM_b", "ITEM_c", "ITEM_d", "ITEM_e"), new HashSet<>(allCollected));
assertEquals(3, pages, "Expected ceil(5/2)=3 pages");
}
}