Search in sources :

Example 1 with JobCoordinator

use of org.apache.samza.coordinator.JobCoordinator in project samza by apache.

the class TestStreamProcessor method testStopByProcessor.

/**
   * Tests stop() method when Container AND JobCoordinator are running
   */
@Test
public void testStopByProcessor() {
    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 StreamProcessorLifecycleListener() {

        @Override
        public void onStart() {
            processorListenerStart.countDown();
        }

        @Override
        public void onShutdown() {
            processorListenerStop.countDown();
        }

        @Override
        public void onFailure(Throwable t) {
        }
    }, mockJobCoordinator);
    Map containers = mock(Map.class);
    doReturn(true).when(containers).containsKey(anyString());
    when(containers.get(anyString())).thenReturn(mock(ContainerModel.class));
    JobModel mockJobModel = mock(JobModel.class);
    when(mockJobModel.getContainers()).thenReturn(containers);
    final CountDownLatch coordinatorStop = new CountDownLatch(1);
    final Thread jcThread = new Thread(() -> {
        try {
            processor.jobCoordinatorListener.onNewJobModel("1", mockJobModel);
            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();
    try {
        processor.start();
        processorListenerStart.await();
        Assert.assertEquals(SamzaContainerStatus.STARTED, processor.containerReference.getStatus());
        // 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();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) StreamTaskFactory(org.apache.samza.task.StreamTaskFactory) ContainerModel(org.apache.samza.job.model.ContainerModel) JobCoordinator(org.apache.samza.coordinator.JobCoordinator) JobModel(org.apache.samza.job.model.JobModel) MapConfig(org.apache.samza.config.MapConfig) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 2 with JobCoordinator

use of org.apache.samza.coordinator.JobCoordinator in project samza by apache.

the class TestZkStreamProcessorBase method createStreamProcessor.

protected StreamProcessor createStreamProcessor(final String pId, Map<String, String> map, final Object mutexStart, final Object mutexStop) {
    map.put(ApplicationConfig.PROCESSOR_ID, pId);
    Config config = new MapConfig(map);
    JobCoordinator jobCoordinator = Util.<JobCoordinatorFactory>getObj(new JobCoordinatorConfig(config).getJobCoordinatorFactoryClassName()).getJobCoordinator(config);
    StreamProcessorLifecycleListener listener = new StreamProcessorLifecycleListener() {

        @Override
        public void onStart() {
            if (mutexStart != null) {
                synchronized (mutexStart) {
                    mutexStart.notifyAll();
                }
            }
            LOG.info("onStart is called for pid=" + pId);
        }

        @Override
        public void onShutdown() {
            if (mutexStop != null) {
                synchronized (mutexStart) {
                    mutexStart.notify();
                }
            }
            LOG.info("onShutdown is called for pid=" + pId);
        }

        @Override
        public void onFailure(Throwable t) {
            LOG.info("onFailure is called for pid=" + pId);
        }
    };
    StreamProcessor processor = new StreamProcessor(config, new HashMap<>(), (StreamTaskFactory) TestStreamTask::new, listener, jobCoordinator);
    return processor;
}
Also used : ApplicationConfig(org.apache.samza.config.ApplicationConfig) MapConfig(org.apache.samza.config.MapConfig) ZkConfig(org.apache.samza.config.ZkConfig) JobCoordinatorConfig(org.apache.samza.config.JobCoordinatorConfig) Config(org.apache.samza.config.Config) JobCoordinator(org.apache.samza.coordinator.JobCoordinator) JobCoordinatorConfig(org.apache.samza.config.JobCoordinatorConfig) MapConfig(org.apache.samza.config.MapConfig)

Aggregations

MapConfig (org.apache.samza.config.MapConfig)2 JobCoordinator (org.apache.samza.coordinator.JobCoordinator)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ApplicationConfig (org.apache.samza.config.ApplicationConfig)1 Config (org.apache.samza.config.Config)1 JobCoordinatorConfig (org.apache.samza.config.JobCoordinatorConfig)1 ZkConfig (org.apache.samza.config.ZkConfig)1 ContainerModel (org.apache.samza.job.model.ContainerModel)1 JobModel (org.apache.samza.job.model.JobModel)1 StreamTaskFactory (org.apache.samza.task.StreamTaskFactory)1 Test (org.junit.Test)1