Ejemplo n.º 1
0
 public EPStatementAgentInstanceHandle(EPStatementHandle statementHandle, IReaderWriterLock statementAgentInstanceLock, int agentInstanceId, StatementAgentInstanceFilterVersion statementFilterVersion)
 {
     StatementHandle             = statementHandle;
     _statementAgentInstanceLock = statementAgentInstanceLock;
     AgentInstanceId             = agentInstanceId;
     _hashCode = 31 * statementHandle.GetHashCode() + agentInstanceId;
     StatementFilterVersion = statementFilterVersion;
 }
Ejemplo n.º 2
0
 public EPStatementAgentInstanceHandle(EPStatementHandle statementHandle, IReaderWriterLock statementAgentInstanceLock, int agentInstanceId, StatementAgentInstanceFilterVersion statementFilterVersion, FilterFaultHandlerFactory filterFaultHandlerFactory)
 {
     StatementHandle             = statementHandle;
     _statementAgentInstanceLock = statementAgentInstanceLock;
     AgentInstanceId             = agentInstanceId;
     _hashCode = 31 * statementHandle.GetHashCode() + agentInstanceId;
     StatementFilterVersion = statementFilterVersion;
     if (filterFaultHandlerFactory != null)
     {
         FilterFaultHandler = filterFaultHandlerFactory.MakeFilterFaultHandler();
     }
 }
        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();
                        }
                    }
                }
            }
        }