Search in sources :

Example 11 with StreamEvent

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

the class DelayWindowProcessor method process.

@Override
protected void process(ComplexEventChunk<StreamEvent> streamEventChunk, Processor nextProcessor, StreamEventCloner streamEventCloner, WindowState windowState) {
    DelayedWindowState state = ((DelayedWindowState) windowState);
    synchronized (state) {
        while (streamEventChunk.hasNext()) {
            StreamEvent streamEvent = streamEventChunk.next();
            long currentTime = siddhiQueryContext.getSiddhiAppContext().getTimestampGenerator().currentTime();
            state.delayedEventQueue.reset();
            while (state.delayedEventQueue.hasNext()) {
                StreamEvent delayedEvent = state.delayedEventQueue.next();
                long timeDiff = delayedEvent.getTimestamp() - currentTime + delayInMilliSeconds;
                if (timeDiff <= 0) {
                    state.delayedEventQueue.remove();
                    // insert delayed event before the current event to stream chunk
                    delayedEvent.setTimestamp(currentTime);
                    streamEventChunk.insertBeforeCurrent(delayedEvent);
                } else {
                    break;
                }
            }
            if (streamEvent.getType() == StreamEvent.Type.CURRENT) {
                state.delayedEventQueue.add(streamEvent);
                if (state.lastTimestamp < streamEvent.getTimestamp()) {
                    getScheduler().notifyAt(streamEvent.getTimestamp() + delayInMilliSeconds);
                    state.lastTimestamp = streamEvent.getTimestamp();
                }
            }
            // current events are not processed, so remove the current event from the stream chunk
            streamEventChunk.remove();
        }
        state.delayedEventQueue.reset();
    }
    // only pass to next processor if there are any events in the stream chunk
    if (streamEventChunk.getFirst() != null) {
        nextProcessor.process(streamEventChunk);
    }
}
Also used : StreamEvent(io.siddhi.core.event.stream.StreamEvent)

Example 12 with StreamEvent

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

the class ExpressionBatchWindowProcessor method processStreamEventAsStream.

private void processStreamEventAsStream(WindowState state, long currentTime, StreamEvent streamEvent, StreamEventCloner streamEventCloner, List<ComplexEventChunk<StreamEvent>> streamEventChunks, boolean output) {
    StateEvent stateEventCurrent = new StateEvent(3, 0);
    stateEventCurrent.setEvent(0, streamEvent);
    if (state.expiredEventQueue.getFirst() != null) {
        stateEventCurrent.setEvent(1, state.expiredEventQueue.getFirst());
    } else {
        stateEventCurrent.setEvent(1, streamEvent);
    }
    stateEventCurrent.setEvent(2, streamEvent);
    ComplexEventChunk<StreamEvent> outputStreamEventChunk = new ComplexEventChunk<>();
    if (output && state.currentEventQueue.getFirst() != null) {
        outputStreamEventChunk.add(state.currentEventQueue.getFirst());
        state.currentEventQueue.clear();
    }
    if (!(Boolean) expressionExecutor.execute(stateEventCurrent)) {
        stateEventCurrent.setType(ComplexEvent.Type.RESET);
        expressionExecutor.execute(stateEventCurrent);
        stateEventCurrent.setType(ComplexEvent.Type.CURRENT);
        expressionExecutor.execute(stateEventCurrent);
        state.expiredEventQueue.reset();
        if (state.expiredEventQueue.getFirst() != null) {
            while (state.expiredEventQueue.hasNext()) {
                StreamEvent expiredEvent = state.expiredEventQueue.next();
                expiredEvent.setTimestamp(currentTime);
            }
            StreamEvent expiredEvent = streamEventCloner.copyStreamEvent(streamEvent);
            expiredEvent.setType(ComplexEvent.Type.EXPIRED);
            if (includeTriggeringEventExecutor != null && (Boolean) includeTriggeringEventExecutor.execute(streamEvent) || includeTriggeringEvent) {
                outputStreamEventChunk.add(streamEvent);
                if (output) {
                    outputStreamEventChunk.add(state.expiredEventQueue.getFirst());
                    expiredEvent.setTimestamp(currentTime);
                    outputStreamEventChunk.add(expiredEvent);
                } else {
                    state.currentEventQueue.add(state.expiredEventQueue.getFirst());
                    state.currentEventQueue.add(expiredEvent);
                }
                state.expiredEventQueue.clear();
            } else {
                if (output) {
                    outputStreamEventChunk.add(state.expiredEventQueue.getFirst());
                } else {
                    state.currentEventQueue.add(state.expiredEventQueue.getFirst());
                }
                outputStreamEventChunk.add(streamEvent);
                state.expiredEventQueue.clear();
                state.expiredEventQueue.add(expiredEvent);
            }
        } else if (output) {
            StreamEvent expiredEvent = streamEventCloner.copyStreamEvent(streamEvent);
            expiredEvent.setType(ComplexEvent.Type.EXPIRED);
            expiredEvent.setTimestamp(currentTime);
            outputStreamEventChunk.add(expiredEvent);
            streamEvent.setTimestamp(currentTime);
            outputStreamEventChunk.add(streamEvent);
        } else {
            state.currentEventQueue.add(streamEvent);
        }
        if (output) {
            streamEventChunks.add(outputStreamEventChunk);
        }
    } else {
        StreamEvent expiredEvent = streamEventCloner.copyStreamEvent(streamEvent);
        expiredEvent.setType(ComplexEvent.Type.EXPIRED);
        state.expiredEventQueue.add(expiredEvent);
        if (output) {
            outputStreamEventChunk.add(streamEvent);
            streamEventChunks.add(outputStreamEventChunk);
        }
    }
}
Also used : ComplexEventChunk(io.siddhi.core.event.ComplexEventChunk) StreamEvent(io.siddhi.core.event.stream.StreamEvent) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent) MetaStateEvent(io.siddhi.core.event.state.MetaStateEvent) StateEvent(io.siddhi.core.event.state.StateEvent)

Example 13 with StreamEvent

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

the class ExpressionBatchWindowProcessor method processStreamEvent.

private void processStreamEvent(WindowState state, long currentTime, StreamEvent streamEvent, StreamEventCloner streamEventCloner, List<ComplexEventChunk<StreamEvent>> streamEventChunks) {
    StateEvent stateEventCurrent = new StateEvent(3, 0);
    stateEventCurrent.setEvent(0, streamEvent);
    if (state.currentEventQueue.getFirst() != null) {
        stateEventCurrent.setEvent(1, state.currentEventQueue.getFirst());
    } else {
        stateEventCurrent.setEvent(1, streamEvent);
    }
    stateEventCurrent.setEvent(2, streamEvent);
    if (!(Boolean) expressionExecutor.execute(stateEventCurrent)) {
        stateEventCurrent.setType(ComplexEvent.Type.RESET);
        expressionExecutor.execute(stateEventCurrent);
        stateEventCurrent.setType(ComplexEvent.Type.CURRENT);
        expressionExecutor.execute(stateEventCurrent);
        ComplexEventChunk<StreamEvent> outputStreamEventChunk = new ComplexEventChunk<>();
        state.expiredEventQueue.reset();
        if (state.expiredEventQueue.getFirst() != null) {
            while (state.expiredEventQueue.hasNext()) {
                StreamEvent expiredEvent = state.expiredEventQueue.next();
                expiredEvent.setTimestamp(currentTime);
            }
            outputStreamEventChunk.add(state.expiredEventQueue.getFirst());
            state.expiredEventQueue.clear();
        }
        if (state.currentEventQueue.getFirst() != null) {
            while (state.currentEventQueue.hasNext()) {
                StreamEvent currentEvent = state.currentEventQueue.next();
                currentEvent.setTimestamp(currentTime);
                StreamEvent expiredEvent = streamEventCloner.copyStreamEvent(currentEvent);
                expiredEvent.setType(ComplexEvent.Type.EXPIRED);
                state.expiredEventQueue.add(expiredEvent);
            }
            outputStreamEventChunk.add(state.currentEventQueue.getFirst());
            state.currentEventQueue.clear();
            if (includeTriggeringEventExecutor != null && (Boolean) includeTriggeringEventExecutor.execute(streamEvent) || includeTriggeringEvent) {
                outputStreamEventChunk.add(streamEvent);
                StreamEvent expiredEvent = streamEventCloner.copyStreamEvent(streamEvent);
                expiredEvent.setTimestamp(currentTime);
                expiredEvent.setType(ComplexEvent.Type.EXPIRED);
                state.expiredEventQueue.add(expiredEvent);
            } else {
                state.currentEventQueue.add(streamEvent);
            }
        } else {
            StreamEvent expiredEvent = streamEventCloner.copyStreamEvent(streamEvent);
            expiredEvent.setType(ComplexEvent.Type.EXPIRED);
            expiredEvent.setTimestamp(currentTime);
            outputStreamEventChunk.add(expiredEvent);
            streamEvent.setTimestamp(currentTime);
            outputStreamEventChunk.add(streamEvent);
        }
        streamEventChunks.add(outputStreamEventChunk);
    } else {
        state.currentEventQueue.add(streamEvent);
    }
}
Also used : ComplexEventChunk(io.siddhi.core.event.ComplexEventChunk) StreamEvent(io.siddhi.core.event.stream.StreamEvent) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent) MetaStateEvent(io.siddhi.core.event.state.MetaStateEvent) StateEvent(io.siddhi.core.event.state.StateEvent)

Example 14 with StreamEvent

use of io.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, WindowState state) {
    ComplexEventChunk<StreamEvent> newEventChunk = new ComplexEventChunk<>();
    ComplexEventChunk<StreamEvent> sentEventChunk = new ComplexEventChunk<>();
    if (state.currentEventChunk.getFirst() != null) {
        if (state.expiredEventChunk != null && state.expiredEventChunk.getFirst() != null) {
            // mark the timestamp for the expiredType event
            state.expiredEventChunk.reset();
            while (state.expiredEventChunk.hasNext()) {
                StreamEvent expiredEvent = state.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(state.resetEvent);
        toResetEvent.setTimestamp(currentTime);
        newEventChunk.add(toResetEvent);
        // add old events
        newEventChunk.add(sentEventChunk.getFirst());
        // move to expired events
        if (preserveCurrentEvents || state.expiredEventChunk != null) {
            state.currentEventChunk.reset();
            while (state.currentEventChunk.hasNext()) {
                StreamEvent currentEvent = state.currentEventChunk.next();
                StreamEvent toExpireEvent = streamEventCloner.copyStreamEvent(currentEvent);
                toExpireEvent.setType(StreamEvent.Type.EXPIRED);
                state.expiredEventChunk.add(toExpireEvent);
            }
        }
        // add current event chunk to next processor
        newEventChunk.add(state.currentEventChunk.getFirst());
    }
    state.currentEventChunk.clear();
    if (newEventChunk.getFirst() != null) {
        complexEventChunks.add(newEventChunk);
    }
}
Also used : ComplexEventChunk(io.siddhi.core.event.ComplexEventChunk) StreamEvent(io.siddhi.core.event.stream.StreamEvent)

Example 15 with StreamEvent

use of io.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, WindowState state) {
    // event incoming trigger process. No events means no action
    if (streamEventChunk.getFirst() == null) {
        return;
    }
    List<ComplexEventChunk<StreamEvent>> complexEventChunks = new ArrayList<ComplexEventChunk<StreamEvent>>();
    synchronized (state) {
        initTiming(streamEventChunk.getFirst(), state);
        StreamEvent nextStreamEvent = streamEventChunk.getFirst();
        while (nextStreamEvent != null) {
            StreamEvent currStreamEvent = nextStreamEvent;
            nextStreamEvent = nextStreamEvent.getNext();
            if (currStreamEvent.getType() == ComplexEvent.Type.TIMER) {
                if (state.lastScheduledTime <= currStreamEvent.getTimestamp()) {
                    // implies that there have not been any more events after this schedule has been done.
                    if (!state.flushed) {
                        flushToOutputChunk(streamEventCloner, complexEventChunks, state.lastCurrentEventTime, true, state);
                        state.flushed = true;
                    } else {
                        if (state.currentEventChunk.getFirst() != null) {
                            appendToOutputChunk(streamEventCloner, complexEventChunks, state.lastCurrentEventTime, true, state);
                        }
                    }
                    // rescheduling to emit the current batch after expiring it if no further events arrive.
                    state.lastScheduledTime = siddhiQueryContext.getSiddhiAppContext().getTimestampGenerator().currentTime() + schedulerTimeout;
                    scheduler.notifyAt(state.lastScheduledTime);
                }
                continue;
            } else if (currStreamEvent.getType() != ComplexEvent.Type.CURRENT) {
                continue;
            }
            long currentEventTime = (Long) timestampExpressionExecutor.execute(currStreamEvent);
            if (state.lastCurrentEventTime < currentEventTime) {
                state.lastCurrentEventTime = currentEventTime;
            }
            if (currentEventTime < state.endTime) {
                cloneAppend(streamEventCloner, currStreamEvent, state);
            } else {
                if (state.flushed) {
                    appendToOutputChunk(streamEventCloner, complexEventChunks, state.lastCurrentEventTime, false, state);
                    state.flushed = false;
                } else {
                    flushToOutputChunk(streamEventCloner, complexEventChunks, state.lastCurrentEventTime, false, state);
                }
                // update timestamp, call next processor
                state.endTime = findEndTime(state.lastCurrentEventTime, state.startTime, timeToKeep);
                cloneAppend(streamEventCloner, currStreamEvent, state);
                // triggering the last batch expiration.
                if (schedulerTimeout > 0) {
                    state.lastScheduledTime = siddhiQueryContext.getSiddhiAppContext().getTimestampGenerator().currentTime() + schedulerTimeout;
                    scheduler.notifyAt(state.lastScheduledTime);
                }
            }
        }
    }
    for (ComplexEventChunk<StreamEvent> complexEventChunk : complexEventChunks) {
        nextProcessor.process(complexEventChunk);
    }
}
Also used : ComplexEventChunk(io.siddhi.core.event.ComplexEventChunk) StreamEvent(io.siddhi.core.event.stream.StreamEvent) ArrayList(java.util.ArrayList)

Aggregations

StreamEvent (io.siddhi.core.event.stream.StreamEvent)203 MetaStreamEvent (io.siddhi.core.event.stream.MetaStreamEvent)92 ComplexEventChunk (io.siddhi.core.event.ComplexEventChunk)74 StateEvent (io.siddhi.core.event.state.StateEvent)52 ComplexEvent (io.siddhi.core.event.ComplexEvent)33 ArrayList (java.util.ArrayList)27 MetaStateEvent (io.siddhi.core.event.state.MetaStateEvent)26 Map (java.util.Map)24 Event (io.siddhi.core.event.Event)23 Test (org.testng.annotations.Test)23 StreamEventFactory (io.siddhi.core.event.stream.StreamEventFactory)20 ExpressionExecutor (io.siddhi.core.executor.ExpressionExecutor)18 Attribute (io.siddhi.query.api.definition.Attribute)17 HashMap (java.util.HashMap)17 Operation (io.siddhi.core.event.stream.Operation)12 LinkedList (java.util.LinkedList)12 SnapshotableStreamEventQueue (io.siddhi.core.event.stream.holder.SnapshotableStreamEventQueue)11 VariableExpressionExecutor (io.siddhi.core.executor.VariableExpressionExecutor)10 HashSet (java.util.HashSet)10 StreamEventConverter (io.siddhi.core.event.stream.converter.StreamEventConverter)8