Search in sources :

Example 11 with ComplexEventPopulater

use of org.wso2.siddhi.core.event.stream.populater.ComplexEventPopulater in project carbon-apimgt by wso2.

the class AsyncAPIThrottleStreamProcessor method process.

@Override
protected void process(ComplexEventChunk<StreamEvent> streamEventChunk, Processor nextProcessor, StreamEventCloner streamEventCloner, ComplexEventPopulater complexEventPopulater) {
    synchronized (this) {
        long currentTime = executionPlanContext.getTimestampGenerator().currentTime();
        if (expireEventTime == -1) {
            if (startTime != -1) {
                expireEventTime = addTimeShift(currentTime);
            } else {
                expireEventTime = executionPlanContext.getTimestampGenerator().currentTime() + timeInMilliSeconds;
            }
            scheduler.notifyAt(expireEventTime);
        }
        if (currentTime >= expireEventTime) {
            expireEventTime += timeInMilliSeconds;
            scheduler.notifyAt(expireEventTime);
            throttledStateMap.clear();
        }
        while (streamEventChunk.hasNext()) {
            String throttleKey;
            long eventCount;
            StreamEvent streamEvent = streamEventChunk.next();
            if (streamEvent.getType() != ComplexEvent.Type.CURRENT) {
                continue;
            }
            if (streamEvent.getOutputData()[0] != null) {
                throttleKey = streamEvent.getOutputData()[0].toString();
                if (throttledStateMap.containsKey(throttleKey)) {
                    eventCount = throttledStateMap.get(throttleKey).incrementAndGet();
                    if (eventCount > maxEventCount) {
                        complexEventPopulater.populateComplexEvent(streamEvent, new Object[] { expireEventTime, true });
                    } else {
                        complexEventPopulater.populateComplexEvent(streamEvent, new Object[] { expireEventTime, false });
                    }
                } else {
                    throttledStateMap.put(throttleKey, new AtomicLong(1));
                    complexEventPopulater.populateComplexEvent(streamEvent, new Object[] { expireEventTime, false });
                }
            } else {
                complexEventPopulater.populateComplexEvent(streamEvent, new Object[] { expireEventTime, false });
            }
            StreamEvent clonedStreamEvent = streamEventCloner.copyStreamEvent(streamEvent);
            clonedStreamEvent.setType(StreamEvent.Type.EXPIRED);
            clonedStreamEvent.setTimestamp(expireEventTime);
            expiredEventChunk.add(clonedStreamEvent);
        }
        expiredEventChunk.reset();
        if (expiredEventChunk.getFirst() != null) {
            streamEventChunk.add(expiredEventChunk.getFirst());
        }
        expiredEventChunk.clear();
    }
    if (streamEventChunk.getFirst() != null) {
        streamEventChunk.setBatch(true);
        nextProcessor.process(streamEventChunk);
        streamEventChunk.setBatch(false);
    }
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent)

Example 12 with ComplexEventPopulater

use of org.wso2.siddhi.core.event.stream.populater.ComplexEventPopulater in project carbon-apimgt by wso2.

the class ThrottleStreamProcessor method process.

@Override
protected void process(ComplexEventChunk<StreamEvent> streamEventChunk, Processor nextProcessor, StreamEventCloner streamEventCloner, ComplexEventPopulater complexEventPopulater) {
    synchronized (this) {
        if (expireEventTime == -1) {
            long currentTime = executionPlanContext.getTimestampGenerator().currentTime();
            if (startTime != -1) {
                expireEventTime = addTimeShift(currentTime);
            } else {
                expireEventTime = executionPlanContext.getTimestampGenerator().currentTime() + timeInMilliSeconds;
            }
            scheduler.notifyAt(expireEventTime);
        }
        long currentTime = executionPlanContext.getTimestampGenerator().currentTime();
        boolean sendEvents;
        if (currentTime >= expireEventTime) {
            expireEventTime += timeInMilliSeconds;
            scheduler.notifyAt(expireEventTime);
            sendEvents = true;
        } else {
            sendEvents = false;
        }
        while (streamEventChunk.hasNext()) {
            StreamEvent streamEvent = streamEventChunk.next();
            if (streamEvent.getType() != ComplexEvent.Type.CURRENT) {
                continue;
            }
            complexEventPopulater.populateComplexEvent(streamEvent, new Object[] { expireEventTime });
            StreamEvent clonedStreamEvent = streamEventCloner.copyStreamEvent(streamEvent);
            clonedStreamEvent.setType(StreamEvent.Type.EXPIRED);
            clonedStreamEvent.setTimestamp(expireEventTime);
            expiredEventChunk.add(clonedStreamEvent);
        }
        if (sendEvents) {
            expiredEventChunk.reset();
            if (expiredEventChunk.getFirst() != null) {
                streamEventChunk.add(expiredEventChunk.getFirst());
            }
            expiredEventChunk.clear();
        }
    }
    if (streamEventChunk.getFirst() != null) {
        streamEventChunk.setBatch(true);
        nextProcessor.process(streamEventChunk);
        streamEventChunk.setBatch(false);
    }
}
Also used : StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent)

Aggregations

SiddhiAppRuntimeException (org.wso2.siddhi.core.exception.SiddhiAppRuntimeException)4 ExpressionExecutor (org.wso2.siddhi.core.executor.ExpressionExecutor)4 StreamEvent (org.wso2.siddhi.core.event.stream.StreamEvent)3 VariableExpressionExecutor (org.wso2.siddhi.core.executor.VariableExpressionExecutor)3 ComplexEvent (org.wso2.siddhi.core.event.ComplexEvent)2 ComplexEventPopulater (org.wso2.siddhi.core.event.stream.populater.ComplexEventPopulater)2 IncrementalAggregateCompileCondition (org.wso2.siddhi.core.util.collection.operator.IncrementalAggregateCompileCondition)2 ArrayList (java.util.ArrayList)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 MetaStateEvent (org.wso2.siddhi.core.event.state.MetaStateEvent)1 StateEventCloner (org.wso2.siddhi.core.event.state.StateEventCloner)1 MetaStreamEvent (org.wso2.siddhi.core.event.stream.MetaStreamEvent)1 StreamEventCloner (org.wso2.siddhi.core.event.stream.StreamEventCloner)1 StreamEventPool (org.wso2.siddhi.core.event.stream.StreamEventPool)1 SiddhiAppCreationException (org.wso2.siddhi.core.exception.SiddhiAppCreationException)1 FindStoreQueryRuntime (org.wso2.siddhi.core.query.FindStoreQueryRuntime)1 ProcessStreamReceiver (org.wso2.siddhi.core.query.input.ProcessStreamReceiver)1 JoinProcessor (org.wso2.siddhi.core.query.input.stream.join.JoinProcessor)1 StreamPreStateProcessor (org.wso2.siddhi.core.query.input.stream.state.StreamPreStateProcessor)1 Processor (org.wso2.siddhi.core.query.processor.Processor)1