卡夫卡生产者无法发送带有NOT_LEADER_FOR_PARTITION例外的消息

时间:2020-05-14 13:27:08

标签: java apache-kafka spring-cloud-stream

我们正在使用spring-cloud-stream-binder-kafka(3.0.3.RELEASE)将消息发送到我们的Kafka集群(2.4.1)。生产者线程不时地收到NOT_LEADER_FOR_PARTITION异常,甚至超过重试次数(当前设置为12,由依赖项spring-retry激活)。我们限制了重试,因为我们正在以大约1k msg / s(每个生产者实例)的速度发送数据,并且担心缓冲区的大小。 这样,我们会定期丢失消息,这对下游用户不利,因为我们不能简单地重现传入的流量。

错误消息是


[Producer clientId=producer-5] Received invalid metadata error in produce request on partition topic-21 due to org.apache.kafka.common.errors.NotLeaderForPartitionException: This server is not the leader for that topic-partition.. Going to request metadata update now
[Producer clientId=producer-5] Got error produce response with correlation id 974706 on topic-partition topic-21, retrying (8 attempts left). Error: NOT_LEADER_FOR_PARTITION
[Producer clientId=producer-5] Got error produce response with correlation id 974707 on topic-partition topic-21, retrying (1 attempts left). Error: NOT_LEADER_FOR_PARTITION

有什么已知的方法可以避免这种情况? 我们应该回到默认的MAX_INT重试吗? 即使它以NOT_LEADER_FOR_PARTITION作为响应,为什么仍继续发送给同一代理?

欢迎任何提示。

编辑:我们刚刚注意到经纪人指标kafka_network_requestmetrics_responsequeuetimems在该时间左右上升,但我们看到的最大值约为2.5s

1 个答案:

答案 0 :(得分:2)

Produce和Fetch请求都发送到分区的领导副本。 NotLeaderForPartitionException:将请求发送到现在不是该分区的领导者副本的分区时,引发该异常。

客户端将有关每个分区的领导者的信息保留为高速缓存。缓存管理的完整过程如下所示。

enter image description here

客户端需要通过在生产者配置中设置CreateObject("MSProject.Project")来刷新此信息。该标签的默认值为300000 ms

您可以阅读以下Apache Kafka文档。

https://kafka.apache.org/documentation/

请检查Sender.java代码。

https://github.com/a0x8o/kafka/blob/master/clients/src/main/java/org/apache/kafka/clients/producer/internals/Sender.java

您将在发件人代码中找到两条错误消息。 metadata.max.age.ms的默认值为3秒。我认为您应该降低此值,然后观察其行为。