-
Notifications
You must be signed in to change notification settings - Fork 38.5k
CaffeineCacheManager getCache method cause thread block #30066
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
Comments
Could you provide a sample application that reproduces the problem? It should be a small and simple application, ideally created with https://start.spring.io. You can share it here as a zip or a git repository. Thanks! |
Ok, wait a moment. |
Then, use the jstack command to view the thread status. You can see that there are a lot of block threads. Here's one of the thread stacks
And, I found this problem in both jdk1.8 and jdk17. I think, the CaffeineCacheManager#getCache() method can be optimized like this:
|
The |
This problem leads to a lot of block threads in our online environment. |
Thanks for the sample @huangxfchn . As explained in this PR comment, the Javadoc for this class seems to indicate that read operations are completely lock free. I've reproduced the behavior you're describing even after pre-populating caches with the set of cache names. We'll revisit the implementation there to avoid using Thanks for your report! |
Prior to this commit, using a dynamic `CaffeineCacheManager` would rely on `ConcurrentHashMap#computeIfAbsent` for retrieving and creating cache instances as needed. It turns out that using this method concurrently can cause lock contention even when all known cache instances are instantiated. This commit avoids using this method if the cache instance already exists and avoid storing `null` entries in the map. This change reduces lock contention and the overall HashMap size in the non-dynamic case. See gh-30066 Fixes gh-30085
FYI, for context on computeIfAbsent(), in 2014:
Caffeine therefore does this precscreen as shown in the benchmarks. Doug decided on adopting 1cif in recent jdks. He is currently pondering a per-entry lock vs the current bin lock, which should resolve this entirely. |
Uh oh!
There was an error while loading. Please reload this page.
here is my thread stack, block 193 threads
here is my code:
The text was updated successfully, but these errors were encountered: