Search in sources :

Example 1 with Window

use of io.siddhi.core.window.Window in project siddhi by wso2.

the class EventTestCase method testQueryParser.

@Test
public void testQueryParser() {
    StreamDefinition streamDefinition = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.FLOAT).attribute("volume", Attribute.Type.INT);
    StreamDefinition outStreamDefinition = StreamDefinition.id("outputStream").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.FLOAT);
    Query query = new Query();
    query.annotation(Annotation.annotation("info").element("name", "query1"));
    query.from(InputStream.stream("cseEventStream").filter(Expression.compare(Expression.variable("volume"), Compare.Operator.NOT_EQUAL, Expression.value(50))));
    query.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("price", Expression.variable("price")));
    query.insertInto("outputStream");
    Map<String, AbstractDefinition> tableDefinitionMap = new HashMap<>();
    Map<String, AbstractDefinition> windowDefinitionMap = new HashMap<>();
    Map<String, AbstractDefinition> aggregationDefinitionMap = new HashMap<>();
    Map<String, Table> tableMap = new HashMap<String, Table>();
    Map<String, Window> eventWindowMap = new HashMap<String, Window>();
    Map<String, AggregationRuntime> aggregationMap = new HashMap<String, AggregationRuntime>();
    Map<String, List<Source>> eventSourceMap = new HashMap<String, List<Source>>();
    Map<String, List<Sink>> eventSinkMap = new HashMap<String, List<Sink>>();
    Map<String, AbstractDefinition> streamDefinitionMap = new HashMap<String, AbstractDefinition>();
    LockSynchronizer lockSynchronizer = new LockSynchronizer();
    streamDefinitionMap.put("cseEventStream", streamDefinition);
    streamDefinitionMap.put("outputStream", outStreamDefinition);
    SiddhiContext siddhicontext = new SiddhiContext();
    SiddhiAppContext context = new SiddhiAppContext();
    context.setSiddhiContext(siddhicontext);
    context.setIdGenerator(new IdGenerator());
    context.setSnapshotService(new SnapshotService(context));
    QueryRuntimeImpl runtime = QueryParser.parse(query, context, streamDefinitionMap, tableDefinitionMap, windowDefinitionMap, aggregationDefinitionMap, tableMap, aggregationMap, eventWindowMap, lockSynchronizer, "1", false, SiddhiConstants.PARTITION_ID_DEFAULT);
    AssertJUnit.assertNotNull(runtime);
    AssertJUnit.assertTrue(runtime.getStreamRuntime() instanceof SingleStreamRuntime);
    AssertJUnit.assertNotNull(runtime.getSelector());
    AssertJUnit.assertTrue(runtime.getMetaComplexEvent() instanceof MetaStreamEvent);
}
Also used : Query(io.siddhi.query.api.execution.query.Query) HashMap(java.util.HashMap) Source(io.siddhi.core.stream.input.source.Source) SiddhiContext(io.siddhi.core.config.SiddhiContext) Sink(io.siddhi.core.stream.output.sink.Sink) LockSynchronizer(io.siddhi.core.util.lock.LockSynchronizer) List(java.util.List) AggregationRuntime(io.siddhi.core.aggregation.AggregationRuntime) Window(io.siddhi.core.window.Window) SnapshotService(io.siddhi.core.util.snapshot.SnapshotService) Table(io.siddhi.core.table.Table) StreamDefinition(io.siddhi.query.api.definition.StreamDefinition) SingleStreamRuntime(io.siddhi.core.query.input.stream.single.SingleStreamRuntime) AbstractDefinition(io.siddhi.query.api.definition.AbstractDefinition) IdGenerator(io.siddhi.core.util.IdGenerator) QueryRuntimeImpl(io.siddhi.core.query.QueryRuntimeImpl) SiddhiAppContext(io.siddhi.core.config.SiddhiAppContext) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent) Test(org.testng.annotations.Test)

Example 2 with Window

use of io.siddhi.core.window.Window 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
 * @param partitioned              is the query partitioned
 * @param partitionId              The ID of the partition
 * @return queryRuntime
 */
public static QueryRuntimeImpl 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, boolean partitioned, String partitionId) {
    List<VariableExpressionExecutor> executors = new ArrayList<>();
    QueryRuntimeImpl queryRuntime;
    Element nameElement = null;
    LatencyTracker latencyTracker = null;
    LockWrapper lockWrapper = null;
    try {
        nameElement = AnnotationHelper.getAnnotationElement("info", "name", query.getAnnotations());
        String queryName;
        if (nameElement != null) {
            queryName = nameElement.getValue();
        } else {
            queryName = "query_" + queryIndex;
        }
        SiddhiQueryContext siddhiQueryContext = new SiddhiQueryContext(siddhiAppContext, queryName, partitionId);
        siddhiQueryContext.setPartitioned(partitioned);
        latencyTracker = QueryParserHelper.createLatencyTracker(siddhiAppContext, siddhiQueryContext.getName(), SiddhiConstants.METRIC_INFIX_QUERIES, null);
        siddhiQueryContext.setLatencyTracker(latencyTracker);
        OutputStream.OutputEventType outputEventType = query.getOutputStream().getOutputEventType();
        if (query.getOutputRate() != null && query.getOutputRate() instanceof SnapshotOutputRate) {
            if (outputEventType != OutputStream.OutputEventType.ALL_EVENTS) {
                throw new SiddhiAppCreationException("As query '" + siddhiQueryContext.getName() + "' is performing snapshot rate limiting, it can only insert '" + OutputStream.OutputEventType.ALL_EVENTS + "' but it is inserting '" + outputEventType + "'!", query.getOutputStream().getQueryContextStartIndex(), query.getOutputStream().getQueryContextEndIndex());
            }
        }
        siddhiQueryContext.setOutputEventType(outputEventType);
        boolean outputExpectsExpiredEvents = false;
        if (outputEventType != OutputStream.OutputEventType.CURRENT_EVENTS) {
            outputExpectsExpiredEvents = true;
        }
        StreamRuntime streamRuntime = InputStreamParser.parse(query.getInputStream(), query, streamDefinitionMap, tableDefinitionMap, windowDefinitionMap, aggregationDefinitionMap, tableMap, windowMap, aggregationMap, executors, outputExpectsExpiredEvents, siddhiQueryContext);
        QuerySelector selector;
        if (streamRuntime.getQuerySelector() != null) {
            selector = streamRuntime.getQuerySelector();
        } else {
            selector = SelectorParser.parse(query.getSelector(), query.getOutputStream(), streamRuntime.getMetaComplexEvent(), tableMap, executors, SiddhiConstants.UNKNOWN_STATE, streamRuntime.getProcessingMode(), outputExpectsExpiredEvents, siddhiQueryContext);
        }
        boolean isWindow = query.getInputStream() instanceof JoinInputStream;
        if (!isWindow && query.getInputStream() instanceof SingleInputStream) {
            for (StreamHandler streamHandler : ((SingleInputStream) query.getInputStream()).getStreamHandlers()) {
                if (streamHandler instanceof io.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, siddhiQueryContext);
        if (outputRateLimiter instanceof WrappedSnapshotOutputRateLimiter) {
            selector.setBatchingEnabled(false);
        }
        boolean groupBy = !query.getSelector().getGroupByList().isEmpty();
        OutputCallback outputCallback = OutputParser.constructOutputCallback(query.getOutputStream(), streamRuntime.getMetaComplexEvent().getOutputStreamDefinition(), tableMap, windowMap, !(streamRuntime instanceof SingleStreamRuntime) || groupBy, siddhiQueryContext);
        QueryParserHelper.reduceMetaComplexEvent(streamRuntime.getMetaComplexEvent());
        QueryParserHelper.updateVariablePosition(streamRuntime.getMetaComplexEvent(), executors);
        QueryParserHelper.initStreamRuntime(streamRuntime, streamRuntime.getMetaComplexEvent(), lockWrapper, siddhiQueryContext.getName());
        // Update cache compile selection variable expression executors
        if (streamRuntime instanceof JoinStreamRuntime) {
            streamRuntime.getSingleStreamRuntimes().forEach((singleStreamRuntime -> {
                Processor processorChain = singleStreamRuntime.getProcessorChain();
                if (processorChain instanceof JoinProcessor) {
                    CompiledSelection compiledSelection = ((JoinProcessor) processorChain).getCompiledSelection();
                    if (compiledSelection instanceof AbstractQueryableRecordTable.CompiledSelectionWithCache) {
                        List<VariableExpressionExecutor> variableExpressionExecutors = ((AbstractQueryableRecordTable.CompiledSelectionWithCache) compiledSelection).getVariableExpressionExecutorsForQuerySelector();
                        QueryParserHelper.updateVariablePosition(streamRuntime.getMetaComplexEvent(), variableExpressionExecutors);
                    }
                }
            }));
        }
        selector.setEventPopulator(StateEventPopulatorFactory.constructEventPopulator(streamRuntime.getMetaComplexEvent()));
        queryRuntime = new QueryRuntimeImpl(query, streamRuntime, selector, outputRateLimiter, outputCallback, streamRuntime.getMetaComplexEvent(), siddhiQueryContext);
        if (outputRateLimiter instanceof WrappedSnapshotOutputRateLimiter) {
            selector.setBatchingEnabled(false);
            ((WrappedSnapshotOutputRateLimiter) outputRateLimiter).init(streamRuntime.getMetaComplexEvent().getOutputStreamDefinition().getAttributeList().size(), selector.getAttributeProcessorList(), streamRuntime.getMetaComplexEvent());
        }
        outputRateLimiter.init(lockWrapper, groupBy, siddhiQueryContext);
    } 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 : DuplicateDefinitionException(io.siddhi.query.api.exception.DuplicateDefinitionException) MetaStateEvent(io.siddhi.core.event.state.MetaStateEvent) AnnotationHelper(io.siddhi.query.api.util.AnnotationHelper) QueryRuntimeImpl(io.siddhi.core.query.QueryRuntimeImpl) OutputCallback(io.siddhi.core.query.output.callback.OutputCallback) SiddhiAppContext(io.siddhi.core.config.SiddhiAppContext) SiddhiAppCreationException(io.siddhi.core.exception.SiddhiAppCreationException) SingleInputStream(io.siddhi.query.api.execution.query.input.stream.SingleInputStream) VariableExpressionExecutor(io.siddhi.core.executor.VariableExpressionExecutor) SiddhiConstants(io.siddhi.core.util.SiddhiConstants) CompiledSelection(io.siddhi.core.util.collection.operator.CompiledSelection) QuerySelector(io.siddhi.core.query.selector.QuerySelector) ArrayList(java.util.ArrayList) StreamRuntime(io.siddhi.core.query.input.stream.StreamRuntime) AbstractDefinition(io.siddhi.query.api.definition.AbstractDefinition) Query(io.siddhi.query.api.execution.query.Query) Table(io.siddhi.core.table.Table) LockSynchronizer(io.siddhi.core.util.lock.LockSynchronizer) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent) AggregationRuntime(io.siddhi.core.aggregation.AggregationRuntime) Map(java.util.Map) SiddhiQueryContext(io.siddhi.core.config.SiddhiQueryContext) SingleStreamRuntime(io.siddhi.core.query.input.stream.single.SingleStreamRuntime) WrappedSnapshotOutputRateLimiter(io.siddhi.core.query.output.ratelimit.snapshot.WrappedSnapshotOutputRateLimiter) LockWrapper(io.siddhi.core.util.lock.LockWrapper) ReentrantLock(java.util.concurrent.locks.ReentrantLock) QueryRuntime(io.siddhi.core.query.QueryRuntime) LatencyTracker(io.siddhi.core.util.statistics.LatencyTracker) EventType(io.siddhi.core.event.stream.MetaStreamEvent.EventType) OutputRateLimiter(io.siddhi.core.query.output.ratelimit.OutputRateLimiter) ExceptionUtil(io.siddhi.core.util.ExceptionUtil) StateEventPopulatorFactory(io.siddhi.core.event.state.populater.StateEventPopulatorFactory) Element(io.siddhi.query.api.annotation.Element) AbstractQueryableRecordTable(io.siddhi.core.table.record.AbstractQueryableRecordTable) List(java.util.List) JoinStreamRuntime(io.siddhi.core.query.input.stream.join.JoinStreamRuntime) OutputStream(io.siddhi.query.api.execution.query.output.stream.OutputStream) SnapshotOutputRate(io.siddhi.query.api.execution.query.output.ratelimit.SnapshotOutputRate) Processor(io.siddhi.core.query.processor.Processor) Window(io.siddhi.core.window.Window) QueryParserHelper(io.siddhi.core.util.parser.helper.QueryParserHelper) JoinProcessor(io.siddhi.core.query.input.stream.join.JoinProcessor) JoinInputStream(io.siddhi.query.api.execution.query.input.stream.JoinInputStream) StreamHandler(io.siddhi.query.api.execution.query.input.handler.StreamHandler) Processor(io.siddhi.core.query.processor.Processor) JoinProcessor(io.siddhi.core.query.input.stream.join.JoinProcessor) Element(io.siddhi.query.api.annotation.Element) OutputStream(io.siddhi.query.api.execution.query.output.stream.OutputStream) ArrayList(java.util.ArrayList) OutputCallback(io.siddhi.core.query.output.callback.OutputCallback) SiddhiQueryContext(io.siddhi.core.config.SiddhiQueryContext) AbstractQueryableRecordTable(io.siddhi.core.table.record.AbstractQueryableRecordTable) JoinInputStream(io.siddhi.query.api.execution.query.input.stream.JoinInputStream) SingleInputStream(io.siddhi.query.api.execution.query.input.stream.SingleInputStream) WrappedSnapshotOutputRateLimiter(io.siddhi.core.query.output.ratelimit.snapshot.WrappedSnapshotOutputRateLimiter) OutputRateLimiter(io.siddhi.core.query.output.ratelimit.OutputRateLimiter) StreamHandler(io.siddhi.query.api.execution.query.input.handler.StreamHandler) ArrayList(java.util.ArrayList) List(java.util.List) StreamRuntime(io.siddhi.core.query.input.stream.StreamRuntime) SingleStreamRuntime(io.siddhi.core.query.input.stream.single.SingleStreamRuntime) JoinStreamRuntime(io.siddhi.core.query.input.stream.join.JoinStreamRuntime) JoinProcessor(io.siddhi.core.query.input.stream.join.JoinProcessor) Window(io.siddhi.core.window.Window) ReentrantLock(java.util.concurrent.locks.ReentrantLock) DuplicateDefinitionException(io.siddhi.query.api.exception.DuplicateDefinitionException) SiddhiAppCreationException(io.siddhi.core.exception.SiddhiAppCreationException) SingleStreamRuntime(io.siddhi.core.query.input.stream.single.SingleStreamRuntime) JoinStreamRuntime(io.siddhi.core.query.input.stream.join.JoinStreamRuntime) VariableExpressionExecutor(io.siddhi.core.executor.VariableExpressionExecutor) QuerySelector(io.siddhi.core.query.selector.QuerySelector) LockWrapper(io.siddhi.core.util.lock.LockWrapper) MetaStateEvent(io.siddhi.core.event.state.MetaStateEvent) SnapshotOutputRate(io.siddhi.query.api.execution.query.output.ratelimit.SnapshotOutputRate) QueryRuntimeImpl(io.siddhi.core.query.QueryRuntimeImpl) CompiledSelection(io.siddhi.core.util.collection.operator.CompiledSelection) LatencyTracker(io.siddhi.core.util.statistics.LatencyTracker) WrappedSnapshotOutputRateLimiter(io.siddhi.core.query.output.ratelimit.snapshot.WrappedSnapshotOutputRateLimiter) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent)

Example 3 with Window

use of io.siddhi.core.window.Window in project siddhi by wso2.

the class SiddhiAppParser method parse.

/**
 * Parse an SiddhiApp returning SiddhiAppRuntime
 *
 * @param siddhiApp       plan to be parsed
 * @param siddhiAppString content of Siddhi application as string
 * @param siddhiContext   SiddhiContext  @return SiddhiAppRuntime
 * @return SiddhiAppRuntimeBuilder
 */
public static SiddhiAppRuntimeBuilder parse(SiddhiApp siddhiApp, String siddhiAppString, SiddhiContext siddhiContext) {
    SiddhiAppContext siddhiAppContext = new SiddhiAppContext();
    siddhiAppContext.setSiddhiContext(siddhiContext);
    siddhiAppContext.setSiddhiAppString(siddhiAppString);
    siddhiAppContext.setSiddhiApp(siddhiApp);
    try {
        Element element = AnnotationHelper.getAnnotationElement(SiddhiConstants.ANNOTATION_NAME, null, siddhiApp.getAnnotations());
        if (element != null) {
            siddhiAppContext.setName(element.getValue());
        } else {
            siddhiAppContext.setName(UUID.randomUUID().toString());
        }
        Annotation annotation = AnnotationHelper.getAnnotation(SiddhiConstants.ANNOTATION_ENFORCE_ORDER, siddhiApp.getAnnotations());
        if (annotation != null) {
            siddhiAppContext.setEnforceOrder(true);
        }
        annotation = AnnotationHelper.getAnnotation(SiddhiConstants.ANNOTATION_ASYNC, siddhiApp.getAnnotations());
        if (annotation != null) {
            throw new SiddhiAppCreationException("@Async not supported in SiddhiApp level, " + "instead use @Async with streams", annotation.getQueryContextStartIndex(), annotation.getQueryContextEndIndex());
        }
        annotation = AnnotationHelper.getAnnotation(SiddhiConstants.ANNOTATION_STATISTICS, siddhiApp.getAnnotations());
        List<Element> statisticsElements = new ArrayList<>();
        if (annotation != null) {
            statisticsElements = annotation.getElements();
        }
        if (siddhiContext.getStatisticsConfiguration() != null) {
            siddhiAppContext.setStatisticsManager(siddhiContext.getStatisticsConfiguration().getFactory().createStatisticsManager(siddhiContext.getStatisticsConfiguration().getMetricPrefix(), siddhiAppContext.getName(), statisticsElements));
        }
        Element statStateEnableElement = AnnotationHelper.getAnnotationElement(SiddhiConstants.ANNOTATION_STATISTICS, SiddhiConstants.ANNOTATION_ELEMENT_ENABLE, siddhiApp.getAnnotations());
        if (statStateEnableElement != null && Boolean.valueOf(statStateEnableElement.getValue())) {
            siddhiAppContext.setRootMetricsLevel(Level.BASIC);
        } else {
            Element statStateElement = AnnotationHelper.getAnnotationElement(SiddhiConstants.ANNOTATION_STATISTICS, null, siddhiApp.getAnnotations());
            // where sp uses @app:statistics('true').
            if (annotation != null && (statStateElement == null || Boolean.valueOf(statStateElement.getValue()))) {
                siddhiAppContext.setRootMetricsLevel(Level.BASIC);
            }
        }
        Element statStateIncludeElement = AnnotationHelper.getAnnotationElement(SiddhiConstants.ANNOTATION_STATISTICS, SiddhiConstants.ANNOTATION_ELEMENT_INCLUDE, siddhiApp.getAnnotations());
        siddhiAppContext.setIncludedMetrics(io.siddhi.core.util.parser.helper.AnnotationHelper.generateIncludedMetrics(statStateIncludeElement));
        Element transportCreationEnabledElement = AnnotationHelper.getAnnotationElement(SiddhiConstants.TRANSPORT_CHANNEL_CREATION_IDENTIFIER, null, siddhiApp.getAnnotations());
        if (transportCreationEnabledElement == null) {
            siddhiAppContext.setTransportChannelCreationEnabled(true);
        } else {
            siddhiAppContext.setTransportChannelCreationEnabled(Boolean.valueOf(transportCreationEnabledElement.getValue()));
        }
        siddhiAppContext.setThreadBarrier(new ThreadBarrier());
        siddhiAppContext.setExecutorService(Executors.newCachedThreadPool(new ThreadFactoryBuilder().setNameFormat("Siddhi-" + siddhiAppContext.getName() + "-executor-thread-%d").build()));
        siddhiAppContext.setScheduledExecutorService(Executors.newScheduledThreadPool(5, new ThreadFactoryBuilder().setNameFormat("Siddhi-" + siddhiAppContext.getName() + "-scheduler-thread-%d").build()));
        // Select the TimestampGenerator based on playback mode on/off
        annotation = AnnotationHelper.getAnnotation(SiddhiConstants.ANNOTATION_PLAYBACK, siddhiApp.getAnnotations());
        if (annotation != null) {
            String idleTime = null;
            String increment = null;
            TimestampGenerator timestampGenerator = new TimestampGeneratorImpl(siddhiAppContext);
            // Get the optional elements of playback annotation
            for (Element e : annotation.getElements()) {
                if (SiddhiConstants.ANNOTATION_ELEMENT_IDLE_TIME.equalsIgnoreCase(e.getKey())) {
                    idleTime = e.getValue();
                } else if (SiddhiConstants.ANNOTATION_ELEMENT_INCREMENT.equalsIgnoreCase(e.getKey())) {
                    increment = e.getValue();
                } else {
                    throw new SiddhiAppValidationException("Playback annotation accepts only idle.time and " + "increment but found " + e.getKey());
                }
            }
            // idleTime and increment are optional but if one presents, the other also should be given
            if (idleTime != null && increment == null) {
                throw new SiddhiAppValidationException("Playback annotation requires both idle.time and " + "increment but increment not found");
            } else if (idleTime == null && increment != null) {
                throw new SiddhiAppValidationException("Playback annotation requires both idle.time and " + "increment but idle.time does not found");
            } else if (idleTime != null) {
                // The fourth case idleTime == null && increment == null are ignored because it means no heartbeat.
                try {
                    timestampGenerator.setIdleTime(SiddhiCompiler.parseTimeConstantDefinition(idleTime).value());
                } catch (SiddhiParserException ex) {
                    throw new SiddhiParserException("Invalid idle.time constant '" + idleTime + "' in playback " + "annotation", ex);
                }
                try {
                    timestampGenerator.setIncrementInMilliseconds(SiddhiCompiler.parseTimeConstantDefinition(increment).value());
                } catch (SiddhiParserException ex) {
                    throw new SiddhiParserException("Invalid increment constant '" + increment + "' in playback " + "annotation", ex);
                }
            }
            siddhiAppContext.setTimestampGenerator(timestampGenerator);
            siddhiAppContext.setPlayback(true);
        } else {
            siddhiAppContext.setTimestampGenerator(new TimestampGeneratorImpl(siddhiAppContext));
        }
        siddhiAppContext.setSnapshotService(new SnapshotService(siddhiAppContext));
        siddhiAppContext.setIdGenerator(new IdGenerator());
    } catch (DuplicateAnnotationException e) {
        throw new DuplicateAnnotationException(e.getMessageWithOutContext() + " for the same Siddhi app " + siddhiApp.toString(), e, e.getQueryContextStartIndex(), e.getQueryContextEndIndex(), siddhiAppContext.getName(), siddhiAppContext.getSiddhiAppString());
    }
    SiddhiAppRuntimeBuilder siddhiAppRuntimeBuilder = new SiddhiAppRuntimeBuilder(siddhiAppContext);
    defineStreamDefinitions(siddhiAppRuntimeBuilder, siddhiApp.getStreamDefinitionMap(), siddhiAppContext);
    defineTableDefinitions(siddhiAppRuntimeBuilder, siddhiApp.getTableDefinitionMap(), siddhiAppContext);
    defineWindowDefinitions(siddhiAppRuntimeBuilder, siddhiApp.getWindowDefinitionMap(), siddhiAppContext);
    defineFunctionDefinitions(siddhiAppRuntimeBuilder, siddhiApp.getFunctionDefinitionMap(), siddhiAppContext);
    defineAggregationDefinitions(siddhiAppRuntimeBuilder, siddhiApp.getAggregationDefinitionMap(), siddhiAppContext);
    // todo fix for query API usecase
    List<String> findExecutedElements = getFindExecutedElements(siddhiApp);
    for (Window window : siddhiAppRuntimeBuilder.getWindowMap().values()) {
        try {
            window.init(siddhiAppRuntimeBuilder.getTableMap(), siddhiAppRuntimeBuilder.getWindowMap(), window.getWindowDefinition().getId(), findExecutedElements.contains(window.getWindowDefinition().getId()));
        } catch (Throwable t) {
            ExceptionUtil.populateQueryContext(t, window.getWindowDefinition(), siddhiAppContext);
            throw t;
        }
    }
    int queryIndex = 1;
    int partitionIndex = 1;
    for (ExecutionElement executionElement : siddhiApp.getExecutionElementList()) {
        if (executionElement instanceof Query) {
            try {
                QueryRuntimeImpl queryRuntime = QueryParser.parse((Query) executionElement, siddhiAppContext, siddhiAppRuntimeBuilder.getStreamDefinitionMap(), siddhiAppRuntimeBuilder.getTableDefinitionMap(), siddhiAppRuntimeBuilder.getWindowDefinitionMap(), siddhiAppRuntimeBuilder.getAggregationDefinitionMap(), siddhiAppRuntimeBuilder.getTableMap(), siddhiAppRuntimeBuilder.getAggregationMap(), siddhiAppRuntimeBuilder.getWindowMap(), siddhiAppRuntimeBuilder.getLockSynchronizer(), String.valueOf(queryIndex), false, SiddhiConstants.PARTITION_ID_DEFAULT);
                siddhiAppRuntimeBuilder.addQuery(queryRuntime);
                siddhiAppContext.addEternalReferencedHolder(queryRuntime);
                queryIndex++;
            } catch (Throwable t) {
                ExceptionUtil.populateQueryContext(t, (Query) executionElement, siddhiAppContext);
                throw t;
            }
        } else {
            try {
                PartitionRuntimeImpl partitionRuntime = PartitionParser.parse(siddhiAppRuntimeBuilder, (Partition) executionElement, siddhiAppContext, queryIndex, partitionIndex);
                siddhiAppRuntimeBuilder.addPartition(partitionRuntime);
                queryIndex += ((Partition) executionElement).getQueryList().size();
                partitionIndex++;
            } catch (Throwable t) {
                ExceptionUtil.populateQueryContext(t, (Partition) executionElement, siddhiAppContext);
                throw t;
            }
        }
    }
    // Done last as they have to be started last
    defineTriggerDefinitions(siddhiAppRuntimeBuilder, siddhiApp.getTriggerDefinitionMap(), siddhiAppContext);
    return siddhiAppRuntimeBuilder;
}
Also used : Query(io.siddhi.query.api.execution.query.Query) Element(io.siddhi.query.api.annotation.Element) ExecutionElement(io.siddhi.query.api.execution.ExecutionElement) ArrayList(java.util.ArrayList) ThreadBarrier(io.siddhi.core.util.ThreadBarrier) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) Window(io.siddhi.core.window.Window) SnapshotService(io.siddhi.core.util.snapshot.SnapshotService) Partition(io.siddhi.query.api.execution.partition.Partition) SiddhiAppCreationException(io.siddhi.core.exception.SiddhiAppCreationException) TimestampGenerator(io.siddhi.core.util.timestamp.TimestampGenerator) SiddhiAppValidationException(io.siddhi.query.api.exception.SiddhiAppValidationException) TimestampGeneratorImpl(io.siddhi.core.util.timestamp.TimestampGeneratorImpl) IdGenerator(io.siddhi.core.util.IdGenerator) Annotation(io.siddhi.query.api.annotation.Annotation) SiddhiAppRuntimeBuilder(io.siddhi.core.util.SiddhiAppRuntimeBuilder) DuplicateAnnotationException(io.siddhi.query.api.exception.DuplicateAnnotationException) QueryRuntimeImpl(io.siddhi.core.query.QueryRuntimeImpl) SiddhiParserException(io.siddhi.query.compiler.exception.SiddhiParserException) ExecutionElement(io.siddhi.query.api.execution.ExecutionElement) SiddhiAppContext(io.siddhi.core.config.SiddhiAppContext) PartitionRuntimeImpl(io.siddhi.core.partition.PartitionRuntimeImpl)

Example 4 with Window

use of io.siddhi.core.window.Window in project siddhi by wso2.

the class DefinitionParserHelper method addWindow.

public static void addWindow(WindowDefinition windowDefinition, ConcurrentMap<String, Window> eventWindowMap, SiddhiAppContext siddhiAppContext) {
    if (!eventWindowMap.containsKey(windowDefinition.getId())) {
        Window window = new Window(windowDefinition, siddhiAppContext);
        eventWindowMap.putIfAbsent(windowDefinition.getId(), window);
    }
}
Also used : Window(io.siddhi.core.window.Window)

Example 5 with Window

use of io.siddhi.core.window.Window in project siddhi by wso2.

the class OnDemandQueryParser method parse.

public static OnDemandQueryRuntime parse(OnDemandQuery onDemandQuery, String onDemandQueryString, SiddhiAppContext siddhiAppContext, Map<String, Table> tableMap, Map<String, Window> windowMap, Map<String, AggregationRuntime> aggregationMap) {
    final LockWrapper lockWrapper = new LockWrapper("OnDemandQueryLock");
    lockWrapper.setLock(new ReentrantLock());
    MetaStreamEvent metaStreamEvent = new MetaStreamEvent();
    int metaPosition = SiddhiConstants.UNKNOWN_STATE;
    String queryName;
    Table table;
    SiddhiQueryContext siddhiQueryContext;
    Expression onCondition;
    SnapshotService.getSkipStateStorageThreadLocal().set(true);
    switch(onDemandQuery.getType()) {
        case FIND:
            Within within = null;
            Expression per = null;
            queryName = "store_select_query_" + onDemandQuery.getInputStore().getStoreId();
            siddhiQueryContext = new SiddhiOnDemandQueryContext(siddhiAppContext, queryName, onDemandQueryString);
            InputStore inputStore = onDemandQuery.getInputStore();
            try {
                onCondition = Expression.value(true);
                metaStreamEvent.setInputReferenceId(inputStore.getStoreReferenceId());
                if (inputStore instanceof AggregationInputStore) {
                    AggregationInputStore aggregationInputStore = (AggregationInputStore) inputStore;
                    if (aggregationMap.get(inputStore.getStoreId()) == null) {
                        throw new OnDemandQueryCreationException("Aggregation \"" + inputStore.getStoreId() + "\" has not been defined");
                    }
                    if (aggregationInputStore.getPer() != null && aggregationInputStore.getWithin() != null) {
                        within = aggregationInputStore.getWithin();
                        per = aggregationInputStore.getPer();
                    } else if (aggregationInputStore.getPer() != null || aggregationInputStore.getWithin() != null) {
                        throw new OnDemandQueryCreationException(inputStore.getStoreId() + " should either have both 'within' and 'per' " + "defined or none.");
                    }
                    if (((AggregationInputStore) inputStore).getOnCondition() != null) {
                        onCondition = ((AggregationInputStore) inputStore).getOnCondition();
                    }
                } else if (inputStore instanceof ConditionInputStore) {
                    if (((ConditionInputStore) inputStore).getOnCondition() != null) {
                        onCondition = ((ConditionInputStore) inputStore).getOnCondition();
                    }
                }
                List<VariableExpressionExecutor> variableExpressionExecutors = new ArrayList<>();
                table = tableMap.get(inputStore.getStoreId());
                if (table != null) {
                    return constructOnDemandQueryRuntime(table, onDemandQuery, tableMap, windowMap, metaPosition, onCondition, metaStreamEvent, variableExpressionExecutors, lockWrapper, siddhiQueryContext);
                } else {
                    AggregationRuntime aggregation = aggregationMap.get(inputStore.getStoreId());
                    if (aggregation != null) {
                        return constructOnDemandQueryRuntime(aggregation, onDemandQuery, tableMap, windowMap, within, per, onCondition, metaStreamEvent, variableExpressionExecutors, lockWrapper, siddhiQueryContext);
                    } else {
                        Window window = windowMap.get(inputStore.getStoreId());
                        if (window != null) {
                            return constructOnDemandQueryRuntime(window, onDemandQuery, tableMap, windowMap, metaPosition, onCondition, metaStreamEvent, variableExpressionExecutors, lockWrapper, siddhiQueryContext);
                        } else {
                            throw new OnDemandQueryCreationException(inputStore.getStoreId() + " is neither a table, aggregation or window");
                        }
                    }
                }
            } finally {
                SnapshotService.getSkipStateStorageThreadLocal().set(null);
            }
        case INSERT:
            InsertIntoStream inserIntoStreamt = (InsertIntoStream) onDemandQuery.getOutputStream();
            queryName = "store_insert_query_" + inserIntoStreamt.getId();
            siddhiQueryContext = new SiddhiOnDemandQueryContext(siddhiAppContext, queryName, onDemandQueryString);
            onCondition = Expression.value(true);
            return getOnDemandQueryRuntime(onDemandQuery, tableMap, windowMap, metaPosition, lockWrapper, metaStreamEvent, inserIntoStreamt, onCondition, siddhiQueryContext);
        case DELETE:
            DeleteStream deleteStream = (DeleteStream) onDemandQuery.getOutputStream();
            queryName = "store_delete_query_" + deleteStream.getId();
            siddhiQueryContext = new SiddhiOnDemandQueryContext(siddhiAppContext, queryName, onDemandQueryString);
            onCondition = deleteStream.getOnDeleteExpression();
            return getOnDemandQueryRuntime(onDemandQuery, tableMap, windowMap, metaPosition, lockWrapper, metaStreamEvent, deleteStream, onCondition, siddhiQueryContext);
        case UPDATE:
            UpdateStream outputStream = (UpdateStream) onDemandQuery.getOutputStream();
            queryName = "store_update_query_" + outputStream.getId();
            siddhiQueryContext = new SiddhiOnDemandQueryContext(siddhiAppContext, queryName, onDemandQueryString);
            onCondition = outputStream.getOnUpdateExpression();
            return getOnDemandQueryRuntime(onDemandQuery, tableMap, windowMap, metaPosition, lockWrapper, metaStreamEvent, outputStream, onCondition, siddhiQueryContext);
        case UPDATE_OR_INSERT:
            UpdateOrInsertStream onDemandQueryOutputStream = (UpdateOrInsertStream) onDemandQuery.getOutputStream();
            queryName = "store_update_or_insert_query_" + onDemandQueryOutputStream.getId();
            siddhiQueryContext = new SiddhiOnDemandQueryContext(siddhiAppContext, queryName, onDemandQueryString);
            onCondition = onDemandQueryOutputStream.getOnUpdateExpression();
            return getOnDemandQueryRuntime(onDemandQuery, tableMap, windowMap, metaPosition, lockWrapper, metaStreamEvent, onDemandQueryOutputStream, onCondition, siddhiQueryContext);
        default:
            return null;
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) Window(io.siddhi.core.window.Window) Table(io.siddhi.core.table.Table) AbstractQueryableRecordTable(io.siddhi.core.table.record.AbstractQueryableRecordTable) VariableExpressionExecutor(io.siddhi.core.executor.VariableExpressionExecutor) InsertIntoStream(io.siddhi.query.api.execution.query.output.stream.InsertIntoStream) ArrayList(java.util.ArrayList) UpdateStream(io.siddhi.query.api.execution.query.output.stream.UpdateStream) LockWrapper(io.siddhi.core.util.lock.LockWrapper) SiddhiOnDemandQueryContext(io.siddhi.core.config.SiddhiOnDemandQueryContext) OnDemandQueryCreationException(io.siddhi.core.exception.OnDemandQueryCreationException) AggregationInputStore(io.siddhi.query.api.execution.query.input.store.AggregationInputStore) SiddhiQueryContext(io.siddhi.core.config.SiddhiQueryContext) UpdateOrInsertStream(io.siddhi.query.api.execution.query.output.stream.UpdateOrInsertStream) Expression(io.siddhi.query.api.expression.Expression) ConditionInputStore(io.siddhi.query.api.execution.query.input.store.ConditionInputStore) DeleteStream(io.siddhi.query.api.execution.query.output.stream.DeleteStream) Within(io.siddhi.query.api.aggregation.Within) ConditionInputStore(io.siddhi.query.api.execution.query.input.store.ConditionInputStore) AggregationInputStore(io.siddhi.query.api.execution.query.input.store.AggregationInputStore) InputStore(io.siddhi.query.api.execution.query.input.store.InputStore) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent) AggregationRuntime(io.siddhi.core.aggregation.AggregationRuntime)

Aggregations

Window (io.siddhi.core.window.Window)7 MetaStreamEvent (io.siddhi.core.event.stream.MetaStreamEvent)5 Table (io.siddhi.core.table.Table)5 AggregationRuntime (io.siddhi.core.aggregation.AggregationRuntime)4 SiddhiAppContext (io.siddhi.core.config.SiddhiAppContext)4 SiddhiAppCreationException (io.siddhi.core.exception.SiddhiAppCreationException)4 ArrayList (java.util.ArrayList)4 SiddhiQueryContext (io.siddhi.core.config.SiddhiQueryContext)3 VariableExpressionExecutor (io.siddhi.core.executor.VariableExpressionExecutor)3 QueryRuntimeImpl (io.siddhi.core.query.QueryRuntimeImpl)3 Query (io.siddhi.query.api.execution.query.Query)3 SiddhiContext (io.siddhi.core.config.SiddhiContext)2 StreamEventFactory (io.siddhi.core.event.stream.StreamEventFactory)2 SingleStreamRuntime (io.siddhi.core.query.input.stream.single.SingleStreamRuntime)2 IdGenerator (io.siddhi.core.util.IdGenerator)2 LockSynchronizer (io.siddhi.core.util.lock.LockSynchronizer)2 LockWrapper (io.siddhi.core.util.lock.LockWrapper)2 SnapshotService (io.siddhi.core.util.snapshot.SnapshotService)2 Element (io.siddhi.query.api.annotation.Element)2 AbstractDefinition (io.siddhi.query.api.definition.AbstractDefinition)2