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();
}
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();
}
}
}
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);
}
}
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;
}
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;
}
Aggregations