Search in sources :

Example 46 with ComplexEventChunk

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

the class UpdateOrInsertReducer method reduceEventsForInsert.

public List<Object[]> reduceEventsForInsert(List<Object[]> failedRecords, Map<String, ExpressionExecutor> inMemorySetExecutors) {
    ComplexEventChunk<StreamEvent> toInsertEventChunk = new ComplexEventChunk<>();
    StateEvent joinEvent = stateEventFactory.newInstance();
    for (Object[] data : failedRecords) {
        StreamEvent failedEvent = streamEventFactory.newInstance();
        failedEvent.setOutputData(data);
        joinEvent.setEvent(streamEventIndex, failedEvent);
        boolean updated = false;
        toInsertEventChunk.reset();
        while (toInsertEventChunk.hasNext()) {
            StreamEvent toInsertEvent = toInsertEventChunk.next();
            joinEvent.setEvent(storeEventIndex, toInsertEvent);
            if ((Boolean) inMemoryCompiledCondition.execute(joinEvent)) {
                for (Map.Entry<String, ExpressionExecutor> entry : inMemorySetExecutors.entrySet()) {
                    toInsertEvent.setOutputData(entry.getValue().execute(failedEvent), attributeMap.get(entry.getKey()));
                }
                updated = true;
            }
        }
        if (!updated) {
            toInsertEventChunk.add(failedEvent);
        }
    }
    List<Object[]> toInsertRecords = new LinkedList<>();
    toInsertEventChunk.reset();
    while (toInsertEventChunk.hasNext()) {
        StreamEvent streamEvent = toInsertEventChunk.next();
        toInsertRecords.add(streamEvent.getOutputData());
    }
    return toInsertRecords;
}
Also used : ComplexEventChunk(io.siddhi.core.event.ComplexEventChunk) ExpressionExecutor(io.siddhi.core.executor.ExpressionExecutor) StreamEvent(io.siddhi.core.event.stream.StreamEvent) LinkedList(java.util.LinkedList) StateEvent(io.siddhi.core.event.state.StateEvent) HashMap(java.util.HashMap) Map(java.util.Map)

Example 47 with ComplexEventChunk

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

the class IncrementalAggregateCompileCondition method find.

public StreamEvent find(StateEvent matchingEvent, Map<TimePeriod.Duration, Executor> incrementalExecutorMap, Map<TimePeriod.Duration, List<ExpressionExecutor>> aggregateProcessingExecutorsMap, Map<TimePeriod.Duration, GroupByKeyGenerator> groupByKeyGeneratorMap, ExpressionExecutor shouldUpdateTimestamp, String timeZone) {
    ComplexEventChunk<StreamEvent> complexEventChunkToHoldWithinMatches = new ComplexEventChunk<>();
    // Create matching event if it is on-demand query
    int additionTimestampAttributesSize = this.timestampFilterExecutors.size() + 2;
    Long[] timestampFilters = new Long[additionTimestampAttributesSize];
    if (matchingEvent.getStreamEvent(0) == null) {
        StreamEvent streamEvent = new StreamEvent(0, additionTimestampAttributesSize, 0);
        matchingEvent.addEvent(0, streamEvent);
    }
    Long[] startTimeEndTime = (Long[]) startTimeEndTimeExpressionExecutor.execute(matchingEvent);
    if (startTimeEndTime == null) {
        throw new SiddhiAppRuntimeException("Start and end times for within duration cannot be retrieved");
    }
    timestampFilters[0] = startTimeEndTime[0];
    timestampFilters[1] = startTimeEndTime[1];
    if (isDistributed) {
        for (int i = 0; i < additionTimestampAttributesSize - 2; i++) {
            timestampFilters[i + 2] = ((Long) this.timestampFilterExecutors.get(i).execute(matchingEvent));
        }
    }
    complexEventPopulater.populateComplexEvent(matchingEvent.getStreamEvent(0), timestampFilters);
    // Get all the aggregates within the given duration, from table corresponding to "per" duration
    // Retrieve per value
    String perValueAsString = perExpressionExecutor.execute(matchingEvent).toString();
    TimePeriod.Duration perValue;
    try {
        // Per time function verification
        perValue = normalizeDuration(perValueAsString);
    } catch (SiddhiAppValidationException e) {
        throw new SiddhiAppRuntimeException("Aggregation Query's per value is expected to be of a valid time function of the " + "following " + TimePeriod.Duration.SECONDS + ", " + TimePeriod.Duration.MINUTES + ", " + TimePeriod.Duration.HOURS + ", " + TimePeriod.Duration.DAYS + ", " + TimePeriod.Duration.MONTHS + ", " + TimePeriod.Duration.YEARS + ".");
    }
    if (!incrementalExecutorMap.keySet().contains(perValue)) {
        throw new SiddhiAppRuntimeException("The aggregate values for " + perValue.toString() + " granularity cannot be provided since aggregation definition " + aggregationName + " does not contain " + perValue.toString() + " duration");
    }
    Table tableForPerDuration = aggregationTableMap.get(perValue);
    StreamEvent withinMatchFromPersistedEvents;
    if (isOptimisedLookup) {
        withinMatchFromPersistedEvents = query(tableForPerDuration, matchingEvent, withinTableCompiledConditions.get(perValue), withinTableCompiledSelection.get(perValue), tableMetaStreamEvent.getLastInputDefinition().getAttributeList().toArray(new Attribute[0]));
    } else {
        withinMatchFromPersistedEvents = tableForPerDuration.find(matchingEvent, withinTableCompiledConditions.get(perValue));
    }
    complexEventChunkToHoldWithinMatches.add(withinMatchFromPersistedEvents);
    // Optimization step.
    long oldestInMemoryEventTimestamp = getOldestInMemoryEventTimestamp(incrementalExecutorMap, activeIncrementalDurations, perValue);
    // If processing on external time, the in-memory data also needs to be queried
    if (isProcessingOnExternalTime || requiresAggregatingInMemoryData(oldestInMemoryEventTimestamp, startTimeEndTime)) {
        if (isDistributed) {
            int perValueIndex = this.activeIncrementalDurations.indexOf(perValue);
            if (perValueIndex != 0) {
                Map<TimePeriod.Duration, CompiledCondition> lowerGranularityLookups = new HashMap<>();
                for (int i = 0; i < perValueIndex; i++) {
                    TimePeriod.Duration key = this.activeIncrementalDurations.get(i);
                    lowerGranularityLookups.put(key, withinTableLowerGranularityCompileCondition.get(key));
                }
                List<StreamEvent> eventChunks = lowerGranularityLookups.entrySet().stream().map((entry) -> {
                    Table table = aggregationTableMap.get(entry.getKey());
                    if (isOptimisedLookup) {
                        return query(table, matchingEvent, entry.getValue(), withinTableCompiledSelection.get(entry.getKey()), tableMetaStreamEvent.getLastInputDefinition().getAttributeList().toArray(new Attribute[0]));
                    } else {
                        return table.find(matchingEvent, entry.getValue());
                    }
                }).filter(Objects::nonNull).collect(Collectors.toList());
                eventChunks.forEach(complexEventChunkToHoldWithinMatches::add);
            }
        } else {
            TimePeriod.Duration rootDuration = activeIncrementalDurations.get(0);
            IncrementalDataAggregator incrementalDataAggregator = new IncrementalDataAggregator(activeIncrementalDurations, perValue, oldestInMemoryEventTimestamp, aggregateProcessingExecutorsMap.get(rootDuration), shouldUpdateTimestamp, groupByKeyGeneratorMap.get(rootDuration) != null, tableMetaStreamEvent, timeZone);
            ComplexEventChunk<StreamEvent> aggregatedInMemoryEventChunk;
            // Aggregate in-memory data and create an event chunk out of it
            aggregatedInMemoryEventChunk = incrementalDataAggregator.aggregateInMemoryData(incrementalExecutorMap);
            // Get the in-memory aggregate data, which is within given duration
            StreamEvent withinMatchFromInMemory = ((Operator) inMemoryStoreCompileCondition).find(matchingEvent, aggregatedInMemoryEventChunk, tableEventCloner);
            complexEventChunkToHoldWithinMatches.add(withinMatchFromInMemory);
        }
    }
    ComplexEventChunk<StreamEvent> processedEvents;
    if (isDistributed || isProcessingOnExternalTime) {
        List<ExpressionExecutor> expressionExecutors = aggregateProcessingExecutorsMap.get(perValue);
        GroupByKeyGenerator groupByKeyGenerator = groupByKeyGeneratorMap.get(perValue);
        OutOfOrderEventsDataAggregator outOfOrderEventsDataAggregator = new OutOfOrderEventsDataAggregator(expressionExecutors, shouldUpdateTimestamp, groupByKeyGenerator, tableMetaStreamEvent);
        processedEvents = outOfOrderEventsDataAggregator.aggregateData(complexEventChunkToHoldWithinMatches);
    } else {
        processedEvents = complexEventChunkToHoldWithinMatches;
    }
    // Get the final event chunk from the data which is within given duration. This event chunk contains the values
    // in the select clause of an aggregate definition.
    ComplexEventChunk<StreamEvent> aggregateSelectionComplexEventChunk = createAggregateSelectionEventChunk(processedEvents, outputExpressionExecutors);
    // Execute the on compile condition
    return ((Operator) onCompiledCondition).find(matchingEvent, aggregateSelectionComplexEventChunk, aggregateEventCloner);
}
Also used : ComplexEventChunk(io.siddhi.core.event.ComplexEventChunk) HashMap(java.util.HashMap) Attribute(io.siddhi.query.api.definition.Attribute) GroupByKeyGenerator(io.siddhi.core.query.selector.GroupByKeyGenerator) SiddhiAppRuntimeException(io.siddhi.core.exception.SiddhiAppRuntimeException) OutOfOrderEventsDataAggregator(io.siddhi.core.aggregation.OutOfOrderEventsDataAggregator) Table(io.siddhi.core.table.Table) VariableExpressionExecutor(io.siddhi.core.executor.VariableExpressionExecutor) ExpressionExecutor(io.siddhi.core.executor.ExpressionExecutor) TimePeriod(io.siddhi.query.api.aggregation.TimePeriod) StreamEvent(io.siddhi.core.event.stream.StreamEvent) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent) SiddhiAppValidationException(io.siddhi.query.api.exception.SiddhiAppValidationException) Time.normalizeDuration(io.siddhi.query.api.expression.Expression.Time.normalizeDuration) IncrementalDataAggregator(io.siddhi.core.aggregation.IncrementalDataAggregator)

Example 48 with ComplexEventChunk

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

the class Scheduler method sendTimerEvents.

/**
 * Go through the timestamps stored in the toNotifyQueue and send the TIMER events for the expired events.
 *
 * @param state current state
 */
private void sendTimerEvents(SchedulerState state) {
    Long toNotifyTime = state.toNotifyQueue.peek();
    long currentTime = siddhiQueryContext.getSiddhiAppContext().getTimestampGenerator().currentTime();
    while (toNotifyTime != null && toNotifyTime - currentTime <= 0) {
        state.toNotifyQueue.poll();
        StreamEvent timerEvent = streamEventFactory.newInstance();
        timerEvent.setType(StreamEvent.Type.TIMER);
        timerEvent.setTimestamp(toNotifyTime);
        if (lockWrapper != null) {
            lockWrapper.lock();
        }
        threadBarrier.enter();
        try {
            ComplexEventChunk<StreamEvent> streamEventChunk = new ComplexEventChunk<>();
            streamEventChunk.add(timerEvent);
            if (Level.BASIC.compareTo(siddhiQueryContext.getSiddhiAppContext().getRootMetricsLevel()) <= 0 && latencyTracker != null) {
                try {
                    latencyTracker.markIn();
                    singleThreadEntryValve.process(streamEventChunk);
                } finally {
                    latencyTracker.markOut();
                }
            } else {
                singleThreadEntryValve.process(streamEventChunk);
            }
        } catch (Throwable t) {
            log.error("Error while sending timer events, " + t.getMessage(), t);
        } finally {
            if (lockWrapper != null) {
                lockWrapper.unlock();
            }
            threadBarrier.exit();
        }
        toNotifyTime = state.toNotifyQueue.peek();
        currentTime = siddhiQueryContext.getSiddhiAppContext().getTimestampGenerator().currentTime();
    }
}
Also used : ComplexEventChunk(io.siddhi.core.event.ComplexEventChunk) StreamEvent(io.siddhi.core.event.stream.StreamEvent)

Example 49 with ComplexEventChunk

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

the class EventChunkOperator method update.

@Override
public void update(ComplexEventChunk<StateEvent> updatingEventChunk, Object storeEvents, InMemoryCompiledUpdateSet compiledUpdateSet) {
    ComplexEventChunk<StreamEvent> storeEventChunk = (ComplexEventChunk<StreamEvent>) storeEvents;
    updatingEventChunk.reset();
    while (updatingEventChunk.hasNext()) {
        StateEvent updatingEvent = updatingEventChunk.next();
        try {
            storeEventChunk.reset();
            while (storeEventChunk.hasNext()) {
                StreamEvent storeEvent = storeEventChunk.next();
                updatingEvent.setEvent(storeEventPosition, storeEvent);
                if ((Boolean) expressionExecutor.execute(updatingEvent)) {
                    for (Map.Entry<Integer, ExpressionExecutor> entry : compiledUpdateSet.getExpressionExecutorMap().entrySet()) {
                        storeEvent.setOutputData(entry.getValue().execute(updatingEvent), entry.getKey());
                    }
                }
            }
        } finally {
            updatingEvent.setEvent(storeEventPosition, null);
        }
    }
}
Also used : ComplexEventChunk(io.siddhi.core.event.ComplexEventChunk) ExpressionExecutor(io.siddhi.core.executor.ExpressionExecutor) StreamEvent(io.siddhi.core.event.stream.StreamEvent) StateEvent(io.siddhi.core.event.state.StateEvent) Map(java.util.Map)

Example 50 with ComplexEventChunk

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

the class EventChunkOperator method delete.

@Override
public void delete(ComplexEventChunk<StateEvent> deletingEventChunk, Object storeEvents) {
    ComplexEventChunk<StreamEvent> storeEventChunk = (ComplexEventChunk<StreamEvent>) storeEvents;
    deletingEventChunk.reset();
    while (deletingEventChunk.hasNext()) {
        StateEvent deletingEvent = deletingEventChunk.next();
        try {
            storeEventChunk.reset();
            while (storeEventChunk.hasNext()) {
                StreamEvent storeEvent = storeEventChunk.next();
                deletingEvent.setEvent(storeEventPosition, storeEvent);
                if ((Boolean) expressionExecutor.execute(deletingEvent)) {
                    storeEventChunk.remove();
                }
            }
        } finally {
            deletingEvent.setEvent(storeEventPosition, null);
        }
    }
}
Also used : ComplexEventChunk(io.siddhi.core.event.ComplexEventChunk) StreamEvent(io.siddhi.core.event.stream.StreamEvent) StateEvent(io.siddhi.core.event.state.StateEvent)

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