Repository navigation
Conversation
Motivation: As discussed in netty#8017, ObjectCleaner leaks into the rest of the environment and interacts with checks in some testing environments. Modifications: Avoid using ObjectCleaner in FastThreadLocal when the extending class is ignoring removal notifications. Result: Reduced usage of ObjectCleaner, and when combined with disabling Recycler, may allow tests to run without ObjectCleaner being used.
| try { | ||
| Class<?> declaringClass = clazz.getMethod("onRemoval", Object.class).getDeclaringClass(); | ||
| return !declaringClass.equals(FastThreadLocal.class); | ||
| } catch (Throwable t) { |
There was a problem hiding this comment.
I wonder if we want to log something
There was a problem hiding this comment.
Added logging at info level.
normanmaurer
left a comment
There was a problem hiding this comment.
This looks like a good idea... Just a few comment.s
| } | ||
|
|
||
| // visible for tests | ||
| static boolean hasOnRemoval(Class<? extends FastThreadLocal> clazz) { |
There was a problem hiding this comment.
Also we use 4 spaces not 2.
There was a problem hiding this comment.
Doh, missed a spot. Fixed.
| assertEquals(4, ObjectCleaner.getLiveSetCount() - sizeWhenStart); | ||
| } | ||
|
|
||
| public void testDetectOnRemoval_whenPresent() { |
There was a problem hiding this comment.
Doh, I started to and then accidentally trashed it all when I realized I didn't need to set a timeout and then thought Netty must be using JUnit 3 (which doesn't make sense). Fixed.
Aaand now my tests are actually running. Aaand they fail.
| assertThat(FastThreadLocal.hasOnRemoval(TestFastThreadLocal.class), is(true)); | ||
| } | ||
|
|
||
| public void testDetectOnRemoval_whenAbsent() { |
| class ExtendingFastThreadLocal<T> extends FastThreadLocal<T> { | ||
| } | ||
|
|
||
| assertThat(FastThreadLocal.hasOnRemoval(FastThreadLocal.class), is(false)); |
| } | ||
|
|
||
| assertThat(FastThreadLocal.hasOnRemoval(FastThreadLocal.class), is(false)); | ||
| assertThat(FastThreadLocal.hasOnRemoval(ExtendingFastThreadLocal.class), is(false)); |
| } | ||
|
|
||
| public void testDetectOnRemoval_whenPresent() { | ||
| assertThat(FastThreadLocal.hasOnRemoval(TestFastThreadLocal.class), is(true)); |
There was a problem hiding this comment.
Changed. I was just following the style of the file, which had avoided assertNull and assertTrue (see testRemoveAll()).
bryce-anderson
left a comment
There was a problem hiding this comment.
This looks good to to me, but I'm far from an expert here so I'm going to defer approval to those more knowledgeable in this area.
| // visible for tests | ||
| static boolean hasOnRemoval(Class<? extends FastThreadLocal> clazz) { | ||
| try { | ||
| Class<?> declaringClass = clazz.getMethod("onRemoval", Object.class).getDeclaringClass(); |
There was a problem hiding this comment.
I know you added some tests for this already, but should we note that the method name itself is important due to reflective usage at the onRemoval declaration?
|
That looks totally reasonable to me so 👍 (pending opened comments from others). |
ejona86
left a comment
There was a problem hiding this comment.
Tests actually being run pointed out the implementation was completely broken. Reimplemented hasOnRemoval(), which unfortunately includes catching an exception in a common case. Still worth it though.
| } | ||
|
|
||
| // visible for tests | ||
| static boolean hasOnRemoval(Class<? extends FastThreadLocal> clazz) { |
There was a problem hiding this comment.
Doh, missed a spot. Fixed.
| // visible for tests | ||
| static boolean hasOnRemoval(Class<? extends FastThreadLocal> clazz) { | ||
| try { | ||
| Class<?> declaringClass = clazz.getMethod("onRemoval", Object.class).getDeclaringClass(); |
| try { | ||
| Class<?> declaringClass = clazz.getMethod("onRemoval", Object.class).getDeclaringClass(); | ||
| return !declaringClass.equals(FastThreadLocal.class); | ||
| } catch (Throwable t) { |
There was a problem hiding this comment.
Added logging at info level.
| assertEquals(4, ObjectCleaner.getLiveSetCount() - sizeWhenStart); | ||
| } | ||
|
|
||
| public void testDetectOnRemoval_whenPresent() { |
There was a problem hiding this comment.
Doh, I started to and then accidentally trashed it all when I realized I didn't need to set a timeout and then thought Netty must be using JUnit 3 (which doesn't make sense). Fixed.
Aaand now my tests are actually running. Aaand they fail.
| assertThat(FastThreadLocal.hasOnRemoval(TestFastThreadLocal.class), is(true)); | ||
| } | ||
|
|
||
| public void testDetectOnRemoval_whenAbsent() { |
| } | ||
|
|
||
| public void testDetectOnRemoval_whenPresent() { | ||
| assertThat(FastThreadLocal.hasOnRemoval(TestFastThreadLocal.class), is(true)); |
There was a problem hiding this comment.
Changed. I was just following the style of the file, which had avoided assertNull and assertTrue (see testRemoveAll()).
| class ExtendingFastThreadLocal<T> extends FastThreadLocal<T> { | ||
| } | ||
|
|
||
| assertThat(FastThreadLocal.hasOnRemoval(FastThreadLocal.class), is(false)); |
| } | ||
|
|
||
| assertThat(FastThreadLocal.hasOnRemoval(FastThreadLocal.class), is(false)); | ||
| assertThat(FastThreadLocal.hasOnRemoval(ExtendingFastThreadLocal.class), is(false)); |
| static boolean hasOnRemoval(Class<? extends FastThreadLocal> origClass) { | ||
| for (Class<?> clazz = origClass; clazz != FastThreadLocal.class; clazz = clazz.getSuperclass()) { | ||
| try { | ||
| // Can't use getMethod since onRemoval is protected |
There was a problem hiding this comment.
@ejona86 couldn't you use the following ?
static boolean hasOnRemoval(Class<? extends FastThreadLocal> origClass) {
try {
origClass.getMethod("onRemoval").getDeclaringClass() != FastThreadLocal.class
} catch (Throwable cause) {
// logging.....
return true;
}
}There was a problem hiding this comment.
actually seems like you tried something like this... Can you explain why this not works ?
There was a problem hiding this comment.
@ejona86 I guess what you could also do is loop through all the classes as you did and call:
getDeclaredMethods() on each class and check if onRemoval exists there. This should work with protected as well. See https://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getDeclaredMethods()
There was a problem hiding this comment.
getMethod() does not return protected methods, so it throws NoSuchMethodException virtually all the time.
From getMethod's docs:
Returns a Method object that reflects the specified public member method...
getDeclaredMethod does return protected methods, but it doesn't recurse to the superclass when the method is not found, so the looping was required.
There was a problem hiding this comment.
could we use a marker interface + instance of check instead?
There was a problem hiding this comment.
@jasontedor I am correct that the problem you mentioned is true for my suggested code but also for the one that is used in this PR, correct ?
There was a problem hiding this comment.
Tagging interface is a bit hard, because existing users of the class won't use it. The reason the reflection is necessary is to continue supporting the current API.
We could create a NoRemovalNotificationFastThreadLocal (NRNFTL; "pending marketing approval") that overrides onRemoval and makes the method final. Then FastThreadLocal could see if the current instance extends NRNFTL. That does mean most existing users of FastThreadLocal would need to be changed, which I think does make it a less-nice change. The in-tree ones could be changed to it easily, though.
The current API is unfortunate, because onRemoval() usage is the uncommon case. There's only three in-tree users: PooledByteBufAllocator, Recycler, and epoll's NativeDatagramPacketArray. PooledByteBufAllocator could avoid get()ing from the thread local when useCacheForAllThreads == true. Recycler could be disabled by default, or replaced (#8052). The NativeDatagramPacketArray code looks like it may only be called from the event loop.
There was a problem hiding this comment.
@ejona86 I agree... that said we could also mark onRemoval as @Deprecated and only handle it from the FastLocalThread (we did the same before which I "fixed"). I am somewhat sure we could remove the onRemoval usage in our internal use-cases in many if not in all cases.
Only partial handle onRemoval is a bit unfortunate but maybe the best bet.
WDYT ?
Also /cc @jasontedor @johnou
|
Closing in favor of #8064 |
Motivation:
As discussed in #8017, ObjectCleaner leaks into the rest of the environment and
interacts with checks in some testing environments.
Modifications:
Avoid using ObjectCleaner in FastThreadLocal when the extending class is
ignoring removal notifications.
Result:
Reduced usage of ObjectCleaner, and when combined with disabling Recycler, may
allow tests to run without ObjectCleaner being used.
I've not actually tested that a sane selection of tests can now run without ObjectCleaner,
but even if we wouldn't be 100% there, this still seems like an easy step in the right
direction.
CC @jasontedor, @normanmaurer, @carl-mastrangelo, @hc-codersatlas, @vkostyukov