Search in sources :

Example 6 with OnDemandQueryRuntime

use of io.siddhi.core.query.OnDemandQueryRuntime in project siddhi by wso2.

the class IncrementalDataPurger method dataToDelete.

Event[] dataToDelete(long purgingTime, Table table) {
    OnDemandQuery onDemandQuery = getOnDemandQuery(table, purgingTime, 0);
    onDemandQuery.setType(OnDemandQuery.OnDemandQueryType.FIND);
    OnDemandQueryRuntime onDemandQueryRuntime = OnDemandQueryParser.parse(onDemandQuery, null, siddhiQueryContext.getSiddhiAppContext(), tableMap, windowMap, aggregationMap);
    return onDemandQueryRuntime.execute();
}
Also used : OnDemandQueryRuntime(io.siddhi.core.query.OnDemandQueryRuntime) OnDemandQuery(io.siddhi.query.api.execution.query.OnDemandQuery)

Example 7 with OnDemandQueryRuntime

use of io.siddhi.core.query.OnDemandQueryRuntime in project siddhi by wso2.

the class SiddhiAppRuntimeImpl method query.

private Event[] query(OnDemandQuery onDemandQuery, String onDemandQueryString) {
    try {
        if (Level.BASIC.compareTo(siddhiAppContext.getRootMetricsLevel()) <= 0 && onDemandQueryLatencyTracker != null) {
            onDemandQueryLatencyTracker.markIn();
        }
        OnDemandQueryRuntime onDemandQueryRuntime;
        synchronized (this) {
            onDemandQueryRuntime = onDemandQueryRuntimeMap.remove(onDemandQuery);
            if (onDemandQueryRuntime == null) {
                onDemandQueryRuntime = OnDemandQueryParser.parse(onDemandQuery, onDemandQueryString, siddhiAppContext, tableMap, windowMap, aggregationMap);
            } else {
                onDemandQueryRuntime.reset();
            }
            onDemandQueryRuntimeMap.put(onDemandQuery, onDemandQueryRuntime);
            if (onDemandQueryRuntimeMap.size() > 50) {
                Iterator i = onDemandQueryRuntimeMap.entrySet().iterator();
                if (i.hasNext()) {
                    i.next();
                    i.remove();
                }
            }
        }
        return onDemandQueryRuntime.execute();
    } catch (RuntimeException e) {
        if (e instanceof SiddhiAppContextException) {
            throw new OnDemandQueryCreationException(((SiddhiAppContextException) e).getMessageWithOutContext(), e, ((SiddhiAppContextException) e).getQueryContextStartIndex(), ((SiddhiAppContextException) e).getQueryContextEndIndex(), null, onDemandQueryString);
        }
        throw new OnDemandQueryCreationException(e.getMessage(), e);
    } finally {
        if (Level.BASIC.compareTo(siddhiAppContext.getRootMetricsLevel()) <= 0 && onDemandQueryLatencyTracker != null) {
            onDemandQueryLatencyTracker.markOut();
        }
    }
}
Also used : SiddhiAppRuntimeException(io.siddhi.core.exception.SiddhiAppRuntimeException) OnDemandQueryRuntime(io.siddhi.core.query.OnDemandQueryRuntime) Iterator(java.util.Iterator) SiddhiAppContextException(io.siddhi.query.api.exception.SiddhiAppContextException) OnDemandQueryCreationException(io.siddhi.core.exception.OnDemandQueryCreationException)

Example 8 with OnDemandQueryRuntime

use of io.siddhi.core.query.OnDemandQueryRuntime in project siddhi by wso2.

the class SiddhiAppRuntimeImpl method getOnDemandQueryOutputAttributes.

/**
 * This method get the onDemandQuery and return the corresponding output and its types.
 *
 * @param onDemandQuery       this onDemandQuery is processed and get the output attributes.
 * @param onDemandQueryString this passed to report errors with context if there are any.
 * @return List of output attributes
 */
private Attribute[] getOnDemandQueryOutputAttributes(OnDemandQuery onDemandQuery, String onDemandQueryString) {
    try {
        OnDemandQueryRuntime onDemandQueryRuntime = onDemandQueryRuntimeMap.get(onDemandQuery);
        if (onDemandQueryRuntime == null) {
            onDemandQueryRuntime = OnDemandQueryParser.parse(onDemandQuery, onDemandQueryString, siddhiAppContext, tableMap, windowMap, aggregationMap);
            onDemandQueryRuntimeMap.put(onDemandQuery, onDemandQueryRuntime);
        }
        return onDemandQueryRuntime.getOnDemandQueryOutputAttributes();
    } catch (RuntimeException e) {
        if (e instanceof SiddhiAppContextException) {
            throw new OnDemandQueryCreationException(((SiddhiAppContextException) e).getMessageWithOutContext(), e, ((SiddhiAppContextException) e).getQueryContextStartIndex(), ((SiddhiAppContextException) e).getQueryContextEndIndex(), null, siddhiAppContext.getSiddhiAppString());
        }
        throw new OnDemandQueryCreationException(e.getMessage(), e);
    }
}
Also used : SiddhiAppRuntimeException(io.siddhi.core.exception.SiddhiAppRuntimeException) OnDemandQueryRuntime(io.siddhi.core.query.OnDemandQueryRuntime) SiddhiAppContextException(io.siddhi.query.api.exception.SiddhiAppContextException) OnDemandQueryCreationException(io.siddhi.core.exception.OnDemandQueryCreationException)

Example 9 with OnDemandQueryRuntime

use of io.siddhi.core.query.OnDemandQueryRuntime in project siddhi by wso2.

the class IncrementalExecutorsInitialiser method initialiseExecutors.

public synchronized void initialiseExecutors() {
    if (this.isInitialised || isReadOnly) {
        // Only cleared when executors change from reading to processing state in one node deployment
        return;
    }
    Event[] events;
    Long lastData = null;
    // Get max(AGG_TIMESTAMP) from table corresponding to max duration
    Table tableForMaxDuration = aggregationTables.get(incrementalDurations.get(incrementalDurations.size() - 1));
    OnDemandQuery onDemandQuery = getOnDemandQuery(tableForMaxDuration, true, endOFLatestEventTimestamp);
    onDemandQuery.setType(OnDemandQuery.OnDemandQueryType.FIND);
    OnDemandQueryRuntime onDemandQueryRuntime = OnDemandQueryParser.parse(onDemandQuery, null, siddhiAppContext, tableMap, windowMap, aggregationMap);
    // Get latest event timestamp in tableForMaxDuration and get the end time of the aggregation record
    events = onDemandQueryRuntime.execute();
    if (events != null) {
        lastData = (Long) events[events.length - 1].getData(0);
        endOFLatestEventTimestamp = IncrementalTimeConverterUtil.getNextEmitTime(lastData, incrementalDurations.get(incrementalDurations.size() - 1), timeZone);
    }
    if (isPersistedAggregation) {
        for (int i = incrementalDurations.size() - 1; i > 0; i--) {
            if (lastData != null && !IncrementalTimeConverterUtil.isAggregationDataComplete(lastData, incrementalDurations.get(i), timeZone)) {
                recreateState(lastData, incrementalDurations.get(i), aggregationTables.get(incrementalDurations.get(i - 1)), i == 1);
            } else if (lastData == null) {
                recreateState(null, incrementalDurations.get(i), aggregationTables.get(incrementalDurations.get(i - 1)), i == 1);
            }
            if (i > 1) {
                onDemandQuery = getOnDemandQuery(aggregationTables.get(incrementalDurations.get(i - 1)), true, endOFLatestEventTimestamp);
                onDemandQuery.setType(OnDemandQuery.OnDemandQueryType.FIND);
                onDemandQueryRuntime = OnDemandQueryParser.parse(onDemandQuery, null, siddhiAppContext, tableMap, windowMap, aggregationMap);
                events = onDemandQueryRuntime.execute();
                if (events != null) {
                    lastData = (Long) events[events.length - 1].getData(0);
                } else {
                    lastData = null;
                }
            }
        }
    } else {
        for (int i = incrementalDurations.size() - 1; i > 0; i--) {
            TimePeriod.Duration recreateForDuration = incrementalDurations.get(i);
            Executor incrementalExecutor = incrementalExecutorMap.get(recreateForDuration);
            // Get the table previous to the duration for which we need to recreate (e.g. if we want to recreate
            // for minute duration, take the second table [provided that aggregation is done for seconds])
            // This lookup is filtered by endOFLatestEventTimestamp
            Table recreateFromTable = aggregationTables.get(incrementalDurations.get(i - 1));
            onDemandQuery = getOnDemandQuery(recreateFromTable, false, endOFLatestEventTimestamp);
            onDemandQuery.setType(OnDemandQuery.OnDemandQueryType.FIND);
            onDemandQueryRuntime = OnDemandQueryParser.parse(onDemandQuery, null, siddhiAppContext, tableMap, windowMap, aggregationMap);
            events = onDemandQueryRuntime.execute();
            if (events != null) {
                long referenceToNextLatestEvent = (Long) events[events.length - 1].getData(0);
                endOFLatestEventTimestamp = IncrementalTimeConverterUtil.getNextEmitTime(referenceToNextLatestEvent, incrementalDurations.get(i - 1), timeZone);
                ComplexEventChunk<StreamEvent> complexEventChunk = new ComplexEventChunk<>();
                for (Event event : events) {
                    StreamEvent streamEvent = streamEventFactory.newInstance();
                    streamEvent.setOutputData(event.getData());
                    complexEventChunk.add(streamEvent);
                }
                incrementalExecutor.execute(complexEventChunk);
                if (i == 1) {
                    TimePeriod.Duration rootDuration = incrementalDurations.get(0);
                    Executor rootIncrementalExecutor = incrementalExecutorMap.get(rootDuration);
                    long emitTimeOfLatestEventInTable = IncrementalTimeConverterUtil.getNextEmitTime(referenceToNextLatestEvent, rootDuration, timeZone);
                    rootIncrementalExecutor.setEmitTime(emitTimeOfLatestEventInTable);
                }
            }
        }
    }
    this.isInitialised = true;
}
Also used : Table(io.siddhi.core.table.Table) ComplexEventChunk(io.siddhi.core.event.ComplexEventChunk) TimePeriod(io.siddhi.query.api.aggregation.TimePeriod) StreamEvent(io.siddhi.core.event.stream.StreamEvent) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent) OnDemandQuery(io.siddhi.query.api.execution.query.OnDemandQuery) OnDemandQueryRuntime(io.siddhi.core.query.OnDemandQueryRuntime) Event(io.siddhi.core.event.Event) StreamEvent(io.siddhi.core.event.stream.StreamEvent) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent)

Example 10 with OnDemandQueryRuntime

use of io.siddhi.core.query.OnDemandQueryRuntime in project siddhi by wso2.

the class OnDemandQueryParser method constructOptimizedOnDemandQueryRuntime.

private static OnDemandQueryRuntime constructOptimizedOnDemandQueryRuntime(Table table, OnDemandQuery onDemandQuery, Map<String, Table> tableMap, int metaPosition, Expression onCondition, MetaStreamEvent metaStreamEvent, List<VariableExpressionExecutor> variableExpressionExecutors, SiddhiQueryContext siddhiQueryContext) {
    MatchingMetaInfoHolder matchingMetaInfoHolder;
    initMetaStreamEvent(metaStreamEvent, table.getTableDefinition());
    matchingMetaInfoHolder = generateMatchingMetaInfoHolder(metaStreamEvent, table.getTableDefinition());
    CompiledCondition compiledCondition = table.compileCondition(onCondition, matchingMetaInfoHolder, variableExpressionExecutors, tableMap, siddhiQueryContext);
    List<Attribute> expectedOutputAttributes = buildExpectedOutputAttributes(onDemandQuery, tableMap, metaPosition, matchingMetaInfoHolder, siddhiQueryContext);
    // MatchingMetaInfoHolder matchingMetaInfoHolderForSelection = generateMatchingMetaInfoHolder(
    // metaStreamEvent, generateTableDefinitionFromOnDemandQuery(onDemandQuery, expectedOutputAttributes),
    // table.getTableDefinition());
    CompiledSelection compiledSelection = ((QueryableProcessor) table).compileSelection(onDemandQuery.getSelector(), expectedOutputAttributes, matchingMetaInfoHolder, variableExpressionExecutors, tableMap, siddhiQueryContext);
    OnDemandQueryRuntime onDemandQueryRuntime = new SelectOnDemandQueryRuntime((QueryableProcessor) table, compiledCondition, compiledSelection, expectedOutputAttributes, siddhiQueryContext.getName());
    try {
        AbstractQueryableRecordTable.CompiledSelectionWithCache compiledSelectionWithCache = (AbstractQueryableRecordTable.CompiledSelectionWithCache) compiledSelection;
        onDemandQueryRuntime.setSelector(compiledSelectionWithCache.getQuerySelector());
        onDemandQueryRuntime.setMetaStreamEvent(metaStreamEvent);
        onDemandQueryRuntime.setStateEventFactory(new StateEventFactory(matchingMetaInfoHolder.getMetaStateEvent()));
    } catch (ClassCastException ignored) {
    }
    QueryParserHelper.reduceMetaComplexEvent(matchingMetaInfoHolder.getMetaStateEvent());
    QueryParserHelper.updateVariablePosition(matchingMetaInfoHolder.getMetaStateEvent(), variableExpressionExecutors);
    return onDemandQueryRuntime;
}
Also used : Attribute(io.siddhi.query.api.definition.Attribute) SelectOnDemandQueryRuntime(io.siddhi.core.query.SelectOnDemandQueryRuntime) AbstractQueryableRecordTable(io.siddhi.core.table.record.AbstractQueryableRecordTable) CompiledCondition(io.siddhi.core.util.collection.operator.CompiledCondition) MatchingMetaInfoHolder(io.siddhi.core.util.collection.operator.MatchingMetaInfoHolder) CompiledSelection(io.siddhi.core.util.collection.operator.CompiledSelection) FindOnDemandQueryRuntime(io.siddhi.core.query.FindOnDemandQueryRuntime) OnDemandQueryRuntime(io.siddhi.core.query.OnDemandQueryRuntime) UpdateOnDemandQueryRuntime(io.siddhi.core.query.UpdateOnDemandQueryRuntime) UpdateOrInsertOnDemandQueryRuntime(io.siddhi.core.query.UpdateOrInsertOnDemandQueryRuntime) InsertOnDemandQueryRuntime(io.siddhi.core.query.InsertOnDemandQueryRuntime) DeleteOnDemandQueryRuntime(io.siddhi.core.query.DeleteOnDemandQueryRuntime) SelectOnDemandQueryRuntime(io.siddhi.core.query.SelectOnDemandQueryRuntime) StateEventFactory(io.siddhi.core.event.state.StateEventFactory) QueryableProcessor(io.siddhi.core.query.processor.stream.window.QueryableProcessor)

Aggregations

OnDemandQueryRuntime (io.siddhi.core.query.OnDemandQueryRuntime)14 OnDemandQuery (io.siddhi.query.api.execution.query.OnDemandQuery)8 Event (io.siddhi.core.event.Event)6 MetaStreamEvent (io.siddhi.core.event.stream.MetaStreamEvent)6 StreamEvent (io.siddhi.core.event.stream.StreamEvent)6 ComplexEventChunk (io.siddhi.core.event.ComplexEventChunk)4 OnDemandQueryCreationException (io.siddhi.core.exception.OnDemandQueryCreationException)4 SiddhiAppRuntimeException (io.siddhi.core.exception.SiddhiAppRuntimeException)4 TimePeriod (io.siddhi.query.api.aggregation.TimePeriod)4 SiddhiAppContextException (io.siddhi.query.api.exception.SiddhiAppContextException)4 MetaStateEvent (io.siddhi.core.event.state.MetaStateEvent)2 StateEvent (io.siddhi.core.event.state.StateEvent)2 StateEventFactory (io.siddhi.core.event.state.StateEventFactory)2 DataPurgingException (io.siddhi.core.exception.DataPurgingException)2 SiddhiAppCreationException (io.siddhi.core.exception.SiddhiAppCreationException)2 DeleteOnDemandQueryRuntime (io.siddhi.core.query.DeleteOnDemandQueryRuntime)2 FindOnDemandQueryRuntime (io.siddhi.core.query.FindOnDemandQueryRuntime)2 InsertOnDemandQueryRuntime (io.siddhi.core.query.InsertOnDemandQueryRuntime)2 SelectOnDemandQueryRuntime (io.siddhi.core.query.SelectOnDemandQueryRuntime)2 UpdateOnDemandQueryRuntime (io.siddhi.core.query.UpdateOnDemandQueryRuntime)2