@Override public void waitForFinish() { this.waitForFinish(Duration.ofSeconds(0)); }
@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); }
@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); } }
@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 testWaitForFinishReturnsBeforeTimeout() { long timeoutInMs = 1000; runner.getShutdownLatch().countDown(); boolean finished = runner.waitForFinish(Duration.ofMillis(timeoutInMs)); assertTrue("Application did not finish before the timeout.", finished); }
@VisibleForTesting StreamProcessor createStreamProcessor(Config config, ApplicationDescriptorImpl<? extends ApplicationDescriptor> appDesc, StreamProcessor.StreamProcessorLifecycleListenerFactory listenerFactory, Optional<ExternalContext> externalContextOptional) { TaskFactory taskFactory = TaskFactoryUtil.getTaskFactory(appDesc); Map<String, MetricsReporter> reporters = new HashMap<>(); String processorId = createProcessorId(new ApplicationConfig(config)); appDesc.getMetricsReporterFactories().forEach((name, factory) -> reporters.put(name, factory.getMetricsReporter(name, processorId, config))); return new StreamProcessor(processorId, config, reporters, taskFactory, appDesc.getApplicationContainerContextFactory(), appDesc.getApplicationTaskContextFactory(), externalContextOptional, listenerFactory, null); }
private void prepareTest() { ApplicationDescriptorImpl<? extends ApplicationDescriptor> appDesc = ApplicationDescriptorUtil.getAppDescriptor(mockApp, config); localPlanner = spy(new LocalJobPlanner(appDesc)); runner = spy(new LocalApplicationRunner(appDesc, localPlanner)); }
@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); }
@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 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(expected = ConfigException.class) public void testCreateProcessorIdShouldThrowExceptionWhenProcessorIdAndGeneratorAreNotDefined() { ApplicationConfig mockConfig = Mockito.mock(ApplicationConfig.class); Mockito.when(mockConfig.getProcessorId()).thenReturn(null); LocalApplicationRunner.createProcessorId(mockConfig); }
@Override public void waitForFinish() { this.waitForFinish(Duration.ofSeconds(0)); }
@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 testCreateProcessorIdShouldInvokeProcessorIdGeneratorDefinedInConfiguration() { String processorId = "testProcessorId"; MapConfig configMap = new MapConfig(ImmutableMap.of(ApplicationConfig.APP_PROCESSOR_ID_GENERATOR_CLASS, MockProcessorIdGenerator.class.getCanonicalName())); String actualProcessorId = LocalApplicationRunner.createProcessorId(new ApplicationConfig(configMap)); assertEquals(processorId, actualProcessorId); }
@Override public void waitForFinish() { this.waitForFinish(Duration.ofSeconds(0)); }
@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 testCreateProcessorIdShouldReturnProcessorIdDefinedInConfiguration() { String processorId = "testProcessorId"; MapConfig configMap = new MapConfig(ImmutableMap.of(ApplicationConfig.PROCESSOR_ID, processorId)); String actualProcessorId = LocalApplicationRunner.createProcessorId(new ApplicationConfig(configMap)); assertEquals(processorId, actualProcessorId); }
@Override public void waitForFinish() { this.waitForFinish(Duration.ofSeconds(0)); }
@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 waitForFinish() { this.waitForFinish(Duration.ofSeconds(0)); }