コード例 #1
0
        public static void WatermarkOffsets(string bootstrapServers, string singlePartitionTopic, string partitionedTopic)
        {
            LogToFile("start WatermarkOffsets");

            var producerConfig = new ProducerConfig {
                BootstrapServers = bootstrapServers
            };

            var testString = "hello world";

            DeliveryReport <Null, string> dr;

            using (var producer = new Producer <Null, string>(producerConfig))
                using (var adminClient = new AdminClient(producer.Handle))
                {
                    dr = producer.ProduceAsync(singlePartitionTopic, new Message <Null, string> {
                        Value = testString
                    }).Result;
                    Assert.Equal(0, producer.Flush(TimeSpan.FromSeconds(10))); // this isn't necessary.

                    var queryOffsets = adminClient.QueryWatermarkOffsets(new TopicPartition(singlePartitionTopic, 0), TimeSpan.FromSeconds(20));
                    Assert.NotEqual(queryOffsets.Low, Offset.Invalid);
                    Assert.NotEqual(queryOffsets.High, Offset.Invalid);

                    // TODO: can anything be said about the high watermark offset c.f. dr.Offset?
                    //       I have seen queryOffsets.High < dr.Offset and also queryOffsets.High = dr.Offset + 1.
                    //       The former only once (or was I in error?). request.required.acks has a default value
                    //       of 1, so with only one broker, I assume the former should never happen.
                    // Console.WriteLine($"Query Offsets: [{queryOffsets.Low} {queryOffsets.High}]. DR Offset: {dr.Offset}");
                    Assert.True(queryOffsets.Low < queryOffsets.High);
                }

            var consumerConfig = new ConsumerConfig
            {
                GroupId          = Guid.NewGuid().ToString(),
                BootstrapServers = bootstrapServers,
                SessionTimeoutMs = 6000
            };

            using (var consumer = new Consumer <byte[], byte[]>(consumerConfig))
                using (var adminClient = new AdminClient(consumer.Handle))
                {
                    consumer.Assign(new List <TopicPartitionOffset>()
                    {
                        dr.TopicPartitionOffset
                    });
                    var record = consumer.Consume(TimeSpan.FromSeconds(10));
                    Assert.NotNull(record.Message);

                    var getOffsets = adminClient.GetWatermarkOffsets(dr.TopicPartition);
                    Assert.Equal(getOffsets.Low, Offset.Invalid);
                    // the offset of the next message to be read.
                    Assert.Equal(getOffsets.High, dr.Offset + 1);

                    var queryOffsets = adminClient.QueryWatermarkOffsets(dr.TopicPartition, TimeSpan.FromSeconds(20));
                    Assert.NotEqual(queryOffsets.Low, Offset.Invalid);
                    Assert.Equal(getOffsets.High, queryOffsets.High);
                }

            Assert.Equal(0, Library.HandleCount);
            LogToFile("end   WatermarkOffsets");
        }