Search in sources :

Example 1 with MessageType

use of org.apache.samza.system.MessageType 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)

Example 2 with MessageType

use of org.apache.samza.system.MessageType in project samza by apache.

the class RunLoop method getWorkersForEnvelope.

/**
 * when elasticity is not enabled, fetch the workers from sspToTaskWorkerMapping using envelope.getSSP()
 * when elasticity is enabled,
 *       sspToTaskWorkerMapping has workers for a SSP which has keyBucket
 *       hence need to use envelop.getSSP(elasticityFactor)
 *       Additionally, when envelope is EnofStream or Watermark, it needs to be sent to all works for the ssp irrespective of keyBucket
 * @param envelope
 * @return list of workers for the envelope
 */
private List<AsyncTaskWorker> getWorkersForEnvelope(IncomingMessageEnvelope envelope) {
    if (elasticityFactor <= 1) {
        return sspToTaskWorkerMapping.get(envelope.getSystemStreamPartition());
    }
    final SystemStreamPartition sspOfEnvelope = envelope.getSystemStreamPartition(elasticityFactor);
    List<AsyncTaskWorker> listOfWorkersForEnvelope = null;
    // if envelope is end of stream or watermark, it needs to be routed to all tasks consuming the ssp irresp of keybucket
    MessageType messageType = MessageType.of(envelope.getMessage());
    if (envelope.isEndOfStream() || MessageType.END_OF_STREAM == messageType || MessageType.WATERMARK == messageType) {
        // sspToTaskWorkerMapping has ssps with keybucket so extract and check only system, stream and partition and ignore the keybucket
        listOfWorkersForEnvelope = sspToTaskWorkerMapping.entrySet().stream().filter(sspToTask -> sspToTask.getKey().getSystemStream().equals(sspOfEnvelope.getSystemStream()) && sspToTask.getKey().getPartition().equals(sspOfEnvelope.getPartition())).map(sspToWorker -> sspToWorker.getValue()).flatMap(Collection::stream).collect(Collectors.toList());
    } else {
        listOfWorkersForEnvelope = sspToTaskWorkerMapping.get(sspOfEnvelope);
    }
    return listOfWorkersForEnvelope;
}
Also used : TaskCallbackManager(org.apache.samza.task.TaskCallbackManager) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) TaskCallbackFactory(org.apache.samza.task.TaskCallbackFactory) SystemStreamPartition(org.apache.samza.system.SystemStreamPartition) ArrayList(java.util.ArrayList) CoordinatorRequests(org.apache.samza.task.CoordinatorRequests) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) HighResolutionClock(org.apache.samza.util.HighResolutionClock) ExecutorService(java.util.concurrent.ExecutorService) TaskCallback(org.apache.samza.task.TaskCallback) IncomingMessageEnvelope(org.apache.samza.system.IncomingMessageEnvelope) Logger(org.slf4j.Logger) Collection(java.util.Collection) ThrottlingScheduler(org.apache.samza.util.ThrottlingScheduler) TaskCallbackListener(org.apache.samza.task.TaskCallbackListener) Set(java.util.Set) MessageType(org.apache.samza.system.MessageType) SystemConsumers(org.apache.samza.system.SystemConsumers) TaskCallbackImpl(org.apache.samza.task.TaskCallbackImpl) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) SamzaException(org.apache.samza.SamzaException) TaskCoordinator(org.apache.samza.task.TaskCoordinator) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) ReadableCoordinator(org.apache.samza.task.ReadableCoordinator) Throttleable(org.apache.samza.util.Throttleable) ArrayDeque(java.util.ArrayDeque) EpochTimeScheduler(org.apache.samza.scheduler.EpochTimeScheduler) Collections(java.util.Collections) Collection(java.util.Collection) MessageType(org.apache.samza.system.MessageType) SystemStreamPartition(org.apache.samza.system.SystemStreamPartition)

Example 3 with MessageType

use of org.apache.samza.system.MessageType in project samza by apache.

the class IntermediateMessageSerde method toBytes.

@Override
public byte[] toBytes(Object object) {
    final byte[] data;
    final MessageType type = MessageType.of(object);
    switch(type) {
        case USER_MESSAGE:
            data = userMessageSerde.toBytes(object);
            break;
        case WATERMARK:
            data = watermarkSerde.toBytes((WatermarkMessage) object);
            break;
        case END_OF_STREAM:
            data = eosSerde.toBytes((EndOfStreamMessage) object);
            break;
        default:
            throw new SamzaException("Unknown message type: " + type.name());
    }
    final byte[] bytes = new byte[data.length + 1];
    bytes[0] = (byte) type.ordinal();
    System.arraycopy(data, 0, bytes, 1, data.length);
    return bytes;
}
Also used : WatermarkMessage(org.apache.samza.system.WatermarkMessage) SamzaException(org.apache.samza.SamzaException) MessageType(org.apache.samza.system.MessageType) EndOfStreamMessage(org.apache.samza.system.EndOfStreamMessage)

Example 4 with MessageType

use of org.apache.samza.system.MessageType in project samza by apache.

the class IntermediateMessageSerde method fromBytes.

@Override
public Object fromBytes(byte[] bytes) {
    try {
        final Object object;
        final MessageType type;
        try {
            type = MessageType.values()[bytes[0]];
        } catch (ArrayIndexOutOfBoundsException e) {
            // has reached retention time.
            throw new SamzaException("Error reading the message type from intermediate message. This may happen if you " + "have recently upgraded from samza version older than 0.13.1 or there are still old messages in the " + "intermediate stream.", e);
        }
        final byte[] data = Arrays.copyOfRange(bytes, 1, bytes.length);
        switch(type) {
            case USER_MESSAGE:
                object = userMessageSerde.fromBytes(data);
                break;
            case WATERMARK:
                object = watermarkSerde.fromBytes(data);
                break;
            case END_OF_STREAM:
                object = eosSerde.fromBytes(data);
                break;
            default:
                throw new UnsupportedOperationException(String.format("Message type %s is not supported", type.name()));
        }
        return object;
    } catch (UnsupportedOperationException ue) {
        throw new SamzaException(ue);
    } catch (Exception e) {
        throw e;
    }
}
Also used : SamzaException(org.apache.samza.SamzaException) MessageType(org.apache.samza.system.MessageType) SamzaException(org.apache.samza.SamzaException)

Aggregations

SamzaException (org.apache.samza.SamzaException)4 MessageType (org.apache.samza.system.MessageType)4 ExecutorService (java.util.concurrent.ExecutorService)2 EndOfStreamMessage (org.apache.samza.system.EndOfStreamMessage)2 IncomingMessageEnvelope (org.apache.samza.system.IncomingMessageEnvelope)2 WatermarkMessage (org.apache.samza.system.WatermarkMessage)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Preconditions (com.google.common.base.Preconditions)1 ArrayDeque (java.util.ArrayDeque)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CompletionStage (java.util.concurrent.CompletionStage)1