Search in sources :

Example 11 with PersistenceStore

use of org.ballerinalang.siddhi.core.util.persistence.PersistenceStore in project ballerina by ballerina-lang.

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.ballerinalang.siddhi.core.stream.input.InputHandler) PersistenceStore(org.ballerinalang.siddhi.core.util.persistence.PersistenceStore) InMemoryPersistenceStore(org.ballerinalang.siddhi.core.util.persistence.InMemoryPersistenceStore) InMemoryPersistenceStore(org.ballerinalang.siddhi.core.util.persistence.InMemoryPersistenceStore) SiddhiAppRuntime(org.ballerinalang.siddhi.core.SiddhiAppRuntime) CannotRestoreSiddhiAppStateException(org.ballerinalang.siddhi.core.exception.CannotRestoreSiddhiAppStateException) SiddhiManager(org.ballerinalang.siddhi.core.SiddhiManager) StreamCallback(org.ballerinalang.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 12 with PersistenceStore

use of org.ballerinalang.siddhi.core.util.persistence.PersistenceStore in project ballerina by ballerina-lang.

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(org.ballerinalang.siddhi.core.stream.input.InputHandler) PersistenceStore(org.ballerinalang.siddhi.core.util.persistence.PersistenceStore) InMemoryPersistenceStore(org.ballerinalang.siddhi.core.util.persistence.InMemoryPersistenceStore) InMemoryPersistenceStore(org.ballerinalang.siddhi.core.util.persistence.InMemoryPersistenceStore) Event(org.ballerinalang.siddhi.core.event.Event) SiddhiAppRuntime(org.ballerinalang.siddhi.core.SiddhiAppRuntime) CannotRestoreSiddhiAppStateException(org.ballerinalang.siddhi.core.exception.CannotRestoreSiddhiAppStateException) SiddhiManager(org.ballerinalang.siddhi.core.SiddhiManager) QueryCallback(org.ballerinalang.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Aggregations

SiddhiAppRuntime (org.ballerinalang.siddhi.core.SiddhiAppRuntime)12 SiddhiManager (org.ballerinalang.siddhi.core.SiddhiManager)12 InputHandler (org.ballerinalang.siddhi.core.stream.input.InputHandler)12 InMemoryPersistenceStore (org.ballerinalang.siddhi.core.util.persistence.InMemoryPersistenceStore)12 PersistenceStore (org.ballerinalang.siddhi.core.util.persistence.PersistenceStore)12 Test (org.testng.annotations.Test)12 CannotRestoreSiddhiAppStateException (org.ballerinalang.siddhi.core.exception.CannotRestoreSiddhiAppStateException)11 QueryCallback (org.ballerinalang.siddhi.core.query.output.callback.QueryCallback)11 Event (org.ballerinalang.siddhi.core.event.Event)10 StreamCallback (org.ballerinalang.siddhi.core.stream.output.StreamCallback)1