Java Code Examples for org.apache.flink.runtime.execution.librarycache.FlinkUserCodeClassLoaders#childFirst()

The following examples show how to use org.apache.flink.runtime.execution.librarycache.FlinkUserCodeClassLoaders#childFirst() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AvroKryoClassloadingTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Test
public void testKryoInChildClasspath() throws Exception {
	final Class<?> avroClass = AvroKryoSerializerUtils.class;

	final URL avroLocation = avroClass.getProtectionDomain().getCodeSource().getLocation();
	final URL kryoLocation = Kryo.class.getProtectionDomain().getCodeSource().getLocation();

	final ClassLoader parentClassLoader = new FilteredClassLoader(
			avroClass.getClassLoader(), AvroKryoSerializerUtils.class.getName());

	final ClassLoader userAppClassLoader = FlinkUserCodeClassLoaders.childFirst(
			new URL[] { avroLocation, kryoLocation },
			parentClassLoader,
			CoreOptions.ALWAYS_PARENT_FIRST_LOADER_PATTERNS.defaultValue().split(";"));

	final Class<?> userLoadedAvroClass = Class.forName(avroClass.getName(), false, userAppClassLoader);
	assertNotEquals(avroClass, userLoadedAvroClass);

	// call the 'addAvroGenericDataArrayRegistration(...)' method
	final Method m = userLoadedAvroClass.getMethod("addAvroGenericDataArrayRegistration", LinkedHashMap.class);

	final LinkedHashMap<String, ?> map = new LinkedHashMap<>();
	m.invoke(userLoadedAvroClass.newInstance(), map);

	assertEquals(1, map.size());
}
 
Example 2
Source File: ClassLoaderTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example 3
Source File: ClassLoaderTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example 4
Source File: ClassLoaderTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example 5
Source File: AvroKryoClassloadingTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testKryoInChildClasspath() throws Exception {
	final Class<?> avroClass = AvroKryoSerializerUtils.class;

	final URL avroLocation = avroClass.getProtectionDomain().getCodeSource().getLocation();
	final URL kryoLocation = Kryo.class.getProtectionDomain().getCodeSource().getLocation();

	final ClassLoader parentClassLoader = new FilteredClassLoader(
			avroClass.getClassLoader(), AvroKryoSerializerUtils.class.getName());

	final ClassLoader userAppClassLoader = FlinkUserCodeClassLoaders.childFirst(
			new URL[] { avroLocation, kryoLocation },
			parentClassLoader,
			CoreOptions.ALWAYS_PARENT_FIRST_LOADER_PATTERNS.defaultValue().split(";"));

	final Class<?> userLoadedAvroClass = Class.forName(avroClass.getName(), false, userAppClassLoader);
	assertNotEquals(avroClass, userLoadedAvroClass);

	// call the 'addAvroGenericDataArrayRegistration(...)' method
	final Method m = userLoadedAvroClass.getMethod("addAvroGenericDataArrayRegistration", LinkedHashMap.class);

	final LinkedHashMap<String, ?> map = new LinkedHashMap<>();
	m.invoke(userLoadedAvroClass.newInstance(), map);

	assertEquals(1, map.size());
}
 
Example 6
Source File: ClassLoaderTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example 7
Source File: ClassLoaderTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example 8
Source File: ClassLoaderTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example 9
Source File: RocksDbMultiClassLoaderTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testTwoSeparateClassLoaders() throws Exception {
	// collect the libraries / class folders with RocksDB related code: the state backend and RocksDB itself
	final URL codePath1 = RocksDBStateBackend.class.getProtectionDomain().getCodeSource().getLocation();
	final URL codePath2 = RocksDB.class.getProtectionDomain().getCodeSource().getLocation();

	final ClassLoader parent = getClass().getClassLoader();
	final ClassLoader loader1 = FlinkUserCodeClassLoaders.childFirst(new URL[] { codePath1, codePath2 }, parent, new String[0]);
	final ClassLoader loader2 = FlinkUserCodeClassLoaders.childFirst(new URL[] { codePath1, codePath2 }, parent, new String[0]);

	final String className = RocksDBStateBackend.class.getName();

	final Class<?> clazz1 = Class.forName(className, false, loader1);
	final Class<?> clazz2 = Class.forName(className, false, loader2);
	assertNotEquals("Test broken - the two reflectively loaded classes are equal", clazz1, clazz2);

	final Object instance1 = clazz1.getConstructor(String.class).newInstance(tmp.newFolder().toURI().toString());
	final Object instance2 = clazz2.getConstructor(String.class).newInstance(tmp.newFolder().toURI().toString());

	final String tempDir = tmp.newFolder().getAbsolutePath();

	final Method meth1 = clazz1.getDeclaredMethod("ensureRocksDBIsLoaded", String.class);
	final Method meth2 = clazz2.getDeclaredMethod("ensureRocksDBIsLoaded", String.class);
	meth1.setAccessible(true);
	meth2.setAccessible(true);

	// if all is well, these methods can both complete successfully
	meth1.invoke(instance1, tempDir);
	meth2.invoke(instance2, tempDir);
}
 
Example 10
Source File: RocksDbMultiClassLoaderTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testTwoSeparateClassLoaders() throws Exception {
	// collect the libraries / class folders with RocksDB related code: the state backend and RocksDB itself
	final URL codePath1 = RocksDBStateBackend.class.getProtectionDomain().getCodeSource().getLocation();
	final URL codePath2 = RocksDB.class.getProtectionDomain().getCodeSource().getLocation();

	final ClassLoader parent = getClass().getClassLoader();
	final ClassLoader loader1 = FlinkUserCodeClassLoaders.childFirst(new URL[] { codePath1, codePath2 }, parent, new String[0]);
	final ClassLoader loader2 = FlinkUserCodeClassLoaders.childFirst(new URL[] { codePath1, codePath2 }, parent, new String[0]);

	final String className = RocksDBStateBackend.class.getName();

	final Class<?> clazz1 = Class.forName(className, false, loader1);
	final Class<?> clazz2 = Class.forName(className, false, loader2);
	assertNotEquals("Test broken - the two reflectively loaded classes are equal", clazz1, clazz2);

	final Object instance1 = clazz1.getConstructor(String.class).newInstance(tmp.newFolder().toURI().toString());
	final Object instance2 = clazz2.getConstructor(String.class).newInstance(tmp.newFolder().toURI().toString());

	final String tempDir = tmp.newFolder().getAbsolutePath();

	final Method meth1 = clazz1.getDeclaredMethod("ensureRocksDBIsLoaded", String.class);
	final Method meth2 = clazz2.getDeclaredMethod("ensureRocksDBIsLoaded", String.class);
	meth1.setAccessible(true);
	meth2.setAccessible(true);

	// if all is well, these methods can both complete successfully
	meth1.invoke(instance1, tempDir);
	meth2.invoke(instance2, tempDir);
}
 
Example 11
Source File: AvroKryoClassloadingTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testKryoInChildClasspath() throws Exception {
	final Class<?> avroClass = AvroKryoSerializerUtils.class;

	final URL avroLocation = avroClass.getProtectionDomain().getCodeSource().getLocation();
	final URL kryoLocation = Kryo.class.getProtectionDomain().getCodeSource().getLocation();

	final ClassLoader parentClassLoader = new FilteredClassLoader(
			avroClass.getClassLoader(), AvroKryoSerializerUtils.class.getName());

	final ClassLoader userAppClassLoader = FlinkUserCodeClassLoaders.childFirst(
			new URL[] { avroLocation, kryoLocation },
			parentClassLoader,
			CoreOptions.ALWAYS_PARENT_FIRST_LOADER_PATTERNS.defaultValue().split(";"),
		NOOP_EXCEPTION_HANDLER);

	final Class<?> userLoadedAvroClass = Class.forName(avroClass.getName(), false, userAppClassLoader);
	assertNotEquals(avroClass, userLoadedAvroClass);

	// call the 'addAvroGenericDataArrayRegistration(...)' method
	final Method m = userLoadedAvroClass.getMethod("addAvroGenericDataArrayRegistration", LinkedHashMap.class);

	final LinkedHashMap<String, ?> map = new LinkedHashMap<>();
	m.invoke(userLoadedAvroClass.newInstance(), map);

	assertEquals(1, map.size());
}
 
Example 12
Source File: RocksDbMultiClassLoaderTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testTwoSeparateClassLoaders() throws Exception {
	// collect the libraries / class folders with RocksDB related code: the state backend and RocksDB itself
	final URL codePath1 = RocksDBStateBackend.class.getProtectionDomain().getCodeSource().getLocation();
	final URL codePath2 = RocksDB.class.getProtectionDomain().getCodeSource().getLocation();

	final ClassLoader parent = getClass().getClassLoader();
	final ClassLoader loader1 = FlinkUserCodeClassLoaders.childFirst(new URL[] { codePath1, codePath2 }, parent, new String[0], NOOP_EXCEPTION_HANDLER);
	final ClassLoader loader2 = FlinkUserCodeClassLoaders.childFirst(new URL[] { codePath1, codePath2 }, parent, new String[0], NOOP_EXCEPTION_HANDLER);

	final String className = RocksDBStateBackend.class.getName();

	final Class<?> clazz1 = Class.forName(className, false, loader1);
	final Class<?> clazz2 = Class.forName(className, false, loader2);
	assertNotEquals("Test broken - the two reflectively loaded classes are equal", clazz1, clazz2);

	final Object instance1 = clazz1.getConstructor(String.class).newInstance(tmp.newFolder().toURI().toString());
	final Object instance2 = clazz2.getConstructor(String.class).newInstance(tmp.newFolder().toURI().toString());

	final String tempDir = tmp.newFolder().getAbsolutePath();

	final Method meth1 = clazz1.getDeclaredMethod("ensureRocksDBIsLoaded", String.class);
	final Method meth2 = clazz2.getDeclaredMethod("ensureRocksDBIsLoaded", String.class);
	meth1.setAccessible(true);
	meth2.setAccessible(true);

	// if all is well, these methods can both complete successfully
	meth1.invoke(instance1, tempDir);
	meth2.invoke(instance2, tempDir);
}