“LEADER_NOT_AVAILABLE” 在网上被说成 Kafka 第一次往新 topic 发消息的固定警告。这篇用 Kafka 4.3.1 在本机试了几种场景:自动创建的 topic、broker 重启、500 分区、关闭自动创建、advertised.listeners 配错。结果是——这个版本这些场景都没有出现 LEADER_NOT_AVAILABLE,而是另外几个错误码;每一种怎么认出来、什么时候该处理,都在下面。
已存在的 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 刚重启(没有等它就绪)就开始发 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 mstopic 不存在时等到 max.block.ms 抛 TimeoutException。
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。