public void ImportContextPartitions(
     ContextControllerState state,
     int pathIdToUse,
     ContextInternalFilterAddendum filterAddendum,
     AgentInstanceSelector agentInstanceSelector)
 {
     InitializeFromState(null, null, filterAddendum, state, pathIdToUse, agentInstanceSelector);
 }
        private void InitializeFromState(EventBean optionalTriggeringEvent,
                                         IDictionary <String, Object> optionalTriggeringPattern,
                                         ContextControllerState controllerState,
                                         int pathIdToUse,
                                         AgentInstanceSelector agentInstanceSelector)
        {
            var factoryContext = _factory.FactoryContext;
            var states         = controllerState.States;
            var childContexts  = ContextControllerStateUtil.GetChildContexts(factoryContext, pathIdToUse, states);

            var maxSubpathId = int.MinValue;

            foreach (var entry in childContexts)
            {
                var hashAlgoGeneratedId = (int?)_factory.Binding.ByteArrayToObject(entry.Value.Blob, null);

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

                    _factory.PopulateContextInternalFilterAddendums(filterAddendumToUse, hashAlgoGeneratedId);
                }

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

                var properties = ContextPropertyEventType.GetHashBean(factoryContext.ContextName, hashAlgoGeneratedId.Value);

                var assignedSubPathId = !controllerState.IsImported ? entry.Key.SubPath : ++_currentSubpathId;
                var handle            = _activationCallback.ContextPartitionInstantiate(entry.Value.OptionalContextPartitionId, assignedSubPathId, entry.Key.SubPath, this, optionalTriggeringEvent, optionalTriggeringPattern, hashAlgoGeneratedId, properties, controllerState, filterAddendumToUse, factoryContext.IsRecoveringResilient, entry.Value.State);
                _partitionKeys.Put(hashAlgoGeneratedId.Value, handle);

                if (entry.Key.SubPath > maxSubpathId)
                {
                    maxSubpathId = assignedSubPathId;
                }
            }
            if (!controllerState.IsImported)
            {
                _currentSubpathId = maxSubpathId != int.MinValue ? maxSubpathId : 0;
            }
        }
Example #3
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;
            }
        }
Example #4
0
 public void ImportStartPaths(ContextControllerState state, AgentInstanceSelector agentInstanceSelector)
 {
     _rootContext.ImportContextPartitions(state, 0, null, agentInstanceSelector);
 }
Example #5
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);
            }
        }
Example #6
0
        private void InitializeFromState(
            EventBean optionalTriggeringEvent,
            IDictionary <String, Object> optionalTriggeringPattern,
            ContextInternalFilterAddendum activationFilterAddendum,
            ContextControllerState controllerState,
            int pathIdToUse,
            AgentInstanceSelector agentInstanceSelector,
            bool loadingExistingState)
        {
            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, loadingExistingState);
                        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, loadingExistingState || 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;
            }
        }
        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;
            }
        }
        public EPContextPartitionImportResult ImportStartPaths(String contextName, EPContextPartitionImportable importable, AgentInstanceSelector agentInstanceSelector)
        {
            var contextManager = CheckedGetContextManager(contextName);
            var importCallback = new CPImportCallback();
            var state          = new ContextControllerState(importable.Paths, true, importCallback);

            contextManager.ImportStartPaths(state, agentInstanceSelector);

            ContextStateCache contextStateCache = contextManager.ContextStateCache;

            foreach (var entry in importable.Paths)
            {
                entry.Value.State = ContextPartitionState.STARTED;
                contextStateCache.UpdateContextPath(contextName, entry.Key, entry.Value);
            }

            return(new EPContextPartitionImportResult(importCallback.ExistingToImported, importCallback.AllocatedToImported));
        }
Example #9
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);
                }
            }
        }
Example #10
0
        public EPContextPartitionImportResult ImportStartPaths(String contextName, EPContextPartitionImportable importable, AgentInstanceSelector agentInstanceSelector)
        {
            ContextManager         contextManager = CheckedGetContextManager(contextName);
            CPImportCallback       importCallback = new CPImportCallback();
            ContextControllerState state          = new ContextControllerState(importable.Paths, true, importCallback);

            contextManager.ImportStartPaths(state, agentInstanceSelector);
            return(new EPContextPartitionImportResult(importCallback.ExistingToImported, importCallback.AllocatedToImported));
        }