コード例 #1
0
ファイル: PingProcessor.cs プロジェクト: danieldeb/EventStore
        public bool Execute(CommandProcessorContext context, string[] args)
        {
            context.IsAsync();

            context.Client.CreateTcpConnection(
                context,
                connectionEstablished: conn =>
                {
                    var package = new TcpPackage(TcpCommand.Ping, Guid.NewGuid(), null);
                    context.Log.Info("[{0}:{1}]: PING...", context.Client.Options.Ip, context.Client.Options.TcpPort);
                    conn.EnqueueSend(package.AsByteArray());
                },
                handlePackage: (conn, pkg) =>
                {
                    if (pkg.Command != TcpCommand.Pong)
                    {
                        context.Fail(reason: string.Format("Unexpected TCP package: {0}.", pkg.Command));
                        return;
                    }
                    context.Log.Info("[{0}:{1}]: PONG!", context.Client.Options.Ip, context.Client.Options.TcpPort);
                    context.Success();
                    conn.Close();
                },
                connectionClosed: (typedConnection, error) => context.Fail(reason: "Connection was closed prematurely."));
            context.WaitForCompletion();
            return true;
        }
コード例 #2
0
ファイル: PingProcessor.cs プロジェクト: jpierson/EventStore
        public bool Execute(CommandProcessorContext context, string[] args)
        {
            context.IsAsync();

            var package = new TcpPackage(TcpCommand.Ping, Guid.NewGuid(), null);
            context.Log.Info("[{0}:{1}]: PING...", context.Client.Options.Ip, context.Client.Options.TcpPort);

            var connection = context.Client.CreateTcpConnection(
                context,
                (conn, pkg) =>
                {
                    if (pkg.Command != TcpCommand.Pong)
                    {
                        context.Fail(reason: string.Format("Unexpected TCP package: {0}.", pkg.Command));
                        return;
                    }
                    context.Log.Info("[{0}:{1}]: PONG!", context.Client.Options.Ip, context.Client.Options.TcpPort);
                    conn.Close();
                    context.Success();
                },
                null,
                (typedConnection, error) =>
                    {
                        if (error == SocketError.Success)
                            context.Success();
                        else
                            context.Fail();
                    });
            connection.EnqueueSend(package.AsByteArray());
            context.WaitForCompletion();
            return true;
        }
コード例 #3
0
        public void when_handling_trusted_write_on_internal_service()
        {
            ManualResetEvent waiter = new ManualResetEvent(false);
            ClientMessage.WriteEvents publishedWrite = null;
            var evnt = new Event(Guid.NewGuid(), "TestEventType", true, new byte[] { }, new byte[] { });
            var write = new TcpClientMessageDto.WriteEvents(
                Guid.NewGuid().ToString(),
                ExpectedVersion.Any,
                new[] { new TcpClientMessageDto.NewEvent(evnt.EventId.ToByteArray(), evnt.EventType, evnt.IsJson ? 1 : 0, 0, evnt.Data, evnt.Metadata) },
                false);

            var package = new TcpPackage(TcpCommand.WriteEvents, Guid.NewGuid(), write.Serialize());
            var dummyConnection = new DummyTcpConnection();
            var publisher = InMemoryBus.CreateTest();

            publisher.Subscribe(new AdHocHandler<ClientMessage.WriteEvents>(x => {
                publishedWrite = x;
                waiter.Set();
            }));

            var tcpConnectionManager = new TcpConnectionManager(
                Guid.NewGuid().ToString(), TcpServiceType.Internal, new ClientTcpDispatcher(),
                publisher, dummyConnection, publisher, new InternalAuthenticationProvider(new Core.Helpers.IODispatcher(publisher, new NoopEnvelope()), new StubPasswordHashAlgorithm(), 1),
                TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(10), (man, err) => { });

            tcpConnectionManager.ProcessPackage(package);

            if (!waiter.WaitOne(TimeSpan.FromSeconds(5)))
            {
                throw new Exception("Timed out waiting for events.");
            }
            Assert.AreEqual(evnt.EventId, publishedWrite.Events.First().EventId, "Expected the published write to be the event that was sent through the tcp connection manager to be the event {0} but got {1}", evnt.EventId, publishedWrite.Events.First().EventId);
        }
コード例 #4
0
        private void PingFloodWaiting(CommandProcessorContext context, int clientsCnt, long requestsCnt)
        {
            context.IsAsync();

            var clients = new List<TcpTypedConnection<byte[]>>();
            var threads = new List<Thread>();
            var doneEvent = new ManualResetEventSlim(false);
            var clientsDone = 0;
            long all = 0;
            for (int i = 0; i < clientsCnt; i++)
            {
                var autoResetEvent = new AutoResetEvent(false);
                var client = context.Client.CreateTcpConnection(
                    context,
                    (_, __) =>
                    {
                        Interlocked.Increment(ref all);
                        autoResetEvent.Set();
                    },
                    connectionClosed: (conn, err) => context.Fail(reason: "Connection was closed prematurely."));
                clients.Add(client);

                var count = requestsCnt / clientsCnt + ((i == clientsCnt - 1) ? requestsCnt % clientsCnt : 0);
                threads.Add(new Thread(() =>
                {
                    for (int j = 0; j < count; ++j)
                    {
                        var package = new TcpPackage(TcpCommand.Ping, Guid.NewGuid(), null);
                        client.EnqueueSend(package.AsByteArray());
                        autoResetEvent.WaitOne();
                    }
                    if (Interlocked.Increment(ref clientsDone) == clientsCnt)
                    {
                        context.Success();
                        doneEvent.Set();
                    }
                }) { IsBackground = true });
            }

            var sw = Stopwatch.StartNew();
            threads.ForEach(thread => thread.Start());
            doneEvent.Wait();
            sw.Stop();
            clients.ForEach(x => x.Close());

            var reqPerSec = (all + 0.0)/sw.ElapsedMilliseconds*1000;
            context.Log.Info("{0} requests completed in {1}ms ({2:0.00} reqs per sec).", all, sw.ElapsedMilliseconds, reqPerSec);
            PerfUtils.LogData(Keyword,
                              PerfUtils.Row(PerfUtils.Col("clientsCnt", clientsCnt),
                                            PerfUtils.Col("requestsCnt", requestsCnt),
                                            PerfUtils.Col("ElapsedMilliseconds", sw.ElapsedMilliseconds)));
            PerfUtils.LogTeamCityGraphData(string.Format("{0}-{1}-{2}-reqPerSec", Keyword, clientsCnt, requestsCnt), (int) reqPerSec);
            PerfUtils.LogTeamCityGraphData(string.Format("{0}-latency-ms", Keyword), (int) Math.Round(sw.Elapsed.TotalMilliseconds/all));

            if (Interlocked.Read(ref all) == requestsCnt)
                context.Success();
            else
                context.Fail();
        }
コード例 #5
0
 private static Message UnwrapPing(TcpPackage package, IEnvelope envelope)
 {
     var data = new byte[package.Data.Count];
     Buffer.BlockCopy(package.Data.Array, package.Data.Offset, data, 0, package.Data.Count);
     var pongMessage = new TcpMessage.PongMessage(package.CorrelationId, data);
     envelope.ReplyWith(pongMessage);
     return pongMessage;
 }
コード例 #6
0
        public bool Execute(CommandProcessorContext context, string[] args)
        {
            var eventStreamId = "test-stream";
            var expectedVersion = ExpectedVersion.Any;

            if (args.Length > 0)
            {
                if (args.Length > 2)
                    return false;
                eventStreamId = args[0];
                if (args.Length == 2)
                    expectedVersion = args[1].Trim().ToUpper() == "ANY" ? ExpectedVersion.Any : int.Parse(args[1]);
            }

            context.IsAsync();
            var sw = new Stopwatch();
            context.Client.CreateTcpConnection(
                context,
                connectionEstablished: conn =>
                {
                    context.Log.Info("[{0}, L{1}]: Trying to delete event stream '{2}'...", conn.RemoteEndPoint, conn.LocalEndPoint, eventStreamId);
                    var corrid = Guid.NewGuid();
                    var deleteDto = new TcpClientMessageDto.DeleteStream(eventStreamId, expectedVersion, false);
                    var package = new TcpPackage(TcpCommand.DeleteStream, corrid, deleteDto.Serialize()).AsByteArray();
                    sw.Start();
                    conn.EnqueueSend(package);
                },
                handlePackage: (conn, pkg) =>
                {
                    sw.Stop();
                    context.Log.Info("Delete request took: {0}.", sw.Elapsed);

                    if (pkg.Command != TcpCommand.DeleteStreamCompleted)
                    {
                        context.Fail(reason: string.Format("Unexpected TCP package: {0}.", pkg.Command));
                        return;
                    }

                    var dto = pkg.Data.Deserialize<TcpClientMessageDto.DeleteStreamCompleted>();
                    if (dto.Result == TcpClientMessageDto.OperationResult.Success)
                    {
                        context.Log.Info("DELETED event stream {0}.", eventStreamId);
                        PerfUtils.LogTeamCityGraphData(string.Format("{0}-latency-ms", Keyword), (int)sw.ElapsedMilliseconds);
                        context.Success();
                    }
                    else
                    {
                        context.Log.Info("DELETION FAILED for event stream {0}: {1} ({2}).", eventStreamId, dto.Message, dto.Result);
                        context.Fail();
                    }
                    conn.Close();
                },
                connectionClosed:(connection, error) => context.Fail(reason: "Connection was closed prematurely."));

            context.WaitForCompletion();
            return true;
        }
コード例 #7
0
 private static StorageMessage.CommitAck UnwrapCommitAck(TcpPackage package, IEnvelope envelope)
 {
     var dto = package.Data.Deserialize<ReplicationMessageDto.CommitAck>();
     return new StorageMessage.CommitAck(package.CorrelationId,
                                         dto.LogPosition,
                                         dto.TransactionPosition,
                                         dto.FirstEventNumber,
                                         dto.LastEventNumber);
 }
コード例 #8
0
        public void not_authorized_with_empty_data_should_serialize_and_deserialize_correctly()
        {
            var corrId = Guid.NewGuid();
            var refPkg = new TcpPackage(TcpCommand.BadRequest, TcpFlags.None, corrId, null, null, new byte[0]);
            var bytes = refPkg.AsArraySegment();

            var pkg = TcpPackage.FromArraySegment(bytes);
            Assert.AreEqual(TcpCommand.BadRequest, pkg.Command);
            Assert.AreEqual(TcpFlags.None, pkg.Flags);
            Assert.AreEqual(corrId, pkg.CorrelationId);
            Assert.AreEqual(null, pkg.Login);
            Assert.AreEqual(null, pkg.Password);

            Assert.AreEqual(0, pkg.Data.Count);
        }
コード例 #9
0
 private ReplicationMessage.ReplicaSubscriptionRequest UnwrapReplicaSubscriptionRequest(TcpPackage package, IEnvelope envelope, TcpConnectionManager connection)
 {
     var dto = package.Data.Deserialize<ReplicationMessageDto.SubscribeReplica>();
     var vnodeTcpEndPoint = new IPEndPoint(new IPAddress(dto.Ip), dto.Port);
     var lastEpochs = dto.LastEpochs.Safe().Select(x => new Epoch(x.EpochPosition, x.EpochNumber, new Guid(x.EpochId))).ToArray();
     return new ReplicationMessage.ReplicaSubscriptionRequest(package.CorrelationId,
                                                              envelope, 
                                                              connection,
                                                              dto.LogPosition,
                                                              new Guid(dto.ChunkId), 
                                                              lastEpochs,
                                                              vnodeTcpEndPoint,
                                                              new Guid(dto.MasterId), 
                                                              new Guid(dto.SubscriptionId), 
                                                              dto.IsPromotable);
 }
コード例 #10
0
        private static ClientMessage.WriteEvents UnwrapWriteEvents(TcpPackage package, IEnvelope envelope,
                                                                   IPrincipal user, string login, string password)
        {
            var dto = package.Data.Deserialize<TcpClientMessageDto.WriteEvents>();
            if (dto == null) return null;
            
            var events = new Event[dto.Events == null ? 0 : dto.Events.Length];
            for (int i = 0; i < events.Length; ++i)
            {
// ReSharper disable PossibleNullReferenceException
                var e = dto.Events[i];
// ReSharper restore PossibleNullReferenceException
                events[i] = new Event(new Guid(e.EventId), e.EventType, e.DataContentType == 1, e.Data, e.Metadata);
            }
            return new ClientMessage.WriteEvents(Guid.NewGuid(), package.CorrelationId, envelope, dto.RequireMaster,
                                                 dto.EventStreamId, dto.ExpectedVersion, events, user, login, password);
        }
コード例 #11
0
        public void authorized_with_data_should_serialize_and_deserialize_correctly()
        {
            var corrId = Guid.NewGuid();
            var refPkg = new TcpPackage(TcpCommand.BadRequest, TcpFlags.Authenticated, corrId, "login", "pa$$", new byte[] { 1, 2, 3 });
            var bytes = refPkg.AsArraySegment();

            var pkg = TcpPackage.FromArraySegment(bytes);
            Assert.AreEqual(TcpCommand.BadRequest, pkg.Command);
            Assert.AreEqual(TcpFlags.Authenticated, pkg.Flags);
            Assert.AreEqual(corrId, pkg.CorrelationId);
            Assert.AreEqual("login", pkg.Login);
            Assert.AreEqual("pa$$", pkg.Password);

            Assert.AreEqual(3, pkg.Data.Count);
            Assert.AreEqual(1, pkg.Data.Array[pkg.Data.Offset + 0]);
            Assert.AreEqual(2, pkg.Data.Array[pkg.Data.Offset + 1]);
            Assert.AreEqual(3, pkg.Data.Array[pkg.Data.Offset + 2]);
        }
コード例 #12
0
        public void when_handling_trusted_write_on_external_service()
        {
            var package = new TcpPackage(TcpCommand.WriteEvents, TcpFlags.TrustedWrite, Guid.NewGuid(), null, null, new byte[] { });

            var dummyConnection = new DummyTcpConnection();

            var tcpConnectionManager = new TcpConnectionManager(
                Guid.NewGuid().ToString(), TcpServiceType.External, new ClientTcpDispatcher(),
                InMemoryBus.CreateTest(), dummyConnection, InMemoryBus.CreateTest(), new InternalAuthenticationProvider(new Core.Helpers.IODispatcher(InMemoryBus.CreateTest(), new NoopEnvelope()), new StubPasswordHashAlgorithm(), 1),
                TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(10), (man, err) => { });

            tcpConnectionManager.ProcessPackage(package);

            var data = dummyConnection.ReceivedData.Last();
            var receivedPackage = TcpPackage.FromArraySegment(data);
           
            Assert.AreEqual(receivedPackage.Command, TcpCommand.BadRequest, "Expected Bad Request but got {0}", receivedPackage.Command);
        }
コード例 #13
0
        public bool Execute(CommandProcessorContext context, string[] args)
        {
            var package = new TcpPackage(TcpCommand.ScavengeDatabase, Guid.NewGuid(), null);
            context.Log.Info("Sending SCAVENGE request...");

            var connection = context.Client.CreateTcpConnection(
                context,
                (conn, pkg) => { },
                null,
                (typedConnection, error) =>
                {
                    if (error == SocketError.Success)
                        context.Success();
                    else
                        context.Fail();
                });
            connection.EnqueueSend(package.AsByteArray());
            connection.Close("OK");
            return true;
        }
コード例 #14
0
 private static ClientMessage.WriteEvents UnwrapWriteEvents(TcpPackage package, IEnvelope envelope)
 {
     var dto = package.Data.Deserialize<TcpClientMessageDto.WriteEvents>();
     if (dto == null) return null;
     return new ClientMessage.WriteEvents(
             package.CorrelationId,
             envelope,
             dto.AllowForwarding,
             dto.EventStreamId,
             dto.ExpectedVersion,
             dto.Events.Select(x => new Event(new Guid(x.EventId), x.EventType, x.IsJson,  x.Data, x.Metadata)).ToArray());
 }
コード例 #15
0
 private ClientMessage.UnsubscribeFromAllStreams UnwrapUnsubscribeFromAllStreams(TcpPackage package, IEnvelope envelope, TcpConnectionManager connection)
 {
     //var dto = package.Data.Deserialize<HttpClientMessageDto.UnsubscribeFromAllStreams>();
     return new ClientMessage.UnsubscribeFromAllStreams(connection, package.CorrelationId);
 }
コード例 #16
0
 private static ClientMessage.ReadStreamEventsForward UnwrapReadStreamEventsForward(TcpPackage package,
                                                                                    IEnvelope envelope,
                                                                                    TcpConnectionManager connection)
 {
     var dto = package.Data.Deserialize<TcpClientMessageDto.ReadStreamEventsForward>();
     if (dto == null) return null;
     return new ClientMessage.ReadStreamEventsForward(package.CorrelationId,
                                                      envelope,
                                                      dto.EventStreamId,
                                                      dto.StartIndex,
                                                      dto.MaxCount,
                                                      dto.ResolveLinkTos);
 }
コード例 #17
0
 private static ClientMessage.TransactionWriteCompleted UnwrapTransactionWriteCompleted(TcpPackage package, IEnvelope envelope)
 {
     var dto = package.Data.Deserialize<TcpClientMessageDto.TransactionWriteCompleted>();
     if (dto == null) return null;
     return new ClientMessage.TransactionWriteCompleted(package.CorrelationId, dto.TransactionId, dto.EventStreamId, (OperationErrorCode)dto.ErrorCode, dto.Error);
 }
コード例 #18
0
        public void ProcessPackage(TcpPackage package)
        {
            if (_serviceType == TcpServiceType.External && (package.Flags & TcpFlags.TrustedWrite) != 0)
            {
                SendBadRequestAndClose(package.CorrelationId, "Trusted writes aren't accepted over the external TCP interface");
                return;
            }
            switch (package.Command)
            {
            case TcpCommand.HeartbeatResponseCommand:
                break;

            case TcpCommand.HeartbeatRequestCommand:
                SendPackage(new TcpPackage(TcpCommand.HeartbeatResponseCommand, package.CorrelationId, null));
                break;

            case TcpCommand.IdentifyClient:
            {
                try
                {
                    var message = (ClientMessage.IdentifyClient)_dispatcher.UnwrapPackage(package, _tcpEnvelope, null, null, null, this, _version);
                    Log.Info("Connection '{connectionName}' ({connectionId:B}) identified by client. Client connection name: '{clientConnectionName}', Client version: {clientVersion}.",
                             ConnectionName, ConnectionId, message.ConnectionName, (ClientVersion)message.Version);
                    _version = (byte)message.Version;
                    _clientConnectionName = message.ConnectionName;
                    _connection.SetClientConnectionName(_clientConnectionName);
                    SendPackage(new TcpPackage(TcpCommand.ClientIdentified, package.CorrelationId, null));
                }
                catch (Exception ex)
                {
                    Log.Error("Error identifying client: {e}", ex);
                }
                break;
            }

            case TcpCommand.BadRequest:
            {
                var reason = string.Empty;
                Helper.EatException(() => reason = Helper.UTF8NoBom.GetString(package.Data.Array, package.Data.Offset, package.Data.Count));
                Log.Error("Bad request received from '{connectionName}{clientConnectionName}' [{remoteEndPoint}, L{localEndPoint}, {connectionId:B}], will stop server. CorrelationId: {correlationId:B}, Error: {e}.",
                          ConnectionName, ClientConnectionName.IsEmptyString() ? string.Empty : ":" + ClientConnectionName, RemoteEndPoint,
                          LocalEndPoint, ConnectionId, package.CorrelationId, reason.IsEmptyString() ? "<reason missing>" : reason);
                break;
            }

            case TcpCommand.Authenticate:
            {
                if ((package.Flags & TcpFlags.Authenticated) == 0)
                {
                    ReplyNotAuthenticated(package.CorrelationId, "No user credentials provided.");
                }
                else
                {
                    var defaultUser = new UserCredentials(package.Login, package.Password, null);
                    Interlocked.Exchange(ref _defaultUser, defaultUser);
                    _authProvider.Authenticate(new TcpDefaultAuthRequest(this, package.CorrelationId, defaultUser));
                }
                break;
            }

            default:
            {
                var defaultUser = _defaultUser;
                if ((package.Flags & TcpFlags.TrustedWrite) != 0)
                {
                    UnwrapAndPublishPackage(package, UserManagement.SystemAccount.Principal, null, null);
                }
                else if ((package.Flags & TcpFlags.Authenticated) != 0)
                {
                    _authProvider.Authenticate(new TcpAuthRequest(this, package, package.Login, package.Password));
                }
                else if (defaultUser != null)
                {
                    if (defaultUser.User != null)
                    {
                        UnwrapAndPublishPackage(package, defaultUser.User, defaultUser.Login, defaultUser.Password);
                    }
                    else
                    {
                        _authProvider.Authenticate(new TcpAuthRequest(this, package, defaultUser.Login, defaultUser.Password));
                    }
                }
                else
                {
                    UnwrapAndPublishPackage(package, null, null, null);
                }
                break;
            }
            }
        }
コード例 #19
0
 private static ClientMessage.ReadAllEventsForward UnwrapReadAllEventsForward(TcpPackage package, IEnvelope envelope, TcpConnectionManager connection)
 {
     var dto = package.Data.Deserialize<TcpClientMessageDto.ReadAllEventsForward>();
     if (dto == null) return null;
     return new ClientMessage.ReadAllEventsForward(package.CorrelationId,
                                                   envelope,
                                                   dto.CommitPosition,
                                                   dto.PreparePosition,
                                                   dto.MaxCount,
                                                   dto.ResolveLinkTos);
 }
コード例 #20
0
        private static ClientMessage.FilteredReadAllEventsForward UnwrapFilteredReadAllEventsForward(TcpPackage package,
                                                                                                     IEnvelope envelope, ClaimsPrincipal user)
        {
            var dto = package.Data.Deserialize <TcpClientMessageDto.FilteredReadAllEvents>();

            if (dto == null)
            {
                return(null);
            }

            IEventFilter eventFilter = EventFilter.Get(dto.Filter);

            int maxSearchWindow = dto.MaxCount;

            if (dto.MaxSearchWindow.HasValue)
            {
                maxSearchWindow = dto.MaxSearchWindow.GetValueOrDefault();
            }

            return(new ClientMessage.FilteredReadAllEventsForward(Guid.NewGuid(), package.CorrelationId, envelope,
                                                                  dto.CommitPosition, dto.PreparePosition, dto.MaxCount,
                                                                  dto.ResolveLinkTos, dto.RequireLeader, maxSearchWindow, null, eventFilter, user, null));
        }
コード例 #21
0
        private static ClientMessage.ReadStreamEventsForward UnwrapReadStreamEventsForward(TcpPackage package, IEnvelope envelope, IPrincipal user)
        {
            var dto = package.Data.Deserialize <TcpClientMessageDto.ReadStreamEvents>();

            if (dto == null)
            {
                return(null);
            }
            return(new ClientMessage.ReadStreamEventsForward(Guid.NewGuid(), package.CorrelationId, envelope,
                                                             dto.EventStreamId, dto.FromEventNumber, dto.MaxCount,
                                                             dto.ResolveLinkTos, dto.RequireMaster, null, user));
        }
コード例 #22
0
 public TcpAuthRequest(TcpConnectionManager manager, TcpPackage package, string login, string password)
     : base(login, password)
 {
     _manager = manager;
     _package = package;
 }
コード例 #23
0
        private static StorageMessage.PrepareAck UnwrapPrepareAck(TcpPackage package, IEnvelope envelope)
        {
            var dto = package.Data.Deserialize <ReplicationMessageDto.PrepareAck>();

            return(new StorageMessage.PrepareAck(package.CorrelationId, dto.LogPosition, (PrepareFlags)dto.Flags));
        }
コード例 #24
0
        public void ProcessPackage(TcpPackage package)
        {
            if (_serviceType == TcpServiceType.External && (package.Flags & TcpFlags.TrustedWrite) != 0)
            {
                SendBadRequestAndClose(package.CorrelationId, "Trusted writes aren't accepted over the external TCP interface");
                return;
            }
            switch (package.Command)
            {
            case TcpCommand.HeartbeatResponseCommand:
                break;

            case TcpCommand.HeartbeatRequestCommand:
                SendPackage(new TcpPackage(TcpCommand.HeartbeatResponseCommand, package.CorrelationId, null));
                break;

            case TcpCommand.BadRequest:
            {
                var reason = string.Empty;
                Helper.EatException(() => reason = Helper.UTF8NoBom.GetString(package.Data.Array, package.Data.Offset, package.Data.Count));
                var exitMessage =
                    string.Format("Bad request received from '{0}' [{1}, L{2}, {3:B}], will stop server. CorrelationId: {4:B}, Error: {5}.",
                                  ConnectionName, RemoteEndPoint, LocalEndPoint, ConnectionId, package.CorrelationId,
                                  reason.IsEmptyString() ? "<reason missing>" : reason);
                Log.Error(exitMessage);
                break;
            }

            case TcpCommand.Authenticate:
            {
                if ((package.Flags & TcpFlags.Authenticated) == 0)
                {
                    ReplyNotAuthenticated(package.CorrelationId, "No user credentials provided.");
                }
                else
                {
                    var defaultUser = new UserCredentials(package.Login, package.Password, null);
                    Interlocked.Exchange(ref _defaultUser, defaultUser);
                    _authProvider.Authenticate(new TcpDefaultAuthRequest(this, package.CorrelationId, defaultUser));
                }
                break;
            }

            default:
            {
                var defaultUser = _defaultUser;
                if ((package.Flags & TcpFlags.TrustedWrite) != 0)
                {
                    UnwrapAndPublishPackage(package, UserManagement.SystemAccount.Principal, null, null);
                }
                else if ((package.Flags & TcpFlags.Authenticated) != 0)
                {
                    _authProvider.Authenticate(new TcpAuthRequest(this, package, package.Login, package.Password));
                }
                else if (defaultUser != null)
                {
                    if (defaultUser.User != null)
                    {
                        UnwrapAndPublishPackage(package, defaultUser.User, defaultUser.Login, defaultUser.Password);
                    }
                    else
                    {
                        _authProvider.Authenticate(new TcpAuthRequest(this, package, defaultUser.Login, defaultUser.Password));
                    }
                }
                else
                {
                    UnwrapAndPublishPackage(package, null, null, null);
                }
                break;
            }
            }
        }
コード例 #25
0
        private ReplicationMessage.ReplicaSubscriptionRequest UnwrapReplicaSubscriptionRequest(TcpPackage package,
                                                                                               IEnvelope envelope, TcpConnectionManager connection)
        {
            var dto = package.Data.Deserialize <ReplicationMessageDto.SubscribeReplica>();
            var vnodeTcpEndPoint = new IPEndPoint(new IPAddress(dto.Ip), dto.Port);
            var lastEpochs       = dto.LastEpochs.Safe()
                                   .Select(x => new Epoch(x.EpochPosition, x.EpochNumber, new Guid(x.EpochId))).ToArray();

            return(new ReplicationMessage.ReplicaSubscriptionRequest(package.CorrelationId,
                                                                     envelope,
                                                                     connection,
                                                                     dto.LogPosition,
                                                                     new Guid(dto.ChunkId),
                                                                     lastEpochs,
                                                                     vnodeTcpEndPoint,
                                                                     new Guid(dto.LeaderId),
                                                                     new Guid(dto.SubscriptionId),
                                                                     dto.IsPromotable));
        }
コード例 #26
0
        private static ReplicationTrackingMessage.LeaderReplicatedTo UnwrapReplicatedTo(TcpPackage package, IEnvelope envelope)
        {
            var dto = package.Data.Deserialize <ReplicationMessageDto.ReplicatedTo>();

            return(new ReplicationTrackingMessage.LeaderReplicatedTo(dto.LogPosition));
        }
コード例 #27
0
        private ReplicationMessage.DropSubscription UnwrapDropSubscription(TcpPackage package, IEnvelope envelope)
        {
            var dto = package.Data.Deserialize <ReplicationMessageDto.CloneAssignment>();

            return(new ReplicationMessage.DropSubscription(new Guid(dto.LeaderId), new Guid(dto.SubscriptionId)));
        }
コード例 #28
0
        private ReplicationMessage.ReplicaSubscriptionRetry UnwrapReplicaSubscriptionRetry(TcpPackage package,
                                                                                           IEnvelope envelope)
        {
            var dto = package.Data.Deserialize <ReplicationMessageDto.ReplicaSubscriptionRetry>();

            return(new ReplicationMessage.ReplicaSubscriptionRetry(new Guid(dto.LeaderId),
                                                                   new Guid(dto.SubscriptionId)));
        }
コード例 #29
0
        private ClientMessage.FilteredSubscribeToStream UnwrapFilteredSubscribeToStream(TcpPackage package,
                                                                                        IEnvelope envelope,
                                                                                        ClaimsPrincipal user,
                                                                                        string login,
                                                                                        string pass,
                                                                                        TcpConnectionManager connection)
        {
            var dto = package.Data.Deserialize <TcpClientMessageDto.FilteredSubscribeToStream>();

            if (dto == null)
            {
                return(null);
            }

            IEventFilter eventFilter = EventFilter.Get(dto.Filter);

            return(new ClientMessage.FilteredSubscribeToStream(Guid.NewGuid(), package.CorrelationId, envelope,
                                                               connection.ConnectionId, dto.EventStreamId, dto.ResolveLinkTos, user, eventFilter,
                                                               dto.CheckpointInterval));
        }
コード例 #30
0
 public TcpAuthRequest(TcpConnectionManager manager, TcpPackage package)
     : base($"(TCP) {manager.RemoteEndPoint}", package.Tokens)
 {
     _manager = manager;
     _package = package;
 }
コード例 #31
0
 public TcpAuthRequest(TcpConnectionManager manager, TcpPackage package, string login, string password)
     : base($"(TCP) {manager.RemoteEndPoint}", login, password)
 {
     _manager = manager;
     _package = package;
 }
コード例 #32
0
 private ClientMessage.ScavengeDatabase UnwrapScavengeDatabase(TcpPackage package, IEnvelope envelope, IPrincipal user)
 {
     return(new ClientMessage.ScavengeDatabase(envelope, package.CorrelationId, user));
 }
コード例 #33
0
ファイル: WriteWorker.cs プロジェクト: vishal-h/EventStore-1
        private void OnPackageArrived(TcpTypedConnection<byte[]> connection, TcpPackage package)
        {
            if (package.Command != TcpCommand.WriteEventsCompleted)
            {
                _worker.SignalWorkerFailed(
                    error: string.Format("Worker {0} received unexpected command. Expected: {1}, received: {2}.",
                                         _worker.Name,
                                         TcpCommand.WriteEventsCompleted,
                                         package.Command));
                return;
            }

            var dto = package.Data.Deserialize<ClientMessageDto.WriteEventsCompleted>();
            var corrId = new Guid(dto.CorrelationId);

            WorkerItem workItem;
            if (!_worker.TryGetWorkItem(corrId, out workItem))
            {
                _worker.SignalWorkerFailed(
                    error:
                        string.Format(
                            "Worker {0} received unexpected CorrId: {1}, no event with such CorrId is in progress.",
                            _worker.Name,
                            corrId));
                return;
            }

            switch ((OperationErrorCode) dto.ErrorCode)
            {
                case OperationErrorCode.Success:
                    {
                        if (_worker.TryRemoveWorkItem(workItem))
                            _worker.NotifyItemProcessed(workItem);
                        break;
                    }
                case OperationErrorCode.PrepareTimeout:
                case OperationErrorCode.CommitTimeout:
                case OperationErrorCode.ForwardTimeout:
                    _worker.Retry(workItem);
                    break;
                case OperationErrorCode.WrongExpectedVersion:
                    if (workItem.Event.ExpectedVersion == ExpectedVersion.Any)
                    {
                        if (workItem.Event.ShouldBeVersion != 1)
                        {
                            _worker.SignalWorkerFailed(
                                error: string.Format("Worker {0} received WrongExpectedVersion for event with "
                                                     + "ExpectedVersion = EventNumber.Any and ShouldBeVersion = 1, "
                                                     + "which shouldn't happen ever!",
                                                     _worker.Name));

                            _worker.TryRemoveWorkItem(workItem);
                            return;
                        }
                    }
                    _worker.Retry(workItem);
                    break;
                case OperationErrorCode.StreamDeleted:
                case OperationErrorCode.InvalidTransaction:
                    _worker.SignalWorkerFailed(
                        error: string.Format("Worker {0} received unexpected OperationErrorCode: {1}.",
                                             _worker.Name,
                                             (OperationErrorCode) dto.ErrorCode));
                    _worker.TryRemoveWorkItem(workItem);
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }
コード例 #34
0
        private static ClientMessage.ReadAllEventsBackward UnwrapReadAllEventsBackward(TcpPackage package, IEnvelope envelope, IPrincipal user)
        {
            var dto = package.Data.Deserialize <TcpClientMessageDto.ReadAllEvents>();

            if (dto == null)
            {
                return(null);
            }
            return(new ClientMessage.ReadAllEventsBackward(Guid.NewGuid(), package.CorrelationId, envelope,
                                                           dto.CommitPosition, dto.PreparePosition, dto.MaxCount,
                                                           dto.ResolveLinkTos, dto.RequireMaster, null, user));
        }
コード例 #35
0
        private void WriteFlood(CommandProcessorContext context, int writeCnt, int clientsCnt, long requestsCnt)
        {
            const string data = "test-data";
            
            context.IsAsync();

            var clients = new List<TcpTypedConnection<byte[]>>();
            var threads = new List<Thread>();
            var doneEvent = new ManualResetEventSlim(false);
            long succ = 0;
            long fail = 0;
            long all = 0;

            for (int i = 0; i < clientsCnt; i++)
            {
                var count = requestsCnt / clientsCnt + ((i == clientsCnt - 1) ? requestsCnt % clientsCnt : 0);
                var localDoneEvent = new AutoResetEvent(false);
                var eventStreamId = "es" + Guid.NewGuid();
                var client = context.Client.CreateTcpConnection(
                    context,
                    (conn, pkg) =>
                    {
                        if (pkg.Command != TcpCommand.WriteEventsCompleted)
                        {
                            context.Fail(reason: string.Format("Unexpected TCP package: {0}.", pkg.Command));
                            return;
                        }

                        var dto = pkg.Data.Deserialize<TcpClientMessageDto.WriteEventsCompleted>();
                        if (dto.Result == TcpClientMessageDto.OperationResult.Success)
                        {
                            if (Interlocked.Increment(ref succ)%1000 == 0) Console.Write(".");
                        }
                        else 
                        {
                            if (Interlocked.Increment(ref fail)%1000 == 0) Console.Write("#");
                        }

                        if (Interlocked.Increment(ref all) == requestsCnt)
                        {
                            context.Success();
                            doneEvent.Set();
                        }
                        localDoneEvent.Set();
                    },
                    connectionClosed: (conn, err) => context.Fail(reason: "Connection was closed prematurely."));
                clients.Add(client);

                threads.Add(new Thread(() =>
                {
                    for (int j = 0; j < count; ++j)
                    {
                        var writeDto = new TcpClientMessageDto.WriteEvents(
                            eventStreamId,
                            ExpectedVersion.Any,
                            Enumerable.Range(0, writeCnt).Select(x => 
                                new TcpClientMessageDto.NewEvent(Guid.NewGuid().ToByteArray(),
                                                                 "type",
                                                                 0,0,
                                                                 Common.Utils.Helper.UTF8NoBom.GetBytes(data),
                                                                 new byte[0])).ToArray(),
                            false);
                        var package = new TcpPackage(TcpCommand.WriteEvents, Guid.NewGuid(), writeDto.Serialize());
                        client.EnqueueSend(package.AsByteArray());
                        localDoneEvent.WaitOne();
                    }
                }) { IsBackground = true });
            }

            var sw = Stopwatch.StartNew();
            threads.ForEach(thread => thread.Start());
            doneEvent.Wait();
            sw.Stop();
            clients.ForEach(client => client.Close());

            var reqPerSec = (all + 0.0) / sw.ElapsedMilliseconds * 1000;
            context.Log.Info("Completed. Successes: {0}, failures: {1}", succ, fail);
            context.Log.Info("{0} requests completed in {1}ms ({2:0.00} reqs per sec).", 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", succ), PerfUtils.Col("failures", fail)));
            PerfUtils.LogTeamCityGraphData(string.Format("{0}-{1}-{2}-reqPerSec", Keyword, clientsCnt, requestsCnt), (int)reqPerSec);
            PerfUtils.LogTeamCityGraphData(string.Format("{0}-{1}-{2}-failureSuccessRate", Keyword, clientsCnt, requestsCnt), (int)(100.0 * fail / (fail + succ)));
            PerfUtils.LogTeamCityGraphData(string.Format("{0}-latency-ms", Keyword), (int)Math.Round(sw.Elapsed.TotalMilliseconds / requestsCnt));

            if (succ != requestsCnt)
                context.Fail(reason: "There were errors or not all requests completed.");
            else
                context.Success();
        }
コード例 #36
0
        public bool Execute(CommandProcessorContext context, string[] args)
        {
            var eventStreamId = "test-stream";
            var expectedVersion = ExpectedVersion.Any;
            var data = GenerateTestData();
            string metadata = null;

            if (args.Length > 0)
            {
                if (args.Length < 3 || args.Length > 4)
                    return false;
                eventStreamId = args[0];
                expectedVersion = args[1].ToUpper() == "ANY" ? ExpectedVersion.Any : int.Parse(args[1]);
                data = args[2];
                if (args.Length == 4)
                    metadata = args[3];
            }

            context.IsAsync();
            var writeDto = new TcpClientMessageDto.WriteEvents(
                eventStreamId,
                expectedVersion,
                new[]
                {
                    new TcpClientMessageDto.NewEvent(Guid.NewGuid().ToByteArray(),
                                                     "JsonDataEvent",
                                                     1,0,
                                                     Helper.UTF8NoBom.GetBytes(data),
                                                     Helper.UTF8NoBom.GetBytes(metadata ?? string.Empty))
                },
                false);
            var package = new TcpPackage(TcpCommand.WriteEvents, Guid.NewGuid(), writeDto.Serialize());

            var sw = new Stopwatch();
            bool dataReceived = false;

            context.Client.CreateTcpConnection(
                context,
                connectionEstablished: conn =>
                {
                    context.Log.Info("[{0}, L{1}]: Writing...", conn.RemoteEndPoint, conn.LocalEndPoint);
                    sw.Start();
                    conn.EnqueueSend(package.AsByteArray());
                },
                handlePackage: (conn, pkg) =>
                {
                    if (pkg.Command != TcpCommand.WriteEventsCompleted)
                    {
                        context.Fail(reason: string.Format("Unexpected TCP package: {0}.", pkg.Command));
                        return;
                    }

                    dataReceived = true;
                    sw.Stop();

                    var dto = pkg.Data.Deserialize<TcpClientMessageDto.WriteEventsCompleted>();
                    if (dto.Result == TcpClientMessageDto.OperationResult.Success)
                    {
                        context.Log.Info("Successfully written. EventId: {0}.", package.CorrelationId);
                        PerfUtils.LogTeamCityGraphData(string.Format("{0}-latency-ms", Keyword), (int)sw.ElapsedMilliseconds);
                    }
                    else
                    {
                        context.Log.Info("Error while writing: {0} ({1}).", dto.Message, dto.Result);
                    }

                    context.Log.Info("Write request took: {0}.", sw.Elapsed);
                    conn.Close();
                    context.Success();
                },
                connectionClosed: (connection, error) =>
                {
                    if (dataReceived && error == SocketError.Success)
                        context.Success();
                    else
                        context.Fail();
                });

            context.WaitForCompletion();
            return true;
        }
コード例 #37
0
        private void OnMessageArrived(ArraySegment <byte> data)
        {
            var package = TcpPackage.FromArraySegment(data);

            OnPackageReceived(package);
        }
コード例 #38
0
        private ReplicationMessage.SlaveAssignment UnwrapSlaveAssignment(TcpPackage package, IEnvelope envelope)
        {
            var dto = package.Data.Deserialize <ReplicationMessageDto.SlaveAssignment>();

            return(new ReplicationMessage.SlaveAssignment(new Guid(dto.MasterId), new Guid(dto.SubscriptionId)));
        }
コード例 #39
0
        public bool Execute(CommandProcessorContext context, string[] args)
        {
            const string data = "test-data";
            var eventStreamId = "test-stream";
            var writeCount = 10;
            var expectedVersion = ExpectedVersion.Any;

            if (args.Length > 0)
            {
                if (args.Length > 3)
                    return false;
                writeCount = int.Parse(args[0]);
                if (args.Length >= 2)
                    eventStreamId = args[1];
                if (args.Length >= 3)
                    expectedVersion = args[2].Trim().ToUpper() == "ANY" ? ExpectedVersion.Any : int.Parse(args[2].Trim());
            }

            context.IsAsync();
            var sw = new Stopwatch();
            context.Client.CreateTcpConnection(
                context,
                connectionEstablished: conn =>
                {
                    context.Log.Info("[{0}, L{1}]: Writing...", conn.RemoteEndPoint, conn.LocalEndPoint);
                    var writeDto = new TcpClientMessageDto.WriteEvents(
                        eventStreamId,
                        expectedVersion,
                        Enumerable.Range(0, writeCount).Select(x => new TcpClientMessageDto.NewEvent(Guid.NewGuid().ToByteArray(),
                                                                                                     "type",
                                                                                                     0,0,
                                                                                                     Helper.UTF8NoBom.GetBytes(data),
                                                                                                     new byte[0])).ToArray(),
                        false);
                    var package = new TcpPackage(TcpCommand.WriteEvents, Guid.NewGuid(), writeDto.Serialize()).AsByteArray();
                    sw.Start();
                    conn.EnqueueSend(package);
                },
                handlePackage: (conn, pkg) =>
                {
                    sw.Stop();
                    context.Log.Info("Write request took: {0}.", sw.Elapsed);

                    if (pkg.Command != TcpCommand.WriteEventsCompleted)
                    {
                        context.Fail(reason: string.Format("Unexpected TCP package: {0}.", pkg.Command));
                        return;
                    }

                    var dto = pkg.Data.Deserialize<TcpClientMessageDto.WriteEventsCompleted>();
                    if (dto.Result == TcpClientMessageDto.OperationResult.Success)
                    {
                        context.Log.Info("Successfully written {0} events.", writeCount);
                        PerfUtils.LogTeamCityGraphData(string.Format("{0}-latency-ms", Keyword), (int)Math.Round(sw.Elapsed.TotalMilliseconds));
                        context.Success();
                    }
                    else
                    {
                        context.Log.Info("Error while writing: {0}.", dto.Result);
                        context.Fail();
                    }
                    conn.Close();
                },
                connectionClosed: (connection, error) => context.Fail(reason: "Connection was closed prematurely."));

            context.WaitForCompletion();
            return true;
        }
コード例 #40
0
        private static ClientMessage.DeleteStreamCompleted UnwrapDeleteStreamCompleted(TcpPackage package, IEnvelope envelope)
        {
            var dto = package.Data.Deserialize <ClientMessageDto.DeleteStreamCompleted>();

            if (dto == null)
            {
                return(null);
            }
            return(new ClientMessage.DeleteStreamCompleted(package.CorrelationId,
                                                           dto.EventStreamId,
                                                           (OperationErrorCode)dto.ErrorCode,
                                                           dto.Error));
        }
コード例 #41
0
ファイル: ReadProcessor.cs プロジェクト: jpierson/EventStore
        public bool Execute(CommandProcessorContext context, string[] args)
        {
            var eventStreamId = "test-stream";
            var fromNumber = 0;

            if (args.Length > 0)
            {
                if (args.Length > 2)
                    return false;

                eventStreamId = args[0];

                if (args.Length == 2)
                    fromNumber = int.Parse(args[1]);
            }

            context.IsAsync();

            var readDto = new ClientMessageDto.ReadEvent(Guid.NewGuid(), eventStreamId, fromNumber);
            var package = new TcpPackage(TcpCommand.ReadEvent, readDto.Serialize());

            var sw = new Stopwatch();

            context.Client.CreateTcpConnection(
                context,
                connectionEstablished: conn =>
                {
                    context.Log.Info("[{0}]: Reading...", conn.EffectiveEndPoint);
                    sw.Start();
                    conn.EnqueueSend(package.AsByteArray());
                },
                handlePackage: (conn, pkg) =>
                {
                    if (pkg.Command != TcpCommand.ReadEventCompleted)
                    {
                        context.Fail(reason: string.Format("Unexpected TCP package: {0}.", pkg.Command));
                        return;
                    }

                    sw.Stop();

                    var dto = pkg.Data.Deserialize<ClientMessageDto.ReadEventCompleted>();

                    context.Log.Info("READ events from <{0}>:\n\n"
                                     + "\tCorrelationId: {1}\n"
                                     + "\tEventStreamId: {2}\n"
                                     + "\tEventNumber:   {3}\n"
                                     + "\tReadResult:    {4}\n"
                                     + "\tEventType:     {5}\n"
                                     + "\tData:          {6}\n"
                                     + "\tMetadata:      {7}\n",
                                     eventStreamId,
                                     dto.CorrelationId,
                                     dto.EventStreamId,
                                     dto.EventNumber,
                                     (SingleReadResult) dto.Result,
                                     dto.EventType,
                                     Encoding.UTF8.GetString(dto.Data ?? new byte[0]),
                                     Encoding.UTF8.GetString(dto.Metadata ?? new byte[0]));

                    context.Log.Info("Read request took: {0}.", sw.Elapsed);

                    PerfUtils.LogTeamCityGraphData(string.Format("{0}-latency-ms", Keyword), (int)sw.ElapsedMilliseconds);

                    conn.Close();
                    context.Success();
                },
                connectionClosed: (connection, error) =>
                {
                    if (error == SocketError.Success)
                        context.Success();
                    else
                        context.Fail();
                });

            context.WaitForCompletion();
            return true;
        }
コード例 #42
0
        private static ClientMessage.ReadStreamEventsBackward UnwrapReadStreamEventsBackward(TcpPackage package,
                                                                                             IEnvelope envelope,
                                                                                             TcpConnectionManager connection)
        {
            var dto = package.Data.Deserialize <ClientMessageDto.ReadStreamEventsBackward>();

            if (dto == null)
            {
                return(null);
            }
            return(new ClientMessage.ReadStreamEventsBackward(package.CorrelationId,
                                                              envelope,
                                                              dto.EventStreamId,
                                                              dto.StartIndex,
                                                              dto.MaxCount,
                                                              dto.ResolveLinkTos));
        }
コード例 #43
0
        private void Flood(CommandProcessorContext context, 
                           string eventStreamId, 
                           int clientsCnt, 
                           int minPerSecond, 
                           int maxPerSecond, 
                           int runTimeMinutes)
        {
            context.IsAsync();

            var clients = new List<TcpTypedConnection<byte[]>>();
            var threads = new List<Thread>();
            var doneEvent = new ManualResetEvent(false);
            var done = false;

            var succ = 0;
            var fail = 0;

            var requestsCnt = 0;

            int sent = 0;
            int received = 0;

            var watchLockRoot = new object();
            var sw = Stopwatch.StartNew();
            for (int i = 0; i < clientsCnt; i++)
            {
                var esId = eventStreamId ?? "Stream-" + Thread.CurrentThread.ManagedThreadId % 3;

                var client = context.Client.CreateTcpConnection(
                    context,
                    (conn, pkg) =>
                    {
                        if (pkg.Command != TcpCommand.WriteEventsCompleted)
                        {
                            context.Fail(reason: string.Format("Unexpected TCP package: {0}.", pkg.Command));
                            return;
                        }

                        var dto = pkg.Data.Deserialize<TcpClientMessageDto.WriteEventsCompleted>();
                        if (dto.Result == TcpClientMessageDto.OperationResult.Success)
                        {
                            var succDone = Interlocked.Increment(ref succ);
                            if (succDone%maxPerSecond == 0)
                                Console.Write(".");

                            Interlocked.Increment(ref requestsCnt);
                        }
                        else
                            Interlocked.Increment(ref fail);

                        Interlocked.Increment(ref received);
                    },
                    connectionClosed: (conn, err) =>
                    {
                        if (!done)
                            context.Fail(reason: "Socket was closed, but not all requests were completed.");
                        else
                            context.Success();
                    });
                clients.Add(client);

                threads.Add(new Thread(() =>
                {
                    var sentCount = 0;
                    var sleepTime = 0;

                    var dataSizeCoefficient = 1;
                    var currentMinute = -1;

                    while (true)
                    {
                        TimeSpan elapsed;
                        lock (watchLockRoot)
                            elapsed = sw.Elapsed;

                        if (elapsed.TotalMinutes > runTimeMinutes)
                        {
                            done = true;
                            doneEvent.Set();
                            break;
                        }

                        if (sentCount == 0)
                        {
                            int elapsedMinutesInt = (int)elapsed.TotalMinutes;
                            lock (_randomLockRoot)
                            {
                                sentCount = minPerSecond == maxPerSecond 
                                            ? maxPerSecond : _random.Next(minPerSecond, maxPerSecond);
                                dataSizeCoefficient = _random.Next(8, 256);
                            }

                            if (currentMinute != elapsedMinutesInt)
                            {
                                currentMinute = elapsedMinutesInt;
                                context.Log.Info("\nElapsed {0} of {1} minutes, sent {2}; next block coef. {3}",
                                                 elapsedMinutesInt,
                                                 runTimeMinutes,
                                                 sent,
                                                 dataSizeCoefficient);
                            }

                            sleepTime = 1000 / sentCount;
                        }

                        var dataSize = dataSizeCoefficient * 8;
                        var write = new TcpClientMessageDto.WriteEvents(
                            esId,
                            ExpectedVersion.Any,
                            new[]
                            {
                                new TcpClientMessageDto.NewEvent(
                                    Guid.NewGuid().ToByteArray(),
                                    "TakeSomeSpaceEvent",
                                    0,0,
                                    Helper.UTF8NoBom.GetBytes("DATA" + dataSize.ToString(" 00000 ") + new string('*', dataSize)),
                                    Helper.UTF8NoBom.GetBytes("METADATA" + new string('$', 100)))
                            },
                            false);
                        var package = new TcpPackage(TcpCommand.WriteEvents, Guid.NewGuid(), write.Serialize());
                        client.EnqueueSend(package.AsByteArray());

                        Interlocked.Increment(ref sent);

                        Thread.Sleep(sleepTime);
                        sentCount -= 1;

                        while (sent - received > context.Client.Options.WriteWindow/clientsCnt)
                        {
                            Thread.Sleep(1);
                        }
                    }
                }));
            }

            foreach (var thread in threads)
            {
                thread.IsBackground = true;
                thread.Start();
            }

            doneEvent.WaitOne();
            sw.Stop();

            foreach (var client in clients)
            {
                client.Close();
            }

            context.Log.Info("Completed. Successes: {0}, failures: {1}", succ, fail);
            var reqPerSec = (requestsCnt + 0.0)/sw.ElapsedMilliseconds*1000;
            context.Log.Info("{0} requests completed in {1}ms ({2:0.00} reqs per sec).",
                             requestsCnt,
                             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", succ), PerfUtils.Col("failures", fail))
            );

            PerfUtils.LogTeamCityGraphData(string.Format("{0}-{1}-{2}-reqPerSec", Keyword, clientsCnt, requestsCnt),
                        (int)reqPerSec);

            PerfUtils.LogTeamCityGraphData(
                string.Format("{0}-{1}-{2}-failureSuccessRate", Keyword, clientsCnt, requestsCnt),
                100*fail/(fail + succ));

            context.Success();
        }
コード例 #44
0
        private static ClientMessage.ReadAllEventsBackward UnwrapReadAllEventsBackward(TcpPackage package, IEnvelope envelope, TcpConnectionManager connection)
        {
            var dto = package.Data.Deserialize <ClientMessageDto.ReadAllEventsBackward>();

            if (dto == null)
            {
                return(null);
            }
            return(new ClientMessage.ReadAllEventsBackward(package.CorrelationId,
                                                           envelope,
                                                           dto.CommitPosition,
                                                           dto.PreparePosition,
                                                           dto.MaxCount,
                                                           dto.ResolveLinkTos));
        }
コード例 #45
0
 private static ClientMessage.CreateStream UnwrapCreateStream(TcpPackage package, IEnvelope envelope)
 {
     var dto = package.Data.Deserialize<TcpClientMessageDto.CreateStream>();
     if (dto == null) return null;
     return new ClientMessage.CreateStream(package.CorrelationId, envelope, dto.AllowForwarding, dto.EventStreamId, dto.IsJson, dto.Metadata);
 }
コード例 #46
0
 private ClientMessage.UnsubscribeFromAllStreams UnwrapUnsubscribeFromAllStreams(TcpPackage package, IEnvelope envelope, TcpConnectionManager connection)
 {
     //var dto = package.Data.Deserialize<ClientMessageDto.UnsubscribeFromAllStreams>();
     return(new ClientMessage.UnsubscribeFromAllStreams(connection, package.CorrelationId));
 }
コード例 #47
0
 private static ClientMessage.ReadEvent UnwrapReadEvents(TcpPackage package, IEnvelope envelope)
 {
     var dto = package.Data.Deserialize<TcpClientMessageDto.ReadEvent>();
     if (dto == null) return null;
     return new ClientMessage.ReadEvent(package.CorrelationId, envelope, dto.EventStreamId, dto.EventNumber, dto.ResolveLinkTos);
 }
コード例 #48
0
 private SystemMessage.ScavengeDatabase UnwrapScavengeDatabase(TcpPackage package, IEnvelope envelope)
 {
     return(new SystemMessage.ScavengeDatabase());
 }
コード例 #49
0
 private static ClientMessage.TransactionStart UnwrapTransactionStart(TcpPackage package, IEnvelope envelope)
 {
     var dto = package.Data.Deserialize<TcpClientMessageDto.TransactionStart>();
     if (dto == null) return null;
     return new ClientMessage.TransactionStart(package.CorrelationId, envelope, dto.AllowForwarding, dto.EventStreamId, dto.ExpectedVersion);
 }
コード例 #50
0
        private static ClientMessage.TransactionWriteCompleted UnwrapTransactionWriteCompleted(TcpPackage package, IEnvelope envelope)
        {
            var dto = package.Data.Deserialize <TcpClientMessageDto.TransactionWriteCompleted>();

            if (dto == null)
            {
                return(null);
            }
            return(new ClientMessage.TransactionWriteCompleted(package.CorrelationId, dto.TransactionId, (OperationResult)dto.Result, dto.Message));
        }
コード例 #51
0
        private static ClientMessage.WriteEventsCompleted UnwrapWriteEventCompleted(TcpPackage package, IEnvelope envelope)
        {
            var dto = package.Data.Deserialize<TcpClientMessageDto.WriteEventsCompleted>();
            if (dto == null) return null;
            if (dto.ErrorCode == (int)OperationErrorCode.Success)
                return new ClientMessage.WriteEventsCompleted(package.CorrelationId, dto.EventStreamId, dto.EventNumber);

            return new ClientMessage.WriteEventsCompleted(package.CorrelationId,
                                                          dto.EventStreamId,
                                                          (OperationErrorCode) dto.ErrorCode,
                                                          dto.Error);
        }
コード例 #52
0
        private static ClientMessage.TransactionCommitCompleted UnwrapTransactionCommitCompleted(TcpPackage package, IEnvelope envelope)
        {
            var dto = package.Data.Deserialize <TcpClientMessageDto.TransactionCommitCompleted>();

            if (dto == null)
            {
                return(null);
            }
            if (dto.Result == TcpClientMessageDto.OperationResult.Success)
            {
                return(new ClientMessage.TransactionCommitCompleted(package.CorrelationId, dto.TransactionId, dto.FirstEventNumber, dto.LastEventNumber, dto.PreparePosition ?? -1, dto.CommitPosition ?? -1));
            }
            return(new ClientMessage.TransactionCommitCompleted(package.CorrelationId, dto.TransactionId, (OperationResult)dto.Result, dto.Message));
        }
コード例 #53
0
 private SystemMessage.ScavengeDatabase UnwrapScavengeDatabase(TcpPackage package, IEnvelope envelope)
 {
     return new SystemMessage.ScavengeDatabase();
 }
コード例 #54
0
        private static ClientMessage.DeleteStreamCompleted UnwrapDeleteStreamCompleted(TcpPackage package, IEnvelope envelope)
        {
            var dto = package.Data.Deserialize <TcpClientMessageDto.DeleteStreamCompleted>();

            if (dto == null)
            {
                return(null);
            }
            return(new ClientMessage.DeleteStreamCompleted(package.CorrelationId, (OperationResult)dto.Result,
                                                           dto.Message,
                                                           dto.PreparePosition ?? -1,
                                                           dto.CommitPosition ?? -1));
        }
コード例 #55
0
 private ClientMessage.UnsubscribeFromStream UnwrapUnsubscribeFromStream(TcpPackage package, IEnvelope envelope, TcpConnectionManager connection)
 {
     var dto = package.Data.Deserialize<TcpClientMessageDto.UnsubscribeFromStream>();
     if (dto == null) return null;
     return new ClientMessage.UnsubscribeFromStream(connection, package.CorrelationId, dto.EventStreamId);
 }
コード例 #56
0
        private ReplicationMessage.ReplicaLogPositionAck UnwrapReplicaLogPositionAck(TcpPackage package, IEnvelope envelope, TcpConnectionManager connection)
        {
            var dto = package.Data.Deserialize <ReplicationMessageDto.ReplicaLogPositionAck>();

            return(new ReplicationMessage.ReplicaLogPositionAck(new Guid(dto.SubscriptionId), dto.ReplicationLogPosition));
        }