Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-349: Eagerly Initialize Transactional Producers #350

Merged
merged 1 commit into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,14 @@ public Thread newThread(Runnable r) {
? Schedulers.newSingle(options.transactionalId())
: options.scheduler()
);

boolean transactional = this.senderOptions.isTransactional();
this.producerMono = Mono
.fromCallable(() -> {
Producer<K, V> producer = producerFactory.createProducer(senderOptions);
if (senderOptions.producerListener() != null) {
senderOptions.producerListener().producerAdded(producerId, producer);
}
if (senderOptions.isTransactional()) {
if (transactional) {
log.info("Initializing transactions for producer {}",
senderOptions.transactionalId());
producer.initTransactions();
Expand All @@ -117,7 +117,10 @@ public Thread newThread(Runnable r) {
: flux;
});

this.transactionManager = senderOptions.isTransactional()
if (transactional) {
this.producerMono.subscribe().dispose();
}
this.transactionManager = transactional
? new DefaultTransactionManager<>(producerMono, senderOptions)
: null;
}
Expand Down
17 changes: 15 additions & 2 deletions src/test/java/reactor/kafka/sender/internals/MockSenderTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2021 VMware Inc. or its affiliates, All Rights Reserved.
* Copyright (c) 2016-2023 VMware Inc. or its affiliates, All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -61,6 +61,7 @@
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;

import static org.awaitility.Awaitility.await;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -113,6 +114,18 @@ public void producerCreate() {
assertEquals(Arrays.asList(producer), producerFactory.producersInUse());
}

/**
* Tests that a transactional Kafka producer is created eagerly.
*/
@Test
public void txProducerCreate() {
Map<String, Object> options = new HashMap<>();
options.put(ProducerConfig.TRANSACTIONAL_ID_CONFIG, "tx-");
sender = new DefaultKafkaSender<>(producerFactory, SenderOptions.create(options));
await().untilAsserted(() -> assertEquals(1, producerFactory.producersInUse().size()));
assertEquals(Arrays.asList(producer), producerFactory.producersInUse());
}

/**
* Tests that closing KafkaSender closes the underlying producer.
*/
Expand Down Expand Up @@ -843,7 +856,7 @@ public OutgoingRecords append(String topic, int count) {
RecordMetadata metadata = null;
Exception e = null;
if (!fail)
metadata = new RecordMetadata(partition, 0, partitionResponses.size(), 0, (Long) 0L, 0, 0);
metadata = new RecordMetadata(partition, 0, partitionResponses.size(), 0, 0L, 0, 0);
else
e = new InvalidTopicException("Topic not found: " + topic);
partitionResponses.add(new Response<>(metadata, e, correlation));
Expand Down