public override OnExprViewResult DetermineOnExprView(AgentInstanceContext agentInstanceContext, IList <StopCallback> stopCallbacks, bool isRecoveringResilient)
        {
            // get result set processor and aggregation services
            Pair <ResultSetProcessor, AggregationService> pair = EPStatementStartMethodHelperUtil.StartResultSetAndAggregation(_resultSetProcessorPrototype, agentInstanceContext, false, null);

            // get named window processor instance
            NamedWindowProcessorInstance processorInstance = _processor.GetProcessorInstance(agentInstanceContext);

            // obtain on-expr view
            EventTable[] indexes = null;
            if (_queryPlan.IndexDescs != null)
            {
                indexes = SubordinateQueryPlannerUtil.RealizeTables(
                    _queryPlan.IndexDescs,
                    _processor.NamedWindowType,
                    processorInstance.RootViewInstance.IndexRepository,
                    processorInstance.RootViewInstance.DataWindowContents,
                    processorInstance.TailViewInstance.AgentInstanceContext,
                    isRecoveringResilient);
            }
            SubordWMatchExprLookupStrategy strategy       = _queryPlan.Factory.Realize(indexes, agentInstanceContext, processorInstance.RootViewInstance.DataWindowContents, processorInstance.RootViewInstance.VirtualDataWindow);
            NamedWindowOnExprBaseView      onExprBaseView = _onExprFactory.Make(strategy, processorInstance.RootViewInstance, agentInstanceContext, pair.First);

            return(new OnExprViewResult(onExprBaseView, pair.Second));
        }
Exemple #2
0
        public override FireAndForgetInstance GetProcessorInstanceNoContext()
        {
            NamedWindowProcessorInstance processorInstance = _namedWindowProcessor.ProcessorInstanceNoContext;

            if (processorInstance == null)
            {
                return(null);
            }
            return(new FireAndForgetInstanceNamedWindow(processorInstance));
        }
Exemple #3
0
        public override FireAndForgetInstance GetProcessorInstanceContextById(int agentInstanceId)
        {
            NamedWindowProcessorInstance processorInstance = _namedWindowProcessor.GetProcessorInstance(agentInstanceId);

            if (processorInstance != null)
            {
                return(new FireAndForgetInstanceNamedWindow(processorInstance));
            }
            return(null);
        }
Exemple #4
0
 public StatementAgentInstanceFactoryCreateWindowResult(Viewable finalView, StopCallback stopCallback, AgentInstanceContext agentInstanceContext, Viewable eventStreamParentViewable, StatementAgentInstancePostLoad postLoad, Viewable topView, NamedWindowProcessorInstance processorInstance, ViewableActivationResult viewableActivationResult)
     : base(finalView, stopCallback, agentInstanceContext, null,
            Collections.GetEmptyMap <ExprSubselectNode, SubSelectStrategyHolder>(),
            Collections.GetEmptyMap <ExprPriorNode, ExprPriorEvalStrategy>(),
            Collections.GetEmptyMap <ExprPreviousNode, ExprPreviousEvalStrategy>(),
            null,
            Collections.GetEmptyMap <ExprTableAccessNode, ExprTableAccessEvalStrategy>(),
            Collections.GetEmptyList <StatementAgentInstancePreload>())
 {
     EventStreamParentViewable = eventStreamParentViewable;
     PostLoad                 = postLoad;
     TopView                  = topView;
     ProcessorInstance        = processorInstance;
     ViewableActivationResult = viewableActivationResult;
 }
        public StatementAgentInstanceFactoryResult NewContext(AgentInstanceContext agentInstanceContext, bool isRecoveringResilient)
        {
            StopCallback stopCallback;

            if (namedWindowProcessor != null)
            {
                // handle named window index
                NamedWindowProcessorInstance processorInstance = namedWindowProcessor.GetProcessorInstance(agentInstanceContext);

                if (namedWindowProcessor.IsVirtualDataWindow)
                {
                    VirtualDWView virtualDWView = processorInstance.RootViewInstance.VirtualDataWindow;
                    virtualDWView.HandleStartIndex(spec);
                    stopCallback = () => {
                        virtualDWView.HandleStopIndex(spec);
                    };
                }
                else
                {
                    try {
                        processorInstance.RootViewInstance.AddExplicitIndex(spec.IsUnique, spec.IndexName, spec.Columns);
                    }
                    catch (ExprValidationException e) {
                        throw new EPException("Failed to create index: " + e.Message, e);
                    }
                    stopCallback = () => {
                    };
                }
            }
            else
            {
                // handle table access
                try {
                    TableStateInstance instance = services.TableService.GetState(tableName, agentInstanceContext.AgentInstanceId);
                    instance.AddExplicitIndex(spec);
                }
                catch (ExprValidationException ex) {
                    throw new EPException("Failed to create index: " + ex.Message, ex);
                }
                stopCallback = () => {
                };
            }

            return(new StatementAgentInstanceFactoryCreateIndexResult(finalView, stopCallback, agentInstanceContext));
        }
Exemple #6
0
        public SubSelectStrategyRealization Instantiate(
            EPServicesContext services,
            Viewable viewableRoot,
            AgentInstanceContext agentInstanceContext,
            IList <StopCallback> stopCallbackList,
            int subqueryNumber,
            bool isRecoveringResilient)
        {
            SubselectAggregationPreprocessorBase subselectAggregationPreprocessor = null;

            AggregationService aggregationService = null;

            if (_aggregationServiceFactory != null)
            {
                aggregationService = _aggregationServiceFactory.AggregationServiceFactory.MakeService(
                    agentInstanceContext, agentInstanceContext.StatementContext.EngineImportService, true, subqueryNumber);
                if (_groupByKeys == null)
                {
                    if (_filterExprEval == null)
                    {
                        subselectAggregationPreprocessor =
                            new SubselectAggregationPreprocessorUnfilteredUngrouped(
                                aggregationService, _filterExprEval, null);
                    }
                    else
                    {
                        subselectAggregationPreprocessor =
                            new SubselectAggregationPreprocessorFilteredUngrouped(aggregationService, _filterExprEval, null);
                    }
                }
                else
                {
                    if (_filterExprEval == null)
                    {
                        subselectAggregationPreprocessor =
                            new SubselectAggregationPreprocessorUnfilteredGrouped(
                                aggregationService, _filterExprEval, _groupByKeys);
                    }
                    else
                    {
                        subselectAggregationPreprocessor =
                            new SubselectAggregationPreprocessorFilteredGrouped(
                                aggregationService, _filterExprEval, _groupByKeys);
                    }
                }
            }

            SubordTableLookupStrategy subqueryLookup;

            if (_optionalNamedWindowProcessor != null)
            {
                NamedWindowProcessorInstance instance =
                    _optionalNamedWindowProcessor.GetProcessorInstance(agentInstanceContext);
                if (_queryPlan == null)
                {
                    if (instance.RootViewInstance.IsQueryPlanLogging && NamedWindowRootView.QueryPlanLog.IsInfoEnabled)
                    {
                        NamedWindowRootView.QueryPlanLog.Info("shared, full table scan");
                    }
                    subqueryLookup =
                        new SubordFullTableScanLookupStrategyLocking(
                            instance.RootViewInstance.DataWindowContents,
                            agentInstanceContext.EpStatementAgentInstanceHandle.StatementAgentInstanceLock);
                }
                else
                {
                    EventTable[] tables = null;
                    if (!_optionalNamedWindowProcessor.IsVirtualDataWindow)
                    {
                        tables = SubordinateQueryPlannerUtil.RealizeTables(
                            _queryPlan.IndexDescs, instance.RootViewInstance.EventType,
                            instance.RootViewInstance.IndexRepository,
                            instance.RootViewInstance.DataWindowContents, agentInstanceContext,
                            isRecoveringResilient);
                    }
                    SubordTableLookupStrategy strategy = _queryPlan.LookupStrategyFactory.MakeStrategy(
                        tables, instance.RootViewInstance.VirtualDataWindow);
                    subqueryLookup = new SubordIndexedTableLookupStrategyLocking(
                        strategy, instance.TailViewInstance.AgentInstanceContext.AgentInstanceLock);
                }
            }
            else
            {
                TableStateInstance state = _tableService.GetState(
                    _optionalTableMetadata.TableName, agentInstanceContext.AgentInstanceId);
                ILockable iLock = agentInstanceContext.StatementContext.IsWritesToTables
                    ? state.TableLevelRWLock.WriteLock
                    : state.TableLevelRWLock.ReadLock;
                if (_queryPlan == null)
                {
                    subqueryLookup = new SubordFullTableScanTableLookupStrategy(iLock, state.IterableTableScan);
                }
                else
                {
                    EventTable[] indexes = new EventTable[_queryPlan.IndexDescs.Length];
                    for (int i = 0; i < indexes.Length; i++)
                    {
                        indexes[i] = state.IndexRepository.GetIndexByDesc(_queryPlan.IndexDescs[i].IndexMultiKey);
                    }
                    subqueryLookup = _queryPlan.LookupStrategyFactory.MakeStrategy(indexes, null);
                    subqueryLookup = new SubordIndexedTableLookupTableStrategy(subqueryLookup, iLock);
                }
            }

            return(new SubSelectStrategyRealization(
                       subqueryLookup, subselectAggregationPreprocessor, aggregationService,
                       Collections.GetEmptyMap <ExprPriorNode, ExprPriorEvalStrategy>(),
                       Collections.GetEmptyMap <ExprPreviousNode, ExprPreviousEvalStrategy>(),
                       null, null));
        }
Exemple #7
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 #8
0
        public StatementAgentInstanceFactoryResult NewContext(AgentInstanceContext agentInstanceContext, bool isRecoveringResilient)
        {
            StopCallback stopCallback;

            int agentInstanceId = agentInstanceContext.AgentInstanceId;

            if (namedWindowProcessor != null)
            {
                // handle named window index
                NamedWindowProcessorInstance processorInstance = namedWindowProcessor.GetProcessorInstance(agentInstanceContext);

                if (namedWindowProcessor.IsVirtualDataWindow)
                {
                    VirtualDWView virtualDWView = processorInstance.RootViewInstance.VirtualDataWindow;
                    virtualDWView.HandleStartIndex(spec);
                    stopCallback = new ProxyStopCallback(() => virtualDWView.HandleStopIndex(spec));
                }
                else
                {
                    try {
                        processorInstance.RootViewInstance.AddExplicitIndex(spec.IsUnique, spec.IndexName, spec.Columns, isRecoveringResilient);
                    }
                    catch (ExprValidationException e) {
                        throw new EPException("Failed to create index: " + e.Message, e);
                    }
                    stopCallback = new ProxyStopCallback(() => {
                        // we remove the index when context partitioned.
                        // when not context partition the index gets removed when the last reference to the named window gets destroyed.
                        if (contextName != null)
                        {
                            var instance = namedWindowProcessor.GetProcessorInstance(agentInstanceId);
                            if (instance != null)
                            {
                                instance.RemoveExplicitIndex(spec.IndexName);
                            }
                        }
                    });
                }
            }
            else
            {
                // handle table access
                try {
                    TableStateInstance instance = services.TableService.GetState(tableName, agentInstanceContext.AgentInstanceId);
                    instance.AddExplicitIndex(spec, isRecoveringResilient, contextName != null);
                }
                catch (ExprValidationException ex) {
                    throw new EPException("Failed to create index: " + ex.Message, ex);
                }
                stopCallback = new ProxyStopCallback(() => {
                    // we remove the index when context partitioned.
                    // when not context partition the index gets removed when the last reference to the table gets destroyed.
                    if (contextName != null)
                    {
                        TableStateInstance instance = services.TableService.GetState(tableName, agentInstanceId);
                        if (instance != null)
                        {
                            instance.RemoveExplicitIndex(spec.IndexName);
                        }
                    }
                });
            }

            return(new StatementAgentInstanceFactoryCreateIndexResult(finalView, stopCallback, agentInstanceContext));
        }
        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 FireAndForgetInstanceNamedWindow(NamedWindowProcessorInstance processorInstance)
 {
     this._processorInstance = processorInstance;
 }