Search in sources :

Example 31 with SiddhiAppRuntime

use of io.siddhi.core.SiddhiAppRuntime in project siddhi by wso2.

the class PersistenceTestCase method persistenceTest7.

@Test(dependsOnMethods = "persistenceTest6")
public void persistenceTest7() throws InterruptedException {
    log.info("persistence test 7 - external time window with group by query");
    PersistenceStore persistenceStore = new InMemoryPersistenceStore();
    SiddhiManager siddhiManager = new SiddhiManager();
    siddhiManager.setPersistenceStore(persistenceStore);
    String siddhiApp = "" + "@app:name('Test') " + "" + "define stream StockStream (symbol string, price float, volume int, timestamp long);" + "" + "@info(name = 'query1')" + "from StockStream#window.externalTime(timestamp,3 sec) " + "select symbol, price, sum(volume) as totalVol, timestamp " + "group by symbol " + "insert into OutStream ";
    QueryCallback queryCallback = new QueryCallback() {

        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            eventArrived = true;
            for (Event inEvent : inEvents) {
                count++;
                AssertJUnit.assertTrue("IBM".equals(inEvent.getData(0)) || "WSO2".equals(inEvent.getData(0)));
                if (count == 5) {
                    AssertJUnit.assertEquals(300L, inEvent.getData(2));
                }
                if (count == 6) {
                    AssertJUnit.assertEquals(100L, inEvent.getData(2));
                }
            }
        }
    };
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("query1", queryCallback);
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("StockStream");
    siddhiAppRuntime.start();
    long currentTime = 0;
    inputHandler.send(new Object[] { "IBM", 75.1f, 100, currentTime + 1000 });
    Thread.sleep(100);
    inputHandler.send(new Object[] { "WSO2", 75.2f, 100, currentTime + 2000 });
    Thread.sleep(100);
    inputHandler.send(new Object[] { "IBM", 75.3f, 100, currentTime + 3000 });
    Thread.sleep(500);
    AssertJUnit.assertTrue(eventArrived);
    AssertJUnit.assertEquals(3, count);
    // persisting
    Thread.sleep(500);
    siddhiAppRuntime.persist();
    // restarting siddhi app
    Thread.sleep(500);
    siddhiAppRuntime.shutdown();
    siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("query1", queryCallback);
    inputHandler = siddhiAppRuntime.getInputHandler("StockStream");
    siddhiAppRuntime.start();
    // loading
    try {
        siddhiAppRuntime.restoreLastRevision();
    } catch (CannotRestoreSiddhiAppStateException e) {
        Assert.fail("Restoring of Siddhi app " + siddhiAppRuntime.getName() + " failed");
    }
    inputHandler.send(new Object[] { "IBM", 75.4f, 100, currentTime + 4000 });
    Thread.sleep(100);
    inputHandler.send(new Object[] { "IBM", 75.5f, 100, currentTime + 5000 });
    Thread.sleep(100);
    inputHandler.send(new Object[] { "WSO2", 75.6f, 100, currentTime + 6000 });
    // shutdown siddhi app
    Thread.sleep(500);
    siddhiAppRuntime.shutdown();
    AssertJUnit.assertEquals(count, 6);
    AssertJUnit.assertEquals(true, eventArrived);
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) InMemoryPersistenceStore(io.siddhi.core.util.persistence.InMemoryPersistenceStore) PersistenceStore(io.siddhi.core.util.persistence.PersistenceStore) InMemoryPersistenceStore(io.siddhi.core.util.persistence.InMemoryPersistenceStore) Event(io.siddhi.core.event.Event) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) CannotRestoreSiddhiAppStateException(io.siddhi.core.exception.CannotRestoreSiddhiAppStateException) SiddhiManager(io.siddhi.core.SiddhiManager) QueryCallback(io.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 32 with SiddhiAppRuntime

use of io.siddhi.core.SiddhiAppRuntime in project siddhi by wso2.

the class PersistenceTestCase method persistenceTest9.

@Test(dependsOnMethods = "persistenceTest8")
public void persistenceTest9() throws InterruptedException {
    log.info("persistence test 9 - batch window query");
    PersistenceStore persistenceStore = new InMemoryPersistenceStore();
    SiddhiManager siddhiManager = new SiddhiManager();
    siddhiManager.setPersistenceStore(persistenceStore);
    String siddhiApp = "" + "@app:name('Test') " + "" + "define stream StockStream ( symbol string, price float, volume long );" + "" + "@info(name = 'query1')" + "from StockStream[price>10]#window.timeBatch(300) " + "select * " + "insert all events into OutStream ";
    QueryCallback queryCallback = new QueryCallback() {

        @Override
        public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            eventArrived = true;
            if (inEvents != null) {
                for (Event inEvent : inEvents) {
                    atomicCount.incrementAndGet();
                    AssertJUnit.assertTrue("IBM".equals(inEvent.getData(0)) || "WSO2".equals(inEvent.getData(0)));
                    lastValue = (Long) inEvent.getData(2);
                }
            }
        }
    };
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("query1", queryCallback);
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("StockStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "IBM", 75.6f, 100L });
    inputHandler.send(new Object[] { "WSO2", 75.6f, 101L });
    inputHandler.send(new Object[] { "IBM", 75.6f, 102L });
    Thread.sleep(400);
    inputHandler.send(new Object[] { "WSO2", 75.6f, 103L });
    inputHandler.send(new Object[] { "WSO2", 75.6f, 104L });
    Thread.sleep(100);
    AssertJUnit.assertTrue(eventArrived);
    // persisting
    siddhiAppRuntime.persist();
    inputHandler.send(new Object[] { "IBM", 75.6f, 105L });
    inputHandler.send(new Object[] { "WSO2", 75.6f, 106L });
    Thread.sleep(50);
    // restarting execution plan
    siddhiAppRuntime.shutdown();
    siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("query1", queryCallback);
    inputHandler = siddhiAppRuntime.getInputHandler("StockStream");
    siddhiAppRuntime.start();
    // loading
    try {
        siddhiAppRuntime.restoreLastRevision();
    } catch (CannotRestoreSiddhiAppStateException e) {
        Assert.fail("Restoring of Siddhi app " + siddhiAppRuntime.getName() + " failed");
    }
    inputHandler.send(new Object[] { "IBM", 75.6f, 107L });
    inputHandler.send(new Object[] { "IBM", 75.6f, 108L });
    Thread.sleep(10);
    SiddhiTestHelper.waitForEvents(100, 7, atomicCount, 10000);
    AssertJUnit.assertEquals(7, atomicCount.get());
    // shutdown siddhi app
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) InMemoryPersistenceStore(io.siddhi.core.util.persistence.InMemoryPersistenceStore) PersistenceStore(io.siddhi.core.util.persistence.PersistenceStore) InMemoryPersistenceStore(io.siddhi.core.util.persistence.InMemoryPersistenceStore) Event(io.siddhi.core.event.Event) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) CannotRestoreSiddhiAppStateException(io.siddhi.core.exception.CannotRestoreSiddhiAppStateException) SiddhiManager(io.siddhi.core.SiddhiManager) QueryCallback(io.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 33 with SiddhiAppRuntime

use of io.siddhi.core.SiddhiAppRuntime in project siddhi by wso2.

the class SelectOptimisationAggregationTestCase method aggregationFunctionTestcase11.

@Test(dependsOnMethods = "aggregationFunctionTestcase10")
public void aggregationFunctionTestcase11() throws InterruptedException {
    LOG.info("aggregationFunctionTestcase10: testing latest incremental aggregator - different select ");
    SiddhiManager siddhiManager = new SiddhiManager();
    String stockStream = "define stream stockStream (symbol string, price float, lastClosingPrice float, volume long , " + "quantity int, timestamp long);";
    String query = "define aggregation stockAggregation " + "from stockStream " + "select symbol, sum(price) as sumPrice  " + "group by symbol " + "aggregate by timestamp every sec...year ;" + "define stream inputStream (symbol string); " + "@info(name = 'query1') " + "from inputStream as i join stockAggregation as s " + "within 1496200000000L, 1596535449000L " + "per \"seconds\" " + "select s.symbol, sum(s.sumPrice) as sumPrice " + "group by i.symbol " + "order by AGG_TIMESTAMP " + "insert all events into outputStream; ";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(stockStream + query);
    try {
        siddhiAppRuntime.addCallback("query1", new QueryCallback() {

            @Override
            public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
                if (inEvents != null) {
                    EventPrinter.print(timestamp, inEvents, removeEvents);
                    for (Event event : inEvents) {
                        inEventsList.add(event.getData());
                        inEventCount.incrementAndGet();
                    }
                    eventArrived = true;
                }
            }
        });
        InputHandler stockStreamInputHandler = siddhiAppRuntime.getInputHandler("stockStream");
        InputHandler inputStreamInputHandler = siddhiAppRuntime.getInputHandler("inputStream");
        siddhiAppRuntime.start();
        // Thursday, June 1, 2017 4:05:50 AM
        stockStreamInputHandler.send(new Object[] { "WSO2", 50f, 60f, 90L, 6, 1496289950000L });
        stockStreamInputHandler.send(new Object[] { "WSO22", 75f, null, 40L, 10, 1496289950100L });
        // Thursday, June 1, 2017 4:05:52 AM
        stockStreamInputHandler.send(new Object[] { "WSO23", 60f, 44f, 200L, 56, 1496289952000L });
        stockStreamInputHandler.send(new Object[] { "WSO24", 100f, null, 200L, 16, 1496289952000L });
        // Thursday, June 1, 2017 4:05:50 AM - Out of order event
        stockStreamInputHandler.send(new Object[] { "WSO23", 70f, null, 40L, 10, 1496289950090L });
        // Thursday, June 1, 2017 4:05:54 AM
        stockStreamInputHandler.send(new Object[] { "IBM", 101f, null, 200L, 26, 1496289954000L });
        stockStreamInputHandler.send(new Object[] { "IBM1", 102f, null, 200L, 100, 1496289954000L });
        // Thursday, June 1, 2017 4:05:56 AM
        stockStreamInputHandler.send(new Object[] { "IBM", 900f, null, 200L, 60, 1496289956000L });
        stockStreamInputHandler.send(new Object[] { "IBM1", 500f, null, 200L, 7, 1496289956000L });
        Thread.sleep(100);
        inputStreamInputHandler.send(new Object[] { "IBM" });
        Thread.sleep(100);
        SiddhiTestHelper.waitForEvents(100, 1, inEventCount, 10000);
        AssertJUnit.assertTrue("Event arrived", eventArrived);
        AssertJUnit.assertEquals("Number of success events", 1, inEventCount.get());
        AssertJUnit.assertTrue("In events matched", Arrays.equals(inEventsList.get(0), new Object[] { "WSO22", 1958.0 }));
    } finally {
        siddhiAppRuntime.shutdown();
    }
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) SiddhiManager(io.siddhi.core.SiddhiManager) QueryCallback(io.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 34 with SiddhiAppRuntime

use of io.siddhi.core.SiddhiAppRuntime in project siddhi by wso2.

the class SelectOptimisationAggregationTestCase method aggregationFunctionTestcase12.

@Test(dependsOnMethods = "aggregationFunctionTestcase11")
public void aggregationFunctionTestcase12() throws InterruptedException {
    LOG.info("aggregationFunctionTestcase11: testing latest incremental aggregator - different select ");
    SiddhiManager siddhiManager = new SiddhiManager();
    String stockStream = "define stream stockStream (symbol string, price float, lastClosingPrice float, volume long , " + "quantity int, timestamp long);";
    String query = "define aggregation stockAggregation " + "from stockStream " + "select symbol, sum(price) as sumPrice " + "group by symbol " + "aggregate by timestamp every sec...year ;" + "define stream inputStream (symbol string); " + "@info(name = 'query1') " + "from inputStream as i join stockAggregation as s " + "within 1496200000000L, 1596535449000L " + "per \"seconds\" " + "select i.symbol, sum(sumPrice) as totalPrice " + "group by i.symbol " + "order by AGG_TIMESTAMP " + "insert all events into outputStream; ";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(stockStream + query);
    try {
        siddhiAppRuntime.addCallback("query1", new QueryCallback() {

            @Override
            public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
                if (inEvents != null) {
                    EventPrinter.print(timestamp, inEvents, removeEvents);
                    for (Event event : inEvents) {
                        inEventsList.add(event.getData());
                        inEventCount.incrementAndGet();
                    }
                    eventArrived = true;
                }
            }
        });
        InputHandler stockStreamInputHandler = siddhiAppRuntime.getInputHandler("stockStream");
        InputHandler inputStreamInputHandler = siddhiAppRuntime.getInputHandler("inputStream");
        siddhiAppRuntime.start();
        // Thursday, June 1, 2017 4:05:50 AM
        stockStreamInputHandler.send(new Object[] { "WSO2", 50f, 60f, 90L, 6, 1496289950000L });
        stockStreamInputHandler.send(new Object[] { "WSO22", 75f, null, 40L, 10, 1496289950100L });
        // Thursday, June 1, 2017 4:05:52 AM
        stockStreamInputHandler.send(new Object[] { "WSO23", 60f, 44f, 200L, 56, 1496289952000L });
        stockStreamInputHandler.send(new Object[] { "WSO24", 100f, null, 200L, 16, 1496289952000L });
        // Thursday, June 1, 2017 4:05:50 AM - Out of order event
        stockStreamInputHandler.send(new Object[] { "WSO23", 70f, null, 40L, 10, 1496289950090L });
        // Thursday, June 1, 2017 4:05:54 AM
        stockStreamInputHandler.send(new Object[] { "IBM", 101f, null, 200L, 26, 1496289954000L });
        stockStreamInputHandler.send(new Object[] { "IBM1", 102f, null, 200L, 100, 1496289954000L });
        // Thursday, June 1, 2017 4:05:56 AM
        stockStreamInputHandler.send(new Object[] { "IBM", 900f, null, 200L, 60, 1496289956000L });
        stockStreamInputHandler.send(new Object[] { "IBM1", 500f, null, 200L, 7, 1496289956000L });
        Thread.sleep(100);
        inputStreamInputHandler.send(new Object[] { "IBM" });
        Thread.sleep(100);
        SiddhiTestHelper.waitForEvents(100, 1, inEventCount, 10000);
        AssertJUnit.assertTrue("Event arrived", eventArrived);
        AssertJUnit.assertEquals("Number of success events", 1, inEventCount.get());
        AssertJUnit.assertTrue("In events matched", Arrays.equals(inEventsList.get(0), new Object[] { "IBM", 1958.0 }));
    } finally {
        siddhiAppRuntime.shutdown();
    }
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) SiddhiManager(io.siddhi.core.SiddhiManager) QueryCallback(io.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 35 with SiddhiAppRuntime

use of io.siddhi.core.SiddhiAppRuntime in project siddhi by wso2.

the class TestDebugger method testDebugger12.

@Test
public void testDebugger12() throws InterruptedException {
    log.info("Siddi Debugger Test 12: Test debugging two queries with concurrent input");
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "@config(async = 'true') " + "define stream cseEventStream (symbol string, price float, volume int); " + "define stream stockEventStream (symbol string, price float, volume int); ";
    final String query = "@info(name = 'query1')" + "from cseEventStream " + "select * " + "insert into OutputStream1; " + "@info(name = 'query2')" + "from stockEventStream " + "select * " + "insert into OutputStream2;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);
    siddhiAppRuntime.addCallback("OutputStream1", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            inEventCount.addAndGet(events.length);
        }
    });
    siddhiAppRuntime.addCallback("OutputStream2", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            inEventCount.addAndGet(events.length);
        }
    });
    final InputHandler cseEventStreamInputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    final InputHandler stockEventStreamInputHandler = siddhiAppRuntime.getInputHandler("stockEventStream");
    SiddhiDebugger siddhiDebugger = siddhiAppRuntime.debug();
    siddhiDebugger.acquireBreakPoint("query1", SiddhiDebugger.QueryTerminal.IN);
    siddhiDebugger.acquireBreakPoint("query2", SiddhiDebugger.QueryTerminal.IN);
    siddhiDebugger.setDebuggerCallback(new SiddhiDebuggerCallback() {

        private AtomicBoolean queryOneResumed = new AtomicBoolean(false);

        @Override
        public void debugEvent(ComplexEvent event, String queryName, SiddhiDebugger.QueryTerminal queryTerminal, SiddhiDebugger debugger) {
            log.info("Query: " + queryName + ":" + queryTerminal);
            log.info(event);
            debugEventCount.addAndGet(getCount(event));
            if ("query1IN".equals(queryName)) {
                try {
                    // Wait for 1 sec
                    Thread.sleep(1000);
                    this.queryOneResumed.set(true);
                } catch (InterruptedException e) {
                }
                AssertJUnit.assertArrayEquals("Incorrect debug event received", new Object[] { "WSO2", 50f, 60 }, event.getOutputData());
            } else if ("query2IN".equals(queryName)) {
                // If query2IN is reached, query1IN must left that break point
                AssertJUnit.assertTrue("Query 2 thread enterted the checkpoint before query 1 is debugged", queryOneResumed.get());
                AssertJUnit.assertArrayEquals("Incorrect debug event received", new Object[] { "IBM", 45f, 80 }, event.getOutputData());
            }
            debugger.next();
        }
    });
    new Thread() {

        @Override
        public void run() {
            try {
                cseEventStreamInputHandler.send(new Object[] { "WSO2", 50f, 60 });
            } catch (InterruptedException e) {
            }
        }
    }.start();
    new Thread() {

        @Override
        public void run() {
            try {
                Thread.sleep(10);
                stockEventStreamInputHandler.send(new Object[] { "IBM", 45f, 80 });
            } catch (InterruptedException e) {
            }
        }
    }.start();
    Thread.sleep(2000);
    AssertJUnit.assertEquals("Invalid number of output events", 2, inEventCount.get());
    AssertJUnit.assertEquals("Invalid number of debug events", 4, debugEventCount.get());
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) StreamCallback(io.siddhi.core.stream.output.StreamCallback) ComplexEvent(io.siddhi.core.event.ComplexEvent) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) ComplexEvent(io.siddhi.core.event.ComplexEvent) StreamEvent(io.siddhi.core.event.stream.StreamEvent) SiddhiManager(io.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Aggregations

SiddhiAppRuntime (io.siddhi.core.SiddhiAppRuntime)1761 SiddhiManager (io.siddhi.core.SiddhiManager)1756 Test (org.testng.annotations.Test)1744 InputHandler (io.siddhi.core.stream.input.InputHandler)1590 Event (io.siddhi.core.event.Event)1257 QueryCallback (io.siddhi.core.query.output.callback.QueryCallback)823 StreamCallback (io.siddhi.core.stream.output.StreamCallback)305 TestUtil (io.siddhi.core.TestUtil)304 SiddhiApp (io.siddhi.query.api.SiddhiApp)90 Query (io.siddhi.query.api.execution.query.Query)85 StreamDefinition (io.siddhi.query.api.definition.StreamDefinition)84 ArrayList (java.util.ArrayList)73 Logger (org.apache.logging.log4j.core.Logger)57 TestAppenderToValidateLogsForCachingTests (io.siddhi.core.query.table.util.TestAppenderToValidateLogsForCachingTests)34 InMemoryBroker (io.siddhi.core.util.transport.InMemoryBroker)32 CannotRestoreSiddhiAppStateException (io.siddhi.core.exception.CannotRestoreSiddhiAppStateException)30 HashMap (java.util.HashMap)25 UnitTestAppender (io.siddhi.core.UnitTestAppender)23 InMemoryConfigManager (io.siddhi.core.util.config.InMemoryConfigManager)21 SiddhiAppCreationException (io.siddhi.core.exception.SiddhiAppCreationException)15