/// <summary> /// Convert a <see cref="Runtime.Grpc.Interaction.CommittedEventStream"/> to <see cref="CommittedEventStream"/> /// </summary> /// <param name="protobuf"><see cref="Runtime.Grpc.Interaction.CommittedEventStream"/> to convert from</param> /// <returns>Converted <see cref="CommittedEventStream"/></returns> public static CommittedEventStream ToCommittedEventStream(this Runtime.Grpc.Interaction.CommittedEventStream protobuf) { var eventSourceId = protobuf.Source.EventSource.ToConcept <EventSourceId>(); var artifactId = protobuf.Source.Artifact.ToConcept <ArtifactId>(); var versionedEventSource = new VersionedEventSource(eventSourceId, artifactId); var commitId = protobuf.Id.ToConcept <CommitId>(); var correlationId = protobuf.CorrelationId.ToConcept <CorrelationId>(); var timeStamp = protobuf.TimeStamp.ToDateTimeOffset(); var events = protobuf.Events.Select(_ => new EventEnvelope( _.Metadata.ToEventMetadata(), _.Event.ToPropertyBag() ) ).ToArray(); return(new CommittedEventStream( protobuf.Sequence, versionedEventSource, commitId, correlationId, timeStamp, new EventStream(events) )); }
/// <summary> /// Builds a <see cref="FilterDefinition{BsonDocument}" /> corresponding to the <see cref="VersionedEventSource" /> supplied. /// </summary> /// <param name="version">A <see cref="VersionedEventSource" />.</param> /// <returns>A <see cref="FilterDefinition{BsonDocument}" /> corresponding to the <see cref="VersionedEventSource" />.</returns> public static FilterDefinition <BsonDocument> ToFilter(this VersionedEventSource version) { var builder = Builders <BsonDocument> .Filter; return(builder.Eq(Constants.EVENTSOURCE_ID, version.EventSource.Value) & builder.Eq(VersionConstants.COMMIT, version.Version.Commit)); }
public static VersionedEventSource Next(this VersionedEventSource version) { Ensure.IsNotNull(nameof(version), version); Ensure.ArgumentPropertyIsNotNull(nameof(version), "Version", version.Version); Ensure.ArgumentPropertyIsNotNull(nameof(version), "EventSource", version.EventSource); return(new VersionedEventSource(version.Version.Next(), new EventSourceKey(version.EventSource, version.Artifact))); }
/// <summary> /// /// </summary> /// <param name="sequence"></param> /// <param name="source"></param> /// <param name="id"></param> /// <param name="timestamp"></param> /// <param name="correlationId"></param> public CommitMetadata(CommitSequenceNumber sequence, VersionedEventSource source, CommitId id, DateTimeOffset timestamp, CorrelationId correlationId) { Sequence = sequence; Source = source; Id = id; Timestamp = timestamp; CorrelationId = correlationId; }
public static UncommittedEventStream BuildUncommitted(this EventSourceKey eventSource, DateTimeOffset?now = null, CorrelationId correlationId = null) { var committed = now ?? DateTimeOffset.Now; var events = BuildEvents(); VersionedEventSource versionedEventSource = eventSource.Id.InitialVersion(eventSource.Artifact); return(BuildFrom(versionedEventSource, committed, correlationId ?? Guid.NewGuid(), events)); }
/// <summary> /// Convert from <see cref="VersionedEventSource"/> to <see cref="grpc.VersionedEventSource"/>. /// </summary> /// <param name="versionedEventSource"><see cref="VersionedEventSource"/> to convert from.</param> /// <returns>Converted <see cref="grpc.VersionedEventSource"/>.</returns> public static grpc.VersionedEventSource ToProtobuf(this VersionedEventSource versionedEventSource) { return(new grpc.VersionedEventSource { Version = versionedEventSource.Version.ToProtobuf(), EventSource = versionedEventSource.EventSource.ToProtobuf(), Artifact = versionedEventSource.Artifact.ToProtobuf() }); }
UncommittedEventStream BuildFrom(VersionedEventSource version, CorrelationId correlationId, DateTimeOffset committed, IEnumerable <VersionedEvent> events) { var envelopes = events.Select(e => e.Event.ToEnvelope(BuildEventMetadata(e.Event, EventId.New(), ToVersionedEventSource(e, version.EventSource, version.Artifact), correlationId, committed))).ToList(); if (envelopes == null || !envelopes.Any()) { throw new ApplicationException("There are no envelopes"); } return(BuildStreamFrom(EventStream.From(envelopes))); }
void ThrowIfConcurrencyConflict(VersionedEventSource version) { if (_versions.TryGetValue(version.Key, out VersionedEventSource ver)) { if (ver == version || ver.Version.Commit >= version.Version.Commit) { throw new EventSourceConcurrencyConflict(version.Version, ver.Version); } } }
/// <summary> /// Converts a <see cref="VersionedEventSource" /> into its <see cref="BsonDocument" /> representation /// </summary> /// <param name="version">The <see cref="VersionedEventSource" /></param> /// <returns>A <see cref="BsonDocument" /> representation of the <see cref="VersionedEventSource" /></returns> public static BsonDocument AsBsonVersion(this VersionedEventSource version) { //expand to hold reference to snapshot, maybe event count for a snapshotting process return(new BsonDocument(new Dictionary <string, object> { { Constants.EVENTSOURCE_ID, version.EventSource.Value }, { VersionConstants.COMMIT, version.Version.Commit }, { VersionConstants.SEQUENCE, version.Version.Sequence } })); }
/// <summary> /// Instantiates a new instance of a <see cref="CommittedEventStream" /> /// </summary> /// <param name="sequence">The <see cref="CommitSequenceNumber" /> for this committed event stream</param> /// <param name="source">The <see cref="VersionedEventSource" /> that this stream applies to</param> /// <param name="id">The unique id in the form of a <see cref="CommitId" /></param> /// <param name="correlationId">A <see cref="CorrelationId" /> used to relate this event stream to other actions in the system</param> /// <param name="timestamp">A timestamp in the form of a <see cref="DateTimeOffset" /> representing when the stream was committed</param> /// <param name="events">An enumerable of <see cref="EventEnvelope" /> representing the events that are committed in this commit</param> public CommittedEventStream(CommitSequenceNumber sequence, VersionedEventSource source, CommitId id, CorrelationId correlationId, DateTimeOffset timestamp, EventStream events) { Sequence = sequence; Source = source; Id = id; Timestamp = timestamp; CorrelationId = correlationId; Events = events; LastEventVersion = source.ToCommittedEventVersion(sequence); }
/// <summary> /// Convert from <see cref="VersionedEventSource"/> to <see cref="Runtime.Grpc.Interaction.VersionedEventSource"/> /// </summary> /// <param name="versionedEventSource"><see cref="VersionedEventSource"/> to convert from</param> /// <returns>Converted <see cref="Runtime.Grpc.Interaction.VersionedEventSource"/></returns> public static Runtime.Grpc.Interaction.VersionedEventSource ToProtobuf(this VersionedEventSource versionedEventSource) { var source = new Runtime.Grpc.Interaction.VersionedEventSource { Version = versionedEventSource.Version.ToProtobuf(), EventSource = versionedEventSource.EventSource.ToProtobuf(), Artifact = versionedEventSource.Artifact.ToProtobuf() }; return(source); }
/// <summary> /// Convert from <see cref="Dolittle.Runtime.Events.VersionedEventSource"/> to <see cref="VersionedEventSource"/> /// </summary> /// <param name="versionedEventSource"><see cref="Dolittle.Runtime.Events.VersionedEventSource"/> to convert from</param> /// <returns>Converted <see cref="VersionedEventSource"/></returns> public static VersionedEventSource ToProtobuf(this Dolittle.Runtime.Events.VersionedEventSource versionedEventSource) { var source = new VersionedEventSource { Version = versionedEventSource.Version.ToProtobuf(), EventSource = versionedEventSource.EventSource.ToProtobuf(), Artifact = versionedEventSource.Artifact.ToProtobuf() }; return(source); }
/// <summary> /// Initializes a new instance of the <see cref="UncommittedEventStream"/> class. /// </summary> /// <param name="id">The unique id in the form of a <see cref="CommitId" />.</param> /// <param name="correlationId">A <see cref="CorrelationId" /> used to relate this event stream to other actions in the system.</param> /// <param name="source">The <see cref="VersionedEventSource" /> that this stream applies to.</param> /// <param name="timestamp">A timestamp in the form of a <see cref="DateTimeOffset" /> representing when the stream was committed.</param> /// <param name="events">An <see cref="EventStream" /> representing the events (an enumerable of <see cref="EventEnvelope" />) that are committed in this commit.</param> public UncommittedEventStream(CommitId id, CorrelationId correlationId, VersionedEventSource source, DateTimeOffset timestamp, EventStream events) { CorrelationId = correlationId; Ensure.IsNotNull(nameof(id), id); Ensure.IsNotNull(nameof(source), source); Ensure.IsNotNull(nameof(events), events); Id = id; Timestamp = timestamp; Source = source; CorrelationId = correlationId; Events = events; }
static UncommittedEventStream BuildFrom(VersionedEventSource version, DateTimeOffset committed, CorrelationId correlationId, IEnumerable <EventEnvelope> events) { var envelopes = new List <EventEnvelope>(); VersionedEventSource vsn = null; events.ForEach(e => { vsn = vsn == null ? version : new VersionedEventSource(vsn.Version.NextSequence(), new EventSourceKey(vsn.EventSource, vsn.Artifact)); envelopes.Add(e.ToNewEnvelope(vsn, committed, correlationId)); }); if (envelopes?.Any() != true) { throw new ApplicationException("There are no envelopes"); } return(BuildStreamFrom(envelopes.ToEventStream())); }
private static UncommittedEventStream BuildFrom(VersionedEventSource version, DateTimeOffset committed, CorrelationId correlationId, IEnumerable <IEvent> events) { var envelopes = new List <EventEnvelope>(); VersionedEventSource vsn = null; events.ForEach(e => { vsn = vsn == null ? version : new VersionedEventSource(vsn.Version.NextSequence(), vsn.EventSource, vsn.Artifact); envelopes.Add(e.ToEnvelope(BuildEventMetadata(EventId.New(), vsn, e.ToArtifact().Initial(), correlationId, committed))); }); if (envelopes == null || !envelopes.Any()) { throw new ApplicationException("There are no envelopes"); } return(BuildStreamFrom(envelopes.ToEventStream())); }
static EventStream BuildEventStreamFrom(VersionedEventSource version, DateTimeOffset committed, CorrelationId correlationId, IEnumerable <IEvent> events) { var envelopes = new List <EventEnvelope>(); VersionedEventSource vsn = null; events.ForEach(e => { vsn = vsn == null ? version : new VersionedEventSource(vsn.Version.NextSequence(), new EventSourceKey(vsn.EventSource, vsn.Artifact)); var envelope = e.ToEnvelope(BuildEventMetadata(EventId.New(), vsn, e is SimpleEvent ? Artifacts.artifact_for_simple_event : Artifacts.artifact_for_another_event, correlationId, DateTimeOffset.UtcNow)); envelopes.Add(envelope); }); if (envelopes?.Any() != true) { throw new ApplicationException("There are no envelopes"); } return(EventStream.From(envelopes)); }
public static CommittedEventStream Build(CommittedEventVersion previousVersion = null) { CommittedEventVersion version; if (previousVersion != null) { version = new CommittedEventVersion(previousVersion.Major + 1, previousVersion.Minor + 1, 0); } else { version = new CommittedEventVersion(1, 1, 0); } var versionedEventSource = new VersionedEventSource(new EventSourceVersion(version.Minor, version.Revision), new EventSourceKey(event_source_id, Artifacts.artifact_for_event_source.Id)); var now = DateTimeOffset.UtcNow; var correlationId = CorrelationId.New(); var eventStream = BuildEventStreamFrom(versionedEventSource, now, correlationId, BuildEvents()); return(new CommittedEventStream(version.Major, eventStream.Last().Metadata.VersionedEventSource, CommitId.New(), correlationId, now, eventStream)); }
static EventMetadata BuildEventMetadata(EventId id, VersionedEventSource versionedEventSource, Artifact artifact, CorrelationId correlationId, DateTimeOffset committed) { return(new EventMetadata(id, versionedEventSource, correlationId, artifact, committed, GetOriginalContext())); }
EventMetadata BuildEventMetadata(IEvent @event, EventId id, VersionedEventSource versionedEventSource, CorrelationId correlationId, DateTimeOffset committed) { return(new EventMetadata(id, versionedEventSource, correlationId, _artifactMap.GetArtifactFor(@event.GetType()), committed, _executionContextManager.Current)); }
public static CommittedEvent build_committed_event(VersionedEventSource versionedEventSource, IEvent @event, CommittedEventVersion version) { var metadata = new EventMetadata(EventId.New(), versionedEventSource, CorrelationId.New(), new Artifact(ArtifactId.New(), 1), DateTime.UtcNow, GetOriginalContext()); return(new CommittedEvent(version, metadata, @event)); }
/// <summary> /// Processes /// </summary> /// <param name="committedEventStreamWithContext"></param> /// <returns></returns> public Task <CommitSequenceNumber> Process(CommittedEventStreamWithContext committedEventStreamWithContext) { try { var originatingSequence = committedEventStreamWithContext.EventStream.Sequence; var context = committedEventStreamWithContext.Context; var particleStream = committedEventStreamWithContext.EventStream; EventSourceVersion version = null; _executionContextManager.CurrentFor(context); using (var _ = _getEventStore()) { version = _.GetNextVersionFor(particleStream.Source.Key); } var versionedEventSource = new VersionedEventSource(version, new EventSourceKey(particleStream.Source.EventSource, particleStream.Source.Artifact)); var eventEnvelopes = new List <EventEnvelope>(); particleStream.Events.ForEach(_ => { _.Metadata.OriginalContext.CommitInOrigin = particleStream.Sequence; var envelope = new EventEnvelope( new EventMetadata( _.Id, new VersionedEventSource(version, new EventSourceKey(particleStream.Source.EventSource, particleStream.Source.Artifact)), _.Metadata.CorrelationId, _.Metadata.Artifact, _.Metadata.Occurred, _.Metadata.OriginalContext ), _.Event ); eventEnvelopes.Add(envelope); version = version.NextSequence(); }); var uncommittedEventStream = new Store.UncommittedEventStream( particleStream.Id, particleStream.CorrelationId, versionedEventSource, particleStream.Timestamp, new Store.EventStream(eventEnvelopes) ); _logger.Information("Commit events to store"); Store.CommittedEventStream committedEventStream = null; using (var _ = _getEventStore()) { committedEventStream = _.Commit(uncommittedEventStream); } SetOffset(_eventHorizonKey, originatingSequence); _logger.Information("Process committed events"); _processingHub.Process(committedEventStream); return(Task.FromResult(committedEventStream.Sequence)); } catch (Exception ex) { _logger.Error(ex, "Couldn't handle incoming commit"); return(Task.FromException <CommitSequenceNumber>(ex)); } }
public static EventEnvelope ToNewEnvelope(this EventEnvelope envelope, VersionedEventSource versionedEventSource, DateTimeOffset committed, CorrelationId correlationId) { return(new EventEnvelope(new EventMetadata(EventId.New(), versionedEventSource, correlationId, envelope.Metadata.Artifact, committed, envelope.Metadata.OriginalContext), envelope.Event)); }
/// <summary> /// Convert from <see cref="VersionedEventSource"/> to <see cref="Dolittle.Runtime.Events.VersionedEventSource"/> /// </summary> /// <param name="protobuf"><see cref="VersionedEventSource"/> to convert from</param> /// <returns>Converted <see cref="Dolittle.Runtime.Events.VersionedEventSource"/></returns> public static Dolittle.Runtime.Events.VersionedEventSource ToVersionedEventSource(this VersionedEventSource protobuf) { return(new Dolittle.Runtime.Events.VersionedEventSource( protobuf.Version.ToEventSourceVersion(), new EventSourceKey(protobuf.EventSource.ToConcept <EventSourceId>(), protobuf.Artifact.ToConcept <ArtifactId>()))); }