Search in sources :

Example 66 with Partition

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

the class Partition method addQuery.

public Partition addQuery(Query query) {
    if (query == null) {
        throw new SiddhiAppValidationException("Query should not be null");
    }
    String name = null;
    Element element = AnnotationHelper.getAnnotationElement(SiddhiConstants.ANNOTATION_INFO, SiddhiConstants.ANNOTATION_ELEMENT_NAME, query.getAnnotations());
    if (element != null) {
        name = element.getValue();
    }
    if (name != null && queryNameList.contains(name)) {
        throw new SiddhiAppValidationException("Cannot add Query as another Execution Element already uses " + "its name=" + name + " within the same Partition", element.getQueryContextStartIndex(), element.getQueryContextEndIndex());
    }
    queryNameList.add(name);
    this.queryList.add(query);
    return this;
}
Also used : Element(org.wso2.siddhi.query.api.annotation.Element) ExecutionElement(org.wso2.siddhi.query.api.execution.ExecutionElement) SiddhiElement(org.wso2.siddhi.query.api.SiddhiElement) SiddhiAppValidationException(org.wso2.siddhi.query.api.exception.SiddhiAppValidationException)

Example 67 with Partition

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

the class PartitionParser method parse.

public static PartitionRuntime parse(SiddhiAppRuntimeBuilder siddhiAppRuntimeBuilder, Partition partition, SiddhiAppContext siddhiAppContext, ConcurrentMap<String, AbstractDefinition> streamDefinitionMap, int queryIndex) {
    PartitionRuntime partitionRuntime = new PartitionRuntime(siddhiAppRuntimeBuilder.getStreamDefinitionMap(), siddhiAppRuntimeBuilder.getStreamJunctions(), partition, siddhiAppContext);
    validateStreamPartitions(partition.getPartitionTypeMap(), streamDefinitionMap);
    for (Query query : partition.getQueryList()) {
        List<VariableExpressionExecutor> executors = new ArrayList<VariableExpressionExecutor>();
        ConcurrentMap<String, AbstractDefinition> combinedStreamMap = new ConcurrentHashMap<String, AbstractDefinition>();
        combinedStreamMap.putAll(streamDefinitionMap);
        combinedStreamMap.putAll(partitionRuntime.getLocalStreamDefinitionMap());
        QueryRuntime queryRuntime = QueryParser.parse(query, siddhiAppContext, combinedStreamMap, siddhiAppRuntimeBuilder.getTableDefinitionMap(), siddhiAppRuntimeBuilder.getWindowDefinitionMap(), siddhiAppRuntimeBuilder.getAggregationDefinitionMap(), siddhiAppRuntimeBuilder.getTableMap(), siddhiAppRuntimeBuilder.getAggregationMap(), siddhiAppRuntimeBuilder.getWindowMap(), siddhiAppRuntimeBuilder.getLockSynchronizer(), String.valueOf(queryIndex));
        queryIndex++;
        MetaStateEvent metaStateEvent = createMetaEventForPartitioner(queryRuntime.getMetaComplexEvent());
        partitionRuntime.addQuery(queryRuntime);
        partitionRuntime.addPartitionReceiver(queryRuntime, executors, metaStateEvent);
        QueryParserHelper.reduceMetaComplexEvent(metaStateEvent);
        if (queryRuntime.getMetaComplexEvent() instanceof MetaStateEvent) {
            QueryParserHelper.updateVariablePosition(metaStateEvent, executors);
        } else {
            QueryParserHelper.updateVariablePosition(metaStateEvent.getMetaStreamEvent(0), executors);
        }
        partitionRuntime.init();
    }
    return partitionRuntime;
}
Also used : Query(org.wso2.siddhi.query.api.execution.query.Query) QueryRuntime(org.wso2.siddhi.core.query.QueryRuntime) VariableExpressionExecutor(org.wso2.siddhi.core.executor.VariableExpressionExecutor) ArrayList(java.util.ArrayList) AbstractDefinition(org.wso2.siddhi.query.api.definition.AbstractDefinition) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) PartitionRuntime(org.wso2.siddhi.core.partition.PartitionRuntime) MetaStateEvent(org.wso2.siddhi.core.event.state.MetaStateEvent)

Example 68 with Partition

use of org.wso2.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);
    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.setStatsEnabled(true);
        } 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.setStatsEnabled(true);
            }
        }
        Element statStateIncludElement = AnnotationHelper.getAnnotationElement(SiddhiConstants.ANNOTATION_STATISTICS, SiddhiConstants.ANNOTATION_ELEMENT_INCLUDE, siddhiApp.getAnnotations());
        siddhiAppContext.setIncludedMetrics(generateIncludedMetrics(statStateIncludElement));
        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;
            EventTimeBasedMillisTimestampGenerator timestampGenerator = new EventTimeBasedMillisTimestampGenerator(siddhiAppContext.getScheduledExecutorService());
            // 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 SystemCurrentTimeMillisTimestampGenerator());
        }
        siddhiAppContext.setSnapshotService(new SnapshotService(siddhiAppContext));
        siddhiAppContext.setPersistenceService(new PersistenceService(siddhiAppContext));
        siddhiAppContext.setElementIdGenerator(new ElementIdGenerator(siddhiAppContext.getName()));
    } 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);
    for (Window window : siddhiAppRuntimeBuilder.getWindowMap().values()) {
        try {
            window.init(siddhiAppRuntimeBuilder.getTableMap(), siddhiAppRuntimeBuilder.getWindowMap(), window.getWindowDefinition().getId());
        } catch (Throwable t) {
            ExceptionUtil.populateQueryContext(t, window.getWindowDefinition(), siddhiAppContext);
            throw t;
        }
    }
    int queryIndex = 1;
    for (ExecutionElement executionElement : siddhiApp.getExecutionElementList()) {
        if (executionElement instanceof Query) {
            try {
                QueryRuntime 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));
                siddhiAppRuntimeBuilder.addQuery(queryRuntime);
                queryIndex++;
            } catch (Throwable t) {
                ExceptionUtil.populateQueryContext(t, (Query) executionElement, siddhiAppContext);
                throw t;
            }
        } else {
            try {
                PartitionRuntime partitionRuntime = PartitionParser.parse(siddhiAppRuntimeBuilder, (Partition) executionElement, siddhiAppContext, siddhiAppRuntimeBuilder.getStreamDefinitionMap(), queryIndex);
                siddhiAppRuntimeBuilder.addPartition(partitionRuntime);
                siddhiAppContext.getSnapshotService().addSnapshotable("partition", partitionRuntime);
                queryIndex += ((Partition) executionElement).getQueryList().size();
            } 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(org.wso2.siddhi.query.api.execution.query.Query) Element(org.wso2.siddhi.query.api.annotation.Element) ExecutionElement(org.wso2.siddhi.query.api.execution.ExecutionElement) ArrayList(java.util.ArrayList) ElementIdGenerator(org.wso2.siddhi.core.util.ElementIdGenerator) EventTimeBasedMillisTimestampGenerator(org.wso2.siddhi.core.util.timestamp.EventTimeBasedMillisTimestampGenerator) ThreadBarrier(org.wso2.siddhi.core.util.ThreadBarrier) QueryRuntime(org.wso2.siddhi.core.query.QueryRuntime) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) Window(org.wso2.siddhi.core.window.Window) SnapshotService(org.wso2.siddhi.core.util.snapshot.SnapshotService) Partition(org.wso2.siddhi.query.api.execution.partition.Partition) SiddhiAppCreationException(org.wso2.siddhi.core.exception.SiddhiAppCreationException) SiddhiAppValidationException(org.wso2.siddhi.query.api.exception.SiddhiAppValidationException) Annotation(org.wso2.siddhi.query.api.annotation.Annotation) PersistenceService(org.wso2.siddhi.core.util.persistence.PersistenceService) SiddhiAppRuntimeBuilder(org.wso2.siddhi.core.util.SiddhiAppRuntimeBuilder) DuplicateAnnotationException(org.wso2.siddhi.query.api.exception.DuplicateAnnotationException) SiddhiParserException(org.wso2.siddhi.query.compiler.exception.SiddhiParserException) ExecutionElement(org.wso2.siddhi.query.api.execution.ExecutionElement) SystemCurrentTimeMillisTimestampGenerator(org.wso2.siddhi.core.util.timestamp.SystemCurrentTimeMillisTimestampGenerator) SiddhiAppContext(org.wso2.siddhi.core.config.SiddhiAppContext) PartitionRuntime(org.wso2.siddhi.core.partition.PartitionRuntime)

Example 69 with Partition

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

the class SnapshotService method restore.

public void restore(byte[] snapshot) throws CannotRestoreSiddhiAppStateException {
    Map<String, Map<String, Object>> snapshots = (Map<String, Map<String, Object>>) ByteSerializer.byteToObject(snapshot, siddhiAppContext);
    List<Snapshotable> snapshotableList;
    try {
        threadBarrier.lock();
        List<Snapshotable> partitionSnapshotables = snapshotableMap.get("partition");
        try {
            if (partitionSnapshotables != null) {
                for (Snapshotable snapshotable : partitionSnapshotables) {
                    snapshotable.restoreState(snapshots.get(snapshotable.getElementId()));
                }
            }
        } catch (Throwable t) {
            throw new CannotRestoreSiddhiAppStateException("Restoring of Siddhi app " + siddhiAppContext.getName() + " not completed properly because content of Siddhi app has changed since " + "last state persistence. Clean persistence store for a fresh deployment.", t);
        }
        for (Map.Entry<String, List<Snapshotable>> entry : snapshotableMap.entrySet()) {
            if (entry.getKey().equals("partition")) {
                continue;
            }
            snapshotableList = entry.getValue();
            try {
                for (Snapshotable snapshotable : snapshotableList) {
                    snapshotable.restoreState(snapshots.get(snapshotable.getElementId()));
                }
            } catch (Throwable t) {
                throw new CannotRestoreSiddhiAppStateException("Restoring of Siddhi app " + siddhiAppContext.getName() + " not completed properly because content of Siddhi app has changed since " + "last state persistence. Clean persistence store for a fresh deployment.", t);
            }
        }
    } finally {
        threadBarrier.unlock();
    }
}
Also used : CannotRestoreSiddhiAppStateException(org.wso2.siddhi.core.exception.CannotRestoreSiddhiAppStateException) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap)

Example 70 with Partition

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

the class PersistenceTestCase method persistenceTest12.

@Test
public void persistenceTest12() throws InterruptedException {
    log.info("persistence test 12 - partition query");
    PersistenceStore persistenceStore = new InMemoryPersistenceStore();
    SiddhiManager siddhiManager = new SiddhiManager();
    siddhiManager.setPersistenceStore(persistenceStore);
    String siddhiApp = "@App:name('TestPlan1')\n" + "define stream TempStream(deviceID long);\n" + "\n" + "define stream DeviceTempStream (deviceID long, count long);\n" + "\n" + "from TempStream\n" + "select * insert into TempInternalStream;\n" + "\n" + "partition with ( deviceID of TempInternalStream )\n" + "begin\n" + "from TempInternalStream\n" + "select deviceID, count(deviceID) as count\n" + "insert into DeviceTempStream\n" + "end;";
    StreamCallback queryCallback = new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            eventArrived = true;
            count++;
            lastValue = (Long) events[0].getData(1);
        }
    };
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("DeviceTempStream", queryCallback);
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("TempStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { 1 });
    Thread.sleep(100);
    inputHandler.send(new Object[] { 1 });
    Thread.sleep(100);
    inputHandler.send(new Object[] { 1 });
    Thread.sleep(100);
    inputHandler.send(new Object[] { 2 });
    Thread.sleep(100);
    inputHandler.send(new Object[] { 2 });
    Thread.sleep(100);
    // persisting
    Thread.sleep(500);
    siddhiAppRuntime.persist();
    inputHandler.send(new Object[] { 2 });
    Thread.sleep(100);
    inputHandler.send(new Object[] { 2 });
    // restarting siddhi app
    Thread.sleep(500);
    siddhiAppRuntime.shutdown();
    siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("DeviceTempStream", queryCallback);
    inputHandler = siddhiAppRuntime.getInputHandler("TempStream");
    siddhiAppRuntime.start();
    // loading
    try {
        siddhiAppRuntime.restoreLastRevision();
    } catch (CannotRestoreSiddhiAppStateException e) {
        Assert.fail("Restoring of Siddhi app " + siddhiAppRuntime.getName() + " failed");
    }
    inputHandler.send(new Object[] { 1 });
    Thread.sleep(10);
    inputHandler.send(new Object[] { 1 });
    Thread.sleep(10);
    inputHandler.send(new Object[] { 2 });
    Thread.sleep(10);
    inputHandler.send(new Object[] { 2 });
    // shutdown siddhi app
    Thread.sleep(500);
    siddhiAppRuntime.shutdown();
    AssertJUnit.assertTrue(count == 11);
    AssertJUnit.assertEquals(new Long(4), lastValue);
    AssertJUnit.assertEquals(true, eventArrived);
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) PersistenceStore(org.wso2.siddhi.core.util.persistence.PersistenceStore) InMemoryPersistenceStore(org.wso2.siddhi.core.util.persistence.InMemoryPersistenceStore) InMemoryPersistenceStore(org.wso2.siddhi.core.util.persistence.InMemoryPersistenceStore) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) CannotRestoreSiddhiAppStateException(org.wso2.siddhi.core.exception.CannotRestoreSiddhiAppStateException) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) StreamCallback(org.wso2.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)132 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)131 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)131 InputHandler (org.wso2.siddhi.core.stream.input.InputHandler)130 StreamCallback (org.wso2.siddhi.core.stream.output.StreamCallback)127 Event (org.wso2.siddhi.core.event.Event)125 Partition (org.wso2.siddhi.query.api.execution.partition.Partition)13 Query (org.wso2.siddhi.query.api.execution.query.Query)11 SiddhiApp (org.wso2.siddhi.query.api.SiddhiApp)7 ArrayList (java.util.ArrayList)5 CannotRestoreSiddhiAppStateException (org.wso2.siddhi.core.exception.CannotRestoreSiddhiAppStateException)4 StreamDefinition (org.wso2.siddhi.query.api.definition.StreamDefinition)4 SiddhiAppValidationException (org.wso2.siddhi.query.api.exception.SiddhiAppValidationException)4 ExecutionElement (org.wso2.siddhi.query.api.execution.ExecutionElement)4 QueryRuntime (org.wso2.siddhi.core.query.QueryRuntime)3 Element (org.wso2.siddhi.query.api.annotation.Element)3 List (java.util.List)2 TestUtil (org.wso2.siddhi.core.TestUtil)2 VariableExpressionExecutor (org.wso2.siddhi.core.executor.VariableExpressionExecutor)2 PartitionRuntime (org.wso2.siddhi.core.partition.PartitionRuntime)2