Search in sources :

Example 1 with Trigger

use of org.wso2.siddhi.core.trigger.Trigger in project siddhi by wso2.

the class ExternalTimeBatchWindowProcessor method process.

/**
 * Here an assumption is taken:
 * Parameter: timestamp: The time which the window determines as current time and will act upon,
 * the value of this parameter should be monotonically increasing.
 * from https://docs.wso2.com/display/CEP400/Inbuilt+Windows#InbuiltWindows-externalTime
 */
@Override
protected void process(ComplexEventChunk<StreamEvent> streamEventChunk, Processor nextProcessor, StreamEventCloner streamEventCloner) {
    // event incoming trigger process. No events means no action
    if (streamEventChunk.getFirst() == null) {
        return;
    }
    List<ComplexEventChunk<StreamEvent>> complexEventChunks = new ArrayList<ComplexEventChunk<StreamEvent>>();
    synchronized (this) {
        initTiming(streamEventChunk.getFirst());
        StreamEvent nextStreamEvent = streamEventChunk.getFirst();
        while (nextStreamEvent != null) {
            StreamEvent currStreamEvent = nextStreamEvent;
            nextStreamEvent = nextStreamEvent.getNext();
            if (currStreamEvent.getType() == ComplexEvent.Type.TIMER) {
                if (lastScheduledTime <= currStreamEvent.getTimestamp()) {
                    // implies that there have not been any more events after this schedule has been done.
                    if (!flushed) {
                        flushToOutputChunk(streamEventCloner, complexEventChunks, lastCurrentEventTime, true);
                        flushed = true;
                    } else {
                        if (currentEventChunk.getFirst() != null) {
                            appendToOutputChunk(streamEventCloner, complexEventChunks, lastCurrentEventTime, true);
                        }
                    }
                    // rescheduling to emit the current batch after expiring it if no further events arrive.
                    lastScheduledTime = siddhiAppContext.getTimestampGenerator().currentTime() + schedulerTimeout;
                    scheduler.notifyAt(lastScheduledTime);
                }
                continue;
            } else if (currStreamEvent.getType() != ComplexEvent.Type.CURRENT) {
                continue;
            }
            long currentEventTime = (Long) timestampExpressionExecutor.execute(currStreamEvent);
            if (lastCurrentEventTime < currentEventTime) {
                lastCurrentEventTime = currentEventTime;
            }
            if (currentEventTime < endTime) {
                cloneAppend(streamEventCloner, currStreamEvent);
            } else {
                if (flushed) {
                    appendToOutputChunk(streamEventCloner, complexEventChunks, lastCurrentEventTime, false);
                    flushed = false;
                } else {
                    flushToOutputChunk(streamEventCloner, complexEventChunks, lastCurrentEventTime, false);
                }
                // update timestamp, call next processor
                endTime = findEndTime(lastCurrentEventTime, startTime, timeToKeep);
                cloneAppend(streamEventCloner, currStreamEvent);
                // triggering the last batch expiration.
                if (schedulerTimeout > 0) {
                    lastScheduledTime = siddhiAppContext.getTimestampGenerator().currentTime() + schedulerTimeout;
                    scheduler.notifyAt(lastScheduledTime);
                }
            }
        }
    }
    for (ComplexEventChunk<StreamEvent> complexEventChunk : complexEventChunks) {
        nextProcessor.process(complexEventChunk);
    }
}
Also used : ComplexEventChunk(org.wso2.siddhi.core.event.ComplexEventChunk) StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent) ArrayList(java.util.ArrayList)

Example 2 with Trigger

use of org.wso2.siddhi.core.trigger.Trigger in project siddhi by wso2.

the class JoinProcessor method process.

/**
 * Process the handed StreamEvent.
 *
 * @param complexEventChunk event chunk to be processed
 */
@Override
public void process(ComplexEventChunk complexEventChunk) {
    if (trigger) {
        ComplexEventChunk<StateEvent> returnEventChunk = new ComplexEventChunk<StateEvent>(true);
        StateEvent joinStateEvent = new StateEvent(2, 0);
        StreamEvent nextEvent = (StreamEvent) complexEventChunk.getFirst();
        complexEventChunk.clear();
        while (nextEvent != null) {
            StreamEvent streamEvent = nextEvent;
            nextEvent = streamEvent.getNext();
            streamEvent.setNext(null);
            joinLockWrapper.lock();
            try {
                ComplexEvent.Type eventType = streamEvent.getType();
                if (eventType == ComplexEvent.Type.TIMER) {
                    continue;
                } else if (eventType == ComplexEvent.Type.RESET) {
                    if (!leftJoinProcessor) {
                        returnEventChunk.add(joinEventBuilder(null, streamEvent, eventType));
                    } else {
                        returnEventChunk.add(joinEventBuilder(streamEvent, null, eventType));
                    }
                } else {
                    joinStateEvent.setEvent(matchingStreamIndex, streamEvent);
                    StreamEvent foundStreamEvent = findableProcessor.find(joinStateEvent, compiledCondition);
                    joinStateEvent.setEvent(matchingStreamIndex, null);
                    if (foundStreamEvent == null) {
                        if (outerJoinProcessor && !leftJoinProcessor) {
                            returnEventChunk.add(joinEventBuilder(null, streamEvent, eventType));
                        } else if (outerJoinProcessor && leftJoinProcessor) {
                            returnEventChunk.add(joinEventBuilder(streamEvent, null, eventType));
                        }
                    } else {
                        while (foundStreamEvent != null) {
                            StreamEvent nextFoundStreamEvent = foundStreamEvent.getNext();
                            foundStreamEvent.setNext(null);
                            if (!leftJoinProcessor) {
                                returnEventChunk.add(joinEventBuilder(foundStreamEvent, streamEvent, eventType));
                            } else {
                                returnEventChunk.add(joinEventBuilder(streamEvent, foundStreamEvent, eventType));
                            }
                            foundStreamEvent = nextFoundStreamEvent;
                        }
                    }
                }
            } finally {
                joinLockWrapper.unlock();
            }
            if (returnEventChunk.getFirst() != null) {
                selector.process(returnEventChunk);
                returnEventChunk.clear();
            }
        }
    } else {
        if (preJoinProcessor) {
            joinLockWrapper.lock();
            try {
                nextProcessor.process(complexEventChunk);
            } finally {
                joinLockWrapper.unlock();
            }
        }
    }
}
Also used : ComplexEvent(org.wso2.siddhi.core.event.ComplexEvent) ComplexEventChunk(org.wso2.siddhi.core.event.ComplexEventChunk) StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent) StateEvent(org.wso2.siddhi.core.event.state.StateEvent)

Example 3 with Trigger

use of org.wso2.siddhi.core.trigger.Trigger in project siddhi by wso2.

the class DefinitionParserHelper method addEventTrigger.

public static void addEventTrigger(TriggerDefinition triggerDefinition, ConcurrentMap<String, Trigger> eventTriggerMap, ConcurrentMap<String, StreamJunction> streamJunctionMap, SiddhiAppContext siddhiAppContext) {
    if (!eventTriggerMap.containsKey(triggerDefinition.getId())) {
        Trigger trigger;
        if (triggerDefinition.getAtEvery() != null) {
            trigger = new PeriodicTrigger();
        } else if (triggerDefinition.getAt().trim().equalsIgnoreCase(SiddhiConstants.TRIGGER_START)) {
            trigger = new StartTrigger();
        } else {
            trigger = new CronTrigger();
        }
        StreamJunction streamJunction = streamJunctionMap.get(triggerDefinition.getId());
        trigger.init(triggerDefinition, siddhiAppContext, streamJunction);
        siddhiAppContext.addEternalReferencedHolder(trigger);
        eventTriggerMap.putIfAbsent(trigger.getId(), trigger);
    }
}
Also used : CronTrigger(org.wso2.siddhi.core.trigger.CronTrigger) StartTrigger(org.wso2.siddhi.core.trigger.StartTrigger) Trigger(org.wso2.siddhi.core.trigger.Trigger) CronTrigger(org.wso2.siddhi.core.trigger.CronTrigger) PeriodicTrigger(org.wso2.siddhi.core.trigger.PeriodicTrigger) StreamJunction(org.wso2.siddhi.core.stream.StreamJunction) StartTrigger(org.wso2.siddhi.core.trigger.StartTrigger) PeriodicTrigger(org.wso2.siddhi.core.trigger.PeriodicTrigger)

Example 4 with Trigger

use of org.wso2.siddhi.core.trigger.Trigger in project siddhi by wso2.

the class ExternalTimeBatchWindowTestCase method testExternalTimeBatchWindow15.

@Test
public void testExternalTimeBatchWindow15() throws Exception {
    log.info("ExternalTimeBatchWindow test15");
    SiddhiManager siddhiManager = new SiddhiManager();
    String stream = "define stream jmxMetric(cpu int, memory int, bytesIn long, bytesOut long, timestamp long); " + "define window jmxMetricWindow(cpu int, memory int, bytesIn long, bytesOut long, timestamp long) " + "externalTimeBatch(timestamp, 10 sec) output current events; ";
    String query = "" + "@info(name = 'query0') " + "from jmxMetric " + "insert into jmxMetricWindow; " + "" + "@info(name = 'downSample') " + "from jmxMetricWindow " + "select avg(cpu) as avgCpu, max(cpu) as maxCpu, min(cpu) as minCpu, " + " '|' as s, " + " avg(memory) as avgMem, max(memory) as maxMem, min(memory) as minMem, " + " '|' as s1, " + " avg(bytesIn) as avgBytesIn, max(bytesIn) as maxBytesIn, min(bytesIn) as minBytesIn, " + " '|' as s2, " + " avg(bytesOut) as avgBytesOut, max(bytesOut) as maxBytesOut, min(bytesOut) as minBytesOut, " + " '|' as s3, " + " timestamp as timeWindowEnds, " + " '|' as s4, " + " count(1) as metric_count " + " INSERT INTO tmp;";
    SiddhiManager sm = new SiddhiManager();
    SiddhiAppRuntime plan = sm.createSiddhiAppRuntime(stream + query);
    InputHandler input = plan.getInputHandler("jmxMetric");
    // stream call back doesn't follow the counter
    final AtomicInteger counter = new AtomicInteger();
    {
        // stream callback
        plan.addCallback("jmxMetric", new StreamCallback() {

            @Override
            public void receive(Event[] arg0) {
                counter.addAndGet(arg0.length);
            }
        });
    }
    final AtomicInteger queryWideCounter = new AtomicInteger();
    {
        plan.addCallback("downSample", new QueryCallback() {

            @Override
            public void receive(long timeStamp, Event[] inevents, Event[] removevents) {
                int currentCount = queryWideCounter.addAndGet(inevents.length);
                log.info(MessageFormat.format("Round {0} ====", currentCount));
                log.info(" events count " + inevents.length);
                EventPrinter.print(inevents);
            }
        });
    }
    plan.start();
    int round = 4;
    int eventsPerRound = 0;
    long externalTs = System.currentTimeMillis();
    for (int i = 0; i < round; i++) {
        eventsPerRound = sendEvent(input, i, externalTs);
        Thread.sleep(3000);
    }
    // trigger next round
    sendEvent(input, round, externalTs);
    plan.shutdown();
    Thread.sleep(1000);
    assertEquals(round * eventsPerRound + eventsPerRound, counter.get());
    assertEquals(round, queryWideCounter.get());
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) StreamCallback(org.wso2.siddhi.core.stream.output.StreamCallback) QueryCallback(org.wso2.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 5 with Trigger

use of org.wso2.siddhi.core.trigger.Trigger in project siddhi by wso2.

the class ExternalTimeBatchWindowTestCase method testExternalTimeBatchWindow14.

@Test
public void testExternalTimeBatchWindow14() throws Exception {
    log.info("ExternalTimeBatchWindow test14");
    SiddhiManager siddhiManager = new SiddhiManager();
    String query = "define stream jmxMetric(cpu int, timestamp long); " + "define window jmxMetricWindow(cpu int, timestamp long) externalTimeBatch(timestamp, 10 sec);" + "@info(name = 'query0') " + "from jmxMetric " + "insert into jmxMetricWindow; " + "" + "@info(name='query1') " + "from jmxMetricWindow " + "select avg(cpu) as avgCpu, count(1) as count insert into tmp;";
    SiddhiAppRuntime runtime = siddhiManager.createSiddhiAppRuntime(query);
    final AtomicInteger recCount = new AtomicInteger(0);
    runtime.addCallback("query1", new QueryCallback() {

        @Override
        public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            assertEquals(1, inEvents.length);
            recCount.incrementAndGet();
            double avgCpu = (Double) inEvents[0].getData()[0];
            if (recCount.get() == 1) {
                assertEquals(15, avgCpu, 0);
            } else if (recCount.get() == 2) {
                assertEquals(85, avgCpu, 0);
            }
            long count = (Long) inEvents[0].getData()[1];
            assertEquals(3, count);
        }
    });
    InputHandler input = runtime.getInputHandler("jmxMetric");
    runtime.start();
    // external events' time stamp less than the window, should not have event recieved in call back.
    long now = 0;
    int length = 3;
    for (int i = 0; i < length; i++) {
        input.send(new Object[] { 15, now + i * 10 });
    }
    // if the trigger event mix with the last window, we should see the avgValue is not expected
    for (int i = 0; i < length; i++) {
        // the first entity of the second round
        input.send(new Object[] { 85, now + 10000 + i * 10 });
    }
    // to trigger second round
    input.send(new Object[] { 10000, now + 10 * 10000 });
    Thread.sleep(1000);
    assertEquals(2, recCount.get());
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) QueryCallback(org.wso2.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)9 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)9 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)9 Event (org.wso2.siddhi.core.event.Event)5 StreamCallback (org.wso2.siddhi.core.stream.output.StreamCallback)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 QueryCallback (org.wso2.siddhi.core.query.output.callback.QueryCallback)4 InputHandler (org.wso2.siddhi.core.stream.input.InputHandler)4 ComplexEventChunk (org.wso2.siddhi.core.event.ComplexEventChunk)2 StreamEvent (org.wso2.siddhi.core.event.stream.StreamEvent)2 Gson (com.google.gson.Gson)1 ArrayList (java.util.ArrayList)1 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)1 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)1 Function (org.wso2.carbon.apimgt.core.models.Function)1 HttpResponse (org.wso2.carbon.apimgt.core.models.HttpResponse)1 ComplexEvent (org.wso2.siddhi.core.event.ComplexEvent)1 StateEvent (org.wso2.siddhi.core.event.state.StateEvent)1 StreamJunction (org.wso2.siddhi.core.stream.StreamJunction)1 CronTrigger (org.wso2.siddhi.core.trigger.CronTrigger)1