mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
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:
1 parent
3726b88fc9
commit
846e78db12
5 files changed
+479
-7
No files matched your search
+134
@@ -0,0 +1,134 @@
|
||||
package com.floci.test;
|
||||
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import software.amazon.awssdk.services.cloudwatchlogs.CloudWatchLogsClient;
|
||||
import software.amazon.awssdk.services.cloudwatchlogs.model.DescribeLogStreamsResponse;
|
||||
import software.amazon.awssdk.services.cloudwatchlogs.model.InputLogEvent;
|
||||
import software.amazon.awssdk.services.cloudwatchlogs.model.InvalidParameterException;
|
||||
import software.amazon.awssdk.services.cloudwatchlogs.model.LogStream;
|
||||
import software.amazon.awssdk.services.cloudwatchlogs.model.OrderBy;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
@DisplayName("CloudWatch Logs DescribeLogStreams ordering and pagination")
|
||||
class CloudWatchLogsDescribeStreamsOrderingTest {
|
||||
|
||||
@Test
|
||||
@DisplayName("orderBy LastEventTime descending with limit 1 returns the most recently active stream")
|
||||
void lastEventTimeDescendingWithLimitReturnsMostRecentStream() {
|
||||
String groupName = "/test/" + TestFixtures.uniqueName("logs-describe-streams-order");
|
||||
|
||||
try (CloudWatchLogsClient logs = TestFixtures.cloudWatchLogsClient()) {
|
||||
try {
|
||||
logs.createLogGroup(request -> request.logGroupName(groupName));
|
||||
// Alphabetical order deliberately disagrees with event recency, so a
|
||||
// name-sorted result (the pre-fix behavior) fails the assertion.
|
||||
long base = System.currentTimeMillis();
|
||||
createStreamWithEvent(logs, groupName, "a-oldest", base - 2000);
|
||||
createStreamWithEvent(logs, groupName, "b-newest", base);
|
||||
createStreamWithEvent(logs, groupName, "c-middle", base - 1000);
|
||||
|
||||
DescribeLogStreamsResponse response = logs.describeLogStreams(request -> request
|
||||
.logGroupName(groupName)
|
||||
.orderBy(OrderBy.LAST_EVENT_TIME)
|
||||
.descending(true)
|
||||
.limit(1));
|
||||
|
||||
assertThat(response.logStreams())
|
||||
.as("limit is honored on the wire")
|
||||
.hasSize(1);
|
||||
assertThat(response.logStreams().get(0).logStreamName())
|
||||
.as("descending LastEventTime puts the most recently active stream first")
|
||||
.isEqualTo("b-newest");
|
||||
assertThat(response.nextToken())
|
||||
.as("more streams remain, so a next page is advertised")
|
||||
.isNotNull();
|
||||
} finally {
|
||||
deleteIfPresent(logs, groupName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("the SDK paginator walks every stream once and terminates")
|
||||
void paginatorWalksEveryStreamOnceAndTerminates() {
|
||||
String groupName = "/test/" + TestFixtures.uniqueName("logs-describe-streams-paging");
|
||||
|
||||
try (CloudWatchLogsClient logs = TestFixtures.cloudWatchLogsClient()) {
|
||||
try {
|
||||
logs.createLogGroup(request -> request.logGroupName(groupName));
|
||||
for (int i = 0; i < 5; i++) {
|
||||
int index = i;
|
||||
logs.createLogStream(request -> request
|
||||
.logGroupName(groupName)
|
||||
.logStreamName("stream-" + index));
|
||||
}
|
||||
|
||||
List<String> names = new ArrayList<>();
|
||||
logs.describeLogStreamsPaginator(request -> request
|
||||
.logGroupName(groupName)
|
||||
.limit(2))
|
||||
.stream()
|
||||
.forEach(page -> page.logStreams().stream()
|
||||
.map(LogStream::logStreamName)
|
||||
.forEach(names::add));
|
||||
|
||||
assertThat(names)
|
||||
.as("every stream is seen exactly once, name-ascending by default")
|
||||
.containsExactly("stream-0", "stream-1", "stream-2", "stream-3", "stream-4");
|
||||
} finally {
|
||||
deleteIfPresent(logs, groupName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("orderBy LastEventTime combined with a name prefix is rejected like real AWS")
|
||||
void lastEventTimeWithPrefixIsRejected() {
|
||||
String groupName = "/test/" + TestFixtures.uniqueName("logs-describe-streams-reject");
|
||||
|
||||
try (CloudWatchLogsClient logs = TestFixtures.cloudWatchLogsClient()) {
|
||||
try {
|
||||
logs.createLogGroup(request -> request.logGroupName(groupName));
|
||||
|
||||
assertThatThrownBy(() -> logs.describeLogStreams(request -> request
|
||||
.logGroupName(groupName)
|
||||
.logStreamNamePrefix("stream")
|
||||
.orderBy(OrderBy.LAST_EVENT_TIME)))
|
||||
.as("real AWS rejects LastEventTime ordering with a logStreamNamePrefix")
|
||||
.isInstanceOf(InvalidParameterException.class);
|
||||
} finally {
|
||||
deleteIfPresent(logs, groupName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void createStreamWithEvent(CloudWatchLogsClient logs, String groupName,
|
||||
String streamName, long timestamp) {
|
||||
logs.createLogStream(request -> request
|
||||
.logGroupName(groupName)
|
||||
.logStreamName(streamName));
|
||||
logs.putLogEvents(request -> request
|
||||
.logGroupName(groupName)
|
||||
.logStreamName(streamName)
|
||||
.logEvents(InputLogEvent.builder()
|
||||
.timestamp(timestamp)
|
||||
.message("event for " + streamName)
|
||||
.build()));
|
||||
}
|
||||
|
||||
private static void deleteIfPresent(CloudWatchLogsClient logs, String groupName) {
|
||||
boolean present = logs.describeLogGroups(request -> request.logGroupNamePrefix(groupName))
|
||||
.logGroups()
|
||||
.stream()
|
||||
.anyMatch(group -> groupName.equals(group.logGroupName()));
|
||||
if (present) {
|
||||
logs.deleteLogGroup(request -> request.logGroupName(groupName));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user