private void TryPushingMessagesToClients()
        {
            lock (_lock)
            {
                if (_state == PersistentSubscriptionState.NotReady)
                {
                    return;
                }

                foreach (StreamBuffer.OutstandingMessagePointer messagePointer in _streamBuffer.Scan())
                {
                    OutstandingMessage message = messagePointer.Message;
                    ConsumerPushResult result  = _pushClients.PushMessageToClient(message.ResolvedEvent, message.RetryCount);
                    if (result == ConsumerPushResult.Sent)
                    {
                        messagePointer.MarkSent();
                        MarkBeginProcessing(message);
                        _lastKnownMessage = Math.Max(_lastKnownMessage, message.ResolvedEvent.OriginalEventNumber);
                    }
                    else if (result == ConsumerPushResult.Skipped)
                    {
                        // The consumer strategy skipped the message so leave it in the buffer and continue.
                    }
                    else if (result == ConsumerPushResult.NoMoreCapacity)
                    {
                        return;
                    }
                }
            }
        }
Exemplo n.º 2
0
        public bool Push(OutstandingMessage message)
        {
            if (!CanSend())
            {
                return(false);
            }

            var evnt = message.ResolvedEvent;

            _allowedMessages--;
            Interlocked.Increment(ref _totalItems);
            if (_extraStatistics != null)
            {
                _extraStatistics.StartOperation(evnt.OriginalEvent.EventId);
            }

            _envelope.ReplyWith(
                new ClientMessage.PersistentSubscriptionStreamEventAppeared(CorrelationId, evnt, message.RetryCount));
            if (!_unconfirmedEvents.ContainsKey(evnt.OriginalEvent.EventId))
            {
                _unconfirmedEvents.Add(evnt.OriginalEvent.EventId, message);
            }

            return(true);
        }
Exemplo n.º 3
0
        public void AddReadMessage(OutstandingMessage ev)
        {
            if (Live)
            {
                return;
            }
            if (_initialSequence != null &&
                ev.EventPosition.CompareTo(_initialSequence) <= 0)
            {
                return;
            }

            var livePosition = TryPeekLive();

            if (livePosition == null || ev.EventPosition.CompareTo(livePosition) < 0)
            {
                _buffer.AddLast(ev);
            }
            else if (livePosition.CompareTo(ev.EventPosition) < 0)
            {
                DrainLiveTo(ev.EventPosition);
                SwitchToLive();
            }
            else
            {
                SwitchToLive();
            }
        }
Exemplo n.º 4
0
        public void AddRetry(OutstandingMessage ev)
        {
            //add parked messages at the end of the list
            if (ev.IsReplayedEvent)
            {
                _retry.AddLast(ev);
                return;
            }

            //if it's not a parked message, it should have an event position
            Debug.Assert(ev.EventPosition != null);

            // Insert the retried event before any events with higher version number.
            var retryEventPosition = ev.EventPosition;

            var currentNode = _retry.First;

            while (currentNode != null)
            {
                var currentEventPosition = currentNode.Value.EventPosition;
                if (retryEventPosition.CompareTo(currentEventPosition) < 0)
                {
                    _retry.AddBefore(currentNode, ev);
                    return;
                }

                currentNode = currentNode.Next;
            }

            _retry.AddLast(ev);
        }
Exemplo n.º 5
0
        public void HandleReadCompleted(ResolvedEvent[] events, IPersistentSubscriptionStreamPosition newPosition, bool isEndOfStream)
        {
            lock (_lock) {
                if (!TryGetStreamBuffer(out var streamBuffer))
                {
                    return;
                }
                if ((_state & PersistentSubscriptionState.OutstandingPageRequest) == 0)
                {
                    return;
                }

                _state &= ~PersistentSubscriptionState.OutstandingPageRequest;

                if (streamBuffer.Live)
                {
                    return;
                }
                foreach (var ev in events)
                {
                    streamBuffer.AddReadMessage(OutstandingMessage.ForNewEvent(ev, _settings.EventSource.GetStreamPositionFor(ev)));
                }

                if (events.Length > 0)
                {
                    _statistics.SetLastKnownEventPosition(_settings.EventSource.GetStreamPositionFor(events[^ 1]));
Exemplo n.º 6
0
 private void MarkBeginProcessing(OutstandingMessage message)
 {
     _statistics.IncrementProcessed();
     StartMessage(message,
                  _settings.MessageTimeout == TimeSpan.MaxValue
                                 ? DateTime.MaxValue
                                 : DateTime.UtcNow + _settings.MessageTimeout);
 }
Exemplo n.º 7
0
        private void OnEventConfirmed(OutstandingMessage ev)
        {
            var handler = EventConfirmed;

            if (handler != null)
            {
                handler(this, ev.ResolvedEvent);
            }
        }
Exemplo n.º 8
0
 private bool ActionTakenForRetriedMessage(OutstandingMessage message)
 {
     if (message.RetryCount < _settings.MaxRetryCount)
     {
         return(false);
     }
     ParkMessage(message.ResolvedEvent, string.Format("Reached retry count of {0}", _settings.MaxRetryCount), 0);
     return(true);
 }
 public void StartMessage(OutstandingMessage message, DateTime expires)
 {
     var found = _outstandingRequests.ContainsKey(message.EventId);
     _outstandingRequests[message.EventId] = message;
     if (!found)
     {
         _bySequences.Add(message.ResolvedEvent.OriginalEventNumber, message.ResolvedEvent.OriginalEventNumber);
         _byTime.Add(new RetryableMessage(message.EventId, expires));
     }
 }
Exemplo n.º 10
0
 public static OutstandingMessage ForRetriedEvent(OutstandingMessage message)
 {
     return(new OutstandingMessage(
                message.EventId,
                message.ResolvedEvent,
                message.RetryCount + 1,
                message.IsReplayedEvent,
                message.EventSequenceNumber,
                message.EventPosition,
                message.PreviousEventPosition));
 }
Exemplo n.º 11
0
 public void AddLiveMessage(OutstandingMessage ev)
 {
     if (Live)
     {
         if (_buffer.Count < _maxBufferSize)
             _buffer.AddLast(ev);
         else
             Live = false;
     }
     _liveBuffer.Enqueue(ev);
 }
Exemplo n.º 12
0
        public void StartMessage(OutstandingMessage message, DateTime expires)
        {
            var found = _outstandingRequests.ContainsKey(message.EventId);

            _outstandingRequests[message.EventId] = message;
            if (!found)
            {
                _bySequences.Add(message.ResolvedEvent.OriginalEventNumber, message.ResolvedEvent.OriginalEventNumber);
                _byTime.Add(new RetryableMessage(message.EventId, expires));
            }
        }
Exemplo n.º 13
0
 public void AddLiveMessage(OutstandingMessage ev)
 {
     if (Live)
     {
         if (_buffer.Count < _maxBufferSize)
             _buffer.Enqueue(ev);
         else
             Live = false;
     }
     _liveBuffer.Enqueue(ev);
 }
Exemplo n.º 14
0
        public bool GetMessageById(Guid id, out OutstandingMessage outstandingMessage)
        {
            outstandingMessage = new OutstandingMessage();
            Tuple <DateTime, OutstandingMessage> m;

            if (_outstandingRequests.TryGetValue(id, out m))
            {
                outstandingMessage = m.Item2;
                return(true);
            }
            return(false);
        }
Exemplo n.º 15
0
        public StartMessageResult StartMessage(OutstandingMessage message, DateTime expires)
        {
            if (_outstandingRequests.ContainsKey(message.EventId))
            {
                return(StartMessageResult.SkippedDuplicate);
            }
            _outstandingRequests[message.EventId] = new Tuple <DateTime, OutstandingMessage>(expires, message);
            _bySequences.Add(message.ResolvedEvent.OriginalEventNumber, message.ResolvedEvent.OriginalEventNumber);
            _byTime.Add(new Tuple <DateTime, RetryableMessage>(expires, new RetryableMessage(message.EventId, expires)), false);

            return(StartMessageResult.Success);
        }
        private void StartMessage(OutstandingMessage message, DateTime expires)
        {
            var result = _outstandingMessages.StartMessage(message, expires);

            if (result == StartMessageResult.SkippedDuplicate)
            {
                Log.Warn("Skipping message {stream}/{eventNumber} with duplicate eventId {eventId}",
                         message.ResolvedEvent.OriginalStreamId,
                         message.ResolvedEvent.OriginalEventNumber,
                         message.EventId);
            }
        }
Exemplo n.º 17
0
 public bool TryPeek(out OutstandingMessage ev)
 {
     ev = new OutstandingMessage();
     if (_retry.Count > 0)
     {
         ev = _retry.Peek();
         return(true);
     }
     if (_buffer.Count <= 0)
     {
         return(false);
     }
     ev = _buffer.Peek();
     return(true);
 }
        public StartMessageResult StartMessage(OutstandingMessage message, DateTime expires)
        {
            if (_outstandingRequests.ContainsKey(message.EventId))
            {
                return(StartMessageResult.SkippedDuplicate);
            }
            _outstandingRequests[message.EventId] = new Tuple <DateTime, OutstandingMessage>(expires, message);
            Debug.Assert(message.IsReplayedEvent || message.EventSequenceNumber.HasValue);
            if (message.EventSequenceNumber.HasValue)
            {
                _bySequences.Add(message.EventSequenceNumber.Value, message);
            }
            _byTime.Add(new Tuple <DateTime, RetryableMessage>(expires, new RetryableMessage(message.EventId, expires)),
                        false);

            return(StartMessageResult.Success);
        }
Exemplo n.º 19
0
 public void AddReadMessage(OutstandingMessage ev)
 {
     if (Live) return;
     if (ev.ResolvedEvent.OriginalEventNumber <= _initialSequence)
         return;
     if (ev.ResolvedEvent.OriginalEventNumber < TryPeekLive())
     {
         _buffer.AddLast(ev);
     }
     else if (ev.ResolvedEvent.OriginalEventNumber > TryPeekLive())
     {
         DrainLiveTo(ev.ResolvedEvent.OriginalEventNumber);
         SwitchToLive();
     }
     else 
     {
         SwitchToLive();
     }
 }
Exemplo n.º 20
0
 public void AddReadMessage(OutstandingMessage ev)
 {
     if (Live) return;
     if (ev.ResolvedEvent.OriginalEventNumber <= _initialSequence)
         return;
     if (ev.ResolvedEvent.OriginalEventNumber < TryPeekLive())
     {
         _buffer.Enqueue(ev);
     }
     else if (ev.ResolvedEvent.OriginalEventNumber > TryPeekLive())
     {
         DrainLiveTo(ev.ResolvedEvent.OriginalEventNumber);
         SwitchToLive();
     }
     else 
     {
         SwitchToLive();
     }
 }
Exemplo n.º 21
0
        public void AddRetry(OutstandingMessage ev)
        {
            // Insert the retried event before any events with higher version number.

            var retryEventNumber = (ev.ResolvedEvent.Event ?? ev.ResolvedEvent.Link).EventNumber;

            var currentNode = _retry.First;

            while (currentNode != null)
            {
                var resolvedEvent = currentNode.Value.ResolvedEvent.Event ?? currentNode.Value.ResolvedEvent.Link;
                if (retryEventNumber < resolvedEvent.EventNumber)
                {
                    _retry.AddBefore(currentNode, ev);
                    return;
                }
                currentNode = currentNode.Next;
            }

            _retry.AddLast(ev);
        }
Exemplo n.º 22
0
        public void AddRetry(OutstandingMessage ev)
        {
            // Insert the retried event before any events with higher version number.

            var retryEventNumber = (ev.ResolvedEvent.Event ?? ev.ResolvedEvent.Link).EventNumber;

            var currentNode = _retry.First;

            while (currentNode != null)
            {
                var resolvedEvent = currentNode.Value.ResolvedEvent.Event ?? currentNode.Value.ResolvedEvent.Link;
                if (retryEventNumber < resolvedEvent.EventNumber)
                {
                    _retry.AddBefore(currentNode, ev);
                    return;
                }
                currentNode = currentNode.Next;
            }

            _retry.AddLast(ev);
        }
Exemplo n.º 23
0
 public void AddRetry(OutstandingMessage ev)
 {
     _retry.Enqueue(ev);    
 }
Exemplo n.º 24
0
 private void MarkBeginProcessing(OutstandingMessage message)
 {
     _statistics.IncrementProcessed();
     _outstandingMessages.StartMessage(message, DateTime.Now + _settings.MessageTimeout);
 }
Exemplo n.º 25
0
 public bool GetMessageById(Guid id, out OutstandingMessage outstandingMessage)
 {
     return(_outstandingRequests.TryGetValue(id, out outstandingMessage));
 }
Exemplo n.º 26
0
 public bool GetMessageById(Guid id, out OutstandingMessage outstandingMessage)
 {
     return _outstandingRequests.TryGetValue(id, out outstandingMessage);
 }
Exemplo n.º 27
0
 public void AddRetry(OutstandingMessage ev)
 {
     _retry.Enqueue(ev);
 }
Exemplo n.º 28
0
 private void MarkBeginProcessing(OutstandingMessage message)
 {
     _statistics.IncrementProcessed();
     _outstandingMessages.StartMessage(message, DateTime.Now + _settings.MessageTimeout);
 }
Exemplo n.º 29
0
 public bool TryDequeue(out OutstandingMessage ev)
 {
     ev = new OutstandingMessage();
     if (_retry.Count > 0)
     {
         ev = _retry.Dequeue();
         return true;
     }
     if (_buffer.Count <= 0) return false;
     ev = _buffer.Dequeue();
     return true;
 }
Exemplo n.º 30
0
        public static (OutstandingMessage message, bool newSequenceNumberAssigned) ForPushedEvent(OutstandingMessage message, long nextSequenceNumber, IPersistentSubscriptionStreamPosition previousEventPosition)
        {
            if (nextSequenceNumber > 0)
            {
                //only the first event may have a null previous event position
                Ensure.NotNull(previousEventPosition, nameof(previousEventPosition));
            }

            if (message.IsReplayedEvent)               //replayed parked message
            {
                return(message, false);
            }

            if (message.EventSequenceNumber.HasValue)               //retried message
            {
                return(message, false);
            }

            return(new OutstandingMessage(              //new event
                       message.EventId,
                       message.ResolvedEvent,
                       message.RetryCount,
                       message.IsReplayedEvent,
                       nextSequenceNumber,
                       message.EventPosition,
                       previousEventPosition), true);
        }
Exemplo n.º 31
0
 private bool ActionTakenForRetriedMessage(OutstandingMessage message)
 {
     if (message.RetryCount < _settings.MaxRetryCount) return false;
     ParkMessage(message.ResolvedEvent, string.Format("Reached retry count of {0}", _settings.MaxRetryCount), 0);
     return true;
 }
Exemplo n.º 32
0
 public ConsumerPushResult PushMessageToClient(OutstandingMessage message)
 {
     return(_consumerStrategy.PushMessageToClient(message));
 }