mridulm commented on a change in pull request #33947:
URL: https://github.com/apache/spark/pull/33947#discussion_r705938781
##########
File path: common/unsafe/src/main/java/org/apache/spark/unsafe/Platform.java
##########
@@ -77,28 +67,52 @@
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:
+ Class<?> cls = Class.forName("java.nio.DirectByteBuffer");
+ Constructor<?> constructor = cls.getDeclaredConstructor(Long.TYPE,
Integer.TYPE);
+ Field cleanerField = cls.getDeclaredField("cleaner");
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);
+ constructor.setAccessible(true);
+ cleanerField.setAccessible(true);
+ } catch (RuntimeException re) {
+ // This is a Java 9+ exception, so needs to be handled without
importing it
+ if
("InaccessibleObjectException".equals(re.getClass().getSimpleName())) {
+ // Continue, but the constructor/field are not available
+ // See comment below for more context
+ constructor = null;
+ cleanerField = null;
Review comment:
I am missing something here, given
[this](https://github.com/apache/spark/blob/09d72879a9752976f11fbce6ed7be224ecb8fba0/common/unsafe/src/main/java/org/apache/spark/unsafe/Platform.java#L84),
`DBB_CONSTRUCTOR` and `DBB_CLEANER_FIELD` are not set.
And as the exception is thrown from the static block, the class `Platform`
will fail to load.
To illustrate:
```
$ cat Test.java
public class Test {
public static void main(String[] args) {
System.out.println("Value = " + TestStatic.value);
}
}
class TestStatic {
static final String value;
static {
if (Boolean.getBoolean("THROW_EXCEPTION")) {
throw new RuntimeException("failed");
}
value = "hi";
}
}
$ java -cp . -DTHROW_EXCEPTION=true Test
Exception in thread "main" java.lang.ExceptionInInitializerError
at Test.main(Test.java:4)
Caused by: java.lang.RuntimeException: failed
at TestStatic.<clinit>(Test.java:13)
... 1 more
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]