Exemple #1
0
        internal void BeginStatePartitionLoad(
            ProjectionMessage.Projections.CommittedEventReceived @event, Action loadCompleted)
        {
            string statePartition = _checkpointStrategy.StatePartitionSelector.GetStatePartition(@event);

            if (statePartition == "") // root is always cached
            {
                loadCompleted();
                return;
            }
            string state = _partitionStateCache.TryGetAndLockPartitionState(statePartition, @event.CheckpointTag);

            if (state != null)
            {
                loadCompleted();
            }
            else
            {
                string partitionStateStreamName = MakePartitionStateStreamName(statePartition);
                _readRequestsInProgress++;
                _readDispatcher.Publish(
                    new ClientMessage.ReadStreamEventsBackward(
                        Guid.NewGuid(), _readDispatcher.Envelope, partitionStateStreamName, -1, 1, resolveLinks: false),
                    m => OnLoadStatePartitionCompleted(statePartition, @event, m, loadCompleted));
            }
        }
Exemple #2
0
 public Guid ReadForward(
     string streamId,
     long fromEventNumber,
     int maxCount,
     bool resolveLinks,
     ClaimsPrincipal principal,
     Action <ClientMessage.ReadStreamEventsForwardCompleted> action,
     Guid?corrId = null)
 {
     if (!corrId.HasValue)
     {
         corrId = Guid.NewGuid();
     }
     AddPendingRequest(corrId.Value);
     return
         (ForwardReader.Publish(
              new ClientMessage.ReadStreamEventsForward(
                  corrId.Value,
                  corrId.Value,
                  ForwardReader.Envelope,
                  streamId,
                  fromEventNumber,
                  maxCount,
                  resolveLinks,
                  false,
                  null,
                  principal),
              res => {
         RemovePendingRequest(res.CorrelationId);
         action(res);
     }));
 }
Exemple #3
0
 public Guid ReadForward(
     string streamId,
     long fromEventNumber,
     int maxCount,
     bool resolveLinks,
     IPrincipal principal,
     Action <ClientMessage.ReadStreamEventsForwardCompleted> action,
     Guid?corrId = null)
 {
     if (!corrId.HasValue)
     {
         corrId = Guid.NewGuid();
     }
     return
         (ForwardReader.Publish(
              new ClientMessage.ReadStreamEventsForward(
                  corrId.Value,
                  corrId.Value,
                  ForwardReader.Envelope,
                  streamId,
                  fromEventNumber,
                  maxCount,
                  resolveLinks,
                  false,
                  null,
                  principal),
              action));
 }
Exemple #4
0
        public void Handle(ProjectionManagementMessage.Internal.Deleted message)
        {
            const string eventStreamId = "$projections-$all";
            var          corrId        = Guid.NewGuid();

            _writeDispatcher.Publish(
                new ClientMessage.WriteEvents(
                    corrId,
                    corrId,
                    _writeDispatcher.Envelope,
                    true,
                    eventStreamId,
                    ExpectedVersion.Any,
                    new Event(
                        Guid.NewGuid(),
                        "$ProjectionDeleted",
                        false,
                        Helper.UTF8NoBom.GetBytes(message.Name),
                        Empty.ByteArray),
                    SystemAccount.Principal),
                m =>
            {
                _awaitingSlaveProjections.Remove(message.Id);     // if any disconnected in error
                _projections.Remove(message.Name);
                _projectionsMap.Remove(message.Id);
            });
        }
Exemple #5
0
        public Guid WriteEvents(
            string streamId,
            long expectedVersion,
            Event[] events,
            ClaimsPrincipal principal,
            Action <ClientMessage.WriteEventsCompleted> action)
        {
            var corrId = Guid.NewGuid();

            AddPendingRequest(corrId);
            return
                (Writer.Publish(
                     new ClientMessage.WriteEvents(
                         corrId,
                         corrId,
                         Writer.Envelope,
                         false,
                         streamId,
                         expectedVersion,
                         events,
                         principal),
                     res => {
                RemovePendingRequest(res.CorrelationId);
                action(res);
            }));
        }
Exemple #6
0
        private void PublishWriteMetaStream()
        {
            var corrId = Guid.NewGuid();

            _writeDispatcher.Publish(
                new ClientMessage.WriteEvents(
                    corrId, corrId, _writeDispatcher.Envelope, true, SystemStreams.MetastreamOf(_streamId), ExpectedVersion.Any,
                    _submittedWriteMetastreamEvent, _writeAs), HandleMetadataWriteCompleted);
        }
Exemple #7
0
        public Guid WriteEvents(
            string streamId, int expectedVersion, Event[] events, IPrincipal principal,
            Action <ClientMessage.WriteEventsCompleted> action)
        {
            var corrId = Guid.NewGuid();

            return(Writer.Publish(
                       new ClientMessage.WriteEvents(corrId, corrId, Writer.Envelope, false, streamId, expectedVersion, events, principal),
                       action));
        }
        protected override void RequestLoadState()
        {
            const int recordsToRequest = 10;
            var       corrId           = Guid.NewGuid();

            _readRequestId = _readDispatcher.Publish(
                new ClientMessage.ReadStreamEventsBackward(
                    corrId, corrId, _readDispatcher.Envelope, _projectionCheckpointStreamId, _nextStateIndexToRequest,
                    recordsToRequest, resolveLinkTos: false, requireMaster: false, validationStreamVersion: null, user: SystemAccount.Principal),
                OnLoadStateReadRequestCompleted);
        }
        private void CreateFakeProjection(Action action)
        {
            var corrId = Guid.NewGuid();

            _writeDispatcher.Publish(
                new ClientMessage.WriteEvents(
                    corrId, corrId, _writeDispatcher.Envelope, true, "$projections-$all", ExpectedVersion.NoStream,
                    new Event(Guid.NewGuid(), "$ProjectionsInitialized", false, Empty.ByteArray, Empty.ByteArray),
                    SystemAccount.Principal),
                completed => WriteFakeProjectionCompleted(completed, action));
        }
Exemple #10
0
 private void BeginLoad(string name)
 {
     _readDispatcher.Publish(
         new ClientMessage.ReadStreamEventsBackward(
             Guid.NewGuid(), _readDispatcher.Envelope, "$projections-" + name, -1, 1, resolveLinks: false),
         LoadCompleted);
 }
Exemple #11
0
        public Guid ReadBackward(
            string streamId,
            int fromEventNumber,
            int maxCount,
            bool resolveLinks,
            IPrincipal principal,
            Action <ClientMessage.ReadStreamEventsBackwardCompleted> action)
        {
            var corrId = Guid.NewGuid();

            return
                (BackwardReader.Publish(
                     new ClientMessage.ReadStreamEventsBackward(
                         corrId,
                         corrId,
                         BackwardReader.Envelope,
                         streamId,
                         fromEventNumber,
                         maxCount,
                         resolveLinks,
                         false,
                         null,
                         principal),
                     action));
        }
Exemple #12
0
        public Guid DeleteStream(
            string streamId,
            long expectedVersion,
            bool hardDelete,
            ClaimsPrincipal principal,
            Action <ClientMessage.DeleteStreamCompleted> action)
        {
            var corrId = Guid.NewGuid();

            AddPendingRequest(corrId);
            return(StreamDeleter.Publish(
                       new ClientMessage.DeleteStream(
                           corrId,
                           corrId,
                           Writer.Envelope,
                           false,
                           streamId,
                           expectedVersion,
                           hardDelete,
                           principal),
                       res => {
                RemovePendingRequest(res.CorrelationId);
                action(res);
            }));
        }
Exemple #13
0
 private void BeginLoadProjectionList(int from = -1)
 {
     _readDispatcher.Publish(
         new ClientMessage.ReadStreamEventsBackward(
             Guid.NewGuid(), _readDispatcher.Envelope, "$projections-$all", from, _readEventsBatchSize,
             resolveLinks: false), m => LoadProjectionListCompleted(m, from));
 }
        public void Handle(ProjectionManagementMessage.Command.Delete message)
        {
            if (!_started)
            {
                return;
            }
            var projection = GetProjection(message.Name);

            if (projection == null)
            {
                message.Envelope.ReplyWith(new ProjectionManagementMessage.NotFound());
            }
            else
            {
                if (!ProjectionManagementMessage.RunAs.ValidateRunAs(projection.GetMode(), ReadWrite.Write, projection.RunAs, message))
                {
                    return;
                }
                try {
                    projection.Handle(message);
                }
                catch (InvalidOperationException ex) {
                    message.Envelope.ReplyWith(new ProjectionManagementMessage.OperationFailed(ex.Message));
                    return;
                }
                _projections.Remove(message.Name);
                _projectionsMap.Remove(projection.Id);
                const string eventStreamId = "$projections-$all";
                var          corrId        = Guid.NewGuid();
                _writeDispatcher.Publish(
                    new ClientMessage.WriteEvents(
                        corrId,
                        corrId,
                        _writeDispatcher.Envelope,
                        true,
                        eventStreamId,
                        ExpectedVersion.Any,
                        new Event(
                            Guid.NewGuid(),
                            "$ProjectionDeleted",
                            false,
                            Helper.UTF8NoBom.GetBytes(message.Name),
                            Empty.ByteArray),
                        SystemAccount.Principal),
                    m => { });
            }
        }
Exemple #15
0
 private void BeginWriteProjectionRegistration(string name, Action completed)
 {
     _writeDispatcher.Publish(
         new ClientMessage.WriteEvents(
             Guid.NewGuid(), _writeDispatcher.Envelope, "$projections-$all", ExpectedVersion.Any,
             new Event(Guid.NewGuid(), "ProjectionCreated", false, Encoding.UTF8.GetBytes(name), new byte[0])),
         m => WriteProjectionRegistrationCompleted(m, completed, name));
 }
        private void BeginLoadProjectionList(Action completedAction, int from = -1)
        {
            var corrId = Guid.NewGuid();

            _readDispatcher.Publish(
                new ClientMessage.ReadStreamEventsBackward(
                    corrId,
                    corrId,
                    _readDispatcher.Envelope,
                    "$projections-$all",
                    from,
                    _readEventsBatchSize,
                    resolveLinkTos: false,
                    requireMaster: false,
                    validationStreamVersion: null,
                    user: SystemAccount.Principal),
                m => LoadProjectionListCompleted(m, from, completedAction));
        }
Exemple #17
0
        public void DeleteStream(
            string streamId, int expectedVersion, bool hardDelete, IPrincipal principal,
            Action <ClientMessage.DeleteStreamCompleted> action)
        {
            var corrId = Guid.NewGuid();

            StreamDeleter.Publish(
                new ClientMessage.DeleteStream(corrId, corrId, Writer.Envelope, false, streamId, expectedVersion, hardDelete, principal), action);
        }
Exemple #18
0
        private void BeginLoad(string name)
        {
            var corrId = Guid.NewGuid();

            _readDispatcher.Publish(
                new ClientMessage.ReadStreamEventsBackward(
                    corrId, corrId, _readDispatcher.Envelope, "$projections-" + name, -1, 1,
                    resolveLinkTos: false, requireMaster: false, validationStreamVersion: null, user: SystemAccount.Principal),
                LoadCompleted);
        }
Exemple #19
0
        public Guid ReadAllForwardFiltered(
            long commitPosition,
            long preparePosition,
            int maxCount,
            bool resolveLinks,
            bool requireLeader,
            int maxSearchWindow,
            long?validationTfLastCommitPosition,
            IEventFilter eventFilter,
            ClaimsPrincipal user,
            TimeSpan?longPollTimeout,
            Action <ClientMessage.FilteredReadAllEventsForwardCompleted> action,
            Action timeoutAction,
            Guid corrId)
        {
            AddPendingRequest(corrId);
            _pendingReads.Register(corrId);

            AllForwardFilteredReader.Publish(
                new ClientMessage.FilteredReadAllEventsForward(
                    corrId,
                    corrId,
                    AllForwardFilteredReader.Envelope,
                    commitPosition,
                    preparePosition,
                    maxCount,
                    resolveLinks,
                    requireLeader,
                    maxSearchWindow,
                    validationTfLastCommitPosition,
                    eventFilter,
                    user,
                    longPollTimeout
                    ),
                res => {
                RemovePendingRequest(corrId);
                if (!_pendingReads.IsRegistered(corrId))
                {
                    return;
                }
                _pendingReads.Remove(corrId);
                action(res);
            });
            Delay(TimeSpan.FromMilliseconds(ReadTimeoutMs), () => {
                RemovePendingRequest(corrId);
                if (!_pendingReads.IsRegistered(corrId))
                {
                    return;
                }
                _pendingReads.Remove(corrId);
                timeoutAction();
            }, corrId);
            return(corrId);
        }
        private void DeleteStream(string streamId, Action completed)
        {
            //delete checkpoint stream
            var correlationId = Guid.NewGuid();

            _streamDispatcher.Publish(new ClientMessage.DeleteStream(
                                          correlationId,
                                          correlationId,
                                          _writeDispatcher.Envelope,
                                          true,
                                          streamId,
                                          ExpectedVersion.Any,
                                          false,
                                          SystemAccount.Principal), m => DeleteStreamCompleted(m, streamId, completed));
        }
Exemple #21
0
        private void BeginWrite(Action completed)
        {
            if (Mode <= ProjectionMode.AdHoc)
            {
                completed();
                return;
            }
            var managedProjectionSerializedState = _persistedState.ToJsonBytes();

            _writeDispatcher.Publish(
                new ClientMessage.WriteEvents(
                    Guid.NewGuid(), _writeDispatcher.Envelope, "$projections-" + _name, ExpectedVersion.Any,
                    new Event(Guid.NewGuid(), "ProjectionUpdated", false, managedProjectionSerializedState, new byte[0])),
                m => WriteCompleted(m, completed));
        }
Exemple #22
0
        internal void BeginStatePartitionLoad(string statePartition, CheckpointTag eventCheckpointTag, Action loadCompleted)
        {
            if (statePartition == "") // root is always cached
            {
                loadCompleted();
                return;
            }
            var state = _partitionStateCache.TryGetAndLockPartitionState(statePartition, eventCheckpointTag);

            if (state != null)
            {
                loadCompleted();
            }
            else
            {
                string partitionStateStreamName = MakePartitionStateStreamName(statePartition);
                _readRequestsInProgress++;
                var requestId = _readDispatcher.Publish(new ClientMessage.ReadStreamEventsBackward(Guid.NewGuid(), _readDispatcher.Envelope, partitionStateStreamName, -1, 1, resolveLinks: false), m => OnLoadStatePartitionCompleted(statePartition, m, loadCompleted, eventCheckpointTag));
                if (requestId != Guid.Empty)
                {
                    _loadStateRequests.Add(requestId);
                }
            }
        }
Exemple #23
0
        private void DeleteCheckpointStream()
        {
            //delete checkpoint stream
            var correlationId        = Guid.NewGuid();
            var checkpointStreamName = new ProjectionNamesBuilder(_name, _persistedState.SourceDefinition).MakeCheckpointStreamName();

            _streamDispatcher.Publish(new ClientMessage.DeleteStream(
                                          correlationId,
                                          correlationId,
                                          _writeDispatcher.Envelope,
                                          true,
                                          checkpointStreamName,
                                          ExpectedVersion.Any,
                                          false,
                                          SystemAccount.Principal), m => StreamDeleted(m, checkpointStreamName));
        }
Exemple #24
0
        private void SubmitListEvents(CheckpointTag upTo, int fromEventNumber = -1)
        {
            if (_awaitingWriteCompleted || _awaitingMetadataWriteCompleted || _awaitingListEventsCompleted)
            {
                throw new Exception();
            }
            _awaitingListEventsCompleted = true;
            var corrId = Guid.NewGuid();

            _readDispatcher.Publish(
                new ClientMessage.ReadStreamEventsBackward(
                    //TODO: reading events history in batches of 1 event (slow?)
                    corrId, corrId, _readDispatcher.Envelope, _streamId, fromEventNumber, 1,
                    resolveLinkTos: false, requireMaster: false, validationStreamVersion: null, user: SystemAccount.Principal),
                completed => ReadStreamEventsBackwardCompleted(completed, upTo));
        }
        private void PublishWriteCheckpointEvent()
        {
            if (_logger != null)
            {
                _logger.Trace(
                    "Writing checkpoint for {0} at {1} with expected version number {2}", _name,
                    _requestedCheckpointPosition, _lastWrittenCheckpointEventNumber);
            }
            var corrId = Guid.NewGuid();

            _writeRequestId = _writeDispatcher.Publish(
                new ClientMessage.WriteEvents(
                    corrId, corrId, _writeDispatcher.Envelope, true, _projectionCheckpointStreamId,
                    _lastWrittenCheckpointEventNumber, _checkpointEventToBePublished, SystemAccount.Principal),
                msg => WriteCheckpointEventCompleted(msg, _projectionCheckpointStreamId));
        }
Exemple #26
0
        public void SubscribeAwake(
            string streamId,
            TFPos from,
            Action <IODispatcherDelayedMessage> action,
            Guid?correlationId = null)
        {
            var corrId = correlationId ?? Guid.NewGuid();

            Awaker.Publish(
                new AwakeServiceMessage.SubscribeAwake(
                    Awaker.Envelope,
                    corrId,
                    streamId,
                    from,
                    new IODispatcherDelayedMessage(corrId, null)),
                action);
        }
Exemple #27
0
        public Guid ReadAllBackward(
            long commitPosition,
            long preparePosition,
            int maxCount,
            bool resolveLinks,
            bool requireMaster,
            long?validationTfLastCommitPosition,
            IPrincipal user,
            TimeSpan?longPollTimeout,
            Action <ClientMessage.ReadAllEventsBackwardCompleted> action,
            Action timeoutAction,
            Guid corrId)
        {
            _pendingReads.Register(corrId);

            AllBackwardReader.Publish(
                new ClientMessage.ReadAllEventsBackward(
                    corrId,
                    corrId,
                    AllBackwardReader.Envelope,
                    commitPosition,
                    preparePosition,
                    maxCount,
                    resolveLinks,
                    requireMaster,
                    validationTfLastCommitPosition,
                    user
                    ),
                res => {
                if (!_pendingReads.IsRegistered(corrId))
                {
                    return;
                }
                _pendingReads.Remove(corrId);
                action(res);
            });
            Delay(TimeSpan.FromMilliseconds(ReadTimeoutMs), () => {
                if (!_pendingReads.IsRegistered(corrId))
                {
                    return;
                }
                _pendingReads.Remove(corrId);
                timeoutAction();
            }, corrId);
            return(corrId);
        }
Exemple #28
0
        public Guid ReadAllBackwardFiltered(
            long commitPosition,
            long preparePosition,
            int maxCount,
            bool resolveLinks,
            bool requireLeader,
            int maxSearchWindow,
            long?validationTfLastCommitPosition,
            IEventFilter eventFilter,
            ClaimsPrincipal user,
            Action <ClientMessage.FilteredReadAllEventsBackwardCompleted> action,
            Action timeoutAction,
            Guid corrId)
        {
            _requestTracker.AddPendingRead(corrId);

            AllBackwardFilteredReader.Publish(
                new ClientMessage.FilteredReadAllEventsBackward(
                    corrId,
                    corrId,
                    AllBackwardFilteredReader.Envelope,
                    commitPosition,
                    preparePosition,
                    maxCount,
                    resolveLinks,
                    requireLeader,
                    maxSearchWindow,
                    validationTfLastCommitPosition,
                    eventFilter,
                    user
                    ),
                res => {
                if (_requestTracker.RemovePendingRead(res.CorrelationId))
                {
                    action(res);
                }
            });
            Delay(TimeSpan.FromMilliseconds(ReadTimeoutMs), () => {
                if (_requestTracker.RemovePendingRead(corrId))
                {
                    timeoutAction();
                }
            }, corrId);
            return(corrId);
        }
Exemple #29
0
 public void Handle(ProjectionManagementMessage.Command.GetResult message)
 {
     _lastAccessed = _timeProvider.Now;
     if (_state >= ManagedProjectionState.Running)
     {
         _getResultDispatcher.Publish(
             new CoreProjectionManagementMessage.GetResult(Guid.NewGuid(), Id, message.Partition, _workerId),
             m =>
             message.Envelope.ReplyWith(
                 new ProjectionManagementMessage.ProjectionResult(_name, m.Partition, m.Result, m.Position)));
     }
     else
     {
         message.Envelope.ReplyWith(
             new ProjectionManagementMessage.ProjectionResult(
                 message.Name, message.Partition, "*** UNKNOWN ***", position: null));
     }
 }
Exemple #30
0
        private void BeginWrite(Action completed)
        {
            if (Mode == ProjectionMode.Transient)
            {
                completed();
                return;
            }
            var managedProjectionSerializedState = _persistedState.ToJsonBytes();
            var eventStreamId = "$projections-" + _name;
            var corrId        = Guid.NewGuid();

            _writeDispatcher.Publish(
                new ClientMessage.WriteEvents(
                    corrId, corrId, _writeDispatcher.Envelope, true, eventStreamId, ExpectedVersion.Any,
                    new Event(Guid.NewGuid(), "$ProjectionUpdated", true, managedProjectionSerializedState, Empty.ByteArray),
                    SystemAccount.Principal),
                m => WriteCompleted(m, completed, eventStreamId));
        }