From 772dcc3b0d3cb9a6321ef4f73209f2b99ba26382 Mon Sep 17 00:00:00 2001 From: joecqupt Date: Sat, 21 Nov 2026 00:08:57 +0800 Subject: [PATCH 1/3] gcp-observability: Cache log filter decisions by RPC method Reuse filter parameters for repeated calls to the same method. Keep separate bounded caches for client and server rules so dynamic method names cannot grow either cache without limit. --- .../interceptors/ConfigFilterHelper.java | 30 ++++++++++++++----- .../interceptors/ConfigFilterHelperTest.java | 9 ++++++ 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/gcp-observability/src/main/java/io/grpc/gcp/observability/interceptors/ConfigFilterHelper.java b/gcp-observability/src/main/java/io/grpc/gcp/observability/interceptors/ConfigFilterHelper.java index 9b05634dbfe..85816c0bd4e 100644 --- a/gcp-observability/src/main/java/io/grpc/gcp/observability/interceptors/ConfigFilterHelper.java +++ b/gcp-observability/src/main/java/io/grpc/gcp/observability/interceptors/ConfigFilterHelper.java @@ -20,6 +20,8 @@ import com.google.auto.value.AutoValue; import com.google.common.annotations.VisibleForTesting; +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; import io.grpc.Internal; import io.grpc.gcp.observability.ObservabilityConfig; import io.grpc.gcp.observability.ObservabilityConfig.LogFilter; @@ -33,7 +35,13 @@ public class ConfigFilterHelper { public static final FilterParams NO_FILTER_PARAMS = FilterParams.create(false, 0, 0); + private static final int MAX_CACHED_METHODS = 1024; + private final ObservabilityConfig config; + private final Cache clientFilterParamsCache = + CacheBuilder.newBuilder().maximumSize(MAX_CACHED_METHODS).build(); + private final Cache serverFilterParamsCache = + CacheBuilder.newBuilder().maximumSize(MAX_CACHED_METHODS).build(); private ConfigFilterHelper(ObservabilityConfig config) { this.config = config; @@ -61,28 +69,34 @@ public static ConfigFilterHelper getInstance(ObservabilityConfig config) { * (log field will be set to true) 2. values of payload limits retrieved from configuration */ public FilterParams logRpcMethod(String fullMethodName, boolean client) { - FilterParams params = NO_FILTER_PARAMS; - - int index = checkNotNull(fullMethodName, "fullMethodName").lastIndexOf('/'); - String serviceName = fullMethodName.substring(0, index); - + checkNotNull(fullMethodName, "fullMethodName"); + Cache cache = client ? clientFilterParamsCache : serverFilterParamsCache; + FilterParams cached = cache.getIfPresent(fullMethodName); + if (cached != null) { + return cached; + } List logFilters = client ? config.getClientLogFilters() : config.getServerLogFilters(); + FilterParams params = matchFilters(fullMethodName, logFilters); + cache.put(fullMethodName, params); + return params; + } - // TODO (dnvindhya): Optimize by caching results for fullMethodName. + private static FilterParams matchFilters(String fullMethodName, List logFilters) { + String serviceName = fullMethodName.substring(0, fullMethodName.lastIndexOf('/')); for (LogFilter logFilter : logFilters) { if (logFilter.matchAll || logFilter.services.contains(serviceName) || logFilter.methods.contains(fullMethodName)) { if (logFilter.excludePattern) { - return params; + return NO_FILTER_PARAMS; } int currentHeaderBytes = logFilter.headerBytes; int currentMessageBytes = logFilter.messageBytes; return FilterParams.create(true, currentHeaderBytes, currentMessageBytes); } } - return params; + return NO_FILTER_PARAMS; } /** diff --git a/gcp-observability/src/test/java/io/grpc/gcp/observability/interceptors/ConfigFilterHelperTest.java b/gcp-observability/src/test/java/io/grpc/gcp/observability/interceptors/ConfigFilterHelperTest.java index 971e6070777..8497b7a12f2 100644 --- a/gcp-observability/src/test/java/io/grpc/gcp/observability/interceptors/ConfigFilterHelperTest.java +++ b/gcp-observability/src/test/java/io/grpc/gcp/observability/interceptors/ConfigFilterHelperTest.java @@ -146,4 +146,13 @@ public void checkMethodToBeLoggedConditional() { = configFilterHelper.logRpcMethod("service4/method3", false); assertThat(serverResultParams).isEqualTo(excludeParams); } + + @Test + public void reusesFilterParamsForSameMethod() { + when(mockConfig.getClientLogFilters()).thenReturn(configLogFilters); + + String method = "service1/Method2"; + FilterParams filterParams = configFilterHelper.logRpcMethod(method, true); + assertThat(configFilterHelper.logRpcMethod(method, true)).isSameInstanceAs(filterParams); + } } From ab5d943f799f2da6ed2f56480fc6784ea66e0da9 Mon Sep 17 00:00:00 2001 From: joecqupt Date: Sat, 21 Nov 2026 00:47:43 +0800 Subject: [PATCH 2/3] gcp-observability: Use a bounded LinkedHashMap for log filters Use access order to evict the least recently used method. Synchronize map access and keep filter matching outside the lock because concurrent misses compute equivalent results. --- .../interceptors/ConfigFilterHelper.java | 33 ++++++++++++++----- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/gcp-observability/src/main/java/io/grpc/gcp/observability/interceptors/ConfigFilterHelper.java b/gcp-observability/src/main/java/io/grpc/gcp/observability/interceptors/ConfigFilterHelper.java index 85816c0bd4e..b8634dcb50c 100644 --- a/gcp-observability/src/main/java/io/grpc/gcp/observability/interceptors/ConfigFilterHelper.java +++ b/gcp-observability/src/main/java/io/grpc/gcp/observability/interceptors/ConfigFilterHelper.java @@ -20,12 +20,13 @@ import com.google.auto.value.AutoValue; import com.google.common.annotations.VisibleForTesting; -import com.google.common.cache.Cache; -import com.google.common.cache.CacheBuilder; import io.grpc.Internal; import io.grpc.gcp.observability.ObservabilityConfig; import io.grpc.gcp.observability.ObservabilityConfig.LogFilter; +import java.util.Collections; +import java.util.LinkedHashMap; import java.util.List; +import java.util.Map; /** * Parses gRPC GcpObservability configuration filters for interceptors usage. @@ -38,10 +39,10 @@ public class ConfigFilterHelper { private static final int MAX_CACHED_METHODS = 1024; private final ObservabilityConfig config; - private final Cache clientFilterParamsCache = - CacheBuilder.newBuilder().maximumSize(MAX_CACHED_METHODS).build(); - private final Cache serverFilterParamsCache = - CacheBuilder.newBuilder().maximumSize(MAX_CACHED_METHODS).build(); + private final Map clientFilterParamsCache = + Collections.synchronizedMap(new LruCache<>(MAX_CACHED_METHODS)); + private final Map serverFilterParamsCache = + Collections.synchronizedMap(new LruCache<>(MAX_CACHED_METHODS)); private ConfigFilterHelper(ObservabilityConfig config) { this.config = config; @@ -70,14 +71,15 @@ public static ConfigFilterHelper getInstance(ObservabilityConfig config) { */ public FilterParams logRpcMethod(String fullMethodName, boolean client) { checkNotNull(fullMethodName, "fullMethodName"); - Cache cache = client ? clientFilterParamsCache : serverFilterParamsCache; - FilterParams cached = cache.getIfPresent(fullMethodName); + Map cache = client ? clientFilterParamsCache : serverFilterParamsCache; + FilterParams cached = cache.get(fullMethodName); if (cached != null) { return cached; } List logFilters = client ? config.getClientLogFilters() : config.getServerLogFilters(); FilterParams params = matchFilters(fullMethodName, logFilters); + // Recomputing the same result on concurrent cache misses is harmless. cache.put(fullMethodName, params); return params; } @@ -99,6 +101,21 @@ private static FilterParams matchFilters(String fullMethodName, List return NO_FILTER_PARAMS; } + @SuppressWarnings("serial") + private static final class LruCache extends LinkedHashMap { + private final int maxSize; + + LruCache(int maxSize) { + super(16, 0.75f, true); + this.maxSize = maxSize; + } + + @Override + protected boolean removeEldestEntry(Map.Entry eldest) { + return size() > maxSize; + } + } + /** * Class containing results for method/service filter information, such as flag for logging * method/service and payload limits to be used for filtering. From aa8c2d125bdaf593f26550950505212c9d088ae5 Mon Sep 17 00:00:00 2001 From: joecqupt Date: Sat, 21 Nov 2026 21:52:33 +0800 Subject: [PATCH 3/3] gcp-observability: Use a bounded FIFO cache for log filters Use ConcurrentHashMap for lookups so cache hits avoid the shared LRU lock. Serialize misses and maintain insertion order to bound each cache and reuse results from concurrent misses. --- .../interceptors/ConfigFilterHelper.java | 68 +++++++++++-------- 1 file changed, 41 insertions(+), 27 deletions(-) diff --git a/gcp-observability/src/main/java/io/grpc/gcp/observability/interceptors/ConfigFilterHelper.java b/gcp-observability/src/main/java/io/grpc/gcp/observability/interceptors/ConfigFilterHelper.java index b8634dcb50c..cd97056ec68 100644 --- a/gcp-observability/src/main/java/io/grpc/gcp/observability/interceptors/ConfigFilterHelper.java +++ b/gcp-observability/src/main/java/io/grpc/gcp/observability/interceptors/ConfigFilterHelper.java @@ -16,6 +16,7 @@ package io.grpc.gcp.observability.interceptors; +import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; import com.google.auto.value.AutoValue; @@ -23,10 +24,12 @@ import io.grpc.Internal; import io.grpc.gcp.observability.ObservabilityConfig; import io.grpc.gcp.observability.ObservabilityConfig.LogFilter; -import java.util.Collections; -import java.util.LinkedHashMap; +import java.util.ArrayDeque; import java.util.List; -import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; +import java.util.function.Function; +import javax.annotation.concurrent.GuardedBy; /** * Parses gRPC GcpObservability configuration filters for interceptors usage. @@ -38,14 +41,14 @@ public class ConfigFilterHelper { private static final int MAX_CACHED_METHODS = 1024; - private final ObservabilityConfig config; - private final Map clientFilterParamsCache = - Collections.synchronizedMap(new LruCache<>(MAX_CACHED_METHODS)); - private final Map serverFilterParamsCache = - Collections.synchronizedMap(new LruCache<>(MAX_CACHED_METHODS)); + private final FifoCache clientFilterParamsCache; + private final FifoCache serverFilterParamsCache; private ConfigFilterHelper(ObservabilityConfig config) { - this.config = config; + clientFilterParamsCache = new FifoCache<>(MAX_CACHED_METHODS, + method -> matchFilters(method, config.getClientLogFilters())); + serverFilterParamsCache = new FifoCache<>(MAX_CACHED_METHODS, + method -> matchFilters(method, config.getServerLogFilters())); } /** @@ -71,17 +74,9 @@ public static ConfigFilterHelper getInstance(ObservabilityConfig config) { */ public FilterParams logRpcMethod(String fullMethodName, boolean client) { checkNotNull(fullMethodName, "fullMethodName"); - Map cache = client ? clientFilterParamsCache : serverFilterParamsCache; - FilterParams cached = cache.get(fullMethodName); - if (cached != null) { - return cached; - } - List logFilters = - client ? config.getClientLogFilters() : config.getServerLogFilters(); - FilterParams params = matchFilters(fullMethodName, logFilters); - // Recomputing the same result on concurrent cache misses is harmless. - cache.put(fullMethodName, params); - return params; + FifoCache cache = + client ? clientFilterParamsCache : serverFilterParamsCache; + return cache.get(fullMethodName); } private static FilterParams matchFilters(String fullMethodName, List logFilters) { @@ -101,18 +96,37 @@ private static FilterParams matchFilters(String fullMethodName, List return NO_FILTER_PARAMS; } - @SuppressWarnings("serial") - private static final class LruCache extends LinkedHashMap { + private static final class FifoCache { private final int maxSize; + private final Function loader; + private final ConcurrentMap entries = new ConcurrentHashMap<>(); + @GuardedBy("this") + private final ArrayDeque insertionOrder = new ArrayDeque<>(); - LruCache(int maxSize) { - super(16, 0.75f, true); + FifoCache(int maxSize, Function loader) { + checkArgument(maxSize > 0, "maxSize must be positive"); this.maxSize = maxSize; + this.loader = checkNotNull(loader, "loader"); + } + + V get(K key) { + V value = entries.get(key); + return value != null ? value : load(key); } - @Override - protected boolean removeEldestEntry(Map.Entry eldest) { - return size() > maxSize; + // Keep eviction and insertion together while allowing cache hits to proceed concurrently. + private synchronized V load(K key) { + V value = entries.get(key); + if (value != null) { + return value; + } + value = checkNotNull(loader.apply(key), "loader returned null"); + if (insertionOrder.size() == maxSize) { + entries.remove(insertionOrder.removeFirst()); + } + entries.put(key, value); + insertionOrder.addLast(key); + return value; } }