Search in sources :

Example 6 with ProcessorLifecycleListener

use of org.apache.samza.runtime.ProcessorLifecycleListener in project samza by apache.

the class TestStreamProcessor method testCoordinatorFailureShouldStopTheStreamProcessor.

@Test
public void testCoordinatorFailureShouldStopTheStreamProcessor() {
    JobCoordinator mockJobCoordinator = Mockito.mock(JobCoordinator.class);
    ProcessorLifecycleListener lifecycleListener = Mockito.mock(ProcessorLifecycleListener.class);
    SamzaContainer mockSamzaContainer = Mockito.mock(SamzaContainer.class);
    MapConfig config = new MapConfig(ImmutableMap.of("task.shutdown.ms", "0"));
    StreamProcessor streamProcessor = new StreamProcessor("TestProcessorId", config, new HashMap<>(), null, Optional.empty(), Optional.empty(), Optional.empty(), sp -> lifecycleListener, mockJobCoordinator, Mockito.mock(MetadataStore.class));
    Exception failureException = new Exception("dummy exception");
    streamProcessor.container = mockSamzaContainer;
    streamProcessor.state = State.RUNNING;
    streamProcessor.jobCoordinatorListener.onCoordinatorFailure(failureException);
    Mockito.doNothing().when(mockSamzaContainer).shutdown();
    Mockito.when(mockSamzaContainer.hasStopped()).thenReturn(false);
    assertEquals(State.STOPPED, streamProcessor.state);
    Mockito.verify(lifecycleListener).afterFailure(failureException);
    Mockito.verify(mockSamzaContainer).shutdown();
}
Also used : MetadataStore(org.apache.samza.metadatastore.MetadataStore) JobCoordinator(org.apache.samza.coordinator.JobCoordinator) MapConfig(org.apache.samza.config.MapConfig) ProcessorLifecycleListener(org.apache.samza.runtime.ProcessorLifecycleListener) SamzaContainer(org.apache.samza.container.SamzaContainer) TimeoutException(java.util.concurrent.TimeoutException) SamzaException(org.apache.samza.SamzaException) Test(org.junit.Test)

Example 7 with ProcessorLifecycleListener

use of org.apache.samza.runtime.ProcessorLifecycleListener in project samza by apache.

the class TestStreamProcessor method testStopByProcessor.

/**
 * Tests stop() method when Container AND JobCoordinator are running
 */
@Test
public void testStopByProcessor() throws InterruptedException {
    JobCoordinator mockJobCoordinator = mock(JobCoordinator.class);
    final CountDownLatch processorListenerStop = new CountDownLatch(1);
    final CountDownLatch processorListenerStart = new CountDownLatch(1);
    TestableStreamProcessor processor = new TestableStreamProcessor(new MapConfig(), new HashMap<>(), mock(StreamTaskFactory.class), new ProcessorLifecycleListener() {

        @Override
        public void afterStart() {
            processorListenerState.put(ListenerCallback.AFTER_START, true);
            processorListenerStart.countDown();
        }

        @Override
        public void afterFailure(Throwable t) {
            processorListenerState.put(ListenerCallback.AFTER_FAILURE, true);
        }

        @Override
        public void afterStop() {
            processorListenerState.put(ListenerCallback.AFTER_STOP, true);
            processorListenerStop.countDown();
        }

        @Override
        public void beforeStart() {
            processorListenerState.put(ListenerCallback.BEFORE_START, true);
        }
    }, mockJobCoordinator, null);
    final CountDownLatch coordinatorStop = new CountDownLatch(1);
    final Thread jcThread = new Thread(() -> {
        try {
            processor.jobCoordinatorListener.onJobModelExpired();
            processor.jobCoordinatorListener.onNewJobModel("1", getMockJobModel());
            coordinatorStop.await();
            processor.jobCoordinatorListener.onCoordinatorStop();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    });
    doAnswer(invocation -> {
        coordinatorStop.countDown();
        return null;
    }).when(mockJobCoordinator).stop();
    doAnswer(invocation -> {
        jcThread.start();
        return null;
    }).when(mockJobCoordinator).start();
    processor.start();
    processorListenerStart.await(10, TimeUnit.SECONDS);
    assertEquals(SamzaContainerStatus.STARTED, processor.getContainerStatus());
    // This block is required for the mockRunloop is actually start.
    // Otherwise, processor.stop gets triggered before mockRunloop begins to block
    processor.runLoopStartForMain.await();
    processor.stop();
    processorListenerStop.await();
    // Assertions on which callbacks are expected to be invoked
    assertTrue(processorListenerState.get(ListenerCallback.BEFORE_START));
    assertTrue(processorListenerState.get(ListenerCallback.AFTER_START));
    assertTrue(processorListenerState.get(ListenerCallback.AFTER_STOP));
    Assert.assertFalse(processorListenerState.get(ListenerCallback.AFTER_FAILURE));
}
Also used : JobCoordinator(org.apache.samza.coordinator.JobCoordinator) MapConfig(org.apache.samza.config.MapConfig) CountDownLatch(java.util.concurrent.CountDownLatch) ProcessorLifecycleListener(org.apache.samza.runtime.ProcessorLifecycleListener) StreamTaskFactory(org.apache.samza.task.StreamTaskFactory) Test(org.junit.Test)

Example 8 with ProcessorLifecycleListener

use of org.apache.samza.runtime.ProcessorLifecycleListener in project samza by apache.

the class TestStreamProcessor method testStartOperationShouldBeIdempotent.

@Test
public void testStartOperationShouldBeIdempotent() {
    JobCoordinator mockJobCoordinator = Mockito.mock(JobCoordinator.class);
    Mockito.doNothing().when(mockJobCoordinator).start();
    ProcessorLifecycleListener lifecycleListener = Mockito.mock(ProcessorLifecycleListener.class);
    StreamProcessor streamProcessor = Mockito.spy(new StreamProcessor("TestProcessorId", new MapConfig(), new HashMap<>(), null, Optional.empty(), Optional.empty(), Optional.empty(), sp -> lifecycleListener, mockJobCoordinator, Mockito.mock(MetadataStore.class)));
    assertEquals(State.NEW, streamProcessor.getState());
    streamProcessor.start();
    assertEquals(State.STARTED, streamProcessor.getState());
    streamProcessor.start();
    assertEquals(State.STARTED, streamProcessor.getState());
    Mockito.verify(mockJobCoordinator, Mockito.times(1)).start();
}
Also used : SamzaContainer(org.apache.samza.container.SamzaContainer) JobCoordinator(org.apache.samza.coordinator.JobCoordinator) TimeoutException(java.util.concurrent.TimeoutException) HashMap(java.util.HashMap) TaskFactory(org.apache.samza.task.TaskFactory) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matchers.anyString(org.mockito.Matchers.anyString) ConcurrentMap(java.util.concurrent.ConcurrentMap) MetadataStore(org.apache.samza.metadatastore.MetadataStore) State(org.apache.samza.processor.StreamProcessor.State) Duration(java.time.Duration) Map(java.util.Map) MetricsReporter(org.apache.samza.metrics.MetricsReporter) StreamTask(org.apache.samza.task.StreamTask) After(org.junit.After) Mockito.doAnswer(org.mockito.Mockito.doAnswer) RunLoop(org.apache.samza.container.RunLoop) MapConfig(org.apache.samza.config.MapConfig) PowerMockito(org.powermock.api.mockito.PowerMockito) Mockito.doReturn(org.mockito.Mockito.doReturn) ExecutorService(java.util.concurrent.ExecutorService) JobModel(org.apache.samza.job.model.JobModel) Before(org.junit.Before) ImmutableMap(com.google.common.collect.ImmutableMap) TaskConfig(org.apache.samza.config.TaskConfig) Assert.assertNotNull(org.junit.Assert.assertNotNull) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) StreamTaskFactory(org.apache.samza.task.StreamTaskFactory) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) SamzaContainerStatus(org.apache.samza.container.SamzaContainerStatus) SamzaException(org.apache.samza.SamzaException) ProcessorLifecycleListener(org.apache.samza.runtime.ProcessorLifecycleListener) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) TimeUnit(java.util.concurrent.TimeUnit) Matchers.any(org.mockito.Matchers.any) CountDownLatch(java.util.concurrent.CountDownLatch) Mockito(org.mockito.Mockito) ChronoUnit(java.time.temporal.ChronoUnit) ContainerModel(org.apache.samza.job.model.ContainerModel) Optional(java.util.Optional) Config(org.apache.samza.config.Config) Assert(org.junit.Assert) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) JobCoordinator(org.apache.samza.coordinator.JobCoordinator) MapConfig(org.apache.samza.config.MapConfig) ProcessorLifecycleListener(org.apache.samza.runtime.ProcessorLifecycleListener) Test(org.junit.Test)

Example 9 with ProcessorLifecycleListener

use of org.apache.samza.runtime.ProcessorLifecycleListener in project samza by apache.

the class TestStreamProcessor method testStopShouldBeIdempotent.

@Test
public void testStopShouldBeIdempotent() {
    JobCoordinator mockJobCoordinator = Mockito.mock(JobCoordinator.class);
    ProcessorLifecycleListener lifecycleListener = Mockito.mock(ProcessorLifecycleListener.class);
    SamzaContainer mockSamzaContainer = Mockito.mock(SamzaContainer.class);
    MapConfig config = new MapConfig(ImmutableMap.of("task.shutdown.ms", "0"));
    StreamProcessor streamProcessor = PowerMockito.spy(new StreamProcessor("TestProcessorId", config, new HashMap<>(), null, Optional.empty(), Optional.empty(), Optional.empty(), sp -> lifecycleListener, mockJobCoordinator, Mockito.mock(MetadataStore.class)));
    Mockito.doNothing().when(mockJobCoordinator).stop();
    Mockito.doNothing().when(mockSamzaContainer).shutdown();
    Mockito.when(mockSamzaContainer.hasStopped()).thenReturn(false);
    Mockito.when(mockSamzaContainer.getStatus()).thenReturn(SamzaContainerStatus.STARTED).thenReturn(SamzaContainerStatus.STOPPED);
    streamProcessor.state = State.RUNNING;
    streamProcessor.stop();
    assertEquals(State.STOPPING, streamProcessor.state);
}
Also used : SamzaContainer(org.apache.samza.container.SamzaContainer) JobCoordinator(org.apache.samza.coordinator.JobCoordinator) TimeoutException(java.util.concurrent.TimeoutException) HashMap(java.util.HashMap) TaskFactory(org.apache.samza.task.TaskFactory) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matchers.anyString(org.mockito.Matchers.anyString) ConcurrentMap(java.util.concurrent.ConcurrentMap) MetadataStore(org.apache.samza.metadatastore.MetadataStore) State(org.apache.samza.processor.StreamProcessor.State) Duration(java.time.Duration) Map(java.util.Map) MetricsReporter(org.apache.samza.metrics.MetricsReporter) StreamTask(org.apache.samza.task.StreamTask) After(org.junit.After) Mockito.doAnswer(org.mockito.Mockito.doAnswer) RunLoop(org.apache.samza.container.RunLoop) MapConfig(org.apache.samza.config.MapConfig) PowerMockito(org.powermock.api.mockito.PowerMockito) Mockito.doReturn(org.mockito.Mockito.doReturn) ExecutorService(java.util.concurrent.ExecutorService) JobModel(org.apache.samza.job.model.JobModel) Before(org.junit.Before) ImmutableMap(com.google.common.collect.ImmutableMap) TaskConfig(org.apache.samza.config.TaskConfig) Assert.assertNotNull(org.junit.Assert.assertNotNull) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) StreamTaskFactory(org.apache.samza.task.StreamTaskFactory) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) SamzaContainerStatus(org.apache.samza.container.SamzaContainerStatus) SamzaException(org.apache.samza.SamzaException) ProcessorLifecycleListener(org.apache.samza.runtime.ProcessorLifecycleListener) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) TimeUnit(java.util.concurrent.TimeUnit) Matchers.any(org.mockito.Matchers.any) CountDownLatch(java.util.concurrent.CountDownLatch) Mockito(org.mockito.Mockito) ChronoUnit(java.time.temporal.ChronoUnit) ContainerModel(org.apache.samza.job.model.ContainerModel) Optional(java.util.Optional) Config(org.apache.samza.config.Config) Assert(org.junit.Assert) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) JobCoordinator(org.apache.samza.coordinator.JobCoordinator) MapConfig(org.apache.samza.config.MapConfig) ProcessorLifecycleListener(org.apache.samza.runtime.ProcessorLifecycleListener) SamzaContainer(org.apache.samza.container.SamzaContainer) Test(org.junit.Test)

Example 10 with ProcessorLifecycleListener

use of org.apache.samza.runtime.ProcessorLifecycleListener in project samza by apache.

the class TestStreamProcessor method testContainerFailureCorrectlyStopsProcessor.

/**
 * Tests that a failure in container correctly stops a running JobCoordinator and propagates the exception
 * through the StreamProcessor
 *
 * Assertions:
 * - JobCoordinator has been stopped from the JobCoordinatorListener callback
 * - ProcessorLifecycleListener#afterStop(Throwable) has been invoked w/ non-null Throwable
 */
@Test
public void testContainerFailureCorrectlyStopsProcessor() throws InterruptedException {
    JobCoordinator mockJobCoordinator = Mockito.mock(JobCoordinator.class);
    Throwable expectedThrowable = new SamzaException("Failure in Container!");
    AtomicReference<Throwable> actualThrowable = new AtomicReference<>();
    final CountDownLatch runLoopStartedLatch = new CountDownLatch(1);
    RunLoop failingRunLoop = mock(RunLoop.class);
    doAnswer(invocation -> {
        try {
            runLoopStartedLatch.countDown();
            throw expectedThrowable;
        } catch (InterruptedException ie) {
            ie.printStackTrace();
        }
        return null;
    }).when(failingRunLoop).run();
    SamzaContainer mockContainer = StreamProcessorTestUtils.getDummyContainer(failingRunLoop, mock(StreamTask.class));
    final CountDownLatch processorListenerFailed = new CountDownLatch(1);
    TestableStreamProcessor processor = new TestableStreamProcessor(new MapConfig(), new HashMap<>(), mock(StreamTaskFactory.class), new ProcessorLifecycleListener() {

        @Override
        public void beforeStart() {
            processorListenerState.put(ListenerCallback.BEFORE_START, true);
        }

        @Override
        public void afterStart() {
            processorListenerState.put(ListenerCallback.AFTER_START, true);
        }

        @Override
        public void afterStop() {
            processorListenerState.put(ListenerCallback.AFTER_STOP, true);
        }

        @Override
        public void afterFailure(Throwable t) {
            processorListenerState.put(ListenerCallback.AFTER_FAILURE, true);
            actualThrowable.getAndSet(t);
            processorListenerFailed.countDown();
        }
    }, mockJobCoordinator, mockContainer);
    final CountDownLatch coordinatorStop = new CountDownLatch(1);
    doAnswer(invocation -> {
        coordinatorStop.countDown();
        return null;
    }).when(mockJobCoordinator).stop();
    doAnswer(invocation -> {
        new Thread(() -> {
            try {
                processor.jobCoordinatorListener.onJobModelExpired();
                processor.jobCoordinatorListener.onNewJobModel("1", getMockJobModel());
                coordinatorStop.await();
                processor.jobCoordinatorListener.onCoordinatorStop();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
        return null;
    }).when(mockJobCoordinator).start();
    processor.start();
    // This block is required for the mockRunloop is actually started.
    // Otherwise, processor.stop gets triggered before mockRunloop begins to block
    runLoopStartedLatch.await();
    assertTrue("Container failed and processor listener failed was not invoked within timeout!", processorListenerFailed.await(30, TimeUnit.SECONDS));
    assertEquals(expectedThrowable, actualThrowable.get());
    assertTrue(processorListenerState.get(ListenerCallback.BEFORE_START));
    assertTrue(processorListenerState.get(ListenerCallback.AFTER_START));
    Assert.assertFalse(processorListenerState.get(ListenerCallback.AFTER_STOP));
    assertTrue(processorListenerState.get(ListenerCallback.AFTER_FAILURE));
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ProcessorLifecycleListener(org.apache.samza.runtime.ProcessorLifecycleListener) SamzaException(org.apache.samza.SamzaException) StreamTaskFactory(org.apache.samza.task.StreamTaskFactory) SamzaContainer(org.apache.samza.container.SamzaContainer) RunLoop(org.apache.samza.container.RunLoop) JobCoordinator(org.apache.samza.coordinator.JobCoordinator) MapConfig(org.apache.samza.config.MapConfig) StreamTask(org.apache.samza.task.StreamTask) Test(org.junit.Test)

Aggregations

MapConfig (org.apache.samza.config.MapConfig)12 JobCoordinator (org.apache.samza.coordinator.JobCoordinator)12 ProcessorLifecycleListener (org.apache.samza.runtime.ProcessorLifecycleListener)12 Test (org.junit.Test)11 MetadataStore (org.apache.samza.metadatastore.MetadataStore)7 SamzaContainer (org.apache.samza.container.SamzaContainer)6 CountDownLatch (java.util.concurrent.CountDownLatch)5 StreamTaskFactory (org.apache.samza.task.StreamTaskFactory)5 ExecutorService (java.util.concurrent.ExecutorService)4 TimeoutException (java.util.concurrent.TimeoutException)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 SamzaException (org.apache.samza.SamzaException)4 HashMap (java.util.HashMap)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 Config (org.apache.samza.config.Config)3 TaskConfig (org.apache.samza.config.TaskConfig)3 RunLoop (org.apache.samza.container.RunLoop)3 JobModel (org.apache.samza.job.model.JobModel)3 StreamTask (org.apache.samza.task.StreamTask)3 ImmutableMap (com.google.common.collect.ImmutableMap)2