Search in sources :

Example 31 with ComplexEventChunk

use of org.ballerinalang.siddhi.core.event.ComplexEventChunk in project ballerina by ballerina-lang.

the class IncrementalAggregationProcessor method process.

@Override
public void process(ComplexEventChunk complexEventChunk) {
    ComplexEventChunk<StreamEvent> streamEventChunk = new ComplexEventChunk<>(complexEventChunk.isBatch());
    try {
        int noOfEvents = 0;
        if (latencyTrackerInsert != null && siddhiAppContext.isStatsEnabled()) {
            latencyTrackerInsert.markIn();
        }
        while (complexEventChunk.hasNext()) {
            ComplexEvent complexEvent = complexEventChunk.next();
            StreamEvent borrowedEvent = streamEventPool.borrowEvent();
            for (int i = 0; i < incomingExpressionExecutors.size(); i++) {
                ExpressionExecutor expressionExecutor = incomingExpressionExecutors.get(i);
                Object outputData = expressionExecutor.execute(complexEvent);
                if (expressionExecutor instanceof IncrementalUnixTimeFunctionExecutor && outputData == null) {
                    throw new SiddhiAppRuntimeException("Cannot retrieve the timestamp of event");
                }
                borrowedEvent.setOutputData(outputData, i);
            }
            streamEventChunk.add(borrowedEvent);
            noOfEvents++;
        }
        incrementalExecutor.execute(streamEventChunk);
        if (throughputTrackerInsert != null && siddhiAppContext.isStatsEnabled()) {
            throughputTrackerInsert.eventsIn(noOfEvents);
        }
    } finally {
        if (latencyTrackerInsert != null && siddhiAppContext.isStatsEnabled()) {
            latencyTrackerInsert.markOut();
        }
    }
}
Also used : ComplexEvent(org.ballerinalang.siddhi.core.event.ComplexEvent) ComplexEventChunk(org.ballerinalang.siddhi.core.event.ComplexEventChunk) ExpressionExecutor(org.ballerinalang.siddhi.core.executor.ExpressionExecutor) MetaStreamEvent(org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent) StreamEvent(org.ballerinalang.siddhi.core.event.stream.StreamEvent) SiddhiAppRuntimeException(org.ballerinalang.siddhi.core.exception.SiddhiAppRuntimeException) IncrementalUnixTimeFunctionExecutor(org.ballerinalang.siddhi.core.executor.incremental.IncrementalUnixTimeFunctionExecutor)

Example 32 with ComplexEventChunk

use of org.ballerinalang.siddhi.core.event.ComplexEventChunk in project ballerina by ballerina-lang.

the class IncrementalExecutor method dispatchEvent.

private void dispatchEvent(long startTimeOfNewAggregates, BaseIncrementalValueStore aBaseIncrementalValueStore) {
    if (aBaseIncrementalValueStore.isProcessed()) {
        StreamEvent streamEvent = aBaseIncrementalValueStore.createStreamEvent();
        ComplexEventChunk<StreamEvent> eventChunk = new ComplexEventChunk<>(true);
        eventChunk.add(streamEvent);
        LOG.debug("Event dispatched by " + this.duration + " incremental executor: " + eventChunk.toString());
        table.addEvents(eventChunk, 1);
        if (getNextExecutor() != null) {
            next.execute(eventChunk);
        }
    }
    cleanBaseIncrementalValueStore(startTimeOfNewAggregates, aBaseIncrementalValueStore);
}
Also used : ComplexEventChunk(org.ballerinalang.siddhi.core.event.ComplexEventChunk) MetaStreamEvent(org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent) StreamEvent(org.ballerinalang.siddhi.core.event.stream.StreamEvent)

Example 33 with ComplexEventChunk

use of org.ballerinalang.siddhi.core.event.ComplexEventChunk in project ballerina by ballerina-lang.

the class IncrementalExecutor method dispatchEvents.

private void dispatchEvents(Map<String, BaseIncrementalValueStore> baseIncrementalValueGroupByStore) {
    int noOfEvents = baseIncrementalValueGroupByStore.size();
    if (noOfEvents > 0) {
        ComplexEventChunk<StreamEvent> eventChunk = new ComplexEventChunk<>(true);
        for (BaseIncrementalValueStore aBaseIncrementalValueStore : baseIncrementalValueGroupByStore.values()) {
            StreamEvent streamEvent = aBaseIncrementalValueStore.createStreamEvent();
            eventChunk.add(streamEvent);
        }
        LOG.debug("Event dispatched by " + this.duration + " incremental executor: " + eventChunk.toString());
        table.addEvents(eventChunk, noOfEvents);
        if (getNextExecutor() != null) {
            next.execute(eventChunk);
        }
    }
    baseIncrementalValueGroupByStore.clear();
}
Also used : ComplexEventChunk(org.ballerinalang.siddhi.core.event.ComplexEventChunk) MetaStreamEvent(org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent) StreamEvent(org.ballerinalang.siddhi.core.event.stream.StreamEvent)

Example 34 with ComplexEventChunk

use of org.ballerinalang.siddhi.core.event.ComplexEventChunk in project ballerina by ballerina-lang.

the class RecreateInMemoryData method recreateInMemoryData.

public void recreateInMemoryData() {
    if (incrementalExecutorMap.get(incrementalDurations.get(0)).getNextEmitTime() != -1) {
        // created. Hence this method does not have to be executed.
        return;
    }
    Event[] events;
    Long latestEventTimestamp = null;
    // Get all events from table corresponding to max duration
    Table tableForMaxDuration = aggregationTables.get(incrementalDurations.get(incrementalDurations.size() - 1));
    StoreQuery storeQuery = StoreQuery.query().from(InputStore.store(tableForMaxDuration.getTableDefinition().getId())).select(Selector.selector().orderBy(Expression.variable("AGG_TIMESTAMP")));
    StoreQueryRuntime storeQueryRuntime = StoreQueryParser.parse(storeQuery, siddhiAppContext, tableMap, windowMap, aggregationMap);
    // Get latest event timestamp in tableForMaxDuration
    events = storeQueryRuntime.execute();
    if (events != null) {
        latestEventTimestamp = (Long) events[events.length - 1].getData(0);
    }
    for (int i = incrementalDurations.size() - 1; i > 0; i--) {
        TimePeriod.Duration recreateForDuration = incrementalDurations.get(i);
        IncrementalExecutor incrementalExecutor = incrementalExecutorMap.get(recreateForDuration);
        // Get the table previous to the duration for which we need to recreate (e.g. if we want to recreate
        // for minute duration, take the second table [provided that aggregation is done for seconds])
        Table recreateFromTable = aggregationTables.get(incrementalDurations.get(i - 1));
        storeQuery = StoreQuery.query().from(InputStore.store(recreateFromTable.getTableDefinition().getId())).select(Selector.selector().orderBy(Expression.variable("AGG_TIMESTAMP")));
        storeQueryRuntime = StoreQueryParser.parse(storeQuery, siddhiAppContext, tableMap, windowMap, aggregationMap);
        events = storeQueryRuntime.execute();
        if (events != null) {
            long referenceToNextLatestEvent = (Long) events[events.length - 1].getData(0);
            String timeZoneOfNextLatestEvent = events[events.length - 1].getData(1).toString();
            if (latestEventTimestamp != null) {
                List<Event> eventsNewerThanLatestEventOfRecreateForDuration = new ArrayList<>();
                for (Event event : events) {
                    // After getting the events from recreateFromTable, get the time bucket it belongs to,
                    // if each of these events were in the recreateForDuration. This helps to identify the events,
                    // which must be processed in-memory for recreateForDuration.
                    long timeBucketForNextDuration = IncrementalTimeConverterUtil.getStartTimeOfAggregates((Long) event.getData(0), recreateForDuration, event.getData(1).toString());
                    if (timeBucketForNextDuration > latestEventTimestamp) {
                        eventsNewerThanLatestEventOfRecreateForDuration.add(event);
                    }
                }
                events = eventsNewerThanLatestEventOfRecreateForDuration.toArray(new Event[eventsNewerThanLatestEventOfRecreateForDuration.size()]);
            }
            latestEventTimestamp = referenceToNextLatestEvent;
            ComplexEventChunk<StreamEvent> complexEventChunk = new ComplexEventChunk<>(false);
            for (Event event : events) {
                StreamEvent streamEvent = streamEventPool.borrowEvent();
                streamEvent.setOutputData(event.getData());
                complexEventChunk.add(streamEvent);
            }
            incrementalExecutor.execute(complexEventChunk);
            if (i == 1) {
                TimePeriod.Duration rootDuration = incrementalDurations.get(0);
                IncrementalExecutor rootIncrementalExecutor = incrementalExecutorMap.get(rootDuration);
                long emitTimeOfLatestEventInTable = IncrementalTimeConverterUtil.getNextEmitTime(latestEventTimestamp, rootDuration, timeZoneOfNextLatestEvent);
                rootIncrementalExecutor.setValuesForInMemoryRecreateFromTable(true, emitTimeOfLatestEventInTable);
            }
        }
    }
}
Also used : Table(org.ballerinalang.siddhi.core.table.Table) ComplexEventChunk(org.ballerinalang.siddhi.core.event.ComplexEventChunk) TimePeriod(org.ballerinalang.siddhi.query.api.aggregation.TimePeriod) StoreQuery(org.ballerinalang.siddhi.query.api.execution.query.StoreQuery) MetaStreamEvent(org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent) StreamEvent(org.ballerinalang.siddhi.core.event.stream.StreamEvent) ArrayList(java.util.ArrayList) Event(org.ballerinalang.siddhi.core.event.Event) MetaStreamEvent(org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent) StreamEvent(org.ballerinalang.siddhi.core.event.stream.StreamEvent) StoreQueryRuntime(org.ballerinalang.siddhi.core.query.StoreQueryRuntime)

Example 35 with ComplexEventChunk

use of org.ballerinalang.siddhi.core.event.ComplexEventChunk in project ballerina by ballerina-lang.

the class FindStoreQueryRuntime method generateResetComplexEventChunk.

private ComplexEventChunk<ComplexEvent> generateResetComplexEventChunk(MetaStreamEvent metaStreamEvent) {
    StreamEvent streamEvent = new StreamEvent(metaStreamEvent.getBeforeWindowData().size(), metaStreamEvent.getOnAfterWindowData().size(), metaStreamEvent.getOutputData().size());
    streamEvent.setType(ComplexEvent.Type.RESET);
    StateEvent stateEvent = stateEventPool.borrowEvent();
    if (eventType == MetaStreamEvent.EventType.AGGREGATE) {
        stateEvent.addEvent(1, streamEvent);
    } else {
        stateEvent.addEvent(0, streamEvent);
    }
    stateEvent.setType(ComplexEvent.Type.RESET);
    ComplexEventChunk<ComplexEvent> complexEventChunk = new ComplexEventChunk<>(true);
    complexEventChunk.add(stateEvent);
    return complexEventChunk;
}
Also used : ComplexEvent(org.ballerinalang.siddhi.core.event.ComplexEvent) ComplexEventChunk(org.ballerinalang.siddhi.core.event.ComplexEventChunk) MetaStreamEvent(org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent) StreamEvent(org.ballerinalang.siddhi.core.event.stream.StreamEvent) StateEvent(org.ballerinalang.siddhi.core.event.state.StateEvent)

Aggregations

ComplexEventChunk (org.ballerinalang.siddhi.core.event.ComplexEventChunk)69 StreamEvent (org.ballerinalang.siddhi.core.event.stream.StreamEvent)41 ComplexEvent (org.ballerinalang.siddhi.core.event.ComplexEvent)30 ArrayList (java.util.ArrayList)23 StateEvent (org.ballerinalang.siddhi.core.event.state.StateEvent)19 GroupedComplexEvent (org.ballerinalang.siddhi.core.event.GroupedComplexEvent)13 MetaStreamEvent (org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent)12 ExpressionExecutor (org.ballerinalang.siddhi.core.executor.ExpressionExecutor)10 Map (java.util.Map)8 VariableExpressionExecutor (org.ballerinalang.siddhi.core.executor.VariableExpressionExecutor)4 HashMap (java.util.HashMap)3 Table (org.ballerinalang.siddhi.core.table.Table)3 Collection (java.util.Collection)2 Iterator (java.util.Iterator)2 Event (org.ballerinalang.siddhi.core.event.Event)2 SiddhiAppRuntimeException (org.ballerinalang.siddhi.core.exception.SiddhiAppRuntimeException)2 StreamPreStateProcessor (org.ballerinalang.siddhi.core.query.input.stream.state.StreamPreStateProcessor)2 TimePeriod (org.ballerinalang.siddhi.query.api.aggregation.TimePeriod)2 IncrementalDataAggregator (org.ballerinalang.siddhi.core.aggregation.IncrementalDataAggregator)1 OperationNotSupportedException (org.ballerinalang.siddhi.core.exception.OperationNotSupportedException)1