lindong28 commented on code in PR #21589:
URL: https://github.com/apache/flink/pull/21589#discussion_r1089893026


##########
flink-connectors/flink-connector-base/src/main/java/org/apache/flink/connector/base/source/reader/fetcher/RemoveSplitsTask.java:
##########
@@ -0,0 +1,71 @@
+/*
+ * 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.base.source.reader.fetcher;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.api.connector.source.SourceSplit;
+import org.apache.flink.connector.base.source.reader.splitreader.SplitReader;
+import org.apache.flink.connector.base.source.reader.splitreader.SplitsRemoval;
+
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+import java.util.function.Consumer;
+import java.util.stream.Collectors;
+
+/** The task to finish reading some splits. */
+@Internal
+public class RemoveSplitsTask<SplitT extends SourceSplit> implements 
SplitFetcherTask {
+    private final SplitReader<?, SplitT> splitReader;
+    private final List<SplitT> removedSplits;
+    private final Map<String, SplitT> assignedSplits;
+    private final Consumer<Collection<String>> splitFinishedCallback;
+
+    RemoveSplitsTask(
+            SplitReader<?, SplitT> splitReader,
+            List<SplitT> removedSplits,
+            Map<String, SplitT> assignedSplits,
+            Consumer<Collection<String>> splitFinishedCallback) {
+        this.splitReader = splitReader;
+        this.removedSplits = removedSplits;
+        this.assignedSplits = assignedSplits;
+        this.splitFinishedCallback = splitFinishedCallback;
+    }
+
+    @Override
+    public boolean run() {
+        for (SplitT s : removedSplits) {
+            assignedSplits.remove(s.splitId());
+        }
+        splitReader.handleSplitsChanges(new SplitsRemoval<>(removedSplits));

Review Comment:
   It seems that this change won't work with `KafkaPartitionSplitReader` 
because `KafkaPartitionSplitReader#handleSplitsChanges` would throw 
`UnsupportedOperationException` if the input is `SplitsRemoval`.
   
   Should we add either unit or integration test to verify that this PR works 
with Kafka Source?



##########
flink-connectors/flink-connector-base/src/main/java/org/apache/flink/connector/base/source/reader/fetcher/RemoveSplitsTask.java:
##########
@@ -0,0 +1,71 @@
+/*
+ * 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.base.source.reader.fetcher;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.api.connector.source.SourceSplit;
+import org.apache.flink.connector.base.source.reader.splitreader.SplitReader;
+import org.apache.flink.connector.base.source.reader.splitreader.SplitsRemoval;
+
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+import java.util.function.Consumer;
+import java.util.stream.Collectors;
+
+/** The task to finish reading some splits. */
+@Internal
+public class RemoveSplitsTask<SplitT extends SourceSplit> implements 
SplitFetcherTask {
+    private final SplitReader<?, SplitT> splitReader;
+    private final List<SplitT> removedSplits;
+    private final Map<String, SplitT> assignedSplits;
+    private final Consumer<Collection<String>> splitFinishedCallback;
+
+    RemoveSplitsTask(
+            SplitReader<?, SplitT> splitReader,
+            List<SplitT> removedSplits,
+            Map<String, SplitT> assignedSplits,
+            Consumer<Collection<String>> splitFinishedCallback) {
+        this.splitReader = splitReader;
+        this.removedSplits = removedSplits;
+        this.assignedSplits = assignedSplits;
+        this.splitFinishedCallback = splitFinishedCallback;
+    }
+
+    @Override
+    public boolean run() {
+        for (SplitT s : removedSplits) {
+            assignedSplits.remove(s.splitId());
+        }
+        splitReader.handleSplitsChanges(new SplitsRemoval<>(removedSplits));
+        splitFinishedCallback.accept(
+                
removedSplits.stream().map(SourceSplit::splitId).collect(Collectors.toList()));
+        return true;

Review Comment:
   Would it be useful to add an info level logging here saying we finished 
reading from this split, similar to this existing log [1]?
   
   And it can be useful to make the log a bit different from [1] so that we 
know which code path is triggered.
   
   [1] 
https://github.com/apache/flink/blob/master/flink-connectors/flink-connector-base/src/main/java/org/apache/flink/connector/base/source/reader/fetcher/SplitFetcher.java#L105



##########
flink-connectors/flink-connector-base/src/main/java/org/apache/flink/connector/base/source/reader/SingleThreadMultiplexSourceReaderBase.java:
##########
@@ -113,4 +115,26 @@ public SingleThreadMultiplexSourceReaderBase(
             SourceReaderContext context) {
         super(elementsQueue, splitFetcherManager, recordEmitter, config, 
context);
     }
+
+    /**
+     * This constructor behaves like {@link 
#SingleThreadMultiplexSourceReaderBase(Supplier,
+     * RecordEmitter, Configuration, SourceReaderContext)}, but accepts a 
specific {@link
+     * FutureCompletingBlockingQueue}, {@link RecordEvaluator} and {@link

Review Comment:
   nits: Would it be better to make the order of these classes in the comment 
more consistent with their order in the code?
   
   Maybe changing to this:
   
   ```
   ... but accepts a specific {@link FutureCompletingBlockingQueue}, {@link 
SingleThreadFetcherManager} and {@link RecordEvaluator}.
   ```



##########
flink-connectors/flink-connector-base/src/main/java/org/apache/flink/connector/base/source/reader/SourceReaderBase.java:
##########
@@ -328,14 +348,86 @@ private SplitContext(String splitId, SplitStateT state) {
             this.splitId = splitId;
         }
 
-        SourceOutput<T> getOrCreateSplitOutput(ReaderOutput<T> mainOutput) {
+        SourceOutput<T> getOrCreateSplitOutput(
+                ReaderOutput<T> mainOutput,
+                @Nullable RecordEvaluator<T> recordEvaluator,
+                SplitT split,
+                SplitFetcherManager<E, SplitT> splitFetcherManager) {
             if (sourceOutput == null) {
                 // The split output should have been created when 
AddSplitsEvent was processed in
                 // SourceOperator. Here we just use this method to get the 
previously created
                 // output.
                 sourceOutput = mainOutput.createOutputForSplit(splitId);
+                if (recordEvaluator != null) {
+                    sourceOutput =
+                            new SourceOutputWrapper<>(
+                                    split, recordEvaluator, sourceOutput, 
splitFetcherManager);

Review Comment:
   nits: Would it be more readable to make the order of SourceOutputWrapper's 
parameters more consistent with the order of getOrCreateSplitOutput's parameter?
   
   For example: sourceOutput, recordEvaluator, split, splitFetcherManager



##########
flink-connectors/flink-connector-base/src/main/java/org/apache/flink/connector/base/source/reader/SourceReaderBase.java:
##########
@@ -211,7 +226,12 @@ private boolean moveToNextSplit(
 
         currentSplitContext = splitStates.get(nextSplitId);
         checkState(currentSplitContext != null, "Have records for a split that 
was not registered");
-        currentSplitOutput = 
currentSplitContext.getOrCreateSplitOutput(output);
+        currentSplitOutput =
+                currentSplitContext.getOrCreateSplitOutput(

Review Comment:
   Would it be simpler and more readable to pass a `Function<T, Boolean>`  
instead of passing `eofRecordEvaluator`, `splitId` and `splitFetcherManager`? 
Then we won't need to add the extra generic type parameters to `SplitContext`.
   
   I made a commit here [1] to show the resulting code.
   
   [1] 
https://github.com/apache/flink/commit/215f80fe4640dc6686ca9ebddc6ae42c5fdb4cb3



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