简体   繁体   English

随机分区器不会在 Kafka 主题分区之间分发消息

[英]Random partitioner does not distribute messages between Kafka topic partitions

I've created a topic in Kafka with 9 partitions, naming it aptly 'test', and knocked together two simple applications in C# (.NET Core), using Confluent.Kafka client library: a producer and a consumer.我在 Kafka 中创建了一个具有 9 个分区的主题,将其命名为“测试”,并使用Confluent.Kafka客户端库将两个简单的 C#(.NET Core)应用程序组合在一起:生产者和消费者。 I did little more than tweak examples from the documentation .我所做的只是调整文档中的示例

I am running two instances of the consumer application and one instance of the producer.我正在运行消费者应用程序的两个实例和生产者的一个实例。 I don't see much point in pasting the consumer code here, it's a trivial 'get a message, print it on screen' app, however, it does also print the number of the partition the message came from.我认为在此处粘贴消费者代码没有多大意义,这是一个微不足道的“获取消息,在屏幕上打印”应用程序,但是,它还打印消息来自的分区编号。

This is the producer app:这是生产者应用程序:

    static async Task Main(string[] args)
    {
        var random = new Random();

        var config = new ProducerConfig {
            BootstrapServers = "10.0.0.5:9092",
            Partitioner = Partitioner.ConsistentRandom
        };

        int counter = 0;
        while (true)
        {
            using (var p = new ProducerBuilder<string, string>(config).Build())
            {
                try
                {
                    p.BeginProduce(
                        "test",
                        new Message<string, string>
                        {
                            //Key = random.Next().ToString(),
                            Value = $"test {++counter}"
                        });

                    if (counter % 10 == 0)
                        p.Flush();
                }
                catch (ProduceException<Null, string> e)
                {
                    Console.WriteLine($"Delivery failed: {e.Error.Reason}");
                }
            }
        }
    }

Problem: If the Key property of the message is not set, all messages get sent to the partition number 7, meaning that one of my consumer instances is idle.问题:如果未设置消息的Key属性,则所有消息都会发送到分区号 7,这意味着我的消费者实例之一处于空闲状态。 I had to manually randomise the key in order to distribute them between partitions (see the commented out line).我必须手动随机化密钥才能在分区之间分配它们(请参阅注释掉的行)。 (The original code, as copied from the docs, used Null as the type of the key, and this sent all messages to the 7th partition too.) (从文档中复制的原始代码使用Null作为键的类型,这也将所有消息发送到第 7 个分区。)

Why is that?这是为什么? According to the documentation of the ProducerConfig.Partitioner property, the consistent_random option should ensure random distribution if the key is not specified.根据ProducerConfig.Partitioner属性的文档,如果未指定密钥,则consistent_random选项应确保随机分布。 I tried using the Partioner.Random option, which should use random distribution regardless of the key, but this did not help.我尝试使用Partioner.Random选项,它应该使用随机分布而不管密钥,但这没有帮助。

Is this the expected behaviour, am I doing something wrong, or did I come across a bug?这是预期的行为,我做错了什么,还是我遇到了错误?

I am using version 1.0.0-RC2 of Confluent.Kafka NuGet.我正在使用 Confluent.Kafka NuGet 的 1.0.0-RC2 版本。

Complete documentation of the Partitioner config:分区器配置的完整文档:

// Summary:
//     Partitioner: `random` - random distribution, `consistent` - CRC32 hash of key
//     (Empty and NULL keys are mapped to single partition), `consistent_random` - CRC32
//     hash of key (Empty and NULL keys are randomly partitioned), `murmur2` - Java
//     Producer compatible Murmur2 hash of key (NULL keys are mapped to single partition),
//     `murmur2_random` - Java Producer compatible Murmur2 hash of key (NULL keys are
//     randomly partitioned. This is functionally equivalent to the default partitioner
//     in the Java Producer.). default: consistent_random importance: high

I encountered the same issue.我遇到了同样的问题。 Seems like when initiating a client, the first message will always go the same partition.似乎在启动客户端时,第一条消息将始终位于同一个分区。 The Partioner.Random will work if you use the same client for all your messages如果您对所有消息使用相同的客户端,则 Partioner.Random 将起作用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM