Search in sources :

Example 6 with Partition

use of io.siddhi.query.api.execution.partition.Partition in project siddhi by wso2.

the class DefinePartitionTestCase method test2.

@Test
public void test2() {
    Partition partition = SiddhiCompiler.parsePartition("partition with (symbol of cseEventStream) begin from " + "cseEventStream select sum(volume) as sumvolume insert into StockStream ;  end ");
    AssertJUnit.assertEquals(Partition.partition().with("cseEventStream", Expression.variable("symbol")).toString().split("queryList")[0], partition.toString().split("queryList")[0]);
}
Also used : Partition(io.siddhi.query.api.execution.partition.Partition) Test(org.testng.annotations.Test)

Example 7 with Partition

use of io.siddhi.query.api.execution.partition.Partition in project siddhi by wso2.

the class DefinePartitionTestCase method test1.

@Test
public void test1() {
    Partition partition = SiddhiCompiler.parsePartition("partition with (200>volume as 'LessValue' or 200<=volume" + " as 'HighValue' of cseEventStream) begin from cseEventStream select sum(volume) as sumvolume insert " + "into StockStream ;  end ");
    AssertJUnit.assertEquals(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")))).toString().split("queryList")[0], partition.toString().split("queryList")[0]);
}
Also used : Partition(io.siddhi.query.api.execution.partition.Partition) Test(org.testng.annotations.Test)

Example 8 with Partition

use of io.siddhi.query.api.execution.partition.Partition in project siddhi by wso2.

the class SiddhiCompiler method parsePartition.

public static Partition parsePartition(String partition) throws SiddhiParserException {
    CharStream input = CharStreams.fromString(partition);
    SiddhiQLLexer lexer = new SiddhiQLLexer(input);
    lexer.removeErrorListeners();
    lexer.addErrorListener(SiddhiErrorListener.INSTANCE);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    SiddhiQLParser parser = new SiddhiQLParser(tokens);
    parser.removeErrorListeners();
    parser.addErrorListener(SiddhiErrorListener.INSTANCE);
    ParseTree tree = parser.partition_final();
    SiddhiQLVisitor eval = new SiddhiQLBaseVisitorImpl();
    return (Partition) eval.visit(tree);
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) Partition(io.siddhi.query.api.execution.partition.Partition) SiddhiQLBaseVisitorImpl(io.siddhi.query.compiler.internal.SiddhiQLBaseVisitorImpl) CharStream(org.antlr.v4.runtime.CharStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 9 with Partition

use of io.siddhi.query.api.execution.partition.Partition in project siddhi by wso2.

the class SiddhiAppParser method getFindExecutedElements.

private static List<String> getFindExecutedElements(SiddhiApp siddhiApp) {
    List<String> findExecutedElements = new ArrayList<>();
    for (ExecutionElement executionElement : siddhiApp.getExecutionElementList()) {
        if (executionElement instanceof Query) {
            List<StreamHandler> streamHandlers = new ArrayList<>();
            if (((Query) executionElement).getInputStream() instanceof JoinInputStream) {
                findExecutedElements.addAll(((Query) executionElement).getInputStream().getAllStreamIds());
                streamHandlers.addAll(((SingleInputStream) ((JoinInputStream) ((Query) executionElement).getInputStream()).getLeftInputStream()).getStreamHandlers());
                streamHandlers.addAll(((SingleInputStream) ((JoinInputStream) ((Query) executionElement).getInputStream()).getRightInputStream()).getStreamHandlers());
            } else if (((Query) executionElement).getInputStream() instanceof SingleInputStream) {
                streamHandlers.addAll(((SingleInputStream) ((Query) executionElement).getInputStream()).getStreamHandlers());
            } else if (((Query) executionElement).getInputStream() instanceof StateInputStream) {
                streamHandlers.addAll((((StateInputStream) ((Query) executionElement).getInputStream()).getStreamHandlers()));
            }
            for (StreamHandler streamHandler : streamHandlers) {
                if (streamHandler instanceof In) {
                    findExecutedElements.add(((In) streamHandler).getSourceId());
                }
            }
        } else {
            List<Query> queries = ((Partition) executionElement).getQueryList();
            for (Query query : queries) {
                List<StreamHandler> streamHandlers = new ArrayList<>();
                if (query.getInputStream() instanceof JoinInputStream) {
                    findExecutedElements.addAll(query.getInputStream().getAllStreamIds());
                    streamHandlers.addAll(((SingleInputStream) ((JoinInputStream) query.getInputStream()).getLeftInputStream()).getStreamHandlers());
                    streamHandlers.addAll(((SingleInputStream) ((JoinInputStream) query.getInputStream()).getRightInputStream()).getStreamHandlers());
                } else if (query.getInputStream() instanceof SingleInputStream) {
                    streamHandlers.addAll(((SingleInputStream) query.getInputStream()).getStreamHandlers());
                } else if (query.getInputStream() instanceof StateInputStream) {
                    streamHandlers.addAll((((StateInputStream) query.getInputStream()).getStreamHandlers()));
                }
                for (StreamHandler streamHandler : streamHandlers) {
                    if (streamHandler instanceof In) {
                        findExecutedElements.add(((In) streamHandler).getSourceId());
                    }
                }
            }
        }
    }
    return findExecutedElements;
}
Also used : Partition(io.siddhi.query.api.execution.partition.Partition) Query(io.siddhi.query.api.execution.query.Query) In(io.siddhi.query.api.expression.condition.In) ArrayList(java.util.ArrayList) ExecutionElement(io.siddhi.query.api.execution.ExecutionElement) JoinInputStream(io.siddhi.query.api.execution.query.input.stream.JoinInputStream) SingleInputStream(io.siddhi.query.api.execution.query.input.stream.SingleInputStream) StreamHandler(io.siddhi.query.api.execution.query.input.handler.StreamHandler) StateInputStream(io.siddhi.query.api.execution.query.input.stream.StateInputStream)

Example 10 with Partition

use of io.siddhi.query.api.execution.partition.Partition 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)

Aggregations

Partition (io.siddhi.query.api.execution.partition.Partition)19 Query (io.siddhi.query.api.execution.query.Query)14 Test (org.testng.annotations.Test)13 SiddhiApp (io.siddhi.query.api.SiddhiApp)10 SiddhiAppRuntime (io.siddhi.core.SiddhiAppRuntime)8 SiddhiManager (io.siddhi.core.SiddhiManager)8 Event (io.siddhi.core.event.Event)8 InputHandler (io.siddhi.core.stream.input.InputHandler)8 StreamCallback (io.siddhi.core.stream.output.StreamCallback)8 StreamDefinition (io.siddhi.query.api.definition.StreamDefinition)7 ExecutionElement (io.siddhi.query.api.execution.ExecutionElement)3 SiddhiQLParser (io.siddhi.query.compiler.SiddhiQLParser)2 ArrayList (java.util.ArrayList)2 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)1 SiddhiAppContext (io.siddhi.core.config.SiddhiAppContext)1 SiddhiAppCreationException (io.siddhi.core.exception.SiddhiAppCreationException)1 PartitionRuntimeImpl (io.siddhi.core.partition.PartitionRuntimeImpl)1 QueryRuntimeImpl (io.siddhi.core.query.QueryRuntimeImpl)1 IdGenerator (io.siddhi.core.util.IdGenerator)1 SiddhiAppRuntimeBuilder (io.siddhi.core.util.SiddhiAppRuntimeBuilder)1