Search in sources :

Example 1 with StreamDeactivationEvent

use of com.datatorrent.stram.api.ContainerEvent.StreamDeactivationEvent in project apex-core by apache.

the class StreamingContainer method disconnectNode.

private void disconnectNode(int nodeid) {
    Node<?> node = nodes.get(nodeid);
    disconnectWindowGenerator(nodeid, node);
    PortMappingDescriptor portMappingDescriptor = node.getPortMappingDescriptor();
    Iterator<String> outputPorts = portMappingDescriptor.outputPorts.keySet().iterator();
    while (outputPorts.hasNext()) {
        String sourceIdentifier = String.valueOf(nodeid).concat(Component.CONCAT_SEPARATOR).concat(outputPorts.next());
        ComponentContextPair<Stream, StreamContext> pair = streams.remove(sourceIdentifier);
        if (pair != null) {
            if (activeStreams.remove(pair.component) != null) {
                pair.component.deactivate();
                eventBus.publish(new StreamDeactivationEvent(pair));
            }
            if (pair.component instanceof Stream.MultiSinkCapableStream) {
                String sinks = pair.context.getSinkId();
                if (sinks == null) {
                    logger.error("mux sinks found connected at {} with sink id null", sourceIdentifier);
                } else {
                    String[] split = sinks.split(MuxStream.MULTI_SINK_ID_CONCAT_SEPARATOR);
                    for (int i = split.length; i-- > 0; ) {
                        ComponentContextPair<Stream, StreamContext> spair = streams.remove(split[i]);
                        if (spair == null) {
                            logger.error("mux is missing the stream for sink {}", split[i]);
                        } else {
                            if (activeStreams.remove(spair.component) != null) {
                                spair.component.deactivate();
                                eventBus.publish(new StreamDeactivationEvent(spair));
                            }
                            spair.component.teardown();
                        }
                    }
                }
            } else {
            // it's either inline stream or it's bufferserver publisher.
            }
            pair.component.teardown();
        }
    }
    Iterator<String> inputPorts = portMappingDescriptor.inputPorts.keySet().iterator();
    while (inputPorts.hasNext()) {
        String sinkIdentifier = String.valueOf(nodeid).concat(Component.CONCAT_SEPARATOR).concat(inputPorts.next());
        ComponentContextPair<Stream, StreamContext> pair = streams.remove(sinkIdentifier);
        if (pair != null) {
            if (activeStreams.remove(pair.component) != null) {
                pair.component.deactivate();
                eventBus.publish(new StreamDeactivationEvent(pair));
            }
            pair.component.teardown();
            /**
             * we should also make sure that if this stream is connected to mux stream,
             * we deregister it from the mux stream to avoid clogged sink problem.
             */
            ComponentContextPair<Stream, StreamContext> sourcePair = streams.get(pair.context.getSourceId());
            if (sourcePair != null) {
                if (sourcePair == pair) {
                    /* for some reason we had the stream stored against both source and sink identifiers */
                    streams.remove(pair.context.getSourceId());
                } else {
                    /* the stream was one of the many streams sourced by a muxstream */
                    unregisterSinkFromMux(sourcePair, sinkIdentifier);
                }
            }
        }
    }
}
Also used : OiOStream(com.datatorrent.stram.stream.OiOStream) InlineStream(com.datatorrent.stram.stream.InlineStream) MuxStream(com.datatorrent.stram.stream.MuxStream) StreamDeactivationEvent(com.datatorrent.stram.api.ContainerEvent.StreamDeactivationEvent) Checkpoint(com.datatorrent.stram.api.Checkpoint) PortMappingDescriptor(com.datatorrent.stram.plan.logical.Operators.PortMappingDescriptor)

Example 2 with StreamDeactivationEvent

use of com.datatorrent.stram.api.ContainerEvent.StreamDeactivationEvent in project apex-core by apache.

the class StreamingContainer method unregisterSinkFromMux.

private boolean unregisterSinkFromMux(ComponentContextPair<Stream, StreamContext> muxpair, String sinkIdentifier) {
    String[] sinks = muxpair.context.getSinkId().split(MuxStream.MULTI_SINK_ID_CONCAT_SEPARATOR);
    boolean found = false;
    for (int i = sinks.length; i-- > 0; ) {
        if (sinks[i].equals(sinkIdentifier)) {
            sinks[i] = null;
            found = true;
            break;
        }
    }
    if (found) {
        ((Stream.MultiSinkCapableStream) muxpair.component).setSink(sinkIdentifier, null);
        if (sinks.length == 1) {
            muxpair.context.setSinkId(null);
            streams.remove(muxpair.context.getSourceId());
            if (activeStreams.remove(muxpair.component) != null) {
                muxpair.component.deactivate();
                eventBus.publish(new StreamDeactivationEvent(muxpair));
            }
            muxpair.component.teardown();
        } else {
            StringBuilder builder = new StringBuilder(muxpair.context.getSinkId().length() - MuxStream.MULTI_SINK_ID_CONCAT_SEPARATOR.length() - sinkIdentifier.length());
            found = false;
            for (int i = sinks.length; i-- > 0; ) {
                if (sinks[i] != null) {
                    if (found) {
                        builder.append(MuxStream.MULTI_SINK_ID_CONCAT_SEPARATOR).append(sinks[i]);
                    } else {
                        builder.append(sinks[i]);
                        found = true;
                    }
                }
            }
            muxpair.context.setSinkId(builder.toString());
        }
    } else {
        logger.error("{} was not connected to stream connected to {}", sinkIdentifier, muxpair.context.getSourceId());
    }
    return found;
}
Also used : StreamDeactivationEvent(com.datatorrent.stram.api.ContainerEvent.StreamDeactivationEvent) Checkpoint(com.datatorrent.stram.api.Checkpoint)

Aggregations

Checkpoint (com.datatorrent.stram.api.Checkpoint)2 StreamDeactivationEvent (com.datatorrent.stram.api.ContainerEvent.StreamDeactivationEvent)2 PortMappingDescriptor (com.datatorrent.stram.plan.logical.Operators.PortMappingDescriptor)1 InlineStream (com.datatorrent.stram.stream.InlineStream)1 MuxStream (com.datatorrent.stram.stream.MuxStream)1 OiOStream (com.datatorrent.stram.stream.OiOStream)1