mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(cloudwatch): bound the datapoints Floci publishes itself (#9348)
The CloudWatch metric store has no size bound, and since SQS publishes AWS/SQS metrics for every active queue each minute it grows with no PutMetricData call, until a persisted cwmetrics.json no longer fits the heap on load. floci.services.cloudwatchmetrics.max-stored-datapoints (default 100000) now caps the datapoints kept per account in each namespace a MinuteMetricsPublisher writes (AWS/SQS, AWS/Redshift). After each tick the publisher drops the oldest over the ceiling, down to nine tenths of it, through the metric key index; other namespaces are never cut, and 0 or less turns the ceiling off.
This commit is contained in:
1 parent
cb573492ca
commit
c40b11fe9a
16 files changed
+328
-17
No files matched your search
+94
@@ -458,6 +458,100 @@ class CloudWatchMetricsServiceTest {
|
||||
60, List.of("Sum"), null, REGION).stream().map(CloudWatchMetricsService.Datapoint::sum).toList());
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteOldestMetricData_cutsOnlyThatNamespaceBackToNineTenthsOfTheCeiling() {
|
||||
long minute = (Instant.now().getEpochSecond() / 60) * 60 - 6000;
|
||||
putOnePerMinute(service, null, "AWS/SQS", minute, 12);
|
||||
putOnePerMinute(service, null, NAMESPACE, minute, 12);
|
||||
|
||||
service.deleteOldestMetricData("AWS/SQS", 10);
|
||||
|
||||
assertEquals(minutesFrom(minute, 3, 12), storedMinutes("AWS/SQS", minute));
|
||||
assertEquals(minutesFrom(minute, 0, 12), storedMinutes(NAMESPACE, minute));
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteOldestMetricData_leavesANamespaceThatHoldsExactlyTheCeiling() {
|
||||
long minute = (Instant.now().getEpochSecond() / 60) * 60 - 6000;
|
||||
putOnePerMinute(service, null, "AWS/SQS", minute, 10);
|
||||
|
||||
service.deleteOldestMetricData("AWS/SQS", 10);
|
||||
|
||||
assertEquals(minutesFrom(minute, 0, 10), storedMinutes("AWS/SQS", minute));
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteOldestMetricData_countsEachAccountOnItsOwn() {
|
||||
AccountAwareStorageBackend<MetricDatum> store = AccountAwareStorageBackend.inMemory("000000000000");
|
||||
CloudWatchMetricsService accountAware = new CloudWatchMetricsService(store, new InMemoryStorage<>(),
|
||||
new RegionResolver("us-east-1", "000000000000"));
|
||||
long minute = (Instant.now().getEpochSecond() / 60) * 60 - 6000;
|
||||
putOnePerMinute(accountAware, "111111111111", "AWS/SQS", minute, 12);
|
||||
putOnePerMinute(accountAware, null, "AWS/SQS", minute, 8);
|
||||
|
||||
accountAware.deleteOldestMetricData("AWS/SQS", 10);
|
||||
|
||||
assertEquals(9, store.scanForAccount("111111111111", k -> true).size());
|
||||
assertEquals(8, store.scan(k -> true).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteOldestMetricData_cutsBackAStoreThatWasLoadedPastTheCeiling() {
|
||||
AccountAwareStorageBackend<MetricDatum> store = AccountAwareStorageBackend.inMemory("000000000000");
|
||||
RegionResolver resolver = new RegionResolver("us-east-1", "000000000000");
|
||||
long minute = (Instant.now().getEpochSecond() / 60) * 60 - 6000;
|
||||
putOnePerMinute(new CloudWatchMetricsService(store, new InMemoryStorage<>(), resolver), null, "AWS/SQS",
|
||||
minute, 30);
|
||||
CloudWatchMetricsService restarted = new CloudWatchMetricsService(store, new InMemoryStorage<>(), resolver);
|
||||
|
||||
restarted.deleteOldestMetricData("AWS/SQS", 10);
|
||||
|
||||
assertEquals(9, store.scan(k -> true).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteOldestMetricData_waitsForTheKeyIndex() {
|
||||
long minute = (Instant.now().getEpochSecond() / 60) * 60 - 6000;
|
||||
putOnePerMinute(service, null, "AWS/SQS", minute, 12);
|
||||
service.beforeReset();
|
||||
|
||||
service.deleteOldestMetricData("AWS/SQS", 10);
|
||||
assertEquals(minutesFrom(minute, 0, 12), storedMinutes("AWS/SQS", minute));
|
||||
|
||||
service.afterReset();
|
||||
service.deleteOldestMetricData("AWS/SQS", 10);
|
||||
assertEquals(minutesFrom(minute, 3, 12), storedMinutes("AWS/SQS", minute));
|
||||
}
|
||||
|
||||
private void putOnePerMinute(CloudWatchMetricsService target, String accountId, String namespace,
|
||||
long firstMinute, int count) {
|
||||
for (int i = 0; i < count; i++) {
|
||||
MetricDatum datum = datum(i % 2 == 0 ? "Sent" : "Received", 1);
|
||||
datum.setTimestamp(firstMinute + 60L * i);
|
||||
target.putMetricDataForAccount(accountId, namespace, List.of(datum), REGION);
|
||||
}
|
||||
}
|
||||
|
||||
/** The minutes for which {@code namespace} still holds a datapoint of either metric, oldest first. */
|
||||
private List<Instant> storedMinutes(String namespace, long firstMinute) {
|
||||
List<Instant> minutes = new ArrayList<>();
|
||||
for (String metric : List.of("Sent", "Received")) {
|
||||
service.getMetricStatistics(namespace, metric, null, Instant.ofEpochSecond(firstMinute),
|
||||
Instant.ofEpochSecond(firstMinute + 6000), 60, List.of("Sum"), null, REGION)
|
||||
.forEach(point -> minutes.add(point.timestamp()));
|
||||
}
|
||||
minutes.sort(null);
|
||||
return minutes;
|
||||
}
|
||||
|
||||
private static List<Instant> minutesFrom(long firstMinute, int from, int to) {
|
||||
List<Instant> minutes = new ArrayList<>();
|
||||
for (int i = from; i < to; i++) {
|
||||
minutes.add(Instant.ofEpochSecond(firstMinute + 60L * i));
|
||||
}
|
||||
return minutes;
|
||||
}
|
||||
|
||||
// ──────────────────────────── GetMetricStatistics ────────────────────────────
|
||||
|
||||
@Test
|
||||
|
||||
@@ -3,6 +3,8 @@ package io.floci.aws.services.cloudwatch.metrics;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
@@ -59,4 +61,34 @@ class MetricKeyIndexTest {
|
||||
assertEquals(List.of(key(SERIES, 60, "b")), index.keysBetween(ACCOUNT, SERIES, 0, 120));
|
||||
assertEquals(List.of(key(otherNamespace, 59, "c")), index.keysBetween(ACCOUNT, otherNamespace, 0, 120));
|
||||
}
|
||||
|
||||
@Test
|
||||
void removeOldest_takesTheOldestKeysAcrossTheNamespacesSeriesUntilTheKeepCountIsLeft() {
|
||||
String received = "us-east-1::AWS/SQS::NumberOfMessagesReceived::QueueName=orders::";
|
||||
String otherNamespace = "us-east-1::AWS/SQSX::NumberOfMessagesSent::QueueName=orders::";
|
||||
MetricKeyIndex index = new MetricKeyIndex();
|
||||
for (String key : List.of(key(SERIES, 10, "a"), key(received, 20, "b"), key(SERIES, 30, "c"),
|
||||
key(received, 40, "d"), key(SERIES, 50, "e"), key(otherNamespace, 1, "f"))) {
|
||||
index.add(ACCOUNT, key);
|
||||
}
|
||||
index.add("111111111111", key(SERIES, 5, "g"));
|
||||
|
||||
Map<String, Set<String>> removed = index.removeOldest("AWS/SQS", 4, 3);
|
||||
|
||||
assertEquals(Map.of(ACCOUNT, Set.of(key(SERIES, 10, "a"), key(received, 20, "b"))), removed);
|
||||
assertEquals(List.of(key(SERIES, 30, "c"), key(SERIES, 50, "e")), index.keysBetween(ACCOUNT, SERIES, 0, 120));
|
||||
assertEquals(List.of(key(received, 40, "d")), index.keysBetween(ACCOUNT, received, 0, 120));
|
||||
assertEquals(List.of(key(otherNamespace, 1, "f")), index.keysBetween(ACCOUNT, otherNamespace, 0, 120));
|
||||
assertEquals(List.of(key(SERIES, 5, "g")), index.keysBetween("111111111111", SERIES, 0, 120));
|
||||
}
|
||||
|
||||
@Test
|
||||
void removeOldest_leavesAnAccountThatHoldsExactlyTheCeiling() {
|
||||
MetricKeyIndex index = new MetricKeyIndex();
|
||||
index.add(ACCOUNT, key(SERIES, 10, "a"));
|
||||
index.add(ACCOUNT, key(SERIES, 20, "b"));
|
||||
|
||||
assertEquals(Map.of(), index.removeOldest("AWS/SQS", 2, 1));
|
||||
assertEquals(2, index.keysBetween(ACCOUNT, SERIES, 0, 120).size());
|
||||
}
|
||||
}
|
||||
+65
-1
@@ -1,5 +1,7 @@
|
||||
package io.floci.aws.services.cloudwatch.metrics;
|
||||
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.core.storage.InMemoryStorage;
|
||||
import io.floci.aws.services.cloudwatch.metrics.model.MetricDatum;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
@@ -13,8 +15,13 @@ import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
@@ -68,8 +75,65 @@ class MinuteMetricsPublisherTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void everyTickCutsTheNamespaceBackToTheCeiling() {
|
||||
publisher(metrics, 500).publish(clock.instant());
|
||||
|
||||
verify(metrics).deleteOldestMetricData("AWS/Test", 500);
|
||||
}
|
||||
|
||||
@Test
|
||||
void aCeilingOfZeroKeepsEveryDatapoint() {
|
||||
publisher(metrics, 0).publish(clock.instant());
|
||||
|
||||
verify(metrics, never()).deleteOldestMetricData(anyString(), anyInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aFailedCutIsTriedAgainTheNextMinute() {
|
||||
doThrow(new IllegalStateException("store closed")).when(metrics).deleteOldestMetricData("AWS/Test", 500);
|
||||
MinuteMetricsPublisher<String> publisher = publisher(metrics, 500);
|
||||
|
||||
publisher.publish(clock.instant());
|
||||
publisher.publish(clock.instant().plusSeconds(60));
|
||||
|
||||
verify(metrics, times(2)).deleteOldestMetricData("AWS/Test", 500);
|
||||
}
|
||||
|
||||
@Test
|
||||
void theCeilingLeavesAClientsOwnNamespaceAlone() {
|
||||
CloudWatchMetricsService stored = new CloudWatchMetricsService(new InMemoryStorage<>(),
|
||||
new InMemoryStorage<>(), new RegionResolver("us-east-1", "111111111111"));
|
||||
Instant start = clock.instant().minusSeconds(86_400);
|
||||
for (int i = 0; i < 3; i++) {
|
||||
stored.putMetricData("MyApp", List.of(MetricDatum.of("Value", "Count", 1, List.of(),
|
||||
start.plusSeconds(60L * i).getEpochSecond())), "us-east-1");
|
||||
}
|
||||
MinuteMetricsPublisher<String> publisher = publisher(stored, 2);
|
||||
|
||||
for (int i = 0; i < 3; i++) {
|
||||
publisher.publish(clock.instant().plusSeconds(60L * i));
|
||||
}
|
||||
|
||||
Instant end = clock.instant().plusSeconds(600);
|
||||
assertEquals(3, stored.getMetricStatistics("MyApp", "Value", null, start, end, 60, List.of("Sum"), null,
|
||||
"us-east-1").size());
|
||||
assertEquals(List.of(Instant.parse("2026-10-27T12:34:00Z"), Instant.parse("2026-10-27T12:35:00Z")),
|
||||
stored.getMetricStatistics("AWS/Test", "Value", null, start, end, 60, List.of("Sum"), null,
|
||||
"us-east-1").stream().map(CloudWatchMetricsService.Datapoint::timestamp).toList());
|
||||
}
|
||||
|
||||
private MinuteMetricsPublisher<String> publisher(boolean enabled) {
|
||||
return new MinuteMetricsPublisher<>(metrics, "AWS/Test", enabled, clock, scheduler) {
|
||||
return publisher(metrics, enabled, 0);
|
||||
}
|
||||
|
||||
private MinuteMetricsPublisher<String> publisher(CloudWatchMetricsService service, int maxStoredDatapoints) {
|
||||
return publisher(service, true, maxStoredDatapoints);
|
||||
}
|
||||
|
||||
private MinuteMetricsPublisher<String> publisher(CloudWatchMetricsService service, boolean enabled,
|
||||
int maxStoredDatapoints) {
|
||||
return new MinuteMetricsPublisher<>(service, "AWS/Test", enabled, maxStoredDatapoints, clock, scheduler) {
|
||||
@Override
|
||||
protected List<String> collectTargets(Instant now) {
|
||||
return List.of("target");
|
||||
|
||||
+1
-1
@@ -41,7 +41,7 @@ class RedshiftMetricsIntegrationTest {
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
publisher = new RedshiftMetricsPublisher(redshift, cloudWatch, collectors, true);
|
||||
publisher = new RedshiftMetricsPublisher(redshift, cloudWatch, collectors, true, 0);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
|
||||
+1
-1
@@ -58,7 +58,7 @@ class RedshiftMetricsPublisherTest {
|
||||
}
|
||||
|
||||
private RedshiftMetricsPublisher publisher(RedshiftMetricCollector... collectors) {
|
||||
return new RedshiftMetricsPublisher(redshift, cloudWatch, List.of(collectors), true);
|
||||
return new RedshiftMetricsPublisher(redshift, cloudWatch, List.of(collectors), true, 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in new issue
Block a user