Example #1
0
        public override void Update(EventBean[] newData, EventBean[] oldData)
        {
            Instrument.With(
                i => i.QViewProcessIRStream(this, _timeWindowViewFactory.ViewName, newData, oldData),
                i => i.AViewProcessIRStream(),
                () =>
            {
                long timestamp = _agentInstanceContext.StatementContext.SchedulingService.Time;

                if (oldData != null)
                {
                    for (int i = 0; i < oldData.Length; i++)
                    {
                        _timeWindow.Remove(oldData[i]);
                    }
                }

                // we don't care about removed data from a prior view
                if ((newData != null) && (newData.Length > 0))
                {
                    // If we have an empty window about to be filled for the first time, schedule a callback
                    // for now plus timeDeltaComputation
                    if (_timeWindow.IsEmpty())
                    {
                        long current = _agentInstanceContext.StatementContext.SchedulingService.Time;
                        ScheduleCallback(_timeDeltaComputation.DeltaMillisecondsAdd(current));
                    }

                    // add data points to the timeWindow
                    for (int i = 0; i < newData.Length; i++)
                    {
                        _timeWindow.Add(timestamp, newData[i]);
                    }

                    if (_viewUpdatedCollection != null)
                    {
                        _viewUpdatedCollection.Update(newData, null);
                    }
                }

                // Update child views
                if (HasViews)
                {
                    Instrument.With(
                        i => i.QViewIndicate(this, _timeWindowViewFactory.ViewName, newData, oldData),
                        i => i.AViewIndicate(),
                        () => UpdateChildren(newData, oldData));
                }
            });
        }
Example #2
0
        protected void ScheduleCallback(long delta)
        {
            ScheduleHandleCallback callback = new ProxyScheduleHandleCallback
            {
                ProcScheduledTrigger = extensionServicesContext => Instrument.With(
                    i => i.QViewScheduledEval(this, _timeLengthBatchViewFactory.ViewName),
                    i => i.AViewScheduledEval(),
                    () => SendBatch(true))
            };

            _handle = new EPStatementHandleCallback(_agentInstanceContext.EpStatementAgentInstanceHandle, callback);
            var currentTime = _agentInstanceContext.StatementContext.SchedulingService.Time;
            var scheduled   = _timeDeltaComputation.DeltaMillisecondsAdd(currentTime) - delta;

            _agentInstanceContext.StatementContext.SchedulingService.Add(scheduled, _handle, _scheduleSlot);
            _callbackScheduledTime = _agentInstanceContext.StatementContext.SchedulingService.Time + scheduled;
        }
Example #3
0
        /// <summary>
        /// Returns the number of milliseconds.
        /// </summary>
        public long GetScheduleForwardDelta(long fromTime, AgentInstanceContext agentInstanceContext)
        {
            if (InstrumentationHelper.ENABLED)
            {
                InstrumentationHelper.Get().QRegIntervalValue(TimePeriodExpr);
            }
            if (_timeDeltaComputation == null)
            {
                _timeDeltaComputation = TimePeriodExpr.ConstEvaluator(new ExprEvaluatorContextStatement(agentInstanceContext.StatementContext, false));
            }
            long result = _timeDeltaComputation.DeltaMillisecondsAdd(fromTime);

            if (InstrumentationHelper.ENABLED)
            {
                InstrumentationHelper.Get().ARegIntervalValue(result);
            }
            return(result);
        }
Example #4
0
        private void ScheduleCallback()
        {
            long afterMSec = _timeDeltaComputation.DeltaMillisecondsAdd(
                AgentInstanceContext.StatementContext.SchedulingService.Time);

            ScheduleHandleCallback callback = new ProxyScheduleHandleCallback
            {
                ProcScheduledTrigger = extensionServicesContext => Instrument.With(
                    i => i.QViewScheduledEval(this, _timeFirstViewFactory.ViewName),
                    i => i.AViewScheduledEval(),
                    () =>
                {
                    _isClosed = true;
                    InternalHandleClosed();
                })
            };

            _handle = new EPStatementHandleCallback(AgentInstanceContext.EpStatementAgentInstanceHandle, callback);
            AgentInstanceContext.StatementContext.SchedulingService.Add(afterMSec, _handle, _scheduleSlot);
        }
        public override void Update(EventBean[] newData, EventBean[] oldData)
        {
            using (Instrument.With(
                       i => i.QViewProcessIRStream(this, _factory.ViewName, newData, oldData),
                       i => i.AViewProcessIRStream()))
            {
                if ((newData != null) && (newData.Length > 0))
                {
                    // If we have an empty window about to be filled for the first time, add a callback
                    bool removeSchedule = false;
                    bool addSchedule    = false;
                    long timestamp      = _agentInstanceContext.StatementContext.SchedulingService.Time;

                    // if the window is already filled, then we may need to reschedule
                    if (_currentBatch.IsNotEmpty())
                    {
                        // check if we need to reschedule
                        long callbackTime = timestamp + _timeDeltaComputation.DeltaMillisecondsAdd(timestamp);
                        if (callbackTime != _callbackScheduledTime)
                        {
                            removeSchedule = true;
                            addSchedule    = true;
                        }
                    }
                    else
                    {
                        addSchedule = true;
                    }

                    if (removeSchedule)
                    {
                        _agentInstanceContext.StatementContext.SchedulingService.Remove(_handle, _scheduleSlot);
                        _callbackScheduledTime = -1;
                    }
                    if (addSchedule)
                    {
                        long msecIntervalSize = _timeDeltaComputation.DeltaMillisecondsAdd(timestamp);
                        _agentInstanceContext.StatementContext.SchedulingService.Add(
                            msecIntervalSize, _handle, _scheduleSlot);
                        _callbackScheduledTime = msecIntervalSize + timestamp;
                    }

                    // add data points to the window
                    for (int i = 0; i < newData.Length; i++)
                    {
                        _currentBatch.Put(newData[i], timestamp);
                        InternalHandleAdded(newData[i], timestamp);
                        _lastEvent = newData[i];
                    }
                }

                if ((oldData != null) && (oldData.Length > 0))
                {
                    bool removedLastEvent = false;
                    foreach (EventBean anOldData in oldData)
                    {
                        _currentBatch.Remove(anOldData);
                        InternalHandleRemoved(anOldData);
                        if (anOldData == _lastEvent)
                        {
                            removedLastEvent = true;
                        }
                    }

                    // we may need to reschedule as the newest event may have been deleted
                    if (_currentBatch.Count == 0)
                    {
                        _agentInstanceContext.StatementContext.SchedulingService.Remove(_handle, _scheduleSlot);
                        _callbackScheduledTime = -1;
                        _lastEvent             = null;
                    }
                    else
                    {
                        // reschedule if the last event was removed
                        if (removedLastEvent)
                        {
                            ICollection <EventBean> keyset = _currentBatch.Keys;
                            EventBean[]             events = keyset.ToArray();
                            _lastEvent = events[events.Length - 1];
                            long lastTimestamp = _currentBatch.Get(_lastEvent);

                            // reschedule, newest event deleted
                            long timestamp    = _agentInstanceContext.StatementContext.SchedulingService.Time;
                            long callbackTime = lastTimestamp + _timeDeltaComputation.DeltaMillisecondsAdd(lastTimestamp);
                            long deltaFromNow = callbackTime - timestamp;
                            if (callbackTime != _callbackScheduledTime)
                            {
                                _agentInstanceContext.StatementContext.SchedulingService.Remove(_handle, _scheduleSlot);
                                _agentInstanceContext.StatementContext.SchedulingService.Add(
                                    deltaFromNow, _handle, _scheduleSlot);
                                _callbackScheduledTime = callbackTime;
                            }
                        }
                    }
                }

                // Update child views
                if (HasViews)
                {
                    using (Instrument.With(
                               i => i.QViewIndicate(this, _factory.ViewName, newData, oldData),
                               i => i.AViewIndicate()))
                    {
                        UpdateChildren(newData, oldData);
                    }
                }
            }
        }
Example #6
0
 private static ExprOptionalConstant GetExprOrConstant(ExprNode exprNode)
 {
     if (exprNode is ExprTimePeriod)
     {
         var timePeriod = (ExprTimePeriod)exprNode;
         if (!timePeriod.HasMonth && !timePeriod.HasYear)
         {
             // no-month and constant
             if (exprNode.IsConstantResult)
             {
                 double sec = timePeriod.EvaluateAsSeconds(null, true, null);
                 var    l   = (long)(sec * 1000L);
                 IntervalDeltaExprEvaluator eval = new ProxyIntervalDeltaExprEvaluator
                 {
                     ProcEvaluate = (reference, eventsPerStream, isNewData, context) => l,
                 };
                 return(new ExprOptionalConstant(eval, l));
             }
             // no-month and not constant
             else
             {
                 IntervalDeltaExprEvaluator eval = new ProxyIntervalDeltaExprEvaluator
                 {
                     ProcEvaluate = (reference, eventsPerStream, isNewData, context) =>
                     {
                         double sec = timePeriod.EvaluateAsSeconds(eventsPerStream, isNewData, context);
                         return((long)Math.Round(sec * 1000d));
                     },
                 };
                 return(new ExprOptionalConstant(eval, null));
             }
         }
         // has-month
         else
         {
             // has-month and constant
             if (exprNode.IsConstantResult)
             {
                 ExprTimePeriodEvalDeltaConst timerPeriodConst = timePeriod.ConstEvaluator(null);
                 IntervalDeltaExprEvaluator   eval             = new ProxyIntervalDeltaExprEvaluator
                 {
                     ProcEvaluate = (reference, eventsPerStream, isNewData, context) =>
                     {
                         return
                             (timerPeriodConst
                              .DeltaMillisecondsAdd
                                  (reference));
                     },
                 };
                 return(new ExprOptionalConstant(eval, null));
             }
             // has-month and not constant
             else
             {
                 ExprTimePeriodEvalDeltaNonConst timerPeriodNonConst = timePeriod.NonconstEvaluator();
                 IntervalDeltaExprEvaluator      eval = new ProxyIntervalDeltaExprEvaluator
                 {
                     ProcEvaluate = (reference, eventsPerStream, isNewData, context) => timerPeriodNonConst.DeltaMillisecondsAdd(
                         reference, eventsPerStream, isNewData, context),
                 };
                 return(new ExprOptionalConstant(eval, null));
             }
         }
     }
     else if (ExprNodeUtility.IsConstantValueExpr(exprNode))
     {
         var  constantNode = (ExprConstantNode)exprNode;
         long l            = constantNode.GetConstantValue(null).AsLong();
         IntervalDeltaExprEvaluator eval = new ProxyIntervalDeltaExprEvaluator
         {
             ProcEvaluate = (reference, eventsPerStream, isNewData, context) => l,
         };
         return(new ExprOptionalConstant(eval, l));
     }
     else
     {
         var evaluator = exprNode.ExprEvaluator;
         IntervalDeltaExprEvaluator eval = new ProxyIntervalDeltaExprEvaluator
         {
             ProcEvaluate = (reference, eventsPerStream, isNewData, context) => evaluator.Evaluate(
                 new EvaluateParams(eventsPerStream, isNewData, context)).AsLong(),
         };
         return(new ExprOptionalConstant(eval, null));
     }
 }
Example #7
0
        public override void Update(EventBean[] newData, EventBean[] oldData)
        {
            using (Instrument.With(
                       i => i.QViewProcessIRStream(this, _factory.ViewName, newData, oldData),
                       i => i.AViewProcessIRStream()))
            {
                // we don't care about removed data from a prior view
                if ((newData == null) || (newData.Length == 0))
                {
                    return;
                }

                // If we have an empty window about to be filled for the first time, addSchedule a callback
                bool removeSchedule = false;
                bool addSchedule    = false;
                long timestamp      = AgentInstanceContext.StatementContext.SchedulingService.Time;

                if (CurrentBatch.IsNotEmpty())
                {
                    // check if we need to reschedule
                    long callbackTime = timestamp + _timeDeltaComputation.DeltaMillisecondsAdd(timestamp);;
                    if (callbackTime != CallbackScheduledTime)
                    {
                        removeSchedule = true;
                        addSchedule    = true;
                    }
                }
                else
                {
                    addSchedule = true;
                }

                if (removeSchedule)
                {
                    AgentInstanceContext.StatementContext.SchedulingService.Remove(Handle, ScheduleSlot);
                }
                if (addSchedule)
                {
                    var msecIntervalSize = _timeDeltaComputation.DeltaMillisecondsAdd(timestamp);
                    AgentInstanceContext.StatementContext.SchedulingService.Add(msecIntervalSize, Handle, ScheduleSlot);
                    CallbackScheduledTime = msecIntervalSize + timestamp;
                }

                // add data points to the window
                foreach (EventBean newEvent in newData)
                {
                    CurrentBatch.Add(newEvent);
                }

                // forward insert stream to child views
                if (ViewUpdatedCollection != null)
                {
                    ViewUpdatedCollection.Update(newData, null);
                }

                // Update child views
                if (HasViews)
                {
                    Instrument.With(
                        i => i.QViewIndicate(this, _factory.ViewName, newData, null),
                        i => i.AViewIndicate(),
                        () => UpdateChildren(newData, null));
                }
            }
        }
Example #8
0
        public override void Update(EventBean[] newData, EventBean[] oldData)
        {
            using (Instrument.With(
                       i => i.QViewProcessIRStream(this, _timeOrderViewFactory.ViewName, newData, oldData),
                       i => i.AViewProcessIRStream()))
            {
                EventBean[] postOldEventsArray = null;

                // Remove old data
                if (oldData != null)
                {
                    for (int i = 0; i < oldData.Length; i++)
                    {
                        var oldDataItem = oldData[i];
                        var sortValues  = GetTimestamp(oldDataItem);
                        var result      = CollectionUtil.RemoveEventByKeyLazyListMap(sortValues, oldDataItem, _sortedEvents);
                        if (result)
                        {
                            _eventCount--;
                            if (postOldEventsArray == null)
                            {
                                postOldEventsArray = oldData;
                            }
                            else
                            {
                                postOldEventsArray = CollectionUtil.AddArrayWithSetSemantics(
                                    postOldEventsArray, oldData);
                            }
                            InternalHandleRemoved(sortValues, oldDataItem);
                        }
                    }
                }

                if ((newData != null) && (newData.Length > 0))
                {
                    // figure out the current tail time
                    long engineTime     = _agentInstanceContext.StatementContext.SchedulingService.Time;
                    long windowTailTime = engineTime - _timeDeltaComputation.DeltaMillisecondsAdd(engineTime) + 1;
                    long oldestEvent    = long.MaxValue;
                    if (_sortedEvents.IsNotEmpty())
                    {
                        oldestEvent = (long)_sortedEvents.Keys.First();
                    }
                    bool addedOlderEvent = false;

                    // add events or post events as remove stream if already older then tail time
                    List <EventBean> postOldEvents = null;
                    for (int i = 0; i < newData.Length; i++)
                    {
                        // get timestamp of event
                        var newEvent  = newData[i];
                        var timestamp = GetTimestamp(newEvent);

                        // if the event timestamp indicates its older then the tail of the window, release it
                        if (timestamp < windowTailTime)
                        {
                            if (postOldEvents == null)
                            {
                                postOldEvents = new List <EventBean>(2);
                            }
                            postOldEvents.Add(newEvent);
                        }
                        else
                        {
                            if (timestamp < oldestEvent)
                            {
                                addedOlderEvent = true;
                                oldestEvent     = timestamp.Value;
                            }

                            // add to list
                            CollectionUtil.AddEventByKeyLazyListMapBack(timestamp, newEvent, _sortedEvents);
                            _eventCount++;
                            InternalHandleAdd(timestamp, newEvent);
                        }
                    }

                    // If we do have data, check the callback
                    if (_sortedEvents.IsNotEmpty())
                    {
                        // If we haven't scheduled a callback yet, schedule it now
                        if (!_isCallbackScheduled)
                        {
                            long callbackWait = oldestEvent - windowTailTime + 1;
                            _agentInstanceContext.StatementContext.SchedulingService.Add(
                                callbackWait, _handle, _scheduleSlot);
                            _isCallbackScheduled = true;
                        }
                        else
                        {
                            // We may need to reschedule, and older event may have been added
                            if (addedOlderEvent)
                            {
                                oldestEvent = (long)_sortedEvents.Keys.First();
                                long callbackWait = oldestEvent - windowTailTime + 1;
                                _agentInstanceContext.StatementContext.SchedulingService.Remove(_handle, _scheduleSlot);
                                _agentInstanceContext.StatementContext.SchedulingService.Add(
                                    callbackWait, _handle, _scheduleSlot);
                                _isCallbackScheduled = true;
                            }
                        }
                    }

                    if (postOldEvents != null)
                    {
                        postOldEventsArray = postOldEvents.ToArray();
                    }

                    if (_optionalSortedRandomAccess != null)
                    {
                        _optionalSortedRandomAccess.Refresh(_sortedEvents, _eventCount, _eventCount);
                    }
                }

                // Update child views
                if (HasViews)
                {
                    Instrument.With(
                        i => i.QViewIndicate(this, _timeOrderViewFactory.ViewName, newData, postOldEventsArray),
                        i => i.AViewIndicate(),
                        () => UpdateChildren(newData, postOldEventsArray));
                }
            }
        }