Search in sources :

Example 1 with SubmoduleClassLoader

use of org.apache.flink.core.classloading.SubmoduleClassLoader in project flink by apache.

the class AkkaRpcSystemLoader method loadRpcSystem.

@Override
public RpcSystem loadRpcSystem(Configuration config) {
    try {
        final ClassLoader flinkClassLoader = RpcSystem.class.getClassLoader();
        final Path tmpDirectory = Paths.get(ConfigurationUtils.parseTempDirectories(config)[0]);
        Files.createDirectories(tmpDirectory);
        final Path tempFile = Files.createFile(tmpDirectory.resolve("flink-rpc-akka_" + UUID.randomUUID() + ".jar"));
        final InputStream resourceStream = flinkClassLoader.getResourceAsStream(FLINK_RPC_AKKA_FAT_JAR);
        if (resourceStream == null) {
            throw new RpcLoaderException(String.format("Akka RPC system could not be found. If this happened while running a test in the IDE, " + "run '%s' on the command-line, " + "or add a test dependency on the flink-rpc-akka-loader test-jar.", HINT_USAGE));
        }
        IOUtils.copyBytes(resourceStream, Files.newOutputStream(tempFile));
        final SubmoduleClassLoader submoduleClassLoader = new SubmoduleClassLoader(new URL[] { tempFile.toUri().toURL() }, flinkClassLoader);
        return new CleanupOnCloseRpcSystem(ServiceLoader.load(RpcSystem.class, submoduleClassLoader).iterator().next(), submoduleClassLoader, tempFile);
    } catch (IOException e) {
        throw new RuntimeException("Could not initialize RPC system.", e);
    }
}
Also used : Path(java.nio.file.Path) InputStream(java.io.InputStream) SubmoduleClassLoader(org.apache.flink.core.classloading.SubmoduleClassLoader) RpcLoaderException(org.apache.flink.runtime.rpc.exceptions.RpcLoaderException) SubmoduleClassLoader(org.apache.flink.core.classloading.SubmoduleClassLoader) IOException(java.io.IOException) RpcSystem(org.apache.flink.runtime.rpc.RpcSystem)

Example 2 with SubmoduleClassLoader

use of org.apache.flink.core.classloading.SubmoduleClassLoader in project flink by apache.

the class FallbackAkkaRpcSystemLoader method loadRpcSystem.

@Override
public RpcSystem loadRpcSystem(Configuration config) {
    try {
        LOG.debug("Using Fallback AkkaRpcSystemLoader; this loader will invoke maven to retrieve the dependencies of flink-rpc-akka.");
        final ClassLoader flinkClassLoader = RpcSystem.class.getClassLoader();
        // flink-rpc/flink-rpc-akka
        final Path akkaRpcModuleDirectory = findAkkaRpcModuleDirectory(getCurrentWorkingDirectory());
        // flink-rpc/flink-rpc-akka/target/classes
        final Path akkaRpcModuleClassesDirectory = akkaRpcModuleDirectory.resolve(Paths.get("target", "classes"));
        // flink-rpc/flink-rpc-akka/target/dependencies
        final Path akkaRpcModuleDependenciesDirectory = akkaRpcModuleDirectory.resolve(Paths.get("target", "dependencies"));
        if (!Files.exists(akkaRpcModuleDependenciesDirectory)) {
            int exitCode = downloadDependencies(akkaRpcModuleDirectory, akkaRpcModuleDependenciesDirectory);
            if (exitCode != 0) {
                throw new RpcLoaderException("Could not download dependencies of flink-rpc-akka, please see the log output for details.");
            }
        } else {
            LOG.debug("Re-using previously downloaded flink-rpc-akka dependencies. If you are experiencing strange issues, try clearing '{}'.", akkaRpcModuleDependenciesDirectory);
        }
        // assemble URL collection containing target/classes and each jar
        final List<URL> urls = new ArrayList<>();
        urls.add(akkaRpcModuleClassesDirectory.toUri().toURL());
        try (final Stream<Path> files = Files.list(akkaRpcModuleDependenciesDirectory)) {
            final List<Path> collect = files.filter(path -> path.getFileName().toString().endsWith(".jar")).collect(Collectors.toList());
            for (Path path : collect) {
                urls.add(path.toUri().toURL());
            }
        }
        final SubmoduleClassLoader submoduleClassLoader = new SubmoduleClassLoader(urls.toArray(new URL[0]), flinkClassLoader);
        return new CleanupOnCloseRpcSystem(ServiceLoader.load(RpcSystem.class, submoduleClassLoader).iterator().next(), submoduleClassLoader, null);
    } catch (Exception e) {
        throw new RpcLoaderException(String.format("Could not initialize RPC system. Run '%s' on the command-line instead.", AkkaRpcSystemLoader.HINT_USAGE), e);
    }
}
Also used : Path(java.nio.file.Path) SubmoduleClassLoader(org.apache.flink.core.classloading.SubmoduleClassLoader) Logger(org.slf4j.Logger) Files(java.nio.file.Files) URL(java.net.URL) Configuration(org.apache.flink.configuration.Configuration) LoggerFactory(org.slf4j.LoggerFactory) IOException(java.io.IOException) ServiceLoader(java.util.ServiceLoader) RpcLoaderException(org.apache.flink.runtime.rpc.exceptions.RpcLoaderException) OperatingSystem(org.apache.flink.util.OperatingSystem) Collectors(java.util.stream.Collectors) RpcSystemLoader(org.apache.flink.runtime.rpc.RpcSystemLoader) ArrayList(java.util.ArrayList) List(java.util.List) RpcSystem(org.apache.flink.runtime.rpc.RpcSystem) Stream(java.util.stream.Stream) Paths(java.nio.file.Paths) Optional(java.util.Optional) Path(java.nio.file.Path) ArrayList(java.util.ArrayList) RpcLoaderException(org.apache.flink.runtime.rpc.exceptions.RpcLoaderException) RpcSystem(org.apache.flink.runtime.rpc.RpcSystem) URL(java.net.URL) IOException(java.io.IOException) RpcLoaderException(org.apache.flink.runtime.rpc.exceptions.RpcLoaderException) SubmoduleClassLoader(org.apache.flink.core.classloading.SubmoduleClassLoader) SubmoduleClassLoader(org.apache.flink.core.classloading.SubmoduleClassLoader)

Aggregations

IOException (java.io.IOException)2 Path (java.nio.file.Path)2 SubmoduleClassLoader (org.apache.flink.core.classloading.SubmoduleClassLoader)2 RpcSystem (org.apache.flink.runtime.rpc.RpcSystem)2 RpcLoaderException (org.apache.flink.runtime.rpc.exceptions.RpcLoaderException)2 InputStream (java.io.InputStream)1 URL (java.net.URL)1 Files (java.nio.file.Files)1 Paths (java.nio.file.Paths)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Optional (java.util.Optional)1 ServiceLoader (java.util.ServiceLoader)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 Configuration (org.apache.flink.configuration.Configuration)1 RpcSystemLoader (org.apache.flink.runtime.rpc.RpcSystemLoader)1 OperatingSystem (org.apache.flink.util.OperatingSystem)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1