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; } } } }
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); }
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(); } }
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); }
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]));
private void MarkBeginProcessing(OutstandingMessage message) { _statistics.IncrementProcessed(); StartMessage(message, _settings.MessageTimeout == TimeSpan.MaxValue ? DateTime.MaxValue : DateTime.UtcNow + _settings.MessageTimeout); }
private void OnEventConfirmed(OutstandingMessage ev) { var handler = EventConfirmed; if (handler != null) { handler(this, ev.ResolvedEvent); } }
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)); } }
public static OutstandingMessage ForRetriedEvent(OutstandingMessage message) { return(new OutstandingMessage( message.EventId, message.ResolvedEvent, message.RetryCount + 1, message.IsReplayedEvent, message.EventSequenceNumber, message.EventPosition, message.PreviousEventPosition)); }
public void AddLiveMessage(OutstandingMessage ev) { if (Live) { if (_buffer.Count < _maxBufferSize) _buffer.AddLast(ev); else Live = false; } _liveBuffer.Enqueue(ev); }
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)); } }
public void AddLiveMessage(OutstandingMessage ev) { if (Live) { if (_buffer.Count < _maxBufferSize) _buffer.Enqueue(ev); else Live = false; } _liveBuffer.Enqueue(ev); }
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); }
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); } }
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); }
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(); } }
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(); } }
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); }
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); }
public void AddRetry(OutstandingMessage ev) { _retry.Enqueue(ev); }
private void MarkBeginProcessing(OutstandingMessage message) { _statistics.IncrementProcessed(); _outstandingMessages.StartMessage(message, DateTime.Now + _settings.MessageTimeout); }
public bool GetMessageById(Guid id, out OutstandingMessage outstandingMessage) { return(_outstandingRequests.TryGetValue(id, out outstandingMessage)); }
public bool GetMessageById(Guid id, out OutstandingMessage outstandingMessage) { return _outstandingRequests.TryGetValue(id, out outstandingMessage); }
public void AddRetry(OutstandingMessage ev) { _retry.Enqueue(ev); }
private void MarkBeginProcessing(OutstandingMessage message) { _statistics.IncrementProcessed(); _outstandingMessages.StartMessage(message, DateTime.Now + _settings.MessageTimeout); }
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; }
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); }
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 ConsumerPushResult PushMessageToClient(OutstandingMessage message) { return(_consumerStrategy.PushMessageToClient(message)); }