Search in sources :

Example 1 with InputOperatorImpl

use of org.apache.samza.operators.impl.InputOperatorImpl in project samza by apache.

the class StreamOperatorTask method processAsync.

/**
 * Passes the incoming message envelopes along to the {@link InputOperatorImpl} node
 * for the input {@link SystemStream}. It is non-blocking and dispatches the message to the container thread
 * pool. The thread pool size is configured through job.container.thread.pool.size. In the absence of the config,
 * the task executes the DAG on the run loop thread.
 * <p>
 * From then on, each {@link org.apache.samza.operators.impl.OperatorImpl} propagates its transformed output to
 * its chained {@link org.apache.samza.operators.impl.OperatorImpl}s itself.
 *
 * @param ime incoming message envelope to process
 * @param collector the collector to send messages with
 * @param coordinator the coordinator to request commits or shutdown
 * @param callback the task callback handle
 */
@Override
public final void processAsync(IncomingMessageEnvelope ime, MessageCollector collector, TaskCoordinator coordinator, TaskCallback callback) {
    Runnable processRunnable = () -> {
        try {
            SystemStream systemStream = ime.getSystemStreamPartition().getSystemStream();
            InputOperatorImpl inputOpImpl = operatorImplGraph.getInputOperator(systemStream);
            if (inputOpImpl != null) {
                CompletionStage<Void> processFuture;
                MessageType messageType = MessageType.of(ime.getMessage());
                switch(messageType) {
                    case USER_MESSAGE:
                        processFuture = inputOpImpl.onMessageAsync(ime, collector, coordinator);
                        break;
                    case END_OF_STREAM:
                        EndOfStreamMessage eosMessage = (EndOfStreamMessage) ime.getMessage();
                        processFuture = inputOpImpl.aggregateEndOfStream(eosMessage, ime.getSystemStreamPartition(), collector, coordinator);
                        break;
                    case WATERMARK:
                        WatermarkMessage watermarkMessage = (WatermarkMessage) ime.getMessage();
                        processFuture = inputOpImpl.aggregateWatermark(watermarkMessage, ime.getSystemStreamPartition(), collector, coordinator);
                        break;
                    default:
                        processFuture = failedFuture(new SamzaException("Unknown message type " + messageType + " encountered."));
                        break;
                }
                processFuture.whenComplete((val, ex) -> {
                    if (ex != null) {
                        callback.failure(ex);
                    } else {
                        callback.complete();
                    }
                });
            } else {
                // If InputOperator is not found in the operator graph for a given SystemStream, throw an exception else the
                // job will timeout due to async task callback timeout (TaskCallbackTimeoutException)
                final String errMessage = String.format("InputOperator not found in OperatorGraph for %s. The available input" + " operators are: %s. Please check SystemStream configuration for the `SystemConsumer` and/or task.inputs" + " task configuration.", systemStream, operatorImplGraph.getAllInputOperators());
                LOG.error(errMessage);
                callback.failure(new SamzaException(errMessage));
            }
        } catch (Exception e) {
            LOG.error("Failed to process the incoming message due to ", e);
            callback.failure(e);
        }
    };
    if (taskThreadPool != null) {
        LOG.debug("Processing message using thread pool.");
        taskThreadPool.submit(processRunnable);
    } else {
        LOG.debug("Processing message on the run loop thread.");
        processRunnable.run();
    }
}
Also used : IncomingMessageEnvelope(org.apache.samza.system.IncomingMessageEnvelope) Logger(org.slf4j.Logger) InputOperatorImpl(org.apache.samza.operators.impl.InputOperatorImpl) LoggerFactory(org.slf4j.LoggerFactory) CompletableFuture(java.util.concurrent.CompletableFuture) Clock(org.apache.samza.util.Clock) MessageType(org.apache.samza.system.MessageType) OperatorSpecGraph(org.apache.samza.operators.OperatorSpecGraph) SamzaException(org.apache.samza.SamzaException) Context(org.apache.samza.context.Context) CompletionStage(java.util.concurrent.CompletionStage) SystemClock(org.apache.samza.util.SystemClock) OperatorImplGraph(org.apache.samza.operators.impl.OperatorImplGraph) SystemStream(org.apache.samza.system.SystemStream) WatermarkMessage(org.apache.samza.system.WatermarkMessage) Preconditions(com.google.common.base.Preconditions) EndOfStreamMessage(org.apache.samza.system.EndOfStreamMessage) VisibleForTesting(com.google.common.annotations.VisibleForTesting) ExecutorService(java.util.concurrent.ExecutorService) WatermarkMessage(org.apache.samza.system.WatermarkMessage) SystemStream(org.apache.samza.system.SystemStream) InputOperatorImpl(org.apache.samza.operators.impl.InputOperatorImpl) SamzaException(org.apache.samza.SamzaException) CompletionStage(java.util.concurrent.CompletionStage) MessageType(org.apache.samza.system.MessageType) EndOfStreamMessage(org.apache.samza.system.EndOfStreamMessage) SamzaException(org.apache.samza.SamzaException)

Aggregations

VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Preconditions (com.google.common.base.Preconditions)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CompletionStage (java.util.concurrent.CompletionStage)1 ExecutorService (java.util.concurrent.ExecutorService)1 SamzaException (org.apache.samza.SamzaException)1 Context (org.apache.samza.context.Context)1 OperatorSpecGraph (org.apache.samza.operators.OperatorSpecGraph)1 InputOperatorImpl (org.apache.samza.operators.impl.InputOperatorImpl)1 OperatorImplGraph (org.apache.samza.operators.impl.OperatorImplGraph)1 EndOfStreamMessage (org.apache.samza.system.EndOfStreamMessage)1 IncomingMessageEnvelope (org.apache.samza.system.IncomingMessageEnvelope)1 MessageType (org.apache.samza.system.MessageType)1 SystemStream (org.apache.samza.system.SystemStream)1 WatermarkMessage (org.apache.samza.system.WatermarkMessage)1 Clock (org.apache.samza.util.Clock)1 SystemClock (org.apache.samza.util.SystemClock)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1