feat(cloudwatch): Adds support for orderBy, pagination, limit in DescribeLogStreams response (#2362)

* feat(cloudwatch): support orderBy, descending, limit and nextToken in DescribeLogStreams

DescribeLogStreams previously ignored orderBy, descending, limit and
nextToken: it always returned every stream in the group sorted
ascending by name, so the common SDK idiom for locating the most
recently active stream (orderBy LastEventTime, descending, limit 1)
silently returned the alphabetically-first stream instead.

Sort by lastEventTimestamp when orderBy=LastEventTime (streams without
events sort oldest, name as tie-break), honor descending, and page with
the same integer-offset nextToken idiom DescribeSubscriptionFilters
uses (limit capped at 50, defaulting to 50). Reject the
LastEventTime + logStreamNamePrefix combination and unknown orderBy
values with InvalidParameterException, matching real AWS.

* test(compat): cover DescribeLogStreams ordering, limit and pagination via the Java SDK

Exercises the new orderBy/descending/limit/nextToken support over the
real wire protocol (AWS SDK for Java 2.52.0): orderBy LastEventTime
descending with limit 1 returns the most recently active stream, the
SDK paginator walks every stream exactly once and terminates, and
LastEventTime combined with logStreamNamePrefix is rejected with
InvalidParameterException as on real AWS.

* fix(cloudwatch): make DescribeLogStreams nextToken a stable cursor and reject malformed tokens

Review feedback on the integer-offset token: a non-numeric nextToken was
silently reset to zero, re-serving the first page and making custom
pagination loops duplicate results or stall; and a positional offset
applied to a freshly scanned collection skipped or repeated streams
whenever creates, deletes, or PutLogEvents reordering shifted positions
between page requests.

The token now encodes the sort key of the last returned stream
(base64 of order, direction, lastEventTimestamp, name) and resumption
takes the first stream strictly after that key in the effective
ordering, so mutations between pages cannot shift the resume point.
Malformed tokens and tokens replayed against a different
orderBy/descending combination throw InvalidParameterException.

* fix(cloudwatch): paginate DescribeLogStreams over a snapshot so reordered streams are never skipped

Review feedback on the sort-key cursor: when an unreturned stream
receives a newer event between descending LastEventTime pages, the next
request sorts it ahead of the saved cursor and skips it, so the
paginator terminates without ever returning the stream. No stateless
token can survive that reordering, so the token now references a
server-side snapshot of the sorted stream names taken when the first
page is served, plus a position in it.

Later pages walk the frozen ordering and re-read each stream live:
streams deleted since the snapshot are dropped and attributes are
current, so every stream that existed when pagination started is
returned exactly once regardless of interleaved creates, deletes or
PutLogEvents. Snapshots are TTL-evicted after 15 minutes; unknown,
expired or malformed tokens, and tokens replayed against a different
group, prefix or ordering, throw InvalidParameterException.
This commit is contained in:
floverfelt authored and GitHub committed 2026-08-18 09:28:19 -04:00
1 parent 3726b88fc9
commit 846e78db12
5 files changed
+479 -7

No files matched your search

@@ -74,6 +74,34 @@ class CloudWatchLogsHandlerTest {
assertEquals(STREAM, streams.get(0).path("logStreamName").asText());
}
@Test
void describeLogStreamsHonorsOrderByDescendingLimitAndReturnsNextToken() {
service.createLogStream(GROUP, "another-stream", REGION);
service.putLogEvents(GROUP, STREAM,
java.util.List.of(java.util.Map.of("timestamp", 2000L, "message", "new")), REGION);
service.putLogEvents(GROUP, "another-stream",
java.util.List.of(java.util.Map.of("timestamp", 1000L, "message", "old")), REGION);
ObjectNode request = MAPPER.createObjectNode()
.put("logGroupName", GROUP)
.put("orderBy", "LastEventTime")
.put("descending", true)
.put("limit", 1);
Response response = handler.handle("DescribeLogStreams", request, REGION);
assertEquals(200, response.getStatus());
ObjectNode entity = (ObjectNode) response.getEntity();
JsonNode streams = entity.path("logStreams");
assertEquals(1, streams.size());
assertEquals(STREAM, streams.get(0).path("logStreamName").asText());
assertTrue(entity.has("nextToken"));
ObjectNode nextRequest = request.deepCopy().put("nextToken", entity.path("nextToken").asText());
ObjectNode nextEntity = (ObjectNode) handler.handle("DescribeLogStreams", nextRequest, REGION).getEntity();
assertEquals("another-stream", nextEntity.path("logStreams").get(0).path("logStreamName").asText());
assertFalse(nextEntity.has("nextToken"));
}
// ──────────────────────────── GetDataProtectionPolicy ────────────────────────────
@Test
@@ -215,6 +215,184 @@ class CloudWatchLogsServiceTest {
assertTrue(service.describeLogStreams("/app/logs", null, REGION).isEmpty());
}
@Test
void describeLogStreamsOrdersByLastEventTimeDescendingWithLimit() {
// The SDK idiom for "find the most recently active stream":
// orderBy(LAST_EVENT_TIME).descending(true).limit(1). Alphabetical order is set up
// to disagree with event recency so a name-sorted result would fail the assertion.
service.createLogGroup("/app/logs", null, null, REGION);
service.createLogStream("/app/logs", "a-oldest", REGION);
service.createLogStream("/app/logs", "b-newest", REGION);
service.createLogStream("/app/logs", "c-middle", REGION);
service.putLogEvents("/app/logs", "a-oldest", List.of(Map.of("timestamp", 1000L, "message", "old")), REGION);
service.putLogEvents("/app/logs", "b-newest", List.of(Map.of("timestamp", 3000L, "message", "new")), REGION);
service.putLogEvents("/app/logs", "c-middle", List.of(Map.of("timestamp", 2000L, "message", "mid")), REGION);
var result = service.describeLogStreams("/app/logs", null, "LastEventTime", true, 1, null, REGION);
assertEquals(1, result.logStreams().size());
assertEquals("b-newest", result.logStreams().getFirst().getLogStreamName());
assertNotNull(result.nextToken());
}
@Test
void describeLogStreamsSortsStreamsWithoutEventsOldestByLastEventTime() {
service.createLogGroup("/app/logs", null, null, REGION);
service.createLogStream("/app/logs", "z-empty", REGION);
service.createLogStream("/app/logs", "a-active", REGION);
service.putLogEvents("/app/logs", "a-active", List.of(Map.of("timestamp", 1000L, "message", "x")), REGION);
var descending = service.describeLogStreams("/app/logs", null, "LastEventTime", true, 0, null, REGION);
assertEquals(List.of("a-active", "z-empty"),
descending.logStreams().stream().map(LogStream::getLogStreamName).toList());
var ascending = service.describeLogStreams("/app/logs", null, "LastEventTime", false, 0, null, REGION);
assertEquals(List.of("z-empty", "a-active"),
ascending.logStreams().stream().map(LogStream::getLogStreamName).toList());
}
@Test
void describeLogStreamsPaginatesWithNextToken() {
service.createLogGroup("/app/logs", null, null, REGION);
service.createLogStream("/app/logs", "stream-1", REGION);
service.createLogStream("/app/logs", "stream-2", REGION);
service.createLogStream("/app/logs", "stream-3", REGION);
var page1 = service.describeLogStreams("/app/logs", null, null, false, 2, null, REGION);
assertEquals(List.of("stream-1", "stream-2"),
page1.logStreams().stream().map(LogStream::getLogStreamName).toList());
assertNotNull(page1.nextToken());
var page2 = service.describeLogStreams("/app/logs", null, null, false, 2, page1.nextToken(), REGION);
assertEquals(List.of("stream-3"),
page2.logStreams().stream().map(LogStream::getLogStreamName).toList());
assertNull(page2.nextToken());
}
@Test
void describeLogStreamsRejectsMalformedNextToken() {
// A garbage token must fail loudly: silently restarting from the first page makes
// custom pagination loops duplicate results or never progress.
service.createLogGroup("/app/logs", null, null, REGION);
service.createLogStream("/app/logs", "stream-1", REGION);
AwsException e = assertThrows(AwsException.class, () ->
service.describeLogStreams("/app/logs", null, null, false, 0, "not-a-token", REGION));
assertEquals("InvalidParameterException", e.getErrorCode());
}
@Test
void describeLogStreamsRejectsTokenFromDifferentOrdering() {
service.createLogGroup("/app/logs", null, null, REGION);
service.createLogStream("/app/logs", "stream-1", REGION);
service.createLogStream("/app/logs", "stream-2", REGION);
String nameOrderToken = service
.describeLogStreams("/app/logs", null, null, false, 1, null, REGION)
.nextToken();
assertNotNull(nameOrderToken);
AwsException e = assertThrows(AwsException.class, () ->
service.describeLogStreams("/app/logs", null, "LastEventTime", true, 1, nameOrderToken, REGION));
assertEquals("InvalidParameterException", e.getErrorCode());
}
@Test
void describeLogStreamsPaginationDoesNotSkipAfterDeletionBetweenPages() {
// A positional offset applied to the re-scanned collection would skip stream-3 here:
// deleting already-returned stream-1 shifts everything left by one. The cursor keeps
// the resume point anchored to the last returned stream instead.
service.createLogGroup("/app/logs", null, null, REGION);
for (int i = 1; i <= 4; i++) {
service.createLogStream("/app/logs", "stream-" + i, REGION);
}
var page1 = service.describeLogStreams("/app/logs", null, null, false, 2, null, REGION);
assertEquals(List.of("stream-1", "stream-2"),
page1.logStreams().stream().map(LogStream::getLogStreamName).toList());
service.deleteLogStream("/app/logs", "stream-1", REGION);
var page2 = service.describeLogStreams("/app/logs", null, null, false, 2, page1.nextToken(), REGION);
assertEquals(List.of("stream-3", "stream-4"),
page2.logStreams().stream().map(LogStream::getLogStreamName).toList());
assertNull(page2.nextToken());
}
@Test
void describeLogStreamsLastEventTimePaginationDoesNotRepeatReorderedStreams() {
// PutLogEvents to an already-returned stream between pages moves it even further
// ahead in descending order. A positional offset would then re-serve the stream at
// the boundary; the cursor never returns anything at-or-before the last seen key.
service.createLogGroup("/app/logs", null, null, REGION);
service.createLogStream("/app/logs", "a", REGION);
service.createLogStream("/app/logs", "b", REGION);
service.createLogStream("/app/logs", "c", REGION);
service.createLogStream("/app/logs", "d", REGION);
service.putLogEvents("/app/logs", "a", List.of(Map.of("timestamp", 1000L, "message", "x")), REGION);
service.putLogEvents("/app/logs", "b", List.of(Map.of("timestamp", 2000L, "message", "x")), REGION);
service.putLogEvents("/app/logs", "c", List.of(Map.of("timestamp", 3000L, "message", "x")), REGION);
service.putLogEvents("/app/logs", "d", List.of(Map.of("timestamp", 4000L, "message", "x")), REGION);
var page1 = service.describeLogStreams("/app/logs", null, "LastEventTime", true, 2, null, REGION);
assertEquals(List.of("d", "c"),
page1.logStreams().stream().map(LogStream::getLogStreamName).toList());
service.putLogEvents("/app/logs", "d", List.of(Map.of("timestamp", 5000L, "message", "x")), REGION);
var page2 = service.describeLogStreams("/app/logs", null, "LastEventTime", true, 2, page1.nextToken(), REGION);
assertEquals(List.of("b", "a"),
page2.logStreams().stream().map(LogStream::getLogStreamName).toList());
assertNull(page2.nextToken());
}
@Test
void describeLogStreamsReturnsUnseenStreamThatReorderedAcrossThePageBoundary() {
// An unreturned stream that receives a newer event between descending LastEventTime
// pages sorts ahead of any saved sort-key cursor on the next request and would be
// skipped forever. The snapshot freezes the ordering at page one, so the stream is
// still returned in its original position.
service.createLogGroup("/app/logs", null, null, REGION);
service.createLogStream("/app/logs", "a", REGION);
service.createLogStream("/app/logs", "b", REGION);
service.createLogStream("/app/logs", "c", REGION);
service.createLogStream("/app/logs", "d", REGION);
service.putLogEvents("/app/logs", "a", List.of(Map.of("timestamp", 1000L, "message", "x")), REGION);
service.putLogEvents("/app/logs", "b", List.of(Map.of("timestamp", 2000L, "message", "x")), REGION);
service.putLogEvents("/app/logs", "c", List.of(Map.of("timestamp", 3000L, "message", "x")), REGION);
service.putLogEvents("/app/logs", "d", List.of(Map.of("timestamp", 4000L, "message", "x")), REGION);
var page1 = service.describeLogStreams("/app/logs", null, "LastEventTime", true, 2, null, REGION);
assertEquals(List.of("d", "c"),
page1.logStreams().stream().map(LogStream::getLogStreamName).toList());
// b was not returned yet; this would now sort it ahead of c, the last returned stream.
service.putLogEvents("/app/logs", "b", List.of(Map.of("timestamp", 9000L, "message", "x")), REGION);
var page2 = service.describeLogStreams("/app/logs", null, "LastEventTime", true, 2, page1.nextToken(), REGION);
assertEquals(List.of("b", "a"),
page2.logStreams().stream().map(LogStream::getLogStreamName).toList());
assertNull(page2.nextToken());
// Attributes are live even though the position is frozen.
assertEquals(9000L, page2.logStreams().getFirst().getLastEventTimestamp());
}
@Test
void describeLogStreamsRejectsLastEventTimeWithPrefix() {
service.createLogGroup("/app/logs", null, null, REGION);
AwsException e = assertThrows(AwsException.class, () ->
service.describeLogStreams("/app/logs", "stream", "LastEventTime", true, 0, null, REGION));
assertEquals("InvalidParameterException", e.getErrorCode());
}
@Test
void describeLogStreamsRejectsUnknownOrderBy() {
service.createLogGroup("/app/logs", null, null, REGION);
AwsException e = assertThrows(AwsException.class, () ->
service.describeLogStreams("/app/logs", null, "CreationTime", false, 0, null, REGION));
assertEquals("InvalidParameterException", e.getErrorCode());
}
@Test
void deleteLogGroupCascadesStreamsAndEvents() {
service.createLogGroup("/app/logs", null, null, REGION);