Search in sources :

Example 1 with TaskResult

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

the class SampleWorker method execute.

@Override
public TaskResult execute(Task task) {
    TaskResult result = new TaskResult(task);
    result.setStatus(Status.COMPLETED);
    // Register the output of the task
    result.getOutputData().put("outputKey1", "value");
    result.getOutputData().put("oddEven", 1);
    result.getOutputData().put("mod", 4);
    return result;
}
Also used : TaskResult(com.netflix.conductor.common.metadata.tasks.TaskResult)

Example 2 with TaskResult

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

the class WorkflowTaskCoordinatorTests method testReturnTaskWhenRejectedExecutionExceptionThrown.

@Test
public void testReturnTaskWhenRejectedExecutionExceptionThrown() {
    Task testTask = new Task();
    testTask.setStatus(Task.Status.IN_PROGRESS);
    Worker worker = mock(Worker.class);
    when(worker.getPollingInterval()).thenReturn(3000);
    when(worker.getPollCount()).thenReturn(1);
    when(worker.getTaskDefName()).thenReturn("test");
    when(worker.preAck(any())).thenReturn(true);
    when(worker.execute(any())).thenAnswer(invocation -> {
        // Sleep for 2 seconds to trigger RejectedExecutionException
        Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS);
        return new TaskResult(testTask);
    });
    TaskClient client = Mockito.mock(TaskClient.class);
    WorkflowTaskCoordinator coordinator = new WorkflowTaskCoordinator.Builder().withWorkers(worker).withThreadCount(1).withWorkerQueueSize(1).withSleepWhenRetry(100000).withUpdateRetryCount(1).withTaskClient(client).withWorkerNamePrefix("test-worker-").build();
    when(client.batchPollTasksInDomain(anyString(), isNull(), isNull(), anyInt(), anyInt())).thenReturn(ImmutableList.of(testTask, testTask, testTask));
    when(client.ack(any(), any())).thenReturn(true);
    CountDownLatch latch = new CountDownLatch(3);
    doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        TaskResult result = (TaskResult) args[0];
        assertEquals(TaskResult.Status.IN_PROGRESS, result.getStatus());
        latch.countDown();
        return null;
    }).when(client).updateTask(any());
    coordinator.init();
    Uninterruptibles.awaitUninterruptibly(latch);
    // With worker queue set to 1, first two tasks can be submitted, and third one would get
    // RejectedExceptionExcpetion, so worker.execute() should be called twice.
    verify(worker, times(2)).execute(any());
    // task must be updated with IN_PROGRESS status three times, two from worker.execute() and
    // one from returnTask caused by RejectedExecutionException.
    verify(client, times(3)).updateTask(any());
}
Also used : TaskClient(com.netflix.conductor.client.http.TaskClient) Task(com.netflix.conductor.common.metadata.tasks.Task) Worker(com.netflix.conductor.client.worker.Worker) TaskResult(com.netflix.conductor.common.metadata.tasks.TaskResult) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 3 with TaskResult

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

the class WorkflowTaskCoordinatorTests method testLargePayloadCanFailUpdateWithRetry.

@Test
public void testLargePayloadCanFailUpdateWithRetry() {
    Task testTask = new Task();
    testTask.setStatus(Task.Status.COMPLETED);
    Worker worker = mock(Worker.class);
    when(worker.getPollingInterval()).thenReturn(3000);
    when(worker.getPollCount()).thenReturn(1);
    when(worker.getTaskDefName()).thenReturn("test");
    when(worker.preAck(any())).thenReturn(true);
    when(worker.execute(any())).thenAnswer(invocation -> {
        // Sleep for 2 seconds to trigger RejectedExecutionException
        Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS);
        return new TaskResult(testTask);
    });
    TaskClient taskClient = Mockito.mock(TaskClient.class);
    WorkflowTaskCoordinator coordinator = new WorkflowTaskCoordinator.Builder().withWorkers(worker).withThreadCount(1).withWorkerQueueSize(1).withSleepWhenRetry(100000).withUpdateRetryCount(3).withTaskClient(taskClient).withWorkerNamePrefix("test-worker-").build();
    when(taskClient.batchPollTasksInDomain(anyString(), isNull(), isNull(), anyInt(), anyInt())).thenReturn(ImmutableList.of(testTask));
    when(taskClient.ack(any(), any())).thenReturn(true);
    doThrow(ConductorClientException.class).when(taskClient).evaluateAndUploadLargePayload(any(TaskResult.class), any());
    CountDownLatch latch = new CountDownLatch(1);
    doAnswer(invocation -> {
        latch.countDown();
        return null;
    }).when(worker).onErrorUpdate(any());
    coordinator.init();
    Uninterruptibles.awaitUninterruptibly(latch);
    // When evaluateAndUploadLargePayload fails indefinitely, task update shouldn't be called.
    verify(taskClient, times(0)).updateTask(any());
}
Also used : TaskClient(com.netflix.conductor.client.http.TaskClient) Task(com.netflix.conductor.common.metadata.tasks.Task) Worker(com.netflix.conductor.client.worker.Worker) TaskResult(com.netflix.conductor.common.metadata.tasks.TaskResult) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 4 with TaskResult

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

the class WorkflowTaskCoordinatorTests method testThreadPool.

@Test
public void testThreadPool() {
    Worker worker = Worker.create("test", TaskResult::new);
    WorkflowTaskCoordinator coordinator = new WorkflowTaskCoordinator.Builder().withWorkers(worker, worker, worker).withTaskClient(new TaskClient()).build();
    // Not initialized yet
    assertEquals(-1, coordinator.getThreadCount());
    coordinator.init();
    assertEquals(3, coordinator.getThreadCount());
    // 100 is the default value
    assertEquals(100, coordinator.getWorkerQueueSize());
    assertEquals(500, coordinator.getSleepWhenRetry());
    assertEquals(3, coordinator.getUpdateRetryCount());
    coordinator = new WorkflowTaskCoordinator.Builder().withWorkers(worker).withThreadCount(100).withWorkerQueueSize(400).withSleepWhenRetry(100).withUpdateRetryCount(10).withTaskClient(new TaskClient()).withWorkerNamePrefix("test-worker-").build();
    assertEquals(100, coordinator.getThreadCount());
    coordinator.init();
    assertEquals(100, coordinator.getThreadCount());
    assertEquals(400, coordinator.getWorkerQueueSize());
    assertEquals(100, coordinator.getSleepWhenRetry());
    assertEquals(10, coordinator.getUpdateRetryCount());
    assertEquals("test-worker-", coordinator.getWorkerNamePrefix());
}
Also used : TaskClient(com.netflix.conductor.client.http.TaskClient) Worker(com.netflix.conductor.client.worker.Worker) TaskResult(com.netflix.conductor.common.metadata.tasks.TaskResult) Test(org.junit.Test)

Example 5 with TaskResult

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

the class TaskPollExecutorTest method testTaskExecutionException.

@Test
public void testTaskExecutionException() {
    Worker worker = Worker.create(TEST_TASK_DEF_NAME, task -> {
        throw new NoSuchMethodError();
    });
    TaskClient taskClient = Mockito.mock(TaskClient.class);
    TaskPollExecutor taskPollExecutor = new TaskPollExecutor(null, taskClient, 1, 1, new HashMap<>(), "test-worker-%d");
    when(taskClient.pollTask(any(), any(), any())).thenReturn(testTask());
    when(taskClient.ack(any(), any())).thenReturn(true);
    CountDownLatch latch = new CountDownLatch(1);
    doAnswer(invocation -> {
        assertEquals("test-worker-1", Thread.currentThread().getName());
        Object[] args = invocation.getArguments();
        TaskResult result = (TaskResult) args[0];
        assertEquals(TaskResult.Status.FAILED, result.getStatus());
        latch.countDown();
        return null;
    }).when(taskClient).updateTask(any());
    Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(() -> taskPollExecutor.pollAndExecute(worker), 0, 1, TimeUnit.SECONDS);
    Uninterruptibles.awaitUninterruptibly(latch);
    verify(taskClient).updateTask(any());
}
Also used : TaskClient(com.netflix.conductor.client.http.TaskClient) Worker(com.netflix.conductor.client.worker.Worker) TaskResult(com.netflix.conductor.common.metadata.tasks.TaskResult) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

TaskResult (com.netflix.conductor.common.metadata.tasks.TaskResult)41 Test (org.junit.Test)29 Task (com.netflix.conductor.common.metadata.tasks.Task)22 Workflow (com.netflix.conductor.common.run.Workflow)17 TaskClient (com.netflix.conductor.client.http.TaskClient)11 Worker (com.netflix.conductor.client.worker.Worker)11 WorkflowTask (com.netflix.conductor.common.metadata.workflow.WorkflowTask)10 CountDownLatch (java.util.concurrent.CountDownLatch)9 SubWorkflow (com.netflix.conductor.core.execution.tasks.SubWorkflow)8 HashMap (java.util.HashMap)8 StartWorkflowRequest (com.netflix.conductor.common.metadata.workflow.StartWorkflowRequest)7 UserTask (com.netflix.conductor.tests.utils.UserTask)7 WorkflowDef (com.netflix.conductor.common.metadata.workflow.WorkflowDef)6 TaskDef (com.netflix.conductor.common.metadata.tasks.TaskDef)4 ApplicationException (com.netflix.conductor.core.execution.ApplicationException)4 Map (java.util.Map)4 ConductorClientException (com.netflix.conductor.client.exceptions.ConductorClientException)2 Action (com.netflix.conductor.common.metadata.events.EventHandler.Action)2 StartWorkflow (com.netflix.conductor.common.metadata.events.EventHandler.StartWorkflow)2 TaskDetails (com.netflix.conductor.common.metadata.events.EventHandler.TaskDetails)2