fix(rds): scope event reconciliation and prune history (#3533)

Co-authored-by: xujiantop-crypto <265865031+xujiantop-crypto@users.noreply.github.com>
This commit is contained in:
XuJianandxujiantop-crypto authored and GitHub committed 2026-09-13 00:58:09 -04:00
1 parent 34efd8957a
commit 04f0c3b7fd
4 files changed
+79 -5

No files matched your search

@@ -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"));
@@ -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());
}