use of org.flyte.api.v1.RunnableTask in project flytekit-java by flyteorg.
the class ProjectClosureTest method testCreateTaskTemplateForRunnableTaskWithResources.
@Test
public void testCreateTaskTemplateForRunnableTaskWithResources() {
// given
Map<Resources.ResourceName, String> resourceValues = new HashMap<>();
resourceValues.put(Resources.ResourceName.MEMORY, "0.5Gi");
Resources expectedResources = Resources.builder().limits(resourceValues).requests(resourceValues).build();
RunnableTask task = createRunnableTask(expectedResources);
String image = "my-image";
// when
TaskTemplate result = ProjectClosure.createTaskTemplateForRunnableTask(task, image);
// then
Container container = result.container();
assertNotNull(container);
assertThat(container.image(), equalTo(image));
assertThat(container.resources(), equalTo(expectedResources));
assertThat(result.interface_(), equalTo(TypedInterface.builder().inputs(SdkTypes.nulls().getVariableMap()).outputs(SdkTypes.nulls().getVariableMap()).build()));
assertThat(result.custom(), equalTo(Struct.of(emptyMap())));
assertThat(result.retries(), equalTo(RetryStrategy.builder().retries(0).build()));
assertThat(result.type(), equalTo("java-task"));
}
use of org.flyte.api.v1.RunnableTask in project flytekit-java by flyteorg.
the class LocalEngineTest method testStructWorkflow.
@Test
public void testStructWorkflow() {
String workflowName = new StructWorkflow().getName();
Map<String, WorkflowTemplate> workflows = loadWorkflows();
Map<String, RunnableTask> tasks = loadTasks();
WorkflowTemplate workflow = workflows.get(workflowName);
Literal inputStructLiteral = Literal.ofScalar(Scalar.ofGeneric(Struct.of(ImmutableMap.of("someKey1", Struct.Value.ofStringValue("some_value_1"), "someKey2", Struct.Value.ofBoolValue(true)))));
Map<String, Literal> inputs = ImmutableMap.of("someString", Literal.ofScalar(Scalar.ofPrimitive(Primitive.ofStringValue("some_string_value"))), "someStruct", inputStructLiteral);
Map<String, Literal> outputs = LocalEngine.compileAndExecute(workflow, tasks, emptyMap(), inputs);
Literal expectedOutput = Literal.ofScalar(Scalar.ofGeneric(Struct.of(ImmutableMap.of("someKey1", Struct.Value.ofStringValue("some_value_1-output"), "someKey2", Struct.Value.ofBoolValue(true)))));
assertEquals(expectedOutput, outputs.get("outputStructData"));
}
use of org.flyte.api.v1.RunnableTask 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));
}
}
use of org.flyte.api.v1.RunnableTask in project flytekit-java by flyteorg.
the class ExecuteDynamicWorkflow method execute.
private void execute() {
Config config = Config.load();
ExecutionConfig executionConfig = ExecutionConfig.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);
Map<String, String> env = getEnv();
Map<WorkflowIdentifier, WorkflowTemplate> workflowTemplates = ClassLoaders.withClassLoader(packageClassLoader, () -> Registrars.loadAll(WorkflowTemplateRegistrar.class, env));
Map<TaskIdentifier, RunnableTask> runnableTasks = ClassLoaders.withClassLoader(packageClassLoader, () -> Registrars.loadAll(RunnableTaskRegistrar.class, env));
Map<TaskIdentifier, DynamicWorkflowTask> dynamicWorkflowTasks = ClassLoaders.withClassLoader(packageClassLoader, () -> Registrars.loadAll(DynamicWorkflowTaskRegistrar.class, env));
// before we run anything, switch class loader, otherwise,
// ServiceLoaders and other things wouldn't work, for instance,
// FileSystemRegister in Apache Beam
// we don't take the whole "custom" field, but only jflyte part, for that we ser-de it
Struct custom = JFlyteCustom.deserializeFromStruct(taskTemplate.custom()).serializeToStruct();
// all tasks already have staged jars, we can reuse 'jflyte' custom from current task to get
// it
Map<TaskIdentifier, TaskTemplate> taskTemplates = mapValues(ProjectClosure.createTaskTemplates(executionConfig, runnableTasks, dynamicWorkflowTasks), template -> template.toBuilder().custom(ProjectClosure.merge(template.custom(), custom)).build());
DynamicJobSpec futures = withClassLoader(packageClassLoader, () -> {
Map<String, Literal> input = protoReader.getInput(inputs);
DynamicWorkflowTask task = getDynamicWorkflowTask(this.task);
return task.run(input);
});
DynamicJobSpec rewrittenFutures = rewrite(executionConfig, futures, taskTemplates, workflowTemplates);
if (rewrittenFutures.nodes().isEmpty()) {
Map<String, Literal> outputs = getLiteralMap(rewrittenFutures.outputs());
protoWriter.writeOutputs(outputs);
} else {
protoWriter.writeFutures(rewrittenFutures);
}
} catch (ContainerError e) {
LOG.error("failed to run dynamic workflow", e);
protoWriter.writeError(ProtoUtil.serializeContainerError(e));
} catch (Throwable e) {
LOG.error("failed to run dynamic workflow", e);
protoWriter.writeError(ProtoUtil.serializeThrowable(e));
}
}
use of org.flyte.api.v1.RunnableTask in project flytekit-java by flyteorg.
the class ExecuteLocal method call.
@Override
public Integer call() {
Map<String, ClassLoader> modules = ExecuteLocalLoader.loadModules(packageDir);
Map<String, String> env = ImmutableMap.of("FLYTE_INTERNAL_DOMAIN", "development", "FLYTE_INTERNAL_VERSION", "test", "FLYTE_INTERNAL_PROJECT", "flytetester");
Map<String, RunnableTask> runnableTasks = ExecuteLocalLoader.loadTasks(modules, env);
Map<String, DynamicWorkflowTask> dynamicWorkflowTasks = // TODO support dynamic tasks
emptyMap();
Map<String, WorkflowTemplate> workflows = ExecuteLocalLoader.loadWorkflows(modules, env);
WorkflowTemplate workflow = Preconditions.checkNotNull(workflows.get(workflowName), "workflow not found [%s]", workflowName);
String synopsis = getCustomSynopsis();
List<String> inputArgsList = inputArgs == null ? Collections.emptyList() : Arrays.asList(inputArgs);
Map<String, Literal> inputs = getArgsParser().parseInputs(synopsis, workflow.interface_().inputs(), inputArgsList);
try {
// TODO, use logging listener here
ExecutionListener listener = NoopExecutionListener.create();
Map<String, Literal> outputs = LocalEngine.compileAndExecute(workflow, runnableTasks, dynamicWorkflowTasks, inputs, listener);
LOG.info("Outputs: " + StringUtil.serializeLiteralMap(outputs));
return 0;
} catch (Throwable e) {
return handleException(e);
}
}
Aggregations