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 41953d209d [core] Fix rollback_to_watermark with repeated snapshot 
watermarks (#11149)
41953d209d is described below

commit 41953d209d2f53032a41d12b4b60444c21cfa78a
Author: chenjian2664 <[email protected]>
AuthorDate: Mon Nov 16 13:47:52 2026 +0800

    [core] Fix rollback_to_watermark with repeated snapshot watermarks (#11149)
---
 .../org/apache/paimon/utils/SnapshotManager.java   |  8 ++--
 .../apache/paimon/utils/SnapshotManagerTest.java   | 54 ++++++++++++++++++++++
 2 files changed, 58 insertions(+), 4 deletions(-)

diff --git 
a/paimon-core/src/main/java/org/apache/paimon/utils/SnapshotManager.java 
b/paimon-core/src/main/java/org/apache/paimon/utils/SnapshotManager.java
index df627955e5..8693d11dac 100644
--- a/paimon-core/src/main/java/org/apache/paimon/utils/SnapshotManager.java
+++ b/paimon-core/src/main/java/org/apache/paimon/utils/SnapshotManager.java
@@ -497,12 +497,12 @@ public class SnapshotManager implements Serializable {
             } else {
                 if (commitWatermark > watermark) {
                     latest = pos - 1; // Search in the left half
-                } else if (commitWatermark < watermark) {
+                } else {
+                    // Upper bound: an exact match is a candidate, but a later 
snapshot may carry
+                    // the same watermark. Watermarks repeat because the 
commit watermark takes the
+                    // maximum, so stopping here would return the middle of 
such a run.
                     earliest = mid + 1; // Search in the right half
                     finalSnapshot = snapshot;
-                } else {
-                    finalSnapshot = snapshot; // Found the exact match
-                    break;
                 }
             }
         }
diff --git 
a/paimon-core/src/test/java/org/apache/paimon/utils/SnapshotManagerTest.java 
b/paimon-core/src/test/java/org/apache/paimon/utils/SnapshotManagerTest.java
index 9bd9444bd5..99484eb5f4 100644
--- a/paimon-core/src/test/java/org/apache/paimon/utils/SnapshotManagerTest.java
+++ b/paimon-core/src/test/java/org/apache/paimon/utils/SnapshotManagerTest.java
@@ -924,6 +924,60 @@ public class SnapshotManagerTest {
         assertThat(snapshotManager.earlierOrEqualWatermark(50)).isNull();
     }
 
+    @Test
+    public void testEarlierOrEqualWatermarkWithRepeatedWatermarks() throws 
IOException {
+        FileIO localFileIO = LocalFileIO.create();
+        SnapshotManager snapshotManager =
+                newSnapshotManager(localFileIO, new Path(tempDir.toString()));
+        // a plateau of identical watermarks: snapshots 1, 2, 3 all carry 
1000. Repeated
+        // watermarks are normal because the commit watermark takes the 
maximum.
+        Map<Long, Long> watermarks = watermarkMap(0, 500, 1, 1000, 2, 1000, 3, 
1000, 4, 2000);
+        for (long i = 0; i < 5; i++) {
+            Snapshot snapshot = createSnapshotWithMillis(i, 1000 + i * 1000, 
watermarks.get(i));
+            localFileIO.tryToWriteAtomic(snapshotManager.snapshotPath(i), 
snapshot.toJson());
+        }
+
+        // upper bound: the last snapshot of the plateau, not the 
binary-search midpoint
+        
assertThat(snapshotManager.earlierOrEqualWatermark(1000).id()).isEqualTo(3);
+        // a value inside the gap still resolves to the end of the previous 
plateau
+        
assertThat(snapshotManager.earlierOrEqualWatermark(1999).id()).isEqualTo(3);
+        // non-repeated watermarks are unaffected
+        
assertThat(snapshotManager.earlierOrEqualWatermark(2000).id()).isEqualTo(4);
+        
assertThat(snapshotManager.earlierOrEqualWatermark(500).id()).isEqualTo(0);
+    }
+
+    @Test
+    public void testEarlierOrEqualWatermarkWithPlateauAtTheLatestSnapshot() 
throws IOException {
+        FileIO localFileIO = LocalFileIO.create();
+        SnapshotManager snapshotManager =
+                newSnapshotManager(localFileIO, new Path(tempDir.toString()));
+        // the plateau ends at the latest snapshot, so the right walk must 
stop there
+        Map<Long, Long> watermarks = watermarkMap(0, 500, 1, 1000, 2, 1000, 3, 
1000);
+        for (long i = 0; i < 4; i++) {
+            Snapshot snapshot = createSnapshotWithMillis(i, 1000 + i * 1000, 
watermarks.get(i));
+            localFileIO.tryToWriteAtomic(snapshotManager.snapshotPath(i), 
snapshot.toJson());
+        }
+
+        
assertThat(snapshotManager.earlierOrEqualWatermark(1000).id()).isEqualTo(3);
+    }
+
+    @Test
+    public void 
testEarlierOrEqualWatermarkWithRepeatedWatermarksAndNullWatermarks()
+            throws IOException {
+        FileIO localFileIO = LocalFileIO.create();
+        SnapshotManager snapshotManager =
+                newSnapshotManager(localFileIO, new Path(tempDir.toString()));
+        // snapshots 2 and 4 carry no watermark and sit inside a plateau of 
1000s
+        Map<Long, Long> watermarks = watermarkMap(0, 500, 1, 1000, 3, 1000, 5, 
1000, 6, 2000);
+        for (long i = 0; i < 7; i++) {
+            Snapshot snapshot = createSnapshotWithMillis(i, 1000 + i * 1000, 
watermarks.get(i));
+            localFileIO.tryToWriteAtomic(snapshotManager.snapshotPath(i), 
snapshot.toJson());
+        }
+
+        // null-watermark snapshots must not shadow the real end of the plateau
+        
assertThat(snapshotManager.earlierOrEqualWatermark(1000).id()).isEqualTo(5);
+    }
+
     @Test
     @Timeout(60) // the search must terminate; defective code loops forever 
here
     public void testEarlierOrEqualWatermarkWithInterleavedNullWatermarks() 
throws IOException {

Reply via email to