Search in sources :

Example 6 with ReadableCoordinator

use of org.apache.samza.task.ReadableCoordinator in project samza by apache.

the class TestRunLoop method testCommitWithMessageInFlightWhenAsyncCommitIsEnabled.

@Test
public void testCommitWithMessageInFlightWhenAsyncCommitIsEnabled() {
    int maxMessagesInFlight = 2;
    ExecutorService taskExecutor = Executors.newFixedThreadPool(2);
    SystemConsumers consumerMultiplexer = mock(SystemConsumers.class);
    OffsetManager offsetManager = mock(OffsetManager.class);
    RunLoopTask task0 = getMockRunLoopTask(taskName0, ssp0);
    when(task0.offsetManager()).thenReturn(offsetManager);
    CountDownLatch firstMessageBarrier = new CountDownLatch(1);
    doAnswer(invocation -> {
        ReadableCoordinator coordinator = invocation.getArgumentAt(1, ReadableCoordinator.class);
        TaskCallbackFactory callbackFactory = invocation.getArgumentAt(2, TaskCallbackFactory.class);
        TaskCallback callback = callbackFactory.createCallback();
        taskExecutor.submit(() -> {
            firstMessageBarrier.await();
            coordinator.commit(TaskCoordinator.RequestScope.CURRENT_TASK);
            callback.complete();
            return null;
        });
        return null;
    }).when(task0).process(eq(envelope00), any(), any());
    CountDownLatch secondMessageBarrier = new CountDownLatch(1);
    doAnswer(invocation -> {
        ReadableCoordinator coordinator = invocation.getArgumentAt(1, ReadableCoordinator.class);
        TaskCallbackFactory callbackFactory = invocation.getArgumentAt(2, TaskCallbackFactory.class);
        TaskCallback callback = callbackFactory.createCallback();
        taskExecutor.submit(() -> {
            // let the first message proceed to ask for a commit
            firstMessageBarrier.countDown();
            // block this message until commit is executed
            secondMessageBarrier.await();
            coordinator.shutdown(TaskCoordinator.RequestScope.CURRENT_TASK);
            callback.complete();
            return null;
        });
        return null;
    }).when(task0).process(eq(envelope01), any(), any());
    doAnswer(invocation -> {
        assertEquals(1, task0.metrics().asyncCallbackCompleted().getCount());
        assertEquals(1, task0.metrics().messagesInFlight().getValue());
        secondMessageBarrier.countDown();
        return null;
    }).when(task0).commit();
    Map<TaskName, RunLoopTask> tasks = new HashMap<>();
    tasks.put(taskName0, task0);
    RunLoop runLoop = new RunLoop(tasks, executor, consumerMultiplexer, maxMessagesInFlight, windowMs, commitMs, callbackTimeoutMs, maxThrottlingDelayMs, maxIdleMs, containerMetrics, () -> 0L, true);
    when(consumerMultiplexer.choose(false)).thenReturn(envelope00).thenReturn(envelope01).thenReturn(null);
    runLoop.run();
    InOrder inOrder = inOrder(task0);
    inOrder.verify(task0).process(eq(envelope00), any(), any());
    inOrder.verify(task0).process(eq(envelope01), any(), any());
    inOrder.verify(task0).commit();
}
Also used : SystemConsumers(org.apache.samza.system.SystemConsumers) InOrder(org.mockito.InOrder) HashMap(java.util.HashMap) OffsetManager(org.apache.samza.checkpoint.OffsetManager) TaskCallback(org.apache.samza.task.TaskCallback) CountDownLatch(java.util.concurrent.CountDownLatch) ExecutorService(java.util.concurrent.ExecutorService) TaskCallbackFactory(org.apache.samza.task.TaskCallbackFactory) ReadableCoordinator(org.apache.samza.task.ReadableCoordinator) Test(org.junit.Test)

Aggregations

HashMap (java.util.HashMap)6 SystemConsumers (org.apache.samza.system.SystemConsumers)6 ReadableCoordinator (org.apache.samza.task.ReadableCoordinator)6 Test (org.junit.Test)6 TaskCallback (org.apache.samza.task.TaskCallback)5 TaskCallbackFactory (org.apache.samza.task.TaskCallbackFactory)5 InOrder (org.mockito.InOrder)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 ExecutorService (java.util.concurrent.ExecutorService)2 OffsetManager (org.apache.samza.checkpoint.OffsetManager)2