Search in sources :

Example 16 with LocalizeResource

use of co.cask.cdap.internal.app.runtime.distributed.LocalizeResource in project cdap by caskdata.

the class SparkPackageUtils method prepareSpark2Framework.

/**
   * Prepares the Spark 2 framework on the location.
   *
   * @param sparkConf the spark configuration
   * @param locationFactory the {@link LocationFactory} for saving the spark framework jar
   * @param tempDir directory for temporary file creation
   * @return A {@link SparkFramework} containing information about the spark framework in localization context.
   * @throws IOException If failed to prepare the framework.
   */
private static SparkFramework prepareSpark2Framework(Properties sparkConf, LocationFactory locationFactory, File tempDir) throws IOException {
    String sparkYarnArchive = sparkConf.getProperty(SPARK_YARN_ARCHIVE);
    if (sparkYarnArchive != null) {
        URI sparkYarnArchiveURI = URI.create(sparkYarnArchive);
        if (locationFactory.getHomeLocation().toURI().getScheme().equals(sparkYarnArchiveURI.getScheme())) {
            Location frameworkLocation = locationFactory.create(URI.create(sparkYarnArchive));
            if (frameworkLocation.exists()) {
                return new SparkFramework(new LocalizeResource(resolveURI(frameworkLocation), true), SPARK_YARN_ARCHIVE);
            }
            LOG.warn("The location {} set by '{}' does not exist.", frameworkLocation, SPARK_YARN_ARCHIVE);
        }
    }
    // If spark.yarn.archive is not defined or doesn't exists, build a archive zip from local FS and upload it
    String sparkVersion = System.getenv(SPARK_VERSION);
    sparkVersion = sparkVersion == null ? SparkCompat.SPARK2_2_11.getCompat() : sparkVersion;
    String archiveName = "spark.archive-" + sparkVersion + ".zip";
    Location frameworkDir = locationFactory.create("/framework/spark");
    Location frameworkLocation = frameworkDir.append(archiveName);
    if (!frameworkLocation.exists()) {
        File archive = new File(tempDir, archiveName);
        try {
            try (ZipOutputStream zipOutput = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(archive)))) {
                zipOutput.setLevel(Deflater.NO_COMPRESSION);
                for (File file : getLocalSparkLibrary(SparkCompat.SPARK2_2_11)) {
                    zipOutput.putNextEntry(new ZipEntry(file.getName()));
                    Files.copy(file.toPath(), zipOutput);
                    zipOutput.closeEntry();
                }
            }
            // Upload spark archive to the framework location
            frameworkDir.mkdirs("755");
            try (OutputStream os = frameworkLocation.getOutputStream("644")) {
                Files.copy(archive.toPath(), os);
            }
        } finally {
            archive.delete();
        }
    }
    return new SparkFramework(new LocalizeResource(resolveURI(frameworkLocation), true), SPARK_YARN_ARCHIVE);
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) LocalizeResource(co.cask.cdap.internal.app.runtime.distributed.LocalizeResource) FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) ZipOutputStream(java.util.zip.ZipOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) URI(java.net.URI) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) Location(org.apache.twill.filesystem.Location)

Example 17 with LocalizeResource

use of co.cask.cdap.internal.app.runtime.distributed.LocalizeResource in project cdap by caskdata.

the class BasicSparkClientContext method localize.

@Override
public void localize(String name, URI uri, boolean archive) {
    try {
        URI actualURI = new URI(uri.getScheme(), uri.getAuthority(), uri.getPath(), uri.getQuery(), name);
        localizeResources.put(name, new LocalizeResource(actualURI, archive));
    } catch (URISyntaxException e) {
        // If it does though, there is nothing that clients can do to recover, so not propagating a checked exception.
        throw Throwables.propagate(e);
    }
}
Also used : LocalizeResource(co.cask.cdap.internal.app.runtime.distributed.LocalizeResource) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Aggregations

LocalizeResource (co.cask.cdap.internal.app.runtime.distributed.LocalizeResource)17 File (java.io.File)11 URI (java.net.URI)8 URISyntaxException (java.net.URISyntaxException)5 ZipOutputStream (java.util.zip.ZipOutputStream)4 Test (org.junit.Test)4 CConfiguration (co.cask.cdap.common.conf.CConfiguration)3 BufferedOutputStream (java.io.BufferedOutputStream)3 FileOutputStream (java.io.FileOutputStream)3 Path (java.nio.file.Path)3 HashMap (java.util.HashMap)3 ZipEntry (java.util.zip.ZipEntry)3 IOException (java.io.IOException)2 OutputStream (java.io.OutputStream)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 Configuration (org.apache.hadoop.conf.Configuration)2 Location (org.apache.twill.filesystem.Location)2 Resources (co.cask.cdap.api.Resources)1 ApplicationSpecification (co.cask.cdap.api.app.ApplicationSpecification)1