bigywater.tech
常见报错 · 51

Kafka LEADER_NOT_AVAILABLE:4.3.1 上第一次往自动创建的 topic 发消息警告的是 UNKNOWN_TOPIC_OR_PARTITION,500 分区首条要 3 秒,advertised.listeners 配错时 8 秒超时

“LEADER_NOT_AVAILABLE” 在网上被说成 Kafka 第一次往新 topic 发消息的固定警告。这篇用 Kafka 4.3.1 在本机试了几种场景:自动创建的 topic、broker 重启、500 分区、关闭自动创建、advertised.listeners 配错。结果是——这个版本这些场景都没有出现 LEADER_NOT_AVAILABLE,而是另外几个错误码;每一种怎么认出来、什么时候该处理,都在下面。

UNKNOWN_TOPIC_OR_PARTITION
4.3.1 上第一次往自动创建的 topic 发消息,日志里的可恢复警告是它,不是 LEADER_NOT_AVAILABLE
3064 ms
num.partitions=500 时第一条消息的耗时(经历 NOT_LEADER_OR_FOLLOWER 重试);普通 topic 是 288 ms
8612 ms
advertised.listeners 配成解析不了的主机名时,第 1 次 send 失败的耗时(delivery.timeout.ms=8000)

自动创建

实测 · 本机执行输出
  已存在的 topic existing:
      第 1 次 send:成功 partition=0 offset=0,用时 286 ms
      第 2 次 send:成功 partition=0 offset=1,用时 8 ms
  不存在的 topic fresh1(自动创建):
      LOG WARN NetworkClient: [Producer clientId=producer-1] The metadata response from the cluster reported a recoverable issue with correlation id N : {fresh1=UNKNOWN_TOPIC_OR_PARTITION}
      第 1 次 send:成功 partition=0 offset=0,用时 288 ms
      第 2 次 send:成功 partition=0 offset=1,用时 7 ms
  再来一个不存在的 fresh2:
      LOG WARN NetworkClient: [Producer clientId=producer-1] The metadata response from the cluster reported a recoverable issue with correlation id N : {fresh2=UNKNOWN_TOPIC_OR_PARTITION}
      第 1 次 send:成功 partition=0 offset=0,用时 280 ms
      第 2 次 send:成功 partition=0 offset=1,用时 8 ms
  自动创建出来的 topic 的分区数与副本数:Partition: 0 Leader: 1 Replicas: 1 Isr: 1 Elr: LastKnownElr:

首条消息慢主要是第一次拿元数据,自动创建只多几毫秒,WARN 可忽略。

broker 重启

实测 · 本机执行输出
  broker 刚重启(没有等它就绪)就开始发 existing,max.block.ms=30000:
      LOG WARN NetworkClient: [Producer clientId=producer-1] Connection to node -1 (/127.0.0.1:9580) could not be established. Node may not be available.  (出现 5 次)
      LOG WARN NetworkClient: [Producer clientId=producer-1] Bootstrap broker 127.0.0.1:9580 (id: -1 rack: null isFenced: false) disconnected  (出现 5 次)
      第 1 次 send:成功 partition=0 offset=2,用时 2275 ms
      第 2 次 send:成功 partition=0 offset=3,用时 8 ms

连不上 broker 是 “Node may not be available”,与 leader 无关。

分区很多

实测 · 本机执行输出
  num.partitions=500,不存在的 topic wide0:
      LOG WARN NetworkClient: [Producer clientId=producer-1] The metadata response from the cluster reported a recoverable issue with correlation id N : {wide0=UNKNOWN_TOPIC_OR_PARTITION}
      LOG WARN Sender: [Producer clientId=producer-1] Got error produce response with correlation id N on topic-partition wide0-329, retrying (2147483646 attempts left). Error: NOT_LEADER_OR_FOLLOWER
      LOG WARN Sender: [Producer clientId=producer-1] Received invalid metadata error in produce request on partition wide0-329 due to org.apache.kafka.common.errors.NotLeaderOrFollowerException: For requests intended only for the leader, this error indicates that the broker is not the current leader. For requests intended for any rep  (出现 5 次)
      LOG WARN Sender: [Producer clientId=producer-1] Got error produce response with correlation id N on topic-partition wide0-329, retrying (2147483645 attempts left). Error: NOT_LEADER_OR_FOLLOWER
      LOG WARN Sender: [Producer clientId=producer-1] Got error produce response with correlation id N on topic-partition wide0-329, retrying (2147483644 attempts left). Error: NOT_LEADER_OR_FOLLOWER
      LOG WARN Sender: [Producer clientId=producer-1] Got error produce response with correlation id N on topic-partition wide0-329, retrying (2147483643 attempts left). Error: NOT_LEADER_OR_FOLLOWER
      LOG WARN Sender: [Producer clientId=producer-1] Got error produce response with correlation id N on topic-partition wide0-329, retrying (2147483642 attempts left). Error: NOT_LEADER_OR_FOLLOWER
      第 1 次 send:成功 partition=329 offset=0,用时 3064 ms
      第 2 次 send:成功 partition=329 offset=1,用时 8 ms
  num.partitions=500,不存在的 topic wide1:
      LOG WARN NetworkClient: [Producer clientId=producer-1] The metadata response from the cluster reported a recoverable issue with correlation id N : {wide1=UNKNOWN_TOPIC_OR_PARTITION}
      LOG WARN Sender: [Producer clientId=producer-1] Got error produce response with correlation id N on topic-partition wide1-339, retrying (2147483646 attempts left). Error: NOT_LEADER_OR_FOLLOWER
      LOG WARN Sender: [Producer clientId=producer-1] Received invalid metadata error in produce request on partition wide1-339 due to org.apache.kafka.common.errors.NotLeaderOrFollowerException: For requests intended only for the leader, this error indicates that the broker is not the current leader. For requests intended for any rep  (出现 5 次)
      LOG WARN Sender: [Producer clientId=producer-1] Got error produce response with correlation id N on topic-partition wide1-339, retrying (2147483645 attempts left). Error: NOT_LEADER_OR_FOLLOWER
      LOG WARN Sender: [Producer clientId=producer-1] Got error produce response with correlation id N on topic-partition wide1-339, retrying (2147483644 attempts left). Error: NOT_LEADER_OR_FOLLOWER
      LOG WARN Sender: [Producer clientId=producer-1] Got error produce response with correlation id N on topic-partition wide1-339, retrying (2147483643 attempts left). Error: NOT_LEADER_OR_FOLLOWER
      LOG WARN Sender: [Producer clientId=producer-1] Got error produce response with correlation id N on topic-partition wide1-339, retrying (2147483642 attempts left). Error: NOT_LEADER_OR_FOLLOWER
      第 1 次 send:成功 partition=339 offset=0,用时 2934 ms
      第 2 次 send:成功 partition=339 offset=1,用时 8 ms
  num.partitions=500,不存在的 topic wide2:
      LOG WARN NetworkClient: [Producer clientId=producer-1] The metadata response from the cluster reported a recoverable issue with correlation id N : {wide2=UNKNOWN_TOPIC_OR_PARTITION}
      LOG WARN Sender: [Producer clientId=producer-1] Got error produce response with correlation id N on topic-partition wide2-472, retrying (2147483646 attempts left). Error: NOT_LEADER_OR_FOLLOWER
      LOG WARN Sender: [Producer clientId=producer-1] Received invalid metadata error in produce request on partition wide2-472 due to org.apache.kafka.common.errors.NotLeaderOrFollowerException: For requests intended only for the leader, this error indicates that the broker is not the current leader. For requests intended for any rep  (出现 5 次)
      LOG WARN Sender: [Producer clientId=producer-1] Got error produce response with correlation id N on topic-partition wide2-472, retrying (2147483645 attempts left). Error: NOT_LEADER_OR_FOLLOWER
      LOG WARN Sender: [Producer clientId=producer-1] Got error produce response with correlation id N on topic-partition wide2-472, retrying (2147483644 attempts left). Error: NOT_LEADER_OR_FOLLOWER
      LOG WARN Sender: [Producer clientId=producer-1] Got error produce response with correlation id N on topic-partition wide2-472, retrying (2147483643 attempts left). Error: NOT_LEADER_OR_FOLLOWER
      LOG WARN Sender: [Producer clientId=producer-1] Got error produce response with correlation id N on topic-partition wide2-472, retrying (2147483642 attempts left). Error: NOT_LEADER_OR_FOLLOWER
      第 1 次 send:成功 partition=472 offset=0,用时 3342 ms
      第 2 次 send:成功 partition=472 offset=1,用时 14 ms
  wide0 实际的分区数:500

新建 500 分区的 topic,首条消息约 3 秒,过程是 NOT_LEADER_OR_FOLLOWER 重试。

不自动创建

实测 · 本机执行输出
  auto.create.topics.enable=false,max.block.ms=3000:
      LOG WARN NetworkClient: [Producer clientId=producer-1] The metadata response from the cluster reported a recoverable issue with correlation id N : {fresh3=UNKNOWN_TOPIC_OR_PARTITION}  (出现 9 次)
      第 1 次 send:失败 TimeoutException: Topic fresh3 not present in metadata after 3000 ms.,用时 3007 ms
      第 2 次 send:失败 TimeoutException: Topic fresh3 not present in metadata after 3000 ms.,用时 3003 ms
  手工创建 fresh3 之后:
      第 1 次 send:成功 partition=0 offset=0,用时 206 ms
      第 2 次 send:成功 partition=0 offset=1,用时 8 ms

topic 不存在时等到 max.block.ms 抛 TimeoutException。

advertised.listeners

实测 · 本机执行输出
  advertised.listeners=kafka-broker-1.invalid:9580,max.block.ms=4000:
      LOG WARN NetworkClient: [Producer clientId=producer-1] The metadata response from the cluster reported a recoverable issue with correlation id N : {existing=UNKNOWN_TOPIC_OR_PARTITION}
      LOG WARN NetworkClient: [Producer clientId=producer-1] Error connecting to node kafka-broker-1.invalid:9580 (id: N rack: null isFenced: false)  (出现 21 次)
      java.net.UnknownHostException: kafka-broker-1.invalid: nodename nor servname provided, or not known  (出现 2 次)
      java.net.UnknownHostException: kafka-broker-1.invalid  (出现 19 次)
      第 1 次 send:失败 TimeoutException: Expiring 1 record(s) for existing-0:8001 ms has passed since batch creation. The request has not been sent, or no server response has been received yet.,用时 8612 ms
      第 2 次 send:失败 TimeoutException: Expiring 1 record(s) for existing-0:8002 ms has passed since batch creation. The request has not been sent, or no server response has been received yet.,用时 8001 ms
  从 bootstrap 能连上:127.0.0.1:9580 端口是通的

bootstrap 能连但元数据地址不通,日志是 Error connecting to node 某主机,超时来自 delivery.timeout.ms。

分步演示

首次发送
本步的对象

    本文用到的工具与边界

    • 本机 macOS 27 arm64;报错文本都是真实复现时原样捕获的(命令行工具统一用 LC_ALL=C 取英文原文,路径换成 );所有服务都是本机上自己起的、只监听 127.0.0.1,不访问外网;计时用 time.perf_counter,数字每次运行会有小幅抖动。本篇用 Homebrew Kafka 4.3.1(KRaft 单节点,127.0.0.1:9580)、kafka-clients 4.3.1、OpenJDK 21.0.13;客户端日志只保留 org.apache.kafka.clients 的 WARN 以上,重复的堆栈已折叠、相同日志行合并计数;每种场景都用新的 broker 实例和数据目录
    • 没有验证:在 4.3.1 上没有任何一种场景复现出 LEADER_NOT_AVAILABLE,所以它在什么条件下出现(多 broker、leader 选举、旧版本 Kafka 或 ZooKeeper 模式)本篇不能回答;多 broker 集群里 leader 迁移和 ISR 变化的表现;Python / librdkafka 客户端的错误码文字(那里可能显示 Broker: Leader not available);SASL / TLS 下 advertised.listeners 配错的表现;容器和云上的内外网地址配置
    京ICP备2026067502号-1京公网安备11010502063487号