Skip to content
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

[FLINK-14522] Introduce JavaGcCleanerWrapper to find Java GC Cleaner depending on JVM version #9997

Merged
merged 1 commit into from Oct 31, 2019

Conversation

azagrebin
Copy link
Contributor

@azagrebin azagrebin commented Oct 25, 2019

What is the purpose of the change

sun.misc.Cleaner is not available since Java 9.

It was moved to jdk.internal.ref.Cleaner of java.base module (Open JDK-8148117), but another new public API was introduced to achieve the same behaviour: java.lang.ref.Cleaner;

A popular solution is use reflection to look up for the location of the Cleaner class depending on running JVM version.

Brief change log

  • Introduce JavaGcCleanerWrapper which uses reflection to look up and provide proper Cleaner implementation depending on JVM version on runtime
  • Introduce JavaGcCleanerWrapperTest to test that the clean method is called

Verifying this change

Run JavaGcCleanerWrapperTest

Does this pull request potentially affect one of the following parts:

  • Dependencies (does it add or upgrade a dependency): (no)
  • The public API, i.e., is any changed class annotated with @Public(Evolving): (no)
  • The serializers: (no)
  • The runtime per-record code paths (performance sensitive): (no)
  • Anything that affects deployment or recovery: JobManager (and its components), Checkpointing, Yarn/Mesos, ZooKeeper: (no)
  • The S3 file system connector: (no)

Documentation

  • Does this pull request introduce a new feature? (no)
  • If yes, how is the feature documented? (not applicable)

@flinkbot
Copy link
Collaborator

flinkbot commented Oct 25, 2019

Thanks a lot for your contribution to the Apache Flink project. I'm the @flinkbot. I help the community
to review your pull request. We will use this comment to track the progress of the review.

Automated Checks

Last check on commit 765f629 (Wed Dec 04 14:53:45 UTC 2019)

Warnings:

  • No documentation files were touched! Remember to keep the Flink docs up to date!

Mention the bot in a comment to re-run the automated checks.

Review Progress

  • ❓ 1. The [description] looks good.
  • ❓ 2. There is [consensus] that the contribution should go into to Flink.
  • ❓ 3. Needs [attention] from.
  • ❓ 4. The change fits into the overall [architecture].
  • ❓ 5. Overall code [quality] is good.

Please see the Pull Request Review Guide for a full explanation of the review process.


The Bot is tracking the review progress through labels. Labels are applied according to the order of the review items. For consensus, approval by a Flink committer of PMC member is required Bot commands
The @flinkbot bot supports the following commands:

  • @flinkbot approve description to approve one or more aspects (aspects: description, consensus, architecture and quality)
  • @flinkbot approve all to approve all aspects
  • @flinkbot approve-until architecture to approve everything until architecture
  • @flinkbot attention @username1 [@username2 ..] to require somebody's attention
  • @flinkbot disapprove architecture to remove an approval you gave earlier

@flinkbot
Copy link
Collaborator

flinkbot commented Oct 25, 2019

CI report:

Bot commands The @flinkbot bot supports the following commands:
  • @flinkbot run travis re-run the last Travis build


if (foundCleanerClass == null) {
throw new FlinkRuntimeException(
String.format("A proper Java Cleaner class is not found among %s", CLEANER_PROVIDERS),
Copy link
Member

@GJL GJL Oct 25, 2019

Choose a reason for hiding this comment

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

	public static void main(String[] args) {
		System.out.println(Arrays.asList(Foo.INSTANCE));
	}

	private enum Foo {
		INSTANCE
	}

output:

[INSTANCE]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you think CLEANER_PROVIDERS should be an enum?

Copy link
Member

Choose a reason for hiding this comment

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

Error will be [...] among [INSTANCE, INSTANCE]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

True, I will convert it to the list of CleanerProvider::getCleanerClassName

AtomicInteger callCounter = new AtomicInteger();
Runnable cleaner = JavaGcCleanerWrapper.create(new Object(), callCounter::incrementAndGet);
System.gc();
Thread.sleep(100); // more chance for GC to run
Copy link
Member

Choose a reason for hiding this comment

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

Can you explain this test? It would also pass without System.gc().

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will rename the test: testCleanOperationRunsOnlyOnceEitherOnGcOrExplicitly.
If GC does not run immediately (it should run 99%) then explicit cleaner running will do the clean operation. Also the cleaner lambda should not capture the owner object in the error log message, I will fix it.

Copy link
Contributor

Choose a reason for hiding this comment

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

The pause is too long imo.

Copy link
Contributor

@tillrohrmann tillrohrmann left a comment

Choose a reason for hiding this comment

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

Thanks a lot for this PR @azagrebin. The changes look good to me. I had some minor comments which you could address while merging the PR. +1 for merging if you've tested this code also with Java 11.

*
* <p>The wrapper looks up the underlying Java GC Cleaner class in different packages
*/
public class JavaGcCleanerWrapper {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can be enum or needs a private constructor as an utility class.

private static final Logger LOG = LoggerFactory.getLogger(JavaGcCleanerWrapper.class);

private static final String LEGACY_CLEANER_CLASS_NAME = "sun.misc.Cleaner"; // before Java 9
private static final String JAVA9_CLEANER_CLASS_NAME = "java.lang.ref.Cleaner"; // since Java 9+
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't these fields be part of the implementation of the different CleanerProviders?

private interface CleanerProvider {
String getCleanerClassName();

CleanerFactory createCleanerFactory(Class<?> cleanerClass);
Copy link
Contributor

Choose a reason for hiding this comment

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

The interface is a bit strange that the provider knows about the class name, but needs to get the Class<?> passed into the createCleanerFactory. Couldn't the createCleanerFactory method do the class lookup?

}
}

private static class LegacyCleanerFactory implements CleanerFactory {
Copy link
Contributor

Choose a reason for hiding this comment

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

final missing.

}
}

private static class Java9CleanerFactory implements CleanerFactory {
Copy link
Contributor

Choose a reason for hiding this comment

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

final missing.

throw new FlinkRuntimeException("Failed to find Java 9 Cleaner$Cleanable#clean method", e);
}
return new Java9CleanerFactory(cleaner, cleanerRegisterMethod, cleanMethod);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This method could be a bit cleaner if we factor out the individual steps into methods which are responsible for the error handling.

AtomicInteger callCounter = new AtomicInteger();
Runnable cleaner = JavaGcCleanerWrapper.create(new Object(), callCounter::incrementAndGet);
System.gc();
Thread.sleep(100); // more chance for GC to run
Copy link
Contributor

Choose a reason for hiding this comment

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

The pause is too long imo.

@azagrebin
Copy link
Contributor Author

Thanks for the reviews @tillrohrmann @GJL
I addressed comments, will merge when the CI is green

AtomicInteger callCounter = new AtomicInteger();
Runnable cleaner = JavaGcCleanerWrapper.create(new Object(), callCounter::incrementAndGet);
System.gc(); // not guaranteed to be run always but should in practice
Thread.sleep(10); // more chance for GC to run
Copy link
Member

Choose a reason for hiding this comment

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

Just an idea: CountDownLatch has a method boolean await(long timeout, TimeUnit unit)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

True, I just wanted to avoid test instabilities if GC is not called for some reason at once.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants