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

Conversation

shivaspeaks
Copy link
Member

No description provided.

@shivaspeaks shivaspeaks requested a review from ejona86 April 7, 2025 18:46
return;
}
LinkedHashMap<K, V> newCache = createEvictingMap(newSize);
newCache.putAll(cache);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because maxSize is only updated after the map is created, this map will contain all the entries from cache.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

createEvictingMap is creating an empty LinkedHashMap. There are separator constructors

LinkedHashMap(int initialCapacity, float loadFactor, boolean accessOrder)
LinkedHashMap(Map<? extends K,? extends V> m)

but none that both take an old map and specify the initialCapacity.

@shivaspeaks shivaspeaks requested a review from ejona86 April 8, 2025 02:27
Copy link
Contributor

@kannanjgithub kannanjgithub left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review comments.

return;
}
LinkedHashMap<K, V> newCache = createEvictingMap(newSize);
newCache.putAll(cache);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

createEvictingMap is creating an empty LinkedHashMap. There are separator constructors

LinkedHashMap(int initialCapacity, float loadFactor, boolean accessOrder)
LinkedHashMap(Map<? extends K,? extends V> m)

but none that both take an old map and specify the initialCapacity.

}

V getOrInsert(K key, Function<K, V> create) {
return cache.computeIfAbsent(key, create);
}

private void resizeCache(int newSize) {
if (newSize >= maxSize) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't understand this. I thought if you get a bigger new size you should resize, and if you get an equal or smaller new size only that should be a no-op because decreasing cache size doesn't make since when another filter instance created a bigger cache.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with your point but well, cache_size is configuration coming externally. We should adhere to the configuration. If the cache size decreases, then we can't use the normal code to discard entries, as that only discards a single entry. So without special code, the configuration wouldn't be observed.

It is generally really bad to not observe configuration updates, from a testing/debugging perspective. You do a config push and see behavior X, but then after restarting you see behavior Y. It can be really hard to track down the bugs.

The cache size doesn't change often, so the performance hit would be temporary (although there are still issues with that). It's also much easier to figure out what happened. If you keep the old cache, then someone may be debugging something and it matters what happened a month ago.

I discussed this with Eric offline on the similar lines and understood this POV.

@shivaspeaks shivaspeaks requested a review from ejona86 April 8, 2025 17:11
CallOptions capturedOptions2 = callOptionsCaptor.getAllValues().get(1);
assertNotNull(capturedOptions2.getCredentials());

assertSame(capturedOptions1.getCredentials(), capturedOptions2.getCredentials());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cached value is tested but not eviction behavior. With cache max size of 1, you need to test using a cluster resource with a 2nd audience string and get the credentials and assert the behavior for the 1st audience string's call credentials getting evicted.

filter max size 2
audience-1 : call-credentials-for-audience-1
filter max size 1
audience-1: call-credentials-1-for-audience-1 (same instance)
audience-2: call-credentials-1-for-audience-2 (causes eviction of call-credentials-1-for-audience-1)
audience-1: call-credentials-2-for-audience-1 (new instance)

Copy link
Contributor

@kannanjgithub kannanjgithub left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add test.

CallOptions options4 = captor.getValue();
assertNotNull(options4.getCredentials());

assertSame(options1.getCredentials(), options2.getCredentials());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These 3 are the only assertions needed in this test. Remove all other assertions as they are asserted in their respective tests and behavior driven unit tests' guidance calls for asserting only the behavior tested by that particular unit test.

Also to make the test more understandable move these assertions to right after the credentials are obtained and are available for assertion.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All asserts should have new line before and after?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we have assertions to do after obtaining call credentials obtained using interceptor 2, 3 and 4 we can think of it as a repeated arrange-act-assert structure suggested for unit tests, so yes there will be new lines before and after

Copy link
Contributor

@kannanjgithub kannanjgithub left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Readability suggestions.

@shivaspeaks shivaspeaks merged commit 7a08fdb into grpc:master Apr 17, 2025
16 checks passed
@shivaspeaks shivaspeaks deleted the share_lru_cache branch April 17, 2025 01:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants