Search in sources :

Example 1 with ComplexEventChunk

use of io.siddhi.core.event.ComplexEventChunk in project siddhi by wso2.

the class IncrementalExecutorsInitialiser method recreateState.

private void recreateState(Long lastData, TimePeriod.Duration recreateForDuration, Table recreateFromTable, boolean isBeforeRoot) {
    Executor incrementalExecutor = incrementalExecutorMap.get(recreateForDuration);
    if (lastData != null) {
        endOFLatestEventTimestamp = IncrementalTimeConverterUtil.getNextEmitTime(lastData, recreateForDuration, timeZone);
    }
    OnDemandQuery onDemandQuery = getOnDemandQuery(recreateFromTable, false, endOFLatestEventTimestamp);
    onDemandQuery.setType(OnDemandQuery.OnDemandQueryType.FIND);
    OnDemandQueryRuntime onDemandQueryRuntime = OnDemandQueryParser.parse(onDemandQuery, null, siddhiAppContext, tableMap, windowMap, aggregationMap);
    Event[] events = onDemandQueryRuntime.execute();
    if (events != null) {
        long referenceToNextLatestEvent = (Long) events[events.length - 1].getData(0);
        ComplexEventChunk<StreamEvent> complexEventChunk = new ComplexEventChunk<>();
        for (Event event : events) {
            StreamEvent streamEvent = streamEventFactory.newInstance();
            streamEvent.setOutputData(event.getData());
            complexEventChunk.add(streamEvent);
        }
        incrementalExecutor.execute(complexEventChunk);
        if (isBeforeRoot) {
            TimePeriod.Duration rootDuration = incrementalDurations.get(0);
            Executor rootIncrementalExecutor = incrementalExecutorMap.get(rootDuration);
            long emitTimeOfLatestEventInTable = IncrementalTimeConverterUtil.getNextEmitTime(referenceToNextLatestEvent, rootDuration, timeZone);
            rootIncrementalExecutor.setEmitTime(emitTimeOfLatestEventInTable);
        }
    }
}
Also used : ComplexEventChunk(io.siddhi.core.event.ComplexEventChunk) OnDemandQueryRuntime(io.siddhi.core.query.OnDemandQueryRuntime) TimePeriod(io.siddhi.query.api.aggregation.TimePeriod) StreamEvent(io.siddhi.core.event.stream.StreamEvent) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent) Event(io.siddhi.core.event.Event) StreamEvent(io.siddhi.core.event.stream.StreamEvent) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent) OnDemandQuery(io.siddhi.query.api.execution.query.OnDemandQuery)

Example 2 with ComplexEventChunk

use of io.siddhi.core.event.ComplexEventChunk 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 3 with ComplexEventChunk

use of io.siddhi.core.event.ComplexEventChunk 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 4 with ComplexEventChunk

use of io.siddhi.core.event.ComplexEventChunk 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 5 with ComplexEventChunk

use of io.siddhi.core.event.ComplexEventChunk 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

ComplexEventChunk (io.siddhi.core.event.ComplexEventChunk)114 StreamEvent (io.siddhi.core.event.stream.StreamEvent)75 ComplexEvent (io.siddhi.core.event.ComplexEvent)41 StateEvent (io.siddhi.core.event.state.StateEvent)38 MetaStreamEvent (io.siddhi.core.event.stream.MetaStreamEvent)34 ExpressionExecutor (io.siddhi.core.executor.ExpressionExecutor)14 GroupedComplexEvent (io.siddhi.core.event.GroupedComplexEvent)13 LinkedList (java.util.LinkedList)12 Map (java.util.Map)12 MetaStateEvent (io.siddhi.core.event.state.MetaStateEvent)10 ArrayList (java.util.ArrayList)10 VariableExpressionExecutor (io.siddhi.core.executor.VariableExpressionExecutor)7 HashMap (java.util.HashMap)7 Attribute (io.siddhi.query.api.definition.Attribute)5 Event (io.siddhi.core.event.Event)4 TimePeriod (io.siddhi.query.api.aggregation.TimePeriod)4 SnapshotableStreamEventQueue (io.siddhi.core.event.stream.holder.SnapshotableStreamEventQueue)3 SiddhiAppRuntimeException (io.siddhi.core.exception.SiddhiAppRuntimeException)3 Table (io.siddhi.core.table.Table)3 ConstantExpressionExecutor (io.siddhi.core.executor.ConstantExpressionExecutor)2