Пример #1
0
        /// <summary>
        /// Write events to specific stream.
        /// </summary>
        /// <param name="events"></param>
        /// <param name="stream"></param>
        /// <param name="expectedVersion"></param>
        /// <returns></returns>
        public async Task WriteEvents(IEnumerable <CreateEvent> events, string stream, long expectedVersion)
        {
            var writeCorrelationId = Guid.NewGuid();
            var pendingWriteTask   = new TaskCompletionSource <object>(TaskCreationOptions.RunContinuationsAsynchronously);

            if (_pendingWrites.TryAdd(writeCorrelationId, pendingWriteTask))
            {
                // Populate protobuf objects for events
                var eventsToWrite = events.Select(e => new NewEvent()
                {
                    EventId             = ByteString.CopyFrom(e.Id.ToByteArray()),
                    EventType           = e.EventType,
                    Data                = ByteString.CopyFrom(e.Data.Array),
                    DataContentType     = e.IsJson ? 1 : 0,
                    Metadata            = ByteString.CopyFrom(e.MetaData.Array),
                    MetadataContentType = 0
                });

                var writeEventsMessage = new WriteEvents()
                {
                    EventStreamId   = stream,
                    ExpectedVersion = expectedVersion,
                    RequireMaster   = true
                };
                writeEventsMessage.Events.AddRange(eventsToWrite);

                // Serialize events
                ArraySegment <byte> eventsSerialized;
                using (var memory = new MemoryStream())
                {
                    writeEventsMessage.WriteTo(memory);
                    eventsSerialized = new ArraySegment <byte>(memory.GetBuffer(), 0, (int)memory.Length);
                }

                // Send write package
                var package = new TcpPackage(TcpCommand.WriteEvents, writeCorrelationId, eventsSerialized.ToArray <byte>());
                _pendingSendMessages.Enqueue(package);

                // Wait for write to be acknowledged by server then remove completed task
                await pendingWriteTask.Task.ConfigureAwait(false);

                _pendingWrites.TryRemove(writeCorrelationId, out var _);

                return;
            }
            throw new Exception("CorrelationId already in use?");
        }
Пример #2
0
        public void Handle(ClientMessage.WriteEvents message)
        {
            var manager = new WriteEvents(
                _bus,
                _commitTimeout,
                message.Envelope,
                message.InternalCorrId,
                message.CorrelationId,
                message.EventStreamId,
                message.ExpectedVersion,
                message.Events,
                _commitSource);

            _currentRequests.Add(message.InternalCorrId, manager);
            _currentTimedRequests.Add(message.InternalCorrId, Stopwatch.StartNew());
            manager.Start();
        }
Пример #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 WriteEvents(
                Guid.NewGuid().ToString(),
                ExpectedVersion.Any,
                new[] {
                new 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(2000),
                publisher, dummyConnection, publisher,
                new InternalAuthenticationProvider(publisher, new Core.Helpers.IODispatcher(publisher, new NoopEnvelope()),
                                                   new StubPasswordHashAlgorithm(), 1, false),
                new AuthorizationGateway(new TestAuthorizationProvider()),
                TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(10), (man, err) => { },
                _connectionPendingSendBytesThreshold, _connectionQueueSizeThreshold);

            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);
        }
        private static TcpPackage WrapWriteEvents(ClientMessage.WriteEvents msg)
        {
            var events = new NewEvent[msg.Events.Length];

            for (int i = 0; i < events.Length; ++i)
            {
                var e = msg.Events[i];
                events[i] = new NewEvent(e.EventId.ToByteArray(),
                                         e.EventType,
                                         e.IsJson ? 1 : 0,
                                         0, e.Data,
                                         e.Metadata);
            }

            var dto = new WriteEvents(msg.EventStreamId, msg.ExpectedVersion, events,
                                      msg.RequireLeader);

            return(CreateWriteRequestPackage(TcpCommand.WriteEvents, msg, dto));
        }
Пример #5
0
        private void WriteFlood(CommandProcessorContext context, WriteFloodStats stats, int clientsCnt, long requestsCnt, int streamsCnt,
                                int size, int batchSize, string streamNamePrefix, RequestMonitor monitor)
        {
            context.IsAsync();

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

            long last = 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());

            Console.WriteLine($"Last stream: {streams.LastOrDefault()}");
            stats.StartTime = DateTime.UtcNow;
            var sw2 = new Stopwatch();

            for (int i = 0; i < clientsCnt; i++)
            {
                var  count    = requestsCnt / clientsCnt + ((i == clientsCnt - 1) ? requestsCnt % clientsCnt : 0);
                long sent     = 0;
                long received = 0;
                var  rnd      = new Random();
                var  client   = context._tcpTestClient.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 <WriteEventsCompleted>();
                    monitor.EndOperation(pkg.CorrelationId);
                    switch (dto.Result)
                    {
                    case OperationResult.Success:
                        Interlocked.Add(ref stats.Succ, batchSize);
                        if (stats.Succ - last > 1000)
                        {
                            last = stats.Succ;
                            Console.Write(".");
                        }

                        break;

                    case OperationResult.PrepareTimeout:
                        Interlocked.Increment(ref stats.PrepTimeout);
                        break;

                    case OperationResult.CommitTimeout:
                        Interlocked.Increment(ref stats.CommitTimeout);
                        break;

                    case OperationResult.ForwardTimeout:
                        Interlocked.Increment(ref stats.ForwardTimeout);
                        break;

                    case OperationResult.WrongExpectedVersion:
                        Interlocked.Increment(ref stats.WrongExpVersion);
                        break;

                    case OperationResult.StreamDeleted:
                        Interlocked.Increment(ref stats.StreamDeleted);
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }

                    if (dto.Result != OperationResult.Success)
                    {
                        if (Interlocked.Increment(ref stats.Fail) % 1000 == 0)
                        {
                            Console.Write('#');
                        }
                    }
                    Interlocked.Increment(ref received);
                    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();
                    }
                },
                    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 events = new NewEvent[batchSize];
                        for (int q = 0; q < batchSize; q++)
                        {
                            events[q] = new NewEvent(Guid.NewGuid().ToByteArray(),
                                                     "TakeSomeSpaceEvent",
                                                     1, 0,
                                                     Common.Utils.Helper.UTF8NoBom.GetBytes(
                                                         "{ \"DATA\" : \"" + new string('*', size) + "\"}"),
                                                     Common.Utils.Helper.UTF8NoBom.GetBytes(
                                                         "{ \"METADATA\" : \"" + new string('$', 100) + "\"}"));
                        }

                        var corrid = Guid.NewGuid();
                        var write  = new WriteEvents(
                            streams[rnd.Next(streamsCnt)],
                            ExpectedVersion.Any,
                            events,
                            false);
                        var package = new TcpPackage(TcpCommand.WriteEvents, corrid, write.Serialize());
                        monitor.StartOperation(corrid);
                        client.EnqueueSend(package.AsByteArray());

                        var localSent = Interlocked.Increment(ref sent);
                        while (localSent - Interlocked.Read(ref received) >
                               context._tcpTestClient.Options.WriteWindow / clientsCnt)
                        {
                            Thread.Sleep(1);
                        }
                    }
                })
                {
                    IsBackground = true
                });
            }

            var sw = Stopwatch.StartNew();

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

            stats.WriteStatsToFile(context.StatsLogger);
            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);

            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();
            }
        }
Пример #6
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._tcpTestClient.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 <WriteEventsCompleted>();
                    if (dto.Result == 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.Information(
                                    "\nElapsed {elapsed} of {runTime} minutes, sent {sent}; next block coef. {dataSizeCoefficient}",
                                    elapsedMinutesInt,
                                    runTimeMinutes,
                                    sent,
                                    dataSizeCoefficient);
                            }

                            sleepTime = 1000 / sentCount;
                        }

                        var dataSize = dataSizeCoefficient * 8;
                        var write    = new WriteEvents(
                            esId,
                            ExpectedVersion.Any,
                            new[] {
                            new 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._tcpTestClient.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.Information("Completed. Successes: {success}, failures: {failures}", succ, fail);
            var reqPerSec = (requestsCnt + 0.0) / sw.ElapsedMilliseconds * 1000;

            context.Log.Information("{requests} requests completed in {elapsed}ms ({rate: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();
        }
Пример #7
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._tcpTestClient.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 <WriteEventsCompleted>();
                    if (dto.Result == 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 WriteEvents(
                            eventStreamId,
                            ExpectedVersion.Any,
                            Enumerable.Range(0, writeCnt).Select(x =>
                                                                 new 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.Information("Completed. Successes: {success}, failures: {failures}", succ, fail);
            context.Log.Information("{requests} requests completed in {elapsed}ms ({rate: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();
            }
        }
Пример #8
0
        private void Write(Status status, int writerIdx, CommandProcessorContext context, int requests,
                           ManualResetEventSlim finish)
        {
            TcpTypedConnection <byte[]> connection;
            var iteration = new AutoResetEvent(false);

            var sent = 0;

            var prepareTimeouts      = 0;
            var commitTimeouts       = 0;
            var forwardTimeouts      = 0;
            var wrongExpectedVersion = 0;
            var streamsDeleted       = 0;

            var failed = 0;

            var rnd = new Random(writerIdx);

            var streamIdx = -1;
            var head      = -1;

            Action <TcpTypedConnection <byte[]>, TcpPackage> packageHandler = (conn, pkg) => {
                var dto = pkg.Data.Deserialize <WriteEventsCompleted>();
                switch (dto.Result)
                {
                case OperationResult.Success:
                    lock (_heads) {
                        var currentHead = _heads[streamIdx];
                        Ensure.Equal(currentHead, head, "currentHead");
                        _heads[streamIdx]++;
                    }

                    break;

                case OperationResult.PrepareTimeout:
                    prepareTimeouts++;
                    failed++;
                    break;

                case OperationResult.CommitTimeout:
                    commitTimeouts++;
                    failed++;
                    break;

                case OperationResult.ForwardTimeout:
                    forwardTimeouts++;
                    failed++;
                    break;

                case OperationResult.WrongExpectedVersion:
                    wrongExpectedVersion++;
                    failed++;
                    break;

                case OperationResult.StreamDeleted:
                    streamsDeleted++;
                    failed++;
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }

                sent++;
                if (sent % 1000 == 0)
                {
                    status.ReportWritesProgress(writerIdx, sent, prepareTimeouts, commitTimeouts, forwardTimeouts,
                                                wrongExpectedVersion, streamsDeleted, failed, requests);
                }
                iteration.Set();
            };

            Action <TcpTypedConnection <byte[]> >             established = _ => { };
            Action <TcpTypedConnection <byte[]>, SocketError> closed      = null;

            closed = (_, __) => {
                if (!context._tcpTestClient.Options.Reconnect)
                {
                    return;
                }
                Thread.Sleep(TimeSpan.FromSeconds(1));
                connection =
                    context._tcpTestClient.CreateTcpConnection(context, packageHandler, cn => iteration.Set(), closed, false);
            };

            connection = context._tcpTestClient.CreateTcpConnection(context, packageHandler, established, closed, false);

            for (var i = 0; i < requests; ++i)
            {
                streamIdx = NextStreamForWriting(rnd, writerIdx);
                lock (_heads) {
                    head = _heads[streamIdx];
                }

                var evnt  = CreateEvent(_streams[streamIdx], head + 1);
                var write = new WriteEvents(
                    _streams[streamIdx],
                    head,
                    new[] {
                    new 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());
                connection.EnqueueSend(package.AsByteArray());
                iteration.WaitOne();
            }

            status.ReportWritesProgress(writerIdx, sent, prepareTimeouts, commitTimeouts, forwardTimeouts,
                                        wrongExpectedVersion, streamsDeleted, failed, requests);
            status.FinilizeStatus(writerIdx, failed != sent);
            context._tcpTestClient.Options.Reconnect = false;
            connection.Close();
            finish.Set();
        }
Пример #9
0
        // Module defining this command


        // Optional custom code for this activity


        /// <summary>
        /// Returns a configured instance of System.Management.Automation.PowerShell, pre-populated with the command to run.
        /// </summary>
        /// <param name="context">The NativeActivityContext for the currently running activity.</param>
        /// <returns>A populated instance of Sytem.Management.Automation.PowerShell</returns>
        /// <remarks>The infrastructure takes responsibility for closing and disposing the PowerShell instance returned.</remarks>
        protected override ActivityImplementationContext GetPowerShell(NativeActivityContext context)
        {
            System.Management.Automation.PowerShell invoker       = global::System.Management.Automation.PowerShell.Create();
            System.Management.Automation.PowerShell targetCommand = invoker.AddCommand(PSCommandName);

            // Initialize the arguments

            if (Job.Expression != null)
            {
                targetCommand.AddParameter("Job", Job.Get(context));
            }

            if (Location.Expression != null)
            {
                targetCommand.AddParameter("Location", Location.Get(context));
            }

            if (Session.Expression != null)
            {
                targetCommand.AddParameter("Session", Session.Get(context));
            }

            if (Keep.Expression != null)
            {
                targetCommand.AddParameter("Keep", Keep.Get(context));
            }

            if (NoRecurse.Expression != null)
            {
                targetCommand.AddParameter("NoRecurse", NoRecurse.Get(context));
            }

            if (Force.Expression != null)
            {
                targetCommand.AddParameter("Force", Force.Get(context));
            }

            if (Wait.Expression != null)
            {
                targetCommand.AddParameter("Wait", Wait.Get(context));
            }

            if (AutoRemoveJob.Expression != null)
            {
                targetCommand.AddParameter("AutoRemoveJob", AutoRemoveJob.Get(context));
            }

            if (WriteEvents.Expression != null)
            {
                targetCommand.AddParameter("WriteEvents", WriteEvents.Get(context));
            }

            if (WriteJobInResults.Expression != null)
            {
                targetCommand.AddParameter("WriteJobInResults", WriteJobInResults.Get(context));
            }

            if (Name.Expression != null)
            {
                targetCommand.AddParameter("Name", Name.Get(context));
            }

            if (InstanceId.Expression != null)
            {
                targetCommand.AddParameter("InstanceId", InstanceId.Get(context));
            }

            if (JobId.Expression != null)
            {
                targetCommand.AddParameter("Id", JobId.Get(context));
            }

            if (GetIsComputerNameSpecified(context) && (PSRemotingBehavior.Get(context) == RemotingBehavior.Custom))
            {
                targetCommand.AddParameter("ComputerName", PSComputerName.Get(context));
            }


            return(new ActivityImplementationContext()
            {
                PowerShellInstance = invoker
            });
        }
Пример #10
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._tcpTestClient.CreateTcpConnection(
                context,
                connectionEstablished: conn => {
                context.Log.Information("[{remoteEndPoint}, L{localEndPoint}]: Writing...", conn.RemoteEndPoint,
                                        conn.LocalEndPoint);
                var writeDto = new WriteEvents(
                    eventStreamId,
                    expectedVersion,
                    Enumerable.Range(0, writeCount).Select(x => new 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.Information("Write request took: {elapsed}.", sw.Elapsed);

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

                var dto = pkg.Data.Deserialize <WriteEventsCompleted>();
                if (dto.Result == EventStore.Client.Messages.OperationResult.Success)
                {
                    context.Log.Information("Successfully written {writeCount} events.", writeCount);
                    PerfUtils.LogTeamCityGraphData(string.Format("{0}-latency-ms", Keyword),
                                                   (int)Math.Round(sw.Elapsed.TotalMilliseconds));
                    context.Success();
                }
                else
                {
                    context.Log.Information("Error while writing: {e}.", dto.Result);
                    context.Fail();
                }

                conn.Close();
            },
                connectionClosed: (connection, error) => context.Fail(reason: "Connection was closed prematurely."));

            context.WaitForCompletion();
            return(true);
        }
Пример #11
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 WriteEvents(
                eventStreamId,
                expectedVersion,
                new[] {
                new 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._tcpTestClient.CreateTcpConnection(
                context,
                connectionEstablished: conn => {
                context.Log.Information("[{remoteEndPoint}, L{localEndPoint}]: 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 <WriteEventsCompleted>();
                if (dto.Result == OperationResult.Success)
                {
                    context.Log.Information("Successfully written. EventId: {correlationId}.", package.CorrelationId);
                    PerfUtils.LogTeamCityGraphData(string.Format("{0}-latency-ms", Keyword),
                                                   (int)Math.Round(sw.Elapsed.TotalMilliseconds));
                }
                else
                {
                    context.Log.Information("Error while writing: {message} ({e}).", dto.Message, dto.Result);
                }

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

            context.WaitForCompletion();
            return(true);
        }
Пример #12
0
        public bool Execute(CommandProcessorContext context, string[] args)
        {
            var    eventStreamId   = "test-stream";
            var    expectedVersion = ExpectedVersion.Any;
            var    data            = "test-data";
            string metadata        = null;
            bool   isJson          = false;
            string login           = null;
            string pass            = null;

            if (args.Length > 0)
            {
                if (args.Length < 3 || args.Length > 7 || args.Length == 6)
                {
                    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];
                }
                if (args.Length >= 5)
                {
                    isJson = bool.Parse(args[4]);
                }
                if (args.Length >= 7)
                {
                    login = args[5];
                    pass  = args[6];
                }
            }

            context.IsAsync();
            var sw = new Stopwatch();

            context._tcpTestClient.CreateTcpConnection(
                context,
                connectionEstablished: conn => {
                context.Log.Information("[{remoteEndPoint}, L{localEndPoint}]: Writing...", conn.RemoteEndPoint,
                                        conn.LocalEndPoint);
                var writeDto = new WriteEvents(
                    eventStreamId,
                    expectedVersion,
                    new[] {
                    new NewEvent(Guid.NewGuid().ToByteArray(),
                                 "TakeSomeSpaceEvent",
                                 isJson ? 1 : 0, 0,
                                 Helper.UTF8NoBom.GetBytes(data),
                                 Helper.UTF8NoBom.GetBytes(metadata ?? string.Empty))
                },
                    false);
                var package = new TcpPackage(TcpCommand.WriteEvents,
                                             login == null ? TcpFlags.None : TcpFlags.Authenticated,
                                             Guid.NewGuid(),
                                             login,
                                             pass,
                                             writeDto.Serialize()).AsByteArray();
                sw.Start();
                conn.EnqueueSend(package);
            },
                handlePackage: (conn, pkg) => {
                sw.Stop();
                context.Log.Information("Write request took: {elapsed}.", sw.Elapsed);

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

                var dto = pkg.Data.Deserialize <WriteEventsCompleted>();
                if (dto.Result == EventStore.Client.Messages.OperationResult.Success)
                {
                    context.Log.Information("Successfully written.");
                    PerfUtils.LogTeamCityGraphData(string.Format("{0}-latency-ms", Keyword),
                                                   (int)Math.Round(sw.Elapsed.TotalMilliseconds));
                    context.Success();
                }
                else
                {
                    context.Log.Information("Error while writing: {message} ({e}).", dto.Message, dto.Result);
                    context.Fail();
                }

                conn.Close();
            },
                connectionClosed: (connection, error) => context.Fail(reason: "Connection was closed prematurely."));

            context.WaitForCompletion();
            return(true);
        }