JingsongLi commented on code in PR #11302:
URL: https://github.com/apache/paimon/pull/11302#discussion_r4662774897


##########
paimon-core/src/main/java/org/apache/paimon/tag/TagAutoCreation.java:
##########
@@ -132,30 +152,138 @@ public boolean forceCreatingSnapshot() {
         return false;
     }
 
+    /** Validate a newly supplied watermark before it can advance the snapshot 
watermark. */
+    public static void validateWatermark(
+            CoreOptions options,
+            long watermark,
+            SnapshotManager snapshotManager,
+            @Nullable Snapshot latestSnapshot,
+            @Nullable TagAutoCreation creation) {
+        if (watermark == Long.MIN_VALUE
+                || options.tagCreationMode() != 
CoreOptions.TagCreationMode.WATERMARK) {
+            return;
+        }
+        ZoneId zone = ZoneId.of(options.sinkWatermarkTimeZone());
+        LocalDateTime time = 
LocalDateTime.ofInstant(Instant.ofEpochMilli(watermark), zone);
+        TagPeriodHandler tagPeriodHandler = TagPeriodHandler.create(options);
+        Duration delay = options.tagCreationDelay();
+        UnaryOperator<LocalDateTime> normalize = 
tagPeriodHandler::normalizeForWatermarkValidation;
+        LocalDateTime firstTag = normalize.apply(time);
+        // A write-only writer cannot tell whether a separate maintainer has 
seen an existing
+        // tag. Until a snapshot supplies a usable watermark, the first tag 
may still be due.
+        boolean mayHaveTagProgress =
+                latestSnapshot != null
+                        && (!options.writeOnly()
+                                || (latestSnapshot.watermark() != null
+                                        && latestSnapshot.watermark() != 
Long.MIN_VALUE));
+        LocalDateTime delayedTime = mayHaveTagProgress ? time.minus(delay) : 
time;
+        LocalDateTime delayedTag =
+                delayedTime.equals(time) ? firstTag : 
normalize.apply(delayedTime);
+        try {
+            tagPeriodHandler.timeToTag(firstTag);
+            if (!firstTag.equals(delayedTag)) {
+                tagPeriodHandler.timeToTag(delayedTag);
+            }
+            // Restoring a custom-period tag from its minute-precision name 
can make a
+            // completion tag due before the watermark-derived boundary 
reaches year 10000.
+            if (!mayHaveTagProgress
+                    || !options.tagAutomaticCompletion()
+                    || delayedTime.getYear() <= MAX_TAG_YEAR) {
+                return;
+            }
+        } catch (DateTimeException e) {
+            // If even the delayed boundary is too far ahead, completing tags 
one at a time
+            // or using a separate tag creator cannot make that boundary 
printable.
+            boolean exceedsTagRange =
+                    firstTag.getYear() > MAX_TAG_YEAR && delayedTag.getYear() 
> MAX_TAG_YEAR;
+            if (!mayHaveTagProgress || exceedsTagRange) {
+                throw invalidWatermark(watermark, zone, e);
+            }
+        }
+
+        if (creation == null) {
+            TagManager tagManager =
+                    new TagManager(
+                            snapshotManager.fileIO(),
+                            snapshotManager.tablePath(),
+                            snapshotManager.branch());
+            creation = create(options, snapshotManager, tagManager, 
Collections.emptyList());
+            checkState(creation != null, "Expected a tag creator for watermark 
mode.");
+        }
+        try {
+            LocalDateTime nextTag = 
creation.processSnapshotsUpTo(latestSnapshot.id(), false);

Review Comment:
   **[P2] Require durable tag progress before accepting an out-of-range 
immediate tag.** With `write-only=true`, hourly UTC tags and a 15-minute delay, 
I committed a valid 2024 watermark and then `+10000-01-01T01:05:00`. The writer 
accepted the second commit by replaying the first snapshot, even though no tag 
had been written. Independent snapshot expiration kept only snapshot 2. A fresh 
tag maintainer then treated that watermark as its first and failed with 
`DateTimeException` when formatting year 10000. Please make acceptance of this 
delayed boundary depend on persisted tag progress, or ensure retention keeps 
the snapshots needed to reconstruct that progress.



-- 
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