You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by se...@apache.org on 2018/01/19 12:41:12 UTC
[2/3] flink git commit: [hotfix] [tests] Add unit tests for
ChildFirstClassLoader
[hotfix] [tests] Add unit tests for ChildFirstClassLoader
Project: http://git-wip-us.apache.org/repos/asf/flink/repo
Commit: http://git-wip-us.apache.org/repos/asf/flink/commit/f9aff592
Tree: http://git-wip-us.apache.org/repos/asf/flink/tree/f9aff592
Diff: http://git-wip-us.apache.org/repos/asf/flink/diff/f9aff592
Branch: refs/heads/master
Commit: f9aff5926254ca05da77ea856e72bb2c71dae7fd
Parents: 25f7aee
Author: Stephan Ewen <se...@apache.org>
Authored: Thu Jan 18 17:58:01 2018 +0100
Committer: Stephan Ewen <se...@apache.org>
Committed: Fri Jan 19 13:40:00 2018 +0100
----------------------------------------------------------------------
.../librarycache/FlinkUserCodeClassLoaders.java | 13 +-
.../runtime/classloading/ClassLoaderTest.java | 139 +++++++++++++++++++
2 files changed, 146 insertions(+), 6 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/flink/blob/f9aff592/flink-runtime/src/main/java/org/apache/flink/runtime/execution/librarycache/FlinkUserCodeClassLoaders.java
----------------------------------------------------------------------
diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/execution/librarycache/FlinkUserCodeClassLoaders.java b/flink-runtime/src/main/java/org/apache/flink/runtime/execution/librarycache/FlinkUserCodeClassLoaders.java
index d40802e..59536a1 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/execution/librarycache/FlinkUserCodeClassLoaders.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/execution/librarycache/FlinkUserCodeClassLoaders.java
@@ -110,16 +110,17 @@ public class FlinkUserCodeClassLoaders {
protected synchronized Class<?> loadClass(
String name, boolean resolve) throws ClassNotFoundException {
- for (String alwaysParentFirstPattern : alwaysParentFirstPatterns) {
- if (name.startsWith(alwaysParentFirstPattern)) {
- return super.loadClass(name, resolve);
- }
- }
-
// First, check if the class has already been loaded
Class<?> c = findLoadedClass(name);
if (c == null) {
+ // check whether the class should go parent-first
+ for (String alwaysParentFirstPattern : alwaysParentFirstPatterns) {
+ if (name.startsWith(alwaysParentFirstPattern)) {
+ return super.loadClass(name, resolve);
+ }
+ }
+
try {
// check the URLs
c = findClass(name);
http://git-wip-us.apache.org/repos/asf/flink/blob/f9aff592/flink-runtime/src/test/java/org/apache/flink/runtime/classloading/ClassLoaderTest.java
----------------------------------------------------------------------
diff --git a/flink-runtime/src/test/java/org/apache/flink/runtime/classloading/ClassLoaderTest.java b/flink-runtime/src/test/java/org/apache/flink/runtime/classloading/ClassLoaderTest.java
new file mode 100644
index 0000000..c02278c
--- /dev/null
+++ b/flink-runtime/src/test/java/org/apache/flink/runtime/classloading/ClassLoaderTest.java
@@ -0,0 +1,139 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.runtime.classloading;
+
+import org.apache.flink.runtime.execution.librarycache.FlinkUserCodeClassLoaders;
+import org.apache.flink.util.TestLogger;
+
+import org.junit.Test;
+
+import java.net.URL;
+import java.net.URLClassLoader;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
+
+/**
+ * Tests for classloading and class loder utilities.
+ */
+public class ClassLoaderTest extends TestLogger {
+
+ @Test
+ public void testParentFirstClassLoading() throws Exception {
+ final ClassLoader parentClassLoader = getClass().getClassLoader();
+
+ // collect the libraries / class folders with RocksDB related code: the state backend and RocksDB itself
+ final URL childCodePath = getClass().getProtectionDomain().getCodeSource().getLocation();
+
+ final URLClassLoader childClassLoader1 = FlinkUserCodeClassLoaders.parentFirst(
+ new URL[] { childCodePath }, parentClassLoader);
+
+ final URLClassLoader childClassLoader2 = FlinkUserCodeClassLoaders.parentFirst(
+ new URL[] { childCodePath }, parentClassLoader);
+
+ final String className = ClassLoaderTest.class.getName();
+
+ final Class<?> clazz1 = Class.forName(className, false, parentClassLoader);
+ final Class<?> clazz2 = Class.forName(className, false, childClassLoader1);
+ final Class<?> clazz3 = Class.forName(className, false, childClassLoader2);
+
+ assertEquals(clazz1, clazz2);
+ assertEquals(clazz1, clazz3);
+
+ childClassLoader1.close();
+ childClassLoader2.close();
+ }
+
+ @Test
+ public void testChildFirstClassLoading() throws Exception {
+ final ClassLoader parentClassLoader = getClass().getClassLoader();
+
+ // collect the libraries / class folders with RocksDB related code: the state backend and RocksDB itself
+ final URL childCodePath = getClass().getProtectionDomain().getCodeSource().getLocation();
+
+ final URLClassLoader childClassLoader1 = FlinkUserCodeClassLoaders.childFirst(
+ new URL[] { childCodePath }, parentClassLoader, new String[0]);
+
+ final URLClassLoader childClassLoader2 = FlinkUserCodeClassLoaders.childFirst(
+ new URL[] { childCodePath }, parentClassLoader, new String[0]);
+
+ final String className = ClassLoaderTest.class.getName();
+
+ final Class<?> clazz1 = Class.forName(className, false, parentClassLoader);
+ final Class<?> clazz2 = Class.forName(className, false, childClassLoader1);
+ final Class<?> clazz3 = Class.forName(className, false, childClassLoader2);
+
+ assertNotEquals(clazz1, clazz2);
+ assertNotEquals(clazz1, clazz3);
+ assertNotEquals(clazz2, clazz3);
+
+ childClassLoader1.close();
+ childClassLoader2.close();
+ }
+
+ @Test
+ public void testRepeatedChildFirstClassLoading() throws Exception {
+ final ClassLoader parentClassLoader = getClass().getClassLoader();
+
+ // collect the libraries / class folders with RocksDB related code: the state backend and RocksDB itself
+ final URL childCodePath = getClass().getProtectionDomain().getCodeSource().getLocation();
+
+ final URLClassLoader childClassLoader = FlinkUserCodeClassLoaders.childFirst(
+ new URL[] { childCodePath }, parentClassLoader, new String[0]);
+
+ final String className = ClassLoaderTest.class.getName();
+
+ final Class<?> clazz1 = Class.forName(className, false, parentClassLoader);
+ final Class<?> clazz2 = Class.forName(className, false, childClassLoader);
+ final Class<?> clazz3 = Class.forName(className, false, childClassLoader);
+ final Class<?> clazz4 = Class.forName(className, false, childClassLoader);
+
+ assertNotEquals(clazz1, clazz2);
+
+ assertEquals(clazz2, clazz3);
+ assertEquals(clazz2, clazz4);
+
+ childClassLoader.close();
+ }
+
+ @Test
+ public void testRepeatedParentFirstPatternClass() throws Exception {
+ final String className = ClassLoaderTest.class.getName();
+ final String parentFirstPattern = className.substring(0, className.lastIndexOf('.'));
+
+ final ClassLoader parentClassLoader = getClass().getClassLoader();
+
+ // collect the libraries / class folders with RocksDB related code: the state backend and RocksDB itself
+ final URL childCodePath = getClass().getProtectionDomain().getCodeSource().getLocation();
+
+ final URLClassLoader childClassLoader = FlinkUserCodeClassLoaders.childFirst(
+ new URL[] { childCodePath }, parentClassLoader, new String[] { parentFirstPattern });
+
+ final Class<?> clazz1 = Class.forName(className, false, parentClassLoader);
+ final Class<?> clazz2 = Class.forName(className, false, childClassLoader);
+ final Class<?> clazz3 = Class.forName(className, false, childClassLoader);
+ final Class<?> clazz4 = Class.forName(className, false, childClassLoader);
+
+ assertEquals(clazz1, clazz2);
+ assertEquals(clazz1, clazz3);
+ assertEquals(clazz1, clazz4);
+
+ childClassLoader.close();
+ }
+}