简体   繁体   English

Kafka事务提交超时异常处理

[英]Kafka transaction commit timeout exception handling

I am running kafka transactions on a large scale and below is the codepiece.我正在大规模运行 kafka 事务,下面是代码片段。

producer.initTransaction();
try {
    producer.beginTransaction();
    producer.send(new ProducerRecord<>(producerTopic, element));
    producer.commitTransaction();
} catch (ProducerFencedException | OutOfOrderSequenceException | AuthorizationException e) {
    producer.close();
    canSendNext = false;
}catch (KafkaException e) {
    producer.abortTransaction();
}

properties used:使用的属性:

ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, STRING_SERIALIZER ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, BYTE_ARRAY_SERIALIZER ProducerConfig.TRANSACTIONAL_ID_CONFIG, UUID.randomUUID().toString() bootstrap.servers=localhost:9092 acks=all retries=1 partitioner.class=org.apache.kafka.clients.producer.RoundRobinPartitioner ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, STRING_SERIALIZER ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, BYTE_ARRAY_SERIALIZER ProducerConfig.TRANSACTIONAL_ID_CONFIG, UUID.randomUUID().toString() bootstrap.servers=localhost:9092 acks=all retries=1 partitioner.class=org.apache.kafka.clients.producer.循环分区器

while the commitTransaction is timing out the catch block of KafkaException runs and try to abort the transaction.当 commitTransaction 超时时,KafkaException 的 catch 块运行并尝试中止事务。 Which fails with the error: ** Cannot attempt operation abortTransaction because the previous call to commitTransaction timed out and must be retried**失败并显示错误:** 无法尝试操作abortTransaction因为之前对commitTransaction的调用超时并且必须重试**

how to handle commit transaction timeout scenario如何处理提交事务超时场景

expecting the code to work期待代码工作

As per the documentation :根据文档

Note that this method will raise TimeoutException if the transaction cannot be committed before expiration of max.block.ms.请注意,如果事务无法在 max.block.ms 到期之前提交,则此方法将引发 TimeoutException。 Additionally, it will raise InterruptException if interrupted.此外,如果被中断,它将引发 InterruptException。 It is safe to retry in either case, but it is not possible to attempt a different operation (such as abortTransaction) since the commit may already be in the progress of completing.在任何一种情况下重试都是安全的,但不可能尝试不同的操作(例如 abortTransaction),因为提交可能已经在完成过程中。 If not retrying, the only option is to close the producer.如果不重试,唯一的选择是关闭生产者。

The problem here is that the kafka producer has timed out and it does not know whether the kafka broker will complete the transaction or not.这里的问题是kafka producer超时了,不知道kafka broker是否会完成交易。 So it cannot provide you abort transaction functionality as there is a chance that the transaction might have been committed by the broker even after the producer times out.因此它不能为您提供中止交易功能,因为即使在生产者超时后,交易也有可能由经纪人提交。

You should configure your kafka producer with a sufficiently large max.block.ms and good number of retries to handle such scenarios (Not sure why you have configured your retries to 1).您应该为您的 kafka 生产者配置足够大的max.block.ms和足够多的retries来处理这种情况(不确定为什么将重试次数配置为 1)。 Ideally you should be timing out very rarely - like when there is some issue in the.network or there is some actual issue going on in kafka brokers.理想情况下,你应该很少超时——比如当 .network 中出现问题或者 kafka 代理中出现一些实际问题时。

In such scenarios, it won't be possible for you to know if your last transaction was actually successful or not.在这种情况下,您将无法知道上次交易是否真的成功。 You cannot do anything but close your kafka producer and create a new one.你只能关闭你的 kafka 生产者并创建一个新的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM