Search in sources :

Example 6 with StreamJunction

use of io.siddhi.core.stream.StreamJunction in project siddhi by wso2.

the class SiddhiAppRuntimeBuilder method defineStream.

public void defineStream(StreamDefinition streamDefinition) {
    DefinitionParserHelper.validateDefinition(streamDefinition, streamDefinitionMap, tableDefinitionMap, windowDefinitionMap, aggregationDefinitionMap);
    AbstractDefinition currentDefinition = streamDefinitionMap.putIfAbsent(streamDefinition.getId(), streamDefinition);
    if (currentDefinition != null) {
        streamDefinition = (StreamDefinition) currentDefinition;
    }
    try {
        DefinitionParserHelper.addStreamJunction(streamDefinition, streamJunctionMap, siddhiAppContext);
    } catch (Throwable t) {
        ExceptionUtil.populateQueryContext(t, streamDefinition, siddhiAppContext);
        throw t;
    }
    DefinitionParserHelper.addEventSource(streamDefinition, sourceMap, siddhiAppContext);
    StreamJunction streamJunction = streamJunctionMap.get(streamDefinition.getId());
    DefinitionParserHelper.addEventSink(streamDefinition, streamJunction, sinkMap, siddhiAppContext);
}
Also used : StreamJunction(io.siddhi.core.stream.StreamJunction) AbstractDefinition(io.siddhi.query.api.definition.AbstractDefinition)

Example 7 with StreamJunction

use of io.siddhi.core.stream.StreamJunction in project siddhi by wso2.

the class SiddhiAppRuntimeBuilder method addQuery.

public String addQuery(QueryRuntimeImpl queryRuntime) {
    QueryRuntime oldQueryRuntime = queryProcessorMap.put(queryRuntime.getQueryId(), queryRuntime);
    if (oldQueryRuntime != null) {
        throw new SiddhiAppCreationException("Multiple queries with name '" + queryRuntime.getQueryId() + "' defined in Siddhi App '" + siddhiAppContext.getName() + "'", queryRuntime.getQuery().getQueryContextStartIndex(), queryRuntime.getQuery().getQueryContextEndIndex());
    }
    StreamRuntime streamRuntime = queryRuntime.getStreamRuntime();
    for (SingleStreamRuntime singleStreamRuntime : streamRuntime.getSingleStreamRuntimes()) {
        ProcessStreamReceiver processStreamReceiver = singleStreamRuntime.getProcessStreamReceiver();
        if (processStreamReceiver.toStream()) {
            StreamJunction streamJunction = streamJunctionMap.get(processStreamReceiver.getStreamId());
            if (streamJunction != null) {
                streamJunction.subscribe(processStreamReceiver);
            } else {
                throw new SiddhiAppCreationException("Expecting a stream, but provided '" + processStreamReceiver.getStreamId() + "' is not a stream");
            }
        }
    }
    OutputCallback outputCallback = queryRuntime.getOutputCallback();
    if (outputCallback != null && outputCallback instanceof InsertIntoStreamCallback) {
        InsertIntoStreamCallback insertIntoStreamCallback = (InsertIntoStreamCallback) outputCallback;
        StreamDefinition streamDefinition = insertIntoStreamCallback.getOutputStreamDefinition();
        streamDefinitionMap.putIfAbsent(streamDefinition.getId(), streamDefinition);
        DefinitionParserHelper.validateOutputStream(streamDefinition, streamDefinitionMap.get(streamDefinition.getId()));
        StreamJunction outputStreamJunction = streamJunctionMap.get(streamDefinition.getId());
        if (outputStreamJunction == null) {
            outputStreamJunction = new StreamJunction(streamDefinition, siddhiAppContext.getExecutorService(), siddhiAppContext.getBufferSize(), null, siddhiAppContext);
            streamJunctionMap.putIfAbsent(streamDefinition.getId(), outputStreamJunction);
        }
        insertIntoStreamCallback.init(streamJunctionMap.get(insertIntoStreamCallback.getOutputStreamDefinition().getId()));
    } else if (outputCallback != null && outputCallback instanceof InsertIntoWindowCallback) {
        InsertIntoWindowCallback insertIntoWindowCallback = (InsertIntoWindowCallback) outputCallback;
        StreamDefinition streamDefinition = insertIntoWindowCallback.getOutputStreamDefinition();
        windowDefinitionMap.putIfAbsent(streamDefinition.getId(), streamDefinition);
        DefinitionParserHelper.validateOutputStream(streamDefinition, windowDefinitionMap.get(streamDefinition.getId()));
        StreamJunction outputStreamJunction = streamJunctionMap.get(streamDefinition.getId());
        if (outputStreamJunction == null) {
            outputStreamJunction = new StreamJunction(streamDefinition, siddhiAppContext.getExecutorService(), siddhiAppContext.getBufferSize(), null, siddhiAppContext);
            streamJunctionMap.putIfAbsent(streamDefinition.getId(), outputStreamJunction);
        }
        insertIntoWindowCallback.getWindow().setPublisher(streamJunctionMap.get(insertIntoWindowCallback.getOutputStreamDefinition().getId()).constructPublisher());
    }
    return queryRuntime.getQueryId();
}
Also used : ProcessStreamReceiver(io.siddhi.core.query.input.ProcessStreamReceiver) StreamDefinition(io.siddhi.query.api.definition.StreamDefinition) InsertIntoWindowCallback(io.siddhi.core.query.output.callback.InsertIntoWindowCallback) QueryRuntime(io.siddhi.core.query.QueryRuntime) SiddhiAppCreationException(io.siddhi.core.exception.SiddhiAppCreationException) SingleStreamRuntime(io.siddhi.core.query.input.stream.single.SingleStreamRuntime) StreamJunction(io.siddhi.core.stream.StreamJunction) StreamRuntime(io.siddhi.core.query.input.stream.StreamRuntime) SingleStreamRuntime(io.siddhi.core.query.input.stream.single.SingleStreamRuntime) InsertIntoStreamCallback(io.siddhi.core.query.output.callback.InsertIntoStreamCallback) OutputCallback(io.siddhi.core.query.output.callback.OutputCallback)

Example 8 with StreamJunction

use of io.siddhi.core.stream.StreamJunction in project siddhi by wso2.

the class SiddhiAppRuntimeImpl method startWithoutSources.

public synchronized void startWithoutSources() {
    if (running || runningWithoutSources) {
        log.warn("Error calling startWithoutSources() for Siddhi App '" + siddhiAppContext.getName() + "', " + "SiddhiApp already started.");
    } else {
        try {
            memoryUsageTracker.disableMemoryUsageMetrics();
            if (siddhiAppContext.getRootMetricsLevel().compareTo(Level.OFF) != 0 && siddhiAppContext.getStatisticsManager() != null) {
                if (siddhiAppContext.getRootMetricsLevel().compareTo(Level.DETAIL) == 0) {
                    memoryUsageTracker.enableMemoryUsageMetrics();
                }
                siddhiAppContext.getStatisticsManager().startReporting();
            }
            for (ExternalReferencedHolder externalReferencedHolder : siddhiAppContext.getExternalReferencedHolders()) {
                externalReferencedHolder.start();
            }
            for (List<Sink> sinks : sinkMap.values()) {
                for (Sink sink : sinks) {
                    sink.connectWithRetry();
                }
            }
            for (Table table : tableMap.values()) {
                table.connectWithRetry();
            }
            for (StreamJunction streamJunction : streamJunctionMap.values()) {
                streamJunction.startProcessing();
            }
            if (incrementalDataPurging) {
                for (AggregationRuntime aggregationRuntime : aggregationMap.values()) {
                    aggregationRuntime.startPurging();
                }
            }
            for (Trigger trigger : siddhiAppContext.getTriggerHolders()) {
                trigger.start();
            }
            inputManager.connect();
            runningWithoutSources = true;
        } catch (Throwable t) {
            log.error("Error starting Siddhi App '" + siddhiAppContext.getName() + "', " + "triggering shutdown process. " + t.getMessage());
            try {
                shutdown();
            } catch (Throwable t1) {
                log.error("Error shutting down partially started Siddhi App '" + siddhiAppContext.getName() + "', " + t1.getMessage());
            }
        }
    }
}
Also used : Table(io.siddhi.core.table.Table) Trigger(io.siddhi.core.trigger.Trigger) Sink(io.siddhi.core.stream.output.sink.Sink) StreamJunction(io.siddhi.core.stream.StreamJunction) ExternalReferencedHolder(io.siddhi.core.util.extension.holder.ExternalReferencedHolder) AggregationRuntime(io.siddhi.core.aggregation.AggregationRuntime)

Example 9 with StreamJunction

use of io.siddhi.core.stream.StreamJunction in project siddhi by wso2.

the class SiddhiAppRuntimeImpl method shutdown.

public synchronized void shutdown() {
    SourceHandlerManager sourceHandlerManager = siddhiAppContext.getSiddhiContext().getSourceHandlerManager();
    for (List<Source> sources : sourceMap.values()) {
        for (Source source : sources) {
            try {
                if (sourceHandlerManager != null) {
                    sourceHandlerManager.unregisterSourceHandler(source.getMapper().getHandler().getId());
                }
                source.shutdown();
            } catch (Throwable t) {
                log.error(StringUtil.removeCRLFCharacters(ExceptionUtil.getMessageWithContext(t, siddhiAppContext)) + " Error in shutting down source '" + StringUtil.removeCRLFCharacters(source.getType()) + "' at '" + StringUtil.removeCRLFCharacters(source.getStreamDefinition().getId()) + "' on Siddhi App '" + siddhiAppContext.getName() + "'.", t);
            }
        }
    }
    for (Table table : tableMap.values()) {
        try {
            table.shutdown();
        } catch (Throwable t) {
            log.error(StringUtil.removeCRLFCharacters(ExceptionUtil.getMessageWithContext(t, siddhiAppContext)) + " Error in shutting down table '" + StringUtil.removeCRLFCharacters(table.getTableDefinition().getId()) + "' on Siddhi App '" + StringUtil.removeCRLFCharacters(siddhiAppContext.getName()) + "'.", t);
        }
    }
    SinkHandlerManager sinkHandlerManager = siddhiAppContext.getSiddhiContext().getSinkHandlerManager();
    for (List<Sink> sinks : sinkMap.values()) {
        for (Sink sink : sinks) {
            try {
                if (sinkHandlerManager != null) {
                    sinkHandlerManager.unregisterSinkHandler(sink.getHandler().getId());
                }
                sink.shutdown();
            } catch (Throwable t) {
                log.error(StringUtil.removeCRLFCharacters(ExceptionUtil.getMessageWithContext(t, siddhiAppContext)) + " Error in shutting down sink '" + StringUtil.removeCRLFCharacters(sink.getType()) + "' at '" + StringUtil.removeCRLFCharacters(sink.getStreamDefinition().getId()) + "' on Siddhi App '" + StringUtil.removeCRLFCharacters(siddhiAppContext.getName()) + "'.", t);
            }
        }
    }
    for (Table table : tableMap.values()) {
        RecordTableHandlerManager recordTableHandlerManager = siddhiAppContext.getSiddhiContext().getRecordTableHandlerManager();
        if (recordTableHandlerManager != null) {
            String elementId = null;
            RecordTableHandler recordTableHandler = table.getHandler();
            if (recordTableHandler != null) {
                elementId = recordTableHandler.getId();
            }
            if (elementId != null) {
                recordTableHandlerManager.unregisterRecordTableHandler(elementId);
            }
        }
        table.shutdown();
    }
    for (ExternalReferencedHolder externalReferencedHolder : siddhiAppContext.getExternalReferencedHolders()) {
        try {
            externalReferencedHolder.stop();
        } catch (Throwable t) {
            log.error(StringUtil.removeCRLFCharacters(ExceptionUtil.getMessageWithContext(t, siddhiAppContext)) + " Error while stopping ExternalReferencedHolder '" + StringUtil.removeCRLFCharacters(externalReferencedHolder.toString()) + "' down Siddhi app '" + StringUtil.removeCRLFCharacters(siddhiAppContext.getName()) + "'.", t);
        }
    }
    inputManager.disconnect();
    Thread thread = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
            for (StreamJunction streamJunction : streamJunctionMap.values()) {
                streamJunction.stopProcessing();
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
            siddhiAppContext.getScheduledExecutorService().shutdownNow();
            siddhiAppContext.getExecutorService().shutdownNow();
        }
    }, "Siddhi-SiddhiApp-" + siddhiAppContext.getName() + "-Shutdown-Cleaner");
    thread.start();
    if (siddhiAppRuntimeMap != null) {
        siddhiAppRuntimeMap.remove(siddhiAppContext.getName());
    }
    if (siddhiAppContext.getStatisticsManager() != null) {
        if (siddhiAppContext.getRootMetricsLevel().compareTo(Level.OFF) != 0) {
            siddhiAppContext.getStatisticsManager().stopReporting();
        }
        siddhiAppContext.getStatisticsManager().cleanup();
    }
    running = false;
    runningWithoutSources = false;
}
Also used : Table(io.siddhi.core.table.Table) SourceHandlerManager(io.siddhi.core.stream.input.source.SourceHandlerManager) RecordTableHandler(io.siddhi.core.table.record.RecordTableHandler) ExternalReferencedHolder(io.siddhi.core.util.extension.holder.ExternalReferencedHolder) Source(io.siddhi.core.stream.input.source.Source) Sink(io.siddhi.core.stream.output.sink.Sink) StreamJunction(io.siddhi.core.stream.StreamJunction) SinkHandlerManager(io.siddhi.core.stream.output.sink.SinkHandlerManager) RecordTableHandlerManager(io.siddhi.core.table.record.RecordTableHandlerManager)

Example 10 with StreamJunction

use of io.siddhi.core.stream.StreamJunction in project siddhi by wso2.

the class InputManager method constructInputHandler.

public synchronized InputHandler constructInputHandler(String streamId) {
    InputHandler inputHandler = new InputHandler(streamId, inputHandlerMap.size(), inputEntryValve, siddhiAppContext);
    StreamJunction streamJunction = streamJunctionMap.get(streamId);
    if (streamJunction == null) {
        throw new DefinitionNotExistException("Stream with stream ID " + streamId + " has not been defined");
    }
    inputDistributor.addInputProcessor(streamJunctionMap.get(streamId).constructPublisher());
    inputHandlerMap.put(streamId, inputHandler);
    return inputHandler;
}
Also used : StreamJunction(io.siddhi.core.stream.StreamJunction) DefinitionNotExistException(io.siddhi.core.exception.DefinitionNotExistException)

Aggregations

StreamJunction (io.siddhi.core.stream.StreamJunction)12 InsertIntoStreamCallback (io.siddhi.core.query.output.callback.InsertIntoStreamCallback)3 DefinitionNotExistException (io.siddhi.core.exception.DefinitionNotExistException)2 SiddhiAppCreationException (io.siddhi.core.exception.SiddhiAppCreationException)2 QueryRuntime (io.siddhi.core.query.QueryRuntime)2 StreamRuntime (io.siddhi.core.query.input.stream.StreamRuntime)2 InsertIntoWindowCallback (io.siddhi.core.query.output.callback.InsertIntoWindowCallback)2 Sink (io.siddhi.core.stream.output.sink.Sink)2 Table (io.siddhi.core.table.Table)2 ExternalReferencedHolder (io.siddhi.core.util.extension.holder.ExternalReferencedHolder)2 StreamDefinition (io.siddhi.query.api.definition.StreamDefinition)2 InsertIntoStream (io.siddhi.query.api.execution.query.output.stream.InsertIntoStream)2 AggregationRuntime (io.siddhi.core.aggregation.AggregationRuntime)1 SiddhiAppRuntimeException (io.siddhi.core.exception.SiddhiAppRuntimeException)1 QueryRuntimeImpl (io.siddhi.core.query.QueryRuntimeImpl)1 ProcessStreamReceiver (io.siddhi.core.query.input.ProcessStreamReceiver)1 SingleStreamRuntime (io.siddhi.core.query.input.stream.single.SingleStreamRuntime)1 OutputCallback (io.siddhi.core.query.output.callback.OutputCallback)1 Source (io.siddhi.core.stream.input.source.Source)1 SourceHandlerManager (io.siddhi.core.stream.input.source.SourceHandlerManager)1