-
Notifications
You must be signed in to change notification settings - Fork 29k
[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
Changes from 3 commits
ebf280d
f137de7
2db719c
7f58ae6
9d775bb
2ee58b2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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,59 @@ 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 | ||
| int majorVersion = Integer.parseInt(System.getProperty("java.version").split("\\.")[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 +212,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); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,7 @@ import java.nio.{ByteBuffer, MappedByteBuffer} | |
| import scala.collection.Map | ||
| import scala.collection.mutable | ||
|
|
||
| import sun.misc.Unsafe | ||
| import sun.nio.ch.DirectBuffer | ||
|
|
||
| import org.apache.spark.internal.Logging | ||
|
|
@@ -193,6 +194,35 @@ private[spark] class StorageStatus( | |
|
|
||
| /** Helper methods for storage-related objects. */ | ||
| private[spark] object StorageUtils extends Logging { | ||
|
|
||
| // In Java 8, the type of DirectBuffer.cleaner() was sun.misc.Cleaner, and it was possible | ||
| // to access the method sun.misc.Cleaner.clean() to invoke it. The type changed to | ||
| // jdk.internal.ref.Cleaner in later JDKs, and the .clean() method is not accessible even with | ||
| // reflection. However sun.misc.Unsafe added a invokeCleaner() method in JDK 9+ and this is | ||
| // still accessible with reflection. | ||
| private val bufferCleaner: DirectBuffer => Unit = | ||
| if (System.getProperty("java.version").split("\\.").head.toInt < 9) { | ||
| // scalastyle:off classforname | ||
| val cleanerMethod = Class.forName("sun.misc.Cleaner").getMethod("clean") | ||
| // scalastyle:on classforname | ||
| (buffer: DirectBuffer) => { | ||
| // Careful to avoid the return type of .cleaner(), which changes with JDK | ||
| val cleaner: AnyRef = buffer.cleaner() | ||
| if (cleaner != null) { | ||
| cleanerMethod.invoke(cleaner) | ||
| } | ||
| } | ||
| } else { | ||
| // scalastyle:off classforname | ||
| val cleanerMethod = | ||
| Class.forName("sun.misc.Unsafe").getMethod("invokeCleaner", classOf[ByteBuffer]) | ||
|
||
| // scalastyle:on classforname | ||
| val unsafeField = classOf[Unsafe].getDeclaredField("theUnsafe") | ||
| unsafeField.setAccessible(true) | ||
| val unsafe = unsafeField.get(null).asInstanceOf[Unsafe] | ||
| (buffer: DirectBuffer) => cleanerMethod.invoke(unsafe, buffer) | ||
| } | ||
|
|
||
| /** | ||
| * Attempt to clean up a ByteBuffer if it is direct or memory-mapped. This uses an *unsafe* Sun | ||
| * API that will cause errors if one attempts to read from the disposed buffer. However, neither | ||
|
|
@@ -204,14 +234,8 @@ private[spark] object StorageUtils extends Logging { | |
| def dispose(buffer: ByteBuffer): Unit = { | ||
| if (buffer != null && buffer.isInstanceOf[MappedByteBuffer]) { | ||
| logTrace(s"Disposing of $buffer") | ||
| cleanDirectBuffer(buffer.asInstanceOf[DirectBuffer]) | ||
| bufferCleaner(buffer.asInstanceOf[DirectBuffer]) | ||
| } | ||
| } | ||
|
|
||
| private def cleanDirectBuffer(buffer: DirectBuffer) = { | ||
| val cleaner = buffer.cleaner() | ||
| if (cleaner != null) { | ||
| cleaner.clean() | ||
| } | ||
| } | ||
| } | ||
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)