Skip to content

Avoid ObjectCleaner in FastThreadLocal when onRemoval is ignored - #8051

Closed
ejona86 wants to merge 2 commits into
netty:4.1from
ejona86:no-unnecessary-objectcleaner
Closed

ejona86 wants to merge 2 commits into
netty:4.1from
ejona86:no-unnecessary-objectcleaner

Conversation

@ejona86

@ejona86 ejona86 commented Jun 25, 2018

Copy link
Copy Markdown
Member

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

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) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

t -> ignore

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I wonder if we want to log something

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Added logging at info level.

@normanmaurer normanmaurer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This looks like a good idea... Just a few comment.s

}

// visible for tests
static boolean hasOnRemoval(Class<? extends FastThreadLocal> clazz) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Also we use 4 spaces not 2.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Doh, missed a spot. Fixed.

assertEquals(4, ObjectCleaner.getLiveSetCount() - sizeWhenStart);
}

public void testDetectOnRemoval_whenPresent() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Missing @Test annotation

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Missing @Test annotation

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed.

class ExtendingFastThreadLocal<T> extends FastThreadLocal<T> {
}

assertThat(FastThreadLocal.hasOnRemoval(FastThreadLocal.class), is(false));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

just use assertFalse ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done.

}

assertThat(FastThreadLocal.hasOnRemoval(FastThreadLocal.class), is(false));
assertThat(FastThreadLocal.hasOnRemoval(ExtendingFastThreadLocal.class), is(false));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

just use assertFalse ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done.

}

public void testDetectOnRemoval_whenPresent() {
assertThat(FastThreadLocal.hasOnRemoval(TestFastThreadLocal.class), is(true));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

just use assertTrue ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Changed. I was just following the style of the file, which had avoided assertNull and assertTrue (see testRemoveAll()).

@bryce-anderson bryce-anderson left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done.

@vkostyukov

Copy link
Copy Markdown
Contributor

That looks totally reasonable to me so 👍 (pending opened comments from others).

@ejona86 ejona86 left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Doh, missed a spot. Fixed.

// visible for tests
static boolean hasOnRemoval(Class<? extends FastThreadLocal> clazz) {
try {
Class<?> declaringClass = clazz.getMethod("onRemoval", Object.class).getDeclaringClass();

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done.

try {
Class<?> declaringClass = clazz.getMethod("onRemoval", Object.class).getDeclaringClass();
return !declaringClass.equals(FastThreadLocal.class);
} catch (Throwable t) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Added logging at info level.

assertEquals(4, ObjectCleaner.getLiveSetCount() - sizeWhenStart);
}

public void testDetectOnRemoval_whenPresent() {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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() {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed.

}

public void testDetectOnRemoval_whenPresent() {
assertThat(FastThreadLocal.hasOnRemoval(TestFastThreadLocal.class), is(true));

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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));

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done.

}

assertThat(FastThreadLocal.hasOnRemoval(FastThreadLocal.class), is(false));
assertThat(FastThreadLocal.hasOnRemoval(ExtendingFastThreadLocal.class), is(false));

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done.

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

didn't know that :(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@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;
    }
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

actually seems like you tried something like this... Can you explain why this not works ?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@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()

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@jasontedor thanks a lot...

@johnou johnou Jun 26, 2018 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

could we use a marker interface + instance of check instead?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@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 ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@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

@ejona86

ejona86 commented Jun 27, 2018

Copy link
Copy Markdown
Member Author

Closing in favor of #8064

@ejona86 ejona86 closed this Jun 27, 2018
@ejona86
ejona86 deleted the no-unnecessary-objectcleaner branch June 27, 2018 17:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants