private void DistributeMessage(ProjectionCoreServiceMessage.CommittedEventDistributed message) { foreach (var subscriber in _headSubscribers.Values) { subscriber.Handle(message); } }
public void Handle(ProjectionCoreServiceMessage.CommittedEventDistributed message) { Guid projectionId; if (_stopped) { return; } if (_headingEventDistributionPoint.Handle(message)) { return; } if (!_distributionPointSubscriptions.TryGetValue(message.CorrelationId, out projectionId)) { return; // unsubscribed } if (TrySubscribeHeadingDistributionPoint(message, projectionId)) { return; } if (message.Data != null) // means notification about the end of the stream/source { _subscriptions[projectionId].Handle(message); } }
private bool TrySubscribeHeadingDistributionPoint( ProjectionCoreServiceMessage.CommittedEventDistributed message, Guid projectionId) { if (_pausedProjections.Contains(projectionId)) { return(false); } var projectionSubscription = _subscriptions[projectionId]; if (message.SafeTransactionFileReaderJoinPosition == null || !_headingEventDistributionPoint.TrySubscribe( projectionId, projectionSubscription, message.SafeTransactionFileReaderJoinPosition.Value)) { return(false); } if (message.Data == null) { _logger.Trace( "The '{0}' is subscribing to the heading distribution point with TF-EOF marker event at '{1}'", projectionId, message.SafeTransactionFileReaderJoinPosition); } Guid distributionPointId = message.CorrelationId; _distributionPoints[distributionPointId].Dispose(); _distributionPoints.Remove(distributionPointId); _distributionPointSubscriptions.Remove(distributionPointId); _projectionDistributionPoints[projectionId] = Guid.Empty; return(true); }
public void setup() { _zeroEvent = new ProjectionCoreServiceMessage.CommittedEventDistributed(Guid.NewGuid(), new EventPosition(10, 0), "stream1", 0, false, new Event(Guid.NewGuid(), "StreamCreated", false, new byte[0], new byte[0])); _firstEvent = new ProjectionCoreServiceMessage.CommittedEventDistributed(Guid.NewGuid(), new EventPosition(30, 20), "stream1", 1, false, new Event(Guid.NewGuid(), "Data", true, Encoding.UTF8.GetBytes("{}"), new byte[0])); _secondEvent = new ProjectionCoreServiceMessage.CommittedEventDistributed(Guid.NewGuid(), new EventPosition(50, 40), "stream2", 0, false, new Event(Guid.NewGuid(), "StreamCreated", false, new byte[0], new byte[0])); _thirdEvent = new ProjectionCoreServiceMessage.CommittedEventDistributed(Guid.NewGuid(), new EventPosition(70, 60), "stream2", 1, false, new Event(Guid.NewGuid(), "Data", true, Encoding.UTF8.GetBytes("{}"), new byte[0])); }
public override CheckpointTag MakeCheckpointTag(CheckpointTag previous, ProjectionCoreServiceMessage.CommittedEventDistributed comittedEvent) { if (comittedEvent.PositionStreamId != _stream) { throw new InvalidOperationException(string.Format("Invalid stream '{0}'. Expected stream is '{1}'", comittedEvent.EventStreamId, _stream)); } return(CheckpointTag.FromStreamPosition(comittedEvent.PositionStreamId, comittedEvent.PositionSequenceNumber)); }
public override bool IsMessageAfterCheckpointTag(CheckpointTag previous, ProjectionCoreServiceMessage.CommittedEventDistributed comittedEvent) { if (previous.GetMode() != CheckpointTag.Mode.Position) { throw new ArgumentException("Mode.Position expected", "previous"); } return(comittedEvent.Position > previous.Position); }
public override CheckpointTag MakeCheckpointTag(CheckpointTag previous, ProjectionCoreServiceMessage.CommittedEventDistributed comittedEvent) { if (!_streams.Contains(comittedEvent.PositionStreamId)) { throw new InvalidOperationException(string.Format("Invalid stream '{0}'", comittedEvent.EventStreamId)); } return(previous.UpdateStreamPosition(comittedEvent.PositionStreamId, comittedEvent.PositionSequenceNumber)); }
public override bool IsMessageAfterCheckpointTag(CheckpointTag previous, ProjectionCoreServiceMessage.CommittedEventDistributed comittedEvent) { if (previous.GetMode() != CheckpointTag.Mode.Stream) { throw new ArgumentException("Mode.Stream expected", "previous"); } return(comittedEvent.PositionStreamId == _stream && comittedEvent.PositionSequenceNumber > previous.Streams[_stream]); }
private void ValidateEventOrder(ProjectionCoreServiceMessage.CommittedEventDistributed message) { if (_lastEventPosition >= message.Position) { throw new InvalidOperationException( string.Format( "Invalid committed event order. Last: '{0}' Received: '{1}'", _lastEventPosition, message.Position)); } _lastEventPosition = message.Position; }
private void CacheRecentMessage(ProjectionCoreServiceMessage.CommittedEventDistributed message) { _lastMessages.Enqueue(message); if (_lastMessages.Count > _eventCacheSize) { _lastMessages.Dequeue(); } var lastAvailableCommittedevent = _lastMessages.Peek(); _subscribeFromPosition = lastAvailableCommittedevent.Position; }
public bool Handle(ProjectionCoreServiceMessage.CommittedEventDistributed message) { EnsureStarted(); if (message.CorrelationId != _distributionPointId) { return(false); } if (message.Data == null) { return(true); } ValidateEventOrder(message); CacheRecentMessage(message); DistributeMessage(message); if (_headSubscribers.Count == 0 && !_headDistributionPointPaused) { _headDistributionPoint.Pause(); _headDistributionPointPaused = true; } return(true); }
public void Handle(ProjectionCoreServiceMessage.CommittedEventDistributed message) { _receivedEvents.Add(message); }
public abstract CheckpointTag MakeCheckpointTag(CheckpointTag previous, ProjectionCoreServiceMessage.CommittedEventDistributed comittedEvent);
public void Handle(ProjectionCoreServiceMessage.CommittedEventDistributed message) { if (message.Data == null) { throw new NotSupportedException(); } // NOTE: we may receive here messages from heading event distribution point // and they may not pass out source filter. Discard them first var roundedProgress = (float)Math.Round(message.Progress, 2); bool progressChanged = _progress != roundedProgress; _progress = roundedProgress; if (!_eventFilter.PassesSource(message.ResolvedLinkTo, message.PositionStreamId)) { if (progressChanged) { _progressHandler.Handle( new ProjectionSubscriptionMessage.ProgressChanged( _projectionCorrelationId, _positionTracker.LastTag, _progress, _subscriptionMessageSequenceNumber++)); } return; } // NOTE: after joining heading distribution point it delivers all cached events to the subscription // some of this events we may have already received. The delivered events may have different order // (in case of partially ordered cases multi-stream reader etc). We discard all the messages that are not // after the last available checkpoint tag if (!_positionTagger.IsMessageAfterCheckpointTag(_positionTracker.LastTag, message)) { _logger.Trace( "Skipping replayed event {0}@{1} at position {2}. the last processed event checkpoint tag is: {3}", message.PositionSequenceNumber, message.PositionStreamId, message.Position, _positionTracker.LastTag); return; } var eventCheckpointTag = _positionTagger.MakeCheckpointTag(_positionTracker.LastTag, message); if (eventCheckpointTag <= _positionTracker.LastTag) { throw new Exception( string.Format( "Invalid checkpoint tag was built. Tag '{0}' must be greater than '{1}'", eventCheckpointTag, _positionTracker.LastTag)); } _positionTracker.UpdateByCheckpointTagForward(eventCheckpointTag); if (_eventFilter.Passes(message.ResolvedLinkTo, message.PositionStreamId, message.Data.EventType)) { _lastPassedOrCheckpointedEventPosition = message.Position; var convertedMessage = ProjectionSubscriptionMessage.CommittedEventReceived.FromCommittedEventDistributed( message, eventCheckpointTag, _eventFilter.GetCategory(message.PositionStreamId), _subscriptionMessageSequenceNumber++); _eventHandler.Handle(convertedMessage); } else { if (_checkpointUnhandledBytesThreshold != null && message.Position.CommitPosition - _lastPassedOrCheckpointedEventPosition.CommitPosition > _checkpointUnhandledBytesThreshold) { _lastPassedOrCheckpointedEventPosition = message.Position; _checkpointHandler.Handle( new ProjectionSubscriptionMessage.CheckpointSuggested( _projectionCorrelationId, _positionTracker.LastTag, message.Progress, _subscriptionMessageSequenceNumber++)); } else { if (progressChanged) { _progressHandler.Handle( new ProjectionSubscriptionMessage.ProgressChanged( _projectionCorrelationId, _positionTracker.LastTag, _progress, _subscriptionMessageSequenceNumber++)); } } } }
public override CheckpointTag MakeCheckpointTag(CheckpointTag previous, ProjectionCoreServiceMessage.CommittedEventDistributed comittedEvent) { return(new CheckpointTag(comittedEvent.Position)); }