public void TryReadingNewBatch() { lock (_lock) { if (!TryGetStreamBuffer(out var streamBuffer)) { return; } if ((_state & PersistentSubscriptionState.OutstandingPageRequest) > 0) { return; } if (streamBuffer.Live) { SetLive(); return; } if (!streamBuffer.CanAccept(_settings.ReadBatchSize)) { return; } _state |= PersistentSubscriptionState.OutstandingPageRequest; _settings.StreamReader.BeginReadEvents(_settings.EventSource, _nextEventToPullFrom, Math.Max(_settings.ReadBatchSize, 10), _settings.ReadBatchSize, _settings.MaxCheckPointCount, _settings.ResolveLinkTos, _skipFirstEvent, HandleReadCompleted, HandleSkippedEvents, HandleReadError); _skipFirstEvent = false; } }
private void OnCheckpointLoaded(int? checkpoint) { lock (_lock) { _state = PersistentSubscriptionState.Behind; if (!checkpoint.HasValue) { Log.Debug(string.Format("Subscription {0}: read no checksum.", _settings.SubscriptionId)); Log.Debug("strtfrom = " + _settings.StartFrom); _lastPulledEvent = _settings.StartFrom >= 0 ? _settings.StartFrom : 0; _streamBuffer = new StreamBuffer(_settings.BufferSize, _settings.LiveBufferSize, -1, _settings.StartFrom >= 0); TryReadingNewBatch(); } else { _lastPulledEvent = checkpoint.Value; Log.Debug(string.Format("Subscription {0}: read checksum {1}", _settings.SubscriptionId, checkpoint.Value)); _streamBuffer = new StreamBuffer(_settings.BufferSize, _settings.LiveBufferSize, -1, true); TryReadingNewBatch(); } } }
public void HandleReadCompleted(ResolvedEvent[] events, long newposition, bool isEndOfStream) { lock (_lock) { if ((_state & PersistentSubscriptionState.OutstandingPageRequest) == 0) return; _state &= ~PersistentSubscriptionState.OutstandingPageRequest; if (_streamBuffer.Live) return; foreach (var ev in events) { _streamBuffer.AddReadMessage(new OutstandingMessage(ev.OriginalEvent.EventId, null, ev, 0)); } if (events.Length > 0) { _statistics.SetLastKnownEventNumber(events[events.Length - 1].OriginalEventNumber); } if (_streamBuffer.Live) { SetLive(); } if (isEndOfStream) { if(_streamBuffer.TryMoveToLive()){ SetLive(); return; } } _nextEventToPullFrom = newposition; TryReadingNewBatch(); TryPushingMessagesToClients(); } }
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 OnCheckpointLoaded(string checkpoint) { lock (_lock) { _state = PersistentSubscriptionState.Behind; if (checkpoint == null) { Log.Debug("Subscription {subscriptionId}: no checkpoint found", _settings.SubscriptionId); Log.Debug("Start from = " + _settings.StartFrom); _nextEventToPullFrom = _settings.StartFrom.IsLivePosition ? _settings.EventSource.StreamStartPosition : _settings.StartFrom; _streamBufferSource.SetResult(new StreamBuffer(_settings.BufferSize, _settings.LiveBufferSize, null, !_settings.StartFrom.IsLivePosition)); TryReadingNewBatch(); } else { _nextEventToPullFrom = _settings.EventSource.GetStreamPositionFor(checkpoint); _skipFirstEvent = true; //skip the checkpointed event //initialize values based on the loaded checkpoint _nextSequenceNumber = 1L; _lastCheckpointedSequenceNumber = 0L; _lastKnownSequenceNumber = 0L; _lastKnownMessage = _settings.EventSource.GetStreamPositionFor(checkpoint); _statistics.SetLastCheckPoint(_lastKnownMessage); Log.Debug("Subscription {subscriptionId}: read checkpoint {checkpoint}", _settings.SubscriptionId, checkpoint); _streamBufferSource.SetResult(new StreamBuffer(_settings.BufferSize, _settings.LiveBufferSize, _nextEventToPullFrom, true)); _settings.MessageParker.BeginLoadStats(TryReadingNewBatch); } } }
private void SetLive() { //TODO GFY this is hacky and just trying to keep the state at this level when it //lives in the streambuffer its for reporting reasons and likely should be revisited //at some point. _state &= ~PersistentSubscriptionState.Behind; _state |= PersistentSubscriptionState.Live; }
public void InitAsNew() { _state = PersistentSubscriptionState.NotReady; _lastCheckPoint = -1; _statistics.SetLastKnownEventNumber(-1); _settings.CheckpointReader.BeginLoadState(SubscriptionId, OnCheckpointLoaded); _pushClients = new PersistentSubscriptionClientCollection(_settings.ConsumerStrategy); }
public void InitAsNew() { _state = PersistentSubscriptionState.NotReady; _lastCheckPoint = -1; _statistics.SetLastKnownEventNumber(-1); _settings.CheckpointReader.BeginLoadState(SubscriptionId, OnCheckpointLoaded); _pushClients = new PersistentSubscriptionClientCollection(_settings.ConsumerStrategy); }
public void InitAsNew() { _state = PersistentSubscriptionState.NotReady; _nextSequenceNumber = 0L; _lastCheckpointedSequenceNumber = -1L; _lastKnownSequenceNumber = -1L; _lastKnownMessage = null; _statistics.SetLastKnownEventPosition(null); _settings.CheckpointReader.BeginLoadState(SubscriptionId, OnCheckpointLoaded); _pushClients = new PersistentSubscriptionClientCollection(_settings.ConsumerStrategy); }
public void TryReadingNewBatch() { lock (_lock) { if ((_state & PersistentSubscriptionState.OutstandingPageRequest) > 0) return; if (_streamBuffer.Live) { SetLive(); return; } if (!_streamBuffer.CanAccept(_settings.ReadBatchSize)) return; _state |= PersistentSubscriptionState.OutstandingPageRequest; _settings.StreamReader.BeginReadEvents(_settings.EventStreamId, _nextEventToPullFrom, Math.Max(_settings.ReadBatchSize, 10), _settings.ReadBatchSize, _settings.ResolveLinkTos, HandleReadCompleted); } }
public void RetryAllParkedMessages() { lock (_lock) { if ((_state & PersistentSubscriptionState.ReplayingParkedMessages) > 0) return; //already replaying _state |= PersistentSubscriptionState.ReplayingParkedMessages; _settings.MessageParker.BeginReadEndSequence(end => { if (!end.HasValue) { _state ^= PersistentSubscriptionState.ReplayingParkedMessages; return; //nothing to do. } TryReadingParkedMessagesFrom(0, end.Value + 1); }); } }
public void RetryParkedMessages(long?stopAt) { lock (_lock) { if ((_state & PersistentSubscriptionState.ReplayingParkedMessages) > 0) { return; //already replaying } _state |= PersistentSubscriptionState.ReplayingParkedMessages; _settings.MessageParker.BeginReadEndSequence(end => { if (!end.HasValue) { _state ^= PersistentSubscriptionState.ReplayingParkedMessages; return; //nothing to do. } var stopRead = stopAt.HasValue ? Math.Min(stopAt.Value, end.Value + 1) : end.Value + 1; TryReadingParkedMessagesFrom(0, stopRead); }); } }
public void HandleParkedReadCompleted(ResolvedEvent[] events, long newposition, bool isEndofStrem, long stopAt) { lock (_lock) { if ((_state & PersistentSubscriptionState.ReplayingParkedMessages) == 0) { return; } foreach (var ev in events) { if (ev.OriginalEventNumber == stopAt) { break; } Log.Debug("Retrying event {eventId} {stream}/{eventNumber} on subscription {subscriptionId}", ev.OriginalEvent.EventId, ev.OriginalStreamId, ev.OriginalEventNumber, _settings.SubscriptionId); StreamBuffer.AddRetry(new OutstandingMessage(ev.OriginalEvent.EventId, null, ev, 0)); } TryPushingMessagesToClients(); if (isEndofStrem || stopAt <= newposition) { var replayedEnd = newposition == -1 ? stopAt : Math.Min(stopAt, newposition); _settings.MessageParker.BeginMarkParkedMessagesReprocessed(replayedEnd); _state ^= PersistentSubscriptionState.ReplayingParkedMessages; } else { TryReadingParkedMessagesFrom(newposition, stopAt); } } }
private void OnCheckpointLoaded(long?checkpoint) { lock (_lock) { _state = PersistentSubscriptionState.Behind; if (!checkpoint.HasValue) { Log.Debug("Subscription {subscriptionId}: no checkpoint found", _settings.SubscriptionId); Log.Debug("Start from = " + _settings.StartFrom); _nextEventToPullFrom = _settings.StartFrom >= 0 ? _settings.StartFrom : 0; _streamBuffer = new StreamBuffer(_settings.BufferSize, _settings.LiveBufferSize, -1, _settings.StartFrom >= 0); TryReadingNewBatch(); } else { _nextEventToPullFrom = checkpoint.Value + 1; Log.Debug("Subscription {subscriptionId}: read checkpoint {checkpoint}", _settings.SubscriptionId, checkpoint.Value); _streamBuffer = new StreamBuffer(_settings.BufferSize, _settings.LiveBufferSize, -1, true); TryReadingNewBatch(); } } }
public void TryReadingNewBatch() { lock (_lock) { if ((_state & PersistentSubscriptionState.OutstandingPageRequest) > 0) return; if (_streamBuffer.Live) { SetLive(); return; } if (!_streamBuffer.CanAccept(_settings.ReadBatchSize)) return; _state |= PersistentSubscriptionState.OutstandingPageRequest; _settings.StreamReader.BeginReadEvents(_settings.EventStreamId, _lastPulledEvent, Math.Max(_settings.ReadBatchSize, 10), _settings.ReadBatchSize, _settings.ResolveLinkTos, HandleReadCompleted); } }
private void SetBehind() { _state |= PersistentSubscriptionState.Behind; _state &= ~PersistentSubscriptionState.Live; }
public void HandleParkedReadCompleted(ResolvedEvent[] events, int newposition, bool isEndofStrem, int stopAt) { lock (_lock) { if ((_state & PersistentSubscriptionState.ReplayingParkedMessages) == 0) return; foreach (var ev in events) { if (ev.OriginalEventNumber == stopAt) { break; } Log.Debug("Retrying event {0} on subscription {1}", ev.OriginalEvent.EventId, _settings.SubscriptionId); _streamBuffer.AddRetry(new OutstandingMessage(ev.OriginalEvent.EventId, null, ev, 0)); } TryPushingMessagesToClients(); if (isEndofStrem || stopAt <= newposition) { var replayedEnd = newposition == -1 ? stopAt : Math.Min(stopAt, newposition); _settings.MessageParker.BeginMarkParkedMessagesReprocessed(replayedEnd); _state ^= PersistentSubscriptionState.ReplayingParkedMessages; } else { TryReadingParkedMessagesFrom(newposition, stopAt); } } }
public void RetryAllParkedMessages() { lock (_lock) { if ((_state & PersistentSubscriptionState.ReplayingParkedMessages) > 0) return; //already replaying _state |= PersistentSubscriptionState.ReplayingParkedMessages; _settings.MessageParker.BeginReadEndSequence(end => { if (!end.HasValue) { _state |= PersistentSubscriptionState.ReplayingParkedMessages; return; //nothing to do. } TryReadingParkedMessagesFrom(0, end.Value + 1); }); } }
public void HandleReadCompleted(ResolvedEvent[] events, int newposition, bool isEndOfStream) { lock (_lock) { if ((_state & PersistentSubscriptionState.OutstandingPageRequest) == 0) return; _state &= ~PersistentSubscriptionState.OutstandingPageRequest; if (_streamBuffer.Live) return; foreach (var ev in events) { _streamBuffer.AddReadMessage(new OutstandingMessage(ev.OriginalEvent.EventId, null, ev, 0)); } if (_streamBuffer.Live) { SetLive(); } if (isEndOfStream) { SetLive(); _streamBuffer.MoveToLive(); return; } _lastPulledEvent = newposition; TryReadingNewBatch(); TryPushingMessagesToClients(); } }
private void SetBehind() { _state |= PersistentSubscriptionState.Behind; _state &= ~PersistentSubscriptionState.Live; }
private void SetLive() { //TODO GFY this is hacky and just trying to keep the state at this level when it //lives in the streambuffer its for reporting reasons and likely should be revisited //at some point. _state &= ~PersistentSubscriptionState.Behind; _state |= PersistentSubscriptionState.Live; }