Example #1
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);
        }
Example #2
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();
     }
 }
Example #3
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);
        }
        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();
        }
Example #5
0
        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));
        }
 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();
 }
Example #7
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);
        }
 public void Handle(CoreProjectionManagementMessage.CreateAndPrepare message)
 {
     try
     {
         //TODO: factory method can throw!
         IProjectionStateHandler stateHandler = message.HandlerFactory();
         // constructor can fail if wrong source defintion
         //TODO: revise it
         var sourceDefintionRecorder = new SourceDefintionRecorder();
         stateHandler.ConfigureSourceProcessingStrategy(sourceDefintionRecorder);
         var sourceDefintion = sourceDefintionRecorder.Build();
         var projection      = new CoreProjection(
             message.Name, message.CorrelationId, _publisher, stateHandler, message.Config, _readDispatcher,
             _writeDispatcher, _logger);
         _projections.Add(message.CorrelationId, projection);
         message.Envelope.ReplyWith(
             new CoreProjectionManagementMessage.Prepared(message.CorrelationId, sourceDefintion));
     }
     catch (Exception ex)
     {
         message.Envelope.ReplyWith(
             new CoreProjectionManagementMessage.Faulted(message.CorrelationId, ex.Message));
     }
 }