Search in sources :

Example 1 with StreamCallback

use of io.siddhi.core.stream.output.StreamCallback in project siddhi by wso2.

the class SiddhiDebuggerClient method start.

/**
 * Start the {@link SiddhiDebuggerClient} and configure the breakpoints.
 *
 * @param siddhiApp the Siddhi query
 * @param input     the user input as a whole text
 */
public void start(final String siddhiApp, String input) {
    SiddhiManager siddhiManager = new SiddhiManager();
    info("Deploying the siddhi app");
    final SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    // Add callbacks for all the streams
    final Set<String> streamNames = SiddhiCompiler.parse(siddhiApp).getStreamDefinitionMap().keySet();
    for (String streamName : streamNames) {
        final String stream = streamName;
        siddhiAppRuntime.addCallback(stream, new StreamCallback() {

            @Override
            public void receive(Event[] events) {
                info("@Receive: Stream: " + stream + ", Event: " + Arrays.deepToString(events));
            }
        });
    }
    SiddhiDebugger siddhiDebugger = siddhiAppRuntime.debug();
    final InputFeeder inputFeeder = new InputFeeder(siddhiAppRuntime, input);
    System.out.println("Configure the breakpoints.\nYou can use the following commands:\n - " + ADD_BREAKPOINT + "<query name>:<IN/OUT>\n - " + REMOVE_BREAKPOINT + "<query name>:<IN/OUT>\n - " + START + "\n - " + STOP);
    printNextLine();
    final Scanner scanner = new Scanner(System.in, "UTF-8");
    while (scanner.hasNextLine()) {
        String userInput = scanner.nextLine().trim();
        String command = userInput.toLowerCase();
        if (command.startsWith(ADD_BREAKPOINT)) {
            if (!command.contains(QUERY_DELIMITER)) {
                error("Invalid add query. The query must be " + ADD_BREAKPOINT + "<query " + "name>:<IN/OUT>. Please try again");
                printNextLine();
                continue;
            }
            String[] components = userInput.substring(ADD_BREAKPOINT.length(), userInput.length()).split(QUERY_DELIMITER);
            String queryName = components[0];
            String terminal = components[1].toLowerCase();
            if (IN.equals(terminal)) {
                siddhiDebugger.acquireBreakPoint(queryName, SiddhiDebugger.QueryTerminal.IN);
                info("Added a breakpoint at the IN terminal of " + queryName);
                printNextLine();
            } else if (OUT.equals(terminal)) {
                siddhiDebugger.acquireBreakPoint(queryName, SiddhiDebugger.QueryTerminal.OUT);
                info("Added a breakpoint at the OUT terminal of " + queryName);
                printNextLine();
            } else {
                error("The terminal must be either IN or OUT but found: " + terminal.toUpperCase() + ". Please try again");
                printNextLine();
            }
        } else if (command.startsWith(REMOVE_BREAKPOINT)) {
            if (!command.contains(QUERY_DELIMITER)) {
                error("Invalid add query. The query must be " + REMOVE_BREAKPOINT + "<query " + "name>:<IN/OUT>. Please try again");
                printNextLine();
                continue;
            }
            String[] components = command.substring(ADD_BREAKPOINT.length(), command.length()).split(QUERY_DELIMITER);
            String queryName = components[0];
            String terminal = components[1];
            if (IN.equals(terminal)) {
                siddhiDebugger.releaseBreakPoint(queryName, SiddhiDebugger.QueryTerminal.IN);
                info("Removed the breakpoint at the IN terminal of " + queryName);
                printNextLine();
            } else if (OUT.equals(terminal)) {
                siddhiDebugger.releaseBreakPoint(queryName, SiddhiDebugger.QueryTerminal.OUT);
                info("Removed the breakpoint at the OUT terminal of " + queryName);
                printNextLine();
            } else {
                error("The terminal must be either IN or OUT but found: " + terminal.toUpperCase());
                printNextLine();
            }
        } else if (STOP.equals(command)) {
            inputFeeder.stop();
            siddhiAppRuntime.shutdown();
            break;
        } else if (START.equals(command)) {
            inputFeeder.start();
            info("Siddhi Debugger starts sending input to Siddhi");
            System.out.println("You can use the following commands:\n - " + NEXT + "\n - " + PLAY + "\n - " + STATE + ":<query name>\n - " + STOP);
            break;
        } else {
            error("Invalid command: " + command);
            printNextLine();
        }
    }
    siddhiDebugger.setDebuggerCallback(new SiddhiDebuggerCallback() {

        @Override
        public void debugEvent(ComplexEvent event, String queryName, SiddhiDebugger.QueryTerminal queryTerminal, SiddhiDebugger debugger) {
            info("@Debug: Query: " + queryName + ", Terminal: " + queryTerminal + ", Event: " + event);
            printNextLine();
            while (scanner.hasNextLine()) {
                String command = scanner.nextLine().trim().toLowerCase();
                if (STOP.equals(command)) {
                    debugger.releaseAllBreakPoints();
                    debugger.play();
                    inputFeeder.stop();
                    siddhiAppRuntime.shutdown();
                    break;
                } else if (NEXT.equals(command)) {
                    debugger.next();
                    break;
                } else if (PLAY.equals(command)) {
                    debugger.play();
                    break;
                } else if (command.startsWith(STATE)) {
                    if (!command.contains(QUERY_DELIMITER)) {
                        error("Invalid get state request. The query must be " + STATE + ":<query " + "name>. Please try again");
                        printNextLine();
                        continue;
                    }
                    String[] components = command.split(QUERY_DELIMITER);
                    String requestQueryName = components[1];
                    Map<String, Object> state = debugger.getQueryState(requestQueryName.trim());
                    System.out.println("Query '" + requestQueryName + "' state : ");
                    for (Map.Entry<String, Object> entry : state.entrySet()) {
                        System.out.println("    '" + entry.getKey() + "' : " + entry.getValue());
                    }
                    printNextLine();
                    continue;
                } else {
                    error("Invalid command: " + command);
                    printNextLine();
                }
            }
        }
    });
    inputFeeder.join();
    if (inputFeeder.isRunning()) {
        info("Input feeder has sopped sending all inputs. If you want to stop the execution, use " + "the STOP command");
        printNextLine();
        while (scanner.hasNextLine()) {
            String command = scanner.nextLine().trim().toLowerCase();
            if (STOP.equals(command)) {
                inputFeeder.stop();
                siddhiAppRuntime.shutdown();
                break;
            } else {
                error("Invalid command: " + command);
                printNextLine();
            }
        }
    }
    scanner.close();
    info("Siddhi Debugger is stopped successfully");
}
Also used : Scanner(java.util.Scanner) StreamCallback(io.siddhi.core.stream.output.StreamCallback) ComplexEvent(io.siddhi.core.event.ComplexEvent) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) ComplexEvent(io.siddhi.core.event.ComplexEvent) Map(java.util.Map) SiddhiManager(io.siddhi.core.SiddhiManager)

Example 2 with StreamCallback

use of io.siddhi.core.stream.output.StreamCallback 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)

Example 3 with StreamCallback

use of io.siddhi.core.stream.output.StreamCallback in project siddhi by wso2.

the class TestDebugger method testDebugger12.

@Test
public void testDebugger12() throws InterruptedException {
    log.info("Siddi Debugger Test 12: Test debugging two queries with concurrent input");
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "@config(async = 'true') " + "define stream cseEventStream (symbol string, price float, volume int); " + "define stream stockEventStream (symbol string, price float, volume int); ";
    final String query = "@info(name = 'query1')" + "from cseEventStream " + "select * " + "insert into OutputStream1; " + "@info(name = 'query2')" + "from stockEventStream " + "select * " + "insert into OutputStream2;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);
    siddhiAppRuntime.addCallback("OutputStream1", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            inEventCount.addAndGet(events.length);
        }
    });
    siddhiAppRuntime.addCallback("OutputStream2", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            inEventCount.addAndGet(events.length);
        }
    });
    final InputHandler cseEventStreamInputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    final InputHandler stockEventStreamInputHandler = siddhiAppRuntime.getInputHandler("stockEventStream");
    SiddhiDebugger siddhiDebugger = siddhiAppRuntime.debug();
    siddhiDebugger.acquireBreakPoint("query1", SiddhiDebugger.QueryTerminal.IN);
    siddhiDebugger.acquireBreakPoint("query2", SiddhiDebugger.QueryTerminal.IN);
    siddhiDebugger.setDebuggerCallback(new SiddhiDebuggerCallback() {

        private AtomicBoolean queryOneResumed = new AtomicBoolean(false);

        @Override
        public void debugEvent(ComplexEvent event, String queryName, SiddhiDebugger.QueryTerminal queryTerminal, SiddhiDebugger debugger) {
            log.info("Query: " + queryName + ":" + queryTerminal);
            log.info(event);
            debugEventCount.addAndGet(getCount(event));
            if ("query1IN".equals(queryName)) {
                try {
                    // Wait for 1 sec
                    Thread.sleep(1000);
                    this.queryOneResumed.set(true);
                } catch (InterruptedException e) {
                }
                AssertJUnit.assertArrayEquals("Incorrect debug event received", new Object[] { "WSO2", 50f, 60 }, event.getOutputData());
            } else if ("query2IN".equals(queryName)) {
                // If query2IN is reached, query1IN must left that break point
                AssertJUnit.assertTrue("Query 2 thread enterted the checkpoint before query 1 is debugged", queryOneResumed.get());
                AssertJUnit.assertArrayEquals("Incorrect debug event received", new Object[] { "IBM", 45f, 80 }, event.getOutputData());
            }
            debugger.next();
        }
    });
    new Thread() {

        @Override
        public void run() {
            try {
                cseEventStreamInputHandler.send(new Object[] { "WSO2", 50f, 60 });
            } catch (InterruptedException e) {
            }
        }
    }.start();
    new Thread() {

        @Override
        public void run() {
            try {
                Thread.sleep(10);
                stockEventStreamInputHandler.send(new Object[] { "IBM", 45f, 80 });
            } catch (InterruptedException e) {
            }
        }
    }.start();
    Thread.sleep(2000);
    AssertJUnit.assertEquals("Invalid number of output events", 2, inEventCount.get());
    AssertJUnit.assertEquals("Invalid number of debug events", 4, debugEventCount.get());
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) StreamCallback(io.siddhi.core.stream.output.StreamCallback) ComplexEvent(io.siddhi.core.event.ComplexEvent) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) ComplexEvent(io.siddhi.core.event.ComplexEvent) StreamEvent(io.siddhi.core.event.stream.StreamEvent) SiddhiManager(io.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Example 4 with StreamCallback

use of io.siddhi.core.stream.output.StreamCallback in project siddhi by wso2.

the class TestDebugger method testDebugger7.

@Test
public void testDebugger7() throws InterruptedException {
    log.info("Siddi Debugger Test 7: Test play traversal in a query with time batch window");
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "define stream cseEventStream (symbol string, price float, volume int);";
    String query = "@info(name = 'query1')" + "from cseEventStream#window.timeBatch(3 sec) " + "select symbol, price, volume " + "insert into OutputStream; ";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);
    siddhiAppRuntime.addCallback("OutputStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            inEventCount.addAndGet(events.length);
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    SiddhiDebugger siddhiDebugger = siddhiAppRuntime.debug();
    siddhiDebugger.acquireBreakPoint("query1", SiddhiDebugger.QueryTerminal.IN);
    siddhiDebugger.setDebuggerCallback(new SiddhiDebuggerCallback() {

        @Override
        public void debugEvent(ComplexEvent event, String queryName, SiddhiDebugger.QueryTerminal queryTerminal, SiddhiDebugger debugger) {
            log.info("Query: " + queryName + "\t" + System.currentTimeMillis());
            log.info(event);
            int count = debugEventCount.addAndGet(getCount(event));
            if (count == 1) {
                AssertJUnit.assertEquals("Incorrect break point", "query1IN", queryName + queryTerminal);
                AssertJUnit.assertArrayEquals("Incorrect debug event received at IN", new Object[] { "WSO2", 50f, 60 }, event.getOutputData());
            } else if (count == 2) {
                AssertJUnit.assertEquals("Incorrect break point", "query1IN", queryName + queryTerminal);
                AssertJUnit.assertArrayEquals("Incorrect debug event received at IN", new Object[] { "WSO2", 70f, 40 }, event.getOutputData());
            } else if (count == 3) {
                AssertJUnit.assertEquals("Incorrect break point", "query1IN", queryName + queryTerminal);
                AssertJUnit.assertArrayEquals("Incorrect debug event received at IN", new Object[] { "WSO2", 60f, 50 }, event.getOutputData());
            }
            debugger.play();
        }
    });
    inputHandler.send(new Object[] { "WSO2", 50f, 60 });
    inputHandler.send(new Object[] { "WSO2", 70f, 40 });
    inputHandler.send(new Object[] { "WSO2", 60f, 50 });
    Thread.sleep(3500);
    AssertJUnit.assertEquals("Invalid number of output events", 3, inEventCount.get());
    AssertJUnit.assertEquals("Invalid number of debug events", 3, debugEventCount.get());
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) StreamCallback(io.siddhi.core.stream.output.StreamCallback) ComplexEvent(io.siddhi.core.event.ComplexEvent) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) ComplexEvent(io.siddhi.core.event.ComplexEvent) StreamEvent(io.siddhi.core.event.stream.StreamEvent) SiddhiManager(io.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Example 5 with StreamCallback

use of io.siddhi.core.stream.output.StreamCallback in project siddhi by wso2.

the class TestDebugger method testDebugger4.

@Test
public void testDebugger4() throws InterruptedException {
    log.info("Siddi Debugger Test 4: Test next traversal in a query with time batch window where next call delays" + " 1 sec");
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "define stream cseEventStream (symbol string, price float, volume int);";
    final String query = "@info(name = 'query1')" + "from cseEventStream#window.timeBatch(1 sec) " + "select symbol, price, volume " + "insert into OutputStream; ";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);
    siddhiAppRuntime.addCallback("OutputStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            inEventCount.addAndGet(events.length);
            AssertJUnit.assertEquals("Cannot emit all three in one time", 1, events.length);
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    SiddhiDebugger siddhiDebugger = siddhiAppRuntime.debug();
    siddhiDebugger.acquireBreakPoint("query1", SiddhiDebugger.QueryTerminal.IN);
    siddhiDebugger.setDebuggerCallback(new SiddhiDebuggerCallback() {

        @Override
        public void debugEvent(ComplexEvent event, String queryName, SiddhiDebugger.QueryTerminal queryTerminal, SiddhiDebugger debugger) {
            log.info(event);
            int count = debugEventCount.addAndGet(getCount(event));
            if (count != 1 && queryTerminal == SiddhiDebugger.QueryTerminal.IN) {
                try {
                    Thread.sleep(1100);
                } catch (InterruptedException e) {
                }
            }
            // next call will not reach OUT since there is a window
            debugger.next();
        }
    });
    inputHandler.send(new Object[] { "WSO2", 50f, 60 });
    inputHandler.send(new Object[] { "WSO2", 70f, 40 });
    inputHandler.send(new Object[] { "WSO2", 60f, 50 });
    Thread.sleep(1500);
    AssertJUnit.assertEquals("Invalid number of output events", 3, inEventCount.get());
    AssertJUnit.assertEquals("Invalid number of debug events", 3, debugEventCount.get());
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) StreamCallback(io.siddhi.core.stream.output.StreamCallback) ComplexEvent(io.siddhi.core.event.ComplexEvent) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) ComplexEvent(io.siddhi.core.event.ComplexEvent) StreamEvent(io.siddhi.core.event.stream.StreamEvent) SiddhiManager(io.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Aggregations

StreamCallback (io.siddhi.core.stream.output.StreamCallback)311 SiddhiAppRuntime (io.siddhi.core.SiddhiAppRuntime)305 SiddhiManager (io.siddhi.core.SiddhiManager)305 Event (io.siddhi.core.event.Event)303 Test (org.testng.annotations.Test)295 InputHandler (io.siddhi.core.stream.input.InputHandler)289 StreamEvent (io.siddhi.core.event.stream.StreamEvent)16 ComplexEvent (io.siddhi.core.event.ComplexEvent)13 UnitTestAppender (io.siddhi.core.UnitTestAppender)12 Logger (org.apache.logging.log4j.core.Logger)12 StreamDefinition (io.siddhi.query.api.definition.StreamDefinition)10 CannotRestoreSiddhiAppStateException (io.siddhi.core.exception.CannotRestoreSiddhiAppStateException)9 SiddhiApp (io.siddhi.query.api.SiddhiApp)9 Partition (io.siddhi.query.api.execution.partition.Partition)8 Query (io.siddhi.query.api.execution.query.Query)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 PrintStream (java.io.PrintStream)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 QueryCallback (io.siddhi.core.query.output.callback.QueryCallback)7 InMemoryBroker (io.siddhi.core.util.transport.InMemoryBroker)7