scwhittle commented on code in PR #41518:
URL: https://github.com/apache/beam/pull/41518#discussion_r4665019239
##########
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));
Review Comment:
instead of throwing an exception is there some conservative behavior we can
take, like assuming it was just modified? or keep the previous modified time
for the file?
##########
sdks/java/core/src/main/java/org/apache/beam/sdk/io/FileIO.java:
##########
@@ -537,25 +543,28 @@ public MatchConfiguration
withEmptyMatchTreatment(EmptyMatchTreatment treatment)
* condition is reached, where the input to the condition is the
filepattern.
*
* <p>If {@code matchUpdatedFiles} is set, also watches for files with
timestamp change, with
- * the watching frequency given by the {@code interval}. The pipeline will
throw a {@code
- * RuntimeError} if timestamp extraction for the matched file has failed,
suggesting the
+ * the watching frequency given by the {@code interval}. The pipeline will
throw a {@link
+ * RuntimeException} if timestamp extraction for the matched file has
failed, suggesting the
* timestamp metadata is not available with the IO connector.
*
- * <p>Matching continuously scales poorly, as it is stateful, and requires
storing file ids in
- * memory. In addition, because it is memory-only, if a pipeline is
restarted, already processed
- * files will be reprocessed. Consider an alternate technique, such as <a
+ * <p>Deduplication state grows with the number of files matched unless
bounded with {@link
+ * #withTimestampCursor()} or {@link #withTimestampCursor(Duration)}. For
a growing directory on
+ * GCS, consider an alternate technique such as <a
*
href="https://cloud.google.com/storage/docs/pubsub-notifications">Pub/Sub
Notifications</a>
- * when using GCS if possible.
+ * if possible.
*/
public MatchConfiguration continuously(
Duration interval, TerminationCondition<String, ?> condition, boolean
matchUpdatedFiles) {
- LOG.warn(
Review Comment:
should we keep this warning if the timestamp cursor isn't enabled?
##########
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:
should we additionally delay the watermark some to account for possible skew
between local times and the file system times? getting it wrong could lead to
data-loss if late data is not handled by the pipeline.
##########
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)
Review Comment:
comment on what the result instant is (or use a explicit type instead of KV
to self-document)
--
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]