fix(cloudwatchlogs): bound stored events and enforce retention (#3611)

* fix(cloudwatchlogs): cap stored log events and enforce retention on ingest

* docs(cloudwatchlogs): document max-stored-events and the slower log flush interval

* fix(cloudwatchlogs): scope eviction to the ingesting account

Use explicit account storage operations for retention and capacity eviction.

* docs(cloudwatchlogs): note the stored-event ceiling applies per account

* docs(cloudwatchlogs): document that the stored-event ceiling is best-effort under concurrent ingest
This commit is contained in:
Chris Jovanovski authored and GitHub committed 2026-09-14 16:18:45 -04:00
1 parent 1482d45b6e
commit 6ce3fbbd2e
6 files changed
+260 -5

No files matched your search

@@ -108,6 +108,74 @@ class CloudWatchLogsServiceTest {
.count());
}
@Test
void explicitAccountRetentionEvictionLeavesOtherAccountEventsUntouched() {
InMemoryStorage<String, LogGroup> rawGroups = new InMemoryStorage<>();
InMemoryStorage<String, LogStream> rawStreams = new InMemoryStorage<>();
InMemoryStorage<String, LogEvent> rawEvents = new InMemoryStorage<>();
String accountA = "111111111111";
String accountB = "222222222222";
CloudWatchLogsService accountService = new CloudWatchLogsService(
new AccountAwareStorageBackend<>(rawGroups, null, accountB),
new AccountAwareStorageBackend<>(rawStreams, null, accountB),
new AccountAwareStorageBackend<>(rawEvents, null, accountB),
new AccountAwareStorageBackend<>(new InMemoryStorage<>(), null, accountB),
10_000, new RegionResolver(REGION, accountB));
long now = System.currentTimeMillis();
long twoDaysAgo = now - 2 * 86_400_000L;
accountService.createLogGroupForAccount(accountA, "/app/logs", 1, null, REGION);
accountService.createLogStreamForAccount(accountA, "/app/logs", "stream", REGION);
accountService.createLogGroupForAccount(accountB, "/app/logs", null, null, REGION);
accountService.createLogStreamForAccount(accountB, "/app/logs", "stream", REGION);
accountService.putLogEventsForAccount(
accountB, "/app/logs", "stream", eventsAt(twoDaysAgo), REGION);
accountService.putLogEventsForAccount(
accountA, "/app/logs", "stream", eventsAt(twoDaysAgo, now), REGION);
String eventPrefix = REGION + "::/app/logs::stream::";
assertEquals(1, rawEvents.keys().stream()
.filter(key -> key.startsWith(accountA + "/" + eventPrefix))
.count());
assertEquals(1, rawEvents.keys().stream()
.filter(key -> key.startsWith(accountB + "/" + eventPrefix))
.count());
}
@Test
void explicitAccountCapacityEvictionDoesNotCountOtherAccountEvents() {
InMemoryStorage<String, LogGroup> rawGroups = new InMemoryStorage<>();
InMemoryStorage<String, LogStream> rawStreams = new InMemoryStorage<>();
InMemoryStorage<String, LogEvent> rawEvents = new InMemoryStorage<>();
String accountA = "111111111111";
String accountB = "222222222222";
CloudWatchLogsService accountService = new CloudWatchLogsService(
new AccountAwareStorageBackend<>(rawGroups, null, accountB),
new AccountAwareStorageBackend<>(rawStreams, null, accountB),
new AccountAwareStorageBackend<>(rawEvents, null, accountB),
new AccountAwareStorageBackend<>(new InMemoryStorage<>(), null, accountB),
10_000, 2, new RegionResolver(REGION, accountB));
accountService.createLogGroupForAccount(accountA, "/app/logs", null, null, REGION);
accountService.createLogStreamForAccount(accountA, "/app/logs", "stream", REGION);
for (long timestamp : List.of(1L, 2L, 3L)) {
LogEvent event = new LogEvent();
event.setTimestamp(timestamp);
event.setEventId("event-" + timestamp);
rawEvents.put(accountB + "/event-" + timestamp, event);
}
accountService.putLogEventsForAccount(
accountA, "/app/logs", "stream", eventsAt(4L), REGION);
assertEquals(3, rawEvents.keys().stream()
.filter(key -> key.startsWith(accountB + "/"))
.count());
assertEquals(1, rawEvents.keys().stream()
.filter(key -> key.startsWith(accountA + "/"))
.count());
}
@Test
void deleteLogGroup() {
service.createLogGroup("/app/logs", null, null, REGION);
@@ -168,6 +236,89 @@ class CloudWatchLogsServiceTest {
assertNull(group.getRetentionInDays());
}
// ──────────────────────────── Stored event ceiling ────────────────────────────
private static CloudWatchLogsService serviceWithStoredEventCeiling(int maxStoredEvents) {
return new CloudWatchLogsService(
new InMemoryStorage<>(),
new InMemoryStorage<>(),
new InMemoryStorage<>(),
new InMemoryStorage<>(),
10000,
maxStoredEvents,
new RegionResolver("us-east-1", "000000000000")
);
}
private static List<Map<String, Object>> eventsAt(long... timestamps) {
List<Map<String, Object>> events = new ArrayList<>();
for (long timestamp : timestamps) {
events.add(Map.of("timestamp", timestamp, "message", "event@" + timestamp));
}
return events;
}
private static List<Long> storedTimestamps(CloudWatchLogsService target, String group, String stream) {
return target.getLogEvents(group, stream, null, null, 100, true, null, REGION).events().stream()
.map(LogEvent::getTimestamp)
.toList();
}
@Test
void putLogEventsEvictsTheOldestEventsBeyondTheStoredEventCeiling() {
CloudWatchLogsService capped = serviceWithStoredEventCeiling(3);
capped.createLogGroup("/app/logs", null, null, REGION);
capped.createLogStream("/app/logs", "stream-1", REGION);
capped.createLogStream("/app/logs", "stream-2", REGION);
capped.putLogEvents("/app/logs", "stream-1", eventsAt(1000, 2000, 3000), REGION);
capped.putLogEvents("/app/logs", "stream-2", eventsAt(4000, 5000), REGION);
assertEquals(List.of(3000L), storedTimestamps(capped, "/app/logs", "stream-1"),
"the oldest events across every stream go first, the store stays within the ceiling");
assertEquals(List.of(4000L, 5000L), storedTimestamps(capped, "/app/logs", "stream-2"));
}
@Test
void putLogEventsKeepsEverythingWhileUnderTheStoredEventCeiling() {
CloudWatchLogsService capped = serviceWithStoredEventCeiling(3);
capped.createLogGroup("/app/logs", null, null, REGION);
capped.createLogStream("/app/logs", "stream-1", REGION);
capped.putLogEvents("/app/logs", "stream-1", eventsAt(1000, 2000, 3000), REGION);
assertEquals(List.of(1000L, 2000L, 3000L), storedTimestamps(capped, "/app/logs", "stream-1"));
}
@Test
void putLogEventsDropsEventsOlderThanTheGroupRetentionPolicy() {
service.createLogGroup("/app/logs", 1, null, REGION);
service.createLogStream("/app/logs", "stream-1", REGION);
long now = System.currentTimeMillis();
long twoDaysAgo = now - 2 * 86_400_000L;
service.putLogEvents("/app/logs", "stream-1", eventsAt(twoDaysAgo, now), REGION);
assertEquals(List.of(now), storedTimestamps(service, "/app/logs", "stream-1"),
"events past the retention window are not kept on disk waiting for a background sweep");
}
@Test
void putLogEventsLeavesOtherGroupsAloneWhenApplyingRetention() {
service.createLogGroup("/short", 1, null, REGION);
service.createLogStream("/short", "stream-1", REGION);
service.createLogGroup("/forever", null, null, REGION);
service.createLogStream("/forever", "stream-1", REGION);
long now = System.currentTimeMillis();
long twoDaysAgo = now - 2 * 86_400_000L;
service.putLogEvents("/forever", "stream-1", eventsAt(twoDaysAgo), REGION);
service.putLogEvents("/short", "stream-1", eventsAt(twoDaysAgo), REGION);
assertEquals(List.of(twoDaysAgo), storedTimestamps(service, "/forever", "stream-1"));
assertEquals(List.of(), storedTimestamps(service, "/short", "stream-1"));
}
// ──────────────────────────── KMS key association ────────────────────────────
@Test