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

The following examples show how to use org.apache.flink.runtime.execution.librarycache.FlinkUserCodeClassLoaders#parentFirst() . 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: ClassLoaderTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example 2
Source File: ClassLoaderTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example 3
Source File: ExecutionContext.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
public ExecutionContext(Environment defaultEnvironment, SessionContext sessionContext, List<URL> dependencies,
		Configuration flinkConfig, Options commandLineOptions, List<CustomCommandLine<?>> availableCommandLines) {
	this.sessionContext = sessionContext.copy(); // create internal copy because session context is mutable
	this.mergedEnv = Environment.merge(defaultEnvironment, sessionContext.getEnvironment());
	this.dependencies = dependencies;
	this.flinkConfig = flinkConfig;

	// create class loader
	classLoader = FlinkUserCodeClassLoaders.parentFirst(
		dependencies.toArray(new URL[dependencies.size()]),
		this.getClass().getClassLoader());

	// create table sources & sinks.
	tableSources = new HashMap<>();
	tableSinks = new HashMap<>();
	mergedEnv.getTables().forEach((name, entry) -> {
		if (entry instanceof SourceTableEntry || entry instanceof SourceSinkTableEntry) {
			tableSources.put(name, createTableSource(mergedEnv.getExecution(), entry.asMap(), classLoader));
		}
		if (entry instanceof SinkTableEntry || entry instanceof SourceSinkTableEntry) {
			tableSinks.put(name, createTableSink(mergedEnv.getExecution(), entry.asMap(), classLoader));
		}
	});

	// create user-defined functions
	functions = new HashMap<>();
	mergedEnv.getFunctions().forEach((name, entry) -> {
		final UserDefinedFunction function = FunctionService.createFunction(entry.getDescriptor(), classLoader, false);
		functions.put(name, function);
	});

	// convert deployment options into command line options that describe a cluster
	commandLine = createCommandLine(mergedEnv.getDeployment(), commandLineOptions);
	activeCommandLine = findActiveCommandLine(availableCommandLines, commandLine);
	runOptions = createRunOptions(commandLine);
	clusterId = activeCommandLine.getClusterId(commandLine);
	clusterSpec = createClusterSpecification(activeCommandLine, commandLine);
}
 
Example 4
Source File: JobWithJars.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
public static ClassLoader buildUserCodeClassLoader(List<URL> jars, List<URL> classpaths, ClassLoader parent) {
	URL[] urls = new URL[jars.size() + classpaths.size()];
	for (int i = 0; i < jars.size(); i++) {
		urls[i] = jars.get(i);
	}
	for (int i = 0; i < classpaths.size(); i++) {
		urls[i + jars.size()] = classpaths.get(i);
	}
	return FlinkUserCodeClassLoaders.parentFirst(urls, parent);
}
 
Example 5
Source File: JobWithJars.java    From flink with Apache License 2.0 5 votes vote down vote up
public static ClassLoader buildUserCodeClassLoader(List<URL> jars, List<URL> classpaths, ClassLoader parent) {
	URL[] urls = new URL[jars.size() + classpaths.size()];
	for (int i = 0; i < jars.size(); i++) {
		urls[i] = jars.get(i);
	}
	for (int i = 0; i < classpaths.size(); i++) {
		urls[i + jars.size()] = classpaths.get(i);
	}
	return FlinkUserCodeClassLoaders.parentFirst(urls, parent);
}
 
Example 6
Source File: StatefulFunctionsJob.java    From stateful-functions with Apache License 2.0 5 votes vote down vote up
private static void setDefaultContextClassLoaderIfAbsent() {
  ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
  if (classLoader == null) {
    URLClassLoader flinkClassLoader =
        FlinkUserCodeClassLoaders.parentFirst(
            new URL[0], StatefulFunctionsJob.class.getClassLoader());
    Thread.currentThread().setContextClassLoader(flinkClassLoader);
  }
}
 
Example 7
Source File: StatefulFunctionsJob.java    From flink-statefun with Apache License 2.0 5 votes vote down vote up
private static void setDefaultContextClassLoaderIfAbsent() {
  ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
  if (classLoader == null) {
    URLClassLoader flinkClassLoader =
        FlinkUserCodeClassLoaders.parentFirst(
            new URL[0], StatefulFunctionsJob.class.getClassLoader());
    Thread.currentThread().setContextClassLoader(flinkClassLoader);
  }
}
 
Example 8
Source File: ExecutionContext.java    From flink with Apache License 2.0 4 votes vote down vote up
public ExecutionContext(Environment defaultEnvironment, SessionContext sessionContext, List<URL> dependencies,
		Configuration flinkConfig, Options commandLineOptions, List<CustomCommandLine<?>> availableCommandLines) {
	this.sessionContext = sessionContext.copy(); // create internal copy because session context is mutable
	this.mergedEnv = Environment.merge(defaultEnvironment, sessionContext.getEnvironment());
	this.dependencies = dependencies;
	this.flinkConfig = flinkConfig;

	// create class loader
	classLoader = FlinkUserCodeClassLoaders.parentFirst(
		dependencies.toArray(new URL[dependencies.size()]),
		this.getClass().getClassLoader());

	// create catalogs
	catalogs = new LinkedHashMap<>();
	mergedEnv.getCatalogs().forEach((name, entry) ->
		catalogs.put(name, createCatalog(name, entry.asMap(), classLoader))
	);

	// create table sources & sinks.
	tableSources = new LinkedHashMap<>();
	tableSinks = new LinkedHashMap<>();
	mergedEnv.getTables().forEach((name, entry) -> {
		if (entry instanceof SourceTableEntry || entry instanceof SourceSinkTableEntry) {
			tableSources.put(name, createTableSource(mergedEnv.getExecution(), entry.asMap(), classLoader));
		}
		if (entry instanceof SinkTableEntry || entry instanceof SourceSinkTableEntry) {
			tableSinks.put(name, createTableSink(mergedEnv.getExecution(), entry.asMap(), classLoader));
		}
	});

	// create user-defined functions
	functions = new LinkedHashMap<>();
	mergedEnv.getFunctions().forEach((name, entry) -> {
		final UserDefinedFunction function = FunctionService.createFunction(entry.getDescriptor(), classLoader, false);
		functions.put(name, function);
	});

	// convert deployment options into command line options that describe a cluster
	commandLine = createCommandLine(mergedEnv.getDeployment(), commandLineOptions);
	activeCommandLine = findActiveCommandLine(availableCommandLines, commandLine);
	runOptions = createRunOptions(commandLine);
	clusterId = activeCommandLine.getClusterId(commandLine);
	clusterSpec = createClusterSpecification(activeCommandLine, commandLine);
}