【线上】记一次kafka排查过程

迷南。 2022-09-07 12:23 448阅读 0赞

问题

上线后发现,经常有个页面显示的信息有缺失,研究代码发现只有收不到kafka消息时,才会这样

排查

消费端等待30s,原则上时间上是比较充足的,后面确认kafka的服务器确实没有收到相关消息,这下子直接把问题指向了生产者了

生产者加上回调,发现发送报错了:org.apache.kafka.common.errors.TimeoutException: Failed to update meta after 0ms

分析线上生产者参数如下:

  1. configs.put("max.block.ms", 0);

分析源码:

  1. private Future<RecordMetadata> doSend(ProducerRecord<K, V> record, Callback callback) {
  2. TopicPartition tp = null;
  3. try {
  4. throwIfProducerClosed();
  5. // first make sure the metadata for the topic is available
  6. ClusterAndWaitTime clusterAndWaitTime;
  7. try {
  8. // 这里会传入这个参数maxBlockTimeMs
  9. // 实际上this.maxBlockTimeMs = config.getLong(ProducerConfig.MAX_BLOCK_MS_CONFIG);
  10. clusterAndWaitTime = waitOnMetadata(record.topic(), record.partition(), maxBlockTimeMs);
  11. } catch (KafkaException e) {
  12. if (metadata.isClosed())
  13. throw new KafkaException("Producer closed while send in progress", e);
  14. throw e;
  15. }
  16. ......
  17. private ClusterAndWaitTime waitOnMetadata(String topic, Integer partition, long maxWaitMs) throws InterruptedException {
  18. ......
  19. try {
  20. metadata.awaitUpdate(version, remainingWaitMs);
  21. } catch (TimeoutException ex) {
  22. // Rethrow with original maxWaitMs to prevent logging exception with remainingWaitMs
  23. // 在这里会发生超时报错
  24. throw new TimeoutException("Failed to update metadata after " + maxWaitMs + " ms.");
  25. }
  26. .....

即更新元数据时超过max.block.ms=0(默认值是60s)的时间了。

max.block.ms: 最长阻塞时间。当producer获取元数据等待时间、或缓存满了允许阻塞的时间,超过时间会抛出异常。

继续分析线上的情况,这个页面是由于运营人员在界面操作时触发kafka的发送和接收、进而界面展示,但这个功能不是经常用,导致超过一段时间后,连接断开了,这时候界面操作时,第一次send会花更多的时间,导致超时。

最终解决:

  1. // 主要是这个参数
  2. configs.put("max.block.ms", "1000");
  3. // 默认是5分钟
  4. configs.put("metadata.max.age.ms", 600_000);

发表评论

表情:
评论列表 (有 0 条评论,448人围观)

还没有评论,来说两句吧...

相关阅读