如何在Java Kafka应用程序客户端中判断是否达到了max.poll.interval.ms?

huangapple go评论75阅读模式
英文:

How to know if max.poll.interval.ms is reached in java kafka application client?

问题

在 Kafka 达到 max.poll.interval.ms 并发生重新平衡时,是否会抛出异常?

英文:

Is there an exception thrown somewhere when kafka max.poll.interval.ms is reached and rebalance happens?

答案1

得分: 2

当您的消费者因长时间的poll()而被从消费者组中踢出时,您将收到一个CommitFailedException。根据文档

也有可能消费者会遇到“活锁”情况,即继续发送心跳,但没有取得任何进展。为了防止消费者在这种情况下无限期地保持其分区,我们提供了一个使用max.poll.interval.ms设置的生存检测机制。基本上,如果您不以至少与配置的最大间隔一样频繁地调用poll,那么客户端将主动离开组,以便另一个消费者可以接管其分区。当发生这种情况时,您可能会看到一个偏移提交失败(由commitSync()调用引发的CommitFailedException表示)。这是一个安全机制,确保只有组的活跃成员能够提交偏移量。因此,要保持在组中,您必须继续调用poll

因此,您可能会捕获到CommitFailedException。实际上,您可以继续调用poll(),直到重新平衡完成并且您的消费者重新加入消费者组。

英文:

Once your consumer gets kicked out of the consumer group due to long poll(), you will receive a CommitFailedException. According to the documentation:

> It is also possible that the consumer could encounter a "livelock"
> situation where it is continuing to send heartbeats, but no progress
> is being made. To prevent the consumer from holding onto its
> partitions indefinitely in this case, we provide a liveness detection
> mechanism using the max.poll.interval.ms setting. Basically if you
> don't call poll at least as frequently as the configured max interval,
> then the client will proactively leave the group so that another
> consumer can take over its partitions. When this happens, you may see
> an offset commit failure (as indicated by a CommitFailedException
> thrown from a call to commitSync()). This is a safety mechanism which
> guarantees that only active members of the group are able to commit
> offsets. So to stay in the group, you must continue to call poll.


Therefore, you could possibly catch CommitFailedException. Actually, you can keep calling poll() until re-balancing is complete and your consumer re-enters the consumer group.

huangapple
  • 本文由 发表于 2020年4月10日 00:13:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/61125555.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定