Search in sources :

Example 31 with Event

use of org.wso2.eventing.Event in project siddhi by wso2.

the class FrequentWindowProcessor method process.

@Override
protected void process(ComplexEventChunk<StreamEvent> streamEventChunk, Processor nextProcessor, StreamEventCloner streamEventCloner) {
    synchronized (this) {
        StreamEvent streamEvent = streamEventChunk.getFirst();
        streamEventChunk.clear();
        long currentTime = siddhiAppContext.getTimestampGenerator().currentTime();
        while (streamEvent != null) {
            StreamEvent next = streamEvent.getNext();
            streamEvent.setNext(null);
            StreamEvent clonedEvent = streamEventCloner.copyStreamEvent(streamEvent);
            clonedEvent.setType(StreamEvent.Type.EXPIRED);
            String key = generateKey(streamEvent);
            StreamEvent oldEvent = map.put(key, clonedEvent);
            if (oldEvent != null) {
                countMap.put(key, countMap.get(key) + 1);
                streamEventChunk.add(streamEvent);
            } else {
                // This is a new event
                if (map.size() > mostFrequentCount) {
                    List<String> keys = new ArrayList<String>(countMap.keySet());
                    for (int i = 0; i < mostFrequentCount; i++) {
                        int count = countMap.get(keys.get(i)) - 1;
                        if (count == 0) {
                            countMap.remove(keys.get(i));
                            StreamEvent expiredEvent = map.remove(keys.get(i));
                            expiredEvent.setTimestamp(currentTime);
                            streamEventChunk.add(expiredEvent);
                        } else {
                            countMap.put(keys.get(i), count);
                        }
                    }
                    // now we have tried to remove one for newly added item
                    if (map.size() > mostFrequentCount) {
                        // nothing happend by the attempt to remove one from the
                        // map so we are ignoring this event
                        map.remove(key);
                    // Here we do nothing just drop the message
                    } else {
                        // we got some space, event is already there in map object
                        // we just have to add it to the countMap
                        countMap.put(key, 1);
                        streamEventChunk.add(streamEvent);
                    }
                } else {
                    countMap.put(generateKey(streamEvent), 1);
                    streamEventChunk.add(streamEvent);
                }
            }
            streamEvent = next;
        }
    }
    nextProcessor.process(streamEventChunk);
}
Also used : StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent) ArrayList(java.util.ArrayList)

Example 32 with Event

use of org.wso2.eventing.Event in project siddhi by wso2.

the class LengthBatchWindowProcessor method process.

@Override
protected void process(ComplexEventChunk<StreamEvent> streamEventChunk, Processor nextProcessor, StreamEventCloner streamEventCloner) {
    List<ComplexEventChunk<StreamEvent>> streamEventChunks = new ArrayList<ComplexEventChunk<StreamEvent>>();
    synchronized (this) {
        ComplexEventChunk<StreamEvent> outputStreamEventChunk = new ComplexEventChunk<StreamEvent>(true);
        long currentTime = siddhiAppContext.getTimestampGenerator().currentTime();
        while (streamEventChunk.hasNext()) {
            StreamEvent streamEvent = streamEventChunk.next();
            StreamEvent clonedStreamEvent = streamEventCloner.copyStreamEvent(streamEvent);
            currentEventChunk.add(clonedStreamEvent);
            count++;
            if (count == length) {
                if (outputExpectsExpiredEvents) {
                    if (expiredEventChunk.getFirst() != null) {
                        while (expiredEventChunk.hasNext()) {
                            StreamEvent expiredEvent = expiredEventChunk.next();
                            expiredEvent.setTimestamp(currentTime);
                        }
                        outputStreamEventChunk.add(expiredEventChunk.getFirst());
                    }
                }
                if (expiredEventChunk != null) {
                    expiredEventChunk.clear();
                }
                if (currentEventChunk.getFirst() != null) {
                    // add reset event in front of current events
                    outputStreamEventChunk.add(resetEvent);
                    resetEvent = null;
                    if (expiredEventChunk != null) {
                        currentEventChunk.reset();
                        while (currentEventChunk.hasNext()) {
                            StreamEvent currentEvent = currentEventChunk.next();
                            StreamEvent toExpireEvent = streamEventCloner.copyStreamEvent(currentEvent);
                            toExpireEvent.setType(StreamEvent.Type.EXPIRED);
                            expiredEventChunk.add(toExpireEvent);
                        }
                    }
                    resetEvent = streamEventCloner.copyStreamEvent(currentEventChunk.getFirst());
                    resetEvent.setType(ComplexEvent.Type.RESET);
                    outputStreamEventChunk.add(currentEventChunk.getFirst());
                }
                currentEventChunk.clear();
                count = 0;
                if (outputStreamEventChunk.getFirst() != null) {
                    streamEventChunks.add(outputStreamEventChunk);
                }
            }
        }
    }
    for (ComplexEventChunk<StreamEvent> outputStreamEventChunk : streamEventChunks) {
        nextProcessor.process(outputStreamEventChunk);
    }
}
Also used : ComplexEventChunk(org.wso2.siddhi.core.event.ComplexEventChunk) StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent) ArrayList(java.util.ArrayList)

Example 33 with Event

use of org.wso2.eventing.Event in project siddhi by wso2.

the class TimeBatchWindowProcessor method process.

@Override
protected void process(ComplexEventChunk<StreamEvent> streamEventChunk, Processor nextProcessor, StreamEventCloner streamEventCloner) {
    synchronized (this) {
        if (nextEmitTime == -1) {
            long currentTime = siddhiAppContext.getTimestampGenerator().currentTime();
            if (isStartTimeEnabled) {
                nextEmitTime = getNextEmitTime(currentTime);
            } else {
                nextEmitTime = siddhiAppContext.getTimestampGenerator().currentTime() + timeInMilliSeconds;
            }
            scheduler.notifyAt(nextEmitTime);
        }
        long currentTime = siddhiAppContext.getTimestampGenerator().currentTime();
        boolean sendEvents;
        if (currentTime >= nextEmitTime) {
            nextEmitTime += timeInMilliSeconds;
            scheduler.notifyAt(nextEmitTime);
            sendEvents = true;
        } else {
            sendEvents = false;
        }
        while (streamEventChunk.hasNext()) {
            StreamEvent streamEvent = streamEventChunk.next();
            if (streamEvent.getType() != ComplexEvent.Type.CURRENT) {
                continue;
            }
            StreamEvent clonedStreamEvent = streamEventCloner.copyStreamEvent(streamEvent);
            currentEventChunk.add(clonedStreamEvent);
        }
        streamEventChunk.clear();
        if (sendEvents) {
            if (outputExpectsExpiredEvents) {
                if (expiredEventChunk.getFirst() != null) {
                    while (expiredEventChunk.hasNext()) {
                        StreamEvent expiredEvent = expiredEventChunk.next();
                        expiredEvent.setTimestamp(currentTime);
                    }
                    streamEventChunk.add(expiredEventChunk.getFirst());
                }
            }
            if (expiredEventChunk != null) {
                expiredEventChunk.clear();
            }
            if (currentEventChunk.getFirst() != null) {
                // add reset event in front of current events
                streamEventChunk.add(resetEvent);
                resetEvent = null;
                if (expiredEventChunk != null) {
                    currentEventChunk.reset();
                    while (currentEventChunk.hasNext()) {
                        StreamEvent currentEvent = currentEventChunk.next();
                        StreamEvent toExpireEvent = streamEventCloner.copyStreamEvent(currentEvent);
                        toExpireEvent.setType(StreamEvent.Type.EXPIRED);
                        expiredEventChunk.add(toExpireEvent);
                    }
                }
                resetEvent = streamEventCloner.copyStreamEvent(currentEventChunk.getFirst());
                resetEvent.setType(ComplexEvent.Type.RESET);
                streamEventChunk.add(currentEventChunk.getFirst());
            }
            currentEventChunk.clear();
        }
    }
    if (streamEventChunk.getFirst() != null) {
        streamEventChunk.setBatch(true);
        nextProcessor.process(streamEventChunk);
        streamEventChunk.setBatch(false);
    }
}
Also used : StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent)

Example 34 with Event

use of org.wso2.eventing.Event in project siddhi by wso2.

the class StreamJunction method sendData.

private void sendData(long timeStamp, Object[] data) {
    // Set timestamp to system if Siddhi is in playback mode
    if (siddhiAppContext.isPlayback()) {
        ((EventTimeBasedMillisTimestampGenerator) this.siddhiAppContext.getTimestampGenerator()).setCurrentTimestamp(timeStamp);
    }
    if (throughputTracker != null && siddhiAppContext.isStatsEnabled()) {
        throughputTracker.eventIn();
    }
    if (disruptor != null) {
        long sequenceNo = ringBuffer.next();
        try {
            Event existingEvent = ringBuffer.get(sequenceNo);
            existingEvent.setTimestamp(timeStamp);
            existingEvent.setIsExpired(false);
            System.arraycopy(data, 0, existingEvent.getData(), 0, data.length);
        } finally {
            ringBuffer.publish(sequenceNo);
        }
    } else {
        for (Receiver receiver : receivers) {
            receiver.receive(timeStamp, data);
        }
    }
}
Also used : EventTimeBasedMillisTimestampGenerator(org.wso2.siddhi.core.util.timestamp.EventTimeBasedMillisTimestampGenerator) Event(org.wso2.siddhi.core.event.Event) ComplexEvent(org.wso2.siddhi.core.event.ComplexEvent)

Example 35 with Event

use of org.wso2.eventing.Event in project siddhi by wso2.

the class StreamJunction method sendEvent.

public void sendEvent(ComplexEvent complexEvent) {
    if (isTraceEnabled) {
        log.trace("Event is received by streamJunction " + this);
    }
    ComplexEvent complexEventList = complexEvent;
    if (disruptor != null) {
        while (complexEventList != null) {
            if (throughputTracker != null && siddhiAppContext.isStatsEnabled()) {
                throughputTracker.eventIn();
            }
            long sequenceNo = ringBuffer.next();
            try {
                Event existingEvent = ringBuffer.get(sequenceNo);
                existingEvent.copyFrom(complexEventList);
            } finally {
                ringBuffer.publish(sequenceNo);
            }
            complexEventList = complexEventList.getNext();
        }
    } else {
        if (throughputTracker != null && siddhiAppContext.isStatsEnabled()) {
            int messageCount = 0;
            while (complexEventList != null) {
                messageCount++;
                complexEventList = complexEventList.getNext();
            }
            throughputTracker.eventsIn(messageCount);
        }
        for (Receiver receiver : receivers) {
            receiver.receive(complexEvent);
        }
    }
}
Also used : ComplexEvent(org.wso2.siddhi.core.event.ComplexEvent) Event(org.wso2.siddhi.core.event.Event) ComplexEvent(org.wso2.siddhi.core.event.ComplexEvent)

Aggregations

Test (org.testng.annotations.Test)1150 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)1146 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)1145 InputHandler (org.wso2.siddhi.core.stream.input.InputHandler)1119 Event (org.wso2.siddhi.core.event.Event)855 QueryCallback (org.wso2.siddhi.core.query.output.callback.QueryCallback)587 TestUtil (org.wso2.siddhi.core.TestUtil)300 StreamCallback (org.wso2.siddhi.core.stream.output.StreamCallback)218 StreamDefinition (org.wso2.siddhi.query.api.definition.StreamDefinition)86 SiddhiApp (org.wso2.siddhi.query.api.SiddhiApp)79 Query (org.wso2.siddhi.query.api.execution.query.Query)79 ArrayList (java.util.ArrayList)68 StreamEvent (org.wso2.siddhi.core.event.stream.StreamEvent)63 ComplexEvent (org.wso2.siddhi.core.event.ComplexEvent)58 ComplexEventChunk (org.wso2.siddhi.core.event.ComplexEventChunk)38 MetaStreamEvent (org.wso2.siddhi.core.event.stream.MetaStreamEvent)35 GroupedComplexEvent (org.wso2.siddhi.core.event.GroupedComplexEvent)16 InMemoryBroker (org.wso2.siddhi.core.util.transport.InMemoryBroker)16 HashMap (java.util.HashMap)14 CannotRestoreSiddhiAppStateException (org.wso2.siddhi.core.exception.CannotRestoreSiddhiAppStateException)13