Search in sources :

Example 1 with Worker

use of com.netflix.conductor.client.worker.Worker 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 2 with Worker

use of com.netflix.conductor.client.worker.Worker 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 3 with Worker

use of com.netflix.conductor.client.worker.Worker 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 4 with Worker

use of com.netflix.conductor.client.worker.Worker 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)

Example 5 with Worker

use of com.netflix.conductor.client.worker.Worker in project conductor by Netflix.

the class TaskPollExecutorTest method testMultipleTasksExecution.

@SuppressWarnings("rawtypes")
@Test
public void testMultipleTasksExecution() {
    String outputKey = "KEY";
    Task task = testTask();
    Worker worker = mock(Worker.class);
    when(worker.getPollingInterval()).thenReturn(3000);
    when(worker.getTaskDefName()).thenReturn(TEST_TASK_DEF_NAME);
    when(worker.execute(any())).thenAnswer(new Answer() {

        private int count = 0;

        Map<String, Object> outputMap = new HashMap<>();

        public TaskResult answer(InvocationOnMock invocation) {
            // Sleep for 2 seconds to simulate task execution
            Uninterruptibles.sleepUninterruptibly(2, TimeUnit.SECONDS);
            TaskResult taskResult = new TaskResult(task);
            outputMap.put(outputKey, count++);
            taskResult.setOutputData(outputMap);
            return taskResult;
        }
    });
    TaskClient taskClient = Mockito.mock(TaskClient.class);
    TaskPollExecutor taskPollExecutor = new TaskPollExecutor(null, taskClient, 1, 1, new HashMap<>(), "test-worker-");
    when(taskClient.pollTask(any(), any(), any())).thenReturn(task);
    when(taskClient.ack(any(), any())).thenReturn(true);
    CountDownLatch latch = new CountDownLatch(3);
    doAnswer(new Answer() {

        private int count = 0;

        public TaskResult answer(InvocationOnMock invocation) {
            Object[] args = invocation.getArguments();
            TaskResult result = (TaskResult) args[0];
            assertEquals(IN_PROGRESS, result.getStatus());
            assertEquals(count, result.getOutputData().get(outputKey));
            count++;
            latch.countDown();
            return null;
        }
    }).when(taskClient).updateTask(any());
    Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(() -> taskPollExecutor.pollAndExecute(worker), 0, 1, TimeUnit.SECONDS);
    Uninterruptibles.awaitUninterruptibly(latch);
    // execute() is called 3 times on the worker (once for each task)
    verify(worker, times(3)).execute(any());
    verify(taskClient, times(3)).updateTask(any());
}
Also used : Task(com.netflix.conductor.common.metadata.tasks.Task) HashMap(java.util.HashMap) CountDownLatch(java.util.concurrent.CountDownLatch) TaskClient(com.netflix.conductor.client.http.TaskClient) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Worker(com.netflix.conductor.client.worker.Worker) TaskResult(com.netflix.conductor.common.metadata.tasks.TaskResult) Test(org.junit.Test)

Aggregations

TaskClient (com.netflix.conductor.client.http.TaskClient)16 Worker (com.netflix.conductor.client.worker.Worker)16 Test (org.junit.Test)14 TaskResult (com.netflix.conductor.common.metadata.tasks.TaskResult)12 Task (com.netflix.conductor.common.metadata.tasks.Task)11 CountDownLatch (java.util.concurrent.CountDownLatch)10 ConductorClientException (com.netflix.conductor.client.exceptions.ConductorClientException)2 HashMap (java.util.HashMap)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 InstanceStatus (com.netflix.appinfo.InstanceInfo.InstanceStatus)1 TaskRunnerConfigurer (com.netflix.conductor.client.automator.TaskRunnerConfigurer)1 MetricsContainer (com.netflix.conductor.client.telemetry.MetricsContainer)1 MetricsContainer.getPollTimer (com.netflix.conductor.client.telemetry.MetricsContainer.getPollTimer)1 MetricsContainer.incrementTaskPollCount (com.netflix.conductor.client.telemetry.MetricsContainer.incrementTaskPollCount)1 PropertyFactory (com.netflix.conductor.client.worker.PropertyFactory)1 RetryUtil (com.netflix.conductor.common.utils.RetryUtil)1 EurekaClient (com.netflix.discovery.EurekaClient)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 Arrays (java.util.Arrays)1