Exemple #1
0
        public static FireAndForgetProcessor ValidateResolveProcessor(StreamSpecCompiled streamSpec, EPServicesContext services)

        {
            // resolve processor
            string processorName;

            if (streamSpec is NamedWindowConsumerStreamSpec)
            {
                NamedWindowConsumerStreamSpec namedSpec = (NamedWindowConsumerStreamSpec)streamSpec;
                processorName = namedSpec.WindowName;
            }
            else
            {
                TableQueryStreamSpec tableSpec = (TableQueryStreamSpec)streamSpec;
                processorName = tableSpec.TableName;
            }

            // get processor instance
            TableMetadata tableMetadata = services.TableService.GetTableMetadata(processorName);

            if (tableMetadata != null)
            {
                return(new FireAndForgetProcessorTable(services.TableService, tableMetadata));
            }
            else
            {
                NamedWindowProcessor nwprocessor = services.NamedWindowService.GetProcessor(processorName);
                if (nwprocessor == null)
                {
                    throw new ExprValidationException("A table or named window by name '" + processorName + "' does not exist");
                }
                return(new FireAndForgetProcessorNamedWindow(nwprocessor));
            }
        }
Exemple #2
0
        internal static void ValidateStatementForContext(String contextName, ContextControllerStatementBase statement, StatementSpecCompiledAnalyzerResult streamAnalysis, ICollection <EventType> itemEventTypes, NamedWindowService namedWindowService)
        {
            IList <FilterSpecCompiled> filters = streamAnalysis.Filters;

            bool isCreateWindow = statement.StatementSpec.CreateWindowDesc != null;

            // if no create-window: at least one of the filters must match one of the filters specified by the context
            if (!isCreateWindow)
            {
                foreach (FilterSpecCompiled filter in filters)
                {
                    foreach (EventType itemEventType in itemEventTypes)
                    {
                        EventType stmtFilterType = filter.FilterForEventType;
                        if (stmtFilterType == itemEventType)
                        {
                            return;
                        }
                        if (EventTypeUtility.IsTypeOrSubTypeOf(stmtFilterType, itemEventType))
                        {
                            return;
                        }

                        NamedWindowProcessor processor = namedWindowService.GetProcessor(stmtFilterType.Name);
                        if (processor != null && processor.ContextName != null && processor.ContextName.Equals(contextName))
                        {
                            return;
                        }
                    }
                }

                if (filters.IsNotEmpty())
                {
                    throw new ExprValidationException(GetTypeValidationMessage(contextName, filters[0].FilterForEventType.Name));
                }
                return;
            }

            // validate create-window with column definition: not allowed, requires typed
            if (statement.StatementSpec.CreateWindowDesc.Columns != null &&
                statement.StatementSpec.CreateWindowDesc.Columns.Count > 0)
            {
                throw new ExprValidationException("Segmented context '" + contextName +
                                                  "' requires that named windows are associated to an existing event type and that the event type is listed among the partitions defined by the create-context statement");
            }

            // validate create-window declared type
            String declaredAsName = statement.StatementSpec.CreateWindowDesc.AsEventTypeName;

            if (declaredAsName != null)
            {
                if (itemEventTypes.Any(itemEventType => itemEventType.Name == declaredAsName))
                {
                    return;
                }

                throw new ExprValidationException(GetTypeValidationMessage(contextName, declaredAsName));
            }
        }
 public StatementAgentInstanceFactoryCreateIndex(EPServicesContext services, CreateIndexDesc spec, Viewable finalView, NamedWindowProcessor namedWindowProcessor, string tableName)
 {
     this.services             = services;
     this.spec                 = spec;
     this.finalView            = finalView;
     this.namedWindowProcessor = namedWindowProcessor;
     this.tableName            = tableName;
 }
 public ViewableActivatorNamedWindow(
     NamedWindowProcessor processor,
     IList <ExprNode> filterExpressions,
     PropertyEvaluator optPropertyEvaluator)
 {
     _processor            = processor;
     _filterExpressions    = filterExpressions;
     _optPropertyEvaluator = optPropertyEvaluator;
 }
        public ViewableActivator CreateNamedWindow(
            NamedWindowProcessor processor,
            IList <ExprNode> filterExpressions,
            PropertyEvaluator optPropertyEvaluator)
        {
            if (ProcCreateNamedWindow == null)
            {
                throw new NotSupportedException();
            }

            return(ProcCreateNamedWindow.Invoke(
                       processor,
                       filterExpressions,
                       optPropertyEvaluator));
        }
Exemple #6
0
        public ViewableActivator CreateNamedWindow(
            NamedWindowProcessor processor,
            NamedWindowConsumerStreamSpec streamSpec,
            StatementContext statementContext)
        {
            if (ProcCreateNamedWindow == null)
            {
                throw new NotSupportedException();
            }

            return(ProcCreateNamedWindow.Invoke(
                       processor,
                       streamSpec,
                       statementContext));
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="StatementAgentInstanceFactoryCreateIndex"/> class.
 /// </summary>
 /// <param name="services">The <see cref="EPServicesContext"/></param>
 /// <param name="spec">The <see cref="CreateIndexDesc"/></param>
 /// <param name="finalView">The <see cref="Viewable"/></param>
 /// <param name="namedWindowProcessor">The <see cref="NamedWindowProcessor"/></param>
 /// <param name="tableName">The <see cref="string"/></param>
 /// <param name="contextName">The <see cref="string"/></param>
 /// <param name="explicitIndexDesc">The <see cref="QueryPlanIndexItem"/></param>
 public StatementAgentInstanceFactoryCreateIndex(
     EPServicesContext services,
     CreateIndexDesc spec,
     Viewable finalView,
     NamedWindowProcessor namedWindowProcessor,
     string tableName,
     string contextName,
     QueryPlanIndexItem explicitIndexDesc)
 {
     _services             = services;
     _spec                 = spec;
     _finalView            = finalView;
     _namedWindowProcessor = namedWindowProcessor;
     _tableName            = tableName;
     _contextName          = contextName;
     _explicitIndexDesc    = explicitIndexDesc;
 }
        public StatementAgentInstanceFactoryOnTriggerNamedWindow(StatementContext statementContext, StatementSpecCompiled statementSpec, EPServicesContext services, ViewableActivator activator, SubSelectStrategyCollection subSelectStrategyCollection, ResultSetProcessorFactoryDesc resultSetProcessorPrototype, ExprNode validatedJoin, ResultSetProcessorFactoryDesc outputResultSetProcessorPrototype, NamedWindowOnExprFactory onExprFactory, OutputProcessViewFactory outputProcessViewFactory, EventType activatorResultEventType, NamedWindowProcessor processor, IList <StopCallback> stopCallbacks)
            : base(statementContext, statementSpec, services, activator, subSelectStrategyCollection)
        {
            _resultSetProcessorPrototype       = resultSetProcessorPrototype;
            _outputResultSetProcessorPrototype = outputResultSetProcessorPrototype;
            _onExprFactory            = onExprFactory;
            _outputProcessViewFactory = outputProcessViewFactory;
            _processor = processor;

            IndexHintPair   pair            = GetIndexHintPair(statementContext, statementSpec);
            IndexHint       indexHint       = pair.IndexHint;
            ExcludePlanHint excludePlanHint = pair.ExcludePlanHint;

            _queryPlan = SubordinateQueryPlanner.PlanOnExpression(
                validatedJoin, activatorResultEventType, indexHint, processor.IsEnableSubqueryIndexShare, -1, excludePlanHint,
                processor.IsVirtualDataWindow, processor.EventTableIndexMetadataRepo, processor.NamedWindowType,
                processor.OptionalUniqueKeyProps, false, statementContext.StatementName, statementContext.StatementId, statementContext.Annotations);
            if (_queryPlan.IndexDescs != null)
            {
                SubordinateQueryPlannerUtil.AddIndexMetaAndRef(_queryPlan.IndexDescs, processor.EventTableIndexMetadataRepo, statementContext.StatementName);
                stopCallbacks.Add(new ProxyStopCallback(() =>
                {
                    for (int i = 0; i < _queryPlan.IndexDescs.Length; i++)
                    {
                        bool last = processor.EventTableIndexMetadataRepo.RemoveIndexReference(_queryPlan.IndexDescs[i].IndexMultiKey, statementContext.StatementName);
                        if (last)
                        {
                            processor.EventTableIndexMetadataRepo.RemoveIndex(_queryPlan.IndexDescs[i].IndexMultiKey);
                            processor.RemoveAllInstanceIndexes(_queryPlan.IndexDescs[i].IndexMultiKey);
                        }
                    }
                }));
            }
            SubordinateQueryPlannerUtil.QueryPlanLogOnExpr(processor.RootView.IsQueryPlanLogging, NamedWindowRootView.QueryPlanLog,
                                                           _queryPlan, statementContext.Annotations, statementContext.EngineImportService);
        }
 public ViewableActivator CreateNamedWindow(NamedWindowProcessor processor, NamedWindowConsumerStreamSpec streamSpec, StatementContext statementContext)
 {
     return(new ViewableActivatorNamedWindow(processor, streamSpec.FilterExpressions, streamSpec.OptPropertyEvaluator));
 }
        private void Preload(
            EPServicesContext services,
            EventTable[] eventIndex,
            Viewable subselectView,
            AgentInstanceContext agentInstanceContext)
        {
            if (_subSelectHolder.StreamSpecCompiled is NamedWindowConsumerStreamSpec)
            {
                var namedSpec = (NamedWindowConsumerStreamSpec)_subSelectHolder.StreamSpecCompiled;
                NamedWindowProcessor processor = services.NamedWindowMgmtService.GetProcessor(namedSpec.WindowName);
                if (processor == null)
                {
                    throw new EPRuntimeException("Failed to find named window by name '" + namedSpec.WindowName + "'");
                }

                var processorInstance = processor.GetProcessorInstance(agentInstanceContext);
                if (processorInstance == null)
                {
                    throw new EPException(
                              "Named window '" + namedSpec.WindowName + "' is associated to context '" + processor.ContextName +
                              "' that is not available for querying");
                }
                var consumerView = processorInstance.TailViewInstance;

                // preload view for stream
                ICollection <EventBean> eventsInWindow;
                if (namedSpec.FilterExpressions != null && !namedSpec.FilterExpressions.IsEmpty())
                {
                    try
                    {
                        var types = new StreamTypeServiceImpl(
                            consumerView.EventType, consumerView.EventType.Name, false, services.EngineURI);
                        var tagged             = new LinkedHashMap <string, Pair <EventType, string> >();
                        var filterSpecCompiled = FilterSpecCompiler.MakeFilterSpec(
                            types.EventTypes[0], types.StreamNames[0],
                            namedSpec.FilterExpressions, null, tagged, tagged, types, null,
                            agentInstanceContext.StatementContext, Collections.SingletonSet(0));
                        var snapshot = consumerView.SnapshotNoLock(
                            filterSpecCompiled, agentInstanceContext.StatementContext.Annotations);
                        eventsInWindow = new List <EventBean>(snapshot.Count);
                        ExprNodeUtility.ApplyFilterExpressions(
                            snapshot, namedSpec.FilterExpressions, agentInstanceContext, eventsInWindow);
                    }
                    catch (Exception ex)
                    {
                        Log.Warn("Unexpected exception analyzing filter paths: " + ex.Message, ex);
                        eventsInWindow = new List <EventBean>();
                        ExprNodeUtility.ApplyFilterExpressionsIterable(
                            consumerView, namedSpec.FilterExpressions, agentInstanceContext, eventsInWindow);
                    }
                }
                else
                {
                    eventsInWindow = new List <EventBean>();
                    for (IEnumerator <EventBean> it = consumerView.GetEnumerator(); it.MoveNext();)
                    {
                        eventsInWindow.Add(it.Current);
                    }
                }
                EventBean[] newEvents = eventsInWindow.ToArray();
                if (subselectView != null)
                {
                    ((View)subselectView).Update(newEvents, null);
                }
                if (eventIndex != null)
                {
                    foreach (var table in eventIndex)
                    {
                        table.Add(newEvents); // fill index
                    }
                }
            }
            else // preload from the data window that sit on top
            {
                // Start up event table from the iterator
                IEnumerator <EventBean> it = subselectView.GetEnumerator();
                if (it.MoveNext())
                {
                    var preloadEvents = new List <EventBean>();
                    do
                    {
                        preloadEvents.Add(it.Current);
                    } while (it.MoveNext());

                    if (eventIndex != null)
                    {
                        foreach (var table in eventIndex)
                        {
                            table.Add(preloadEvents.ToArray());
                        }
                    }
                }
            }
        }
        protected override StatementAgentInstanceFactoryResult NewContextInternal(AgentInstanceContext agentInstanceContext, bool isRecoveringResilient)
        {
            IList <StopCallback> stopCallbacks = new List <StopCallback>(2);

            Viewable finalView;
            var      viewableActivationResult = new ViewableActivationResult[_eventStreamParentViewableActivators.Length];
            IDictionary <ExprSubselectNode, SubSelectStrategyHolder> subselectStrategies;
            AggregationService aggregationService;

            Viewable[] streamViews;
            Viewable[] eventStreamParentViewable;
            Viewable[] topViews;
            IDictionary <ExprPriorNode, ExprPriorEvalStrategy>             priorNodeStrategies;
            IDictionary <ExprPreviousNode, ExprPreviousEvalStrategy>       previousNodeStrategies;
            IDictionary <ExprTableAccessNode, ExprTableAccessEvalStrategy> tableAccessStrategies;
            RegexExprPreviousEvalStrategy         regexExprPreviousEvalStrategy = null;
            IList <StatementAgentInstancePreload> preloadList = new List <StatementAgentInstancePreload>();

            EvalRootState[] patternRoots;
            StatementAgentInstancePostLoad postLoadJoin = null;
            var suppressSameEventMatches = false;
            var discardPartialsOnMatch   = false;
            EvalRootMatchRemover evalRootMatchRemover = null;

            try {
                // create root viewables
                eventStreamParentViewable = new Viewable[_numStreams];
                patternRoots = new EvalRootState[_numStreams];

                for (var stream = 0; stream < _eventStreamParentViewableActivators.Length; stream++)
                {
                    var activationResult = _eventStreamParentViewableActivators[stream].Activate(agentInstanceContext, false, isRecoveringResilient);
                    viewableActivationResult[stream] = activationResult;
                    stopCallbacks.Add(activationResult.StopCallback);
                    suppressSameEventMatches = activationResult.IsSuppressSameEventMatches;
                    discardPartialsOnMatch   = activationResult.IsDiscardPartialsOnMatch;

                    // add stop callback for any stream-level viewable when applicable
                    if (activationResult.Viewable is StopCallback)
                    {
                        stopCallbacks.Add((StopCallback)activationResult.Viewable);
                    }

                    eventStreamParentViewable[stream] = activationResult.Viewable;
                    patternRoots[stream] = activationResult.OptionalPatternRoot;
                    if (stream == 0)
                    {
                        evalRootMatchRemover = activationResult.OptEvalRootMatchRemover;
                    }

                    if (activationResult.OptionalLock != null)
                    {
                        agentInstanceContext.EpStatementAgentInstanceHandle.StatementAgentInstanceLock = activationResult.OptionalLock;
                        _statementContext.DefaultAgentInstanceLock = activationResult.OptionalLock;
                    }
                }

                // compile view factories adding "prior" as necessary
                var viewFactoryChains = new IList <ViewFactory> [_numStreams];
                for (var i = 0; i < _numStreams; i++)
                {
                    var viewFactoryChain = _unmaterializedViewChain[i].FactoryChain;

                    // add "prior" view factory
                    var hasPrior = _viewResourceDelegate.PerStream[i].PriorRequests != null && !_viewResourceDelegate.PerStream[i].PriorRequests.IsEmpty();
                    if (hasPrior)
                    {
                        var priorEventViewFactory = EPStatementStartMethodHelperPrior.GetPriorEventViewFactory(
                            agentInstanceContext.StatementContext, i, viewFactoryChain.IsEmpty(), false, -1);
                        viewFactoryChain = new List <ViewFactory>(viewFactoryChain);
                        viewFactoryChain.Add(priorEventViewFactory);
                    }
                    viewFactoryChains[i] = viewFactoryChain;
                }

                // create view factory chain context: holds stream-specific services
                var viewFactoryChainContexts = new AgentInstanceViewFactoryChainContext[_numStreams];
                for (var i = 0; i < _numStreams; i++)
                {
                    viewFactoryChainContexts[i] = AgentInstanceViewFactoryChainContext.Create(viewFactoryChains[i], agentInstanceContext, _viewResourceDelegate.PerStream[i]);
                }

                // handle "prior" nodes and their strategies
                priorNodeStrategies = EPStatementStartMethodHelperPrior.CompilePriorNodeStrategies(_viewResourceDelegate, viewFactoryChainContexts);

                // handle "previous" nodes and their strategies
                previousNodeStrategies = EPStatementStartMethodHelperPrevious.CompilePreviousNodeStrategies(_viewResourceDelegate, viewFactoryChainContexts);

                // materialize views
                streamViews = new Viewable[_numStreams];
                topViews    = new Viewable[_numStreams];
                for (var i = 0; i < _numStreams; i++)
                {
                    var hasPreviousNode = _viewResourceDelegate.PerStream[i].PreviousRequests != null && !_viewResourceDelegate.PerStream[i].PreviousRequests.IsEmpty();

                    var createResult = _services.ViewService.CreateViews(eventStreamParentViewable[i], viewFactoryChains[i], viewFactoryChainContexts[i], hasPreviousNode);
                    topViews[i]    = createResult.TopViewable;
                    streamViews[i] = createResult.FinalViewable;

                    var isReuseableView = _eventStreamParentViewableActivators[i] is ViewableActivatorStreamReuseView;
                    if (isReuseableView)
                    {
                        var viewsCreated = createResult.NewViews;
                        var stopCallback = new ProxyStopCallback(() => {
                            ViewServiceHelper.RemoveFirstUnsharedView(viewsCreated);
                        });
                        stopCallbacks.Add(stopCallback);
                    }

                    // add views to stop callback if applicable
                    AddViewStopCallback(stopCallbacks, createResult.NewViews);
                }

                // determine match-recognize "previous"-node strategy (none if not present, or one handling and number of nodes)
                var matchRecognize = EventRowRegexHelper.RecursiveFindRegexService(topViews[0]);
                if (matchRecognize != null)
                {
                    regexExprPreviousEvalStrategy = matchRecognize.PreviousEvaluationStrategy;
                    stopCallbacks.Add(new ProxyStopCallback(matchRecognize.Stop));
                }

                // start subselects
                subselectStrategies = EPStatementStartMethodHelperSubselect.StartSubselects(_services, _subSelectStrategyCollection, agentInstanceContext, stopCallbacks, isRecoveringResilient);

                // plan table access
                tableAccessStrategies = EPStatementStartMethodHelperTableAccess.AttachTableAccess(_services, agentInstanceContext, _statementSpec.TableNodes);

                // obtain result set processor and aggregation services
                var processorPair      = EPStatementStartMethodHelperUtil.StartResultSetAndAggregation(_resultSetProcessorFactoryDesc, agentInstanceContext, false, null);
                var resultSetProcessor = processorPair.First;
                aggregationService = processorPair.Second;
                stopCallbacks.Add(aggregationService);
                stopCallbacks.Add(resultSetProcessor);

                // for just 1 event stream without joins, handle the one-table process separately.
                JoinPreloadMethod   joinPreloadMethod;
                JoinSetComposerDesc joinSetComposer = null;
                if (streamViews.Length == 1)
                {
                    finalView         = HandleSimpleSelect(streamViews[0], resultSetProcessor, agentInstanceContext, evalRootMatchRemover, suppressSameEventMatches, discardPartialsOnMatch);
                    joinPreloadMethod = null;
                }
                else
                {
                    var joinPlanResult = HandleJoin(_typeService.StreamNames, streamViews, resultSetProcessor,
                                                    agentInstanceContext, stopCallbacks, _joinAnalysisResult, isRecoveringResilient);
                    finalView         = joinPlanResult.Viewable;
                    joinPreloadMethod = joinPlanResult.PreloadMethod;
                    joinSetComposer   = joinPlanResult.JoinSetComposerDesc;
                }

                // for stoppable final views, add callback
                if (finalView is StopCallback)
                {
                    stopCallbacks.Add((StopCallback)finalView);
                }

                // Replay any named window data, for later consumers of named data windows
                if (_services.EventTableIndexService.AllowInitIndex(isRecoveringResilient))
                {
                    var hasNamedWindow             = false;
                    var namedWindowPostloadFilters = new FilterSpecCompiled[_statementSpec.StreamSpecs.Length];
                    var namedWindowTailViews       = new NamedWindowTailViewInstance[_statementSpec.StreamSpecs.Length];
                    var namedWindowFilters         = new IList <ExprNode> [_statementSpec.StreamSpecs.Length];

                    for (var i = 0; i < _statementSpec.StreamSpecs.Length; i++)
                    {
                        var streamNum  = i;
                        var streamSpec = _statementSpec.StreamSpecs[i];

                        if (streamSpec is NamedWindowConsumerStreamSpec)
                        {
                            hasNamedWindow = true;
                            var namedSpec = (NamedWindowConsumerStreamSpec)streamSpec;
                            NamedWindowProcessor processor = _services.NamedWindowMgmtService.GetProcessor(namedSpec.WindowName);
                            var processorInstance          = processor.GetProcessorInstance(agentInstanceContext);
                            if (processorInstance != null)
                            {
                                var consumerView = processorInstance.TailViewInstance;
                                namedWindowTailViews[i] = consumerView;
                                var view = (NamedWindowConsumerView)viewableActivationResult[i].Viewable;

                                // determine preload/postload filter for index access
                                if (!namedSpec.FilterExpressions.IsEmpty())
                                {
                                    namedWindowFilters[streamNum] = namedSpec.FilterExpressions;
                                    try {
                                        var streamName = streamSpec.OptionalStreamName != null ? streamSpec.OptionalStreamName : consumerView.EventType.Name;
                                        var types      = new StreamTypeServiceImpl(consumerView.EventType, streamName, false, _services.EngineURI);
                                        var tagged     = new LinkedHashMap <string, Pair <EventType, string> >();
                                        namedWindowPostloadFilters[i] = FilterSpecCompiler.MakeFilterSpec(types.EventTypes[0], types.StreamNames[0],
                                                                                                          namedSpec.FilterExpressions, null, tagged, tagged, types, null, _statementContext, Collections.SingletonSet(0));
                                    }
                                    catch (Exception ex) {
                                        Log.Warn("Unexpected exception analyzing filter paths: " + ex.Message, ex);
                                    }
                                }

                                // preload view for stream unless the expiry policy is batch window
                                var preload = !consumerView.TailView.IsParentBatchWindow && consumerView.HasFirst();
                                if (preload)
                                {
                                    if (isRecoveringResilient && _numStreams < 2)
                                    {
                                        preload = false;
                                    }
                                }
                                if (preload)
                                {
                                    var yesRecoveringResilient = isRecoveringResilient;
                                    var preloadFilterSpec      = namedWindowPostloadFilters[i];
                                    preloadList.Add(new ProxyStatementAgentInstancePreload()
                                    {
                                        ProcExecutePreload = () => {
                                            var snapshot       = consumerView.Snapshot(preloadFilterSpec, _statementContext.Annotations);
                                            var eventsInWindow = new List <EventBean>(snapshot.Count);
                                            ExprNodeUtility.ApplyFilterExpressionsIterable(snapshot, namedSpec.FilterExpressions, agentInstanceContext, eventsInWindow);
                                            EventBean[] newEvents = eventsInWindow.ToArray();
                                            view.Update(newEvents, null);
                                            if (!yesRecoveringResilient && joinPreloadMethod != null && !joinPreloadMethod.IsPreloading && agentInstanceContext.EpStatementAgentInstanceHandle.OptionalDispatchable != null)
                                            {
                                                agentInstanceContext.EpStatementAgentInstanceHandle.OptionalDispatchable.Execute();
                                            }
                                        },
                                    });
                                }
                            }
                            else
                            {
                                Log.Info("Named window access is out-of-context, the named window '" + namedSpec.WindowName + "' has been declared for a different context then the current statement, the aggregation and join state will not be initialized for statement expression [" + _statementContext.Expression + "]");
                            }

                            preloadList.Add(new ProxyStatementAgentInstancePreload()
                            {
                                ProcExecutePreload = () => {
                                    // in a join, preload indexes, if any
                                    if (joinPreloadMethod != null)
                                    {
                                        joinPreloadMethod.PreloadFromBuffer(streamNum);
                                    }
                                    else
                                    {
                                        if (agentInstanceContext.EpStatementAgentInstanceHandle.OptionalDispatchable != null)
                                        {
                                            agentInstanceContext.EpStatementAgentInstanceHandle.OptionalDispatchable.Execute();
                                        }
                                    }
                                },
                            });
                        }
                    }

                    // last, for aggregation we need to send the current join results to the result set processor
                    if ((hasNamedWindow) && (joinPreloadMethod != null) && (!isRecoveringResilient) && _resultSetProcessorFactoryDesc.ResultSetProcessorFactory.HasAggregation)
                    {
                        preloadList.Add(new ProxyStatementAgentInstancePreload()
                        {
                            ProcExecutePreload = () => {
                                joinPreloadMethod.PreloadAggregation(resultSetProcessor);
                            },
                        });
                    }

                    if (isRecoveringResilient)
                    {
                        postLoadJoin = new StatementAgentInstancePostLoadSelect(streamViews, joinSetComposer, namedWindowTailViews, namedWindowPostloadFilters, namedWindowFilters, _statementContext.Annotations, agentInstanceContext);
                    }
                    else if (joinSetComposer != null)
                    {
                        postLoadJoin = new StatementAgentInstancePostLoadIndexVisiting(joinSetComposer.JoinSetComposer);
                    }
                }
            }
            catch (Exception) {
                var stopCallback = StatementAgentInstanceUtil.GetStopCallback(stopCallbacks, agentInstanceContext);
                StatementAgentInstanceUtil.StopSafe(stopCallback, _statementContext);
                if (InstrumentationHelper.ENABLED)
                {
                    InstrumentationHelper.Get().AContextPartitionAllocate();
                }
                throw;
            }

            var selectResult = new StatementAgentInstanceFactorySelectResult(finalView, null, agentInstanceContext, aggregationService, subselectStrategies, priorNodeStrategies, previousNodeStrategies, regexExprPreviousEvalStrategy, tableAccessStrategies, preloadList, patternRoots, postLoadJoin, topViews, eventStreamParentViewable, viewableActivationResult);

            if (_statementContext.StatementExtensionServicesContext != null)
            {
                _statementContext.StatementExtensionServicesContext.ContributeStopCallback(selectResult, stopCallbacks);
            }
            var stopCallbackX = StatementAgentInstanceUtil.GetStopCallback(stopCallbacks, agentInstanceContext);

            selectResult.StopCallback = stopCallbackX;

            return(selectResult);
        }
Exemple #12
0
        protected override StatementAgentInstanceFactoryResult NewContextInternal(AgentInstanceContext agentInstanceContext, bool isRecoveringResilient)
        {
            var stopCallbacks = new List <StopCallback>();

            String   windowName = _statementSpec.CreateWindowDesc.WindowName;
            Viewable finalView;
            Viewable eventStreamParentViewable;
            StatementAgentInstancePostLoad postLoad;
            Viewable topView;
            NamedWindowProcessorInstance processorInstance;
            ViewableActivationResult     viewableActivationResult;

            try
            {
                // Register interest
                viewableActivationResult = _activator.Activate(agentInstanceContext, false, isRecoveringResilient);
                stopCallbacks.Add(viewableActivationResult.StopCallback);
                eventStreamParentViewable = viewableActivationResult.Viewable;

                // Obtain processor for this named window
                var processor = _services.NamedWindowMgmtService.GetProcessor(windowName);
                if (processor == null)
                {
                    throw new Exception("Failed to obtain named window processor for named window '" + windowName + "'");
                }

                // Allocate processor instance
                processorInstance = processor.AddInstance(agentInstanceContext);
                var rootView = processorInstance.RootViewInstance;
                eventStreamParentViewable.AddView(rootView);

                // Materialize views
                var viewFactoryChainContext = new AgentInstanceViewFactoryChainContext(agentInstanceContext, true, null, null);
                var createResult            = _services.ViewService.CreateViews(rootView, _unmaterializedViewChain.FactoryChain, viewFactoryChainContext, false);
                topView   = createResult.TopViewable;
                finalView = createResult.FinalViewable;

                // add views to stop callback if applicable
                StatementAgentInstanceFactorySelect.AddViewStopCallback(stopCallbacks, createResult.NewViews);

                // If this is a virtual data window implementation, bind it to the context for easy lookup
                StopCallback envStopCallback = null;
                if (finalView is VirtualDWView)
                {
                    var objectName    = "/virtualdw/" + windowName;
                    var virtualDWView = (VirtualDWView)finalView;
                    _services.EngineEnvContext.Bind(objectName, virtualDWView.VirtualDataWindow);
                    envStopCallback = new ProxyStopCallback(() =>
                    {
                        virtualDWView.Dispose();
                        _services.EngineEnvContext.Unbind(objectName);
                    });
                }
                StopCallback environmentStopCallback = envStopCallback;

                // Only if we are context-allocated: destroy the instance
                var contextName        = processor.ContextName;
                var agentInstanceId    = agentInstanceContext.AgentInstanceId;
                var allInOneStopMethod = new ProxyStopCallback(() =>
                {
                    var windowNameX = _statementSpec.CreateWindowDesc.WindowName;
                    var processorX  = _services.NamedWindowMgmtService.GetProcessor(windowNameX);
                    if (processorX == null)
                    {
                        Log.Warn("Named window processor by name '" + windowNameX + "' has not been found");
                    }
                    else
                    {
                        NamedWindowProcessorInstance instance =
                            processorX.GetProcessorInstanceAllowUnpartitioned(agentInstanceId);
                        if (instance != null)
                        {
                            if (contextName != null)
                            {
                                instance.Dispose();
                            }
                            else
                            {
                                instance.Stop();
                            }
                        }
                    }
                    if (environmentStopCallback != null)
                    {
                        environmentStopCallback.Stop();
                    }
                });

                stopCallbacks.Add(allInOneStopMethod);

                // Attach tail view
                NamedWindowTailViewInstance tailView = processorInstance.TailViewInstance;
                finalView.AddView(tailView);
                finalView = tailView;

                // obtain result set processor
                ResultSetProcessor resultSetProcessor = EPStatementStartMethodHelperAssignExpr.GetAssignResultSetProcessor(agentInstanceContext, _resultSetProcessorPrototype, false, null, false);

                // Attach output view
                View outputView = _outputProcessViewFactory.MakeView(resultSetProcessor, agentInstanceContext);
                finalView.AddView(outputView);
                finalView = outputView;

                // obtain post load
                postLoad = processorInstance.PostLoad;

                // Handle insert case
                if (_statementSpec.CreateWindowDesc.IsInsert && !_isRecoveringStatement)
                {
                    String insertFromWindow = _statementSpec.CreateWindowDesc.InsertFromWindow;
                    NamedWindowProcessor         namedWindowProcessor  = _services.NamedWindowMgmtService.GetProcessor(insertFromWindow);
                    NamedWindowProcessorInstance sourceWindowInstances = namedWindowProcessor.GetProcessorInstance(agentInstanceContext);
                    IList <EventBean>            events = new List <EventBean>();
                    if (_statementSpec.CreateWindowDesc.InsertFilter != null)
                    {
                        var eventsPerStream = new EventBean[1];
                        var filter          = _statementSpec.CreateWindowDesc.InsertFilter.ExprEvaluator;
                        for (IEnumerator <EventBean> it = sourceWindowInstances.TailViewInstance.GetEnumerator(); it.MoveNext();)
                        {
                            var candidate = it.Current;
                            eventsPerStream[0] = candidate;
                            var result = filter.Evaluate(new EvaluateParams(eventsPerStream, true, agentInstanceContext));
                            if ((result == null) || (false.Equals(result)))
                            {
                                continue;
                            }
                            events.Add(candidate);
                        }
                    }
                    else
                    {
                        for (IEnumerator <EventBean> it = sourceWindowInstances.TailViewInstance.GetEnumerator(); it.MoveNext();)
                        {
                            events.Add(it.Current);
                        }
                    }
                    if (events.Count > 0)
                    {
                        EventType   rootViewType    = rootView.EventType;
                        EventBean[] convertedEvents = _services.EventAdapterService.TypeCast(events, rootViewType);
                        rootView.Update(convertedEvents, null);
                    }
                }
            }
            catch (Exception)
            {
                StopCallback callback = StatementAgentInstanceUtil.GetStopCallback(stopCallbacks, agentInstanceContext);
                StatementAgentInstanceUtil.StopSafe(callback, _statementContext);
                throw;
            }

            var createWindowResult = new StatementAgentInstanceFactoryCreateWindowResult(
                finalView, null, agentInstanceContext, eventStreamParentViewable, postLoad, topView, processorInstance, viewableActivationResult);

            if (_statementContext.StatementExtensionServicesContext != null)
            {
                _statementContext.StatementExtensionServicesContext.ContributeStopCallback(createWindowResult, stopCallbacks);
            }

            Log.Debug(".start Statement start completed");
            StopCallback stopCallback = StatementAgentInstanceUtil.GetStopCallback(stopCallbacks, agentInstanceContext);

            createWindowResult.StopCallback = stopCallback;

            return(createWindowResult);
        }
Exemple #13
0
        public SubSelectStrategyFactoryIndexShare(
            string statementName,
            int statementId,
            int subqueryNum,
            EventType[] outerEventTypesSelect,
            NamedWindowProcessor optionalNamedWindowProcessor,
            TableMetadata optionalTableMetadata,
            bool fullTableScan,
            IndexHint optionalIndexHint,
            SubordPropPlan joinedPropPlan,
            ExprEvaluator filterExprEval,
            AggregationServiceFactoryDesc aggregationServiceFactory,
            ExprEvaluator[] groupByKeys,
            TableService tableService,
            Attribute[] annotations,
            StatementStopService statementStopService)
        {
            _optionalNamedWindowProcessor = optionalNamedWindowProcessor;
            _optionalTableMetadata        = optionalTableMetadata;
            _filterExprEval            = filterExprEval;
            _aggregationServiceFactory = aggregationServiceFactory;
            _groupByKeys  = groupByKeys;
            _tableService = tableService;

            bool isLogging;
            ILog log;

            if (optionalTableMetadata != null)
            {
                isLogging  = optionalTableMetadata.IsQueryPlanLogging;
                log        = TableServiceImpl.QueryPlanLog;
                _queryPlan = SubordinateQueryPlanner.PlanSubquery(outerEventTypesSelect, joinedPropPlan, false, fullTableScan, optionalIndexHint, true, subqueryNum,
                                                                  false, optionalTableMetadata.EventTableIndexMetadataRepo, optionalTableMetadata.UniqueKeyProps, true, statementName, statementId, annotations);
                if (_queryPlan != null)
                {
                    for (int i = 0; i < _queryPlan.IndexDescs.Length; i++)
                    {
                        optionalTableMetadata.AddIndexReference(_queryPlan.IndexDescs[i].IndexName, statementName);
                    }
                }
            }
            else
            {
                isLogging  = optionalNamedWindowProcessor.RootView.IsQueryPlanLogging;
                log        = NamedWindowRootView.QueryPlanLog;
                _queryPlan = SubordinateQueryPlanner.PlanSubquery(outerEventTypesSelect, joinedPropPlan, false, fullTableScan, optionalIndexHint, true, subqueryNum,
                                                                  optionalNamedWindowProcessor.IsVirtualDataWindow, optionalNamedWindowProcessor.EventTableIndexMetadataRepo, optionalNamedWindowProcessor.OptionalUniqueKeyProps, false, statementName, statementId, annotations);
                if (_queryPlan != null && _queryPlan.IndexDescs != null)
                {
                    SubordinateQueryPlannerUtil.AddIndexMetaAndRef(_queryPlan.IndexDescs, optionalNamedWindowProcessor.EventTableIndexMetadataRepo, statementName);
                    statementStopService.StatementStopped += () =>
                    {
                        for (int i = 0; i < _queryPlan.IndexDescs.Length; i++)
                        {
                            bool last = optionalNamedWindowProcessor.EventTableIndexMetadataRepo.RemoveIndexReference(_queryPlan.IndexDescs[i].IndexMultiKey, statementName);
                            if (last)
                            {
                                optionalNamedWindowProcessor.EventTableIndexMetadataRepo.RemoveIndex(_queryPlan.IndexDescs[i].IndexMultiKey);
                                optionalNamedWindowProcessor.RemoveAllInstanceIndexes(_queryPlan.IndexDescs[i].IndexMultiKey);
                            }
                        }
                    };
                }
            }

            SubordinateQueryPlannerUtil.QueryPlanLogOnSubq(isLogging, log, _queryPlan, subqueryNum, annotations);
        }
Exemple #14
0
        private string GetStatementName(string windowName)
        {
            NamedWindowProcessor processor = ((EPServiceProviderSPI)epService).NamedWindowService.GetProcessor(windowName);

            return(processor.StatementName);
        }
Exemple #15
0
        private string GetEPL(string windowName)
        {
            NamedWindowProcessor processor = ((EPServiceProviderSPI)epService).NamedWindowService.GetProcessor(windowName);

            return(processor.EplExpression);
        }
Exemple #16
0
 internal FireAndForgetProcessorNamedWindow(NamedWindowProcessor namedWindowProcessor)
 {
     this._namedWindowProcessor = namedWindowProcessor;
 }
Exemple #17
0
        private long GetCount(string windowName)
        {
            NamedWindowProcessor processor = ((EPServiceProviderSPI)epService).NamedWindowService.GetProcessor(windowName);

            return(processor.GetProcessorInstance(null).CountDataWindow);
        }
        private void Preload(
            EPServicesContext services,
            EventTable[] eventIndex,
            Viewable subselectView,
            AgentInstanceContext agentInstanceContext)
        {
            if (_subSelectHolder.StreamSpecCompiled is NamedWindowConsumerStreamSpec)
            {
                var namedSpec = (NamedWindowConsumerStreamSpec)_subSelectHolder.StreamSpecCompiled;
                NamedWindowProcessor processor = services.NamedWindowMgmtService.GetProcessor(namedSpec.WindowName);
                if (processor == null)
                {
                    throw new EPRuntimeException("Failed to find named window by name '" + namedSpec.WindowName + "'");
                }

                var processorInstance = processor.GetProcessorInstance(agentInstanceContext);
                if (processorInstance == null)
                {
                    throw new EPException(
                              "Named window '" + namedSpec.WindowName + "' is associated to context '" + processor.ContextName +
                              "' that is not available for querying");
                }
                var consumerView = processorInstance.TailViewInstance;

                // preload view for stream
                ICollection <EventBean> eventsInWindow;
                if (namedSpec.FilterExpressions != null && !namedSpec.FilterExpressions.IsEmpty())
                {
                    var types      = new StreamTypeServiceImpl(consumerView.EventType, consumerView.EventType.Name, false, services.EngineURI);
                    var queryGraph = ExprNodeUtility.ValidateFilterGetQueryGraphSafe(ExprNodeUtility.ConnectExpressionsByLogicalAndWhenNeeded(namedSpec.FilterExpressions), agentInstanceContext.StatementContext, types);
                    var snapshot   = consumerView.SnapshotNoLock(queryGraph, agentInstanceContext.StatementContext.Annotations);
                    eventsInWindow = new List <EventBean>();
                    ExprNodeUtility.ApplyFilterExpressionsIterable(snapshot, namedSpec.FilterExpressions, agentInstanceContext, eventsInWindow);
                }
                else
                {
                    eventsInWindow = new List <EventBean>();
                    for (IEnumerator <EventBean> it = consumerView.GetEnumerator(); it.MoveNext();)
                    {
                        eventsInWindow.Add(it.Current);
                    }
                }
                EventBean[] newEvents = eventsInWindow.ToArray();
                if (subselectView != null)
                {
                    ((View)subselectView).Update(newEvents, null);
                }
                if (eventIndex != null)
                {
                    foreach (var table in eventIndex)
                    {
                        table.Add(newEvents, agentInstanceContext); // fill index
                    }
                }
            }
            else // preload from the data window that sit on top
            {
                // Start up event table from the iterator
                IEnumerator <EventBean> it = subselectView.GetEnumerator();
                if (it.MoveNext())
                {
                    var preloadEvents = new List <EventBean>();
                    do
                    {
                        preloadEvents.Add(it.Current);
                    } while (it.MoveNext());

                    if (eventIndex != null)
                    {
                        foreach (var table in eventIndex)
                        {
                            table.Add(preloadEvents.ToArray(), agentInstanceContext);
                        }
                    }
                }
            }
        }
        private long GetCount(EPServiceProvider epService, string windowName)
        {
            NamedWindowProcessor processor = GetNWMW(epService).GetProcessor(windowName);

            return(processor.GetProcessorInstance(null).CountDataWindow);
        }
        public static StatementAgentInstanceFactoryResult Start(EPServicesContext servicesContext, ContextControllerStatementBase statement, bool isSingleInstanceContext, int agentInstanceId, MappedEventBean agentInstanceProperties, AgentInstanceFilterProxy agentInstanceFilterProxy, bool isRecoveringResilient)
        {
            var statementContext = statement.StatementContext;

            // for on-trigger statements against named windows we must use the named window lock
            OnTriggerDesc optOnTriggerDesc = statement.StatementSpec.OnTriggerDesc;
            String        namedWindowName  = null;

            if ((optOnTriggerDesc != null) && (optOnTriggerDesc is OnTriggerWindowDesc))
            {
                String windowName = ((OnTriggerWindowDesc)optOnTriggerDesc).WindowName;
                if (servicesContext.TableService.GetTableMetadata(windowName) == null)
                {
                    namedWindowName = windowName;
                }
            }

            // determine lock to use
            IReaderWriterLock agentInstanceLock;

            if (namedWindowName != null)
            {
                NamedWindowProcessor         processor = servicesContext.NamedWindowMgmtService.GetProcessor(namedWindowName);
                NamedWindowProcessorInstance instance  = processor.GetProcessorInstance(agentInstanceId);
                agentInstanceLock = instance.RootViewInstance.AgentInstanceContext.EpStatementAgentInstanceHandle.StatementAgentInstanceLock;
            }
            else
            {
                if (isSingleInstanceContext)
                {
                    agentInstanceLock = statementContext.DefaultAgentInstanceLock;
                }
                else
                {
                    agentInstanceLock = servicesContext.StatementLockFactory.GetStatementLock(
                        statementContext.StatementName, statementContext.Annotations, statementContext.IsStatelessSelect);
                }
            }

            // share the filter version between agent instance handle (callbacks) and agent instance context
            var filterVersion = new StatementAgentInstanceFilterVersion();

            // create handle that comtains lock for use in scheduling and filter callbacks
            var agentInstanceHandle = new EPStatementAgentInstanceHandle(statementContext.EpStatementHandle, agentInstanceLock, agentInstanceId, filterVersion, statementContext.FilterFaultHandlerFactory);

            // create agent instance context
            AgentInstanceScriptContext agentInstanceScriptContext = null;

            if (statementContext.DefaultAgentInstanceScriptContext != null)
            {
                agentInstanceScriptContext = AgentInstanceScriptContext.From(statementContext.EventAdapterService);
            }
            var agentInstanceContext       = new AgentInstanceContext(statementContext, agentInstanceHandle, agentInstanceId, agentInstanceFilterProxy, agentInstanceProperties, agentInstanceScriptContext);
            var statementAgentInstanceLock = agentInstanceContext.EpStatementAgentInstanceHandle.StatementAgentInstanceLock;

            using (Instrument.With(
                       i => i.QContextPartitionAllocate(agentInstanceContext),
                       i => i.AContextPartitionAllocate()))
            {
                using (statementAgentInstanceLock.AcquireWriteLock())
                {
                    try
                    {
                        // start
                        var startResult = statement.Factory.NewContext(agentInstanceContext, isRecoveringResilient);

                        // hook up with listeners+subscribers
                        startResult.FinalView.AddView(statement.MergeView); // hook output to merge view

                        // assign agents for expression-node based strategies
                        var aiExprSvc        = statementContext.StatementAgentInstanceRegistry.AgentInstanceExprService;
                        var aiAggregationSvc =
                            statementContext.StatementAgentInstanceRegistry.AgentInstanceAggregationService;

                        // allocate aggregation service
                        if (startResult.OptionalAggegationService != null)
                        {
                            aiAggregationSvc.AssignService(agentInstanceId, startResult.OptionalAggegationService);
                        }

                        // allocate subquery
                        foreach (var item in startResult.SubselectStrategies)
                        {
                            var node           = item.Key;
                            var strategyHolder = item.Value;

                            aiExprSvc.GetSubselectService(node).AssignService(agentInstanceId, strategyHolder.Stategy);
                            aiExprSvc.GetSubselectAggregationService(node)
                            .AssignService(agentInstanceId, strategyHolder.SubselectAggregationService);

                            // allocate prior within subquery
                            foreach (var priorEntry in strategyHolder.PriorStrategies)
                            {
                                aiExprSvc.GetPriorServices(priorEntry.Key).AssignService(agentInstanceId, priorEntry.Value);
                            }

                            // allocate previous within subquery
                            foreach (var prevEntry in strategyHolder.PreviousNodeStrategies)
                            {
                                aiExprSvc.GetPreviousServices(prevEntry.Key)
                                .AssignService(agentInstanceId, prevEntry.Value);
                            }
                        }

                        // allocate prior-expressions
                        foreach (var item in startResult.PriorNodeStrategies)
                        {
                            aiExprSvc.GetPriorServices(item.Key).AssignService(agentInstanceId, item.Value);
                        }

                        // allocate previous-expressions
                        foreach (var item in startResult.PreviousNodeStrategies)
                        {
                            aiExprSvc.GetPreviousServices(item.Key).AssignService(agentInstanceId, item.Value);
                        }

                        // allocate match-recognize previous expressions
                        var regexExprPreviousEvalStrategy = startResult.RegexExprPreviousEvalStrategy;
                        aiExprSvc.GetMatchRecognizePrevious().AssignService(agentInstanceId, regexExprPreviousEvalStrategy);

                        // allocate table-access-expressions
                        foreach (var item in startResult.TableAccessEvalStrategies)
                        {
                            aiExprSvc.GetTableAccessServices(item.Key).AssignService(agentInstanceId, item.Value);
                        }

                        // execute preloads, if any
                        foreach (var preload in startResult.PreloadList)
                        {
                            preload.ExecutePreload(agentInstanceContext);
                        }

                        if (statementContext.StatementExtensionServicesContext != null &&
                            statementContext.StatementExtensionServicesContext.StmtResources != null)
                        {
                            var holder = statementContext.StatementExtensionServicesContext.ExtractStatementResourceHolder(startResult);
                            statementContext.StatementExtensionServicesContext.StmtResources.SetPartitioned(agentInstanceId, holder);
                        }

                        // instantiate
                        return(startResult);
                    }
                    finally
                    {
                        if (agentInstanceContext.StatementContext.EpStatementHandle.HasTableAccess)
                        {
                            agentInstanceContext.TableExprEvaluatorContext.ReleaseAcquiredLocks();
                        }
                    }
                }
            }
        }
Exemple #21
0
 public ViewableActivator CreateNamedWindow(NamedWindowProcessor processor, IList <ExprNode> filterExpressions, PropertyEvaluator optPropertyEvaluator)
 {
     return(new ViewableActivatorNamedWindow(processor, filterExpressions, optPropertyEvaluator));
 }
Exemple #22
0
        private long GetCount(string windowName)
        {
            NamedWindowProcessor processor = _epService.NamedWindowMgmtService.GetProcessor(windowName);

            return(processor.GetProcessorInstance(null).CountDataWindow);
        }