Search in sources :

Example 1 with TaskContext

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

the class TestLocalTableProvider method testInit.

@Test
public void testInit() {
    Context context = mock(Context.class);
    JobContext jobContext = mock(JobContext.class);
    when(context.getJobContext()).thenReturn(jobContext);
    when(jobContext.getConfig()).thenReturn(new MapConfig());
    ContainerContext containerContext = mock(ContainerContext.class);
    when(context.getContainerContext()).thenReturn(containerContext);
    when(containerContext.getContainerMetricsRegistry()).thenReturn(new NoOpMetricsRegistry());
    TaskContext taskContext = mock(TaskContext.class);
    when(context.getTaskContext()).thenReturn(taskContext);
    when(taskContext.getStore(any())).thenReturn(mock(KeyValueStore.class));
    TableProvider tableProvider = createTableProvider("t1");
    tableProvider.init(context);
    Assert.assertNotNull(tableProvider.getTable());
}
Also used : Context(org.apache.samza.context.Context) JobContext(org.apache.samza.context.JobContext) ContainerContext(org.apache.samza.context.ContainerContext) TaskContext(org.apache.samza.context.TaskContext) ContainerContext(org.apache.samza.context.ContainerContext) TaskContext(org.apache.samza.context.TaskContext) NoOpMetricsRegistry(org.apache.samza.util.NoOpMetricsRegistry) JobContext(org.apache.samza.context.JobContext) MapConfig(org.apache.samza.config.MapConfig) TableProvider(org.apache.samza.table.TableProvider) Test(org.junit.Test)

Example 2 with TaskContext

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

the class QueryTranslator method translate.

/**
 * For unit testing only
 */
@VisibleForTesting
void translate(SamzaSqlQueryParser.QueryInfo queryInfo, StreamApplicationDescriptor appDesc, int queryId) {
    QueryPlanner planner = new QueryPlanner(sqlConfig.getRelSchemaProviders(), sqlConfig.getInputSystemStreamConfigBySource(), sqlConfig.getUdfMetadata(), sqlConfig.isQueryPlanOptimizerEnabled());
    final RelRoot relRoot = planner.plan(queryInfo.getSelectQuery());
    SamzaSqlExecutionContext executionContext = new SamzaSqlExecutionContext(sqlConfig);
    TranslatorContext translatorContext = new TranslatorContext(appDesc, relRoot, executionContext);
    translate(relRoot, sqlConfig.getOutputSystemStreams().get(queryId), translatorContext, queryId);
    Map<Integer, TranslatorContext> translatorContexts = new HashMap<>();
    translatorContexts.put(queryId, translatorContext.clone());
    appDesc.withApplicationTaskContextFactory(new ApplicationTaskContextFactory<SamzaSqlApplicationContext>() {

        @Override
        public SamzaSqlApplicationContext create(ExternalContext externalContext, JobContext jobContext, ContainerContext containerContext, TaskContext taskContext, ApplicationContainerContext applicationContainerContext) {
            return new SamzaSqlApplicationContext(translatorContexts);
        }
    });
}
Also used : TaskContext(org.apache.samza.context.TaskContext) HashMap(java.util.HashMap) RelRoot(org.apache.calcite.rel.RelRoot) QueryPlanner(org.apache.samza.sql.planner.QueryPlanner) ApplicationContainerContext(org.apache.samza.context.ApplicationContainerContext) ContainerContext(org.apache.samza.context.ContainerContext) ApplicationContainerContext(org.apache.samza.context.ApplicationContainerContext) SamzaSqlApplicationContext(org.apache.samza.sql.runner.SamzaSqlApplicationContext) ExternalContext(org.apache.samza.context.ExternalContext) SamzaSqlExecutionContext(org.apache.samza.sql.data.SamzaSqlExecutionContext) JobContext(org.apache.samza.context.JobContext) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 3 with TaskContext

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

the class SamzaTimerInternalsFactoryTest method createNonKeyedStateInternalsFactory.

private static SamzaStoreStateInternals.Factory<?> createNonKeyedStateInternalsFactory(SamzaPipelineOptions pipelineOptions, KeyValueStore<ByteArray, StateValue<?>> store) {
    final TaskContext context = mock(TaskContext.class);
    when(context.getStore(anyString())).thenReturn((KeyValueStore) store);
    return SamzaStoreStateInternals.createNonKeyedStateInternalsFactory("42", context, pipelineOptions);
}
Also used : TaskContext(org.apache.samza.context.TaskContext)

Example 4 with TaskContext

use of org.apache.samza.context.TaskContext 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 5 with TaskContext

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

the class OperatorImpl method init.

/**
 * Initialize this {@link OperatorImpl} and its user-defined functions.
 *
 * @param internalTaskContext the {@link InternalTaskContext} for the task
 */
public final void init(InternalTaskContext internalTaskContext) {
    final Context context = internalTaskContext.getContext();
    String opId = getOpImplId();
    if (initialized) {
        throw new IllegalStateException(String.format("Attempted to initialize Operator %s more than once.", opId));
    }
    if (closed) {
        throw new IllegalStateException(String.format("Attempted to initialize Operator %s after it was closed.", opId));
    }
    this.highResClock = createHighResClock(context.getJobContext().getConfig());
    registeredOperators = new LinkedHashSet<>();
    prevOperators = new LinkedHashSet<>();
    inputStreams = new LinkedHashSet<>();
    final ContainerContext containerContext = context.getContainerContext();
    final MetricsRegistry metricsRegistry = containerContext.getContainerMetricsRegistry();
    this.numMessage = metricsRegistry.newCounter(METRICS_GROUP, opId + "-messages");
    this.handleMessageNs = metricsRegistry.newTimer(METRICS_GROUP, opId + "-handle-message-ns");
    this.handleTimerNs = metricsRegistry.newTimer(METRICS_GROUP, opId + "-handle-timer-ns");
    final TaskContext taskContext = context.getTaskContext();
    this.taskName = taskContext.getTaskModel().getTaskName();
    this.eosStates = (EndOfStreamStates) internalTaskContext.fetchObject(EndOfStreamStates.class.getName());
    this.watermarkStates = (WatermarkStates) internalTaskContext.fetchObject(WatermarkStates.class.getName());
    this.controlMessageSender = new ControlMessageSender(internalTaskContext.getStreamMetadataCache());
    this.taskModel = taskContext.getTaskModel();
    this.callbackScheduler = taskContext.getCallbackScheduler();
    handleInit(context);
    this.elasticityFactor = new JobConfig(context.getJobContext().getConfig()).getElasticityFactor();
    initialized = true;
}
Also used : TaskContext(org.apache.samza.context.TaskContext) ContainerContext(org.apache.samza.context.ContainerContext) Context(org.apache.samza.context.Context) InternalTaskContext(org.apache.samza.context.InternalTaskContext) ContainerContext(org.apache.samza.context.ContainerContext) MetricsRegistry(org.apache.samza.metrics.MetricsRegistry) TaskContext(org.apache.samza.context.TaskContext) InternalTaskContext(org.apache.samza.context.InternalTaskContext) JobConfig(org.apache.samza.config.JobConfig)

Aggregations

TaskContext (org.apache.samza.context.TaskContext)5 ContainerContext (org.apache.samza.context.ContainerContext)4 JobContext (org.apache.samza.context.JobContext)3 HashMap (java.util.HashMap)2 RelRoot (org.apache.calcite.rel.RelRoot)2 ApplicationContainerContext (org.apache.samza.context.ApplicationContainerContext)2 Context (org.apache.samza.context.Context)2 ExternalContext (org.apache.samza.context.ExternalContext)2 SamzaSqlExecutionContext (org.apache.samza.sql.data.SamzaSqlExecutionContext)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 LinkedList (java.util.LinkedList)1 JobConfig (org.apache.samza.config.JobConfig)1 MapConfig (org.apache.samza.config.MapConfig)1 InternalTaskContext (org.apache.samza.context.InternalTaskContext)1 MetricsRegistry (org.apache.samza.metrics.MetricsRegistry)1 QueryPlanner (org.apache.samza.sql.planner.QueryPlanner)1 SamzaSqlApplicationContext (org.apache.samza.sql.runner.SamzaSqlApplicationContext)1 QueryTranslator (org.apache.samza.sql.translator.QueryTranslator)1 TranslatorContext (org.apache.samza.sql.translator.TranslatorContext)1 TableProvider (org.apache.samza.table.TableProvider)1