Search in sources :

Example 1 with IncrementalFileSystemPersistenceStore

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

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

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

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

Example 5 with IncrementalFileSystemPersistenceStore

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

the class IncrementalPersistenceTestCase method incrementalPersistenceTest10.

@Test
public void incrementalPersistenceTest10() throws InterruptedException {
    log.info("Incremental persistence test 10 - partitioned sum with group-by on length windows.");
    final int inputEventCount = 10;
    SiddhiManager siddhiManager = new SiddhiManager();
    siddhiManager.setIncrementalPersistenceStore(new IncrementalFileSystemPersistenceStore(storageFilePath));
    String siddhiApp = "@app:name('incrementalPersistenceTest10') " + "define stream cseEventStreamOne (symbol string, price float,volume int);" + "partition with (price>=100 as 'large' or price<100 as 'small' of cseEventStreamOne) " + "begin @info(name " + "= 'query1') from cseEventStreamOne#window.length(4) select symbol,sum(price) as price " + "group by symbol insert into " + "OutStockStream ;  end ";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    StreamCallback streamCallback = new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            eventArrived = true;
            if (events != null) {
                for (Event event : events) {
                    count++;
                    inEventsList.add(event.getData());
                    inEventCount.incrementAndGet();
                    lastValue = ((Double) event.getData(1)).longValue();
                }
            }
        }
    };
    siddhiAppRuntime.addCallback("OutStockStream", streamCallback);
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStreamOne");
    siddhiAppRuntime.start();
    for (int i = 0; i < inputEventCount; i++) {
        inputHandler.send(new Object[] { "IBM", 95f + i, 100 });
        Thread.sleep(500);
        siddhiAppRuntime.persist();
    }
    siddhiAppRuntime.persist();
    inputHandler.send(new Object[] { "IBM", 205f, 100 });
    Thread.sleep(2000);
    siddhiAppRuntime.shutdown();
    siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    // siddhiAppRuntime.addCallback("query1", queryCallback);
    siddhiAppRuntime.addCallback("OutStockStream", streamCallback);
    inputHandler = siddhiAppRuntime.getInputHandler("cseEventStreamOne");
    // loading
    try {
        siddhiAppRuntime.restoreLastRevision();
    } catch (CannotRestoreSiddhiAppStateException e) {
        Assert.fail("Restoring of Siddhi app " + siddhiAppRuntime.getName() + " failed", e);
    }
    siddhiAppRuntime.start();
    Thread.sleep(2000);
    inputHandler.send(new Object[] { "IBM", 105f, 100 });
    Thread.sleep(1000);
    AssertJUnit.assertEquals(new Long(414), lastValue);
    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) StreamCallback(io.siddhi.core.stream.output.StreamCallback) 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