Search in sources :

Example 6 with IncrementalFileSystemPersistenceStore

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

the class IncrementalPersistenceTestCase method incrementalPersistenceTest1.

@Test
public void incrementalPersistenceTest1() throws InterruptedException {
    log.info("Incremental persistence test 1 - length window query");
    final int inputEventCount = 10;
    final int eventWindowSize = 4;
    SiddhiManager siddhiManager = new SiddhiManager();
    siddhiManager.setIncrementalPersistenceStore(new IncrementalFileSystemPersistenceStore(storageFilePath));
    String siddhiApp = "" + "@app:name('incrementalPersistenceTest1') " + "" + "define stream StockStream ( symbol string, price float, volume int );" + "" + "@info(name = 'query1')" + "from StockStream[price>10]#window.length(" + eventWindowSize + ") " + "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();
    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[] { "IBM", 100.4f, 100 });
    // Thread.sleep(100);
    inputHandler.send(new Object[] { "WSO2", 200.4f, 100 });
    inputHandler.send(new Object[] { "IBM", 300.4f, 100 });
    // Thread.sleep(100);
    inputHandler.send(new Object[] { "WSO2", 400.4f, 200 });
    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) {
        log.error(e.getMessage(), e);
        Assert.fail("Restoring of Siddhi app " + siddhiAppRuntime.getName() + " failed");
    }
    siddhiAppRuntime.start();
    Thread.sleep(5000);
    inputHandler.send(new Object[] { "IBM", 500.6f, 300 });
    inputHandler.send(new Object[] { "WSO2", 600.6f, 400 });
    // shutdown Siddhi app
    Thread.sleep(500);
    siddhiAppRuntime.shutdown();
    AssertJUnit.assertTrue(count <= (inputEventCount + 6));
    AssertJUnit.assertEquals(new Long(1000), 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 7 with IncrementalFileSystemPersistenceStore

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

the class IncrementalPersistenceTestCase method incrementalPersistenceTest11.

@Test
public void incrementalPersistenceTest11() throws InterruptedException {
    log.info("Incremental persistence test 11");
    AtomicInteger count = new AtomicInteger();
    AtomicLong ibmCount = new AtomicLong();
    AtomicLong wso2Count = new AtomicLong();
    SiddhiManager siddhiManager = new SiddhiManager();
    siddhiManager.setIncrementalPersistenceStore(new IncrementalFileSystemPersistenceStore(storageFilePath));
    String siddhiApp = "@app:name('incrementalPersistenceTest11') " + "define stream StockQuote (symbol string, price float, volume int);" + "partition with (symbol of StockQuote) " + "begin " + "@info(name = 'query1') " + "from StockQuote#window.length(4)  " + "select symbol, count(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);
            AssertJUnit.assertTrue("IBM".equals(events[0].getData(0)) || "WSO2".equals(events[0].getData(0)));
            count.addAndGet(events.length);
            for (Event event : events) {
                if ("IBM".equals(event.getData(0))) {
                    ibmCount.set((Long) event.getData(1));
                } else {
                    wso2Count.set((Long) event.getData(1));
                }
            }
            eventArrived = true;
        }
    };
    siddhiAppRuntime.addCallback("OutStockStream", streamCallback);
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("StockQuote");
    siddhiAppRuntime.start();
    inputHandler.send(new Event(System.currentTimeMillis(), new Object[] { "IBM", 700f, 100 }));
    inputHandler.send(new Event(System.currentTimeMillis(), new Object[] { "WSO2", 60f, 50 }));
    inputHandler.send(new Event(System.currentTimeMillis(), new Object[] { "WSO2", 50f, 60 }));
    inputHandler.send(new Event(System.currentTimeMillis(), new Object[] { "WSO2", 40f, 60 }));
    inputHandler.send(new Event(System.currentTimeMillis(), new Object[] { "WSO2", 30f, 60 }));
    siddhiAppRuntime.persist();
    Thread.sleep(2000);
    siddhiAppRuntime.shutdown();
    siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("OutStockStream", streamCallback);
    inputHandler = siddhiAppRuntime.getInputHandler("StockQuote");
    // loading
    try {
        siddhiAppRuntime.restoreLastRevision();
    } catch (CannotRestoreSiddhiAppStateException e) {
        Assert.fail("Restoring of Siddhi app " + siddhiAppRuntime.getName() + " failed");
    }
    siddhiAppRuntime.start();
    Thread.sleep(2000);
    inputHandler.send(new Event(System.currentTimeMillis(), new Object[] { "IBM", 800f, 100 }));
    inputHandler.send(new Event(System.currentTimeMillis(), new Object[] { "WSO2", 20f, 60 }));
    AssertJUnit.assertTrue(eventArrived);
    AssertJUnit.assertEquals(7, count.get());
    AssertJUnit.assertEquals(2, ibmCount.get());
    AssertJUnit.assertEquals(4, wso2Count.get());
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) 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) StreamCallback(io.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 8 with IncrementalFileSystemPersistenceStore

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

the class IncrementalPersistenceTestCase method incrementalPersistenceTest4_1.

@Test
public void incrementalPersistenceTest4_1() throws InterruptedException {
    log.info("Incremental persistence test 4_1 - 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(5000);
    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(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 9 with IncrementalFileSystemPersistenceStore

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

the class IncrementalPersistenceTestCase method incrementalPersistenceTest6.

@Test
public void incrementalPersistenceTest6() throws InterruptedException {
    log.info("Incremental persistence test 6 - in-memory table persistance test without primary key.");
    int inputEventCountPerCategory = 10;
    SiddhiManager siddhiManager = new SiddhiManager();
    siddhiManager.setIncrementalPersistenceStore(new IncrementalFileSystemPersistenceStore(storageFilePath));
    String streams = "" + "@app:name('incrementalPersistenceTest6') " + "" + "define stream StockStream (symbol2 string, price float, volume long); " + "define stream CheckStockStream (symbol1 string); " + "define stream OutStream (symbol1 string, TB long); " + "define table StockTable (symbol2 string, price float, volume long); ";
    String query = "" + "@info(name = 'query1') " + "from StockStream " + "insert into StockTable ;" + "" + "@info(name = 'query2') " + "from StockTable join CheckStockStream " + " on symbol2 == symbol1 " + "select symbol2 as symbol1, sum(StockTable.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", 55.6f + i, 180L + i });
        }
        for (int i = 0; i < inputEventCountPerCategory; i++) {
            stockStream.send(new Object[] { "IBM", 55.6f + i, 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");
        }
        siddhiAppRuntime.start();
        Thread.sleep(2000);
        stockStream.send(new Object[] { "WSO2", 100.6f, 180L });
        stockStream.send(new Object[] { "IBM", 100.6f, 100L });
        stockStream.send(new Object[] { "WSO2", 8.6f, 13L });
        stockStream.send(new Object[] { "IBM", 7.6f, 14L });
        checkStockStream.send(new Object[] { "IBM" });
        checkStockStream.send(new Object[] { "WSO2" });
        List<Object[]> expected = Arrays.asList(new Object[] { "IBM", 1159L }, new Object[] { "WSO2", 2038L });
        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 10 with IncrementalFileSystemPersistenceStore

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

the class IncrementalPersistenceTestCase method incrementalPersistenceTest9.

@Test
public void incrementalPersistenceTest9() throws InterruptedException {
    log.info("Incremental persistence test 9 - min-max counting with group-by.");
    final int inputEventCount = 10;
    SiddhiManager siddhiManager = new SiddhiManager();
    siddhiManager.setIncrementalPersistenceStore(new IncrementalFileSystemPersistenceStore(storageFilePath));
    String streams = "" + "@app:name('incrementalPersistenceTest9') " + "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 " + "group by roomNo " + "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[] { 4, 50L });
        // AssertJUnit.assertTrue(count <= (inputEventCount + 3));
        AssertJUnit.assertEquals(new Long(59), 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)

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