You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kafka.apache.org by ch...@apache.org on 2026/04/15 17:30:46 UTC
(kafka) branch trunk updated: MINOR: Split up ShareConsumerTest (#22017)
This is an automated email from the ASF dual-hosted git repository.
chia7712 pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/kafka.git
The following commit(s) were added to refs/heads/trunk by this push:
new 07a6da3eaeb MINOR: Split up ShareConsumerTest (#22017)
07a6da3eaeb is described below
commit 07a6da3eaeb21bcf631431c9bbb1897c18870fba
Author: Andrew Schofield <as...@confluent.io>
AuthorDate: Wed Apr 15 18:30:37 2026 +0100
MINOR: Split up ShareConsumerTest (#22017)
Split up the excessively large `ShareConsumerTest` into a handful of
smaller source files.
Reviewers: Maros Orsak <ma...@gmail.com>, Chia-Ping Tsai
<ch...@gmail.com>
---
.../consumer/ShareConsumerCallbackTest.java | 346 +++
.../consumer/ShareConsumerDeliveryTest.java | 1302 +++++++++
.../clients/consumer/ShareConsumerLagTest.java | 418 +++
.../consumer/ShareConsumerRackAwareTest.java | 37 +-
.../clients/consumer/ShareConsumerRenewTest.java | 304 ++
.../kafka/clients/consumer/ShareConsumerTest.java | 2931 +-------------------
.../clients/consumer/ShareConsumerTestBase.java | 773 ++++++
7 files changed, 3215 insertions(+), 2896 deletions(-)
diff --git a/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/ShareConsumerCallbackTest.java b/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/ShareConsumerCallbackTest.java
new file mode 100644
index 00000000000..483cb043450
--- /dev/null
+++ b/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/ShareConsumerCallbackTest.java
@@ -0,0 +1,346 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.clients.consumer;
+
+import org.apache.kafka.clients.producer.Producer;
+import org.apache.kafka.clients.producer.ProducerRecord;
+import org.apache.kafka.common.KafkaException;
+import org.apache.kafka.common.TopicIdPartition;
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.errors.InvalidRecordStateException;
+import org.apache.kafka.common.errors.WakeupException;
+import org.apache.kafka.common.test.ClusterInstance;
+import org.apache.kafka.common.test.api.ClusterConfigProperty;
+import org.apache.kafka.common.test.api.ClusterTest;
+import org.apache.kafka.common.test.api.ClusterTestDefaults;
+import org.apache.kafka.common.test.api.Type;
+import org.apache.kafka.test.NoRetryException;
+import org.apache.kafka.test.TestUtils;
+
+import org.junit.jupiter.api.Timeout;
+
+import java.time.Duration;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import static org.apache.kafka.test.TestUtils.DEFAULT_MAX_WAIT_MS;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+@Timeout(1200)
+@ClusterTestDefaults(
+ types = {Type.KRAFT},
+ serverProperties = {
+ @ClusterConfigProperty(key = "auto.create.topics.enable", value = "false"),
+ @ClusterConfigProperty(key = "group.share.max.partition.max.record.locks", value = "10000"),
+ @ClusterConfigProperty(key = "group.share.partition.max.record.locks", value = "10000"),
+ @ClusterConfigProperty(key = "group.share.record.lock.duration.ms", value = "15000"),
+ @ClusterConfigProperty(key = "offsets.topic.replication.factor", value = "1"),
+ @ClusterConfigProperty(key = "share.coordinator.state.topic.min.isr", value = "1"),
+ @ClusterConfigProperty(key = "share.coordinator.state.topic.num.partitions", value = "3"),
+ @ClusterConfigProperty(key = "share.coordinator.state.topic.replication.factor", value = "1"),
+ @ClusterConfigProperty(key = "transaction.state.log.min.isr", value = "1"),
+ @ClusterConfigProperty(key = "transaction.state.log.replication.factor", value = "1")
+ }
+)
+public class ShareConsumerCallbackTest extends ShareConsumerTestBase {
+
+ public ShareConsumerCallbackTest(ClusterInstance cluster) {
+ super(cluster);
+ }
+
+ @ClusterTest
+ public void testAcknowledgementSentOnSubscriptionChange() throws ExecutionException, InterruptedException {
+ alterShareAutoOffsetReset("group1", "earliest");
+ try (Producer<byte[], byte[]> producer = createProducer();
+ ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer("group1")) {
+
+ Map<TopicPartition, Set<Long>> partitionOffsetsMap = new HashMap<>();
+ Map<TopicPartition, Exception> partitionExceptionMap = new HashMap<>();
+
+ ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), "value".getBytes());
+ producer.send(record);
+ ProducerRecord<byte[], byte[]> record2 = new ProducerRecord<>(tp2.topic(), tp2.partition(), null, "key".getBytes(), "value".getBytes());
+ producer.send(record2).get();
+ producer.flush();
+ shareConsumer.setAcknowledgementCommitCallback(new TestableAcknowledgementCommitCallback(partitionOffsetsMap, partitionExceptionMap));
+
+ shareConsumer.subscribe(Set.of(tp.topic()));
+ assertEquals(Optional.empty(), shareConsumer.acquisitionLockTimeoutMs());
+
+ TestUtils.waitForCondition(() -> shareConsumer.poll(Duration.ofMillis(2000)).count() == 1,
+ DEFAULT_MAX_WAIT_MS, 100L, () -> "Failed to consume records for share consumer");
+
+ assertEquals(Optional.of(15000), shareConsumer.acquisitionLockTimeoutMs());
+ shareConsumer.subscribe(Set.of(tp2.topic()));
+ assertEquals(Optional.of(15000), shareConsumer.acquisitionLockTimeoutMs());
+
+ // Waiting for heartbeat to propagate the subscription change.
+ TestUtils.waitForCondition(() -> {
+ shareConsumer.poll(Duration.ofMillis(500));
+ return partitionOffsetsMap.containsKey(tp) && partitionOffsetsMap.containsKey(tp2);
+ }, DEFAULT_MAX_WAIT_MS, 100L, () -> "Failed to consume records from the updated subscription");
+
+ // Verifying if the callback was invoked without exceptions for the partitions for both topics.
+ assertFalse(partitionExceptionMap.containsKey(tp));
+ assertFalse(partitionExceptionMap.containsKey(tp2));
+ verifyShareGroupStateTopicRecordsProduced();
+ }
+ }
+
+ @ClusterTest
+ public void testAcknowledgementCommitCallbackSuccessfulAcknowledgement() throws Exception {
+ alterShareAutoOffsetReset("group1", "earliest");
+ try (Producer<byte[], byte[]> producer = createProducer();
+ ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer("group1")) {
+
+ Map<TopicPartition, Set<Long>> partitionOffsetsMap = new HashMap<>();
+ Map<TopicPartition, Exception> partitionExceptionMap = new HashMap<>();
+ ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), "value".getBytes());
+
+ producer.send(record);
+ producer.flush();
+
+ shareConsumer.setAcknowledgementCommitCallback(new TestableAcknowledgementCommitCallback(partitionOffsetsMap, partitionExceptionMap));
+ shareConsumer.subscribe(Set.of(tp.topic()));
+
+ TestUtils.waitForCondition(() -> shareConsumer.poll(Duration.ofMillis(2000)).count() == 1,
+ DEFAULT_MAX_WAIT_MS, 100L, () -> "Failed to consume records for share consumer");
+
+ // The callback should be called before the return of the poll, even when there are no more records.
+ ConsumerRecords<byte[], byte[]> records = shareConsumer.poll(Duration.ofMillis(2000));
+ assertEquals(0, records.count());
+ assertTrue(partitionOffsetsMap.containsKey(tp));
+
+ // We expect no exception as the acknowledgement error code is null.
+ assertFalse(partitionExceptionMap.containsKey(tp));
+ verifyShareGroupStateTopicRecordsProduced();
+ }
+ }
+
+ @ClusterTest
+ public void testAcknowledgementCommitCallbackSuccessfulAcknowledgementOnCommitSync() throws Exception {
+ alterShareAutoOffsetReset("group1", "earliest");
+ try (Producer<byte[], byte[]> producer = createProducer();
+ ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer("group1")) {
+
+ Map<TopicPartition, Set<Long>> partitionOffsetsMap = new HashMap<>();
+ Map<TopicPartition, Exception> partitionExceptionMap = new HashMap<>();
+ ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), "value".getBytes());
+
+ producer.send(record);
+ producer.flush();
+
+ shareConsumer.setAcknowledgementCommitCallback(new TestableAcknowledgementCommitCallback(partitionOffsetsMap, partitionExceptionMap));
+ shareConsumer.subscribe(Set.of(tp.topic()));
+
+ TestUtils.waitForCondition(() -> shareConsumer.poll(Duration.ofMillis(2000)).count() == 1,
+ DEFAULT_MAX_WAIT_MS, 100L, () -> "Failed to consume records for share consumer");
+
+ // The acknowledgement commit callback should be called before the commitSync returns
+ // once the records have been confirmed to have been acknowledged.
+ Map<TopicIdPartition, Optional<KafkaException>> result = shareConsumer.commitSync();
+ assertEquals(1, result.size());
+ assertTrue(partitionOffsetsMap.containsKey(tp));
+
+ // We expect no exception as the acknowledgement error code is null.
+ assertFalse(partitionExceptionMap.containsKey(tp));
+ verifyShareGroupStateTopicRecordsProduced();
+ }
+ }
+
+ @ClusterTest
+ public void testAcknowledgementCommitCallbackOnClose() {
+ alterShareAutoOffsetReset("group1", "earliest");
+ try (Producer<byte[], byte[]> producer = createProducer();
+ ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer("group1")) {
+
+ Map<TopicPartition, Set<Long>> partitionOffsetsMap = new HashMap<>();
+ Map<TopicPartition, Exception> partitionExceptionMap = new HashMap<>();
+ ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), "value".getBytes());
+ producer.send(record);
+ producer.flush();
+ shareConsumer.setAcknowledgementCommitCallback(new TestableAcknowledgementCommitCallback(partitionOffsetsMap, partitionExceptionMap));
+ shareConsumer.subscribe(Set.of(tp.topic()));
+
+ ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 2500L, 1);
+ assertEquals(1, records.count());
+
+ // Now in the second poll, we implicitly acknowledge the record received in the first poll.
+ // We get back the acknowledgement error code asynchronously after the second poll.
+ // The acknowledgement commit callback is invoked in close.
+ shareConsumer.poll(Duration.ofMillis(1000));
+ shareConsumer.close();
+
+ // We expect no exception as the acknowledgement error code is null.
+ assertFalse(partitionExceptionMap.containsKey(tp));
+ verifyShareGroupStateTopicRecordsProduced();
+ }
+ }
+
+ @ClusterTest
+ public void testAcknowledgementCommitCallbackInvalidRecordStateException() throws Exception {
+ alterShareAutoOffsetReset("group1", "earliest");
+ try (Producer<byte[], byte[]> producer = createProducer();
+ ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer("group1")) {
+
+ Map<TopicPartition, Set<Long>> partitionOffsetsMap = new HashMap<>();
+ Map<TopicPartition, Exception> partitionExceptionMap = new HashMap<>();
+ ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), "value".getBytes());
+ producer.send(record);
+ producer.flush();
+
+ shareConsumer.setAcknowledgementCommitCallback(new TestableAcknowledgementCommitCallback(partitionOffsetsMap, partitionExceptionMap));
+ shareConsumer.subscribe(Set.of(tp.topic()));
+
+ ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 2500L, 1);
+ assertEquals(1, records.count());
+
+ // Waiting until the acquisition lock expires.
+ Thread.sleep(20000);
+
+ TestUtils.waitForCondition(() -> {
+ shareConsumer.poll(Duration.ofMillis(500));
+ return partitionExceptionMap.containsKey(tp) && partitionExceptionMap.get(tp) instanceof InvalidRecordStateException;
+ }, DEFAULT_MAX_WAIT_MS, 100L, () -> "Failed to be notified by InvalidRecordStateException");
+ }
+ }
+
+ /**
+ * Test to verify that the acknowledgement commit callback cannot invoke methods of ShareConsumer.
+ * The exception thrown is verified in {@link TestableAcknowledgementCommitCallbackWithShareConsumer}
+ */
+ @ClusterTest
+ public void testAcknowledgementCommitCallbackCallsShareConsumerDisallowed() {
+ alterShareAutoOffsetReset("group1", "earliest");
+ try (Producer<byte[], byte[]> producer = createProducer();
+ ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer("group1")) {
+
+ ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), "value".getBytes());
+ producer.send(record);
+ producer.flush();
+
+ shareConsumer.setAcknowledgementCommitCallback(new TestableAcknowledgementCommitCallbackWithShareConsumer<>(shareConsumer));
+ shareConsumer.subscribe(Set.of(tp.topic()));
+
+ // The acknowledgement commit callback will try to call a method of ShareConsumer
+ shareConsumer.poll(Duration.ofMillis(5000));
+ // The second poll sends the acknowledgements implicitly.
+ // The acknowledgement commit callback will be called and the exception is thrown.
+ // This is verified inside the onComplete() method implementation.
+ shareConsumer.poll(Duration.ofMillis(500));
+ verifyShareGroupStateTopicRecordsProduced();
+ }
+ }
+
+ private class TestableAcknowledgementCommitCallbackWithShareConsumer<K, V> implements AcknowledgementCommitCallback {
+ private final ShareConsumer<K, V> shareConsumer;
+
+ TestableAcknowledgementCommitCallbackWithShareConsumer(ShareConsumer<K, V> shareConsumer) {
+ this.shareConsumer = shareConsumer;
+ }
+
+ @Override
+ public void onComplete(Map<TopicIdPartition, Set<Long>> offsetsMap, Exception exception) {
+ // Accessing methods of ShareConsumer should throw an exception.
+ assertThrows(IllegalStateException.class, shareConsumer::close);
+ assertThrows(IllegalStateException.class, () -> shareConsumer.subscribe(Set.of(tp.topic())));
+ assertThrows(IllegalStateException.class, () -> shareConsumer.poll(Duration.ofMillis(5000)));
+ }
+ }
+
+ /**
+ * Test to verify that the acknowledgement commit callback can invoke ShareConsumer.wakeup() and it
+ * wakes up the enclosing poll.
+ */
+ @ClusterTest
+ public void testAcknowledgementCommitCallbackCallsShareConsumerWakeup() throws InterruptedException {
+ alterShareAutoOffsetReset("group1", "earliest");
+ try (Producer<byte[], byte[]> producer = createProducer();
+ ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer("group1")) {
+
+ ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), "value".getBytes());
+ producer.send(record);
+ producer.flush();
+
+ // The acknowledgement commit callback will try to call a method of ShareConsumer
+ shareConsumer.setAcknowledgementCommitCallback((__, ___) -> shareConsumer.wakeup());
+ shareConsumer.subscribe(Set.of(tp.topic()));
+
+ TestUtils.waitForCondition(() -> shareConsumer.poll(Duration.ofMillis(2000)).count() == 1,
+ DEFAULT_MAX_WAIT_MS, 100L, () -> "Failed to consume records for share consumer");
+
+ // The second poll sends the acknowledgements implicitly.
+ shareConsumer.poll(Duration.ofMillis(2000));
+
+ // Till now acknowledgement commit callback has not been called, so no exception thrown yet.
+ // On 3rd poll, the acknowledgement commit callback will be called and the exception is thrown.
+ AtomicBoolean exceptionThrown = new AtomicBoolean(false);
+ TestUtils.waitForCondition(() -> {
+ try {
+ shareConsumer.poll(Duration.ofMillis(500));
+ } catch (WakeupException e) {
+ exceptionThrown.set(true);
+ }
+ return exceptionThrown.get();
+ }, DEFAULT_MAX_WAIT_MS, 100L, () -> "Failed to receive expected exception");
+ verifyShareGroupStateTopicRecordsProduced();
+ }
+ }
+
+ /**
+ * Test to verify that the acknowledgement commit callback can throw an exception, and it is not propagated
+ * to the caller of poll().
+ */
+ @ClusterTest
+ public void testAcknowledgementCommitCallbackThrowsException() throws InterruptedException {
+ alterShareAutoOffsetReset("group1", "earliest");
+ try (Producer<byte[], byte[]> producer = createProducer();
+ ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer("group1")) {
+
+ ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), "value".getBytes());
+ producer.send(record);
+ producer.flush();
+
+ AtomicBoolean callbackCalled = new AtomicBoolean(false);
+ shareConsumer.setAcknowledgementCommitCallback((__, ___) -> {
+ callbackCalled.set(true);
+ throw new org.apache.kafka.common.errors.OutOfOrderSequenceException("Exception thrown in AcknowledgementCommitCallback.onComplete");
+ });
+ shareConsumer.subscribe(Set.of(tp.topic()));
+
+ TestUtils.waitForCondition(() -> shareConsumer.poll(Duration.ofMillis(2000)).count() == 1,
+ DEFAULT_MAX_WAIT_MS, 100L, () -> "Failed to consume records for share consumer");
+
+ TestUtils.waitForCondition(() -> {
+ try {
+ shareConsumer.poll(Duration.ofMillis(500));
+ } catch (Exception e) {
+ throw new NoRetryException(e);
+ }
+ return callbackCalled.get();
+ }, DEFAULT_MAX_WAIT_MS, 100L, () -> "Received unexpected exception or callback not called");
+ verifyShareGroupStateTopicRecordsProduced();
+ }
+ }
+}
\ No newline at end of file
diff --git a/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/ShareConsumerDeliveryTest.java b/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/ShareConsumerDeliveryTest.java
new file mode 100644
index 00000000000..771d95ac444
--- /dev/null
+++ b/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/ShareConsumerDeliveryTest.java
@@ -0,0 +1,1302 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.clients.consumer;
+
+import org.apache.kafka.clients.admin.Admin;
+import org.apache.kafka.clients.admin.AlterConfigOp;
+import org.apache.kafka.clients.admin.ConfigEntry;
+import org.apache.kafka.clients.admin.RecordsToDelete;
+import org.apache.kafka.clients.producer.Producer;
+import org.apache.kafka.clients.producer.ProducerRecord;
+import org.apache.kafka.common.KafkaException;
+import org.apache.kafka.common.TopicIdPartition;
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.config.ConfigResource;
+import org.apache.kafka.common.errors.InvalidConfigurationException;
+import org.apache.kafka.common.test.ClusterInstance;
+import org.apache.kafka.common.test.api.ClusterConfigProperty;
+import org.apache.kafka.common.test.api.ClusterTest;
+import org.apache.kafka.common.test.api.ClusterTestDefaults;
+import org.apache.kafka.common.test.api.Type;
+import org.apache.kafka.coordinator.group.GroupConfig;
+import org.apache.kafka.test.TestUtils;
+
+import org.junit.jupiter.api.Timeout;
+
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import static org.apache.kafka.test.TestUtils.DEFAULT_MAX_WAIT_MS;
+import static org.apache.kafka.test.TestUtils.waitForCondition;
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+@Timeout(1200)
+@ClusterTestDefaults(
+ types = {Type.KRAFT},
+ serverProperties = {
+ @ClusterConfigProperty(key = "auto.create.topics.enable", value = "false"),
+ @ClusterConfigProperty(key = "group.share.max.partition.max.record.locks", value = "10000"),
+ @ClusterConfigProperty(key = "group.share.partition.max.record.locks", value = "10000"),
+ @ClusterConfigProperty(key = "group.share.record.lock.duration.ms", value = "15000"),
+ @ClusterConfigProperty(key = "offsets.topic.replication.factor", value = "1"),
+ @ClusterConfigProperty(key = "share.coordinator.state.topic.min.isr", value = "1"),
+ @ClusterConfigProperty(key = "share.coordinator.state.topic.num.partitions", value = "3"),
+ @ClusterConfigProperty(key = "share.coordinator.state.topic.replication.factor", value = "1"),
+ @ClusterConfigProperty(key = "transaction.state.log.min.isr", value = "1"),
+ @ClusterConfigProperty(key = "transaction.state.log.replication.factor", value = "1")
+ }
+)
+public class ShareConsumerDeliveryTest extends ShareConsumerTestBase {
+
+ public ShareConsumerDeliveryTest(ClusterInstance cluster) {
+ super(cluster);
+ }
+
+ @ClusterTest
+ public void testShareAutoOffsetResetDefaultValue() {
+ try (ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer("group1");
+ Producer<byte[], byte[]> producer = createProducer()) {
+
+ shareConsumer.subscribe(Set.of(tp.topic()));
+ ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), "value".getBytes());
+ // Producing a record.
+ producer.send(record);
+ producer.flush();
+ ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 2500L, 0, true, "group1", List.of(tp));
+ // No records should be consumed because share.auto.offset.reset has a default of "latest". Since the record
+ // was produced before share partition was initialized (which happens after the first share fetch request
+ // in the poll method), the start offset would be the latest offset, i.e. 1 (the next offset after the already
+ // present 0th record)
+ assertEquals(0, records.count());
+ // Producing another record.
+ producer.send(record);
+ producer.flush();
+ records = shareConsumer.poll(Duration.ofMillis(5000));
+ // Now the next record should be consumed successfully
+ assertEquals(1, records.count());
+ verifyShareGroupStateTopicRecordsProduced();
+ }
+ }
+
+ @ClusterTest
+ public void testShareAutoOffsetResetEarliest() {
+ alterShareAutoOffsetReset("group1", "earliest");
+ try (ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer("group1");
+ Producer<byte[], byte[]> producer = createProducer()) {
+
+ shareConsumer.subscribe(Set.of(tp.topic()));
+ ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), "value".getBytes());
+ // Producing a record.
+ producer.send(record);
+ producer.flush();
+ ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 2500L, 1);
+ // Since the value for share.auto.offset.reset has been altered to "earliest", the consumer should consume
+ // all messages present on the partition
+ assertEquals(1, records.count());
+ // Producing another record.
+ producer.send(record);
+ producer.flush();
+ records = shareConsumer.poll(Duration.ofMillis(5000));
+ // The next records should also be consumed successfully
+ assertEquals(1, records.count());
+ verifyShareGroupStateTopicRecordsProduced();
+ }
+ }
+
+ @ClusterTest
+ public void testShareAutoOffsetResetEarliestAfterLsoMovement() {
+ alterShareAutoOffsetReset("group1", "earliest");
+ try (
+ Producer<byte[], byte[]> producer = createProducer();
+ Admin adminClient = createAdminClient()
+ ) {
+
+ ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), "value".getBytes());
+ // We write 10 records to the topic, so they would be written from offsets 0-9 on the topic.
+ for (int i = 0; i < 10; i++) {
+ assertDoesNotThrow(() -> producer.send(record).get(), "Failed to send records");
+ }
+
+ // We delete records before offset 5, so the LSO should move to 5.
+ assertDoesNotThrow(() -> adminClient.deleteRecords(Map.of(tp, RecordsToDelete.beforeOffset(5L))).all().get(), "Failed to delete records");
+
+ int consumedMessageCount = consumeMessages(new AtomicInteger(0), 5, "group1", 1, 10, true);
+ // The records returned belong to offsets 5-9.
+ assertEquals(5, consumedMessageCount);
+ verifyShareGroupStateTopicRecordsProduced();
+ }
+ }
+
+ @ClusterTest
+ public void testShareAutoOffsetResetMultipleGroupsWithDifferentValue() {
+ alterShareAutoOffsetReset("group1", "earliest");
+ alterShareAutoOffsetReset("group2", "latest");
+ try (ShareConsumer<byte[], byte[]> shareConsumerEarliest = createShareConsumer("group1");
+ ShareConsumer<byte[], byte[]> shareConsumerLatest = createShareConsumer("group2");
+ Producer<byte[], byte[]> producer = createProducer()) {
+
+ shareConsumerEarliest.subscribe(Set.of(tp.topic()));
+ shareConsumerLatest.subscribe(Set.of(tp.topic()));
+
+ ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), "value".getBytes());
+ // Producing a record.
+ producer.send(record);
+ producer.flush();
+ ConsumerRecords<byte[], byte[]> records1 = waitedPoll(shareConsumerEarliest, 2500L, 1);
+ // Since the value for share.auto.offset.reset has been altered to "earliest", the consumer should consume
+ // all messages present on the partition
+ assertEquals(1, records1.count());
+
+ ConsumerRecords<byte[], byte[]> records2 = waitedPoll(shareConsumerLatest, 2500L, 0, true, "group2", List.of(tp));
+ // Since the value for share.auto.offset.reset has been altered to "latest", the consumer should not consume
+ // any message
+ assertEquals(0, records2.count());
+
+ // Producing another record.
+ producer.send(record);
+
+ records1 = shareConsumerEarliest.poll(Duration.ofMillis(5000));
+ // The next record should also be consumed successfully by group1
+ assertEquals(1, records1.count());
+
+ records2 = shareConsumerLatest.poll(Duration.ofMillis(5000));
+ // The next record should also be consumed successfully by group2
+ assertEquals(1, records2.count());
+ verifyShareGroupStateTopicRecordsProduced();
+ }
+ }
+
+ @ClusterTest
+ public void testShareAutoOffsetResetByDuration() throws Exception {
+ // Set auto offset reset to 1 hour before current time
+ alterShareAutoOffsetReset("group1", "by_duration:PT1H");
+
+ try (ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer("group1");
+ Producer<byte[], byte[]> producer = createProducer()) {
+
+ long currentTime = System.currentTimeMillis();
+ long twoHoursAgo = currentTime - TimeUnit.HOURS.toMillis(2);
+ long thirtyMinsAgo = currentTime - TimeUnit.MINUTES.toMillis(30);
+
+ // Produce messages with different timestamps
+ ProducerRecord<byte[], byte[]> oldRecord = new ProducerRecord<>(tp.topic(), tp.partition(),
+ twoHoursAgo, "old_key".getBytes(), "old_value".getBytes());
+ ProducerRecord<byte[], byte[]> recentRecord = new ProducerRecord<>(tp.topic(), tp.partition(),
+ thirtyMinsAgo, "recent_key".getBytes(), "recent_value".getBytes());
+ ProducerRecord<byte[], byte[]> currentRecord = new ProducerRecord<>(tp.topic(), tp.partition(),
+ currentTime, "current_key".getBytes(), "current_value".getBytes());
+
+ producer.send(oldRecord).get();
+ producer.send(recentRecord).get();
+ producer.send(currentRecord).get();
+ producer.flush();
+
+ shareConsumer.subscribe(Set.of(tp.topic()));
+
+ // Should only receive messages from last hour (recent and current)
+ List<ConsumerRecord<byte[], byte[]>> records = consumeRecords(shareConsumer, 2);
+ assertEquals(2, records.size());
+
+ // Verify timestamps and order
+ assertEquals(thirtyMinsAgo, records.get(0).timestamp());
+ assertEquals("recent_key", new String(records.get(0).key()));
+ assertEquals(currentTime, records.get(1).timestamp());
+ assertEquals("current_key", new String(records.get(1).key()));
+ }
+
+ // Set the auto offset reset to 3 hours before current time
+ // so the consumer should consume all messages (3 records)
+ alterShareAutoOffsetReset("group2", "by_duration:PT3H");
+ try (ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer("group2")) {
+
+ shareConsumer.subscribe(Set.of(tp.topic()));
+ List<ConsumerRecord<byte[], byte[]>> records = consumeRecords(shareConsumer, 3);
+ assertEquals(3, records.size());
+ verifyShareGroupStateTopicRecordsProduced();
+ }
+ }
+
+ @ClusterTest
+ public void testShareAutoOffsetResetByDurationInvalidFormat() {
+ // Test invalid duration format
+ ConfigResource configResource = new ConfigResource(ConfigResource.Type.GROUP, "group1");
+ Map<ConfigResource, Collection<AlterConfigOp>> alterEntries = new HashMap<>();
+
+ // Test invalid duration format
+ try (Admin adminClient = createAdminClient()) {
+ alterEntries.put(configResource, List.of(new AlterConfigOp(new ConfigEntry(
+ GroupConfig.SHARE_AUTO_OFFSET_RESET_CONFIG, "by_duration:1h"), AlterConfigOp.OpType.SET)));
+ ExecutionException e1 = assertThrows(ExecutionException.class, () ->
+ adminClient.incrementalAlterConfigs(alterEntries).all().get());
+ assertInstanceOf(InvalidConfigurationException.class, e1.getCause());
+
+ // Test negative duration
+ alterEntries.put(configResource, List.of(new AlterConfigOp(new ConfigEntry(
+ GroupConfig.SHARE_AUTO_OFFSET_RESET_CONFIG, "by_duration:-PT1H"), AlterConfigOp.OpType.SET)));
+ ExecutionException e2 = assertThrows(ExecutionException.class, () ->
+ adminClient.incrementalAlterConfigs(alterEntries).all().get());
+ assertInstanceOf(InvalidConfigurationException.class, e2.getCause());
+ }
+ }
+
+ @ClusterTest
+ public void testDeliveryCountDifferentBehaviorWhenClosingSessionWithExplicitAcknowledgement() {
+ alterShareAutoOffsetReset("group1", "earliest");
+ try (Producer<byte[], byte[]> producer = createProducer();
+ ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer(
+ "group1",
+ Map.of(ConsumerConfig.SHARE_ACKNOWLEDGEMENT_MODE_CONFIG, EXPLICIT))) {
+
+ ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null,
+ "key".getBytes(), "value".getBytes());
+ producer.send(record);
+ producer.send(record);
+ producer.flush();
+
+ shareConsumer.subscribe(Set.of(tp.topic()));
+ ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 2500L, 2);
+ assertEquals(2, records.count());
+ // Acknowledge the first record with AcknowledgeType.RELEASE
+ shareConsumer.acknowledge(records.records(tp).get(0), AcknowledgeType.RELEASE);
+ Map<TopicIdPartition, Optional<KafkaException>> result = shareConsumer.commitSync();
+ assertEquals(1, result.size());
+ }
+
+ // Test delivery count
+ try (ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer("group1", Map.of())) {
+ shareConsumer.subscribe(Set.of(tp.topic()));
+ ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 2500L, 2);
+ assertEquals(2, records.count());
+ assertEquals((short) 2, records.records(tp).get(0).deliveryCount().get());
+ assertEquals((short) 2, records.records(tp).get(1).deliveryCount().get());
+ }
+ }
+
+ @ClusterTest(
+ serverProperties = {
+ @ClusterConfigProperty(key = "group.share.delivery.count.limit", value = "3"),
+ }
+ )
+ public void testBehaviorOnDeliveryCountBoundary() {
+ alterShareAutoOffsetReset("group1", "earliest");
+ try (Producer<byte[], byte[]> producer = createProducer();
+ ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer(
+ "group1",
+ Map.of(ConsumerConfig.SHARE_ACKNOWLEDGEMENT_MODE_CONFIG, EXPLICIT))) {
+
+ ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null,
+ "key".getBytes(), "value".getBytes());
+ producer.send(record);
+ producer.flush();
+
+ shareConsumer.subscribe(Set.of(tp.topic()));
+ ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 2500L, 1);
+ assertEquals(1, records.count());
+ assertEquals((short) 1, records.records(tp).get(0).deliveryCount().get());
+ // Acknowledge the record with AcknowledgeType.RELEASE.
+ shareConsumer.acknowledge(records.records(tp).get(0), AcknowledgeType.RELEASE);
+ Map<TopicIdPartition, Optional<KafkaException>> result = shareConsumer.commitSync();
+ assertEquals(1, result.size());
+
+ // Consume again, the delivery count should be 2.
+ records = waitedPoll(shareConsumer, 2500L, 1);
+ assertEquals(1, records.count());
+ assertEquals((short) 2, records.records(tp).get(0).deliveryCount().get());
+ }
+
+ // Start again and same record should be delivered
+ try (ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer("group1", Map.of())) {
+ shareConsumer.subscribe(Set.of(tp.topic()));
+ ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 2500L, 1);
+ assertEquals(1, records.count());
+ assertEquals((short) 3, records.records(tp).get(0).deliveryCount().get());
+ }
+ }
+
+ @ClusterTest
+ public void testReadCommittedIsolationLevel() {
+ alterShareAutoOffsetReset("group1", "earliest");
+ alterShareIsolationLevel("group1", "read_committed");
+ try (Producer<byte[], byte[]> transactionalProducer = createProducer("T1");
+ ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer("group1")) {
+ produceCommittedAndAbortedTransactionsInInterval(transactionalProducer, 10, 5);
+ shareConsumer.subscribe(Set.of(tp.topic()));
+ ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 5000L, 8);
+ // 5th and 10th message transaction was aborted, hence they won't be included in the fetched records.
+ assertEquals(8, records.count());
+ int messageCounter = 1;
+ for (ConsumerRecord<byte[], byte[]> record : records) {
+ assertEquals(tp.topic(), record.topic());
+ assertEquals(tp.partition(), record.partition());
+ if (messageCounter % 5 == 0)
+ messageCounter++;
+ assertEquals("Message " + messageCounter, new String(record.value()));
+ messageCounter++;
+ }
+ }
+ verifyShareGroupStateTopicRecordsProduced();
+ }
+
+ @ClusterTest
+ public void testReadUncommittedIsolationLevel() {
+ alterShareAutoOffsetReset("group1", "earliest");
+ alterShareIsolationLevel("group1", "read_uncommitted");
+ try (Producer<byte[], byte[]> transactionalProducer = createProducer("T1");
+ ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer("group1")) {
+ produceCommittedAndAbortedTransactionsInInterval(transactionalProducer, 10, 5);
+ shareConsumer.subscribe(Set.of(tp.topic()));
+ ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 5000L, 10);
+ // Even though 5th and 10th message transaction was aborted, they will be included in the fetched records since IsolationLevel is READ_UNCOMMITTED.
+ assertEquals(10, records.count());
+ int messageCounter = 1;
+ for (ConsumerRecord<byte[], byte[]> record : records) {
+ assertEquals(tp.topic(), record.topic());
+ assertEquals(tp.partition(), record.partition());
+ assertEquals("Message " + messageCounter, new String(record.value()));
+ messageCounter++;
+ }
+ }
+ verifyShareGroupStateTopicRecordsProduced();
+ }
+
+ @ClusterTest
+ public void testAlterReadUncommittedToReadCommittedIsolationLevel() {
+ alterShareAutoOffsetReset("group1", "earliest");
+ alterShareIsolationLevel("group1", "read_uncommitted");
+ try (Producer<byte[], byte[]> transactionalProducer = createProducer("T1");
+ ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer("group1", Map.of(
+ ConsumerConfig.SHARE_ACKNOWLEDGEMENT_MODE_CONFIG, EXPLICIT))) {
+ shareConsumer.subscribe(Set.of(tp.topic()));
+ transactionalProducer.initTransactions();
+ try {
+ // First transaction is committed.
+ produceCommittedTransaction(transactionalProducer, "Message 1");
+
+ ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 5000L, 1);
+ assertEquals(1, records.count());
+ ConsumerRecord<byte[], byte[]> record = records.iterator().next();
+ assertEquals("Message 1", new String(record.value()));
+ assertEquals(tp.topic(), record.topic());
+ assertEquals(tp.partition(), record.partition());
+ records.forEach(consumedRecord -> shareConsumer.acknowledge(consumedRecord, AcknowledgeType.ACCEPT));
+ shareConsumer.commitSync();
+
+ // Second transaction is aborted.
+ produceAbortedTransaction(transactionalProducer, "Message 2");
+
+ records = waitedPoll(shareConsumer, 5000L, 1);
+ assertEquals(1, records.count());
+ record = records.iterator().next();
+ assertEquals("Message 2", new String(record.value()));
+ records.forEach(consumedRecord -> shareConsumer.acknowledge(consumedRecord, AcknowledgeType.ACCEPT));
+ shareConsumer.commitSync();
+
+ // Third transaction is committed.
+ produceCommittedTransaction(transactionalProducer, "Message 3");
+ // Fourth transaction is aborted.
+ produceAbortedTransaction(transactionalProducer, "Message 4");
+
+ records = waitedPoll(shareConsumer, 5000L, 2);
+ // Message 3 and Message 4 would be returned by this poll.
+ assertEquals(2, records.count());
+ Iterator<ConsumerRecord<byte[], byte[]>> recordIterator = records.iterator();
+ record = recordIterator.next();
+ assertEquals("Message 3", new String(record.value()));
+ record = recordIterator.next();
+ assertEquals("Message 4", new String(record.value()));
+ // We will make Message 3 and Message 4 available for re-consumption.
+ records.forEach(consumedRecord -> shareConsumer.acknowledge(consumedRecord, AcknowledgeType.RELEASE));
+ shareConsumer.commitSync();
+
+ // We are altering IsolationLevel to READ_COMMITTED now. We will only read committed transactions now.
+ alterShareIsolationLevel("group1", "read_committed");
+
+ // Fifth transaction is committed.
+ produceCommittedTransaction(transactionalProducer, "Message 5");
+ // Sixth transaction is aborted.
+ produceAbortedTransaction(transactionalProducer, "Message 6");
+ // Seventh transaction is aborted.
+ produceAbortedTransaction(transactionalProducer, "Message 7");
+ // Eighth transaction is committed.
+ produceCommittedTransaction(transactionalProducer, "Message 8");
+
+ // Since isolation level is READ_COMMITTED, we can consume Message 3 (committed transaction that was released), Message 5 and Message 8.
+ // We cannot consume Message 4 (aborted transaction that was released), Message 6 and Message 7 since they were aborted.
+ List<String> messages = new ArrayList<>();
+ TestUtils.waitForCondition(() -> {
+ ConsumerRecords<byte[], byte[]> pollRecords = shareConsumer.poll(Duration.ofMillis(5000));
+ if (pollRecords.count() > 0) {
+ for (ConsumerRecord<byte[], byte[]> pollRecord : pollRecords)
+ messages.add(new String(pollRecord.value()));
+ pollRecords.forEach(consumedRecord -> shareConsumer.acknowledge(consumedRecord, AcknowledgeType.ACCEPT));
+ shareConsumer.commitSync();
+ }
+ return messages.size() == 3;
+ }, DEFAULT_MAX_WAIT_MS, 100L, () -> "Failed to consume all records post altering share isolation level");
+
+ assertEquals("Message 3", messages.get(0));
+ assertEquals("Message 5", messages.get(1));
+ assertEquals("Message 8", messages.get(2));
+ } catch (InterruptedException e) {
+ throw new RuntimeException(e);
+ } finally {
+ transactionalProducer.close();
+ }
+ }
+ verifyShareGroupStateTopicRecordsProduced();
+ }
+
+ @ClusterTest
+ public void testAlterReadCommittedToReadUncommittedIsolationLevelWithReleaseAck() {
+ alterShareAutoOffsetReset("group1", "earliest");
+ alterShareIsolationLevel("group1", "read_committed");
+ try (Producer<byte[], byte[]> transactionalProducer = createProducer("T1");
+ ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer("group1", Map.of(
+ ConsumerConfig.SHARE_ACKNOWLEDGEMENT_MODE_CONFIG, EXPLICIT))) {
+ shareConsumer.subscribe(Set.of(tp.topic()));
+ transactionalProducer.initTransactions();
+
+ try {
+ // First transaction is committed.
+ produceCommittedTransaction(transactionalProducer, "Message 1");
+
+ ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 5000L, 1);
+ assertEquals(1, records.count());
+ ConsumerRecord<byte[], byte[]> record = records.iterator().next();
+ assertEquals("Message 1", new String(record.value()));
+ assertEquals(tp.topic(), record.topic());
+ assertEquals(tp.partition(), record.partition());
+ records.forEach(consumedRecord -> shareConsumer.acknowledge(consumedRecord, AcknowledgeType.ACCEPT));
+ shareConsumer.commitSync();
+
+ // Second transaction is aborted.
+ produceAbortedTransaction(transactionalProducer, "Message 2");
+
+ // Setting the acknowledgement commit callback to verify acknowledgement completion.
+ Map<TopicPartition, Set<Long>> partitionOffsetsMap = new HashMap<>();
+ shareConsumer.setAcknowledgementCommitCallback(new TestableAcknowledgementCommitCallback(partitionOffsetsMap, Map.of()));
+
+ // We will not receive any records since the transaction for Message 2 was aborted. Wait for the
+ // aborted marker offset for Message 2 (3L) to be fetched and acknowledged by the consumer.
+ TestUtils.waitForCondition(() -> {
+ ConsumerRecords<byte[], byte[]> pollRecords = shareConsumer.poll(Duration.ofMillis(500));
+ return pollRecords.count() == 0 && partitionOffsetsMap.containsKey(tp) && partitionOffsetsMap.get(tp).contains(3L);
+ }, DEFAULT_MAX_WAIT_MS, 100L, () -> "Failed to consume abort transaction and marker offset for Message 2");
+
+ // Third transaction is committed.
+ produceCommittedTransaction(transactionalProducer, "Message 3");
+ // Fourth transaction is aborted.
+ produceAbortedTransaction(transactionalProducer, "Message 4");
+
+ // Setting the acknowledgement commit callback to verify acknowledgement completion.
+ Map<TopicPartition, Set<Long>> partitionOffsetsMap2 = new HashMap<>();
+ shareConsumer.setAcknowledgementCommitCallback(new TestableAcknowledgementCommitCallback(partitionOffsetsMap2, Map.of()));
+
+ records = waitedPoll(shareConsumer, 5000L, 1);
+ // Message 3 would be returned by this poll.
+ assertEquals(1, records.count());
+ record = records.iterator().next();
+ assertEquals("Message 3", new String(record.value()));
+ // We will make Message 3 available for re-consumption.
+ records.forEach(consumedRecord -> shareConsumer.acknowledge(consumedRecord, AcknowledgeType.RELEASE));
+ shareConsumer.commitSync();
+
+ // Wait for the aborted marker offset for Message 4 (7L) to be fetched and acknowledged by the consumer.
+ TestUtils.waitForCondition(() -> {
+ ConsumerRecords<byte[], byte[]> pollRecords = shareConsumer.poll(Duration.ofMillis(500));
+ if (pollRecords.count() > 0) {
+ // We will release Message 3 again if it was received in this poll().
+ pollRecords.forEach(consumerRecord -> shareConsumer.acknowledge(consumerRecord, AcknowledgeType.RELEASE));
+ }
+ return partitionOffsetsMap2.containsKey(tp) && partitionOffsetsMap2.get(tp).contains(7L);
+ }, DEFAULT_MAX_WAIT_MS, 100L, () -> "Failed to consume abort transaction marker offset for Message 4");
+
+ // We are altering IsolationLevel to READ_UNCOMMITTED now. We will read both committed/aborted transactions now.
+ alterShareIsolationLevel("group1", "read_uncommitted");
+
+ // Fifth transaction is committed.
+ produceCommittedTransaction(transactionalProducer, "Message 5");
+ // Sixth transaction is aborted.
+ produceAbortedTransaction(transactionalProducer, "Message 6");
+ // Seventh transaction is aborted.
+ produceAbortedTransaction(transactionalProducer, "Message 7");
+ // Eighth transaction is committed.
+ produceCommittedTransaction(transactionalProducer, "Message 8");
+
+ // Since isolation level is READ_UNCOMMITTED, we can consume Message 3 (committed transaction that was released), Message 5, Message 6, Message 7 and Message 8.
+ Set<String> finalMessages = new HashSet<>();
+ TestUtils.waitForCondition(() -> {
+ ConsumerRecords<byte[], byte[]> pollRecords = shareConsumer.poll(Duration.ofMillis(5000));
+ if (pollRecords.count() > 0) {
+ for (ConsumerRecord<byte[], byte[]> pollRecord : pollRecords)
+ finalMessages.add(new String(pollRecord.value()));
+ pollRecords.forEach(consumedRecord -> shareConsumer.acknowledge(consumedRecord, AcknowledgeType.ACCEPT));
+ shareConsumer.commitSync();
+ }
+ return finalMessages.size() == 5;
+ }, DEFAULT_MAX_WAIT_MS, 100L, () -> "Failed to consume all records post altering share isolation level");
+
+ Set<String> expected = Set.of("Message 3", "Message 5", "Message 6", "Message 7", "Message 8");
+ assertEquals(expected, finalMessages);
+ } catch (InterruptedException e) {
+ throw new RuntimeException(e);
+ } finally {
+ transactionalProducer.close();
+ }
+ }
+ verifyShareGroupStateTopicRecordsProduced();
+ }
+
+ @ClusterTest
+ public void testAlterReadCommittedToReadUncommittedIsolationLevelWithRejectAck() {
+ alterShareAutoOffsetReset("group1", "earliest");
+ alterShareIsolationLevel("group1", "read_committed");
+ try (Producer<byte[], byte[]> transactionalProducer = createProducer("T1");
+ ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer("group1", Map.of(
+ ConsumerConfig.SHARE_ACKNOWLEDGEMENT_MODE_CONFIG, EXPLICIT))) {
+ shareConsumer.subscribe(Set.of(tp.topic()));
+ transactionalProducer.initTransactions();
+
+ try {
+ // First transaction is committed.
+ produceCommittedTransaction(transactionalProducer, "Message 1");
+
+ ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 5000L, 1);
+ assertEquals(1, records.count());
+ ConsumerRecord<byte[], byte[]> record = records.iterator().next();
+ assertEquals("Message 1", new String(record.value()));
+ assertEquals(tp.topic(), record.topic());
+ assertEquals(tp.partition(), record.partition());
+ records.forEach(consumedRecord -> shareConsumer.acknowledge(consumedRecord, AcknowledgeType.ACCEPT));
+ shareConsumer.commitSync();
+
+ // Second transaction is aborted.
+ produceAbortedTransaction(transactionalProducer, "Message 2");
+
+ // Setting the acknowledgement commit callback to verify acknowledgement completion.
+ Map<TopicPartition, Set<Long>> partitionOffsetsMap = new HashMap<>();
+ shareConsumer.setAcknowledgementCommitCallback(new TestableAcknowledgementCommitCallback(partitionOffsetsMap, Map.of()));
+
+ // We will not receive any records since the transaction for Message 2 was aborted. Wait for the
+ // aborted marker offset for Message 2 (3L) to be fetched and acknowledged by the consumer.
+ TestUtils.waitForCondition(() -> {
+ ConsumerRecords<byte[], byte[]> pollRecords = shareConsumer.poll(Duration.ofMillis(500));
+ return pollRecords.count() == 0 && partitionOffsetsMap.containsKey(tp) && partitionOffsetsMap.get(tp).contains(3L);
+ }, DEFAULT_MAX_WAIT_MS, 100L, () -> "Failed to consume abort transaction and marker offset for Message 2");
+
+ // Third transaction is committed.
+ produceCommittedTransaction(transactionalProducer, "Message 3");
+ // Fourth transaction is aborted.
+ produceAbortedTransaction(transactionalProducer, "Message 4");
+
+ // Setting the acknowledgement commit callback to verify acknowledgement completion.
+ Map<TopicPartition, Set<Long>> partitionOffsetsMap2 = new HashMap<>();
+ shareConsumer.setAcknowledgementCommitCallback(new TestableAcknowledgementCommitCallback(partitionOffsetsMap2, Map.of()));
+
+ records = waitedPoll(shareConsumer, 5000L, 1);
+ // Message 3 would be returned by this poll.
+ assertEquals(1, records.count());
+ record = records.iterator().next();
+ assertEquals("Message 3", new String(record.value()));
+ // We will make Message 3 available for re-consumption.
+ records.forEach(consumedRecord -> shareConsumer.acknowledge(consumedRecord, AcknowledgeType.REJECT));
+ shareConsumer.commitSync();
+
+ // Wait for the aborted marker offset for Message 4 (7L) to be fetched and acknowledged by the consumer.
+ TestUtils.waitForCondition(() -> {
+ shareConsumer.poll(Duration.ofMillis(500));
+ return partitionOffsetsMap2.containsKey(tp) && partitionOffsetsMap2.get(tp).contains(7L);
+ }, DEFAULT_MAX_WAIT_MS, 100L, () -> "Failed to consume abort transaction marker offset for Message 4");
+
+ // We are altering IsolationLevel to READ_UNCOMMITTED now. We will read both committed/aborted transactions now.
+ alterShareIsolationLevel("group1", "read_uncommitted");
+
+ // Fifth transaction is committed.
+ produceCommittedTransaction(transactionalProducer, "Message 5");
+ // Sixth transaction is aborted.
+ produceAbortedTransaction(transactionalProducer, "Message 6");
+ // Seventh transaction is aborted.
+ produceAbortedTransaction(transactionalProducer, "Message 7");
+ // Eighth transaction is committed.
+ produceCommittedTransaction(transactionalProducer, "Message 8");
+
+ // Since isolation level is READ_UNCOMMITTED, we can consume Message 5, Message 6, Message 7 and Message 8.
+ List<String> finalMessages = new ArrayList<>();
+ TestUtils.waitForCondition(() -> {
+ ConsumerRecords<byte[], byte[]> pollRecords = shareConsumer.poll(Duration.ofMillis(5000));
+ if (pollRecords.count() > 0) {
+ for (ConsumerRecord<byte[], byte[]> pollRecord : pollRecords)
+ finalMessages.add(new String(pollRecord.value()));
+ pollRecords.forEach(consumedRecord -> shareConsumer.acknowledge(consumedRecord, AcknowledgeType.ACCEPT));
+ shareConsumer.commitSync();
+ }
+ return finalMessages.size() == 4;
+ }, DEFAULT_MAX_WAIT_MS, 100L, () -> "Failed to consume all records post altering share isolation level");
+
+ assertEquals("Message 5", finalMessages.get(0));
+ assertEquals("Message 6", finalMessages.get(1));
+ assertEquals("Message 7", finalMessages.get(2));
+ assertEquals("Message 8", finalMessages.get(3));
+ } catch (InterruptedException e) {
+ throw new RuntimeException(e);
+ } finally {
+ transactionalProducer.close();
+ }
+ }
+ verifyShareGroupStateTopicRecordsProduced();
+ }
+
+ @ClusterTest
+ public void testPollInBatchOptimizedMode() {
+ alterShareAutoOffsetReset("group1", "earliest");
+ try (Producer<byte[], byte[]> producer = createProducer();
+ ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer(
+ "group1",
+ Map.of(ConsumerConfig.MAX_POLL_RECORDS_CONFIG, 5, ConsumerConfig.SHARE_ACQUIRE_MODE_CONFIG, BATCH_OPTIMIZED))
+ ) {
+ ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), "value".getBytes());
+ for (int i = 0; i < 10; i++) {
+ producer.send(record);
+ }
+ producer.flush();
+ shareConsumer.subscribe(List.of(tp.topic()));
+
+ // although max.poll.records is set to 5, in batch optimized mode we will still get all 10 records.
+ ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 2500L, 10);
+ assertEquals(10, records.count());
+ verifyShareGroupStateTopicRecordsProduced();
+ }
+ }
+
+ @ClusterTest
+ public void testPollInRecordLimitMode() {
+ alterShareAutoOffsetReset("group1", "earliest");
+ try (Producer<byte[], byte[]> producer = createProducer();
+ ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer(
+ "group1",
+ Map.of(ConsumerConfig.MAX_POLL_RECORDS_CONFIG, 5, ConsumerConfig.SHARE_ACQUIRE_MODE_CONFIG, RECORD_LIMIT))
+ ) {
+ ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), "value".getBytes());
+ for (int i = 0; i < 10; i++) {
+ producer.send(record);
+ }
+ producer.flush();
+ shareConsumer.subscribe(List.of(tp.topic()));
+
+ // In record limit mode we will get only up to max.poll.records number of records.
+ ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 2500L, 5);
+ assertEquals(5, records.count());
+ verifyShareGroupStateTopicRecordsProduced();
+ }
+ }
+
+ @ClusterTest
+ public void testPollAndExplicitAcknowledgeSingleMessageInRecordLimitMode() {
+ alterShareAutoOffsetReset("group1", "earliest");
+ try (Producer<byte[], byte[]> producer = createProducer();
+ ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer(
+ "group1",
+ Map.of(ConsumerConfig.MAX_POLL_RECORDS_CONFIG, 1,
+ ConsumerConfig.SHARE_ACQUIRE_MODE_CONFIG, RECORD_LIMIT,
+ ConsumerConfig.SHARE_ACKNOWLEDGEMENT_MODE_CONFIG, EXPLICIT))
+ ) {
+ ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), "value".getBytes());
+ for (int i = 0; i < 10; i++) {
+ producer.send(record);
+ }
+ producer.flush();
+ shareConsumer.subscribe(List.of(tp.topic()));
+
+ for (int i = 0; i < 10; i++) {
+ ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 2500L, 1);
+ assertEquals(1, records.count());
+ for (ConsumerRecord<byte[], byte[]> rec : records) {
+ shareConsumer.acknowledge(rec, AcknowledgeType.ACCEPT);
+ }
+ shareConsumer.commitSync();
+ }
+ verifyShareGroupStateTopicRecordsProduced();
+ }
+ }
+
+ @ClusterTest
+ public void testExplicitAcknowledgeSuccessInRecordLimitMode() {
+ alterShareAutoOffsetReset("group1", "earliest");
+ try (Producer<byte[], byte[]> producer = createProducer();
+ ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer(
+ "group1",
+ Map.of(ConsumerConfig.MAX_POLL_RECORDS_CONFIG, 10,
+ ConsumerConfig.SHARE_ACQUIRE_MODE_CONFIG, RECORD_LIMIT,
+ ConsumerConfig.SHARE_ACKNOWLEDGEMENT_MODE_CONFIG, EXPLICIT))
+ ) {
+ ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), "value".getBytes());
+ for (int i = 0; i < 15; i++) {
+ producer.send(record);
+ }
+ producer.flush();
+ shareConsumer.subscribe(List.of(tp.topic()));
+ ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 2500L, 10);
+ assertEquals(10, records.count());
+ for (ConsumerRecord<byte[], byte[]> rec : records) {
+ shareConsumer.acknowledge(rec, AcknowledgeType.ACCEPT);
+ }
+ shareConsumer.commitSync();
+
+ records = waitedPoll(shareConsumer, 2500L, 5);
+ assertEquals(5, records.count());
+ verifyShareGroupStateTopicRecordsProduced();
+ }
+ }
+
+ @ClusterTest
+ public void testExplicitAcknowledgeReleaseAcceptInRecordLimitMode() {
+ alterShareAutoOffsetReset("group1", "earliest");
+ try (Producer<byte[], byte[]> producer = createProducer();
+ ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer(
+ "group1",
+ Map.of(ConsumerConfig.MAX_POLL_RECORDS_CONFIG, 10,
+ ConsumerConfig.SHARE_ACQUIRE_MODE_CONFIG, RECORD_LIMIT,
+ ConsumerConfig.SHARE_ACKNOWLEDGEMENT_MODE_CONFIG, EXPLICIT))
+ ) {
+ for (int i = 0; i < 20; i++) {
+ ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp2.topic(), tp2.partition(), null, "key".getBytes(), ("Message " + i).getBytes());
+ producer.send(record);
+ }
+ producer.flush();
+ shareConsumer.subscribe(List.of(tp2.topic()));
+ ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 2500L, 10);
+ assertEquals(10, records.count());
+
+ int count = 0;
+ Map<TopicIdPartition, Optional<KafkaException>> result;
+ for (ConsumerRecord<byte[], byte[]> record : records) {
+ if (count % 2 == 0) {
+ shareConsumer.acknowledge(record, AcknowledgeType.ACCEPT);
+ } else {
+ shareConsumer.acknowledge(record, AcknowledgeType.RELEASE);
+ }
+ result = shareConsumer.commitSync();
+ assertEquals(1, result.size());
+ count++;
+ }
+
+ // Poll again to get 10 records.
+ records = waitedPoll(shareConsumer, 2500L, 10);
+ assertEquals(10, records.count());
+ for (ConsumerRecord<byte[], byte[]> record : records) {
+ shareConsumer.acknowledge(record, AcknowledgeType.ACCEPT);
+ }
+
+ // Get the rest of all 5 records.
+ records = waitedPoll(shareConsumer, 2500L, 5);
+ assertEquals(5, records.count());
+ verifyShareGroupStateTopicRecordsProduced();
+ }
+ }
+
+ @ClusterTest
+ public void testFetchWithThrottledDelivery() {
+ alterShareAutoOffsetReset("group1", "earliest");
+ try (Producer<byte[], byte[]> producer = createProducer();
+ ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer(
+ "group1",
+ Map.of(ConsumerConfig.SHARE_ACKNOWLEDGEMENT_MODE_CONFIG, EXPLICIT))
+ ) {
+ // Produce a batch of 100 messages
+ for (int i = 0; i < 100; i++) {
+ ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), ("Message " + i).getBytes());
+ producer.send(record);
+ }
+ producer.flush();
+
+ shareConsumer.subscribe(List.of(tp.topic()));
+ // Fetch records in 5 iterations, each time acknowledging with RELEASE. 5 is the default
+ // delivery limit hence we should see throttling from Math.ceil(5/2) = 3 fetches.
+ int throttleDeliveryLimit = 3;
+ for (int i = 0; i < 5; i++) {
+ // Adjust expected fetch count based on throttling. If i < throttleDeliveryLimit, we get full batch of 100.
+ // If i == 4 i.e. the last delivery, then we get 1 record.
+ // Otherwise, we get half the previous fetch count due to throttling. In this case, 100 >> (i - throttleDeliveryLimit + 1) it is 50 for i=3.
+ int expectedFetchCount = (i < throttleDeliveryLimit) ? 100 : ((i == 4) ? 1 : 50);
+ ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 2500L, expectedFetchCount);
+ assertEquals(expectedFetchCount, records.count());
+
+ records.forEach(record -> shareConsumer.acknowledge(record, AcknowledgeType.RELEASE));
+ Map<TopicIdPartition, Optional<KafkaException>> result = shareConsumer.commitSync();
+ assertEquals(1, result.size());
+ assertEquals(Optional.empty(),
+ result.get(new TopicIdPartition(tpId, tp.partition(), tp.topic())));
+ }
+
+ // Offset 0 has already reached the delivery limit hence shall be archived.
+ // Offset 1 to 49 shall be in last delivery attempt and hence 1 record per poll.
+ validateExpectedRecordsInEachPollAndRelease(shareConsumer, 1, 50, 1);
+ // Delivery limit 4.
+ validateExpectedRecordsInEachPollAndRelease(shareConsumer, 50, 100, 50);
+ // Delivery limit 5.
+ validateExpectedRecordsInEachPollAndRelease(shareConsumer, 50, 100, 1);
+ // Next poll should not have any records as all records have reached delivery limit.
+ ConsumerRecords<byte[], byte[]> records = shareConsumer.poll(Duration.ofMillis(2500L));
+ assertTrue(records.isEmpty(), "Records should be empty as all records have reached delivery limit. But received: " + records.count());
+ }
+ }
+
+ @ClusterTest(
+ serverProperties = {
+ @ClusterConfigProperty(key = "group.share.delivery.count.limit", value = "10"),
+ }
+ )
+ public void testFetchWithThrottledDeliveryBatchesWithIncreasedDeliveryLimit() {
+ alterShareAutoOffsetReset("group1", "earliest");
+ try (Producer<byte[], byte[]> producer = createProducer();
+ ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer(
+ "group1",
+ Map.of(
+ ConsumerConfig.SHARE_ACKNOWLEDGEMENT_MODE_CONFIG, EXPLICIT,
+ ConsumerConfig.MAX_POLL_RECORDS_CONFIG, 512
+ )
+ )
+ ) {
+ // Produce records in complete power of 2 to fully test the throttling behavior.
+ int producedMessageCount = 512;
+ // Produce a batch of 512 messages
+ for (int i = 0; i < producedMessageCount; i++) {
+ ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(),
+ null, "key".getBytes(), ("Message " + i).getBytes());
+ producer.send(record);
+ }
+ producer.flush();
+
+ // Map which defines expected fetch count for each delivery attempt from 1 to 10.
+ Map<Integer, Integer> expectedFetchCountMap = Map.of(1, 512, 2, 512, 3, 512, 4, 512, 5, 512,
+ 6, 256, 7, 128, 8, 64, 9, 32, 10, 1);
+ shareConsumer.subscribe(List.of(tp.topic()));
+ // Fetch records in 10 iterations, each time acknowledging with RELEASE. 10 is the
+ // delivery limit hence we should see throttling from Math.ceil(10/2) = 5 fetches.
+ for (int i = 0; i < 10; i++) {
+ int expectedFetchCount = expectedFetchCountMap.get(i + 1);
+ ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 2500L, expectedFetchCount);
+ assertEquals(expectedFetchCount, records.count());
+ // Acknowledge all records with RELEASE.
+ records.forEach(record -> shareConsumer.acknowledge(record, AcknowledgeType.RELEASE));
+ Map<TopicIdPartition, Optional<KafkaException>> result = shareConsumer.commitSync();
+ assertEquals(1, result.size());
+ assertEquals(Optional.empty(), result.get(new TopicIdPartition(tpId, tp.partition(), tp.topic())));
+ }
+
+ // Offset 0 is already verified above, so start from offset 1 and as it's last delivery cycle
+ // hence expectedRecords is 1 for each poll till offset 32.
+ // Delivery 10
+ validateExpectedRecordsInEachPollAndRelease(shareConsumer, 1, 32, 1);
+ // Delivery 9
+ validateExpectedRecordsInEachPollAndRelease(shareConsumer, 32, 64, 32);
+ // Delivery 10
+ validateExpectedRecordsInEachPollAndRelease(shareConsumer, 32, 64, 1);
+ // Delivery 8
+ validateExpectedRecordsInEachPollAndRelease(shareConsumer, 64, 128, 64);
+ // Delivery 9
+ validateExpectedRecordsInEachPollAndRelease(shareConsumer, 64, 96, 32);
+ // Delivery 10
+ validateExpectedRecordsInEachPollAndRelease(shareConsumer, 64, 96, 1);
+ // Delivery 9
+ validateExpectedRecordsInEachPollAndRelease(shareConsumer, 96, 128, 32);
+ // Delivery 10
+ validateExpectedRecordsInEachPollAndRelease(shareConsumer, 96, 128, 1);
+ // Delivery 7
+ validateExpectedRecordsInEachPollAndRelease(shareConsumer, 128, 256, 128);
+ // Delivery 8
+ validateExpectedRecordsInEachPollAndRelease(shareConsumer, 128, 192, 64);
+ // Delivery 9
+ validateExpectedRecordsInEachPollAndRelease(shareConsumer, 128, 160, 32);
+ // Delivery 10
+ validateExpectedRecordsInEachPollAndRelease(shareConsumer, 128, 160, 1);
+ // Delivery 9
+ validateExpectedRecordsInEachPollAndRelease(shareConsumer, 160, 192, 32);
+ // Delivery 10
+ validateExpectedRecordsInEachPollAndRelease(shareConsumer, 160, 192, 1);
+ // Delivery 8
+ validateExpectedRecordsInEachPollAndRelease(shareConsumer, 192, 256, 64);
+ // Delivery 9
+ validateExpectedRecordsInEachPollAndRelease(shareConsumer, 192, 224, 32);
+ // Delivery 10
+ validateExpectedRecordsInEachPollAndRelease(shareConsumer, 192, 224, 1);
+ // Delivery 9
+ validateExpectedRecordsInEachPollAndRelease(shareConsumer, 224, 256, 32);
+ // Delivery 10
+ validateExpectedRecordsInEachPollAndRelease(shareConsumer, 224, 256, 1);
+ // Delivery 6
+ validateExpectedRecordsInEachPollAndRelease(shareConsumer, 256, 512, 256);
+ // Delivery 7
+ validateExpectedRecordsInEachPollAndRelease(shareConsumer, 256, 384, 128);
+ // Delivery 8
+ validateExpectedRecordsInEachPollAndRelease(shareConsumer, 256, 320, 64);
+ // Delivery 9
+ validateExpectedRecordsInEachPollAndRelease(shareConsumer, 256, 288, 32);
+ // Delivery 10
+ validateExpectedRecordsInEachPollAndRelease(shareConsumer, 256, 288, 1);
+ // Delivery 9
+ validateExpectedRecordsInEachPollAndRelease(shareConsumer, 288, 320, 32);
+ // Delivery 10
+ validateExpectedRecordsInEachPollAndRelease(shareConsumer, 288, 320, 1);
+ // Delivery 8
+ validateExpectedRecordsInEachPollAndRelease(shareConsumer, 320, 384, 64);
+ // Delivery 9
+ validateExpectedRecordsInEachPollAndRelease(shareConsumer, 320, 352, 32);
+ // Delivery 10
+ validateExpectedRecordsInEachPollAndRelease(shareConsumer, 320, 352, 1);
+ // Delivery 9
+ validateExpectedRecordsInEachPollAndRelease(shareConsumer, 352, 384, 32);
+ // Delivery 10
+ validateExpectedRecordsInEachPollAndRelease(shareConsumer, 352, 384, 1);
+ // Delivery 7
+ validateExpectedRecordsInEachPollAndRelease(shareConsumer, 384, 512, 128);
+ // Delivery 8
+ validateExpectedRecordsInEachPollAndRelease(shareConsumer, 384, 448, 64);
+ // Delivery 9
+ validateExpectedRecordsInEachPollAndRelease(shareConsumer, 384, 416, 32);
+ // Delivery 10
+ validateExpectedRecordsInEachPollAndRelease(shareConsumer, 384, 416, 1);
+ // Delivery 9
+ validateExpectedRecordsInEachPollAndRelease(shareConsumer, 416, 448, 32);
+ // Delivery 10
+ validateExpectedRecordsInEachPollAndRelease(shareConsumer, 416, 448, 1);
+ // Delivery 8
+ validateExpectedRecordsInEachPollAndRelease(shareConsumer, 448, 512, 64);
+ // Delivery 9
+ validateExpectedRecordsInEachPollAndRelease(shareConsumer, 448, 480, 32);
+ // Delivery 10
+ validateExpectedRecordsInEachPollAndRelease(shareConsumer, 448, 480, 1);
+ // Delivery 9
+ validateExpectedRecordsInEachPollAndRelease(shareConsumer, 480, 512, 32);
+ // Delivery 10
+ validateExpectedRecordsInEachPollAndRelease(shareConsumer, 480, 512, 1);
+ // Next poll should not have any records as all records have reached delivery limit.
+ ConsumerRecords<byte[], byte[]> records = shareConsumer.poll(Duration.ofMillis(2500L));
+ assertTrue(records.isEmpty(), "Records should be empty as all records have reached delivery limit. But received: " + records.count());
+ }
+ }
+
+ @ClusterTest(
+ serverProperties = {
+ @ClusterConfigProperty(key = "group.share.delivery.count.limit", value = "10"),
+ }
+ )
+ public void testFetchWithThrottledDeliveryValidateDeliveryCount() throws InterruptedException {
+ alterShareAutoOffsetReset("group1", "earliest");
+ try (Producer<byte[], byte[]> producer = createProducer();
+ ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer(
+ "group1",
+ Map.of(ConsumerConfig.SHARE_ACKNOWLEDGEMENT_MODE_CONFIG, EXPLICIT))
+ ) {
+ int producedMessageCount = 500;
+ // Produce a batch of 500 messages
+ for (int i = 0; i < producedMessageCount; i++) {
+ ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(),
+ null, "key".getBytes(), ("Message " + i).getBytes());
+ producer.send(record);
+ }
+ producer.flush();
+
+ // Map to track delivery count for each offset.
+ Map<Long, Integer> offsetToDeliveryCountMap = new HashMap<>();
+ // Map which defines expected fetch count for each delivery attempt from 1 to 10.
+ Map<Integer, Integer> expectedFetchCountMap = Map.of(1, 500, 2, 500, 3, 500, 4, 500, 5, 500,
+ 6, 250, 7, 125, 8, 62, 9, 31, 10, 1);
+ shareConsumer.subscribe(List.of(tp.topic()));
+ // Fetch records in 10 iterations, each time acknowledging with RELEASE. 10 is the
+ // delivery limit hence we should see throttling from Math.ceil(10/2) = 5 fetches.
+ for (int i = 0; i < 10; i++) {
+ int expectedFetchCount = expectedFetchCountMap.get(i + 1);
+ ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 2500L, expectedFetchCount);
+ assertEquals(expectedFetchCount, records.count());
+ // Update delivery count for each offset.
+ records.forEach(record -> {
+ if (!offsetToDeliveryCountMap.containsKey(record.offset())) {
+ offsetToDeliveryCountMap.put(record.offset(), 1);
+ } else {
+ offsetToDeliveryCountMap.put(record.offset(), offsetToDeliveryCountMap.get(record.offset()) + 1);
+ }
+ });
+ // Acknowledge with RELEASE.
+ records.forEach(record -> shareConsumer.acknowledge(record, AcknowledgeType.RELEASE));
+ Map<TopicIdPartition, Optional<KafkaException>> result = shareConsumer.commitSync();
+ assertEquals(1, result.size());
+ assertEquals(Optional.empty(), result.get(new TopicIdPartition(tpId, tp.partition(), tp.topic())));
+ }
+
+ // Validate every offset is delivered at most till delivery limit.
+ waitForCondition(() -> {
+ ConsumerRecords<byte[], byte[]> records = shareConsumer.poll(Duration.ofMillis(2500L));
+ if (!records.isEmpty()) {
+ records.forEach(record -> {
+ if (!offsetToDeliveryCountMap.containsKey(record.offset())) {
+ offsetToDeliveryCountMap.put(record.offset(), 1);
+ } else {
+ offsetToDeliveryCountMap.put(record.offset(), offsetToDeliveryCountMap.get(record.offset()) + 1);
+ }
+ });
+ records.forEach(record -> shareConsumer.acknowledge(record, AcknowledgeType.RELEASE));
+ Map<TopicIdPartition, Optional<KafkaException>> result = shareConsumer.commitSync();
+ assertEquals(1, result.size());
+ assertEquals(Optional.empty(),
+ result.get(new TopicIdPartition(tpId, tp.partition(), tp.topic())));
+ }
+ return offsetToDeliveryCountMap.size() == 500 &&
+ offsetToDeliveryCountMap.values().stream().allMatch(deliveryCount -> deliveryCount == 10);
+ },
+ 120000L, // 120 seconds.
+ 50L,
+ () -> "failed to get records till delivery limit"
+ );
+
+ // Next poll should not have any records as all records have reached delivery limit.
+ ConsumerRecords<byte[], byte[]> records = shareConsumer.poll(Duration.ofMillis(2500L));
+ assertTrue(records.isEmpty(), "Records should be empty as all records have reached delivery limit. But received: " + records.count());
+ }
+ }
+
+ @ClusterTest(
+ serverProperties = {
+ @ClusterConfigProperty(key = "group.share.delivery.count.limit", value = "2"),
+ }
+ )
+ public void testFetchWithThrottledDeliveryBatchesWithDecreasedDeliveryLimit() {
+ alterShareAutoOffsetReset("group1", "earliest");
+ try (Producer<byte[], byte[]> producer = createProducer();
+ ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer(
+ "group1",
+ Map.of(
+ ConsumerConfig.SHARE_ACKNOWLEDGEMENT_MODE_CONFIG, EXPLICIT,
+ ConsumerConfig.MAX_POLL_RECORDS_CONFIG, 512
+ ))
+ ) {
+ // Produce records in complete power of 2 to fully test the throttling behavior.
+ int producedMessageCount = 512;
+ // Produce a batch of 512 messages
+ for (int i = 0; i < producedMessageCount; i++) {
+ ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(),
+ null, "key".getBytes(), ("Message " + i).getBytes());
+ producer.send(record);
+ }
+ producer.flush();
+
+ shareConsumer.subscribe(List.of(tp.topic()));
+ // Fetch records in 2 iterations, each time acknowledging with RELEASE. As throttling
+ // currently applies for delivery limit > 2, hence we should get full batch in both fetches.
+ validateExpectedRecordsInEachPollAndRelease(shareConsumer, 0, 512, 512);
+ validateExpectedRecordsInEachPollAndRelease(shareConsumer, 0, 512, 512);
+ // Next poll should not have any records as all records have reached delivery limit.
+ ConsumerRecords<byte[], byte[]> records = shareConsumer.poll(Duration.ofMillis(2500L));
+ assertTrue(records.isEmpty(), "Records should be empty as all records have reached delivery limit. But received: " + records.count());
+ }
+ }
+
+ @ClusterTest
+ public void testFetchWithThrottledDeliveryBatchesMultipleConsumers() {
+ alterShareAutoOffsetReset("group1", "earliest");
+ try (Producer<byte[], byte[]> producer = createProducer();
+ ShareConsumer<byte[], byte[]> shareConsumer1 = createShareConsumer(
+ "group1",
+ Map.of(
+ ConsumerConfig.SHARE_ACKNOWLEDGEMENT_MODE_CONFIG, EXPLICIT,
+ ConsumerConfig.MAX_POLL_RECORDS_CONFIG, 1
+ ));
+ ShareConsumer<byte[], byte[]> shareConsumer2 = createShareConsumer(
+ "group1",
+ Map.of(
+ ConsumerConfig.SHARE_ACKNOWLEDGEMENT_MODE_CONFIG, EXPLICIT,
+ ConsumerConfig.MAX_POLL_RECORDS_CONFIG, 2
+ ))
+ ) {
+ // Produce 2 records in separate batches.
+ for (int i = 0; i < 2; i++) {
+ ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(),
+ null, "key".getBytes(), ("Message " + i).getBytes());
+ producer.send(record);
+ // Flush immediately to create 2 different batches.
+ producer.flush();
+ }
+
+ shareConsumer1.subscribe(List.of(tp.topic()));
+ shareConsumer2.subscribe(List.of(tp.topic()));
+ // Fetch from consumer1 - should get 1 record as max.poll.records=1.
+ ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer1, 2500L, 1);
+ assertEquals(1, records.count());
+ // Verify the first offset of the fetched records.
+ assertEquals(0, records.iterator().next().offset());
+ // Fetch from consumer2 - should get 1 record as offset 0 is Acquired by consumer1.
+ // Release the record from consumer2 after fetching until the last delivery attempt.
+ validateExpectedRecordsInEachPollAndRelease(shareConsumer2, 1, 2, 1);
+ validateExpectedRecordsInEachPollAndRelease(shareConsumer2, 1, 2, 1);
+ validateExpectedRecordsInEachPollAndRelease(shareConsumer2, 1, 2, 1);
+ validateExpectedRecordsInEachPollAndRelease(shareConsumer2, 1, 2, 1);
+
+ // Now release the record from consumer1. Fetch again from consumer2 to verify it gets the released record.
+ // And should only get 1 record at offset 0 as offset 1 record is in final delivery attempt.
+ records.forEach(record -> shareConsumer1.acknowledge(record, AcknowledgeType.RELEASE));
+ Map<TopicIdPartition, Optional<KafkaException>> result = shareConsumer1.commitSync();
+ assertEquals(1, result.size());
+ assertEquals(Optional.empty(), result.get(new TopicIdPartition(tpId, tp.partition(), tp.topic())));
+ // Fetch from consumer2 - should get the released record at offset 0. Accept the record after fetching.
+ validateExpectedRecordsInEachPollAndAcknowledge(shareConsumer2, 0, 1, 1, AcknowledgeType.ACCEPT);
+ // Now fetch the last record at offset 1 from consumer2 in its final delivery attempt.
+ validateExpectedRecordsInEachPollAndAcknowledge(shareConsumer2, 1, 2, 1, AcknowledgeType.ACCEPT);
+
+ // Next poll from consumer1 should not have any records as all records have reached delivery limit.
+ records = shareConsumer1.poll(Duration.ofMillis(2500L));
+ assertTrue(records.isEmpty(), "Records should be empty as all records have reached delivery limit. But received: " + records.count());
+ }
+ }
+
+ @ClusterTest
+ public void testDynamicDeliveryCountLimitDecreaseArchivesRecords() {
+ alterShareAutoOffsetReset("group1", "earliest");
+ try (Producer<byte[], byte[]> producer = createProducer();
+ ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer(
+ "group1",
+ Map.of(ConsumerConfig.SHARE_ACKNOWLEDGEMENT_MODE_CONFIG, EXPLICIT))
+ ) {
+ // Produce 1 record.
+ ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(),
+ null, "key".getBytes(), "value".getBytes());
+ producer.send(record);
+ producer.flush();
+
+ shareConsumer.subscribe(Set.of(tp.topic()));
+
+ // Consume and release twice (deliveryCount becomes 2).
+ // Delivery 1: acquire (deliveryCount=1) → release (1 < default limit 5 → AVAILABLE).
+ ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 2500L, 1);
+ assertEquals(1, records.count());
+ records.forEach(r -> shareConsumer.acknowledge(r, AcknowledgeType.RELEASE));
+ shareConsumer.commitSync();
+
+ // Delivery 2: acquire (deliveryCount=2) → release (2 < 5 → AVAILABLE).
+ records = waitedPoll(shareConsumer, 2500L, 1);
+ assertEquals(1, records.count());
+ assertEquals((short) 2, records.records(tp).get(0).deliveryCount().get());
+ records.forEach(r -> shareConsumer.acknowledge(r, AcknowledgeType.RELEASE));
+ shareConsumer.commitSync();
+
+ // Dynamically decrease delivery count limit to 3 via group config.
+ alterShareDeliveryCountLimit("group1", "3");
+
+ // Delivery 3: acquire (deliveryCount=3) → release (3 >= 3 → ARCHIVED).
+ records = waitedPoll(shareConsumer, 2500L, 1);
+ assertEquals(1, records.count());
+ assertEquals((short) 3, records.records(tp).get(0).deliveryCount().get());
+ records.forEach(r -> shareConsumer.acknowledge(r, AcknowledgeType.RELEASE));
+ shareConsumer.commitSync();
+
+ // Next poll should have no records because the record was archived.
+ records = shareConsumer.poll(Duration.ofMillis(2500L));
+ assertTrue(records.isEmpty(),
+ "Records should be empty as the record was archived. But received: " + records.count());
+ }
+ }
+
+ @ClusterTest(
+ serverProperties = {
+ @ClusterConfigProperty(key = "group.share.delivery.count.limit", value = "3"),
+ }
+ )
+ public void testDynamicDeliveryCountLimitIncreaseAllowsMoreDeliveries() {
+ alterShareAutoOffsetReset("group1", "earliest");
+ try (Producer<byte[], byte[]> producer = createProducer();
+ ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer(
+ "group1",
+ Map.of(ConsumerConfig.SHARE_ACKNOWLEDGEMENT_MODE_CONFIG, EXPLICIT))
+ ) {
+ // Produce 1 record.
+ ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(),
+ null, "key".getBytes(), "value".getBytes());
+ producer.send(record);
+ producer.flush();
+
+ shareConsumer.subscribe(Set.of(tp.topic()));
+
+ // Consume and release twice (deliveryCount becomes 2).
+ // Delivery 1: acquire (deliveryCount=1) → release (1 < broker limit 3 → AVAILABLE).
+ ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 2500L, 1);
+ assertEquals(1, records.count());
+ records.forEach(r -> shareConsumer.acknowledge(r, AcknowledgeType.RELEASE));
+ shareConsumer.commitSync();
+
+ // Delivery 2: acquire (deliveryCount=2) → release (2 < 3 → AVAILABLE).
+ records = waitedPoll(shareConsumer, 2500L, 1);
+ assertEquals(1, records.count());
+ assertEquals((short) 2, records.records(tp).get(0).deliveryCount().get());
+ records.forEach(r -> shareConsumer.acknowledge(r, AcknowledgeType.RELEASE));
+ shareConsumer.commitSync();
+
+ // Dynamically increase delivery count limit to 5 via group config.
+ alterShareDeliveryCountLimit("group1", "5");
+
+ // Delivery 3: acquire (deliveryCount=3) → release (3 < 5 → AVAILABLE).
+ // Without the config increase, 3 >= 3 would have caused archival.
+ records = waitedPoll(shareConsumer, 2500L, 1);
+ assertEquals(1, records.count());
+ assertEquals((short) 3, records.records(tp).get(0).deliveryCount().get());
+ records.forEach(r -> shareConsumer.acknowledge(r, AcknowledgeType.RELEASE));
+ shareConsumer.commitSync();
+
+ // Delivery 4: acquire (deliveryCount=4) → release (4 < 5 → AVAILABLE).
+ records = waitedPoll(shareConsumer, 2500L, 1);
+ assertEquals(1, records.count());
+ assertEquals((short) 4, records.records(tp).get(0).deliveryCount().get());
+ records.forEach(r -> shareConsumer.acknowledge(r, AcknowledgeType.RELEASE));
+ shareConsumer.commitSync();
+
+ // Delivery 5: acquire (deliveryCount=5) → release (5 >= 5 → ARCHIVED).
+ records = waitedPoll(shareConsumer, 2500L, 1);
+ assertEquals(1, records.count());
+ assertEquals((short) 5, records.records(tp).get(0).deliveryCount().get());
+ records.forEach(r -> shareConsumer.acknowledge(r, AcknowledgeType.RELEASE));
+ shareConsumer.commitSync();
+
+ // Next poll should have no records because the record was archived.
+ records = shareConsumer.poll(Duration.ofMillis(2500L));
+ assertTrue(records.isEmpty(),
+ "Records should be empty as the record was archived. But received: " + records.count());
+ }
+ }
+
+ @ClusterTest
+ public void testDynamicPartitionMaxRecordLocks() {
+ // Verify that the group-level share.partition.max.record.locks config can be
+ // dynamically set and read back via describe configs.
+ alterSharePartitionMaxRecordLocks("group1", "500");
+
+ // Verify the config can be updated dynamically.
+ alterSharePartitionMaxRecordLocks("group1", "1000");
+ }
+}
\ No newline at end of file
diff --git a/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/ShareConsumerLagTest.java b/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/ShareConsumerLagTest.java
new file mode 100644
index 00000000000..372acaec371
--- /dev/null
+++ b/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/ShareConsumerLagTest.java
@@ -0,0 +1,418 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.clients.consumer;
+
+import kafka.server.KafkaBroker;
+
+import org.apache.kafka.clients.admin.Admin;
+import org.apache.kafka.clients.producer.Producer;
+import org.apache.kafka.clients.producer.ProducerRecord;
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.internals.Topic;
+import org.apache.kafka.common.test.ClusterInstance;
+import org.apache.kafka.common.test.api.ClusterConfigProperty;
+import org.apache.kafka.common.test.api.ClusterTest;
+import org.apache.kafka.common.test.api.ClusterTestDefaults;
+import org.apache.kafka.common.test.api.Type;
+import org.apache.kafka.test.TestUtils;
+
+import org.junit.jupiter.api.Timeout;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.ExecutionException;
+
+import static org.apache.kafka.test.TestUtils.DEFAULT_MAX_WAIT_MS;
+import static org.apache.kafka.test.TestUtils.DEFAULT_POLL_INTERVAL_MS;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.fail;
+
+@Timeout(1200)
+@ClusterTestDefaults(
+ types = {Type.KRAFT},
+ serverProperties = {
+ @ClusterConfigProperty(key = "auto.create.topics.enable", value = "false"),
+ @ClusterConfigProperty(key = "group.share.max.partition.max.record.locks", value = "10000"),
+ @ClusterConfigProperty(key = "group.share.partition.max.record.locks", value = "10000"),
+ @ClusterConfigProperty(key = "group.share.record.lock.duration.ms", value = "15000"),
+ @ClusterConfigProperty(key = "offsets.topic.replication.factor", value = "1"),
+ @ClusterConfigProperty(key = "share.coordinator.state.topic.min.isr", value = "1"),
+ @ClusterConfigProperty(key = "share.coordinator.state.topic.num.partitions", value = "3"),
+ @ClusterConfigProperty(key = "share.coordinator.state.topic.replication.factor", value = "1"),
+ @ClusterConfigProperty(key = "transaction.state.log.min.isr", value = "1"),
+ @ClusterConfigProperty(key = "transaction.state.log.replication.factor", value = "1")
+ }
+)
+public class ShareConsumerLagTest extends ShareConsumerTestBase {
+
+ public ShareConsumerLagTest(ClusterInstance cluster) {
+ super(cluster);
+ }
+
+ @ClusterTest
+ public void testSharePartitionLagForSingleShareConsumer() {
+ String groupId = "group1";
+ alterShareAutoOffsetReset(groupId, "earliest");
+ try (Producer<byte[], byte[]> producer = createProducer();
+ ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer(groupId);
+ Admin adminClient = createAdminClient()) {
+ ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), "Message".getBytes());
+ producer.send(record);
+ producer.flush();
+ shareConsumer.subscribe(List.of(tp.topic()));
+ // Polling share consumer to make sure the share partition is created and the record is consumed.
+ waitedPoll(shareConsumer, 2500L, 1);
+ // Acknowledge and commit the consumed record to update the share partition state.
+ shareConsumer.commitSync();
+ // After the acknowledgement is successful, the share partition lag should be 0 because the only produced record has been consumed.
+ verifySharePartitionLag(adminClient, groupId, tp, 0L);
+ // Producing another record to the share partition.
+ producer.send(record);
+ producer.flush();
+ // Since the new record has not been consumed yet, the share partition lag should be 1.
+ verifySharePartitionLag(adminClient, groupId, tp, 1L);
+ } catch (InterruptedException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ @ClusterTest
+ public void testSharePartitionLagForMultipleShareConsumers() {
+ String groupId = "group1";
+ alterShareAutoOffsetReset(groupId, "earliest");
+ try (Producer<byte[], byte[]> producer = createProducer();
+ ShareConsumer<byte[], byte[]> shareConsumer1 = createShareConsumer(groupId);
+ ShareConsumer<byte[], byte[]> shareConsumer2 = createShareConsumer(groupId);
+ Admin adminClient = createAdminClient()) {
+ ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), "Message".getBytes());
+ producer.send(record);
+ producer.flush();
+ shareConsumer1.subscribe(List.of(tp.topic()));
+ shareConsumer2.subscribe(List.of(tp.topic()));
+ // Polling share consumer 1 to make sure the share partition is created and the records are consumed.
+ waitedPoll(shareConsumer1, 2500L, 1);
+ // Acknowledge and commit the consumed records to update the share partition state.
+ shareConsumer1.commitSync();
+ // After the acknowledgement is successful, the share partition lag should be 0 because the all produced records have been consumed.
+ verifySharePartitionLag(adminClient, groupId, tp, 0L);
+ // Producing more records to the share partition.
+ producer.send(record);
+ // Polling share consumer 2 this time.
+ waitedPoll(shareConsumer2, 2500L, 1);
+ // Since the consumed record hasn't been acknowledged yet, the share partition lag should be 1.
+ verifySharePartitionLag(adminClient, groupId, tp, 1L);
+ // Acknowledge and commit the consumed records to update the share partition state.
+ shareConsumer2.commitSync();
+ // After the acknowledgement is successful, the share partition lag should be 0 because the all produced records have been consumed.
+ verifySharePartitionLag(adminClient, groupId, tp, 0L);
+ // Producing another record to the share partition.
+ producer.send(record);
+ producer.flush();
+ // Since the new record has not been consumed yet, the share partition lag should be 1.
+ verifySharePartitionLag(adminClient, groupId, tp, 1L);
+ } catch (InterruptedException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ @ClusterTest
+ public void testSharePartitionLagWithReleaseAcknowledgement() {
+ String groupId = "group1";
+ alterShareAutoOffsetReset(groupId, "earliest");
+ try (Producer<byte[], byte[]> producer = createProducer();
+ ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer(groupId, Map.of(ConsumerConfig.SHARE_ACKNOWLEDGEMENT_MODE_CONFIG, EXPLICIT));
+ Admin adminClient = createAdminClient()) {
+ ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), "Message".getBytes());
+ producer.send(record);
+ producer.flush();
+ shareConsumer.subscribe(List.of(tp.topic()));
+ // Polling share consumer to make sure the share partition is created and the record is consumed.
+ ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 2500L, 1);
+ // Accept the record first to move the offset forward and register the state with persister.
+ records.forEach(r -> shareConsumer.acknowledge(r, AcknowledgeType.ACCEPT));
+ shareConsumer.commitSync();
+ // After accepting, the lag should be 0 because the record is consumed successfully.
+ verifySharePartitionLag(adminClient, groupId, tp, 0L);
+ // Producing another record to the share partition.
+ producer.send(record);
+ producer.flush();
+ // The produced record is consumed.
+ records = waitedPoll(shareConsumer, 2500L, 1);
+ // Now release the record - it should be available for redelivery.
+ records.forEach(r -> shareConsumer.acknowledge(r, AcknowledgeType.RELEASE));
+ shareConsumer.commitSync();
+ // After releasing the lag should be 1, because the record is released for redelivery.
+ verifySharePartitionLag(adminClient, groupId, tp, 1L);
+ // The record is now consumed again.
+ records = waitedPoll(shareConsumer, 2500L, 1);
+ // Accept the record to mark it as consumed.
+ records.forEach(r -> shareConsumer.acknowledge(r, AcknowledgeType.ACCEPT));
+ shareConsumer.commitSync();
+ // After accepting the record, the lag should be 0 because all the produced records have been consumed.
+ verifySharePartitionLag(adminClient, groupId, tp, 0L);
+ } catch (InterruptedException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ @ClusterTest
+ public void testSharePartitionLagWithRejectAcknowledgement() {
+ String groupId = "group1";
+ alterShareAutoOffsetReset(groupId, "earliest");
+ try (Producer<byte[], byte[]> producer = createProducer();
+ ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer(groupId, Map.of(ConsumerConfig.SHARE_ACKNOWLEDGEMENT_MODE_CONFIG, EXPLICIT));
+ Admin adminClient = createAdminClient()) {
+ ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), "Message".getBytes());
+ producer.send(record);
+ producer.flush();
+ shareConsumer.subscribe(List.of(tp.topic()));
+ // Polling share consumer to make sure the share partition is created and the record is consumed.
+ ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 2500L, 1);
+ // Accept the record first to move the offset forward and register the state with persister.
+ records.forEach(r -> shareConsumer.acknowledge(r, AcknowledgeType.ACCEPT));
+ shareConsumer.commitSync();
+ // After accepting, the lag should be 0 because the record is consumed successfully.
+ verifySharePartitionLag(adminClient, groupId, tp, 0L);
+ // Producing another record to the share partition.
+ producer.send(record);
+ producer.flush();
+ // The produced record is consumed.
+ records = waitedPoll(shareConsumer, 2500L, 1);
+ // Now reject the record - it should not be available for redelivery.
+ records.forEach(r -> shareConsumer.acknowledge(r, AcknowledgeType.REJECT));
+ shareConsumer.commitSync();
+ // After rejecting the lag should be 0, because the record is permanently rejected and offset moves forward.
+ verifySharePartitionLag(adminClient, groupId, tp, 0L);
+ } catch (InterruptedException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ @ClusterTest(
+ brokers = 3,
+ serverProperties = {
+ @ClusterConfigProperty(key = "offsets.topic.num.partitions", value = "1"),
+ @ClusterConfigProperty(key = "offsets.topic.replication.factor", value = "3"),
+ @ClusterConfigProperty(key = "share.coordinator.state.topic.num.partitions", value = "1"),
+ @ClusterConfigProperty(key = "share.coordinator.state.topic.replication.factor", value = "3")
+ }
+ )
+ public void testSharePartitionLagOnGroupCoordinatorMovement() {
+ String groupId = "group1";
+ alterShareAutoOffsetReset(groupId, "earliest");
+ try (Producer<byte[], byte[]> producer = createProducer();
+ ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer(groupId);
+ Admin adminClient = createAdminClient()) {
+ String topicName = "testTopicWithReplicas";
+ // Create a topic with replication factor 3
+ createTopic(topicName, 1, 3);
+ TopicPartition tp = new TopicPartition(topicName, 0);
+ ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), "Message".getBytes());
+ // Produce first record and consume it
+ producer.send(record);
+ producer.flush();
+ shareConsumer.subscribe(List.of(tp.topic()));
+ // Polling share consumer to make sure the share partition is created and the record is consumed.
+ waitedPoll(shareConsumer, 2500L, 1);
+ // Acknowledge and commit the consumed record to update the share partition state.
+ shareConsumer.commitSync();
+ // After the acknowledgement is successful, the share partition lag should be 0 because the only produced record has been consumed.
+ verifySharePartitionLag(adminClient, groupId, tp, 0L);
+ // Producing another record to the share partition.
+ producer.send(record);
+ producer.flush();
+ // Since the new record has not been consumed yet, the share partition lag should be 1.
+ verifySharePartitionLag(adminClient, groupId, tp, 1L);
+ List<Integer> curGroupCoordNodeId;
+ // Find the broker which is the group coordinator for the share group.
+ curGroupCoordNodeId = topicPartitionLeader(adminClient, Topic.GROUP_METADATA_TOPIC_NAME, 0);
+ assertEquals(1, curGroupCoordNodeId.size());
+ // Shut down the coordinator broker
+ KafkaBroker broker = cluster.brokers().get(curGroupCoordNodeId.get(0));
+ cluster.shutdownBroker(curGroupCoordNodeId.get(0));
+ // Wait for it to be completely shutdown
+ broker.awaitShutdown();
+ // Wait for the leaders of share coordinator, group coordinator and topic partition to be elected, if needed, on a different broker.
+ TestUtils.waitForCondition(() -> {
+ List<Integer> newShareCoordNodeId = topicPartitionLeader(adminClient, Topic.SHARE_GROUP_STATE_TOPIC_NAME, 0);
+ List<Integer> newGroupCoordNodeId = topicPartitionLeader(adminClient, Topic.GROUP_METADATA_TOPIC_NAME, 0);
+ List<Integer> newTopicPartitionLeader = topicPartitionLeader(adminClient, tp.topic(), tp.partition());
+
+ return newShareCoordNodeId.size() == 1 && !Objects.equals(newShareCoordNodeId.get(0), curGroupCoordNodeId.get(0)) &&
+ newGroupCoordNodeId.size() == 1 && !Objects.equals(newGroupCoordNodeId.get(0), curGroupCoordNodeId.get(0)) &&
+ newTopicPartitionLeader.size() == 1 && !Objects.equals(newTopicPartitionLeader.get(0), curGroupCoordNodeId.get(0));
+ }, DEFAULT_MAX_WAIT_MS, DEFAULT_POLL_INTERVAL_MS, () -> "Failed to elect new leaders after broker shutdown");
+ // After group coordinator shutdown, check that lag is still 1
+ verifySharePartitionLag(adminClient, groupId, tp, 1L);
+ } catch (InterruptedException | ExecutionException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ @ClusterTest(
+ brokers = 3,
+ serverProperties = {
+ @ClusterConfigProperty(key = "offsets.topic.num.partitions", value = "1"),
+ @ClusterConfigProperty(key = "offsets.topic.replication.factor", value = "3"),
+ @ClusterConfigProperty(key = "share.coordinator.state.topic.num.partitions", value = "1"),
+ @ClusterConfigProperty(key = "share.coordinator.state.topic.replication.factor", value = "3")
+ }
+ )
+ public void testSharePartitionLagOnShareCoordinatorMovement() {
+ String groupId = "group1";
+ alterShareAutoOffsetReset(groupId, "earliest");
+ try (Producer<byte[], byte[]> producer = createProducer();
+ ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer(groupId);
+ Admin adminClient = createAdminClient()) {
+ String topicName = "testTopicWithReplicas";
+ // Create a topic with replication factor 3
+ createTopic(topicName, 1, 3);
+ TopicPartition tp = new TopicPartition(topicName, 0);
+ ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), "Message".getBytes());
+ // Produce first record and consume it
+ producer.send(record);
+ producer.flush();
+ shareConsumer.subscribe(List.of(tp.topic()));
+ // Polling share consumer to make sure the share partition is created and the record is consumed.
+ waitedPoll(shareConsumer, 2500L, 1);
+ // Acknowledge and commit the consumed record to update the share partition state.
+ shareConsumer.commitSync();
+ // After the acknowledgement is successful, the share partition lag should be 0 because the only produced record has been consumed.
+ verifySharePartitionLag(adminClient, groupId, tp, 0L);
+ // Producing another record to the share partition.
+ producer.send(record);
+ producer.flush();
+ // Since the new record has not been consumed yet, the share partition lag should be 1.
+ verifySharePartitionLag(adminClient, groupId, tp, 1L);
+ List<Integer> curShareCoordNodeId;
+ // Find the broker which is the share coordinator for the share partition.
+ curShareCoordNodeId = topicPartitionLeader(adminClient, Topic.SHARE_GROUP_STATE_TOPIC_NAME, 0);
+ assertEquals(1, curShareCoordNodeId.size());
+ // Shut down the coordinator broker
+ KafkaBroker broker = cluster.brokers().get(curShareCoordNodeId.get(0));
+ cluster.shutdownBroker(curShareCoordNodeId.get(0));
+ // Wait for it to be completely shutdown
+ broker.awaitShutdown();
+ // Wait for the leaders of share coordinator, group coordinator and topic partition to be elected, if needed, on a different broker.
+ TestUtils.waitForCondition(() -> {
+ List<Integer> newShareCoordNodeId = topicPartitionLeader(adminClient, Topic.SHARE_GROUP_STATE_TOPIC_NAME, 0);
+ List<Integer> newGroupCoordNodeId = topicPartitionLeader(adminClient, Topic.GROUP_METADATA_TOPIC_NAME, 0);
+ List<Integer> newTopicPartitionLeader = topicPartitionLeader(adminClient, tp.topic(), tp.partition());
+
+ return newShareCoordNodeId.size() == 1 && !Objects.equals(newShareCoordNodeId.get(0), curShareCoordNodeId.get(0)) &&
+ newGroupCoordNodeId.size() == 1 && !Objects.equals(newGroupCoordNodeId.get(0), curShareCoordNodeId.get(0)) &&
+ newTopicPartitionLeader.size() == 1 && !Objects.equals(newTopicPartitionLeader.get(0), curShareCoordNodeId.get(0));
+ }, DEFAULT_MAX_WAIT_MS, DEFAULT_POLL_INTERVAL_MS, () -> "Failed to elect new leaders after broker shutdown");
+ // After share coordinator shutdown and new leader's election, check that lag is still 1
+ verifySharePartitionLag(adminClient, groupId, tp, 1L);
+ } catch (InterruptedException | ExecutionException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ @ClusterTest
+ public void testSharePartitionLagAfterAlterShareGroupOffsets() {
+ String groupId = "group1";
+ try (Producer<byte[], byte[]> producer = createProducer();
+ Admin adminClient = createAdminClient()) {
+ ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), "Message".getBytes());
+ // Producing 100 records to the topic partition.
+ for (int i = 0; i < 100; i++) {
+ producer.send(record);
+ }
+ producer.flush();
+
+ // Create a new share consumer. Since the share.auto.offset.reset is not altered, it should be latest by default.
+ ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer(groupId, Map.of(ConsumerConfig.SHARE_ACKNOWLEDGEMENT_MODE_CONFIG, EXPLICIT));
+ shareConsumer.subscribe(List.of(tp.topic()));
+ // Polling share consumer to make sure it joins the group and subscribes to the topic.
+ waitedPoll(shareConsumer, 2500L, 0, true, groupId, List.of(new TopicPartition(tp.topic(), 0)));
+ // Producing 5 additional records to the topic partition.
+ for (int i = 0; i < 5; i++) {
+ producer.send(record);
+ }
+ producer.flush();
+ // Polling share consumer to make sure the records are consumed.
+ ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 2500L, 5);
+ assertEquals(5, records.count());
+ // Accept the record first to move the offset forward and register the state with persister.
+ records.forEach(r -> shareConsumer.acknowledge(r, AcknowledgeType.ACCEPT));
+ shareConsumer.commitSync();
+ // After accepting, the lag should be 0 because the record is consumed successfully.
+ verifySharePartitionLag(adminClient, groupId, tp, 0L);
+ // Closing the share consumer so that the offsets can be altered.
+ shareConsumer.close();
+ // Alter the start offset of the share partition to 40.
+ alterShareGroupOffsets(adminClient, groupId, tp, 40L);
+ // After altering, the share partition start offset should be 40.
+ verifySharePartitionStartOffset(adminClient, groupId, tp, 40L);
+ // Verify that the lag is now 65 since the start offset is altered to 40 and there are total 105 records in the partition.
+ verifySharePartitionLag(adminClient, groupId, tp, 65L);
+ } catch (InterruptedException | ExecutionException e) {
+ fail("Test failed with exception: " + e.getMessage());
+ }
+ }
+
+ @ClusterTest
+ public void testSharePartitionLagAfterDeleteShareGroupOffsets() {
+ String groupId = "group1";
+ alterShareAutoOffsetReset(groupId, "earliest");
+ try (Producer<byte[], byte[]> producer = createProducer();
+ Admin adminClient = createAdminClient()) {
+ ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), "Message".getBytes());
+ // Producing 5 records to the topic partition.
+ for (int i = 0; i < 5; i++) {
+ producer.send(record);
+ }
+ producer.flush();
+ // Create a new share consumer.
+ ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer(groupId, Map.of(ConsumerConfig.SHARE_ACKNOWLEDGEMENT_MODE_CONFIG, EXPLICIT));
+ shareConsumer.subscribe(List.of(tp.topic()));
+ // Polling share consumer to make sure it joins the group and consumes the produced records.
+ ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 2500L, 5);
+ assertEquals(5, records.count());
+ // Accept the records first to move the offset forward and register the state with persister.
+ records.forEach(r -> shareConsumer.acknowledge(r, AcknowledgeType.ACCEPT));
+ shareConsumer.commitSync();
+ // After accepting, the lag should be 0 because the record is consumed successfully.
+ verifySharePartitionLag(adminClient, groupId, tp, 0L);
+ // Closing the share consumer so that the offsets can be deleted.
+ shareConsumer.close();
+ // Delete the share group offsets.
+ deleteShareGroupOffsets(adminClient, groupId, tp.topic());
+ // Verify that the share partition offsets are deleted.
+ verifySharePartitionOffsetsDeleted(adminClient, groupId, tp);
+ // Create a new share consumer.
+ ShareConsumer<byte[], byte[]> shareConsumer2 = createShareConsumer(groupId, Map.of(ConsumerConfig.SHARE_ACKNOWLEDGEMENT_MODE_CONFIG, EXPLICIT));
+ shareConsumer2.subscribe(List.of(tp.topic()));
+ // Since the offsets are deleted, the share consumer should consume from the beginning (share.auto.offset.reset is earliest).
+ // Thus, the consumer should consume all 5 records again.
+ records = waitedPoll(shareConsumer2, 2500L, 5);
+ assertEquals(5, records.count());
+ // Accept the records first to move the offset forward and register the state with persister.
+ records.forEach(r -> shareConsumer2.acknowledge(r, AcknowledgeType.ACCEPT));
+ shareConsumer2.commitSync();
+ // After accepting, the lag should be 0 because the records are consumed successfully.
+ verifySharePartitionLag(adminClient, groupId, tp, 0L);
+ // Closing the share consumer so that the offsets can be deleted.
+ shareConsumer2.close();
+ } catch (InterruptedException | ExecutionException e) {
+ fail("Test failed with exception: " + e.getMessage());
+ }
+ }
+}
\ No newline at end of file
diff --git a/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/ShareConsumerRackAwareTest.java b/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/ShareConsumerRackAwareTest.java
index dcf58b5f238..98dec6a6357 100644
--- a/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/ShareConsumerRackAwareTest.java
+++ b/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/ShareConsumerRackAwareTest.java
@@ -29,6 +29,7 @@ import org.apache.kafka.common.test.ClusterInstance;
import org.apache.kafka.common.test.api.ClusterConfigProperty;
import org.apache.kafka.common.test.api.ClusterTest;
import org.apache.kafka.common.test.api.ClusterTestDefaults;
+import org.apache.kafka.common.test.api.ClusterTests;
import org.apache.kafka.common.test.api.Type;
import org.apache.kafka.coordinator.group.GroupCoordinatorConfig;
import org.apache.kafka.test.TestUtils;
@@ -42,26 +43,26 @@ import java.util.concurrent.ExecutionException;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+@ClusterTestDefaults(
+ types = {Type.KRAFT},
+ brokers = 3,
+ serverProperties = {
+ @ClusterConfigProperty(id = 0, key = "broker.rack", value = "rack0"),
+ @ClusterConfigProperty(id = 1, key = "broker.rack", value = "rack1"),
+ @ClusterConfigProperty(id = 2, key = "broker.rack", value = "rack2"),
+ @ClusterConfigProperty(key = GroupCoordinatorConfig.SHARE_GROUP_ASSIGNORS_CONFIG, value = "org.apache.kafka.clients.consumer.RackAwareAssignor")
+ }
+)
public class ShareConsumerRackAwareTest {
- @ClusterTestDefaults(
- serverProperties = {
+ @ClusterTests({
+ @ClusterTest(serverProperties = {
@ClusterConfigProperty(key = GroupCoordinatorConfig.SHARE_GROUP_ASSIGNMENT_INTERVAL_MS_CONFIG, value = "0")
- }
- )
- public static class WithAssignmentBatchingDisabledTest extends ShareConsumerRackAwareTest {
- }
-
- @ClusterTest(
- types = {Type.KRAFT},
- brokers = 3,
- serverProperties = {
- @ClusterConfigProperty(id = 0, key = "broker.rack", value = "rack0"),
- @ClusterConfigProperty(id = 1, key = "broker.rack", value = "rack1"),
- @ClusterConfigProperty(id = 2, key = "broker.rack", value = "rack2"),
- @ClusterConfigProperty(key = GroupCoordinatorConfig.SHARE_GROUP_ASSIGNORS_CONFIG, value = "org.apache.kafka.clients.consumer.RackAwareAssignor")
- }
- )
+ }),
+ @ClusterTest(serverProperties = {
+ @ClusterConfigProperty(key = GroupCoordinatorConfig.SHARE_GROUP_ASSIGNMENT_INTERVAL_MS_CONFIG, value = "1000")
+ })
+ })
void testShareConsumerWithRackAwareAssignor(ClusterInstance clusterInstance) throws ExecutionException, InterruptedException {
String groupId = "group0";
String topic = "test-topic";
@@ -194,4 +195,4 @@ public class ShareConsumerRackAwareTest {
}
);
}
-}
+}
\ No newline at end of file
diff --git a/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/ShareConsumerRenewTest.java b/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/ShareConsumerRenewTest.java
new file mode 100644
index 00000000000..c5bd2650590
--- /dev/null
+++ b/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/ShareConsumerRenewTest.java
@@ -0,0 +1,304 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.clients.consumer;
+
+import org.apache.kafka.clients.producer.Producer;
+import org.apache.kafka.clients.producer.ProducerRecord;
+import org.apache.kafka.common.KafkaException;
+import org.apache.kafka.common.TopicIdPartition;
+import org.apache.kafka.common.errors.InvalidRecordStateException;
+import org.apache.kafka.common.test.ClusterInstance;
+import org.apache.kafka.common.test.api.ClusterConfigProperty;
+import org.apache.kafka.common.test.api.ClusterTest;
+import org.apache.kafka.common.test.api.ClusterTestDefaults;
+import org.apache.kafka.common.test.api.Type;
+import org.apache.kafka.server.metrics.KafkaYammerMetrics;
+
+import com.yammer.metrics.core.Meter;
+
+import org.junit.jupiter.api.Timeout;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+@Timeout(1200)
+@ClusterTestDefaults(
+ types = {Type.KRAFT},
+ serverProperties = {
+ @ClusterConfigProperty(key = "auto.create.topics.enable", value = "false"),
+ @ClusterConfigProperty(key = "group.share.max.partition.max.record.locks", value = "10000"),
+ @ClusterConfigProperty(key = "group.share.partition.max.record.locks", value = "10000"),
+ @ClusterConfigProperty(key = "group.share.record.lock.duration.ms", value = "15000"),
+ @ClusterConfigProperty(key = "offsets.topic.replication.factor", value = "1"),
+ @ClusterConfigProperty(key = "share.coordinator.state.topic.min.isr", value = "1"),
+ @ClusterConfigProperty(key = "share.coordinator.state.topic.num.partitions", value = "3"),
+ @ClusterConfigProperty(key = "share.coordinator.state.topic.replication.factor", value = "1"),
+ @ClusterConfigProperty(key = "transaction.state.log.min.isr", value = "1"),
+ @ClusterConfigProperty(key = "transaction.state.log.replication.factor", value = "1")
+ }
+)
+public class ShareConsumerRenewTest extends ShareConsumerTestBase {
+
+ public ShareConsumerRenewTest(ClusterInstance cluster) {
+ super(cluster);
+ }
+
+ @ClusterTest
+ public void testRenewAcknowledgementOnPoll() {
+ alterShareAutoOffsetReset("group1", "earliest");
+ try (Producer<byte[], byte[]> producer = createProducer();
+ ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer(
+ "group1",
+ Map.of(ConsumerConfig.SHARE_ACKNOWLEDGEMENT_MODE_CONFIG, EXPLICIT))
+ ) {
+ AtomicInteger acknowledgementsCommitted = new AtomicInteger(0);
+ shareConsumer.setAcknowledgementCommitCallback((offsetsByTopicPartition, exception) ->
+ offsetsByTopicPartition.forEach((tip, offsets) -> acknowledgementsCommitted.addAndGet(offsets.size())));
+
+ for (int i = 0; i < 10; i++) {
+ ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), ("Message " + i).getBytes());
+ producer.send(record);
+ }
+ producer.flush();
+
+ shareConsumer.subscribe(List.of(tp.topic()));
+ ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 2500L, 10);
+ assertEquals(10, records.count());
+
+ int count = 0;
+ for (ConsumerRecord<byte[], byte[]> record : records) {
+ if (count % 2 == 0) {
+ shareConsumer.acknowledge(record, AcknowledgeType.ACCEPT);
+ } else {
+ shareConsumer.acknowledge(record, AcknowledgeType.RENEW);
+ }
+ count++;
+ }
+
+ // Get the rest of all 5 records.
+ records = waitedPoll(shareConsumer, 2500L, 5);
+ assertEquals(5, records.count());
+ for (ConsumerRecord<byte[], byte[]> record : records) {
+ shareConsumer.acknowledge(record, AcknowledgeType.ACCEPT);
+ }
+
+ shareConsumer.commitSync();
+ assertEquals(15, acknowledgementsCommitted.get());
+ }
+ verifyYammerMetricCount("ackType=Renew", 5);
+ }
+
+ @ClusterTest
+ public void testRenewAcknowledgementOnCommitSync() {
+ alterShareAutoOffsetReset("group1", "earliest");
+ try (Producer<byte[], byte[]> producer = createProducer();
+ ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer(
+ "group1",
+ Map.of(ConsumerConfig.SHARE_ACKNOWLEDGEMENT_MODE_CONFIG, EXPLICIT))
+ ) {
+ AtomicInteger acknowledgementsCommitted = new AtomicInteger(0);
+ shareConsumer.setAcknowledgementCommitCallback((offsetsByTopicPartition, exception) ->
+ offsetsByTopicPartition.forEach((tip, offsets) -> acknowledgementsCommitted.addAndGet(offsets.size())));
+
+ for (int i = 0; i < 10; i++) {
+ ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), ("Message " + i).getBytes());
+ producer.send(record);
+ }
+ producer.flush();
+
+ shareConsumer.subscribe(List.of(tp.topic()));
+ ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 2500L, 10);
+ assertEquals(10, records.count());
+ assertEquals(Optional.of(15000), shareConsumer.acquisitionLockTimeoutMs());
+
+ // The updated acquisition lock timeout is only applied when the next poll is called.
+ alterShareRecordLockDurationMs("group1", 25000);
+
+ int count = 0;
+ Map<TopicIdPartition, Optional<KafkaException>> result;
+ for (ConsumerRecord<byte[], byte[]> record : records) {
+ if (count % 2 == 0) {
+ shareConsumer.acknowledge(record, AcknowledgeType.ACCEPT);
+ } else {
+ shareConsumer.acknowledge(record, AcknowledgeType.RENEW);
+ }
+ result = shareConsumer.commitSync();
+ assertEquals(1, result.size());
+ assertEquals(Optional.of(15000), shareConsumer.acquisitionLockTimeoutMs());
+ assertEquals(Optional.empty(), result.get(new TopicIdPartition(tpId, tp.partition(), tp.topic())));
+ count++;
+ }
+
+ // Get the rest of all 5 records.
+ records = waitedPoll(shareConsumer, 2500L, 5);
+ assertEquals(5, records.count());
+ assertEquals(Optional.of(25000), shareConsumer.acquisitionLockTimeoutMs());
+ for (ConsumerRecord<byte[], byte[]> record : records) {
+ shareConsumer.acknowledge(record, AcknowledgeType.ACCEPT);
+ }
+ }
+ verifyYammerMetricCount("ackType=Renew", 5);
+ }
+
+ @ClusterTest
+ public void testRenewAcknowledgementInvalidStateRecord() {
+ alterShareAutoOffsetReset("group1", "earliest");
+ try (Producer<byte[], byte[]> producer = createProducer();
+ ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer(
+ "group1",
+ Map.of(ConsumerConfig.SHARE_ACKNOWLEDGEMENT_MODE_CONFIG, EXPLICIT))
+ ) {
+ AtomicInteger acknowledgementsCommitted = new AtomicInteger(0);
+ shareConsumer.setAcknowledgementCommitCallback((offsetsByTopicPartition, exception) ->
+ offsetsByTopicPartition.forEach((tip, offsets) -> acknowledgementsCommitted.addAndGet(offsets.size())));
+
+ ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), "Message ".getBytes());
+ producer.send(record);
+ producer.flush();
+
+ shareConsumer.subscribe(List.of(tp.topic()));
+ ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 2500L, 1);
+ assertEquals(1, records.count());
+
+ for (ConsumerRecord<byte[], byte[]> rec : records) {
+ shareConsumer.acknowledge(rec, AcknowledgeType.REJECT);
+ shareConsumer.commitSync();
+ assertThrows(IllegalStateException.class, () -> shareConsumer.acknowledge(rec, AcknowledgeType.RENEW));
+ }
+ }
+ verifyYammerMetricCount("ackType=Renew", 0);
+ }
+
+ @ClusterTest
+ public void testRenewAcknowledgementDisabled() {
+ alterShareAutoOffsetReset("group1", "earliest");
+ alterShareRenewAcknowledgeEnable("group1", false);
+ try (Producer<byte[], byte[]> producer = createProducer();
+ ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer(
+ "group1",
+ Map.of(ConsumerConfig.SHARE_ACKNOWLEDGEMENT_MODE_CONFIG, EXPLICIT))
+ ) {
+ ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), "Message".getBytes());
+ producer.send(record);
+ producer.flush();
+
+ shareConsumer.subscribe(List.of(tp.topic()));
+ ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 2500L, 1);
+ assertEquals(1, records.count());
+
+ for (ConsumerRecord<byte[], byte[]> rec : records) {
+ shareConsumer.acknowledge(rec, AcknowledgeType.RENEW);
+ }
+
+ Map<TopicIdPartition, Optional<KafkaException>> result = shareConsumer.commitSync();
+ assertEquals(1, result.size());
+ Optional<KafkaException> error = result.get(new TopicIdPartition(tpId, tp.partition(), tp.topic()));
+ assertTrue(error.isPresent());
+ assertInstanceOf(InvalidRecordStateException.class, error.get());
+ }
+ }
+
+ @ClusterTest(
+ brokers = 1,
+ serverProperties = {
+ @ClusterConfigProperty(key = "group.share.record.lock.duration.ms", value = "12000"),
+ @ClusterConfigProperty(key = "group.share.min.record.lock.duration.ms", value = "12000"),
+ }
+ )
+ public void testRenewAcknowledgementNoResultInPoll() {
+ alterShareAutoOffsetReset("group1", "earliest");
+ try (Producer<byte[], byte[]> producer = createProducer();
+ ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer(
+ "group1",
+ Map.of(ConsumerConfig.SHARE_ACKNOWLEDGEMENT_MODE_CONFIG, EXPLICIT))
+ ) {
+ AtomicInteger acknowledgementsCommitted = new AtomicInteger(0);
+ shareConsumer.setAcknowledgementCommitCallback((offsetsByTopicPartition, exception) ->
+ offsetsByTopicPartition.forEach((tip, offsets) -> acknowledgementsCommitted.addAndGet(offsets.size())));
+
+ for (int i = 0; i < 10; i++) {
+ ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), ("Message " + i).getBytes());
+ producer.send(record);
+ }
+ producer.flush();
+
+ shareConsumer.subscribe(List.of(tp.topic()));
+ ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 2500L, 10);
+ assertEquals(10, records.count());
+
+ int count = 0;
+ for (ConsumerRecord<byte[], byte[]> record : records) {
+ if (count % 2 == 0) {
+ shareConsumer.acknowledge(record, AcknowledgeType.ACCEPT);
+ } else {
+ shareConsumer.acknowledge(record, AcknowledgeType.RENEW);
+ }
+ count++;
+ }
+
+ // 5 more records (total 15 produced).
+ for (int i = 10; i < 15; i++) {
+ ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), ("Message " + i).getBytes());
+ producer.send(record);
+ }
+
+ // Get the rest of all 5 records.
+ records = waitedPoll(shareConsumer, 11500L, 0); // This will send the acks but not return next 5 records (10-15)
+ assertEquals(10, acknowledgementsCommitted.get());
+ assertEquals(0, records.count());
+ verifyYammerMetricCount("ackType=Renew", 5);
+
+ // Renewal duration passed, now records will be back.
+ records = waitedPoll(shareConsumer, 2500L, 5); // Renewed records as well as 10-15 records.
+ assertEquals(5, records.count());
+ for (ConsumerRecord<byte[], byte[]> record : records) {
+ shareConsumer.acknowledge(record, AcknowledgeType.ACCEPT);
+ }
+
+ shareConsumer.commitSync();
+
+ records = waitedPoll(shareConsumer, 2500L, 5); // 10-15 records.
+ assertEquals(5, records.count());
+ for (ConsumerRecord<byte[], byte[]> record : records) {
+ shareConsumer.acknowledge(record, AcknowledgeType.ACCEPT);
+ }
+
+ shareConsumer.commitSync();
+
+ // Initial - 5 renew + 5 accept, Subsequent - 5 renewed accepted + 5 fresh accepted (10-15)
+ assertEquals(20, acknowledgementsCommitted.get());
+ }
+ verifyYammerMetricCount("ackType=Renew", 5);
+ }
+
+ private void verifyYammerMetricCount(String filterString, int count) {
+ com.yammer.metrics.core.Metric renewAck = KafkaYammerMetrics.defaultRegistry().allMetrics().entrySet().stream()
+ .filter(entry -> entry.getKey().toString().contains(filterString))
+ .findAny()
+ .orElseThrow(() -> new AssertionError("metric not found"))
+ .getValue();
+
+ assertEquals(count, ((Meter) renewAck).count());
+ }
+}
\ No newline at end of file
diff --git a/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/ShareConsumerTest.java b/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/ShareConsumerTest.java
index deac9011c78..6f188c6d1ea 100644
--- a/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/ShareConsumerTest.java
+++ b/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/ShareConsumerTest.java
@@ -19,20 +19,7 @@ package org.apache.kafka.clients.consumer;
import kafka.server.KafkaBroker;
import org.apache.kafka.clients.admin.Admin;
-import org.apache.kafka.clients.admin.AlterConfigOp;
-import org.apache.kafka.clients.admin.AlterConfigsOptions;
-import org.apache.kafka.clients.admin.AlterShareGroupOffsetsOptions;
-import org.apache.kafka.clients.admin.Config;
-import org.apache.kafka.clients.admin.ConfigEntry;
-import org.apache.kafka.clients.admin.CreateTopicsResult;
-import org.apache.kafka.clients.admin.DeleteShareGroupOffsetsOptions;
-import org.apache.kafka.clients.admin.DescribeShareGroupsOptions;
-import org.apache.kafka.clients.admin.ListShareGroupOffsetsOptions;
-import org.apache.kafka.clients.admin.ListShareGroupOffsetsResult;
-import org.apache.kafka.clients.admin.ListShareGroupOffsetsSpec;
-import org.apache.kafka.clients.admin.NewTopic;
import org.apache.kafka.clients.admin.RecordsToDelete;
-import org.apache.kafka.clients.admin.ShareMemberDescription;
import org.apache.kafka.clients.admin.SharePartitionOffsetInfo;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerConfig;
@@ -42,11 +29,8 @@ import org.apache.kafka.common.KafkaException;
import org.apache.kafka.common.TopicIdPartition;
import org.apache.kafka.common.TopicPartition;
import org.apache.kafka.common.Uuid;
-import org.apache.kafka.common.config.ConfigResource;
import org.apache.kafka.common.errors.GroupMaxSizeReachedException;
import org.apache.kafka.common.errors.InterruptException;
-import org.apache.kafka.common.errors.InvalidConfigurationException;
-import org.apache.kafka.common.errors.InvalidRecordStateException;
import org.apache.kafka.common.errors.InvalidTopicException;
import org.apache.kafka.common.errors.NotLeaderOrFollowerException;
import org.apache.kafka.common.errors.RecordDeserializationException;
@@ -57,7 +41,6 @@ import org.apache.kafka.common.errors.WakeupException;
import org.apache.kafka.common.header.Header;
import org.apache.kafka.common.header.Headers;
import org.apache.kafka.common.internals.Topic;
-import org.apache.kafka.common.network.ListenerName;
import org.apache.kafka.common.record.TimestampType;
import org.apache.kafka.common.serialization.ByteArrayDeserializer;
import org.apache.kafka.common.serialization.Deserializer;
@@ -71,55 +54,36 @@ import org.apache.kafka.common.test.api.ClusterTestDefaults;
import org.apache.kafka.common.test.api.ClusterTests;
import org.apache.kafka.common.test.api.Type;
import org.apache.kafka.common.utils.Utils;
-import org.apache.kafka.coordinator.group.GroupConfig;
import org.apache.kafka.coordinator.group.GroupCoordinatorConfig;
-import org.apache.kafka.coordinator.group.modern.share.ShareGroupConfig;
-import org.apache.kafka.server.metrics.KafkaYammerMetrics;
import org.apache.kafka.server.share.SharePartitionKey;
-import org.apache.kafka.server.util.ServerTestUtils;
-import org.apache.kafka.test.NoRetryException;
import org.apache.kafka.test.TestUtils;
-import com.yammer.metrics.core.Meter;
-
-import org.junit.jupiter.api.AfterEach;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Timeout;
import java.nio.ByteBuffer;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
-import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
-import java.util.Objects;
import java.util.Optional;
-import java.util.Properties;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
-import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
-import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
-import java.util.function.BiConsumer;
import java.util.function.Function;
-import java.util.function.Predicate;
-import java.util.stream.IntStream;
import java.util.stream.Stream;
import static org.apache.kafka.test.TestUtils.DEFAULT_MAX_WAIT_MS;
-import static org.apache.kafka.test.TestUtils.DEFAULT_POLL_INTERVAL_MS;
import static org.apache.kafka.test.TestUtils.waitForCondition;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -132,9 +96,7 @@ import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
-@SuppressWarnings({"ClassFanOutComplexity", "ClassDataAbstractionCoupling"})
@Timeout(1200)
-@Tag("integration")
@ClusterTestDefaults(
types = {Type.KRAFT},
serverProperties = {
@@ -150,42 +112,10 @@ import static org.junit.jupiter.api.Assertions.fail;
@ClusterConfigProperty(key = "transaction.state.log.replication.factor", value = "1")
}
)
-public class ShareConsumerTest {
- private final ClusterInstance cluster;
- private final TopicPartition tp = new TopicPartition("topic", 0);
- private Uuid tpId;
- private final TopicPartition tp2 = new TopicPartition("topic2", 0);
- private final TopicPartition warmupTp = new TopicPartition("warmup", 0);
- private List<TopicPartition> sgsTopicPartitions;
- private static final String KEY = "content-type";
- private static final String VALUE = "application/octet-stream";
- private static final String EXPLICIT = "explicit";
- private static final String IMPLICIT = "implicit";
- private static final String RECORD_LIMIT = "record_limit";
- private static final String BATCH_OPTIMIZED = "batch_optimized";
+public class ShareConsumerTest extends ShareConsumerTestBase {
public ShareConsumerTest(ClusterInstance cluster) {
- this.cluster = cluster;
- }
-
- @BeforeEach
- public void setup() {
- try {
- this.cluster.waitForReadyBrokers();
- tpId = createTopic("topic");
- createTopic("topic2");
- sgsTopicPartitions = IntStream.range(0, 3)
- .mapToObj(part -> new TopicPartition(Topic.SHARE_GROUP_STATE_TOPIC_NAME, part))
- .toList();
- this.warmup();
- } catch (Exception e) {
- fail(e);
- }
- }
-
- @AfterEach
- public void tearDown() {
- ServerTestUtils.clearYammerMetrics();
+ super(cluster);
}
@ClusterTest
@@ -278,10 +208,10 @@ public class ShareConsumerTest {
@ClusterTests({
@ClusterTest(serverProperties = {
- @ClusterConfigProperty(key = GroupCoordinatorConfig.CONSUMER_GROUP_ASSIGNMENT_INTERVAL_MS_CONFIG, value = "0")
+ @ClusterConfigProperty(key = GroupCoordinatorConfig.SHARE_GROUP_ASSIGNMENT_INTERVAL_MS_CONFIG, value = "0")
}),
@ClusterTest(serverProperties = {
- @ClusterConfigProperty(key = GroupCoordinatorConfig.CONSUMER_GROUP_ASSIGNMENT_INTERVAL_MS_CONFIG, value = "1000")
+ @ClusterConfigProperty(key = GroupCoordinatorConfig.SHARE_GROUP_ASSIGNMENT_INTERVAL_MS_CONFIG, value = "1000")
})
})
public void testSubscriptionAndPoll() {
@@ -303,10 +233,10 @@ public class ShareConsumerTest {
@ClusterTests({
@ClusterTest(serverProperties = {
- @ClusterConfigProperty(key = GroupCoordinatorConfig.CONSUMER_GROUP_ASSIGNMENT_INTERVAL_MS_CONFIG, value = "0")
+ @ClusterConfigProperty(key = GroupCoordinatorConfig.SHARE_GROUP_ASSIGNMENT_INTERVAL_MS_CONFIG, value = "0")
}),
@ClusterTest(serverProperties = {
- @ClusterConfigProperty(key = GroupCoordinatorConfig.CONSUMER_GROUP_ASSIGNMENT_INTERVAL_MS_CONFIG, value = "1000")
+ @ClusterConfigProperty(key = GroupCoordinatorConfig.SHARE_GROUP_ASSIGNMENT_INTERVAL_MS_CONFIG, value = "1000")
})
})
public void testSubscriptionAndPollMultiple() {
@@ -350,196 +280,6 @@ public class ShareConsumerTest {
}
}
- @ClusterTest
- public void testAcknowledgementSentOnSubscriptionChange() throws ExecutionException, InterruptedException {
- alterShareAutoOffsetReset("group1", "earliest");
- try (Producer<byte[], byte[]> producer = createProducer();
- ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer("group1")) {
-
- Map<TopicPartition, Set<Long>> partitionOffsetsMap = new HashMap<>();
- Map<TopicPartition, Exception> partitionExceptionMap = new HashMap<>();
-
- ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), "value".getBytes());
- producer.send(record);
- ProducerRecord<byte[], byte[]> record2 = new ProducerRecord<>(tp2.topic(), tp2.partition(), null, "key".getBytes(), "value".getBytes());
- producer.send(record2).get();
- producer.flush();
- shareConsumer.setAcknowledgementCommitCallback(new TestableAcknowledgementCommitCallback(partitionOffsetsMap, partitionExceptionMap));
-
- shareConsumer.subscribe(Set.of(tp.topic()));
- assertEquals(Optional.empty(), shareConsumer.acquisitionLockTimeoutMs());
-
- TestUtils.waitForCondition(() -> shareConsumer.poll(Duration.ofMillis(2000)).count() == 1,
- DEFAULT_MAX_WAIT_MS, 100L, () -> "Failed to consume records for share consumer");
-
- assertEquals(Optional.of(15000), shareConsumer.acquisitionLockTimeoutMs());
- shareConsumer.subscribe(Set.of(tp2.topic()));
- assertEquals(Optional.of(15000), shareConsumer.acquisitionLockTimeoutMs());
-
- // Waiting for heartbeat to propagate the subscription change.
- TestUtils.waitForCondition(() -> {
- shareConsumer.poll(Duration.ofMillis(500));
- return partitionOffsetsMap.containsKey(tp) && partitionOffsetsMap.containsKey(tp2);
- }, DEFAULT_MAX_WAIT_MS, 100L, () -> "Failed to consume records from the updated subscription");
-
- // Verifying if the callback was invoked without exceptions for the partitions for both topics.
- assertFalse(partitionExceptionMap.containsKey(tp));
- assertFalse(partitionExceptionMap.containsKey(tp2));
- verifyShareGroupStateTopicRecordsProduced();
- }
- }
-
- @ClusterTest
- public void testAcknowledgementCommitCallbackSuccessfulAcknowledgement() throws Exception {
- alterShareAutoOffsetReset("group1", "earliest");
- try (Producer<byte[], byte[]> producer = createProducer();
- ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer("group1")) {
-
- Map<TopicPartition, Set<Long>> partitionOffsetsMap = new HashMap<>();
- Map<TopicPartition, Exception> partitionExceptionMap = new HashMap<>();
- ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), "value".getBytes());
-
- producer.send(record);
- producer.flush();
-
- shareConsumer.setAcknowledgementCommitCallback(new TestableAcknowledgementCommitCallback(partitionOffsetsMap, partitionExceptionMap));
- shareConsumer.subscribe(Set.of(tp.topic()));
-
- TestUtils.waitForCondition(() -> shareConsumer.poll(Duration.ofMillis(2000)).count() == 1,
- DEFAULT_MAX_WAIT_MS, 100L, () -> "Failed to consume records for share consumer");
-
- // The callback should be called before the return of the poll, even when there are no more records.
- ConsumerRecords<byte[], byte[]> records = shareConsumer.poll(Duration.ofMillis(2000));
- assertEquals(0, records.count());
- assertTrue(partitionOffsetsMap.containsKey(tp));
-
- // We expect no exception as the acknowledgement error code is null.
- assertFalse(partitionExceptionMap.containsKey(tp));
- verifyShareGroupStateTopicRecordsProduced();
- }
- }
-
- @ClusterTest
- public void testAcknowledgementCommitCallbackSuccessfulAcknowledgementOnCommitSync() throws Exception {
- alterShareAutoOffsetReset("group1", "earliest");
- try (Producer<byte[], byte[]> producer = createProducer();
- ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer("group1")) {
-
- Map<TopicPartition, Set<Long>> partitionOffsetsMap = new HashMap<>();
- Map<TopicPartition, Exception> partitionExceptionMap = new HashMap<>();
- ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), "value".getBytes());
-
- producer.send(record);
- producer.flush();
-
- shareConsumer.setAcknowledgementCommitCallback(new TestableAcknowledgementCommitCallback(partitionOffsetsMap, partitionExceptionMap));
- shareConsumer.subscribe(Set.of(tp.topic()));
-
- TestUtils.waitForCondition(() -> shareConsumer.poll(Duration.ofMillis(2000)).count() == 1,
- DEFAULT_MAX_WAIT_MS, 100L, () -> "Failed to consume records for share consumer");
-
- // The acknowledgement commit callback should be called before the commitSync returns
- // once the records have been confirmed to have been acknowledged.
- Map<TopicIdPartition, Optional<KafkaException>> result = shareConsumer.commitSync();
- assertEquals(1, result.size());
- assertTrue(partitionOffsetsMap.containsKey(tp));
-
- // We expect no exception as the acknowledgement error code is null.
- assertFalse(partitionExceptionMap.containsKey(tp));
- verifyShareGroupStateTopicRecordsProduced();
- }
- }
-
- @ClusterTest
- public void testAcknowledgementCommitCallbackOnClose() {
- alterShareAutoOffsetReset("group1", "earliest");
- try (Producer<byte[], byte[]> producer = createProducer();
- ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer("group1")) {
-
- Map<TopicPartition, Set<Long>> partitionOffsetsMap = new HashMap<>();
- Map<TopicPartition, Exception> partitionExceptionMap = new HashMap<>();
- ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), "value".getBytes());
- producer.send(record);
- producer.flush();
- shareConsumer.setAcknowledgementCommitCallback(new TestableAcknowledgementCommitCallback(partitionOffsetsMap, partitionExceptionMap));
- shareConsumer.subscribe(Set.of(tp.topic()));
-
- ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 2500L, 1);
- assertEquals(1, records.count());
-
- // Now in the second poll, we implicitly acknowledge the record received in the first poll.
- // We get back the acknowledgement error code asynchronously after the second poll.
- // The acknowledgement commit callback is invoked in close.
- shareConsumer.poll(Duration.ofMillis(1000));
- shareConsumer.close();
-
- // We expect no exception as the acknowledgement error code is null.
- assertFalse(partitionExceptionMap.containsKey(tp));
- verifyShareGroupStateTopicRecordsProduced();
- }
- }
-
- @ClusterTest
- public void testAcknowledgementCommitCallbackInvalidRecordStateException() throws Exception {
- alterShareAutoOffsetReset("group1", "earliest");
- try (Producer<byte[], byte[]> producer = createProducer();
- ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer("group1")) {
-
- Map<TopicPartition, Set<Long>> partitionOffsetsMap = new HashMap<>();
- Map<TopicPartition, Exception> partitionExceptionMap = new HashMap<>();
- ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), "value".getBytes());
- producer.send(record);
- producer.flush();
-
- shareConsumer.setAcknowledgementCommitCallback(new TestableAcknowledgementCommitCallback(partitionOffsetsMap, partitionExceptionMap));
- shareConsumer.subscribe(Set.of(tp.topic()));
-
- ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 2500L, 1);
- assertEquals(1, records.count());
-
- // Waiting until the acquisition lock expires.
- Thread.sleep(20000);
-
- TestUtils.waitForCondition(() -> {
- shareConsumer.poll(Duration.ofMillis(500));
- return partitionExceptionMap.containsKey(tp) && partitionExceptionMap.get(tp) instanceof InvalidRecordStateException;
- }, DEFAULT_MAX_WAIT_MS, 100L, () -> "Failed to be notified by InvalidRecordStateException");
- }
- }
-
- /**
- * Test implementation of AcknowledgementCommitCallback to track the completed acknowledgements.
- * partitionOffsetsMap is used to track the offsets acknowledged for each partition.
- * partitionExceptionMap is used to track the exception encountered for each partition if any.
- * Note - Multiple calls to {@link #onComplete(Map, Exception)} will not update the partitionExceptionMap for any existing partitions,
- * so please ensure to clear the partitionExceptionMap after every call to onComplete() in a single test.
- */
- private static class TestableAcknowledgementCommitCallback implements AcknowledgementCommitCallback {
- private final Map<TopicPartition, Set<Long>> partitionOffsetsMap;
- private final Map<TopicPartition, Exception> partitionExceptionMap;
-
- public TestableAcknowledgementCommitCallback(Map<TopicPartition, Set<Long>> partitionOffsetsMap,
- Map<TopicPartition, Exception> partitionExceptionMap) {
- this.partitionOffsetsMap = partitionOffsetsMap;
- this.partitionExceptionMap = partitionExceptionMap;
- }
-
- @Override
- public void onComplete(Map<TopicIdPartition, Set<Long>> offsetsMap, Exception exception) {
- offsetsMap.forEach((partition, offsets) -> {
- partitionOffsetsMap.merge(partition.topicPartition(), offsets, (oldOffsets, newOffsets) -> {
- Set<Long> mergedOffsets = new HashSet<>();
- mergedOffsets.addAll(oldOffsets);
- mergedOffsets.addAll(newOffsets);
- return mergedOffsets;
- });
- if (!partitionExceptionMap.containsKey(partition.topicPartition()) && exception != null) {
- partitionExceptionMap.put(partition.topicPartition(), exception);
- }
- });
- }
- }
-
@ClusterTest
public void testHeaders() {
alterShareAutoOffsetReset("group1", "earliest");
@@ -1445,10 +1185,10 @@ public class ShareConsumerTest {
@ClusterTests({
@ClusterTest(serverProperties = {
- @ClusterConfigProperty(key = GroupCoordinatorConfig.CONSUMER_GROUP_ASSIGNMENT_INTERVAL_MS_CONFIG, value = "0")
+ @ClusterConfigProperty(key = GroupCoordinatorConfig.SHARE_GROUP_ASSIGNMENT_INTERVAL_MS_CONFIG, value = "0")
}),
@ClusterTest(serverProperties = {
- @ClusterConfigProperty(key = GroupCoordinatorConfig.CONSUMER_GROUP_ASSIGNMENT_INTERVAL_MS_CONFIG, value = "1000")
+ @ClusterConfigProperty(key = GroupCoordinatorConfig.SHARE_GROUP_ASSIGNMENT_INTERVAL_MS_CONFIG, value = "1000")
})
})
public void testMultipleConsumersInGroupSequentialConsumption() {
@@ -1488,10 +1228,10 @@ public class ShareConsumerTest {
@ClusterTests({
@ClusterTest(serverProperties = {
- @ClusterConfigProperty(key = GroupCoordinatorConfig.CONSUMER_GROUP_ASSIGNMENT_INTERVAL_MS_CONFIG, value = "0")
+ @ClusterConfigProperty(key = GroupCoordinatorConfig.SHARE_GROUP_ASSIGNMENT_INTERVAL_MS_CONFIG, value = "0")
}),
@ClusterTest(serverProperties = {
- @ClusterConfigProperty(key = GroupCoordinatorConfig.CONSUMER_GROUP_ASSIGNMENT_INTERVAL_MS_CONFIG, value = "1000")
+ @ClusterConfigProperty(key = GroupCoordinatorConfig.SHARE_GROUP_ASSIGNMENT_INTERVAL_MS_CONFIG, value = "1000")
})
})
public void testMultipleConsumersInGroupConcurrentConsumption()
@@ -1529,10 +1269,10 @@ public class ShareConsumerTest {
@ClusterTests({
@ClusterTest(serverProperties = {
- @ClusterConfigProperty(key = GroupCoordinatorConfig.CONSUMER_GROUP_ASSIGNMENT_INTERVAL_MS_CONFIG, value = "0")
+ @ClusterConfigProperty(key = GroupCoordinatorConfig.SHARE_GROUP_ASSIGNMENT_INTERVAL_MS_CONFIG, value = "0")
}),
@ClusterTest(serverProperties = {
- @ClusterConfigProperty(key = GroupCoordinatorConfig.CONSUMER_GROUP_ASSIGNMENT_INTERVAL_MS_CONFIG, value = "1000")
+ @ClusterConfigProperty(key = GroupCoordinatorConfig.SHARE_GROUP_ASSIGNMENT_INTERVAL_MS_CONFIG, value = "1000")
})
})
public void testMultipleConsumersInMultipleGroupsConcurrentConsumption()
@@ -1649,10 +1389,10 @@ public class ShareConsumerTest {
@ClusterTests({
@ClusterTest(serverProperties = {
- @ClusterConfigProperty(key = GroupCoordinatorConfig.CONSUMER_GROUP_ASSIGNMENT_INTERVAL_MS_CONFIG, value = "0")
+ @ClusterConfigProperty(key = GroupCoordinatorConfig.SHARE_GROUP_ASSIGNMENT_INTERVAL_MS_CONFIG, value = "0")
}),
@ClusterTest(serverProperties = {
- @ClusterConfigProperty(key = GroupCoordinatorConfig.CONSUMER_GROUP_ASSIGNMENT_INTERVAL_MS_CONFIG, value = "1000")
+ @ClusterConfigProperty(key = GroupCoordinatorConfig.SHARE_GROUP_ASSIGNMENT_INTERVAL_MS_CONFIG, value = "1000")
})
})
public void testMultipleConsumersInGroupFailureConcurrentConsumption()
@@ -1759,148 +1499,6 @@ public class ShareConsumerTest {
}
}
- /**
- * Test to verify that the acknowledgement commit callback cannot invoke methods of ShareConsumer.
- * The exception thrown is verified in {@link TestableAcknowledgementCommitCallbackWithShareConsumer}
- */
- @ClusterTest
- public void testAcknowledgementCommitCallbackCallsShareConsumerDisallowed() {
- alterShareAutoOffsetReset("group1", "earliest");
- try (Producer<byte[], byte[]> producer = createProducer();
- ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer("group1")) {
-
- ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), "value".getBytes());
- producer.send(record);
- producer.flush();
-
- shareConsumer.setAcknowledgementCommitCallback(new TestableAcknowledgementCommitCallbackWithShareConsumer<>(shareConsumer));
- shareConsumer.subscribe(Set.of(tp.topic()));
-
- // The acknowledgement commit callback will try to call a method of ShareConsumer
- shareConsumer.poll(Duration.ofMillis(5000));
- // The second poll sends the acknowledgements implicitly.
- // The acknowledgement commit callback will be called and the exception is thrown.
- // This is verified inside the onComplete() method implementation.
- shareConsumer.poll(Duration.ofMillis(500));
- verifyShareGroupStateTopicRecordsProduced();
- }
- }
-
- private class TestableAcknowledgementCommitCallbackWithShareConsumer<K, V> implements AcknowledgementCommitCallback {
- private final ShareConsumer<K, V> shareConsumer;
-
- TestableAcknowledgementCommitCallbackWithShareConsumer(ShareConsumer<K, V> shareConsumer) {
- this.shareConsumer = shareConsumer;
- }
-
- @Override
- public void onComplete(Map<TopicIdPartition, Set<Long>> offsetsMap, Exception exception) {
- // Accessing methods of ShareConsumer should throw an exception.
- assertThrows(IllegalStateException.class, shareConsumer::close);
- assertThrows(IllegalStateException.class, () -> shareConsumer.subscribe(Set.of(tp.topic())));
- assertThrows(IllegalStateException.class, () -> shareConsumer.poll(Duration.ofMillis(5000)));
- }
- }
-
- /**
- * Test to verify that the acknowledgement commit callback can invoke ShareConsumer.wakeup() and it
- * wakes up the enclosing poll.
- */
- @ClusterTest
- public void testAcknowledgementCommitCallbackCallsShareConsumerWakeup() throws InterruptedException {
- alterShareAutoOffsetReset("group1", "earliest");
- try (Producer<byte[], byte[]> producer = createProducer();
- ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer("group1")) {
-
- ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), "value".getBytes());
- producer.send(record);
- producer.flush();
-
- // The acknowledgement commit callback will try to call a method of ShareConsumer
- shareConsumer.setAcknowledgementCommitCallback(new TestableAcknowledgementCommitCallbackWakeup<>(shareConsumer));
- shareConsumer.subscribe(Set.of(tp.topic()));
-
- TestUtils.waitForCondition(() -> shareConsumer.poll(Duration.ofMillis(2000)).count() == 1,
- DEFAULT_MAX_WAIT_MS, 100L, () -> "Failed to consume records for share consumer");
-
- // The second poll sends the acknowledgements implicitly.
- shareConsumer.poll(Duration.ofMillis(2000));
-
- // Till now acknowledgement commit callback has not been called, so no exception thrown yet.
- // On 3rd poll, the acknowledgement commit callback will be called and the exception is thrown.
- AtomicBoolean exceptionThrown = new AtomicBoolean(false);
- TestUtils.waitForCondition(() -> {
- try {
- shareConsumer.poll(Duration.ofMillis(500));
- } catch (WakeupException e) {
- exceptionThrown.set(true);
- }
- return exceptionThrown.get();
- }, DEFAULT_MAX_WAIT_MS, 100L, () -> "Failed to receive expected exception");
- verifyShareGroupStateTopicRecordsProduced();
- }
- }
-
- private static class TestableAcknowledgementCommitCallbackWakeup<K, V> implements AcknowledgementCommitCallback {
- private final ShareConsumer<K, V> shareConsumer;
-
- TestableAcknowledgementCommitCallbackWakeup(ShareConsumer<K, V> shareConsumer) {
- this.shareConsumer = shareConsumer;
- }
-
- @Override
- public void onComplete(Map<TopicIdPartition, Set<Long>> offsetsMap, Exception exception) {
- shareConsumer.wakeup();
- }
- }
-
- /**
- * Test to verify that the acknowledgement commit callback can throw an exception, and it is not propagated
- * to the caller of poll().
- */
- @ClusterTest
- public void testAcknowledgementCommitCallbackThrowsException() throws InterruptedException {
- alterShareAutoOffsetReset("group1", "earliest");
- try (Producer<byte[], byte[]> producer = createProducer();
- ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer("group1")) {
-
- ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), "value".getBytes());
- producer.send(record);
- producer.flush();
-
- AtomicBoolean callbackCalled = new AtomicBoolean(false);
- shareConsumer.setAcknowledgementCommitCallback(new TestableAcknowledgementCommitCallbackThrows(callbackCalled));
- shareConsumer.subscribe(Set.of(tp.topic()));
-
- TestUtils.waitForCondition(() -> shareConsumer.poll(Duration.ofMillis(2000)).count() == 1,
- DEFAULT_MAX_WAIT_MS, 100L, () -> "Failed to consume records for share consumer");
-
- TestUtils.waitForCondition(() -> {
- try {
- shareConsumer.poll(Duration.ofMillis(500));
- } catch (Exception e) {
- throw new NoRetryException(e);
- }
- return callbackCalled.get();
- }, DEFAULT_MAX_WAIT_MS, 100L, () -> "Received unexpected exception or callback not called");
- verifyShareGroupStateTopicRecordsProduced();
- }
- }
-
- private static class TestableAcknowledgementCommitCallbackThrows implements AcknowledgementCommitCallback {
- private final AtomicBoolean callbackCalled;
-
- public TestableAcknowledgementCommitCallbackThrows(AtomicBoolean callbackCalled) {
- this.callbackCalled = callbackCalled;
- }
-
- @Override
- public void onComplete(Map<TopicIdPartition, Set<Long>> offsetsMap, Exception exception) {
- callbackCalled.set(true);
- throw new org.apache.kafka.common.errors.OutOfOrderSequenceException("Exception thrown in TestableAcknowledgementCommitCallbackThrows.onComplete");
- }
- }
-
/**
* Test to verify that calling Thread.interrupt() before ShareConsumer.poll(Duration)
* causes it to throw InterruptException
@@ -1969,10 +1567,10 @@ public class ShareConsumerTest {
@ClusterTests({
@ClusterTest(serverProperties = {
- @ClusterConfigProperty(key = GroupCoordinatorConfig.CONSUMER_GROUP_ASSIGNMENT_INTERVAL_MS_CONFIG, value = "0")
+ @ClusterConfigProperty(key = GroupCoordinatorConfig.SHARE_GROUP_ASSIGNMENT_INTERVAL_MS_CONFIG, value = "0")
}),
@ClusterTest(serverProperties = {
- @ClusterConfigProperty(key = GroupCoordinatorConfig.CONSUMER_GROUP_ASSIGNMENT_INTERVAL_MS_CONFIG, value = "1000")
+ @ClusterConfigProperty(key = GroupCoordinatorConfig.SHARE_GROUP_ASSIGNMENT_INTERVAL_MS_CONFIG, value = "1000")
})
})
public void testSubscriptionFollowedByTopicCreation() throws InterruptedException {
@@ -2005,10 +1603,10 @@ public class ShareConsumerTest {
@ClusterTests({
@ClusterTest(serverProperties = {
- @ClusterConfigProperty(key = GroupCoordinatorConfig.CONSUMER_GROUP_ASSIGNMENT_INTERVAL_MS_CONFIG, value = "0")
+ @ClusterConfigProperty(key = GroupCoordinatorConfig.SHARE_GROUP_ASSIGNMENT_INTERVAL_MS_CONFIG, value = "0")
}),
@ClusterTest(serverProperties = {
- @ClusterConfigProperty(key = GroupCoordinatorConfig.CONSUMER_GROUP_ASSIGNMENT_INTERVAL_MS_CONFIG, value = "1000")
+ @ClusterConfigProperty(key = GroupCoordinatorConfig.SHARE_GROUP_ASSIGNMENT_INTERVAL_MS_CONFIG, value = "1000")
})
})
public void testSubscriptionAndPollFollowedByTopicDeletion() throws InterruptedException, ExecutionException {
@@ -2097,215 +1695,28 @@ public class ShareConsumerTest {
}
}
- @ClusterTest
- public void testShareAutoOffsetResetDefaultValue() {
- try (ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer("group1");
- Producer<byte[], byte[]> producer = createProducer()) {
-
- shareConsumer.subscribe(Set.of(tp.topic()));
- ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), "value".getBytes());
- // Producing a record.
- producer.send(record);
- producer.flush();
- ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 2500L, 0, true, "group1", List.of(tp));
- // No records should be consumed because share.auto.offset.reset has a default of "latest". Since the record
- // was produced before share partition was initialized (which happens after the first share fetch request
- // in the poll method), the start offset would be the latest offset, i.e. 1 (the next offset after the already
- // present 0th record)
- assertEquals(0, records.count());
- // Producing another record.
- producer.send(record);
- producer.flush();
- records = shareConsumer.poll(Duration.ofMillis(5000));
- // Now the next record should be consumed successfully
- assertEquals(1, records.count());
- verifyShareGroupStateTopicRecordsProduced();
+ @ClusterTest(
+ brokers = 3,
+ serverProperties = {
+ @ClusterConfigProperty(key = "auto.create.topics.enable", value = "false"),
+ @ClusterConfigProperty(key = "group.share.max.partition.max.record.locks", value = "10000"),
+ @ClusterConfigProperty(key = "group.share.partition.max.record.locks", value = "10000"),
+ @ClusterConfigProperty(key = "group.share.record.lock.duration.ms", value = "15000"),
+ @ClusterConfigProperty(key = "offsets.topic.num.partitions", value = "3"),
+ @ClusterConfigProperty(key = "offsets.topic.replication.factor", value = "3"),
+ @ClusterConfigProperty(key = "share.coordinator.state.topic.num.partitions", value = "3"),
+ @ClusterConfigProperty(key = "share.coordinator.state.topic.replication.factor", value = "3")
}
- }
+ )
+ @Timeout(90)
+ public void testShareConsumerAfterCoordinatorMovement() throws Exception {
+ String topicName = "multipart";
+ String groupId = "multipartGrp";
+ Uuid topicId = createTopic(topicName, 3, 3);
+ alterShareAutoOffsetReset(groupId, "earliest");
+ ScheduledExecutorService service = Executors.newScheduledThreadPool(5);
- @ClusterTest
- public void testShareAutoOffsetResetEarliest() {
- alterShareAutoOffsetReset("group1", "earliest");
- try (ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer("group1");
- Producer<byte[], byte[]> producer = createProducer()) {
-
- shareConsumer.subscribe(Set.of(tp.topic()));
- ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), "value".getBytes());
- // Producing a record.
- producer.send(record);
- producer.flush();
- ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 2500L, 1);
- // Since the value for share.auto.offset.reset has been altered to "earliest", the consumer should consume
- // all messages present on the partition
- assertEquals(1, records.count());
- // Producing another record.
- producer.send(record);
- producer.flush();
- records = shareConsumer.poll(Duration.ofMillis(5000));
- // The next records should also be consumed successfully
- assertEquals(1, records.count());
- verifyShareGroupStateTopicRecordsProduced();
- }
- }
-
- @ClusterTest
- public void testShareAutoOffsetResetEarliestAfterLsoMovement() {
- alterShareAutoOffsetReset("group1", "earliest");
- try (
- Producer<byte[], byte[]> producer = createProducer();
- Admin adminClient = createAdminClient()
- ) {
-
- ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), "value".getBytes());
- // We write 10 records to the topic, so they would be written from offsets 0-9 on the topic.
- for (int i = 0; i < 10; i++) {
- assertDoesNotThrow(() -> producer.send(record).get(), "Failed to send records");
- }
-
- // We delete records before offset 5, so the LSO should move to 5.
- assertDoesNotThrow(() -> adminClient.deleteRecords(Map.of(tp, RecordsToDelete.beforeOffset(5L))).all().get(), "Failed to delete records");
-
- int consumedMessageCount = consumeMessages(new AtomicInteger(0), 5, "group1", 1, 10, true);
- // The records returned belong to offsets 5-9.
- assertEquals(5, consumedMessageCount);
- verifyShareGroupStateTopicRecordsProduced();
- }
- }
-
- @ClusterTest
- public void testShareAutoOffsetResetMultipleGroupsWithDifferentValue() {
- alterShareAutoOffsetReset("group1", "earliest");
- alterShareAutoOffsetReset("group2", "latest");
- try (ShareConsumer<byte[], byte[]> shareConsumerEarliest = createShareConsumer("group1");
- ShareConsumer<byte[], byte[]> shareConsumerLatest = createShareConsumer("group2");
- Producer<byte[], byte[]> producer = createProducer()) {
-
- shareConsumerEarliest.subscribe(Set.of(tp.topic()));
- shareConsumerLatest.subscribe(Set.of(tp.topic()));
-
- ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), "value".getBytes());
- // Producing a record.
- producer.send(record);
- producer.flush();
- ConsumerRecords<byte[], byte[]> records1 = waitedPoll(shareConsumerEarliest, 2500L, 1);
- // Since the value for share.auto.offset.reset has been altered to "earliest", the consumer should consume
- // all messages present on the partition
- assertEquals(1, records1.count());
-
- ConsumerRecords<byte[], byte[]> records2 = waitedPoll(shareConsumerLatest, 2500L, 0, true, "group2", List.of(tp));
- // Since the value for share.auto.offset.reset has been altered to "latest", the consumer should not consume
- // any message
- assertEquals(0, records2.count());
-
- // Producing another record.
- producer.send(record);
-
- records1 = shareConsumerEarliest.poll(Duration.ofMillis(5000));
- // The next record should also be consumed successfully by group1
- assertEquals(1, records1.count());
-
- records2 = shareConsumerLatest.poll(Duration.ofMillis(5000));
- // The next record should also be consumed successfully by group2
- assertEquals(1, records2.count());
- verifyShareGroupStateTopicRecordsProduced();
- }
- }
-
- @ClusterTest
- public void testShareAutoOffsetResetByDuration() throws Exception {
- // Set auto offset reset to 1 hour before current time
- alterShareAutoOffsetReset("group1", "by_duration:PT1H");
-
- try (ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer("group1");
- Producer<byte[], byte[]> producer = createProducer()) {
-
- long currentTime = System.currentTimeMillis();
- long twoHoursAgo = currentTime - TimeUnit.HOURS.toMillis(2);
- long thirtyMinsAgo = currentTime - TimeUnit.MINUTES.toMillis(30);
-
- // Produce messages with different timestamps
- ProducerRecord<byte[], byte[]> oldRecord = new ProducerRecord<>(tp.topic(), tp.partition(),
- twoHoursAgo, "old_key".getBytes(), "old_value".getBytes());
- ProducerRecord<byte[], byte[]> recentRecord = new ProducerRecord<>(tp.topic(), tp.partition(),
- thirtyMinsAgo, "recent_key".getBytes(), "recent_value".getBytes());
- ProducerRecord<byte[], byte[]> currentRecord = new ProducerRecord<>(tp.topic(), tp.partition(),
- currentTime, "current_key".getBytes(), "current_value".getBytes());
-
- producer.send(oldRecord).get();
- producer.send(recentRecord).get();
- producer.send(currentRecord).get();
- producer.flush();
-
- shareConsumer.subscribe(Set.of(tp.topic()));
-
- // Should only receive messages from last hour (recent and current)
- List<ConsumerRecord<byte[], byte[]>> records = consumeRecords(shareConsumer, 2);
- assertEquals(2, records.size());
-
- // Verify timestamps and order
- assertEquals(thirtyMinsAgo, records.get(0).timestamp());
- assertEquals("recent_key", new String(records.get(0).key()));
- assertEquals(currentTime, records.get(1).timestamp());
- assertEquals("current_key", new String(records.get(1).key()));
- }
-
- // Set the auto offset reset to 3 hours before current time
- // so the consumer should consume all messages (3 records)
- alterShareAutoOffsetReset("group2", "by_duration:PT3H");
- try (ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer("group2")) {
-
- shareConsumer.subscribe(Set.of(tp.topic()));
- List<ConsumerRecord<byte[], byte[]>> records = consumeRecords(shareConsumer, 3);
- assertEquals(3, records.size());
- verifyShareGroupStateTopicRecordsProduced();
- }
- }
-
- @ClusterTest
- public void testShareAutoOffsetResetByDurationInvalidFormat() {
- // Test invalid duration format
- ConfigResource configResource = new ConfigResource(ConfigResource.Type.GROUP, "group1");
- Map<ConfigResource, Collection<AlterConfigOp>> alterEntries = new HashMap<>();
-
- // Test invalid duration format
- try (Admin adminClient = createAdminClient()) {
- alterEntries.put(configResource, List.of(new AlterConfigOp(new ConfigEntry(
- GroupConfig.SHARE_AUTO_OFFSET_RESET_CONFIG, "by_duration:1h"), AlterConfigOp.OpType.SET)));
- ExecutionException e1 = assertThrows(ExecutionException.class, () ->
- adminClient.incrementalAlterConfigs(alterEntries).all().get());
- assertInstanceOf(InvalidConfigurationException.class, e1.getCause());
-
- // Test negative duration
- alterEntries.put(configResource, List.of(new AlterConfigOp(new ConfigEntry(
- GroupConfig.SHARE_AUTO_OFFSET_RESET_CONFIG, "by_duration:-PT1H"), AlterConfigOp.OpType.SET)));
- ExecutionException e2 = assertThrows(ExecutionException.class, () ->
- adminClient.incrementalAlterConfigs(alterEntries).all().get());
- assertInstanceOf(InvalidConfigurationException.class, e2.getCause());
- }
- }
-
- @ClusterTest(
- brokers = 3,
- serverProperties = {
- @ClusterConfigProperty(key = "auto.create.topics.enable", value = "false"),
- @ClusterConfigProperty(key = "group.share.max.partition.max.record.locks", value = "10000"),
- @ClusterConfigProperty(key = "group.share.partition.max.record.locks", value = "10000"),
- @ClusterConfigProperty(key = "group.share.record.lock.duration.ms", value = "15000"),
- @ClusterConfigProperty(key = "offsets.topic.num.partitions", value = "3"),
- @ClusterConfigProperty(key = "offsets.topic.replication.factor", value = "3"),
- @ClusterConfigProperty(key = "share.coordinator.state.topic.num.partitions", value = "3"),
- @ClusterConfigProperty(key = "share.coordinator.state.topic.replication.factor", value = "3")
- }
- )
- @Timeout(90)
- public void testShareConsumerAfterCoordinatorMovement() throws Exception {
- String topicName = "multipart";
- String groupId = "multipartGrp";
- Uuid topicId = createTopic(topicName, 3, 3);
- alterShareAutoOffsetReset(groupId, "earliest");
- ScheduledExecutorService service = Executors.newScheduledThreadPool(5);
-
- TopicPartition tpMulti = new TopicPartition(topicName, 0);
+ TopicPartition tpMulti = new TopicPartition(topicName, 0);
// produce some messages
ClientState prodState = new ClientState();
@@ -2423,80 +1834,6 @@ public class ShareConsumerTest {
verifyShareGroupStateTopicRecordsProduced();
}
- @ClusterTest
- public void testDeliveryCountDifferentBehaviorWhenClosingSessionWithExplicitAcknowledgement() {
- alterShareAutoOffsetReset("group1", "earliest");
- try (Producer<byte[], byte[]> producer = createProducer();
- ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer(
- "group1",
- Map.of(ConsumerConfig.SHARE_ACKNOWLEDGEMENT_MODE_CONFIG, EXPLICIT))) {
-
- ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null,
- "key".getBytes(), "value".getBytes());
- producer.send(record);
- producer.send(record);
- producer.flush();
-
- shareConsumer.subscribe(Set.of(tp.topic()));
- ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 2500L, 2);
- assertEquals(2, records.count());
- // Acknowledge the first record with AcknowledgeType.RELEASE
- shareConsumer.acknowledge(records.records(tp).get(0), AcknowledgeType.RELEASE);
- Map<TopicIdPartition, Optional<KafkaException>> result = shareConsumer.commitSync();
- assertEquals(1, result.size());
- }
-
- // Test delivery count
- try (ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer("group1", Map.of())) {
- shareConsumer.subscribe(Set.of(tp.topic()));
- ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 2500L, 2);
- assertEquals(2, records.count());
- assertEquals((short) 2, records.records(tp).get(0).deliveryCount().get());
- assertEquals((short) 2, records.records(tp).get(1).deliveryCount().get());
- }
- }
-
- @ClusterTest(
- serverProperties = {
- @ClusterConfigProperty(key = "group.share.delivery.count.limit", value = "3"),
- }
- )
- public void testBehaviorOnDeliveryCountBoundary() {
- alterShareAutoOffsetReset("group1", "earliest");
- try (Producer<byte[], byte[]> producer = createProducer();
- ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer(
- "group1",
- Map.of(ConsumerConfig.SHARE_ACKNOWLEDGEMENT_MODE_CONFIG, EXPLICIT))) {
-
- ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null,
- "key".getBytes(), "value".getBytes());
- producer.send(record);
- producer.flush();
-
- shareConsumer.subscribe(Set.of(tp.topic()));
- ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 2500L, 1);
- assertEquals(1, records.count());
- assertEquals((short) 1, records.records(tp).get(0).deliveryCount().get());
- // Acknowledge the record with AcknowledgeType.RELEASE.
- shareConsumer.acknowledge(records.records(tp).get(0), AcknowledgeType.RELEASE);
- Map<TopicIdPartition, Optional<KafkaException>> result = shareConsumer.commitSync();
- assertEquals(1, result.size());
-
- // Consume again, the delivery count should be 2.
- records = waitedPoll(shareConsumer, 2500L, 1);
- assertEquals(1, records.count());
- assertEquals((short) 2, records.records(tp).get(0).deliveryCount().get());
- }
-
- // Start again and same record should be delivered
- try (ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer("group1", Map.of())) {
- shareConsumer.subscribe(Set.of(tp.topic()));
- ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 2500L, 1);
- assertEquals(1, records.count());
- assertEquals((short) 3, records.records(tp).get(0).deliveryCount().get());
- }
- }
-
@ClusterTest(
brokers = 3,
serverProperties = {
@@ -2647,755 +1984,36 @@ public class ShareConsumerTest {
}
@ClusterTest
- public void testReadCommittedIsolationLevel() {
- alterShareAutoOffsetReset("group1", "earliest");
- alterShareIsolationLevel("group1", "read_committed");
- try (Producer<byte[], byte[]> transactionalProducer = createProducer("T1");
- ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer("group1")) {
- produceCommittedAndAbortedTransactionsInInterval(transactionalProducer, 10, 5);
- shareConsumer.subscribe(Set.of(tp.topic()));
- ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 5000L, 8);
- // 5th and 10th message transaction was aborted, hence they won't be included in the fetched records.
- assertEquals(8, records.count());
- int messageCounter = 1;
- for (ConsumerRecord<byte[], byte[]> record : records) {
- assertEquals(tp.topic(), record.topic());
- assertEquals(tp.partition(), record.partition());
- if (messageCounter % 5 == 0)
- messageCounter++;
- assertEquals("Message " + messageCounter, new String(record.value()));
- messageCounter++;
- }
- }
- verifyShareGroupStateTopicRecordsProduced();
- }
-
- @ClusterTest
- public void testReadUncommittedIsolationLevel() {
- alterShareAutoOffsetReset("group1", "earliest");
- alterShareIsolationLevel("group1", "read_uncommitted");
- try (Producer<byte[], byte[]> transactionalProducer = createProducer("T1");
- ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer("group1")) {
- produceCommittedAndAbortedTransactionsInInterval(transactionalProducer, 10, 5);
- shareConsumer.subscribe(Set.of(tp.topic()));
- ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 5000L, 10);
- // Even though 5th and 10th message transaction was aborted, they will be included in the fetched records since IsolationLevel is READ_UNCOMMITTED.
- assertEquals(10, records.count());
- int messageCounter = 1;
- for (ConsumerRecord<byte[], byte[]> record : records) {
- assertEquals(tp.topic(), record.topic());
- assertEquals(tp.partition(), record.partition());
- assertEquals("Message " + messageCounter, new String(record.value()));
- messageCounter++;
- }
- }
- verifyShareGroupStateTopicRecordsProduced();
- }
-
- @ClusterTest
- public void testAlterReadUncommittedToReadCommittedIsolationLevel() {
- alterShareAutoOffsetReset("group1", "earliest");
- alterShareIsolationLevel("group1", "read_uncommitted");
- try (Producer<byte[], byte[]> transactionalProducer = createProducer("T1");
- ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer("group1", Map.of(
- ConsumerConfig.SHARE_ACKNOWLEDGEMENT_MODE_CONFIG, EXPLICIT))) {
- shareConsumer.subscribe(Set.of(tp.topic()));
- transactionalProducer.initTransactions();
- try {
- // First transaction is committed.
- produceCommittedTransaction(transactionalProducer, "Message 1");
-
- ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 5000L, 1);
- assertEquals(1, records.count());
- ConsumerRecord<byte[], byte[]> record = records.iterator().next();
- assertEquals("Message 1", new String(record.value()));
- assertEquals(tp.topic(), record.topic());
- assertEquals(tp.partition(), record.partition());
- records.forEach(consumedRecord -> shareConsumer.acknowledge(consumedRecord, AcknowledgeType.ACCEPT));
- shareConsumer.commitSync();
-
- // Second transaction is aborted.
- produceAbortedTransaction(transactionalProducer, "Message 2");
-
- records = waitedPoll(shareConsumer, 5000L, 1);
- assertEquals(1, records.count());
- record = records.iterator().next();
- assertEquals("Message 2", new String(record.value()));
- records.forEach(consumedRecord -> shareConsumer.acknowledge(consumedRecord, AcknowledgeType.ACCEPT));
- shareConsumer.commitSync();
-
- // Third transaction is committed.
- produceCommittedTransaction(transactionalProducer, "Message 3");
- // Fourth transaction is aborted.
- produceAbortedTransaction(transactionalProducer, "Message 4");
-
- records = waitedPoll(shareConsumer, 5000L, 2);
- // Message 3 and Message 4 would be returned by this poll.
- assertEquals(2, records.count());
- Iterator<ConsumerRecord<byte[], byte[]>> recordIterator = records.iterator();
- record = recordIterator.next();
- assertEquals("Message 3", new String(record.value()));
- record = recordIterator.next();
- assertEquals("Message 4", new String(record.value()));
- // We will make Message 3 and Message 4 available for re-consumption.
- records.forEach(consumedRecord -> shareConsumer.acknowledge(consumedRecord, AcknowledgeType.RELEASE));
- shareConsumer.commitSync();
-
- // We are altering IsolationLevel to READ_COMMITTED now. We will only read committed transactions now.
- alterShareIsolationLevel("group1", "read_committed");
-
- // Fifth transaction is committed.
- produceCommittedTransaction(transactionalProducer, "Message 5");
- // Sixth transaction is aborted.
- produceAbortedTransaction(transactionalProducer, "Message 6");
- // Seventh transaction is aborted.
- produceAbortedTransaction(transactionalProducer, "Message 7");
- // Eighth transaction is committed.
- produceCommittedTransaction(transactionalProducer, "Message 8");
-
- // Since isolation level is READ_COMMITTED, we can consume Message 3 (committed transaction that was released), Message 5 and Message 8.
- // We cannot consume Message 4 (aborted transaction that was released), Message 6 and Message 7 since they were aborted.
- List<String> messages = new ArrayList<>();
- TestUtils.waitForCondition(() -> {
- ConsumerRecords<byte[], byte[]> pollRecords = shareConsumer.poll(Duration.ofMillis(5000));
- if (pollRecords.count() > 0) {
- for (ConsumerRecord<byte[], byte[]> pollRecord : pollRecords)
- messages.add(new String(pollRecord.value()));
- pollRecords.forEach(consumedRecord -> shareConsumer.acknowledge(consumedRecord, AcknowledgeType.ACCEPT));
- shareConsumer.commitSync();
- }
- return messages.size() == 3;
- }, DEFAULT_MAX_WAIT_MS, 100L, () -> "Failed to consume all records post altering share isolation level");
-
- assertEquals("Message 3", messages.get(0));
- assertEquals("Message 5", messages.get(1));
- assertEquals("Message 8", messages.get(2));
- } catch (InterruptedException e) {
- throw new RuntimeException(e);
- } finally {
- transactionalProducer.close();
- }
- }
- verifyShareGroupStateTopicRecordsProduced();
- }
-
- @ClusterTest
- public void testAlterReadCommittedToReadUncommittedIsolationLevelWithReleaseAck() {
- alterShareAutoOffsetReset("group1", "earliest");
- alterShareIsolationLevel("group1", "read_committed");
- try (Producer<byte[], byte[]> transactionalProducer = createProducer("T1");
- ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer("group1", Map.of(
- ConsumerConfig.SHARE_ACKNOWLEDGEMENT_MODE_CONFIG, EXPLICIT))) {
- shareConsumer.subscribe(Set.of(tp.topic()));
- transactionalProducer.initTransactions();
-
- try {
- // First transaction is committed.
- produceCommittedTransaction(transactionalProducer, "Message 1");
-
- ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 5000L, 1);
- assertEquals(1, records.count());
- ConsumerRecord<byte[], byte[]> record = records.iterator().next();
- assertEquals("Message 1", new String(record.value()));
- assertEquals(tp.topic(), record.topic());
- assertEquals(tp.partition(), record.partition());
- records.forEach(consumedRecord -> shareConsumer.acknowledge(consumedRecord, AcknowledgeType.ACCEPT));
- shareConsumer.commitSync();
-
- // Second transaction is aborted.
- produceAbortedTransaction(transactionalProducer, "Message 2");
-
- // Setting the acknowledgement commit callback to verify acknowledgement completion.
- Map<TopicPartition, Set<Long>> partitionOffsetsMap = new HashMap<>();
- shareConsumer.setAcknowledgementCommitCallback(new TestableAcknowledgementCommitCallback(partitionOffsetsMap, Map.of()));
-
- // We will not receive any records since the transaction for Message 2 was aborted. Wait for the
- // aborted marker offset for Message 2 (3L) to be fetched and acknowledged by the consumer.
- TestUtils.waitForCondition(() -> {
- ConsumerRecords<byte[], byte[]> pollRecords = shareConsumer.poll(Duration.ofMillis(500));
- return pollRecords.count() == 0 && partitionOffsetsMap.containsKey(tp) && partitionOffsetsMap.get(tp).contains(3L);
- }, DEFAULT_MAX_WAIT_MS, 100L, () -> "Failed to consume abort transaction and marker offset for Message 2");
-
- // Third transaction is committed.
- produceCommittedTransaction(transactionalProducer, "Message 3");
- // Fourth transaction is aborted.
- produceAbortedTransaction(transactionalProducer, "Message 4");
-
- // Setting the acknowledgement commit callback to verify acknowledgement completion.
- Map<TopicPartition, Set<Long>> partitionOffsetsMap2 = new HashMap<>();
- shareConsumer.setAcknowledgementCommitCallback(new TestableAcknowledgementCommitCallback(partitionOffsetsMap2, Map.of()));
-
- records = waitedPoll(shareConsumer, 5000L, 1);
- // Message 3 would be returned by this poll.
- assertEquals(1, records.count());
- record = records.iterator().next();
- assertEquals("Message 3", new String(record.value()));
- // We will make Message 3 available for re-consumption.
- records.forEach(consumedRecord -> shareConsumer.acknowledge(consumedRecord, AcknowledgeType.RELEASE));
- shareConsumer.commitSync();
-
- // Wait for the aborted marker offset for Message 4 (7L) to be fetched and acknowledged by the consumer.
- TestUtils.waitForCondition(() -> {
- ConsumerRecords<byte[], byte[]> pollRecords = shareConsumer.poll(Duration.ofMillis(500));
- if (pollRecords.count() > 0) {
- // We will release Message 3 again if it was received in this poll().
- pollRecords.forEach(consumerRecord -> shareConsumer.acknowledge(consumerRecord, AcknowledgeType.RELEASE));
- }
- return partitionOffsetsMap2.containsKey(tp) && partitionOffsetsMap2.get(tp).contains(7L);
- }, DEFAULT_MAX_WAIT_MS, 100L, () -> "Failed to consume abort transaction marker offset for Message 4");
-
- // We are altering IsolationLevel to READ_UNCOMMITTED now. We will read both committed/aborted transactions now.
- alterShareIsolationLevel("group1", "read_uncommitted");
-
- // Fifth transaction is committed.
- produceCommittedTransaction(transactionalProducer, "Message 5");
- // Sixth transaction is aborted.
- produceAbortedTransaction(transactionalProducer, "Message 6");
- // Seventh transaction is aborted.
- produceAbortedTransaction(transactionalProducer, "Message 7");
- // Eighth transaction is committed.
- produceCommittedTransaction(transactionalProducer, "Message 8");
-
- // Since isolation level is READ_UNCOMMITTED, we can consume Message 3 (committed transaction that was released), Message 5, Message 6, Message 7 and Message 8.
- Set<String> finalMessages = new HashSet<>();
- TestUtils.waitForCondition(() -> {
- ConsumerRecords<byte[], byte[]> pollRecords = shareConsumer.poll(Duration.ofMillis(5000));
- if (pollRecords.count() > 0) {
- for (ConsumerRecord<byte[], byte[]> pollRecord : pollRecords)
- finalMessages.add(new String(pollRecord.value()));
- pollRecords.forEach(consumedRecord -> shareConsumer.acknowledge(consumedRecord, AcknowledgeType.ACCEPT));
- shareConsumer.commitSync();
- }
- return finalMessages.size() == 5;
- }, DEFAULT_MAX_WAIT_MS, 100L, () -> "Failed to consume all records post altering share isolation level");
-
- Set<String> expected = Set.of("Message 3", "Message 5", "Message 6", "Message 7", "Message 8");
- assertEquals(expected, finalMessages);
- } catch (InterruptedException e) {
- throw new RuntimeException(e);
- } finally {
- transactionalProducer.close();
- }
- }
- verifyShareGroupStateTopicRecordsProduced();
- }
-
- @ClusterTest
- public void testAlterReadCommittedToReadUncommittedIsolationLevelWithRejectAck() {
- alterShareAutoOffsetReset("group1", "earliest");
- alterShareIsolationLevel("group1", "read_committed");
- try (Producer<byte[], byte[]> transactionalProducer = createProducer("T1");
- ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer("group1", Map.of(
- ConsumerConfig.SHARE_ACKNOWLEDGEMENT_MODE_CONFIG, EXPLICIT))) {
- shareConsumer.subscribe(Set.of(tp.topic()));
- transactionalProducer.initTransactions();
-
- try {
- // First transaction is committed.
- produceCommittedTransaction(transactionalProducer, "Message 1");
-
- ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 5000L, 1);
- assertEquals(1, records.count());
- ConsumerRecord<byte[], byte[]> record = records.iterator().next();
- assertEquals("Message 1", new String(record.value()));
- assertEquals(tp.topic(), record.topic());
- assertEquals(tp.partition(), record.partition());
- records.forEach(consumedRecord -> shareConsumer.acknowledge(consumedRecord, AcknowledgeType.ACCEPT));
- shareConsumer.commitSync();
-
- // Second transaction is aborted.
- produceAbortedTransaction(transactionalProducer, "Message 2");
-
- // Setting the acknowledgement commit callback to verify acknowledgement completion.
- Map<TopicPartition, Set<Long>> partitionOffsetsMap = new HashMap<>();
- shareConsumer.setAcknowledgementCommitCallback(new TestableAcknowledgementCommitCallback(partitionOffsetsMap, Map.of()));
-
- // We will not receive any records since the transaction for Message 2 was aborted. Wait for the
- // aborted marker offset for Message 2 (3L) to be fetched and acknowledged by the consumer.
- TestUtils.waitForCondition(() -> {
- ConsumerRecords<byte[], byte[]> pollRecords = shareConsumer.poll(Duration.ofMillis(500));
- return pollRecords.count() == 0 && partitionOffsetsMap.containsKey(tp) && partitionOffsetsMap.get(tp).contains(3L);
- }, DEFAULT_MAX_WAIT_MS, 100L, () -> "Failed to consume abort transaction and marker offset for Message 2");
-
- // Third transaction is committed.
- produceCommittedTransaction(transactionalProducer, "Message 3");
- // Fourth transaction is aborted.
- produceAbortedTransaction(transactionalProducer, "Message 4");
-
- // Setting the acknowledgement commit callback to verify acknowledgement completion.
- Map<TopicPartition, Set<Long>> partitionOffsetsMap2 = new HashMap<>();
- shareConsumer.setAcknowledgementCommitCallback(new TestableAcknowledgementCommitCallback(partitionOffsetsMap2, Map.of()));
-
- records = waitedPoll(shareConsumer, 5000L, 1);
- // Message 3 would be returned by this poll.
- assertEquals(1, records.count());
- record = records.iterator().next();
- assertEquals("Message 3", new String(record.value()));
- // We will make Message 3 available for re-consumption.
- records.forEach(consumedRecord -> shareConsumer.acknowledge(consumedRecord, AcknowledgeType.REJECT));
- shareConsumer.commitSync();
-
- // Wait for the aborted marker offset for Message 4 (7L) to be fetched and acknowledged by the consumer.
- TestUtils.waitForCondition(() -> {
- shareConsumer.poll(Duration.ofMillis(500));
- return partitionOffsetsMap2.containsKey(tp) && partitionOffsetsMap2.get(tp).contains(7L);
- }, DEFAULT_MAX_WAIT_MS, 100L, () -> "Failed to consume abort transaction marker offset for Message 4");
-
- // We are altering IsolationLevel to READ_UNCOMMITTED now. We will read both committed/aborted transactions now.
- alterShareIsolationLevel("group1", "read_uncommitted");
-
- // Fifth transaction is committed.
- produceCommittedTransaction(transactionalProducer, "Message 5");
- // Sixth transaction is aborted.
- produceAbortedTransaction(transactionalProducer, "Message 6");
- // Seventh transaction is aborted.
- produceAbortedTransaction(transactionalProducer, "Message 7");
- // Eighth transaction is committed.
- produceCommittedTransaction(transactionalProducer, "Message 8");
-
- // Since isolation level is READ_UNCOMMITTED, we can consume Message 5, Message 6, Message 7 and Message 8.
- List<String> finalMessages = new ArrayList<>();
- TestUtils.waitForCondition(() -> {
- ConsumerRecords<byte[], byte[]> pollRecords = shareConsumer.poll(Duration.ofMillis(5000));
- if (pollRecords.count() > 0) {
- for (ConsumerRecord<byte[], byte[]> pollRecord : pollRecords)
- finalMessages.add(new String(pollRecord.value()));
- pollRecords.forEach(consumedRecord -> shareConsumer.acknowledge(consumedRecord, AcknowledgeType.ACCEPT));
- shareConsumer.commitSync();
- }
- return finalMessages.size() == 4;
- }, DEFAULT_MAX_WAIT_MS, 100L, () -> "Failed to consume all records post altering share isolation level");
-
- assertEquals("Message 5", finalMessages.get(0));
- assertEquals("Message 6", finalMessages.get(1));
- assertEquals("Message 7", finalMessages.get(2));
- assertEquals("Message 8", finalMessages.get(3));
- } catch (InterruptedException e) {
- throw new RuntimeException(e);
- } finally {
- transactionalProducer.close();
- }
- }
- verifyShareGroupStateTopicRecordsProduced();
- }
-
- @ClusterTest
- public void testPollInBatchOptimizedMode() {
+ public void testCommitSyncFailsForDeletedTopic() throws InterruptedException {
+ Uuid topicId = createTopic("baz", 1, 1);
alterShareAutoOffsetReset("group1", "earliest");
try (Producer<byte[], byte[]> producer = createProducer();
ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer(
- "group1",
- Map.of(ConsumerConfig.MAX_POLL_RECORDS_CONFIG, 5, ConsumerConfig.SHARE_ACQUIRE_MODE_CONFIG, BATCH_OPTIMIZED))
+ "group1",
+ Map.of(ConsumerConfig.SHARE_ACKNOWLEDGEMENT_MODE_CONFIG, EXPLICIT))
) {
- ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), "value".getBytes());
for (int i = 0; i < 10; i++) {
+ ProducerRecord<byte[], byte[]> record = new ProducerRecord<>("baz", 0, null, "key".getBytes(), ("Message " + i).getBytes());
producer.send(record);
}
producer.flush();
- shareConsumer.subscribe(List.of(tp.topic()));
- // although max.poll.records is set to 5, in batch optimized mode we will still get all 10 records.
+ shareConsumer.subscribe(List.of("baz"));
ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 2500L, 10);
assertEquals(10, records.count());
- verifyShareGroupStateTopicRecordsProduced();
- }
- }
- @ClusterTest
- public void testPollInRecordLimitMode() {
- alterShareAutoOffsetReset("group1", "earliest");
- try (Producer<byte[], byte[]> producer = createProducer();
- ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer(
- "group1",
- Map.of(ConsumerConfig.MAX_POLL_RECORDS_CONFIG, 5, ConsumerConfig.SHARE_ACQUIRE_MODE_CONFIG, RECORD_LIMIT))
- ) {
- ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), "value".getBytes());
- for (int i = 0; i < 10; i++) {
- producer.send(record);
- }
- producer.flush();
- shareConsumer.subscribe(List.of(tp.topic()));
+ records.forEach(shareConsumer::acknowledge);
- // In record limit mode we will get only up to max.poll.records number of records.
- ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 2500L, 5);
- assertEquals(5, records.count());
- verifyShareGroupStateTopicRecordsProduced();
+ // Topic deletion does not necessarily become apparent across the cluster immediately, so sleep a short while
+ deleteTopic("baz");
+ Thread.sleep(5000);
+
+ Map<TopicIdPartition, Optional<KafkaException>> commitResult = shareConsumer.commitSync();
+ assertEquals(1, commitResult.size());
+ assertInstanceOf(UnknownTopicIdException.class, commitResult.get(new TopicIdPartition(topicId, 0, "baz")).get());
}
}
- @ClusterTest
- public void testPollAndExplicitAcknowledgeSingleMessageInRecordLimitMode() {
- alterShareAutoOffsetReset("group1", "earliest");
- try (Producer<byte[], byte[]> producer = createProducer();
- ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer(
- "group1",
- Map.of(ConsumerConfig.MAX_POLL_RECORDS_CONFIG, 1,
- ConsumerConfig.SHARE_ACQUIRE_MODE_CONFIG, RECORD_LIMIT,
- ConsumerConfig.SHARE_ACKNOWLEDGEMENT_MODE_CONFIG, EXPLICIT))
- ) {
- ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), "value".getBytes());
- for (int i = 0; i < 10; i++) {
- producer.send(record);
- }
- producer.flush();
- shareConsumer.subscribe(List.of(tp.topic()));
-
- for (int i = 0; i < 10; i++) {
- ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 2500L, 1);
- assertEquals(1, records.count());
- for (ConsumerRecord<byte[], byte[]> rec : records) {
- shareConsumer.acknowledge(rec, AcknowledgeType.ACCEPT);
- }
- shareConsumer.commitSync();
- }
- verifyShareGroupStateTopicRecordsProduced();
- }
- }
-
- @ClusterTest
- public void testExplicitAcknowledgeSuccessInRecordLimitMode() {
- alterShareAutoOffsetReset("group1", "earliest");
- try (Producer<byte[], byte[]> producer = createProducer();
- ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer(
- "group1",
- Map.of(ConsumerConfig.MAX_POLL_RECORDS_CONFIG, 10,
- ConsumerConfig.SHARE_ACQUIRE_MODE_CONFIG, RECORD_LIMIT,
- ConsumerConfig.SHARE_ACKNOWLEDGEMENT_MODE_CONFIG, EXPLICIT))
- ) {
- ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), "value".getBytes());
- for (int i = 0; i < 15; i++) {
- producer.send(record);
- }
- producer.flush();
- shareConsumer.subscribe(List.of(tp.topic()));
- ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 2500L, 10);
- assertEquals(10, records.count());
- for (ConsumerRecord<byte[], byte[]> rec : records) {
- shareConsumer.acknowledge(rec, AcknowledgeType.ACCEPT);
- }
- shareConsumer.commitSync();
-
- records = waitedPoll(shareConsumer, 2500L, 5);
- assertEquals(5, records.count());
- verifyShareGroupStateTopicRecordsProduced();
- }
- }
-
- @ClusterTest
- public void testExplicitAcknowledgeReleaseAcceptInRecordLimitMode() {
- alterShareAutoOffsetReset("group1", "earliest");
- try (Producer<byte[], byte[]> producer = createProducer();
- ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer(
- "group1",
- Map.of(ConsumerConfig.MAX_POLL_RECORDS_CONFIG, 10,
- ConsumerConfig.SHARE_ACQUIRE_MODE_CONFIG, RECORD_LIMIT,
- ConsumerConfig.SHARE_ACKNOWLEDGEMENT_MODE_CONFIG, EXPLICIT))
- ) {
- for (int i = 0; i < 20; i++) {
- ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp2.topic(), tp2.partition(), null, "key".getBytes(), ("Message " + i).getBytes());
- producer.send(record);
- }
- producer.flush();
- shareConsumer.subscribe(List.of(tp2.topic()));
- ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 2500L, 10);
- assertEquals(10, records.count());
-
- int count = 0;
- Map<TopicIdPartition, Optional<KafkaException>> result;
- for (ConsumerRecord<byte[], byte[]> record : records) {
- if (count % 2 == 0) {
- shareConsumer.acknowledge(record, AcknowledgeType.ACCEPT);
- } else {
- shareConsumer.acknowledge(record, AcknowledgeType.RELEASE);
- }
- result = shareConsumer.commitSync();
- assertEquals(1, result.size());
- count++;
- }
-
- // Poll again to get 10 records.
- records = waitedPoll(shareConsumer, 2500L, 10);
- assertEquals(10, records.count());
- for (ConsumerRecord<byte[], byte[]> record : records) {
- shareConsumer.acknowledge(record, AcknowledgeType.ACCEPT);
- }
-
- // Get the rest of all 5 records.
- records = waitedPoll(shareConsumer, 2500L, 5);
- assertEquals(5, records.count());
- verifyShareGroupStateTopicRecordsProduced();
- }
- }
-
- @ClusterTest
- public void testCommitSyncFailsForDeletedTopic() throws InterruptedException {
- Uuid topicId = createTopic("baz", 1, 1);
- alterShareAutoOffsetReset("group1", "earliest");
- try (Producer<byte[], byte[]> producer = createProducer();
- ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer(
- "group1",
- Map.of(ConsumerConfig.SHARE_ACKNOWLEDGEMENT_MODE_CONFIG, EXPLICIT))
- ) {
- for (int i = 0; i < 10; i++) {
- ProducerRecord<byte[], byte[]> record = new ProducerRecord<>("baz", 0, null, "key".getBytes(), ("Message " + i).getBytes());
- producer.send(record);
- }
- producer.flush();
-
- shareConsumer.subscribe(List.of("baz"));
- ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 2500L, 10);
- assertEquals(10, records.count());
-
- records.forEach(shareConsumer::acknowledge);
-
- // Topic deletion does not necessarily become apparent across the cluster immediately, so sleep a short while
- deleteTopic("baz");
- Thread.sleep(5000);
-
- Map<TopicIdPartition, Optional<KafkaException>> commitResult = shareConsumer.commitSync();
- assertEquals(1, commitResult.size());
- assertInstanceOf(UnknownTopicIdException.class, commitResult.get(new TopicIdPartition(topicId, 0, "baz")).get());
- }
- }
-
- @ClusterTest
- public void testRenewAcknowledgementOnPoll() {
- alterShareAutoOffsetReset("group1", "earliest");
- try (Producer<byte[], byte[]> producer = createProducer();
- ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer(
- "group1",
- Map.of(ConsumerConfig.SHARE_ACKNOWLEDGEMENT_MODE_CONFIG, EXPLICIT))
- ) {
- AtomicInteger acknowledgementsCommitted = new AtomicInteger(0);
- shareConsumer.setAcknowledgementCommitCallback((offsetsByTopicPartition, exception) ->
- offsetsByTopicPartition.forEach((tip, offsets) -> acknowledgementsCommitted.addAndGet(offsets.size())));
-
- for (int i = 0; i < 10; i++) {
- ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), ("Message " + i).getBytes());
- producer.send(record);
- }
- producer.flush();
-
- shareConsumer.subscribe(List.of(tp.topic()));
- ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 2500L, 10);
- assertEquals(10, records.count());
-
- int count = 0;
- for (ConsumerRecord<byte[], byte[]> record : records) {
- if (count % 2 == 0) {
- shareConsumer.acknowledge(record, AcknowledgeType.ACCEPT);
- } else {
- shareConsumer.acknowledge(record, AcknowledgeType.RENEW);
- }
- count++;
- }
-
- // Get the rest of all 5 records.
- records = waitedPoll(shareConsumer, 2500L, 5);
- assertEquals(5, records.count());
- for (ConsumerRecord<byte[], byte[]> record : records) {
- shareConsumer.acknowledge(record, AcknowledgeType.ACCEPT);
- }
-
- shareConsumer.commitSync();
- assertEquals(15, acknowledgementsCommitted.get());
- }
- verifyYammerMetricCount("ackType=Renew", 5);
- }
-
- @ClusterTest
- public void testRenewAcknowledgementOnCommitSync() {
- alterShareAutoOffsetReset("group1", "earliest");
- try (Producer<byte[], byte[]> producer = createProducer();
- ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer(
- "group1",
- Map.of(ConsumerConfig.SHARE_ACKNOWLEDGEMENT_MODE_CONFIG, EXPLICIT))
- ) {
- AtomicInteger acknowledgementsCommitted = new AtomicInteger(0);
- shareConsumer.setAcknowledgementCommitCallback((offsetsByTopicPartition, exception) ->
- offsetsByTopicPartition.forEach((tip, offsets) -> acknowledgementsCommitted.addAndGet(offsets.size())));
-
- for (int i = 0; i < 10; i++) {
- ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), ("Message " + i).getBytes());
- producer.send(record);
- }
- producer.flush();
-
- shareConsumer.subscribe(List.of(tp.topic()));
- ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 2500L, 10);
- assertEquals(10, records.count());
- assertEquals(Optional.of(15000), shareConsumer.acquisitionLockTimeoutMs());
-
- // The updated acquisition lock timeout is only applied when the next poll is called.
- alterShareRecordLockDurationMs("group1", 25000);
-
- int count = 0;
- Map<TopicIdPartition, Optional<KafkaException>> result;
- for (ConsumerRecord<byte[], byte[]> record : records) {
- if (count % 2 == 0) {
- shareConsumer.acknowledge(record, AcknowledgeType.ACCEPT);
- } else {
- shareConsumer.acknowledge(record, AcknowledgeType.RENEW);
- }
- result = shareConsumer.commitSync();
- assertEquals(1, result.size());
- assertEquals(Optional.of(15000), shareConsumer.acquisitionLockTimeoutMs());
- assertEquals(Optional.empty(), result.get(new TopicIdPartition(tpId, tp.partition(), tp.topic())));
- count++;
- }
-
- // Get the rest of all 5 records.
- records = waitedPoll(shareConsumer, 2500L, 5);
- assertEquals(5, records.count());
- assertEquals(Optional.of(25000), shareConsumer.acquisitionLockTimeoutMs());
- for (ConsumerRecord<byte[], byte[]> record : records) {
- shareConsumer.acknowledge(record, AcknowledgeType.ACCEPT);
- }
- }
- verifyYammerMetricCount("ackType=Renew", 5);
- }
-
- @ClusterTest
- public void testRenewAcknowledgementInvalidStateRecord() {
- alterShareAutoOffsetReset("group1", "earliest");
- try (Producer<byte[], byte[]> producer = createProducer();
- ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer(
- "group1",
- Map.of(ConsumerConfig.SHARE_ACKNOWLEDGEMENT_MODE_CONFIG, EXPLICIT))
- ) {
- AtomicInteger acknowledgementsCommitted = new AtomicInteger(0);
- shareConsumer.setAcknowledgementCommitCallback((offsetsByTopicPartition, exception) ->
- offsetsByTopicPartition.forEach((tip, offsets) -> acknowledgementsCommitted.addAndGet(offsets.size())));
-
- ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), "Message ".getBytes());
- producer.send(record);
- producer.flush();
-
- shareConsumer.subscribe(List.of(tp.topic()));
- ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 2500L, 1);
- assertEquals(1, records.count());
-
- for (ConsumerRecord<byte[], byte[]> rec : records) {
- shareConsumer.acknowledge(rec, AcknowledgeType.REJECT);
- shareConsumer.commitSync();
- assertThrows(IllegalStateException.class, () -> shareConsumer.acknowledge(rec, AcknowledgeType.RENEW));
- }
- }
- verifyYammerMetricCount("ackType=Renew", 0);
- }
-
- @ClusterTest
- public void testRenewAcknowledgementDisabled() {
- alterShareAutoOffsetReset("group1", "earliest");
- alterShareRenewAcknowledgeEnable("group1", false);
- try (Producer<byte[], byte[]> producer = createProducer();
- ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer(
- "group1",
- Map.of(ConsumerConfig.SHARE_ACKNOWLEDGEMENT_MODE_CONFIG, EXPLICIT))
- ) {
- ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), "Message".getBytes());
- producer.send(record);
- producer.flush();
-
- shareConsumer.subscribe(List.of(tp.topic()));
- ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 2500L, 1);
- assertEquals(1, records.count());
-
- for (ConsumerRecord<byte[], byte[]> rec : records) {
- shareConsumer.acknowledge(rec, AcknowledgeType.RENEW);
- }
-
- Map<TopicIdPartition, Optional<KafkaException>> result = shareConsumer.commitSync();
- assertEquals(1, result.size());
- Optional<KafkaException> error = result.get(new TopicIdPartition(tpId, tp.partition(), tp.topic()));
- assertTrue(error.isPresent());
- assertInstanceOf(InvalidRecordStateException.class, error.get());
- }
- }
-
- @ClusterTest(
- brokers = 1,
- serverProperties = {
- @ClusterConfigProperty(key = "group.share.record.lock.duration.ms", value = "12000"),
- @ClusterConfigProperty(key = "group.share.min.record.lock.duration.ms", value = "12000"),
- }
- )
- public void testRenewAcknowledgementNoResultInPoll() {
- alterShareAutoOffsetReset("group1", "earliest");
- try (Producer<byte[], byte[]> producer = createProducer();
- ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer(
- "group1",
- Map.of(ConsumerConfig.SHARE_ACKNOWLEDGEMENT_MODE_CONFIG, EXPLICIT))
- ) {
- AtomicInteger acknowledgementsCommitted = new AtomicInteger(0);
- shareConsumer.setAcknowledgementCommitCallback((offsetsByTopicPartition, exception) ->
- offsetsByTopicPartition.forEach((tip, offsets) -> acknowledgementsCommitted.addAndGet(offsets.size())));
-
- for (int i = 0; i < 10; i++) {
- ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), ("Message " + i).getBytes());
- producer.send(record);
- }
- producer.flush();
-
- shareConsumer.subscribe(List.of(tp.topic()));
- ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 2500L, 10);
- assertEquals(10, records.count());
-
- int count = 0;
- for (ConsumerRecord<byte[], byte[]> record : records) {
- if (count % 2 == 0) {
- shareConsumer.acknowledge(record, AcknowledgeType.ACCEPT);
- } else {
- shareConsumer.acknowledge(record, AcknowledgeType.RENEW);
- }
- count++;
- }
-
- // 5 more records (total 15 produced).
- for (int i = 10; i < 15; i++) {
- ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), ("Message " + i).getBytes());
- producer.send(record);
- }
-
- // Get the rest of all 5 records.
- records = waitedPoll(shareConsumer, 11500L, 0); // This will send the acks but not return next 5 records (10-15)
- assertEquals(10, acknowledgementsCommitted.get());
- assertEquals(0, records.count());
- verifyYammerMetricCount("ackType=Renew", 5);
-
- // Renewal duration passed, now records will be back.
- records = waitedPoll(shareConsumer, 2500L, 5); // Renewed records as well as 10-15 records.
- assertEquals(5, records.count());
- for (ConsumerRecord<byte[], byte[]> record : records) {
- shareConsumer.acknowledge(record, AcknowledgeType.ACCEPT);
- }
-
- shareConsumer.commitSync();
-
- records = waitedPoll(shareConsumer, 2500L, 5); // 10-15 records.
- assertEquals(5, records.count());
- for (ConsumerRecord<byte[], byte[]> record : records) {
- shareConsumer.acknowledge(record, AcknowledgeType.ACCEPT);
- }
-
- shareConsumer.commitSync();
-
- // Initial - 5 renew + 5 accept, Subsequent - 5 renewed accepted + 5 fresh accepted (10-15)
- assertEquals(20, acknowledgementsCommitted.get());
- }
- verifyYammerMetricCount("ackType=Renew", 5);
- }
-
- private void verifyYammerMetricCount(String filterString, int count) {
- com.yammer.metrics.core.Metric renewAck = KafkaYammerMetrics.defaultRegistry().allMetrics().entrySet().stream()
- .filter(entry -> entry.getKey().toString().contains(filterString))
- .findAny()
- .orElseThrow(() -> new AssertionError("metric not found"))
- .getValue();
-
- assertEquals(count, ((Meter) renewAck).count());
- }
-
@ClusterTest
public void testDescribeShareGroupOffsetsForEmptySharePartition() {
String groupId = "group1";
@@ -3412,1447 +2030,4 @@ public class ShareConsumerTest {
throw new RuntimeException(e);
}
}
-
- @ClusterTest
- public void testSharePartitionLagForSingleShareConsumer() {
- String groupId = "group1";
- alterShareAutoOffsetReset(groupId, "earliest");
- try (Producer<byte[], byte[]> producer = createProducer();
- ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer(groupId);
- Admin adminClient = createAdminClient()) {
- ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), "Message".getBytes());
- producer.send(record);
- producer.flush();
- shareConsumer.subscribe(List.of(tp.topic()));
- // Polling share consumer to make sure the share partition is created and the record is consumed.
- waitedPoll(shareConsumer, 2500L, 1);
- // Acknowledge and commit the consumed record to update the share partition state.
- shareConsumer.commitSync();
- // After the acknowledgement is successful, the share partition lag should be 0 because the only produced record has been consumed.
- verifySharePartitionLag(adminClient, groupId, tp, 0L);
- // Producing another record to the share partition.
- producer.send(record);
- producer.flush();
- // Since the new record has not been consumed yet, the share partition lag should be 1.
- verifySharePartitionLag(adminClient, groupId, tp, 1L);
- } catch (InterruptedException e) {
- throw new RuntimeException(e);
- }
- }
-
- @ClusterTest
- public void testSharePartitionLagForMultipleShareConsumers() {
- String groupId = "group1";
- alterShareAutoOffsetReset(groupId, "earliest");
- try (Producer<byte[], byte[]> producer = createProducer();
- ShareConsumer<byte[], byte[]> shareConsumer1 = createShareConsumer(groupId);
- ShareConsumer<byte[], byte[]> shareConsumer2 = createShareConsumer(groupId);
- Admin adminClient = createAdminClient()) {
- ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), "Message".getBytes());
- producer.send(record);
- producer.flush();
- shareConsumer1.subscribe(List.of(tp.topic()));
- shareConsumer2.subscribe(List.of(tp.topic()));
- // Polling share consumer 1 to make sure the share partition is created and the records are consumed.
- waitedPoll(shareConsumer1, 2500L, 1);
- // Acknowledge and commit the consumed records to update the share partition state.
- shareConsumer1.commitSync();
- // After the acknowledgement is successful, the share partition lag should be 0 because the all produced records have been consumed.
- verifySharePartitionLag(adminClient, groupId, tp, 0L);
- // Producing more records to the share partition.
- producer.send(record);
- // Polling share consumer 2 this time.
- waitedPoll(shareConsumer2, 2500L, 1);
- // Since the consumed record hasn't been acknowledged yet, the share partition lag should be 1.
- verifySharePartitionLag(adminClient, groupId, tp, 1L);
- // Acknowledge and commit the consumed records to update the share partition state.
- shareConsumer2.commitSync();
- // After the acknowledgement is successful, the share partition lag should be 0 because the all produced records have been consumed.
- verifySharePartitionLag(adminClient, groupId, tp, 0L);
- // Producing another record to the share partition.
- producer.send(record);
- producer.flush();
- // Since the new record has not been consumed yet, the share partition lag should be 1.
- verifySharePartitionLag(adminClient, groupId, tp, 1L);
- } catch (InterruptedException e) {
- throw new RuntimeException(e);
- }
- }
-
- @ClusterTest
- public void testSharePartitionLagWithReleaseAcknowledgement() {
- String groupId = "group1";
- alterShareAutoOffsetReset(groupId, "earliest");
- try (Producer<byte[], byte[]> producer = createProducer();
- ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer(groupId, Map.of(ConsumerConfig.SHARE_ACKNOWLEDGEMENT_MODE_CONFIG, EXPLICIT));
- Admin adminClient = createAdminClient()) {
- ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), "Message".getBytes());
- producer.send(record);
- producer.flush();
- shareConsumer.subscribe(List.of(tp.topic()));
- // Polling share consumer to make sure the share partition is created and the record is consumed.
- ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 2500L, 1);
- // Accept the record first to move the offset forward and register the state with persister.
- records.forEach(r -> shareConsumer.acknowledge(r, AcknowledgeType.ACCEPT));
- shareConsumer.commitSync();
- // After accepting, the lag should be 0 because the record is consumed successfully.
- verifySharePartitionLag(adminClient, groupId, tp, 0L);
- // Producing another record to the share partition.
- producer.send(record);
- producer.flush();
- // The produced record is consumed.
- records = waitedPoll(shareConsumer, 2500L, 1);
- // Now release the record - it should be available for redelivery.
- records.forEach(r -> shareConsumer.acknowledge(r, AcknowledgeType.RELEASE));
- shareConsumer.commitSync();
- // After releasing the lag should be 1, because the record is released for redelivery.
- verifySharePartitionLag(adminClient, groupId, tp, 1L);
- // The record is now consumed again.
- records = waitedPoll(shareConsumer, 2500L, 1);
- // Accept the record to mark it as consumed.
- records.forEach(r -> shareConsumer.acknowledge(r, AcknowledgeType.ACCEPT));
- shareConsumer.commitSync();
- // After accepting the record, the lag should be 0 because all the produced records have been consumed.
- verifySharePartitionLag(adminClient, groupId, tp, 0L);
- } catch (InterruptedException e) {
- throw new RuntimeException(e);
- }
- }
-
- @ClusterTest
- public void testSharePartitionLagWithRejectAcknowledgement() {
- String groupId = "group1";
- alterShareAutoOffsetReset(groupId, "earliest");
- try (Producer<byte[], byte[]> producer = createProducer();
- ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer(groupId, Map.of(ConsumerConfig.SHARE_ACKNOWLEDGEMENT_MODE_CONFIG, EXPLICIT));
- Admin adminClient = createAdminClient()) {
- ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), "Message".getBytes());
- producer.send(record);
- producer.flush();
- shareConsumer.subscribe(List.of(tp.topic()));
- // Polling share consumer to make sure the share partition is created and the record is consumed.
- ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 2500L, 1);
- // Accept the record first to move the offset forward and register the state with persister.
- records.forEach(r -> shareConsumer.acknowledge(r, AcknowledgeType.ACCEPT));
- shareConsumer.commitSync();
- // After accepting, the lag should be 0 because the record is consumed successfully.
- verifySharePartitionLag(adminClient, groupId, tp, 0L);
- // Producing another record to the share partition.
- producer.send(record);
- producer.flush();
- // The produced record is consumed.
- records = waitedPoll(shareConsumer, 2500L, 1);
- // Now reject the record - it should not be available for redelivery.
- records.forEach(r -> shareConsumer.acknowledge(r, AcknowledgeType.REJECT));
- shareConsumer.commitSync();
- // After rejecting the lag should be 0, because the record is permanently rejected and offset moves forward.
- verifySharePartitionLag(adminClient, groupId, tp, 0L);
- } catch (InterruptedException e) {
- throw new RuntimeException(e);
- }
- }
-
- @ClusterTest(
- brokers = 3,
- serverProperties = {
- @ClusterConfigProperty(key = "offsets.topic.num.partitions", value = "1"),
- @ClusterConfigProperty(key = "offsets.topic.replication.factor", value = "3"),
- @ClusterConfigProperty(key = "share.coordinator.state.topic.num.partitions", value = "1"),
- @ClusterConfigProperty(key = "share.coordinator.state.topic.replication.factor", value = "3")
- }
- )
- public void testSharePartitionLagOnGroupCoordinatorMovement() {
- String groupId = "group1";
- alterShareAutoOffsetReset(groupId, "earliest");
- try (Producer<byte[], byte[]> producer = createProducer();
- ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer(groupId);
- Admin adminClient = createAdminClient()) {
- String topicName = "testTopicWithReplicas";
- // Create a topic with replication factor 3
- createTopic(topicName, 1, 3);
- TopicPartition tp = new TopicPartition(topicName, 0);
- ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), "Message".getBytes());
- // Produce first record and consume it
- producer.send(record);
- producer.flush();
- shareConsumer.subscribe(List.of(tp.topic()));
- // Polling share consumer to make sure the share partition is created and the record is consumed.
- waitedPoll(shareConsumer, 2500L, 1);
- // Acknowledge and commit the consumed record to update the share partition state.
- shareConsumer.commitSync();
- // After the acknowledgement is successful, the share partition lag should be 0 because the only produced record has been consumed.
- verifySharePartitionLag(adminClient, groupId, tp, 0L);
- // Producing another record to the share partition.
- producer.send(record);
- producer.flush();
- // Since the new record has not been consumed yet, the share partition lag should be 1.
- verifySharePartitionLag(adminClient, groupId, tp, 1L);
- List<Integer> curGroupCoordNodeId;
- // Find the broker which is the group coordinator for the share group.
- curGroupCoordNodeId = topicPartitionLeader(adminClient, Topic.GROUP_METADATA_TOPIC_NAME, 0);
- assertEquals(1, curGroupCoordNodeId.size());
- // Shut down the coordinator broker
- KafkaBroker broker = cluster.brokers().get(curGroupCoordNodeId.get(0));
- cluster.shutdownBroker(curGroupCoordNodeId.get(0));
- // Wait for it to be completely shutdown
- broker.awaitShutdown();
- // Wait for the leaders of share coordinator, group coordinator and topic partition to be elected, if needed, on a different broker.
- TestUtils.waitForCondition(() -> {
- List<Integer> newShareCoordNodeId = topicPartitionLeader(adminClient, Topic.SHARE_GROUP_STATE_TOPIC_NAME, 0);
- List<Integer> newGroupCoordNodeId = topicPartitionLeader(adminClient, Topic.GROUP_METADATA_TOPIC_NAME, 0);
- List<Integer> newTopicPartitionLeader = topicPartitionLeader(adminClient, tp.topic(), tp.partition());
-
- return newShareCoordNodeId.size() == 1 && !Objects.equals(newShareCoordNodeId.get(0), curGroupCoordNodeId.get(0)) &&
- newGroupCoordNodeId.size() == 1 && !Objects.equals(newGroupCoordNodeId.get(0), curGroupCoordNodeId.get(0)) &&
- newTopicPartitionLeader.size() == 1 && !Objects.equals(newTopicPartitionLeader.get(0), curGroupCoordNodeId.get(0));
- }, DEFAULT_MAX_WAIT_MS, DEFAULT_POLL_INTERVAL_MS, () -> "Failed to elect new leaders after broker shutdown");
- // After group coordinator shutdown, check that lag is still 1
- verifySharePartitionLag(adminClient, groupId, tp, 1L);
- } catch (InterruptedException | ExecutionException e) {
- throw new RuntimeException(e);
- }
- }
-
- @ClusterTest(
- brokers = 3,
- serverProperties = {
- @ClusterConfigProperty(key = "offsets.topic.num.partitions", value = "1"),
- @ClusterConfigProperty(key = "offsets.topic.replication.factor", value = "3"),
- @ClusterConfigProperty(key = "share.coordinator.state.topic.num.partitions", value = "1"),
- @ClusterConfigProperty(key = "share.coordinator.state.topic.replication.factor", value = "3")
- }
- )
- public void testSharePartitionLagOnShareCoordinatorMovement() {
- String groupId = "group1";
- alterShareAutoOffsetReset(groupId, "earliest");
- try (Producer<byte[], byte[]> producer = createProducer();
- ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer(groupId);
- Admin adminClient = createAdminClient()) {
- String topicName = "testTopicWithReplicas";
- // Create a topic with replication factor 3
- createTopic(topicName, 1, 3);
- TopicPartition tp = new TopicPartition(topicName, 0);
- ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), "Message".getBytes());
- // Produce first record and consume it
- producer.send(record);
- producer.flush();
- shareConsumer.subscribe(List.of(tp.topic()));
- // Polling share consumer to make sure the share partition is created and the record is consumed.
- waitedPoll(shareConsumer, 2500L, 1);
- // Acknowledge and commit the consumed record to update the share partition state.
- shareConsumer.commitSync();
- // After the acknowledgement is successful, the share partition lag should be 0 because the only produced record has been consumed.
- verifySharePartitionLag(adminClient, groupId, tp, 0L);
- // Producing another record to the share partition.
- producer.send(record);
- producer.flush();
- // Since the new record has not been consumed yet, the share partition lag should be 1.
- verifySharePartitionLag(adminClient, groupId, tp, 1L);
- List<Integer> curShareCoordNodeId;
- // Find the broker which is the share coordinator for the share partition.
- curShareCoordNodeId = topicPartitionLeader(adminClient, Topic.SHARE_GROUP_STATE_TOPIC_NAME, 0);
- assertEquals(1, curShareCoordNodeId.size());
- // Shut down the coordinator broker
- KafkaBroker broker = cluster.brokers().get(curShareCoordNodeId.get(0));
- cluster.shutdownBroker(curShareCoordNodeId.get(0));
- // Wait for it to be completely shutdown
- broker.awaitShutdown();
- // Wait for the leaders of share coordinator, group coordinator and topic partition to be elected, if needed, on a different broker.
- TestUtils.waitForCondition(() -> {
- List<Integer> newShareCoordNodeId = topicPartitionLeader(adminClient, Topic.SHARE_GROUP_STATE_TOPIC_NAME, 0);
- List<Integer> newGroupCoordNodeId = topicPartitionLeader(adminClient, Topic.GROUP_METADATA_TOPIC_NAME, 0);
- List<Integer> newTopicPartitionLeader = topicPartitionLeader(adminClient, tp.topic(), tp.partition());
-
- return newShareCoordNodeId.size() == 1 && !Objects.equals(newShareCoordNodeId.get(0), curShareCoordNodeId.get(0)) &&
- newGroupCoordNodeId.size() == 1 && !Objects.equals(newGroupCoordNodeId.get(0), curShareCoordNodeId.get(0)) &&
- newTopicPartitionLeader.size() == 1 && !Objects.equals(newTopicPartitionLeader.get(0), curShareCoordNodeId.get(0));
- }, DEFAULT_MAX_WAIT_MS, DEFAULT_POLL_INTERVAL_MS, () -> "Failed to elect new leaders after broker shutdown");
- // After share coordinator shutdown and new leader's election, check that lag is still 1
- verifySharePartitionLag(adminClient, groupId, tp, 1L);
- } catch (InterruptedException | ExecutionException e) {
- throw new RuntimeException(e);
- }
- }
-
- @ClusterTest
- public void testSharePartitionLagAfterAlterShareGroupOffsets() {
- String groupId = "group1";
- try (Producer<byte[], byte[]> producer = createProducer();
- Admin adminClient = createAdminClient()) {
- ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), "Message".getBytes());
- // Producing 100 records to the topic partition.
- for (int i = 0; i < 100; i++) {
- producer.send(record);
- }
- producer.flush();
-
- // Create a new share consumer. Since the share.auto.offset.reset is not altered, it should be latest by default.
- ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer(groupId, Map.of(ConsumerConfig.SHARE_ACKNOWLEDGEMENT_MODE_CONFIG, EXPLICIT));
- shareConsumer.subscribe(List.of(tp.topic()));
- // Polling share consumer to make sure it joins the group and subscribes to the topic.
- waitedPoll(shareConsumer, 2500L, 0, true, groupId, List.of(new TopicPartition(tp.topic(), 0)));
- // Producing 5 additional records to the topic partition.
- for (int i = 0; i < 5; i++) {
- producer.send(record);
- }
- producer.flush();
- // Polling share consumer to make sure the records are consumed.
- ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 2500L, 5);
- assertEquals(5, records.count());
- // Accept the record first to move the offset forward and register the state with persister.
- records.forEach(r -> shareConsumer.acknowledge(r, AcknowledgeType.ACCEPT));
- shareConsumer.commitSync();
- // After accepting, the lag should be 0 because the record is consumed successfully.
- verifySharePartitionLag(adminClient, groupId, tp, 0L);
- // Closing the share consumer so that the offsets can be altered.
- shareConsumer.close();
- // Alter the start offset of the share partition to 40.
- alterShareGroupOffsets(adminClient, groupId, tp, 40L);
- // After altering, the share partition start offset should be 40.
- verifySharePartitionStartOffset(adminClient, groupId, tp, 40L);
- // Verify that the lag is now 65 since the start offset is altered to 40 and there are total 105 records in the partition.
- verifySharePartitionLag(adminClient, groupId, tp, 65L);
- } catch (InterruptedException | ExecutionException e) {
- fail("Test failed with exception: " + e.getMessage());
- }
- }
-
- @ClusterTest
- public void testSharePartitionLagAfterDeleteShareGroupOffsets() {
- String groupId = "group1";
- alterShareAutoOffsetReset(groupId, "earliest");
- try (Producer<byte[], byte[]> producer = createProducer();
- Admin adminClient = createAdminClient()) {
- ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), "Message".getBytes());
- // Producing 5 records to the topic partition.
- for (int i = 0; i < 5; i++) {
- producer.send(record);
- }
- producer.flush();
- // Create a new share consumer.
- ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer(groupId, Map.of(ConsumerConfig.SHARE_ACKNOWLEDGEMENT_MODE_CONFIG, EXPLICIT));
- shareConsumer.subscribe(List.of(tp.topic()));
- // Polling share consumer to make sure it joins the group and consumes the produced records.
- ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 2500L, 5);
- assertEquals(5, records.count());
- // Accept the records first to move the offset forward and register the state with persister.
- records.forEach(r -> shareConsumer.acknowledge(r, AcknowledgeType.ACCEPT));
- shareConsumer.commitSync();
- // After accepting, the lag should be 0 because the record is consumed successfully.
- verifySharePartitionLag(adminClient, groupId, tp, 0L);
- // Closing the share consumer so that the offsets can be deleted.
- shareConsumer.close();
- // Delete the share group offsets.
- deleteShareGroupOffsets(adminClient, groupId, tp.topic());
- // Verify that the share partition offsets are deleted.
- verifySharePartitionOffsetsDeleted(adminClient, groupId, tp);
- // Create a new share consumer.
- ShareConsumer<byte[], byte[]> shareConsumer2 = createShareConsumer(groupId, Map.of(ConsumerConfig.SHARE_ACKNOWLEDGEMENT_MODE_CONFIG, EXPLICIT));
- shareConsumer2.subscribe(List.of(tp.topic()));
- // Since the offsets are deleted, the share consumer should consume from the beginning (share.auto.offset.reset is earliest).
- // Thus, the consumer should consume all 5 records again.
- records = waitedPoll(shareConsumer2, 2500L, 5);
- assertEquals(5, records.count());
- // Accept the records first to move the offset forward and register the state with persister.
- records.forEach(r -> shareConsumer2.acknowledge(r, AcknowledgeType.ACCEPT));
- shareConsumer2.commitSync();
- // After accepting, the lag should be 0 because the records are consumed successfully.
- verifySharePartitionLag(adminClient, groupId, tp, 0L);
- // Closing the share consumer so that the offsets can be deleted.
- shareConsumer2.close();
- } catch (InterruptedException | ExecutionException e) {
- fail("Test failed with exception: " + e.getMessage());
- }
- }
-
- @ClusterTest
- public void testFetchWithThrottledDelivery() {
- alterShareAutoOffsetReset("group1", "earliest");
- try (Producer<byte[], byte[]> producer = createProducer();
- ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer(
- "group1",
- Map.of(ConsumerConfig.SHARE_ACKNOWLEDGEMENT_MODE_CONFIG, EXPLICIT))
- ) {
- // Produce a batch of 100 messages
- for (int i = 0; i < 100; i++) {
- ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), ("Message " + i).getBytes());
- producer.send(record);
- }
- producer.flush();
-
- shareConsumer.subscribe(List.of(tp.topic()));
- // Fetch records in 5 iterations, each time acknowledging with RELEASE. 5 is the default
- // delivery limit hence we should see throttling from Math.ceil(5/2) = 3 fetches.
- int throttleDeliveryLimit = 3;
- for (int i = 0; i < 5; i++) {
- // Adjust expected fetch count based on throttling. If i < throttleDeliveryLimit, we get full batch of 100.
- // If i == 4 i.e. the last delivery, then we get 1 record.
- // Otherwise, we get half the previous fetch count due to throttling. In this case, 100 >> (i - throttleDeliveryLimit + 1) it is 50 for i=3.
- int expectedFetchCount = (i < throttleDeliveryLimit) ? 100 : ((i == 4) ? 1 : 50);
- ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 2500L, expectedFetchCount);
- assertEquals(expectedFetchCount, records.count());
-
- records.forEach(record -> shareConsumer.acknowledge(record, AcknowledgeType.RELEASE));
- Map<TopicIdPartition, Optional<KafkaException>> result = shareConsumer.commitSync();
- assertEquals(1, result.size());
- assertEquals(Optional.empty(),
- result.get(new TopicIdPartition(tpId, tp.partition(), tp.topic())));
- }
-
- // Offset 0 has already reached the delivery limit hence shall be archived.
- // Offset 1 to 49 shall be in last delivery attempt and hence 1 record per poll.
- validateExpectedRecordsInEachPollAndRelease(shareConsumer, 1, 50, 1);
- // Delivery limit 4.
- validateExpectedRecordsInEachPollAndRelease(shareConsumer, 50, 100, 50);
- // Delivery limit 5.
- validateExpectedRecordsInEachPollAndRelease(shareConsumer, 50, 100, 1);
- // Next poll should not have any records as all records have reached delivery limit.
- ConsumerRecords<byte[], byte[]> records = shareConsumer.poll(Duration.ofMillis(2500L));
- assertTrue(records.isEmpty(), "Records should be empty as all records have reached delivery limit. But received: " + records.count());
- }
- }
-
- @ClusterTest(
- serverProperties = {
- @ClusterConfigProperty(key = "group.share.delivery.count.limit", value = "10"),
- }
- )
- public void testFetchWithThrottledDeliveryBatchesWithIncreasedDeliveryLimit() {
- alterShareAutoOffsetReset("group1", "earliest");
- try (Producer<byte[], byte[]> producer = createProducer();
- ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer(
- "group1",
- Map.of(
- ConsumerConfig.SHARE_ACKNOWLEDGEMENT_MODE_CONFIG, EXPLICIT,
- ConsumerConfig.MAX_POLL_RECORDS_CONFIG, 512
- )
- )
- ) {
- // Produce records in complete power of 2 to fully test the throttling behavior.
- int producedMessageCount = 512;
- // Produce a batch of 512 messages
- for (int i = 0; i < producedMessageCount; i++) {
- ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(),
- null, "key".getBytes(), ("Message " + i).getBytes());
- producer.send(record);
- }
- producer.flush();
-
- // Map which defines expected fetch count for each delivery attempt from 1 to 10.
- Map<Integer, Integer> expectedFetchCountMap = Map.of(1, 512, 2, 512, 3, 512, 4, 512, 5, 512,
- 6, 256, 7, 128, 8, 64, 9, 32, 10, 1);
- shareConsumer.subscribe(List.of(tp.topic()));
- // Fetch records in 10 iterations, each time acknowledging with RELEASE. 10 is the
- // delivery limit hence we should see throttling from Math.ceil(10/2) = 5 fetches.
- for (int i = 0; i < 10; i++) {
- int expectedFetchCount = expectedFetchCountMap.get(i + 1);
- ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 2500L, expectedFetchCount);
- assertEquals(expectedFetchCount, records.count());
- // Acknowledge all records with RELEASE.
- records.forEach(record -> shareConsumer.acknowledge(record, AcknowledgeType.RELEASE));
- Map<TopicIdPartition, Optional<KafkaException>> result = shareConsumer.commitSync();
- assertEquals(1, result.size());
- assertEquals(Optional.empty(), result.get(new TopicIdPartition(tpId, tp.partition(), tp.topic())));
- }
-
- // Offset 0 is already verified above, so start from offset 1 and as it's last delivery cycle
- // hence expectedRecords is 1 for each poll till offset 32.
- // Delivery 10
- validateExpectedRecordsInEachPollAndRelease(shareConsumer, 1, 32, 1);
- // Delivery 9
- validateExpectedRecordsInEachPollAndRelease(shareConsumer, 32, 64, 32);
- // Delivery 10
- validateExpectedRecordsInEachPollAndRelease(shareConsumer, 32, 64, 1);
- // Delivery 8
- validateExpectedRecordsInEachPollAndRelease(shareConsumer, 64, 128, 64);
- // Delivery 9
- validateExpectedRecordsInEachPollAndRelease(shareConsumer, 64, 96, 32);
- // Delivery 10
- validateExpectedRecordsInEachPollAndRelease(shareConsumer, 64, 96, 1);
- // Delivery 9
- validateExpectedRecordsInEachPollAndRelease(shareConsumer, 96, 128, 32);
- // Delivery 10
- validateExpectedRecordsInEachPollAndRelease(shareConsumer, 96, 128, 1);
- // Delivery 7
- validateExpectedRecordsInEachPollAndRelease(shareConsumer, 128, 256, 128);
- // Delivery 8
- validateExpectedRecordsInEachPollAndRelease(shareConsumer, 128, 192, 64);
- // Delivery 9
- validateExpectedRecordsInEachPollAndRelease(shareConsumer, 128, 160, 32);
- // Delivery 10
- validateExpectedRecordsInEachPollAndRelease(shareConsumer, 128, 160, 1);
- // Delivery 9
- validateExpectedRecordsInEachPollAndRelease(shareConsumer, 160, 192, 32);
- // Delivery 10
- validateExpectedRecordsInEachPollAndRelease(shareConsumer, 160, 192, 1);
- // Delivery 8
- validateExpectedRecordsInEachPollAndRelease(shareConsumer, 192, 256, 64);
- // Delivery 9
- validateExpectedRecordsInEachPollAndRelease(shareConsumer, 192, 224, 32);
- // Delivery 10
- validateExpectedRecordsInEachPollAndRelease(shareConsumer, 192, 224, 1);
- // Delivery 9
- validateExpectedRecordsInEachPollAndRelease(shareConsumer, 224, 256, 32);
- // Delivery 10
- validateExpectedRecordsInEachPollAndRelease(shareConsumer, 224, 256, 1);
- // Delivery 6
- validateExpectedRecordsInEachPollAndRelease(shareConsumer, 256, 512, 256);
- // Delivery 7
- validateExpectedRecordsInEachPollAndRelease(shareConsumer, 256, 384, 128);
- // Delivery 8
- validateExpectedRecordsInEachPollAndRelease(shareConsumer, 256, 320, 64);
- // Delivery 9
- validateExpectedRecordsInEachPollAndRelease(shareConsumer, 256, 288, 32);
- // Delivery 10
- validateExpectedRecordsInEachPollAndRelease(shareConsumer, 256, 288, 1);
- // Delivery 9
- validateExpectedRecordsInEachPollAndRelease(shareConsumer, 288, 320, 32);
- // Delivery 10
- validateExpectedRecordsInEachPollAndRelease(shareConsumer, 288, 320, 1);
- // Delivery 8
- validateExpectedRecordsInEachPollAndRelease(shareConsumer, 320, 384, 64);
- // Delivery 9
- validateExpectedRecordsInEachPollAndRelease(shareConsumer, 320, 352, 32);
- // Delivery 10
- validateExpectedRecordsInEachPollAndRelease(shareConsumer, 320, 352, 1);
- // Delivery 9
- validateExpectedRecordsInEachPollAndRelease(shareConsumer, 352, 384, 32);
- // Delivery 10
- validateExpectedRecordsInEachPollAndRelease(shareConsumer, 352, 384, 1);
- // Delivery 7
- validateExpectedRecordsInEachPollAndRelease(shareConsumer, 384, 512, 128);
- // Delivery 8
- validateExpectedRecordsInEachPollAndRelease(shareConsumer, 384, 448, 64);
- // Delivery 9
- validateExpectedRecordsInEachPollAndRelease(shareConsumer, 384, 416, 32);
- // Delivery 10
- validateExpectedRecordsInEachPollAndRelease(shareConsumer, 384, 416, 1);
- // Delivery 9
- validateExpectedRecordsInEachPollAndRelease(shareConsumer, 416, 448, 32);
- // Delivery 10
- validateExpectedRecordsInEachPollAndRelease(shareConsumer, 416, 448, 1);
- // Delivery 8
- validateExpectedRecordsInEachPollAndRelease(shareConsumer, 448, 512, 64);
- // Delivery 9
- validateExpectedRecordsInEachPollAndRelease(shareConsumer, 448, 480, 32);
- // Delivery 10
- validateExpectedRecordsInEachPollAndRelease(shareConsumer, 448, 480, 1);
- // Delivery 9
- validateExpectedRecordsInEachPollAndRelease(shareConsumer, 480, 512, 32);
- // Delivery 10
- validateExpectedRecordsInEachPollAndRelease(shareConsumer, 480, 512, 1);
- // Next poll should not have any records as all records have reached delivery limit.
- ConsumerRecords<byte[], byte[]> records = shareConsumer.poll(Duration.ofMillis(2500L));
- assertTrue(records.isEmpty(), "Records should be empty as all records have reached delivery limit. But received: " + records.count());
- }
- }
-
- @ClusterTest(
- serverProperties = {
- @ClusterConfigProperty(key = "group.share.delivery.count.limit", value = "10"),
- }
- )
- public void testFetchWithThrottledDeliveryValidateDeliveryCount() throws InterruptedException {
- alterShareAutoOffsetReset("group1", "earliest");
- try (Producer<byte[], byte[]> producer = createProducer();
- ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer(
- "group1",
- Map.of(ConsumerConfig.SHARE_ACKNOWLEDGEMENT_MODE_CONFIG, EXPLICIT))
- ) {
- int producedMessageCount = 500;
- // Produce a batch of 500 messages
- for (int i = 0; i < producedMessageCount; i++) {
- ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(),
- null, "key".getBytes(), ("Message " + i).getBytes());
- producer.send(record);
- }
- producer.flush();
-
- // Map to track delivery count for each offset.
- Map<Long, Integer> offsetToDeliveryCountMap = new HashMap<>();
- // Map which defines expected fetch count for each delivery attempt from 1 to 10.
- Map<Integer, Integer> expectedFetchCountMap = Map.of(1, 500, 2, 500, 3, 500, 4, 500, 5, 500,
- 6, 250, 7, 125, 8, 62, 9, 31, 10, 1);
- shareConsumer.subscribe(List.of(tp.topic()));
- // Fetch records in 10 iterations, each time acknowledging with RELEASE. 10 is the
- // delivery limit hence we should see throttling from Math.ceil(10/2) = 5 fetches.
- for (int i = 0; i < 10; i++) {
- int expectedFetchCount = expectedFetchCountMap.get(i + 1);
- ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 2500L, expectedFetchCount);
- assertEquals(expectedFetchCount, records.count());
- // Update delivery count for each offset.
- records.forEach(record -> {
- if (!offsetToDeliveryCountMap.containsKey(record.offset())) {
- offsetToDeliveryCountMap.put(record.offset(), 1);
- } else {
- offsetToDeliveryCountMap.put(record.offset(), offsetToDeliveryCountMap.get(record.offset()) + 1);
- }
- });
- // Acknowledge with RELEASE.
- records.forEach(record -> shareConsumer.acknowledge(record, AcknowledgeType.RELEASE));
- Map<TopicIdPartition, Optional<KafkaException>> result = shareConsumer.commitSync();
- assertEquals(1, result.size());
- assertEquals(Optional.empty(), result.get(new TopicIdPartition(tpId, tp.partition(), tp.topic())));
- }
-
- // Validate every offset is delivered at most till delivery limit.
- waitForCondition(() -> {
- ConsumerRecords<byte[], byte[]> records = shareConsumer.poll(Duration.ofMillis(2500L));
- if (!records.isEmpty()) {
- records.forEach(record -> {
- if (!offsetToDeliveryCountMap.containsKey(record.offset())) {
- offsetToDeliveryCountMap.put(record.offset(), 1);
- } else {
- offsetToDeliveryCountMap.put(record.offset(), offsetToDeliveryCountMap.get(record.offset()) + 1);
- }
- });
- records.forEach(record -> shareConsumer.acknowledge(record, AcknowledgeType.RELEASE));
- Map<TopicIdPartition, Optional<KafkaException>> result = shareConsumer.commitSync();
- assertEquals(1, result.size());
- assertEquals(Optional.empty(),
- result.get(new TopicIdPartition(tpId, tp.partition(), tp.topic())));
- }
- return offsetToDeliveryCountMap.size() == 500 &&
- offsetToDeliveryCountMap.values().stream().allMatch(deliveryCount -> deliveryCount == 10);
- },
- 120000L, // 120 seconds.
- 50L,
- () -> "failed to get records till delivery limit"
- );
-
- // Next poll should not have any records as all records have reached delivery limit.
- ConsumerRecords<byte[], byte[]> records = shareConsumer.poll(Duration.ofMillis(2500L));
- assertTrue(records.isEmpty(), "Records should be empty as all records have reached delivery limit. But received: " + records.count());
- }
- }
-
- @ClusterTest(
- serverProperties = {
- @ClusterConfigProperty(key = "group.share.delivery.count.limit", value = "2"),
- }
- )
- public void testFetchWithThrottledDeliveryBatchesWithDecreasedDeliveryLimit() {
- alterShareAutoOffsetReset("group1", "earliest");
- try (Producer<byte[], byte[]> producer = createProducer();
- ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer(
- "group1",
- Map.of(
- ConsumerConfig.SHARE_ACKNOWLEDGEMENT_MODE_CONFIG, EXPLICIT,
- ConsumerConfig.MAX_POLL_RECORDS_CONFIG, 512
- ))
- ) {
- // Produce records in complete power of 2 to fully test the throttling behavior.
- int producedMessageCount = 512;
- // Produce a batch of 512 messages
- for (int i = 0; i < producedMessageCount; i++) {
- ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(),
- null, "key".getBytes(), ("Message " + i).getBytes());
- producer.send(record);
- }
- producer.flush();
-
- shareConsumer.subscribe(List.of(tp.topic()));
- // Fetch records in 2 iterations, each time acknowledging with RELEASE. As throttling
- // currently applies for delivery limit > 2, hence we should get full batch in both fetches.
- validateExpectedRecordsInEachPollAndRelease(shareConsumer, 0, 512, 512);
- validateExpectedRecordsInEachPollAndRelease(shareConsumer, 0, 512, 512);
- // Next poll should not have any records as all records have reached delivery limit.
- ConsumerRecords<byte[], byte[]> records = shareConsumer.poll(Duration.ofMillis(2500L));
- assertTrue(records.isEmpty(), "Records should be empty as all records have reached delivery limit. But received: " + records.count());
- }
- }
-
- @ClusterTest
- public void testFetchWithThrottledDeliveryBatchesMultipleConsumers() {
- alterShareAutoOffsetReset("group1", "earliest");
- try (Producer<byte[], byte[]> producer = createProducer();
- ShareConsumer<byte[], byte[]> shareConsumer1 = createShareConsumer(
- "group1",
- Map.of(
- ConsumerConfig.SHARE_ACKNOWLEDGEMENT_MODE_CONFIG, EXPLICIT,
- ConsumerConfig.MAX_POLL_RECORDS_CONFIG, 1
- ));
- ShareConsumer<byte[], byte[]> shareConsumer2 = createShareConsumer(
- "group1",
- Map.of(
- ConsumerConfig.SHARE_ACKNOWLEDGEMENT_MODE_CONFIG, EXPLICIT,
- ConsumerConfig.MAX_POLL_RECORDS_CONFIG, 2
- ))
- ) {
- // Produce 2 records in separate batches.
- for (int i = 0; i < 2; i++) {
- ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(),
- null, "key".getBytes(), ("Message " + i).getBytes());
- producer.send(record);
- // Flush immediately to create 2 different batches.
- producer.flush();
- }
-
- shareConsumer1.subscribe(List.of(tp.topic()));
- shareConsumer2.subscribe(List.of(tp.topic()));
- // Fetch from consumer1 - should get 1 record as max.poll.records=1.
- ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer1, 2500L, 1);
- assertEquals(1, records.count());
- // Verify the first offset of the fetched records.
- assertEquals(0, records.iterator().next().offset());
- // Fetch from consumer2 - should get 1 record as offset 0 is Acquired by consumer1.
- // Release the record from consumer2 after fetching until the last delivery attempt.
- validateExpectedRecordsInEachPollAndRelease(shareConsumer2, 1, 2, 1);
- validateExpectedRecordsInEachPollAndRelease(shareConsumer2, 1, 2, 1);
- validateExpectedRecordsInEachPollAndRelease(shareConsumer2, 1, 2, 1);
- validateExpectedRecordsInEachPollAndRelease(shareConsumer2, 1, 2, 1);
-
- // Now release the record from consumer1. Fetch again from consumer2 to verify it gets the released record.
- // And should only get 1 record at offset 0 as offset 1 record is in final delivery attempt.
- records.forEach(record -> shareConsumer1.acknowledge(record, AcknowledgeType.RELEASE));
- Map<TopicIdPartition, Optional<KafkaException>> result = shareConsumer1.commitSync();
- assertEquals(1, result.size());
- assertEquals(Optional.empty(), result.get(new TopicIdPartition(tpId, tp.partition(), tp.topic())));
- // Fetch from consumer2 - should get the released record at offset 0. Accept the record after fetching.
- validateExpectedRecordsInEachPollAndAcknowledge(shareConsumer2, 0, 1, 1, AcknowledgeType.ACCEPT);
- // Now fetch the last record at offset 1 from consumer2 in its final delivery attempt.
- validateExpectedRecordsInEachPollAndAcknowledge(shareConsumer2, 1, 2, 1, AcknowledgeType.ACCEPT);
-
- // Next poll from consumer1 should not have any records as all records have reached delivery limit.
- records = shareConsumer1.poll(Duration.ofMillis(2500L));
- assertTrue(records.isEmpty(), "Records should be empty as all records have reached delivery limit. But received: " + records.count());
- }
- }
-
- @ClusterTest
- public void testDynamicDeliveryCountLimitDecreaseArchivesRecords() {
- alterShareAutoOffsetReset("group1", "earliest");
- try (Producer<byte[], byte[]> producer = createProducer();
- ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer(
- "group1",
- Map.of(ConsumerConfig.SHARE_ACKNOWLEDGEMENT_MODE_CONFIG, EXPLICIT))
- ) {
- // Produce 1 record.
- ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(),
- null, "key".getBytes(), "value".getBytes());
- producer.send(record);
- producer.flush();
-
- shareConsumer.subscribe(Set.of(tp.topic()));
-
- // Consume and release twice (deliveryCount becomes 2).
- // Delivery 1: acquire (deliveryCount=1) → release (1 < default limit 5 → AVAILABLE).
- ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 2500L, 1);
- assertEquals(1, records.count());
- records.forEach(r -> shareConsumer.acknowledge(r, AcknowledgeType.RELEASE));
- shareConsumer.commitSync();
-
- // Delivery 2: acquire (deliveryCount=2) → release (2 < 5 → AVAILABLE).
- records = waitedPoll(shareConsumer, 2500L, 1);
- assertEquals(1, records.count());
- assertEquals((short) 2, records.records(tp).get(0).deliveryCount().get());
- records.forEach(r -> shareConsumer.acknowledge(r, AcknowledgeType.RELEASE));
- shareConsumer.commitSync();
-
- // Dynamically decrease delivery count limit to 3 via group config.
- alterShareDeliveryCountLimit("group1", "3");
-
- // Delivery 3: acquire (deliveryCount=3) → release (3 >= 3 → ARCHIVED).
- records = waitedPoll(shareConsumer, 2500L, 1);
- assertEquals(1, records.count());
- assertEquals((short) 3, records.records(tp).get(0).deliveryCount().get());
- records.forEach(r -> shareConsumer.acknowledge(r, AcknowledgeType.RELEASE));
- shareConsumer.commitSync();
-
- // Next poll should have no records because the record was archived.
- records = shareConsumer.poll(Duration.ofMillis(2500L));
- assertTrue(records.isEmpty(),
- "Records should be empty as the record was archived. But received: " + records.count());
- }
- }
-
- @ClusterTest(
- serverProperties = {
- @ClusterConfigProperty(key = "group.share.delivery.count.limit", value = "3"),
- }
- )
- public void testDynamicDeliveryCountLimitIncreaseAllowsMoreDeliveries() {
- alterShareAutoOffsetReset("group1", "earliest");
- try (Producer<byte[], byte[]> producer = createProducer();
- ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer(
- "group1",
- Map.of(ConsumerConfig.SHARE_ACKNOWLEDGEMENT_MODE_CONFIG, EXPLICIT))
- ) {
- // Produce 1 record.
- ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(),
- null, "key".getBytes(), "value".getBytes());
- producer.send(record);
- producer.flush();
-
- shareConsumer.subscribe(Set.of(tp.topic()));
-
- // Consume and release twice (deliveryCount becomes 2).
- // Delivery 1: acquire (deliveryCount=1) → release (1 < broker limit 3 → AVAILABLE).
- ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 2500L, 1);
- assertEquals(1, records.count());
- records.forEach(r -> shareConsumer.acknowledge(r, AcknowledgeType.RELEASE));
- shareConsumer.commitSync();
-
- // Delivery 2: acquire (deliveryCount=2) → release (2 < 3 → AVAILABLE).
- records = waitedPoll(shareConsumer, 2500L, 1);
- assertEquals(1, records.count());
- assertEquals((short) 2, records.records(tp).get(0).deliveryCount().get());
- records.forEach(r -> shareConsumer.acknowledge(r, AcknowledgeType.RELEASE));
- shareConsumer.commitSync();
-
- // Dynamically increase delivery count limit to 5 via group config.
- alterShareDeliveryCountLimit("group1", "5");
-
- // Delivery 3: acquire (deliveryCount=3) → release (3 < 5 → AVAILABLE).
- // Without the config increase, 3 >= 3 would have caused archival.
- records = waitedPoll(shareConsumer, 2500L, 1);
- assertEquals(1, records.count());
- assertEquals((short) 3, records.records(tp).get(0).deliveryCount().get());
- records.forEach(r -> shareConsumer.acknowledge(r, AcknowledgeType.RELEASE));
- shareConsumer.commitSync();
-
- // Delivery 4: acquire (deliveryCount=4) → release (4 < 5 → AVAILABLE).
- records = waitedPoll(shareConsumer, 2500L, 1);
- assertEquals(1, records.count());
- assertEquals((short) 4, records.records(tp).get(0).deliveryCount().get());
- records.forEach(r -> shareConsumer.acknowledge(r, AcknowledgeType.RELEASE));
- shareConsumer.commitSync();
-
- // Delivery 5: acquire (deliveryCount=5) → release (5 >= 5 → ARCHIVED).
- records = waitedPoll(shareConsumer, 2500L, 1);
- assertEquals(1, records.count());
- assertEquals((short) 5, records.records(tp).get(0).deliveryCount().get());
- records.forEach(r -> shareConsumer.acknowledge(r, AcknowledgeType.RELEASE));
- shareConsumer.commitSync();
-
- // Next poll should have no records because the record was archived.
- records = shareConsumer.poll(Duration.ofMillis(2500L));
- assertTrue(records.isEmpty(),
- "Records should be empty as the record was archived. But received: " + records.count());
- }
- }
-
- @ClusterTest
- public void testDynamicPartitionMaxRecordLocks() {
- // Verify that the group-level share.partition.max.record.locks config can be
- // dynamically set and read back via describe configs.
- alterSharePartitionMaxRecordLocks("group1", "500");
-
- // Verify the config can be updated dynamically.
- alterSharePartitionMaxRecordLocks("group1", "1000");
- }
-
- /**
- * Util class to encapsulate state for a consumer/producer
- * being executed by an {@link ExecutorService}.
- */
- private static class ClientState {
- private final AtomicBoolean done = new AtomicBoolean(false);
- private final AtomicInteger count = new AtomicInteger(0);
-
- AtomicBoolean done() {
- return done;
- }
-
- AtomicInteger count() {
- return count;
- }
- }
-
- private int produceMessages(int messageCount) {
- try (Producer<byte[], byte[]> producer = createProducer()) {
- ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), "value".getBytes());
- IntStream.range(0, messageCount).forEach(__ -> producer.send(record));
- producer.flush();
- }
- return messageCount;
- }
-
- private void produceMessagesWithTimestamp(int messageCount, long startingTimestamp) {
- try (Producer<byte[], byte[]> producer = createProducer()) {
- for (int i = 0; i < messageCount; i++) {
- ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), startingTimestamp + i,
- ("key " + i).getBytes(), ("value " + i).getBytes());
- producer.send(record);
- }
- producer.flush();
- }
- }
-
- private void produceCommittedTransaction(Producer<byte[], byte[]> transactionalProducer, String message) {
- try {
- transactionalProducer.beginTransaction();
- ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, message.getBytes(), message.getBytes());
- Future<RecordMetadata> future = transactionalProducer.send(record);
- transactionalProducer.flush();
- future.get(); // Ensure producer send is complete before committing
- transactionalProducer.commitTransaction();
- } catch (Exception e) {
- transactionalProducer.abortTransaction();
- }
- }
-
- private void produceAbortedTransaction(Producer<byte[], byte[]> transactionalProducer, String message) {
- try {
- transactionalProducer.beginTransaction();
- ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, message.getBytes(), message.getBytes());
- Future<RecordMetadata> future = transactionalProducer.send(record);
- transactionalProducer.flush();
- future.get(); // Ensure producer send is complete before aborting
- transactionalProducer.abortTransaction();
- } catch (Exception e) {
- transactionalProducer.abortTransaction();
- }
- }
-
- private void produceCommittedAndAbortedTransactionsInInterval(Producer<byte[], byte[]> transactionalProducer, int messageCount, int intervalAbortedTransactions) {
- transactionalProducer.initTransactions();
- int transactionCount = 0;
- try {
- for (int i = 0; i < messageCount; i++) {
- transactionalProducer.beginTransaction();
- String recordMessage = "Message " + (i + 1);
- ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, recordMessage.getBytes(), recordMessage.getBytes());
- Future<RecordMetadata> future = transactionalProducer.send(record);
- transactionalProducer.flush();
- // Increment transaction count
- transactionCount++;
- if (transactionCount % intervalAbortedTransactions == 0) {
- // Aborts every intervalAbortedTransactions transaction
- transactionalProducer.abortTransaction();
- } else {
- // Commits other transactions
- future.get(); // Ensure producer send is complete before committing
- transactionalProducer.commitTransaction();
- }
- }
- } catch (Exception e) {
- transactionalProducer.abortTransaction();
- } finally {
- transactionalProducer.close();
- }
- }
-
- private int consumeMessages(AtomicInteger totalMessagesConsumed,
- int totalMessages,
- String groupId,
- int consumerNumber,
- int maxPolls,
- boolean commit) {
- return assertDoesNotThrow(() -> {
- try (ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer(groupId)) {
- shareConsumer.subscribe(Set.of(tp.topic()));
- return consumeMessages(shareConsumer, totalMessagesConsumed, totalMessages, consumerNumber, maxPolls, commit);
- }
- }, "Consumer " + consumerNumber + " failed with exception");
- }
-
- private int consumeMessages(AtomicInteger totalMessagesConsumed,
- int totalMessages,
- String groupId,
- int consumerNumber,
- int maxPolls,
- boolean commit,
- int maxFetchBytes) {
- return assertDoesNotThrow(() -> {
- try (ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer(
- groupId,
- Map.of(ConsumerConfig.MAX_PARTITION_FETCH_BYTES_CONFIG, maxFetchBytes))) {
- shareConsumer.subscribe(Set.of(tp.topic()));
- return consumeMessages(shareConsumer, totalMessagesConsumed, totalMessages, consumerNumber, maxPolls, commit);
- }
- }, "Consumer " + consumerNumber + " failed with exception");
- }
-
- private int consumeMessages(ShareConsumer<byte[], byte[]> consumer,
- AtomicInteger totalMessagesConsumed,
- int totalMessages,
- int consumerNumber,
- int maxPolls,
- boolean commit) {
- return assertDoesNotThrow(() -> {
- int messagesConsumed = 0;
- int retries = 0;
- if (totalMessages > 0) {
- while (totalMessagesConsumed.get() < totalMessages && retries < maxPolls) {
- ConsumerRecords<byte[], byte[]> records = consumer.poll(Duration.ofMillis(2000));
- messagesConsumed += records.count();
- totalMessagesConsumed.addAndGet(records.count());
- retries++;
- }
- } else {
- while (retries < maxPolls) {
- ConsumerRecords<byte[], byte[]> records = consumer.poll(Duration.ofMillis(2000));
- messagesConsumed += records.count();
- totalMessagesConsumed.addAndGet(records.count());
- retries++;
- }
- }
-
- if (commit) {
- // Complete acknowledgement of the records
- consumer.commitSync(Duration.ofMillis(10000));
- }
- return messagesConsumed;
- }, "Consumer " + consumerNumber + " failed with exception");
- }
-
- private <K, V> List<ConsumerRecord<K, V>> consumeRecords(ShareConsumer<K, V> consumer,
- int numRecords) {
- ArrayList<ConsumerRecord<K, V>> accumulatedRecords = new ArrayList<>();
- long startTimeMs = System.currentTimeMillis();
- while (accumulatedRecords.size() < numRecords) {
- ConsumerRecords<K, V> records = consumer.poll(Duration.ofMillis(100));
- records.forEach(accumulatedRecords::add);
- long currentTimeMs = System.currentTimeMillis();
- assertFalse(currentTimeMs - startTimeMs > 60000, "Timed out before consuming expected records.");
- }
- return accumulatedRecords;
- }
-
- private Uuid createTopic(String topicName) {
- return createTopic(topicName, 1, 1);
- }
-
- private Uuid createTopic(String topicName, int numPartitions, int replicationFactor) {
- AtomicReference<Uuid> topicId = new AtomicReference<>(null);
- assertDoesNotThrow(() -> {
- try (Admin admin = createAdminClient()) {
- CreateTopicsResult result = admin.createTopics(Set.of(new NewTopic(topicName, numPartitions, (short) replicationFactor)));
- result.all().get();
- topicId.set(result.topicId(topicName).get());
- }
- }, "Failed to create topic");
-
- return topicId.get();
- }
-
- private void deleteTopic(String topicName) {
- assertDoesNotThrow(() -> {
- try (Admin admin = createAdminClient()) {
- admin.deleteTopics(Set.of(topicName)).all().get();
- }
- }, "Failed to delete topic");
- }
-
- private Admin createAdminClient() {
- return cluster.admin();
- }
-
- private <K, V> Producer<K, V> createProducer() {
- return createProducer(Map.of());
- }
-
- private <K, V> Producer<K, V> createProducer(Map<String, Object> config) {
- return cluster.producer(config);
- }
-
- private <K, V> Producer<K, V> createProducer(String transactionalId) {
- return createProducer(
- Map.of(
- ProducerConfig.TRANSACTIONAL_ID_CONFIG, transactionalId
- )
- );
- }
-
- private <K, V> ShareConsumer<K, V> createShareConsumer(String groupId) {
- return createShareConsumer(groupId, Map.of());
- }
-
- private <K, V> ShareConsumer<K, V> createShareConsumer(
- String groupId,
- Map<?, ?> additionalProperties
- ) {
- return createShareConsumer(groupId, additionalProperties, null, null);
- }
-
- private <K, V> ShareConsumer<K, V> createShareConsumer(
- String groupId,
- Map<?, ?> additionalProperties,
- Deserializer<K> keyDeserializer,
- Deserializer<V> valueDeserializer
- ) {
- Properties props = new Properties();
- props.putAll(additionalProperties);
- props.put(ConsumerConfig.GROUP_ID_CONFIG, groupId);
- Map<String, Object> conf = new HashMap<>();
- props.forEach((k, v) -> conf.put((String) k, v));
- return cluster.shareConsumer(conf, keyDeserializer, valueDeserializer);
- }
-
- private void warmup() throws InterruptedException {
- createTopic(warmupTp.topic());
- waitForMetadataCache();
- ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(warmupTp.topic(), warmupTp.partition(), null, "key".getBytes(), "value".getBytes());
- Set<String> subscription = Set.of(warmupTp.topic());
- alterShareAutoOffsetReset("warmupgroup1", "earliest");
- try (Producer<byte[], byte[]> producer = createProducer();
- ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer("warmupgroup1")) {
-
- producer.send(record);
- producer.flush();
-
- shareConsumer.subscribe(subscription);
- TestUtils.waitForCondition(
- () -> shareConsumer.poll(Duration.ofMillis(5000)).count() == 1, 30000, 200L, () -> "warmup record not received");
- }
- }
-
- private void waitForMetadataCache() throws InterruptedException {
- TestUtils.waitForCondition(() ->
- !cluster.brokers().get(0).metadataCache().getAliveBrokerNodes(new ListenerName("EXTERNAL")).isEmpty(),
- DEFAULT_MAX_WAIT_MS, 100L, () -> "cache not up yet");
- }
-
- private void verifyShareGroupStateTopicRecordsProduced() {
- try {
- try (Consumer<byte[], byte[]> consumer = cluster.consumer()) {
- consumer.assign(sgsTopicPartitions);
- consumer.seekToBeginning(sgsTopicPartitions);
- Set<ConsumerRecord<byte[], byte[]>> records = new HashSet<>();
- TestUtils.waitForCondition(() -> {
- ConsumerRecords<byte[], byte[]> msgs = consumer.poll(Duration.ofMillis(5000L));
- if (msgs.count() > 0) {
- msgs.records(Topic.SHARE_GROUP_STATE_TOPIC_NAME).forEach(records::add);
- }
- return records.size() > 2; // +2 because of extra warmup records
- },
- 30000L,
- 200L,
- () -> "no records produced"
- );
- }
- } catch (InterruptedException e) {
- fail(e);
- }
- }
-
- private void alterShareGroupConfig(String groupId, String configKey, String newValue) {
- ConfigResource configResource = new ConfigResource(ConfigResource.Type.GROUP, groupId);
- Map<ConfigResource, Collection<AlterConfigOp>> alterEntries = new HashMap<>();
- alterEntries.put(configResource, List.of(new AlterConfigOp(new ConfigEntry(
- configKey, newValue), AlterConfigOp.OpType.SET)));
- AlterConfigsOptions alterOptions = new AlterConfigsOptions();
- try (Admin adminClient = createAdminClient()) {
- assertDoesNotThrow(() -> adminClient.incrementalAlterConfigs(alterEntries, alterOptions)
- .all()
- .get(60, TimeUnit.SECONDS), "Failed to alter configs");
- }
-
- // This config is changed dynamically in tests, and we need it to have propagated before the test proceeds.
- // Describing the config with a new admin client is not totally foolproof, but it's better than just
- // altering the config and continuing.
- try (Admin adminClient = createAdminClient()) {
- assertDoesNotThrow(() ->
- TestUtils.waitForCondition(() -> {
- Config config = adminClient.describeConfigs(List.of(configResource)).all().get().get(configResource);
- ConfigEntry entry = config.get(configKey);
- return entry != null && entry.value().equals(newValue);
- }, 10000L, 100L, () -> "New config value did not propagate"), "Failed to describe configs");
- }
- }
-
- private void alterShareAutoOffsetReset(String groupId, String newValue) {
- alterShareGroupConfig(groupId, GroupConfig.SHARE_AUTO_OFFSET_RESET_CONFIG, newValue);
- }
-
- private void alterShareDeliveryCountLimit(String groupId, String newValue) {
- alterShareGroupConfig(groupId, GroupConfig.SHARE_DELIVERY_COUNT_LIMIT_CONFIG, newValue);
- }
-
- private void alterShareIsolationLevel(String groupId, String newValue) {
- alterShareGroupConfig(groupId, GroupConfig.SHARE_ISOLATION_LEVEL_CONFIG, newValue);
- }
-
- private void alterShareRenewAcknowledgeEnable(String groupId, boolean newValue) {
- alterShareGroupConfig(groupId, GroupConfig.SHARE_RENEW_ACKNOWLEDGE_ENABLE_CONFIG, Boolean.toString(newValue));
- }
-
- private List<Integer> topicPartitionLeader(Admin adminClient, String topicName, int partition) throws InterruptedException, ExecutionException {
- return adminClient.describeTopics(List.of(topicName)).allTopicNames().get().get(topicName)
- .partitions().stream()
- .filter(info -> info.partition() == partition)
- .map(info -> info.leader().id())
- .filter(info -> info != -1)
- .toList();
- }
-
- private SharePartitionOffsetInfo sharePartitionOffsetInfo(Admin adminClient, String groupId, TopicPartition tp) throws InterruptedException, ExecutionException {
- SharePartitionOffsetInfo partitionResult;
- ListShareGroupOffsetsResult result = adminClient.listShareGroupOffsets(
- Map.of(groupId, new ListShareGroupOffsetsSpec().topicPartitions(List.of(tp))),
- new ListShareGroupOffsetsOptions().timeoutMs(30000)
- );
- partitionResult = result.partitionsToOffsetInfo(groupId).get().get(tp);
- return partitionResult;
- }
-
- private void verifySharePartitionStartOffset(Admin adminClient, String groupId, TopicPartition tp, long expectedStartOffset) throws InterruptedException {
- TestUtils.waitForCondition(() -> {
- SharePartitionOffsetInfo sharePartitionOffsetInfo = sharePartitionOffsetInfo(adminClient, groupId, tp);
- return sharePartitionOffsetInfo != null &&
- sharePartitionOffsetInfo.startOffset() == expectedStartOffset;
- }, DEFAULT_MAX_WAIT_MS, DEFAULT_POLL_INTERVAL_MS, () -> "Failed to retrieve share partition lag");
- }
-
- private void verifySharePartitionLag(Admin adminClient, String groupId, TopicPartition tp, long expectedLag) throws InterruptedException {
- TestUtils.waitForCondition(() -> {
- SharePartitionOffsetInfo sharePartitionOffsetInfo = sharePartitionOffsetInfo(adminClient, groupId, tp);
- return sharePartitionOffsetInfo != null &&
- sharePartitionOffsetInfo.lag().isPresent() &&
- sharePartitionOffsetInfo.lag().get() == expectedLag;
- }, DEFAULT_MAX_WAIT_MS, DEFAULT_POLL_INTERVAL_MS, () -> "Failed to retrieve share partition lag");
- }
-
- private void verifySharePartitionOffsetsDeleted(Admin adminClient, String groupId, TopicPartition tp) throws InterruptedException {
- TestUtils.waitForCondition(
- () -> sharePartitionOffsetInfo(adminClient, groupId, tp) == null,
- DEFAULT_MAX_WAIT_MS,
- DEFAULT_POLL_INTERVAL_MS,
- () -> "Failed to retrieve share partition lag");
- }
-
- private void alterShareGroupOffsets(Admin adminClient, String groupId, TopicPartition topicPartition, Long newOffset) throws InterruptedException, ExecutionException {
- adminClient.alterShareGroupOffsets(
- groupId,
- Map.of(topicPartition, newOffset),
- new AlterShareGroupOffsetsOptions().timeoutMs(30000)).partitionResult(topicPartition).get();
- }
-
- private void deleteShareGroupOffsets(Admin adminClient, String groupId, String topic) throws InterruptedException, ExecutionException {
- adminClient.deleteShareGroupOffsets(
- groupId,
- Set.of(topic),
- new DeleteShareGroupOffsetsOptions().timeoutMs(30000)).topicResult(topic).get();
- }
-
- private void alterSharePartitionMaxRecordLocks(String groupId, String newValue) {
- alterShareGroupConfig(groupId, GroupConfig.SHARE_PARTITION_MAX_RECORD_LOCKS_CONFIG, newValue);
- }
-
- private void alterShareRecordLockDurationMs(String groupId, int newValue) {
- alterShareGroupConfig(groupId, GroupConfig.SHARE_RECORD_LOCK_DURATION_MS_CONFIG, Integer.toString(newValue));
- }
-
- /**
- * Test utility which encapsulates a {@link ShareConsumer} whose record processing
- * behavior can be supplied as a function argument.
- * <p></p>
- * This can be used to create different consume patterns on the broker and study
- * the status of broker side share group abstractions.
- *
- * @param <K> - key type of the records consumed
- * @param <V> - value type of the records consumed
- */
- private static class ComplexShareConsumer<K, V> implements Runnable {
- public static final int POLL_TIMEOUT_MS = 15000;
- public static final int MAX_DELIVERY_COUNT = ShareGroupConfig.SHARE_GROUP_DELIVERY_COUNT_LIMIT_DEFAULT;
-
- private final String topicName;
- private final Map<String, Object> configs = new HashMap<>();
- private final ClientState state = new ClientState();
- private final Predicate<ConsumerRecords<K, V>> exitCriteria;
- private final BiConsumer<ShareConsumer<K, V>, ConsumerRecord<K, V>> processFunc;
-
- ComplexShareConsumer(
- String bootstrapServers,
- String topicName,
- String groupId,
- Map<String, Object> additionalProperties
- ) {
- this(
- bootstrapServers,
- topicName,
- groupId,
- additionalProperties,
- records -> records.count() == 0,
- (consumer, record) -> {
- short deliveryCountBeforeAccept = (short) ((record.offset() + record.offset() / (MAX_DELIVERY_COUNT + 2)) % (MAX_DELIVERY_COUNT + 2));
- if (deliveryCountBeforeAccept == 0) {
- consumer.acknowledge(record, AcknowledgeType.REJECT);
- } else if (record.deliveryCount().get() == deliveryCountBeforeAccept) {
- consumer.acknowledge(record, AcknowledgeType.ACCEPT);
- } else {
- consumer.acknowledge(record, AcknowledgeType.RELEASE);
- }
- }
- );
- }
-
- ComplexShareConsumer(
- String bootstrapServers,
- String topicName,
- String groupId,
- Map<String, Object> additionalProperties,
- Predicate<ConsumerRecords<K, V>> exitCriteria,
- BiConsumer<ShareConsumer<K, V>, ConsumerRecord<K, V>> processFunc
- ) {
- this.exitCriteria = Objects.requireNonNull(exitCriteria);
- this.processFunc = Objects.requireNonNull(processFunc);
- this.topicName = topicName;
- this.configs.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
- this.configs.put(ConsumerConfig.GROUP_ID_CONFIG, groupId);
- this.configs.putAll(additionalProperties);
- this.configs.putIfAbsent(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, ByteArrayDeserializer.class);
- this.configs.putIfAbsent(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, ByteArrayDeserializer.class);
- }
-
- @Override
- public void run() {
- try (ShareConsumer<K, V> consumer = new KafkaShareConsumer<>(configs)) {
- consumer.subscribe(Set.of(this.topicName));
- while (!state.done().get()) {
- ConsumerRecords<K, V> records = consumer.poll(Duration.ofMillis(POLL_TIMEOUT_MS));
- state.count().addAndGet(records.count());
- if (exitCriteria.test(records)) {
- state.done().set(true);
- }
- records.forEach(record -> processFunc.accept(consumer, record));
- }
- }
- }
-
- boolean isDone() {
- return state.done().get();
- }
-
- int recordsRead() {
- return state.count().get();
- }
- }
-
- private void shutdownExecutorService(ExecutorService service) {
- service.shutdown();
- try {
- if (!service.awaitTermination(5L, TimeUnit.SECONDS)) {
- service.shutdownNow();
- }
- } catch (Exception e) {
- service.shutdownNow();
- Thread.currentThread().interrupt();
- }
- }
-
- private ConsumerRecords<byte[], byte[]> waitedPoll(
- ShareConsumer<byte[], byte[]> shareConsumer,
- long pollMs,
- int recordCount
- ) {
- return waitedPoll(shareConsumer, pollMs, recordCount, false, "", List.of());
- }
-
- private ConsumerRecords<byte[], byte[]> waitedPoll(
- ShareConsumer<byte[], byte[]> shareConsumer,
- long pollMs,
- int recordCount,
- boolean checkAssignment,
- String groupId,
- List<TopicPartition> tps
- ) {
- AtomicReference<ConsumerRecords<byte[], byte[]>> recordsAtomic = new AtomicReference<>();
- try {
- waitForCondition(() -> {
- ConsumerRecords<byte[], byte[]> recs = shareConsumer.poll(Duration.ofMillis(pollMs));
- recordsAtomic.set(recs);
- if (checkAssignment) {
- waitForAssignment(groupId, tps);
- }
- return recs.count() == recordCount;
- },
- DEFAULT_MAX_WAIT_MS,
- 500L,
- () -> "failed to get records"
- );
- return recordsAtomic.get();
- } catch (InterruptedException e) {
- throw new RuntimeException(e);
- }
- }
-
- private void validateExpectedRecordsInEachPollAndRelease(
- ShareConsumer<byte[], byte[]> shareConsumer,
- int startOffset,
- int lastOffset,
- int expectedRecordsInEachPoll
- ) {
- validateExpectedRecordsInEachPollAndAcknowledge(shareConsumer, startOffset, lastOffset, expectedRecordsInEachPoll, AcknowledgeType.RELEASE);
- }
-
- private void validateExpectedRecordsInEachPollAndAcknowledge(
- ShareConsumer<byte[], byte[]> shareConsumer,
- int startOffset,
- int lastOffset,
- int expectedRecordsInEachPoll,
- AcknowledgeType acknowledgeType
- ) {
- for (int i = startOffset; i < lastOffset; i = i + expectedRecordsInEachPoll) {
- ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 2500L, expectedRecordsInEachPoll);
- assertEquals(expectedRecordsInEachPoll, records.count());
- // Verify the first offset of the fetched records.
- assertEquals(i, records.iterator().next().offset());
-
- records.forEach(record -> shareConsumer.acknowledge(record, acknowledgeType));
- Map<TopicIdPartition, Optional<KafkaException>> result = shareConsumer.commitSync();
- assertEquals(1, result.size());
- assertEquals(Optional.empty(), result.get(new TopicIdPartition(tpId, tp.partition(), tp.topic())));
- }
- }
-
- private void waitForAssignment(String groupId, List<TopicPartition> tps) {
- try {
- waitForCondition(() -> {
- try (Admin admin = createAdminClient()) {
- Collection<ShareMemberDescription> members = admin.describeShareGroups(List.of(groupId),
- new DescribeShareGroupsOptions().includeAuthorizedOperations(true)
- ).describedGroups().get(groupId).get().members();
- Set<TopicPartition> assigned = new HashSet<>();
- members.forEach(desc -> {
- if (desc.assignment() != null) {
- assigned.addAll(desc.assignment().topicPartitions());
- }
- });
- return assigned.containsAll(tps);
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- },
- DEFAULT_MAX_WAIT_MS,
- 1000L,
- () -> "tps not assigned to members"
- );
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
-
- public static class SerializerImpl implements Serializer<byte[]> {
-
- @Override
- public byte[] serialize(String topic, Headers headers, byte[] data) {
- headers.add(KEY, VALUE.getBytes());
- return data;
- }
-
- @Override
- public byte[] serialize(String topic, byte[] data) {
- fail("method should not be invoked");
- return null;
- }
- }
-
- public static class DeserializerImpl implements Deserializer<byte[]> {
-
- @Override
- public byte[] deserialize(String topic, Headers headers, byte[] data) {
- Header header = headers.lastHeader(KEY);
- assertEquals("application/octet-stream", header == null ? null : new String(header.value()));
- return data;
- }
-
- @Override
- public byte[] deserialize(String topic, byte[] data) {
- fail("method should not be invoked");
- return null;
- }
- }
-}
+}
\ No newline at end of file
diff --git a/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/ShareConsumerTestBase.java b/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/ShareConsumerTestBase.java
new file mode 100644
index 00000000000..6ddb28e835d
--- /dev/null
+++ b/clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/ShareConsumerTestBase.java
@@ -0,0 +1,773 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.clients.consumer;
+
+import org.apache.kafka.clients.admin.Admin;
+import org.apache.kafka.clients.admin.AlterConfigOp;
+import org.apache.kafka.clients.admin.AlterConfigsOptions;
+import org.apache.kafka.clients.admin.AlterShareGroupOffsetsOptions;
+import org.apache.kafka.clients.admin.Config;
+import org.apache.kafka.clients.admin.ConfigEntry;
+import org.apache.kafka.clients.admin.CreateTopicsResult;
+import org.apache.kafka.clients.admin.DeleteShareGroupOffsetsOptions;
+import org.apache.kafka.clients.admin.DescribeShareGroupsOptions;
+import org.apache.kafka.clients.admin.ListShareGroupOffsetsOptions;
+import org.apache.kafka.clients.admin.ListShareGroupOffsetsResult;
+import org.apache.kafka.clients.admin.ListShareGroupOffsetsSpec;
+import org.apache.kafka.clients.admin.NewTopic;
+import org.apache.kafka.clients.admin.ShareMemberDescription;
+import org.apache.kafka.clients.admin.SharePartitionOffsetInfo;
+import org.apache.kafka.clients.producer.Producer;
+import org.apache.kafka.clients.producer.ProducerConfig;
+import org.apache.kafka.clients.producer.ProducerRecord;
+import org.apache.kafka.clients.producer.RecordMetadata;
+import org.apache.kafka.common.KafkaException;
+import org.apache.kafka.common.TopicIdPartition;
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.Uuid;
+import org.apache.kafka.common.config.ConfigResource;
+import org.apache.kafka.common.header.Header;
+import org.apache.kafka.common.header.Headers;
+import org.apache.kafka.common.internals.Topic;
+import org.apache.kafka.common.network.ListenerName;
+import org.apache.kafka.common.serialization.ByteArrayDeserializer;
+import org.apache.kafka.common.serialization.Deserializer;
+import org.apache.kafka.common.serialization.Serializer;
+import org.apache.kafka.common.test.ClusterInstance;
+import org.apache.kafka.coordinator.group.GroupConfig;
+import org.apache.kafka.coordinator.group.modern.share.ShareGroupConfig;
+import org.apache.kafka.server.util.ServerTestUtils;
+import org.apache.kafka.test.TestUtils;
+
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.Properties;
+import java.util.Set;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.function.BiConsumer;
+import java.util.function.Predicate;
+import java.util.stream.IntStream;
+
+import static org.apache.kafka.test.TestUtils.DEFAULT_MAX_WAIT_MS;
+import static org.apache.kafka.test.TestUtils.DEFAULT_POLL_INTERVAL_MS;
+import static org.apache.kafka.test.TestUtils.waitForCondition;
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.fail;
+
+public abstract class ShareConsumerTestBase {
+ protected final ClusterInstance cluster;
+ protected final TopicPartition tp = new TopicPartition("topic", 0);
+ protected Uuid tpId;
+ protected final TopicPartition tp2 = new TopicPartition("topic2", 0);
+ protected final TopicPartition warmupTp = new TopicPartition("warmup", 0);
+ protected List<TopicPartition> sgsTopicPartitions;
+ protected static final String KEY = "content-type";
+ protected static final String VALUE = "application/octet-stream";
+ protected static final String EXPLICIT = "explicit";
+ protected static final String IMPLICIT = "implicit";
+ protected static final String RECORD_LIMIT = "record_limit";
+ protected static final String BATCH_OPTIMIZED = "batch_optimized";
+
+ public ShareConsumerTestBase(ClusterInstance cluster) {
+ this.cluster = cluster;
+ }
+
+ @BeforeEach
+ public void setup() {
+ try {
+ this.cluster.waitForReadyBrokers();
+ tpId = createTopic("topic");
+ createTopic("topic2");
+ sgsTopicPartitions = IntStream.range(0, 3)
+ .mapToObj(part -> new TopicPartition(Topic.SHARE_GROUP_STATE_TOPIC_NAME, part))
+ .toList();
+ this.warmup();
+ } catch (Exception e) {
+ fail(e);
+ }
+ }
+
+ @AfterEach
+ public void tearDown() {
+ ServerTestUtils.clearYammerMetrics();
+ }
+
+ /**
+ * Test implementation of AcknowledgementCommitCallback to track the completed acknowledgements.
+ * partitionOffsetsMap is used to track the offsets acknowledged for each partition.
+ * partitionExceptionMap is used to track the exception encountered for each partition if any.
+ * Note - Multiple calls to {@link #onComplete(Map, Exception)} will not update the partitionExceptionMap for any existing partitions,
+ * so please ensure to clear the partitionExceptionMap after every call to onComplete() in a single test.
+ */
+ public static class TestableAcknowledgementCommitCallback implements AcknowledgementCommitCallback {
+ private final Map<TopicPartition, Set<Long>> partitionOffsetsMap;
+ private final Map<TopicPartition, Exception> partitionExceptionMap;
+
+ public TestableAcknowledgementCommitCallback(Map<TopicPartition, Set<Long>> partitionOffsetsMap,
+ Map<TopicPartition, Exception> partitionExceptionMap) {
+ this.partitionOffsetsMap = partitionOffsetsMap;
+ this.partitionExceptionMap = partitionExceptionMap;
+ }
+
+ @Override
+ public void onComplete(Map<TopicIdPartition, Set<Long>> offsetsMap, Exception exception) {
+ offsetsMap.forEach((partition, offsets) -> {
+ partitionOffsetsMap.merge(partition.topicPartition(), offsets, (oldOffsets, newOffsets) -> {
+ Set<Long> mergedOffsets = new HashSet<>();
+ mergedOffsets.addAll(oldOffsets);
+ mergedOffsets.addAll(newOffsets);
+ return mergedOffsets;
+ });
+ if (!partitionExceptionMap.containsKey(partition.topicPartition()) && exception != null) {
+ partitionExceptionMap.put(partition.topicPartition(), exception);
+ }
+ });
+ }
+ }
+
+ /**
+ * Util class to encapsulate state for a consumer/producer
+ * being executed by an {@link ExecutorService}.
+ */
+ protected static class ClientState {
+ private final AtomicBoolean done = new AtomicBoolean(false);
+ private final AtomicInteger count = new AtomicInteger(0);
+
+ AtomicBoolean done() {
+ return done;
+ }
+
+ AtomicInteger count() {
+ return count;
+ }
+ }
+
+ protected int produceMessages(int messageCount) {
+ try (Producer<byte[], byte[]> producer = createProducer()) {
+ ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, "key".getBytes(), "value".getBytes());
+ IntStream.range(0, messageCount).forEach(__ -> producer.send(record));
+ producer.flush();
+ }
+ return messageCount;
+ }
+
+ protected void produceMessagesWithTimestamp(int messageCount, long startingTimestamp) {
+ try (Producer<byte[], byte[]> producer = createProducer()) {
+ for (int i = 0; i < messageCount; i++) {
+ ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), startingTimestamp + i,
+ ("key " + i).getBytes(), ("value " + i).getBytes());
+ producer.send(record);
+ }
+ producer.flush();
+ }
+ }
+
+ protected void produceCommittedTransaction(Producer<byte[], byte[]> transactionalProducer, String message) {
+ try {
+ transactionalProducer.beginTransaction();
+ ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, message.getBytes(), message.getBytes());
+ Future<RecordMetadata> future = transactionalProducer.send(record);
+ transactionalProducer.flush();
+ future.get(); // Ensure producer send is complete before committing
+ transactionalProducer.commitTransaction();
+ } catch (Exception e) {
+ transactionalProducer.abortTransaction();
+ }
+ }
+
+ protected void produceAbortedTransaction(Producer<byte[], byte[]> transactionalProducer, String message) {
+ try {
+ transactionalProducer.beginTransaction();
+ ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, message.getBytes(), message.getBytes());
+ Future<RecordMetadata> future = transactionalProducer.send(record);
+ transactionalProducer.flush();
+ future.get(); // Ensure producer send is complete before aborting
+ transactionalProducer.abortTransaction();
+ } catch (Exception e) {
+ transactionalProducer.abortTransaction();
+ }
+ }
+
+ protected void produceCommittedAndAbortedTransactionsInInterval(Producer<byte[], byte[]> transactionalProducer, int messageCount, int intervalAbortedTransactions) {
+ transactionalProducer.initTransactions();
+ int transactionCount = 0;
+ try {
+ for (int i = 0; i < messageCount; i++) {
+ transactionalProducer.beginTransaction();
+ String recordMessage = "Message " + (i + 1);
+ ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(tp.topic(), tp.partition(), null, recordMessage.getBytes(), recordMessage.getBytes());
+ Future<RecordMetadata> future = transactionalProducer.send(record);
+ transactionalProducer.flush();
+ // Increment transaction count
+ transactionCount++;
+ if (transactionCount % intervalAbortedTransactions == 0) {
+ // Aborts every intervalAbortedTransactions transaction
+ transactionalProducer.abortTransaction();
+ } else {
+ // Commits other transactions
+ future.get(); // Ensure producer send is complete before committing
+ transactionalProducer.commitTransaction();
+ }
+ }
+ } catch (Exception e) {
+ transactionalProducer.abortTransaction();
+ } finally {
+ transactionalProducer.close();
+ }
+ }
+
+ protected int consumeMessages(AtomicInteger totalMessagesConsumed,
+ int totalMessages,
+ String groupId,
+ int consumerNumber,
+ int maxPolls,
+ boolean commit) {
+ return assertDoesNotThrow(() -> {
+ try (ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer(groupId)) {
+ shareConsumer.subscribe(Set.of(tp.topic()));
+ return consumeMessages(shareConsumer, totalMessagesConsumed, totalMessages, consumerNumber, maxPolls, commit);
+ }
+ }, "Consumer " + consumerNumber + " failed with exception");
+ }
+
+ protected int consumeMessages(AtomicInteger totalMessagesConsumed,
+ int totalMessages,
+ String groupId,
+ int consumerNumber,
+ int maxPolls,
+ boolean commit,
+ int maxFetchBytes) {
+ return assertDoesNotThrow(() -> {
+ try (ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer(
+ groupId,
+ Map.of(ConsumerConfig.MAX_PARTITION_FETCH_BYTES_CONFIG, maxFetchBytes))) {
+ shareConsumer.subscribe(Set.of(tp.topic()));
+ return consumeMessages(shareConsumer, totalMessagesConsumed, totalMessages, consumerNumber, maxPolls, commit);
+ }
+ }, "Consumer " + consumerNumber + " failed with exception");
+ }
+
+ protected int consumeMessages(ShareConsumer<byte[], byte[]> consumer,
+ AtomicInteger totalMessagesConsumed,
+ int totalMessages,
+ int consumerNumber,
+ int maxPolls,
+ boolean commit) {
+ return assertDoesNotThrow(() -> {
+ int messagesConsumed = 0;
+ int retries = 0;
+ if (totalMessages > 0) {
+ while (totalMessagesConsumed.get() < totalMessages && retries < maxPolls) {
+ ConsumerRecords<byte[], byte[]> records = consumer.poll(Duration.ofMillis(2000));
+ messagesConsumed += records.count();
+ totalMessagesConsumed.addAndGet(records.count());
+ retries++;
+ }
+ } else {
+ while (retries < maxPolls) {
+ ConsumerRecords<byte[], byte[]> records = consumer.poll(Duration.ofMillis(2000));
+ messagesConsumed += records.count();
+ totalMessagesConsumed.addAndGet(records.count());
+ retries++;
+ }
+ }
+
+ if (commit) {
+ // Complete acknowledgement of the records
+ consumer.commitSync(Duration.ofMillis(10000));
+ }
+ return messagesConsumed;
+ }, "Consumer " + consumerNumber + " failed with exception");
+ }
+
+ protected <K, V> List<ConsumerRecord<K, V>> consumeRecords(ShareConsumer<K, V> consumer,
+ int numRecords) {
+ ArrayList<ConsumerRecord<K, V>> accumulatedRecords = new ArrayList<>();
+ long startTimeMs = System.currentTimeMillis();
+ while (accumulatedRecords.size() < numRecords) {
+ ConsumerRecords<K, V> records = consumer.poll(Duration.ofMillis(100));
+ records.forEach(accumulatedRecords::add);
+ long currentTimeMs = System.currentTimeMillis();
+ assertFalse(currentTimeMs - startTimeMs > 60000, "Timed out before consuming expected records.");
+ }
+ return accumulatedRecords;
+ }
+
+ protected Uuid createTopic(String topicName) {
+ return createTopic(topicName, 1, 1);
+ }
+
+ protected Uuid createTopic(String topicName, int numPartitions, int replicationFactor) {
+ AtomicReference<Uuid> topicId = new AtomicReference<>(null);
+ assertDoesNotThrow(() -> {
+ try (Admin admin = createAdminClient()) {
+ CreateTopicsResult result = admin.createTopics(Set.of(new NewTopic(topicName, numPartitions, (short) replicationFactor)));
+ result.all().get();
+ topicId.set(result.topicId(topicName).get());
+ }
+ }, "Failed to create topic");
+
+ return topicId.get();
+ }
+
+ protected void deleteTopic(String topicName) {
+ assertDoesNotThrow(() -> {
+ try (Admin admin = createAdminClient()) {
+ admin.deleteTopics(Set.of(topicName)).all().get();
+ }
+ }, "Failed to delete topic");
+ }
+
+ protected Admin createAdminClient() {
+ return cluster.admin();
+ }
+
+ protected <K, V> Producer<K, V> createProducer() {
+ return createProducer(Map.of());
+ }
+
+ protected <K, V> Producer<K, V> createProducer(Map<String, Object> config) {
+ return cluster.producer(config);
+ }
+
+ protected <K, V> Producer<K, V> createProducer(String transactionalId) {
+ return createProducer(
+ Map.of(
+ ProducerConfig.TRANSACTIONAL_ID_CONFIG, transactionalId
+ )
+ );
+ }
+
+ protected <K, V> ShareConsumer<K, V> createShareConsumer(String groupId) {
+ return createShareConsumer(groupId, Map.of());
+ }
+
+ protected <K, V> ShareConsumer<K, V> createShareConsumer(
+ String groupId,
+ Map<?, ?> additionalProperties
+ ) {
+ return createShareConsumer(groupId, additionalProperties, null, null);
+ }
+
+ protected <K, V> ShareConsumer<K, V> createShareConsumer(
+ String groupId,
+ Map<?, ?> additionalProperties,
+ Deserializer<K> keyDeserializer,
+ Deserializer<V> valueDeserializer
+ ) {
+ Properties props = new Properties();
+ props.putAll(additionalProperties);
+ props.put(ConsumerConfig.GROUP_ID_CONFIG, groupId);
+ Map<String, Object> conf = new HashMap<>();
+ props.forEach((k, v) -> conf.put((String) k, v));
+ return cluster.shareConsumer(conf, keyDeserializer, valueDeserializer);
+ }
+
+ protected void warmup() throws InterruptedException {
+ createTopic(warmupTp.topic());
+ waitForMetadataCache();
+ ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(warmupTp.topic(), warmupTp.partition(), null, "key".getBytes(), "value".getBytes());
+ Set<String> subscription = Set.of(warmupTp.topic());
+ alterShareAutoOffsetReset("warmupgroup1", "earliest");
+ try (Producer<byte[], byte[]> producer = createProducer();
+ ShareConsumer<byte[], byte[]> shareConsumer = createShareConsumer("warmupgroup1")) {
+
+ producer.send(record);
+ producer.flush();
+
+ shareConsumer.subscribe(subscription);
+ TestUtils.waitForCondition(
+ () -> shareConsumer.poll(Duration.ofMillis(5000)).count() == 1, 30000, 200L, () -> "warmup record not received");
+ }
+ }
+
+ protected void waitForMetadataCache() throws InterruptedException {
+ TestUtils.waitForCondition(() ->
+ !cluster.brokers().get(0).metadataCache().getAliveBrokerNodes(new ListenerName("EXTERNAL")).isEmpty(),
+ DEFAULT_MAX_WAIT_MS, 100L, () -> "cache not up yet");
+ }
+
+ protected void verifyShareGroupStateTopicRecordsProduced() {
+ try {
+ try (Consumer<byte[], byte[]> consumer = cluster.consumer()) {
+ consumer.assign(sgsTopicPartitions);
+ consumer.seekToBeginning(sgsTopicPartitions);
+ Set<ConsumerRecord<byte[], byte[]>> records = new HashSet<>();
+ TestUtils.waitForCondition(() -> {
+ ConsumerRecords<byte[], byte[]> msgs = consumer.poll(Duration.ofMillis(5000L));
+ if (msgs.count() > 0) {
+ msgs.records(Topic.SHARE_GROUP_STATE_TOPIC_NAME).forEach(records::add);
+ }
+ return records.size() > 2; // +2 because of extra warmup records
+ },
+ 30000L,
+ 200L,
+ () -> "no records produced"
+ );
+ }
+ } catch (InterruptedException e) {
+ fail(e);
+ }
+ }
+
+ protected void alterShareGroupConfig(String groupId, String configKey, String newValue) {
+ ConfigResource configResource = new ConfigResource(ConfigResource.Type.GROUP, groupId);
+ Map<ConfigResource, Collection<AlterConfigOp>> alterEntries = new HashMap<>();
+ alterEntries.put(configResource, List.of(new AlterConfigOp(new ConfigEntry(
+ configKey, newValue), AlterConfigOp.OpType.SET)));
+ AlterConfigsOptions alterOptions = new AlterConfigsOptions();
+ try (Admin adminClient = createAdminClient()) {
+ assertDoesNotThrow(() -> adminClient.incrementalAlterConfigs(alterEntries, alterOptions)
+ .all()
+ .get(60, TimeUnit.SECONDS), "Failed to alter configs");
+ }
+
+ // This config is changed dynamically in tests, and we need it to have propagated before the test proceeds.
+ // Describing the config with a new admin client is not totally foolproof, but it's better than just
+ // altering the config and continuing.
+ try (Admin adminClient = createAdminClient()) {
+ assertDoesNotThrow(() ->
+ TestUtils.waitForCondition(() -> {
+ Config config = adminClient.describeConfigs(List.of(configResource)).all().get().get(configResource);
+ ConfigEntry entry = config.get(configKey);
+ return entry != null && entry.value().equals(newValue);
+ }, 10000L, 100L, () -> "New config value did not propagate"), "Failed to describe configs");
+ }
+ }
+
+ protected void alterShareAutoOffsetReset(String groupId, String newValue) {
+ alterShareGroupConfig(groupId, GroupConfig.SHARE_AUTO_OFFSET_RESET_CONFIG, newValue);
+ }
+
+ protected void alterShareDeliveryCountLimit(String groupId, String newValue) {
+ alterShareGroupConfig(groupId, GroupConfig.SHARE_DELIVERY_COUNT_LIMIT_CONFIG, newValue);
+ }
+
+ protected void alterShareIsolationLevel(String groupId, String newValue) {
+ alterShareGroupConfig(groupId, GroupConfig.SHARE_ISOLATION_LEVEL_CONFIG, newValue);
+ }
+
+ protected void alterShareRenewAcknowledgeEnable(String groupId, boolean newValue) {
+ alterShareGroupConfig(groupId, GroupConfig.SHARE_RENEW_ACKNOWLEDGE_ENABLE_CONFIG, Boolean.toString(newValue));
+ }
+
+ protected List<Integer> topicPartitionLeader(Admin adminClient, String topicName, int partition) throws InterruptedException, ExecutionException {
+ return adminClient.describeTopics(List.of(topicName)).allTopicNames().get().get(topicName)
+ .partitions().stream()
+ .filter(info -> info.partition() == partition)
+ .map(info -> info.leader().id())
+ .filter(info -> info != -1)
+ .toList();
+ }
+
+ protected SharePartitionOffsetInfo sharePartitionOffsetInfo(Admin adminClient, String groupId, TopicPartition tp) throws InterruptedException, ExecutionException {
+ SharePartitionOffsetInfo partitionResult;
+ ListShareGroupOffsetsResult result = adminClient.listShareGroupOffsets(
+ Map.of(groupId, new ListShareGroupOffsetsSpec().topicPartitions(List.of(tp))),
+ new ListShareGroupOffsetsOptions().timeoutMs(30000)
+ );
+ partitionResult = result.partitionsToOffsetInfo(groupId).get().get(tp);
+ return partitionResult;
+ }
+
+ protected void verifySharePartitionStartOffset(Admin adminClient, String groupId, TopicPartition tp, long expectedStartOffset) throws InterruptedException {
+ TestUtils.waitForCondition(() -> {
+ SharePartitionOffsetInfo sharePartitionOffsetInfo = sharePartitionOffsetInfo(adminClient, groupId, tp);
+ return sharePartitionOffsetInfo != null &&
+ sharePartitionOffsetInfo.startOffset() == expectedStartOffset;
+ }, DEFAULT_MAX_WAIT_MS, DEFAULT_POLL_INTERVAL_MS, () -> "Failed to retrieve share partition lag");
+ }
+
+ protected void verifySharePartitionLag(Admin adminClient, String groupId, TopicPartition tp, long expectedLag) throws InterruptedException {
+ TestUtils.waitForCondition(() -> {
+ SharePartitionOffsetInfo sharePartitionOffsetInfo = sharePartitionOffsetInfo(adminClient, groupId, tp);
+ return sharePartitionOffsetInfo != null &&
+ sharePartitionOffsetInfo.lag().isPresent() &&
+ sharePartitionOffsetInfo.lag().get() == expectedLag;
+ }, DEFAULT_MAX_WAIT_MS, DEFAULT_POLL_INTERVAL_MS, () -> "Failed to retrieve share partition lag");
+ }
+
+ protected void verifySharePartitionOffsetsDeleted(Admin adminClient, String groupId, TopicPartition tp) throws InterruptedException {
+ TestUtils.waitForCondition(
+ () -> sharePartitionOffsetInfo(adminClient, groupId, tp) == null,
+ DEFAULT_MAX_WAIT_MS,
+ DEFAULT_POLL_INTERVAL_MS,
+ () -> "Failed to retrieve share partition lag");
+ }
+
+ protected void alterShareGroupOffsets(Admin adminClient, String groupId, TopicPartition topicPartition, Long newOffset) throws InterruptedException, ExecutionException {
+ adminClient.alterShareGroupOffsets(
+ groupId,
+ Map.of(topicPartition, newOffset),
+ new AlterShareGroupOffsetsOptions().timeoutMs(30000)).partitionResult(topicPartition).get();
+ }
+
+ protected void deleteShareGroupOffsets(Admin adminClient, String groupId, String topic) throws InterruptedException, ExecutionException {
+ adminClient.deleteShareGroupOffsets(
+ groupId,
+ Set.of(topic),
+ new DeleteShareGroupOffsetsOptions().timeoutMs(30000)).topicResult(topic).get();
+ }
+
+ protected void alterSharePartitionMaxRecordLocks(String groupId, String newValue) {
+ alterShareGroupConfig(groupId, GroupConfig.SHARE_PARTITION_MAX_RECORD_LOCKS_CONFIG, newValue);
+ }
+
+ protected void alterShareRecordLockDurationMs(String groupId, int newValue) {
+ alterShareGroupConfig(groupId, GroupConfig.SHARE_RECORD_LOCK_DURATION_MS_CONFIG, Integer.toString(newValue));
+ }
+
+ /**
+ * Test utility which encapsulates a {@link ShareConsumer} whose record processing
+ * behavior can be supplied as a function argument.
+ * <p></p>
+ * This can be used to create different consume patterns on the broker and study
+ * the status of broker side share group abstractions.
+ *
+ * @param <K> - key type of the records consumed
+ * @param <V> - value type of the records consumed
+ */
+ protected static class ComplexShareConsumer<K, V> implements Runnable {
+ public static final int POLL_TIMEOUT_MS = 15000;
+ public static final int MAX_DELIVERY_COUNT = ShareGroupConfig.SHARE_GROUP_DELIVERY_COUNT_LIMIT_DEFAULT;
+
+ private final String topicName;
+ private final Map<String, Object> configs = new HashMap<>();
+ private final ClientState state = new ClientState();
+ private final Predicate<ConsumerRecords<K, V>> exitCriteria;
+ private final BiConsumer<ShareConsumer<K, V>, ConsumerRecord<K, V>> processFunc;
+
+ ComplexShareConsumer(
+ String bootstrapServers,
+ String topicName,
+ String groupId,
+ Map<String, Object> additionalProperties
+ ) {
+ this(
+ bootstrapServers,
+ topicName,
+ groupId,
+ additionalProperties,
+ records -> records.count() == 0,
+ (consumer, record) -> {
+ short deliveryCountBeforeAccept = (short) ((record.offset() + record.offset() / (MAX_DELIVERY_COUNT + 2)) % (MAX_DELIVERY_COUNT + 2));
+ if (deliveryCountBeforeAccept == 0) {
+ consumer.acknowledge(record, AcknowledgeType.REJECT);
+ } else if (record.deliveryCount().get() == deliveryCountBeforeAccept) {
+ consumer.acknowledge(record, AcknowledgeType.ACCEPT);
+ } else {
+ consumer.acknowledge(record, AcknowledgeType.RELEASE);
+ }
+ }
+ );
+ }
+
+ ComplexShareConsumer(
+ String bootstrapServers,
+ String topicName,
+ String groupId,
+ Map<String, Object> additionalProperties,
+ Predicate<ConsumerRecords<K, V>> exitCriteria,
+ BiConsumer<ShareConsumer<K, V>, ConsumerRecord<K, V>> processFunc
+ ) {
+ this.exitCriteria = Objects.requireNonNull(exitCriteria);
+ this.processFunc = Objects.requireNonNull(processFunc);
+ this.topicName = topicName;
+ this.configs.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
+ this.configs.put(ConsumerConfig.GROUP_ID_CONFIG, groupId);
+ this.configs.putAll(additionalProperties);
+ this.configs.putIfAbsent(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, ByteArrayDeserializer.class);
+ this.configs.putIfAbsent(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, ByteArrayDeserializer.class);
+ }
+
+ @Override
+ public void run() {
+ try (ShareConsumer<K, V> consumer = new KafkaShareConsumer<>(configs)) {
+ consumer.subscribe(Set.of(this.topicName));
+ while (!state.done().get()) {
+ ConsumerRecords<K, V> records = consumer.poll(Duration.ofMillis(POLL_TIMEOUT_MS));
+ state.count().addAndGet(records.count());
+ if (exitCriteria.test(records)) {
+ state.done().set(true);
+ }
+ records.forEach(record -> processFunc.accept(consumer, record));
+ }
+ }
+ }
+
+ boolean isDone() {
+ return state.done().get();
+ }
+
+ int recordsRead() {
+ return state.count().get();
+ }
+ }
+
+ protected void shutdownExecutorService(ExecutorService service) {
+ service.shutdown();
+ try {
+ if (!service.awaitTermination(5L, TimeUnit.SECONDS)) {
+ service.shutdownNow();
+ }
+ } catch (Exception e) {
+ service.shutdownNow();
+ Thread.currentThread().interrupt();
+ }
+ }
+
+ protected ConsumerRecords<byte[], byte[]> waitedPoll(
+ ShareConsumer<byte[], byte[]> shareConsumer,
+ long pollMs,
+ int recordCount
+ ) {
+ return waitedPoll(shareConsumer, pollMs, recordCount, false, "", List.of());
+ }
+
+ protected ConsumerRecords<byte[], byte[]> waitedPoll(
+ ShareConsumer<byte[], byte[]> shareConsumer,
+ long pollMs,
+ int recordCount,
+ boolean checkAssignment,
+ String groupId,
+ List<TopicPartition> tps
+ ) {
+ AtomicReference<ConsumerRecords<byte[], byte[]>> recordsAtomic = new AtomicReference<>();
+ try {
+ waitForCondition(() -> {
+ ConsumerRecords<byte[], byte[]> recs = shareConsumer.poll(Duration.ofMillis(pollMs));
+ recordsAtomic.set(recs);
+ if (checkAssignment) {
+ waitForAssignment(groupId, tps);
+ }
+ return recs.count() == recordCount;
+ },
+ DEFAULT_MAX_WAIT_MS,
+ 500L,
+ () -> "failed to get records"
+ );
+ return recordsAtomic.get();
+ } catch (InterruptedException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ protected void validateExpectedRecordsInEachPollAndRelease(
+ ShareConsumer<byte[], byte[]> shareConsumer,
+ int startOffset,
+ int lastOffset,
+ int expectedRecordsInEachPoll
+ ) {
+ validateExpectedRecordsInEachPollAndAcknowledge(shareConsumer, startOffset, lastOffset, expectedRecordsInEachPoll, AcknowledgeType.RELEASE);
+ }
+
+ protected void validateExpectedRecordsInEachPollAndAcknowledge(
+ ShareConsumer<byte[], byte[]> shareConsumer,
+ int startOffset,
+ int lastOffset,
+ int expectedRecordsInEachPoll,
+ AcknowledgeType acknowledgeType
+ ) {
+ for (int i = startOffset; i < lastOffset; i = i + expectedRecordsInEachPoll) {
+ ConsumerRecords<byte[], byte[]> records = waitedPoll(shareConsumer, 2500L, expectedRecordsInEachPoll);
+ assertEquals(expectedRecordsInEachPoll, records.count());
+ // Verify the first offset of the fetched records.
+ assertEquals(i, records.iterator().next().offset());
+
+ records.forEach(record -> shareConsumer.acknowledge(record, acknowledgeType));
+ Map<TopicIdPartition, Optional<KafkaException>> result = shareConsumer.commitSync();
+ assertEquals(1, result.size());
+ assertEquals(Optional.empty(), result.get(new TopicIdPartition(tpId, tp.partition(), tp.topic())));
+ }
+ }
+
+ protected void waitForAssignment(String groupId, List<TopicPartition> tps) {
+ try {
+ waitForCondition(() -> {
+ try (Admin admin = createAdminClient()) {
+ Collection<ShareMemberDescription> members = admin.describeShareGroups(List.of(groupId),
+ new DescribeShareGroupsOptions().includeAuthorizedOperations(true)
+ ).describedGroups().get(groupId).get().members();
+ Set<TopicPartition> assigned = new HashSet<>();
+ members.forEach(desc -> {
+ if (desc.assignment() != null) {
+ assigned.addAll(desc.assignment().topicPartitions());
+ }
+ });
+ return assigned.containsAll(tps);
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ },
+ DEFAULT_MAX_WAIT_MS,
+ 1000L,
+ () -> "tps not assigned to members"
+ );
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public static class SerializerImpl implements Serializer<byte[]> {
+
+ @Override
+ public byte[] serialize(String topic, Headers headers, byte[] data) {
+ headers.add(KEY, VALUE.getBytes());
+ return data;
+ }
+
+ @Override
+ public byte[] serialize(String topic, byte[] data) {
+ fail("method should not be invoked");
+ return null;
+ }
+ }
+
+ public static class DeserializerImpl implements Deserializer<byte[]> {
+
+ @Override
+ public byte[] deserialize(String topic, Headers headers, byte[] data) {
+ Header header = headers.lastHeader(KEY);
+ assertEquals("application/octet-stream", header == null ? null : new String(header.value()));
+ return data;
+ }
+
+ @Override
+ public byte[] deserialize(String topic, byte[] data) {
+ fail("method should not be invoked");
+ return null;
+ }
+ }
+}
\ No newline at end of file