-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-24421][BUILD][CORE] Accessing sun.misc.Cleaner in JDK11 #22993
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
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ebf280d
Avoid sun.misc.Cleaner in JDK 9+ while retaining Java 8 compatibility…
srowen f137de7
Update deps
srowen 2db719c
Fix compile error in hive-thriftserver
srowen 7f58ae6
Split java.version on non-digit char
srowen 9d775bb
Use Utils.classForName and lang3 for Java version
srowen 2ee58b2
Update lang3 deps
srowen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,10 +19,10 @@ | |
|
|
||
| import java.lang.reflect.Constructor; | ||
| import java.lang.reflect.Field; | ||
| import java.lang.reflect.InvocationTargetException; | ||
| import java.lang.reflect.Method; | ||
| import java.nio.ByteBuffer; | ||
|
|
||
| import sun.misc.Cleaner; | ||
| import sun.misc.Unsafe; | ||
|
|
||
| public final class Platform { | ||
|
|
@@ -67,6 +67,60 @@ public final class Platform { | |
| unaligned = _unaligned; | ||
| } | ||
|
|
||
| // Access fields and constructors once and store them, for performance: | ||
|
|
||
| private static final Constructor<?> DBB_CONSTRUCTOR; | ||
| private static final Field DBB_CLEANER_FIELD; | ||
| static { | ||
| try { | ||
| Class<?> cls = Class.forName("java.nio.DirectByteBuffer"); | ||
| Constructor<?> constructor = cls.getDeclaredConstructor(Long.TYPE, Integer.TYPE); | ||
| constructor.setAccessible(true); | ||
| Field cleanerField = cls.getDeclaredField("cleaner"); | ||
| cleanerField.setAccessible(true); | ||
| DBB_CONSTRUCTOR = constructor; | ||
| DBB_CLEANER_FIELD = cleanerField; | ||
| } catch (ClassNotFoundException | NoSuchMethodException | NoSuchFieldException e) { | ||
| throw new IllegalStateException(e); | ||
| } | ||
| } | ||
|
|
||
| private static final Method CLEANER_CREATE_METHOD; | ||
| static { | ||
| // The implementation of Cleaner changed from JDK 8 to 9 | ||
| // Split java.version on non-digit chars: | ||
| int majorVersion = Integer.parseInt(System.getProperty("java.version").split("\\D+")[0]); | ||
| String cleanerClassName; | ||
| if (majorVersion < 9) { | ||
| cleanerClassName = "sun.misc.Cleaner"; | ||
| } else { | ||
| cleanerClassName = "jdk.internal.ref.Cleaner"; | ||
| } | ||
| try { | ||
| Class<?> cleanerClass = Class.forName(cleanerClassName); | ||
| Method createMethod = cleanerClass.getMethod("create", Object.class, Runnable.class); | ||
| // Accessing jdk.internal.ref.Cleaner should actually fail by default in JDK 9+, | ||
| // unfortunately, unless the user has allowed access with something like | ||
| // --add-opens java.base/java.lang=ALL-UNNAMED If not, we can't really use the Cleaner | ||
| // hack below. It doesn't break, just means the user might run into the default JVM limit | ||
| // on off-heap memory and increase it or set the flag above. This tests whether it's | ||
| // available: | ||
| try { | ||
| createMethod.invoke(null, null, null); | ||
| } catch (IllegalAccessException e) { | ||
| // Don't throw an exception, but can't log here? | ||
| createMethod = null; | ||
| } catch (InvocationTargetException ite) { | ||
| // shouldn't happen; report it | ||
| throw new IllegalStateException(ite); | ||
| } | ||
| CLEANER_CREATE_METHOD = createMethod; | ||
| } catch (ClassNotFoundException | NoSuchMethodException e) { | ||
| throw new IllegalStateException(e); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * @return true when running JVM is having sun's Unsafe package available in it and underlying | ||
| * system having unaligned-access capability. | ||
|
|
@@ -159,18 +213,18 @@ public static long reallocateMemory(long address, long oldSize, long newSize) { | |
| * MaxDirectMemorySize limit (the default limit is too low and we do not want to require users | ||
| * to increase it). | ||
| */ | ||
| @SuppressWarnings("unchecked") | ||
| public static ByteBuffer allocateDirectBuffer(int size) { | ||
| try { | ||
| Class<?> cls = Class.forName("java.nio.DirectByteBuffer"); | ||
| Constructor<?> constructor = cls.getDeclaredConstructor(Long.TYPE, Integer.TYPE); | ||
| constructor.setAccessible(true); | ||
| Field cleanerField = cls.getDeclaredField("cleaner"); | ||
| cleanerField.setAccessible(true); | ||
| long memory = allocateMemory(size); | ||
| ByteBuffer buffer = (ByteBuffer) constructor.newInstance(memory, size); | ||
| Cleaner cleaner = Cleaner.create(buffer, () -> freeMemory(memory)); | ||
| cleanerField.set(buffer, cleaner); | ||
| ByteBuffer buffer = (ByteBuffer) DBB_CONSTRUCTOR.newInstance(memory, size); | ||
| if (CLEANER_CREATE_METHOD != null) { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See #23424 ; I now think this was an error. |
||
| try { | ||
| DBB_CLEANER_FIELD.set(buffer, | ||
| CLEANER_CREATE_METHOD.invoke(null, buffer, (Runnable) () -> freeMemory(memory))); | ||
| } catch (IllegalAccessException | InvocationTargetException e) { | ||
| throw new IllegalStateException(e); | ||
| } | ||
| } | ||
| return buffer; | ||
| } catch (Exception e) { | ||
| throwException(e); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
Why do we need to reference java 9, its dead right and it will have no LTS AFAIK.
Shouldnt we only support LTS?
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.
Yes, this comment is just noting that the change happened between Java 8 and 9. We are targeting support for Java 11. However I expect virtually all of the changes that we have to make are due to changes in Java 9. (And I have no reason to believe Java 9-10 wouldn't work; we want them to work too)