private async void Append(EventData eventData) { if (_connection != null) { await _connection.AppendToStreamAsync(_configurationSettings.Stream, ExpectedVersion.Any, eventData); } }
public void Deliver(Message msg) { //var actorRef = GetRef(msg.ActorId); //actorRef.Enqueue(msg, _connection, _queue); var evnt = new EventData(msg.Id, "type", true, null, null); _connection.AppendToStream(_queue, ExpectedVersion.Any, evnt); }
public void Create(Guid eventId, string eventType, string streamName, object message) { var byteData = Encoding.UTF8.GetBytes(_serializer.Serialize(message)); var e = new EventData(eventId, eventType, true, byteData, null); _connection.AppendToStreamAsync(streamName, ExpectedVersion.Any, e).Wait(); }
private async void Append(EventData eventData) { if (_connection != null) { await _connection.AppendToStreamAsync(StreamName, ExpectedVersion.Any, eventData); } }
private static void Main(string[] args) { var connection = EventStoreConnection.Create(new IPEndPoint(IPAddress.Loopback, 1113)); // Don't forget to tell the connection to connect! connection.ConnectAsync().Wait(); var myEvent = new EventData(Guid.NewGuid(), "helloWorldEvent", false, Encoding.UTF8.GetBytes("hello world data"), Encoding.UTF8.GetBytes("hello world metadata")); connection.AppendToStreamAsync("helloWorld-stream", ExpectedVersion.Any, myEvent).Wait(); var streamEvents = connection.ReadStreamEventsForwardAsync("helloWorld-stream", 0, 1, false).Result; var returnedEvent = streamEvents.Events[0].Event; Console.WriteLine("Read event with data: {0}, metadata: {1}", Encoding.UTF8.GetString(returnedEvent.Data), Encoding.UTF8.GetString(returnedEvent.Metadata)); }
public static EventData ToEventData(this object data, IDictionary<string, object> metadata, Guid? eventId = null) { eventId = eventId ?? Guid.NewGuid(); byte[] dataBytes = data.Serialize(); byte[] metadatBytes = metadata.Serialize(data); string typeName = data.GetType().FullName; var eventData = new EventData(eventId.Value, typeName, true, dataBytes, metadatBytes); return eventData; }
private static void AppendToStream(IEventStoreConnection connection) { byte[] data = Encoding.UTF8.GetBytes("event data"); byte[] metadata = Encoding.UTF8.GetBytes("event metadata"); EventData eventData = new EventData(Guid.NewGuid(), "testEvent", false, data, metadata); connection.AppendToStreamAsync("test-stream", ExpectedVersion.Any, eventData).Wait(); }
static void Main(string[] args) { IEventStoreConnection connection = EventStoreConnection.Create(new IPEndPoint(IPAddress.Loopback, 1113)); connection.ConnectAsync(); var aggregateId = Guid.NewGuid(); List<IEvent> eventsToRun = new List<IEvent>(); //Commands //Domain Logic/Model //Events eventsToRun.Add(new AccountCreatedEvent(aggregateId, "Greg Chavez")); eventsToRun.Add(new FundsDespoitedEvent(aggregateId, 150)); eventsToRun.Add(new FundsDespoitedEvent(aggregateId, 100)); eventsToRun.Add(new FundsWithdrawedEvent(aggregateId, 60)); eventsToRun.Add(new FundsWithdrawedEvent(aggregateId, 94)); eventsToRun.Add(new FundsDespoitedEvent(aggregateId, 4)); //100 foreach (var item in eventsToRun) { var jsonString = JsonConvert.SerializeObject(item, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.None }); var jsonPayload = Encoding.UTF8.GetBytes(jsonString); var eventStoreDataType = new EventData(Guid.NewGuid(), item.GetType().Name, true, jsonPayload, null); connection.AppendToStreamAsync(StreamId(aggregateId), ExpectedVersion.Any, eventStoreDataType); } var results = Task.Run(() => connection.ReadStreamEventsForwardAsync(StreamId(aggregateId), StreamPosition.Start, 999, false)); Task.WaitAll(); var resultsData = results.Result; var bankState = new BankAccount(); foreach (var evnt in resultsData.Events) { var esJsonData = Encoding.UTF8.GetString(evnt.Event.Data); if (evnt.Event.EventType == "AccountCreatedEvent") { var objState = JsonConvert.DeserializeObject<AccountCreatedEvent>(esJsonData); bankState.Apply(objState); } else if (evnt.Event.EventType == "FundsDespoitedEvent") { var objState = JsonConvert.DeserializeObject<FundsDespoitedEvent>(esJsonData); bankState.Apply(objState); } else { var objState = JsonConvert.DeserializeObject<FundsWithdrawedEvent>(esJsonData); bankState.Apply(objState); } } Console.ReadLine(); }
public void SetUp() { this.eventData = new EventDataConverter ().ToEventData ( new AnyEvent (new AnyEventHeader { EventId = expectedEventId }, new AnyEventBody { EventData = JsonConvert.SerializeObject (new SimpleEvent { Name = "A simple event" }), EventType = typeof(SimpleEvent) })); }
private void Publish(string clientResponseStream, Event response) { var json = Json.To(response); var ev = new EventData(Guid.NewGuid(), response.GetType().Name, true, Encoding.UTF8.GetBytes(json), new byte[0]); _connection.AppendToStreamAsync(clientResponseStream, ExpectedVersion.Any, new[] {ev}, _credentials); }
public void Save(Type aggregateRootType, IEvent <TAuthenticationToken> @event) { EventData eventData = EventBuilder.CreateFrameworkEvent(@event); string streamName = string.Format(CqrsEventStoreStreamNamePattern, aggregateRootType.FullName, @event.Id); using (EventStoreTransaction transaction = EventStoreConnection.StartTransactionAsync(streamName, ExpectedVersion.Any).Result) { WriteResult saveResult = EventStoreConnection.AppendToStreamAsync(streamName, ExpectedVersion.Any, new[] { eventData }).Result; WriteResult commitResult = transaction.CommitAsync().Result; } }
public static ES.EventData[] ToESEventData(this EventData[] events) { Ensure.NotNull(events, nameof(events)); var result = new ES.EventData[events.Length]; for (int i = 0; i < events.Length; i++) { result[i] = events[i].ToESEventData(); } return(result); }
public static bool Equal(EventData[] expected, RecordedEvent[] actual) { if (expected.Length != actual.Length) return false; for (var i = 0; i < expected.Length; i++) { if (!Equal(expected[i], actual[i])) return false; } return true; }
protected override void Given() { _serverEndPoint = new IPEndPoint(IPAddress.Loopback, PortsHelper.GetAvailablePort(IPAddress.Loopback)); _url = _HttpEndPoint.ToHttpUrl("/stats/tcp"); var settings = ConnectionSettings.Create(); _connection = EventStoreConnection.Create(settings, _node.TcpEndPoint); _connection.ConnectAsync().Wait(); var testEvent = new EventData(Guid.NewGuid(),"TestEvent",true,Encoding.ASCII.GetBytes("{'Test' : 'OneTwoThree'}"),null); _connection.AppendToStreamAsync("tests",ExpectedVersion.Any,testEvent).Wait(); _portableServer = new PortableServer(_serverEndPoint); _portableServer.SetUp(); }
public static bool Equal(EventData expected, RecordedEvent actual) { if (expected.EventId != actual.EventId) return false; if (expected.Type != actual.EventType) return false; var expectedDataString = Helper.UTF8NoBom.GetString(expected.Data ?? new byte[0]); var expectedMetadataString = Helper.UTF8NoBom.GetString(expected.Metadata ?? new byte[0]); var actualDataString = Helper.UTF8NoBom.GetString(actual.Data ?? new byte[0]); var actualMetadataDataString = Helper.UTF8NoBom.GetString(actual.Metadata ?? new byte[0]); return expectedDataString == actualDataString && expectedMetadataString == actualMetadataDataString; }
public void Send(EventData message) { var transaction = Transaction.Current; var transactionId = transaction.TransactionInformation.LocalIdentifier; IList<EventData> messages; if (!OutstandingOperations.TryGetValue(transactionId, out messages)) { messages = new List<EventData>(); transaction.TransactionCompleted += CommitTransaction; OutstandingOperations.Add(transactionId, messages); } messages.Add(message); }
public override void SetUp() { base.SetUp(); _node = new MiniNode(PathName); _node.Start(); _firstEvent = TestEvent.NewTestEvent(); _connection = EventStoreConnection.Create(); _connection.Connect(_node.TcpEndPoint); _connection.AppendToStream("test-stream", ExpectedVersion.NoStream, _firstEvent, TestEvent.NewTestEvent(), TestEvent.NewTestEvent()); using (var transaction = _connection.StartTransaction("test-stream", 3)) { transaction.Commit(); } }
private void btnUpdateClient_Click(object sender, EventArgs e) { // Create instance of domain object var client = new Client() { Name = "Roger Bloggs" }; EventData eventData; // Serialize domain object to Event Data using (var stream = new MemoryStream()) { new DataContractJsonSerializer(typeof(Client)).WriteObject(stream, client); eventData = new EventData(Guid.NewGuid(), "Client Changed Name", true, stream.ToArray(), null); } // Write Event to event stream this.eventStoreConnection.AppendToStream("ClientStream", ExpectedVersion.Any, eventData); MessageBox.Show("Client updated"); }
public override void SetUp() { base.SetUp(); _node = new MiniNode(PathName); _node.Start(); _firstEvent = TestEvent.NewTestEvent(); _connection = BuildConnection(_node); _connection.ConnectAsync().Wait(); Assert.AreEqual(2, _connection.AppendToStreamAsync("test-stream", ExpectedVersion.NoStream, _firstEvent, TestEvent.NewTestEvent(), TestEvent.NewTestEvent()).Result.NextExpectedVersion); using (var transaction = _connection.StartTransactionAsync("test-stream", 2).Result) { Assert.AreEqual(2, transaction.CommitAsync().Result.NextExpectedVersion); } }
public override void SetUp() { base.SetUp(); _node = new MiniNode(PathName); _node.Start(); _firstEvent = TestEvent.NewTestEvent(); _connection = TestConnection.Create(_node.TcpEndPoint); _connection.Connect(); Assert.AreEqual(2, _connection.AppendToStream("test-stream", ExpectedVersion.NoStream, _firstEvent, TestEvent.NewTestEvent(), TestEvent.NewTestEvent()).NextExpectedVersion); using (var transaction = _connection.StartTransaction("test-stream", 2)) { Assert.AreEqual(2, transaction.Commit().NextExpectedVersion); } }
public EventData ToEventData(IEvent eventToConvert) { if (eventToConvert == null) { throw new ArgumentNullException ("eventToConvert"); } if (eventToConvert.Header == null) { throw new ArgumentException ("Event header is required"); } if (eventToConvert.Body == null) { throw new ArgumentException ("Event body is required"); } var metadata = Encoding.UTF8.GetBytes (JsonConvert.SerializeObject (eventToConvert.Header)); var data = eventToConvert.Body.EventData != null ? Encoding.UTF8.GetBytes (eventToConvert.Body.EventData) : new byte[0]; var eventData = new EventData (Guid.NewGuid (), eventToConvert.Body.EventType != null ? eventToConvert.Body.EventType.AssemblyQualifiedName : null, true, data, metadata); return eventData; }
public void Test() { var settings = PersistentSubscriptionSettings .Create() .StartFromCurrent() .ResolveLinkTos() .Build(); _conn.CreatePersistentSubscriptionAsync(StreamName, GroupName, settings, DefaultData.AdminCredentials) .Wait(); _conn.ConnectToPersistentSubscription(StreamName, GroupName, (subscription, resolvedEvent) => { subscription.Acknowledge(resolvedEvent); if (Interlocked.Increment(ref _eventReceivedCount) == EventWriteCount) { _eventsReceived.Set(); } }, (sub, reason, exception) => Console.WriteLine("Subscription dropped (reason:{0}, exception:{1}).", reason, exception), bufferSize: 10, autoAck: false, userCredentials: DefaultData.AdminCredentials); for (var i = 0; i < EventWriteCount; i++) { var eventData = new EventData(Guid.NewGuid(), "SomeEvent", false, new byte[0], new byte[0]); _conn.AppendToStreamAsync(StreamName, ExpectedVersion.Any, DefaultData.AdminCredentials, eventData); } if (!_eventsReceived.WaitOne(TimeSpan.FromSeconds(5))) { throw new Exception("Timed out waiting for events."); } }
public void idempotency_is_correct_for_explicit_transactions_with_expected_version_any() { const string streamId = "idempotency_is_correct_for_explicit_transactions_with_expected_version_any"; using (var store = TestConnection.Create(_node.TcpEndPoint)) { store.Connect(); var e = new EventData(Guid.NewGuid(), "SomethingHappened", true, Helper.UTF8NoBom.GetBytes("{Value:42}"), null); var transaction1 = store.StartTransaction(streamId, ExpectedVersion.Any); transaction1.Write(new[] {e}); Assert.AreEqual(0, transaction1.Commit().NextExpectedVersion); var transaction2 = store.StartTransaction(streamId, ExpectedVersion.Any); transaction2.Write(new[] {e}); Assert.AreEqual(0, transaction2.Commit().NextExpectedVersion); var res = store.ReadStreamEventsForward(streamId, 0, 100, false); Assert.AreEqual(1, res.Events.Length); Assert.AreEqual(e.EventId, res.Events[0].Event.EventId); } }
void Dead(Message msg) { var evnt = new EventData(msg.Id, "type", true, null, null); _connection.AppendToStream(_deadQueue, ExpectedVersion.Any, evnt); }
public StreamData(string streamName, EventData data) { StreamName = streamName; Event = data; }
public void Enqueue(Message msg, IEventStoreConnection connection, string queue) { //todo var evnt = new EventData(msg.Id, "type", true, null, null); connection.AppendToStream(queue, ExpectedVersion.Any, evnt); }
public Task<WriteResult> SetStreamMetadataAsync(string stream, int expectedMetastreamVersion, byte[] metadata, UserCredentials userCredentials = null) { Ensure.NotNullOrEmpty(stream, "stream"); if (SystemStreams.IsMetastream(stream)) throw new ArgumentException(string.Format("Setting metadata for metastream '{0}' is not supported.", stream), "stream"); var source = new TaskCompletionSource<WriteResult>(); var metaevent = new EventData(Guid.NewGuid(), SystemEventTypes.StreamMetadata, true, metadata ?? Empty.ByteArray, null); EnqueueOperation(new AppendToStreamOperation(_settings.Log, source, _settings.RequireMaster, SystemStreams.MetastreamOf(stream), expectedMetastreamVersion, new[] { metaevent }, userCredentials)); return source.Task; }
public void Test() { var settings = PersistentSubscriptionSettings .Create() .StartFromBeginning() .ResolveLinkTos() .Build(); for (var i = 0; i < EventWriteCount; i++) { var eventData = new EventData(Guid.NewGuid(), "SomeEvent", false, new byte[0], new byte[0]); _conn.AppendToStreamAsync(StreamName + "original", ExpectedVersion.Any, DefaultData.AdminCredentials, eventData).Wait(); } _conn.CreatePersistentSubscriptionAsync(StreamName, GroupName, settings, DefaultData.AdminCredentials) .Wait(); _conn.ConnectToPersistentSubscription(StreamName, GroupName, (subscription, resolvedEvent) => { if (Interlocked.Increment(ref _eventReceivedCount) == EventWriteCount) { _eventsReceived.Set(); } }, (sub, reason, exception) => Console.WriteLine("Subscription dropped (reason:{0}, exception:{1}).", reason, exception), userCredentials: DefaultData.AdminCredentials, autoAck: true); for (var i = 0; i < EventWriteCount; i++) { var eventData = new EventData(Guid.NewGuid(), SystemEventTypes.LinkTo, false, Encoding.UTF8.GetBytes(i + "@" + StreamName + "original"), null); _conn.AppendToStreamAsync(StreamName, ExpectedVersion.Any, DefaultData.AdminCredentials, eventData).Wait(); } if (!_eventsReceived.WaitOne(TimeSpan.FromSeconds(5))) { throw new Exception("Timed out waiting for events."); } }
public TailWriter Then(EventData @event, int expectedVersion) { _store.AppendToStream(_stream, expectedVersion, new[] {@event}); return this; }
protected override async Task SaveAsync(SnapshotMetadata metadata, object snapshot) { var connection = await GetConnection(); var streamName = GetStreamName(metadata.PersistenceId); var data = _serializer.ToBinary(new SelectedSnapshot(metadata, snapshot)); var eventData = new EventData(Guid.NewGuid(), typeof(Snapshot).Name, false, data, new byte[0]); await connection.AppendToStreamAsync(streamName, ExpectedVersion.Any, eventData); }
private async Task WriteFlood(CommandProcessorContext context, WriteFloodStats stats, int clientsCnt, long requestsCnt, int streamsCnt, int size, int batchSize, string streamPrefix, RequestMonitor monitor) { context.IsAsync(); var doneEvent = new ManualResetEventSlim(false); var clients = new List <IEventStoreConnection>(); long last = 0; var streams = Enumerable.Range(0, streamsCnt).Select(x => string.IsNullOrWhiteSpace(streamPrefix) ? Guid.NewGuid().ToString() : $"{streamPrefix}-{x}").ToArray(); context.Log.Information("Writing streams randomly between {first} and {last}", streams.FirstOrDefault(), streams.LastOrDefault()); var start = new TaskCompletionSource(); stats.StartTime = DateTime.UtcNow; var sw2 = new Stopwatch(); var capacity = 2000 / clientsCnt; var clientTasks = new List <Task>(); for (int i = 0; i < clientsCnt; i++) { var count = requestsCnt / clientsCnt + ((i == clientsCnt - 1) ? requestsCnt % clientsCnt : 0); var client = context._clientApiTestClient.CreateConnection(); await client.ConnectAsync(); clientTasks.Add(RunClient(client, count)); } async Task RunClient(IEventStoreConnection client, long count) { var rnd = new Random(); List <Task> pending = new List <Task>(capacity); await start.Task; for (int j = 0; j < count; ++j) { var events = new EventData[batchSize]; for (int q = 0; q < batchSize; q++) { events[q] = new EventData(Guid.NewGuid(), "TakeSomeSpaceEvent", false, Common.Utils.Helper.UTF8NoBom.GetBytes( "{ \"DATA\" : \"" + new string('*', size) + "\"}"), Common.Utils.Helper.UTF8NoBom.GetBytes( "{ \"METADATA\" : \"" + new string('$', 100) + "\"}")); } var corrid = Guid.NewGuid(); monitor.StartOperation(corrid); pending.Add(client.AppendToStreamAsync(streams[rnd.Next(streamsCnt)], ExpectedVersion.Any, events) .ContinueWith(t => { monitor.EndOperation(corrid); if (t.IsCompletedSuccessfully) { Interlocked.Add(ref stats.Succ, batchSize); } else { if (Interlocked.Increment(ref stats.Fail) % 1000 == 0) { Console.Write("#"); } if (t.Exception != null) { var exception = t.Exception.Flatten(); switch (exception.InnerException) { case WrongExpectedVersionException _: Interlocked.Increment(ref stats.WrongExpVersion); break; case StreamDeletedException _: Interlocked.Increment(ref stats.StreamDeleted); break; case OperationTimedOutException _: Interlocked.Increment(ref stats.CommitTimeout); break; } } } Interlocked.Add(ref stats.Succ, batchSize); if (stats.Succ - last > 1000) { last = stats.Succ; Console.Write("."); } var localAll = Interlocked.Add(ref stats.All, batchSize); if (localAll % 100000 == 0) { stats.Elapsed = sw2.Elapsed; stats.Rate = 1000.0 * 100000 / stats.Elapsed.TotalMilliseconds; sw2.Restart(); context.Log.Debug( "\nDONE TOTAL {writes} WRITES IN {elapsed} ({rate:0.0}/s) [S:{success}, F:{failures} (WEV:{wrongExpectedVersion}, P:{prepareTimeout}, C:{commitTimeout}, F:{forwardTimeout}, D:{streamDeleted})].", localAll, stats.Elapsed, stats.Rate, stats.Succ, stats.Fail, stats.WrongExpVersion, stats.PrepTimeout, stats.CommitTimeout, stats.ForwardTimeout, stats.StreamDeleted); stats.WriteStatsToFile(context.StatsLogger); } if (localAll >= requestsCnt) { context.Success(); doneEvent.Set(); } })); if (pending.Count == capacity) { await Task.WhenAny(pending).ConfigureAwait(false); while (pending.Count > 0 && Task.WhenAny(pending).IsCompleted) { pending.RemoveAll(x => x.IsCompleted); if (stats.Succ - last > 1000) { Console.Write("."); last = stats.Succ; } } } } if (pending.Count > 0) { await Task.WhenAll(pending); } } var sw = Stopwatch.StartNew(); sw2.Start(); start.SetResult(); await Task.WhenAll(clientTasks); sw.Stop(); clients.ForEach(client => client.Close()); context.Log.Information( "Completed. Successes: {success}, failures: {failures} (WRONG VERSION: {wrongExpectedVersion}, P: {prepareTimeout}, C: {commitTimeout}, F: {forwardTimeout}, D: {streamDeleted})", stats.Succ, stats.Fail, stats.WrongExpVersion, stats.PrepTimeout, stats.CommitTimeout, stats.ForwardTimeout, stats.StreamDeleted); stats.WriteStatsToFile(context.StatsLogger); var reqPerSec = (stats.All + 0.0) / sw.ElapsedMilliseconds * 1000; context.Log.Information("{requests} requests completed in {elapsed}ms ({rate:0.00} reqs per sec).", stats.All, sw.ElapsedMilliseconds, reqPerSec); PerfUtils.LogData( Keyword, PerfUtils.Row(PerfUtils.Col("clientsCnt", clientsCnt), PerfUtils.Col("requestsCnt", requestsCnt), PerfUtils.Col("ElapsedMilliseconds", sw.ElapsedMilliseconds)), PerfUtils.Row(PerfUtils.Col("successes", stats.Succ), PerfUtils.Col("failures", stats.Fail))); var failuresRate = (int)(100 * stats.Fail / (stats.Fail + stats.Succ)); PerfUtils.LogTeamCityGraphData(string.Format("{0}-{1}-{2}-reqPerSec", Keyword, clientsCnt, requestsCnt), (int)reqPerSec); PerfUtils.LogTeamCityGraphData( string.Format("{0}-{1}-{2}-failureSuccessRate", Keyword, clientsCnt, requestsCnt), failuresRate); PerfUtils.LogTeamCityGraphData( string.Format("{0}-c{1}-r{2}-st{3}-s{4}-reqPerSec", Keyword, clientsCnt, requestsCnt, streamsCnt, size), (int)reqPerSec); PerfUtils.LogTeamCityGraphData( string.Format("{0}-c{1}-r{2}-st{3}-s{4}-failureSuccessRate", Keyword, clientsCnt, requestsCnt, streamsCnt, size), failuresRate); monitor.GetMeasurementDetails(); if (Interlocked.Read(ref stats.Succ) != requestsCnt) { context.Fail(reason: "There were errors or not all requests completed."); } else { context.Success(); } }
public Event Deserialize(EventData serialized_event) { return deserializer_(serialized_event); }