kafka protocol_error ai_generated true

org.apache.kafka.common.errors.TransactionalCoordinatorFencedException: 纪元 5 的事务协调器已被较新的纪元 6 隔离

org.apache.kafka.common.errors.TransactionalCoordinatorFencedException: The transactional coordinator with epoch 5 has been fenced by a newer epoch 6

ID: kafka/transactional-coordinator-fenced

其他格式: JSON · Markdown 中文 · English
82%修复率
84%置信度
1证据数
2023-06-05首次发现

版本兼容性

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

根因分析

事务协调器被具有更高纪元的新的协调器替换(例如,在领导变更后),导致旧协调器的请求被拒绝为隔离状态。

English

A transactional coordinator was replaced by a new coordinator with a higher epoch (e.g., after a leader change), causing the old coordinator's requests to be rejected as fenced.

generic

官方文档

https://kafka.apache.org/documentation/#transactional_id

解决方案

  1. Handle the exception in the producer by retrying the transaction with a new transactional ID or by resetting the producer:
    
    producer.initTransactions();
    try {
        producer.beginTransaction();
        // Send messages
        producer.commitTransaction();
    } catch (TransactionalCoordinatorFencedException e) {
        // Close and recreate the producer to get a fresh coordinator
        producer.close();
        producer = createNewProducer();
        producer.initTransactions();
        // Retry the transaction
    }
  2. Ensure the transactional.id is unique per producer instance and that the broker's transaction.state.log.replication.factor is sufficient to avoid coordinator failures:
    
    transaction.state.log.replication.factor=3
    
    Also monitor the broker logs for coordinator changes and consider increasing the number of transaction coordinator threads.

无效尝试

常见但无效的做法:

  1. Increase the transaction.timeout.ms to a very high value 85% 失败

    Transaction timeout controls how long a transaction can remain open, not coordinator fencing; it does not prevent epoch conflicts.

  2. Disable idempotent producer and transactions 70% 失败

    This avoids the error but disables exactly-once semantics, which may be required for the application; it is a workaround that changes behavior.