Search in sources :

Example 1 with QuerySelector

use of org.wso2.siddhi.core.query.selector.QuerySelector in project siddhi by wso2.

the class StateMultiProcessStreamReceiver method processAndClear.

protected void processAndClear(int processIndex, StreamEvent streamEvent) {
    ComplexEventChunk<StateEvent> retEventChunk = new ComplexEventChunk<StateEvent>(batchProcessingAllowed);
    ComplexEventChunk<StreamEvent> currentStreamEventChunk = new ComplexEventChunk<StreamEvent>(streamEvent, streamEvent, batchProcessingAllowed);
    ComplexEventChunk<StateEvent> eventChunk = ((StreamPreStateProcessor) nextProcessors[processIndex]).processAndReturn(currentStreamEventChunk);
    if (eventChunk.getFirst() != null) {
        retEventChunk.add(eventChunk.getFirst());
    }
    eventChunk.clear();
    if (querySelector != null) {
        while (retEventChunk.hasNext()) {
            StateEvent stateEvent = retEventChunk.next();
            retEventChunk.remove();
            querySelector.process(new ComplexEventChunk<StateEvent>(stateEvent, stateEvent, batchProcessingAllowed));
        }
    }
}
Also used : ComplexEventChunk(org.wso2.siddhi.core.event.ComplexEventChunk) StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent) StateEvent(org.wso2.siddhi.core.event.state.StateEvent) StreamPreStateProcessor(org.wso2.siddhi.core.query.input.stream.state.StreamPreStateProcessor)

Example 2 with QuerySelector

use of org.wso2.siddhi.core.query.selector.QuerySelector in project siddhi by wso2.

the class SingleStreamRuntime method clone.

@Override
public StreamRuntime clone(String key) {
    ProcessStreamReceiver clonedProcessStreamReceiver = this.processStreamReceiver.clone(key);
    EntryValveProcessor entryValveProcessor = null;
    SchedulingProcessor schedulingProcessor;
    Processor clonedProcessorChain = null;
    if (processorChain != null) {
        if (!(processorChain instanceof QuerySelector || processorChain instanceof OutputRateLimiter)) {
            clonedProcessorChain = processorChain.cloneProcessor(key);
            if (clonedProcessorChain instanceof EntryValveProcessor) {
                entryValveProcessor = (EntryValveProcessor) clonedProcessorChain;
            }
        }
        Processor processor = processorChain.getNextProcessor();
        while (processor != null) {
            if (!(processor instanceof QuerySelector || processor instanceof OutputRateLimiter)) {
                Processor clonedProcessor = processor.cloneProcessor(key);
                clonedProcessorChain.setToLast(clonedProcessor);
                if (clonedProcessor instanceof EntryValveProcessor) {
                    entryValveProcessor = (EntryValveProcessor) clonedProcessor;
                } else if (clonedProcessor instanceof SchedulingProcessor) {
                    schedulingProcessor = (SchedulingProcessor) clonedProcessor;
                    schedulingProcessor.setScheduler(((SchedulingProcessor) processor).getScheduler().clone(key, entryValveProcessor));
                }
            }
            processor = processor.getNextProcessor();
        }
    }
    return new SingleStreamRuntime(clonedProcessStreamReceiver, clonedProcessorChain, metaComplexEvent);
}
Also used : ProcessStreamReceiver(org.wso2.siddhi.core.query.input.ProcessStreamReceiver) SchedulingProcessor(org.wso2.siddhi.core.query.processor.SchedulingProcessor) Processor(org.wso2.siddhi.core.query.processor.Processor) SchedulingProcessor(org.wso2.siddhi.core.query.processor.SchedulingProcessor) OutputRateLimiter(org.wso2.siddhi.core.query.output.ratelimit.OutputRateLimiter) QuerySelector(org.wso2.siddhi.core.query.selector.QuerySelector)

Example 3 with QuerySelector

use of org.wso2.siddhi.core.query.selector.QuerySelector in project siddhi by wso2.

the class QuerySelector method clone.

public QuerySelector clone(String key) {
    QuerySelector clonedQuerySelector = new QuerySelector(id + key, selector, currentOn, expiredOn, siddhiAppContext);
    List<AttributeProcessor> clonedAttributeProcessorList = new ArrayList<AttributeProcessor>();
    for (AttributeProcessor attributeProcessor : attributeProcessorList) {
        clonedAttributeProcessorList.add(attributeProcessor.cloneProcessor(key));
    }
    clonedQuerySelector.attributeProcessorList = clonedAttributeProcessorList;
    clonedQuerySelector.isGroupBy = isGroupBy;
    clonedQuerySelector.containsAggregator = containsAggregator;
    clonedQuerySelector.groupByKeyGenerator = groupByKeyGenerator;
    clonedQuerySelector.havingConditionExecutor = havingConditionExecutor;
    clonedQuerySelector.eventPopulator = eventPopulator;
    clonedQuerySelector.batchingEnabled = batchingEnabled;
    clonedQuerySelector.isOrderBy = isOrderBy;
    clonedQuerySelector.orderByEventComparator = orderByEventComparator;
    clonedQuerySelector.limit = limit;
    return clonedQuerySelector;
}
Also used : ArrayList(java.util.ArrayList) AttributeProcessor(org.wso2.siddhi.core.query.selector.attribute.processor.AttributeProcessor)

Example 4 with QuerySelector

use of org.wso2.siddhi.core.query.selector.QuerySelector in project siddhi by wso2.

the class StoreQueryParser method populateFindStoreQueryRuntime.

private static void populateFindStoreQueryRuntime(FindStoreQueryRuntime findStoreQueryRuntime, MatchingMetaInfoHolder metaStreamInfoHolder, Selector selector, List<VariableExpressionExecutor> variableExpressionExecutors, SiddhiAppContext siddhiAppContext, Map<String, Table> tableMap, String queryName, int metaPosition) {
    QuerySelector querySelector = SelectorParser.parse(selector, new ReturnStream(OutputStream.OutputEventType.CURRENT_EVENTS), siddhiAppContext, metaStreamInfoHolder.getMetaStateEvent(), tableMap, variableExpressionExecutors, queryName, metaPosition);
    QueryParserHelper.reduceMetaComplexEvent(metaStreamInfoHolder.getMetaStateEvent());
    QueryParserHelper.updateVariablePosition(metaStreamInfoHolder.getMetaStateEvent(), variableExpressionExecutors);
    querySelector.setEventPopulator(StateEventPopulatorFactory.constructEventPopulator(metaStreamInfoHolder.getMetaStateEvent()));
    findStoreQueryRuntime.setStateEventPool(new StateEventPool(metaStreamInfoHolder.getMetaStateEvent(), 5));
    findStoreQueryRuntime.setSelector(querySelector);
    findStoreQueryRuntime.setOutputAttributes(metaStreamInfoHolder.getMetaStateEvent().getOutputStreamDefinition().getAttributeList());
}
Also used : StateEventPool(org.wso2.siddhi.core.event.state.StateEventPool) QuerySelector(org.wso2.siddhi.core.query.selector.QuerySelector) ReturnStream(org.wso2.siddhi.query.api.execution.query.output.stream.ReturnStream)

Example 5 with QuerySelector

use of org.wso2.siddhi.core.query.selector.QuerySelector in project siddhi by wso2.

the class QueryParser method parse.

/**
 * Parse a query and return corresponding QueryRuntime.
 *
 * @param query                    query to be parsed.
 * @param siddhiAppContext         associated Siddhi app context.
 * @param streamDefinitionMap      keyvalue containing user given stream definitions.
 * @param tableDefinitionMap       keyvalue containing table definitions.
 * @param windowDefinitionMap      keyvalue containing window definition map.
 * @param aggregationDefinitionMap keyvalue containing aggregation definition map.
 * @param tableMap                 keyvalue containing event tables.
 * @param aggregationMap           keyvalue containing aggrigation runtimes.
 * @param windowMap                keyvalue containing event window map.
 * @param lockSynchronizer         Lock synchronizer for sync the lock across queries.
 * @param queryIndex               query index to identify unknown query by number
 * @return queryRuntime
 */
public static QueryRuntime parse(Query query, SiddhiAppContext siddhiAppContext, Map<String, AbstractDefinition> streamDefinitionMap, Map<String, AbstractDefinition> tableDefinitionMap, Map<String, AbstractDefinition> windowDefinitionMap, Map<String, AbstractDefinition> aggregationDefinitionMap, Map<String, Table> tableMap, Map<String, AggregationRuntime> aggregationMap, Map<String, Window> windowMap, LockSynchronizer lockSynchronizer, String queryIndex) {
    List<VariableExpressionExecutor> executors = new ArrayList<VariableExpressionExecutor>();
    QueryRuntime queryRuntime;
    Element nameElement = null;
    LatencyTracker latencyTracker = null;
    LockWrapper lockWrapper = null;
    try {
        nameElement = AnnotationHelper.getAnnotationElement("info", "name", query.getAnnotations());
        String queryName = null;
        if (nameElement != null) {
            queryName = nameElement.getValue();
        } else {
            queryName = "query_" + queryIndex + "_" + UUID.randomUUID().toString();
        }
        latencyTracker = QueryParserHelper.createLatencyTracker(siddhiAppContext, queryName, SiddhiConstants.METRIC_INFIX_QUERIES, null);
        OutputStream.OutputEventType outputEventType = query.getOutputStream().getOutputEventType();
        boolean outputExpectsExpiredEvents = false;
        if (outputEventType != OutputStream.OutputEventType.CURRENT_EVENTS) {
            outputExpectsExpiredEvents = true;
        }
        StreamRuntime streamRuntime = InputStreamParser.parse(query.getInputStream(), siddhiAppContext, streamDefinitionMap, tableDefinitionMap, windowDefinitionMap, aggregationDefinitionMap, tableMap, windowMap, aggregationMap, executors, latencyTracker, outputExpectsExpiredEvents, queryName);
        QuerySelector selector = SelectorParser.parse(query.getSelector(), query.getOutputStream(), siddhiAppContext, streamRuntime.getMetaComplexEvent(), tableMap, executors, queryName, SiddhiConstants.UNKNOWN_STATE);
        boolean isWindow = query.getInputStream() instanceof JoinInputStream;
        if (!isWindow && query.getInputStream() instanceof SingleInputStream) {
            for (StreamHandler streamHandler : ((SingleInputStream) query.getInputStream()).getStreamHandlers()) {
                if (streamHandler instanceof org.wso2.siddhi.query.api.execution.query.input.handler.Window) {
                    isWindow = true;
                    break;
                }
            }
        }
        Element synchronizedElement = AnnotationHelper.getAnnotationElement("synchronized", null, query.getAnnotations());
        if (synchronizedElement != null) {
            if (!("false".equalsIgnoreCase(synchronizedElement.getValue()))) {
                // Query LockWrapper does not need a unique
                lockWrapper = new LockWrapper("");
                // id since it will
                // not be passed to the LockSynchronizer.
                // LockWrapper does not have a default lock
                lockWrapper.setLock(new ReentrantLock());
            }
        } else {
            if (isWindow || !(streamRuntime instanceof SingleStreamRuntime)) {
                if (streamRuntime instanceof JoinStreamRuntime) {
                    // If at least one Window is involved in the join, use the LockWrapper of that window
                    // for the query as well.
                    // If join is between two EventWindows, sync the locks of the LockWrapper of those windows
                    // and use either of them for query.
                    MetaStateEvent metaStateEvent = (MetaStateEvent) streamRuntime.getMetaComplexEvent();
                    MetaStreamEvent[] metaStreamEvents = metaStateEvent.getMetaStreamEvents();
                    if (metaStreamEvents[0].getEventType() == EventType.WINDOW && metaStreamEvents[1].getEventType() == EventType.WINDOW) {
                        LockWrapper leftLockWrapper = windowMap.get(metaStreamEvents[0].getLastInputDefinition().getId()).getLock();
                        LockWrapper rightLockWrapper = windowMap.get(metaStreamEvents[1].getLastInputDefinition().getId()).getLock();
                        if (!leftLockWrapper.equals(rightLockWrapper)) {
                            // Sync the lock across both wrappers
                            lockSynchronizer.sync(leftLockWrapper, rightLockWrapper);
                        }
                        // Can use either leftLockWrapper or rightLockWrapper since both of them will hold the
                        // same lock internally
                        // If either of their lock is updated later, the other lock also will be update by the
                        // LockSynchronizer.
                        lockWrapper = leftLockWrapper;
                    } else if (metaStreamEvents[0].getEventType() == EventType.WINDOW) {
                        // Share the same wrapper as the query lock wrapper
                        lockWrapper = windowMap.get(metaStreamEvents[0].getLastInputDefinition().getId()).getLock();
                    } else if (metaStreamEvents[1].getEventType() == EventType.WINDOW) {
                        // Share the same wrapper as the query lock wrapper
                        lockWrapper = windowMap.get(metaStreamEvents[1].getLastInputDefinition().getId()).getLock();
                    } else {
                        // Join does not contain any Window
                        // Query LockWrapper does not need a unique
                        lockWrapper = new LockWrapper("");
                        // id since
                        // it will not be passed to the LockSynchronizer.
                        // LockWrapper does not have a default lock
                        lockWrapper.setLock(new ReentrantLock());
                    }
                } else {
                    lockWrapper = new LockWrapper("");
                    lockWrapper.setLock(new ReentrantLock());
                }
            }
        }
        OutputRateLimiter outputRateLimiter = OutputParser.constructOutputRateLimiter(query.getOutputStream().getId(), query.getOutputRate(), query.getSelector().getGroupByList().size() != 0, isWindow, siddhiAppContext.getScheduledExecutorService(), siddhiAppContext, queryName);
        if (outputRateLimiter instanceof WrappedSnapshotOutputRateLimiter) {
            selector.setBatchingEnabled(false);
        }
        siddhiAppContext.addEternalReferencedHolder(outputRateLimiter);
        OutputCallback outputCallback = OutputParser.constructOutputCallback(query.getOutputStream(), streamRuntime.getMetaComplexEvent().getOutputStreamDefinition(), tableMap, windowMap, siddhiAppContext, !(streamRuntime instanceof SingleStreamRuntime), queryName);
        QueryParserHelper.reduceMetaComplexEvent(streamRuntime.getMetaComplexEvent());
        QueryParserHelper.updateVariablePosition(streamRuntime.getMetaComplexEvent(), executors);
        QueryParserHelper.initStreamRuntime(streamRuntime, streamRuntime.getMetaComplexEvent(), lockWrapper, queryName);
        selector.setEventPopulator(StateEventPopulatorFactory.constructEventPopulator(streamRuntime.getMetaComplexEvent()));
        queryRuntime = new QueryRuntime(query, siddhiAppContext, streamRuntime, selector, outputRateLimiter, outputCallback, streamRuntime.getMetaComplexEvent(), lockWrapper != null, queryName);
        if (outputRateLimiter instanceof WrappedSnapshotOutputRateLimiter) {
            selector.setBatchingEnabled(false);
            ((WrappedSnapshotOutputRateLimiter) outputRateLimiter).init(streamRuntime.getMetaComplexEvent().getOutputStreamDefinition().getAttributeList().size(), selector.getAttributeProcessorList(), streamRuntime.getMetaComplexEvent());
        }
        outputRateLimiter.init(siddhiAppContext, lockWrapper, queryName);
    } catch (DuplicateDefinitionException e) {
        if (nameElement != null) {
            throw new DuplicateDefinitionException(e.getMessageWithOutContext() + ", when creating query " + nameElement.getValue(), e, e.getQueryContextStartIndex(), e.getQueryContextEndIndex(), siddhiAppContext.getName(), siddhiAppContext.getSiddhiAppString());
        } else {
            throw new DuplicateDefinitionException(e.getMessage(), e, e.getQueryContextStartIndex(), e.getQueryContextEndIndex(), siddhiAppContext.getName(), siddhiAppContext.getSiddhiAppString());
        }
    } catch (Throwable t) {
        ExceptionUtil.populateQueryContext(t, query, siddhiAppContext);
        throw t;
    }
    return queryRuntime;
}
Also used : Element(org.wso2.siddhi.query.api.annotation.Element) OutputStream(org.wso2.siddhi.query.api.execution.query.output.stream.OutputStream) ArrayList(java.util.ArrayList) OutputCallback(org.wso2.siddhi.core.query.output.callback.OutputCallback) QueryRuntime(org.wso2.siddhi.core.query.QueryRuntime) JoinInputStream(org.wso2.siddhi.query.api.execution.query.input.stream.JoinInputStream) SingleInputStream(org.wso2.siddhi.query.api.execution.query.input.stream.SingleInputStream) WrappedSnapshotOutputRateLimiter(org.wso2.siddhi.core.query.output.ratelimit.snapshot.WrappedSnapshotOutputRateLimiter) OutputRateLimiter(org.wso2.siddhi.core.query.output.ratelimit.OutputRateLimiter) StreamHandler(org.wso2.siddhi.query.api.execution.query.input.handler.StreamHandler) SingleStreamRuntime(org.wso2.siddhi.core.query.input.stream.single.SingleStreamRuntime) StreamRuntime(org.wso2.siddhi.core.query.input.stream.StreamRuntime) JoinStreamRuntime(org.wso2.siddhi.core.query.input.stream.join.JoinStreamRuntime) Window(org.wso2.siddhi.core.window.Window) ReentrantLock(java.util.concurrent.locks.ReentrantLock) DuplicateDefinitionException(org.wso2.siddhi.query.api.exception.DuplicateDefinitionException) SingleStreamRuntime(org.wso2.siddhi.core.query.input.stream.single.SingleStreamRuntime) JoinStreamRuntime(org.wso2.siddhi.core.query.input.stream.join.JoinStreamRuntime) VariableExpressionExecutor(org.wso2.siddhi.core.executor.VariableExpressionExecutor) QuerySelector(org.wso2.siddhi.core.query.selector.QuerySelector) LockWrapper(org.wso2.siddhi.core.util.lock.LockWrapper) MetaStateEvent(org.wso2.siddhi.core.event.state.MetaStateEvent) LatencyTracker(org.wso2.siddhi.core.util.statistics.LatencyTracker) WrappedSnapshotOutputRateLimiter(org.wso2.siddhi.core.query.output.ratelimit.snapshot.WrappedSnapshotOutputRateLimiter) MetaStreamEvent(org.wso2.siddhi.core.event.stream.MetaStreamEvent)

Aggregations

QuerySelector (org.wso2.siddhi.core.query.selector.QuerySelector)5 OutputRateLimiter (org.wso2.siddhi.core.query.output.ratelimit.OutputRateLimiter)3 ArrayList (java.util.ArrayList)2 ReentrantLock (java.util.concurrent.locks.ReentrantLock)2 VariableExpressionExecutor (org.wso2.siddhi.core.executor.VariableExpressionExecutor)2 StreamRuntime (org.wso2.siddhi.core.query.input.stream.StreamRuntime)2 OutputCallback (org.wso2.siddhi.core.query.output.callback.OutputCallback)2 AttributeProcessor (org.wso2.siddhi.core.query.selector.attribute.processor.AttributeProcessor)2 LockWrapper (org.wso2.siddhi.core.util.lock.LockWrapper)2 ComplexEventChunk (org.wso2.siddhi.core.event.ComplexEventChunk)1 MetaStateEvent (org.wso2.siddhi.core.event.state.MetaStateEvent)1 StateEvent (org.wso2.siddhi.core.event.state.StateEvent)1 StateEventPool (org.wso2.siddhi.core.event.state.StateEventPool)1 MetaStreamEvent (org.wso2.siddhi.core.event.stream.MetaStreamEvent)1 StreamEvent (org.wso2.siddhi.core.event.stream.StreamEvent)1 ConstantExpressionExecutor (org.wso2.siddhi.core.executor.ConstantExpressionExecutor)1 ExpressionExecutor (org.wso2.siddhi.core.executor.ExpressionExecutor)1 ConditionExpressionExecutor (org.wso2.siddhi.core.executor.condition.ConditionExpressionExecutor)1 QueryRuntime (org.wso2.siddhi.core.query.QueryRuntime)1 ProcessStreamReceiver (org.wso2.siddhi.core.query.input.ProcessStreamReceiver)1