Search in sources :

Example 6 with Worker

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

the class TaskPollExecutorTest method testTaskPoll.

@Test
public void testTaskPoll() {
    Task task = testTask();
    Worker worker = mock(Worker.class);
    when(worker.getPollingInterval()).thenReturn(3000);
    when(worker.getTaskDefName()).thenReturn("test");
    when(worker.execute(any())).thenReturn(new TaskResult(task));
    TaskClient taskClient = Mockito.mock(TaskClient.class);
    when(taskClient.pollTask(any(), any(), any())).thenReturn(new Task()).thenReturn(task);
    TaskPollExecutor taskPollExecutor = new TaskPollExecutor(null, taskClient, 1, 1, new HashMap<>(), "test-worker-");
    CountDownLatch latch = new CountDownLatch(1);
    doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        TaskResult result = (TaskResult) args[0];
        assertEquals(IN_PROGRESS, result.getStatus());
        assertEquals(task.getTaskId(), result.getTaskId());
        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) 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 7 with Worker

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

the class TaskRunnerConfigurerTest method testThreadPool.

@Test
public void testThreadPool() {
    Worker worker = Worker.create(TEST_TASK_DEF_NAME, TaskResult::new);
    TaskRunnerConfigurer configurer = new TaskRunnerConfigurer.Builder(new TaskClient(), Arrays.asList(worker, worker, worker)).build();
    configurer.init();
    assertEquals(3, configurer.getThreadCount());
    assertEquals(500, configurer.getSleepWhenRetry());
    assertEquals(3, configurer.getUpdateRetryCount());
    configurer = new TaskRunnerConfigurer.Builder(new TaskClient(), Collections.singletonList(worker)).withThreadCount(100).withSleepWhenRetry(100).withUpdateRetryCount(10).withWorkerNamePrefix("test-worker-").build();
    assertEquals(100, configurer.getThreadCount());
    configurer.init();
    assertEquals(100, configurer.getThreadCount());
    assertEquals(100, configurer.getSleepWhenRetry());
    assertEquals(10, configurer.getUpdateRetryCount());
    assertEquals("test-worker-", configurer.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 8 with Worker

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

the class TaskRunnerConfigurerTest method testMultipleWorkersExecution.

@Test
public void testMultipleWorkersExecution() {
    String task1Name = "task1";
    Worker worker1 = mock(Worker.class);
    when(worker1.getPollingInterval()).thenReturn(3000);
    when(worker1.getTaskDefName()).thenReturn(task1Name);
    when(worker1.getIdentity()).thenReturn("worker1");
    when(worker1.execute(any())).thenAnswer(invocation -> {
        // Sleep for 2 seconds to simulate task execution
        Uninterruptibles.sleepUninterruptibly(2, TimeUnit.SECONDS);
        TaskResult taskResult = new TaskResult();
        taskResult.setStatus(COMPLETED);
        return taskResult;
    });
    String task2Name = "task2";
    Worker worker2 = mock(Worker.class);
    when(worker2.getPollingInterval()).thenReturn(3000);
    when(worker2.getTaskDefName()).thenReturn(task2Name);
    when(worker2.getIdentity()).thenReturn("worker2");
    when(worker2.execute(any())).thenAnswer(invocation -> {
        // Sleep for 2 seconds to simulate task execution
        Uninterruptibles.sleepUninterruptibly(2, TimeUnit.SECONDS);
        TaskResult taskResult = new TaskResult();
        taskResult.setStatus(COMPLETED);
        return taskResult;
    });
    Task task1 = testTask(task1Name);
    Task task2 = testTask(task2Name);
    TaskClient taskClient = Mockito.mock(TaskClient.class);
    TaskRunnerConfigurer configurer = new TaskRunnerConfigurer.Builder(taskClient, Arrays.asList(worker1, worker2)).withThreadCount(2).withSleepWhenRetry(100000).withUpdateRetryCount(1).withWorkerNamePrefix("test-worker-").build();
    when(taskClient.pollTask(any(), any(), any())).thenAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        String taskName = args[0].toString();
        if (taskName.equals(task1Name)) {
            return task1;
        } else if (taskName.equals(task2Name)) {
            return task2;
        } else {
            return null;
        }
    });
    when(taskClient.ack(any(), any())).thenReturn(true);
    AtomicInteger task1Counter = new AtomicInteger(0);
    AtomicInteger task2Counter = new AtomicInteger(0);
    CountDownLatch latch = new CountDownLatch(2);
    doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        TaskResult result = (TaskResult) args[0];
        assertEquals(COMPLETED, result.getStatus());
        if (result.getWorkerId().equals("worker1")) {
            task1Counter.incrementAndGet();
        } else if (result.getWorkerId().equals("worker2")) {
            task2Counter.incrementAndGet();
        }
        latch.countDown();
        return null;
    }).when(taskClient).updateTask(any());
    configurer.init();
    Uninterruptibles.awaitUninterruptibly(latch);
    assertEquals(1, task1Counter.get());
    assertEquals(1, task2Counter.get());
}
Also used : TaskClient(com.netflix.conductor.client.http.TaskClient) Task(com.netflix.conductor.common.metadata.tasks.Task) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) 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 9 with Worker

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

the class WorkflowTaskCoordinatorTests method testNoOpWhenAckFailed.

@Test
public void testNoOpWhenAckFailed() {
    Worker worker = mock(Worker.class);
    when(worker.getPollingInterval()).thenReturn(1000);
    when(worker.getPollCount()).thenReturn(1);
    when(worker.getTaskDefName()).thenReturn("test");
    when(worker.preAck(any())).thenReturn(true);
    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();
    Task testTask = new Task();
    testTask.setStatus(Task.Status.IN_PROGRESS);
    when(client.batchPollTasksInDomain(any(), any(), any(), anyInt(), anyInt())).thenReturn(ImmutableList.of(testTask));
    when(client.ack(any(), any())).thenReturn(false);
    coordinator.init();
    Uninterruptibles.sleepUninterruptibly(2, TimeUnit.SECONDS);
    verify(client, atLeastOnce()).ack(any(), any());
    // then worker.execute must not be called and task must be updated with IN_PROGRESS status
    verify(worker, never()).execute(any());
    verify(client, never()).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) Test(org.junit.Test)

Example 10 with Worker

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

the class WorkflowTaskCoordinatorTests method testNoOpWhenAckThrowsException.

@Test
public void testNoOpWhenAckThrowsException() {
    Worker worker = mock(Worker.class);
    when(worker.getPollingInterval()).thenReturn(1000);
    when(worker.getPollCount()).thenReturn(1);
    when(worker.getTaskDefName()).thenReturn("test");
    when(worker.preAck(any())).thenReturn(true);
    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();
    Task testTask = new Task();
    testTask.setStatus(Task.Status.IN_PROGRESS);
    when(client.batchPollTasksInDomain(any(), any(), any(), anyInt(), anyInt())).thenReturn(ImmutableList.of(testTask));
    when(client.ack(any(), any())).thenThrow(new RuntimeException("Ack failed"));
    coordinator.init();
    Uninterruptibles.sleepUninterruptibly(2, TimeUnit.SECONDS);
    verify(client).ack(any(), any());
    // then worker.execute must not be called and task must be updated with IN_PROGRESS status
    verify(worker, never()).execute(any());
    verify(client, never()).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) 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