Exemplo n.º 1
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;
        }
            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
                }
            }
Exemplo n.º 3
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);
        }