Search in sources :

Example 1 with ChildFirstClassLoader

use of com.hazelcast.jet.impl.deployment.ChildFirstClassLoader in project hazelcast by hazelcast.

the class JobClassLoaderService method createProcessorClassLoaders.

private Map<String, ClassLoader> createProcessorClassLoaders(long jobId, JobConfig jobConfig, ClassLoader parent) {
    logger.fine("Create processor classloader map for job " + idToString(jobId));
    String customLibDir = nodeEngine.getProperties().getString(ClusterProperty.PROCESSOR_CUSTOM_LIB_DIR);
    Map<String, ClassLoader> classLoaderMap = new HashMap<>();
    for (Entry<String, List<String>> entry : jobConfig.getCustomClassPaths().entrySet()) {
        List<URL> list = entry.getValue().stream().map(jar -> {
            try {
                Path path = Paths.get(customLibDir, jar);
                return path.toUri().toURL();
            } catch (MalformedURLException e) {
                throw new JetException(e);
            }
        }).collect(Collectors.toList());
        URL[] urls = list.toArray(new URL[] {});
        classLoaderMap.put(entry.getKey(), new ChildFirstClassLoader(urls, parent));
    }
    return unmodifiableMap(classLoaderMap);
}
Also used : URL(java.net.URL) LoggingUtil.logFinest(com.hazelcast.jet.impl.util.LoggingUtil.logFinest) HashMap(java.util.HashMap) JetDelegatingClassLoader(com.hazelcast.jet.impl.deployment.JetDelegatingClassLoader) JetException(com.hazelcast.jet.JetException) ILogger(com.hazelcast.logging.ILogger) Map(java.util.Map) ChildFirstClassLoader(com.hazelcast.jet.impl.deployment.ChildFirstClassLoader) Nonnull(javax.annotation.Nonnull) Path(java.nio.file.Path) EnumSet(java.util.EnumSet) JetConfig(com.hazelcast.jet.config.JetConfig) HazelcastException(com.hazelcast.core.HazelcastException) NodeEngine(com.hazelcast.spi.impl.NodeEngine) MalformedURLException(java.net.MalformedURLException) JobConfig(com.hazelcast.jet.config.JobConfig) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IOException(java.io.IOException) PrivilegedAction(java.security.PrivilegedAction) Collectors(java.util.stream.Collectors) ClusterProperty(com.hazelcast.spi.properties.ClusterProperty) List(java.util.List) Util.idToString(com.hazelcast.jet.Util.idToString) Paths(java.nio.file.Paths) LoggingUtil.logFine(com.hazelcast.jet.impl.util.LoggingUtil.logFine) Entry(java.util.Map.Entry) JetClassLoader(com.hazelcast.jet.impl.deployment.JetClassLoader) Collections.unmodifiableMap(java.util.Collections.unmodifiableMap) AccessController(java.security.AccessController) Collections(java.util.Collections) Util(com.hazelcast.jet.Util) Path(java.nio.file.Path) MalformedURLException(java.net.MalformedURLException) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Util.idToString(com.hazelcast.jet.Util.idToString) JetException(com.hazelcast.jet.JetException) URL(java.net.URL) ChildFirstClassLoader(com.hazelcast.jet.impl.deployment.ChildFirstClassLoader) JetDelegatingClassLoader(com.hazelcast.jet.impl.deployment.JetDelegatingClassLoader) ChildFirstClassLoader(com.hazelcast.jet.impl.deployment.ChildFirstClassLoader) JetClassLoader(com.hazelcast.jet.impl.deployment.JetClassLoader) List(java.util.List)

Example 2 with ChildFirstClassLoader

use of com.hazelcast.jet.impl.deployment.ChildFirstClassLoader in project hazelcast by hazelcast.

the class JobClassLoaderService method tryRemoveClassloadersForJob.

/**
 * Try to remove and close/shutdown job classloader and any processor
 * classloaders for given job
 * <p>
 * We keep track of phases where the classloader is used and remove
 * the classloader only if there are no more phases left.
 */
public void tryRemoveClassloadersForJob(long jobId, JobPhase phase) {
    logFinest(logger, "Try remove classloaders for jobId=%s, phase=%s", idToString(jobId), phase);
    classLoaders.compute(jobId, (k, jobClassLoaders) -> {
        if (jobClassLoaders == null) {
            logger.warning("JobClassLoaders for jobId=" + idToString(jobId) + " already removed");
            return null;
        }
        int phaseCount = jobClassLoaders.removePhase(phase);
        if (phaseCount == 0) {
            logFinest(logger, "JobClassLoaders phaseCount = 0, removing classloaders for jobId=%s", idToString(jobId));
            Map<String, ClassLoader> processorCls = jobClassLoaders.processorCls();
            if (processorCls != null) {
                for (ClassLoader cl : processorCls.values()) {
                    try {
                        ((ChildFirstClassLoader) cl).close();
                    } catch (IOException e) {
                        logger.warning("Exception when closing processor classloader", e);
                    }
                }
            }
            // the class loader might not have been initialized if the job failed before that
            JetDelegatingClassLoader jobClassLoader = jobClassLoaders.jobClassLoader();
            jobClassLoader.shutdown();
            logFine(logger, "Finish JobClassLoaders phaseCount = 0," + " removing classloaders for jobId=%s", idToString(jobId));
            // Removes the item from the map
            return null;
        } else {
            logFinest(logger, "JobClassLoaders refCount > 0, NOT removing classloaders for jobId=%s", idToString(jobId));
            return jobClassLoaders;
        }
    });
}
Also used : JetDelegatingClassLoader(com.hazelcast.jet.impl.deployment.JetDelegatingClassLoader) ChildFirstClassLoader(com.hazelcast.jet.impl.deployment.ChildFirstClassLoader) JetDelegatingClassLoader(com.hazelcast.jet.impl.deployment.JetDelegatingClassLoader) ChildFirstClassLoader(com.hazelcast.jet.impl.deployment.ChildFirstClassLoader) JetClassLoader(com.hazelcast.jet.impl.deployment.JetClassLoader) Util.idToString(com.hazelcast.jet.Util.idToString) IOException(java.io.IOException)

Example 3 with ChildFirstClassLoader

use of com.hazelcast.jet.impl.deployment.ChildFirstClassLoader in project hazelcast by hazelcast.

the class Hazelcast3Proxy method newHazelcastInstance.

public HazelcastInstance newHazelcastInstance(String xmlConfig) {
    try {
        File hazelcastJar = locateVersion("3.12.12", new File("target"), false)[HAZELCAST_JAR_INDEX];
        URLClassLoader classLoader = new ChildFirstClassLoader(new URL[] { hazelcastJar.toURI().toURL() }, Hazelcast3Proxy.class.getClassLoader());
        Object config = buildHz3Config(xmlConfig, classLoader);
        Object hz3Instance = newHazelcast3Instance(classLoader, config);
        return proxyInstance(hz3Instance, HazelcastInstance.class);
    } catch (MalformedURLException | ClassNotFoundException | NoSuchMethodException | InvocationTargetException | InstantiationException | IllegalAccessException e) {
        throw new RuntimeException(e);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ChildFirstClassLoader(com.hazelcast.jet.impl.deployment.ChildFirstClassLoader) URLClassLoader(java.net.URLClassLoader) File(java.io.File)

Aggregations

ChildFirstClassLoader (com.hazelcast.jet.impl.deployment.ChildFirstClassLoader)3 Util.idToString (com.hazelcast.jet.Util.idToString)2 JetClassLoader (com.hazelcast.jet.impl.deployment.JetClassLoader)2 JetDelegatingClassLoader (com.hazelcast.jet.impl.deployment.JetDelegatingClassLoader)2 IOException (java.io.IOException)2 MalformedURLException (java.net.MalformedURLException)2 HazelcastException (com.hazelcast.core.HazelcastException)1 JetException (com.hazelcast.jet.JetException)1 Util (com.hazelcast.jet.Util)1 JetConfig (com.hazelcast.jet.config.JetConfig)1 JobConfig (com.hazelcast.jet.config.JobConfig)1 LoggingUtil.logFine (com.hazelcast.jet.impl.util.LoggingUtil.logFine)1 LoggingUtil.logFinest (com.hazelcast.jet.impl.util.LoggingUtil.logFinest)1 ILogger (com.hazelcast.logging.ILogger)1 NodeEngine (com.hazelcast.spi.impl.NodeEngine)1 ClusterProperty (com.hazelcast.spi.properties.ClusterProperty)1 File (java.io.File)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 URL (java.net.URL)1 URLClassLoader (java.net.URLClassLoader)1