use of io.siddhi.core.exception.OnDemandQueryCreationException in project siddhi by wso2.
the class OnDemandQueryTableTestCase method test21.
@Test
public void test21() throws InterruptedException {
log.info("Testing update on-demand query parser failure");
SiddhiManager siddhiManager = new SiddhiManager();
String stockStream = "define stream stockStream (symbol string, price float, lastClosingPrice float, volume long , " + "quantity int, timestamp long);";
String query = "define aggregation stockAggregation " + "from stockStream " + "select symbol, sum(price) as totalPrice, avg(price) as avgPrice " + "group by symbol " + "aggregate by timestamp every sec...year ;";
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(stockStream + query);
siddhiAppRuntime.start();
try {
siddhiAppRuntime.query("from stockAggregation within 0L, 1543664151000L per " + "'minutes' select AGG_TIMESTAMP2, symbol, totalPrice, avgPrice ");
Thread.sleep(100);
Assert.fail("Expected OnDemandQueryCreationException exception");
} catch (OnDemandQueryCreationException e) {
String expectedCauseBy = "@ Line: 1. Position: 83, near 'AGG_TIMESTAMP2'. " + "No matching stream reference found for attribute 'AGG_TIMESTAMP2'";
Assert.assertTrue(e.getCause().getMessage().endsWith(expectedCauseBy));
}
siddhiAppRuntime.shutdown();
}
use of io.siddhi.core.exception.OnDemandQueryCreationException 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.exception.OnDemandQueryCreationException 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.exception.OnDemandQueryCreationException 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;
}
}
use of io.siddhi.core.exception.OnDemandQueryCreationException in project siddhi by wso2.
the class OnDemandQueryParser method getOnDemandQueryRuntime.
private static OnDemandQueryRuntime getOnDemandQueryRuntime(OnDemandQuery onDemandQuery, Map<String, Table> tableMap, Map<String, Window> windowMap, int metaPosition, LockWrapper lockWrapper, MetaStreamEvent metaStreamEvent, OutputStream outputStream, Expression onCondition, SiddhiQueryContext siddhiQueryContext) {
try {
List<VariableExpressionExecutor> variableExpressionExecutors = new ArrayList<>();
Table table = tableMap.get(outputStream.getId());
if (table != null) {
return constructOnDemandQueryRuntime(table, onDemandQuery, tableMap, windowMap, metaPosition, onCondition, metaStreamEvent, variableExpressionExecutors, lockWrapper, siddhiQueryContext);
} else {
throw new OnDemandQueryCreationException(outputStream.getId() + " is not a table.");
}
} finally {
SnapshotService.getSkipStateStorageThreadLocal().set(null);
}
}
Aggregations