Search in sources :

Example 16 with Context

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

the class TestStreamOperatorTask method testCloseDuringInitializationErrors.

@Test
public void testCloseDuringInitializationErrors() throws Exception {
    Context context = mock(Context.class);
    JobContext jobContext = mock(JobContext.class);
    when(context.getJobContext()).thenReturn(jobContext);
    doThrow(new RuntimeException("Failed to get config")).when(jobContext).getConfig();
    StreamOperatorTask operatorTask = new StreamOperatorTask(mock(OperatorSpecGraph.class), mock(Clock.class));
    try {
        operatorTask.init(context);
    } catch (RuntimeException e) {
        if (e instanceof NullPointerException) {
            fail("Unexpected null pointer exception");
        }
    }
    operatorTask.close();
}
Also used : JobContext(org.apache.samza.context.JobContext) Context(org.apache.samza.context.Context) OperatorSpecGraph(org.apache.samza.operators.OperatorSpecGraph) JobContext(org.apache.samza.context.JobContext) Clock(org.apache.samza.util.Clock) Test(org.junit.Test)

Example 17 with Context

use of org.apache.samza.context.Context 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

Context (org.apache.samza.context.Context)17 MapConfig (org.apache.samza.config.MapConfig)8 ContainerContext (org.apache.samza.context.ContainerContext)8 HashMap (java.util.HashMap)7 MockContext (org.apache.samza.context.MockContext)7 Test (org.junit.Test)7 JobContext (org.apache.samza.context.JobContext)5 MetricsRegistry (org.apache.samza.metrics.MetricsRegistry)5 Counter (org.apache.samza.metrics.Counter)4 Timer (org.apache.samza.metrics.Timer)4 RelNode (org.apache.calcite.rel.RelNode)3 StreamApplicationDescriptorImpl (org.apache.samza.application.descriptors.StreamApplicationDescriptorImpl)3 TaskModel (org.apache.samza.job.model.TaskModel)3 MessageStream (org.apache.samza.operators.MessageStream)3 MessageStreamImpl (org.apache.samza.operators.MessageStreamImpl)3 ArrayList (java.util.ArrayList)2 DataContext (org.apache.calcite.DataContext)2 RelDataType (org.apache.calcite.rel.type.RelDataType)2 RexNode (org.apache.calcite.rex.RexNode)2 TaskName (org.apache.samza.container.TaskName)2