Search in sources :

Example 11 with StreamDefinition

use of org.wso2.siddhi.query.api.definition.StreamDefinition 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 12 with StreamDefinition

use of org.wso2.siddhi.query.api.definition.StreamDefinition in project siddhi by wso2.

the class DefinitionParserHelper method validateDefinition.

public static void validateDefinition(AbstractDefinition definition, ConcurrentMap<String, AbstractDefinition> streamDefinitionMap, ConcurrentMap<String, AbstractDefinition> tableDefinitionMap, ConcurrentMap<String, AbstractDefinition> windowDefinitionMap, ConcurrentMap<String, AbstractDefinition> aggregationDefinitionMap) {
    AbstractDefinition existingTableDefinition = tableDefinitionMap.get(definition.getId());
    if (existingTableDefinition != null && (!existingTableDefinition.equals(definition) || definition instanceof StreamDefinition)) {
        throw new DuplicateDefinitionException("Table Definition with same Stream Id '" + definition.getId() + "' already exist : " + existingTableDefinition + ", hence cannot add " + definition, definition.getQueryContextStartIndex(), definition.getQueryContextEndIndex());
    }
    AbstractDefinition existingStreamDefinition = streamDefinitionMap.get(definition.getId());
    if (existingStreamDefinition != null && (!existingStreamDefinition.equals(definition) || definition instanceof TableDefinition)) {
        throw new DuplicateDefinitionException("Stream Definition with same Stream Id '" + definition.getId() + "' already exist : " + existingStreamDefinition + ", hence cannot add " + definition, definition.getQueryContextStartIndex(), definition.getQueryContextEndIndex());
    }
    AbstractDefinition existingWindowDefinition = windowDefinitionMap.get(definition.getId());
    if (existingWindowDefinition != null && (!existingWindowDefinition.equals(definition) || definition instanceof WindowDefinition)) {
        throw new DuplicateDefinitionException("Window Definition with same Window Id '" + definition.getId() + "' already exist : " + existingWindowDefinition + ", hence cannot add " + definition, definition.getQueryContextStartIndex(), definition.getQueryContextEndIndex());
    }
    AbstractDefinition existingAggregationDefinition = aggregationDefinitionMap.get(definition.getId());
    if (existingAggregationDefinition != null && (!existingAggregationDefinition.equals(definition) || definition instanceof AggregationDefinition)) {
        throw new DuplicateDefinitionException("Aggregation Definition with same Aggregation Id '" + definition.getId() + "' already exist : " + existingWindowDefinition + ", hence cannot add " + definition, definition.getQueryContextStartIndex(), definition.getQueryContextEndIndex());
    }
}
Also used : StreamDefinition(org.wso2.siddhi.query.api.definition.StreamDefinition) DuplicateDefinitionException(org.wso2.siddhi.query.api.exception.DuplicateDefinitionException) AggregationDefinition(org.wso2.siddhi.query.api.definition.AggregationDefinition) AbstractDefinition(org.wso2.siddhi.query.api.definition.AbstractDefinition) TableDefinition(org.wso2.siddhi.query.api.definition.TableDefinition) WindowDefinition(org.wso2.siddhi.query.api.definition.WindowDefinition)

Example 13 with StreamDefinition

use of org.wso2.siddhi.query.api.definition.StreamDefinition 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 StreamDefinition

use of org.wso2.siddhi.query.api.definition.StreamDefinition in project siddhi by wso2.

the class PartitionTestCase1 method testPartitionQuery3.

@Test
public void testPartitionQuery3() throws InterruptedException {
    log.info("Partition test3");
    SiddhiManager siddhiManager = new SiddhiManager();
    SiddhiApp siddhiApp = new SiddhiApp("plan3");
    StreamDefinition streamDefinition = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.FLOAT).attribute("volume", Attribute.Type.INT);
    siddhiApp.defineStream(streamDefinition);
    Partition partition = Partition.partition().with("cseEventStream", Expression.variable("symbol"));
    Query query = Query.query();
    query.from(InputStream.stream("cseEventStream"));
    query.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("price", Expression.variable("price")).select("volume", Expression.variable("volume")));
    query.insertIntoInner("StockStream");
    Query query1 = Query.query();
    query1.from(InputStream.innerStream("StockStream"));
    query1.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("price", Expression.variable("price")).select("volume", Expression.variable("volume")));
    query1.insertInto("OutStockStream");
    partition.addQuery(query);
    partition.addQuery(query1);
    siddhiApp.addPartition(partition);
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("OutStockStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            count.addAndGet(events.length);
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "IBM", 75.6f, 100 });
    inputHandler.send(new Object[] { "WSO2", 75.6f, 100 });
    inputHandler.send(new Object[] { "IBM", 75.6f, 100 });
    inputHandler.send(new Object[] { "ORACLE", 75.6f, 100 });
    SiddhiTestHelper.waitForEvents(100, 4, count, 60000);
    siddhiAppRuntime.shutdown();
    AssertJUnit.assertEquals(4, count.get());
}
Also used : Partition(org.wso2.siddhi.query.api.execution.partition.Partition) InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) SiddhiApp(org.wso2.siddhi.query.api.SiddhiApp) StreamDefinition(org.wso2.siddhi.query.api.definition.StreamDefinition) Query(org.wso2.siddhi.query.api.execution.query.Query) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) StreamCallback(org.wso2.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 15 with StreamDefinition

use of org.wso2.siddhi.query.api.definition.StreamDefinition in project siddhi by wso2.

the class PartitionTestCase1 method testPartitionQuery17.

@Test
public void testPartitionQuery17() throws InterruptedException {
    log.info("Partition test17");
    SiddhiManager siddhiManager = new SiddhiManager();
    SiddhiApp siddhiApp = new SiddhiApp("plan17");
    StreamDefinition streamDefinition = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.FLOAT).attribute("volume", Attribute.Type.INT);
    siddhiApp.defineStream(streamDefinition);
    Partition partition = Partition.partition().with("cseEventStream", Partition.range("LessValue", Expression.compare(Expression.value(200), Compare.Operator.GREATER_THAN, Expression.variable("volume"))), Partition.range("HighValue", Expression.compare(Expression.value(200), Compare.Operator.LESS_THAN_EQUAL, Expression.variable("volume"))));
    Query query = Query.query();
    query.from(InputStream.stream("cseEventStream"));
    query.select(Selector.selector().select("sumvolume", Expression.function("sum", Expression.variable("volume"))));
    query.insertInto("StockStream");
    partition.addQuery(query);
    siddhiApp.addPartition(partition);
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("StockStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            for (Event event : events) {
                count.incrementAndGet();
                if (count.get() == 1) {
                    AssertJUnit.assertEquals(100L, event.getData()[0]);
                } else if (count.get() == 2) {
                    AssertJUnit.assertEquals(600L, event.getData()[0]);
                } else if (count.get() == 3) {
                    AssertJUnit.assertEquals(200L, event.getData()[0]);
                } else if (count.get() == 4) {
                    AssertJUnit.assertEquals(250L, event.getData()[0]);
                }
                eventArrived = true;
            }
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "IBM", 75.6f, 100 });
    inputHandler.send(new Object[] { "WSO2", 75.6f, 600 });
    inputHandler.send(new Object[] { "IBM", 75.6f, 100 });
    Thread.sleep(100);
    inputHandler.send(new Object[] { "ORACLE", 75.6f, 50 });
    SiddhiTestHelper.waitForEvents(100, 4, count, 60000);
    AssertJUnit.assertEquals(4, count.get());
    siddhiAppRuntime.shutdown();
}
Also used : Partition(org.wso2.siddhi.query.api.execution.partition.Partition) InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) SiddhiApp(org.wso2.siddhi.query.api.SiddhiApp) StreamDefinition(org.wso2.siddhi.query.api.definition.StreamDefinition) Query(org.wso2.siddhi.query.api.execution.query.Query) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) StreamCallback(org.wso2.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Aggregations

StreamDefinition (org.wso2.siddhi.query.api.definition.StreamDefinition)110 Test (org.testng.annotations.Test)105 Event (org.wso2.siddhi.core.event.Event)84 Query (org.wso2.siddhi.query.api.execution.query.Query)83 SiddhiApp (org.wso2.siddhi.query.api.SiddhiApp)81 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)80 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)80 InputHandler (org.wso2.siddhi.core.stream.input.InputHandler)77 QueryCallback (org.wso2.siddhi.core.query.output.callback.QueryCallback)73 Attribute (org.wso2.siddhi.query.api.definition.Attribute)15 SiddhiAppCreationException (org.wso2.siddhi.core.exception.SiddhiAppCreationException)12 MetaStreamEvent (org.wso2.siddhi.core.event.stream.MetaStreamEvent)11 StreamEvent (org.wso2.siddhi.core.event.stream.StreamEvent)10 StreamCallback (org.wso2.siddhi.core.stream.output.StreamCallback)10 ArrayList (java.util.ArrayList)9 TableDefinition (org.wso2.siddhi.query.api.definition.TableDefinition)8 VariableExpressionExecutor (org.wso2.siddhi.core.executor.VariableExpressionExecutor)7 StreamEventPool (org.wso2.siddhi.core.event.stream.StreamEventPool)6 ExpressionExecutor (org.wso2.siddhi.core.executor.ExpressionExecutor)6 MetaStateEvent (org.wso2.siddhi.core.event.state.MetaStateEvent)5