-
Notifications
You must be signed in to change notification settings - Fork 981
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
Avoid keeping a strong reference to caches being monitored #2643
Avoid keeping a strong reference to caches being monitored #2643
Conversation
I'm getting test failures locally in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The implementations of Gauge and FunctionCounter do not call the value function if the underlying obj is null
. For example, in CacheMeterBinder
, where it has
FunctionCounter.builder("cache.gets", cache.get(), c -> hitCount())
if the weak reference's get()
returns null
then the hitCount()
method will not be called. This is why with the current implementation, we don't really care what the abstract methods return if the cache is GC'd because they won't be called. This behavior is not very apparent and took me a bit of looking at it to realize.
...eter-core/src/main/java/io/micrometer/core/instrument/binder/cache/CaffeineCacheMetrics.java
Outdated
Show resolved
Hide resolved
@shakuzen do you want me to fix all the caches in this same PR, or should I do it in separate PRs? |
@shakuzen do you want me to add some tests for validating that no strong reference is kept? It's probably doable by doing one or more |
Yes, I think the cause and fix is the same for all so fixing them all in the same pull request sounds reasonable to me. And yes, a test would be good. I have one locally I can work into the pull request if you don't feel like making one. |
I'll update the PR with the same kind of fix for all them then 👍
Sounds great! |
Updated the PR accordingly. AFAICT, the |
...eter-core/src/main/java/io/micrometer/core/instrument/binder/cache/CaffeineCacheMetrics.java
Outdated
Show resolved
Hide resolved
...ter-core/src/main/java/io/micrometer/core/instrument/binder/cache/HazelcastCacheMetrics.java
Show resolved
Hide resolved
This looks good to me the only thing I miss is testing this, I guess we have two issues here:
@shakuzen Could you please share what do you have locally to test this? public class MockReference<T> extends WeakReference<T> {
private T referent;
public MockReference(T referent) {
super(referent);
this.referent = referent;
}
@Nullable
@Override
public T get() {
return this.referent;
}
public void simulateGC() {
this.referent = null;
}
} |
It may depend on the JVM and GC, but at least in my testing locally, it was reliable enough to do the following to garbage collect the cache: cache = null;
System.gc(); |
f7de149
to
99bd536
Compare
99bd536
to
a333965
Compare
I added some generics and the dereference test to the cache metrics TCK. The current build failure looks unrelated. We'll see if we can get that resolved. |
a333965
to
47a8ca0
Compare
47a8ca0
to
07607dc
Compare
I was going to merge this to the maintenance branches originally, but with the addition of generics, it is more appropriate to include this in the next feature release. We can separately consider a back-port to maintenance branches without the generics if there is demand. |
Fixes #2642.