protected override StatementAgentInstanceFactoryResult NewContextInternal(AgentInstanceContext agentInstanceContext, bool isRecoveringResilient) { StopCallback stopCallback; IList <StopCallback> stopCallbacks = new List <StopCallback>(); IDictionary <ExprSubselectNode, SubSelectStrategyHolder> subselectStrategies; try { stopCallbacks.Add(new ProxyStopCallback(() => _services.InternalEventRouter.RemovePreprocessing(_streamEventType, _desc))); _services.InternalEventRouter.AddPreprocessing(_routerDesc, _onExprView, agentInstanceContext.AgentInstanceLock, !_subSelectStrategyCollection.Subqueries.IsEmpty()); // start subselects subselectStrategies = EPStatementStartMethodHelperSubselect.StartSubselects(_services, _subSelectStrategyCollection, agentInstanceContext, stopCallbacks, isRecoveringResilient); } catch (Exception ex) { stopCallback = StatementAgentInstanceUtil.GetStopCallback(stopCallbacks, agentInstanceContext); StatementAgentInstanceUtil.StopSafe(stopCallback, _statementContext); throw; } StatementAgentInstanceFactoryUpdateResult result = new StatementAgentInstanceFactoryUpdateResult(_onExprView, null, agentInstanceContext, subselectStrategies); if (_statementContext.StatementExtensionServicesContext != null) { _statementContext.StatementExtensionServicesContext.ContributeStopCallback(result, stopCallbacks); } stopCallback = StatementAgentInstanceUtil.GetStopCallback(stopCallbacks, agentInstanceContext); result.StopCallback = stopCallback; return(result); }
private void RemoveStatement(int statementId) { var statementDesc = _statements.Get(statementId); if (statementDesc == null) { return; } foreach (var contextPartitionEntry in _agentInstances) { var instanceList = contextPartitionEntry.Value.AgentInstances .Where(instance => instance.AgentInstanceContext.StatementContext.StatementId == statementId) .ToList(); instanceList.ForEach( instance => { StatementAgentInstanceUtil.Stop( instance.StopCallback, instance.AgentInstanceContext, instance.FinalView, _servicesContext, true, false, true); contextPartitionEntry.Value.AgentInstances.Remove(instance); }); } _statements.Remove(statementId); }
public ContextControllerInstanceHandle ContextPartitionInstantiate( int?optionalContextPartitionId, int subPathId, int?importSubpathId, ContextController originator, EventBean optionalTriggeringEvent, IDictionary <string, object> optionalTriggeringPattern, object partitionKey, IDictionary <string, object> contextProperties, ContextControllerState states, ContextInternalFilterAddendum filterAddendum, bool isRecoveringResilient, ContextPartitionState state) { using (_uLock.Acquire()) { // assign context id int assignedContextId; if (optionalContextPartitionId != null && !states.IsImported) { assignedContextId = optionalContextPartitionId.Value; _contextPartitionIdManager.AddExisting(optionalContextPartitionId.Value); } else { assignedContextId = _contextPartitionIdManager.AllocateId(); if (states != null && states.PartitionImportCallback != null && optionalContextPartitionId != null) { states.PartitionImportCallback.Allocated(assignedContextId, optionalContextPartitionId.Value); } } // handle leaf creation IList <AgentInstance> newInstances = new List <AgentInstance>(); if (state == ContextPartitionState.STARTED) { foreach (var statementEntry in _statements) { var statementDesc = statementEntry.Value; var instance = StartStatement(assignedContextId, statementDesc, originator, partitionKey, contextProperties, isRecoveringResilient); newInstances.Add(instance); } } // for all new contexts: evaluate this event for this statement if (optionalTriggeringEvent != null || optionalTriggeringPattern != null) { StatementAgentInstanceUtil.EvaluateEventForStatement(_servicesContext, optionalTriggeringEvent, optionalTriggeringPattern, newInstances); } // save leaf var filterVersion = _servicesContext.FilterService.FiltersVersion; var agentInstanceList = new ContextControllerTreeAgentInstanceList(filterVersion, partitionKey, contextProperties, newInstances, state); _agentInstances.Put(assignedContextId, agentInstanceList); // Update the filter version for this handle _factory.FactoryContext.AgentInstanceContextCreate.EpStatementAgentInstanceHandle.StatementFilterVersion.StmtFilterVersion = filterVersion; return(new ContextNestedHandleImpl(subPathId, assignedContextId, agentInstanceList)); } }
private AgentInstance StartStatement( int contextId, ContextControllerStatementDesc statementDesc, ContextController originator, Object partitionKey, IDictionary <String, Object> contextProperties, bool isRecoveringResilient) { // build filters var proxy = GetMergedFilterAddendums(statementDesc, originator, partitionKey, contextId); // build built-in context properties var properties = ContextPropertyEventType.GetNestedBeanBase(_contextName, contextId); properties.Put( _nestedContextFactories[_nestedContextFactories.Length - 1].FactoryContext.ContextName, contextProperties); RecursivePopulateBuiltinProps(originator, properties); properties.Put(ContextPropertyEventType.PROP_CTX_NAME, _contextName); properties.Put(ContextPropertyEventType.PROP_CTX_ID, contextId); var contextBean = (MappedEventBean) _servicesContext.EventAdapterService.AdapterForTypedMap( properties, _contextDescriptor.ContextPropertyRegistry.ContextEventType); // activate var result = StatementAgentInstanceUtil.Start( _servicesContext, statementDesc.Statement, false, contextId, contextBean, proxy, isRecoveringResilient); return(new AgentInstance(result.StopCallback, result.AgentInstanceContext, result.FinalView)); }
public void Activate(EventBean optionalTriggeringEvent, MatchedEventMap priorMatches, long timeOffset, bool isRecoveringResilient) { FilterHandleCallback filterCallback = new ProxyFilterHandleCallback { ProcStatementId = () => _agentInstanceContext.StatementContext.StatementId, ProcIsSubselect = () => false, ProcMatchFound = (theEvent, allStmtMatches) => FilterMatchFound(theEvent) }; // determine addendum, if any FilterValueSetParam[][] addendum = null; if (_filterAddendum != null) { addendum = _filterAddendum.GetFilterAddendum(_endpointFilterSpec.FilterSpecCompiled); } _filterHandle = new EPStatementHandleCallback(_agentInstanceContext.EpStatementAgentInstanceHandle, filterCallback); FilterValueSet filterValueSet = _endpointFilterSpec.FilterSpecCompiled.GetValueSet(null, _agentInstanceContext, addendum); _filterServiceEntry = _servicesContext.FilterService.Add(filterValueSet, _filterHandle); if (optionalTriggeringEvent != null) { bool match = StatementAgentInstanceUtil.EvaluateFilterForStatement(_servicesContext, optionalTriggeringEvent, _agentInstanceContext, _filterHandle); if (match) { FilterMatchFound(optionalTriggeringEvent); } } }
public void Close(DataFlowOpCloseContext openContext) { if (_selectResult != null) { StatementAgentInstanceUtil.StopSafe(_selectResult.StopCallback, _agentInstanceContext.StatementContext); } }
public void Activate(EventBean optionalTriggeringEvent, IDictionary <String, Object> optionalTriggeringPattern, ContextControllerState controllerState, ContextInternalFilterAddendum filterAddendum, int?importPathId) { var factoryContext = _factory.FactoryContext; _activationFilterAddendum = filterAddendum; foreach (var item in _factory.SegmentedSpec.Items) { var callback = new ContextControllerPartitionedFilterCallback(factoryContext.ServicesContext, factoryContext.AgentInstanceContextCreate, item, this, filterAddendum); _filterCallbacks.Add(callback); if (optionalTriggeringEvent != null) { var match = StatementAgentInstanceUtil.EvaluateFilterForStatement(factoryContext.ServicesContext, optionalTriggeringEvent, factoryContext.AgentInstanceContextCreate, callback.FilterHandle); if (match) { callback.MatchFound(optionalTriggeringEvent, null); } } } if (factoryContext.NestingLevel == 1) { controllerState = ContextControllerStateUtil.GetRecoveryStates(_factory.FactoryContext.StateCache, factoryContext.OutermostContextName); } if (controllerState == null) { return; } int?pathIdToUse = importPathId ?? _pathId; InitializeFromState(optionalTriggeringEvent, optionalTriggeringPattern, filterAddendum, controllerState, pathIdToUse.Value, null, false); }
public void Stop() { foreach (StopCallback stopCallback in _stopCallbacks) { StatementAgentInstanceUtil.StopSafe(stopCallback, _statementContext); } }
public bool HandleFilterFault(EventBean theEvent, long version) { using (_uLock.Acquire()) { StatementAgentInstanceUtil.HandleFilterFault(theEvent, version, _servicesContext, _agentInstances); return(false); } }
public StatementAgentInstanceFactoryResult NewContext(AgentInstanceContext agentInstanceContext, bool isRecoveringResilient) { IList <StopCallback> stopCallbacks = new List <StopCallback>(); View view; IDictionary <ExprSubselectNode, SubSelectStrategyHolder> subselectStrategies; AggregationService aggregationService; EvalRootState optPatternRoot; IDictionary <ExprTableAccessNode, ExprTableAccessEvalStrategy> tableAccessStrategies; ViewableActivationResult activationResult; StopCallback stopCallback; try { if (Services.SchedulableAgentInstanceDirectory != null) { Services.SchedulableAgentInstanceDirectory.Add(agentInstanceContext.EpStatementAgentInstanceHandle); } OnExprViewResult onExprViewResult = DetermineOnExprView(agentInstanceContext, stopCallbacks); view = onExprViewResult.OnExprView; aggregationService = onExprViewResult.OptionalAggregationService; // attach stream to view activationResult = _activator.Activate(agentInstanceContext, false, isRecoveringResilient); activationResult.Viewable.AddView(view); stopCallbacks.Add(activationResult.StopCallback); optPatternRoot = activationResult.OptionalPatternRoot; // determine final output view view = DetermineFinalOutputView(agentInstanceContext, view); // start subselects subselectStrategies = EPStatementStartMethodHelperSubselect.StartSubselects(Services, _subSelectStrategyCollection, agentInstanceContext, stopCallbacks); // plan table access tableAccessStrategies = EPStatementStartMethodHelperTableAccess.AttachTableAccess(Services, agentInstanceContext, StatementSpec.TableNodes); } catch (Exception) { stopCallback = StatementAgentInstanceUtil.GetStopCallback(stopCallbacks, agentInstanceContext); StatementAgentInstanceUtil.StopSafe(stopCallback, StatementContext); throw; } StatementAgentInstanceFactoryOnTriggerResult onTriggerResult = new StatementAgentInstanceFactoryOnTriggerResult(view, null, agentInstanceContext, aggregationService, subselectStrategies, optPatternRoot, tableAccessStrategies, activationResult); if (StatementContext.StatementExtensionServicesContext != null) { StatementContext.StatementExtensionServicesContext.ContributeStopCallback(onTriggerResult, stopCallbacks); } Log.Debug(".start Statement start completed"); stopCallback = StatementAgentInstanceUtil.GetStopCallback(stopCallbacks, agentInstanceContext); onTriggerResult.StopCallback = stopCallback; return(onTriggerResult); }
/// <summary> /// Provides the sub-context that ends. /// </summary> /// <param name="contextNestedHandle">The context nested handle.</param> /// <param name="terminationProperties">The termination properties.</param> /// <param name="leaveLocksAcquired">if set to <c>true</c> [leave locks acquired].</param> /// <param name="agentInstances">The agent instances.</param> public void ContextPartitionTerminate( ContextControllerInstanceHandle contextNestedHandle, IDictionary <String, Object> terminationProperties, bool leaveLocksAcquired, IList <AgentInstance> agentInstances) { var handle = (ContextManagerNestedInstanceHandle)contextNestedHandle; if (handle.IsBranch) { var branchHandle = handle; var branch = branchHandle.Controller; RecursiveDeactivateStop(branch, leaveLocksAcquired, agentInstances); if (Log.IsDebugEnabled) { Log.Debug( "Terminated context branch for " + _contextName + " from level " + branch.Factory.FactoryContext.NestingLevel + "(" + branch.Factory.FactoryContext.ContextName + ")" + " parentPath " + branch.PathId); } } else { var leafHandle = handle; var leaf = leafHandle.Controller; var leafEntry = _subcontexts.Get(leaf); if (leafEntry != null) { // could be terminated earlier var ailist = leafEntry.AgentInstances.Get(leafHandle.ContextPartitionOrPathId); if (ailist != null) { StatementAgentInstanceUtil.StopAgentInstances( ailist.AgentInstances, null, _servicesContext, false, false); _contextPartitionIdManager.RemoveId(leafHandle.ContextPartitionOrPathId); ailist.AgentInstances.Clear(); } if (Log.IsDebugEnabled) { Log.Debug( "Terminated context leaf for " + _contextName + " from level " + leaf.Factory.FactoryContext.NestingLevel + "(" + leaf.Factory.FactoryContext.ContextName + ")" + " parentPath " + leaf.PathId + " contextPartId " + leafHandle.ContextPartitionOrPathId); } } } }
public void ContextPartitionNavigate(ContextControllerInstanceHandle existingHandle, ContextController originator, ContextControllerState controllerState, int exportedCPOrPathId, ContextInternalFilterAddendum filterAddendum, AgentInstanceSelector agentInstanceSelector, byte[] payload, bool isRecoveringResilient) { var entry = _agentInstances.Get(existingHandle.ContextPartitionOrPathId); if (entry == null) { return; } if (entry.State == ContextPartitionState.STOPPED) { entry.State = ContextPartitionState.STARTED; entry.AgentInstances.Clear(); foreach (var statement in _statements) { var instance = StartStatement(existingHandle.ContextPartitionOrPathId, statement.Value, originator, entry.InitPartitionKey, entry.InitContextProperties, false); entry.AgentInstances.Add(instance); } var key = new ContextStatePathKey(1, 0, existingHandle.SubPathId); var value = new ContextStatePathValue(existingHandle.ContextPartitionOrPathId, payload, ContextPartitionState.STARTED); _rootContext.Factory.FactoryContext.StateCache.UpdateContextPath(_contextName, key, value); } else { IList <AgentInstance> removed = new List <AgentInstance>(2); IList <AgentInstance> added = new List <AgentInstance>(2); foreach (var agentInstance in entry.AgentInstances) { if (!agentInstanceSelector.Select(agentInstance)) { continue; } // remove StatementAgentInstanceUtil.StopAgentInstanceRemoveResources(agentInstance, null, _servicesContext, false, false); removed.Add(agentInstance); // start var statementDesc = _statements.Get(agentInstance.AgentInstanceContext.StatementId); var instance = StartStatement(existingHandle.ContextPartitionOrPathId, statementDesc, originator, entry.InitPartitionKey, entry.InitContextProperties, isRecoveringResilient); added.Add(instance); if (controllerState.PartitionImportCallback != null) { controllerState.PartitionImportCallback.Existing(existingHandle.ContextPartitionOrPathId, exportedCPOrPathId); } } entry.AgentInstances.RemoveAll(removed); entry.AgentInstances.AddAll(added); } }
private void RecursiveDeactivateStop( ContextController currentContext, bool leaveLocksAcquired, IList <AgentInstance> agentInstancesCollected) { // deactivate currentContext.Deactivate(); // remove state var entry = _subcontexts.Pluck(currentContext); if (entry == null) { return; } // remove from parent var parent = _subcontexts.Get(entry.Parent); if (parent != null) { parent.ChildContexts.Remove(currentContext.PathId); } // stop instances if (entry.AgentInstances != null) { foreach (var entryCP in entry.AgentInstances) { StatementAgentInstanceUtil.StopAgentInstances( entryCP.Value.AgentInstances, null, _servicesContext, false, leaveLocksAcquired); if (agentInstancesCollected != null) { agentInstancesCollected.AddAll(entryCP.Value.AgentInstances); } _contextPartitionIdManager.RemoveId(entryCP.Key); } } // deactivate child contexts if (entry.ChildContexts == null || entry.ChildContexts.IsEmpty()) { return; } foreach (ContextController inner in entry.ChildContexts.Values) { RecursiveDeactivateStop(inner, leaveLocksAcquired, agentInstancesCollected); } }
public ContextStatePathDescriptor ExtractDestroyPaths(ContextPartitionSelector selector) { var states = ExtractPaths(selector); foreach (var entry in states.Paths) { var agentInstanceId = entry.Value.OptionalContextPartitionId.Value; var descriptor = states.ContextPartitionInformation.Get(agentInstanceId); _rootContext.DeletePath(descriptor.Identifier); var list = _agentInstances.Delete(agentInstanceId); StatementAgentInstanceUtil.StopAgentInstances(list.AgentInstances, null, _servicesContext, false, false); list.ClearAgentInstances(); _rootContext.Factory.FactoryContext.StateCache.RemoveContextPath(_contextName, entry.Key.Level, entry.Key.ParentPath, entry.Key.SubPath); } return(states); }
public virtual bool HandleFilterFault(EventBean theEvent, long version) { using (_iLock.Acquire()) { foreach (var entry in _subcontexts) { if (entry.Value.AgentInstances != null) { StatementAgentInstanceUtil.HandleFilterFault( theEvent, version, _servicesContext, entry.Value.AgentInstances); } } return(false); } }
public ContextStatePathDescriptor ExtractStopPaths(ContextPartitionSelector selector) { var states = ExtractPaths(selector); foreach (var entry in states.Paths) { var agentInstanceId = entry.Value.OptionalContextPartitionId.Value; var list = _agentInstances.Get(agentInstanceId); list.State = ContextPartitionState.STOPPED; StatementAgentInstanceUtil.StopAgentInstances(list.AgentInstances, null, _servicesContext, false, false); list.ClearAgentInstances(); entry.Value.State = ContextPartitionState.STOPPED; _rootContext.Factory.FactoryContext.StateCache.UpdateContextPath(_contextName, entry.Key, entry.Value); } return(states); }
public void SafeDestroy() { if (_rootContext != null) { // deactivate _rootContext.Deactivate(); _factory.FactoryContext.StateCache.RemoveContext(_contextName); foreach (var entryCP in _agentInstances) { StatementAgentInstanceUtil.StopAgentInstances(entryCP.Value.AgentInstances, null, _servicesContext, true, false); } _agentInstances.Clear(); _contextPartitionIdManager.Clear(); _statements.Clear(); } }
public void ContextPartitionTerminate(ContextControllerInstanceHandle contextNestedHandle, IDictionary <String, Object> terminationProperties, bool leaveLocksAcquired, IList <AgentInstance> agentInstancesCollected) { using (_uLock.Acquire()) { var handle = (ContextNestedHandleImpl)contextNestedHandle; var entry = _agentInstances.Delete(handle.ContextPartitionOrPathId); if (entry != null) { StatementAgentInstanceUtil.StopAgentInstances(entry.AgentInstances, terminationProperties, _servicesContext, false, leaveLocksAcquired); if (agentInstancesCollected != null) { agentInstancesCollected.AddAll(entry.AgentInstances); } entry.AgentInstances.Clear(); _contextPartitionIdManager.RemoveId(contextNestedHandle.ContextPartitionOrPathId); } } }
private AgentInstance StartStatement(int contextId, ContextControllerStatementDesc statementDesc, ContextController originator, Object partitionKey, IDictionary <String, Object> contextProperties, bool isRecoveringResilient) { // build filters var filterAddendum = new IdentityDictionary <FilterSpecCompiled, FilterValueSetParam[][]>(); originator.Factory.PopulateFilterAddendums(filterAddendum, statementDesc, partitionKey, contextId); AgentInstanceFilterProxy proxy = new AgentInstanceFilterProxyImpl(filterAddendum); // build built-in context properties contextProperties.Put(ContextPropertyEventType.PROP_CTX_NAME, _contextName); contextProperties.Put(ContextPropertyEventType.PROP_CTX_ID, contextId); var contextBean = (MappedEventBean)_servicesContext.EventAdapterService.AdapterForTypedMap(contextProperties, _contextDescriptor.ContextPropertyRegistry.ContextEventType); // activate var result = StatementAgentInstanceUtil.Start(_servicesContext, statementDesc.Statement, false, contextId, contextBean, proxy, isRecoveringResilient); // save only instance data return(new AgentInstance(result.StopCallback, result.AgentInstanceContext, result.FinalView)); }
protected void ActivateFilters(EventBean optionalTriggeringEvent) { var factoryContext = _factory.FactoryContext; foreach (var item in _factory.HashedSpec.Items) { var callback = new ContextControllerHashedFilterCallback(factoryContext.ServicesContext, factoryContext.AgentInstanceContextCreate, item, this, _activationFilterAddendum); _filterCallbacks.Add(callback); if (optionalTriggeringEvent != null) { var match = StatementAgentInstanceUtil.EvaluateFilterForStatement(factoryContext.ServicesContext, optionalTriggeringEvent, factoryContext.AgentInstanceContextCreate, callback.FilterHandle); if (match) { callback.MatchFound(optionalTriggeringEvent, null); } } } }
private void RemoveStatement(int statementId) { var statementDesc = _statements.Get(statementId); if (statementDesc == null) { return; } foreach (var entry in _subcontexts) { // ignore branches if (entry.Key.Factory.FactoryContext.NestingLevel < _nestedContextFactories.Length) { continue; } if (entry.Value.AgentInstances == null || entry.Value.AgentInstances.IsEmpty()) { continue; } foreach (var contextPartitionEntry in entry.Value.AgentInstances) { var agentInstances = contextPartitionEntry.Value.AgentInstances; for (int ii = agentInstances.Count - 1; ii >= 0; ii--) { var instance = agentInstances[ii]; if (instance.AgentInstanceContext.StatementContext.StatementId != statementId) { continue; } StatementAgentInstanceUtil.Stop( instance.StopCallback, instance.AgentInstanceContext, instance.FinalView, _servicesContext, true, false, true); agentInstances.RemoveAt(ii); } } } _statements.Remove(statementId); }
public ContextStatePathDescriptor ExtractStopPaths(ContextPartitionSelector selector) { var visitor = GetContextPartitionPathsInternal(selector); foreach (var entry in visitor.ControllerAgentInstances) { var treeEntry = _subcontexts.Get(entry.Key); foreach (var leaf in entry.Value) { var agentInstanceId = leaf.Value.OptionalContextPartitionId.GetValueOrDefault(); var list = treeEntry.AgentInstances.Get(agentInstanceId); list.State = ContextPartitionState.STOPPED; StatementAgentInstanceUtil.StopAgentInstances( list.AgentInstances, null, _servicesContext, false, false); list.ClearAgentInstances(); leaf.Value.State = ContextPartitionState.STOPPED; _rootContext.Factory.FactoryContext.StateCache.UpdateContextPath(_contextName, leaf.Key, leaf.Value); } } return(new ContextStatePathDescriptor(visitor.States, visitor.AgentInstanceInfo)); }
public ContextStatePathDescriptor ExtractDestroyPaths(ContextPartitionSelector selector) { var visitor = GetContextPartitionPathsInternal(selector); foreach (var entry in visitor.ControllerAgentInstances) { var treeEntry = _subcontexts.Get(entry.Key); foreach (var leaf in entry.Value) { var agentInstanceId = leaf.Value.OptionalContextPartitionId.GetValueOrDefault(); var list = treeEntry.AgentInstances.Get(agentInstanceId); StatementAgentInstanceUtil.StopAgentInstances( list.AgentInstances, null, _servicesContext, false, false); _rootContext.Factory.FactoryContext.StateCache.RemoveContextPath( _contextName, leaf.Key.Level, leaf.Key.ParentPath, leaf.Key.SubPath); var descriptor = visitor.AgentInstanceInfo.Get(agentInstanceId); var nestedIdent = (ContextPartitionIdentifierNested)descriptor.Identifier; entry.Key.DeletePath(nestedIdent.Identifiers[_nestedContextFactories.Length - 1]); } } return(new ContextStatePathDescriptor(visitor.States, visitor.AgentInstanceInfo)); }
public bool HandleFilterFault(EventBean theEvent, long version) { /* * Handle filter faults such as * - a) App thread determines event E1 applies to CTX + CP1 * b) Timer thread destroys CP1 * c) App thread processes E1 for CTX allocating CP2, processing E1 for CP2 * d) App thread processes E1 for CP1, filter-faulting and ending up dropping the event for CP1 because of this handler * * - a) App thread determines event E1 applies to CTX + CP1 * b) App thread processes E1 for CTX, no action * c) Timer thread destroys CP1 * d) App thread processes E1 for CP1, filter-faulting and ending up processing E1 into CTX because of this handler */ AgentInstanceContext aiCreate = _contextControllerInitTerm.Factory.FactoryContext.AgentInstanceContextCreate; using (aiCreate.EpStatementAgentInstanceHandle.StatementAgentInstanceLock.AcquireWriteLock()) { Object key = _contextControllerInitTerm.GetDistinctKey(theEvent); EventBean trigger = _contextControllerInitTerm.DistinctContexts.Get(key); // see if we find that context partition if (trigger != null) { // true for we have already handled this event // false for filter fault return(trigger.Equals(theEvent)); } // not found: evaluate against context StatementAgentInstanceUtil.EvaluateEventForStatement( _contextControllerInitTerm.Factory.FactoryContext.ServicesContext, theEvent, null, Collections.SingletonList(new AgentInstance(null, aiCreate, null))); return(true); // we handled the event } }
public bool HandleFilterFault(EventBean theEvent, long version) { // // Handle filter faults such as // - a) App thread determines event E1 applies to CP1 // b) Timer thread destroys CP1 // c) App thread processes E1 for CP1, filter-faulting and ending up reprocessing the event against CTX because of this handler // AgentInstanceContext aiCreate = _contextControllerInitTerm.Factory.FactoryContext.AgentInstanceContextCreate; using (aiCreate.EpStatementAgentInstanceHandle.StatementAgentInstanceLock.AcquireWriteLock()) { EventBean trigger = _contextControllerInitTerm._nonDistinctLastTrigger; if (theEvent != trigger) { StatementAgentInstanceUtil.EvaluateEventForStatement( _contextControllerInitTerm.Factory.FactoryContext.ServicesContext, theEvent, null, Collections.SingletonList(new AgentInstance(null, aiCreate, null))); } return(true); // we handled the event } }
public override EPStatementStartResult StartInternal(EPServicesContext services, StatementContext statementContext, bool isNewStatement, bool isRecoveringStatement, bool isRecoveringResilient) { // validate use of table: may not both read and write ValidateTableAccessUse(StatementSpec.IntoTableSpec, StatementSpec.TableNodes); var contextName = StatementSpec.OptionalContextName; var defaultAgentInstanceContext = GetDefaultAgentInstanceContext(statementContext); var selectDesc = EPStatementStartMethodSelectUtil.Prepare(StatementSpec, services, statementContext, isRecoveringResilient, defaultAgentInstanceContext, IsQueryPlanLogging(services), null, null, null); statementContext.StatementAgentInstanceFactory = selectDesc.StatementAgentInstanceFactorySelect; // allow extension to walk statementContext.StatementExtensionServicesContext.PreStartWalk(selectDesc); // Determine context EPStatementStopMethod stopStatementMethod; Viewable finalViewable; AggregationService aggregationService; IDictionary <ExprSubselectNode, SubSelectStrategyHolder> subselectStrategyInstances; IDictionary <ExprPriorNode, ExprPriorEvalStrategy> priorStrategyInstances; IDictionary <ExprPreviousNode, ExprPreviousEvalStrategy> previousStrategyInstances; IDictionary <ExprTableAccessNode, ExprTableAccessEvalStrategy> tableAccessStrategyInstances; var preloadList = Collections.GetEmptyList <StatementAgentInstancePreload>(); RegexExprPreviousEvalStrategy matchRecognizePrevEvalStrategy; // With context - delegate instantiation to context if (StatementSpec.OptionalContextName != null) { // use statement-wide agent-instance-specific aggregation service aggregationService = statementContext.StatementAgentInstanceRegistry.AgentInstanceAggregationService; // use statement-wide agent-instance-specific subselects var aiRegistryExpr = statementContext.StatementAgentInstanceRegistry.AgentInstanceExprService; subselectStrategyInstances = new Dictionary <ExprSubselectNode, SubSelectStrategyHolder>(); foreach (var entry in selectDesc.SubSelectStrategyCollection.Subqueries) { var specificService = aiRegistryExpr.AllocateSubselect(entry.Key); entry.Key.Strategy = specificService; IDictionary <ExprPriorNode, ExprPriorEvalStrategy> subselectPriorStrategies = new Dictionary <ExprPriorNode, ExprPriorEvalStrategy>(); foreach (var subselectPrior in entry.Value.PriorNodesList) { var specificSubselectPriorService = aiRegistryExpr.AllocatePrior(subselectPrior); subselectPriorStrategies.Put(subselectPrior, specificSubselectPriorService); } IDictionary <ExprPreviousNode, ExprPreviousEvalStrategy> subselectPreviousStrategies = new Dictionary <ExprPreviousNode, ExprPreviousEvalStrategy>(); foreach (var subselectPrevious in entry.Value.PrevNodesList) { var specificSubselectPreviousService = aiRegistryExpr.AllocatePrevious(subselectPrevious); subselectPreviousStrategies.Put(subselectPrevious, specificSubselectPreviousService); } var subselectAggregation = aiRegistryExpr.AllocateSubselectAggregation(entry.Key); var strategyHolder = new SubSelectStrategyHolder(specificService, subselectAggregation, subselectPriorStrategies, subselectPreviousStrategies, null, null, null); subselectStrategyInstances.Put(entry.Key, strategyHolder); } // use statement-wide agent-instance-specific "prior" priorStrategyInstances = new Dictionary <ExprPriorNode, ExprPriorEvalStrategy>(); foreach (var priorNode in selectDesc.ViewResourceDelegateUnverified.PriorRequests) { var specificService = aiRegistryExpr.AllocatePrior(priorNode); priorStrategyInstances.Put(priorNode, specificService); } // use statement-wide agent-instance-specific "previous" previousStrategyInstances = new Dictionary <ExprPreviousNode, ExprPreviousEvalStrategy>(); foreach (var previousNode in selectDesc.ViewResourceDelegateUnverified.PreviousRequests) { var specificService = aiRegistryExpr.AllocatePrevious(previousNode); previousStrategyInstances.Put(previousNode, specificService); } // use statement-wide agent-instance-specific match-recognize "previous" matchRecognizePrevEvalStrategy = aiRegistryExpr.AllocateMatchRecognizePrevious(); // use statement-wide agent-instance-specific tables tableAccessStrategyInstances = new Dictionary <ExprTableAccessNode, ExprTableAccessEvalStrategy>(); if (StatementSpec.TableNodes != null) { foreach (ExprTableAccessNode tableNode in StatementSpec.TableNodes) { var specificService = aiRegistryExpr.AllocateTableAccess(tableNode); tableAccessStrategyInstances.Put(tableNode, specificService); } } var mergeView = new ContextMergeView(selectDesc.ResultSetProcessorPrototypeDesc.ResultSetProcessorFactory.ResultEventType); finalViewable = mergeView; var statement = new ContextManagedStatementSelectDesc(StatementSpec, statementContext, mergeView, selectDesc.StatementAgentInstanceFactorySelect, selectDesc.ResultSetProcessorPrototypeDesc.AggregationServiceFactoryDesc.Expressions, selectDesc.SubSelectStrategyCollection); services.ContextManagementService.AddStatement(contextName, statement, isRecoveringResilient); var selectStop = selectDesc.StopMethod; stopStatementMethod = new ProxyEPStatementStopMethod(() => { services.ContextManagementService.StoppedStatement( contextName, statementContext.StatementName, statementContext.StatementId, statementContext.Expression, statementContext.ExceptionHandlingService); selectStop.Stop(); }); selectDesc.DestroyCallbacks.AddCallback(new EPStatementDestroyCallbackContext(services.ContextManagementService, contextName, statementContext.StatementName, statementContext.StatementId)); } // Without context - start here else { var resultOfStart = (StatementAgentInstanceFactorySelectResult)selectDesc.StatementAgentInstanceFactorySelect.NewContext(defaultAgentInstanceContext, isRecoveringResilient); finalViewable = resultOfStart.FinalView; var startResultStop = services.EpStatementFactory.MakeStopMethod(resultOfStart); var selectStop = selectDesc.StopMethod; stopStatementMethod = new ProxyEPStatementStopMethod(() => { StatementAgentInstanceUtil.StopSafe(startResultStop, statementContext); selectStop.Stop(); }); aggregationService = resultOfStart.OptionalAggegationService; subselectStrategyInstances = resultOfStart.SubselectStrategies; priorStrategyInstances = resultOfStart.PriorNodeStrategies; previousStrategyInstances = resultOfStart.PreviousNodeStrategies; tableAccessStrategyInstances = resultOfStart.TableAccessEvalStrategies; preloadList = resultOfStart.PreloadList; matchRecognizePrevEvalStrategy = null; if (resultOfStart.TopViews.Length > 0) { EventRowRegexNFAViewService matchRecognize = EventRowRegexHelper.RecursiveFindRegexService(resultOfStart.TopViews[0]); if (matchRecognize != null) { matchRecognizePrevEvalStrategy = matchRecognize.PreviousEvaluationStrategy; } } if (statementContext.StatementExtensionServicesContext != null && statementContext.StatementExtensionServicesContext.StmtResources != null) { StatementResourceHolder holder = statementContext.StatementExtensionServicesContext.ExtractStatementResourceHolder(resultOfStart); statementContext.StatementExtensionServicesContext.StmtResources.Unpartitioned = holder; statementContext.StatementExtensionServicesContext.PostProcessStart(resultOfStart, isRecoveringResilient); } } var matchRecognizeNodes = selectDesc.StatementAgentInstanceFactorySelect.ViewResourceDelegate.PerStream[0].MatchRecognizePreviousRequests; // assign strategies to expression nodes EPStatementStartMethodHelperAssignExpr.AssignExpressionStrategies( selectDesc, aggregationService, subselectStrategyInstances, priorStrategyInstances, previousStrategyInstances, matchRecognizeNodes, matchRecognizePrevEvalStrategy, tableAccessStrategyInstances); // execute preload if any foreach (var preload in preloadList) { preload.ExecutePreload(); } // handle association to table if (StatementSpec.IntoTableSpec != null) { services.StatementVariableRefService.AddReferences(statementContext.StatementName, StatementSpec.IntoTableSpec.Name); } return(new EPStatementStartResult(finalViewable, stopStatementMethod, selectDesc.DestroyCallbacks)); }
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); }
public void ContextPartitionNavigate( ContextControllerInstanceHandle existingHandle, ContextController originator, ContextControllerState controllerState, int exportedCPOrPathId, ContextInternalFilterAddendum filterAddendum, AgentInstanceSelector agentInstanceSelector, byte[] payload, bool isRecoveringResilient) { var nestedHandle = (ContextManagerNestedInstanceHandle)existingHandle; // detect non-leaf var nestingLevel = originator.Factory.FactoryContext.NestingLevel; // starts at 1 for root if (nestingLevel < _nestedContextFactories.Length) { nestedHandle.Controller.ImportContextPartitions( controllerState, exportedCPOrPathId, filterAddendum, agentInstanceSelector); return; } var entry = _subcontexts.Get(originator); if (entry == null) { return; } foreach (var cpEntry in entry.AgentInstances.ToArray()) { if (cpEntry.Value.State == ContextPartitionState.STOPPED) { cpEntry.Value.State = ContextPartitionState.STARTED; entry.AgentInstances.Clear(); foreach (var statement in _statements) { var instance = StartStatement( existingHandle.ContextPartitionOrPathId, statement.Value, originator, cpEntry.Value.InitPartitionKey, entry.InitContextProperties, false); cpEntry.Value.AgentInstances.Add(instance); } var key = new ContextStatePathKey( _nestedContextFactories.Length, originator.PathId, existingHandle.SubPathId); var value = new ContextStatePathValue( existingHandle.ContextPartitionOrPathId, payload, ContextPartitionState.STARTED); originator.Factory.FactoryContext.StateCache.UpdateContextPath(_contextName, key, value); } else { IList <AgentInstance> removed = new List <AgentInstance>(2); IList <AgentInstance> added = new List <AgentInstance>(2); var current = cpEntry.Value.AgentInstances; foreach (var agentInstance in current) { if (!agentInstanceSelector.Select(agentInstance)) { continue; } // remove StatementAgentInstanceUtil.StopAgentInstanceRemoveResources( agentInstance, null, _servicesContext, false, false); removed.Add(agentInstance); // start var statementDesc = _statements.Get(agentInstance.AgentInstanceContext.StatementId); var instance = StartStatement( cpEntry.Key, statementDesc, originator, cpEntry.Value.InitPartitionKey, entry.InitContextProperties, false); added.Add(instance); if (controllerState.PartitionImportCallback != null) { controllerState.PartitionImportCallback.Existing( existingHandle.ContextPartitionOrPathId, exportedCPOrPathId); } } current.RemoveAll(removed); current.AddAll(added); } } }
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); }
public ContextControllerInstanceHandle ContextPartitionInstantiate( int?optionalContextPartitionId, int subPathId, int?importSubpathId, ContextController originator, EventBean optionalTriggeringEvent, IDictionary <String, Object> optionalTriggeringPattern, Object partitionKey, IDictionary <String, Object> contextProperties, ContextControllerState states, ContextInternalFilterAddendum filterAddendum, bool isRecoveringResilient, ContextPartitionState state) { using (_iLock.Acquire()) { ContextControllerTreeEntry entry; // detect non-leaf var nestingLevel = originator.Factory.FactoryContext.NestingLevel; // starts at 1 for root if (nestingLevel < _nestedContextFactories.Length) { // next sub-sontext var nextFactory = _nestedContextFactories[originator.Factory.FactoryContext.NestingLevel]; var nextContext = nextFactory.CreateNoCallback(subPathId, this); // link current context to sub-context var branch = _subcontexts.Get(originator); if (branch.ChildContexts == null) { branch.ChildContexts = new Dictionary <int, ContextController>(); } branch.ChildContexts.Put(subPathId, nextContext); // save child branch, linking sub-context to its parent entry = new ContextControllerTreeEntry(originator, null, partitionKey, contextProperties); _subcontexts.Put(nextContext, entry); // now post-initialize, this may actually call back nextContext.Activate( optionalTriggeringEvent, optionalTriggeringPattern, states, filterAddendum, importSubpathId); if (Log.IsDebugEnabled) { Log.Debug( "Instantiating branch context path for " + _contextName + " from level " + originator.Factory.FactoryContext.NestingLevel + "(" + originator.Factory.FactoryContext.ContextName + ")" + " parentPath " + originator.PathId + " for level " + nextContext.Factory.FactoryContext.NestingLevel + "(" + nextContext.Factory.FactoryContext.ContextName + ")" + " childPath " + subPathId ); } return(new ContextManagerNestedInstanceHandle(subPathId, nextContext, subPathId, true, null)); } // assign context id int assignedContextId; if (optionalContextPartitionId != null && !states.IsImported) { assignedContextId = optionalContextPartitionId.Value; _contextPartitionIdManager.AddExisting(optionalContextPartitionId.Value); } else { assignedContextId = _contextPartitionIdManager.AllocateId(); if (states != null && states.PartitionImportCallback != null && optionalContextPartitionId != null) { states.PartitionImportCallback.Allocated(assignedContextId, optionalContextPartitionId.Value); } } if (Log.IsDebugEnabled) { Log.Debug( "Instantiating agent instance for " + _contextName + " from level " + originator.Factory.FactoryContext.NestingLevel + "(" + originator.Factory.FactoryContext.ContextName + ")" + " parentPath " + originator.PathId + " contextPartId " + assignedContextId); } // handle leaf creation IList <AgentInstance> newInstances = new List <AgentInstance>(); if (state == ContextPartitionState.STARTED) { foreach (var statementEntry in _statements) { var statementDesc = statementEntry.Value; var instance = StartStatement( assignedContextId, statementDesc, originator, partitionKey, contextProperties, isRecoveringResilient); newInstances.Add(instance); } } // for all new contexts: evaluate this event for this statement if (optionalTriggeringEvent != null) { StatementAgentInstanceUtil.EvaluateEventForStatement( _servicesContext, optionalTriggeringEvent, optionalTriggeringPattern, newInstances); } // save leaf entry = _subcontexts.Get(originator); if (entry.AgentInstances == null) { entry.AgentInstances = new LinkedHashMap <int, ContextControllerTreeAgentInstanceList>(); } var filterVersion = _servicesContext.FilterService.FiltersVersion; var agentInstanceList = new ContextControllerTreeAgentInstanceList( filterVersion, partitionKey, contextProperties, newInstances, state); entry.AgentInstances.Put(assignedContextId, agentInstanceList); return(new ContextManagerNestedInstanceHandle( subPathId, originator, assignedContextId, false, agentInstanceList)); } }