/// <inheritdoc/> public EventSourceVersion GetVersionFor(IApplicationResourceIdentifier eventSource, EventSourceId eventSourceId) { var partitionKeyFilter = GetPartitionKeyFilterFor(eventSource, eventSourceId); var query = new TableQuery <DynamicTableEntity>().Select(new[] { "Version" }).Where(partitionKeyFilter); var events = new List <DynamicTableEntity>(); TableContinuationToken continuationToken = null; do { _table.ExecuteQuerySegmentedAsync(query, continuationToken).ContinueWith(e => { events.AddRange(e.Result.Results); continuationToken = e.Result.ContinuationToken; }).Wait(); } while (continuationToken != null); var result = events.OrderByDescending(e => long.Parse(e.RowKey)).FirstOrDefault(); if (result == null) { return(EventSourceVersion.Zero); } var value = result.Properties["Version"].DoubleValue.Value; var version = EventSourceVersion.FromCombined(value); return(version); }
public object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { var versionAsDouble = bsonReader.ReadDouble(); var version = EventSourceVersion.FromCombined(versionAsDouble); return(version); }
public object NullSafeGet(IDataReader rs, string[] names, object owner) { var versionAsDouble = (double)NHibernateUtil.Double.NullSafeGet(rs, names[0]); var version = EventSourceVersion.FromCombined(versionAsDouble); return(version); }
public EventSourceVersion GetLastCommittedVersion(EventSource eventSource, Guid eventSourceId) { var eventSourceParam = new OracleParameter(EventParameters.EVENTSOURCE, OracleDbType.NVarchar2, 512); eventSourceParam.Value = eventSource.GetType().AssemblyQualifiedName; var eventSourceIdParam = new OracleParameter(EventParameters.EVENTSOURCEID, OracleDbType.Raw, 16); eventSourceIdParam.Value = eventSourceId.ToByteArray(); var version = EventSourceVersion.Zero; try { OpenConnection(); using (var command = _connection.CreateCommand()) { command.CommandText = LAST_VERSION_STATEMENT; command.Parameters.Add(eventSourceIdParam); command.Parameters.Add(eventSourceParam); command.BindByName = true; var reader = command.ExecuteReader(CommandBehavior.SingleResult); if (reader.Read()) { version = EventSourceVersion.FromCombined(Convert.ToDouble(reader.GetDecimal(0))); } } } finally { EnsureConnectionClosed(_connection); } return(version); }
/// <inheritdoc/> public IEnumerable <EventAndEnvelope> GetFor(IApplicationResourceIdentifier eventSource, EventSourceId eventSourceId) { var eventSourceIdentifier = _applicationResourceIdentifierConverter.AsString(eventSource); var eventPath = GetPathFor(eventSourceIdentifier, eventSourceId); var files = _files.GetFilesIn(eventPath).OrderBy(f => f); var eventFiles = files.Where(f => f.EndsWith(".event")).ToArray(); var envelopeFiles = files.Where(f => f.EndsWith(".envelope")).ToArray(); if (eventFiles.Length != envelopeFiles.Length) { throw new Exception($"There is a problem with event files for {eventSourceIdentifier} with Id {eventSourceId}"); } var events = new List <EventAndEnvelope>(); for (var eventIndex = 0; eventIndex < eventFiles.Length; eventIndex++) { var envelopeFile = envelopeFiles[eventIndex]; var eventFile = eventFiles[eventIndex]; var envelopeAsJson = _files.ReadString(Path.GetDirectoryName(envelopeFile), Path.GetFileName(envelopeFile)); var eventAsJson = _files.ReadString(Path.GetDirectoryName(eventFile), Path.GetFileName(eventFile)); var envelopeValues = _serializer.GetKeyValuesFromJson(envelopeAsJson); var _correllationId = Guid.Parse((string)envelopeValues["CorrelationId"]); var _eventId = Guid.Parse((string)envelopeValues["EventId"]); var _sequenceNumber = (long)envelopeValues["SequenceNumber"]; var _sequenceNumberForEventType = (long)envelopeValues["SequenceNumberForEventType"]; var _generation = (long)envelopeValues["Generation"]; var _event = _applicationResourceIdentifierConverter.FromString((string)envelopeValues["Event"]); var _eventSourceId = Guid.Parse((string)envelopeValues["EventSourceId"]); var _eventSource = _applicationResourceIdentifierConverter.FromString((string)envelopeValues["EventSource"]); var _eventSourceVersion = EventSourceVersion.FromCombined(double.Parse(envelopeValues["Version"].ToString())); var _causedBy = (string)envelopeValues["CausedBy"]; var _occurred = (DateTime)envelopeValues["Occurred"]; var envelope = new EventEnvelope( _correllationId, _eventId, _sequenceNumber, _sequenceNumberForEventType, (int)_generation, _event, _eventSourceId, _eventSource, _eventSourceVersion, _causedBy, _occurred ); var eventType = _applicationResourceResolver.Resolve(envelope.Event); var @event = Activator.CreateInstance(eventType, eventSourceId) as IEvent; _serializer.FromJson(@event, eventAsJson); events.Add(new EventAndEnvelope(envelope, @event)); } return(events); }
Task Receive(Message message, CancellationToken token) { var dynamicEventsAndEnvelopes = new List <dynamic>(); var json = System.Text.Encoding.UTF8.GetString(message.Body); _serializer.FromJson(dynamicEventsAndEnvelopes, json); var eventsAndEnvelopes = new List <EventAndEnvelope>(); foreach (var dynamicEventAndEnvelope in dynamicEventsAndEnvelopes) { var env = dynamicEventAndEnvelope.Envelope; var correlationId = (TransactionCorrelationId)Guid.Parse(env.CorrelationId.ToString()); var eventId = (EventId)Guid.Parse(env.EventId.ToString()); var sequenceNumber = (EventSequenceNumber)long.Parse(env.SequenceNumber.ToString()); var sequenceNumberForEventType = (EventSequenceNumber)long.Parse(env.SequenceNumberForEventType.ToString()); var generation = (EventGeneration)long.Parse(env.Generation.ToString()); var @event = _applicationResourceIdentifierConverter.FromString(env.Event.ToString()); var eventSourceId = (EventSourceId)Guid.Parse(env.EventSourceId.ToString()); var eventSource = _applicationResourceIdentifierConverter.FromString(env.EventSource.ToString()); var version = (EventSourceVersion)EventSourceVersion.FromCombined(double.Parse(env.Version.ToString())); var causedBy = (CausedBy)env.CausedBy.ToString(); var occurred = DateTimeOffset.Parse(env.Occurred.ToString()); var envelope = new EventEnvelope( correlationId, eventId, sequenceNumber, sequenceNumberForEventType, generation, @event, eventSourceId, eventSource, version, causedBy, occurred ); var eventType = _applicationResourceResolver.Resolve(@event); var eventInstance = Activator.CreateInstance(eventType, new object[] { eventSourceId }) as IEvent; var e = dynamicEventAndEnvelope.Event.ToString(); _serializer.FromJson(eventInstance, e); eventsAndEnvelopes.Add(new EventAndEnvelope(envelope, eventInstance)); } var stream = new CommittedEventStream(eventsAndEnvelopes.First().Envelope.EventSourceId, eventsAndEnvelopes); Received(stream); _queueClient.CompleteAsync(message.SystemProperties.LockToken); return(Task.CompletedTask); }
/// <inheritdoc/> public EventSourceVersion GetVersionFor(IApplicationResourceIdentifier eventSource, EventSourceId eventSourceId) { var filter = Builders <BsonDocument> .Filter.Eq("EventSourceId", eventSourceId); var result = _collection.Find(filter).SortByDescending(bson => bson["Version"]).Single(); var value = result["Version"].AsDouble; var version = EventSourceVersion.FromCombined(value); return(version); }
/// <inheritdoc/> public EventSourceVersion GetLastCommittedVersionFor(IEventSource eventSource) { var version = EventSourceVersion.Zero; _client .ExecuteStoredProcedureAsync <double>(_getLastCommittedVersionStoredProcedure.SelfLink, eventSource.EventSourceId) .ContinueWith(t => version = EventSourceVersion.FromCombined(t.Result)) .Wait(); return(version); }
void PopulateGenericEventParametersFromDto(IEvent @event, EventDto eventDto) { @event.Id = eventDto.Id; @event.Name = eventDto.Name; @event.Occured = eventDto.Occurred; @event.Origin = eventDto.Origin; @event.EventSource = eventDto.EventSource; @event.EventSourceId = eventDto.EventSourceId; @event.CommandContext = eventDto.CommandContext; @event.CausedBy = eventDto.CausedBy; @event.Version = EventSourceVersion.FromCombined(eventDto.Version); }
public EventSourceVersion GetLastCommittedVersion(EventSource eventSource, Guid eventSourceId) { var query = Query.EQ("EventSourceId", eventSourceId); var sort = SortBy.Descending(Version); var @event = _collection.FindAs <BsonDocument>(query).SetSortOrder(sort).FirstOrDefault(); if (@event == null) { return(EventSourceVersion.Zero); } return(EventSourceVersion.FromCombined(@event[Version].AsDouble)); }
/// <inheritdoc/> public EventSourceVersion GetFor(IApplicationResourceIdentifier eventSource, EventSourceId eventSourceId) { var fileName = GetFileNameFor(eventSource, eventSourceId); var version = EventSourceVersion.Zero; if (_files.Exists(_path, fileName)) { var versionAsString = _files.ReadString(_path, fileName); version = EventSourceVersion.FromCombined(double.Parse(versionAsString)); } else { version = _eventStore.GetVersionFor(eventSource, eventSourceId); } return(version); }
/// <inheritdoc/> public EventSourceVersion GetVersionFor(IApplicationResourceIdentifier eventSource, EventSourceId eventSourceId) { var eventSourceIdentifier = _applicationResourceIdentifierConverter.AsString(eventSource); var eventPath = GetPathFor(eventSourceIdentifier, eventSourceId); var first = _files.GetFilesIn(eventPath, "*.event").OrderByDescending(f => f).FirstOrDefault(); if (first == null) { return(EventSourceVersion.Zero); } var versionAsString = Path.GetFileNameWithoutExtension(first); var versionAsDouble = double.Parse(versionAsString, CultureInfo.InvariantCulture); return(EventSourceVersion.FromCombined(versionAsDouble)); }
/// <inheritdoc/> public EventSourceVersion GetFor(IApplicationResourceIdentifier eventSource, EventSourceId eventSourceId) { var key = GetKeyFor(eventSource, eventSourceId); var version = EventSourceVersion.Zero; var value = _database.StringGet(key); if (value.IsNull) { version = _eventStore.GetVersionFor(eventSource, eventSourceId); SetFor(eventSource, eventSourceId, version); } else { version = EventSourceVersion.FromCombined(double.Parse(value.ToString())); } return(version); }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { return(EventSourceVersion.FromCombined((double)reader.Value)); }
/// <summary> /// Initializes a new instance of <see cref="CommittedEventStreamCoordinator"/> /// </summary> /// <param name="serializer"><see cref="ISerializer"/> to use for deserializing <see cref="IEvent">events</see></param> /// <param name="applicationResourceIdentifierConverter"><see cref="IApplicationResourceIdentifierConverter"/> used for converting resource identifiers</param> /// <param name="applicationResourceResolver"><see cref="IApplicationResourceResolver"/> used for resolving types from <see cref="IApplicationResourceIdentifier"/></param> /// <param name="connectionStringProvider"><see cref="ICanProvideConnectionStringToReceiver">Provider</see> of connection string</param> public CommittedEventStreamReceiver( ISerializer serializer, IApplicationResourceIdentifierConverter applicationResourceIdentifierConverter, IApplicationResourceResolver applicationResourceResolver, ICanProvideConnectionStringToReceiver connectionStringProvider) { _serializer = serializer; var factory = new ConnectionFactory(); factory.Uri = connectionStringProvider(); var connection = factory.CreateConnection(); var exchangeName = "Events"; var routingKey = "RoutingKey"; var queueName = "Events"; var model = connection.CreateModel(); model.ExchangeDeclare(exchangeName, ExchangeType.Direct); model.QueueDeclare(queueName, false, false, false, null); model.QueueBind(queueName, exchangeName, routingKey, null); var consumer = new EventingBasicConsumer(model); consumer.Received += (ch, ea) => { try { var dynamicEventsAndEnvelopes = new List <dynamic>(); var json = System.Text.Encoding.UTF8.GetString(ea.Body); _serializer.FromJson(dynamicEventsAndEnvelopes, json); var eventsAndEnvelopes = new List <EventAndEnvelope>(); foreach (var dynamicEventAndEnvelope in dynamicEventsAndEnvelopes) { var env = dynamicEventAndEnvelope.Envelope; var correlationId = (TransactionCorrelationId)Guid.Parse(env.CorrelationId.ToString()); var eventId = (EventId)Guid.Parse(env.EventId.ToString()); var sequenceNumber = (EventSequenceNumber)long.Parse(env.SequenceNumber.ToString()); var sequenceNumberForEventType = (EventSequenceNumber)long.Parse(env.SequenceNumberForEventType.ToString()); var generation = (EventGeneration)long.Parse(env.Generation.ToString()); var @event = applicationResourceIdentifierConverter.FromString(env.Event.ToString()); var eventSourceId = (EventSourceId)Guid.Parse(env.EventSourceId.ToString()); var eventSource = applicationResourceIdentifierConverter.FromString(env.EventSource.ToString()); var version = (EventSourceVersion)EventSourceVersion.FromCombined(double.Parse(env.Version.ToString())); var causedBy = (CausedBy)env.CausedBy.ToString(); var occurred = DateTimeOffset.Parse(env.Occurred.ToString()); var envelope = new EventEnvelope( correlationId, eventId, sequenceNumber, sequenceNumberForEventType, generation, @event, eventSourceId, eventSource, version, causedBy, occurred ); var eventType = applicationResourceResolver.Resolve(@event); var eventInstance = Activator.CreateInstance(eventType, new object[] { eventSourceId }) as IEvent; var e = dynamicEventAndEnvelope.Event.ToString(); _serializer.FromJson(eventInstance, e); eventsAndEnvelopes.Add(new EventAndEnvelope(envelope, eventInstance)); } var stream = new CommittedEventStream(eventsAndEnvelopes.First().Envelope.EventSourceId, eventsAndEnvelopes); Received(stream); model.BasicAck(ea.DeliveryTag, false); } catch { } finally { } }; Task.Run(() => model.BasicConsume(queueName, false, consumer)); }
public BoundedContextListener( KafkaConnectionString connectionString, ListenerConfiguration configuration, IEventConverter eventConverter, IUncommittedEventStreamCoordinator uncommittedEventStreamCoordinator, ISerializer serializer, ILogger logger, IApplicationResourceIdentifierConverter applicationResourceIdentifierConverter, IImplementationsOf <IEvent> eventTypes, IEventStore eventStore, IEventEnvelopes eventEnvelopes, IEventSequenceNumbers eventSequenceNumbers, IEventSourceVersions eventSourceVersions, ICommittedEventStreamBridge committedEventStreamBridge) { _serializer = serializer; _eventConverter = eventConverter; _uncommittedEventStreamCoordinator = uncommittedEventStreamCoordinator; _logger = logger; _applicationResourceIdentifierConverter = applicationResourceIdentifierConverter; _eventTypes = eventTypes; _eventSequenceNumbers = eventSequenceNumbers; _eventStore = eventStore; _eventEnvelopes = eventEnvelopes; _eventSourceVersions = eventSourceVersions; _committedEventStreamBridge = committedEventStreamBridge; logger.Information($"Listening on topic '{configuration.Topic}' from '{connectionString}'"); var config = new Dictionary <string, object> { { "bootstrap.servers", connectionString }, { "group.id", "simple-consumer" }, { "enable.auto.commit", true }, { "auto.commit.interval.ms", 1000 }, { "default.topic.config", new Dictionary <string, object>() { { "auto.offset.reset", "smallest" } } } }; _consumer = new Consumer <Ignore, string>(config, null, new StringDeserializer(Encoding.UTF8)); _consumer.Assign(new [] { new TopicPartition(configuration.Topic, 0) }); _consumer.OnMessage += (_, msg) => { try { logger.Trace($"Message received '{msg.Value}'"); var raw = _serializer.FromJson <dynamic[]>(msg.Value); foreach (var rawContentAndEnvelope in raw) { var eventSourceId = (EventSourceId)Guid.Parse(rawContentAndEnvelope.Content.EventSourceId.ToString()); var eventIdentifier = _applicationResourceIdentifierConverter.FromString(rawContentAndEnvelope.Envelope.Event.ToString()); var version = EventSourceVersion.FromCombined((double)rawContentAndEnvelope.Envelope.Version); var correlationId = (TransactionCorrelationId)Guid.Parse(rawContentAndEnvelope.Envelope.CorrelationId.ToString()); CorrelationId = correlationId; _logger.Trace($"Received event of with resource name '{eventIdentifier.Resource.Name}' from '{eventSourceId}' with version '{version}' in correlation '{correlationId}'"); var eventType = _eventTypes.SingleOrDefault(et => et.Name == eventIdentifier.Resource.Name); if (eventType != null) { _logger.Trace("Matching Event Type : " + eventType.AssemblyQualifiedName); var @event = Activator.CreateInstance(eventType, eventSourceId) as IEvent; _serializer.FromJson(@event, rawContentAndEnvelope.Content.ToString()); var eventSource = new ExternalSource(eventSourceId); var uncommittedEventStream = new UncommittedEventStream(eventSource); uncommittedEventStream.Append(@event, version); _logger.Information($"Committing uncommitted event stream with correlationId '{correlationId}'"); var envelopes = _eventEnvelopes.CreateFrom(eventSource, uncommittedEventStream.EventsAndVersion); var envelopesAsArray = envelopes.ToArray(); var eventsAsArray = uncommittedEventStream.ToArray(); _logger.Trace("Create an array of events and envelopes"); var eventsAndEnvelopes = new List <EventAndEnvelope>(); for (var eventIndex = 0; eventIndex < eventsAsArray.Length; eventIndex++) { var envelope = envelopesAsArray[eventIndex]; var currentEvent = eventsAsArray[eventIndex]; eventsAndEnvelopes.Add(new EventAndEnvelope( envelope .WithTransactionCorrelationId(correlationId) .WithSequenceNumber(_eventSequenceNumbers.Next()) .WithSequenceNumberForEventType(_eventSequenceNumbers.NextForType(envelope.Event)), currentEvent )); } _logger.Trace("Committing events to event store"); _eventStore.Commit(eventsAndEnvelopes); _logger.Trace($"Set event source versions for the event source '{envelopesAsArray[0].EventSource}' with id '{envelopesAsArray[0].EventSourceId}'"); _eventSourceVersions.SetFor(envelopesAsArray[0].EventSource, envelopesAsArray[0].EventSourceId, envelopesAsArray[envelopesAsArray.Length - 1].Version); _logger.Trace("Create a committed event stream"); var committedEventStream = new CommittedEventStream(uncommittedEventStream.EventSourceId, eventsAndEnvelopes); _committedEventStreamBridge.Send(committedEventStream); CorrelationId = Guid.Empty; } } } catch (Exception ex) { _logger.Error(ex, "Error during receiving"); } }; }
void Received(Topic topic, string eventAsJson) { try { _logger.Trace($"Message received 'eventAsJson'"); dynamic raw = JsonConvert.DeserializeObject(eventAsJson); foreach (var rawContentAndEnvelope in raw) { var eventSourceId = (EventSourceId)Guid.Parse(rawContentAndEnvelope.Envelope.EventSourceId.ToString()); var eventIdentifier = _applicationResourceIdentifierConverter.FromString(rawContentAndEnvelope.Envelope.Event.ToString()); var version = EventSourceVersion.FromCombined((double)rawContentAndEnvelope.Envelope.Version); var correlationId = (TransactionCorrelationId)Guid.Parse(rawContentAndEnvelope.Envelope.CorrelationId.ToString()); var eventSource = new ExternalSource(eventSourceId); CorrelationId = correlationId; _logger.Trace($"Received event of with resource name '{eventIdentifier.Resource.Name}' from '{eventSourceId}' with version '{version}' in correlation '{correlationId}'"); var eventType = _eventTypes.SingleOrDefault(et => et.Name == eventIdentifier.Resource.Name); if (eventType != null) { _logger.Trace("Matching Event Type : " + eventType.AssemblyQualifiedName); var @event = GetEventFrom(rawContentAndEnvelope, eventSourceId, eventType); var uncommittedEventStream = new UncommittedEventStream(eventSource); uncommittedEventStream.Append(@event, version); _logger.Information($"Committing uncommitted event stream with correlationId '{correlationId}'"); var envelopes = _eventEnvelopes.CreateFrom(eventSource, uncommittedEventStream.EventsAndVersion); var envelopesAsArray = envelopes.ToArray(); var eventsAsArray = uncommittedEventStream.ToArray(); _logger.Trace("Create an array of events and envelopes"); var eventsAndEnvelopes = new List <EventAndEnvelope>(); for (var eventIndex = 0; eventIndex < eventsAsArray.Length; eventIndex++) { var envelope = envelopesAsArray[eventIndex]; var currentEvent = eventsAsArray[eventIndex]; eventsAndEnvelopes.Add(new EventAndEnvelope( envelope .WithTransactionCorrelationId(correlationId) .WithSequenceNumber(_eventSequenceNumbers.Next()) .WithSequenceNumberForEventType(_eventSequenceNumbers.NextForType(envelope.Event)), currentEvent )); } _logger.Trace("Committing events to event store"); _eventStore.Commit(eventsAndEnvelopes); _logger.Trace($"Set event source versions for the event source '{envelopesAsArray[0].EventSource}' with id '{envelopesAsArray[0].EventSourceId}'"); _eventSourceVersions.SetFor(envelopesAsArray[0].EventSource, envelopesAsArray[0].EventSourceId, envelopesAsArray[envelopesAsArray.Length - 1].Version); _logger.Trace("Create a committed event stream"); var committedEventStream = new CommittedEventStream(uncommittedEventStream.EventSourceId, eventsAndEnvelopes); _committedEventStreamBridge.Send(committedEventStream); CorrelationId = Guid.Empty; } } } catch (Exception ex) { _logger.Error(ex, "Error during receiving"); } }
/// <summary> /// Get a value from a <see cref="DynamicTableEntity"/> by giving a property info representing the property to get value for /// </summary> /// <param name="entity"><see cref="DynamicTableEntity">Entity</see> to get from</param> /// <param name="propertyInfo"><see cref="PropertyInfo"/> for the property</param> /// <returns>Value of the property from the <see cref="DynamicTableEntity">entity</see></returns> public static object GetValue(DynamicTableEntity entity, PropertyInfo propertyInfo) { var valueType = propertyInfo.PropertyType; var entityProperty = entity.Properties[propertyInfo.Name]; var concept = valueType.IsConcept(); object value = null; if (concept) { valueType = valueType.GetConceptValueType(); } if (valueType == typeof(EventSourceVersion)) { value = EventSourceVersion.FromCombined(entityProperty.DoubleValue.Value); } if (valueType == typeof(Guid)) { value = entityProperty.GuidValue.Value; } if (valueType == typeof(int)) { value = entityProperty.Int32Value.Value; } if (valueType == typeof(long)) { value = entityProperty.Int64Value.Value; } if (valueType == typeof(string)) { value = entityProperty.StringValue; } if (valueType == typeof(DateTime)) { value = entityProperty.DateTime.Value; } if (valueType == typeof(DateTimeOffset)) { value = entityProperty.DateTimeOffsetValue.Value; } if (valueType == typeof(bool)) { value = entityProperty.BooleanValue.Value; } if (valueType == typeof(double)) { value = entityProperty.DoubleValue.Value; } if (valueType == typeof(float)) { value = (float)entityProperty.DoubleValue.Value; } if (concept) { return(ConceptFactory.CreateConceptInstance(propertyInfo.PropertyType, value)); } return(value); }