public static StopCallback GetStopCallback(IList <StopCallback> stopCallbacks, AgentInstanceContext agentInstanceContext)
        {
            var stopCallbackArr = stopCallbacks.ToArray();

            return(new ProxyStopCallback(() => StopSafe(
                                             agentInstanceContext.TerminationCallbackRO, stopCallbackArr,
                                             agentInstanceContext.StatementContext)));
        }
        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();
                        }
                    }
                }
            }
        }
        public static bool EvaluateFilterForStatement(EPServicesContext servicesContext, EventBean theEvent, AgentInstanceContext agentInstanceContext, FilterHandle filterHandle)
        {
            // context was created - reevaluate for the given event
            var callbacks = new ArrayDeque <FilterHandle>();

            servicesContext.FilterService.Evaluate(theEvent, callbacks, agentInstanceContext.StatementContext.StatementId);

            try
            {
                servicesContext.VariableService.SetLocalVersion();

                // sub-selects always go first
                if (callbacks.Any(handle => handle == filterHandle))
                {
                    return(true);
                }

                agentInstanceContext.EpStatementAgentInstanceHandle.InternalDispatch();
            }
            catch (Exception ex)
            {
                servicesContext.ExceptionHandlingService.HandleException(
                    ex, agentInstanceContext.EpStatementAgentInstanceHandle, ExceptionHandlerExceptionType.PROCESS,
                    theEvent);
            }

            return(false);
        }
Ejemplo n.º 4
0
        public static AgentInstanceViewFactoryChainContext Create(IList <ViewFactory> viewFactoryChain, AgentInstanceContext agentInstanceContext, ViewResourceDelegateVerifiedStream viewResourceDelegate)
        {
            Object previousNodeGetter = null;

            if (viewResourceDelegate.PreviousRequests != null && !viewResourceDelegate.PreviousRequests.IsEmpty())
            {
                DataWindowViewWithPrevious factoryFound = EPStatementStartMethodHelperPrevious.FindPreviousViewFactory(viewFactoryChain);
                previousNodeGetter = factoryFound.MakePreviousGetter();
            }

            ViewUpdatedCollection priorViewUpdatedCollection = null;

            if (viewResourceDelegate.PriorRequests != null && !viewResourceDelegate.PriorRequests.IsEmpty())
            {
                var priorEventViewFactory = EPStatementStartMethodHelperPrior.FindPriorViewFactory(viewFactoryChain);
                var callbacksPerIndex     = viewResourceDelegate.PriorRequests;
                priorViewUpdatedCollection = priorEventViewFactory.MakeViewUpdatedCollection(callbacksPerIndex, agentInstanceContext.AgentInstanceId);
            }

            bool removedStream = false;

            if (viewFactoryChain.Count > 1)
            {
                int countDataWindow = 0;
                foreach (ViewFactory viewFactory in viewFactoryChain)
                {
                    if (viewFactory is DataWindowViewFactory)
                    {
                        countDataWindow++;
                    }
                }
                removedStream = countDataWindow > 1;
            }

            return(new AgentInstanceViewFactoryChainContext(agentInstanceContext, removedStream, previousNodeGetter, priorViewUpdatedCollection));
        }