From cc2bdb5f515b94555c4828eb5adfc9642854fd08 Mon Sep 17 00:00:00 2001 From: chencb Date: Wed, 13 Nov 2019 18:32:46 +0800 Subject: [PATCH] Fix hadoop task jdk11 compatible (#8799) * Fix hadoop task jdk11 compatible * Fix HadoopTaskTest --- .../indexing/common/task/HadoopTask.java | 12 +++++++++- .../indexing/common/task/HadoopTaskTest.java | 22 +++++++++++++++---- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/indexing-service/src/main/java/org/apache/druid/indexing/common/task/HadoopTask.java b/indexing-service/src/main/java/org/apache/druid/indexing/common/task/HadoopTask.java index eba75fce099..d2022d5f305 100644 --- a/indexing-service/src/main/java/org/apache/druid/indexing/common/task/HadoopTask.java +++ b/indexing-service/src/main/java/org/apache/druid/indexing/common/task/HadoopTask.java @@ -171,9 +171,19 @@ public abstract class HadoopTask extends AbstractBatchIndexTask localClassLoaderURLs.addAll(Arrays.asList(hadoopLoader.getURLs())); } + ClassLoader parent = null; + if (JvmUtils.isIsJava9Compatible()) { + try { + // See also https://docs.oracle.com/en/java/javase/11/migrate/index.html#JSMIG-GUID-A868D0B9-026F-4D46-B979-901834343F9E + parent = (ClassLoader) ClassLoader.class.getMethod("getPlatformClassLoader").invoke(null); + } + catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { + throw new RuntimeException(e); + } + } final ClassLoader classLoader = new URLClassLoader( localClassLoaderURLs.toArray(new URL[0]), - null + parent ); final String hadoopContainerDruidClasspathJars; diff --git a/indexing-service/src/test/java/org/apache/druid/indexing/common/task/HadoopTaskTest.java b/indexing-service/src/test/java/org/apache/druid/indexing/common/task/HadoopTaskTest.java index 409e7d0f669..990888e5a96 100644 --- a/indexing-service/src/test/java/org/apache/druid/indexing/common/task/HadoopTaskTest.java +++ b/indexing-service/src/test/java/org/apache/druid/indexing/common/task/HadoopTaskTest.java @@ -29,6 +29,7 @@ import org.apache.druid.indexing.common.config.TaskConfig; import org.apache.druid.java.util.common.StringUtils; import org.apache.druid.java.util.common.granularity.Granularity; import org.apache.druid.timeline.DataSegment; +import org.apache.druid.utils.JvmUtils; import org.apache.hadoop.yarn.util.ApplicationClassLoader; import org.easymock.EasyMock; import org.joda.time.Interval; @@ -125,18 +126,31 @@ public class HadoopTaskTest final Class hadoopClazz = Class.forName("org.apache.hadoop.fs.FSDataInputStream", false, classLoader); assertClassLoaderIsSingular(hadoopClazz.getClassLoader()); - final Class druidHadoopConfigClazz = Class.forName("org.apache.druid.indexer.HadoopDruidIndexerConfig", false, classLoader); + final Class druidHadoopConfigClazz = Class.forName( + "org.apache.druid.indexer.HadoopDruidIndexerConfig", + false, + classLoader + ); assertClassLoaderIsSingular(druidHadoopConfigClazz.getClassLoader()); } public static void assertClassLoaderIsSingular(ClassLoader classLoader) { - // This is a check against the current HadoopTask which creates a single URLClassLoader with null parent - Assert.assertNull(classLoader.getParent()); + if (JvmUtils.isIsJava9Compatible()) { + // See also https://docs.oracle.com/en/java/javase/11/migrate/index.html#JSMIG-GUID-A868D0B9-026F-4D46-B979-901834343F9E + Assert.assertEquals("PlatformClassLoader", classLoader.getParent().getClass().getSimpleName()); + } else { + // This is a check against the current HadoopTask which creates a single URLClassLoader with null parent + Assert.assertNull(classLoader.getParent()); + } Assert.assertFalse(classLoader instanceof ApplicationClassLoader); Assert.assertTrue(classLoader instanceof URLClassLoader); final ClassLoader appLoader = HadoopDruidIndexerConfig.class.getClassLoader(); - Assert.assertNotEquals(StringUtils.format("ClassLoader [%s] is not isolated!", classLoader), appLoader, classLoader); + Assert.assertNotEquals( + StringUtils.format("ClassLoader [%s] is not isolated!", classLoader), + appLoader, + classLoader + ); } }