You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
KafkaContainer transactions: Timeout expired while initializing transactional state in 60000ms. #1816
I'm trying create a test that uses kafka transactions. If I use a local instance of Kafka instead of KafkaContainer everything works fine, but when I use KafkaContainer it fails and I see the following:
Timeout expired while initializing transactional state in 60000ms.
org.apache.kafka.common.errors.TimeoutException: Timeout expired while initializing transactional state in 60000ms.
Attached are the source for my test and the logfile from the run. kafka.log Test.java.txt
Reacted by Chiawen Chen, Brian Zhao and Kenzo Tanaka
I just had a quick check with our KafkaContainerTest and added a transaction example and I see the same behaviour, but with a slightly different error mesage:
Timeout expired after 60000milliseconds while awaiting InitProducerId
org.apache.kafka.common.errors.TimeoutException: Timeout expired after 60000milliseconds while awaiting InitProducerId
changed the title [-]Timeout expired while initializing transactional state in 60000ms.[/-][+]KafkaContainer transactions: Timeout expired while initializing transactional state in 60000ms.[/+]on Sep 9, 2019
you need to configure some of the broker parameters in order transaction state store will be initialized correctly.
Here's correct test
NOTE: it's always good to enable log output from the container to debug this kind of issues.
I use Slf4jLogConsumer from TC.
@Slf4j
public class ProducerTransactionTest {
public static KafkaContainer kafka = new KafkaContainer("5.2.1")
.withLogConsumer(new Slf4jLogConsumer(log));
@BeforeClass
public static void prep() {
// see https://cwiki.apache.org/confluence/display/KAFKA/KIP-98+-+Exactly+Once+Delivery+and+Transactional+Messaging#KIP-98-ExactlyOnceDeliveryandTransactionalMessaging-NewConfigurations
kafka.addEnv("KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR", "1");
kafka.addEnv("KAFKA_TRANSACTION_STATE_LOG_MIN_ISR", "1");
kafka.start();
}
@Test
public void testIt() {
Properties props = new Properties();
props.put(BOOTSTRAP_SERVERS_CONFIG, kafka.getBootstrapServers());
props.put(KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
props.put(VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
props.put(ENABLE_IDEMPOTENCE_CONFIG, "true");
props.put(TRANSACTIONAL_ID_CONFIG, "prod-0");
KafkaProducer<String, String> producer = new KafkaProducer<>(props);
ProducerRecord<String, String> record = new ProducerRecord<>("something", "A message");
producer.initTransactions();
producer.beginTransaction();
producer.send(record);
producer.commitTransaction();
}
}
Let me know if you have any questions.
@bsideup@kiview do you want me to send PR with a test for this issue?
Reacted by Sergei Egorov, Kevin Wittek, Sunghyouk Bae, Sean Glover, Guilherme Torres Castro, Maciek Bryński, Lukas Mikelionis, Marius Mans, Mikhail Nemenko , Jeff Evans and 6 more
Reacted by Sergei Egorov, Kevin Wittek, Sunghyouk Bae and Maik Fleuter
Reacted by Sergei Egorov, Kevin Wittek, Sunghyouk Bae, ponangi-csdisco, Csongor Zalatnai, Maik Fleuter, frenchippoarchive and Jeff Evans
The default value of transaction.state.log.replication.factor is 3 and transaction.state.log.min.isr is 2. So if broker count in your cluster is less than 3, kafka server fails to (automatically) create the topic __transaction_state, thus client got timedout error.
It went to following code, got the transactionManager.initializeTransactions into result but timing out in last line result.await(maxBlockTimeMs, TimeUnit.MILLISECONDS);
public void initTransactions() {
throwIfNoTransactionManager();
throwIfProducerClosed();
TransactionalRequestResult result = transactionManager.initializeTransactions();
sender.wakeup();
result.await(maxBlockTimeMs, TimeUnit.MILLISECONDS);
}
I am also getting same exception. I am using 'KafkaTemplate' with 'executeInTransaction'
props.put(ProducerConfig.ACKS_CONFIG, "all");
props.put(ProducerConfig.MAX_BLOCK_MS_CONFIG, 10000);
props.put(ProducerConfig.ENABLE_IDEMPOTENCE_CONFIG, true);
props.put(ProducerConfig.TRANSACTIONAL_ID_CONFIG, "prod-0"); // I am really not clear what this is for. I set it once in the producerConfig. Then create KafkaTemplate and use it many times...
This is how I call Kafka template:
kafkaTemplate.setTransactionIdPrefix(message.getGuid()); // I am also not sure about this line. I tried to put a unique value to prevent same message to be created twice.kafkaTemplate.executeInTransaction(kafkaTemplate -> {
ListenableFuture<SendResult<String, Event>> future = kafkaTemplate.send(topic, message);
future.addCallback(newListenableFutureCallback<SendResult<String, Event>>() {
@OverridepublicvoidonSuccess(SendResult<String, Event> result) {
}
@OverridepublicvoidonFailure(Throwableex) {
}
});
returnnull;
});
This is what I receive
org.apache.kafka.common.errors.TimeoutException: Timeout expired after 10000milliseconds while awaiting InitProducerId
you need to configure some of the broker parameters in order transaction state store will be initialized correctly.
Here's correct test
NOTE: it's always good to enable log output from the container to debug this kind of issues.
I use Slf4jLogConsumer from TC.
@Slf4j
public class ProducerTransactionTest {
public static KafkaContainer kafka = new KafkaContainer("5.2.1")
.withLogConsumer(new Slf4jLogConsumer(log));
@BeforeClass
public static void prep() {
// see https://cwiki.apache.org/confluence/display/KAFKA/KIP-98+-+Exactly+Once+Delivery+and+Transactional+Messaging#KIP-98-ExactlyOnceDeliveryandTransactionalMessaging-NewConfigurations
kafka.addEnv("KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR", "1");
kafka.addEnv("KAFKA_TRANSACTION_STATE_LOG_MIN_ISR", "1");
kafka.start();
}
@Test
public void testIt() {
Properties props = new Properties();
props.put(BOOTSTRAP_SERVERS_CONFIG, kafka.getBootstrapServers());
props.put(KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
props.put(VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
props.put(ENABLE_IDEMPOTENCE_CONFIG, "true");
props.put(TRANSACTIONAL_ID_CONFIG, "prod-0");
KafkaProducer<String, String> producer = new KafkaProducer<>(props);
ProducerRecord<String, String> record = new ProducerRecord<>("something", "A message");
producer.initTransactions();
producer.beginTransaction();
producer.send(record);
producer.commitTransaction();
}
}
Let me know if you have any questions.
@bsideup@kiview do you want me to send PR with a test for this issue?
This did the trick for me 👆❤️
In case anyone has the same issue using EmbeddedKafka in Spring Boot test, the values can be set when defining the config for the embedded Kafka instance as follows:
I made some modifications(I removed use of chainedTransactionManager), and it works Zero errors in console output, but when I use chainedTransactionManager :
2020-11-26 13:15:01.309 WARN [poc-test,,,] 523026 --- [| producer-tx-0] org.apache.kafka.clients.NetworkClient : [Producer clientId=producer-tx-0, transactionalId=tx-0] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
2020-11-26 13:15:01.309 WARN [poc-test,,,] 523026 --- [| producer-tx-0] org.apache.kafka.clients.NetworkClient : [Producer clientId=producer-tx-0, transactionalId=tx-0] Bootstrap broker localhost:9092 (id: -1 rack: null) disconnected
well, for some reason the problem was caused by the chainedTransactionManager definition, I think the console output is very weird and led me to confusion.
Thanks anyway.
I got the same issue while trying to apply transaction producers.
I fixed it by overriding these two settings at server side:
transaction.state.log.min.isr = 1
transaction.state.log.replication.factor = 1
if you are using Kafka container with docker then most likely you will go with:
KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1
KAFKA_TRANSACTION_STATE_LOG_MIN_ISR: 1
This is fixed since 1.15.2: the Kafka containers set KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR and KAFKA_TRANSACTION_STATE_LOG_MIN_ISR to 1. #12102 adds tests for transactions so this does not break again. I think this issue can be closed.
Activity
kiview commentedon Sep 9, 2019
I just had a quick check with our
KafkaContainerTestand added a transaction example and I see the same behaviour, but with a slightly different error mesage:It happens on
producer.initTransactions().[-]Timeout expired while initializing transactional state in 60000ms.[/-][+]KafkaContainer transactions: Timeout expired while initializing transactional state in 60000ms.[/+]bsideup commentedon Sep 9, 2019
@gAmUssA any idea? :)
gAmUssA commentedon Sep 10, 2019
@bsideup @kiview
Gents
Let me take a look
gAmUssA commentedon Sep 10, 2019
@ernesthill
you need to configure some of the broker parameters in order transaction state store will be initialized correctly.
Here's correct test
NOTE: it's always good to enable log output from the container to debug this kind of issues.
I use
Slf4jLogConsumerfrom TC.Let me know if you have any questions.
@bsideup @kiview do you want me to send PR with a test for this issue?
ernesthill commentedon Sep 16, 2019
Thanks for the information.
debop commentedon Sep 23, 2019
Thanks for this information !!!
seglo commentedon Oct 11, 2019
This should be part of default Kafka test container config, since (AFAIK) it always runs in a single broker configuration.
zenglian commentedon Nov 5, 2019
The default value of
transaction.state.log.replication.factoris 3 andtransaction.state.log.min.isris 2. So if broker count in your cluster is less than 3, kafka server fails to (automatically) create the topic__transaction_state, thus client got timedout error.zaanwer commentedon Nov 11, 2019
I did have this transaction.state.log.replication.factor set to 1 but still seeing this error in producer.initTransaction()
"Timeout expired while initializing transactional state in 60000ms"
zaanwer commentedon Nov 11, 2019
It went to following code, got the transactionManager.initializeTransactions into result but timing out in last line result.await(maxBlockTimeMs, TimeUnit.MILLISECONDS);
public void initTransactions() {
throwIfNoTransactionManager();
throwIfProducerClosed();
TransactionalRequestResult result = transactionManager.initializeTransactions();
sender.wakeup();
result.await(maxBlockTimeMs, TimeUnit.MILLISECONDS);
}
zenglian commentedon Nov 13, 2019
Note there are 2 props to change. Do not try to debug the client code. It’s an server error.
6 remaining items
gAmUssA commentedon Jun 8, 2020
@pancudaniel7 could you share a reproducer?
Thank you
cemkayan commentedon Aug 3, 2020
I am also getting same exception. I am using 'KafkaTemplate' with 'executeInTransaction'
This is how I call Kafka template:
This is what I receive
maver1ck commentedon Sep 29, 2020
Same problem here :(
I'm using Flink job with FlinkKafkaProducer.Semantic.EXACTLY_ONCE.
When using same job without Test Containers everything is working fine.
bsideup commentedon Sep 29, 2020
@maver1ck the transactions are not enabled by default, see @gAmUssA's answer:
#1816 (comment)
maver1ck commentedon Sep 29, 2020
@bsideup checking this
EDIT: It works. Thanks.
MrMikeFloyd commentedon Nov 12, 2020
This did the trick for me 👆❤️
In case anyone has the same issue using
EmbeddedKafkain Spring Boot test, the values can be set when defining the config for the embedded Kafka instance as follows:kcotzen commentedon Nov 26, 2020
Hi everyone, Unfortunately I have the same error:
In the console output I see this:
My configuration of KafkaContainer :
And my configuration in the application.yml looks like this:
I'm not getting it to start up successfully, has anyone been able to overcome this?
thanks in advance
maver1ck commentedon Nov 26, 2020
kafka.addEnv("KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR", "2")
Why 2?
kcotzen commentedon Nov 26, 2020
If I use :
The error is the same. If I use 1 for both, I got this:
bsideup commentedon Nov 26, 2020
@kcotzen it should be
1, unless you start & connect two brokerskcotzen commentedon Nov 26, 2020
I used value 1 for both, and I got this error:
kcotzen commentedon Nov 26, 2020
I made some modifications(I removed use of chainedTransactionManager), and it works Zero errors in console output, but when I use chainedTransactionManager :
sounds weird. Any ideas please?
kcotzen commentedon Nov 26, 2020
well, for some reason the problem was caused by the chainedTransactionManager definition, I think the console output is very weird and led me to confusion.
Thanks anyway.
daoanhvu commentedon Jul 31, 2024
I got the same issue while trying to apply transaction producers.
I fixed it by overriding these two settings at server side:
transaction.state.log.min.isr = 1
transaction.state.log.replication.factor = 1
if you are using Kafka container with docker then most likely you will go with:
KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1
KAFKA_TRANSACTION_STATE_LOG_MIN_ISR: 1
aymenjamaaoui commentedon Oct 4, 2026
This is fixed since 1.15.2: the Kafka containers set
KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTORandKAFKA_TRANSACTION_STATE_LOG_MIN_ISRto 1. #12102 adds tests for transactions so this does not break again. I think this issue can be closed.