Search in sources :

Example 1 with PersistenceStore

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

the class PersistenceTestCase method persistenceTest5.

@Test(dependsOnMethods = "persistenceTest4")
public void persistenceTest5() throws InterruptedException {
    log.info("persistence test 5 - window restart expired event ");
    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 );" + "" + "@info(name = 'query1')" + "from StockStream[price>10]#window.time(10 sec) " + "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;
            if (inEvents != null) {
                for (Event inEvent : inEvents) {
                    count++;
                    AssertJUnit.assertTrue("IBM".equals(inEvent.getData(0)) || "WSO2".equals(inEvent.getData(0)));
                    firstValue = (Long) inEvent.getData(2);
                }
            }
            if (removeEvents != null) {
                for (Event removeEvent : removeEvents) {
                    count++;
                    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, 100 });
    Thread.sleep(100);
    inputHandler.send(new Object[] { "WSO2", 75.6f, 100 });
    Thread.sleep(100);
    AssertJUnit.assertTrue(eventArrived);
    AssertJUnit.assertEquals(firstValue, 200);
    // persisting
    Thread.sleep(500);
    siddhiAppRuntime.persist();
    inputHandler.send(new Object[] { "IBM", 75.6f, 100 });
    Thread.sleep(100);
    inputHandler.send(new Object[] { "WSO2", 75.6f, 100 });
    // restarting siddhi app
    Thread.sleep(500);
    siddhiAppRuntime.shutdown();
    siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("query1", queryCallback);
    siddhiAppRuntime.getInputHandler("StockStream");
    siddhiAppRuntime.start();
    // loading
    try {
        siddhiAppRuntime.restoreLastRevision();
    } catch (CannotRestoreSiddhiAppStateException e) {
        Assert.fail("Restoring of Siddhi app " + siddhiAppRuntime.getName() + " failed");
    }
    // shutdown siddhi app
    Thread.sleep(15000);
    siddhiAppRuntime.shutdown();
    AssertJUnit.assertEquals(400, firstValue);
    AssertJUnit.assertEquals(null, 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) 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 2 with PersistenceStore

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

the class PersistenceTestCase method persistenceTest10.

@Test(dependsOnMethods = "persistenceTest9")
public void persistenceTest10() throws InterruptedException {
    log.info("persistence test 10 - sort 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 int );" + "" + "@info(name = 'query1') " + "from StockStream#window.sort(2,volume) " + "select volume " + "insert all events into outputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    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) {
                count++;
            }
            if (removeEvents != null) {
                for (Event removeEvent : removeEvents) {
                    lastValueRemoved = (Integer) removeEvent.getData(0);
                }
            }
        }
    };
    siddhiAppRuntime.addCallback("query1", queryCallback);
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("StockStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "WSO2", 55.6f, 100 });
    inputHandler.send(new Object[] { "IBM", 75.6f, 300 });
    inputHandler.send(new Object[] { "WSO2", 57.6f, 200 });
    Thread.sleep(1000);
    AssertJUnit.assertEquals(3, count);
    AssertJUnit.assertTrue(eventArrived);
    // persisting
    siddhiAppRuntime.persist();
    inputHandler.send(new Object[] { "WSO2", 55.6f, 20 });
    inputHandler.send(new Object[] { "WSO2", 57.6f, 40 });
    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[] { "WSO2", 55.6f, 20 });
    SiddhiTestHelper.waitForEvents(100, 6, atomicCount, 10000);
    AssertJUnit.assertEquals(true, eventArrived);
    AssertJUnit.assertEquals(200, lastValueRemoved);
    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) QueryCallback(io.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 3 with PersistenceStore

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

the class PersistenceTestCase method persistenceTest2.

@Test(dependsOnMethods = "persistenceTest1")
public void persistenceTest2() throws InterruptedException {
    log.info("persistence test 2 - pattern count query");
    PersistenceStore persistenceStore = new InMemoryPersistenceStore();
    SiddhiManager siddhiManager = new SiddhiManager();
    siddhiManager.setPersistenceStore(persistenceStore);
    String siddhiApp = "" + "@app:name('Test') " + "" + "define stream Stream1 (symbol string, price float, volume int); " + "define stream Stream2 (symbol string, price float, volume int); " + "" + "@info(name = 'query1') " + "from e1=Stream1[price>20] <2:5> -> e2=Stream2[price>20] " + "select e1[0].price as price1_0, e1[1].price as price1_1, e1[2].price as price1_2, " + "   e1[3].price as price1_3, e2.price as price2 " + "insert into OutputStream ;";
    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.assertArrayEquals(new Object[] { 25.6f, 47.6f, null, null, 45.7f }, inEvent.getData());
            }
        }
    };
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("query1", queryCallback);
    InputHandler stream1 = siddhiAppRuntime.getInputHandler("Stream1");
    InputHandler stream2;
    siddhiAppRuntime.start();
    stream1.send(new Object[] { "WSO2", 25.6f, 100 });
    Thread.sleep(100);
    stream1.send(new Object[] { "GOOG", 47.6f, 100 });
    Thread.sleep(100);
    stream1.send(new Object[] { "GOOG", 13.7f, 100 });
    Thread.sleep(100);
    AssertJUnit.assertEquals("Number of success events", 0, count);
    AssertJUnit.assertEquals("Event arrived", false, eventArrived);
    // persisting
    Thread.sleep(500);
    siddhiAppRuntime.persist();
    // restarting siddhi app
    Thread.sleep(500);
    siddhiAppRuntime.shutdown();
    siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("query1", queryCallback);
    stream1 = siddhiAppRuntime.getInputHandler("Stream1");
    stream2 = siddhiAppRuntime.getInputHandler("Stream2");
    siddhiAppRuntime.start();
    // loading
    try {
        siddhiAppRuntime.restoreLastRevision();
    } catch (CannotRestoreSiddhiAppStateException e) {
        Assert.fail("Restoring of Siddhi app " + siddhiAppRuntime.getName() + " failed");
    }
    stream2.send(new Object[] { "IBM", 45.7f, 100 });
    Thread.sleep(500);
    stream1.send(new Object[] { "GOOG", 47.8f, 100 });
    Thread.sleep(500);
    stream2.send(new Object[] { "IBM", 55.7f, 100 });
    Thread.sleep(500);
    // shutdown siddhi app
    Thread.sleep(500);
    siddhiAppRuntime.shutdown();
    AssertJUnit.assertEquals("Number of success events", 1, count);
    AssertJUnit.assertEquals("Event arrived", 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 4 with PersistenceStore

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

the class PersistenceTestCase method persistenceTest1.

@Test
public void persistenceTest1() throws InterruptedException {
    log.info("persistence test 1 - 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 int );" + "" + "@info(name = 'query1')" + "from StockStream[price>10]#window.length(10) " + "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);
            }
        }
    };
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("query1", queryCallback);
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("StockStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "IBM", 75.6f, 100 });
    Thread.sleep(100);
    inputHandler.send(new Object[] { "WSO2", 75.6f, 100 });
    Thread.sleep(100);
    AssertJUnit.assertTrue(eventArrived);
    AssertJUnit.assertEquals(new Long(200), lastValue);
    // persisting
    Thread.sleep(500);
    siddhiAppRuntime.persist();
    inputHandler.send(new Object[] { "IBM", 75.6f, 100 });
    Thread.sleep(100);
    inputHandler.send(new Object[] { "WSO2", 75.6f, 100 });
    // 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", e);
    }
    inputHandler.send(new Object[] { "IBM", 75.6f, 100 });
    Thread.sleep(10);
    inputHandler.send(new Object[] { "WSO2", 75.6f, 100 });
    // shutdown siddhi app
    Thread.sleep(500);
    siddhiAppRuntime.shutdown();
    AssertJUnit.assertTrue(count <= 6);
    AssertJUnit.assertEquals(new Long(400), 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) 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 5 with PersistenceStore

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

the class PersistenceTestCase method persistenceTest6.

@Test(dependsOnMethods = "persistenceTest5")
public void persistenceTest6() throws InterruptedException {
    log.info("persistence test 6 - 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 int );" + "" + "@info(name = 'query1')" + "from StockStream[price>10]#window.timeBatch(10) " + "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)));
            }
        }
    };
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("query1", queryCallback);
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("StockStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "IBM", 75.6f, 100 });
    Thread.sleep(100);
    inputHandler.send(new Object[] { "WSO2", 75.6f, 100 });
    Thread.sleep(500);
    AssertJUnit.assertTrue(eventArrived);
    AssertJUnit.assertEquals(2, count);
    // persisting
    Thread.sleep(500);
    siddhiAppRuntime.persist();
    inputHandler.send(new Object[] { "IBM", 75.6f, 100 });
    Thread.sleep(100);
    inputHandler.send(new Object[] { "WSO2", 75.6f, 100 });
    // 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) {
        log.error(e.getMessage(), e);
        Assert.fail("Restoring of Siddhi app " + siddhiAppRuntime.getName() + " failed");
    }
    inputHandler.send(new Object[] { "IBM", 75.6f, 100 });
    Thread.sleep(100);
    inputHandler.send(new Object[] { "WSO2", 75.6f, 100 });
    // 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)

Aggregations

PersistenceStore (io.siddhi.core.util.persistence.PersistenceStore)18 SiddhiAppRuntime (io.siddhi.core.SiddhiAppRuntime)15 SiddhiManager (io.siddhi.core.SiddhiManager)15 CannotRestoreSiddhiAppStateException (io.siddhi.core.exception.CannotRestoreSiddhiAppStateException)15 InputHandler (io.siddhi.core.stream.input.InputHandler)15 InMemoryPersistenceStore (io.siddhi.core.util.persistence.InMemoryPersistenceStore)15 Test (org.testng.annotations.Test)15 Event (io.siddhi.core.event.Event)14 QueryCallback (io.siddhi.core.query.output.callback.QueryCallback)11 StreamCallback (io.siddhi.core.stream.output.StreamCallback)4 NoPersistenceStoreException (io.siddhi.core.exception.NoPersistenceStoreException)3 IncrementalPersistenceStore (io.siddhi.core.util.persistence.IncrementalPersistenceStore)3 SiddhiAppContext (io.siddhi.core.config.SiddhiAppContext)1 CannotClearSiddhiAppStateException (io.siddhi.core.exception.CannotClearSiddhiAppStateException)1 PersistenceStoreException (io.siddhi.core.exception.PersistenceStoreException)1 SiddhiAppRuntimeException (io.siddhi.core.exception.SiddhiAppRuntimeException)1 ThreadBarrier (io.siddhi.core.util.ThreadBarrier)1 IncrementalSnapshotInfo (io.siddhi.core.util.persistence.util.IncrementalSnapshotInfo)1 PersistenceConstants (io.siddhi.core.util.persistence.util.PersistenceConstants)1 PersistenceHelper (io.siddhi.core.util.persistence.util.PersistenceHelper)1