Search in sources :

Example 1 with FileSystem

use of org.flyte.jflyte.api.FileSystem in project flytekit-java by flyteorg.

the class PackageLoader method loadPackage.

private static ClassLoader loadPackage(Map<String, FileSystem> fileSystems, List<Artifact> artifacts) {
    try {
        Path tmp = Files.createTempDirectory("tasks");
        for (Artifact artifact : artifacts) {
            FileSystem fileSystem = FileSystemLoader.getFileSystem(fileSystems, artifact.location());
            try (ReadableByteChannel reader = fileSystem.reader(artifact.location())) {
                Path path = tmp.resolve(artifact.name());
                if (path.toFile().exists()) {
                    // file already exists, but we have checksums, so we should be ok
                    LOG.warn("Duplicate entry in artifacts: [{}]", artifact);
                    continue;
                }
                LOG.info("Copied {} to {}", artifact.location(), path);
                Files.copy(Channels.newInputStream(reader), path);
            }
        }
        return ClassLoaders.forDirectory(tmp.toFile());
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
Also used : Path(java.nio.file.Path) ReadableByteChannel(java.nio.channels.ReadableByteChannel) FileSystem(org.flyte.jflyte.api.FileSystem) UncheckedIOException(java.io.UncheckedIOException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException)

Example 2 with FileSystem

use of org.flyte.jflyte.api.FileSystem in project flytekit-java by flyteorg.

the class FileSystemLoader method getFileSystem.

static FileSystem getFileSystem(Map<String, FileSystem> fileSystems, URI uri) {
    String scheme = uri.getScheme();
    FileSystem fileSystem = fileSystems.get(scheme);
    return Verify.verifyNotNull(fileSystem, "Can't find FileSystem for [%s]", scheme);
}
Also used : FileSystem(org.flyte.jflyte.api.FileSystem)

Example 3 with FileSystem

use of org.flyte.jflyte.api.FileSystem in project flytekit-java by flyteorg.

the class FileSystemLoader method loadFileSystems.

static List<FileSystem> loadFileSystems() {
    ServiceLoader<FileSystemRegistrar> loader = ServiceLoader.load(FileSystemRegistrar.class);
    LOG.debug("Discovering FileSystemRegistrar");
    List<FileSystem> fileSystems = new ArrayList<>();
    Map<String, String> env = ImmutableMap.copyOf(System.getenv());
    for (FileSystemRegistrar registrar : loader) {
        for (FileSystem fileSystem : registrar.load(env)) {
            LOG.debug(String.format("Discovered FileSystem [%s]", fileSystem.getClass().getName()));
            fileSystems.add(fileSystem);
        }
    }
    return fileSystems;
}
Also used : FileSystemRegistrar(org.flyte.jflyte.api.FileSystemRegistrar) FileSystem(org.flyte.jflyte.api.FileSystem) ArrayList(java.util.ArrayList)

Example 4 with FileSystem

use of org.flyte.jflyte.api.FileSystem in project flytekit-java by flyteorg.

the class ArtifactStager method create.

static ArtifactStager create(Config config, Collection<ClassLoader> modules) {
    try {
        String stagingLocation = config.stagingLocation();
        if (stagingLocation == null) {
            throw new IllegalArgumentException("Environment variable 'FLYTE_STAGING_LOCATION' isn't set");
        }
        URI stagingUri = new URI(stagingLocation);
        Map<String, FileSystem> fileSystems = FileSystemLoader.loadFileSystems(modules);
        FileSystem stagingFileSystem = FileSystemLoader.getFileSystem(fileSystems, stagingUri);
        return new ArtifactStager(stagingLocation, stagingFileSystem);
    } catch (URISyntaxException e) {
        throw new IllegalArgumentException("Failed to parse stagingLocation", e);
    }
}
Also used : FileSystem(org.flyte.jflyte.api.FileSystem) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 5 with FileSystem

use of org.flyte.jflyte.api.FileSystem in project flytekit-java by flyteorg.

the class Execute method execute.

private void execute() {
    Config config = Config.load();
    Collection<ClassLoader> modules = ClassLoaders.forModuleDir(config.moduleDir()).values();
    Map<String, FileSystem> fileSystems = FileSystemLoader.loadFileSystems(modules);
    FileSystem outputFs = FileSystemLoader.getFileSystem(fileSystems, outputPrefix);
    ProtoWriter protoWriter = new ProtoWriter(outputPrefix, outputFs);
    try {
        FileSystem inputFs = FileSystemLoader.getFileSystem(fileSystems, inputs);
        ProtoReader protoReader = new ProtoReader(inputFs);
        TaskTemplate taskTemplate = protoReader.getTaskTemplate(taskTemplatePath);
        ClassLoader packageClassLoader = PackageLoader.load(fileSystems, taskTemplate);
        // before we run anything, switch class loader, otherwise,
        // ServiceLoaders and other things wouldn't work, for instance,
        // FileSystemRegister in Apache Beam
        Map<String, Literal> outputs = withClassLoader(packageClassLoader, () -> {
            Map<String, Literal> input = protoReader.getInput(inputs);
            RunnableTask runnableTask = getTask(task);
            return runnableTask.run(input);
        });
        protoWriter.writeOutputs(outputs);
    } catch (ContainerError e) {
        LOG.error("failed to run task", e);
        protoWriter.writeError(ProtoUtil.serializeContainerError(e));
    } catch (Throwable e) {
        LOG.error("failed to run task", e);
        protoWriter.writeError(ProtoUtil.serializeThrowable(e));
    }
}
Also used : TaskTemplate(org.flyte.api.v1.TaskTemplate) FileSystem(org.flyte.jflyte.api.FileSystem) Literal(org.flyte.api.v1.Literal) ClassLoaders.withClassLoader(org.flyte.jflyte.ClassLoaders.withClassLoader) RunnableTask(org.flyte.api.v1.RunnableTask) ContainerError(org.flyte.api.v1.ContainerError)

Aggregations

FileSystem (org.flyte.jflyte.api.FileSystem)6 ContainerError (org.flyte.api.v1.ContainerError)2 Literal (org.flyte.api.v1.Literal)2 RunnableTask (org.flyte.api.v1.RunnableTask)2 TaskTemplate (org.flyte.api.v1.TaskTemplate)2 ClassLoaders.withClassLoader (org.flyte.jflyte.ClassLoaders.withClassLoader)2 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 ReadableByteChannel (java.nio.channels.ReadableByteChannel)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 DynamicJobSpec (org.flyte.api.v1.DynamicJobSpec)1 DynamicWorkflowTask (org.flyte.api.v1.DynamicWorkflowTask)1 DynamicWorkflowTaskRegistrar (org.flyte.api.v1.DynamicWorkflowTaskRegistrar)1 PartialTaskIdentifier (org.flyte.api.v1.PartialTaskIdentifier)1 PartialWorkflowIdentifier (org.flyte.api.v1.PartialWorkflowIdentifier)1 RunnableTaskRegistrar (org.flyte.api.v1.RunnableTaskRegistrar)1 Struct (org.flyte.api.v1.Struct)1