Search in sources :

Example 91 with TaskDef

use of com.netflix.conductor.common.metadata.tasks.TaskDef in project conductor by Netflix.

the class TestDeciderService method testTaskRetry.

@SuppressWarnings("unchecked")
@Test
public void testTaskRetry() {
    Workflow workflow = createDefaultWorkflow();
    workflow.getWorkflowDefinition().setSchemaVersion(2);
    Map<String, Object> inputParams = new HashMap<>();
    inputParams.put("workflowInputParam", "${workflow.input.requestId}");
    inputParams.put("taskOutputParam", "${task2.output.location}");
    inputParams.put("constParam", "Some String value");
    inputParams.put("nullValue", null);
    inputParams.put("task2Status", "${task2.status}");
    inputParams.put("null", null);
    inputParams.put("task_id", "${CPEWF_TASK_ID}");
    Map<String, Object> env = new HashMap<>();
    env.put("env_task_id", "${CPEWF_TASK_ID}");
    inputParams.put("env", env);
    Map<String, Object> taskInput = parametersUtils.getTaskInput(inputParams, workflow, null, "t1");
    Task task = new Task();
    task.getInputData().putAll(taskInput);
    task.setStatus(Status.FAILED);
    task.setTaskId("t1");
    TaskDef taskDef = new TaskDef();
    WorkflowTask workflowTask = new WorkflowTask();
    workflowTask.getInputParameters().put("task_id", "${CPEWF_TASK_ID}");
    workflowTask.getInputParameters().put("env", env);
    Optional<Task> task2 = deciderService.retry(taskDef, workflowTask, task, workflow);
    assertEquals("t1", task.getInputData().get("task_id"));
    assertEquals("t1", ((Map<String, Object>) task.getInputData().get("env")).get("env_task_id"));
    assertNotSame(task.getTaskId(), task2.get().getTaskId());
    assertEquals(task2.get().getTaskId(), task2.get().getInputData().get("task_id"));
    assertEquals(task2.get().getTaskId(), ((Map<String, Object>) task2.get().getInputData().get("env")).get("env_task_id"));
    Task task3 = new Task();
    task3.getInputData().putAll(taskInput);
    task3.setStatus(Status.FAILED_WITH_TERMINAL_ERROR);
    task3.setTaskId("t1");
    when(metadataDAO.getWorkflowDef(anyString(), anyInt())).thenReturn(Optional.of(new WorkflowDef()));
    exception.expect(TerminateWorkflowException.class);
    deciderService.retry(taskDef, workflowTask, task3, workflow);
}
Also used : Task(com.netflix.conductor.common.metadata.tasks.Task) WorkflowTask(com.netflix.conductor.common.metadata.workflow.WorkflowTask) WorkflowDef(com.netflix.conductor.common.metadata.workflow.WorkflowDef) HashMap(java.util.HashMap) TaskDef(com.netflix.conductor.common.metadata.tasks.TaskDef) Workflow(com.netflix.conductor.common.run.Workflow) WorkflowTask(com.netflix.conductor.common.metadata.workflow.WorkflowTask) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 92 with TaskDef

use of com.netflix.conductor.common.metadata.tasks.TaskDef in project conductor by Netflix.

the class TestDeciderService method testWorkflowTaskRetry.

@Test
public void testWorkflowTaskRetry() {
    Workflow workflow = createDefaultWorkflow();
    workflow.getWorkflowDefinition().setSchemaVersion(2);
    Map<String, Object> inputParams = new HashMap<>();
    inputParams.put("workflowInputParam", "${workflow.input.requestId}");
    inputParams.put("taskOutputParam", "${task2.output.location}");
    inputParams.put("constParam", "Some String value");
    inputParams.put("nullValue", null);
    inputParams.put("task2Status", "${task2.status}");
    inputParams.put("null", null);
    inputParams.put("task_id", "${CPEWF_TASK_ID}");
    Map<String, Object> env = new HashMap<>();
    env.put("env_task_id", "${CPEWF_TASK_ID}");
    inputParams.put("env", env);
    Map<String, Object> taskInput = parametersUtils.getTaskInput(inputParams, workflow, null, "t1");
    // Create a first failed task
    Task task = new Task();
    task.getInputData().putAll(taskInput);
    task.setStatus(Status.FAILED);
    task.setTaskId("t1");
    TaskDef taskDef = new TaskDef();
    assertEquals(3, taskDef.getRetryCount());
    WorkflowTask workflowTask = new WorkflowTask();
    workflowTask.getInputParameters().put("task_id", "${CPEWF_TASK_ID}");
    workflowTask.getInputParameters().put("env", env);
    workflowTask.setRetryCount(1);
    // Retry the failed task and assert that a new one has been created
    Optional<Task> task2 = deciderService.retry(taskDef, workflowTask, task, workflow);
    assertEquals("t1", task.getInputData().get("task_id"));
    assertEquals("t1", ((Map<String, Object>) task.getInputData().get("env")).get("env_task_id"));
    assertNotSame(task.getTaskId(), task2.get().getTaskId());
    assertEquals(task2.get().getTaskId(), task2.get().getInputData().get("task_id"));
    assertEquals(task2.get().getTaskId(), ((Map<String, Object>) task2.get().getInputData().get("env")).get("env_task_id"));
    // Set the retried task to FAILED, retry it again and assert that the workflow failed
    task2.get().setStatus(Status.FAILED);
    exception.expect(TerminateWorkflowException.class);
    final Optional<Task> task3 = deciderService.retry(taskDef, workflowTask, task2.get(), workflow);
    assertFalse(task3.isPresent());
    assertEquals(WorkflowStatus.FAILED, workflow.getStatus());
}
Also used : Task(com.netflix.conductor.common.metadata.tasks.Task) WorkflowTask(com.netflix.conductor.common.metadata.workflow.WorkflowTask) HashMap(java.util.HashMap) TaskDef(com.netflix.conductor.common.metadata.tasks.TaskDef) Workflow(com.netflix.conductor.common.run.Workflow) WorkflowTask(com.netflix.conductor.common.metadata.workflow.WorkflowTask) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 93 with TaskDef

use of com.netflix.conductor.common.metadata.tasks.TaskDef in project conductor by Netflix.

the class TestDeciderService method createLinearWorkflow.

private WorkflowDef createLinearWorkflow() {
    Map<String, Object> inputParams = new HashMap<>();
    inputParams.put("p1", "workflow.input.param1");
    inputParams.put("p2", "workflow.input.param2");
    WorkflowTask workflowTask1 = new WorkflowTask();
    workflowTask1.setName("junit_task_l1");
    workflowTask1.setInputParameters(inputParams);
    workflowTask1.setTaskReferenceName("s1");
    workflowTask1.setTaskDefinition(new TaskDef("junit_task_l1"));
    WorkflowTask workflowTask2 = new WorkflowTask();
    workflowTask2.setName("junit_task_l2");
    workflowTask2.setInputParameters(inputParams);
    workflowTask2.setTaskReferenceName("s2");
    workflowTask2.setTaskDefinition(new TaskDef("junit_task_l2"));
    WorkflowDef workflowDef = new WorkflowDef();
    workflowDef.setSchemaVersion(2);
    workflowDef.setInputParameters(Arrays.asList("param1", "param2"));
    workflowDef.setName("Linear Workflow");
    workflowDef.getTasks().addAll(Arrays.asList(workflowTask1, workflowTask2));
    return workflowDef;
}
Also used : WorkflowDef(com.netflix.conductor.common.metadata.workflow.WorkflowDef) HashMap(java.util.HashMap) TaskDef(com.netflix.conductor.common.metadata.tasks.TaskDef) WorkflowTask(com.netflix.conductor.common.metadata.workflow.WorkflowTask) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString)

Example 94 with TaskDef

use of com.netflix.conductor.common.metadata.tasks.TaskDef in project conductor by Netflix.

the class TestDeciderService method testExponentialBackoff.

@Test
public void testExponentialBackoff() {
    Workflow workflow = createDefaultWorkflow();
    Task task = new Task();
    task.setStatus(Status.FAILED);
    task.setTaskId("t1");
    TaskDef taskDef = new TaskDef();
    taskDef.setRetryDelaySeconds(60);
    taskDef.setRetryLogic(TaskDef.RetryLogic.EXPONENTIAL_BACKOFF);
    WorkflowTask workflowTask = new WorkflowTask();
    Optional<Task> task2 = deciderService.retry(taskDef, workflowTask, task, workflow);
    assertEquals(60, task2.get().getCallbackAfterSeconds());
    Optional<Task> task3 = deciderService.retry(taskDef, workflowTask, task2.get(), workflow);
    assertEquals(120, task3.get().getCallbackAfterSeconds());
    Optional<Task> task4 = deciderService.retry(taskDef, workflowTask, task3.get(), workflow);
    assertEquals(240, task4.get().getCallbackAfterSeconds());
    taskDef.setRetryCount(Integer.MAX_VALUE);
    task4.get().setRetryCount(Integer.MAX_VALUE - 100);
    Optional<Task> task5 = deciderService.retry(taskDef, workflowTask, task4.get(), workflow);
    assertEquals(Integer.MAX_VALUE, task5.get().getCallbackAfterSeconds());
}
Also used : Task(com.netflix.conductor.common.metadata.tasks.Task) WorkflowTask(com.netflix.conductor.common.metadata.workflow.WorkflowTask) TaskDef(com.netflix.conductor.common.metadata.tasks.TaskDef) Workflow(com.netflix.conductor.common.run.Workflow) WorkflowTask(com.netflix.conductor.common.metadata.workflow.WorkflowTask) Test(org.junit.Test)

Example 95 with TaskDef

use of com.netflix.conductor.common.metadata.tasks.TaskDef in project conductor by Netflix.

the class TestDeciderService method testConcurrentTaskInputCalc.

@SuppressWarnings("unchecked")
@Test
public void testConcurrentTaskInputCalc() throws InterruptedException {
    TaskDef def = new TaskDef();
    Map<String, Object> inputMap = new HashMap<>();
    inputMap.put("path", "${workflow.input.inputLocation}");
    inputMap.put("type", "${workflow.input.sourceType}");
    inputMap.put("channelMapping", "${workflow.input.channelMapping}");
    List<Map<String, Object>> input = new LinkedList<>();
    input.add(inputMap);
    Map<String, Object> body = new HashMap<>();
    body.put("input", input);
    def.getInputTemplate().putAll(body);
    ExecutorService es = Executors.newFixedThreadPool(10);
    final int[] result = new int[10];
    CountDownLatch latch = new CountDownLatch(10);
    for (int i = 0; i < 10; i++) {
        final int x = i;
        es.submit(() -> {
            try {
                Map<String, Object> workflowInput = new HashMap<>();
                workflowInput.put("outputLocation", "baggins://outputlocation/" + x);
                workflowInput.put("inputLocation", "baggins://inputlocation/" + x);
                workflowInput.put("sourceType", "MuxedSource");
                workflowInput.put("channelMapping", x);
                WorkflowDef workflowDef = new WorkflowDef();
                workflowDef.setName("testConcurrentTaskInputCalc");
                workflowDef.setVersion(1);
                Workflow workflow = new Workflow();
                workflow.setWorkflowDefinition(workflowDef);
                workflow.setInput(workflowInput);
                Map<String, Object> taskInput = parametersUtils.getTaskInputV2(new HashMap<>(), workflow, null, def);
                Object reqInputObj = taskInput.get("input");
                assertNotNull(reqInputObj);
                assertTrue(reqInputObj instanceof List);
                List<Map<String, Object>> reqInput = (List<Map<String, Object>>) reqInputObj;
                Object cmObj = reqInput.get(0).get("channelMapping");
                assertNotNull(cmObj);
                if (!(cmObj instanceof Number)) {
                    result[x] = -1;
                } else {
                    Number channelMapping = (Number) cmObj;
                    result[x] = channelMapping.intValue();
                }
                latch.countDown();
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }
    latch.await(1, TimeUnit.MINUTES);
    if (latch.getCount() > 0) {
        fail("Executions did not complete in a minute.  Something wrong with the build server?");
    }
    es.shutdownNow();
    for (int i = 0; i < result.length; i++) {
        assertEquals(i, result[i]);
    }
}
Also used : WorkflowDef(com.netflix.conductor.common.metadata.workflow.WorkflowDef) HashMap(java.util.HashMap) Workflow(com.netflix.conductor.common.run.Workflow) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CountDownLatch(java.util.concurrent.CountDownLatch) LinkedList(java.util.LinkedList) ExpectedException(org.junit.rules.ExpectedException) IOException(java.io.IOException) TaskDef(com.netflix.conductor.common.metadata.tasks.TaskDef) ExecutorService(java.util.concurrent.ExecutorService) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Aggregations

TaskDef (com.netflix.conductor.common.metadata.tasks.TaskDef)172 Test (org.junit.Test)128 WorkflowTask (com.netflix.conductor.common.metadata.workflow.WorkflowTask)121 Task (com.netflix.conductor.common.metadata.tasks.Task)77 Workflow (com.netflix.conductor.common.run.Workflow)76 WorkflowDef (com.netflix.conductor.common.metadata.workflow.WorkflowDef)73 HashMap (java.util.HashMap)56 ArrayList (java.util.ArrayList)32 ConstraintViolation (javax.validation.ConstraintViolation)31 SubWorkflow (com.netflix.conductor.core.execution.tasks.SubWorkflow)30 UserTask (com.netflix.conductor.tests.utils.UserTask)28 LinkedList (java.util.LinkedList)28 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)27 List (java.util.List)22 Map (java.util.Map)19 ApplicationException (com.netflix.conductor.core.execution.ApplicationException)18 Before (org.junit.Before)14 ExpectedException (org.junit.rules.ExpectedException)13 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)12 Collectors (java.util.stream.Collectors)11