public override ContextPartitionIdentifier KeyPayloadToIdentifier(object payload)
        {
            ContextControllerInitTermState state = (ContextControllerInitTermState)payload;

            return(new ContextPartitionIdentifierInitiatedTerminated(
                       state == null ? null : state.PatternData,
                       state == null ? 0 : state.StartTime,
                       null));
        }
Ejemplo n.º 2
0
        public void VisitSelectedPartitions(ContextPartitionSelector contextPartitionSelector, ContextPartitionVisitor visitor)
        {
            var nestingLevel = _factory.FactoryContext.NestingLevel;

            if (contextPartitionSelector is ContextPartitionSelectorFiltered)
            {
                var filter     = (ContextPartitionSelectorFiltered)contextPartitionSelector;
                var identifier = new ContextPartitionIdentifierInitiatedTerminated();
                foreach (var entry in EndConditions)
                {
                    identifier.EndTime            = entry.Value.EndTime;
                    identifier.StartTime          = entry.Value.StartTime;
                    identifier.Properties         = entry.Value.StartProperties;
                    identifier.ContextPartitionId = entry.Value.InstanceHandle.ContextPartitionOrPathId;
                    if (filter.Filter(identifier))
                    {
                        var state = new ContextControllerInitTermState(_factory.FactoryContext.ServicesContext.SchedulingService.Time, entry.Value.StartProperties);
                        visitor.Visit(nestingLevel, _pathId, _factory.Binding, state, this, entry.Value.InstanceHandle);
                    }
                }
                return;
            }
            if (contextPartitionSelector is ContextPartitionSelectorById)
            {
                var filter = (ContextPartitionSelectorById)contextPartitionSelector;
                foreach (var entry in EndConditions)
                {
                    if (filter.ContextPartitionIds.Contains(entry.Value.InstanceHandle.ContextPartitionOrPathId))
                    {
                        var state = new ContextControllerInitTermState(_factory.FactoryContext.ServicesContext.SchedulingService.Time, entry.Value.StartProperties);
                        visitor.Visit(nestingLevel, _pathId, _factory.Binding, state, this, entry.Value.InstanceHandle);
                    }
                }
                return;
            }
            if (contextPartitionSelector is ContextPartitionSelectorAll)
            {
                foreach (var entry in EndConditions)
                {
                    var state = new ContextControllerInitTermState(_factory.FactoryContext.ServicesContext.SchedulingService.Time, entry.Value.StartProperties);
                    visitor.Visit(nestingLevel, _pathId, _factory.Binding, state, this, entry.Value.InstanceHandle);
                }
                return;
            }
            throw ContextControllerSelectorUtil.GetInvalidSelector(new Type[0], contextPartitionSelector);
        }
Ejemplo n.º 3
0
            public byte[] ToByteArray(Object contextInfo)
            {
                var state             = (ContextControllerInitTermState)contextInfo;
                var serializableProps = new Dictionary <String, Object>();

                if (state.PatternData != null)
                {
                    serializableProps.PutAll(state.PatternData);
                    foreach (var entry in state.PatternData)
                    {
                        if (entry.Value is EventBean)
                        {
                            var @event = (EventBean)entry.Value;
                            serializableProps.Put(
                                entry.Key,
                                new EventBeanNameValuePair(
                                    @event.EventType.Name, SerializerUtil.ObjectToByteArr(@event.Underlying)));
                        }
                    }
                }
                var serialized = new ContextControllerInitTermState(state.StartTime, serializableProps);

                return(SerializerUtil.ObjectToByteArr(serialized));
            }
Ejemplo n.º 4
0
        public void Activate(EventBean optionalTriggeringEvent, IDictionary <String, Object> optionalTriggeringPattern, ContextControllerState controllerState, ContextInternalFilterAddendum filterAddendum, int?importPathId)
        {
            if (_factory.FactoryContext.NestingLevel == 1)
            {
                controllerState = ContextControllerStateUtil.GetRecoveryStates(_factory.FactoryContext.StateCache, _factory.FactoryContext.OutermostContextName);
            }

            bool currentlyRunning;
            var  contextDetailInitiatedTerminated = _factory.ContextDetailInitiatedTerminated;

            if (controllerState == null)
            {
                StartCondition = MakeEndpoint(contextDetailInitiatedTerminated.Start, filterAddendum, true, 0);

                // if this is single-instance mode, check if we are currently running according to schedule
                currentlyRunning = StartCondition.IsImmediate;
                if (!contextDetailInitiatedTerminated.IsOverlapping)
                {
                    currentlyRunning = DetermineCurrentlyRunning(StartCondition);
                }

                if (currentlyRunning)
                {
                    CurrentSubpathId++;
                    var endEndpoint = MakeEndpoint(contextDetailInitiatedTerminated.End, filterAddendum, false, CurrentSubpathId);
                    endEndpoint.Activate(optionalTriggeringEvent, null, 0, _factory.FactoryContext.IsRecoveringResilient);
                    var startTime      = _factory.SchedulingService.Time;
                    var endTime        = endEndpoint.ExpectedEndTime;
                    var builtinProps   = GetBuiltinProperties(_factory.FactoryContext.ContextName, startTime, endTime, Collections.GetEmptyMap <string, object>());
                    var instanceHandle = ActivationCallback.ContextPartitionInstantiate(null, CurrentSubpathId, null, this, optionalTriggeringEvent, optionalTriggeringPattern, null, builtinProps, controllerState, filterAddendum, _factory.FactoryContext.IsRecoveringResilient, ContextPartitionState.STARTED);
                    EndConditions.Put(endEndpoint, new ContextControllerInitTermInstance(instanceHandle, null, startTime, endTime, CurrentSubpathId));

                    var state = new ContextControllerInitTermState(_factory.FactoryContext.ServicesContext.SchedulingService.Time, builtinProps);
                    _factory.FactoryContext.StateCache.AddContextPath(_factory.FactoryContext.OutermostContextName, _factory.FactoryContext.NestingLevel, _pathId, CurrentSubpathId, instanceHandle.ContextPartitionOrPathId, state, _factory.Binding);
                }

                // non-overlapping and not currently running, or overlapping
                if ((!contextDetailInitiatedTerminated.IsOverlapping && !currentlyRunning) ||
                    contextDetailInitiatedTerminated.IsOverlapping)
                {
                    StartCondition.Activate(optionalTriggeringEvent, null, 0, _factory.FactoryContext.IsRecoveringResilient);
                }
                return;
            }

            StartCondition = MakeEndpoint(contextDetailInitiatedTerminated.Start, filterAddendum, true, 0);

            // if this is single-instance mode, check if we are currently running according to schedule
            currentlyRunning = false;
            if (!contextDetailInitiatedTerminated.IsOverlapping)
            {
                currentlyRunning = DetermineCurrentlyRunning(StartCondition);
            }
            if (!currentlyRunning)
            {
                StartCondition.Activate(optionalTriggeringEvent, null, 0, _factory.FactoryContext.IsRecoveringResilient);
            }

            int pathIdToUse = importPathId ?? _pathId;

            InitializeFromState(optionalTriggeringEvent, optionalTriggeringPattern, filterAddendum, controllerState, pathIdToUse, null, false);
        }
Ejemplo n.º 5
0
        public void RangeNotification(
            IDictionary <String, Object> builtinProperties,
            ContextControllerCondition originCondition,
            EventBean optionalTriggeringEvent,
            IDictionary <String, Object> optionalTriggeringPattern,
            ContextInternalFilterAddendum filterAddendum)
        {
            var endConditionNotification = originCondition != StartCondition;
            var startNow = StartCondition is ContextControllerConditionImmediate;
            IList <AgentInstance> agentInstancesLocksHeld = null;

            _nonDistinctLastTrigger = optionalTriggeringEvent;

            ILockable tempLock = startNow
                ? _factory.FactoryContext.ServicesContext.FilterService.WriteLock
                : new VoidLock();

            using (tempLock.Acquire())
            {
                try
                {
                    if (endConditionNotification)
                    {
                        if (originCondition.IsRunning)
                        {
                            originCondition.Deactivate();
                        }

                        // indicate terminate
                        var instance = EndConditions.Pluck(originCondition);
                        if (instance == null)
                        {
                            return;
                        }

                        // For start-now (non-overlapping only) we hold the lock of the existing agent instance
                        // until the new one is ready.
                        if (startNow)
                        {
                            agentInstancesLocksHeld = new List <AgentInstance>();
                            optionalTriggeringEvent = null;
                            // since we are restarting, we don't want to evaluate the event twice
                            optionalTriggeringPattern = null;
                        }
                        ActivationCallback.ContextPartitionTerminate(
                            instance.InstanceHandle, builtinProperties, startNow, agentInstancesLocksHeld);

                        // remove distinct key
                        RemoveDistinctKey(instance);

                        // re-activate start condition if not overlapping
                        if (!_factory.ContextDetailInitiatedTerminated.IsOverlapping)
                        {
                            StartCondition.Activate(optionalTriggeringEvent, null, 0, false);
                        }

                        _factory.FactoryContext.StateCache.RemoveContextPath(
                            _factory.FactoryContext.OutermostContextName, _factory.FactoryContext.NestingLevel, _pathId,
                            instance.SubPathId);
                    }

                    // handle start-condition notification
                    if (!endConditionNotification || startNow)
                    {
                        // Check if this is distinct-only and the key already exists
                        if (_distinctContexts != null)
                        {
                            var added = AddDistinctKey(optionalTriggeringEvent);
                            if (!added)
                            {
                                return;
                            }
                        }

                        // For single-instance mode, deactivate
                        if (!_factory.ContextDetailInitiatedTerminated.IsOverlapping)
                        {
                            if (StartCondition.IsRunning)
                            {
                                StartCondition.Deactivate();
                            }
                        }
                        // For overlapping mode, make sure we activate again or stay activated
                        else
                        {
                            if (!StartCondition.IsRunning)
                            {
                                StartCondition.Activate(null, null, 0, _factory.FactoryContext.IsRecoveringResilient);
                            }
                        }

                        CurrentSubpathId++;
                        var endEndpoint = MakeEndpoint(
                            _factory.ContextDetailInitiatedTerminated.End, filterAddendum, false, CurrentSubpathId);
                        var matchedEventMap = GetMatchedEventMap(builtinProperties);
                        endEndpoint.Activate(null, matchedEventMap, 0, false);
                        var startTime      = _factory.SchedulingService.Time;
                        var endTime        = endEndpoint.ExpectedEndTime;
                        var builtinProps   = GetBuiltinProperties(_factory.FactoryContext.ContextName, startTime, endTime, builtinProperties);
                        var instanceHandle = ActivationCallback.ContextPartitionInstantiate(
                            null, CurrentSubpathId, null, this, optionalTriggeringEvent, optionalTriggeringPattern,
                            new ContextControllerInitTermState(
                                _factory.SchedulingService.Time, matchedEventMap.MatchingEventsAsMap), builtinProps,
                            null, filterAddendum, _factory.FactoryContext.IsRecoveringResilient,
                            ContextPartitionState.STARTED);
                        EndConditions.Put(
                            endEndpoint,
                            new ContextControllerInitTermInstance(
                                instanceHandle, builtinProperties, startTime, endTime, CurrentSubpathId));

                        // install filter fault handlers, if necessary
                        InstallFilterFaultHandler(instanceHandle);

                        var state =
                            new ContextControllerInitTermState(
                                _factory.FactoryContext.ServicesContext.SchedulingService.Time, builtinProperties);
                        _factory.FactoryContext.StateCache.AddContextPath(
                            _factory.FactoryContext.OutermostContextName, _factory.FactoryContext.NestingLevel, _pathId,
                            CurrentSubpathId, instanceHandle.ContextPartitionOrPathId, state, _factory.Binding);
                    }
                }
                finally
                {
                    if (agentInstancesLocksHeld != null)
                    {
                        foreach (var agentInstance in agentInstancesLocksHeld)
                        {
                            agentInstance.AgentInstanceContext.EpStatementAgentInstanceHandle.StatementFilterVersion.StmtFilterVersion = long.MaxValue;
                            if (agentInstance.AgentInstanceContext.StatementContext.EpStatementHandle.HasTableAccess)
                            {
                                agentInstance.AgentInstanceContext.TableExprEvaluatorContext.ReleaseAcquiredLocks();
                            }
                            agentInstance.AgentInstanceContext.AgentInstanceLock.WriteLock.Release();
                        }
                    }
                }
            }
        }