Search in sources :

Example 1 with QueryCallback

use of io.siddhi.core.query.output.callback.QueryCallback in project siddhi by wso2.

the class OutputRateLimiter method sendToCallBacks.

public void sendToCallBacks(ComplexEventChunk complexEventChunk) {
    MultiProcessStreamReceiver.ReturnEventHolder returnEventHolder = MultiProcessStreamReceiver.getMultiProcessReturn().get();
    if (Level.BASIC.compareTo(siddhiQueryContext.getSiddhiAppContext().getRootMetricsLevel()) <= 0 && latencyTracker != null) {
        latencyTracker.markOut();
    }
    if (returnEventHolder != null) {
        returnEventHolder.setReturnEvents(complexEventChunk);
        return;
    } else if (lockWrapper != null) {
        lockWrapper.unlock();
    }
    if (Level.BASIC.compareTo(siddhiQueryContext.getSiddhiAppContext().getRootMetricsLevel()) <= 0 && latencyTracker != null) {
        latencyTracker.markOut();
    }
    if (lockWrapper != null) {
        lockWrapper.unlock();
    }
    if (!queryCallbacks.isEmpty()) {
        for (QueryCallback callback : queryCallbacks) {
            callback.receiveStreamEvent(complexEventChunk);
        }
    }
    if (outputCallback != null && complexEventChunk.getFirst() != null) {
        complexEventChunk.reset();
        int noOfEvents = 0;
        while (complexEventChunk.hasNext()) {
            ComplexEvent complexEvent = complexEventChunk.next();
            if (complexEvent.getType() == ComplexEvent.Type.EXPIRED) {
                complexEvent.setType(ComplexEvent.Type.CURRENT);
                noOfEvents++;
            } else if (complexEvent.getType() == ComplexEvent.Type.RESET) {
                complexEventChunk.remove();
            } else {
                noOfEvents++;
            }
        }
        if (complexEventChunk.getFirst() != null) {
            outputCallback.send(complexEventChunk, noOfEvents);
        }
    }
}
Also used : ComplexEvent(io.siddhi.core.event.ComplexEvent) MultiProcessStreamReceiver(io.siddhi.core.query.input.MultiProcessStreamReceiver) QueryCallback(io.siddhi.core.query.output.callback.QueryCallback)

Example 2 with QueryCallback

use of io.siddhi.core.query.output.callback.QueryCallback in project siddhi by wso2.

the class UpdateOrInsertTestStoreTestCase method updateOrInsertTableTest10.

@Test
public void updateOrInsertTableTest10() throws InterruptedException, SQLException {
    log.info("updateOrInsertTableTest10");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream StockStream (symbol string, price float, volume long); " + "define stream CheckStockStream (symbol string, volume long, price float); " + "define stream UpdateStockStream (comp string, vol long); " + "@Store(type=\"testStoreContainingInMemoryTable\")\n" + "define table StockTable (symbol string, price float, volume long); ";
    String query = "" + "@info(name = 'query1') " + "from StockStream " + "insert into StockTable ;" + "" + "@info(name = 'query2') " + "from UpdateStockStream left outer join StockTable " + "   on UpdateStockStream.comp == StockTable.symbol " + "select comp as symbol, ifThenElse(price is nulL,5F,price) as price, vol as volume " + "update or insert into StockTable " + "   on StockTable.symbol==symbol;" + "" + "@info(name = 'query3') " + "from CheckStockStream[(symbol==StockTable.symbol and volume == StockTable.volume and " + "price < StockTable.price) in StockTable] " + "insert into OutStream;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    siddhiAppRuntime.addCallback("query3", new QueryCallback() {

        @Override
        public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            if (inEvents != null) {
                for (Event event : inEvents) {
                    inEventCount++;
                    switch(inEventCount) {
                        case 1:
                            AssertJUnit.assertArrayEquals(new Object[] { "IBM", 200L, 0f }, event.getData());
                            break;
                        case 2:
                            AssertJUnit.assertArrayEquals(new Object[] { "WSO2", 300L, 4.6f }, event.getData());
                            break;
                        default:
                            AssertJUnit.assertSame(2, inEventCount);
                    }
                }
                eventArrived = true;
            }
            if (removeEvents != null) {
                removeEventCount = removeEventCount + removeEvents.length;
            }
            eventArrived = true;
        }
    });
    InputHandler stockStream = siddhiAppRuntime.getInputHandler("StockStream");
    InputHandler checkStockStream = siddhiAppRuntime.getInputHandler("CheckStockStream");
    InputHandler updateStockStream = siddhiAppRuntime.getInputHandler("UpdateStockStream");
    siddhiAppRuntime.start();
    stockStream.send(new Object[] { "WSO2", 55.6F, 100L });
    checkStockStream.send(new Object[] { "IBM", 100L, 155.6f });
    checkStockStream.send(new Object[] { "WSO2", 100L, 155.6f });
    updateStockStream.send(new Object[] { "IBM", 200L });
    updateStockStream.send(new Object[] { "WSO2", 300L });
    checkStockStream.send(new Object[] { "IBM", 200L, 0f });
    checkStockStream.send(new Object[] { "WSO2", 300L, 4.6f });
    Thread.sleep(1000);
    AssertJUnit.assertEquals("Number of success events", 2, inEventCount);
    AssertJUnit.assertEquals("Number of remove events", 0, removeEventCount);
    AssertJUnit.assertEquals("Event arrived", true, eventArrived);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) SiddhiManager(io.siddhi.core.SiddhiManager) QueryCallback(io.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 3 with QueryCallback

use of io.siddhi.core.query.output.callback.QueryCallback in project siddhi by wso2.

the class UpdateOrInsertTestStoreTestCase method updateOrInsertTableTest7.

@Test
public void updateOrInsertTableTest7() throws InterruptedException, SQLException {
    log.info("updateOrInsertTableTest7");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream StockStream (symbol string, price float, volume long); " + "define stream CheckStockStream (symbol string, volume long, price float); " + "define stream UpdateStockStream (comp string, vol long); " + "@Store(type=\"testStoreContainingInMemoryTable\")\n" + "define table StockTable (symbol string, price float, volume long); ";
    String query = "" + "@info(name = 'query1') " + "from StockStream " + "insert into StockTable ;" + "" + "@info(name = 'query2') " + "from UpdateStockStream " + "select comp as symbol,  5f as price, vol as volume " + "update or insert into StockTable " + "   on StockTable.symbol==symbol;" + "" + "@info(name = 'query3') " + "from CheckStockStream[(symbol==StockTable.symbol and volume==StockTable.volume and price " + "< StockTable.price) in StockTable] " + "insert into OutStream;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    siddhiAppRuntime.addCallback("query3", new QueryCallback() {

        @Override
        public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            if (inEvents != null) {
                for (Event event : inEvents) {
                    inEventCount++;
                    switch(inEventCount) {
                        case 1:
                            AssertJUnit.assertArrayEquals(new Object[] { "IBM", 100L, 56.6f }, event.getData());
                            break;
                        case 2:
                            AssertJUnit.assertArrayEquals(new Object[] { "IBM", 200L, 0f }, event.getData());
                            break;
                        default:
                            AssertJUnit.assertSame(2, inEventCount);
                    }
                }
                eventArrived = true;
            }
            if (removeEvents != null) {
                removeEventCount = removeEventCount + removeEvents.length;
            }
            eventArrived = true;
        }
    });
    InputHandler stockStream = siddhiAppRuntime.getInputHandler("StockStream");
    InputHandler checkStockStream = siddhiAppRuntime.getInputHandler("CheckStockStream");
    InputHandler updateStockStream = siddhiAppRuntime.getInputHandler("UpdateStockStream");
    siddhiAppRuntime.start();
    stockStream.send(new Object[] { "WSO2", 55.6F, 100L });
    stockStream.send(new Object[] { "IBM", 155.6F, 100L });
    checkStockStream.send(new Object[] { "IBM", 100L, 56.6f });
    checkStockStream.send(new Object[] { "WSO2", 100L, 155.6f });
    updateStockStream.send(new Object[] { "IBM", 200L });
    checkStockStream.send(new Object[] { "IBM", 200L, 0f });
    checkStockStream.send(new Object[] { "WSO2", 100L, 155.6f });
    Thread.sleep(2000);
    AssertJUnit.assertEquals("Number of success events", 2, inEventCount);
    AssertJUnit.assertEquals("Number of remove events", 0, removeEventCount);
    AssertJUnit.assertEquals("Event arrived", true, eventArrived);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) SiddhiManager(io.siddhi.core.SiddhiManager) QueryCallback(io.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 4 with QueryCallback

use of io.siddhi.core.query.output.callback.QueryCallback in project siddhi by wso2.

the class CacheLRUTestCase method cacheLRUTestCase5.

@Test(description = "cacheLRUTestCase5", dependsOnMethods = { "cacheLRUTestCase4" })
public // 2 primary keys & LRu & update func
void cacheLRUTestCase5() throws InterruptedException, SQLException {
    final TestAppenderToValidateLogsForCachingTests appender = new TestAppenderToValidateLogsForCachingTests("TestAppenderToValidateLogsForCachingTests", null);
    final Logger logger = (Logger) LogManager.getRootLogger();
    logger.setLevel(Level.DEBUG);
    logger.addAppender(appender);
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream StockStream (symbol string, price float, volume long); " + "define stream UpdateStockStream (symbol string, price float, volume long); " + "@Store(type=\"testStoreForCacheMiss\", @Cache(size=\"2\", cache.policy=\"LRU\"))\n" + "@PrimaryKey(\'symbol\', \'price\') " + "define table StockTable (symbol string, price float, volume long); ";
    String query = "" + "@info(name = 'query1') " + "from StockStream " + "insert into StockTable ;" + "" + "@info(name = 'query2') " + "from UpdateStockStream\n" + "select symbol, price, volume\n" + "update StockTable\n" + "on (StockTable.symbol == symbol AND StockTable.price == price);";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    siddhiAppRuntime.addCallback("query2", new QueryCallback() {

        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            if (inEvents != null) {
                for (Event event : inEvents) {
                    inEventCount++;
                    switch(inEventCount) {
                        case 1:
                            Assert.assertEquals(event.getData(), new Object[] { "WSO2", 55.6f, 3L });
                            break;
                    }
                }
                eventArrived = true;
            }
        }
    });
    InputHandler stockStream = siddhiAppRuntime.getInputHandler("StockStream");
    InputHandler updateStockStream = siddhiAppRuntime.getInputHandler("UpdateStockStream");
    siddhiAppRuntime.start();
    stockStream.send(new Object[] { "WSO2", 55.6f, 1L });
    Thread.sleep(10);
    stockStream.send(new Object[] { "APPLE", 75.6f, 2L });
    Thread.sleep(10);
    updateStockStream.send(new Object[] { "WSO2", 55.6f, 3L });
    Thread.sleep(10);
    stockStream.send(new Object[] { "CISCO", 86.6f, 5L });
    Event[] events = siddhiAppRuntime.query("" + "from StockTable " + "on symbol == \"APPLE\" AND price == 75.6f ");
    EventPrinter.print(events);
    AssertJUnit.assertEquals(1, events.length);
    final List<String> loggedEvents = ((TestAppenderToValidateLogsForCachingTests) logger.getAppenders().get("TestAppenderToValidateLogsForCachingTests")).getLog();
    List<String> logMessages = new ArrayList<>();
    for (String logEvent : loggedEvents) {
        String message = String.valueOf(logEvent);
        if (message.contains(":")) {
            message = message.split(":")[1].trim();
        }
        logMessages.add(message);
    }
    Assert.assertEquals(logMessages.contains("store table size is smaller than max cache. Sending results from cache"), false);
    Assert.assertEquals(logMessages.contains("store table size is bigger than cache."), true);
    Assert.assertEquals(Collections.frequency(logMessages, "store table size is bigger than cache."), 1);
    Assert.assertEquals(logMessages.contains("cache constraints satisfied. Checking cache"), true);
    Assert.assertEquals(Collections.frequency(logMessages, "cache constraints satisfied. Checking cache"), 1);
    Assert.assertEquals(logMessages.contains("cache hit. Sending results from cache"), false);
    Assert.assertEquals(logMessages.contains("cache miss. Loading from store"), true);
    Assert.assertEquals(Collections.frequency(logMessages, "cache miss. Loading from store"), 1);
    Assert.assertEquals(logMessages.contains("store also miss. sending null"), false);
    Assert.assertEquals(logMessages.contains("sending results from cache after loading from store"), true);
    Assert.assertEquals(Collections.frequency(logMessages, "sending results from cache after loading from store"), 1);
    Assert.assertEquals(logMessages.contains("sending results from store"), false);
    logger.removeAppender(appender);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) TestAppenderToValidateLogsForCachingTests(io.siddhi.core.query.table.util.TestAppenderToValidateLogsForCachingTests) ArrayList(java.util.ArrayList) Logger(org.apache.logging.log4j.core.Logger) QueryCallback(io.siddhi.core.query.output.callback.QueryCallback) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) SiddhiManager(io.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Example 5 with QueryCallback

use of io.siddhi.core.query.output.callback.QueryCallback in project siddhi by wso2.

the class CacheLRUTestCase method cacheLRUTestCase8.

@Test(description = "cacheLRUTestCase8", dependsOnMethods = { "cacheLRUTestCase7" })
public // 2 primary keys & LRu & update or add func with update
void cacheLRUTestCase8() throws InterruptedException, SQLException {
    final TestAppenderToValidateLogsForCachingTests appender = new TestAppenderToValidateLogsForCachingTests("TestAppenderToValidateLogsForCachingTests", null);
    final Logger logger = (Logger) LogManager.getRootLogger();
    logger.setLevel(Level.DEBUG);
    logger.addAppender(appender);
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream StockStream (symbol string, price float, volume long); " + "define stream UpdateStockStream (symbol string, price float, volume long); " + "@Store(type=\"testStoreForCacheMiss\", @Cache(size=\"2\", cache.policy=\"LRU\"))\n" + "@PrimaryKey(\'symbol\', \'price\') " + "define table StockTable (symbol string, price float, volume long); ";
    String query = "" + "@info(name = 'query1') " + "from StockStream " + "insert into StockTable ;" + "" + "@info(name = 'query2') " + "from UpdateStockStream " + "update or insert into StockTable " + "   on (StockTable.symbol == symbol AND StockTable.price == price);";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    siddhiAppRuntime.addCallback("query2", new QueryCallback() {

        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            if (inEvents != null) {
                for (Event event : inEvents) {
                    inEventCount++;
                    switch(inEventCount) {
                        case 1:
                            Assert.assertEquals(event.getData(), new Object[] { "WSO2", 55.6f, 3L });
                            break;
                    }
                }
                eventArrived = true;
            }
        }
    });
    InputHandler stockStream = siddhiAppRuntime.getInputHandler("StockStream");
    InputHandler updateStockStream = siddhiAppRuntime.getInputHandler("UpdateStockStream");
    siddhiAppRuntime.start();
    stockStream.send(new Object[] { "WSO2", 55.6f, 1L });
    Thread.sleep(10);
    stockStream.send(new Object[] { "APPLE", 75.6f, 2L });
    Thread.sleep(10);
    updateStockStream.send(new Object[] { "WSO2", 55.6f, 3L });
    Thread.sleep(10);
    stockStream.send(new Object[] { "CISCO", 86.6f, 5L });
    Event[] events = siddhiAppRuntime.query("" + "from StockTable " + "on symbol == \"APPLE\" AND price == 75.6f ");
    EventPrinter.print(events);
    AssertJUnit.assertEquals(1, events.length);
    final List<String> loggedEvents = ((TestAppenderToValidateLogsForCachingTests) logger.getAppenders().get("TestAppenderToValidateLogsForCachingTests")).getLog();
    List<String> logMessages = new ArrayList<>();
    for (String logEvent : loggedEvents) {
        String message = String.valueOf(logEvent);
        if (message.contains(":")) {
            message = message.split(":")[1].trim();
        }
        logMessages.add(message);
    }
    Assert.assertEquals(logMessages.contains("store table size is smaller than max cache. Sending results from cache"), false);
    Assert.assertEquals(logMessages.contains("store table size is bigger than cache."), true);
    Assert.assertEquals(Collections.frequency(logMessages, "store table size is bigger than cache."), 1);
    Assert.assertEquals(logMessages.contains("cache constraints satisfied. Checking cache"), true);
    Assert.assertEquals(Collections.frequency(logMessages, "cache constraints satisfied. Checking cache"), 1);
    Assert.assertEquals(logMessages.contains("cache hit. Sending results from cache"), false);
    Assert.assertEquals(logMessages.contains("cache miss. Loading from store"), true);
    Assert.assertEquals(Collections.frequency(logMessages, "cache miss. Loading from store"), 1);
    Assert.assertEquals(logMessages.contains("store also miss. sending null"), false);
    Assert.assertEquals(logMessages.contains("sending results from cache after loading from store"), true);
    Assert.assertEquals(Collections.frequency(logMessages, "sending results from cache after loading from store"), 1);
    Assert.assertEquals(logMessages.contains("sending results from store"), false);
    logger.removeAppender(appender);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) TestAppenderToValidateLogsForCachingTests(io.siddhi.core.query.table.util.TestAppenderToValidateLogsForCachingTests) ArrayList(java.util.ArrayList) Logger(org.apache.logging.log4j.core.Logger) QueryCallback(io.siddhi.core.query.output.callback.QueryCallback) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) SiddhiManager(io.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Aggregations

QueryCallback (io.siddhi.core.query.output.callback.QueryCallback)824 SiddhiAppRuntime (io.siddhi.core.SiddhiAppRuntime)823 Test (org.testng.annotations.Test)822 SiddhiManager (io.siddhi.core.SiddhiManager)821 Event (io.siddhi.core.event.Event)818 InputHandler (io.siddhi.core.stream.input.InputHandler)817 StreamDefinition (io.siddhi.query.api.definition.StreamDefinition)75 SiddhiApp (io.siddhi.query.api.SiddhiApp)74 Query (io.siddhi.query.api.execution.query.Query)74 ArrayList (java.util.ArrayList)43 Logger (org.apache.logging.log4j.core.Logger)26 CannotRestoreSiddhiAppStateException (io.siddhi.core.exception.CannotRestoreSiddhiAppStateException)23 TestAppenderToValidateLogsForCachingTests (io.siddhi.core.query.table.util.TestAppenderToValidateLogsForCachingTests)21 InMemoryPersistenceStore (io.siddhi.core.util.persistence.InMemoryPersistenceStore)11 IncrementalFileSystemPersistenceStore (io.siddhi.core.util.persistence.IncrementalFileSystemPersistenceStore)11 PersistenceStore (io.siddhi.core.util.persistence.PersistenceStore)11 AtomicLong (java.util.concurrent.atomic.AtomicLong)9 StreamCallback (io.siddhi.core.stream.output.StreamCallback)7 StringConcatAggregatorExecutorString (io.siddhi.core.query.extension.util.StringConcatAggregatorExecutorString)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6