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);
        }
예제 #2
0
        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));
            }
        }
예제 #3
0
        public void Create(int id, EventBean theEvent)
        {
            lock (this)
            {
                ContextControllerFactoryContext factoryContext = _factory.FactoryContext;
                if (_partitionKeys.ContainsKey(id))
                {
                    return;
                }

                IDictionary <String, Object> properties = ContextPropertyEventType.GetHashBean(factoryContext.ContextName, id);
                _currentSubpathId++;

                // merge filter addendum, if any
                ContextInternalFilterAddendum filterAddendumToUse = _activationFilterAddendum;
                if (_factory.HasFiltersSpecsNestedContexts)
                {
                    filterAddendumToUse = _activationFilterAddendum != null?_activationFilterAddendum.DeepCopy() : new ContextInternalFilterAddendum();

                    _factory.PopulateContextInternalFilterAddendums(filterAddendumToUse, id);
                }

                ContextControllerInstanceHandle handle = _activationCallback.ContextPartitionInstantiate(null, _currentSubpathId, null, this, theEvent, null, id, properties, null, filterAddendumToUse, _factory.FactoryContext.IsRecoveringResilient, ContextPartitionState.STARTED);
                _partitionKeys.Put(id, handle);
                _factory.FactoryContext.StateCache.AddContextPath(factoryContext.OutermostContextName, factoryContext.NestingLevel, _pathId, _currentSubpathId, handle.ContextPartitionOrPathId, id, _factory.Binding);

                // update the filter version for this handle
                long filterVersion = factoryContext.ServicesContext.FilterService.FiltersVersion;
                _factory.FactoryContext.AgentInstanceContextCreate.EpStatementAgentInstanceHandle.StatementFilterVersion.StmtFilterVersion = filterVersion;
            }
        }
예제 #4
0
        public void PopulateContextInternalFilterAddendums(ContextInternalFilterAddendum filterAddendum, object key)
        {
            var assignedContextPartition = key.AsInt();
            var code = assignedContextPartition % _hashedSpec.Granularity;

            GetAddendumFilters(filterAddendum.FilterAddendum, code, _filtersSpecsNestedContexts, _hashedSpec, null);
        }
 public static ContextControllerCondition GetEndpoint(String contextName,
                                                      EPServicesContext servicesContext,
                                                      AgentInstanceContext agentInstanceContext,
                                                      ContextDetailCondition endpoint,
                                                      ContextControllerConditionCallback callback,
                                                      ContextInternalFilterAddendum filterAddendum,
                                                      bool isStartEndpoint,
                                                      int nestingLevel,
                                                      int pathId,
                                                      int subpathId) {
     if (endpoint is ContextDetailConditionCrontab) {
         var crontab = (ContextDetailConditionCrontab) endpoint;
         var scheduleSlot = agentInstanceContext.StatementContext.ScheduleBucket.AllocateSlot();
         return new ContextControllerConditionCrontab(agentInstanceContext.StatementContext, scheduleSlot, crontab, callback, filterAddendum);
     }
     else if (endpoint is ContextDetailConditionFilter) {
         var filter = (ContextDetailConditionFilter) endpoint;
         return new ContextControllerConditionFilter(servicesContext, agentInstanceContext, filter, callback, filterAddendum);
     }
     else if (endpoint is ContextDetailConditionPattern) {
         var key = new ContextStatePathKey(nestingLevel, pathId, subpathId);
         var pattern = (ContextDetailConditionPattern) endpoint;
         return new ContextControllerConditionPattern(servicesContext, agentInstanceContext, pattern, callback, filterAddendum, isStartEndpoint, key);
     }
     else if (endpoint is ContextDetailConditionTimePeriod) {
         var timePeriod = (ContextDetailConditionTimePeriod) endpoint;
         var scheduleSlot = agentInstanceContext.StatementContext.ScheduleBucket.AllocateSlot();
         return new ContextControllerConditionTimePeriod(contextName, agentInstanceContext, scheduleSlot, timePeriod, callback, filterAddendum);
     }
     else if (endpoint is ContextDetailConditionImmediate) {
         return new ContextControllerConditionImmediate();
     }
     throw new IllegalStateException("Unrecognized context range endpoint " + endpoint.GetType());
 }
예제 #6
0
 public void ImportContextPartitions(
     ContextControllerState state,
     int pathIdToUse,
     ContextInternalFilterAddendum filterAddendum,
     AgentInstanceSelector agentInstanceSelector)
 {
     InitializeFromState(null, null, filterAddendum, state, pathIdToUse, agentInstanceSelector);
 }
예제 #7
0
 public void PopulateContextInternalFilterAddendums(ContextInternalFilterAddendum filterAddendum, object key)
 {
     if (_filtersSpecsNestedContexts == null || _filtersSpecsNestedContexts.IsEmpty())
     {
         return;
     }
     ContextControllerPartitionedUtil.PopulateAddendumFilters(key, _filtersSpecsNestedContexts, _segmentedSpec, null, filterAddendum.FilterAddendum);
 }
 public ContextControllerConditionCrontab(StatementContext statementContext, ScheduleSlot scheduleSlot, ContextDetailConditionCrontab spec, ContextControllerConditionCallback callback, ContextInternalFilterAddendum filterAddendum)
 {
     _statementContext = statementContext;
     _scheduleSlot     = scheduleSlot;
     _spec             = spec;
     _callback         = callback;
     _filterAddendum   = filterAddendum;
 }
예제 #9
0
 public ContextControllerConditionFilter(EPServicesContext servicesContext, AgentInstanceContext agentInstanceContext, ContextDetailConditionFilter endpointFilterSpec, ContextControllerConditionCallback callback, ContextInternalFilterAddendum filterAddendum)
 {
     _servicesContext      = servicesContext;
     _agentInstanceContext = agentInstanceContext;
     _endpointFilterSpec   = endpointFilterSpec;
     _callback             = callback;
     _filterAddendum       = filterAddendum;
 }
예제 #10
0
        public void Activate(EventBean optionalTriggeringEvent, IDictionary <String, Object> optionalTriggeringPattern, ContextControllerState controllerState, ContextInternalFilterAddendum activationFilterAddendum, int?importPathId)
        {
            ContextControllerFactoryContext factoryContext = _factory.FactoryContext;

            _activationFilterAddendum = activationFilterAddendum;

            if (factoryContext.NestingLevel == 1)
            {
                controllerState = ContextControllerStateUtil.GetRecoveryStates(_factory.FactoryContext.StateCache, factoryContext.OutermostContextName);
            }
            if (controllerState == null)
            {
                // handle preallocate
                if (_factory.HashedSpec.IsPreallocate)
                {
                    for (int i = 0; i < _factory.HashedSpec.Granularity; i++)
                    {
                        var properties = ContextPropertyEventType.GetHashBean(factoryContext.ContextName, i);
                        _currentSubpathId++;

                        // merge filter addendum, if any
                        var filterAddendumToUse = activationFilterAddendum;
                        if (_factory.HasFiltersSpecsNestedContexts)
                        {
                            filterAddendumToUse = activationFilterAddendum != null?activationFilterAddendum.DeepCopy() : new ContextInternalFilterAddendum();

                            _factory.PopulateContextInternalFilterAddendums(filterAddendumToUse, i);
                        }

                        ContextControllerInstanceHandle handle = _activationCallback.ContextPartitionInstantiate(null, _currentSubpathId, null, this, optionalTriggeringEvent, null, i, properties, controllerState, filterAddendumToUse, _factory.FactoryContext.IsRecoveringResilient, ContextPartitionState.STARTED);
                        _partitionKeys.Put(i, handle);

                        _factory.FactoryContext.StateCache.AddContextPath(
                            _factory.FactoryContext.OutermostContextName,
                            _factory.FactoryContext.NestingLevel,
                            _pathId, _currentSubpathId, handle.ContextPartitionOrPathId, i,
                            _factory.Binding);
                    }
                    return;
                }

                // start filters if not preallocated
                ActivateFilters(optionalTriggeringEvent);

                return;
            }

            // initialize from existing state
            int pathIdToUse = importPathId ?? _pathId;

            InitializeFromState(optionalTriggeringEvent, optionalTriggeringPattern, controllerState, pathIdToUse, null, false);

            // activate filters
            if (!_factory.HashedSpec.IsPreallocate)
            {
                ActivateFilters(null);
            }
        }
 public ContextControllerConditionPattern(EPServicesContext servicesContext, AgentInstanceContext agentInstanceContext, ContextDetailConditionPattern endpointPatternSpec, ContextControllerConditionCallback callback, ContextInternalFilterAddendum filterAddendum, bool startEndpoint, ContextStatePathKey contextStatePathKey)
 {
     _servicesContext      = servicesContext;
     _agentInstanceContext = agentInstanceContext;
     _endpointPatternSpec  = endpointPatternSpec;
     _callback             = callback;
     _filterAddendum       = filterAddendum;
     _isStartEndpoint      = startEndpoint;
     _contextStatePathKey  = contextStatePathKey;
 }
예제 #12
0
        public void Activate(
            EventBean optionalTriggeringEvent,
            IDictionary <String, Object> optionalTriggeringPattern,
            ContextControllerState controllerState,
            ContextInternalFilterAddendum activationFilterAddendum,
            int?importPathId)
        {
            if (Factory.FactoryContext.NestingLevel == 1)
            {
                controllerState = ContextControllerStateUtil.GetRecoveryStates(
                    Factory.StateCache, Factory.FactoryContext.OutermostContextName);
            }

            if (controllerState == null)
            {
                var count = 0;
                foreach (var category in _factory.CategorySpec.Items)
                {
                    var context =
                        ContextPropertyEventType.GetCategorizedBean(
                            Factory.FactoryContext.ContextName, 0, category.Name);
                    _currentSubpathId++;

                    // merge filter addendum, if any
                    var filterAddendumToUse = activationFilterAddendum;
                    if (_factory.HasFiltersSpecsNestedContexts())
                    {
                        filterAddendumToUse = activationFilterAddendum != null
                            ? activationFilterAddendum.DeepCopy()
                            : new ContextInternalFilterAddendum();

                        _factory.PopulateContextInternalFilterAddendums(filterAddendumToUse, count);
                    }

                    var handle = _activationCallback.ContextPartitionInstantiate(
                        null, _currentSubpathId, null, this, optionalTriggeringEvent, optionalTriggeringPattern, count,
                        context, controllerState, filterAddendumToUse, Factory.FactoryContext.IsRecoveringResilient,
                        ContextPartitionState.STARTED);
                    _handleCategories.Put(count, handle);

                    Factory.StateCache.AddContextPath(
                        Factory.FactoryContext.OutermostContextName, Factory.FactoryContext.NestingLevel, PathId,
                        _currentSubpathId, handle.ContextPartitionOrPathId, count, _factory.Binding);
                    count++;
                }
                return;
            }

            var pathIdToUse = importPathId != null ? importPathId.Value : PathId;

            InitializeFromState(
                optionalTriggeringEvent, optionalTriggeringPattern, activationFilterAddendum, controllerState,
                pathIdToUse, null);
        }
예제 #13
0
 public ContextControllerConditionTimePeriod(
     String contextName,
     AgentInstanceContext agentInstanceContext,
     long scheduleSlot,
     ContextDetailConditionTimePeriod spec,
     ContextControllerConditionCallback callback,
     ContextInternalFilterAddendum filterAddendum)
 {
     _contextName          = contextName;
     _agentInstanceContext = agentInstanceContext;
     _scheduleSlot         = scheduleSlot;
     _spec           = spec;
     _callback       = callback;
     _filterAddendum = filterAddendum;
 }
예제 #14
0
        public ContextInternalFilterAddendum DeepCopy()
        {
            var copy = new ContextInternalFilterAddendum();

            foreach (var entry in _filterAddendum)
            {
                var copy2Dim = new FilterValueSetParam[entry.Value.Length][];
                copy.FilterAddendum[entry.Key] = copy2Dim;
                for (int ii = 0; ii < entry.Value.Length; ii++)
                {
                    var copyList = new FilterValueSetParam[entry.Value[ii].Length];
                    copy2Dim[ii] = copyList;
                    Array.Copy(entry.Value[ii], 0, copyList, 0, copyList.Length);
                }
                copy.FilterAddendum[entry.Key] = copy2Dim;
            }
            return(copy);
        }
        public ContextControllerHashedFilterCallback(
            EPServicesContext servicesContext,
            AgentInstanceContext agentInstanceContextCreateContext,
            ContextDetailHashItem hashItem,
            ContextControllerHashedInstanceCallback callback,
            ContextInternalFilterAddendum filterAddendum)
        {
            _agentInstanceContextCreateContext = agentInstanceContextCreateContext;
            _callback = callback;
            _getter   = hashItem.Lookupable.Getter;

            _filterHandle = new EPStatementHandleCallback(agentInstanceContextCreateContext.EpStatementAgentInstanceHandle, this);

            FilterValueSetParam[][] addendum = filterAddendum != null?filterAddendum.GetFilterAddendum(hashItem.FilterSpecCompiled) : null;

            FilterValueSet filterValueSet = hashItem.FilterSpecCompiled.GetValueSet(null, null, addendum);

            _filterServiceEntry = servicesContext.FilterService.Add(filterValueSet, _filterHandle);

            long filtersVersion = servicesContext.FilterService.FiltersVersion;

            agentInstanceContextCreateContext.EpStatementAgentInstanceHandle.StatementFilterVersion.StmtFilterVersion = filtersVersion;
        }
        public ContextControllerPartitionedFilterCallback(EPServicesContext servicesContext, AgentInstanceContext agentInstanceContextCreateContext, ContextDetailPartitionItem partitionItem, ContextControllerPartitionedInstanceCreateCallback callback, ContextInternalFilterAddendum filterAddendum)
        {
            _agentInstanceContextCreateContext = agentInstanceContextCreateContext;
            _callback = callback;

            _filterHandle = new EPStatementHandleCallback(agentInstanceContextCreateContext.EpStatementAgentInstanceHandle, this);

            _getters = new EventPropertyGetter[partitionItem.PropertyNames.Count];
            for (int i = 0; i < partitionItem.PropertyNames.Count; i++)
            {
                var propertyName = partitionItem.PropertyNames[i];
                var getter       = partitionItem.FilterSpecCompiled.FilterForEventType.GetGetter(propertyName);
                _getters[i] = getter;
            }

            var addendum = filterAddendum != null?filterAddendum.GetFilterAddendum(partitionItem.FilterSpecCompiled) : null;

            var filterValueSet = partitionItem.FilterSpecCompiled.GetValueSet(null, null, addendum);

            _filterServiceEntry = servicesContext.FilterService.Add(filterValueSet, _filterHandle);
            var filtersVersion = servicesContext.FilterService.FiltersVersion;

            agentInstanceContextCreateContext.EpStatementAgentInstanceHandle.StatementFilterVersion.StmtFilterVersion = filtersVersion;
        }
예제 #17
0
        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));
            }
        }
예제 #18
0
        private void InitializeFromState(
            EventBean optionalTriggeringEvent,
            IDictionary <String, Object> optionalTriggeringPattern,
            ContextInternalFilterAddendum activationFilterAddendum,
            ContextControllerState controllerState,
            int pathIdToUse,
            AgentInstanceSelector agentInstanceSelector)
        {
            var states        = controllerState.States;
            var childContexts = ContextControllerStateUtil.GetChildContexts(
                Factory.FactoryContext, pathIdToUse, states);

            int maxSubpathId = int.MinValue;

            foreach (var entry in childContexts)
            {
                var categoryNumber = (int)_factory.Binding.ByteArrayToObject(entry.Value.Blob, null);
                ContextDetailCategoryItem category = _factory.CategorySpec.Items[categoryNumber];

                // merge filter addendum, if any
                var filterAddendumToUse = activationFilterAddendum;
                if (_factory.HasFiltersSpecsNestedContexts())
                {
                    filterAddendumToUse = activationFilterAddendum != null
                        ? activationFilterAddendum.DeepCopy()
                        : new ContextInternalFilterAddendum();

                    _factory.PopulateContextInternalFilterAddendums(filterAddendumToUse, categoryNumber);
                }

                // check if exists already
                if (controllerState.IsImported)
                {
                    var existingHandle = _handleCategories.Get(categoryNumber);
                    if (existingHandle != null)
                    {
                        _activationCallback.ContextPartitionNavigate(
                            existingHandle, this, controllerState, entry.Value.OptionalContextPartitionId.Value,
                            filterAddendumToUse, agentInstanceSelector, entry.Value.Blob);
                        continue;
                    }
                }

                var context =
                    ContextPropertyEventType.GetCategorizedBean(Factory.FactoryContext.ContextName, 0, category.Name);

                var contextPartitionId = entry.Value.OptionalContextPartitionId.Value;
                var assignedSubPathId  = !controllerState.IsImported ? entry.Key.SubPath : ++_currentSubpathId;
                var handle             =
                    _activationCallback.ContextPartitionInstantiate(
                        contextPartitionId, assignedSubPathId, entry.Key.SubPath, this, null, null, categoryNumber,
                        context, controllerState, filterAddendumToUse, Factory.FactoryContext.IsRecoveringResilient,
                        entry.Value.State);
                _handleCategories.Put(categoryNumber, handle);

                if (entry.Key.SubPath > maxSubpathId)
                {
                    maxSubpathId = assignedSubPathId;
                }
            }
            if (!controllerState.IsImported)
            {
                _currentSubpathId = maxSubpathId != int.MinValue ? maxSubpathId : 0;
            }
        }
예제 #19
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();
                        }
                    }
                }
            }
        }
예제 #20
0
        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);
                }
            }
        }
예제 #21
0
        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 InitializeFromState(
            EventBean optionalTriggeringEvent,
            IDictionary <String, Object> optionalTriggeringPattern,
            ContextInternalFilterAddendum filterAddendum,
            ContextControllerState controllerState,
            int pathIdToUse,
            AgentInstanceSelector agentInstanceSelector,
            bool loadingExistingState)
        {
            var factoryContext = _factory.FactoryContext;
            var states         = controllerState.States;

            // restart if there are states
            var maxSubpathId        = int.MinValue;
            var childContexts       = ContextControllerStateUtil.GetChildContexts(factoryContext, pathIdToUse, states);
            var eventAdapterService = _factory.FactoryContext.ServicesContext.EventAdapterService;

            foreach (var entry in childContexts)
            {
                var keys   = (Object[])_factory.Binding.ByteArrayToObject(entry.Value.Blob, eventAdapterService);
                var mapKey = GetKeyObjectForLookup(keys);

                // merge filter addendum, if any
                var myFilterAddendum = _activationFilterAddendum;
                if (_factory.HasFiltersSpecsNestedContexts)
                {
                    filterAddendum = _activationFilterAddendum != null
                        ? _activationFilterAddendum.DeepCopy()
                        : new ContextInternalFilterAddendum();

                    _factory.PopulateContextInternalFilterAddendums(filterAddendum, mapKey);
                }

                // check if exists already
                if (controllerState.IsImported)
                {
                    var existingHandle = _partitionKeys.Get(mapKey);
                    if (existingHandle != null)
                    {
                        _activationCallback.ContextPartitionNavigate(
                            existingHandle, this, controllerState, entry.Value.OptionalContextPartitionId.Value,
                            myFilterAddendum, agentInstanceSelector, entry.Value.Blob, loadingExistingState);
                        continue;
                    }
                }

                var props = ContextPropertyEventType.GetPartitionBean(
                    factoryContext.ContextName, 0, mapKey, _factory.SegmentedSpec.Items[0].PropertyNames);

                var assignedSubpathId = !controllerState.IsImported ? entry.Key.SubPath : ++_currentSubpathId;
                var handle            =
                    _activationCallback.ContextPartitionInstantiate(
                        entry.Value.OptionalContextPartitionId, assignedSubpathId, entry.Key.SubPath, this,
                        optionalTriggeringEvent, optionalTriggeringPattern, mapKey, props, controllerState,
                        myFilterAddendum, loadingExistingState || factoryContext.IsRecoveringResilient, entry.Value.State);
                _partitionKeys.Put(mapKey, handle);

                if (entry.Key.SubPath > maxSubpathId)
                {
                    maxSubpathId = assignedSubpathId;
                }
            }
            if (!controllerState.IsImported)
            {
                _currentSubpathId = maxSubpathId != int.MinValue ? maxSubpathId : 0;
            }
        }
예제 #23
0
        private void InitializeFromState(
            EventBean optionalTriggeringEvent,
            IDictionary <String, Object> optionalTriggeringPattern,
            ContextInternalFilterAddendum filterAddendum,
            ContextControllerState controllerState,
            int pathIdToUse,
            AgentInstanceSelector agentInstanceSelector,
            bool loadingExistingState)
        {
            var states              = controllerState.States;
            var childContexts       = ContextControllerStateUtil.GetChildContexts(_factory.FactoryContext, pathIdToUse, states);
            var eventAdapterService = _factory.FactoryContext.ServicesContext.EventAdapterService;

            var maxSubpathId = int.MinValue;

            foreach (var entry in childContexts)
            {
                var state = (ContextControllerInitTermState)_factory.Binding.ByteArrayToObject(entry.Value.Blob, eventAdapterService);

                if (_distinctContexts != null)
                {
                    var filter = (ContextControllerConditionFilter)StartCondition;
                    var @event = (EventBean)state.PatternData.Get(filter.EndpointFilterSpec.OptionalFilterAsName);
                    AddDistinctKey(@event);
                }

                if (controllerState.IsImported)
                {
                    KeyValuePair <ContextControllerCondition, ContextControllerInitTermInstance>?existing = null;
                    foreach (var entryExisting in EndConditions)
                    {
                        if (Compare(state.StartTime, state.PatternData, null,
                                    entryExisting.Value.StartTime, entryExisting.Value.StartProperties, null))
                        {
                            existing = entryExisting;
                            break;
                        }
                    }
                    if (existing != null)
                    {
                        ContextControllerInstanceHandle existingHandle = existing.Value.Value.InstanceHandle;
                        if (existingHandle != null)
                        {
                            ActivationCallback.ContextPartitionNavigate(existingHandle, this, controllerState, entry.Value.OptionalContextPartitionId.Value, filterAddendum, agentInstanceSelector, entry.Value.Blob, loadingExistingState);
                            continue;
                        }
                    }
                }

                var endEndpoint = MakeEndpoint(_factory.ContextDetailInitiatedTerminated.End, filterAddendum, false, entry.Key.SubPath);
                var timeOffset  = _factory.FactoryContext.ServicesContext.SchedulingService.Time - state.StartTime;

                endEndpoint.Activate(optionalTriggeringEvent, null, timeOffset, _factory.FactoryContext.IsRecoveringResilient);
                var startTime          = state.StartTime;
                var endTime            = endEndpoint.ExpectedEndTime;
                var builtinProps       = GetBuiltinProperties(_factory.FactoryContext.ContextName, startTime, endTime, state.PatternData);
                var contextPartitionId = entry.Value.OptionalContextPartitionId;

                var assignedSubPathId = !controllerState.IsImported ? entry.Key.SubPath : ++CurrentSubpathId;
                var instanceHandle    = ActivationCallback.ContextPartitionInstantiate(contextPartitionId, assignedSubPathId, entry.Key.SubPath, this, optionalTriggeringEvent, optionalTriggeringPattern, null, builtinProps, controllerState, filterAddendum, loadingExistingState || _factory.FactoryContext.IsRecoveringResilient, entry.Value.State);
                EndConditions.Put(endEndpoint, new ContextControllerInitTermInstance(instanceHandle, state.PatternData, startTime, endTime, assignedSubPathId));

                if (entry.Key.SubPath > maxSubpathId)
                {
                    maxSubpathId = assignedSubPathId;
                }
            }

            if (!controllerState.IsImported)
            {
                CurrentSubpathId = maxSubpathId != int.MinValue ? maxSubpathId : 0;
            }
        }
예제 #24
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);
        }
예제 #25
0
 protected ContextControllerCondition MakeEndpoint(ContextDetailCondition endpoint, ContextInternalFilterAddendum filterAddendum, bool isStartEndpoint, int subPathId)
 {
     return(ContextControllerConditionFactory.GetEndpoint(_factory.FactoryContext.ContextName, _factory.FactoryContext.ServicesContext, _factory.FactoryContext.AgentInstanceContextCreate,
                                                          endpoint, this, filterAddendum, isStartEndpoint,
                                                          _factory.FactoryContext.NestingLevel, _pathId, subPathId));
 }
        public void PopulateContextInternalFilterAddendums(ContextInternalFilterAddendum filterAddendum, object categoryIndex)
        {
            var category = _categorySpec.Items[categoryIndex.AsInt()];

            GetAddendumFilters(filterAddendum.FilterAddendum, category, _categorySpec, _filtersSpecsNestedContexts, null);
        }