Search in sources :

Example 6 with Location

use of com.continuuity.weave.filesystem.Location in project weave by continuuity.

the class YarnWeavePreparer method saveKafka.

private void saveKafka(Map<String, LocalFile> localFiles) throws IOException {
    LOG.debug("Copy {}", Constants.Files.KAFKA);
    Location location = copyFromURL(getClass().getClassLoader().getResource(KAFKA_ARCHIVE), createTempLocation(Constants.Files.KAFKA));
    LOG.debug("Done {}", Constants.Files.KAFKA);
    localFiles.put(Constants.Files.KAFKA, createLocalFile(Constants.Files.KAFKA, location, true));
}
Also used : Location(com.continuuity.weave.filesystem.Location)

Example 7 with Location

use of com.continuuity.weave.filesystem.Location in project weave by continuuity.

the class YarnWeavePreparer method createContainerJar.

private void createContainerJar(ApplicationBundler bundler, Map<String, LocalFile> localFiles) throws IOException {
    try {
        Set<Class<?>> classes = Sets.newIdentityHashSet();
        classes.add(WeaveContainerMain.class);
        classes.addAll(dependencies);
        ClassLoader classLoader = getClassLoader();
        for (RuntimeSpecification spec : weaveSpec.getRunnables().values()) {
            classes.add(classLoader.loadClass(spec.getRunnableSpecification().getClassName()));
        }
        LOG.debug("Create and copy {}", Constants.Files.CONTAINER_JAR);
        Location location = createTempLocation(Constants.Files.CONTAINER_JAR);
        bundler.createBundle(location, classes, resources);
        LOG.debug("Done {}", Constants.Files.CONTAINER_JAR);
        localFiles.put(Constants.Files.CONTAINER_JAR, createLocalFile(Constants.Files.CONTAINER_JAR, location));
    } catch (ClassNotFoundException e) {
        throw Throwables.propagate(e);
    }
}
Also used : DefaultRuntimeSpecification(com.continuuity.weave.internal.DefaultRuntimeSpecification) RuntimeSpecification(com.continuuity.weave.api.RuntimeSpecification) Location(com.continuuity.weave.filesystem.Location)

Example 8 with Location

use of com.continuuity.weave.filesystem.Location in project weave by continuuity.

the class YarnWeavePreparer method saveLauncher.

/**
   * Creates the launcher.jar for launch the main application.
   */
private void saveLauncher(Map<String, LocalFile> localFiles) throws URISyntaxException, IOException {
    LOG.debug("Create and copy {}", Constants.Files.LAUNCHER_JAR);
    Location location = createTempLocation(Constants.Files.LAUNCHER_JAR);
    final String launcherName = WeaveLauncher.class.getName();
    // Create a jar file with the WeaveLauncher optionally a json serialized classpath.json in it.
    final JarOutputStream jarOut = new JarOutputStream(location.getOutputStream());
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    if (classLoader == null) {
        classLoader = getClass().getClassLoader();
    }
    Dependencies.findClassDependencies(classLoader, new Dependencies.ClassAcceptor() {

        @Override
        public boolean accept(String className, URL classUrl, URL classPathUrl) {
            Preconditions.checkArgument(className.startsWith(launcherName), "Launcher jar should not have dependencies: %s", className);
            try {
                jarOut.putNextEntry(new JarEntry(className.replace('.', '/') + ".class"));
                InputStream is = classUrl.openStream();
                try {
                    ByteStreams.copy(is, jarOut);
                } finally {
                    is.close();
                }
            } catch (IOException e) {
                throw Throwables.propagate(e);
            }
            return true;
        }
    }, WeaveLauncher.class.getName());
    try {
        if (!classPaths.isEmpty()) {
            jarOut.putNextEntry(new JarEntry("classpath"));
            jarOut.write(Joiner.on(':').join(classPaths).getBytes(Charsets.UTF_8));
        }
    } finally {
        jarOut.close();
    }
    LOG.debug("Done {}", Constants.Files.LAUNCHER_JAR);
    localFiles.put(Constants.Files.LAUNCHER_JAR, createLocalFile(Constants.Files.LAUNCHER_JAR, location));
}
Also used : InputStream(java.io.InputStream) JarOutputStream(java.util.jar.JarOutputStream) WeaveLauncher(com.continuuity.weave.launcher.WeaveLauncher) Dependencies(com.continuuity.weave.internal.utils.Dependencies) IOException(java.io.IOException) JarEntry(java.util.jar.JarEntry) URL(java.net.URL) Location(com.continuuity.weave.filesystem.Location)

Example 9 with Location

use of com.continuuity.weave.filesystem.Location in project weave by continuuity.

the class ApplicationBundlerTest method testFindDependencies.

@Test
public void testFindDependencies() throws IOException, ClassNotFoundException {
    Location location = new LocalLocationFactory(tmpDir.newFolder()).create("test.jar");
    // Create a jar file with by tracing dependency
    ApplicationBundler bundler = new ApplicationBundler(ImmutableList.<String>of());
    bundler.createBundle(location, ApplicationBundler.class);
    File targetDir = tmpDir.newFolder();
    unjar(new File(location.toURI()), targetDir);
    // Load the class back, it should be loaded by the custom classloader
    ClassLoader classLoader = createClassLoader(targetDir);
    Class<?> clz = classLoader.loadClass(ApplicationBundler.class.getName());
    Assert.assertSame(classLoader, clz.getClassLoader());
    // For system classes, they shouldn't be packaged, hence loaded by different classloader.
    clz = classLoader.loadClass(Object.class.getName());
    Assert.assertNotSame(classLoader, clz.getClassLoader());
}
Also used : URLClassLoader(java.net.URLClassLoader) LocalLocationFactory(com.continuuity.weave.filesystem.LocalLocationFactory) ApplicationBundler(com.continuuity.weave.internal.ApplicationBundler) File(java.io.File) Location(com.continuuity.weave.filesystem.Location) Test(org.junit.Test)

Example 10 with Location

use of com.continuuity.weave.filesystem.Location in project weave by continuuity.

the class YarnWeavePreparer method populateRunnableLocalFiles.

/**
   * Based on the given {@link WeaveSpecification}, upload LocalFiles to Yarn Cluster.
   * @param weaveSpec The {@link WeaveSpecification} for populating resource.
   * @param localFiles A Multimap to store runnable name to transformed LocalFiles.
   * @throws IOException
   */
private void populateRunnableLocalFiles(WeaveSpecification weaveSpec, Multimap<String, LocalFile> localFiles) throws IOException {
    LOG.debug("Populating Runnable LocalFiles");
    for (Map.Entry<String, RuntimeSpecification> entry : weaveSpec.getRunnables().entrySet()) {
        String runnableName = entry.getKey();
        for (LocalFile localFile : entry.getValue().getLocalFiles()) {
            Location location;
            URI uri = localFile.getURI();
            if ("hdfs".equals(uri.getScheme())) {
                // Assuming the location factory is HDFS one. If it is not, it will failed, which is the correct behavior.
                location = locationFactory.create(uri);
            } else {
                URL url = uri.toURL();
                LOG.debug("Create and copy {} : {}", runnableName, url);
                // Preserves original suffix for expansion.
                location = copyFromURL(url, createTempLocation(Paths.appendSuffix(url.getFile(), localFile.getName())));
                LOG.debug("Done {} : {}", runnableName, url);
            }
            localFiles.put(runnableName, new DefaultLocalFile(localFile.getName(), location.toURI(), location.lastModified(), location.length(), localFile.isArchive(), localFile.getPattern()));
        }
    }
    LOG.debug("Done Runnable LocalFiles");
}
Also used : LocalFile(com.continuuity.weave.api.LocalFile) DefaultLocalFile(com.continuuity.weave.internal.DefaultLocalFile) DefaultLocalFile(com.continuuity.weave.internal.DefaultLocalFile) DefaultRuntimeSpecification(com.continuuity.weave.internal.DefaultRuntimeSpecification) RuntimeSpecification(com.continuuity.weave.api.RuntimeSpecification) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) URI(java.net.URI) URL(java.net.URL) Location(com.continuuity.weave.filesystem.Location)

Aggregations

Location (com.continuuity.weave.filesystem.Location)13 OutputStreamWriter (java.io.OutputStreamWriter)4 Writer (java.io.Writer)4 RuntimeSpecification (com.continuuity.weave.api.RuntimeSpecification)3 DefaultRuntimeSpecification (com.continuuity.weave.internal.DefaultRuntimeSpecification)3 IOException (java.io.IOException)3 LocalFile (com.continuuity.weave.api.LocalFile)2 DefaultLocalFile (com.continuuity.weave.internal.DefaultLocalFile)2 BufferedInputStream (java.io.BufferedInputStream)2 DataInputStream (java.io.DataInputStream)2 URL (java.net.URL)2 Credentials (org.apache.hadoop.security.Credentials)2 EventHandlerSpecification (com.continuuity.weave.api.EventHandlerSpecification)1 LocalLocationFactory (com.continuuity.weave.filesystem.LocalLocationFactory)1 ApplicationBundler (com.continuuity.weave.internal.ApplicationBundler)1 DefaultWeaveSpecification (com.continuuity.weave.internal.DefaultWeaveSpecification)1 LogOnlyEventHandler (com.continuuity.weave.internal.LogOnlyEventHandler)1 LocalFileCodec (com.continuuity.weave.internal.json.LocalFileCodec)1 Dependencies (com.continuuity.weave.internal.utils.Dependencies)1 WeaveLauncher (com.continuuity.weave.launcher.WeaveLauncher)1