pltbkd commented on code in PR #20415: URL: https://github.com/apache/flink/pull/20415#discussion_r937498023
########## flink-connectors/flink-connector-files/src/main/java/org/apache/flink/connector/file/src/impl/DynamicFileSplitEnumerator.java: ########## @@ -0,0 +1,209 @@ +/* + * 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.flink.connector.file.src.impl; + +import org.apache.flink.annotation.Internal; +import org.apache.flink.api.connector.source.SourceEvent; +import org.apache.flink.api.connector.source.SplitEnumerator; +import org.apache.flink.api.connector.source.SplitEnumeratorContext; +import org.apache.flink.api.connector.source.SupportsHandleExecutionAttemptSourceEvent; +import org.apache.flink.connector.file.src.FileSource; +import org.apache.flink.connector.file.src.FileSourceSplit; +import org.apache.flink.connector.file.src.PendingSplitsCheckpoint; +import org.apache.flink.connector.file.src.assigners.FileSplitAssigner; +import org.apache.flink.connector.file.src.enumerate.DynamicFileEnumerator; +import org.apache.flink.connector.file.src.enumerate.FileEnumerator; +import org.apache.flink.core.fs.Path; +import org.apache.flink.table.connector.source.DynamicFilteringData; +import org.apache.flink.table.connector.source.DynamicFilteringEvent; +import org.apache.flink.util.FlinkRuntimeException; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.annotation.Nullable; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; + +import static org.apache.flink.util.Preconditions.checkNotNull; + +/** + * A SplitEnumerator implementation for bounded / batch {@link FileSource} input. + * + * <p>This enumerator takes all files that are present in the configured input directories and + * assigns them to the readers. Once all files are processed, the source is finished. + * + * <p>The implementation of this class is rather thin. The actual logic for creating the set of + * FileSourceSplits to process, and the logic to decide which reader gets what split, are in {@link + * FileEnumerator} and in {@link FileSplitAssigner}, respectively. + */ +@Internal +public class DynamicFileSplitEnumerator<SplitT extends FileSourceSplit> + implements SplitEnumerator<SplitT, PendingSplitsCheckpoint<SplitT>>, + SupportsHandleExecutionAttemptSourceEvent { + + private static final Logger LOG = LoggerFactory.getLogger(DynamicFileSplitEnumerator.class); + + private final SplitEnumeratorContext<SplitT> context; + + private final DynamicFileEnumerator.Provider fileEnumeratorFactory; + + private final FileSplitAssigner.Provider splitAssignerFactory; + + /** + * Stores the id of splits that has been assigned. The split assigner may be rebuilt when a + * DynamicFilteringEvent is received. After that, the splits that are already assigned can be + * assigned for the second time. We have to retain the state and filter out the splits that has + * been assigned with this set. + */ + private final Set<String> assignedSplits; + + private transient Set<String> allEnumeratingSplits; + + private transient FileSplitAssigner splitAssigner; + + // ------------------------------------------------------------------------ + + public DynamicFileSplitEnumerator( + SplitEnumeratorContext<SplitT> context, + DynamicFileEnumerator.Provider fileEnumeratorFactory, + FileSplitAssigner.Provider splitAssignerFactory) { + this.context = checkNotNull(context); + this.splitAssignerFactory = checkNotNull(splitAssignerFactory); + this.fileEnumeratorFactory = checkNotNull(fileEnumeratorFactory); + this.assignedSplits = new HashSet<>(); + } + + @Override + public void start() { + // no resources to start + } + + @Override + public void close() throws IOException { + // no resources to close + } + + @Override + public void addReader(int subtaskId) { + // this source is purely lazy-pull-based, nothing to do upon registration + } + + @Override + public void handleSplitRequest(int subtask, @Nullable String hostname) { + if (!context.registeredReaders().containsKey(subtask)) { + // reader failed between sending the request and now. skip this request. + return; + } + + if (splitAssigner == null) { + // No DynamicFilteringData is received before the first split request, + // create a split assigner that handles all splits + createSplitAssigner(null); + } + + if (LOG.isDebugEnabled()) { + final String hostInfo = + hostname == null ? "(no host locality info)" : "(on host '" + hostname + "')"; + LOG.debug("Subtask {} {} is requesting a file source split", subtask, hostInfo); + } + + final Optional<FileSourceSplit> nextSplit = getNextUnassignedSplit(hostname); + if (nextSplit.isPresent()) { + final FileSourceSplit split = nextSplit.get(); + context.assignSplit((SplitT) split, subtask); + assignedSplits.add(split.splitId()); + LOG.debug("Assigned split to subtask {} : {}", subtask, split); + } else { + context.signalNoMoreSplits(subtask); + LOG.info("No more splits available for subtask {}", subtask); + } + } + + private Optional<FileSourceSplit> getNextUnassignedSplit(String hostname) { + Optional<FileSourceSplit> nextSplit = splitAssigner.getNext(hostname); + while (nextSplit.isPresent()) { Review Comment: I'm afraid we still need the assigned splits here. Suppose that the enumerator keeps split 1,2, and filtered out split 3, and 1 has been assigned. Then it receives an event without any filtering. It's now enumerating 1,2,3 again, but it isn't aware whether 1 and 3 are assigned, since the assigner only tells that 2 is not assigned yet. The case should be handled by the DynamicFilteringDataCollectorCoordinator now by not sending duplicate events. But in case any further changes on that coordinator, and giving the fact that the duplication costs little memory, maybe it's ok to keep the set. -- 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]
