Beispiel #1
0
        public override void Activate(
            IntSeqKey path,
            object[] parentPartitionKeys,
            EventBean optionalTriggeringEvent,
            IDictionary<string, object> optionalTriggeringPattern)
        {
            hashSvc.MgmtCreate(path, parentPartitionKeys);

            if (factory.HashSpec.IsPreallocate) {
                int[] subpathOrCPIds = ActivateByPreallocate(path, parentPartitionKeys, optionalTriggeringEvent);
                hashSvc.MgmtSetSubpathOrCPIdsWhenPreallocate(path, subpathOrCPIds);
                return;
            }

            ContextControllerDetailHashItem[] hashItems = factory.HashSpec.Items;
            ContextControllerFilterEntry[] filterEntries = new ContextControllerFilterEntry[hashItems.Length];

            for (int i = 0; i < hashItems.Length; i++) {
                ContextControllerDetailHashItem item = hashItems[i];
                filterEntries[i] = new ContextControllerHashFilterEntry(this, path, item, parentPartitionKeys);

                if (optionalTriggeringEvent != null) {
                    bool match = AgentInstanceUtil.EvaluateFilterForStatement(
                        optionalTriggeringEvent,
                        realization.AgentInstanceContextCreate,
                        filterEntries[i].FilterHandle);

                    if (match) {
                        MatchFound(item, optionalTriggeringEvent, path);
                    }
                }
            }

            hashSvc.MgmtSetFilters(path, filterEntries);
        }
Beispiel #2
0
 public void RemoveStatement(ContextControllerStatementDesc statementDesc)
 {
     var contextControllers = ContextControllers;
     foreach (var id in ContextManager.ContextPartitionIdService.Ids) {
         AgentInstanceUtil.ContextPartitionTerminate(id, statementDesc, contextControllers, null, false, null);
     }
 }
Beispiel #3
0
        public bool HandleFilterFault(
            EventBean theEvent,
            long version)
        {
            // We handle context-management filter faults always the same way.
            // Statement-partition filter faults are specific to the controller.
            //
            // Hashed-context without preallocate: every time a new bucket shows up the filter version changes, faulting for those that are not aware of the new bucket
            // Example:
            //   T0 sends {key='A', id='E1'}
            //   T1 sends {key='A', id='E1'}
            //   T0 receives create-ctx-ai-lock, processes "matchFound", allocates stmt, which adds filter, sets filter version
            //   T1 encounteres newer filter version, invokes filter fault handler, evaluates event against filters, passes event to statement-partition
            //
            // To avoid duplicate processing into the statement-partition, filter by comparing statement-partition filter version.
            var ids = ContextManager.ContextPartitionIdService.Ids;
            foreach (var stmt in ContextManager.Statements) {
                var agentInstances = ContextManagerUtil.GetAgentInstancesFiltered(
                    stmt.Value,
                    ids,
                    agentInstance => agentInstance.AgentInstanceContext.FilterVersionAfterAllocation >= version);
                AgentInstanceUtil.EvaluateEventForStatement(theEvent, null, agentInstances, AgentInstanceContextCreate);
            }

            return false;
        }
Beispiel #4
0
        public void StartLateStatement(ContextControllerStatementDesc statement)
        {
            var ids = ContextManager.ContextPartitionIdService.Ids;
            foreach (var cpid in ids) {
                var partitionKeys = ContextManager.ContextPartitionIdService.GetPartitionKeys(cpid);

                // create context properties bean
                var contextBean = ContextManagerUtil.BuildContextProperties(
                    cpid,
                    partitionKeys,
                    ContextManager.ContextDefinition,
                    AgentInstanceContextCreate.StatementContext);

                // create filter proxies
                Supplier<IDictionary<FilterSpecActivatable, FilterValueSetParam[][]>> generator = () =>
                    ContextManagerUtil.ComputeAddendumForStatement(
                        statement,
                        ContextManager.Statements,
                        ContextManager.ContextDefinition.ControllerFactories,
                        partitionKeys,
                        AgentInstanceContextCreate);
                AgentInstanceFilterProxy proxy = new AgentInstanceFilterProxyImpl(generator);

                // start
                AgentInstanceUtil.StartStatement(
                    ContextManager.StatementContextCreate.StatementContextRuntimeServices,
                    cpid,
                    statement,
                    contextBean,
                    proxy);
            }
        }
Beispiel #5
0
        public bool Activate(
            EventBean optionalTriggeringEvent,
            ContextControllerEndConditionMatchEventProvider endConditionMatchEventProvider)
        {
            AgentInstanceContext agentInstanceContext = controller.Realization.AgentInstanceContextCreate;

            FilterHandleCallback filterCallback = new ProxyFilterHandleCallback() {
                ProcMatchFound = (
                    theEvent,
                    allStmtMatches) => FilterMatchFound(theEvent),
                ProcIsSubselect = () => false,
            };

            FilterValueSetParam[][] addendum = ContextManagerUtil.ComputeAddendumNonStmt(
                partitionKeys,
                filter.FilterSpecActivatable,
                controller.Realization);
            filterHandle = new EPStatementHandleCallbackFilter(
                agentInstanceContext.EpStatementAgentInstanceHandle,
                filterCallback);
            FilterValueSetParam[][] filterValueSet = filter.FilterSpecActivatable.GetValueSet(
                null,
                addendum,
                agentInstanceContext,
                agentInstanceContext.StatementContextFilterEvalEnv);
            agentInstanceContext.FilterService.Add(
                filter.FilterSpecActivatable.FilterForEventType,
                filterValueSet,
                filterHandle);
            long filtersVersion = agentInstanceContext.FilterService.FiltersVersion;
            agentInstanceContext.EpStatementAgentInstanceHandle.StatementFilterVersion.StmtFilterVersion =
                filtersVersion;

            bool match = false;
            if (optionalTriggeringEvent != null) {
                match = AgentInstanceUtil.EvaluateFilterForStatement(
                    optionalTriggeringEvent,
                    agentInstanceContext,
                    filterHandle);
            }

            return match;
        }
Beispiel #6
0
        private ContextControllerFilterEntry ActivateFilterNoInit(
            ContextControllerDetailKeyedItem item,
            EventBean optionalTriggeringEvent,
            IntSeqKey controllerPath,
            object[] parentPartitionKeys)
        {
            var callback = new ContextControllerKeyedFilterEntryNoInit(this, controllerPath, parentPartitionKeys, item);
            if (optionalTriggeringEvent != null) {
                var match = AgentInstanceUtil.EvaluateFilterForStatement(
                    optionalTriggeringEvent,
                    realization.AgentInstanceContextCreate,
                    callback.FilterHandle);

                if (match) {
                    callback.MatchFound(optionalTriggeringEvent, null);
                }
            }

            return callback;
        }
            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
                var aiCreate = contextControllerInitTerm.Realization.AgentInstanceContextCreate;
                using (aiCreate.EpStatementAgentInstanceHandle.StatementAgentInstanceLock.AcquireWriteLock()) {
                    var trigger = contextControllerInitTerm.LastTriggerEvent;
                    if (theEvent != trigger) {
                        AgentInstanceUtil.EvaluateEventForStatement(
                            theEvent,
                            null,
                            Collections.SingletonList(new AgentInstance(null, aiCreate, null)),
                            aiCreate);
                    }

                    return true; // we handled the event
                }
            }
Beispiel #8
0
        public bool Activate(
            EventBean optionalTriggeringEvent,
            ContextControllerEndConditionMatchEventProvider endConditionMatchEventProvider,
            IDictionary<string, object> optionalTriggeringPattern)
        {
            var agentInstanceContext = controller.Realization.AgentInstanceContextCreate;

            FilterHandleCallback filterCallback = new ProxyFilterHandleCallback() {
                ProcMatchFound = (
                    theEvent,
                    allStmtMatches) => FilterMatchFound(theEvent),
                ProcIsSubselect = () => false,
            };

            filterHandle = new EPStatementHandleCallbackFilter(
                agentInstanceContext.EpStatementAgentInstanceHandle,
                filterCallback);
            var filterValueSet = ComputeFilterValues(agentInstanceContext);
            if (filterValueSet != null) {
                agentInstanceContext.FilterService.Add(
                    filter.FilterSpecActivatable.FilterForEventType,
                    filterValueSet,
                    filterHandle);
                var filtersVersion = agentInstanceContext.FilterService.FiltersVersion;
                agentInstanceContext.EpStatementAgentInstanceHandle.StatementFilterVersion.StmtFilterVersion = filtersVersion;
            }

            var match = false;
            if (optionalTriggeringEvent != null) {
                match = AgentInstanceUtil.EvaluateFilterForStatement(
                    optionalTriggeringEvent,
                    agentInstanceContext,
                    filterHandle);
            }

            return match;
        }
Beispiel #9
0
 public IReaderWriterLock ObtainAgentInstanceLock(
     StatementContext statementContext,
     int agentInstanceId)
 {
     return AgentInstanceUtil.NewLock(statementContext);
 }
Beispiel #10
0
        public ContextPartitionInstantiationResult ContextPartitionInstantiate(
            IntSeqKey controllerPathId,
            int subpathId,
            ContextController originator,
            EventBean optionalTriggeringEvent,
            IDictionary<string, object> optionalPatternForInclusiveEval,
            object[] parentPartitionKeys,
            object partitionKey)
        {
            // detect non-leaf
            var controllerEnv = originator.Factory.FactoryEnv;
            if (controllerPathId.Length != controllerEnv.NestingLevel - 1) {
                throw new IllegalStateException("Unexpected controller path");
            }

            if (parentPartitionKeys.Length != controllerEnv.NestingLevel - 1) {
                throw new IllegalStateException("Unexpected partition key size");
            }

            var nestingLevel = controllerEnv.NestingLevel; // starts at 1 for root
            if (nestingLevel < ContextControllers.Length) {
                // next sub-sontext
                var nextContext = ContextControllers[nestingLevel];

                // add a partition key
                var nestedPartitionKeys = AddPartitionKey(nestingLevel, parentPartitionKeys, partitionKey);

                // now post-initialize, this may actually call back
                var childPath = controllerPathId.AddToEnd(subpathId);
                nextContext.Activate(
                    childPath,
                    nestedPartitionKeys,
                    optionalTriggeringEvent,
                    optionalPatternForInclusiveEval);

                return new ContextPartitionInstantiationResult(subpathId, Collections.GetEmptyList<AgentInstance>());
            }

            // assign context id
            var allPartitionKeys = CollectionUtil.AddValue(parentPartitionKeys, partitionKey);
            var assignedContextId = ContextManager.ContextPartitionIdService.AllocateId(allPartitionKeys);

            // build built-in context properties
            var contextBean = ContextManagerUtil.BuildContextProperties(
                assignedContextId,
                allPartitionKeys,
                ContextManager.ContextDefinition,
                AgentInstanceContextCreate.StatementContext);

            // handle leaf creation
            IList<AgentInstance> startedInstances = new List<AgentInstance>(2);
            foreach (var statementEntry in ContextManager.Statements) {
                var statementDesc = statementEntry.Value;

                Supplier<IDictionary<FilterSpecActivatable, FilterValueSetParam[][]>> generator = () =>
                    ContextManagerUtil.ComputeAddendumForStatement(
                        statementDesc,
                        ContextManager.Statements,
                        ContextManager.ContextDefinition.ControllerFactories,
                        allPartitionKeys,
                        AgentInstanceContextCreate);
                
                AgentInstanceFilterProxy proxy = new AgentInstanceFilterProxyImpl(generator);

                var agentInstance = AgentInstanceUtil.StartStatement(
                    ContextManager.StatementContextCreate.StatementContextRuntimeServices,
                    assignedContextId,
                    statementDesc,
                    contextBean,
                    proxy);
                startedInstances.Add(agentInstance);
            }

            // for all new contexts: evaluate this event for this statement
            if (optionalTriggeringEvent != null || optionalPatternForInclusiveEval != null) {
                // comment-in: log.info("Thread " + Thread.currentThread().getId() + " event " + optionalTriggeringEvent.getUnderlying() + " evaluateEventForStatement assignedContextId=" + assignedContextId);
                AgentInstanceUtil.EvaluateEventForStatement(
                    optionalTriggeringEvent,
                    optionalPatternForInclusiveEval,
                    startedInstances,
                    AgentInstanceContextCreate);
            }

            if (ContextManager.ListenersMayNull != null) {
                var identifier = ContextManager.GetContextPartitionIdentifier(allPartitionKeys);
                ContextStateEventUtil.DispatchPartition(
                    ContextManager.ListenersMayNull,
                    () => new ContextStateEventContextPartitionAllocated(
                        AgentInstanceContextCreate.RuntimeURI,
                        ContextManager.ContextRuntimeDescriptor.ContextDeploymentId,
                        ContextManager.ContextDefinition.ContextName,
                        assignedContextId,
                        identifier),
                    (
                        listener,
                        context) => listener.OnContextPartitionAllocated(context));
            }

            return new ContextPartitionInstantiationResult(assignedContextId, startedInstances);
        }
Beispiel #11
0
        public void ContextPartitionTerminate(
            IntSeqKey controllerPath,
            int subpathIdOrCPId,
            ContextController originator,
            IDictionary<string, object> terminationProperties,
            bool leaveLocksAcquired,
            IList<AgentInstance> agentInstancesLocksHeld)
        {
            if (controllerPath.Length != originator.Factory.FactoryEnv.NestingLevel - 1) {
                throw new IllegalStateException("Unrecognized controller path");
            }

            // detect non-leaf
            var controllerEnv = originator.Factory.FactoryEnv;
            var nestingLevel = controllerEnv.NestingLevel; // starts at 1 for root
            if (nestingLevel < ContextControllers.Length) {
                var childController = ContextControllers[nestingLevel];
                var path = controllerPath.AddToEnd(subpathIdOrCPId);
                childController.Deactivate(path, true);
                return;
            }

            var agentInstanceId = subpathIdOrCPId;

            // stop - in reverse order of statements, to allow termination to use tables+named-windows
            var contextControllers = ContextControllers;
            var contextControllerStatementDescList =
                new List<ContextControllerStatementDesc>(ContextManager.Statements.Values);
            contextControllerStatementDescList.Reverse();
            foreach (var statementDesc in contextControllerStatementDescList) {
                AgentInstanceUtil.ContextPartitionTerminate(
                    agentInstanceId,
                    statementDesc,
                    contextControllers,
                    terminationProperties,
                    leaveLocksAcquired,
                    agentInstancesLocksHeld);
            }

            // remove all context partition statement resources
            foreach (var statementEntry in ContextManager.Statements) {
                var statementDesc = statementEntry.Value;
                var svc = statementDesc.Lightweight.StatementContext.StatementCPCacheService.StatementResourceService;
                var holder = svc.DeallocatePartitioned(agentInstanceId);
            }

            // remove id
            ContextManager.ContextPartitionIdService.RemoveId(agentInstanceId);
            if (ContextManager.ListenersMayNull != null) {
                ContextStateEventUtil.DispatchPartition(
                    ContextManager.ListenersMayNull,
                    () => new ContextStateEventContextPartitionDeallocated(
                        AgentInstanceContextCreate.RuntimeURI,
                        ContextManager.ContextRuntimeDescriptor.ContextDeploymentId,
                        ContextManager.ContextDefinition.ContextName,
                        agentInstanceId),
                    (
                        listener,
                        context) => listener.OnContextPartitionDeallocated(context));
            }
        }
Beispiel #12
0
 public void Close(DataFlowOpCloseContext closeContext)
 {
     AgentInstanceUtil.Stop(startResult.StopCallback, agentInstanceContext, startResult.FinalView, false, false);
     factory.ResourceRegistry.Deassign(agentInstanceContext.AgentInstanceId);
 }