You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by "amogh-jahagirdar (via GitHub)" <gi...@apache.org> on 2026/09/17 02:37:29 UTC

Re: [PR] Kafka Connect: Fix coordinator potentially committing files from prior commit in certain rebalance scenarios [iceberg]

amogh-jahagirdar commented on code in PR #17713:
URL: https://github.com/apache/iceberg/pull/17713#discussion_r4032523362


##########
kafka-connect/kafka-connect/src/main/java/org/apache/iceberg/connect/channel/Channel.java:
##########
@@ -119,21 +120,32 @@ protected void send(List<Event> events, Map<TopicPartition, Offset> sourceOffset
   protected void consumeAvailable(Duration pollDuration) {
     ConsumerRecords<String, byte[]> records = consumer.poll(pollDuration);
     while (!records.isEmpty()) {
-      records.forEach(
-          record -> {
-            // the consumer stores the offsets that corresponds to the next record to consume,
-            // so increment the record offset by one
-            controlTopicOffsets.put(record.partition(), record.offset() + 1);
-
-            Event event = AvroUtil.decode(record.value());
-
-            if (event.groupId().equals(connectGroupId)) {
-              LOG.debug("Received event of type: {}", event.type().name());
-              if (receive(new Envelope(event, record.partition(), record.offset()))) {
-                LOG.info("Handled event of type: {}", event.type().name());
-              }
-            }
-          });
+      for (ConsumerRecord<String, byte[]> record : records) {
+        Long nextOffset = controlTopicOffsets.get(record.partition());
+        // A rebalance may revoke and later reassign this control partition to the same

Review Comment:
   Nit:
   ```
   // A rebalance can rewind the consumer to the committed offset, which can lag the in-memory
   //position. Skip already-processed records.
   ```



##########
kafka-connect/kafka-connect/src/test/java/org/apache/iceberg/connect/channel/TestControlTopicReplay.java:
##########
@@ -0,0 +1,211 @@
+/*
+ * 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.iceberg.connect.channel;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.io.IOException;
+import java.time.Duration;
+import java.util.List;
+import java.util.Optional;
+import java.util.UUID;
+import org.apache.iceberg.DataFile;
+import org.apache.iceberg.DataFiles;
+import org.apache.iceberg.FileFormat;
+import org.apache.iceberg.FileScanTask;
+import org.apache.iceberg.PartitionSpec;
+import org.apache.iceberg.Snapshot;
+import org.apache.iceberg.connect.IcebergSinkConfig;
+import org.apache.iceberg.connect.events.AvroUtil;
+import org.apache.iceberg.connect.events.DataComplete;
+import org.apache.iceberg.connect.events.DataWritten;
+import org.apache.iceberg.connect.events.Event;
+import org.apache.iceberg.connect.events.StartCommit;
+import org.apache.iceberg.connect.events.TableReference;
+import org.apache.iceberg.connect.events.TopicPartitionOffset;
+import org.apache.iceberg.io.CloseableIterable;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableSet;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.types.Types.StructType;
+import org.apache.kafka.clients.admin.MemberAssignment;
+import org.apache.kafka.clients.admin.MemberDescription;
+import org.apache.kafka.clients.consumer.ConsumerRecord;
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.connect.sink.SinkTaskContext;
+import org.junit.jupiter.api.Test;
+
+class TestControlTopicReplay extends ChannelTestBase {
+
+  @Test
+  void handlesReplayedControlRecordsOnce() {
+    TrackingChannel channel =
+        new TrackingChannel(config, clientFactory, mock(SinkTaskContext.class));
+    channel.start();
+    initConsumer();
+
+    addStartCommit(0L);
+    addStartCommit(1L);
+    channel.process();
+
+    assertThat(channel.receivedCount()).isEqualTo(2);
+    assertThat(channel.nextOffset(0)).isEqualTo(2L);
+
+    consumer.seek(new TopicPartition(CTL_TOPIC_NAME, 0), 0L);
+    addStartCommit(0L);
+    addStartCommit(1L);
+    channel.process();
+
+    assertThat(channel.receivedCount()).isEqualTo(2);
+    assertThat(channel.nextOffset(0)).isEqualTo(2L);
+  }
+
+  @Test
+  void doesNotCommitReplayedDataFilesTwice() throws IOException {
+    when(config.commitIntervalMs()).thenReturn(0);
+    when(config.commitTimeoutMs()).thenReturn(Integer.MAX_VALUE);
+
+    MemberAssignment assignment =
+        new MemberAssignment(
+            ImmutableSet.of(
+                new TopicPartition(SRC_TOPIC_NAME, 0),
+                new TopicPartition(SRC_TOPIC_NAME, 1),
+                new TopicPartition(SRC_TOPIC_NAME, 2)));
+    MemberDescription member =
+        new MemberDescription(null, Optional.empty(), null, null, assignment);
+    Coordinator coordinator =
+        new Coordinator(
+            catalog, config, ImmutableList.of(member), clientFactory, mock(SinkTaskContext.class));
+    coordinator.start();
+    initConsumer();
+    coordinator.process();
+
+    UUID commitId =
+        ((StartCommit) AvroUtil.decode(producer.history().get(0).value()).payload()).commitId();
+    DataFile file1 = dataFile("path/to/file-1.parquet");
+    DataFile file2 = dataFile("path/to/file-2.parquet");
+
+    addDataWritten(0L, commitId, file1);
+    addDataComplete(1L, commitId, 0);
+    addDataWritten(2L, commitId, file2);
+    addDataComplete(3L, commitId, 1);
+    coordinator.process();
+

Review Comment:
   minor: maybe a comment like
   
   ```
   // Simulate a rebalance by rewinding the consumer and replaying records before the commit
   // is ready, since a snapshot would record these offsets and filter them.
   ```



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org