use of io.siddhi.core.exception.SiddhiAppCreationException in project siddhi by wso2.
the class SessionWindowTestCase method testSessionWindow5.
@Test(description = "This test checks if Siddhi App creation fails " + "when session key parameter with wrong data type", expectedExceptions = SiddhiAppCreationException.class)
public void testSessionWindow5() {
log.info("SessionWindow Test5: Testing session window " + "with defining wrong data type for session key parameter ");
SiddhiManager siddhiManager = new SiddhiManager();
SiddhiAppRuntime siddhiAppRuntime = null;
String purchaseEventStream = "" + "define stream purchaseEventStream (user string, item_number int, price float, quantity int);";
String query = "" + "@info(name = 'query0') " + "from purchaseEventStream#window.session(5 sec, item_number, 2 sec) " + "select * " + "insert all events into outputStream ;";
try {
siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(purchaseEventStream + query);
} catch (SiddhiAppCreationException e) {
AssertJUnit.assertEquals("There is no parameterOverload for 'session' that matches attribute types " + "'<LONG, INT, LONG>'. Supported parameter overloads are (<INT|LONG|TIME> session.gap), " + "(<INT|LONG|TIME> session.gap, <STRING> session.key), (<INT|LONG|TIME> session.gap, " + "<STRING> session.key, <INT|LONG|TIME> allowed.latency).", e.getCause().getMessage());
throw e;
} finally {
if (siddhiAppRuntime != null) {
siddhiAppRuntime.shutdown();
}
}
}
use of io.siddhi.core.exception.SiddhiAppCreationException in project siddhi by wso2.
the class SessionWindowTestCase method testSessionWindow3.
@Test(description = "This test checks if Siddhi App creation fails " + "when session gap parameter with wrong data type ", expectedExceptions = SiddhiAppCreationException.class)
public void testSessionWindow3() {
log.info("SessionWindow Test3: Testing session window " + "with providing wrong data type for session gap parameter ");
SiddhiManager siddhiManager = new SiddhiManager();
SiddhiAppRuntime siddhiAppRuntime = null;
String purchaseEventStream = "" + "define stream purchaseEventStream (user string, item_number int, price float, quantity int);";
String query = "" + "@info(name = 'query0') " + "from purchaseEventStream#window.session('3', user, 2 sec) " + "select * " + "insert all events into outputStream ;";
try {
siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(purchaseEventStream + query);
} catch (SiddhiAppCreationException e) {
AssertJUnit.assertEquals("There is no parameterOverload for 'session' that matches attribute types " + "'<STRING, STRING, LONG>'. Supported parameter overloads are (<INT|LONG|TIME> session.gap), " + "(<INT|LONG|TIME> session.gap, <STRING> session.key), (<INT|LONG|TIME> session.gap, " + "<STRING> session.key, <INT|LONG|TIME> allowed.latency).", e.getCause().getMessage());
throw e;
} finally {
if (siddhiAppRuntime != null) {
siddhiAppRuntime.shutdown();
}
}
}
use of io.siddhi.core.exception.SiddhiAppCreationException in project siddhi by wso2.
the class AbstractQueryableRecordTable method compileSelection.
public CompiledSelection compileSelection(Selector selector, List<Attribute> expectedOutputAttributes, MatchingMetaInfoHolder matchingMetaInfoHolder, List<VariableExpressionExecutor> variableExpressionExecutors, Map<String, Table> tableMap, SiddhiQueryContext siddhiQueryContext) {
selectorForTestOnDemandQuery = selector;
siddhiQueryContextForTestOnDemandQuery = siddhiQueryContext;
matchingMetaInfoHolderForTestOnDemandQuery = matchingMetaInfoHolder;
List<OutputAttribute> outputAttributes = selector.getSelectionList();
if (outputAttributes.size() == 0) {
MetaStreamEvent metaStreamEvent = matchingMetaInfoHolder.getMetaStateEvent().getMetaStreamEvent(matchingMetaInfoHolder.getStoreEventIndex());
List<Attribute> attributeList = metaStreamEvent.getLastInputDefinition().getAttributeList();
for (Attribute attribute : attributeList) {
outputAttributes.add(new OutputAttribute(new Variable(attribute.getName())));
}
}
List<SelectAttributeBuilder> selectAttributeBuilders = new ArrayList<>(outputAttributes.size());
for (OutputAttribute outputAttribute : outputAttributes) {
ExpressionBuilder expressionBuilder = new ExpressionBuilder(outputAttribute.getExpression(), matchingMetaInfoHolder, variableExpressionExecutors, tableMap, null, null, siddhiQueryContext);
selectAttributeBuilders.add(new SelectAttributeBuilder(expressionBuilder, outputAttribute.getRename()));
}
MatchingMetaInfoHolder metaInfoHolderAfterSelect = new MatchingMetaInfoHolder(matchingMetaInfoHolder.getMetaStateEvent(), matchingMetaInfoHolder.getMatchingStreamEventIndex(), matchingMetaInfoHolder.getStoreEventIndex(), matchingMetaInfoHolder.getMatchingStreamDefinition(), matchingMetaInfoHolder.getMatchingStreamDefinition(), matchingMetaInfoHolder.getCurrentState());
List<ExpressionBuilder> groupByExpressionBuilders = null;
if (selector.getGroupByList().size() != 0) {
groupByExpressionBuilders = new ArrayList<>(outputAttributes.size());
for (Variable variable : selector.getGroupByList()) {
groupByExpressionBuilders.add(new ExpressionBuilder(variable, metaInfoHolderAfterSelect, variableExpressionExecutors, tableMap, null, null, siddhiQueryContext));
}
}
ExpressionBuilder havingExpressionBuilder = null;
if (selector.getHavingExpression() != null) {
havingExpressionBuilder = new ExpressionBuilder(selector.getHavingExpression(), metaInfoHolderAfterSelect, variableExpressionExecutors, tableMap, null, null, siddhiQueryContext);
}
List<OrderByAttributeBuilder> orderByAttributeBuilders = null;
if (selector.getOrderByList().size() != 0) {
orderByAttributeBuilders = new ArrayList<>(selector.getOrderByList().size());
for (OrderByAttribute orderByAttribute : selector.getOrderByList()) {
ExpressionBuilder expressionBuilder = new ExpressionBuilder(orderByAttribute.getVariable(), metaInfoHolderAfterSelect, variableExpressionExecutors, tableMap, null, null, siddhiQueryContext);
orderByAttributeBuilders.add(new OrderByAttributeBuilder(expressionBuilder, orderByAttribute.getOrder()));
}
}
Long limit = null;
Long offset = null;
if (selector.getLimit() != null) {
ExpressionExecutor expressionExecutor = ExpressionParser.parseExpression((Expression) selector.getLimit(), metaInfoHolderAfterSelect.getMetaStateEvent(), SiddhiConstants.HAVING_STATE, tableMap, variableExpressionExecutors, false, 0, ProcessingMode.BATCH, false, siddhiQueryContext);
limit = ((Number) (((ConstantExpressionExecutor) expressionExecutor).getValue())).longValue();
if (limit < 0) {
throw new SiddhiAppCreationException("'limit' cannot have negative value, but found '" + limit + "'", selector, siddhiQueryContext.getSiddhiAppContext());
}
}
if (selector.getOffset() != null) {
ExpressionExecutor expressionExecutor = ExpressionParser.parseExpression((Expression) selector.getOffset(), metaInfoHolderAfterSelect.getMetaStateEvent(), SiddhiConstants.HAVING_STATE, tableMap, variableExpressionExecutors, false, 0, ProcessingMode.BATCH, false, siddhiQueryContext);
offset = ((Number) (((ConstantExpressionExecutor) expressionExecutor).getValue())).longValue();
if (offset < 0) {
throw new SiddhiAppCreationException("'offset' cannot have negative value, but found '" + offset + "'", selector, siddhiQueryContext.getSiddhiAppContext());
}
}
CompiledSelection compiledSelection = compileSelection(selectAttributeBuilders, groupByExpressionBuilders, havingExpressionBuilder, orderByAttributeBuilders, limit, offset);
Map<String, ExpressionExecutor> expressionExecutorMap = new HashMap<>();
if (selectAttributeBuilders.size() != 0) {
for (SelectAttributeBuilder selectAttributeBuilder : selectAttributeBuilders) {
expressionExecutorMap.putAll(selectAttributeBuilder.getExpressionBuilder().getVariableExpressionExecutorMap());
}
}
if (groupByExpressionBuilders != null && groupByExpressionBuilders.size() != 0) {
for (ExpressionBuilder groupByExpressionBuilder : groupByExpressionBuilders) {
expressionExecutorMap.putAll(groupByExpressionBuilder.getVariableExpressionExecutorMap());
}
}
if (havingExpressionBuilder != null) {
expressionExecutorMap.putAll(havingExpressionBuilder.getVariableExpressionExecutorMap());
}
if (orderByAttributeBuilders != null && orderByAttributeBuilders.size() != 0) {
for (OrderByAttributeBuilder orderByAttributeBuilder : orderByAttributeBuilders) {
expressionExecutorMap.putAll(orderByAttributeBuilder.getExpressionBuilder().getVariableExpressionExecutorMap());
}
}
if (cacheEnabled) {
CompiledSelectionWithCache compiledSelectionWithCache;
MetaStateEvent metaStateEventForCacheSelection = matchingMetaInfoHolder.getMetaStateEvent().clone();
ReturnStream returnStream = new ReturnStream(OutputStream.OutputEventType.CURRENT_EVENTS);
int metaPosition = SiddhiConstants.UNKNOWN_STATE;
List<VariableExpressionExecutor> variableExpressionExecutorsForQuerySelector = new ArrayList<>();
QuerySelector querySelector = SelectorParser.parse(selector, returnStream, metaStateEventForCacheSelection, tableMap, variableExpressionExecutorsForQuerySelector, metaPosition, ProcessingMode.BATCH, false, siddhiQueryContext);
if (matchingMetaInfoHolder.getMetaStateEvent().getOutputDataAttributes().size() == 0) {
for (MetaStateEventAttribute outputDataAttribute : metaStateEventForCacheSelection.getOutputDataAttributes()) {
matchingMetaInfoHolder.getMetaStateEvent().addOutputDataAllowingDuplicate(outputDataAttribute);
}
}
QueryParserHelper.updateVariablePosition(metaStateEventForCacheSelection, variableExpressionExecutorsForQuerySelector);
querySelector.setEventPopulator(StateEventPopulatorFactory.constructEventPopulator(metaStateEventForCacheSelection));
RecordStoreCompiledSelection recordStoreCompiledSelection = new RecordStoreCompiledSelection(expressionExecutorMap, compiledSelection);
compiledSelectionWithCache = new CompiledSelectionWithCache(recordStoreCompiledSelection, querySelector, metaStateEventForCacheSelection, matchingMetaInfoHolder.getStoreEventIndex(), variableExpressionExecutorsForQuerySelector);
return compiledSelectionWithCache;
} else {
return new RecordStoreCompiledSelection(expressionExecutorMap, compiledSelection);
}
}
use of io.siddhi.core.exception.SiddhiAppCreationException in project siddhi by wso2.
the class AbstractQueryableRecordTable method initCache.
@Override
public void initCache(TableDefinition tableDefinition, SiddhiAppContext siddhiAppContext, StreamEventCloner storeEventCloner, ConfigReader configReader) {
String[] annotationNames = { ANNOTATION_STORE, ANNOTATION_CACHE };
Annotation cacheTableAnnotation = getAnnotation(annotationNames, tableDefinition.getAnnotations());
if (cacheTableAnnotation != null) {
cacheEnabled = true;
maxCacheSize = Integer.parseInt(cacheTableAnnotation.getElement(CACHE_TABLE_SIZE));
TableDefinition cacheTableDefinition = TableDefinition.id(tableDefinition.getId());
for (Attribute attribute : tableDefinition.getAttributeList()) {
cacheTableDefinition.attribute(attribute.getName(), attribute.getType());
}
for (Annotation annotation : tableDefinition.getAnnotations()) {
if (!annotation.getName().equalsIgnoreCase("Store")) {
cacheTableDefinition.annotation(annotation);
}
}
String cachePolicy = cacheTableAnnotation.getElement(ANNOTATION_CACHE_POLICY);
if (cachePolicy == null || cachePolicy.equalsIgnoreCase("FIFO")) {
cachePolicy = "FIFO";
cacheTable = new CacheTableFIFO();
} else if (cachePolicy.equalsIgnoreCase("LRU")) {
cacheTable = new CacheTableLRU();
} else if (cachePolicy.equalsIgnoreCase("LFU")) {
cacheTable = new CacheTableLFU();
} else {
throw new SiddhiAppCreationException(siddhiAppContext.getName() + " : Cache policy can only be one " + "of FIFO, LRU, and LFU but given as " + cachePolicy);
}
// check if cache expiry enabled and initialize relevant parameters
if (cacheTableAnnotation.getElement(ANNOTATION_CACHE_RETENTION_PERIOD) != null) {
cacheExpiryEnabled = true;
retentionPeriod = Expression.Time.timeToLong(cacheTableAnnotation.getElement(ANNOTATION_CACHE_RETENTION_PERIOD));
if (cacheTableAnnotation.getElement(ANNOTATION_CACHE_PURGE_INTERVAL) == null) {
purgeInterval = retentionPeriod;
} else {
purgeInterval = Expression.Time.timeToLong(cacheTableAnnotation.getElement(ANNOTATION_CACHE_PURGE_INTERVAL));
}
storeSizeCheckInterval = purgeInterval * 5;
} else {
storeSizeCheckInterval = 10000;
}
((CacheTable) cacheTable).initCacheTable(cacheTableDefinition, configReader, siddhiAppContext, recordTableHandler, cacheExpiryEnabled, maxCacheSize, cachePolicy);
// creating objects needed to load cache
SiddhiQueryContext siddhiQueryContext = new SiddhiQueryContext(siddhiAppContext, CACHE_QUERY_NAME + tableDefinition.getId());
MatchingMetaInfoHolder matchingMetaInfoHolder = generateMatchingMetaInfoHolderForCacheTable(tableDefinition);
OnDemandQuery onDemandQuery = OnDemandQuery.query().from(InputStore.store(tableDefinition.getId())).select(Selector.selector().limit(Expression.value((maxCacheSize + 1))));
List<VariableExpressionExecutor> variableExpressionExecutors = new ArrayList<>();
compiledConditionForCaching = compileCondition(Expression.value(true), matchingMetaInfoHolder, variableExpressionExecutors, tableMap, siddhiQueryContext);
List<Attribute> expectedOutputAttributes = buildExpectedOutputAttributes(onDemandQuery, tableMap, SiddhiConstants.UNKNOWN_STATE, matchingMetaInfoHolder, siddhiQueryContext);
compiledSelectionForCaching = compileSelection(onDemandQuery.getSelector(), expectedOutputAttributes, matchingMetaInfoHolder, variableExpressionExecutors, tableMap, siddhiQueryContext);
outputAttributesForCaching = expectedOutputAttributes.toArray(new Attribute[0]);
QueryParserHelper.reduceMetaComplexEvent(matchingMetaInfoHolder.getMetaStateEvent());
QueryParserHelper.updateVariablePosition(matchingMetaInfoHolder.getMetaStateEvent(), variableExpressionExecutors);
compiledSelectionForSelectAll = generateCSForSelectAll();
}
}
use of io.siddhi.core.exception.SiddhiAppCreationException in project siddhi by wso2.
the class SiddhiAppRuntimeBuilder method addQuery.
public String addQuery(QueryRuntimeImpl queryRuntime) {
QueryRuntime oldQueryRuntime = queryProcessorMap.put(queryRuntime.getQueryId(), queryRuntime);
if (oldQueryRuntime != null) {
throw new SiddhiAppCreationException("Multiple queries with name '" + queryRuntime.getQueryId() + "' defined in Siddhi App '" + siddhiAppContext.getName() + "'", queryRuntime.getQuery().getQueryContextStartIndex(), queryRuntime.getQuery().getQueryContextEndIndex());
}
StreamRuntime streamRuntime = queryRuntime.getStreamRuntime();
for (SingleStreamRuntime singleStreamRuntime : streamRuntime.getSingleStreamRuntimes()) {
ProcessStreamReceiver processStreamReceiver = singleStreamRuntime.getProcessStreamReceiver();
if (processStreamReceiver.toStream()) {
StreamJunction streamJunction = streamJunctionMap.get(processStreamReceiver.getStreamId());
if (streamJunction != null) {
streamJunction.subscribe(processStreamReceiver);
} else {
throw new SiddhiAppCreationException("Expecting a stream, but provided '" + processStreamReceiver.getStreamId() + "' is not a stream");
}
}
}
OutputCallback outputCallback = queryRuntime.getOutputCallback();
if (outputCallback != null && outputCallback instanceof InsertIntoStreamCallback) {
InsertIntoStreamCallback insertIntoStreamCallback = (InsertIntoStreamCallback) outputCallback;
StreamDefinition streamDefinition = insertIntoStreamCallback.getOutputStreamDefinition();
streamDefinitionMap.putIfAbsent(streamDefinition.getId(), streamDefinition);
DefinitionParserHelper.validateOutputStream(streamDefinition, streamDefinitionMap.get(streamDefinition.getId()));
StreamJunction outputStreamJunction = streamJunctionMap.get(streamDefinition.getId());
if (outputStreamJunction == null) {
outputStreamJunction = new StreamJunction(streamDefinition, siddhiAppContext.getExecutorService(), siddhiAppContext.getBufferSize(), null, siddhiAppContext);
streamJunctionMap.putIfAbsent(streamDefinition.getId(), outputStreamJunction);
}
insertIntoStreamCallback.init(streamJunctionMap.get(insertIntoStreamCallback.getOutputStreamDefinition().getId()));
} else if (outputCallback != null && outputCallback instanceof InsertIntoWindowCallback) {
InsertIntoWindowCallback insertIntoWindowCallback = (InsertIntoWindowCallback) outputCallback;
StreamDefinition streamDefinition = insertIntoWindowCallback.getOutputStreamDefinition();
windowDefinitionMap.putIfAbsent(streamDefinition.getId(), streamDefinition);
DefinitionParserHelper.validateOutputStream(streamDefinition, windowDefinitionMap.get(streamDefinition.getId()));
StreamJunction outputStreamJunction = streamJunctionMap.get(streamDefinition.getId());
if (outputStreamJunction == null) {
outputStreamJunction = new StreamJunction(streamDefinition, siddhiAppContext.getExecutorService(), siddhiAppContext.getBufferSize(), null, siddhiAppContext);
streamJunctionMap.putIfAbsent(streamDefinition.getId(), outputStreamJunction);
}
insertIntoWindowCallback.getWindow().setPublisher(streamJunctionMap.get(insertIntoWindowCallback.getOutputStreamDefinition().getId()).constructPublisher());
}
return queryRuntime.getQueryId();
}
Aggregations