From d7a3461fbc0bd57e5bd273d08573354ff439400d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?TATSUNO=20=E2=80=9CTaz=E2=80=9D=20Yasuhiro?= Date: Wed, 4 Nov 2026 11:09:18 +0900 Subject: [PATCH] perf(cloudfront): skip the host lookup when no distribution exists (#7467) * perf(cloudfront): skip the host lookup when no distribution exists CloudFrontDistributionFilter scanned the distribution store on every request of every service. CloudFrontService.hasDistributions() lets the filter return at once while the store is empty, which is what findByHost answered then anyway. * perf(cloudfront): keep the distribution check free once one exists AccountAwareStorageBackend.isEmpty() copies every key once the store holds an entry, so a distribution in any account made each request pay that copy before the same host scan as before. The filter only needs to know whether any distribution exists, which the backing store answers without a copy. --- .../core/storage/AccountAwareStorageBackend.java | 5 +++++ .../cloudfront/CloudFrontDistributionFilter.java | 7 ++++++- .../services/cloudfront/CloudFrontService.java | 7 ++++++- .../storage/AccountAwareStorageBackendTest.java | 16 ++++++++++++++++ 4 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/github/hectorvent/floci/core/storage/AccountAwareStorageBackend.java b/src/main/java/io/github/hectorvent/floci/core/storage/AccountAwareStorageBackend.java index d2429c09d1..496507fa13 100644 --- a/src/main/java/io/github/hectorvent/floci/core/storage/AccountAwareStorageBackend.java +++ b/src/main/java/io/github/hectorvent/floci/core/storage/AccountAwareStorageBackend.java @@ -177,6 +177,11 @@ public class AccountAwareStorageBackend implements StorageBackend return delegate.scan(k -> true); } + /** Whether no account holds a value. */ + public boolean isEmptyInAllAccounts() { + return delegate.isEmpty(); + } + /** Removes matching values using their actual stored keys, including unprefixed legacy keys. */ public int deleteAllAccountsMatching(Predicate valueFilter) { int removed = 0; diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontDistributionFilter.java b/src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontDistributionFilter.java index 4275295f35..04491c76b4 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontDistributionFilter.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontDistributionFilter.java @@ -55,7 +55,12 @@ public class CloudFrontDistributionFilter implements ContainerRequestFilter { return; } - Distribution dist = service.get().findByHost(host); + CloudFrontService cloudFront = service.get(); + if (!cloudFront.hasDistributions()) { + return; + } + + Distribution dist = cloudFront.findByHost(host); if (dist == null) { return; } diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontService.java b/src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontService.java index 0380f47d16..d6b0459f11 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontService.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontService.java @@ -10,6 +10,7 @@ import io.github.hectorvent.floci.core.common.CloudFrontEdgeDomain; import io.github.hectorvent.floci.core.common.RegionResolver; import io.github.hectorvent.floci.core.common.SmithyValidationErrors; import io.github.hectorvent.floci.core.common.dns.EmbeddedDnsServer; +import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend; import io.github.hectorvent.floci.core.storage.StorageBackend; import io.github.hectorvent.floci.core.storage.StorageFactory; import io.github.hectorvent.floci.services.acm.AcmCertificateReferenceProvider.CertificateReference; @@ -119,7 +120,7 @@ public class CloudFrontService implements ServerCertificateReferenceProvider { private static final Map MANAGED_CACHE_POLICIES = managedCachePolicies(); private static final String MANAGED_POLICY_ETAG = "E23ZP02F085DFQ"; - private final StorageBackend distStore; + private final AccountAwareStorageBackend distStore; private final StorageBackend> invalidationStore; private final StorageBackend cachePolicyStore; private final StorageBackend orpStore; @@ -560,6 +561,10 @@ public class CloudFrontService implements ServerCertificateReferenceProvider { distStore.put(targetDistributionId, dist); } + public boolean hasDistributions() { + return !distStore.isEmptyInAllAccounts(); + } + /** * Finds the distribution whose data-plane requests should be served for the given {@code Host} * header. A distribution matches when the host equals its assigned CloudFront domain name diff --git a/src/test/java/io/github/hectorvent/floci/core/storage/AccountAwareStorageBackendTest.java b/src/test/java/io/github/hectorvent/floci/core/storage/AccountAwareStorageBackendTest.java index 30d01a21d1..92b5bc46bc 100644 --- a/src/test/java/io/github/hectorvent/floci/core/storage/AccountAwareStorageBackendTest.java +++ b/src/test/java/io/github/hectorvent/floci/core/storage/AccountAwareStorageBackendTest.java @@ -52,6 +52,22 @@ class AccountAwareStorageBackendTest { assertTrue(second.isEmpty()); } + @Test + void isEmptyInAllAccountsSeesEveryAccount() { + InMemoryStorage raw = new InMemoryStorage<>(); + AccountAwareStorageBackend first = + new AccountAwareStorageBackend<>(raw, null, "111111111111"); + AccountAwareStorageBackend second = + new AccountAwareStorageBackend<>(raw, null, "222222222222"); + + assertTrue(second.isEmptyInAllAccounts()); + + first.put("resource", "value"); + + assertTrue(second.isEmpty()); + assertFalse(second.isEmptyInAllAccounts()); + } + @Test void putAllPrefixesEveryEntryWithTheRequestScope() { InMemoryStorage raw = new InMemoryStorage<>();