Shekharrajak commented on code in PR #271:
URL: 
https://github.com/apache/flink-connector-kafka/pull/271#discussion_r3448006303


##########
flink-connector-kafka/src/test/java/org/apache/flink/connector/kafka/share/KafkaShareAckTransactionITCase.java:
##########
@@ -0,0 +1,723 @@
+/*
+ * 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.flink.connector.kafka.share;
+
+import org.apache.flink.api.common.functions.RichMapFunction;
+import org.apache.flink.configuration.Configuration;
+import 
org.apache.flink.connector.kafka.sink.internal.FlinkKafkaInternalProducer;
+import 
org.apache.flink.connector.kafka.source.reader.transaction.KafkaShareAckTransactionManager;
+import 
org.apache.flink.connector.kafka.source.reader.transaction.ShareAckTransactionClient;
+import 
org.apache.flink.connector.kafka.source.reader.transaction.ShareAckTransactionHandle;
+import org.apache.flink.connector.kafka.testutils.TestKafkaContainer;
+import org.apache.flink.runtime.testutils.MiniClusterResourceConfiguration;
+import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
+import 
org.apache.flink.streaming.api.functions.source.legacy.RichParallelSourceFunction;
+import org.apache.flink.streaming.api.functions.source.legacy.SourceFunction;
+import org.apache.flink.test.util.MiniClusterWithClientResource;
+import org.apache.flink.util.CloseableIterator;
+import org.apache.flink.core.testutils.CommonTestUtils;
+
+import org.apache.kafka.clients.admin.AdminClient;
+import org.apache.kafka.clients.admin.AlterConfigOp;
+import org.apache.kafka.clients.admin.ConfigEntry;
+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.SharePartitionOffsetInfo;
+import org.apache.kafka.clients.consumer.AcknowledgeType;
+import org.apache.kafka.clients.consumer.ConsumerConfig;
+import org.apache.kafka.clients.consumer.ConsumerRecord;
+import org.apache.kafka.clients.consumer.ConsumerRecords;
+import org.apache.kafka.clients.consumer.KafkaShareConsumer;
+import org.apache.kafka.clients.producer.KafkaProducer;
+import org.apache.kafka.clients.producer.ProducerConfig;
+import org.apache.kafka.clients.producer.ProducerRecord;
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.config.ConfigResource;
+import org.apache.kafka.common.config.ConfigResource.Type;
+import org.apache.kafka.common.serialization.ByteArrayDeserializer;
+import org.apache.kafka.common.serialization.ByteArraySerializer;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Assumptions;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.Timeout;
+import org.junit.jupiter.api.parallel.ResourceLock;
+import org.testcontainers.utility.DockerImageName;
+
+import java.io.IOException;
+import java.io.Serializable;
+import java.lang.reflect.Method;
+import java.nio.charset.StandardCharsets;
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
+import java.util.UUID;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+@Timeout(180)
+@ResourceLock("KafkaTestBase")
+class KafkaShareAckTransactionITCase {
+
+    private static final String BOOTSTRAP_SERVERS_PROPERTY =
+            "flink.kafka.share.it.bootstrap.servers";
+    private static final String KAFKA_IMAGE_PROPERTY = 
"flink.kafka.share.it.image";
+    private static final String SHARE_ACK_MODE_CONFIG = 
"share.acknowledgement.mode";
+    private static final String SHARE_AUTO_OFFSET_RESET_CONFIG = 
"share.auto.offset.reset";
+    private static final Duration POLL_TIMEOUT = Duration.ofMillis(500);
+    private static final Duration WAIT_TIMEOUT = Duration.ofSeconds(30);
+    private static final int PARALLELISM = 4;
+
+    private TestKafkaContainer kafkaContainer;
+
+    @AfterEach
+    void tearDown() {
+        if (kafkaContainer != null) {
+            kafkaContainer.stop();
+            kafkaContainer = null;
+        }
+    }
+
+    @Test
+    void testShareAckCommitOnCheckpoint() throws Exception {
+        ShareTestContext context = createContext();
+        produce(context.bootstrapServers, context.topic, "first");
+
+        ReflectiveShareAckTransactionClient client =
+                new ReflectiveShareAckTransactionClient(
+                        context.bootstrapServers, context.groupId, 
context.topic);
+        try (KafkaShareAckTransactionManager manager =
+                        new KafkaShareAckTransactionManager(client, 
context.groupId, 0, List.of());
+                AdminClient admin = createAdmin(context.bootstrapServers)) {
+            ConsumerRecord<byte[], byte[]> record = client.pollOne();
+            assertThat(value(record)).isEqualTo("first");
+
+            client.acknowledgeAccept(record);
+            manager.stageAcknowledgements();
+            assertThat(client.pollCount()).isZero();
+
+            List<ShareAckCommittable> committables = 
manager.snapshotState(42L);
+            assertThat(committables).hasSize(1);
+
+            client.commit(committables.get(0));
+            manager.markCommittedUpTo(42L);
+
+            waitForShareLag(admin, context.groupId, context.topicPartition, 
0L);
+            assertThat(client.pollCount()).isZero();
+            assertThat(manager.snapshotState(43L)).isEmpty();
+        }
+    }
+
+    @Test
+    void testShareAckAbortAfterFailedCheckpointRedelivers() throws Exception {
+        ShareTestContext context = createContext();
+        produce(context.bootstrapServers, context.topic, "redeliver");
+
+        ReflectiveShareAckTransactionClient client =
+                new ReflectiveShareAckTransactionClient(
+                        context.bootstrapServers, context.groupId, 
context.topic);
+        try (KafkaShareAckTransactionManager manager =
+                        new KafkaShareAckTransactionManager(client, 
context.groupId, 1, List.of());
+                AdminClient admin = createAdmin(context.bootstrapServers)) {
+            ConsumerRecord<byte[], byte[]> record = client.pollOne();
+            assertThat(value(record)).isEqualTo("redeliver");
+
+            client.acknowledgeAccept(record);
+            manager.stageAcknowledgements();
+
+            ShareAckCommittable committable = 
manager.snapshotState(42L).get(0);
+            client.abort(committable);
+
+            waitForShareLag(admin, context.groupId, context.topicPartition, 
1L);
+            ConsumerRecord<byte[], byte[]> redelivered = client.pollOne();
+            assertThat(redelivered.offset()).isEqualTo(record.offset());
+            assertThat(value(redelivered)).isEqualTo("redeliver");
+        }
+    }
+
+    @Test
+    void testMultiplePollAcksCommitInOneCheckpointTransaction() throws 
Exception {
+        ShareTestContext context = createContext();
+        produce(context.bootstrapServers, context.topic, "first");
+
+        ReflectiveShareAckTransactionClient client =
+                new ReflectiveShareAckTransactionClient(
+                        context.bootstrapServers, context.groupId, 
context.topic);
+        try (KafkaShareAckTransactionManager manager =
+                        new KafkaShareAckTransactionManager(client, 
context.groupId, 2, List.of());
+                AdminClient admin = createAdmin(context.bootstrapServers)) {
+            ConsumerRecord<byte[], byte[]> first = client.pollOne();
+            assertThat(value(first)).isEqualTo("first");
+            client.acknowledgeAccept(first);
+            manager.stageAcknowledgements();
+
+            produce(context.bootstrapServers, context.topic, "second");

Review Comment:
   
https://github.com/apache/kafka/pull/22357/changes#diff-e0d38249bff5ecbefc48e59ea476349099a02e0ee880b24ac4d9b628c27d03acR242



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to