fix(logs): paginate log group descriptions (#6142)

* fix(logs): paginate log group descriptions

* fix(logs): keep group paging and validation in service

Preserve legacy ordered callers and share the existing bad-token error. Render out-of-range limits with SmithyValidationErrors.

For 1000 reverse-ordered groups, a 50-group page now reads names 2199 times instead of 4197 in each of three samples. Keep a deterministic budget regression for the redundant sort.
This commit is contained in:
Aniket Shukla authored and GitHub committed 2026-10-21 12:07:52 -04:00
1 parent 4fa11104b4
commit 804b1d9597
5 files changed
+273 -6

No files matched your search

@@ -0,0 +1,71 @@
package io.github.hectorvent.floci.services.cloudwatch.logs;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
import io.github.hectorvent.floci.services.cloudwatch.logs.model.LogGroup;
import io.github.hectorvent.floci.services.cloudwatch.metrics.CloudWatchMetricsService;
import jakarta.ws.rs.core.Response;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Predicate;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
class CloudWatchLogsGroupPageSortTest {
@Test
void aLargePageStaysWithinOneSortsNameReadBudget() {
AtomicInteger nameReads = new AtomicInteger();
List<LogGroup> groups = new ArrayList<>();
for (int i = 999; i >= 0; i--) {
LogGroup group = new LogGroup() {
@Override
public String getLogGroupName() {
nameReads.incrementAndGet();
return super.getLogGroupName();
}
};
group.setLogGroupName(String.format("group-%03d", i));
groups.add(group);
}
InMemoryStorage<String, LogGroup> groupStore = new InMemoryStorage<>() {
@Override
public List<LogGroup> scan(Predicate<String> keyFilter) {
return new ArrayList<>(groups);
}
};
RegionResolver resolver = new RegionResolver("us-east-1", "000000000000");
CloudWatchLogsService service = new CloudWatchLogsService(groupStore,
new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(),
10_000, resolver);
ObjectMapper mapper = new ObjectMapper();
CloudWatchLogsHandler handler = new CloudWatchLogsHandler(service,
new CloudWatchLogsCrossAccountService(new InMemoryStorage<>(), new InMemoryStorage<>(), resolver, mapper),
new CloudWatchLogsMetricFilterService(service, mock(CloudWatchMetricsService.class), resolver), mapper);
List<Integer> measurements = new ArrayList<>();
for (int sample = 0; sample < 3; sample++) {
nameReads.set(0);
try (Response response = handler.handle("DescribeLogGroups", mapper.createObjectNode(), "us-east-1")) {
ObjectNode entity = (ObjectNode) response.getEntity();
assertEquals(50, entity.path("logGroups").size());
assertEquals("group-000", entity.path("logGroups").get(0).path("logGroupName").asText());
assertEquals("group-049", entity.path("logGroups").get(49).path("logGroupName").asText());
assertTrue(entity.has("nextToken"));
}
measurements.add(nameReads.get());
}
// A single TimSort run over 1000 reverse-ordered names takes about 2000 reads; allow page rendering too.
assertTrue(measurements.stream().allMatch(reads -> reads < 3000),
"Redundant full-list sorting exceeds the name-read budget: " + measurements);
List<LogGroup> unpaged = service.describeLogGroups(null, "us-east-1");
assertEquals("group-000", unpaged.getFirst().getLogGroupName());
assertEquals("group-999", unpaged.getLast().getLogGroupName());
}
}
@@ -0,0 +1,156 @@
package io.github.hectorvent.floci.services.cloudwatch.logs;
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.response.Response;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@QuarkusTest
class CloudWatchLogsGroupPaginationIntegrationTest {
private final String prefix = "/floci/group-pages/" + UUID.randomUUID() + "/";
private final List<String> groups = new ArrayList<>();
@BeforeAll
static void configureContentTypes() {
RestAssuredJsonUtils.configureAwsContentTypes();
}
@AfterEach
void deleteOwnedGroups() {
for (String group : groups) {
logs("DeleteLogGroup", Map.of("logGroupName", group)).then().statusCode(200);
}
}
@Test
void traversesAsciiSortedPagesWithoutRepeatingOrSkippingGroups() {
createGroups(3);
List<String> found = new ArrayList<>();
String token = null;
for (int page = 0; page < 3; page++) {
Map<String, Object> request = new HashMap<>(Map.of("logGroupNamePrefix", prefix, "limit", 1));
if (token != null) {
request.put("nextToken", token);
}
Response response = logs("DescribeLogGroups", request).then().statusCode(200).extract().response();
List<String> names = response.jsonPath().getList("logGroups.logGroupName", String.class);
assertEquals(1, names.size());
found.addAll(names);
token = response.path("nextToken");
if (page < 2) {
assertNotNull(token);
} else {
assertFalse(response.jsonPath().getMap("$").containsKey("nextToken"));
}
}
assertEquals(groups.stream().sorted().toList(), found);
}
@Test
void defaultsToFiftyGroupsAndReturnsTheRemainder() {
createGroups(51);
Response first = logs("DescribeLogGroups", Map.of("logGroupNamePrefix", prefix))
.then().statusCode(200).extract().response();
assertEquals(50, first.jsonPath().getList("logGroups").size());
String token = first.path("nextToken");
assertNotNull(token);
Response last = logs("DescribeLogGroups", Map.of("logGroupNamePrefix", prefix, "nextToken", token))
.then().statusCode(200).extract().response();
assertEquals(List.of(groups.stream().sorted().toList().get(50)),
last.jsonPath().getList("logGroups.logGroupName", String.class));
assertFalse(last.jsonPath().getMap("$").containsKey("nextToken"));
}
@ParameterizedTest
@ValueSource(strings = {"0", "-1", "51", "1.5", "\"one\"", "999999999999999999999"})
void rejectsInvalidLimits(String limit) {
given().contentType("application/x-amz-json-1.1")
.header("X-Amz-Target", "Logs_20140328.DescribeLogGroups")
.body("{\"limit\":" + limit + "}").post("/").then().statusCode(400)
.body("__type", equalTo("InvalidParameterException"));
}
@ParameterizedTest
@CsvSource({"0, greater than or equal to 1", "51, less than or equal to 50"})
void reportsOutOfRangeLimitsWithSmithyValidation(int limit, String constraint) {
logs("DescribeLogGroups", Map.of("limit", limit)).then().statusCode(400)
.body("__type", equalTo("InvalidParameterException"))
.body("message", equalTo("1 validation error detected: Value '" + limit
+ "' at 'limit' failed to satisfy constraint: Member must have value " + constraint));
}
@ParameterizedTest
@ValueSource(strings = {"\"not-base64!\"", "\"d3Jvbmctb3BlcmF0aW9u\"", "\"\"", "123", "{}"})
void rejectsInvalidTokens(String token) {
given().contentType("application/x-amz-json-1.1")
.header("X-Amz-Target", "Logs_20140328.DescribeLogGroups")
.body("{\"nextToken\":" + token + "}").post("/").then().statusCode(400)
.body("__type", equalTo("InvalidParameterException"))
.body("message", equalTo("The specified nextToken is invalid."));
}
@Test
void reportsBadTokensLikeDescribeLogStreams() {
createGroups(1);
String group = groups.getFirst();
logs("CreateLogStream", Map.of("logGroupName", group, "logStreamName", "stream"))
.then().statusCode(200);
String streamError = logs("DescribeLogStreams", Map.of("logGroupName", group, "nextToken", "bad!"))
.then().statusCode(400).extract().path("message");
logs("DescribeLogGroups", Map.of("nextToken", "bad!")).then().statusCode(400)
.body("__type", equalTo("InvalidParameterException"))
.body("message", equalTo(streamError));
}
@Test
void resumesAfterADeletedCursorAndHandlesEmptyResults() {
createGroups(3);
Response first = logs("DescribeLogGroups", Map.of("logGroupNamePrefix", prefix, "limit", 1))
.then().statusCode(200).extract().response();
String cursorGroup = first.path("logGroups[0].logGroupName");
String token = first.path("nextToken");
assertNotNull(token);
logs("DeleteLogGroup", Map.of("logGroupName", cursorGroup)).then().statusCode(200);
groups.remove(cursorGroup);
Response next = logs("DescribeLogGroups", Map.of("logGroupNamePrefix", prefix, "nextToken", token))
.then().statusCode(200).extract().response();
assertEquals(groups.stream().sorted().toList(),
next.jsonPath().getList("logGroups.logGroupName", String.class));
assertFalse(next.jsonPath().getMap("$").containsKey("nextToken"));
Response empty = logs("DescribeLogGroups", Map.of("logGroupNamePrefix", prefix + "missing"))
.then().statusCode(200).extract().response();
assertEquals(List.of(), empty.jsonPath().getList("logGroups"));
assertFalse(empty.jsonPath().getMap("$").containsKey("nextToken"));
}
private void createGroups(int count) {
for (int i = count - 1; i >= 0; i--) {
String group = prefix + String.format("%03d", i);
logs("CreateLogGroup", Map.of("logGroupName", group)).then().statusCode(200);
groups.add(group);
}
}
private Response logs(String action, Map<String, ?> body) {
return given().contentType("application/x-amz-json-1.1")
.header("X-Amz-Target", "Logs_20140328." + action).body(body).post("/");
}
}