kafka protocol_error ai_generated true

org.apache.kafka.common.errors.InvalidFetchSizeException: 获取大小0无效

org.apache.kafka.common.errors.InvalidFetchSizeException: Fetch size 0 is invalid

ID: kafka/invalid-fetch-size

其他格式: JSON · Markdown 中文 · English
80%修复率
83%置信度
1证据数
2024-06-12首次发现

版本兼容性

版本状态引入弃用备注
Kafka 3.4.0 active
Kafka 3.5.0 active
Kafka 3.6.0 active

根因分析

消费者或跟随者获取请求指定了0字节的获取大小,代理协议不允许此操作。

English

Consumer or follower fetch request specified a fetch size of 0 bytes, which is not allowed by the broker protocol.

generic

官方文档

https://kafka.apache.org/documentation/#consumerconfigs_fetch.message.max.bytes

解决方案

  1. Set fetch.max.bytes to a positive value (e.g., 52428800) in consumer properties.
    Example:
    properties.put(ConsumerConfig.FETCH_MAX_BYTES_CONFIG, 52428800);
    properties.put(ConsumerConfig.MAX_PARTITION_FETCH_BYTES_CONFIG, 1048576);
    # Also ensure fetch.min.bytes is > 0
    properties.put(ConsumerConfig.FETCH_MIN_BYTES_CONFIG, 1);
  2. If using Kafka Streams, ensure 'fetch.max.bytes' is set in StreamsConfig.
    Command to check current config:
    kafka-consumer-groups --bootstrap-server localhost:9092 --group my-group --describe
    # If fetch.max.bytes is 0, override in properties:
    props.put(StreamsConfig.consumerPrefix(ConsumerConfig.FETCH_MAX_BYTES_CONFIG), 52428800);

无效尝试

常见但无效的做法:

  1. Set fetch.min.bytes to 1 instead of 0 50% 失败

    fetch.min.bytes controls minimum bytes before returning data, not the fetch size itself; setting to 1 may cause unnecessary polling.

  2. Disable fetch.max.bytes in consumer config 70% 失败

    Removing the config may default to 0 in some clients, causing the same error.

  3. Upgrade Kafka client to latest version 40% 失败

    The issue is often a misconfigured client, not a bug; upgrading may not fix if config is wrong.