Search in sources :

Example 6 with Annotation

use of io.siddhi.query.api.annotation.Annotation in project siddhi by wso2.

the class SiddhiQLBaseVisitorImpl method visitApp_annotation.

/**
 * {@inheritDoc}
 * <p>The default implementation returns the result of calling
 * {@link #visitChildren} on {@code ctx}.</p>
 *
 * @param ctx
 */
@Override
public Annotation visitApp_annotation(@NotNull SiddhiQLParser.App_annotationContext ctx) {
    Annotation annotation = new Annotation((String) visit(ctx.name()));
    for (SiddhiQLParser.Annotation_elementContext elementContext : ctx.annotation_element()) {
        annotation.element((Element) visit(elementContext));
    }
    populateQueryContext(annotation, ctx);
    return annotation;
}
Also used : SiddhiQLParser(io.siddhi.query.compiler.SiddhiQLParser) Annotation(io.siddhi.query.api.annotation.Annotation)

Example 7 with Annotation

use of io.siddhi.query.api.annotation.Annotation in project siddhi by wso2.

the class SiddhiAppParser method parse.

/**
 * Parse an SiddhiApp returning SiddhiAppRuntime
 *
 * @param siddhiApp       plan to be parsed
 * @param siddhiAppString content of Siddhi application as string
 * @param siddhiContext   SiddhiContext  @return SiddhiAppRuntime
 * @return SiddhiAppRuntimeBuilder
 */
public static SiddhiAppRuntimeBuilder parse(SiddhiApp siddhiApp, String siddhiAppString, SiddhiContext siddhiContext) {
    SiddhiAppContext siddhiAppContext = new SiddhiAppContext();
    siddhiAppContext.setSiddhiContext(siddhiContext);
    siddhiAppContext.setSiddhiAppString(siddhiAppString);
    siddhiAppContext.setSiddhiApp(siddhiApp);
    try {
        Element element = AnnotationHelper.getAnnotationElement(SiddhiConstants.ANNOTATION_NAME, null, siddhiApp.getAnnotations());
        if (element != null) {
            siddhiAppContext.setName(element.getValue());
        } else {
            siddhiAppContext.setName(UUID.randomUUID().toString());
        }
        Annotation annotation = AnnotationHelper.getAnnotation(SiddhiConstants.ANNOTATION_ENFORCE_ORDER, siddhiApp.getAnnotations());
        if (annotation != null) {
            siddhiAppContext.setEnforceOrder(true);
        }
        annotation = AnnotationHelper.getAnnotation(SiddhiConstants.ANNOTATION_ASYNC, siddhiApp.getAnnotations());
        if (annotation != null) {
            throw new SiddhiAppCreationException("@Async not supported in SiddhiApp level, " + "instead use @Async with streams", annotation.getQueryContextStartIndex(), annotation.getQueryContextEndIndex());
        }
        annotation = AnnotationHelper.getAnnotation(SiddhiConstants.ANNOTATION_STATISTICS, siddhiApp.getAnnotations());
        List<Element> statisticsElements = new ArrayList<>();
        if (annotation != null) {
            statisticsElements = annotation.getElements();
        }
        if (siddhiContext.getStatisticsConfiguration() != null) {
            siddhiAppContext.setStatisticsManager(siddhiContext.getStatisticsConfiguration().getFactory().createStatisticsManager(siddhiContext.getStatisticsConfiguration().getMetricPrefix(), siddhiAppContext.getName(), statisticsElements));
        }
        Element statStateEnableElement = AnnotationHelper.getAnnotationElement(SiddhiConstants.ANNOTATION_STATISTICS, SiddhiConstants.ANNOTATION_ELEMENT_ENABLE, siddhiApp.getAnnotations());
        if (statStateEnableElement != null && Boolean.valueOf(statStateEnableElement.getValue())) {
            siddhiAppContext.setRootMetricsLevel(Level.BASIC);
        } else {
            Element statStateElement = AnnotationHelper.getAnnotationElement(SiddhiConstants.ANNOTATION_STATISTICS, null, siddhiApp.getAnnotations());
            // where sp uses @app:statistics('true').
            if (annotation != null && (statStateElement == null || Boolean.valueOf(statStateElement.getValue()))) {
                siddhiAppContext.setRootMetricsLevel(Level.BASIC);
            }
        }
        Element statStateIncludeElement = AnnotationHelper.getAnnotationElement(SiddhiConstants.ANNOTATION_STATISTICS, SiddhiConstants.ANNOTATION_ELEMENT_INCLUDE, siddhiApp.getAnnotations());
        siddhiAppContext.setIncludedMetrics(io.siddhi.core.util.parser.helper.AnnotationHelper.generateIncludedMetrics(statStateIncludeElement));
        Element transportCreationEnabledElement = AnnotationHelper.getAnnotationElement(SiddhiConstants.TRANSPORT_CHANNEL_CREATION_IDENTIFIER, null, siddhiApp.getAnnotations());
        if (transportCreationEnabledElement == null) {
            siddhiAppContext.setTransportChannelCreationEnabled(true);
        } else {
            siddhiAppContext.setTransportChannelCreationEnabled(Boolean.valueOf(transportCreationEnabledElement.getValue()));
        }
        siddhiAppContext.setThreadBarrier(new ThreadBarrier());
        siddhiAppContext.setExecutorService(Executors.newCachedThreadPool(new ThreadFactoryBuilder().setNameFormat("Siddhi-" + siddhiAppContext.getName() + "-executor-thread-%d").build()));
        siddhiAppContext.setScheduledExecutorService(Executors.newScheduledThreadPool(5, new ThreadFactoryBuilder().setNameFormat("Siddhi-" + siddhiAppContext.getName() + "-scheduler-thread-%d").build()));
        // Select the TimestampGenerator based on playback mode on/off
        annotation = AnnotationHelper.getAnnotation(SiddhiConstants.ANNOTATION_PLAYBACK, siddhiApp.getAnnotations());
        if (annotation != null) {
            String idleTime = null;
            String increment = null;
            TimestampGenerator timestampGenerator = new TimestampGeneratorImpl(siddhiAppContext);
            // Get the optional elements of playback annotation
            for (Element e : annotation.getElements()) {
                if (SiddhiConstants.ANNOTATION_ELEMENT_IDLE_TIME.equalsIgnoreCase(e.getKey())) {
                    idleTime = e.getValue();
                } else if (SiddhiConstants.ANNOTATION_ELEMENT_INCREMENT.equalsIgnoreCase(e.getKey())) {
                    increment = e.getValue();
                } else {
                    throw new SiddhiAppValidationException("Playback annotation accepts only idle.time and " + "increment but found " + e.getKey());
                }
            }
            // idleTime and increment are optional but if one presents, the other also should be given
            if (idleTime != null && increment == null) {
                throw new SiddhiAppValidationException("Playback annotation requires both idle.time and " + "increment but increment not found");
            } else if (idleTime == null && increment != null) {
                throw new SiddhiAppValidationException("Playback annotation requires both idle.time and " + "increment but idle.time does not found");
            } else if (idleTime != null) {
                // The fourth case idleTime == null && increment == null are ignored because it means no heartbeat.
                try {
                    timestampGenerator.setIdleTime(SiddhiCompiler.parseTimeConstantDefinition(idleTime).value());
                } catch (SiddhiParserException ex) {
                    throw new SiddhiParserException("Invalid idle.time constant '" + idleTime + "' in playback " + "annotation", ex);
                }
                try {
                    timestampGenerator.setIncrementInMilliseconds(SiddhiCompiler.parseTimeConstantDefinition(increment).value());
                } catch (SiddhiParserException ex) {
                    throw new SiddhiParserException("Invalid increment constant '" + increment + "' in playback " + "annotation", ex);
                }
            }
            siddhiAppContext.setTimestampGenerator(timestampGenerator);
            siddhiAppContext.setPlayback(true);
        } else {
            siddhiAppContext.setTimestampGenerator(new TimestampGeneratorImpl(siddhiAppContext));
        }
        siddhiAppContext.setSnapshotService(new SnapshotService(siddhiAppContext));
        siddhiAppContext.setIdGenerator(new IdGenerator());
    } catch (DuplicateAnnotationException e) {
        throw new DuplicateAnnotationException(e.getMessageWithOutContext() + " for the same Siddhi app " + siddhiApp.toString(), e, e.getQueryContextStartIndex(), e.getQueryContextEndIndex(), siddhiAppContext.getName(), siddhiAppContext.getSiddhiAppString());
    }
    SiddhiAppRuntimeBuilder siddhiAppRuntimeBuilder = new SiddhiAppRuntimeBuilder(siddhiAppContext);
    defineStreamDefinitions(siddhiAppRuntimeBuilder, siddhiApp.getStreamDefinitionMap(), siddhiAppContext);
    defineTableDefinitions(siddhiAppRuntimeBuilder, siddhiApp.getTableDefinitionMap(), siddhiAppContext);
    defineWindowDefinitions(siddhiAppRuntimeBuilder, siddhiApp.getWindowDefinitionMap(), siddhiAppContext);
    defineFunctionDefinitions(siddhiAppRuntimeBuilder, siddhiApp.getFunctionDefinitionMap(), siddhiAppContext);
    defineAggregationDefinitions(siddhiAppRuntimeBuilder, siddhiApp.getAggregationDefinitionMap(), siddhiAppContext);
    // todo fix for query API usecase
    List<String> findExecutedElements = getFindExecutedElements(siddhiApp);
    for (Window window : siddhiAppRuntimeBuilder.getWindowMap().values()) {
        try {
            window.init(siddhiAppRuntimeBuilder.getTableMap(), siddhiAppRuntimeBuilder.getWindowMap(), window.getWindowDefinition().getId(), findExecutedElements.contains(window.getWindowDefinition().getId()));
        } catch (Throwable t) {
            ExceptionUtil.populateQueryContext(t, window.getWindowDefinition(), siddhiAppContext);
            throw t;
        }
    }
    int queryIndex = 1;
    int partitionIndex = 1;
    for (ExecutionElement executionElement : siddhiApp.getExecutionElementList()) {
        if (executionElement instanceof Query) {
            try {
                QueryRuntimeImpl queryRuntime = QueryParser.parse((Query) executionElement, siddhiAppContext, siddhiAppRuntimeBuilder.getStreamDefinitionMap(), siddhiAppRuntimeBuilder.getTableDefinitionMap(), siddhiAppRuntimeBuilder.getWindowDefinitionMap(), siddhiAppRuntimeBuilder.getAggregationDefinitionMap(), siddhiAppRuntimeBuilder.getTableMap(), siddhiAppRuntimeBuilder.getAggregationMap(), siddhiAppRuntimeBuilder.getWindowMap(), siddhiAppRuntimeBuilder.getLockSynchronizer(), String.valueOf(queryIndex), false, SiddhiConstants.PARTITION_ID_DEFAULT);
                siddhiAppRuntimeBuilder.addQuery(queryRuntime);
                siddhiAppContext.addEternalReferencedHolder(queryRuntime);
                queryIndex++;
            } catch (Throwable t) {
                ExceptionUtil.populateQueryContext(t, (Query) executionElement, siddhiAppContext);
                throw t;
            }
        } else {
            try {
                PartitionRuntimeImpl partitionRuntime = PartitionParser.parse(siddhiAppRuntimeBuilder, (Partition) executionElement, siddhiAppContext, queryIndex, partitionIndex);
                siddhiAppRuntimeBuilder.addPartition(partitionRuntime);
                queryIndex += ((Partition) executionElement).getQueryList().size();
                partitionIndex++;
            } catch (Throwable t) {
                ExceptionUtil.populateQueryContext(t, (Partition) executionElement, siddhiAppContext);
                throw t;
            }
        }
    }
    // Done last as they have to be started last
    defineTriggerDefinitions(siddhiAppRuntimeBuilder, siddhiApp.getTriggerDefinitionMap(), siddhiAppContext);
    return siddhiAppRuntimeBuilder;
}
Also used : Query(io.siddhi.query.api.execution.query.Query) Element(io.siddhi.query.api.annotation.Element) ExecutionElement(io.siddhi.query.api.execution.ExecutionElement) ArrayList(java.util.ArrayList) ThreadBarrier(io.siddhi.core.util.ThreadBarrier) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) Window(io.siddhi.core.window.Window) SnapshotService(io.siddhi.core.util.snapshot.SnapshotService) Partition(io.siddhi.query.api.execution.partition.Partition) SiddhiAppCreationException(io.siddhi.core.exception.SiddhiAppCreationException) TimestampGenerator(io.siddhi.core.util.timestamp.TimestampGenerator) SiddhiAppValidationException(io.siddhi.query.api.exception.SiddhiAppValidationException) TimestampGeneratorImpl(io.siddhi.core.util.timestamp.TimestampGeneratorImpl) IdGenerator(io.siddhi.core.util.IdGenerator) Annotation(io.siddhi.query.api.annotation.Annotation) SiddhiAppRuntimeBuilder(io.siddhi.core.util.SiddhiAppRuntimeBuilder) DuplicateAnnotationException(io.siddhi.query.api.exception.DuplicateAnnotationException) QueryRuntimeImpl(io.siddhi.core.query.QueryRuntimeImpl) SiddhiParserException(io.siddhi.query.compiler.exception.SiddhiParserException) ExecutionElement(io.siddhi.query.api.execution.ExecutionElement) SiddhiAppContext(io.siddhi.core.config.SiddhiAppContext) PartitionRuntimeImpl(io.siddhi.core.partition.PartitionRuntimeImpl)

Example 8 with Annotation

use of io.siddhi.query.api.annotation.Annotation in project siddhi by wso2.

the class DefinitionParserHelper method addEventSource.

public static void addEventSource(StreamDefinition streamDefinition, ConcurrentMap<String, List<Source>> eventSourceMap, SiddhiAppContext siddhiAppContext) {
    for (Annotation sourceAnnotation : streamDefinition.getAnnotations()) {
        if (SiddhiConstants.ANNOTATION_SOURCE.equalsIgnoreCase(sourceAnnotation.getName())) {
            try {
                sourceAnnotation = updateAnnotationRef(sourceAnnotation, SiddhiConstants.NAMESPACE_SOURCE, siddhiAppContext);
                Annotation mapAnnotation = AnnotationHelper.getAnnotation(SiddhiConstants.ANNOTATION_MAP, sourceAnnotation.getAnnotations());
                if (mapAnnotation == null) {
                    mapAnnotation = Annotation.annotation(SiddhiConstants.ANNOTATION_MAP).element(SiddhiConstants.ANNOTATION_ELEMENT_TYPE, "passThrough");
                }
                final String sourceType = sourceAnnotation.getElement(SiddhiConstants.ANNOTATION_ELEMENT_TYPE);
                if (sourceType == null) {
                    throw new SiddhiAppCreationException("Attribute 'type' does not exist for annotation '" + sourceAnnotation + "'", sourceAnnotation, siddhiAppContext);
                }
                final String mapType = mapAnnotation.getElement(SiddhiConstants.ANNOTATION_ELEMENT_TYPE);
                if (mapType == null) {
                    throw new SiddhiAppCreationException("Attribute 'type' does not exist for annotation '" + mapAnnotation + "'", mapAnnotation, siddhiAppContext);
                }
                SourceHandlerManager sourceHandlerManager = siddhiAppContext.getSiddhiContext().getSourceHandlerManager();
                SourceHandler sourceHandler = null;
                if (sourceHandlerManager != null) {
                    sourceHandler = sourceHandlerManager.generateSourceHandler(sourceType);
                }
                // load input transport extension
                Extension sourceExtension = constructExtension(streamDefinition, SiddhiConstants.ANNOTATION_SOURCE, sourceType, sourceAnnotation, SiddhiConstants.NAMESPACE_SOURCE);
                Source source = (Source) SiddhiClassLoader.loadExtensionImplementation(sourceExtension, SourceExecutorExtensionHolder.getInstance(siddhiAppContext));
                ConfigReader configReader = siddhiAppContext.getSiddhiContext().getConfigManager().generateConfigReader(sourceExtension.getNamespace(), sourceExtension.getName());
                // load input mapper extension
                Extension mapperExtension = constructExtension(streamDefinition, SiddhiConstants.ANNOTATION_MAP, mapType, sourceAnnotation, SiddhiConstants.NAMESPACE_SOURCE_MAPPER);
                SourceMapper sourceMapper = (SourceMapper) SiddhiClassLoader.loadExtensionImplementation(mapperExtension, SourceMapperExecutorExtensionHolder.getInstance(siddhiAppContext));
                ConfigReader mapperConfigReader = siddhiAppContext.getSiddhiContext().getConfigManager().generateConfigReader(mapperExtension.getNamespace(), mapperExtension.getName());
                validateSourceMapperCompatibility(streamDefinition, sourceType, mapType, source, sourceMapper, sourceAnnotation);
                io.siddhi.annotation.Extension sourceExt = source.getClass().getAnnotation(io.siddhi.annotation.Extension.class);
                OptionHolder sourceOptionHolder = constructOptionHolder(streamDefinition, sourceAnnotation, sourceExt, null, true);
                Map<String, String> deploymentProperties = createDeploymentProperties(sourceAnnotation, sourceExt);
                OptionHolder mapOptionHolder = constructOptionHolder(streamDefinition, mapAnnotation, sourceMapper.getClass().getAnnotation(io.siddhi.annotation.Extension.class), null, false);
                AttributesHolder attributesHolder = getAttributeMappings(mapAnnotation, mapType, streamDefinition);
                String[] transportPropertyNames = getTransportPropertyNames(attributesHolder);
                source.init(sourceType, sourceOptionHolder, sourceMapper, transportPropertyNames, configReader, mapType, mapOptionHolder, attributesHolder.payloadMappings, attributesHolder.transportMappings, mapperConfigReader, sourceHandler, streamDefinition, deploymentProperties, siddhiAppContext);
                if (sourceHandlerManager != null) {
                    sourceHandlerManager.registerSourceHandler(sourceHandler.getId(), sourceHandler);
                }
                List<Source> eventSources = eventSourceMap.get(streamDefinition.getId());
                if (eventSources == null) {
                    eventSources = new ArrayList<>();
                    eventSources.add(source);
                    eventSourceMap.put(streamDefinition.getId(), eventSources);
                } else {
                    eventSources.add(source);
                }
            } catch (Throwable t) {
                ExceptionUtil.populateQueryContext(t, sourceAnnotation, siddhiAppContext);
                throw t;
            }
        }
    }
}
Also used : SourceHandler(io.siddhi.core.stream.input.source.SourceHandler) SiddhiAppCreationException(io.siddhi.core.exception.SiddhiAppCreationException) SourceHandlerManager(io.siddhi.core.stream.input.source.SourceHandlerManager) ConfigReader(io.siddhi.core.util.config.ConfigReader) Annotation(io.siddhi.query.api.annotation.Annotation) Source(io.siddhi.core.stream.input.source.Source) Extension(io.siddhi.query.api.extension.Extension) OptionHolder(io.siddhi.core.util.transport.OptionHolder) SourceMapper(io.siddhi.core.stream.input.source.SourceMapper)

Example 9 with Annotation

use of io.siddhi.query.api.annotation.Annotation in project siddhi by wso2.

the class DefinitionParserHelper method addEventSink.

public static void addEventSink(StreamDefinition streamDefinition, StreamJunction streamJunction, ConcurrentMap<String, List<Sink>> eventSinkMap, SiddhiAppContext siddhiAppContext) {
    for (Annotation sinkAnnotation : streamDefinition.getAnnotations()) {
        if (SiddhiConstants.ANNOTATION_SINK.equalsIgnoreCase(sinkAnnotation.getName())) {
            try {
                sinkAnnotation = updateAnnotationRef(sinkAnnotation, SiddhiConstants.NAMESPACE_SINK, siddhiAppContext);
                Annotation mapAnnotation = AnnotationHelper.getAnnotation(SiddhiConstants.ANNOTATION_MAP, sinkAnnotation.getAnnotations());
                String sinkType = sinkAnnotation.getElement(SiddhiConstants.ANNOTATION_ELEMENT_TYPE);
                if (sinkType == null) {
                    throw new SiddhiAppCreationException("Attribute 'type' does not exist for annotation '" + sinkAnnotation + "'", sinkAnnotation, siddhiAppContext);
                }
                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<>();
                    List<Map<String, String>> destinationDeploymentProperties = new ArrayList<>();
                    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);
                        destinationDeploymentProperties = createDestinationDeploymentProperties(distributionAnnotation, sink);
                    }
                    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));
                        io.siddhi.annotation.Extension sinkExt = sink.getClass().getAnnotation(io.siddhi.annotation.Extension.class);
                        OptionHolder transportOptionHolder = constructOptionHolder(streamDefinition, sinkAnnotation, sinkExt, supportedDynamicOptions, true);
                        Map<String, String> deploymentProperties = createDeploymentProperties(sinkAnnotation, sinkExt);
                        OptionHolder mapOptionHolder = constructOptionHolder(streamDefinition, mapAnnotation, sinkMapper.getClass().getAnnotation(io.siddhi.annotation.Extension.class), sinkMapper.getSupportedDynamicOptions(), false);
                        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, true);
                            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));
                            distributionStrategy.init(streamDefinition, transportOptionHolder, distributionOptHolder, destinationOptHolders, configReader);
                            ((DistributedTransport) sink).init(streamDefinition, sinkType, transportOptionHolder, sinkConfigReader, sinkMapper, mapType, mapOptionHolder, sinkHandler, payloadElementList, mapperConfigReader, streamJunction, siddhiAppContext, destinationOptHolders, sinkAnnotation, distributionStrategy, supportedDynamicOptions, deploymentProperties, destinationDeploymentProperties);
                        } else {
                            sink.init(streamDefinition, sinkType, transportOptionHolder, sinkConfigReader, sinkMapper, mapType, mapOptionHolder, sinkHandler, payloadElementList, mapperConfigReader, deploymentProperties, streamJunction, siddhiAppContext);
                        }
                        if (sinkHandlerManager != null) {
                            sinkHandlerManager.registerSinkHandler(sinkHandler.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);
                        }
                        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("Attribute 'type' does not exist for annotation '" + mapAnnotation + "'", mapAnnotation, siddhiAppContext);
                    }
                } else {
                    throw new SiddhiAppCreationException("Both @sink(type=) and @map(type=) are required.", sinkAnnotation.getQueryContextStartIndex(), sinkAnnotation.getQueryContextEndIndex());
                }
            } catch (Throwable t) {
                ExceptionUtil.populateQueryContext(t, sinkAnnotation, siddhiAppContext);
                throw t;
            }
        }
    }
}
Also used : Element(io.siddhi.query.api.annotation.Element) ArrayList(java.util.ArrayList) OutputGroupDeterminer(io.siddhi.core.stream.output.sink.OutputGroupDeterminer) SingleClientDistributedSink(io.siddhi.core.util.transport.SingleClientDistributedSink) SingleClientDistributedSink(io.siddhi.core.util.transport.SingleClientDistributedSink) MultiClientDistributedSink(io.siddhi.core.util.transport.MultiClientDistributedSink) Sink(io.siddhi.core.stream.output.sink.Sink) OptionHolder(io.siddhi.core.util.transport.OptionHolder) SinkMapper(io.siddhi.core.stream.output.sink.SinkMapper) SinkHandlerManager(io.siddhi.core.stream.output.sink.SinkHandlerManager) SinkHandler(io.siddhi.core.stream.output.sink.SinkHandler) DistributedTransport(io.siddhi.core.stream.output.sink.distributed.DistributedTransport) DistributionStrategy(io.siddhi.core.stream.output.sink.distributed.DistributionStrategy) SiddhiAppCreationException(io.siddhi.core.exception.SiddhiAppCreationException) ConfigReader(io.siddhi.core.util.config.ConfigReader) Annotation(io.siddhi.query.api.annotation.Annotation) Extension(io.siddhi.query.api.extension.Extension) MultiClientDistributedSink(io.siddhi.core.util.transport.MultiClientDistributedSink) Map(java.util.Map) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap)

Example 10 with Annotation

use of io.siddhi.query.api.annotation.Annotation in project siddhi by wso2.

the class DefineStreamTestCase method testAnnotatingStreamDefinition3.

@Test
public void testAnnotatingStreamDefinition3() {
    Element element = new Element("name", "query1");
    String annotationString = "[@map( type = \"xml\", namespace = \"h=uri, a=uri\", @attributes( \"//h:time\", " + "\"//h:data\"))]";
    String elementString = "name = \"query1\"";
    Annotation annotation = Annotation.annotation("source").element("type", "http").element("context", "/test").element("transport", "http,https").annotation(Annotation.annotation("map").element("type", "xml").element("namespace", "h=uri, a=uri").annotation(Annotation.annotation("attributes").element("//h:time").element("//h:data")));
    StreamDefinition streamDefinition = StreamDefinition.id("StockStream").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.INT).attribute("volume", Attribute.Type.FLOAT).annotation(annotation.element(element));
    annotation.setName("sink");
    AssertJUnit.assertEquals(annotationString, annotation.getAnnotations().toString());
    AssertJUnit.assertEquals(annotationString, annotation.getAnnotations("map").toString());
    AssertJUnit.assertEquals("sink", annotation.getName());
    AssertJUnit.assertEquals("http", annotation.getElement("type"));
    AssertJUnit.assertEquals("name", element.getKey());
    AssertJUnit.assertEquals("query1", element.getValue());
    AssertJUnit.assertEquals(elementString, element.toString());
}
Also used : StreamDefinition(io.siddhi.query.api.definition.StreamDefinition) Element(io.siddhi.query.api.annotation.Element) Annotation(io.siddhi.query.api.annotation.Annotation) Test(org.testng.annotations.Test)

Aggregations

Annotation (io.siddhi.query.api.annotation.Annotation)21 Attribute (io.siddhi.query.api.definition.Attribute)10 SiddhiAppCreationException (io.siddhi.core.exception.SiddhiAppCreationException)8 Element (io.siddhi.query.api.annotation.Element)8 ArrayList (java.util.ArrayList)7 MetaStreamEvent (io.siddhi.core.event.stream.MetaStreamEvent)6 TableDefinition (io.siddhi.query.api.definition.TableDefinition)6 HashMap (java.util.HashMap)6 StreamEventCloner (io.siddhi.core.event.stream.StreamEventCloner)5 InMemoryTable (io.siddhi.core.table.InMemoryTable)4 Table (io.siddhi.core.table.Table)4 ConfigReader (io.siddhi.core.util.config.ConfigReader)4 OutputAttribute (io.siddhi.query.api.execution.query.selection.OutputAttribute)4 Extension (io.siddhi.query.api.extension.Extension)4 Map (java.util.Map)4 SiddhiAppContext (io.siddhi.core.config.SiddhiAppContext)3 StreamEventFactory (io.siddhi.core.event.stream.StreamEventFactory)3 VariableExpressionExecutor (io.siddhi.core.executor.VariableExpressionExecutor)3 StreamDefinition (io.siddhi.query.api.definition.StreamDefinition)3 SiddhiQueryContext (io.siddhi.core.config.SiddhiQueryContext)2