public static ClientMessage.ReadAllEventsForwardCompleted ReadAllEventsForward(MiniClusterNode node,
                                                                                       long position)
        {
            ClientMessage.ReadAllEventsForwardCompleted readResult = null;
            var readEvent = new ManualResetEventSlim();
            var done      = false;

            while (!done)
            {
                var read = new ClientMessage.ReadAllEventsForward(Guid.NewGuid(), Guid.NewGuid(), new CallbackEnvelope(
                                                                      msg => {
                    readResult = (ClientMessage.ReadAllEventsForwardCompleted)msg;
                    readEvent.Set();
                }),
                                                                  0, 0, 100, false, false, null, SystemAccount.Principal);
                node.Node.MainQueue.Publish(read);

                if (!readEvent.Wait(_timeout))
                {
                    Assert.Fail("Timed out waiting for events to be read forward");
                    return(null);
                }

                if (readResult.Result == ReadAllResult.Error)
                {
                    Assert.Fail("Failed to read forwards. Read result error: {0}", readResult.Error);
                    return(null);
                }

                done = readResult.NextPos.CommitPosition > position;
                readEvent.Reset();
            }

            return(readResult);
        }
Example #2
0
        void IHandle <ClientMessage.ReadAllEventsForward> .Handle(ClientMessage.ReadAllEventsForward message)
        {
            var pos    = new TFPos(message.CommitPosition, message.PreparePosition);
            var result = _readIndex.ReadAllEventsForward(pos, message.MaxCount, message.ResolveLinks);

            message.Envelope.ReplyWith(new ClientMessage.ReadAllEventsForwardCompleted(message.CorrelationId, result));
        }
            private void RequestTfEvents(bool delay)
            {
                if (_disposed)
                {
                    throw new InvalidOperationException("Disposed");
                }
                if (_reader.PauseRequested || _reader.Paused)
                {
                    throw new InvalidOperationException("Paused or pause requested");
                }
                if (_tfEventsRequested)
                {
                    return;
                }

                _tfEventsRequested           = true;
                _pendingRequestCorrelationId = Guid.NewGuid();
                //TODO: we do not need resolve links, but lets check first with
                var readRequest = new ClientMessage.ReadAllEventsForward(
                    _pendingRequestCorrelationId, _pendingRequestCorrelationId, new SendToThisEnvelope(this),
                    _fromTfPosition.CommitPosition,
                    _fromTfPosition.PreparePosition == -1 ? 0 : _fromTfPosition.PreparePosition,
                    EventByTypeIndexEventReader.MaxReadCount,
                    true, false, null, _readAs);

                var timeoutMessage = TimerMessage.Schedule.Create(
                    TimeSpan.FromMilliseconds(ESConsts.ReadRequestTimeout),
                    new SendToThisEnvelope(this),
                    new ProjectionManagementMessage.Internal.ReadTimeout(_pendingRequestCorrelationId, "$all"));

                _reader.PublishIORequest(delay, readRequest, timeoutMessage, _pendingRequestCorrelationId);
            }
Example #4
0
        public void Handle(ClientMessage.ReadAllEventsForward message)
        {
            if (!_readAllEnabled)
            {
                return;
            }
            var from    = new TFPos(message.CommitPosition, message.PreparePosition);
            var records = _all.SkipWhile(v => v.Key < from).Take(message.MaxCount).ToArray();
            var list    = new List <ResolvedEvent>();
            var pos     = from;
            var next    = pos;
            var prev    = new TFPos(pos.CommitPosition, Int64.MaxValue);

            foreach (KeyValuePair <TFPos, EventRecord> record in records)
            {
                pos  = record.Key;
                next = new TFPos(pos.CommitPosition, pos.PreparePosition + 1);
                list.Add(BuildEvent(record.Value, message.ResolveLinkTos, record.Key.CommitPosition));
            }
            var events = list.ToArray();

            message.Envelope.ReplyWith(
                new ClientMessage.ReadAllEventsForwardCompleted(
                    message.CorrelationId, ReadAllResult.Success, "", events, null, false, message.MaxCount, pos, next, prev,
                    _fakePosition));
        }
            private void RequestTfEvents(bool delay)
            {
                if (_disposed)
                {
                    throw new InvalidOperationException("Disposed");
                }
                if (_reader.PauseRequested || _reader.Paused)
                {
                    throw new InvalidOperationException("Paused or pause requested");
                }
                if (_tfEventsRequested)
                {
                    return;
                }

                _tfEventsRequested = true;
                //TODO: we do not need resolve links, but lets check first with
                var readRequest = new ClientMessage.ReadAllEventsForward(
                    Guid.NewGuid(), _reader.EventReaderCorrelationId, new SendToThisEnvelope(this),
                    _fromTfPosition.CommitPosition,
                    _fromTfPosition.PreparePosition == -1 ? 0 : _fromTfPosition.PreparePosition, 111,
                    true, false, null, _readAs);

                _reader.PublishIORequest(delay, readRequest, Guid.NewGuid());
            }
Example #6
0
 public void Handle(ClientMessage.ReadAllEventsForward message)
 {
     _externalRequestQueue.Publish(
         new ClientMessage.ReadAllEventsForward(
             message.CorrelationId, new PublishToWrapEnvelop(_inputQueue, message.Envelope),
             message.CommitPosition, message.PreparePosition, message.MaxCount, message.ResolveLinks));
 }
Example #7
0
 private ClientMessage.ReadAllEventsForwardCompleted NoData(ClientMessage.ReadAllEventsForward msg,
                                                            ReadAllResult result, TFPos pos, long lastCommitPosition, string error = null)
 {
     return(new ClientMessage.ReadAllEventsForwardCompleted(
                msg.CorrelationId, result, error, ResolvedEvent.EmptyArray, null, false,
                msg.MaxCount, pos, TFPos.Invalid, TFPos.Invalid, lastCommitPosition));
 }
Example #8
0
 public void Handle(ClientMessage.ReadAllEventsForward msg)
 {
     _externalRequestQueue.Publish(
         new ClientMessage.ReadAllEventsForward(
             msg.InternalCorrId, msg.CorrelationId, new PublishToWrapEnvelop(_inputQueue, msg.Envelope),
             msg.CommitPosition, msg.PreparePosition, msg.MaxCount, msg.ResolveLinkTos, msg.RequireMaster,
             msg.ValidationTfLastCommitPosition, msg.User));
 }
Example #9
0
        void IHandle <ClientMessage.ReadAllEventsForward> .Handle(ClientMessage.ReadAllEventsForward msg)
        {
            if (msg.Expires < DateTime.UtcNow)
            {
                if (LogExpiredMessage(msg.Expires))
                {
                    Log.Debug(
                        "Read All Stream Events Forward operation has expired for C:{commitPosition}/P:{preparePosition}. Operation Expired at {expiryDateTime}",
                        msg.CommitPosition, msg.PreparePosition, msg.Expires);
                }
                return;
            }

            using (HistogramService.Measure(_readerAllRangeHistogram)) {
                var res = ReadAllEventsForward(msg);
                switch (res.Result)
                {
                case ReadAllResult.Success:
                    if (msg.LongPollTimeout.HasValue && res.IsEndOfStream && res.Events.Length == 0)
                    {
                        _publisher.Publish(new SubscriptionMessage.PollStream(
                                               SubscriptionsService.AllStreamsSubscriptionId, res.TfLastCommitPosition, null,
                                               DateTime.UtcNow + msg.LongPollTimeout.Value, msg));
                    }
                    else
                    {
                        msg.Envelope.ReplyWith(res);
                    }

                    break;

                case ReadAllResult.NotModified:
                    if (msg.LongPollTimeout.HasValue && res.IsEndOfStream &&
                        res.CurrentPos.CommitPosition > res.TfLastCommitPosition)
                    {
                        _publisher.Publish(new SubscriptionMessage.PollStream(
                                               SubscriptionsService.AllStreamsSubscriptionId, res.TfLastCommitPosition, null,
                                               DateTime.UtcNow + msg.LongPollTimeout.Value, msg));
                    }
                    else
                    {
                        msg.Envelope.ReplyWith(res);
                    }

                    break;

                case ReadAllResult.Error:
                case ReadAllResult.AccessDenied:
                    msg.Envelope.ReplyWith(res);
                    break;

                default:
                    throw new ArgumentOutOfRangeException(string.Format("Unknown ReadAllResult: {0}", res.Result));
                }
            }
        }
 public TcpPackage CreateNetworkPackage()
 {
     lock (_corrIdLock)
     {
         var dto = new ClientMessage.ReadAllEventsForward(_position.CommitPosition,
                                                          _position.PreparePosition,
                                                          _maxCount,
                                                          _resolveLinkTos);
         return(new TcpPackage(TcpCommand.ReadAllEventsForward, _corrId, dto.Serialize()));
     }
 }
Example #11
0
        private ClientMessage.ReadAllEventsForwardCompleted ReadAllEventsForward(ClientMessage.ReadAllEventsForward msg)
        {
            using (HistogramService.Measure(_readerAllRangeHistogram))
            {
                var pos = new TFPos(msg.CommitPosition, msg.PreparePosition);
                var lastCommitPosition = _readIndex.LastReplicatedPosition;
                try
                {
                    if (msg.MaxCount > MaxPageSize)
                    {
                        throw new ArgumentException(string.Format("Read size too big, should be less than {0} items",
                                                                  MaxPageSize));
                    }
                    if (pos == TFPos.HeadOfTf)
                    {
                        var checkpoint = _writerCheckpoint.Read();
                        pos = new TFPos(checkpoint, checkpoint);
                    }
                    if (pos.CommitPosition < 0 || pos.PreparePosition < 0)
                    {
                        return(NoData(msg, ReadAllResult.Error, pos, lastCommitPosition, "Invalid position."));
                    }
                    if (msg.ValidationTfLastCommitPosition == lastCommitPosition)
                    {
                        return(NoData(msg, ReadAllResult.NotModified, pos, lastCommitPosition));
                    }
                    var access = _readIndex.CheckStreamAccess(SystemStreams.AllStream, StreamAccessType.Read, msg.User);
                    if (!access.Granted)
                    {
                        return(NoData(msg, ReadAllResult.AccessDenied, pos, lastCommitPosition));
                    }


                    var res      = _readIndex.ReadAllEventsForward(pos, msg.MaxCount);
                    var resolved = ResolveReadAllResult(res.Records, msg.ResolveLinkTos, msg.User);
                    if (resolved == null)
                    {
                        return(NoData(msg, ReadAllResult.AccessDenied, pos, lastCommitPosition));
                    }

                    var metadata = _readIndex.GetStreamMetadata(SystemStreams.AllStream);
                    return(new ClientMessage.ReadAllEventsForwardCompleted(
                               msg.CorrelationId, ReadAllResult.Success, null, resolved, metadata, access.Public, msg.MaxCount,
                               res.CurrentPos, res.NextPos, res.PrevPos, lastCommitPosition));
                }
                catch (Exception exc)
                {
                    Log.ErrorException(exc, "Error during processing ReadAllEventsForward request.");
                    return(NoData(msg, ReadAllResult.Error, pos, lastCommitPosition, exc.Message));
                }
            }
        }
        private Message BuildMessage(Guid operationId, IEnvelope envelope, TFPos position)
        {
            var message = new ClientMessage.ReadAllEventsForward(
                internalCorrId: operationId,
                correlationId: operationId,
                envelope,
                commitPosition: position.CommitPosition,
                preparePosition: position.PreparePosition,
                maxCount: 100,
                resolveLinkTos: false,
                requireMaster: false,
                validationTfLastCommitPosition: null,
                user: SystemAccount.Principal
                );

            return(message);
        }
Example #13
0
        public Task <AllEventsSlice> ReadAllEventsForwardAsync(Position position, int maxCount, bool resolveLinkTos, UserCredentials userCredentials = null)
        {
            Ensure.Positive(maxCount, "maxCount");

            var source = new TaskCompletionSource <AllEventsSlice>();

            var envelope = new EmbeddedResponseEnvelope(new EmbeddedResponders.ReadAllEventsForward(source));

            Guid corrId = Guid.NewGuid();

            var message = new ClientMessage.ReadAllEventsForward(corrId, corrId, envelope,
                                                                 position.CommitPosition,
                                                                 position.PreparePosition, maxCount, resolveLinkTos, false, null, SystemAccount.Principal);

            _publisher.Publish(message);

            return(source.Task);
        }
 private void HandleAsNonMaster(ClientMessage.ReadAllEventsForward message)
 {
     if (message.RequireMaster)
     {
         if (_master == null)
         {
             DenyRequestBecauseNotReady(message.Envelope, message.CorrelationId);
         }
         else
         {
             DenyRequestBecauseNotMaster(message.CorrelationId, message.Envelope);
         }
     }
     else
     {
         _outputBus.Publish(message);
     }
 }
Example #15
0
        private ClientMessage.ReadAllEventsForwardCompleted ReadAllEventsForward(ClientMessage.ReadAllEventsForward msg)
        {
            var pos = new TFPos(msg.CommitPosition, msg.PreparePosition);

            try
            {
                if (pos == TFPos.HeadOfTf)
                {
                    var checkpoint = _writerCheckpoint.Read();
                    pos = new TFPos(checkpoint, checkpoint);
                }
                if (pos.CommitPosition < 0 || pos.PreparePosition < 0)
                {
                    return(NoData(msg, ReadAllResult.Error, pos, "Invalid position."));
                }
                if (msg.ValidationTfEofPosition.HasValue && _readIndex.LastCommitPosition == msg.ValidationTfEofPosition.Value)
                {
                    return(NoData(msg, ReadAllResult.NotModified, pos));
                }
                var access = _readIndex.CheckStreamAccess(SystemStreams.AllStream, StreamAccessType.Read, msg.User);
                if (!access.Granted)
                {
                    return(NoData(msg, ReadAllResult.AccessDenied, pos));
                }

                var res      = _readIndex.ReadAllEventsForward(pos, msg.MaxCount);
                var resolved = ResolveReadAllResult(res.Records, msg.ResolveLinkTos, msg.User);
                if (resolved == null)
                {
                    return(NoData(msg, ReadAllResult.AccessDenied, pos));
                }

                return(new ClientMessage.ReadAllEventsForwardCompleted(
                           msg.CorrelationId, ReadAllResult.Success, null, resolved, res.Metadata, access.Public, msg.MaxCount,
                           res.CurrentPos, res.NextPos, res.PrevPos, res.TfEofPosition));
            }
            catch (Exception exc)
            {
                Log.ErrorException(exc, "Error during processing ReadAllEventsForward request.");
                return(NoData(msg, ReadAllResult.Error, pos, exc.Message));
            }
        }
Example #16
0
        void IHandle <ClientMessage.ReadAllEventsForward> .Handle(ClientMessage.ReadAllEventsForward msg)
        {
            var res = ReadAllEventsForward(msg);

            switch (res.Result)
            {
            case ReadAllResult.Success:
                if (msg.LongPollTimeout.HasValue && res.IsEndOfStream && res.Events.Length == 0)
                {
                    _publisher.Publish(new SubscriptionMessage.PollStream(
                                           SubscriptionsService.AllStreamsSubscriptionId, res.TfLastCommitPosition, null,
                                           DateTime.UtcNow + msg.LongPollTimeout.Value, msg));
                }
                else
                {
                    msg.Envelope.ReplyWith(res);
                }
                break;

            case ReadAllResult.NotModified:
                if (msg.LongPollTimeout.HasValue && res.IsEndOfStream && res.CurrentPos.CommitPosition > res.TfLastCommitPosition)
                {
                    _publisher.Publish(new SubscriptionMessage.PollStream(
                                           SubscriptionsService.AllStreamsSubscriptionId, res.TfLastCommitPosition, null,
                                           DateTime.UtcNow + msg.LongPollTimeout.Value, msg));
                }
                else
                {
                    msg.Envelope.ReplyWith(res);
                }
                break;

            case ReadAllResult.Error:
            case ReadAllResult.AccessDenied:
                msg.Envelope.ReplyWith(res);
                break;

            default:
                throw new ArgumentOutOfRangeException(string.Format("Unknown ReadAllResult: {0}", res.Result));
            }
        }
Example #17
0
 void IHandle <ClientMessage.ReadAllEventsForward> .Handle(ClientMessage.ReadAllEventsForward msg)
 {
     msg.Envelope.ReplyWith(ReadAllEventsForward(msg));
 }