This is an automated email from the ASF dual-hosted git repository.

JingsongLi pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/paimon.git


The following commit(s) were added to refs/heads/master by this push:
     new b89905ea16 [flink] Reset consumer progress when source reader 
assignments are returned (#11345)
b89905ea16 is described below

commit b89905ea160ea57dac5d6b6ce866fc6b66572483
Author: YueZhang <[email protected]>
AuthorDate: Sun Nov 22 05:51:23 2026 -0500

    [flink] Reset consumer progress when source reader assignments are returned 
(#11345)
---
 .../flink/source/ConsumerProgressCalculator.java   |  44 ++++++-
 .../source/ContinuousFileSplitEnumerator.java      |   5 +-
 .../flink/source/ConsumerProgressFailoverTest.java | 145 +++++++++++++++++++++
 .../source/ContinuousFileSplitEnumeratorTest.java  |  50 +++++++
 4 files changed, 239 insertions(+), 5 deletions(-)

diff --git 
a/paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/source/ConsumerProgressCalculator.java
 
b/paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/source/ConsumerProgressCalculator.java
index f88428a2e8..dc95a3530f 100644
--- 
a/paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/source/ConsumerProgressCalculator.java
+++ 
b/paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/source/ConsumerProgressCalculator.java
@@ -20,7 +20,9 @@ package org.apache.paimon.flink.source;
 
 import org.apache.paimon.flink.utils.TableScanUtils;
 
+import java.util.Collection;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.NavigableMap;
 import java.util.Optional;
@@ -35,32 +37,66 @@ public class ConsumerProgressCalculator {
 
     private final Map<Integer, Long> assignedSnapshotPerReader;
 
+    private final Map<Integer, Long> lastAssignedSnapshotPerReader;
+
     private final Map<Integer, Long> consumingSnapshotPerReader;
 
     public ConsumerProgressCalculator(int parallelism) {
         this.minNextSnapshotPerCheckpoint = new TreeMap<>();
         this.assignedSnapshotPerReader = new HashMap<>(parallelism);
+        this.lastAssignedSnapshotPerReader = new HashMap<>(parallelism);
         this.consumingSnapshotPerReader = new HashMap<>(parallelism);
     }
 
+    public void resetReaderProgress(int subtaskId) {
+        assignedSnapshotPerReader.remove(subtaskId);
+        lastAssignedSnapshotPerReader.remove(subtaskId);
+        consumingSnapshotPerReader.remove(subtaskId);
+        // These minima were computed before the reader reset and cannot 
describe its restored
+        // split state. Wait for fresh progress before advancing the consumer 
again.
+        minNextSnapshotPerCheckpoint.clear();
+    }
+
     public void updateConsumeProgress(int subtaskId, 
ReaderConsumeProgressEvent event) {
-        consumingSnapshotPerReader.put(subtaskId, 
event.lastConsumeSnapshotId());
+        Long lastAssigned = lastAssignedSnapshotPerReader.get(subtaskId);
+        // Restored splits are unordered, and the event for a finished split 
trails the request for
+        // the next one. Only progress within the current assignment proves 
older splits are done.
+        if (lastAssigned != null && event.lastConsumeSnapshotId() <= 
lastAssigned) {
+            consumingSnapshotPerReader.put(subtaskId, 
event.lastConsumeSnapshotId());
+        }
     }
 
-    public void updateAssignInformation(int subtaskId, FileStoreSourceSplit 
split) {
-        TableScanUtils.getSnapshotId(split)
+    public void updateAssignInformation(int subtaskId, 
List<FileStoreSourceSplit> splits) {
+        TableScanUtils.getSnapshotId(splits.get(0))
                 .ifPresent(snapshotId -> 
assignedSnapshotPerReader.put(subtaskId, snapshotId));
+        TableScanUtils.getSnapshotId(splits.get(splits.size() - 1))
+                .ifPresent(snapshotId -> 
lastAssignedSnapshotPerReader.put(subtaskId, snapshotId));
+        // returned splits can be older than what this reader has consumed
+        consumingSnapshotPerReader.remove(subtaskId);
     }
 
     public void notifySnapshotState(
             long checkpointId,
             Set<Integer> readersAwaitingSplit,
             Function<Integer, Long> unassignedCalculationFunction,
+            Collection<FileStoreSourceSplit> unassignedSplits,
             int parallelism) {
         computeMinNextSnapshotId(readersAwaitingSplit, 
unassignedCalculationFunction, parallelism)
                 .ifPresent(
                         minNextSnapshotId ->
-                                minNextSnapshotPerCheckpoint.put(checkpointId, 
minNextSnapshotId));
+                                minNextSnapshotPerCheckpoint.put(
+                                        checkpointId,
+                                        minWithUnassigned(minNextSnapshotId, 
unassignedSplits)));
+    }
+
+    /** Returned splits may be queued behind newer ones, so looking at the 
head is not enough. */
+    private static long minWithUnassigned(
+            long minSnapshotId, Collection<FileStoreSourceSplit> 
unassignedSplits) {
+        long min = minSnapshotId;
+        for (FileStoreSourceSplit split : unassignedSplits) {
+            min = Math.min(min, 
TableScanUtils.getSnapshotId(split).orElse(min));
+        }
+        return min;
     }
 
     public OptionalLong notifyCheckpointComplete(long checkpointId) {
diff --git 
a/paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/source/ContinuousFileSplitEnumerator.java
 
b/paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/source/ContinuousFileSplitEnumerator.java
index 2613d786f9..04c79a2f8a 100644
--- 
a/paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/source/ContinuousFileSplitEnumerator.java
+++ 
b/paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/source/ContinuousFileSplitEnumerator.java
@@ -204,7 +204,9 @@ public class ContinuousFileSplitEnumerator
     @Override
     public void addSplitsBack(List<FileStoreSourceSplit> splits, int 
subtaskId) {
         LOG.debug("File Source Enumerator adds splits back: {}", splits);
+        readersAwaitingSplit.remove(subtaskId);
         splitAssigner.addSplitsBack(subtaskId, splits);
+        consumerProgressCalculator.resetReaderProgress(subtaskId);
     }
 
     @Override
@@ -217,6 +219,7 @@ public class ContinuousFileSplitEnumerator
                 checkpointId,
                 readersAwaitingSplit,
                 subtask -> 
splitAssigner.getNextSnapshotId(subtask).orElse(nextSnapshotId),
+                splits,
                 context.currentParallelism());
 
         LOG.debug("Source Checkpoint is {}", checkpoint);
@@ -314,7 +317,7 @@ public class ContinuousFileSplitEnumerator
             List<FileStoreSourceSplit> splits = splitAssigner.getNext(task, 
null);
             if (!splits.isEmpty()) {
                 assignment.put(task, splits);
-                consumerProgressCalculator.updateAssignInformation(task, 
splits.get(0));
+                consumerProgressCalculator.updateAssignInformation(task, 
splits);
             }
         }
 
diff --git 
a/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/source/ConsumerProgressFailoverTest.java
 
b/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/source/ConsumerProgressFailoverTest.java
new file mode 100644
index 0000000000..5ede82b930
--- /dev/null
+++ 
b/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/source/ConsumerProgressFailoverTest.java
@@ -0,0 +1,145 @@
+/*
+ * 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.paimon.flink.source;
+
+import org.apache.paimon.data.GenericRow;
+import org.apache.paimon.fs.Path;
+import org.apache.paimon.fs.local.LocalFileIO;
+import org.apache.paimon.schema.FileSystemSchemaManager;
+import org.apache.paimon.schema.Schema;
+import org.apache.paimon.table.FileStoreTable;
+import org.apache.paimon.table.FileStoreTableFactory;
+import org.apache.paimon.table.sink.StreamTableCommit;
+import org.apache.paimon.table.sink.StreamTableWrite;
+import org.apache.paimon.table.sink.StreamWriteBuilder;
+import org.apache.paimon.table.source.StreamTableScan;
+import org.apache.paimon.types.DataTypes;
+
+import 
org.apache.flink.connector.testutils.source.reader.TestingSplitEnumeratorContext;
+import org.junit.jupiter.api.io.TempDir;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+class ConsumerProgressFailoverTest {
+    @TempDir java.nio.file.Path tempDir;
+
+    @ParameterizedTest
+    @CsvSource({
+        "false, false, false",
+        "true, false, false",
+        "false, true, false",
+        "true, true, false",
+        "false, true, true",
+        "true, true, true"
+    })
+    void 
returnedUncheckpointedAssignmentsDoNotAdvanceConsumerPastRestoredReader(
+            boolean pendingCheckpoint, boolean waitingReader, boolean 
emptyReturn)
+            throws Exception {
+        Map<String, String> options = new HashMap<>();
+        options.put("consumer-id", "regional-failover");
+        options.put("scan.mode", "from-snapshot");
+        options.put("scan.snapshot-id", "1");
+        Path path = new Path(tempDir.toUri());
+        new FileSystemSchemaManager(LocalFileIO.create(), path)
+                .createTable(
+                        Schema.newBuilder().column("id", 
DataTypes.INT()).options(options).build());
+        FileStoreTable table = 
FileStoreTableFactory.create(LocalFileIO.create(), path);
+        StreamWriteBuilder builder = 
table.newStreamWriteBuilder().withCommitUser("writer");
+        int snapshotCount = emptyReturn ? 1 : 3;
+        try (StreamTableWrite write = builder.newWrite();
+                StreamTableCommit commit = builder.newCommit()) {
+            for (int i = 1; i <= snapshotCount; i++) {
+                write.write(GenericRow.of(i));
+                commit.commit(i, write.prepareCommit(true, i));
+            }
+        }
+        StreamTableScan scan = table.newStreamScan();
+        FileStoreSourceSplitGenerator generator = new 
FileStoreSourceSplitGenerator();
+        List<FileStoreSourceSplit> splits = new ArrayList<>();
+        // from-snapshot first establishes its next snapshot and returns an 
empty starting plan.
+        for (int i = 0; i <= snapshotCount && splits.size() < snapshotCount; 
i++) {
+            splits.addAll(generator.createSplits(scan.plan()));
+        }
+        assertThat(splits).hasSize(snapshotCount);
+        TestingSplitEnumeratorContext<FileStoreSourceSplit> context =
+                new TestingSplitEnumeratorContext<>(1);
+        context.registerReader(0, "host");
+        ContinuousFileSplitEnumerator enumerator =
+                new ContinuousFileSplitEnumerator(
+                        context,
+                        splits,
+                        scan.checkpoint(),
+                        Long.MAX_VALUE,
+                        scan,
+                        false,
+                        1,
+                        false,
+                        -1,
+                        1);
+        enumerator.handleSplitRequest(0, "host");
+        enumerator.snapshotState(10L);
+        enumerator.notifyCheckpointComplete(10L);
+        
assertThat(table.consumerManager().consumer("regional-failover").get().nextSnapshot())
+                .isEqualTo(1L);
+        if (!emptyReturn) {
+            enumerator.handleSourceEvent(0, new 
ReaderConsumeProgressEvent(1L));
+            enumerator.handleSplitRequest(0, "host");
+            enumerator.handleSourceEvent(0, new 
ReaderConsumeProgressEvent(2L));
+            enumerator.handleSplitRequest(0, "host");
+        }
+        if (waitingReader) {
+            enumerator.handleSourceEvent(0, new 
ReaderConsumeProgressEvent(snapshotCount));
+            enumerator.handleSplitRequest(0, "host");
+            assertThat(enumerator.readersAwaitingSplit).contains(0);
+        }
+        if (pendingCheckpoint) {
+            enumerator.snapshotState(11L);
+        }
+        // The reader restores split1 from checkpoint10. The coordinator 
returns only assignments
+        // made after that checkpoint (split2 and split3), and the restored 
reader does not request
+        // another split while it is still replaying split1.
+        context.registeredReaders().remove(0);
+        enumerator.addSplitsBack(
+                emptyReturn
+                        ? java.util.Collections.emptyList()
+                        : Arrays.asList(splits.get(1), splits.get(2)),
+                0);
+        context.registerReader(0, "restored-host");
+        enumerator.addReader(0);
+        enumerator.snapshotState(12L);
+        enumerator.notifyCheckpointComplete(12L);
+        
assertThat(table.consumerManager().consumer("regional-failover").get().nextSnapshot())
+                .isEqualTo(1L);
+        enumerator.handleSourceEvent(0, new ReaderConsumeProgressEvent(1L));
+        enumerator.handleSplitRequest(0, "host");
+        enumerator.snapshotState(13L);
+        enumerator.notifyCheckpointComplete(13L);
+        
assertThat(table.consumerManager().consumer("regional-failover").get().nextSnapshot())
+                .isEqualTo(2L);
+        enumerator.close();
+    }
+}
diff --git 
a/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/source/ContinuousFileSplitEnumeratorTest.java
 
b/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/source/ContinuousFileSplitEnumeratorTest.java
index 9453c89b64..9dae923803 100644
--- 
a/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/source/ContinuousFileSplitEnumeratorTest.java
+++ 
b/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/source/ContinuousFileSplitEnumeratorTest.java
@@ -822,6 +822,56 @@ public class ContinuousFileSplitEnumeratorTest
         assertThat(scan.getNextSnapshotIdForConsumer()).isEqualTo(2L);
     }
 
+    @Test
+    public void testConsumerProgressWithOutOfOrderSplits() throws Exception {
+        List<Long> progress = new ArrayList<>();
+        List<FileStoreSourceSplit> splits = new ArrayList<>();
+        for (int i = 1; i <= 5; i++) {
+            splits.add(createSnapshotSplit(i, 0, Collections.emptyList()));
+        }
+        MockScan scan = new MockScan(new TreeMap<>());
+        ContinuousFileSplitEnumerator enumerator =
+                new Builder()
+                        
.setSplitEnumeratorContext(getSplitEnumeratorContext(2))
+                        .setInitialSplits(splits)
+                        .setScan(scan)
+                        .unawareBucket(true)
+                        .build();
+        enumerator.handleSplitRequest(0, "test-host");
+        enumerator.handleSplitRequest(1, "test-host");
+        enumerator.handleSplitRequest(1, "test-host");
+
+        // separate resets leave the shared queue as [2, 3, 1, 4, 5]
+        enumerator.addSplitsBack(splits.subList(0, 1), 0);
+        enumerator.addSplitsBack(splits.subList(1, 3), 1);
+        enumerator.handleSplitRequest(0, "test-host");
+        enumerator.handleSplitRequest(1, "test-host");
+        triggerCheckpointAndComplete(enumerator, 1L); // split 1 is still 
pending
+        progress.add(scan.getNextSnapshotIdForConsumer());
+
+        // readers request the next split before reporting the one they have 
finished
+        enumerator.handleSplitRequest(1, "test-host");
+        enumerator.handleSourceEvent(1, new ReaderConsumeProgressEvent(3L));
+        enumerator.handleSplitRequest(0, "test-host");
+        enumerator.handleSourceEvent(0, new ReaderConsumeProgressEvent(2L));
+        triggerCheckpointAndComplete(enumerator, 2L); // split 1 is still 
being read
+        progress.add(scan.getNextSnapshotIdForConsumer());
+
+        // restored splits have no order, so finishing 5 does not mean that 2 
has finished
+        MockScan restoredScan = new MockScan(new TreeMap<>());
+        ContinuousFileSplitEnumerator restored =
+                new Builder()
+                        
.setSplitEnumeratorContext(getSplitEnumeratorContext(1))
+                        .setScan(restoredScan)
+                        .build();
+        restored.addSplitsBack(Collections.emptyList(), 0);
+        restored.handleSourceEvent(0, new ReaderConsumeProgressEvent(5L));
+        triggerCheckpointAndComplete(restored, 1L);
+        progress.add(restoredScan.getNextSnapshotIdForConsumer());
+
+        assertThat(progress).containsExactly(1L, 1L, null);
+    }
+
     @Test
     public void testEnumeratorSplitMax() throws Exception {
         final TestingSplitEnumeratorContext<FileStoreSourceSplit> context =

Reply via email to