Kafka ignoring `transaction.timeout.ms` for producer

瘦欲@ 提交于 2021-02-19 04:46:25

问题


I configure the producer to 10-second timeout using the transaction.timeout.ms property. However, it seems that the transaction is aborted after 60 seconds, which is much longer.

See the following program:

Properties properties = new Properties();
properties.setProperty("bootstrap.servers", brokerConnectionString);
properties.setProperty("transactional.id", "my-transactional-id");
properties.setProperty("transaction.timeout.ms", "5000");

// start the first producer and write one event
KafkaProducer<String, String> producer =
        new KafkaProducer<>(properties, new StringSerializer(), new StringSerializer());
producer.initTransactions();
producer.beginTransaction();
producer.send(new ProducerRecord<>("topic", "value"));
// note the transaction is left non-completed

// start another producer with different txn.id and write second event
properties.setProperty("transactional.id", "another-transactional-id");
KafkaProducer<String, String> producer2 =
        new KafkaProducer<>(properties, new StringSerializer(), new StringSerializer());
producer2.initTransactions();
producer2.beginTransaction();
producer2.send(new ProducerRecord<>("topic", "value2"));
producer2.commitTransaction();

// consume the events using read-committed
Properties consumerProps = new Properties();
consumerProps.setProperty("bootstrap.servers", brokerConnectionString);
consumerProps.setProperty("group.id", "my-group");
consumerProps.setProperty("auto.offset.reset", "earliest");
consumerProps.setProperty("isolation.level", "read_committed");
KafkaConsumer<String, String> consumer = 
        new KafkaConsumer<>(consumerProps, new StringDeserializer(), new StringDeserializer());
consumer.subscribe(singleton("topic"));
while (true) {
    for (ConsumerRecord<String, String> record : consumer.poll(Duration.ofSeconds(1))) {
        logger.info(record.toString());
    }
}

The value2 is printed after about 60 seconds, which is the default value for the transaction.timeout.ms parameter. Do I understand the property incorrectly?


回答1:


In the course of writing the question I found the answer. Broker is configured to check timed out producers every 60 seconds, so the transaction is aborted at next check. This property configures it: transaction.abort.timed.out.transaction.cleanup.interval.ms. I started the broker just before the test, that's why it always took about 60 seconds.



来源:https://stackoverflow.com/questions/56460688/kafka-ignoring-transaction-timeout-ms-for-producer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!