Search in sources :

Example 86 with StreamCallback

use of org.ballerinalang.siddhi.core.stream.output.StreamCallback in project ballerina by ballerina-lang.

the class PartitionTestCase1 method testPartitionQuery17.

@Test
public void testPartitionQuery17() throws InterruptedException {
    log.info("Partition test17");
    SiddhiManager siddhiManager = new SiddhiManager();
    SiddhiApp siddhiApp = new SiddhiApp("plan17");
    StreamDefinition streamDefinition = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.FLOAT).attribute("volume", Attribute.Type.INT);
    siddhiApp.defineStream(streamDefinition);
    Partition partition = Partition.partition().with("cseEventStream", Partition.range("LessValue", Expression.compare(Expression.value(200), Compare.Operator.GREATER_THAN, Expression.variable("volume"))), Partition.range("HighValue", Expression.compare(Expression.value(200), Compare.Operator.LESS_THAN_EQUAL, Expression.variable("volume"))));
    Query query = Query.query();
    query.from(InputStream.stream("cseEventStream"));
    query.select(Selector.selector().select("sumvolume", Expression.function("sum", Expression.variable("volume"))));
    query.insertInto("StockStream");
    partition.addQuery(query);
    siddhiApp.addPartition(partition);
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("StockStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            for (Event event : events) {
                count.incrementAndGet();
                if (count.get() == 1) {
                    AssertJUnit.assertEquals(100L, event.getData()[0]);
                } else if (count.get() == 2) {
                    AssertJUnit.assertEquals(600L, event.getData()[0]);
                } else if (count.get() == 3) {
                    AssertJUnit.assertEquals(200L, event.getData()[0]);
                } else if (count.get() == 4) {
                    AssertJUnit.assertEquals(250L, event.getData()[0]);
                }
                eventArrived = true;
            }
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "IBM", 75.6f, 100 });
    inputHandler.send(new Object[] { "WSO2", 75.6f, 600 });
    inputHandler.send(new Object[] { "IBM", 75.6f, 100 });
    Thread.sleep(100);
    inputHandler.send(new Object[] { "ORACLE", 75.6f, 50 });
    SiddhiTestHelper.waitForEvents(100, 4, count, 60000);
    AssertJUnit.assertEquals(4, count.get());
    siddhiAppRuntime.shutdown();
}
Also used : Partition(org.ballerinalang.siddhi.query.api.execution.partition.Partition) InputHandler(org.ballerinalang.siddhi.core.stream.input.InputHandler) SiddhiApp(org.ballerinalang.siddhi.query.api.SiddhiApp) StreamDefinition(org.ballerinalang.siddhi.query.api.definition.StreamDefinition) Query(org.ballerinalang.siddhi.query.api.execution.query.Query) SiddhiAppRuntime(org.ballerinalang.siddhi.core.SiddhiAppRuntime) Event(org.ballerinalang.siddhi.core.event.Event) SiddhiManager(org.ballerinalang.siddhi.core.SiddhiManager) StreamCallback(org.ballerinalang.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 87 with StreamCallback

use of org.ballerinalang.siddhi.core.stream.output.StreamCallback in project ballerina by ballerina-lang.

the class PartitionTestCase1 method testPartitionQuery42.

@Test(expectedExceptions = SiddhiAppCreationException.class)
public void testPartitionQuery42() throws InterruptedException {
    log.info("Partition test");
    SiddhiApp siddhiApp = SiddhiApp.siddhiApp("Test").defineStream(StreamDefinition.id("streamA").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.INT));
    Query query = Query.query();
    query.annotation(Annotation.annotation("info").element("name", "query1"));
    query.from(InputStream.stream("streamA"));
    query.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("price", Expression.variable("price")));
    query.insertInto("StockQuote");
    Partition partition = Partition.partition();
    partition.addQuery(query);
    siddhiApp.addPartition(partition);
    SiddhiManager siddhiManager = new SiddhiManager();
    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);
            eventArrived = true;
        }
    };
    siddhiAppRuntime.addCallback("StockQuote", streamCallback);
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("streamA");
    siddhiAppRuntime.start();
    inputHandler.send(new Event(System.currentTimeMillis(), new Object[] { "IBM", 700 }));
    inputHandler.send(new Event(System.currentTimeMillis(), new Object[] { "WSO2", 60 }));
    inputHandler.send(new Event(System.currentTimeMillis(), new Object[] { "WSO2", 60 }));
    Thread.sleep(1000);
    AssertJUnit.assertEquals(0, count.get());
    siddhiAppRuntime.shutdown();
}
Also used : Partition(org.ballerinalang.siddhi.query.api.execution.partition.Partition) InputHandler(org.ballerinalang.siddhi.core.stream.input.InputHandler) SiddhiApp(org.ballerinalang.siddhi.query.api.SiddhiApp) Query(org.ballerinalang.siddhi.query.api.execution.query.Query) SiddhiAppRuntime(org.ballerinalang.siddhi.core.SiddhiAppRuntime) Event(org.ballerinalang.siddhi.core.event.Event) SiddhiManager(org.ballerinalang.siddhi.core.SiddhiManager) StreamCallback(org.ballerinalang.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 88 with StreamCallback

use of org.ballerinalang.siddhi.core.stream.output.StreamCallback in project ballerina by ballerina-lang.

the class PartitionTestCase1 method testPartitionQuery5.

@Test
public void testPartitionQuery5() throws InterruptedException {
    log.info("Partition test5");
    final SiddhiManager siddhiManager = new SiddhiManager();
    SiddhiApp siddhiApp = new SiddhiApp("plan5");
    StreamDefinition streamDefinition = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.FLOAT).attribute("volume", Attribute.Type.INT);
    StreamDefinition streamDefinition1 = StreamDefinition.id("cseEventStream1").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.FLOAT).attribute("volume", Attribute.Type.INT);
    StreamDefinition streamDefinition2 = StreamDefinition.id("StockStream").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.FLOAT).attribute("volume", Attribute.Type.INT);
    siddhiApp.defineStream(streamDefinition);
    siddhiApp.defineStream(streamDefinition1);
    siddhiApp.defineStream(streamDefinition2);
    Partition partition = Partition.partition().with("cseEventStream", Expression.variable("symbol"));
    Query query = Query.query();
    query.from(InputStream.stream("cseEventStream")).annotation(Annotation.annotation("info").element("name", "query"));
    query.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("price", Expression.variable("price")).select("volume", Expression.variable("volume")));
    query.insertIntoInner("StockStream");
    Query query1 = Query.query();
    query1.from(InputStream.innerStream("StockStream")).annotation(Annotation.annotation("info").element("name", "query1"));
    query1.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("price", Expression.variable("price")).select("volume", Expression.variable("volume")));
    query1.insertInto("OutStockStream");
    // //////////////////////////////////partition-2/////////////////
    Partition partition1 = Partition.partition().with("cseEventStream1", Expression.variable("symbol"));
    Query query2 = Query.query();
    query2.from(InputStream.stream("cseEventStream1")).annotation(Annotation.annotation("info").element("name", "query2"));
    query2.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("price", Expression.variable("price")).select("volume", Expression.variable("volume")));
    query2.insertIntoInner("StockStream");
    Query query3 = Query.query();
    query3.from(InputStream.innerStream("StockStream")).annotation(Annotation.annotation("info").element("name", "query3"));
    query3.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("price", Expression.variable("price")).select("volume", Expression.variable("volume")));
    query3.insertInto("OutStockStream");
    Query query4 = Query.query();
    query4.from(InputStream.stream("StockStream")).annotation(Annotation.annotation("info").element("name", "query4"));
    query4.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("price", Expression.variable("price")).select("volume", Expression.variable("volume")));
    query4.insertInto("OutStockStream");
    Query query5 = Query.query();
    query5.from(InputStream.innerStream("StockStream")).annotation(Annotation.annotation("info").element("name", "query5"));
    query5.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("price", Expression.variable("price")).select("volume", Expression.variable("volume")));
    query5.insertInto("StockStream");
    partition.addQuery(query);
    partition.addQuery(query1);
    partition.addQuery(query5);
    partition1.addQuery(query2);
    partition1.addQuery(query3);
    siddhiApp.addPartition(partition);
    siddhiApp.addPartition(partition1);
    siddhiApp.addQuery(query4);
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("StockStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            stockStreamEventCount = stockStreamEventCount + events.length;
        }
    });
    siddhiAppRuntime.addCallback("OutStockStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            synchronized (siddhiManager) {
                count.addAndGet(events.length);
                eventArrived = true;
            }
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    InputHandler inputHandler2 = siddhiAppRuntime.getInputHandler("cseEventStream1");
    InputHandler inputHandler3 = siddhiAppRuntime.getInputHandler("StockStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "IBM", 75.6f, 100 });
    inputHandler.send(new Object[] { "WSO2", 75.6f, 100 });
    inputHandler.send(new Object[] { "IBM", 75.6f, 100 });
    inputHandler.send(new Object[] { "ORACLE", 75.6f, 100 });
    inputHandler2.send(new Object[] { "IBM1", 75.6f, 100 });
    inputHandler2.send(new Object[] { "WSO21", 75.6f, 100 });
    inputHandler2.send(new Object[] { "IBM1", 75.6f, 100 });
    inputHandler2.send(new Object[] { "ORACLE1", 75.6f, 100 });
    inputHandler3.send(new Object[] { "ABC", 75.6f, 100 });
    inputHandler3.send(new Object[] { "DEF", 75.6f, 100 });
    inputHandler3.send(new Object[] { "KLM", 75.6f, 100 });
    inputHandler3.send(new Object[] { "ABC", 75.6f, 100 });
    SiddhiTestHelper.waitForEvents(100, 16, count, 60000);
    AssertJUnit.assertEquals(16, count.get());
    Thread.sleep(100);
    AssertJUnit.assertEquals(8, stockStreamEventCount);
    siddhiAppRuntime.shutdown();
}
Also used : Partition(org.ballerinalang.siddhi.query.api.execution.partition.Partition) InputHandler(org.ballerinalang.siddhi.core.stream.input.InputHandler) SiddhiApp(org.ballerinalang.siddhi.query.api.SiddhiApp) StreamDefinition(org.ballerinalang.siddhi.query.api.definition.StreamDefinition) Query(org.ballerinalang.siddhi.query.api.execution.query.Query) SiddhiAppRuntime(org.ballerinalang.siddhi.core.SiddhiAppRuntime) Event(org.ballerinalang.siddhi.core.event.Event) SiddhiManager(org.ballerinalang.siddhi.core.SiddhiManager) StreamCallback(org.ballerinalang.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 89 with StreamCallback

use of org.ballerinalang.siddhi.core.stream.output.StreamCallback in project ballerina by ballerina-lang.

the class PartitionTestCase1 method testPartitionQuery35.

@Test
public void testPartitionQuery35() throws InterruptedException {
    log.info("Partition test34");
    SiddhiManager siddhiManager = new SiddhiManager();
    String siddhiApp = "@app:name('PartitionTest34') " + "define stream cseEventStream (atr1 string,  atr2 float, atr3 int, atr4 double, " + "atr5 long,  atr6 long,  atr7 double,  atr8 float , atr9 bool, atr10 bool,  atr11 int);" + "partition with (atr1 of cseEventStream) begin @info(name = 'query1') from " + "cseEventStream[700>atr5 OR atr8 > atr3 OR atr6 > atr2 OR atr4 " + "> atr6 OR atr2 > atr7 OR atr3 > atr4 OR atr4 > atr3 OR atr6 > atr3 OR atr3" + "> atr2 OR atr8 > atr5 OR atr6 > atr4 OR atr3 > atr6 OR atr7 > atr8 OR atr7 " + "> atr4 OR atr6 > atr5 OR atr11 > atr3 OR atr8 > atr2] select" + " atr1 as symbol, sum(atr2) as price" + " insert into " + "OutStockStream ;  end ";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("OutStockStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            count.addAndGet(events.length);
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "IBM", 75.6f, 100, 101.0, 500L, 200L, 102.0, 75.7f, false, true, 105 });
    inputHandler.send(new Object[] { "WSO2", 75.6f, 100, 101.0, 501L, 201L, 103.0, 76.7f, false, true, 106 });
    inputHandler.send(new Object[] { "IBM", 75.6f, 100, 102.0, 502L, 202L, 104.0, 77.7f, false, true, 107 });
    inputHandler.send(new Object[] { "ORACLE", 75.6f, 100, 101.0, 502L, 202L, 104.0, 77.7f, false, true, 108 });
    SiddhiTestHelper.waitForEvents(100, 4, count, 60000);
    AssertJUnit.assertEquals(4, count.get());
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.ballerinalang.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.ballerinalang.siddhi.core.SiddhiAppRuntime) Event(org.ballerinalang.siddhi.core.event.Event) SiddhiManager(org.ballerinalang.siddhi.core.SiddhiManager) StreamCallback(org.ballerinalang.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 90 with StreamCallback

use of org.ballerinalang.siddhi.core.stream.output.StreamCallback in project ballerina by ballerina-lang.

the class TestSiddhiLatency method init.

public static void init() {
    SiddhiManager siddhiManager = new SiddhiManager();
    siddhiManager.setExtension("str:concat", ConcatFunctionExtension.class);
    String eligibilityStream = " define stream EligibilityStream (rule string, messageID string, isEligible bool," + " isLocallyThrottled bool, throttle_key string , timeNow long); ";
    String commonQuery = "FROM EligibilityStream[isEligible==false]\n" + "\t\tSELECT rule, messageID, false AS isThrottled, timeNow\n" + "\t\tINSERT INTO ThrottleStream;\n" + "\n" + "\t\tFROM EligibilityStream[isEligible==true AND isLocallyThrottled==true]\n" + "\t\tSELECT rule, messageID, true AS isThrottled, timeNow\n" + "\t\tINSERT INTO ThrottleStream; \n" + "\n" + "\t\tFROM EligibilityStream[isEligible==true AND isLocallyThrottled==false]\n" + "\t\tSELECT rule, messageID, false AS isThrottled, timeNow\n" + "\t\tINSERT INTO ThrottleStream; ";
    SiddhiAppRuntime commonSiddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(eligibilityStream + commonQuery);
    commonSiddhiAppRuntime.addCallback("ThrottleStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            log.info("Common callback latency =" + (System.currentTimeMillis() - (Long) events[0].getData(3)));
            for (Event event : events) {
                resultMap.get(event.getData(1).toString()).addResult((String) event.getData(0), (Boolean) event.getData(2));
            }
        }
    });
    commonSiddhiAppRuntime.start();
    // get and register inputHandler
    eligibilityStreamInputHandler = commonSiddhiAppRuntime.getInputHandler("EligibilityStream");
    String requestStream = "define stream RequestStream (messageID string, app_key string, api_key string, " + "app_tier string, api_tier string, user_id string, properties object, timeNow long);";
    String eligibilityQuery = "FROM RequestStream\n" + "SELECT 'sub_gold' AS rule, messageID, ( api_tier == 'Gold') AS isEligible,false as " + "isLocallyThrottled,  str:concat('sub_gold_', api_key,app_key,user_id,'_key') AS throttle_key, " + "timeNow\n" + "INSERT INTO EligibilityStream;";
    SiddhiAppRuntime ruleRuntime = siddhiManager.createSiddhiAppRuntime(requestStream + eligibilityQuery);
    ruleRuntime.addCallback("EligibilityStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            try {
                log.info("Gold callback latency =" + (System.currentTimeMillis() - (Long) events[0].getData(5)));
                eligibilityStreamInputHandler.send(events);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                log.error("Error occurred when publishing to EligibilityStream of throttling policy " + "sub+gold", e);
            }
        }
    });
    ruleRuntime.start();
    // get and register input handler for RequestStream, so isThrottled() can use it.
    requestStreamInputHandlerMap.put("sub_gold", ruleRuntime.getInputHandler("RequestStream"));
    ruleCount.incrementAndGet();
}
Also used : Event(org.ballerinalang.siddhi.core.event.Event) StreamCallback(org.ballerinalang.siddhi.core.stream.output.StreamCallback)

Aggregations

StreamCallback (org.ballerinalang.siddhi.core.stream.output.StreamCallback)209 Test (org.testng.annotations.Test)204 Event (org.ballerinalang.siddhi.core.event.Event)203 SiddhiAppRuntime (org.ballerinalang.siddhi.core.SiddhiAppRuntime)201 SiddhiManager (org.ballerinalang.siddhi.core.SiddhiManager)201 InputHandler (org.ballerinalang.siddhi.core.stream.input.InputHandler)190 ComplexEvent (org.ballerinalang.siddhi.core.event.ComplexEvent)13 StreamDefinition (org.ballerinalang.siddhi.query.api.definition.StreamDefinition)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 PrintStream (java.io.PrintStream)6 SiddhiApp (org.ballerinalang.siddhi.query.api.SiddhiApp)6 Partition (org.ballerinalang.siddhi.query.api.execution.partition.Partition)6 Query (org.ballerinalang.siddhi.query.api.execution.query.Query)6 StreamEvent (org.ballerinalang.siddhi.core.event.stream.StreamEvent)5 QueryCallback (org.ballerinalang.siddhi.core.query.output.callback.QueryCallback)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 CannotRestoreSiddhiAppStateException (org.ballerinalang.siddhi.core.exception.CannotRestoreSiddhiAppStateException)2 BlockingWaitStrategy (com.lmax.disruptor.BlockingWaitStrategy)1 ProducerType (com.lmax.disruptor.dsl.ProducerType)1 Constructor (java.lang.reflect.Constructor)1