Search in sources :

Example 1 with RemoteJobPlanner

use of org.apache.samza.execution.RemoteJobPlanner in project samza by apache.

the class TestJobCoordinatorLaunchUtil method testRunJobCoordinator.

@Test
public void testRunJobCoordinator() throws Exception {
    String jobCoordinatorFactoryClass = "org.apache.samza.custom.MyJobCoordinatorFactory";
    Config originalConfig = buildOriginalConfig(ImmutableMap.of(JobCoordinatorConfig.JOB_COORDINATOR_FACTORY, jobCoordinatorFactoryClass));
    JobConfig fullConfig = new JobConfig(new MapConfig(originalConfig, Collections.singletonMap("isAfterPlanning", "true")));
    Config autoSizingConfig = new MapConfig(Collections.singletonMap(JobConfig.JOB_AUTOSIZING_CONTAINER_COUNT, "10"));
    Config finalConfig = new MapConfig(autoSizingConfig, fullConfig);
    RemoteJobPlanner remoteJobPlanner = mock(RemoteJobPlanner.class);
    CoordinatorStreamStore coordinatorStreamStore = mock(CoordinatorStreamStore.class);
    JobCoordinatorFactory jobCoordinatorFactory = mock(JobCoordinatorFactory.class);
    JobCoordinator jobCoordinator = mock(JobCoordinator.class);
    PowerMockito.mockStatic(CoordinatorStreamUtil.class);
    PowerMockito.doNothing().when(CoordinatorStreamUtil.class, "createCoordinatorStream", any());
    PowerMockito.doReturn(new MapConfig()).when(CoordinatorStreamUtil.class, "buildCoordinatorStreamConfig", any());
    PowerMockito.doReturn(autoSizingConfig).when(CoordinatorStreamUtil.class, "readLaunchConfigFromCoordinatorStream", any(), any());
    PowerMockito.whenNew(CoordinatorStreamStore.class).withAnyArguments().thenReturn(coordinatorStreamStore);
    PowerMockito.whenNew(RemoteJobPlanner.class).withAnyArguments().thenReturn(remoteJobPlanner);
    when(remoteJobPlanner.prepareJobs()).thenReturn(Collections.singletonList(fullConfig));
    PowerMockito.mockStatic(ReflectionUtil.class);
    PowerMockito.doReturn(jobCoordinatorFactory).when(ReflectionUtil.class, "getObj", jobCoordinatorFactoryClass, JobCoordinatorFactory.class);
    when(jobCoordinatorFactory.getJobCoordinator(eq("samza-job-coordinator"), eq(finalConfig), any(), eq(coordinatorStreamStore))).thenReturn(jobCoordinator);
    // use a latch to keep track of when shutdown hook was added to know when we should start verifications
    CountDownLatch addShutdownHookLatch = new CountDownLatch(1);
    PowerMockito.spy(JobCoordinatorLaunchUtil.class);
    PowerMockito.doAnswer(invocation -> {
        addShutdownHookLatch.countDown();
        return null;
    }).when(JobCoordinatorLaunchUtil.class, "addShutdownHook", any());
    MetricsReporter metricsReporter = mock(MetricsReporter.class);
    Map<String, MetricsReporter> metricsReporterMap = ImmutableMap.of("reporter", metricsReporter);
    PowerMockito.mockStatic(MetricsReporterLoader.class);
    PowerMockito.doReturn(metricsReporterMap).when(MetricsReporterLoader.class, "getMetricsReporters", new MetricsConfig(finalConfig), "JobCoordinator");
    NoProcessorJobCoordinatorListener jobCoordinatorListener = mock(NoProcessorJobCoordinatorListener.class);
    PowerMockito.whenNew(NoProcessorJobCoordinatorListener.class).withAnyArguments().thenReturn(jobCoordinatorListener);
    Thread runThread = new Thread(() -> JobCoordinatorLaunchUtil.run(new MockStreamApplication(), originalConfig));
    runThread.start();
    // last thing before waiting for shutdown is to add shutdown hook, so do verifications once hook is added
    addShutdownHookLatch.await();
    verifyStatic();
    CoordinatorStreamUtil.createCoordinatorStream(fullConfig);
    verifyStatic();
    CoordinatorStreamUtil.writeConfigToCoordinatorStream(finalConfig, true);
    verifyStatic();
    JobCoordinatorLaunchUtil.addShutdownHook(jobCoordinator);
    InOrder inOrder = Mockito.inOrder(metricsReporter, jobCoordinator);
    inOrder.verify(metricsReporter).register(eq("JobCoordinator"), any());
    inOrder.verify(metricsReporter).start();
    ArgumentCaptor<CountDownLatch> countDownLatchArgumentCaptor = ArgumentCaptor.forClass(CountDownLatch.class);
    verifyNew(NoProcessorJobCoordinatorListener.class).withArguments(countDownLatchArgumentCaptor.capture());
    inOrder.verify(jobCoordinator).setListener(jobCoordinatorListener);
    inOrder.verify(jobCoordinator).start();
    // wait some time and then make sure the run thread is still alive
    Thread.sleep(Duration.ofMillis(500).toMillis());
    assertTrue(runThread.isAlive());
    // trigger the count down latch so that the run thread can exit
    countDownLatchArgumentCaptor.getValue().countDown();
    runThread.join(Duration.ofSeconds(10).toMillis());
    assertFalse(runThread.isAlive());
    verify(metricsReporter).stop();
}
Also used : CoordinatorStreamStore(org.apache.samza.coordinator.metadatastore.CoordinatorStreamStore) MetricsConfig(org.apache.samza.config.MetricsConfig) JobCoordinator(org.apache.samza.coordinator.JobCoordinator) RunWith(org.junit.runner.RunWith) JobConfig(org.apache.samza.config.JobConfig) HashMap(java.util.HashMap) PowerMockito.verifyNew(org.powermock.api.mockito.PowerMockito.verifyNew) NoProcessorJobCoordinatorListener(org.apache.samza.coordinator.NoProcessorJobCoordinatorListener) ArgumentCaptor(org.mockito.ArgumentCaptor) Matchers.eq(org.mockito.Matchers.eq) Duration(java.time.Duration) Map(java.util.Map) MetricsReporter(org.apache.samza.metrics.MetricsReporter) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PowerMockRunner(org.powermock.modules.junit4.PowerMockRunner) MockStreamApplication(org.apache.samza.application.MockStreamApplication) MapConfig(org.apache.samza.config.MapConfig) PowerMockito(org.powermock.api.mockito.PowerMockito) PowerMockito.verifyStatic(org.powermock.api.mockito.PowerMockito.verifyStatic) InOrder(org.mockito.InOrder) ImmutableMap(com.google.common.collect.ImmutableMap) MetricsReporterLoader(org.apache.samza.util.MetricsReporterLoader) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Matchers.any(org.mockito.Matchers.any) CountDownLatch(java.util.concurrent.CountDownLatch) Mockito(org.mockito.Mockito) PowerMockito.mock(org.powermock.api.mockito.PowerMockito.mock) RemoteJobPlanner(org.apache.samza.execution.RemoteJobPlanner) ReflectionUtil(org.apache.samza.util.ReflectionUtil) Assert.assertFalse(org.junit.Assert.assertFalse) JobCoordinatorConfig(org.apache.samza.config.JobCoordinatorConfig) Config(org.apache.samza.config.Config) CoordinatorStreamUtil(org.apache.samza.util.CoordinatorStreamUtil) Collections(java.util.Collections) JobCoordinatorFactory(org.apache.samza.coordinator.JobCoordinatorFactory) MetricsRegistryMap(org.apache.samza.metrics.MetricsRegistryMap) InOrder(org.mockito.InOrder) MockStreamApplication(org.apache.samza.application.MockStreamApplication) MetricsConfig(org.apache.samza.config.MetricsConfig) JobConfig(org.apache.samza.config.JobConfig) MapConfig(org.apache.samza.config.MapConfig) JobCoordinatorConfig(org.apache.samza.config.JobCoordinatorConfig) Config(org.apache.samza.config.Config) CountDownLatch(java.util.concurrent.CountDownLatch) JobConfig(org.apache.samza.config.JobConfig) MetricsConfig(org.apache.samza.config.MetricsConfig) CoordinatorStreamStore(org.apache.samza.coordinator.metadatastore.CoordinatorStreamStore) NoProcessorJobCoordinatorListener(org.apache.samza.coordinator.NoProcessorJobCoordinatorListener) MetricsReporter(org.apache.samza.metrics.MetricsReporter) JobCoordinatorFactory(org.apache.samza.coordinator.JobCoordinatorFactory) JobCoordinator(org.apache.samza.coordinator.JobCoordinator) MapConfig(org.apache.samza.config.MapConfig) RemoteJobPlanner(org.apache.samza.execution.RemoteJobPlanner) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 2 with RemoteJobPlanner

use of org.apache.samza.execution.RemoteJobPlanner in project samza by apache.

the class RemoteApplicationRunner method run.

@Override
public void run(ExternalContext externalContext) {
    if (new JobConfig(config).getConfigLoaderFactory().isPresent()) {
        JobRunner runner = new JobRunner(JobPlanner.generateSingleJobConfig(config));
        runner.submit();
        return;
    }
    // TODO SAMZA-2432: Clean this up once SAMZA-2405 is completed when legacy flow is removed.
    try {
        JobPlanner planner = new RemoteJobPlanner(ApplicationDescriptorUtil.getAppDescriptor(app, config));
        List<JobConfig> jobConfigs = planner.prepareJobs();
        if (jobConfigs.isEmpty()) {
            throw new SamzaException("No jobs to run.");
        }
        // 3. submit jobs for remote execution
        jobConfigs.forEach(jobConfig -> {
            LOG.info("Starting job {} with config {}", jobConfig.getName(), jobConfig);
            JobRunner runner = new JobRunner(jobConfig);
            runner.run(true);
        });
    } catch (Throwable t) {
        throw new SamzaException("Failed to run application", t);
    }
}
Also used : JobRunner(org.apache.samza.job.JobRunner) JobPlanner(org.apache.samza.execution.JobPlanner) RemoteJobPlanner(org.apache.samza.execution.RemoteJobPlanner) SamzaException(org.apache.samza.SamzaException) JobConfig(org.apache.samza.config.JobConfig) RemoteJobPlanner(org.apache.samza.execution.RemoteJobPlanner)

Example 3 with RemoteJobPlanner

use of org.apache.samza.execution.RemoteJobPlanner in project samza by apache.

the class TestJobCoordinatorLaunchUtil method testRunClusterBasedJobCoordinator.

@Test
public void testRunClusterBasedJobCoordinator() throws Exception {
    Config originalConfig = buildOriginalConfig(ImmutableMap.of());
    JobConfig fullConfig = new JobConfig(new MapConfig(originalConfig, Collections.singletonMap("isAfterPlanning", "true")));
    Config autoSizingConfig = new MapConfig(Collections.singletonMap(JobConfig.JOB_AUTOSIZING_CONTAINER_COUNT, "10"));
    Config finalConfig = new MapConfig(autoSizingConfig, fullConfig);
    RemoteJobPlanner mockJobPlanner = mock(RemoteJobPlanner.class);
    CoordinatorStreamStore mockCoordinatorStreamStore = mock(CoordinatorStreamStore.class);
    ClusterBasedJobCoordinator mockJC = mock(ClusterBasedJobCoordinator.class);
    PowerMockito.mockStatic(CoordinatorStreamUtil.class);
    PowerMockito.doNothing().when(CoordinatorStreamUtil.class, "createCoordinatorStream", any());
    PowerMockito.doReturn(new MapConfig()).when(CoordinatorStreamUtil.class, "buildCoordinatorStreamConfig", any());
    PowerMockito.doReturn(autoSizingConfig).when(CoordinatorStreamUtil.class, "readLaunchConfigFromCoordinatorStream", any(), any());
    PowerMockito.whenNew(CoordinatorStreamStore.class).withAnyArguments().thenReturn(mockCoordinatorStreamStore);
    PowerMockito.whenNew(RemoteJobPlanner.class).withAnyArguments().thenReturn(mockJobPlanner);
    PowerMockito.whenNew(ClusterBasedJobCoordinator.class).withAnyArguments().thenReturn(mockJC);
    when(mockJobPlanner.prepareJobs()).thenReturn(Collections.singletonList(fullConfig));
    JobCoordinatorLaunchUtil.run(new MockStreamApplication(), originalConfig);
    verifyNew(ClusterBasedJobCoordinator.class).withArguments(any(MetricsRegistryMap.class), eq(mockCoordinatorStreamStore), eq(finalConfig));
    verify(mockJC, times(1)).run();
    verifyStatic(times(1));
    CoordinatorStreamUtil.createCoordinatorStream(fullConfig);
    verifyStatic(times(1));
    CoordinatorStreamUtil.writeConfigToCoordinatorStream(finalConfig, true);
}
Also used : CoordinatorStreamStore(org.apache.samza.coordinator.metadatastore.CoordinatorStreamStore) MockStreamApplication(org.apache.samza.application.MockStreamApplication) MetricsConfig(org.apache.samza.config.MetricsConfig) JobConfig(org.apache.samza.config.JobConfig) MapConfig(org.apache.samza.config.MapConfig) JobCoordinatorConfig(org.apache.samza.config.JobCoordinatorConfig) Config(org.apache.samza.config.Config) MapConfig(org.apache.samza.config.MapConfig) MetricsRegistryMap(org.apache.samza.metrics.MetricsRegistryMap) JobConfig(org.apache.samza.config.JobConfig) RemoteJobPlanner(org.apache.samza.execution.RemoteJobPlanner) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 4 with RemoteJobPlanner

use of org.apache.samza.execution.RemoteJobPlanner in project samza by apache.

the class JobCoordinatorLaunchUtil method run.

/**
 * Run {@link ClusterBasedJobCoordinator} with full job config.
 *
 * @param app SamzaApplication to run.
 * @param config full job config.
 */
@SuppressWarnings("rawtypes")
public static void run(SamzaApplication app, Config config) {
    // Execute planning
    ApplicationDescriptorImpl<? extends ApplicationDescriptor> appDesc = ApplicationDescriptorUtil.getAppDescriptor(app, config);
    RemoteJobPlanner planner = new RemoteJobPlanner(appDesc);
    List<JobConfig> jobConfigs = planner.prepareJobs();
    if (jobConfigs.size() != 1) {
        throw new SamzaException("Only support single remote job is supported.");
    }
    Config fullConfig = jobConfigs.get(0);
    // Create coordinator stream if does not exist before fetching launch config from it.
    CoordinatorStreamUtil.createCoordinatorStream(fullConfig);
    MetricsRegistryMap metrics = new MetricsRegistryMap();
    MetadataStore metadataStore = new CoordinatorStreamStore(CoordinatorStreamUtil.buildCoordinatorStreamConfig(fullConfig), metrics);
    // MetadataStore will be closed in ClusterBasedJobCoordinator#onShutDown
    // initialization of MetadataStore can be moved to ClusterBasedJobCoordinator after we clean up
    // ClusterBasedJobCoordinator#createFromMetadataStore
    metadataStore.init();
    // Reads extra launch config from metadata store.
    Config launchConfig = CoordinatorStreamUtil.readLaunchConfigFromCoordinatorStream(fullConfig, metadataStore);
    Config finalConfig = new MapConfig(launchConfig, fullConfig);
    // This needs to be consistent with RemoteApplicationRunner#run where JobRunner#submit to be called instead of JobRunner#run
    CoordinatorStreamUtil.writeConfigToCoordinatorStream(finalConfig, true);
    DiagnosticsUtil.createDiagnosticsStream(finalConfig);
    Optional<String> jobCoordinatorFactoryClassName = new JobCoordinatorConfig(config).getOptionalJobCoordinatorFactoryClassName();
    if (jobCoordinatorFactoryClassName.isPresent()) {
        runJobCoordinator(jobCoordinatorFactoryClassName.get(), metrics, metadataStore, finalConfig);
    } else {
        ClusterBasedJobCoordinator jc = new ClusterBasedJobCoordinator(metrics, metadataStore, finalConfig);
        jc.run();
    }
}
Also used : MetricsConfig(org.apache.samza.config.MetricsConfig) JobConfig(org.apache.samza.config.JobConfig) MapConfig(org.apache.samza.config.MapConfig) JobCoordinatorConfig(org.apache.samza.config.JobCoordinatorConfig) Config(org.apache.samza.config.Config) SamzaException(org.apache.samza.SamzaException) JobConfig(org.apache.samza.config.JobConfig) MetadataStore(org.apache.samza.metadatastore.MetadataStore) CoordinatorStreamStore(org.apache.samza.coordinator.metadatastore.CoordinatorStreamStore) JobCoordinatorConfig(org.apache.samza.config.JobCoordinatorConfig) MapConfig(org.apache.samza.config.MapConfig) MetricsRegistryMap(org.apache.samza.metrics.MetricsRegistryMap) RemoteJobPlanner(org.apache.samza.execution.RemoteJobPlanner)

Aggregations

JobConfig (org.apache.samza.config.JobConfig)4 RemoteJobPlanner (org.apache.samza.execution.RemoteJobPlanner)4 Config (org.apache.samza.config.Config)3 JobCoordinatorConfig (org.apache.samza.config.JobCoordinatorConfig)3 MapConfig (org.apache.samza.config.MapConfig)3 MetricsConfig (org.apache.samza.config.MetricsConfig)3 CoordinatorStreamStore (org.apache.samza.coordinator.metadatastore.CoordinatorStreamStore)3 MetricsRegistryMap (org.apache.samza.metrics.MetricsRegistryMap)3 SamzaException (org.apache.samza.SamzaException)2 MockStreamApplication (org.apache.samza.application.MockStreamApplication)2 Test (org.junit.Test)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 Duration (java.time.Duration)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 JobCoordinator (org.apache.samza.coordinator.JobCoordinator)1 JobCoordinatorFactory (org.apache.samza.coordinator.JobCoordinatorFactory)1 NoProcessorJobCoordinatorListener (org.apache.samza.coordinator.NoProcessorJobCoordinatorListener)1