Search in sources :

Example 11 with RunnableTask

use of org.flyte.api.v1.RunnableTask in project flytekit-java by flyteorg.

the class LocalEngineTest method testFibonacci.

@Test
void testFibonacci() {
    String workflowName = new FibonacciWorkflow().getName();
    Literal fib0 = Literal.ofScalar(Scalar.ofPrimitive(Primitive.ofIntegerValue(0L)));
    Literal fib1 = Literal.ofScalar(Scalar.ofPrimitive(Primitive.ofIntegerValue(1L)));
    Literal fib2 = Literal.ofScalar(Scalar.ofPrimitive(Primitive.ofIntegerValue(1L)));
    Literal fib3 = Literal.ofScalar(Scalar.ofPrimitive(Primitive.ofIntegerValue(2L)));
    Literal fib4 = Literal.ofScalar(Scalar.ofPrimitive(Primitive.ofIntegerValue(3L)));
    Literal fib5 = Literal.ofScalar(Scalar.ofPrimitive(Primitive.ofIntegerValue(5L)));
    Map<String, WorkflowTemplate> workflows = loadWorkflows();
    Map<String, RunnableTask> tasks = loadTasks();
    TestingListener listener = new TestingListener();
    Map<String, Literal> outputs = LocalEngine.compileAndExecute(workflows.get(workflowName), tasks, emptyMap(), ImmutableMap.of("fib0", fib0, "fib1", fib1), listener);
    assertEquals(ImmutableMap.of("fib4", fib4, "fib5", fib5), outputs);
    assertEquals(ImmutableList.<List<Object>>builder().add(ofPending("fib-2")).add(ofPending("fib-3")).add(ofPending("fib-4")).add(ofPending("fib-5")).add(ofStarting("fib-2", ImmutableMap.of("a", fib0, "b", fib1))).add(ofCompleted("fib-2", ImmutableMap.of("a", fib0, "b", fib1), ImmutableMap.of("c", fib2))).add(ofStarting("fib-3", ImmutableMap.of("a", fib1, "b", fib2))).add(ofCompleted("fib-3", ImmutableMap.of("a", fib1, "b", fib2), ImmutableMap.of("c", fib3))).add(ofStarting("fib-4", ImmutableMap.of("a", fib2, "b", fib3))).add(ofCompleted("fib-4", ImmutableMap.of("a", fib2, "b", fib3), ImmutableMap.of("c", fib4))).add(ofStarting("fib-5", ImmutableMap.of("a", fib3, "b", fib4))).add(ofCompleted("fib-5", ImmutableMap.of("a", fib3, "b", fib4), ImmutableMap.of("c", fib5))).build(), listener.actions);
}
Also used : WorkflowTemplate(org.flyte.api.v1.WorkflowTemplate) Literal(org.flyte.api.v1.Literal) List(java.util.List) RunnableTask(org.flyte.api.v1.RunnableTask) FibonacciWorkflow(org.flyte.localengine.examples.FibonacciWorkflow) Test(org.junit.jupiter.api.Test)

Example 12 with RunnableTask

use of org.flyte.api.v1.RunnableTask in project flytekit-java by flyteorg.

the class LocalEngineTest method testRetryableTask_completed.

@Test
public void testRetryableTask_completed() {
    String workflowName = new RetryableWorkflow().getName();
    Map<String, WorkflowTemplate> workflows = loadWorkflows();
    Map<String, RunnableTask> tasks = loadTasks();
    WorkflowTemplate workflow = workflows.get(workflowName);
    TestingListener listener = new TestingListener();
    // make sure we don't run two tests in parallel
    synchronized (RetryableTask.class) {
        RetryableTask.ATTEMPTS_BEFORE_SUCCESS.set(5L);
        RetryableTask.ATTEMPTS.set(0L);
        LocalEngine.compileAndExecute(workflow, tasks, emptyMap(), ImmutableMap.of(), listener);
        assertEquals(ImmutableList.<List<Object>>builder().add(ofPending("node-1")).add(ofStarting("node-1", ImmutableMap.of())).add(ofRetrying("node-1", ImmutableMap.of(), "oops", /* attempt= */
        1)).add(ofRetrying("node-1", ImmutableMap.of(), "oops", /* attempt= */
        2)).add(ofRetrying("node-1", ImmutableMap.of(), "oops", /* attempt= */
        3)).add(ofRetrying("node-1", ImmutableMap.of(), "oops", /* attempt= */
        4)).add(ofCompleted("node-1", ImmutableMap.of(), ImmutableMap.of())).build(), listener.actions);
        // will finish on attempt number 5 according to ATTEMPTS_BEFORE_SUCCESS
        assertEquals(5L, RetryableTask.ATTEMPTS.get());
    }
}
Also used : RetryableWorkflow(org.flyte.localengine.examples.RetryableWorkflow) WorkflowTemplate(org.flyte.api.v1.WorkflowTemplate) RetryableTask(org.flyte.localengine.examples.RetryableTask) RunnableTask(org.flyte.api.v1.RunnableTask) Test(org.junit.jupiter.api.Test)

Example 13 with RunnableTask

use of org.flyte.api.v1.RunnableTask in project flytekit-java by flyteorg.

the class LocalEngineTest method testBindingCollection.

@Test
public void testBindingCollection() {
    String workflowName = new ListWorkflow().getName();
    Map<String, WorkflowTemplate> workflows = loadWorkflows();
    Map<String, RunnableTask> tasks = loadTasks();
    WorkflowTemplate workflow = workflows.get(workflowName);
    Map<String, Literal> outputs = LocalEngine.compileAndExecute(workflow, tasks, emptyMap(), ImmutableMap.of());
    // 3 = 1 + 2, 7 = 3 + 4
    Literal i3 = Literal.ofScalar(Scalar.ofPrimitive(Primitive.ofIntegerValue(3)));
    Literal i7 = Literal.ofScalar(Scalar.ofPrimitive(Primitive.ofIntegerValue(7)));
    assertEquals(ImmutableMap.of("list", Literal.ofCollection(ImmutableList.of(i3, i7))), outputs);
}
Also used : WorkflowTemplate(org.flyte.api.v1.WorkflowTemplate) Literal(org.flyte.api.v1.Literal) RunnableTask(org.flyte.api.v1.RunnableTask) ListWorkflow(org.flyte.localengine.examples.ListWorkflow) Test(org.junit.jupiter.api.Test)

Example 14 with RunnableTask

use of org.flyte.api.v1.RunnableTask in project flytekit-java by flyteorg.

the class SdkRunnableTaskRegistrarTest method shouldLoadRunnableTasksFromDiscoveredRegistries.

@Test
void shouldLoadRunnableTasksFromDiscoveredRegistries() {
    // given
    String testTaskName = "org.flyte.flytekit.SdkRunnableTaskRegistrarTest$TestTask";
    String otherTestTaskName = "org.flyte.flytekit.SdkRunnableTaskRegistrarTest$OtherTestTask";
    TaskIdentifier expectedTestTaskId = TaskIdentifier.builder().project("project").domain("domain").name(testTaskName).version("version").build();
    TypedInterface typedInterface = TypedInterface.builder().inputs(SdkTypes.nulls().getVariableMap()).outputs(SdkTypes.nulls().getVariableMap()).build();
    RetryStrategy retries = RetryStrategy.builder().retries(0).build();
    RetryStrategy otherRetries = RetryStrategy.builder().retries(1).build();
    Map<Resources.ResourceName, String> limits = new HashMap<>();
    limits.put(Resources.ResourceName.CPU, "0.5");
    limits.put(Resources.ResourceName.MEMORY, "2Gi");
    Map<Resources.ResourceName, String> requests = new HashMap<>();
    requests.put(Resources.ResourceName.CPU, "2");
    requests.put(Resources.ResourceName.MEMORY, "5Gi");
    Resources resources = Resources.builder().limits(limits).requests(requests).build();
    RunnableTask expectedTask = createRunnableTask(testTaskName, typedInterface, retries, null);
    TaskIdentifier expectedOtherTestTaskId = TaskIdentifier.builder().project("project").domain("domain").name(otherTestTaskName).version("version").build();
    RunnableTask expectedOtherTask = createRunnableTask(otherTestTaskName, typedInterface, otherRetries, resources);
    // when
    Map<TaskIdentifier, RunnableTask> tasks = registrar.load(ENV);
    // then
    assertAll(() -> assertThat(tasks, hasKey(is(expectedTestTaskId))), () -> assertThat(tasks, hasKey(is(expectedOtherTestTaskId))));
    assertTaskEquals(tasks.get(expectedTestTaskId), expectedTask);
    assertTaskEquals(tasks.get(expectedOtherTestTaskId), expectedOtherTask);
}
Also used : TypedInterface(org.flyte.api.v1.TypedInterface) TaskIdentifier(org.flyte.api.v1.TaskIdentifier) HashMap(java.util.HashMap) Resources(org.flyte.api.v1.Resources) RunnableTask(org.flyte.api.v1.RunnableTask) RetryStrategy(org.flyte.api.v1.RetryStrategy) Test(org.junit.jupiter.api.Test)

Example 15 with RunnableTask

use of org.flyte.api.v1.RunnableTask in project flytekit-java by flyteorg.

the class ExecutionNodeCompiler method compile.

static ExecutionNode compile(Node node, Map<String, RunnableTask> runnableTasks, Map<String, DynamicWorkflowTask> dynamicWorkflowTasks) {
    List<String> upstreamNodeIds = new ArrayList<>();
    node.inputs().stream().map(Binding::binding).flatMap(ExecutionNodeCompiler::unpackBindingData).filter(x -> x.kind() == BindingData.Kind.PROMISE).map(x -> x.promise().nodeId()).forEach(upstreamNodeIds::add);
    upstreamNodeIds.addAll(node.upstreamNodeIds());
    if (upstreamNodeIds.isEmpty()) {
        upstreamNodeIds.add(START_NODE_ID);
    }
    if (node.branchNode() != null) {
        throw new IllegalArgumentException("BranchNode isn't yet supported for local execution");
    }
    if (node.workflowNode() != null) {
        throw new IllegalArgumentException("WorkflowNode isn't yet supported for local execution");
    }
    String taskName = node.taskNode().referenceId().name();
    DynamicWorkflowTask dynamicWorkflowTask = dynamicWorkflowTasks.get(taskName);
    RunnableTask runnableTask = runnableTasks.get(taskName);
    if (dynamicWorkflowTask != null) {
        throw new IllegalArgumentException("DynamicWorkflowTask isn't yet supported for local execution");
    }
    Objects.requireNonNull(runnableTask, () -> String.format("Couldn't find task [%s]", taskName));
    int attempts = runnableTask.getRetries().retries() + 1;
    return ExecutionNode.builder().nodeId(node.id()).bindings(node.inputs()).runnableTask(runnableTask).upstreamNodeIds(upstreamNodeIds).attempts(attempts).build();
}
Also used : Binding(org.flyte.api.v1.Binding) Node(org.flyte.api.v1.Node) Collections.emptyList(java.util.Collections.emptyList) START_NODE_ID(org.flyte.api.v1.Node.START_NODE_ID) DynamicWorkflowTask(org.flyte.api.v1.DynamicWorkflowTask) Set(java.util.Set) HashMap(java.util.HashMap) Binding(org.flyte.api.v1.Binding) Deque(java.util.Deque) RunnableTask(org.flyte.api.v1.RunnableTask) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Objects(java.util.Objects) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) BindingData(org.flyte.api.v1.BindingData) Stream(java.util.stream.Stream) Map(java.util.Map) ArrayDeque(java.util.ArrayDeque) Comparator(java.util.Comparator) DynamicWorkflowTask(org.flyte.api.v1.DynamicWorkflowTask) ArrayList(java.util.ArrayList) RunnableTask(org.flyte.api.v1.RunnableTask)

Aggregations

RunnableTask (org.flyte.api.v1.RunnableTask)14 Test (org.junit.jupiter.api.Test)10 Literal (org.flyte.api.v1.Literal)8 WorkflowTemplate (org.flyte.api.v1.WorkflowTemplate)8 HashMap (java.util.HashMap)5 TaskTemplate (org.flyte.api.v1.TaskTemplate)5 List (java.util.List)4 TaskIdentifier (org.flyte.api.v1.TaskIdentifier)4 DynamicWorkflowTask (org.flyte.api.v1.DynamicWorkflowTask)3 Resources (org.flyte.api.v1.Resources)3 ByteString (com.google.protobuf.ByteString)2 Container (org.flyte.api.v1.Container)2 ContainerError (org.flyte.api.v1.ContainerError)2 PartialTaskIdentifier (org.flyte.api.v1.PartialTaskIdentifier)2 ClassLoaders.withClassLoader (org.flyte.jflyte.ClassLoaders.withClassLoader)2 FileSystem (org.flyte.jflyte.api.FileSystem)2 RetryableTask (org.flyte.localengine.examples.RetryableTask)2 RetryableWorkflow (org.flyte.localengine.examples.RetryableWorkflow)2 ArrayDeque (java.util.ArrayDeque)1 ArrayList (java.util.ArrayList)1