sundargates commented on code in PR #8553: URL: https://github.com/apache/iceberg/pull/8553#discussion_r1338898564
########## flink/v1.17/flink/src/main/java/org/apache/iceberg/flink/source/reader/IcebergTimestampWatermarkExtractor.java: ########## @@ -0,0 +1,64 @@ +/* + * 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.iceberg.flink.source.reader; + +import java.io.Serializable; +import java.util.Comparator; +import org.apache.flink.table.data.RowData; +import org.apache.iceberg.Schema; +import org.apache.iceberg.flink.source.split.IcebergSourceSplit; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; +import org.apache.iceberg.types.Conversions; +import org.apache.iceberg.types.Type; +import org.apache.iceberg.types.Types; + +/** + * {@link IcebergWatermarkExtractor} implementation which uses an Iceberg timestamp column to get + * the watermarks and the event times for the {@link RowData} read by the reader function. + */ +public class IcebergTimestampWatermarkExtractor + implements IcebergWatermarkExtractor<RowData>, Serializable { + private final int tsFieldId; + + /** + * Creates the extractor. + * + * @param schema The schema of the Table + * @param tsFieldName The timestamp column which should be used as an event time + */ + public IcebergTimestampWatermarkExtractor(Schema schema, String tsFieldName) { + Types.NestedField field = schema.findField(tsFieldName); + Preconditions.checkArgument( + field.type().typeId().equals(Type.TypeID.TIMESTAMP), "Type should be timestamp"); + this.tsFieldId = field.fieldId(); + } + + @Override + public long extractWatermark(IcebergSourceSplit split) { + return split.task().files().stream() + .map( + scanTask -> + (long) + Conversions.fromByteBuffer( + Types.LongType.get(), scanTask.file().lowerBounds().get(tsFieldId)) + / 1000L) + .min(Comparator.comparingLong(l -> l)) + .get(); Review Comment: Can a split exist without any associated files? I assume no, but just wanted to be sure. ########## flink/v1.17/flink/src/main/java/org/apache/iceberg/flink/source/reader/WatermarkExtractorRecordEmitter.java: ########## @@ -0,0 +1,64 @@ +/* + * 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.iceberg.flink.source.reader; + +import org.apache.flink.api.common.eventtime.Watermark; +import org.apache.flink.api.connector.source.SourceOutput; +import org.apache.iceberg.flink.source.split.IcebergSourceSplit; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Emitter which emits the record with event time and updates the split position. + * + * <p>The Emitter also emits watermarks at the beginning of every split, and sets the event + * timestamp based on the provided {@link IcebergWatermarkExtractor}. + */ +class WatermarkExtractorRecordEmitter<T> implements SerializableRecordEmitter<T> { + private static final Logger LOG = LoggerFactory.getLogger(WatermarkExtractorRecordEmitter.class); + private final IcebergWatermarkExtractor timeExtractor; + private String lastSplit = null; + private long watermark; + + WatermarkExtractorRecordEmitter(IcebergWatermarkExtractor timeExtractor) { + this.timeExtractor = timeExtractor; + } + + @Override + public void emitRecord( + RecordAndPosition<T> element, SourceOutput<T> output, IcebergSourceSplit split) { + if (!split.splitId().equals(lastSplit)) { Review Comment: just curious, when do you expect this condition to be triggered? ########## flink/v1.17/flink/src/main/java/org/apache/iceberg/flink/source/split/SplitComparators.java: ########## @@ -56,4 +57,20 @@ public static SerializableComparator<IcebergSourceSplit> fileSequenceNumber() { } }; } + + /** Comparator which orders the splits based on watermark of the splits */ + public static SerializableComparator<IcebergSourceSplit> watermarkComparator( Review Comment: nit: s/watermarkComparator/watermarksAwareComparator ########## flink/v1.17/flink/src/main/java/org/apache/iceberg/flink/source/IcebergSource.java: ########## @@ -429,6 +440,20 @@ public Builder<T> setAll(Map<String, String> properties) { return this; } + /** + * Sets the {@link IcebergWatermarkExtractor} to retrieve the split watermark before emitting + * the records for a given split. The {@link + * IcebergWatermarkExtractor#extractWatermark(IcebergSourceSplit)} is also used for ordering the + * splits for read. + */ + public Builder<T> watermarkExtractor(IcebergWatermarkExtractor<T> newWatermarkExtractor) { Review Comment: nit: Instead of requiring the user to provide watermark extractor here and then creating the assignerFactory, a better approach would be to allow the user to create the assignerFactory with the watermark extractor. This would enable the decoupling of the iceberg source from watermark extraction. As a result, the code could be simplified, and some of the preconditions could be removed. ########## flink/v1.17/flink/src/main/java/org/apache/iceberg/flink/source/reader/SerializableRecordEmitter.java: ########## @@ -18,19 +18,21 @@ */ package org.apache.iceberg.flink.source.reader; -import org.apache.flink.api.connector.source.SourceOutput; +import java.io.Serializable; import org.apache.flink.connector.base.source.reader.RecordEmitter; import org.apache.iceberg.flink.source.split.IcebergSourceSplit; -final class IcebergSourceRecordEmitter<T> - implements RecordEmitter<RecordAndPosition<T>, T, IcebergSourceSplit> { - - IcebergSourceRecordEmitter() {} +public interface SerializableRecordEmitter<T> Review Comment: nit: @FunctionalInterface? ########## flink/v1.17/flink/src/main/java/org/apache/iceberg/flink/source/reader/WatermarkExtractorRecordEmitter.java: ########## @@ -0,0 +1,64 @@ +/* + * 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.iceberg.flink.source.reader; + +import org.apache.flink.api.common.eventtime.Watermark; +import org.apache.flink.api.connector.source.SourceOutput; +import org.apache.iceberg.flink.source.split.IcebergSourceSplit; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Emitter which emits the record with event time and updates the split position. + * + * <p>The Emitter also emits watermarks at the beginning of every split, and sets the event + * timestamp based on the provided {@link IcebergWatermarkExtractor}. + */ +class WatermarkExtractorRecordEmitter<T> implements SerializableRecordEmitter<T> { + private static final Logger LOG = LoggerFactory.getLogger(WatermarkExtractorRecordEmitter.class); + private final IcebergWatermarkExtractor timeExtractor; + private String lastSplit = null; + private long watermark; + + WatermarkExtractorRecordEmitter(IcebergWatermarkExtractor timeExtractor) { + this.timeExtractor = timeExtractor; + } + + @Override + public void emitRecord( + RecordAndPosition<T> element, SourceOutput<T> output, IcebergSourceSplit split) { + if (!split.splitId().equals(lastSplit)) { + long newWatermark = timeExtractor.extractWatermark(split); + if (newWatermark < watermark) { + LOG.warn( + "Watermark decreased. PreviousWM {}, currentWM {}, previousSplit {}, currentSplit {}.", + watermark, + newWatermark, + lastSplit, + split.splitId()); + } Review Comment: Shouldn't Flink's watermark strategy determine if it's monotonic? Wondering if we would end up duplicating this logic in two places. -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
