Skip to content

xds: float LRU cache across interceptors #11992

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Apr 17, 2025
29 changes: 25 additions & 4 deletions xds/src/main/java/io/grpc/xds/GcpAuthenticationFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@
import io.grpc.xds.MetadataRegistry.MetadataValueParser;
import io.grpc.xds.XdsConfig.XdsClusterConfig;
import io.grpc.xds.client.XdsResourceType.ResourceInvalidException;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ScheduledExecutorService;
import java.util.function.Function;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -102,11 +104,14 @@ public ConfigOrError<GcpAuthenticationConfig> parseFilterConfig(Message rawProto
// Validate cache_config
if (gcpAuthnProto.hasCacheConfig()) {
TokenCacheConfig cacheConfig = gcpAuthnProto.getCacheConfig();
cacheSize = cacheConfig.getCacheSize().getValue();
if (cacheSize == 0) {
return ConfigOrError.fromError(
"cache_config.cache_size must be greater than zero");
if (cacheConfig.hasCacheSize()) {
cacheSize = cacheConfig.getCacheSize().getValue();
if (cacheSize == 0) {
return ConfigOrError.fromError(
"cache_config.cache_size must be greater than zero");
}
}

// LruCache's size is an int and briefly exceeds its maximum size before evicting entries
cacheSize = UnsignedLongs.min(cacheSize, Integer.MAX_VALUE - 1);
}
Expand All @@ -128,6 +133,7 @@ public ClientInterceptor buildClientInterceptor(FilterConfig config,
@Nullable FilterConfig overrideConfig, ScheduledExecutorService scheduler) {

ComputeEngineCredentials credentials = ComputeEngineCredentials.create();
callCredentialsCache.resizeCache(((GcpAuthenticationConfig) config).getCacheSize());
return new ClientInterceptor() {
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
Expand Down Expand Up @@ -254,8 +260,10 @@ public void sendMessage(ReqT message) {}
private static final class LruCache<K, V> {

private final Map<K, V> cache;
private int maxSize;

LruCache(int maxSize) {
this.maxSize = maxSize;
this.cache = new LinkedHashMap<K, V>(
maxSize,
0.75f,
Expand All @@ -270,6 +278,19 @@ protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
V getOrInsert(K key, Function<K, V> create) {
return cache.computeIfAbsent(key, create);
}

private void resizeCache(int newSize) {
while (cache.size() > newSize) {
Iterator<Entry<K, V>> iterator = cache.entrySet().iterator();
if (iterator.hasNext()) {
iterator.next();
iterator.remove();
} else {
break;
}
}
maxSize = newSize;
}
}

static class AudienceMetadataParser implements MetadataValueParser {
Expand Down
Loading