@Override public void run(ExternalContext externalContext) { try { List<JobConfig> jobConfigs = planner.prepareJobs(); // create the StreamProcessors if (jobConfigs.isEmpty()) { throw new SamzaException("No jobs to run."); } jobConfigs.forEach(jobConfig -> { LOG.debug("Starting job {} StreamProcessor with config {}", jobConfig.getName(), jobConfig); StreamProcessor processor = createStreamProcessor(jobConfig, appDesc, sp -> new LocalStreamProcessorLifecycleListener(sp, jobConfig), Optional.ofNullable(externalContext)); processors.add(processor); }); numProcessorsToStart.set(processors.size()); // start the StreamProcessors processors.forEach(StreamProcessor::start); } catch (Throwable throwable) { appStatus = ApplicationStatus.unsuccessfulFinish(throwable); shutdownLatch.countDown(); throw new SamzaException(String.format("Failed to start application: %s", new ApplicationConfig(appDesc.getConfig()).getGlobalAppId()), throwable); } }
@Override public void run() { try { List<JobConfig> jobConfigs = planner.prepareJobs(); // create the StreamProcessors if (jobConfigs.isEmpty()) { throw new SamzaException("No jobs to run."); } jobConfigs.forEach(jobConfig -> { LOG.debug("Starting job {} StreamProcessor with config {}", jobConfig.getName(), jobConfig); StreamProcessor processor = createStreamProcessor(jobConfig, appDesc, sp -> new LocalStreamProcessorLifecycleListener(sp, jobConfig)); processors.add(processor); }); numProcessorsToStart.set(processors.size()); // start the StreamProcessors processors.forEach(StreamProcessor::start); } catch (Throwable throwable) { appStatus = ApplicationStatus.unsuccessfulFinish(throwable); shutdownLatch.countDown(); throw new SamzaException(String.format("Failed to start application: %s", new ApplicationConfig(appDesc.getConfig()).getGlobalAppId()), throwable); } }
@Override public void run() { try { List<JobConfig> jobConfigs = planner.prepareJobs(); // create the StreamProcessors if (jobConfigs.isEmpty()) { throw new SamzaException("No jobs to run."); } jobConfigs.forEach(jobConfig -> { LOG.debug("Starting job {} StreamProcessor with config {}", jobConfig.getName(), jobConfig); StreamProcessor processor = createStreamProcessor(jobConfig, appDesc, sp -> new LocalStreamProcessorLifecycleListener(sp, jobConfig)); processors.add(processor); }); numProcessorsToStart.set(processors.size()); // start the StreamProcessors processors.forEach(StreamProcessor::start); } catch (Throwable throwable) { appStatus = ApplicationStatus.unsuccessfulFinish(throwable); shutdownLatch.countDown(); throw new SamzaException(String.format("Failed to start application: %s", new ApplicationConfig(appDesc.getConfig()).getGlobalAppId()), throwable); } }
@Override public void run() { try { List<JobConfig> jobConfigs = planner.prepareJobs(); // create the StreamProcessors if (jobConfigs.isEmpty()) { throw new SamzaException("No jobs to run."); } jobConfigs.forEach(jobConfig -> { LOG.debug("Starting job {} StreamProcessor with config {}", jobConfig.getName(), jobConfig); StreamProcessor processor = createStreamProcessor(jobConfig, appDesc, sp -> new LocalStreamProcessorLifecycleListener(sp, jobConfig)); processors.add(processor); }); numProcessorsToStart.set(processors.size()); // start the StreamProcessors processors.forEach(StreamProcessor::start); } catch (Throwable throwable) { appStatus = ApplicationStatus.unsuccessfulFinish(throwable); shutdownLatch.countDown(); throw new SamzaException(String.format("Failed to start application: %s", new ApplicationConfig(appDesc.getConfig()).getGlobalAppId()), throwable); } }
@Override public void run() { try { List<JobConfig> jobConfigs = planner.prepareJobs(); // create the StreamProcessors if (jobConfigs.isEmpty()) { throw new SamzaException("No jobs to run."); } jobConfigs.forEach(jobConfig -> { LOG.debug("Starting job {} StreamProcessor with config {}", jobConfig.getName(), jobConfig); StreamProcessor processor = createStreamProcessor(jobConfig, appDesc, sp -> new LocalStreamProcessorLifecycleListener(sp, jobConfig)); processors.add(processor); }); numProcessorsToStart.set(processors.size()); // start the StreamProcessors processors.forEach(StreamProcessor::start); } catch (Throwable throwable) { appStatus = ApplicationStatus.unsuccessfulFinish(throwable); shutdownLatch.countDown(); throw new SamzaException(String.format("Failed to start application: %s", new ApplicationConfig(appDesc.getConfig()).getGlobalAppId()), throwable); } }
@Test public void testRunStreamTaskWithoutExternalContext() { final Map<String, String> cfgs = new HashMap<>(); cfgs.put(ApplicationConfig.APP_PROCESSOR_ID_GENERATOR_CLASS, UUIDGenerator.class.getName()); cfgs.put(JobConfig.JOB_NAME(), "test-task-job"); cfgs.put(JobConfig.JOB_ID(), "jobId"); config = new MapConfig(cfgs); mockApp = new LegacyTaskApplication(IdentityStreamTask.class.getName()); prepareTest(); StreamProcessor sp = mock(StreamProcessor.class); ArgumentCaptor<StreamProcessor.StreamProcessorLifecycleListenerFactory> captor = ArgumentCaptor.forClass(StreamProcessor.StreamProcessorLifecycleListenerFactory.class); doAnswer(i -> { ProcessorLifecycleListener listener = captor.getValue().createInstance(sp); listener.afterStart(); listener.afterStop(); return null; }).when(sp).start(); doReturn(sp).when(runner).createStreamProcessor(anyObject(), anyObject(), captor.capture(), eq(Optional.empty())); doReturn(ApplicationStatus.SuccessfulFinish).when(runner).status(); runner.run(); assertEquals(ApplicationStatus.SuccessfulFinish, runner.status()); }
@Test public void testRunFailure() { Map<String, String> cfgs = new HashMap<>(); cfgs.put(ApplicationConfig.PROCESSOR_ID, "0"); config = new MapConfig(cfgs); ProcessorLifecycleListenerFactory mockFactory = (pContext, cfg) -> mock(ProcessorLifecycleListener.class); mockApp = (StreamApplication) appDesc -> { appDesc.withProcessorLifecycleListenerFactory(mockFactory); }; prepareTest(); // return the jobConfigs from the planner doReturn(Collections.singletonList(new JobConfig(new MapConfig(config)))).when(localPlanner).prepareJobs(); StreamProcessor sp = mock(StreamProcessor.class); ArgumentCaptor<StreamProcessor.StreamProcessorLifecycleListenerFactory> captor = ArgumentCaptor.forClass(StreamProcessor.StreamProcessorLifecycleListenerFactory.class); doAnswer(i -> { throw new Exception("test failure"); }).when(sp).start(); ExternalContext externalContext = mock(ExternalContext.class); doReturn(sp).when(runner) .createStreamProcessor(anyObject(), anyObject(), captor.capture(), eq(Optional.of(externalContext))); try { runner.run(externalContext); runner.waitForFinish(); } catch (Throwable th) { assertNotNull(th); } assertEquals(runner.status(), ApplicationStatus.UnsuccessfulFinish); }
@Test public void testRunStreamTask() { final Map<String, String> cfgs = new HashMap<>(); cfgs.put(ApplicationConfig.APP_PROCESSOR_ID_GENERATOR_CLASS, UUIDGenerator.class.getName()); cfgs.put(JobConfig.JOB_NAME(), "test-task-job"); cfgs.put(JobConfig.JOB_ID(), "jobId"); config = new MapConfig(cfgs); mockApp = new LegacyTaskApplication(IdentityStreamTask.class.getName()); prepareTest(); StreamProcessor sp = mock(StreamProcessor.class); ArgumentCaptor<StreamProcessor.StreamProcessorLifecycleListenerFactory> captor = ArgumentCaptor.forClass(StreamProcessor.StreamProcessorLifecycleListenerFactory.class); doAnswer(i -> { ProcessorLifecycleListener listener = captor.getValue().createInstance(sp); listener.afterStart(); listener.afterStop(); return null; }).when(sp).start(); ExternalContext externalContext = mock(ExternalContext.class); doReturn(sp).when(runner) .createStreamProcessor(anyObject(), anyObject(), captor.capture(), eq(Optional.of(externalContext))); doReturn(ApplicationStatus.SuccessfulFinish).when(runner).status(); runner.run(externalContext); assertEquals(ApplicationStatus.SuccessfulFinish, runner.status()); }
@Test public void testRunComplete() { Map<String, String> cfgs = new HashMap<>(); cfgs.put(ApplicationConfig.APP_PROCESSOR_ID_GENERATOR_CLASS, UUIDGenerator.class.getName()); config = new MapConfig(cfgs); ProcessorLifecycleListenerFactory mockFactory = (pContext, cfg) -> mock(ProcessorLifecycleListener.class); mockApp = (StreamApplication) appDesc -> { appDesc.withProcessorLifecycleListenerFactory(mockFactory); }; prepareTest(); // return the jobConfigs from the planner doReturn(Collections.singletonList(new JobConfig(new MapConfig(config)))).when(localPlanner).prepareJobs(); StreamProcessor sp = mock(StreamProcessor.class); ArgumentCaptor<StreamProcessor.StreamProcessorLifecycleListenerFactory> captor = ArgumentCaptor.forClass(StreamProcessor.StreamProcessorLifecycleListenerFactory.class); doAnswer(i -> { ProcessorLifecycleListener listener = captor.getValue().createInstance(sp); listener.afterStart(); listener.afterStop(); return null; }).when(sp).start(); ExternalContext externalContext = mock(ExternalContext.class); doReturn(sp).when(runner) .createStreamProcessor(anyObject(), anyObject(), captor.capture(), eq(Optional.of(externalContext))); runner.run(externalContext); runner.waitForFinish(); assertEquals(runner.status(), ApplicationStatus.SuccessfulFinish); }