laughingman7743 commented on code in PR #28191:
URL: https://github.com/apache/flink/pull/28191#discussion_r4729914589


##########
flink-connectors/flink-connector-base/src/main/java/org/apache/flink/connector/base/source/hybrid/HybridSourceReader.java:
##########
@@ -107,10 +106,11 @@ public InputStatus pollNext(ReaderOutput output) throws 
Exception {
 
     @Override
     public List<HybridSourceSplit> snapshotState(long checkpointId) {
-        List<? extends SourceSplit> state =
-                currentReader != null
-                        ? currentReader.snapshotState(checkpointId)
-                        : Collections.emptyList();
+        if (currentReader == null) {
+            return new ArrayList<>(restoredSplits);
+        }
+
+        List<? extends SourceSplit> state = 
currentReader.snapshotState(checkpointId);
         return HybridSourceSplit.wrapSplits(state, currentSourceIndex, 
switchedSources);

Review Comment:
   The early return covers a reader that has not switched to any source yet, 
but not one that has switched while it still buffers restored splits of a later 
source. That happens when a failover restores a reader onto an earlier source, 
for example while it replays source 0 with a split of source 1 in 
`restoredSplits`; `snapshotState` then returns only the current reader's splits 
and drops the buffered one.
   
   `setCurrentReader` moves a source's splits out of `restoredSplits` when it 
switches to that source, and `addSplits` sends everything to the current reader 
once one exists, so `restoredSplits` never holds a split of the current source. 
Appending it in both cases therefore cannot duplicate a split:
   
   ```suggestion
           List<HybridSourceSplit> splits = new ArrayList<>();
           if (currentReader != null) {
               splits.addAll(
                       HybridSourceSplit.wrapSplits(
                               currentReader.snapshotState(checkpointId),
                               currentSourceIndex,
                               switchedSources));
           }
           // Restored splits of a source this reader has not switched to yet.
           splits.addAll(restoredSplits);
           return splits;
   ```
   
   A test for that case, in the style of this file (it needs an import of 
`java.util.Arrays`):
   
   ```java
   @Test
   void testReaderRecoverySnapshotKeepsRestoredSplitsOfLaterSource() throws 
Exception {
       TestingReaderContext readerContext = new TestingReaderContext();
       MockBaseSource source = new MockBaseSource(1, 1, Boundedness.BOUNDED);
       SwitchedSources switchedSources = new SwitchedSources();
       switchedSources.put(0, source);
       switchedSources.put(1, source);
       HybridSourceSplit firstSourceSplit =
               HybridSourceSplit.wrapSplit(
                       new MockSourceSplit(0, 0, Integer.MAX_VALUE), 0, 
switchedSources);
       HybridSourceSplit secondSourceSplit =
               HybridSourceSplit.wrapSplit(
                       new MockSourceSplit(1, 0, Integer.MAX_VALUE), 1, 
switchedSources);
   
       // A reader restored onto the first source still buffers the second 
source's split.
       HybridSourceReader<Integer> reader = new 
HybridSourceReader<>(readerContext);
       reader.addSplits(Arrays.asList(firstSourceSplit, secondSourceSplit));
       reader.start();
       assertAndClearSourceReaderFinishedEvent(readerContext, -1);
       reader.handleSourceEvents(new SwitchSourceEvent(0, source, false));
       assertThat(currentReader(reader)).isNotNull();
   
       assertThat(reader.snapshotState(1)).contains(secondSourceSplit);
   
       reader.close();
   }
   ```
   
   I ran `HybridSourceReaderTest` from this PR's head (d505f902) with that test 
added, compiling only `HybridSourceReader` and resolving the rest against the 
2.2.1 artifacts: with the PR as is, the new test fails and the other seven 
pass; with the suggestion applied, all eight pass, including 
`testReaderRecoverySnapshotBeforeSwitchSourceEvent`. This covers only the 
snapshot; I have not checked end to end what a job restored from such a 
checkpoint then does with the later source's split.



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