Search in sources :

Example 1 with WorkflowSystemTask

use of com.netflix.conductor.core.execution.tasks.WorkflowSystemTask in project conductor by Netflix.

the class TestWorkflowRepairService method verifyAndRepairSystemTask.

@Test
public void verifyAndRepairSystemTask() {
    Task task = new Task();
    task.setTaskType("TEST_SYS_TASK");
    task.setStatus(Task.Status.SCHEDULED);
    task.setTaskId("abcd");
    task.setCallbackAfterSeconds(60);
    // Create a Custom system task to init WorkflowSystemTask registry.
    WorkflowSystemTask workflowSystemTask = new WorkflowSystemTask("TEST_SYS_TASK") {

        @Override
        public boolean isAsync() {
            return true;
        }

        @Override
        public boolean isAsyncComplete(Task task) {
            return false;
        }

        @Override
        public void start(Workflow workflow, Task task, WorkflowExecutor executor) {
            super.start(workflow, task, executor);
        }
    };
    when(queueDAO.containsMessage(anyString(), anyString())).thenReturn(false);
    assertTrue(workflowRepairService.verifyAndRepairTask(task));
    // Verify that a new queue message is pushed for tasks that fails queue contains check.
    verify(queueDAO, times(1)).push(anyString(), anyString(), anyLong());
    // Verify a system task in IN_PROGRESS state can be recovered.
    Mockito.reset(queueDAO);
    task.setStatus(Task.Status.IN_PROGRESS);
    assertTrue(workflowRepairService.verifyAndRepairTask(task));
    // Verify that a new queue message is pushed for async System task in IN_PROGRESS state that fails queue contains check.
    verify(queueDAO, times(1)).push(anyString(), anyString(), anyLong());
}
Also used : WorkflowSystemTask(com.netflix.conductor.core.execution.tasks.WorkflowSystemTask) Task(com.netflix.conductor.common.metadata.tasks.Task) WorkflowSystemTask(com.netflix.conductor.core.execution.tasks.WorkflowSystemTask) SubWorkflow(com.netflix.conductor.core.execution.tasks.SubWorkflow) Workflow(com.netflix.conductor.common.run.Workflow) Test(org.junit.Test)

Example 2 with WorkflowSystemTask

use of com.netflix.conductor.core.execution.tasks.WorkflowSystemTask in project conductor by Netflix.

the class TestWorkflowRepairService method assertAsyncCompleteSystemTasksAreNotCheckedAgainstQueue.

@Test
public void assertAsyncCompleteSystemTasksAreNotCheckedAgainstQueue() {
    Task task = new Task();
    task.setTaskType("SUB_WORKFLOW");
    task.setStatus(Task.Status.IN_PROGRESS);
    task.setTaskId("abcd");
    task.setCallbackAfterSeconds(60);
    WorkflowSystemTask workflowSystemTask = new SubWorkflow();
    assertTrue(workflowSystemTask.isAsyncComplete(task));
    assertFalse(workflowRepairService.verifyAndRepairTask(task));
    // Verify that queue message is never pushed for async complete system tasks
    verify(queueDAO, never()).containsMessage(anyString(), anyString());
    verify(queueDAO, never()).push(anyString(), anyString(), anyLong());
}
Also used : WorkflowSystemTask(com.netflix.conductor.core.execution.tasks.WorkflowSystemTask) SubWorkflow(com.netflix.conductor.core.execution.tasks.SubWorkflow) Task(com.netflix.conductor.common.metadata.tasks.Task) WorkflowSystemTask(com.netflix.conductor.core.execution.tasks.WorkflowSystemTask) Test(org.junit.Test)

Example 3 with WorkflowSystemTask

use of com.netflix.conductor.core.execution.tasks.WorkflowSystemTask in project conductor by Netflix.

the class WorkflowExecutor method decide.

/**
 * @param workflowId ID of the workflow to evaluate the state for
 * @return true if the workflow has completed (success or failed), false otherwise.
 * @throws ApplicationException If there was an error - caller should retry in this case.
 */
public boolean decide(String workflowId) {
    if (!executionLockService.acquireLock(workflowId)) {
        return false;
    }
    // If it is a new workflow, the tasks will be still empty even though include tasks is true
    Workflow workflow = executionDAOFacade.getWorkflowById(workflowId, true);
    // FIXME Backwards compatibility for legacy workflows already running.
    // This code will be removed in a future version.
    workflow = metadataMapperService.populateWorkflowWithDefinitions(workflow);
    if (workflow.getStatus().isTerminal()) {
        if (!workflow.getStatus().isSuccessful()) {
            cancelNonTerminalTasks(workflow);
        }
        return true;
    }
    try {
        DeciderService.DeciderOutcome outcome = deciderService.decide(workflow);
        if (outcome.isComplete) {
            endExecution(workflow);
            return true;
        }
        List<Task> tasksToBeScheduled = outcome.tasksToBeScheduled;
        setTaskDomains(tasksToBeScheduled, workflow);
        List<Task> tasksToBeUpdated = outcome.tasksToBeUpdated;
        boolean stateChanged = false;
        tasksToBeScheduled = dedupAndAddTasks(workflow, tasksToBeScheduled);
        for (Task task : outcome.tasksToBeScheduled) {
            if (isSystemTask.and(isNonTerminalTask).test(task)) {
                WorkflowSystemTask workflowSystemTask = WorkflowSystemTask.get(task.getTaskType());
                Workflow workflowInstance = deciderService.populateWorkflowAndTaskData(workflow);
                if (!workflowSystemTask.isAsync() && workflowSystemTask.execute(workflowInstance, task, this)) {
                    deciderService.externalizeTaskData(task);
                    tasksToBeUpdated.add(task);
                    stateChanged = true;
                }
            }
        }
        if (!outcome.tasksToBeUpdated.isEmpty() || !tasksToBeScheduled.isEmpty()) {
            executionDAOFacade.updateTasks(tasksToBeUpdated);
            executionDAOFacade.updateWorkflow(workflow);
        }
        stateChanged = scheduleTask(workflow, tasksToBeScheduled) || stateChanged;
        if (stateChanged) {
            decide(workflowId);
        }
    } catch (TerminateWorkflowException twe) {
        LOGGER.info("Execution terminated of workflow: {}", workflowId, twe);
        terminate(workflow, twe);
        return true;
    } catch (RuntimeException e) {
        LOGGER.error("Error deciding workflow: {}", workflowId, e);
        throw e;
    } finally {
        executionLockService.releaseLock(workflowId);
    }
    return false;
}
Also used : WorkflowSystemTask(com.netflix.conductor.core.execution.tasks.WorkflowSystemTask) Task(com.netflix.conductor.common.metadata.tasks.Task) WorkflowSystemTask(com.netflix.conductor.core.execution.tasks.WorkflowSystemTask) WorkflowTask(com.netflix.conductor.common.metadata.workflow.WorkflowTask) SubWorkflow(com.netflix.conductor.core.execution.tasks.SubWorkflow) Workflow(com.netflix.conductor.common.run.Workflow)

Example 4 with WorkflowSystemTask

use of com.netflix.conductor.core.execution.tasks.WorkflowSystemTask in project conductor by Netflix.

the class WorkflowExecutor method executeSubworkflowTaskAndSyncData.

private void executeSubworkflowTaskAndSyncData(Workflow subWorkflow, Task subWorkflowTask) {
    WorkflowSystemTask subWorkflowSystemTask = WorkflowSystemTask.get(SubWorkflow.NAME);
    subWorkflowSystemTask.execute(subWorkflow, subWorkflowTask, this);
    // Keep Subworkflow task's data consistent with Subworkflow's.
    if (subWorkflowTask.getStatus().isTerminal() && subWorkflowTask.getExternalOutputPayloadStoragePath() != null && !subWorkflowTask.getOutputData().isEmpty()) {
        Map<String, Object> parentWorkflowTaskOutputData = subWorkflowTask.getOutputData();
        deciderService.populateTaskData(subWorkflowTask);
        subWorkflowTask.getOutputData().putAll(parentWorkflowTaskOutputData);
        deciderService.externalizeTaskData(subWorkflowTask);
    }
}
Also used : WorkflowSystemTask(com.netflix.conductor.core.execution.tasks.WorkflowSystemTask)

Example 5 with WorkflowSystemTask

use of com.netflix.conductor.core.execution.tasks.WorkflowSystemTask in project conductor by Netflix.

the class TestWorkflowExecutor method testScheduleTask.

@Test
public void testScheduleTask() {
    AtomicBoolean httpTaskExecuted = new AtomicBoolean(false);
    AtomicBoolean http2TaskExecuted = new AtomicBoolean(false);
    new Wait();
    new WorkflowSystemTask("HTTP") {

        @Override
        public boolean isAsync() {
            return true;
        }

        @Override
        public void start(Workflow workflow, Task task, WorkflowExecutor executor) {
            httpTaskExecuted.set(true);
            task.setStatus(Status.COMPLETED);
            super.start(workflow, task, executor);
        }
    };
    new WorkflowSystemTask("HTTP2") {

        @Override
        public void start(Workflow workflow, Task task, WorkflowExecutor executor) {
            http2TaskExecuted.set(true);
            task.setStatus(Status.COMPLETED);
            super.start(workflow, task, executor);
        }
    };
    Workflow workflow = new Workflow();
    workflow.setWorkflowId("1");
    List<Task> tasks = new LinkedList<>();
    WorkflowTask taskToSchedule = new WorkflowTask();
    taskToSchedule.setWorkflowTaskType(TaskType.USER_DEFINED);
    taskToSchedule.setType("HTTP");
    WorkflowTask taskToSchedule2 = new WorkflowTask();
    taskToSchedule2.setWorkflowTaskType(TaskType.USER_DEFINED);
    taskToSchedule2.setType("HTTP2");
    WorkflowTask wait = new WorkflowTask();
    wait.setWorkflowTaskType(TaskType.WAIT);
    wait.setType("WAIT");
    wait.setTaskReferenceName("wait");
    Task task1 = new Task();
    task1.setTaskType(taskToSchedule.getType());
    task1.setTaskDefName(taskToSchedule.getName());
    task1.setReferenceTaskName(taskToSchedule.getTaskReferenceName());
    task1.setWorkflowInstanceId(workflow.getWorkflowId());
    task1.setCorrelationId(workflow.getCorrelationId());
    task1.setScheduledTime(System.currentTimeMillis());
    task1.setTaskId(IDGenerator.generate());
    task1.setInputData(new HashMap<>());
    task1.setStatus(Status.SCHEDULED);
    task1.setRetryCount(0);
    task1.setCallbackAfterSeconds(taskToSchedule.getStartDelay());
    task1.setWorkflowTask(taskToSchedule);
    Task task2 = new Task();
    task2.setTaskType(Wait.NAME);
    task2.setTaskDefName(taskToSchedule.getName());
    task2.setReferenceTaskName(taskToSchedule.getTaskReferenceName());
    task2.setWorkflowInstanceId(workflow.getWorkflowId());
    task2.setCorrelationId(workflow.getCorrelationId());
    task2.setScheduledTime(System.currentTimeMillis());
    task2.setInputData(new HashMap<>());
    task2.setTaskId(IDGenerator.generate());
    task2.setStatus(Status.IN_PROGRESS);
    task2.setWorkflowTask(taskToSchedule);
    Task task3 = new Task();
    task3.setTaskType(taskToSchedule2.getType());
    task3.setTaskDefName(taskToSchedule.getName());
    task3.setReferenceTaskName(taskToSchedule.getTaskReferenceName());
    task3.setWorkflowInstanceId(workflow.getWorkflowId());
    task3.setCorrelationId(workflow.getCorrelationId());
    task3.setScheduledTime(System.currentTimeMillis());
    task3.setTaskId(IDGenerator.generate());
    task3.setInputData(new HashMap<>());
    task3.setStatus(Status.SCHEDULED);
    task3.setRetryCount(0);
    task3.setCallbackAfterSeconds(taskToSchedule.getStartDelay());
    task3.setWorkflowTask(taskToSchedule);
    tasks.add(task1);
    tasks.add(task2);
    tasks.add(task3);
    when(executionDAOFacade.createTasks(tasks)).thenReturn(tasks);
    AtomicInteger startedTaskCount = new AtomicInteger(0);
    doAnswer(invocation -> {
        startedTaskCount.incrementAndGet();
        return null;
    }).when(executionDAOFacade).updateTask(any());
    AtomicInteger queuedTaskCount = new AtomicInteger(0);
    final Answer answer = invocation -> {
        String queueName = invocation.getArgument(0, String.class);
        System.out.println(queueName);
        queuedTaskCount.incrementAndGet();
        return null;
    };
    doAnswer(answer).when(queueDAO).push(any(), any(), anyLong());
    doAnswer(answer).when(queueDAO).push(any(), any(), anyInt(), anyLong());
    boolean stateChanged = workflowExecutor.scheduleTask(workflow, tasks);
    assertEquals(2, startedTaskCount.get());
    assertEquals(1, queuedTaskCount.get());
    assertTrue(stateChanged);
    assertFalse(httpTaskExecuted.get());
    assertTrue(http2TaskExecuted.get());
}
Also used : SubWorkflow(com.netflix.conductor.core.execution.tasks.SubWorkflow) ExecutionLockService(com.netflix.conductor.service.ExecutionLockService) Arrays(java.util.Arrays) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Wait(com.netflix.conductor.core.execution.tasks.Wait) MetadataDAO(com.netflix.conductor.dao.MetadataDAO) Task(com.netflix.conductor.common.metadata.tasks.Task) SimpleTaskMapper(com.netflix.conductor.core.execution.mapper.SimpleTaskMapper) ExecutionDAOFacade(com.netflix.conductor.core.orchestration.ExecutionDAOFacade) WorkflowSystemTask(com.netflix.conductor.core.execution.tasks.WorkflowSystemTask) Mockito.doThrow(org.mockito.Mockito.doThrow) RerunWorkflowRequest(com.netflix.conductor.common.metadata.workflow.RerunWorkflowRequest) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Workflow(com.netflix.conductor.common.run.Workflow) Map(java.util.Map) Mockito.doAnswer(org.mockito.Mockito.doAnswer) PollData(com.netflix.conductor.common.metadata.tasks.PollData) Terminate(com.netflix.conductor.core.execution.tasks.Terminate) LambdaTaskMapper(com.netflix.conductor.core.execution.mapper.LambdaTaskMapper) ForkJoinTaskMapper(com.netflix.conductor.core.execution.mapper.ForkJoinTaskMapper) TaskDef(com.netflix.conductor.common.metadata.tasks.TaskDef) HTTPTaskMapper(com.netflix.conductor.core.execution.mapper.HTTPTaskMapper) WorkflowStatus(com.netflix.conductor.common.run.Workflow.WorkflowStatus) MetadataMapperService(com.netflix.conductor.core.metadata.MetadataMapperService) UUID(java.util.UUID) Mockito.doNothing(org.mockito.Mockito.doNothing) Collectors(java.util.stream.Collectors) WorkflowTask(com.netflix.conductor.common.metadata.workflow.WorkflowTask) QueueDAO(com.netflix.conductor.dao.QueueDAO) List(java.util.List) Assert.assertFalse(org.junit.Assert.assertFalse) Optional(java.util.Optional) JsonMapperProvider(com.netflix.conductor.common.utils.JsonMapperProvider) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) TaskMapper(com.netflix.conductor.core.execution.mapper.TaskMapper) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) ForkJoinDynamicTaskMapper(com.netflix.conductor.core.execution.mapper.ForkJoinDynamicTaskMapper) IDGenerator(com.netflix.conductor.core.utils.IDGenerator) Status(com.netflix.conductor.common.metadata.tasks.Task.Status) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) ArgumentMatchers.anyBoolean(org.mockito.ArgumentMatchers.anyBoolean) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Answer(org.mockito.stubbing.Answer) Lists(com.google.common.collect.Lists) ArgumentCaptor(org.mockito.ArgumentCaptor) ExternalPayloadStorageUtils(com.netflix.conductor.core.utils.ExternalPayloadStorageUtils) UserDefinedTaskMapper(com.netflix.conductor.core.execution.mapper.UserDefinedTaskMapper) WaitTaskMapper(com.netflix.conductor.core.execution.mapper.WaitTaskMapper) JoinTaskMapper(com.netflix.conductor.core.execution.mapper.JoinTaskMapper) Lambda(com.netflix.conductor.core.execution.tasks.Lambda) LinkedList(java.util.LinkedList) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) Before(org.junit.Before) SubWorkflowTaskMapper(com.netflix.conductor.core.execution.mapper.SubWorkflowTaskMapper) Comparator.comparingInt(java.util.Comparator.comparingInt) Uninterruptibles(com.google.common.util.concurrent.Uninterruptibles) DecisionTaskMapper(com.netflix.conductor.core.execution.mapper.DecisionTaskMapper) DynamicTaskMapper(com.netflix.conductor.core.execution.mapper.DynamicTaskMapper) Assert.assertNotNull(org.junit.Assert.assertNotNull) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) WorkflowDef(com.netflix.conductor.common.metadata.workflow.WorkflowDef) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Collectors.maxBy(java.util.stream.Collectors.maxBy) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) Mockito.never(org.mockito.Mockito.never) Assert.assertNull(org.junit.Assert.assertNull) TaskType(com.netflix.conductor.common.metadata.workflow.TaskType) EventTaskMapper(com.netflix.conductor.core.execution.mapper.EventTaskMapper) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Task(com.netflix.conductor.common.metadata.tasks.Task) WorkflowSystemTask(com.netflix.conductor.core.execution.tasks.WorkflowSystemTask) WorkflowTask(com.netflix.conductor.common.metadata.workflow.WorkflowTask) SubWorkflow(com.netflix.conductor.core.execution.tasks.SubWorkflow) Workflow(com.netflix.conductor.common.run.Workflow) WorkflowTask(com.netflix.conductor.common.metadata.workflow.WorkflowTask) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) LinkedList(java.util.LinkedList) WorkflowSystemTask(com.netflix.conductor.core.execution.tasks.WorkflowSystemTask) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Answer(org.mockito.stubbing.Answer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Wait(com.netflix.conductor.core.execution.tasks.Wait) Test(org.junit.Test)

Aggregations

WorkflowSystemTask (com.netflix.conductor.core.execution.tasks.WorkflowSystemTask)9 Task (com.netflix.conductor.common.metadata.tasks.Task)8 WorkflowTask (com.netflix.conductor.common.metadata.workflow.WorkflowTask)5 SubWorkflow (com.netflix.conductor.core.execution.tasks.SubWorkflow)5 Workflow (com.netflix.conductor.common.run.Workflow)4 Test (org.junit.Test)4 ArrayList (java.util.ArrayList)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 List (java.util.List)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Lists (com.google.common.collect.Lists)1 Uninterruptibles (com.google.common.util.concurrent.Uninterruptibles)1 PollData (com.netflix.conductor.common.metadata.tasks.PollData)1 Status (com.netflix.conductor.common.metadata.tasks.Task.Status)1 TaskDef (com.netflix.conductor.common.metadata.tasks.TaskDef)1 TaskResult (com.netflix.conductor.common.metadata.tasks.TaskResult)1 RerunWorkflowRequest (com.netflix.conductor.common.metadata.workflow.RerunWorkflowRequest)1 TaskType (com.netflix.conductor.common.metadata.workflow.TaskType)1 WorkflowDef (com.netflix.conductor.common.metadata.workflow.WorkflowDef)1 WorkflowStatus (com.netflix.conductor.common.run.Workflow.WorkflowStatus)1