Exemple #1
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;
            }
        }
Exemple #2
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);
            }
        }
Exemple #3
0
 public ContextControllerInitTermInstance(ContextControllerInstanceHandle instanceHandle,
                                          IDictionary <String, Object> startProperties,
                                          long startTime,
                                          long?endTime,
                                          int subPathId)
 {
     InstanceHandle  = instanceHandle;
     StartProperties = startProperties;
     StartTime       = startTime;
     EndTime         = endTime;
     SubPathId       = subPathId;
 }
Exemple #4
0
        /// <summary>
        /// Provides the sub-context that ends.
        /// </summary>
        /// <param name="contextNestedHandle">The context nested handle.</param>
        /// <param name="terminationProperties">The termination properties.</param>
        /// <param name="leaveLocksAcquired">if set to <c>true</c> [leave locks acquired].</param>
        /// <param name="agentInstances">The agent instances.</param>
        public void ContextPartitionTerminate(
            ContextControllerInstanceHandle contextNestedHandle,
            IDictionary <String, Object> terminationProperties,
            bool leaveLocksAcquired,
            IList <AgentInstance> agentInstances)
        {
            var handle = (ContextManagerNestedInstanceHandle)contextNestedHandle;

            if (handle.IsBranch)
            {
                var branchHandle = handle;
                var branch       = branchHandle.Controller;
                RecursiveDeactivateStop(branch, leaveLocksAcquired, agentInstances);

                if (Log.IsDebugEnabled)
                {
                    Log.Debug(
                        "Terminated context branch for " + _contextName +
                        " from level " + branch.Factory.FactoryContext.NestingLevel +
                        "(" + branch.Factory.FactoryContext.ContextName + ")" +
                        " parentPath " + branch.PathId);
                }
            }
            else
            {
                var leafHandle = handle;
                var leaf       = leafHandle.Controller;
                var leafEntry  = _subcontexts.Get(leaf);
                if (leafEntry != null)
                {
                    // could be terminated earlier
                    var ailist = leafEntry.AgentInstances.Get(leafHandle.ContextPartitionOrPathId);
                    if (ailist != null)
                    {
                        StatementAgentInstanceUtil.StopAgentInstances(
                            ailist.AgentInstances, null, _servicesContext, false, false);
                        _contextPartitionIdManager.RemoveId(leafHandle.ContextPartitionOrPathId);
                        ailist.AgentInstances.Clear();
                    }

                    if (Log.IsDebugEnabled)
                    {
                        Log.Debug(
                            "Terminated context leaf for " + _contextName +
                            " from level " + leaf.Factory.FactoryContext.NestingLevel +
                            "(" + leaf.Factory.FactoryContext.ContextName + ")" +
                            " parentPath " + leaf.PathId +
                            " contextPartId " + leafHandle.ContextPartitionOrPathId);
                    }
                }
            }
        }
Exemple #5
0
 public void Visit(
     int nestingLevel,
     int pathId,
     ContextStatePathValueBinding binding,
     Object payload,
     ContextController contextController,
     ContextControllerInstanceHandle instanceHandle)
 {
     if (nestingLevel == _maxNestingLevel)
     {
         _agentInstanceIds.Add(instanceHandle.ContextPartitionOrPathId);
     }
 }
Exemple #6
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);
            }
        }
Exemple #7
0
 public void ContextPartitionTerminate(ContextControllerInstanceHandle contextNestedHandle, IDictionary <String, Object> terminationProperties, bool leaveLocksAcquired, IList <AgentInstance> agentInstancesCollected)
 {
     using (_uLock.Acquire()) {
         var handle = (ContextNestedHandleImpl)contextNestedHandle;
         var entry  = _agentInstances.Delete(handle.ContextPartitionOrPathId);
         if (entry != null)
         {
             StatementAgentInstanceUtil.StopAgentInstances(entry.AgentInstances, terminationProperties, _servicesContext, false, leaveLocksAcquired);
             if (agentInstancesCollected != null)
             {
                 agentInstancesCollected.AddAll(entry.AgentInstances);
             }
             entry.AgentInstances.Clear();
             _contextPartitionIdManager.RemoveId(contextNestedHandle.ContextPartitionOrPathId);
         }
     }
 }
Exemple #8
0
        private void InstallFilterFaultHandler(ContextControllerInstanceHandle instanceHandle)
        {
            FilterFaultHandler myFaultHandler = null;

            if (DistinctContexts != null)
            {
                myFaultHandler = new DistinctFilterFaultHandler(this);
            }
            else
            {
                if (StartCondition is ContextControllerConditionFilter)
                {
                    myFaultHandler = new NonDistinctFilterFaultHandler(this);
                }
            }

            if (myFaultHandler != null && instanceHandle.Instances != null)
            {
                foreach (AgentInstance agentInstance in instanceHandle.Instances.AgentInstances)
                {
                    agentInstance.AgentInstanceContext.EpStatementAgentInstanceHandle.FilterFaultHandler = myFaultHandler;
                }
            }
        }
Exemple #9
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;
            }
        }
        public void Visit(int nestingLevel, int pathId, ContextStatePathValueBinding binding, Object payload, ContextController contextController, ContextControllerInstanceHandle instanceHandle)
        {
            ContextStatePathKey key = new ContextStatePathKey(nestingLevel, pathId, instanceHandle.SubPathId);
            int agentInstanceId     = instanceHandle.ContextPartitionOrPathId;

            States.Put(key, new ContextStatePathValue(agentInstanceId, binding.ToByteArray(payload), instanceHandle.Instances.State));

            ContextPartitionState      state      = instanceHandle.Instances.State;
            ContextPartitionIdentifier identifier = contextController.Factory.KeyPayloadToIdentifier(payload);
            ContextPartitionDescriptor descriptor = new ContextPartitionDescriptor(agentInstanceId, identifier, state);

            ContextPartitionInfo.Put(agentInstanceId, descriptor);
        }
Exemple #11
0
        public void Visit(int nestingLevel, int pathId, ContextStatePathValueBinding binding, Object payload, ContextController contextController, ContextControllerInstanceHandle instanceHandle)
        {
            var key             = new ContextStatePathKey(nestingLevel, pathId, instanceHandle.SubPathId);
            int maxNestingLevel = _nestedFactories.Length;

            int contextPartitionOrSubPath = instanceHandle.ContextPartitionOrPathId;

            if (contextController.Factory.FactoryContext.NestingLevel == maxNestingLevel)
            {
                var agentInstances = ControllerAgentInstances.Get(contextController);
                if (agentInstances == null)
                {
                    agentInstances = new List <LeafDesc>();
                    ControllerAgentInstances.Put(contextController, agentInstances);
                }
                var value = new ContextStatePathValue(contextPartitionOrSubPath, binding.ToByteArray(payload), instanceHandle.Instances.State);
                agentInstances.Add(new LeafDesc(key, value));

                // generate a nice payload text from the paths of keys
                var entry      = _subcontexts.Get(contextController);
                var keys       = ContextManagerNested.GetTreeCompositeKey(_nestedFactories, payload, entry, _subcontexts);
                var descriptor = new ContextPartitionDescriptor(contextPartitionOrSubPath, new ContextPartitionIdentifierNested(keys), value.State);
                AgentInstanceInfo.Put(contextPartitionOrSubPath, descriptor);
                States.Put(key, value);
            }
            else
            {
                // handle non-leaf
                Subpaths.Add(contextPartitionOrSubPath);
                States.Put(key, new ContextStatePathValue(contextPartitionOrSubPath, binding.ToByteArray(payload), ContextPartitionState.STARTED));
            }
        }
Exemple #12
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);
                }
            }
        }