コード例 #1
0
 public void Handle(CoreProjectionManagementMessage.CreateAndPrepareSlave message)
 {
     try
     {
         //TODO: factory method can throw!
         IProjectionStateHandler stateHandler = message.HandlerFactory();
         string name                         = message.Name;
         var    sourceDefinition             = ProjectionSourceDefinition.From(name, stateHandler.GetSourceDefinition(), null, null);
         var    projectionVersion            = message.Version;
         var    projectionConfig             = message.Config.SetIsSlave();
         var    projectionProcessingStrategy =
             _processingStrategySelector.CreateSlaveProjectionProcessingStrategy(
                 name, projectionVersion, sourceDefinition, projectionConfig, stateHandler,
                 message.ResultsPublisher, message.MasterCoreProjectionId, this);
         CreateCoreProjection(message.ProjectionId, projectionConfig.RunAs, projectionProcessingStrategy);
         message.Envelope.ReplyWith(
             new CoreProjectionManagementMessage.Prepared(
                 message.ProjectionId, sourceDefinition, slaveProjections: null));
     }
     catch (Exception ex)
     {
         message.Envelope.ReplyWith(
             new CoreProjectionManagementMessage.Faulted(message.ProjectionId, ex.Message));
     }
 }
コード例 #2
0
        public void can_compile_million_times()
        {
            for (var i = 0; i < 10000000; i++)
            {
                if (_stateHandler != null)
                    _stateHandler.Dispose();
                _stateHandler = null;
/*
                _state = null;
*/
                _projection = null;
                _projection = @"
                fromAll();
                on_raw(function(state, event, streamId, eventType, sequenceNumber, metadata) {
                    emit('output-stream' + sequenceNumber, 'emitted-event' + sequenceNumber, {a: JSON.parse(event).a});
                    return {};
                });
            ";
                _logged = new List<string>();
                _stateHandlerFactory = new ProjectionStateHandlerFactory();
                _stateHandler = _stateHandlerFactory.Create(
                    "JS", _projection, logger: (s, _) =>
                        {
                            if (!s.StartsWith("P:")) _logged.Add(s);
                            else _logDelegate(s);
                        }); // skip prelude debug output
/*
                if (_state != null)
                    _stateHandler.Load(_state);
                else
                    _stateHandler.Initialize();
*/
                Console.Write(".");
            }
        }
コード例 #3
0
 public ParallelQueryProcessingStrategy(
     string name, ProjectionVersion projectionVersion, IProjectionStateHandler stateHandler,
     Func <IProjectionStateHandler> handlerFactory, ProjectionConfig projectionConfig,
     IQueryDefinition sourceDefinition, ProjectionNamesBuilder namesBuilder, ILogger logger,
     SpooledStreamReadingDispatcher spoolProcessingResponseDispatcher,
     ReaderSubscriptionDispatcher subscriptionDispatcher)
     : base(name, projectionVersion, projectionConfig, sourceDefinition, logger, subscriptionDispatcher)
 {
     _stateHandler     = stateHandler;
     _projectionConfig = projectionConfig;
     _sourceDefinition = sourceDefinition;
     _namesBuilder     = namesBuilder;
     _spoolProcessingResponseDispatcher = spoolProcessingResponseDispatcher;
     if (_sourceDefinition.CatalogStream == SystemStreams.AllStream)
     {
         _catalogStreamName = SystemStreams.AllStream;
     }
     else if (_sourceDefinition.HasCategories())
     {
         _catalogStreamName = _namesBuilder.GetCategoryCatalogStreamName(_sourceDefinition.Categories[0]);
     }
     else
     {
         _catalogStreamName = _sourceDefinition.CatalogStream;
     }
 }
コード例 #4
0
        public void Setup()
        {
            _state      = null;
            _projection = null;
            Given();
            _logged = new List <string>();
            _stateHandlerFactory = new ProjectionStateHandlerFactory();
            _stateHandler        = _stateHandlerFactory.Create(
                "JS", _projection, logger: (s, _) => {
                if (s.StartsWith("P:"))
                {
                    Console.WriteLine(s);
                }
                else
                {
                    _logged.Add(s);
                }
            });                     // skip prelude debug output
            _source = _stateHandler.GetSourceDefinition();

            if (_state != null)
            {
                _stateHandler.Load(_state);
            }
            else
            {
                _stateHandler.Initialize();
            }

            if (_sharedState != null)
            {
                _stateHandler.LoadShared(_sharedState);
            }
            When();
        }
コード例 #5
0
        public void Setup()
        {
            _state = null;
            _projection = null;
            Given();
            _logged = new List<string>();
            _stateHandlerFactory = new ProjectionStateHandlerFactory();
            _stateHandler = _stateHandlerFactory.Create(
                "JS", _projection, logger: s =>
                    {
                        if (s.StartsWith("P:"))
                            Console.WriteLine(s);
                        else
                            _logged.Add(s);
                    }); // skip prelude debug output
            _source = _stateHandler.GetSourceDefinition();

            if (_state != null)
                _stateHandler.Load(_state);
            else
                _stateHandler.Initialize();

            if (_sharedState != null)
                _stateHandler.LoadShared(_sharedState);
            When();
        }
コード例 #6
0
        public ProjectionProcessingStrategy CreateProjectionProcessingStrategy(
            string name, ProjectionVersion projectionVersion, ProjectionNamesBuilder namesBuilder,
            IQueryDefinition sourceDefinition, ProjectionConfig projectionConfig,
            Func <IProjectionStateHandler> handlerFactory, IProjectionStateHandler stateHandler)
        {
            if (!sourceDefinition.DisableParallelismOption && projectionConfig.StopOnEof && sourceDefinition.ByStreams &&
                sourceDefinition.DefinesFold && !string.IsNullOrEmpty(sourceDefinition.CatalogStream))
            {
                return(new ParallelQueryProcessingStrategy(
                           name, projectionVersion, stateHandler, handlerFactory, projectionConfig, sourceDefinition,
                           namesBuilder, _logger, _spoolProcessingResponseDispatcher, _subscriptionDispatcher));
            }

            if (!sourceDefinition.DisableParallelismOption && projectionConfig.StopOnEof && sourceDefinition.ByStreams &&
                sourceDefinition.DefinesFold && sourceDefinition.HasCategories())
            {
                return(new ParallelQueryProcessingStrategy(
                           name, projectionVersion, stateHandler, handlerFactory, projectionConfig, sourceDefinition,
                           namesBuilder, _logger, _spoolProcessingResponseDispatcher, _subscriptionDispatcher));
            }

            return(projectionConfig.StopOnEof
                ? (ProjectionProcessingStrategy)
                   new QueryProcessingStrategy(
                       name, projectionVersion, stateHandler, projectionConfig, sourceDefinition, _logger,
                       _subscriptionDispatcher)
                : new ContinuousProjectionProcessingStrategy(
                       name, projectionVersion, stateHandler, projectionConfig, sourceDefinition, _logger,
                       _subscriptionDispatcher));
        }
コード例 #7
0
 public ParallelQueryProcessingStrategy(
     string name, ProjectionVersion projectionVersion, IProjectionStateHandler stateHandler,
     Func<IProjectionStateHandler> handlerFactory, ProjectionConfig projectionConfig,
     IQueryDefinition sourceDefinition, ProjectionNamesBuilder namesBuilder, ILogger logger,
     SpooledStreamReadingDispatcher spoolProcessingResponseDispatcher,
     ReaderSubscriptionDispatcher subscriptionDispatcher)
     : base(name, projectionVersion, projectionConfig, sourceDefinition, logger, subscriptionDispatcher)
 {
     _stateHandler = stateHandler;
     _projectionConfig = projectionConfig;
     _sourceDefinition = sourceDefinition;
     _namesBuilder = namesBuilder;
     _spoolProcessingResponseDispatcher = spoolProcessingResponseDispatcher;
     if (_sourceDefinition.CatalogStream == SystemStreams.AllStream)
     {
         _catalogStreamName = SystemStreams.AllStream;
     }
     else if (_sourceDefinition.HasCategories())
     {
         _catalogStreamName = _namesBuilder.GetCategoryCatalogStreamName(_sourceDefinition.Categories[0]);
     }
     else
     {
         _catalogStreamName = _sourceDefinition.CatalogStream;
     }
 }
コード例 #8
0
        public void Handle(CoreProjectionManagementMessage.CreateAndPrepare message)
        {
            try
            {
                //TODO: factory method can throw
                IProjectionStateHandler stateHandler = message.HandlerFactory();
                string name             = message.Name;
                var    sourceDefinition = ProjectionSourceDefinition.From(
                    name, stateHandler.GetSourceDefinition(), message.HandlerType, message.Query);
                var projectionVersion = message.Version;
                var projectionConfig  = message.Config;
                var namesBuilder      = new ProjectionNamesBuilder(name, sourceDefinition);

                var projectionProcessingStrategy = _processingStrategySelector.CreateProjectionProcessingStrategy(
                    name, projectionVersion, namesBuilder,
                    sourceDefinition, projectionConfig, message.HandlerFactory, stateHandler);

                var slaveProjections = projectionProcessingStrategy.GetSlaveProjections();
                CreateCoreProjection(message.ProjectionId, projectionConfig.RunAs, projectionProcessingStrategy);
                message.Envelope.ReplyWith(
                    new CoreProjectionManagementMessage.Prepared(
                        message.ProjectionId, sourceDefinition, slaveProjections));
            }
            catch (Exception ex)
            {
                message.Envelope.ReplyWith(
                    new CoreProjectionManagementMessage.Faulted(message.ProjectionId, ex.Message));
            }
        }
コード例 #9
0
        public void Setup()
        {
            _state      = null;
            _projection = null;
            Given();
            _logged = new List <string>();
            _stateHandlerFactory =
                new ProjectionStateHandlerFactory(CompilationTimeout, ExecutionTimeout, JavascriptProjectionRuntime.Interpreted);
            _stateHandler = CreateStateHandler();
            _source       = _stateHandler.GetSourceDefinition();

            if (_state != null)
            {
                _stateHandler.Load(_state);
            }
            else
            {
                _stateHandler.Initialize();
            }

            if (_sharedState != null)
            {
                _stateHandler.LoadShared(_sharedState);
            }
            When();
        }
コード例 #10
0
        public void Setup()
        {
            _state      = null;
            _projection = null;
            Given();
            _logged = new List <string>();
            _stateHandlerFactory = new ProjectionStateHandlerFactory();
            _stateHandler        = CreateStateHandler();
            _source = _stateHandler.GetSourceDefinition();

            if (_state != null)
            {
                _stateHandler.Load(_state);
            }
            else
            {
                _stateHandler.Initialize();
            }

            if (_sharedState != null)
            {
                _stateHandler.LoadShared(_sharedState);
            }
            When();
        }
コード例 #11
0
        public void Setup()
        {
            _state      = null;
            _projection = null;
            Given();
            _logged = new List <string>();
            _stateHandlerFactory = new ProjectionStateHandlerFactory(TimeSpan.FromMilliseconds(1000), TimeSpan.FromMilliseconds(500), JavascriptProjectionRuntime.Legacy);
            _stateHandler        = CreateStateHandler();
            _source = _stateHandler.GetSourceDefinition();

            if (_state != null)
            {
                _stateHandler.Load(_state);
            }
            else
            {
                _stateHandler.Initialize();
            }

            if (_sharedState != null)
            {
                _stateHandler.LoadShared(_sharedState);
            }
            When();
        }
コード例 #12
0
 public void setup()
 {
     _state      = null;
     _projection = null;
     Given();
     _logged = new List <string>();
     _stateHandlerFactory = new ProjectionStateHandlerFactory();
     _stateHandler        = _stateHandlerFactory.Create(
         "JS", _projection, s =>
     {
         if (!s.StartsWith("P:"))
         {
             _logged.Add(s);
         }
         else
         {
             Console.WriteLine(s);
         }
     });         // skip prelude debug output
     _source = new SourceRecorder();
     _stateHandler.ConfigureSourceProcessingStrategy(_source);
     if (_state != null)
     {
         _stateHandler.Load(_state);
     }
     else
     {
         _stateHandler.Initialize();
     }
 }
コード例 #13
0
 public ProjectionProcessingStrategy CreateProjectionProcessingStrategy(
     string name,
     ProjectionVersion projectionVersion,
     ProjectionNamesBuilder namesBuilder,
     IQuerySources sourceDefinition,
     ProjectionConfig projectionConfig,
     IProjectionStateHandler stateHandler, string handlerType, string query)
 {
     return(projectionConfig.StopOnEof
                         ? (ProjectionProcessingStrategy)
            new QueryProcessingStrategy(
                name,
                projectionVersion,
                stateHandler,
                projectionConfig,
                sourceDefinition,
                _logger,
                _subscriptionDispatcher)
                         : new ContinuousProjectionProcessingStrategy(
                name,
                projectionVersion,
                stateHandler,
                projectionConfig,
                sourceDefinition,
                _logger,
                _subscriptionDispatcher));
 }
コード例 #14
0
        public ProjectionProcessingStrategy CreateProjectionProcessingStrategy(
            string name, ProjectionVersion projectionVersion, ProjectionNamesBuilder namesBuilder,
            IQueryDefinition sourceDefinition, ProjectionConfig projectionConfig,
            Func<IProjectionStateHandler> handlerFactory, IProjectionStateHandler stateHandler)
        {

            if (!sourceDefinition.DisableParallelismOption && projectionConfig.StopOnEof && sourceDefinition.ByStreams
                && sourceDefinition.DefinesFold && !string.IsNullOrEmpty(sourceDefinition.CatalogStream))
            {
                return new ParallelQueryProcessingStrategy(
                    name, projectionVersion, stateHandler, handlerFactory, projectionConfig, sourceDefinition,
                    namesBuilder, _logger, _spoolProcessingResponseDispatcher, _subscriptionDispatcher);
            }

            if (!sourceDefinition.DisableParallelismOption && projectionConfig.StopOnEof && sourceDefinition.ByStreams
                && sourceDefinition.DefinesFold && sourceDefinition.HasCategories())
            {
                return new ParallelQueryProcessingStrategy(
                    name, projectionVersion, stateHandler, handlerFactory, projectionConfig, sourceDefinition,
                    namesBuilder, _logger, _spoolProcessingResponseDispatcher, _subscriptionDispatcher);
            }

            return projectionConfig.StopOnEof
                ? (ProjectionProcessingStrategy)
                    new QueryProcessingStrategy(
                        name, projectionVersion, stateHandler, projectionConfig, sourceDefinition, _logger,
                        _subscriptionDispatcher)
                : new ContinuousProjectionProcessingStrategy(
                    name, projectionVersion, stateHandler, projectionConfig, sourceDefinition, _logger,
                    _subscriptionDispatcher);
        }
コード例 #15
0
 public void Teardown()
 {
     if (_stateHandler != null)
         _stateHandler.Dispose();
     _stateHandler = null;
     GC.Collect(2, GCCollectionMode.Forced);
     GC.WaitForPendingFinalizers();
 }
コード例 #16
0
 protected DefaultProjectionProcessingStrategy(
     string name, ProjectionVersion projectionVersion, IProjectionStateHandler stateHandler,
     ProjectionConfig projectionConfig, IQuerySources sourceDefinition, ILogger logger,
     ReaderSubscriptionDispatcher subscriptionDispatcher, bool enableContentTypeValidation)
     : base(name, projectionVersion, projectionConfig, sourceDefinition, logger, subscriptionDispatcher, enableContentTypeValidation)
 {
     _stateHandler = stateHandler;
 }
コード例 #17
0
 public StatePartitionSelector CreateStatePartitionSelector(IProjectionStateHandler projectionStateHandler)
 {
     return(_byCustomPartitions
                ? new ByHandleStatePartitionSelector(projectionStateHandler)
                : (_byStream
                       ? (StatePartitionSelector) new ByStreamStatePartitionSelector()
                       : new NoopStatePartitionSelector()));
 }
コード例 #18
0
 public QueryProcessingStrategy(
     string name, ProjectionVersion projectionVersion, IProjectionStateHandler stateHandler,
     ProjectionConfig projectionConfig, IQuerySources sourceDefinition, ILogger logger,
     ReaderSubscriptionDispatcher subscriptionDispatcher)
     : base(
         name, projectionVersion, stateHandler, projectionConfig, sourceDefinition, logger,
         subscriptionDispatcher)
 {
 }
コード例 #19
0
 private static StatePartitionSelector CreateStatePartitionSelector(
     IProjectionStateHandler projectionStateHandler, bool byCustomPartitions, bool byStream)
 {
     return(byCustomPartitions
         ? new ByHandleStatePartitionSelector(projectionStateHandler)
         : (byStream
             ? (StatePartitionSelector) new ByStreamStatePartitionSelector()
             : new NoopStatePartitionSelector()));
 }
コード例 #20
0
 public ProjectionProcessingStrategy CreateSlaveProjectionProcessingStrategy(
     string name, ProjectionVersion projectionVersion, ProjectionSourceDefinition sourceDefinition,
     ProjectionConfig projectionConfig, IProjectionStateHandler stateHandler, IPublisher resultsEnvelope,
     Guid masterCoreProjectionId, ProjectionCoreService projectionCoreService)
 {
     return(new SlaveQueryProcessingStrategy(
                name, projectionVersion, stateHandler, projectionConfig, sourceDefinition, projectionCoreService.Logger,
                resultsEnvelope, masterCoreProjectionId, _subscriptionDispatcher));
 }
コード例 #21
0
 public QueryProcessingStrategy(
     string name, ProjectionVersion projectionVersion, IProjectionStateHandler stateHandler,
     ProjectionConfig projectionConfig, IQuerySources sourceDefinition, ILogger logger,
     ReaderSubscriptionDispatcher subscriptionDispatcher)
     : base(
         name, projectionVersion, stateHandler, projectionConfig, sourceDefinition, logger,
         subscriptionDispatcher)
 {
 }
コード例 #22
0
 public ProjectionProcessingStrategy CreateSlaveProjectionProcessingStrategy(
     string name, ProjectionVersion projectionVersion, ProjectionSourceDefinition sourceDefinition,
     ProjectionConfig projectionConfig, IProjectionStateHandler stateHandler, IPublisher resultsEnvelope,
     Guid masterCoreProjectionId, ProjectionCoreService projectionCoreService)
 {
     return new SlaveQueryProcessingStrategy(
         name, projectionVersion, stateHandler, projectionConfig, sourceDefinition, projectionCoreService.Logger,
         resultsEnvelope, masterCoreProjectionId, _subscriptionDispatcher);
 }
コード例 #23
0
        private void BeginCreateAndPrepare(
            ProjectionStateHandlerFactory handlerFactory, ProjectionConfig config,
            Action onPrepared)
        {
            _onPrepared = onPrepared;
            if (handlerFactory == null)
            {
                throw new ArgumentNullException("handlerFactory");
            }
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            //TODO: which states are allowed here?
            if (_state >= ManagedProjectionState.Preparing)
            {
                throw new InvalidOperationException("Already preparing or has been prepared");
            }

            //TODO: load configuration from the definition


            var createProjectionMessage =
                new CoreProjectionManagementMessage.CreateAndPrepare(
                    new PublishEnvelope(_inputQueue), _id, _name, config, delegate
            {
                // this delegate runs in the context of a projection core thread
                // TODO: move this code to the projection core service as we may be in different processes in the future
                IProjectionStateHandler stateHandler = null;
                try
                {
                    stateHandler = handlerFactory.Create(HandlerType, Query, Console.WriteLine);
                    var checkpointStrategyBuilder = new CheckpointStrategy.Builder();
                    stateHandler.ConfigureSourceProcessingStrategy(checkpointStrategyBuilder);
                    checkpointStrategyBuilder.Validate(Mode);             // avoid future exceptions in coreprojection
                    return(stateHandler);
                }
                catch (Exception ex)
                {
                    SetFaulted(
                        string.Format(
                            "Cannot create a projection state handler.\r\n\r\nHandler type: {0}\r\nQuery:\r\n\r\n{1}\r\n\r\nMessage:\r\n\r\n{2}",
                            HandlerType, Query, ex.Message), ex);
                    if (stateHandler != null)
                    {
                        stateHandler.Dispose();
                    }
                    throw;
                }
            });

            //note: set runnign before start as coreProjection.start() can respond with faulted
            _state = ManagedProjectionState.Preparing;
            _coreQueue.Publish(createProjectionMessage);
        }
コード例 #24
0
 public void Teardown()
 {
     if (_stateHandler != null)
     {
         _stateHandler.Dispose();
     }
     _stateHandler = null;
     GC.Collect(2, GCCollectionMode.Forced);
     GC.WaitForPendingFinalizers();
 }
コード例 #25
0
 public static bool ProcessEvent(
     this IProjectionStateHandler self, string partition, CheckpointTag eventPosition, string streamId,
     string eventType, string category, Guid eventId, long eventSequenceNumber, string metadata, string data,
     out string state, out string sharedState, out EmittedEventEnvelope[] emittedEvents, bool isJson = true)
 {
     return(self.ProcessEvent(
                partition, eventPosition, category,
                new ResolvedEvent(
                    streamId, eventSequenceNumber, streamId, eventSequenceNumber, false, new TFPos(0, -1), eventId,
                    eventType, isJson, data, metadata), out state, out sharedState, out emittedEvents));
 }
コード例 #26
0
 public SlaveQueryProcessingStrategy(
     string name, ProjectionVersion projectionVersion, IProjectionStateHandler stateHandler,
     ProjectionConfig projectionConfig, IQuerySources sourceDefinition, ILogger logger,
     IPublisher resultsPublisher, Guid masterCoreProjectionId,
     ReaderSubscriptionDispatcher subscriptionDispatcher)
     : base(
         name, projectionVersion, stateHandler, projectionConfig, sourceDefinition, logger,
         subscriptionDispatcher)
 {
     _resultsPublisher = resultsPublisher;
     _masterCoreProjectionId = masterCoreProjectionId;
 }
コード例 #27
0
        public EventProcessingProjectionProcessingPhase(
            CoreProjection coreProjection,
            Guid projectionCorrelationId,
            IPublisher publisher,
            IPublisher inputQueue,
            ProjectionConfig projectionConfig,
            Action updateStatistics,
            IProjectionStateHandler projectionStateHandler,
            PartitionStateCache partitionStateCache,
            bool definesStateTransform,
            string projectionName,
            ILogger logger,
            CheckpointTag zeroCheckpointTag,
            ICoreProjectionCheckpointManager coreProjectionCheckpointManager,
            StatePartitionSelector statePartitionSelector,
            ReaderSubscriptionDispatcher subscriptionDispatcher,
            IReaderStrategy readerStrategy,
            IResultWriter resultWriter,
            bool useCheckpoints,
            bool stopOnEof,
            bool isBiState,
            bool orderedPartitionProcessing,
            IEmittedStreamsTracker emittedStreamsTracker,
            bool enableContentTypeValidation)
            : base(
                publisher,
                inputQueue,
                coreProjection,
                projectionCorrelationId,
                coreProjectionCheckpointManager,
                projectionConfig,
                projectionName,
                logger,
                zeroCheckpointTag,
                partitionStateCache,
                resultWriter,
                updateStatistics,
                subscriptionDispatcher,
                readerStrategy,
                useCheckpoints,
                stopOnEof,
                orderedPartitionProcessing,
                isBiState,
                emittedStreamsTracker,
                enableContentTypeValidation)
        {
            _projectionStateHandler = projectionStateHandler;
            _definesStateTransform  = definesStateTransform;
            _statePartitionSelector = statePartitionSelector;
            _isBiState = isBiState;

            _stopwatch = new Stopwatch();
        }
コード例 #28
0
 public SlaveQueryProcessingStrategy(
     string name, ProjectionVersion projectionVersion, IProjectionStateHandler stateHandler,
     ProjectionConfig projectionConfig, IQuerySources sourceDefinition, ILogger logger,
     IPublisher resultsPublisher, Guid masterCoreProjectionId,
     ReaderSubscriptionDispatcher subscriptionDispatcher)
     : base(
         name, projectionVersion, stateHandler, projectionConfig, sourceDefinition, logger,
         subscriptionDispatcher)
 {
     _resultsPublisher = resultsPublisher;
     _masterCoreProjectionId = masterCoreProjectionId;
 }
コード例 #29
0
        public CoreProjection(
            string name, Guid projectionCorrelationId, IPublisher publisher,
            IProjectionStateHandler projectionStateHandler, ProjectionConfig projectionConfig,
            RequestResponseDispatcher
            <ClientMessage.ReadStreamEventsBackward, ClientMessage.ReadStreamEventsBackwardCompleted> readDispatcher,
            RequestResponseDispatcher <ClientMessage.WriteEvents, ClientMessage.WriteEventsCompleted> writeDispatcher,
            ILogger logger = null)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }
            if (name == "")
            {
                throw new ArgumentException("name");
            }
            if (publisher == null)
            {
                throw new ArgumentNullException("publisher");
            }
            if (projectionStateHandler == null)
            {
                throw new ArgumentNullException("projectionStateHandler");
            }
            _projectionStateHandler  = projectionStateHandler;
            _projectionCorrelationId = projectionCorrelationId;

            var namingBuilder = new ProjectionNamesBuilder();

            _projectionStateHandler.ConfigureSourceProcessingStrategy(namingBuilder);

            _name = namingBuilder.ForceProjectionName ?? name;
            //TODO: move into name builder
            _stateStreamNamePattern = namingBuilder.StateStreamName ?? ProjectionsStreamPrefix + _name + "-{0}" + ProjectionsStateStreamSuffix;
            _stateStreamName        = namingBuilder.StateStreamName ?? ProjectionsStreamPrefix + _name + ProjectionsStateStreamSuffix;
            _projectionConfig       = projectionConfig;
            _logger          = logger;
            _publisher       = publisher;
            _readDispatcher  = readDispatcher;
            _writeDispatcher = writeDispatcher;
            var builder = new CheckpointStrategy.Builder();

            _projectionStateHandler.ConfigureSourceProcessingStrategy(builder);
            _checkpointStrategy  = builder.Build(_projectionConfig.Mode);
            _partitionStateCache = new PartitionStateCache();
            _processingQueue     = new CoreProjectionQueue(
                projectionCorrelationId, publisher, projectionConfig.PendingEventsThreshold, UpdateStatistics);
            _checkpointManager = this._checkpointStrategy.CreateCheckpointManager(
                this, projectionCorrelationId, this._publisher, this._readDispatcher,
                this._writeDispatcher, this._projectionConfig, this._name, _stateStreamName);
            GoToState(State.Initial);
        }
コード例 #30
0
        private void Start(IPublisher coreOutput, ProjectionStateHandlerFactory handlerFactory, ProjectionConfig config)
        {
            if (coreOutput == null)
            {
                throw new ArgumentNullException("coreOutput");
            }
            if (handlerFactory == null)
            {
                throw new ArgumentNullException("handlerFactory");
            }
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            if (_coreProjection != null)
            {
                throw new InvalidOperationException("CoreProjection has been already created");
            }

            IProjectionStateHandler stateHandler = null;

            try
            {
                stateHandler = handlerFactory.Create(HandlerType, Query, Console.WriteLine);
                var checkpointStrategyBuilder = new CheckpointStrategy.Builder();
                stateHandler.ConfigureSourceProcessingStrategy(checkpointStrategyBuilder);
                checkpointStrategyBuilder.Validate(this.Mode); // avoid future exceptions in coreprojection
                // constructor can fail if wrong source defintion
                //TODO: revise it
                _coreProjection = new CoreProjection(_name, _id, coreOutput, stateHandler, config, _logger);
            }
            catch (Exception ex)
            {
                SetFaulted(
                    String.Format(
                        "Cannot create a projection state handler.\r\n\r\nHandler type: {0}\r\nQuery:\r\n\r\n{1}\r\n\r\nMessage:\r\n\r\n{2}",
                        HandlerType, Query, ex.Message), ex);
                if (stateHandler != null)
                {
                    stateHandler.Dispose();
                }
                return;
            }

            //TODO: load configuration from the definition
            _state = ManagedProjectionState.Running;
            //note: set runnign before start as coreProjection.start() can respond with faulted
            _coreProjection.Start();
        }
コード例 #31
0
 public ParallelQueryMasterProjectionProcessingPhase(
     CoreProjection coreProjection, Guid projectionCorrelationId, IPublisher publisher,
     ProjectionConfig projectionConfig, Action updateStatistics, IProjectionStateHandler stateHandler,
     PartitionStateCache partitionStateCache, string name, ILogger logger, CheckpointTag zeroCheckpointTag,
     ICoreProjectionCheckpointManager checkpointManager, ReaderSubscriptionDispatcher subscriptionDispatcher,
     IReaderStrategy readerStrategy, IResultWriter resultWriter, bool checkpointsEnabled, bool stopOnEof,
     SpooledStreamReadingDispatcher spoolProcessingResponseDispatcher)
     : base(
         publisher, coreProjection, projectionCorrelationId, checkpointManager, projectionConfig, name, logger,
         zeroCheckpointTag, partitionStateCache, resultWriter, updateStatistics, subscriptionDispatcher,
         readerStrategy, checkpointsEnabled, stopOnEof, orderedPartitionProcessing: true, isBiState: false)
 {
     _stateHandler = stateHandler;
     _spoolProcessingResponseDispatcher = spoolProcessingResponseDispatcher;
     _spoolProcessingWorkItems          = new Dictionary <Guid, SpoolStreamProcessingWorkItem>();
 }
コード例 #32
0
 public void Handle(ProjectionMessage.CoreService.Management.Create message)
 {
     try
     {
         //TODO: factory method can throw!
         IProjectionStateHandler stateHandler = message.HandlerFactory();
         // constructor can fail if wrong source defintion
         //TODO: revise it
         var projection = new CoreProjection(message.Name, message.CorrelationId, _publisher, stateHandler, message.Config, _readDispatcher, _writeDispatcher, _logger);
         _projections.Add(message.CorrelationId, projection);
     }
     catch (Exception ex)
     {
         message.Envelope.ReplyWith(new ProjectionMessage.Projections.StatusReport.Faulted(message.CorrelationId, ex.Message));
     }
 }
コード例 #33
0
ファイル: ManagedProjection.cs プロジェクト: eleks/EventStore
        private void Start(ProjectionStateHandlerFactory handlerFactory, ProjectionConfig config)
        {
            if (handlerFactory == null)
            {
                throw new ArgumentNullException("handlerFactory");
            }
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            if (_state == ManagedProjectionState.Running)
            {
                throw new InvalidOperationException("Already started");
            }

            //TODO: load configuration from the definition


            var createProjectionMessage = new ProjectionMessage.CoreService.Management.Create(new PublishEnvelope(_inputQueue), _id, _name, config, delegate
            {
                IProjectionStateHandler stateHandler = null;
                try
                {
                    stateHandler = handlerFactory.Create(this.HandlerType, this.Query, Console.WriteLine);
                    var checkpointStrategyBuilder = new CheckpointStrategy.Builder();
                    stateHandler.ConfigureSourceProcessingStrategy(checkpointStrategyBuilder);
                    checkpointStrategyBuilder.Validate(Mode);     // avoid future exceptions in coreprojection
                    return(stateHandler);
                }
                catch (Exception ex)
                {
                    SetFaulted(String.Format("Cannot create a projection state handler.\r\n\r\nHandler type: {0}\r\nQuery:\r\n\r\n{1}\r\n\r\nMessage:\r\n\r\n{2}", HandlerType, Query, ex.Message), ex);
                    if (stateHandler != null)
                    {
                        stateHandler.Dispose();
                    }
                    throw;
                }
            });

            //note: set runnign before start as coreProjection.start() can respond with faulted
            _state = ManagedProjectionState.Running;
            _coreQueue.Publish(createProjectionMessage);
            _coreQueue.Publish(new ProjectionMessage.Projections.Management.Start(_id));
        }
コード例 #34
0
        public void can_compile_million_times()
        {
            for (var i = 0; i < 10000000; i++)
            {
                if (_stateHandler != null)
                {
                    _stateHandler.Dispose();
                }
                _stateHandler = null;

/*
 *              _state = null;
 */
                _projection          = null;
                _projection          = @"
                fromAll();
                on_raw(function(state, event, streamId, eventType, sequenceNumber, metadata) {
                    emit('output-stream' + sequenceNumber, 'emitted-event' + sequenceNumber, {a: JSON.parse(event).a});
                    return {};
                });
            ";
                _logged              = new List <string>();
                _stateHandlerFactory = new ProjectionStateHandlerFactory();
                _stateHandler        = _stateHandlerFactory.Create(
                    "JS", _projection, s =>
                {
                    if (!s.StartsWith("P:"))
                    {
                        _logged.Add(s);
                    }
                    else
                    {
                        _logDelegate(s);
                    }
                });         // skip prelude debug output

/*
 *              if (_state != null)
 *                  _stateHandler.Load(_state);
 *              else
 *                  _stateHandler.Initialize();
 */
                Console.Write(".");
            }
        }
コード例 #35
0
        public EventProcessingProjectionProcessingPhase(
            CoreProjection coreProjection, Guid projectionCorrelationId, IPublisher publisher,
            ProjectionConfig projectionConfig, Action updateStatistics, IProjectionStateHandler projectionStateHandler,
            PartitionStateCache partitionStateCache, bool definesStateTransform, string projectionName, ILogger logger,
            CheckpointTag zeroCheckpointTag, ICoreProjectionCheckpointManager coreProjectionCheckpointManager,
            StatePartitionSelector statePartitionSelector, ReaderSubscriptionDispatcher subscriptionDispatcher,
            IReaderStrategy readerStrategy, IResultWriter resultWriter, bool useCheckpoints, bool stopOnEof)
            : base(
                publisher, coreProjection, projectionCorrelationId, coreProjectionCheckpointManager, projectionConfig,
                projectionName, logger, zeroCheckpointTag, partitionStateCache, resultWriter, updateStatistics,
                subscriptionDispatcher, readerStrategy, useCheckpoints, stopOnEof)
        {
            _projectionStateHandler = projectionStateHandler;
            _definesStateTransform  = definesStateTransform;
            _statePartitionSelector = statePartitionSelector;

            _stopwatch = new Stopwatch();
        }
コード例 #36
0
        public ParallelQueryProcessingStrategy(
            string name,
            ProjectionVersion projectionVersion,
            IProjectionStateHandler stateHandler,
            ProjectionConfig projectionConfig,
            IQuerySources sourceDefinition,
            string handlerType,
            string query,
            ProjectionNamesBuilder namesBuilder,
            ILogger logger,
            SpooledStreamReadingDispatcher spoolProcessingResponseDispatcher,
            ReaderSubscriptionDispatcher subscriptionDispatcher)
            : base(name, projectionVersion, projectionConfig, sourceDefinition, logger, subscriptionDispatcher)
        {
            if (string.IsNullOrEmpty(handlerType))
            {
                throw new ArgumentNullException("handlerType");
            }
            if (string.IsNullOrEmpty(query))
            {
                throw new ArgumentNullException("query");
            }

            _stateHandler     = stateHandler;
            _projectionConfig = projectionConfig;
            _sourceDefinition = sourceDefinition;
            _handlerType      = handlerType;
            _query            = query;
            _namesBuilder     = namesBuilder;
            _spoolProcessingResponseDispatcher = spoolProcessingResponseDispatcher;
            if (_sourceDefinition.CatalogStream == SystemStreams.AllStream)
            {
                _catalogStreamName = SystemStreams.AllStream;
            }
            else if (_sourceDefinition.HasCategories())
            {
                _catalogStreamName = _namesBuilder.GetCategoryCatalogStreamName(_sourceDefinition.Categories[0]);
            }
            else
            {
                _catalogStreamName = _sourceDefinition.CatalogStream;
            }
        }
コード例 #37
0
 public void setup()
 {
     _state = null;
     _projection = null;
     Given();
     _logged = new List<string>();
     _stateHandlerFactory = new ProjectionStateHandlerFactory();
     _stateHandler = _stateHandlerFactory.Create(
         "JS", _projection, s =>
             {
                 if (!s.StartsWith("P:")) _logged.Add(s);
                 else Console.WriteLine(s);
             }); // skip prelude debug output
     _source = new SourceRecorder();
     _stateHandler.ConfigureSourceProcessingStrategy(_source);
     if (_state != null)
         _stateHandler.Load(_state);
     else
         _stateHandler.Initialize();
 }
コード例 #38
0
        public CoreProjection(
            string name, Guid projectionCorrelationId, IPublisher publisher,
            IProjectionStateHandler projectionStateHandler, ProjectionConfig projectionConfig, ILogger logger = null)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }
            if (name == "")
            {
                throw new ArgumentException("name");
            }
            if (publisher == null)
            {
                throw new ArgumentNullException("publisher");
            }
            if (projectionStateHandler == null)
            {
                throw new ArgumentNullException("projectionStateHandler");
            }
            _projectionCorrelationId = projectionCorrelationId;
            _name                   = name;
            _projectionConfig       = projectionConfig;
            _logger                 = logger;
            _publisher              = publisher;
            _projectionStateHandler = projectionStateHandler;
            _readDispatcher         =
                new RequestResponseDispatcher
                <ClientMessage.ReadEventsBackwards, ClientMessage.ReadEventsBackwardsCompleted>(
                    _publisher, v => v.CorrelationId, v => v.CorrelationId);
            _projectionCheckpointStreamId = ProjectionsStreamPrefix + _name + ProjectionCheckpointStreamSuffix;
            var builder = new CheckpointStrategy.Builder();

            _projectionStateHandler.ConfigureSourceProcessingStrategy(builder);
            _checkpointStrategy         = builder.Build(_projectionConfig.Mode);
            _eventFilter                = _checkpointStrategy.EventFilter;
            _lastProcessedEventPosition = new PositionTracker(_checkpointStrategy.PositionTagger);
            _partitionStateCache        = new PartitionStateCache();
            GoToState(State.Initial);
        }
コード例 #39
0
 public void Handle(CoreProjectionManagementMessage.CreateAndPrepare message)
 {
     try
     {
         //TODO: factory method can throw!
         IProjectionStateHandler stateHandler = message.HandlerFactory();
         // constructor can fail if wrong source definition
         ProjectionSourceDefinition sourceDefinition;
         var projection = CoreProjection.CreateAndPrepare(
             message.Name, message.Version, message.ProjectionId, _publisher, stateHandler, message.Config,
             _readDispatcher, _writeDispatcher, _subscriptionDispatcher, _logger, _timeProvider,
             out sourceDefinition);
         _projections.Add(message.ProjectionId, projection);
         message.Envelope.ReplyWith(
             new CoreProjectionManagementMessage.Prepared(message.ProjectionId, sourceDefinition));
     }
     catch (Exception ex)
     {
         message.Envelope.ReplyWith(
             new CoreProjectionManagementMessage.Faulted(message.ProjectionId, ex.Message));
     }
 }
コード例 #40
0
        public ParallelQueryProcessingStrategy(
            string name,
            ProjectionVersion projectionVersion,
            IProjectionStateHandler stateHandler,
            ProjectionConfig projectionConfig,
            IQuerySources sourceDefinition,
            string handlerType,
            string query,
            ProjectionNamesBuilder namesBuilder,
            ILogger logger,
            SpooledStreamReadingDispatcher spoolProcessingResponseDispatcher,
            ReaderSubscriptionDispatcher subscriptionDispatcher)
            : base(name, projectionVersion, projectionConfig, sourceDefinition, logger, subscriptionDispatcher)
        {
            if (string.IsNullOrEmpty(handlerType)) throw new ArgumentNullException("handlerType");
            if (string.IsNullOrEmpty(query)) throw new ArgumentNullException("query");

            _stateHandler = stateHandler;
            _projectionConfig = projectionConfig;
            _sourceDefinition = sourceDefinition;
            _handlerType = handlerType;
            _query = query;
            _namesBuilder = namesBuilder;
            _spoolProcessingResponseDispatcher = spoolProcessingResponseDispatcher;
            if (_sourceDefinition.CatalogStream == SystemStreams.AllStream)
            {
                _catalogStreamName = SystemStreams.AllStream;
            }
            else if (_sourceDefinition.HasCategories())
            {
                _catalogStreamName = _namesBuilder.GetCategoryCatalogStreamName(_sourceDefinition.Categories[0]);
            }
            else
            {
                _catalogStreamName = _sourceDefinition.CatalogStream;
            }
        }
コード例 #41
0
 public ByHandleStatePartitionSelector(IProjectionStateHandler handler)
 {
     _handler = handler;
 }
コード例 #42
0
 private static StatePartitionSelector CreateStatePartitionSelector(
     IProjectionStateHandler projectionStateHandler, bool byCustomPartitions, bool byStream)
 {
     return byCustomPartitions
         ? new ByHandleStatePartitionSelector(projectionStateHandler)
         : (byStream
             ? (StatePartitionSelector) new ByStreamStatePartitionSelector()
             : new NoopStatePartitionSelector());
 }
コード例 #43
0
 public StatePartitionSelector CreateStatePartitionSelector(IProjectionStateHandler projectionStateHandler)
 {
     return _byCustomPartitions
                ? new ByHandleStatePartitionSelector(projectionStateHandler)
                : (_byStream
                       ? (StatePartitionSelector) new ByStreamStatePartitionSelector()
                       : new NoopStatePartitionSelector());
 }