-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Fix send offset to tx, when no active tx #4096
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,6 +47,7 @@ | |
| import org.apache.kafka.common.header.Header; | ||
| import org.apache.kafka.common.header.internals.RecordHeader; | ||
| import org.apache.kafka.common.header.internals.RecordHeaders; | ||
| import org.jspecify.annotations.Nullable; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.mockito.ArgumentCaptor; | ||
|
|
@@ -75,6 +76,7 @@ | |
| import org.springframework.kafka.transaction.KafkaTransactionManager; | ||
| import org.springframework.messaging.MessageHeaders; | ||
| import org.springframework.transaction.TransactionDefinition; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
| import org.springframework.transaction.support.DefaultTransactionDefinition; | ||
| import org.springframework.util.backoff.FixedBackOff; | ||
|
|
||
|
|
@@ -140,6 +142,8 @@ public class TransactionalContainerTests { | |
|
|
||
| public static final String topic10 = "txTopic10"; | ||
|
|
||
| public static final String topic11 = "txTopic11"; | ||
|
|
||
| private static EmbeddedKafkaBroker embeddedKafka; | ||
|
|
||
| @BeforeAll | ||
|
|
@@ -1148,4 +1152,65 @@ public void onMessage(List<ConsumerRecord<Integer, String>> data) { | |
| container.stop(); | ||
| } | ||
|
|
||
| @Test | ||
| void testSendOffsetOnlyOnActiveTransaction() throws InterruptedException { | ||
| // init producer | ||
| Map<String, Object> producerProperties = KafkaTestUtils.producerProps(embeddedKafka); | ||
| DefaultKafkaProducerFactory<Object, Object> pf = new DefaultKafkaProducerFactory<>(producerProperties); | ||
| pf.setTransactionIdPrefix("testSendOffsetOnlyOnActiveTransaction.recordListener"); | ||
| final KafkaTemplate<Object, Object> template = new KafkaTemplate<>(pf); | ||
|
|
||
| // init consumer | ||
| String group = "testSendOffsetOnlyOnActiveTransaction"; | ||
| Map<String, Object> consumerProperties = KafkaTestUtils.consumerProps(embeddedKafka, group, false); | ||
| consumerProperties.put(ConsumerConfig.ISOLATION_LEVEL_CONFIG, "read_committed"); | ||
| DefaultKafkaConsumerFactory<Integer, String> cf = new DefaultKafkaConsumerFactory<>(consumerProperties); | ||
| ContainerProperties containerProps = new ContainerProperties(topic11); | ||
| containerProps.setPollTimeout(10_000); | ||
| final var successLatch = new AtomicReference<>(new CountDownLatch(2)); | ||
| containerProps.setMessageListener(new MessageListener<Integer, String>() { | ||
| @Transactional("testSendOffsetOnlyOnActiveTransaction") | ||
| @Override | ||
| public void onMessage(ConsumerRecord<Integer, String> data) { | ||
| } | ||
| }); | ||
|
|
||
| // init container | ||
| KafkaTransactionManager<Object, Object> tm = new KafkaTransactionManager<>(pf); | ||
| containerProps.setKafkaAwareTransactionManager(tm); | ||
| KafkaMessageListenerContainer<Integer, String> container = new KafkaMessageListenerContainer<>(cf, containerProps); | ||
| container.setBeanName("testSendOffsetOnlyOnActiveTransaction"); | ||
| container.setRecordInterceptor(new RecordInterceptor<Integer, String>() { | ||
| boolean isFirst = true; | ||
|
|
||
| @Override | ||
| public @Nullable ConsumerRecord<Integer, String> intercept( | ||
| ConsumerRecord<Integer, String> record, | ||
| Consumer<Integer, String> consumer) { | ||
| if (isFirst) { | ||
| isFirst = false; | ||
| return record; | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public void afterRecord( | ||
| ConsumerRecord<Integer, String> record, | ||
| Consumer<Integer, String> consumer) { | ||
| successLatch.get().countDown(); | ||
| } | ||
| }); | ||
| container.start(); | ||
|
|
||
| template.executeInTransaction(t -> { | ||
| template.send(new ProducerRecord<>(topic11, 0, 0, "bar1")); | ||
| template.send(new ProducerRecord<>(topic11, 0, 0, "bar2")); | ||
| return null; | ||
| }); | ||
| assertThat(successLatch.get().await(30, TimeUnit.SECONDS)).isTrue(); | ||
|
||
|
|
||
| container.stop(); | ||
| pf.destroy(); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if the
kafkaTxManageris null? Don't we need to address that also? If it is null, we can return as there is no point in sending the offsets to transaction. Do I miss anything on that?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sobychacko You're right. I fixed it. Thanks for the feedback.