gaoyunhaii commented on code in PR #20415: URL: https://github.com/apache/flink/pull/20415#discussion_r937444774
########## 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 a bit tend to we not keep a set of assignedSplits in this class, and remove the assigned ones on-the-fly. Currently `splitAssigner` has kept the information of assigned splits, which indicating some kind of information duplication, also the iteration here might cause a bit more overhead. Instead, we might 1. Not keep the assignedSplits set. 2. When creating a new splitAssigner, we could retrieve the unassigned sets from the existing splitAssigner. Then we could remove the splits not in the unassigned sets. -- 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: issues-unsubscr...@flink.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org