fix(cloudwatchlogs): persist event batches once (#3767)

* fix(cloudwatchlogs): persist event batches once

* test(storage): cover AccountAware putAll prefixing
This commit is contained in:
Aniket Shukla authored and GitHub committed 2026-09-21 14:53:09 -04:00
1 parent 6a3380d13a
commit bb67a443ce
7 files changed
+121 -1

No files matched your search

@@ -35,6 +35,20 @@ class AccountAwareStorageBackendTest {
assertEquals("111111111111", storage.accountId());
}
@Test
void putAllPrefixesEveryEntryWithTheRequestScope() {
InMemoryStorage<String, String> raw = new InMemoryStorage<>();
AccountAwareStorageBackend<String> storage =
new AccountAwareStorageBackend<>(raw, null, "111111111111");
storage.putAll(Map.of("key1", "value1", "key2", "value2"));
assertEquals("value1", raw.get("111111111111/key1").orElseThrow());
assertEquals("value2", raw.get("111111111111/key2").orElseThrow());
assertTrue(raw.get("key1").isEmpty());
assertTrue(raw.get("key2").isEmpty());
}
@Test
void guardedLegacyLookupMigratesMatchingRawValue() {
InMemoryStorage<String, String> raw = new InMemoryStorage<>();
@@ -43,6 +43,22 @@ class PersistentStorageTest {
assertEquals("value2", store2.get("key2").orElseThrow());
}
@Test
void putAllPersistsEveryEntry() {
Path filePath = tempDir.resolve("batch-persist-test.json");
PersistentStorage<String, String> writer = new PersistentStorage<>(
filePath, new TypeReference<Map<String, String>>() {});
writer.putAll(Map.of("key1", "value1", "key2", "value2", "key3", "value3"));
PersistentStorage<String, String> reader = new PersistentStorage<>(
filePath, new TypeReference<Map<String, String>>() {});
reader.load();
assertEquals("value1", reader.get("key1").orElseThrow());
assertEquals("value2", reader.get("key2").orElseThrow());
assertEquals("value3", reader.get("key3").orElseThrow());
}
@Test
void delete() {
storage.put("key1", "value1");
@@ -818,6 +818,49 @@ class CloudWatchLogsServiceTest {
assertNotNull(stream.getLastIngestionTime());
}
@Test
void putLogEventsStoresTheBatchWithOneBackendWrite() {
String accountId = "111111111111";
CountingStorageBackend<String, LogEvent> rawEvents = new CountingStorageBackend<>();
CloudWatchLogsService batchService = new CloudWatchLogsService(
new AccountAwareStorageBackend<>(new InMemoryStorage<>(), null, "000000000000"),
new AccountAwareStorageBackend<>(new InMemoryStorage<>(), null, "000000000000"),
new AccountAwareStorageBackend<>(rawEvents, null, "000000000000"),
new AccountAwareStorageBackend<>(new InMemoryStorage<>(), null, "000000000000"),
new AccountAwareStorageBackend<>(new InMemoryStorage<>(), null, "000000000000"),
10_000,
new RegionResolver(REGION, "000000000000"));
batchService.createLogGroupForAccount(accountId, "/app/logs", null, null, REGION);
batchService.createLogStreamForAccount(accountId, "/app/logs", "stream-1", REGION);
batchService.putLogEventsForAccount(accountId, "/app/logs", "stream-1", List.of(
Map.of("timestamp", 1_000L, "message", "first"),
Map.of("timestamp", 2_000L, "message", "second"),
Map.of("timestamp", 3_000L, "message", "third")), REGION);
assertEquals(1, rawEvents.putAllCalls);
assertEquals(0, rawEvents.putCalls);
assertEquals(3, rawEvents.keys().size());
assertTrue(rawEvents.keys().stream().allMatch(key -> key.startsWith(accountId + "/")));
}
private static final class CountingStorageBackend<K, V> extends InMemoryStorage<K, V> {
private int putCalls;
private int putAllCalls;
@Override
public void put(K key, V value) {
putCalls++;
super.put(key, value);
}
@Override
public void putAll(Map<K, V> entries) {
putAllCalls++;
entries.forEach((key, value) -> super.put(key, value));
}
}
@Test
void maxEventsPerQueryIsRespected() {
CloudWatchLogsService limitedService = new CloudWatchLogsService(