Search in sources :

Example 1 with LocalFile

use of org.apache.twill.api.LocalFile in project cdap by caskdata.

the class AbstractRuntimeTwillPreparer method populateRunnableLocalFiles.

/**
 * Based on the given {@link TwillSpecification}, copy file to local filesystem.
 * @param spec The {@link TwillSpecification} for populating resource.
 */
private Map<String, Collection<LocalFile>> populateRunnableLocalFiles(TwillSpecification spec, Path stagingDir) throws IOException {
    Map<String, Collection<LocalFile>> localFiles = new HashMap<>();
    LOG.debug("Populating Runnable LocalFiles");
    for (Map.Entry<String, RuntimeSpecification> entry : spec.getRunnables().entrySet()) {
        String runnableName = entry.getKey();
        for (LocalFile localFile : entry.getValue().getLocalFiles()) {
            LocalFile resolvedLocalFile = resolveLocalFile(localFile, stagingDir);
            localFiles.computeIfAbsent(runnableName, s -> new ArrayList<>()).add(resolvedLocalFile);
            LOG.debug("Added file {}", resolvedLocalFile.getURI());
        }
    }
    LOG.debug("Done Runnable LocalFiles");
    return localFiles;
}
Also used : ArrayListMultimap(com.google.common.collect.ArrayListMultimap) Arrays(java.util.Arrays) ListMultimap(com.google.common.collect.ListMultimap) URL(java.net.URL) TwillPreparer(org.apache.twill.api.TwillPreparer) LoggerFactory(org.slf4j.LoggerFactory) TimeoutException(java.util.concurrent.TimeoutException) SecureStore(org.apache.twill.api.SecureStore) DirectoryStream(java.nio.file.DirectoryStream) DefaultLocalFile(org.apache.twill.internal.DefaultLocalFile) Configuration(org.apache.hadoop.conf.Configuration) Map(java.util.Map) Cancellable(org.apache.twill.common.Cancellable) URI(java.net.URI) Path(java.nio.file.Path) EventHandlerSpecification(org.apache.twill.api.EventHandlerSpecification) RuntimeSpecification(org.apache.twill.api.RuntimeSpecification) TwillController(org.apache.twill.api.TwillController) LocalFile(org.apache.twill.api.LocalFile) Collection(java.util.Collection) DefaultRuntimeSpecification(org.apache.twill.internal.DefaultRuntimeSpecification) Set(java.util.Set) LoggingContextAccessor(io.cdap.cdap.common.logging.LoggingContextAccessor) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) LocationCache(org.apache.twill.internal.io.LocationCache) StandardCharsets(java.nio.charset.StandardCharsets) LoggingContext(io.cdap.cdap.common.logging.LoggingContext) Objects(java.util.Objects) List(java.util.List) ArgumentsCodec(org.apache.twill.internal.json.ArgumentsCodec) JvmOptions(org.apache.twill.internal.JvmOptions) Writer(java.io.Writer) DirUtils(io.cdap.cdap.common.utils.DirUtils) LogHandler(org.apache.twill.api.logging.LogHandler) Joiner(com.google.common.base.Joiner) LogEntry(org.apache.twill.api.logging.LogEntry) Iterables(com.google.common.collect.Iterables) Location(org.apache.twill.filesystem.Location) Paths(org.apache.twill.internal.utils.Paths) Hashing(com.google.common.hash.Hashing) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) StandardCopyOption(java.nio.file.StandardCopyOption) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) JarEntry(java.util.jar.JarEntry) ProgramRunId(io.cdap.cdap.proto.id.ProgramRunId) TwillRuntimeSpecification(org.apache.twill.internal.TwillRuntimeSpecification) ProgramOptions(io.cdap.cdap.app.runtime.ProgramOptions) TwillRuntimeSpecificationAdapter(org.apache.twill.internal.json.TwillRuntimeSpecificationAdapter) Locations(io.cdap.cdap.common.io.Locations) Constants(org.apache.twill.internal.Constants) Hasher(com.google.common.hash.Hasher) ApplicationBundler(org.apache.twill.internal.ApplicationBundler) JarOutputStream(java.util.jar.JarOutputStream) LoggingContextHelper(io.cdap.cdap.logging.context.LoggingContextHelper) ClassAcceptor(org.apache.twill.api.ClassAcceptor) Logger(org.slf4j.Logger) Files(java.nio.file.Files) RunIds(io.cdap.cdap.common.app.RunIds) IOException(java.io.IOException) LocationFactory(org.apache.twill.filesystem.LocationFactory) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) Maps(com.google.common.collect.Maps) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) CConfiguration(io.cdap.cdap.common.conf.CConfiguration) LogOnlyEventHandler(org.apache.twill.internal.LogOnlyEventHandler) DefaultTwillSpecification(org.apache.twill.internal.DefaultTwillSpecification) Preconditions(com.google.common.base.Preconditions) TwillSpecification(org.apache.twill.api.TwillSpecification) Arguments(org.apache.twill.internal.Arguments) Collections(java.util.Collections) InputStream(java.io.InputStream) DefaultLocalFile(org.apache.twill.internal.DefaultLocalFile) LocalFile(org.apache.twill.api.LocalFile) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) Collection(java.util.Collection) RuntimeSpecification(org.apache.twill.api.RuntimeSpecification) DefaultRuntimeSpecification(org.apache.twill.internal.DefaultRuntimeSpecification) TwillRuntimeSpecification(org.apache.twill.internal.TwillRuntimeSpecification) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with LocalFile

use of org.apache.twill.api.LocalFile in project cdap by caskdata.

the class AbstractRuntimeTwillPreparer method saveSpecification.

private TwillRuntimeSpecification saveSpecification(TwillSpecification spec, Path targetFile, Path stagingDir) throws IOException {
    final Map<String, Collection<LocalFile>> runnableLocalFiles = populateRunnableLocalFiles(spec, stagingDir);
    // Rewrite LocalFiles inside twillSpec
    Map<String, RuntimeSpecification> runtimeSpec = spec.getRunnables().entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> {
        RuntimeSpecification value = e.getValue();
        return new DefaultRuntimeSpecification(value.getName(), value.getRunnableSpecification(), value.getResourceSpecification(), runnableLocalFiles.getOrDefault(e.getKey(), Collections.emptyList()));
    }));
    // Serialize into a local temp file.
    LOG.debug("Creating {}", targetFile);
    try (Writer writer = Files.newBufferedWriter(targetFile, StandardCharsets.UTF_8)) {
        EventHandlerSpecification eventHandler = spec.getEventHandler();
        if (eventHandler == null) {
            eventHandler = new LogOnlyEventHandler().configure();
        }
        TwillSpecification newTwillSpec = new DefaultTwillSpecification(spec.getName(), runtimeSpec, spec.getOrders(), spec.getPlacementPolicies(), eventHandler);
        Map<String, String> configMap = Maps.newHashMap();
        for (Map.Entry<String, String> entry : hConf) {
            if (entry.getKey().startsWith("twill.")) {
                configMap.put(entry.getKey(), entry.getValue());
            }
        }
        TwillRuntimeSpecification twillRuntimeSpec = new TwillRuntimeSpecification(newTwillSpec, "", URI.create("."), "", RunIds.fromString(programRunId.getRun()), twillSpec.getName(), null, logLevels, maxRetries, configMap, runnableConfigs);
        TwillRuntimeSpecificationAdapter.create().toJson(twillRuntimeSpec, writer);
        LOG.debug("Done {}", targetFile);
        return twillRuntimeSpec;
    }
}
Also used : ArrayListMultimap(com.google.common.collect.ArrayListMultimap) Arrays(java.util.Arrays) ListMultimap(com.google.common.collect.ListMultimap) URL(java.net.URL) TwillPreparer(org.apache.twill.api.TwillPreparer) LoggerFactory(org.slf4j.LoggerFactory) TimeoutException(java.util.concurrent.TimeoutException) SecureStore(org.apache.twill.api.SecureStore) DirectoryStream(java.nio.file.DirectoryStream) DefaultLocalFile(org.apache.twill.internal.DefaultLocalFile) Configuration(org.apache.hadoop.conf.Configuration) Map(java.util.Map) Cancellable(org.apache.twill.common.Cancellable) URI(java.net.URI) Path(java.nio.file.Path) EventHandlerSpecification(org.apache.twill.api.EventHandlerSpecification) RuntimeSpecification(org.apache.twill.api.RuntimeSpecification) TwillController(org.apache.twill.api.TwillController) LocalFile(org.apache.twill.api.LocalFile) Collection(java.util.Collection) DefaultRuntimeSpecification(org.apache.twill.internal.DefaultRuntimeSpecification) Set(java.util.Set) LoggingContextAccessor(io.cdap.cdap.common.logging.LoggingContextAccessor) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) LocationCache(org.apache.twill.internal.io.LocationCache) StandardCharsets(java.nio.charset.StandardCharsets) LoggingContext(io.cdap.cdap.common.logging.LoggingContext) Objects(java.util.Objects) List(java.util.List) ArgumentsCodec(org.apache.twill.internal.json.ArgumentsCodec) JvmOptions(org.apache.twill.internal.JvmOptions) Writer(java.io.Writer) DirUtils(io.cdap.cdap.common.utils.DirUtils) LogHandler(org.apache.twill.api.logging.LogHandler) Joiner(com.google.common.base.Joiner) LogEntry(org.apache.twill.api.logging.LogEntry) Iterables(com.google.common.collect.Iterables) Location(org.apache.twill.filesystem.Location) Paths(org.apache.twill.internal.utils.Paths) Hashing(com.google.common.hash.Hashing) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) StandardCopyOption(java.nio.file.StandardCopyOption) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) JarEntry(java.util.jar.JarEntry) ProgramRunId(io.cdap.cdap.proto.id.ProgramRunId) TwillRuntimeSpecification(org.apache.twill.internal.TwillRuntimeSpecification) ProgramOptions(io.cdap.cdap.app.runtime.ProgramOptions) TwillRuntimeSpecificationAdapter(org.apache.twill.internal.json.TwillRuntimeSpecificationAdapter) Locations(io.cdap.cdap.common.io.Locations) Constants(org.apache.twill.internal.Constants) Hasher(com.google.common.hash.Hasher) ApplicationBundler(org.apache.twill.internal.ApplicationBundler) JarOutputStream(java.util.jar.JarOutputStream) LoggingContextHelper(io.cdap.cdap.logging.context.LoggingContextHelper) ClassAcceptor(org.apache.twill.api.ClassAcceptor) Logger(org.slf4j.Logger) Files(java.nio.file.Files) RunIds(io.cdap.cdap.common.app.RunIds) IOException(java.io.IOException) LocationFactory(org.apache.twill.filesystem.LocationFactory) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) Maps(com.google.common.collect.Maps) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) CConfiguration(io.cdap.cdap.common.conf.CConfiguration) LogOnlyEventHandler(org.apache.twill.internal.LogOnlyEventHandler) DefaultTwillSpecification(org.apache.twill.internal.DefaultTwillSpecification) Preconditions(com.google.common.base.Preconditions) TwillSpecification(org.apache.twill.api.TwillSpecification) Arguments(org.apache.twill.internal.Arguments) Collections(java.util.Collections) InputStream(java.io.InputStream) DefaultRuntimeSpecification(org.apache.twill.internal.DefaultRuntimeSpecification) DefaultTwillSpecification(org.apache.twill.internal.DefaultTwillSpecification) EventHandlerSpecification(org.apache.twill.api.EventHandlerSpecification) RuntimeSpecification(org.apache.twill.api.RuntimeSpecification) DefaultRuntimeSpecification(org.apache.twill.internal.DefaultRuntimeSpecification) TwillRuntimeSpecification(org.apache.twill.internal.TwillRuntimeSpecification) DefaultTwillSpecification(org.apache.twill.internal.DefaultTwillSpecification) TwillSpecification(org.apache.twill.api.TwillSpecification) LogOnlyEventHandler(org.apache.twill.internal.LogOnlyEventHandler) TwillRuntimeSpecification(org.apache.twill.internal.TwillRuntimeSpecification) Collection(java.util.Collection) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Writer(java.io.Writer)

Example 3 with LocalFile

use of org.apache.twill.api.LocalFile in project cdap by caskdata.

the class AbstractRuntimeTwillPreparer method resolveLocalFile.

private LocalFile resolveLocalFile(LocalFile localFile, Path stagingDir) throws IOException {
    URI uri = localFile.getURI();
    String scheme = uri.getScheme();
    // If local file, resolve the last modified time and the file size
    if (scheme == null || "file".equals(scheme)) {
        File file = new File(uri.getPath());
        return new DefaultLocalFile(localFile.getName(), uri, file.lastModified(), file.length(), localFile.isArchive(), localFile.getPattern());
    }
    // If have the same scheme as the location factory, resolve time and size using Location
    if (Objects.equals(locationFactory.getHomeLocation().toURI().getScheme(), scheme)) {
        Location location = locationFactory.create(uri);
        return new DefaultLocalFile(localFile.getName(), uri, location.lastModified(), location.length(), localFile.isArchive(), localFile.getPattern());
    }
    // For other cases, attempt to save the URI content to local file, using support URLSteamHandler
    try (InputStream input = uri.toURL().openStream()) {
        Path tempFile = Files.createTempFile(stagingDir, localFile.getName(), Paths.getExtension(localFile.getName()));
        Files.copy(input, tempFile, StandardCopyOption.REPLACE_EXISTING);
        BasicFileAttributes attrs = Files.readAttributes(tempFile, BasicFileAttributes.class);
        return new DefaultLocalFile(localFile.getName(), tempFile.toUri(), attrs.lastModifiedTime().toMillis(), attrs.size(), localFile.isArchive(), localFile.getPattern());
    }
}
Also used : Path(java.nio.file.Path) InputStream(java.io.InputStream) DefaultLocalFile(org.apache.twill.internal.DefaultLocalFile) URI(java.net.URI) DefaultLocalFile(org.apache.twill.internal.DefaultLocalFile) LocalFile(org.apache.twill.api.LocalFile) File(java.io.File) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) Location(org.apache.twill.filesystem.Location)

Example 4 with LocalFile

use of org.apache.twill.api.LocalFile in project cdap by caskdata.

the class RuntimeJobTwillPreparer method launch.

@Override
protected void launch(TwillRuntimeSpecification twillRuntimeSpec, RuntimeSpecification runtimeSpec, JvmOptions jvmOptions, Map<String, String> environments, Map<String, LocalFile> localFiles, TimeoutChecker timeoutChecker) throws Exception {
    try (RuntimeJobManager jobManager = jobManagerSupplier.get()) {
        timeoutChecker.throwIfTimeout();
        Map<String, LocalFile> localizeFiles = new HashMap<>(localFiles);
        for (Map.Entry<String, Location> secretFile : secretFiles.entrySet()) {
            Location secretFileLocation = secretFile.getValue();
            localizeFiles.put(secretFile.getKey(), new DefaultLocalFile(secretFile.getKey(), secretFileLocation.toURI(), secretFileLocation.lastModified(), secretFileLocation.length(), false, null));
        }
        RuntimeJobInfo runtimeJobInfo = createRuntimeJobInfo(runtimeSpec, localizeFiles, jvmOptions.getRunnableExtraOptions(runtimeSpec.getName()));
        LOG.info("Starting runnable {} for runId {} with job manager.", runtimeSpec.getName(), getProgramRunId());
        // launch job using job manager
        jobManager.launch(runtimeJobInfo);
    }
}
Also used : DefaultLocalFile(org.apache.twill.internal.DefaultLocalFile) LocalFile(org.apache.twill.api.LocalFile) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) RuntimeJobManager(io.cdap.cdap.runtime.spi.runtimejob.RuntimeJobManager) DefaultRuntimeJobInfo(io.cdap.cdap.internal.app.runtime.distributed.runtimejob.DefaultRuntimeJobInfo) RuntimeJobInfo(io.cdap.cdap.runtime.spi.runtimejob.RuntimeJobInfo) DefaultLocalFile(org.apache.twill.internal.DefaultLocalFile) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Location(org.apache.twill.filesystem.Location)

Example 5 with LocalFile

use of org.apache.twill.api.LocalFile in project cdap by caskdata.

the class TetheringRuntimeJobManager method getLocalFileAsCompressedBytes.

/**
 * Use GZIPOutputStream to compress the LocalFile
 */
@VisibleForTesting
byte[] getLocalFileAsCompressedBytes(LocalFile localFile) throws IOException {
    File file = new File(localFile.getURI());
    // use 500kb buffer
    byte[] buffer = new byte[1024 * 500];
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try (GZIPOutputStream os = new GZIPOutputStream(baos);
        FileInputStream fis = new FileInputStream(file)) {
        int length;
        while ((length = fis.read(buffer)) > 0) {
            os.write(buffer, 0, length);
        }
    }
    return baos.toByteArray();
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) LocalFile(org.apache.twill.api.LocalFile) File(java.io.File) FileInputStream(java.io.FileInputStream) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

LocalFile (org.apache.twill.api.LocalFile)15 DefaultLocalFile (org.apache.twill.internal.DefaultLocalFile)11 File (java.io.File)9 URI (java.net.URI)8 Location (org.apache.twill.filesystem.Location)7 InputStream (java.io.InputStream)6 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 Map (java.util.Map)6 Collection (java.util.Collection)5 LinkedHashMap (java.util.LinkedHashMap)5 IOException (java.io.IOException)4 Path (java.nio.file.Path)4 Hashing (com.google.common.hash.Hashing)3 Writer (java.io.Writer)3 URL (java.net.URL)3 StandardCharsets (java.nio.charset.StandardCharsets)3 DirectoryStream (java.nio.file.DirectoryStream)3 Files (java.nio.file.Files)3 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)3