Search in sources :

Example 16 with StreamJunction

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

the class SiddhiAppRuntime 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().getElementId());
                }
                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().getElementId());
                }
                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.getElementId();
            }
            if (elementId != null) {
                recordTableHandlerManager.unregisterRecordTableHandler(elementId);
            }
        }
        table.shutdown();
    }
    for (EternalReferencedHolder eternalReferencedHolder : siddhiAppContext.getEternalReferencedHolders()) {
        try {
            eternalReferencedHolder.stop();
        } catch (Throwable t) {
            log.error(StringUtil.removeCRLFCharacters(ExceptionUtil.getMessageWithContext(t, siddhiAppContext)) + " Error while stopping EternalReferencedHolder '" + StringUtil.removeCRLFCharacters(eternalReferencedHolder.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.isStatsEnabled()) {
            siddhiAppContext.getStatisticsManager().stopReporting();
        }
        siddhiAppContext.getStatisticsManager().cleanup();
    }
    running = false;
}
Also used : Table(org.wso2.siddhi.core.table.Table) SourceHandlerManager(org.wso2.siddhi.core.stream.input.source.SourceHandlerManager) RecordTableHandler(org.wso2.siddhi.core.table.record.RecordTableHandler) EternalReferencedHolder(org.wso2.siddhi.core.util.extension.holder.EternalReferencedHolder) Source(org.wso2.siddhi.core.stream.input.source.Source) Sink(org.wso2.siddhi.core.stream.output.sink.Sink) StreamJunction(org.wso2.siddhi.core.stream.StreamJunction) SinkHandlerManager(org.wso2.siddhi.core.stream.output.sink.SinkHandlerManager) RecordTableHandlerManager(org.wso2.siddhi.core.table.record.RecordTableHandlerManager)

Example 17 with StreamJunction

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

the class QueryRuntime method clone.

public QueryRuntime clone(String key, ConcurrentMap<String, StreamJunction> localStreamJunctionMap) {
    LockWrapper lockWrapper = null;
    if (synchronised) {
        lockWrapper = new LockWrapper("");
        lockWrapper.setLock(new ReentrantLock());
    }
    StreamRuntime clonedStreamRuntime = this.streamRuntime.clone(key);
    QuerySelector clonedSelector = this.selector.clone(key);
    OutputRateLimiter clonedOutputRateLimiter = outputRateLimiter.clone(key);
    clonedOutputRateLimiter.init(siddhiAppContext, lockWrapper, queryId);
    QueryRuntime queryRuntime = new QueryRuntime(query, siddhiAppContext, clonedStreamRuntime, clonedSelector, clonedOutputRateLimiter, outputCallback, this.metaComplexEvent, synchronised, this.queryId + key);
    QueryParserHelper.initStreamRuntime(clonedStreamRuntime, metaComplexEvent, lockWrapper, queryId);
    queryRuntime.setToLocalStream(toLocalStream);
    if (!toLocalStream) {
        queryRuntime.outputRateLimiter.setOutputCallback(outputCallback);
        queryRuntime.outputCallback = this.outputCallback;
    } else {
        OutputCallback clonedQueryOutputCallback = OutputParser.constructOutputCallback(query.getOutputStream(), key, localStreamJunctionMap, outputStreamDefinition, siddhiAppContext, queryId);
        queryRuntime.outputRateLimiter.setOutputCallback(clonedQueryOutputCallback);
        queryRuntime.outputCallback = clonedQueryOutputCallback;
    }
    return queryRuntime;
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) OutputRateLimiter(org.wso2.siddhi.core.query.output.ratelimit.OutputRateLimiter) QuerySelector(org.wso2.siddhi.core.query.selector.QuerySelector) LockWrapper(org.wso2.siddhi.core.util.lock.LockWrapper) StreamRuntime(org.wso2.siddhi.core.query.input.stream.StreamRuntime) OutputCallback(org.wso2.siddhi.core.query.output.callback.OutputCallback)

Example 18 with StreamJunction

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

the class PartitionRuntime method clonePartition.

private synchronized void clonePartition(String key) {
    PartitionInstanceRuntime partitionInstance = this.partitionInstanceRuntimeMap.get(key);
    if (partitionInstance == null) {
        List<QueryRuntime> queryRuntimeList = new ArrayList<QueryRuntime>();
        List<QueryRuntime> partitionedQueryRuntimeList = new ArrayList<QueryRuntime>();
        for (QueryRuntime queryRuntime : metaQueryRuntimeMap.values()) {
            QueryRuntime clonedQueryRuntime = queryRuntime.clone(key, localStreamJunctionMap);
            queryRuntimeList.add(clonedQueryRuntime);
            QueryParserHelper.registerMemoryUsageTracking(clonedQueryRuntime.getQueryId(), queryRuntime, SiddhiConstants.METRIC_INFIX_QUERIES, siddhiAppContext, memoryUsageTracker);
            if (queryRuntime.isFromLocalStream()) {
                for (int i = 0; i < clonedQueryRuntime.getStreamRuntime().getSingleStreamRuntimes().size(); i++) {
                    String streamId = queryRuntime.getStreamRuntime().getSingleStreamRuntimes().get(i).getProcessStreamReceiver().getStreamId();
                    StreamDefinition streamDefinition;
                    if (streamId.startsWith("#")) {
                        streamDefinition = (StreamDefinition) localStreamDefinitionMap.get(streamId);
                    } else {
                        streamDefinition = (StreamDefinition) streamDefinitionMap.get(streamId);
                    }
                    StreamJunction streamJunction = localStreamJunctionMap.get(streamId + key);
                    if (streamJunction == null) {
                        streamJunction = new StreamJunction(streamDefinition, siddhiAppContext.getExecutorService(), siddhiAppContext.getBufferSize(), siddhiAppContext);
                        localStreamJunctionMap.put(streamId + key, streamJunction);
                    }
                    streamJunction.subscribe(clonedQueryRuntime.getStreamRuntime().getSingleStreamRuntimes().get(i).getProcessStreamReceiver());
                }
            } else {
                partitionedQueryRuntimeList.add(clonedQueryRuntime);
            }
        }
        partitionInstanceRuntimeMap.putIfAbsent(key, new PartitionInstanceRuntime(key, queryRuntimeList));
        updatePartitionStreamReceivers(key, partitionedQueryRuntimeList);
    }
}
Also used : StreamDefinition(org.wso2.siddhi.query.api.definition.StreamDefinition) QueryRuntime(org.wso2.siddhi.core.query.QueryRuntime) StreamJunction(org.wso2.siddhi.core.stream.StreamJunction) ArrayList(java.util.ArrayList)

Example 19 with StreamJunction

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

the class PartitionStreamReceiver method addStreamJunction.

/**
 * create local streamJunctions through which events received by partitionStreamReceiver, are sent to
 * queryStreamReceivers
 *
 * @param key              partitioning key
 * @param queryRuntimeList queryRuntime list of the partition
 */
public void addStreamJunction(String key, List<QueryRuntime> queryRuntimeList) {
    StreamJunction streamJunction = cachedStreamJunctionMap.get(streamId + key);
    if (streamJunction == null) {
        streamJunction = partitionRuntime.getLocalStreamJunctionMap().get(streamId + key);
        if (streamJunction == null) {
            streamJunction = createStreamJunction();
            partitionRuntime.addStreamJunction(streamId + key, streamJunction);
        }
        cachedStreamJunctionMap.put(streamId + key, streamJunction);
    }
    for (QueryRuntime queryRuntime : queryRuntimeList) {
        StreamRuntime streamRuntime = queryRuntime.getStreamRuntime();
        for (int i = 0; i < queryRuntime.getInputStreamId().size(); i++) {
            if ((streamRuntime.getSingleStreamRuntimes().get(i)).getProcessStreamReceiver().getStreamId().equals(streamId + key)) {
                streamJunction.subscribe((streamRuntime.getSingleStreamRuntimes().get(i)).getProcessStreamReceiver());
            }
        }
    }
}
Also used : QueryRuntime(org.wso2.siddhi.core.query.QueryRuntime) StreamJunction(org.wso2.siddhi.core.stream.StreamJunction) StreamRuntime(org.wso2.siddhi.core.query.input.stream.StreamRuntime)

Example 20 with StreamJunction

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

the class StreamJunction method sendEvent.

private void sendEvent(List<Event> events) {
    if (isTraceEnabled) {
        log.trace("Event is received by streamJunction " + this);
    }
    if (disruptor != null) {
        for (Event event : events) {
            // Todo : optimize for arrays
            long sequenceNo = ringBuffer.next();
            try {
                Event existingEvent = ringBuffer.get(sequenceNo);
                existingEvent.copyFrom(event);
            } finally {
                ringBuffer.publish(sequenceNo);
            }
        }
    } else {
        for (Receiver receiver : receivers) {
            receiver.receive(events.toArray(new Event[events.size()]));
        }
    }
}
Also used : Event(org.wso2.siddhi.core.event.Event) ComplexEvent(org.wso2.siddhi.core.event.ComplexEvent)

Aggregations

StreamJunction (org.wso2.siddhi.core.stream.StreamJunction)10 Event (org.wso2.siddhi.core.event.Event)8 StreamDefinition (org.wso2.siddhi.query.api.definition.StreamDefinition)8 Test (org.testng.annotations.Test)5 StreamEvent (org.wso2.siddhi.core.event.stream.StreamEvent)5 StreamCallback (org.wso2.siddhi.core.stream.output.StreamCallback)5 ComplexEvent (org.wso2.siddhi.core.event.ComplexEvent)4 QueryRuntime (org.wso2.siddhi.core.query.QueryRuntime)3 StreamRuntime (org.wso2.siddhi.core.query.input.stream.StreamRuntime)3 InsertIntoStreamCallback (org.wso2.siddhi.core.query.output.callback.InsertIntoStreamCallback)3 ArrayList (java.util.ArrayList)2 StreamEventPool (org.wso2.siddhi.core.event.stream.StreamEventPool)2 DefinitionNotExistException (org.wso2.siddhi.core.exception.DefinitionNotExistException)2 SiddhiAppCreationException (org.wso2.siddhi.core.exception.SiddhiAppCreationException)2 OutputCallback (org.wso2.siddhi.core.query.output.callback.OutputCallback)2 InsertIntoStream (org.wso2.siddhi.query.api.execution.query.output.stream.InsertIntoStream)2 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1 MetaStreamEvent (org.wso2.siddhi.core.event.stream.MetaStreamEvent)1 StreamEventCloner (org.wso2.siddhi.core.event.stream.StreamEventCloner)1 ProcessStreamReceiver (org.wso2.siddhi.core.query.input.ProcessStreamReceiver)1