Search in sources :

Example 11 with IncrementalFileSystemPersistenceStore

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

the class IncrementalPersistenceTestCase method incrementalPersistenceTest8.

@Test
public void incrementalPersistenceTest8() throws InterruptedException {
    log.info("Incremental persistence test 8 - min-max counting.");
    final int inputEventCount = 10;
    SiddhiManager siddhiManager = new SiddhiManager();
    siddhiManager.setIncrementalPersistenceStore(new IncrementalFileSystemPersistenceStore(storageFilePath));
    String streams = "" + "@app:name('incrementalPersistenceTest8') " + "define stream TempStream (roomNo long, temp long); " + "define stream MaxTempStream (roomNo long, maxTemp long); ";
    String query = "" + "@info(name = 'query1') " + "from TempStream#window.length(10) " + "select roomNo, max(temp) as maxTemp " + "insert into MaxTempStream; ";
    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);
            eventArrived = true;
            if (inEvents != null) {
                for (Event event : inEvents) {
                    count++;
                    inEventsList.add(event.getData());
                    inEventCount.incrementAndGet();
                    lastValue = (Long) event.getData(1);
                }
                eventArrived = true;
            }
            if (removeEvents != null) {
                removeEventCount = removeEventCount + removeEvents.length;
            }
        }
    };
    try {
        siddhiAppRuntime.addCallback("query1", queryCallback);
        InputHandler stockStream = siddhiAppRuntime.getInputHandler("TempStream");
        siddhiAppRuntime.start();
        for (int i = 0; i < inputEventCount; i++) {
            stockStream.send(new Object[] { i, 55L + i });
        }
        // persisting
        siddhiAppRuntime.persist();
        Thread.sleep(5000);
        siddhiAppRuntime.shutdown();
        siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
        siddhiAppRuntime.addCallback("query1", queryCallback);
        stockStream = siddhiAppRuntime.getInputHandler("TempStream");
        // loading
        try {
            siddhiAppRuntime.restoreLastRevision();
        } catch (CannotRestoreSiddhiAppStateException e) {
            Assert.fail("Restoring of Siddhi app " + siddhiAppRuntime.getName() + " failed");
        }
        siddhiAppRuntime.start();
        Thread.sleep(2000);
        stockStream.send(new Object[] { inputEventCount + 1, 1000L });
        // persisting
        siddhiAppRuntime.persist();
        Thread.sleep(2000);
        stockStream.send(new Object[] { inputEventCount + 2, 20L });
        siddhiAppRuntime.persist();
        Thread.sleep(2000);
        stockStream.send(new Object[] { inputEventCount + 3, 30L });
        AssertJUnit.assertTrue(count <= (inputEventCount + 3));
        AssertJUnit.assertEquals(new Long(1000), lastValue);
        AssertJUnit.assertEquals(true, eventArrived);
    } finally {
        siddhiAppRuntime.shutdown();
    }
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) IncrementalFileSystemPersistenceStore(io.siddhi.core.util.persistence.IncrementalFileSystemPersistenceStore) AtomicLong(java.util.concurrent.atomic.AtomicLong) 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 12 with IncrementalFileSystemPersistenceStore

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

the class IncrementalPersistenceTestCase method incrementalPersistenceTest12.

@Test
public void incrementalPersistenceTest12() throws InterruptedException {
    log.info("Incremental file persistence test 12 - length window query with max attribute aggregator");
    final int eventWindowSize = 5;
    SiddhiManager siddhiManager = new SiddhiManager();
    siddhiManager.setIncrementalPersistenceStore(new IncrementalFileSystemPersistenceStore(storageFilePath));
    String siddhiApp = "" + "@app:name('incrementalPersistenceTest12') " + "" + "define stream StockStream ( symbol string, price float, volume int );" + "" + "@info(name = 'query1')" + "from StockStream#window.length(" + eventWindowSize + ") " + "select symbol, price, max(volume) as maxVol " + "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 = new Long((Integer) 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, 500 });
    inputHandler.send(new Object[] { "IBM", 75.6f, 200 });
    inputHandler.send(new Object[] { "IBM", 75.6f, 300 });
    inputHandler.send(new Object[] { "IBM", 75.6f, 250 });
    inputHandler.send(new Object[] { "IBM", 75.6f, 150 });
    Thread.sleep(100);
    AssertJUnit.assertTrue(eventArrived);
    AssertJUnit.assertEquals(new Long(500L), lastValue);
    // persisting
    siddhiAppRuntime.persist();
    Thread.sleep(5000);
    // persisting for the second time to store the inc-snapshot
    siddhiAppRuntime.persist();
    Thread.sleep(100);
    siddhiAppRuntime.shutdown();
    siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("query1", queryCallback);
    inputHandler = siddhiAppRuntime.getInputHandler("StockStream");
    // loading
    try {
        siddhiAppRuntime.restoreLastRevision();
    } catch (CannotRestoreSiddhiAppStateException e) {
        log.error(e.getMessage(), e);
        Assert.fail("Restoring of Siddhi app " + siddhiAppRuntime.getName() + " failed");
    }
    siddhiAppRuntime.start();
    Thread.sleep(5000);
    inputHandler.send(new Object[] { "IBM", 100.4f, 280 });
    AssertJUnit.assertEquals((Long) 300L, lastValue);
    inputHandler.send(new Object[] { "WSO2", 200.4f, 150 });
    AssertJUnit.assertEquals((Long) 300L, lastValue);
    inputHandler.send(new Object[] { "IBM", 300.4f, 200 });
    AssertJUnit.assertEquals((Long) 280L, lastValue);
    inputHandler.send(new Object[] { "WSO2", 400.4f, 270 });
    AssertJUnit.assertEquals((Long) 280L, lastValue);
    inputHandler.send(new Object[] { "WSO2", 400.4f, 280 });
    AssertJUnit.assertEquals((Long) 280L, lastValue);
    // shutdown Siddhi app
    Thread.sleep(500);
    siddhiAppRuntime.shutdown();
    AssertJUnit.assertEquals(true, eventArrived);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) 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 13 with IncrementalFileSystemPersistenceStore

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

the class IncrementalPersistenceTestCase method incrementalPersistenceTest5.

@Test
public void incrementalPersistenceTest5() throws InterruptedException {
    log.info("Incremental persistence test 5 - external time window query");
    final int inputEventCount = 10;
    final int eventWindowSizeInSeconds = 2;
    SiddhiManager siddhiManager = new SiddhiManager();
    siddhiManager.setIncrementalPersistenceStore(new IncrementalFileSystemPersistenceStore(storageFilePath));
    String siddhiApp = "" + "@app:name('incrementalPersistenceTest5') " + "" + "define stream StockStream ( iij_timestamp long, price float, volume int );" + "" + "@info(name = 'query1')" + "from StockStream[price>10]#window.externalTime(iij_timestamp, " + eventWindowSizeInSeconds + " sec) " + "select iij_timestamp, 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++;
                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[] { (long) i, 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[] { (1L + inputEventCount), 100.4f, 150 });
    // Thread.sleep(100);
    inputHandler.send(new Object[] { (2L + inputEventCount), 200.4f, 110 });
    inputHandler.send(new Object[] { (3L + inputEventCount), 300.4f, 100 });
    // Thread.sleep(100);
    inputHandler.send(new Object[] { (4L + inputEventCount), 400.4f, 300 });
    inputHandler.send(new Object[] { (5L + inputEventCount), 500.6f, 120 });
    Thread.sleep(10);
    inputHandler.send(new Object[] { (6L + inputEventCount), 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();
    inputHandler.send(new Object[] { (7L + inputEventCount), 700.6f, 230 });
    Thread.sleep(10);
    inputHandler.send(new Object[] { (8L + inputEventCount), 800.6f, 125 });
    inputHandler.send(new Object[] { (9L + inputEventCount), 900.6f, 370 });
    Thread.sleep(10);
    inputHandler.send(new Object[] { (10L + inputEventCount), 1000.6f, 140 });
    // shutdown Siddhi app
    Thread.sleep(5000);
    siddhiAppRuntime.shutdown();
    AssertJUnit.assertTrue(count <= (inputEventCount + 10));
    AssertJUnit.assertEquals(new Long(3045), 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

SiddhiAppRuntime (io.siddhi.core.SiddhiAppRuntime)13 SiddhiManager (io.siddhi.core.SiddhiManager)13 Event (io.siddhi.core.event.Event)13 CannotRestoreSiddhiAppStateException (io.siddhi.core.exception.CannotRestoreSiddhiAppStateException)13 InputHandler (io.siddhi.core.stream.input.InputHandler)13 IncrementalFileSystemPersistenceStore (io.siddhi.core.util.persistence.IncrementalFileSystemPersistenceStore)13 Test (org.testng.annotations.Test)13 QueryCallback (io.siddhi.core.query.output.callback.QueryCallback)11 AtomicLong (java.util.concurrent.atomic.AtomicLong)11 StreamCallback (io.siddhi.core.stream.output.StreamCallback)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2