Search in sources :

Example 1 with StreamActivationEvent

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

the class StreamingContainer method activate.

public synchronized void activate(final Map<Integer, OperatorDeployInfo> nodeMap, Map<String, ComponentContextPair<Stream, StreamContext>> newStreams) {
    for (ComponentContextPair<Stream, StreamContext> pair : newStreams.values()) {
        activeStreams.put(pair.component, pair.context);
        pair.component.activate(pair.context);
        eventBus.publish(new StreamActivationEvent(pair));
    }
    for (final OperatorDeployInfo ndi : nodeMap.values()) {
        /*
       * OiO nodes get activated with their primary nodes.
       */
        if (ndi.type == OperatorType.OIO) {
            continue;
        }
        final Node<?> node = nodes.get(ndi.id);
        final String name = new StringBuilder(Integer.toString(ndi.id)).append('/').append(ndi.name).append(':').append(node.getOperator().getClass().getSimpleName()).toString();
        final Thread thread = new Thread(name) {

            @Override
            public void run() {
                HashSet<OperatorDeployInfo> setOperators = new HashSet<>();
                OperatorDeployInfo currentdi = ndi;
                try {
                    /* primary operator initialization */
                    setupNode(currentdi);
                    setOperators.add(currentdi);
                    /* lets go for OiO operator initialization */
                    List<Integer> oioNodeIdList = oioGroups.get(ndi.id);
                    if (oioNodeIdList != null) {
                        for (Integer oioNodeId : oioNodeIdList) {
                            currentdi = nodeMap.get(oioNodeId);
                            setupNode(currentdi);
                            setOperators.add(currentdi);
                        }
                    }
                    currentdi = null;
                    node.run();
                /* this is a blocking call */
                } catch (Error error) {
                    int[] operators;
                    //fetch logFileInfo before logging exception, to get offset before exception
                    LogFileInformation logFileInfo = LoggerUtil.getLogFileInformation();
                    if (currentdi == null) {
                        logger.error("Voluntary container termination due to an error in operator set {}.", setOperators, error);
                        operators = new int[setOperators.size()];
                        int i = 0;
                        for (Iterator<OperatorDeployInfo> it = setOperators.iterator(); it.hasNext(); i++) {
                            operators[i] = it.next().id;
                        }
                    } else {
                        logger.error("Voluntary container termination due to an error in operator {}.", currentdi, error);
                        operators = new int[] { currentdi.id };
                    }
                    try {
                        umbilical.reportError(containerId, operators, "Voluntary container termination due to an error. " + ExceptionUtils.getStackTrace(error), logFileInfo);
                    } catch (Exception e) {
                        logger.debug("Fail to log", e);
                    } finally {
                        System.exit(1);
                    }
                } catch (Exception ex) {
                    //fetch logFileInfo before logging exception, to get offset before exception
                    LogFileInformation logFileInfo = LoggerUtil.getLogFileInformation();
                    if (currentdi == null) {
                        failedNodes.add(ndi.id);
                        logger.error("Operator set {} stopped running due to an exception.", setOperators, ex);
                        int[] operators = new int[] { ndi.id };
                        try {
                            umbilical.reportError(containerId, operators, "Stopped running due to an exception. " + ExceptionUtils.getStackTrace(ex), logFileInfo);
                        } catch (Exception e) {
                            logger.debug("Fail to log", e);
                        }
                    } else {
                        failedNodes.add(currentdi.id);
                        logger.error("Abandoning deployment of operator {} due to setup failure.", currentdi, ex);
                        int[] operators = new int[] { currentdi.id };
                        try {
                            umbilical.reportError(containerId, operators, "Abandoning deployment due to setup failure. " + ExceptionUtils.getStackTrace(ex), logFileInfo);
                        } catch (Exception e) {
                            logger.debug("Fail to log", e);
                        }
                    }
                } finally {
                    if (setOperators.contains(ndi)) {
                        try {
                            teardownNode(ndi);
                        } catch (Exception ex) {
                            failedNodes.add(ndi.id);
                            logger.error("Shutdown of operator {} failed due to an exception.", ndi, ex);
                        }
                    }
                    List<Integer> oioNodeIdList = oioGroups.get(ndi.id);
                    if (oioNodeIdList != null) {
                        for (Integer oioNodeId : oioNodeIdList) {
                            OperatorDeployInfo oiodi = nodeMap.get(oioNodeId);
                            if (setOperators.contains(oiodi)) {
                                try {
                                    teardownNode(oiodi);
                                } catch (Exception ex) {
                                    failedNodes.add(oiodi.id);
                                    logger.error("Shutdown of operator {} failed due to an exception.", oiodi, ex);
                                }
                            }
                        }
                    }
                }
            }
        };
        node.context.setThread(thread);
        List<Integer> oioNodeIdList = oioGroups.get(ndi.id);
        if (oioNodeIdList != null) {
            for (Integer oioNodeId : oioNodeIdList) {
                Node<?> oioNode = nodes.get(oioNodeId);
                oioNode.context.setThread(thread);
            }
        }
        thread.start();
    }
    for (WindowGenerator wg : generators.values()) {
        if (!activeGenerators.containsKey(wg)) {
            activeGenerators.put(wg, generators);
            wg.activate(null);
        }
    }
}
Also used : OperatorDeployInfo(com.datatorrent.stram.api.OperatorDeployInfo) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) StreamActivationEvent(com.datatorrent.stram.api.ContainerEvent.StreamActivationEvent) LogFileInformation(org.apache.apex.log.LogFileInformation) Iterator(java.util.Iterator) OiOStream(com.datatorrent.stram.stream.OiOStream) InlineStream(com.datatorrent.stram.stream.InlineStream) MuxStream(com.datatorrent.stram.stream.MuxStream) HashSet(java.util.HashSet)

Aggregations

StreamActivationEvent (com.datatorrent.stram.api.ContainerEvent.StreamActivationEvent)1 OperatorDeployInfo (com.datatorrent.stram.api.OperatorDeployInfo)1 InlineStream (com.datatorrent.stram.stream.InlineStream)1 MuxStream (com.datatorrent.stram.stream.MuxStream)1 OiOStream (com.datatorrent.stram.stream.OiOStream)1 IOException (java.io.IOException)1 UnknownHostException (java.net.UnknownHostException)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 LogFileInformation (org.apache.apex.log.LogFileInformation)1