Search in sources :

Example 6 with ExternalContext

use of org.apache.samza.context.ExternalContext in project samza by apache.

the class SamzaSqlApplication method describe.

@Override
public void describe(StreamApplicationDescriptor appDescriptor) {
    try {
        // TODO: Introduce an API to return a dsl string containing one or more sql statements.
        List<String> dslStmts = SamzaSqlDslConverter.fetchSqlFromConfig(appDescriptor.getConfig());
        Map<Integer, TranslatorContext> translatorContextMap = new HashMap<>();
        // 1. Get Calcite plan
        List<String> inputSystemStreams = new LinkedList<>();
        List<String> outputSystemStreams = new LinkedList<>();
        Collection<RelRoot> relRoots = SamzaSqlApplicationConfig.populateSystemStreamsAndGetRelRoots(dslStmts, appDescriptor.getConfig(), inputSystemStreams, outputSystemStreams);
        // 2. Populate configs
        SamzaSqlApplicationConfig sqlConfig = new SamzaSqlApplicationConfig(appDescriptor.getConfig(), inputSystemStreams, outputSystemStreams);
        // 3. Translate Calcite plan to Samza stream operators
        QueryTranslator queryTranslator = new QueryTranslator(appDescriptor, sqlConfig);
        SamzaSqlExecutionContext executionContext = new SamzaSqlExecutionContext(sqlConfig);
        // QueryId implies the index of the query in multiple query statements scenario. It should always start with 0.
        int queryId = 0;
        for (RelRoot relRoot : relRoots) {
            LOG.info("Translating relRoot {} to samza stream graph with queryId {}", relRoot, queryId);
            TranslatorContext translatorContext = new TranslatorContext(appDescriptor, relRoot, executionContext);
            translatorContextMap.put(queryId, translatorContext);
            queryTranslator.translate(relRoot, sqlConfig.getOutputSystemStreams().get(queryId), translatorContext, queryId);
            queryId++;
        }
        // 4. Set all translator contexts
        /*
       * TODO When serialization of ApplicationDescriptor is actually needed, then something will need to be updated here,
       * since translatorContext is not Serializable. Currently, a new ApplicationDescriptor instance is created in each
       * container, so it does not need to be serialized. Therefore, the translatorContext is recreated in each container
       * and does not need to be serialized.
       */
        appDescriptor.withApplicationTaskContextFactory(new ApplicationTaskContextFactory<SamzaSqlApplicationContext>() {

            @Override
            public SamzaSqlApplicationContext create(ExternalContext externalContext, JobContext jobContext, ContainerContext containerContext, TaskContext taskContext, ApplicationContainerContext applicationContainerContext) {
                return new SamzaSqlApplicationContext(translatorContextMap);
            }
        });
    } catch (RuntimeException e) {
        LOG.error("SamzaSqlApplication threw exception.", e);
        throw e;
    }
}
Also used : TaskContext(org.apache.samza.context.TaskContext) HashMap(java.util.HashMap) RelRoot(org.apache.calcite.rel.RelRoot) LinkedList(java.util.LinkedList) ContainerContext(org.apache.samza.context.ContainerContext) ApplicationContainerContext(org.apache.samza.context.ApplicationContainerContext) ApplicationContainerContext(org.apache.samza.context.ApplicationContainerContext) TranslatorContext(org.apache.samza.sql.translator.TranslatorContext) ExternalContext(org.apache.samza.context.ExternalContext) SamzaSqlExecutionContext(org.apache.samza.sql.data.SamzaSqlExecutionContext) JobContext(org.apache.samza.context.JobContext) QueryTranslator(org.apache.samza.sql.translator.QueryTranslator)

Example 7 with ExternalContext

use of org.apache.samza.context.ExternalContext in project samza by apache.

the class TestLocalApplicationRunner method testRunCompleteWithouCoordinatorStreamStore.

@Test
public void testRunCompleteWithouCoordinatorStreamStore() throws Exception {
    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)), eq(null));
    doReturn(null).when(runner).createCoordinatorStreamStore(any(Config.class));
    runner.run(externalContext);
    runner.waitForFinish();
    assertEquals(runner.status(), ApplicationStatus.SuccessfulFinish);
}
Also used : LegacyTaskApplication(org.apache.samza.application.LegacyTaskApplication) StreamProcessor(org.apache.samza.processor.StreamProcessor) ApplicationDescriptorUtil(org.apache.samza.application.descriptors.ApplicationDescriptorUtil) Matchers.eq(org.mockito.Matchers.eq) Duration(java.time.Duration) Map(java.util.Map) Mockito.doAnswer(org.mockito.Mockito.doAnswer) SamzaApplication(org.apache.samza.application.SamzaApplication) InMemoryMetadataStoreFactory(org.apache.samza.metadatastore.InMemoryMetadataStoreFactory) ExternalContext(org.apache.samza.context.ExternalContext) InMemoryMetadataStore(org.apache.samza.metadatastore.InMemoryMetadataStore) MapConfig(org.apache.samza.config.MapConfig) Mockito.doReturn(org.mockito.Mockito.doReturn) ClusterMembership(org.apache.samza.coordinator.ClusterMembership) ImmutableMap(com.google.common.collect.ImmutableMap) CoordinationUtils(org.apache.samza.coordinator.CoordinationUtils) Mockito.atLeastOnce(org.mockito.Mockito.atLeastOnce) MetricsRegistry(org.apache.samza.metrics.MetricsRegistry) CoordinatorStreamMetadataStoreFactory(org.apache.samza.coordinator.metadatastore.CoordinatorStreamMetadataStoreFactory) PassthroughJobCoordinatorFactory(org.apache.samza.standalone.PassthroughJobCoordinatorFactory) DistributedLock(org.apache.samza.coordinator.DistributedLock) Matchers.any(org.mockito.Matchers.any) CoordinationConstants(org.apache.samza.coordinator.CoordinationConstants) MetadataStoreFactory(org.apache.samza.metadatastore.MetadataStoreFactory) IdentityStreamTask(org.apache.samza.task.IdentityStreamTask) Assert.assertFalse(org.junit.Assert.assertFalse) ZkMetadataStoreFactory(org.apache.samza.zk.ZkMetadataStoreFactory) Optional(java.util.Optional) JobCoordinatorConfig(org.apache.samza.config.JobCoordinatorConfig) Config(org.apache.samza.config.Config) StreamApplication(org.apache.samza.application.StreamApplication) ApplicationStatus(org.apache.samza.job.ApplicationStatus) SystemAdmins(org.apache.samza.system.SystemAdmins) Mockito.mock(org.mockito.Mockito.mock) ConfigException(org.apache.samza.config.ConfigException) CoordinatorStreamStore(org.apache.samza.coordinator.metadatastore.CoordinatorStreamStore) RunWith(org.junit.runner.RunWith) JobConfig(org.apache.samza.config.JobConfig) HashMap(java.util.HashMap) Mockito.spy(org.mockito.Mockito.spy) Matchers.anyString(org.mockito.Matchers.anyString) Answer(org.mockito.stubbing.Answer) MetadataStore(org.apache.samza.metadatastore.MetadataStore) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ArgumentCaptor(org.mockito.ArgumentCaptor) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Matchers.anyObject(org.mockito.Matchers.anyObject) ApplicationConfig(org.apache.samza.config.ApplicationConfig) ApplicationDescriptor(org.apache.samza.application.descriptors.ApplicationDescriptor) PowerMockRunner(org.powermock.modules.junit4.PowerMockRunner) PowerMockito(org.powermock.api.mockito.PowerMockito) Before(org.junit.Before) Assert.assertNotNull(org.junit.Assert.assertNotNull) ZkMetadataStore(org.apache.samza.zk.ZkMetadataStore) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Mockito(org.mockito.Mockito) Mockito.never(org.mockito.Mockito.never) Assert.assertNull(org.junit.Assert.assertNull) ApplicationDescriptorImpl(org.apache.samza.application.descriptors.ApplicationDescriptorImpl) LocalJobPlanner(org.apache.samza.execution.LocalJobPlanner) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) StreamProcessor(org.apache.samza.processor.StreamProcessor) HashMap(java.util.HashMap) MapConfig(org.apache.samza.config.MapConfig) JobCoordinatorConfig(org.apache.samza.config.JobCoordinatorConfig) Config(org.apache.samza.config.Config) JobConfig(org.apache.samza.config.JobConfig) ApplicationConfig(org.apache.samza.config.ApplicationConfig) Matchers.anyString(org.mockito.Matchers.anyString) JobConfig(org.apache.samza.config.JobConfig) ExternalContext(org.apache.samza.context.ExternalContext) MapConfig(org.apache.samza.config.MapConfig) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 8 with ExternalContext

use of org.apache.samza.context.ExternalContext in project samza by apache.

the class TestLocalApplicationRunner method testRunComplete.

@Test
public void testRunComplete() throws Exception {
    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);
    CoordinatorStreamStore coordinatorStreamStore = mock(CoordinatorStreamStore.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)), any(CoordinatorStreamStore.class));
    doReturn(coordinatorStreamStore).when(runner).createCoordinatorStreamStore(any(Config.class));
    runner.run(externalContext);
    runner.waitForFinish();
    verify(coordinatorStreamStore).init();
    verify(coordinatorStreamStore).close();
    assertEquals(runner.status(), ApplicationStatus.SuccessfulFinish);
}
Also used : LegacyTaskApplication(org.apache.samza.application.LegacyTaskApplication) StreamProcessor(org.apache.samza.processor.StreamProcessor) ApplicationDescriptorUtil(org.apache.samza.application.descriptors.ApplicationDescriptorUtil) Matchers.eq(org.mockito.Matchers.eq) Duration(java.time.Duration) Map(java.util.Map) Mockito.doAnswer(org.mockito.Mockito.doAnswer) SamzaApplication(org.apache.samza.application.SamzaApplication) InMemoryMetadataStoreFactory(org.apache.samza.metadatastore.InMemoryMetadataStoreFactory) ExternalContext(org.apache.samza.context.ExternalContext) InMemoryMetadataStore(org.apache.samza.metadatastore.InMemoryMetadataStore) MapConfig(org.apache.samza.config.MapConfig) Mockito.doReturn(org.mockito.Mockito.doReturn) ClusterMembership(org.apache.samza.coordinator.ClusterMembership) ImmutableMap(com.google.common.collect.ImmutableMap) CoordinationUtils(org.apache.samza.coordinator.CoordinationUtils) Mockito.atLeastOnce(org.mockito.Mockito.atLeastOnce) MetricsRegistry(org.apache.samza.metrics.MetricsRegistry) CoordinatorStreamMetadataStoreFactory(org.apache.samza.coordinator.metadatastore.CoordinatorStreamMetadataStoreFactory) PassthroughJobCoordinatorFactory(org.apache.samza.standalone.PassthroughJobCoordinatorFactory) DistributedLock(org.apache.samza.coordinator.DistributedLock) Matchers.any(org.mockito.Matchers.any) CoordinationConstants(org.apache.samza.coordinator.CoordinationConstants) MetadataStoreFactory(org.apache.samza.metadatastore.MetadataStoreFactory) IdentityStreamTask(org.apache.samza.task.IdentityStreamTask) Assert.assertFalse(org.junit.Assert.assertFalse) ZkMetadataStoreFactory(org.apache.samza.zk.ZkMetadataStoreFactory) Optional(java.util.Optional) JobCoordinatorConfig(org.apache.samza.config.JobCoordinatorConfig) Config(org.apache.samza.config.Config) StreamApplication(org.apache.samza.application.StreamApplication) ApplicationStatus(org.apache.samza.job.ApplicationStatus) SystemAdmins(org.apache.samza.system.SystemAdmins) Mockito.mock(org.mockito.Mockito.mock) ConfigException(org.apache.samza.config.ConfigException) CoordinatorStreamStore(org.apache.samza.coordinator.metadatastore.CoordinatorStreamStore) RunWith(org.junit.runner.RunWith) JobConfig(org.apache.samza.config.JobConfig) HashMap(java.util.HashMap) Mockito.spy(org.mockito.Mockito.spy) Matchers.anyString(org.mockito.Matchers.anyString) Answer(org.mockito.stubbing.Answer) MetadataStore(org.apache.samza.metadatastore.MetadataStore) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ArgumentCaptor(org.mockito.ArgumentCaptor) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Matchers.anyObject(org.mockito.Matchers.anyObject) ApplicationConfig(org.apache.samza.config.ApplicationConfig) ApplicationDescriptor(org.apache.samza.application.descriptors.ApplicationDescriptor) PowerMockRunner(org.powermock.modules.junit4.PowerMockRunner) PowerMockito(org.powermock.api.mockito.PowerMockito) Before(org.junit.Before) Assert.assertNotNull(org.junit.Assert.assertNotNull) ZkMetadataStore(org.apache.samza.zk.ZkMetadataStore) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Mockito(org.mockito.Mockito) Mockito.never(org.mockito.Mockito.never) Assert.assertNull(org.junit.Assert.assertNull) ApplicationDescriptorImpl(org.apache.samza.application.descriptors.ApplicationDescriptorImpl) LocalJobPlanner(org.apache.samza.execution.LocalJobPlanner) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) StreamProcessor(org.apache.samza.processor.StreamProcessor) HashMap(java.util.HashMap) MapConfig(org.apache.samza.config.MapConfig) JobCoordinatorConfig(org.apache.samza.config.JobCoordinatorConfig) Config(org.apache.samza.config.Config) JobConfig(org.apache.samza.config.JobConfig) ApplicationConfig(org.apache.samza.config.ApplicationConfig) Matchers.anyString(org.mockito.Matchers.anyString) JobConfig(org.apache.samza.config.JobConfig) CoordinatorStreamStore(org.apache.samza.coordinator.metadatastore.CoordinatorStreamStore) ExternalContext(org.apache.samza.context.ExternalContext) MapConfig(org.apache.samza.config.MapConfig) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 9 with ExternalContext

use of org.apache.samza.context.ExternalContext in project beam by apache.

the class SamzaRunner method runSamzaApp.

private ApplicationRunner runSamzaApp(StreamApplication app, Config config) {
    final ApplicationRunner runner = ApplicationRunners.getApplicationRunner(app, config);
    ExternalContext externalContext = null;
    if (listener != null) {
        externalContext = listener.onStart();
    }
    runner.run(externalContext);
    if (listener != null && options.getSamzaExecutionEnvironment() == SamzaExecutionEnvironment.YARN) {
        listener.onSubmit();
    }
    return runner;
}
Also used : ApplicationRunner(org.apache.samza.runtime.ApplicationRunner) ExternalContext(org.apache.samza.context.ExternalContext)

Aggregations

ExternalContext (org.apache.samza.context.ExternalContext)9 HashMap (java.util.HashMap)8 LegacyTaskApplication (org.apache.samza.application.LegacyTaskApplication)6 ApplicationConfig (org.apache.samza.config.ApplicationConfig)6 Config (org.apache.samza.config.Config)6 JobConfig (org.apache.samza.config.JobConfig)6 JobCoordinatorConfig (org.apache.samza.config.JobCoordinatorConfig)6 MapConfig (org.apache.samza.config.MapConfig)6 CoordinatorStreamStore (org.apache.samza.coordinator.metadatastore.CoordinatorStreamStore)6 StreamProcessor (org.apache.samza.processor.StreamProcessor)6 ImmutableMap (com.google.common.collect.ImmutableMap)5 Duration (java.time.Duration)5 Collections (java.util.Collections)5 Map (java.util.Map)5 Optional (java.util.Optional)5 SamzaApplication (org.apache.samza.application.SamzaApplication)5 StreamApplication (org.apache.samza.application.StreamApplication)5 ApplicationDescriptor (org.apache.samza.application.descriptors.ApplicationDescriptor)5 ApplicationDescriptorImpl (org.apache.samza.application.descriptors.ApplicationDescriptorImpl)5 ApplicationDescriptorUtil (org.apache.samza.application.descriptors.ApplicationDescriptorUtil)5