public EventData Create(EventMetadata em, IEvent ev, Func <Type, string> evName) { var str = JsonConvert.SerializeObject(ev); var evData = Encoding.UTF8.GetBytes(str); var strMeta = JsonConvert.SerializeObject(em); var evMeta = Encoding.UTF8.GetBytes(strMeta); var evTypeName = evName(ev.GetType()); return(new EventData(Uuid.FromGuid(ev.Id), evTypeName, evData, evMeta)); }
private async Task SendAcceptAsync(Guid serverId, EndPoint serverHttpEndPoint, Guid leaderId, EndPoint leaderHttp, int view, DateTime deadline) { var request = new AcceptRequest { ServerId = Uuid.FromGuid(serverId).ToDto(), ServerHttp = new GossipEndPoint(serverHttpEndPoint.GetHost(), (uint)serverHttpEndPoint.GetPort()), LeaderId = Uuid.FromGuid(leaderId).ToDto(), LeaderHttp = new GossipEndPoint(leaderHttp.GetHost(), (uint)leaderHttp.GetPort()), View = view }; await _electionsClient.AcceptAsync(request); _electionsClient.Accept(request, deadline: deadline.ToUniversalTime()); }
private async Task SendAcceptAsync(Guid serverId, IPEndPoint serverInternalHttp, Guid leaderId, IPEndPoint leaderInternalHttp, int view, DateTime deadline) { var request = new AcceptRequest { ServerId = Uuid.FromGuid(serverId).ToDto(), ServerInternalHttp = new EndPoint(serverInternalHttp.Address.ToString(), (uint)serverInternalHttp.Port), LeaderId = Uuid.FromGuid(leaderId).ToDto(), LeaderInternalHttp = new EndPoint(leaderInternalHttp.Address.ToString(), (uint)leaderInternalHttp.Port), View = view }; await _electionsClient.AcceptAsync(request); _electionsClient.Accept(request, deadline: deadline.ToUniversalTime()); }
private async Task SendPrepareOkAsync(int view, Guid serverId, IPEndPoint serverInternalHttp, int epochNumber, long epochPosition, Guid epochId, long lastCommitPosition, long writerCheckpoint, long chaserCheckpoint, int nodePriority, DateTime deadline) { var request = new PrepareOkRequest { View = view, ServerId = Uuid.FromGuid(serverId).ToDto(), ServerInternalHttp = new EndPoint(serverInternalHttp.Address.ToString(), (uint)serverInternalHttp.Port), EpochNumber = epochNumber, EpochPosition = epochPosition, EpochId = Uuid.FromGuid(epochId).ToDto(), LastCommitPosition = lastCommitPosition, WriterCheckpoint = writerCheckpoint, ChaserCheckpoint = chaserCheckpoint, NodePriority = nodePriority }; await _electionsClient.PrepareOkAsync(request, deadline : deadline.ToUniversalTime()); }
public static EventStore.Cluster.ClusterInfo ToGrpcClusterInfo(ClusterInfo cluster) { var members = Array.ConvertAll(cluster.Members, x => new EventStore.Cluster.MemberInfo { InstanceId = Uuid.FromGuid(x.InstanceId).ToDto(), TimeStamp = x.TimeStamp.ToTicksSinceEpoch(), State = (EventStore.Cluster.MemberInfo.Types.VNodeState)x.State, IsAlive = x.IsAlive, HttpEndPoint = new EventStore.Cluster.EndPoint( x.HttpEndPoint.GetHost(), (uint)x.HttpEndPoint.GetPort()), InternalTcp = x.InternalSecureTcpEndPoint != null ? new EventStore.Cluster.EndPoint( x.InternalSecureTcpEndPoint.GetHost(), (uint)x.InternalSecureTcpEndPoint.GetPort()) : new EventStore.Cluster.EndPoint( x.InternalTcpEndPoint.GetHost(), (uint)x.InternalTcpEndPoint.GetPort()), InternalTcpUsesTls = x.InternalSecureTcpEndPoint != null, ExternalTcp = x.ExternalSecureTcpEndPoint != null ? new EventStore.Cluster.EndPoint( x.ExternalSecureTcpEndPoint.GetHost(), (uint)x.ExternalSecureTcpEndPoint.GetPort()) : x.ExternalTcpEndPoint != null ? new EventStore.Cluster.EndPoint( x.ExternalTcpEndPoint.GetHost(), (uint)x.ExternalTcpEndPoint.GetPort()) : null, ExternalTcpUsesTls = x.ExternalSecureTcpEndPoint != null, LastCommitPosition = x.LastCommitPosition, WriterCheckpoint = x.WriterCheckpoint, ChaserCheckpoint = x.ChaserCheckpoint, EpochPosition = x.EpochPosition, EpochNumber = x.EpochNumber, EpochId = Uuid.FromGuid(x.EpochId).ToDto(), NodePriority = x.NodePriority, IsReadOnlyReplica = x.IsReadOnlyReplica, AdvertiseHostToClientAs = x.AdvertiseHostToClientAs ?? "", AdvertiseHttpPortToClientAs = (uint)x.AdvertiseHttpPortToClientAs, AdvertiseTcpPortToClientAs = (uint)x.AdvertiseTcpPortToClientAs }).ToArray(); var info = new EventStore.Cluster.ClusterInfo(); info.Members.AddRange(members); return(info); }
private async Task SendPrepareOkAsync(int view, Guid serverId, EndPoint serverHttpEndPoint, int epochNumber, long epochPosition, Guid epochId, Guid epochLeaderInstanceId, long lastCommitPosition, long writerCheckpoint, long chaserCheckpoint, int nodePriority, ClusterInfo clusterInfo, DateTime deadline) { var request = new PrepareOkRequest { View = view, ServerId = Uuid.FromGuid(serverId).ToDto(), ServerHttp = new GossipEndPoint(serverHttpEndPoint.GetHost(), (uint)serverHttpEndPoint.GetPort()), EpochNumber = epochNumber, EpochPosition = epochPosition, EpochId = Uuid.FromGuid(epochId).ToDto(), EpochLeaderInstanceId = Uuid.FromGuid(epochLeaderInstanceId).ToDto(), LastCommitPosition = lastCommitPosition, WriterCheckpoint = writerCheckpoint, ChaserCheckpoint = chaserCheckpoint, NodePriority = nodePriority, ClusterInfo = ClusterInfo.ToGrpcClusterInfo(clusterInfo) }; await _electionsClient.PrepareOkAsync(request, deadline : deadline.ToUniversalTime()); }
private async Task AppendEvent(IPEndPoint endpoint, string stream, long expectedVersion) { using var channel = GrpcChannel.ForAddress(new Uri($"https://{endpoint}"), new GrpcChannelOptions { HttpClient = _httpClient }); var streamClient = new Streams.StreamsClient(channel); using var call = streamClient.Append(GetCallOptions()); var optionsAppendReq = new AppendReq { Options = new() { StreamIdentifier = new() { StreamName = ByteString.CopyFromUtf8(stream) }, } }; switch (expectedVersion) { case ExpectedVersion.Any: optionsAppendReq.Options.Any = new Empty(); break; case ExpectedVersion.NoStream: optionsAppendReq.Options.NoStream = new Empty(); break; default: optionsAppendReq.Options.Revision = (ulong)expectedVersion; break; } await call.RequestStream.WriteAsync(optionsAppendReq); await call.RequestStream.WriteAsync(new AppendReq { ProposedMessage = new() { Id = new() { String = Uuid.FromGuid(Guid.NewGuid()).ToString() }, Data = ByteString.Empty, Metadata = {
private async Task SendProposalAsync(Guid serverId, EndPoint serverHttpEndPoint, Guid leaderId, EndPoint leaderHttp, int view, int epochNumber, long epochPosition, Guid epochId, long lastCommitPosition, long writerCheckpoint, long chaserCheckpoint, int nodePriority, DateTime deadline) { var request = new ProposalRequest { ServerId = Uuid.FromGuid(serverId).ToDto(), ServerHttp = new GossipEndPoint(serverHttpEndPoint.GetHost(), (uint)serverHttpEndPoint.GetPort()), LeaderId = Uuid.FromGuid(leaderId).ToDto(), LeaderHttp = new GossipEndPoint(leaderHttp.GetHost(), (uint)leaderHttp.GetPort()), View = view, EpochNumber = epochNumber, EpochPosition = epochPosition, EpochId = Uuid.FromGuid(epochId).ToDto(), LastCommitPosition = lastCommitPosition, WriterCheckpoint = writerCheckpoint, ChaserCheckpoint = chaserCheckpoint, NodePriority = nodePriority }; await _electionsClient.ProposalAsync(request, deadline : deadline.ToUniversalTime()); }
public void Equality() { var sut = Uuid.NewUuid(); Assert.AreEqual(Uuid.FromGuid(sut.ToGuid()), sut); }
public void EqualityOperator() { var sut = Uuid.NewUuid(); Assert.True(Uuid.FromGuid(sut.ToGuid()) == sut); }
private async Task WriteFlood(CommandProcessorContext context, WriteFloodStats stats, long interval, int clientsCnt, long requestsCnt, int streamsCnt, int size, int batchSize, string streamNamePrefix, RequestMonitor monitor) { context.IsAsync(); long last = 0; long currentInterval = 0; string[] streams = Enumerable.Range(0, streamsCnt).Select(x => string.IsNullOrWhiteSpace(streamNamePrefix) ? Guid.NewGuid().ToString() : $"{streamNamePrefix}-{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._grpcTestClient.CreateGrpcClient(); clientTasks.Add(RunClient(client, count)); } async Task RunClient(EventStoreClient 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(Uuid.FromGuid(Guid.NewGuid()), "TakeSomeSpaceEvent", UTF8NoBom.GetBytes( "{ \"DATA\" : \"" + new string('*', size) + "\"}"), UTF8NoBom.GetBytes( "{ \"METADATA\" : \"" + new string('$', 100) + "\"}")); } var corrid = Guid.NewGuid(); monitor.StartOperation(corrid); pending.Add(client.AppendToStreamAsync(streams[rnd.Next(streamsCnt)], StreamState.Any, events) .ContinueWith(t => { if (t.IsCompletedSuccessfully) { Interlocked.Increment(ref stats.Succ); } else { if (Interlocked.Increment(ref stats.Fail) % 1000 == 0) { Console.Write('#'); } } var localAll = Interlocked.Add(ref stats.All, batchSize); if (localAll - currentInterval > interval) { var localInterval = Interlocked.Exchange(ref currentInterval, localAll); stats.Elapsed = sw2.Elapsed; stats.Rate = 1000.0 * (localAll - localInterval) / stats.Elapsed.TotalMilliseconds; sw2.Restart(); context.Log.Information( "\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); } monitor.EndOperation(corrid); })); 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(); 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(); } }
protected override async Task Given() { var node = GetFollowers()[0]; await Task.WhenAll(node.AdminUserCreated, node.Started); using var channel = GrpcChannel.ForAddress(new Uri($"https://{node.HttpEndPoint}"), new GrpcChannelOptions { HttpClient = new HttpClient(new SocketsHttpHandler { SslOptions = { RemoteCertificateValidationCallback = delegate { return(true); } } }, true) }); var callInvoker = channel.CreateCallInvoker(); using var call = callInvoker.AsyncClientStreamingCall( new Method <AppendReq, AppendResp>(MethodType.ClientStreaming, "event_store.client.streams.Streams", "Append", Marshallers.Create(MessageExtensions.ToByteArray, AppendReq.Parser.ParseFrom), Marshallers.Create(MessageExtensions.ToByteArray, AppendResp.Parser.ParseFrom)), default, new CallOptions(credentials: CallCredentials.FromInterceptor((credentials, metadata) => { metadata.Add("authorization", AuthorizationHeaderValue); return(Task.CompletedTask); }))); await call.RequestStream.WriteAsync(new AppendReq { Options = new AppendReq.Types.Options { NoStream = new Empty(), StreamIdentifier = new StreamIdentifier { StreamName = ByteString.CopyFromUtf8(ProtectedStream) } } }); await call.RequestStream.WriteAsync(new AppendReq { ProposedMessage = new AppendReq.Types.ProposedMessage { Id = new UUID { String = Uuid.FromGuid(Guid.NewGuid()).ToString() }, CustomMetadata = ByteString.Empty, Data = ByteString.Empty, Metadata = { { EventStore.Core.Services.Transport.Grpc.Constants.Metadata.Type, "-" }, { EventStore.Core.Services.Transport.Grpc.Constants.Metadata.ContentType, EventStore.Core.Services.Transport.Grpc.Constants.Metadata.ContentTypes .ApplicationOctetStream } } } }); await call.RequestStream.CompleteAsync(); await call.ResponseHeadersAsync; await call.ResponseAsync; _status = call.GetStatus(); await base.Given(); }
protected override async Task WriteEventsToStoreAsync(IEnumerable <IDomainEventDescriptor> events, CancellationToken cancellationToken) { var aggregateGroup = events.GroupBy(x => GenerateStreamId(x.AggregateType, x.AggregateId)); var eventDataGroups = aggregateGroup.Select(group => { var eventList = group.ToList(); var eventDatas = eventList.Select(x => { byte[] data; byte[] metadata; // serialize data using (var ms = new MemoryStream()) using (var writer = new BsonDataWriter(ms)) { JsonSerializer serializer = new JsonSerializer(); serializer.TypeNameHandling = TypeNameHandling.All; serializer.Serialize(writer, x.Event); data = ms.ToArray(); } // serialize metadata using (var ms = new MemoryStream()) using (var writer = new BsonDataWriter(ms)) { JsonSerializer serializer = new JsonSerializer(); serializer.Serialize(writer, x.Metadata); metadata = ms.ToArray(); } return(new EventData( Uuid.FromGuid(x.EventId), x.EventType, data, metadata, "application/octet-stream" )); }); if (eventList.Any(x => x.IsPublished)) { throw new InvalidOperationException("Cannot publish events that have been published"); } var firstEvent = eventList.First(); return(StreamId: group.Key, EventDatas: eventDatas, ExpectedVersionBeforeAppend: firstEvent.Version - 1L); }); foreach (var group in eventDataGroups) { // expect no stream when there is not positive version if (group.ExpectedVersionBeforeAppend == -1) { await EventStoreClient.AppendToStreamAsync( group.StreamId, StreamState.NoStream, group.EventDatas, cancellationToken : cancellationToken ); } // expect stream when there an unknow version else if (group.ExpectedVersionBeforeAppend == DomainEventDescriptor.UnknowVersion - 1) { await EventStoreClient.AppendToStreamAsync( group.StreamId, StreamState.StreamExists, group.EventDatas, cancellationToken : cancellationToken ); } // expect stream when there is a valid version else { await EventStoreClient.AppendToStreamAsync( group.StreamId, StreamRevision.FromInt64(group.ExpectedVersionBeforeAppend), group.EventDatas, cancellationToken : cancellationToken ); } } }
public Task <AppendEventsResult> AppendEvents( StreamName stream, ExpectedStreamVersion expectedVersion, IReadOnlyCollection <StreamEvent> events, CancellationToken cancellationToken ) { var proposedEvents = events.Select(ToEventData); var resultTask = expectedVersion == ExpectedStreamVersion.NoStream ? _client.AppendToStreamAsync( stream, StreamState.NoStream, proposedEvents, cancellationToken: cancellationToken ) : AnyOrNot( expectedVersion, () => _client.AppendToStreamAsync( stream, StreamState.Any, proposedEvents, cancellationToken: cancellationToken ), () => _client.AppendToStreamAsync( stream, expectedVersion.AsStreamRevision(), proposedEvents, cancellationToken: cancellationToken ) ); return(TryExecute( async() => { var result = await resultTask.NoContext(); return new AppendEventsResult( result.LogPosition.CommitPosition, result.NextExpectedStreamRevision.ToInt64() ); }, stream, () => new ErrorInfo("Unable to appends events to {Stream}", stream), (s, ex) => { EventuousEventSource.Log.UnableToAppendEvents(stream, ex); return new AppendToStreamException(s, ex); } )); EventData ToEventData(StreamEvent streamEvent) { var(eventType, contentType, payload) = _serializer.SerializeEvent(streamEvent.Payload !); return(new EventData( Uuid.FromGuid(streamEvent.Id), eventType, payload, _metaSerializer.Serialize(streamEvent.Metadata), contentType )); } }
EventData TestEvent(Guid id, string type, object data) { return(new EventData( Uuid.FromGuid(id), type, Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(data)) )); }