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);
}
}
}
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);
}
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations