Search in sources :

Example 16 with QueryCallback

use of io.siddhi.core.query.output.callback.QueryCallback in project siddhi by wso2.

the class QuerySyncTestCase method querySyncTest4.

@Test
public void querySyncTest4() throws InterruptedException {
    log.info("querySync test4");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream Stream1 (symbol string, price float, volume int); " + "define stream Stream2 (symbol string, price float, volume int); ";
    String query = "" + "@info(name = 'query1') " + "@synchronized('true') " + "from every ( e1=Stream1[price>20] -> e3=Stream1[price>20]) -> e2=Stream2[price>e1.price] " + "select e1.price as price1, e3.price as price3, e2.price as price2 " + "insert into OutputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    siddhiAppRuntime.addCallback("query1", new QueryCallback() {

        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            if (inEvents != null) {
                for (Event event : inEvents) {
                    inEventCount.incrementAndGet();
                    switch(inEventCount.get()) {
                        case 1:
                            org.testng.AssertJUnit.assertArrayEquals(new Object[] { 55.6f, 54f, 57.7f }, event.getData());
                            break;
                        case 2:
                            org.testng.AssertJUnit.assertArrayEquals(new Object[] { 53.6f, 53f, 57.7f }, event.getData());
                            break;
                        default:
                            org.testng.AssertJUnit.assertSame(2, inEventCount);
                    }
                }
                eventArrived = true;
            }
            if (removeEvents != null) {
                removeEventCount.addAndGet(removeEvents.length);
            }
            eventArrived = true;
        }
    });
    InputHandler stream1 = siddhiAppRuntime.getInputHandler("Stream1");
    InputHandler stream2 = siddhiAppRuntime.getInputHandler("Stream2");
    siddhiAppRuntime.start();
    stream1.send(new Object[] { "WSO2", 55.6f, 100 });
    Thread.sleep(100);
    stream1.send(new Object[] { "GOOG", 54f, 100 });
    Thread.sleep(100);
    stream1.send(new Object[] { "WSO2", 53.6f, 100 });
    Thread.sleep(100);
    stream1.send(new Object[] { "GOOG", 53f, 100 });
    Thread.sleep(100);
    stream2.send(new Object[] { "IBM", 57.7f, 100 });
    Thread.sleep(100);
    org.testng.AssertJUnit.assertEquals("Number of success events", 2, inEventCount.get());
    org.testng.AssertJUnit.assertEquals("Number of remove events", 0, removeEventCount.get());
    org.testng.AssertJUnit.assertEquals("Event arrived", true, eventArrived);
    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 17 with QueryCallback

use of io.siddhi.core.query.output.callback.QueryCallback in project siddhi by wso2.

the class IncrementalPersistenceTestCase method incrementalPersistenceTest2.

@Test
public void incrementalPersistenceTest2() throws InterruptedException {
    log.info("Incremental persistence test 2 - length batch window query");
    final int inputEventCount = 10;
    final int eventWindowSize = 4;
    SiddhiManager siddhiManager = new SiddhiManager();
    siddhiManager.setIncrementalPersistenceStore(new IncrementalFileSystemPersistenceStore(storageFilePath));
    String siddhiApp = "" + "@app:name('incrementalPersistenceTest2') " + "" + "define stream StockStream ( symbol string, price float, volume int );" + "" + "@info(name = 'query1')" + "from StockStream[price>10]#window.lengthBatch(" + eventWindowSize + ") " + "select symbol, price, sum(volume) as totalVol " + "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;
            for (Event inEvent : inEvents) {
                count++;
                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();
    for (int i = 0; i < inputEventCount; i++) {
        inputHandler.send(new Object[] { "IBM", 75.6f + i, 100 });
    }
    Thread.sleep(100);
    AssertJUnit.assertTrue(eventArrived);
    AssertJUnit.assertEquals(new Long(400), lastValue);
    // persisting
    siddhiAppRuntime.persist();
    Thread.sleep(5000);
    inputHandler.send(new Object[] { "WSO2", 100.4f, 150 });
    // Thread.sleep(100);
    inputHandler.send(new Object[] { "WSO2", 200.4f, 110 });
    inputHandler.send(new Object[] { "IBM", 300.4f, 100 });
    // Thread.sleep(100);
    inputHandler.send(new Object[] { "WSO2", 400.4f, 300 });
    inputHandler.send(new Object[] { "IBM", 500.6f, 120 });
    Thread.sleep(10);
    inputHandler.send(new Object[] { "WSO2", 600.6f, 400 });
    Thread.sleep(100);
    siddhiAppRuntime.persist();
    Thread.sleep(5000);
    siddhiAppRuntime.shutdown();
    siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("query1", queryCallback);
    inputHandler = siddhiAppRuntime.getInputHandler("StockStream");
    // loading
    try {
        siddhiAppRuntime.restoreLastRevision();
    } catch (CannotRestoreSiddhiAppStateException e) {
        Assert.fail("Restoring of Siddhi app " + siddhiAppRuntime.getName() + " failed", e);
    }
    siddhiAppRuntime.start();
    Thread.sleep(500);
    inputHandler.send(new Object[] { "IBM", 700.6f, 230 });
    Thread.sleep(10);
    inputHandler.send(new Object[] { "WSO2", 800.6f, 125 });
    inputHandler.send(new Object[] { "IBM", 900.6f, 370 });
    Thread.sleep(10);
    inputHandler.send(new Object[] { "WSO2", 1000.6f, 140 });
    // shutdown Siddhi app
    Thread.sleep(500);
    siddhiAppRuntime.shutdown();
    AssertJUnit.assertTrue(count <= (inputEventCount + 6));
    AssertJUnit.assertEquals(new Long(865), lastValue);
    AssertJUnit.assertEquals(true, eventArrived);
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) IncrementalFileSystemPersistenceStore(io.siddhi.core.util.persistence.IncrementalFileSystemPersistenceStore) AtomicLong(java.util.concurrent.atomic.AtomicLong) 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 18 with QueryCallback

use of io.siddhi.core.query.output.callback.QueryCallback in project siddhi by wso2.

the class IncrementalPersistenceTestCase method incrementalPersistenceTest7.

@Test
public void incrementalPersistenceTest7() throws InterruptedException {
    log.info("Incremental persistence test 7 - in-memory table persistance test with primary key.");
    int inputEventCountPerCategory = 10;
    SiddhiManager siddhiManager = new SiddhiManager();
    siddhiManager.setIncrementalPersistenceStore(new IncrementalFileSystemPersistenceStore(storageFilePath));
    String streams = "" + "@app:name('incrementalPersistenceTest7') " + "" + "define stream StockStream (symbol2 string, price float, volume long); " + "define stream CheckStockStream (symbol1 string); " + "define stream OutStream (symbol1 string, TB long); " + "@PrimaryKey('symbol2') " + "define table StockTable (symbol2 string, price float, volume long); ";
    String query = "" + "@info(name = 'query1') " + "from StockStream " + "insert into StockTable ;" + "" + "@info(name = 'query2') " + "from CheckStockStream join StockTable " + " on symbol1 == symbol2 " + "select symbol2 as symbol1, volume as TB " + "group by symbol2 " + "insert all events into OutStream;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    QueryCallback queryCallback = new QueryCallback() {

        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            if (inEvents != null) {
                for (Event event : inEvents) {
                    inEventsList.add(event.getData());
                    inEventCount.incrementAndGet();
                }
                eventArrived = true;
            }
            if (removeEvents != null) {
                removeEventCount = removeEventCount + removeEvents.length;
            }
            eventArrived = true;
        }
    };
    try {
        siddhiAppRuntime.addCallback("query2", queryCallback);
        InputHandler stockStream = siddhiAppRuntime.getInputHandler("StockStream");
        siddhiAppRuntime.start();
        for (int i = 0; i < inputEventCountPerCategory; i++) {
            stockStream.send(new Object[] { "WSO2-" + i, 55.6f, 180L + i });
        }
        for (int i = 0; i < inputEventCountPerCategory; i++) {
            stockStream.send(new Object[] { "IBM-" + i, 55.6f, 100L + i });
        }
        // persisting
        siddhiAppRuntime.persist();
        Thread.sleep(5000);
        siddhiAppRuntime.shutdown();
        siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
        siddhiAppRuntime.addCallback("query2", queryCallback);
        stockStream = siddhiAppRuntime.getInputHandler("StockStream");
        InputHandler checkStockStream = siddhiAppRuntime.getInputHandler("CheckStockStream");
        // loading
        try {
            siddhiAppRuntime.restoreLastRevision();
        } catch (CannotRestoreSiddhiAppStateException e) {
            Assert.fail("Restoring of Siddhi app " + siddhiAppRuntime.getName() + " failed.", e);
        }
        siddhiAppRuntime.start();
        Thread.sleep(2000);
        stockStream.send(new Object[] { "WSO2-" + (inputEventCountPerCategory + 1), 100.6f, 180L });
        stockStream.send(new Object[] { "IBM-" + (inputEventCountPerCategory + 1), 100.6f, 100L });
        stockStream.send(new Object[] { "WSO2-" + (inputEventCountPerCategory + 2), 8.6f, 13L });
        stockStream.send(new Object[] { "IBM-" + (inputEventCountPerCategory + 2), 7.6f, 14L });
        checkStockStream.send(new Object[] { "IBM-5" });
        checkStockStream.send(new Object[] { "WSO2-5" });
        List<Object[]> expected = Arrays.asList(new Object[] { "IBM-5", 105L }, new Object[] { "WSO2-5", 185L });
        Thread.sleep(1000);
        AssertJUnit.assertEquals("In events matched", true, SiddhiTestHelper.isEventsMatch(inEventsList, expected));
    } finally {
        siddhiAppRuntime.shutdown();
    }
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) IncrementalFileSystemPersistenceStore(io.siddhi.core.util.persistence.IncrementalFileSystemPersistenceStore) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) 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 19 with QueryCallback

use of io.siddhi.core.query.output.callback.QueryCallback in project siddhi by wso2.

the class IncrementalPersistenceTestCase method incrementalPersistenceTest3.

@Test
public void incrementalPersistenceTest3() throws InterruptedException {
    log.info("Incremental persistence test 3 - time window query");
    final int inputEventCount = 10;
    final int eventWindowSizeInSeconds = 2;
    SiddhiManager siddhiManager = new SiddhiManager();
    siddhiManager.setIncrementalPersistenceStore(new IncrementalFileSystemPersistenceStore(storageFilePath));
    String siddhiApp = "" + "@app:name('incrementalPersistenceTest3') " + "@app:playback " + "" + "define stream StockStream ( symbol string, price float, volume int );" + "" + "@info(name = 'query1')" + "from StockStream[price>10]#window.time(" + eventWindowSizeInSeconds + " sec) " + "select symbol, price, sum(volume) as totalVol " + "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)));
                lastValue = (Long) inEvent.getData(2);
                log.info("last value: " + lastValue);
            }
        }
    };
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("query1", queryCallback);
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("StockStream");
    siddhiAppRuntime.start();
    for (int i = 0; i < inputEventCount; i++) {
        inputHandler.send(i, new Object[] { "IBM", 75.6f + i, 100 });
    }
    Thread.sleep(4000);
    AssertJUnit.assertTrue(eventArrived);
    AssertJUnit.assertEquals(new Long(1000), lastValue);
    // persisting
    siddhiAppRuntime.persist();
    Thread.sleep(5000);
    inputHandler.send(3000, new Object[] { "WSO2", 100.4f, 150 });
    // Thread.sleep(100);
    inputHandler.send(3010, new Object[] { "WSO2", 200.4f, 110 });
    inputHandler.send(3020, new Object[] { "IBM", 300.4f, 100 });
    // Thread.sleep(100);
    inputHandler.send(3030, new Object[] { "WSO2", 400.4f, 300 });
    inputHandler.send(3040, new Object[] { "IBM", 500.6f, 120 });
    Thread.sleep(10);
    inputHandler.send(3050, new Object[] { "WSO2", 600.6f, 400 });
    Thread.sleep(100);
    siddhiAppRuntime.persist();
    Thread.sleep(5000);
    siddhiAppRuntime.shutdown();
    siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("query1", queryCallback);
    inputHandler = siddhiAppRuntime.getInputHandler("StockStream");
    try {
        // loading
        siddhiAppRuntime.restoreLastRevision();
    } catch (CannotRestoreSiddhiAppStateException e) {
        Assert.fail("Restoring of Siddhi app " + siddhiAppRuntime.getName() + " failed");
    }
    siddhiAppRuntime.start();
    Thread.sleep(500);
    inputHandler.send(3500, new Object[] { "IBM", 700.6f, 230 });
    Thread.sleep(10);
    inputHandler.send(3540, new Object[] { "WSO2", 800.6f, 125 });
    inputHandler.send(3590, new Object[] { "IBM", 900.6f, 370 });
    Thread.sleep(10);
    inputHandler.send(3600, new Object[] { "WSO2", 1000.6f, 140 });
    // shutdown Siddhi app
    Thread.sleep(5000);
    siddhiAppRuntime.shutdown();
    AssertJUnit.assertTrue(count <= (inputEventCount + 10));
    AssertJUnit.assertEquals(new Long(2045), lastValue);
    AssertJUnit.assertEquals(true, eventArrived);
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) IncrementalFileSystemPersistenceStore(io.siddhi.core.util.persistence.IncrementalFileSystemPersistenceStore) AtomicLong(java.util.concurrent.atomic.AtomicLong) 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 20 with QueryCallback

use of io.siddhi.core.query.output.callback.QueryCallback in project siddhi by wso2.

the class IncrementalPersistenceTestCase method incrementalPersistenceTest4.

@Test
public void incrementalPersistenceTest4() throws InterruptedException {
    log.info("Incremental persistence test 4 - time batch window query");
    final int inputEventCount = 10;
    final int eventWindowSizeInSeconds = 2;
    SiddhiManager siddhiManager = new SiddhiManager();
    siddhiManager.setIncrementalPersistenceStore(new IncrementalFileSystemPersistenceStore(storageFilePath));
    String siddhiApp = "" + "@app:name('incrementalPersistenceTest4') " + "" + "define stream StockStream ( symbol string, price float, volume int );" + "" + "@info(name = 'query1')" + "from StockStream[price>10]#window.timeBatch(" + eventWindowSizeInSeconds + " sec) " + "select symbol, price, sum(volume) as totalVol " + "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)));
                lastValue = (Long) inEvent.getData(2);
                log.info("last value: " + lastValue);
            }
        }
    };
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("query1", queryCallback);
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("StockStream");
    siddhiAppRuntime.start();
    for (int i = 0; i < inputEventCount; i++) {
        inputHandler.send(new Object[] { "IBM", 75.6f + i, 100 });
    }
    Thread.sleep(4000);
    AssertJUnit.assertTrue(eventArrived);
    AssertJUnit.assertEquals(new Long(1000), lastValue);
    // persisting
    siddhiAppRuntime.persist();
    Thread.sleep(5000);
    inputHandler.send(new Object[] { "WSO2", 100.4f, 150 });
    // Thread.sleep(100);
    inputHandler.send(new Object[] { "WSO2", 200.4f, 110 });
    inputHandler.send(new Object[] { "IBM", 300.4f, 100 });
    // Thread.sleep(100);
    inputHandler.send(new Object[] { "WSO2", 400.4f, 300 });
    inputHandler.send(new Object[] { "IBM", 500.6f, 120 });
    Thread.sleep(10);
    inputHandler.send(new Object[] { "WSO2", 600.6f, 400 });
    Thread.sleep(100);
    siddhiAppRuntime.persist();
    Thread.sleep(5000);
    siddhiAppRuntime.shutdown();
    siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("query1", queryCallback);
    inputHandler = siddhiAppRuntime.getInputHandler("StockStream");
    // loading
    try {
        siddhiAppRuntime.restoreLastRevision();
    } catch (CannotRestoreSiddhiAppStateException e) {
        Assert.fail("Restoring of Siddhi app " + siddhiAppRuntime.getName() + " failed");
    }
    siddhiAppRuntime.start();
    Thread.sleep(50);
    inputHandler.send(new Object[] { "IBM", 700.6f, 230 });
    Thread.sleep(10);
    inputHandler.send(new Object[] { "WSO2", 800.6f, 125 });
    inputHandler.send(new Object[] { "IBM", 900.6f, 370 });
    Thread.sleep(10);
    inputHandler.send(new Object[] { "WSO2", 1000.6f, 140 });
    // shutdown Siddhi app
    Thread.sleep(5000);
    siddhiAppRuntime.shutdown();
    AssertJUnit.assertTrue(count <= (inputEventCount + 10));
    AssertJUnit.assertEquals(new Long(2045), lastValue);
    AssertJUnit.assertEquals(true, eventArrived);
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) IncrementalFileSystemPersistenceStore(io.siddhi.core.util.persistence.IncrementalFileSystemPersistenceStore) AtomicLong(java.util.concurrent.atomic.AtomicLong) 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)

Aggregations

QueryCallback (io.siddhi.core.query.output.callback.QueryCallback)824 SiddhiAppRuntime (io.siddhi.core.SiddhiAppRuntime)823 Test (org.testng.annotations.Test)822 SiddhiManager (io.siddhi.core.SiddhiManager)821 Event (io.siddhi.core.event.Event)818 InputHandler (io.siddhi.core.stream.input.InputHandler)817 StreamDefinition (io.siddhi.query.api.definition.StreamDefinition)75 SiddhiApp (io.siddhi.query.api.SiddhiApp)74 Query (io.siddhi.query.api.execution.query.Query)74 ArrayList (java.util.ArrayList)43 Logger (org.apache.logging.log4j.core.Logger)26 CannotRestoreSiddhiAppStateException (io.siddhi.core.exception.CannotRestoreSiddhiAppStateException)23 TestAppenderToValidateLogsForCachingTests (io.siddhi.core.query.table.util.TestAppenderToValidateLogsForCachingTests)21 InMemoryPersistenceStore (io.siddhi.core.util.persistence.InMemoryPersistenceStore)11 IncrementalFileSystemPersistenceStore (io.siddhi.core.util.persistence.IncrementalFileSystemPersistenceStore)11 PersistenceStore (io.siddhi.core.util.persistence.PersistenceStore)11 AtomicLong (java.util.concurrent.atomic.AtomicLong)9 StreamCallback (io.siddhi.core.stream.output.StreamCallback)7 StringConcatAggregatorExecutorString (io.siddhi.core.query.extension.util.StringConcatAggregatorExecutorString)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6