コード例 #1
0
        public int Start(BatchedConsumerOptions opts)
        {
            var histogram = Metric.Histogram("batch.size", Unit.Items);
            var timer     = Metric.Timer("message.latency", Unit.Events);

            Metric.Config.WithReporting(r => r.WithConsoleReport(TimeSpan.FromSeconds(5)));

            using (var consumer = new Kafka.Basic.BatchedConsumer(opts.ZkConnect, opts.Group, opts.Topic, opts.Threads, opts.BatchTimeoutMs))
            {
                ListenToConsole(consumer);

                consumer
                .Start(m =>
                {
                    var time = DateTime.UtcNow.Ticks;
                    var list = m.ToList();
                    list
                    .ForEach(message =>
                    {
                        var value = long.Parse(message.Value);
                        var diff  = (time - value) / 10000;
                        timer.Record(diff, TimeUnit.Milliseconds);
                    });
                    histogram.Update(list.Count);
                });
            }

            return(0);
        }
コード例 #2
0
        public int Start(ChaosMonkeyOptions opts)
        {
            var histogram = Metric.Histogram("batch.size", Unit.Items);
            var timer     = Metric.Timer("message.latency", Unit.Events);

            var random           = new Random();
            var exceptionModulus = random.Next(76, 100);
            var waitModulus      = random.Next(50, 75);

            using (var consumer = new Kafka.Basic.BatchedConsumer(opts.ZkConnect, opts.Group, opts.Topic, opts.Threads, opts.BatchTimeoutMs, opts.MaxBatchSize))
            {
                var call = 0;
                consumer
                .Start(
                    m =>
                {
                    var time = DateTime.UtcNow.Ticks;

                    call++;
                    var list = m.ToList();

                    WriteLog($"Received batch {call} of {list.Count} messages:");

                    list
                    .ForEach(message =>
                    {
                        var value = long.Parse(message.Value);
                        var diff  = (time - value) / 10000;
                        timer.Record(diff, TimeUnit.Milliseconds);
                        WriteLog($"P:{message.Partition},O:{message.Offset},L:{diff}ms");
                    });

                    if (call % exceptionModulus == 0)
                    {
                        WriteLog($"Faking an exception to test shutdown behaviour... Expect to see the above {list.Count} messages come through again.");
                        throw new ChaosException();
                    }

                    if (call % waitModulus == 0)
                    {
                        WriteLog("Faking a long consume operation by sending the current thread to sleep for 5s.");
                        Thread.Sleep(5000);
                    }

                    histogram.Update(list.Count);
                },
                    e =>
                {
                    if (e is ChaosException)
                    {
                        WriteLog("Chaos achieved and handled.");
                    }
                    else
                    {
                        WriteLog("Got an error... ", e);
                    }
                });
            }

            return(0);
        }