Light, fluffy, and always free - The AWS Local Emulator alternative
fix(rds): scope event reconciliation and prune history (#3533)
XuJian04f0c3b7fd36
diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/RdsQueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/rds/RdsQueryHandler.javaindex d12ca849ac..417d17eec6 100644--- a/src/main/java/io/github/hectorvent/floci/services/rds/RdsQueryHandler.java+++ b/src/main/java/io/github/hectorvent/floci/services/rds/RdsQueryHandler.java@@ -256,10 +256,12 @@ public class RdsQueryHandler {throw new AwsException("InvalidParameterCombination", "StartTime must be before EndTime.", 400);}- // Reconcile current instance health before returning the event history. This mirrors the- // same control-plane observation used by DescribeDBInstances and records the transition once.- for (DbInstance instance : service.listDbInstances(null, region)) {- service.refreshDbInstanceRuntimeHealth(instance);+ // Only instance events depend on container health. Preserve the unfiltered reconciliation,+ // but do not inspect unrelated instances for a source-scoped or non-instance query.+ if (sourceType == null || "db-instance".equals(sourceType)) {+ for (DbInstance instance : service.listDbInstances(sourceIdentifier, region)) {+ service.refreshDbInstanceRuntimeHealth(instance);+ }}List<RdsEvent> all = service.describeEvents(sourceIdentifier, sourceType, start, end, duration);int offset = parseMarker(params.getFirst("Marker"));diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java b/src/main/java/io/github/hectorvent/floci/services/rds/RdsService.javaindex a4000239b9..0557e375e3 100644--- a/src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java+++ b/src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java@@ -50,6 +50,7 @@ import jakarta.inject.Inject;import org.jboss.logging.Logger;import java.security.SecureRandom;+import java.time.Duration;import java.time.Instant;import java.util.ArrayList;import java.util.Collection;@@ -74,6 +75,7 @@ public class RdsService implements Resettable, ResourceProvider {private static final Logger LOG = Logger.getLogger(RdsService.class);private static final ObjectMapper JSON = new ObjectMapper();+ private static final Duration EVENT_RETENTION = Duration.ofDays(14);/** Value AWS reports as the OwningService of a secret RDS manages. */private static final String MANAGED_SECRET_OWNING_SERVICE = "rds";private static final List<ManagedClusterParameterGroup> MANAGED_CLUSTER_PARAMETER_GROUPS = List.of(@@ -1296,7 +1298,9 @@ public class RdsService implements Resettable, ResourceProvider {public List<RdsEvent> describeEvents(String sourceIdentifier, String sourceType,Instant startTime, Instant endTime, Integer durationMinutes) {- Instant effectiveEnd = endTime != null ? endTime : Instant.now();+ Instant now = Instant.now();+ pruneExpiredEvents(now);+ Instant effectiveEnd = endTime != null ? endTime : now;Instant effectiveStart = startTime != null ? startTime: effectiveEnd.minusSeconds((durationMinutes != null ? durationMinutes : 60) * 60L);return events.scan(_ -> true).stream()@@ -1307,6 +1311,16 @@ public class RdsService implements Resettable, ResourceProvider {.toList();}+ private void pruneExpiredEvents(Instant now) {+ Instant cutoff = now.minus(EVENT_RETENTION);+ for (String eventId : new ArrayList<>(events.keys())) {+ RdsEvent event = events.get(eventId).orElse(null);+ if (event != null && event.date() != null && event.date().isBefore(cutoff)) {+ events.delete(eventId);+ }+ }+ }+public Collection<DbInstance> listDbInstancesByDbiResourceIds(Collection<String> resourceIds) {return listDbInstancesByDbiResourceIds(resourceIds, regionResolver.getDefaultRegion());}diff --git a/src/test/java/io/github/hectorvent/floci/services/rds/RdsQueryHandlerTest.java b/src/test/java/io/github/hectorvent/floci/services/rds/RdsQueryHandlerTest.javaindex 42ab7ac3fd..56ed1edae7 100644--- a/src/test/java/io/github/hectorvent/floci/services/rds/RdsQueryHandlerTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/rds/RdsQueryHandlerTest.java@@ -56,6 +56,39 @@ class RdsQueryHandlerTest {handler = new RdsQueryHandler(service, config, docDbHandler, neptuneHandler);}+ @Test+ void describeEvents_reconcilesOnlyTheRequestedDbInstance() {+ DbInstance requested = makeInstance("requested");+ when(service.listDbInstances("requested", null)).thenReturn(List.of(requested));+ when(service.describeEvents("requested", "db-instance", null, null, null))+ .thenReturn(List.of());+ MultivaluedMap<String, String> p = params();+ p.add("SourceIdentifier", "requested");+ p.add("SourceType", "db-instance");++ Response response = handler.handle("DescribeEvents", p);++ assertEquals(200, response.getStatus());+ verify(service).listDbInstances("requested", null);+ verify(service).refreshDbInstanceRuntimeHealth(requested);+ verify(service, never()).listDbInstances(null, null);+ }++ @Test+ void describeEvents_doesNotReconcileInstancesForAnotherSourceType() {+ when(service.describeEvents("cluster", "db-cluster", null, null, null))+ .thenReturn(List.of());+ MultivaluedMap<String, String> p = params();+ p.add("SourceIdentifier", "cluster");+ p.add("SourceType", "db-cluster");++ Response response = handler.handle("DescribeEvents", p);++ assertEquals(200, response.getStatus());+ verify(service, never()).listDbInstances(any(), any());+ verify(service, never()).refreshDbInstanceRuntimeHealth(any());+ }+// ──────────────────────────── DBInstances XML tag ────────────────────────────@Testdiff --git a/src/test/java/io/github/hectorvent/floci/services/rds/RdsServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/rds/RdsServiceTest.javaindex a7f1d08d78..1dafb7ad8b 100644--- a/src/test/java/io/github/hectorvent/floci/services/rds/RdsServiceTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/rds/RdsServiceTest.java@@ -29,6 +29,7 @@ import io.github.hectorvent.floci.services.rds.model.DbProxyTargetGroup;import io.github.hectorvent.floci.services.rds.model.DbSubnetGroup;import io.github.hectorvent.floci.services.rds.model.OptionGroup;import io.github.hectorvent.floci.services.rds.model.OptionGroupOption;+import io.github.hectorvent.floci.services.rds.model.RdsEvent;import io.github.hectorvent.floci.services.rds.proxy.RdsAuthProxy;import io.github.hectorvent.floci.services.rds.proxy.RdsProxyManager;import io.github.hectorvent.floci.services.secretsmanager.SecretsManagerService;@@ -39,6 +40,8 @@ import org.junit.jupiter.params.ParameterizedTest;import org.junit.jupiter.params.provider.CsvSource;import org.mockito.ArgumentCaptor;+import java.lang.reflect.Field;+import java.time.Duration;import java.time.Instant;import java.util.Collection;import java.util.List;@@ -5541,6 +5544,28 @@ class RdsServiceTest {assertEquals(1, rdsService.describeEvents("dead-db", "db-instance", null, null, 60).size());}+ @Test+ void describeEventsPrunesEventsOlderThanFourteenDays() throws Exception {+ InMemoryStorage<String, RdsEvent> eventStore = new InMemoryStorage<>();+ Field eventsField = RdsService.class.getDeclaredField("events");+ eventsField.setAccessible(true);+ eventsField.set(rdsService, eventStore);+ Instant now = Instant.now();+ RdsEvent expired = new RdsEvent("expired", "old-db", "db-instance", "old",+ List.of("availability"), now.minus(Duration.ofDays(15)), "old-arn");+ RdsEvent retained = new RdsEvent("retained", "recent-db", "db-instance", "recent",+ List.of("availability"), now.minus(Duration.ofDays(13)), "recent-arn");+ eventStore.put(expired.id(), expired);+ eventStore.put(retained.id(), retained);++ List<RdsEvent> result = rdsService.describeEvents(+ null, null, now.minus(Duration.ofDays(20)), now.plusSeconds(1), null);++ assertEquals(List.of(retained), result);+ assertTrue(eventStore.get("retained").isPresent());+ assertTrue(eventStore.get("expired").isEmpty());+ }+@Testvoid optionGroupsUseTheInjectedAccountAwareStorage() {String accountId = "123456789012";