Search in sources :

Example 6 with InMemoryPersistenceStore

use of io.siddhi.core.util.persistence.InMemoryPersistenceStore 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 7 with InMemoryPersistenceStore

use of io.siddhi.core.util.persistence.InMemoryPersistenceStore 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 8 with InMemoryPersistenceStore

use of io.siddhi.core.util.persistence.InMemoryPersistenceStore in project siddhi by wso2.

the class DelayWindowTestCase method delayWindowTest7.

@Test(description = "Check if events are persisted when using delay window")
public void delayWindowTest7() throws InterruptedException {
    log.info("DelayWindow Test7: Testing persistence ");
    PersistenceStore persistenceStore = new InMemoryPersistenceStore();
    SiddhiManager siddhiManager = new SiddhiManager();
    siddhiManager.setPersistenceStore(persistenceStore);
    String cseEventStream = "" + "define window delayWindow(symbol string, volume int) delay(1450);" + "define stream inputStream(symbol string, volume int);" + "define window timeWindow(symbol string) time(2 sec);";
    String query = "" + "@info(name='query1') " + "from inputStream " + "select symbol, volume " + "insert into delayWindow; " + "" + "@info(name = 'query2') " + "from delayWindow " + "select symbol, sum(volume) as totalVolume " + "insert into analyzeStream; " + "" + "@info(name='query3') " + "from inputStream " + "select symbol " + "insert into timeWindow; " + "" + "@info(name='query4') " + "from analyzeStream join timeWindow " + "on analyzeStream.symbol == timeWindow.symbol " + "select analyzeStream.symbol, analyzeStream.totalVolume " + "insert into outputStream; ";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);
    siddhiAppRuntime.addCallback("outputStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            count.addAndGet(events.length);
            for (Event event : events) {
                AssertJUnit.assertTrue(("IBM".equals(event.getData(0)) || "WSO2".equals(event.getData(0))));
            }
            lastValue = (Long) events[0].getData(1);
        }
    });
    InputHandler input = siddhiAppRuntime.getInputHandler("inputStream");
    siddhiAppRuntime.start();
    input.send(new Object[] { "IBM", 700 });
    input.send(new Object[] { "WSO2", 750 });
    SiddhiTestHelper.waitForEvents(100, 2, count, 4000);
    siddhiAppRuntime.persist();
    siddhiAppRuntime.shutdown();
    input = siddhiAppRuntime.getInputHandler("inputStream");
    siddhiAppRuntime.start();
    try {
        siddhiAppRuntime.restoreLastRevision();
    } catch (CannotRestoreSiddhiAppStateException e) {
        Assert.fail("Restoring of Siddhi app " + siddhiAppRuntime.getName() + " failed");
    }
    input.send(new Object[] { "WSO2", 600 });
    SiddhiTestHelper.waitForEvents(100, 3, count, 4000);
    AssertJUnit.assertEquals(Long.valueOf(2050), lastValue);
    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) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) CannotRestoreSiddhiAppStateException(io.siddhi.core.exception.CannotRestoreSiddhiAppStateException) SiddhiManager(io.siddhi.core.SiddhiManager) StreamCallback(io.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 9 with InMemoryPersistenceStore

use of io.siddhi.core.util.persistence.InMemoryPersistenceStore in project siddhi by wso2.

the class PersistenceTestCase method persistenceTest11.

@Test(dependsOnMethods = "persistenceTest10")
public void persistenceTest11() throws InterruptedException {
    log.info("persistence test 11 - 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.lengthBatch(2) " + "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;
            atomicCount.incrementAndGet();
            for (Event inEvent : inEvents) {
                AssertJUnit.assertTrue("IBM".equals(inEvent.getData(0)) || "WSO2".equals(inEvent.getData(0)));
            }
            if (removeEvents != null) {
                for (Event removeEvent : removeEvents) {
                    lastValue = (Long) removeEvent.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 });
    inputHandler.send(new Object[] { "IBM", 75.6f, 103L });
    SiddhiTestHelper.waitForEvents(100, 2, atomicCount, 10000);
    AssertJUnit.assertTrue(eventArrived);
    AssertJUnit.assertEquals(new Long(101), lastValue);
    // persisting
    siddhiAppRuntime.persist();
    Thread.sleep(500);
    inputHandler.send(new Object[] { "WSO2", 75.6f, 50L });
    inputHandler.send(new Object[] { "IBM", 75.6f, 50L });
    inputHandler.send(new Object[] { "IBM", 75.6f, 50L });
    // restarting execution plan
    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.6f, 100L });
    // shutdown siddhi app
    Thread.sleep(500);
    SiddhiTestHelper.waitForEvents(100, 3, atomicCount, 10000);
    AssertJUnit.assertEquals(new Long(103), lastValue);
    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 10 with InMemoryPersistenceStore

use of io.siddhi.core.util.persistence.InMemoryPersistenceStore in project siddhi by wso2.

the class PersistenceTestCase method persistenceTest12.

@Test(dependsOnMethods = "persistenceTest11")
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() 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(600);
    // persisting
    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(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) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) CannotRestoreSiddhiAppStateException(io.siddhi.core.exception.CannotRestoreSiddhiAppStateException) SiddhiManager(io.siddhi.core.SiddhiManager) StreamCallback(io.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Aggregations

SiddhiAppRuntime (io.siddhi.core.SiddhiAppRuntime)15 SiddhiManager (io.siddhi.core.SiddhiManager)15 InputHandler (io.siddhi.core.stream.input.InputHandler)15 InMemoryPersistenceStore (io.siddhi.core.util.persistence.InMemoryPersistenceStore)15 PersistenceStore (io.siddhi.core.util.persistence.PersistenceStore)15 Test (org.testng.annotations.Test)15 Event (io.siddhi.core.event.Event)14 CannotRestoreSiddhiAppStateException (io.siddhi.core.exception.CannotRestoreSiddhiAppStateException)14 QueryCallback (io.siddhi.core.query.output.callback.QueryCallback)11 StreamCallback (io.siddhi.core.stream.output.StreamCallback)4