Abacn commented on code in PR #41518:
URL: https://github.com/apache/beam/pull/41518#discussion_r4668749705


##########
sdks/java/core/src/main/java/org/apache/beam/sdk/io/FileIO.java:
##########
@@ -776,14 +865,92 @@ public void process(ProcessContext c) throws Exception {
       }
     }
 
-    private static class MatchPollFn extends PollFn<String, 
MatchResult.Metadata> {
+    @VisibleForTesting
+    static long ensureLastModifiedMillis(MatchResult.Metadata input) {
+      long timestamp = input.lastModifiedMillis();
+      if (0L == timestamp) {
+        throw new RuntimeException(
+            String.format(
+                "Extract file timestamp failed: got file timestamp == 0 for 
%s.",
+                input.resourceId()));
+      }
+      return timestamp;
+    }
+
+    @VisibleForTesting
+    static class MatchPollFn extends PollFn<String, MatchResult.Metadata> {
+      private final @Nullable Duration timestampCursorAllowedLateness;
+
+      // Best-effort watermark tracking (not deduplication, which lives in 
Watch's checkpointed
+      // restriction): remembers the greatest last-modified time seen so far 
so a poll that
+      // discovers newer files holds the watermark at the filesystem clock, 
while a quiet poll
+      // releases it to poll time.
+      private transient @Nullable Instant newestMtime;
+
+      MatchPollFn() {
+        this(null);
+      }
+
+      MatchPollFn(@Nullable Duration timestampCursorAllowedLateness) {
+        this.timestampCursorAllowedLateness = timestampCursorAllowedLateness;
+      }
+
+      private synchronized boolean updateNewestMtime(Instant candidate) {
+        if (newestMtime == null || candidate.isAfter(newestMtime)) {
+          newestMtime = candidate;
+          return true;
+        }
+        return false;
+      }
+
+      private static Instant saturatingMinus(Instant instant, Duration 
duration) {
+        long millis;
+        try {
+          millis = Math.subtractExact(instant.getMillis(), 
duration.getMillis());
+        } catch (ArithmeticException e) {
+          millis = BoundedWindow.TIMESTAMP_MIN_VALUE.getMillis();
+        }
+        return new Instant(Math.max(millis, 
BoundedWindow.TIMESTAMP_MIN_VALUE.getMillis()));
+      }
+
+      @VisibleForTesting
+      KV<List<TimestampedValue<MatchResult.Metadata>>, Instant> poll(String 
element, Instant now)
+          throws Exception {
+        List<MatchResult.Metadata> metadataList =
+            FileSystems.match(element, EmptyMatchTreatment.ALLOW).metadata();
+        List<TimestampedValue<MatchResult.Metadata>> outputs = new 
ArrayList<>(metadataList.size());
+        if (timestampCursorAllowedLateness == null) {
+          for (MatchResult.Metadata metadata : metadataList) {
+            outputs.add(TimestampedValue.of(metadata, now));
+          }
+          return KV.of(outputs, now);
+        }
+
+        Instant newest = null;
+        for (MatchResult.Metadata metadata : metadataList) {
+          Instant mtime = new Instant(ensureLastModifiedMillis(metadata));
+          outputs.add(TimestampedValue.of(metadata, mtime));
+          if (newest == null || mtime.isAfter(newest)) {
+            newest = mtime;
+          }
+        }
+
+        // A poll that turned up a newer last-modified time has just read the 
filesystem clock,
+        // so hold the watermark there (capped at the poll time). A poll that 
found nothing newer
+        // takes the poll time so a quiet directory does not stall downstream 
event-time windows.
+        boolean foundNewer = newest != null && updateNewestMtime(newest);
+        Instant baseWatermark =
+            (foundNewer && newest != null && newest.isBefore(now)) ? newest : 
now;

Review Comment:
   Agree and in general, inconsistency of element timestamp and worker clock 
always exists in Beam
   
   Regardless of this situation, now set poll watermark delta to `now - 
max(pollInterval, allowedLateness)` (previously was `now - allowedLateness`) 
which defaults to `now`



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