Search in sources :

Example 11 with SiddhiAppCreationException

use of org.wso2.siddhi.core.exception.SiddhiAppCreationException in project siddhi by wso2.

the class SingleInputStreamParser method initMetaStreamEvent.

/**
 * Method to generate MetaStreamEvent reagent to the given input stream. Empty definition will be created and
 * definition and reference is will be set accordingly in this method.
 *
 * @param inputStream              InputStream
 * @param streamDefinitionMap      StreamDefinition Map
 * @param tableDefinitionMap       TableDefinition Map
 * @param aggregationDefinitionMap AggregationDefinition Map
 * @param metaStreamEvent          MetaStreamEvent
 */
private static void initMetaStreamEvent(SingleInputStream inputStream, Map<String, AbstractDefinition> streamDefinitionMap, Map<String, AbstractDefinition> tableDefinitionMap, Map<String, AbstractDefinition> windowDefinitionMap, Map<String, AbstractDefinition> aggregationDefinitionMap, MetaStreamEvent metaStreamEvent) {
    String streamId = inputStream.getStreamId();
    if (!inputStream.isInnerStream() && windowDefinitionMap != null && windowDefinitionMap.containsKey(streamId)) {
        AbstractDefinition inputDefinition = windowDefinitionMap.get(streamId);
        if (!metaStreamEvent.getInputDefinitions().contains(inputDefinition)) {
            metaStreamEvent.addInputDefinition(inputDefinition);
        }
    } else if (streamDefinitionMap != null && streamDefinitionMap.containsKey(streamId)) {
        AbstractDefinition inputDefinition = streamDefinitionMap.get(streamId);
        metaStreamEvent.addInputDefinition(inputDefinition);
    } else if (!inputStream.isInnerStream() && tableDefinitionMap != null && tableDefinitionMap.containsKey(streamId)) {
        AbstractDefinition inputDefinition = tableDefinitionMap.get(streamId);
        metaStreamEvent.addInputDefinition(inputDefinition);
    } else if (!inputStream.isInnerStream() && aggregationDefinitionMap != null && aggregationDefinitionMap.containsKey(streamId)) {
        AbstractDefinition inputDefinition = aggregationDefinitionMap.get(streamId);
        metaStreamEvent.addInputDefinition(inputDefinition);
    } else {
        throw new SiddhiAppCreationException("Stream/table/window/aggregation definition with ID '" + inputStream.getStreamId() + "' has not been defined", inputStream.getQueryContextStartIndex(), inputStream.getQueryContextEndIndex());
    }
    if ((inputStream.getStreamReferenceId() != null) && !(inputStream.getStreamId()).equals(inputStream.getStreamReferenceId())) {
        // if ref id is provided
        metaStreamEvent.setInputReferenceId(inputStream.getStreamReferenceId());
    }
}
Also used : SiddhiAppCreationException(org.wso2.siddhi.core.exception.SiddhiAppCreationException) AbstractDefinition(org.wso2.siddhi.query.api.definition.AbstractDefinition)

Example 12 with SiddhiAppCreationException

use of org.wso2.siddhi.core.exception.SiddhiAppCreationException in project siddhi by wso2.

the class DefinitionParserHelper method constructExtension.

public static Extension constructExtension(StreamDefinition streamDefinition, String typeName, String typeValue, Annotation annotation, String defaultNamespace) {
    String[] namespaceAndName = typeValue.split(SiddhiConstants.EXTENSION_SEPARATOR);
    String namespace;
    String name;
    if (namespaceAndName.length == 1) {
        namespace = defaultNamespace;
        name = namespaceAndName[0];
    } else if (namespaceAndName.length == 2) {
        namespace = namespaceAndName[0];
        name = namespaceAndName[1];
    } else {
        throw new SiddhiAppCreationException("Malformed '" + typeName + "' annotation type '" + typeValue + "' " + "provided, for annotation '" + annotation + "' on stream '" + streamDefinition.getId() + "', " + "it should be either '<namespace>:<name>' or '<name>'", annotation.getQueryContextStartIndex(), annotation.getQueryContextEndIndex());
    }
    return new Extension() {

        @Override
        public String getNamespace() {
            return namespace;
        }

        @Override
        public String getName() {
            return name;
        }
    };
}
Also used : Extension(org.wso2.siddhi.query.api.extension.Extension) SiddhiAppCreationException(org.wso2.siddhi.core.exception.SiddhiAppCreationException)

Example 13 with SiddhiAppCreationException

use of org.wso2.siddhi.core.exception.SiddhiAppCreationException in project siddhi by wso2.

the class DefinitionParserHelper method addEventSink.

public static void addEventSink(StreamDefinition streamDefinition, ConcurrentMap<String, List<Sink>> eventSinkMap, SiddhiAppContext siddhiAppContext) {
    for (Annotation sinkAnnotation : streamDefinition.getAnnotations()) {
        if (SiddhiConstants.ANNOTATION_SINK.equalsIgnoreCase(sinkAnnotation.getName())) {
            sinkAnnotation = updateAnnotationRef(sinkAnnotation, SiddhiConstants.NAMESPACE_SINK, siddhiAppContext);
            Annotation mapAnnotation = AnnotationHelper.getAnnotation(SiddhiConstants.ANNOTATION_MAP, sinkAnnotation.getAnnotations());
            if (mapAnnotation == null) {
                mapAnnotation = Annotation.annotation(SiddhiConstants.ANNOTATION_MAP).element(SiddhiConstants.ANNOTATION_ELEMENT_TYPE, "passThrough");
            }
            Annotation distributionAnnotation = AnnotationHelper.getAnnotation(SiddhiConstants.ANNOTATION_DISTRIBUTION, sinkAnnotation.getAnnotations());
            if (mapAnnotation != null) {
                String[] supportedDynamicOptions = null;
                List<OptionHolder> destinationOptHolders = new ArrayList<>();
                String sinkType = sinkAnnotation.getElement(SiddhiConstants.ANNOTATION_ELEMENT_TYPE);
                Extension sinkExtension = constructExtension(streamDefinition, SiddhiConstants.ANNOTATION_SINK, sinkType, sinkAnnotation, SiddhiConstants.NAMESPACE_SINK);
                ConfigReader sinkConfigReader = siddhiAppContext.getSiddhiContext().getConfigManager().generateConfigReader(sinkExtension.getNamespace(), sinkExtension.getName());
                final boolean isDistributedTransport = (distributionAnnotation != null);
                boolean isMultiClient = false;
                if (isDistributedTransport) {
                    Sink sink = createSink(sinkExtension, siddhiAppContext);
                    isMultiClient = isMultiClientDistributedTransport(sink, streamDefinition, distributionAnnotation, siddhiAppContext);
                    supportedDynamicOptions = sink.getSupportedDynamicOptions();
                    destinationOptHolders = createDestinationOptionHolders(distributionAnnotation, streamDefinition, sink, siddhiAppContext);
                }
                final String mapType = mapAnnotation.getElement(SiddhiConstants.ANNOTATION_ELEMENT_TYPE);
                if (mapType != null) {
                    Sink sink;
                    if (isDistributedTransport) {
                        sink = (isMultiClient) ? new MultiClientDistributedSink() : new SingleClientDistributedSink();
                    } else {
                        sink = createSink(sinkExtension, siddhiAppContext);
                    }
                    if (supportedDynamicOptions == null) {
                        supportedDynamicOptions = sink.getSupportedDynamicOptions();
                    }
                    // load output mapper extension
                    Extension mapperExtension = constructExtension(streamDefinition, SiddhiConstants.ANNOTATION_MAP, mapType, sinkAnnotation, SiddhiConstants.NAMESPACE_SINK_MAPPER);
                    ConfigReader mapperConfigReader = siddhiAppContext.getSiddhiContext().getConfigManager().generateConfigReader(sinkExtension.getNamespace(), sinkExtension.getName());
                    SinkMapper sinkMapper = (SinkMapper) SiddhiClassLoader.loadExtensionImplementation(mapperExtension, SinkMapperExecutorExtensionHolder.getInstance(siddhiAppContext));
                    org.wso2.siddhi.annotation.Extension sinkExt = sink.getClass().getAnnotation(org.wso2.siddhi.annotation.Extension.class);
                    OptionHolder transportOptionHolder = constructOptionHolder(streamDefinition, sinkAnnotation, sinkExt, supportedDynamicOptions);
                    OptionHolder mapOptionHolder = constructOptionHolder(streamDefinition, mapAnnotation, sinkMapper.getClass().getAnnotation(org.wso2.siddhi.annotation.Extension.class), sinkMapper.getSupportedDynamicOptions());
                    List<Element> payloadElementList = getPayload(mapAnnotation);
                    OptionHolder distributionOptHolder = null;
                    SinkHandlerManager sinkHandlerManager = siddhiAppContext.getSiddhiContext().getSinkHandlerManager();
                    SinkHandler sinkHandler = null;
                    if (sinkHandlerManager != null) {
                        sinkHandler = sinkHandlerManager.generateSinkHandler();
                    }
                    if (isDistributedTransport) {
                        distributionOptHolder = constructOptionHolder(streamDefinition, distributionAnnotation, sinkExt, supportedDynamicOptions);
                        String strategyType = distributionOptHolder.validateAndGetStaticValue(SiddhiConstants.DISTRIBUTION_STRATEGY_KEY);
                        Extension strategyExtension = constructExtension(streamDefinition, SiddhiConstants.ANNOTATION_SINK, strategyType, sinkAnnotation, SiddhiConstants.NAMESPACE_DISTRIBUTION_STRATEGY);
                        ConfigReader configReader = siddhiAppContext.getSiddhiContext().getConfigManager().generateConfigReader(strategyExtension.getNamespace(), strategyExtension.getName());
                        DistributionStrategy distributionStrategy = (DistributionStrategy) SiddhiClassLoader.loadExtensionImplementation(strategyExtension, DistributionStrategyExtensionHolder.getInstance(siddhiAppContext));
                        try {
                            distributionStrategy.init(streamDefinition, transportOptionHolder, distributionOptHolder, destinationOptHolders, configReader);
                            ((DistributedTransport) sink).init(streamDefinition, sinkType, transportOptionHolder, sinkConfigReader, sinkMapper, mapType, mapOptionHolder, sinkHandler, payloadElementList, mapperConfigReader, siddhiAppContext, destinationOptHolders, sinkAnnotation, distributionStrategy, supportedDynamicOptions);
                        } catch (Throwable t) {
                            ExceptionUtil.populateQueryContext(t, sinkAnnotation, siddhiAppContext);
                            throw t;
                        }
                    } else {
                        try {
                            sink.init(streamDefinition, sinkType, transportOptionHolder, sinkConfigReader, sinkMapper, mapType, mapOptionHolder, sinkHandler, payloadElementList, mapperConfigReader, siddhiAppContext);
                        } catch (Throwable t) {
                            ExceptionUtil.populateQueryContext(t, sinkAnnotation, siddhiAppContext);
                            throw t;
                        }
                    }
                    if (sinkHandlerManager != null) {
                        sinkHandlerManager.registerSinkHandler(sinkHandler.getElementId(), sinkHandler);
                        siddhiAppContext.getSnapshotService().addSnapshotable(streamDefinition.getId(), sinkHandler);
                    }
                    validateSinkMapperCompatibility(streamDefinition, sinkType, mapType, sink, sinkMapper, sinkAnnotation);
                    // Setting the output group determiner
                    OutputGroupDeterminer groupDeterminer = constructOutputGroupDeterminer(transportOptionHolder, distributionOptHolder, streamDefinition, destinationOptHolders.size());
                    if (groupDeterminer != null) {
                        sink.getMapper().setGroupDeterminer(groupDeterminer);
                    }
                    siddhiAppContext.getSnapshotService().addSnapshotable(sink.getStreamDefinition().getId(), sink);
                    List<Sink> eventSinks = eventSinkMap.get(streamDefinition.getId());
                    if (eventSinks == null) {
                        eventSinks = new ArrayList<>();
                        eventSinks.add(sink);
                        eventSinkMap.put(streamDefinition.getId(), eventSinks);
                    } else {
                        eventSinks.add(sink);
                    }
                }
            } else {
                throw new SiddhiAppCreationException("Both @sink(type=) and @map(type=) are required.", sinkAnnotation.getQueryContextStartIndex(), sinkAnnotation.getQueryContextEndIndex());
            }
        }
    }
}
Also used : Element(org.wso2.siddhi.query.api.annotation.Element) ArrayList(java.util.ArrayList) OutputGroupDeterminer(org.wso2.siddhi.core.stream.output.sink.OutputGroupDeterminer) SingleClientDistributedSink(org.wso2.siddhi.core.util.transport.SingleClientDistributedSink) MultiClientDistributedSink(org.wso2.siddhi.core.util.transport.MultiClientDistributedSink) SingleClientDistributedSink(org.wso2.siddhi.core.util.transport.SingleClientDistributedSink) Sink(org.wso2.siddhi.core.stream.output.sink.Sink) OptionHolder(org.wso2.siddhi.core.util.transport.OptionHolder) SinkMapper(org.wso2.siddhi.core.stream.output.sink.SinkMapper) SinkHandlerManager(org.wso2.siddhi.core.stream.output.sink.SinkHandlerManager) SinkHandler(org.wso2.siddhi.core.stream.output.sink.SinkHandler) DistributedTransport(org.wso2.siddhi.core.stream.output.sink.distributed.DistributedTransport) DistributionStrategy(org.wso2.siddhi.core.stream.output.sink.distributed.DistributionStrategy) SiddhiAppCreationException(org.wso2.siddhi.core.exception.SiddhiAppCreationException) ConfigReader(org.wso2.siddhi.core.util.config.ConfigReader) Annotation(org.wso2.siddhi.query.api.annotation.Annotation) Extension(org.wso2.siddhi.query.api.extension.Extension) MultiClientDistributedSink(org.wso2.siddhi.core.util.transport.MultiClientDistributedSink)

Example 14 with SiddhiAppCreationException

use of org.wso2.siddhi.core.exception.SiddhiAppCreationException in project siddhi by wso2.

the class TimeWindowTestCase method timeWindowTest6.

@Test(expectedExceptions = SiddhiAppCreationException.class)
public void timeWindowTest6() throws InterruptedException, SiddhiAppCreationException {
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "" + "define stream cseEventStream (symbol string, time long, volume int);";
    String query = "" + "@info(name = 'query1') " + "from cseEventStream#window.time(4.7) " + "select symbol,price,volume " + "insert all events into outputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);
}
Also used : SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Example 15 with SiddhiAppCreationException

use of org.wso2.siddhi.core.exception.SiddhiAppCreationException in project siddhi by wso2.

the class AggregationRuntime method compileExpression.

public CompiledCondition compileExpression(Expression expression, Within within, Expression per, MatchingMetaInfoHolder matchingMetaInfoHolder, List<VariableExpressionExecutor> variableExpressionExecutors, Map<String, Table> tableMap, String queryName, SiddhiAppContext siddhiAppContext) {
    Map<TimePeriod.Duration, CompiledCondition> withinTableCompiledConditions = new HashMap<>();
    CompiledCondition withinInMemoryCompileCondition;
    CompiledCondition onCompiledCondition;
    List<Attribute> additionalAttributes = new ArrayList<>();
    // Define additional attribute list
    additionalAttributes.add(new Attribute("_START", Attribute.Type.LONG));
    additionalAttributes.add(new Attribute("_END", Attribute.Type.LONG));
    // Get table definition. Table definitions for all the tables used to persist aggregates are similar.
    // Therefore it's enough to get the definition from one table.
    AbstractDefinition tableDefinition = ((Table) aggregationTables.values().toArray()[0]).getTableDefinition();
    // Alter existing meta stream event or create new one if a meta stream doesn't exist
    // After calling this method the original MatchingMetaInfoHolder's meta stream event would be altered
    MetaStreamEvent newMetaStreamEventWithStartEnd = createNewMetaStreamEventWithStartEnd(matchingMetaInfoHolder, additionalAttributes);
    MatchingMetaInfoHolder alteredMatchingMetaInfoHolder = null;
    // Alter meta info holder to contain stream event and aggregate both when it's a store query
    if (matchingMetaInfoHolder.getMetaStateEvent().getMetaStreamEvents().length == 1) {
        matchingMetaInfoHolder = alterMetaInfoHolderForStoreQuery(newMetaStreamEventWithStartEnd, matchingMetaInfoHolder);
        alteredMatchingMetaInfoHolder = matchingMetaInfoHolder;
    }
    // Create new MatchingMetaInfoHolder containing newMetaStreamEventWithStartEnd and table meta event
    MatchingMetaInfoHolder streamTableMetaInfoHolderWithStartEnd = createNewStreamTableMetaInfoHolder(newMetaStreamEventWithStartEnd, tableDefinition);
    // Create per expression executor
    ExpressionExecutor perExpressionExecutor = ExpressionParser.parseExpression(per, matchingMetaInfoHolder.getMetaStateEvent(), matchingMetaInfoHolder.getCurrentState(), tableMap, variableExpressionExecutors, siddhiAppContext, false, 0, queryName);
    if (perExpressionExecutor.getReturnType() != Attribute.Type.STRING) {
        throw new SiddhiAppCreationException("Query " + queryName + "'s per value expected a string but found " + perExpressionExecutor.getReturnType(), per.getQueryContextStartIndex(), per.getQueryContextEndIndex());
    }
    // Create within expression
    Expression withinExpression;
    Expression start = Expression.variable(additionalAttributes.get(0).getName());
    Expression end = Expression.variable(additionalAttributes.get(1).getName());
    Expression compareWithStartTime = Compare.compare(start, Compare.Operator.LESS_THAN_EQUAL, Expression.variable("AGG_TIMESTAMP"));
    Expression compareWithEndTime = Compare.compare(Expression.variable("AGG_TIMESTAMP"), Compare.Operator.LESS_THAN, end);
    withinExpression = Expression.and(compareWithStartTime, compareWithEndTime);
    // Create start and end time expression
    Expression startEndTimeExpression;
    if (within.getTimeRange().size() == 1) {
        startEndTimeExpression = new AttributeFunction("incrementalAggregator", "startTimeEndTime", within.getTimeRange().get(0));
    } else {
        // within.getTimeRange().size() == 2
        startEndTimeExpression = new AttributeFunction("incrementalAggregator", "startTimeEndTime", within.getTimeRange().get(0), within.getTimeRange().get(1));
    }
    ExpressionExecutor startTimeEndTimeExpressionExecutor = ExpressionParser.parseExpression(startEndTimeExpression, matchingMetaInfoHolder.getMetaStateEvent(), matchingMetaInfoHolder.getCurrentState(), tableMap, variableExpressionExecutors, siddhiAppContext, false, 0, queryName);
    // These compile conditions are used to check whether the aggregates in tables are within the given duration.
    for (Map.Entry<TimePeriod.Duration, Table> entry : aggregationTables.entrySet()) {
        CompiledCondition withinTableCompileCondition = entry.getValue().compileCondition(withinExpression, streamTableMetaInfoHolderWithStartEnd, siddhiAppContext, variableExpressionExecutors, tableMap, queryName);
        withinTableCompiledConditions.put(entry.getKey(), withinTableCompileCondition);
    }
    // Create compile condition for in-memory data.
    // This compile condition is used to check whether the running aggregates (in-memory data)
    // are within given duration
    withinInMemoryCompileCondition = OperatorParser.constructOperator(new ComplexEventChunk<>(true), withinExpression, streamTableMetaInfoHolderWithStartEnd, siddhiAppContext, variableExpressionExecutors, tableMap, queryName);
    // On compile condition.
    // After finding all the aggregates belonging to within duration, the final on condition (given as
    // "on stream1.name == aggregator.nickName ..." in the join query) must be executed on that data.
    // This condition is used for that purpose.
    onCompiledCondition = OperatorParser.constructOperator(new ComplexEventChunk<>(true), expression, matchingMetaInfoHolder, siddhiAppContext, variableExpressionExecutors, tableMap, queryName);
    return new IncrementalAggregateCompileCondition(withinTableCompiledConditions, withinInMemoryCompileCondition, onCompiledCondition, tableMetaStreamEvent, aggregateMetaSteamEvent, additionalAttributes, alteredMatchingMetaInfoHolder, perExpressionExecutor, startTimeEndTimeExpressionExecutor);
}
Also used : Table(org.wso2.siddhi.core.table.Table) VariableExpressionExecutor(org.wso2.siddhi.core.executor.VariableExpressionExecutor) ExpressionExecutor(org.wso2.siddhi.core.executor.ExpressionExecutor) ComplexEventChunk(org.wso2.siddhi.core.event.ComplexEventChunk) HashMap(java.util.HashMap) Attribute(org.wso2.siddhi.query.api.definition.Attribute) SiddhiAppCreationException(org.wso2.siddhi.core.exception.SiddhiAppCreationException) ArrayList(java.util.ArrayList) IncrementalAggregateCompileCondition(org.wso2.siddhi.core.util.collection.operator.IncrementalAggregateCompileCondition) AbstractDefinition(org.wso2.siddhi.query.api.definition.AbstractDefinition) AttributeFunction(org.wso2.siddhi.query.api.expression.AttributeFunction) CompiledCondition(org.wso2.siddhi.core.util.collection.operator.CompiledCondition) Expression(org.wso2.siddhi.query.api.expression.Expression) MatchingMetaInfoHolder(org.wso2.siddhi.core.util.collection.operator.MatchingMetaInfoHolder) HashMap(java.util.HashMap) Map(java.util.Map) MetaStreamEvent(org.wso2.siddhi.core.event.stream.MetaStreamEvent)

Aggregations

SiddhiAppCreationException (org.wso2.siddhi.core.exception.SiddhiAppCreationException)30 Attribute (org.wso2.siddhi.query.api.definition.Attribute)14 Expression (org.wso2.siddhi.query.api.expression.Expression)10 VariableExpressionExecutor (org.wso2.siddhi.core.executor.VariableExpressionExecutor)9 MetaStreamEvent (org.wso2.siddhi.core.event.stream.MetaStreamEvent)8 ExpressionExecutor (org.wso2.siddhi.core.executor.ExpressionExecutor)8 Element (org.wso2.siddhi.query.api.annotation.Element)7 ArrayList (java.util.ArrayList)6 AbstractDefinition (org.wso2.siddhi.query.api.definition.AbstractDefinition)6 Variable (org.wso2.siddhi.query.api.expression.Variable)6 HashMap (java.util.HashMap)5 ConfigReader (org.wso2.siddhi.core.util.config.ConfigReader)5 Annotation (org.wso2.siddhi.query.api.annotation.Annotation)5 ReturnAttribute (org.wso2.siddhi.annotation.ReturnAttribute)4 MetaStateEvent (org.wso2.siddhi.core.event.state.MetaStateEvent)4 Table (org.wso2.siddhi.core.table.Table)4 CompiledCondition (org.wso2.siddhi.core.util.collection.operator.CompiledCondition)4 MatchingMetaInfoHolder (org.wso2.siddhi.core.util.collection.operator.MatchingMetaInfoHolder)4 SiddhiAppValidationException (org.wso2.siddhi.query.api.exception.SiddhiAppValidationException)4 AttributeFunction (org.wso2.siddhi.query.api.expression.AttributeFunction)4