Kafka消费者rebalanced异常分析

2023年2月5日10:29:34

异常

org.apache.kafka.clients.consumer.CommitFailedException: Commit cannot 
be completed since the group has already rebalanced and assigned the 
partitions to another member. This means that the time between 
subsequent calls to poll() was longer than the configured 
max.poll.interval.ms, which typically implies that the poll loop is 
spending too much time message processing. You can address this either
 by increasing the session timeout or by reducing the maximum size of 
 batches returned in poll() with max.poll.records.

大概意思:该Consumer不能提交offset了,应为分区已经分配给另一个消费者。是因为你的两次poll的间隔(poll会发送心跳)大于你要报告给server的最大时间(max.poll.interval.ms)。解决办法是,要么增加超时时间,要么减少每次poll回来的消息个数(max.poll.records)。

参数解释

max.poll.interval.ms:两次poll的最大间隔时间,超过该时间则被kafka认为是挂掉了。
需要注意的是,如果我们采取的是先消费完拉取的任务,再提交offset的模式,要确保消费完这些任务的时间小于max.poll.interval.ms。
但是一般情况下我们采用的是另起线程或者线程池的方式消费消息。

session.timeout.ms:检测心跳的最长时间间隔。如果超过该时间没有检测到心跳,则认为该消费者挂掉。

poll(timeout):timeout的表示,如果主题中有消息,则拉取,否则等待timeout的时间后再拉取。调用poll方法的时候回发送心跳。

max.poll.records:最大拉取多少条记录。

:在0.10.1之后的版本中,poll会启动两个线程,一个用于拉取消息。一个用于专门上报心跳,间隔是heartbeat.interval.ms。注意这个时间要小于session.timeout.ms。
这样的好处是:就算消费消息的时间超过了max.poll.interval.ms,也会应为有专门的线程在上报心跳,而不会将该消费者移除。

  • 作者:qq_20846769
  • 原文链接:https://blog.csdn.net/qq_20846769/article/details/118660107
    更新时间:2023年2月5日10:29:34 ,共 1096 字。