Search in sources :

Example 6 with StreamEvent

use of org.wso2.siddhi.core.event.stream.StreamEvent 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 7 with StreamEvent

use of org.wso2.siddhi.core.event.stream.StreamEvent in project siddhi by wso2.

the class ExternalTimeBatchWindowProcessor method appendToOutputChunk.

private void appendToOutputChunk(StreamEventCloner streamEventCloner, List<ComplexEventChunk<StreamEvent>> complexEventChunks, long currentTime, boolean preserveCurrentEvents) {
    ComplexEventChunk<StreamEvent> newEventChunk = new ComplexEventChunk<StreamEvent>(true);
    ComplexEventChunk<StreamEvent> sentEventChunk = new ComplexEventChunk<StreamEvent>(true);
    if (currentEventChunk.getFirst() != null) {
        if (expiredEventChunk.getFirst() != null) {
            // mark the timestamp for the expiredType event
            expiredEventChunk.reset();
            while (expiredEventChunk.hasNext()) {
                StreamEvent expiredEvent = expiredEventChunk.next();
                if (outputExpectsExpiredEvents) {
                    // add expired event to newEventChunk.
                    StreamEvent toExpireEvent = streamEventCloner.copyStreamEvent(expiredEvent);
                    toExpireEvent.setTimestamp(currentTime);
                    newEventChunk.add(toExpireEvent);
                }
                StreamEvent toSendEvent = streamEventCloner.copyStreamEvent(expiredEvent);
                toSendEvent.setType(ComplexEvent.Type.CURRENT);
                sentEventChunk.add(toSendEvent);
            }
        }
        // add reset event in front of current events
        StreamEvent toResetEvent = streamEventCloner.copyStreamEvent(resetEvent);
        toResetEvent.setTimestamp(currentTime);
        newEventChunk.add(toResetEvent);
        // add old events
        newEventChunk.add(sentEventChunk.getFirst());
        // move to expired events
        if (preserveCurrentEvents || storeExpiredEvents) {
            currentEventChunk.reset();
            while (currentEventChunk.hasNext()) {
                StreamEvent currentEvent = currentEventChunk.next();
                StreamEvent toExpireEvent = streamEventCloner.copyStreamEvent(currentEvent);
                toExpireEvent.setType(StreamEvent.Type.EXPIRED);
                expiredEventChunk.add(toExpireEvent);
            }
        }
        // add current event chunk to next processor
        newEventChunk.add(currentEventChunk.getFirst());
    }
    currentEventChunk.clear();
    if (newEventChunk.getFirst() != null) {
        complexEventChunks.add(newEventChunk);
    }
}
Also used : ComplexEventChunk(org.wso2.siddhi.core.event.ComplexEventChunk) StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent)

Example 8 with StreamEvent

use of org.wso2.siddhi.core.event.stream.StreamEvent in project siddhi by wso2.

the class FindStoreQueryRuntime method executeSelector.

private Event[] executeSelector(StreamEvent streamEvents, MetaStreamEvent.EventType eventType) {
    ComplexEventChunk<StateEvent> complexEventChunk = new ComplexEventChunk<>(true);
    while (streamEvents != null) {
        StreamEvent streamEvent = streamEvents;
        streamEvents = streamEvents.getNext();
        streamEvent.setNext(null);
        StateEvent stateEvent = stateEventPool.borrowEvent();
        if (eventType == MetaStreamEvent.EventType.AGGREGATE) {
            stateEvent.addEvent(1, streamEvent);
        } else {
            stateEvent.addEvent(0, streamEvent);
        }
        complexEventChunk.add(stateEvent);
    }
    ComplexEventChunk outputComplexEventChunk = selector.execute(complexEventChunk);
    if (outputComplexEventChunk != null) {
        List<Event> events = new ArrayList<>();
        outputComplexEventChunk.reset();
        while (outputComplexEventChunk.hasNext()) {
            ComplexEvent complexEvent = outputComplexEventChunk.next();
            events.add(new Event(complexEvent.getTimestamp(), complexEvent.getOutputData()));
        }
        return events.toArray(new Event[0]);
    } else {
        return null;
    }
}
Also used : ComplexEvent(org.wso2.siddhi.core.event.ComplexEvent) ComplexEventChunk(org.wso2.siddhi.core.event.ComplexEventChunk) MetaStreamEvent(org.wso2.siddhi.core.event.stream.MetaStreamEvent) StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent) ArrayList(java.util.ArrayList) StateEvent(org.wso2.siddhi.core.event.state.StateEvent) MetaStreamEvent(org.wso2.siddhi.core.event.stream.MetaStreamEvent) StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent) ComplexEvent(org.wso2.siddhi.core.event.ComplexEvent) Event(org.wso2.siddhi.core.event.Event) StateEvent(org.wso2.siddhi.core.event.state.StateEvent)

Example 9 with StreamEvent

use of org.wso2.siddhi.core.event.stream.StreamEvent in project siddhi by wso2.

the class SelectStoreQueryRuntime method execute.

public Event[] execute() {
    try {
        StateEvent stateEvent = new StateEvent(1, 0);
        StreamEvent streamEvents = queryableProcessor.query(stateEvent, compiledCondition, compiledSelection);
        if (streamEvents == null) {
            return null;
        } else {
            List<Event> events = new ArrayList<Event>();
            while (streamEvents != null) {
                events.add(new Event(streamEvents.getTimestamp(), streamEvents.getOutputData()));
                streamEvents = streamEvents.getNext();
            }
            return events.toArray(new Event[0]);
        }
    } catch (Throwable t) {
        throw new StoreQueryRuntimeException("Error executing '" + queryName + "', " + t.getMessage(), t);
    }
}
Also used : StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent) StoreQueryRuntimeException(org.wso2.siddhi.core.exception.StoreQueryRuntimeException) ArrayList(java.util.ArrayList) StateEvent(org.wso2.siddhi.core.event.state.StateEvent) StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent) Event(org.wso2.siddhi.core.event.Event) StateEvent(org.wso2.siddhi.core.event.state.StateEvent)

Example 10 with StreamEvent

use of org.wso2.siddhi.core.event.stream.StreamEvent in project siddhi by wso2.

the class MultiProcessStreamReceiver method receive.

@Override
public void receive(ComplexEvent complexEvent) {
    ComplexEvent aComplexEvent = complexEvent;
    while (aComplexEvent != null) {
        synchronized (this) {
            stabilizeStates();
            for (int anEventSequence : eventSequence) {
                StreamEventConverter aStreamEventConverter = streamEventConverters[anEventSequence];
                StreamEventPool aStreamEventPool = streamEventPools[anEventSequence];
                StreamEvent borrowedEvent = aStreamEventPool.borrowEvent();
                aStreamEventConverter.convertComplexEvent(aComplexEvent, borrowedEvent);
                process(anEventSequence, borrowedEvent);
            }
        }
        aComplexEvent = aComplexEvent.getNext();
    }
}
Also used : ComplexEvent(org.wso2.siddhi.core.event.ComplexEvent) StreamEventConverter(org.wso2.siddhi.core.event.stream.converter.StreamEventConverter) MetaStreamEvent(org.wso2.siddhi.core.event.stream.MetaStreamEvent) StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent) StreamEventPool(org.wso2.siddhi.core.event.stream.StreamEventPool)

Aggregations

StreamEvent (org.wso2.siddhi.core.event.stream.StreamEvent)121 MetaStreamEvent (org.wso2.siddhi.core.event.stream.MetaStreamEvent)42 ComplexEventChunk (org.wso2.siddhi.core.event.ComplexEventChunk)41 StateEvent (org.wso2.siddhi.core.event.state.StateEvent)23 ComplexEvent (org.wso2.siddhi.core.event.ComplexEvent)20 Test (org.testng.annotations.Test)19 Event (org.wso2.siddhi.core.event.Event)16 StreamEventPool (org.wso2.siddhi.core.event.stream.StreamEventPool)16 ExpressionExecutor (org.wso2.siddhi.core.executor.ExpressionExecutor)14 ArrayList (java.util.ArrayList)13 Map (java.util.Map)10 ConstantExpressionExecutor (org.wso2.siddhi.core.executor.ConstantExpressionExecutor)9 HashSet (java.util.HashSet)8 StreamEventConverter (org.wso2.siddhi.core.event.stream.converter.StreamEventConverter)8 Attribute (org.wso2.siddhi.query.api.definition.Attribute)8 StreamDefinition (org.wso2.siddhi.query.api.definition.StreamDefinition)8 VariableExpressionExecutor (org.wso2.siddhi.core.executor.VariableExpressionExecutor)7 ConversionStreamEventChunk (org.wso2.siddhi.core.event.stream.converter.ConversionStreamEventChunk)6 StreamCallback (org.wso2.siddhi.core.stream.output.StreamCallback)6 HashMap (java.util.HashMap)5