1.報錯背景
服務器:Linux集群 3台,32GB內存;
Kafka創建了大約140個Topic,和與Topic數目相同的Consumer,設置分區數量不祥;
每個Consumer都在GroupId,沒有單獨的standalone consumer;
項目在運行過程中突然出現了大量分區提交失敗的現象。
2.報錯現象
報錯截圖
報錯日志(保密原因,groupId做過處理)
2020-04-28 at 16:59:36 CST traceId:[] ERROR org.apache.kafka.clients.consumer.internals.ConsumerCoordinator$OffsetCommitResponseHandler 867 handle - [Consumer clientId=consumer-1158, groupId=144rrr4001020008SYS18] Offset commit failed on partition 001020008SYS18-3 at offset 594: The coordinator is not aware of this member. 2020-04-28 at 16:59:37 CST traceId:[] ERROR org.apache.kafka.clients.consumer.internals.ConsumerCoordinator$OffsetCommitResponseHandler 867 handle - [Consumer clientId=consumer-1330, groupId=144rrr4001020001SYS01] Offset commit failed on partition 001020001SYS01-1 at offset 12405: The coordinator is not aware of this member. 2020-04-28 at 16:59:37 CST traceId:[] ERROR org.apache.kafka.clients.consumer.internals.ConsumerCoordinator$OffsetCommitResponseHandler 867 handle - [Consumer clientId=consumer-93, groupId=144rrr4001020012SYS02] Offset commit failed on partition 001020012SYS02-0 at offset 957: The coordinator is not aware of this member. 2020-04-28 at 16:59:37 CST traceId:[] ERROR org.apache.kafka.clients.consumer.internals.ConsumerCoordinator$OffsetCommitResponseHandler 867 handle - [Consumer clientId=consumer-872, groupId=144rrr4001020010SYS20] Offset commit failed on partition 001020010SYS20-3 at offset 982: The coordinator is not aware of this member. 2020-04-28 at 16:59:37 CST traceId:[] ERROR org.apache.kafka.clients.consumer.internals.ConsumerCoordinator$OffsetCommitResponseHandler 867 handle - [Consumer clientId=consumer-16, groupId=144rrr4001020012SYS03] Offset commit failed on partition 001020012SYS03-0 at offset 1045: The coordinator is not aware of this member. 2020-04-28 at 16:59:37 CST traceId:[] ERROR org.apache.kafka.clients.consumer.internals.ConsumerCoordinator$OffsetCommitResponseHandler 867 handle - [Consumer clientId=consumer-820, groupId=144rrr4001020010SYS20] Offset commit failed on partition 001020010SYS20-3 at offset 982: The coordinator is not aware of this member. 2020-04-28 at 16:59:37 CST traceId:[] ERROR org.apache.kafka.clients.consumer.internals.ConsumerCoordinator$OffsetCommitResponseHandler 867 handle - [Consumer clientId=consumer-912, groupId=144rrr4001020003SYS01] Offset commit failed on partition 001020003SYS01-1 at offset 12387: The coordinator is not aware of this member. 2020-04-28 at 16:59:37 CST traceId:[] ERROR org.apache.kafka.clients.consumer.internals.ConsumerCoordinator$OffsetCommitResponseHandler 867 handle - [Consumer clientId=consumer-719, groupId=144rrr4001020002SYS01] Offset commit failed on partition 001020002SYS01-3 at offset 5274: The coordinator is not aware of this member. 2020-04-28 at 16:59:37 CST traceId:[] ERROR org.apache.kafka.clients.consumer.internals.ConsumerCoordinator$OffsetCommitResponseHandler 867 handle - [Consumer clientId=consumer-250, groupId=144rrr4001020006SYS18] Offset commit failed on partition 001020006SYS18-2 at offset 312: The coordinator is not aware of this member. 2020-04-28 at 16:59:37 CST traceId:[] ERROR org.apache.kafka.clients.consumer.internals.ConsumerCoordinator$OffsetCommitResponseHandler 867 handle - [Consumer clientId=consumer-66, groupId=144rrr4001020012SYS02] Offset commit failed on partition 001020012SYS02-0 at offset 957: The coordinator is not aware of this member. 2020-04-28 at 16:59:37 CST traceId:[] ERROR org.apache.kafka.clients.consumer.internals.ConsumerCoordinator$OffsetCommitResponseHandler 867 handle - [Consumer clientId=consumer-277, groupId=144rrr4001020004SYS03] Offset commit failed on partition 001020004SYS03-3 at offset 390: The coordinator is not aware of this member. 2020-04-28 at 16:59:37 CST traceId:[] ERROR org.apache.kafka.clients.consumer.internals.ConsumerCoordinator$OffsetCommitResponseHandler 867 handle - [Consumer clientId=consumer-880, groupId=144rrr4001020010SYS20] Offset commit failed on partition 001020010SYS20-3 at offset 982: The coordinator is not aware of this member. 2020-04-28 at 16:59:37 CST traceId:[] ERROR org.apache.kafka.clients.consumer.internals.ConsumerCoordinator$OffsetCommitResponseHandler 867 handle - [Consumer clientId=consumer-488, groupId=144rrr4001020009SYS01] Offset commit failed on partition 001020009SYS01-2 at offset 7875: The coordinator is not aware of this member.
3.報錯原因
依據網上資料進行大膽猜測:
第一步
分析觸發報錯的直接原因:
Offset commit failed on partition 001020004SYS03-3 at offset 390: The coordinator is not aware of this member.
翻譯過來:Offset提交失敗,Kafka的group協調器不認識該成員。
第二步
分析造成Kafka的group協調器不認識該成員的原因:
參考資料:https://www.cnblogs.com/yoke/p/11405397.html
(1)Consumer中出現單獨的standalone consume,而且standalone consume的名字和某個GroupId名字相同,這種現象就會觸發報錯,具體原因我未深究,因為本項目沒有standalone consume存在;
(2)組成員“崩潰”,造成kafka被動Rebalance,這樣就會觸發The coordinator is not aware of this member.報錯,我猜測我的報錯可能就是這個原因。
因為在崩潰時成員並不會主動地告知coordinator此事,coordinator有可能需要一個完整的session.timeout周期(心跳周期)才能檢測到這種崩潰,這必然會造成consumer的滯后。可以說離開組是主動地發起rebalance;而崩潰則是被動地發起rebalance。
第三步
分析造成組成員“崩潰”的原因:
參考資料:https://blog.csdn.net/u013256816/article/details/82804525
他這里面的原因和我問題的原因可能不是相同,但是也可以作為一個排除點來看一下。
查看Linux的軟硬連接限制數:
ulimit -Hn
ulimit -Sn
查看kafka創建鏈接數:
ls /proc/[kafka進程號]/fd | wc -l
看是否超出了Linux的最大限制
第四步
后來發現上面的原因分析有點走偏了,都開始找系統的錯誤了,肯定是我的某些地方做錯了。
再分析,分析造成Kafka的group協調器不認識該成員的原因:
很有可能是因為心跳檢測已經檢測不到它了,所以小組管理員就默認它死掉了,但是它真的死了嗎?可能還沒有。有可能只是它在忙別的事情,沒聽到組長叫它,所以就認定死亡了,繼而又被活埋了。造成了自動提交offset失敗。
第五步
分析心跳檢測失敗的原因:
一般來說producer的生產消息的邏輯速度都會比consumer的消費消息的邏輯速度快,當producer在短時間內產生大量的數據丟進kafka的broker里面時,kafka的consumer會出現以下行為:
(1)kafka的consumer會從broker里面取出一批數據,給消費線程進行消費;
(2)由於取出的一批消息數量太大,consumer在session.timeout.ms
時間之內沒有消費完成;
(3)consumer coordinator
會由於沒有接受到心跳而掛掉,並且出現一些日志(報錯日志在上面);
(4)日志的意思大概是coordinator
掛掉了,然后自動提交offset失敗,然后重新分配partition給客戶端;
(5)由於自動提交offset失敗,導致重新分配了partition的客戶端又重新消費之前的一批數據;
(6)接着consumer重新消費,又出現了消費超時,無限循環下去。
這應該是報錯的一個重要原因。
4.報錯解決
第一步
查看kafka的進程號
第二步
進入kafka的進程目錄查看:
cd /proc/11298/fd
可以看到里面有很多軟連接,而且絕大多數都是失效的軟連接,這樣就很懵逼了,不知道這是什么操作。但是可以看到里面還有幾個正常的日志文件,挨個打開看一下。
第三步
查看log文件,看看有沒有異常
查看controller.log
第四步
上面這條路走不通了! 換了一條路...
針對上面原因第五步,提出優化方案:
(1)配置方面,要使consumer和patition一一對應,如果有多個patition,最好要均衡每個patition的數據量,防止出現數據傾斜,導致某個consumer掛掉;
(2)配置方面,提高了partition的數量,從而提高了consumer的並行能力,從而提高數據的消費能力;
(3)配置方面,auto.commit.interval.ms 大一點,但是治標不治本;
(4)對於單partition的消費線程,增加了一個固定長度的阻塞隊列和工作線程池進一步提高並行消費的能力(未驗證過);
(5)代碼方面,把kafka-client的enable.auto.commit
設置成了false,表示禁止kafka-client自動提交offset,下面是我們項目中的自動提交設置,把它設置成flase,很有可能是一個優化點,但是這里設置成手動提交可能會有未知錯誤產生,要驗證一下。