stevenzwu commented on a change in pull request #15924:
URL: https://github.com/apache/flink/pull/15924#discussion_r641282961



##########
File path: 
flink-connectors/flink-connector-base/src/main/java/org/apache/flink/connector/base/source/hybrid/HybridSourceSplitEnumerator.java
##########
@@ -0,0 +1,406 @@
+/*
+ * 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.hybrid;
+
+import org.apache.flink.api.connector.source.ReaderInfo;
+import org.apache.flink.api.connector.source.Source;
+import org.apache.flink.api.connector.source.SourceEvent;
+import org.apache.flink.api.connector.source.SourceSplit;
+import org.apache.flink.api.connector.source.SplitEnumerator;
+import org.apache.flink.api.connector.source.SplitEnumeratorContext;
+import org.apache.flink.api.connector.source.SplitsAssignment;
+import org.apache.flink.metrics.MetricGroup;
+import org.apache.flink.util.Preconditions;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.TreeMap;
+import java.util.concurrent.Callable;
+import java.util.function.BiConsumer;
+
+/**
+ * Wraps the actual split enumerators and facilitates source switching. 
Enumerators are created
+ * lazily when source switch occurs to support runtime position conversion.
+ *
+ * <p>This enumerator delegates to the current underlying split enumerator and 
transitions to the
+ * next source once all readers have indicated via {@link 
SourceReaderFinishedEvent} that all input
+ * was consumed.
+ *
+ * <p>Switching between enumerators occurs by either creating the new 
enumerator with a fixed start
+ * position via {@link Source#createEnumerator(SplitEnumeratorContext)} or by 
using the state of the
+ * previous enumerator and the optional user supplied checkpoint state 
converter to create the next
+ * enumerator with a runtime determined start position via {@link
+ * Source#restoreEnumerator(SplitEnumeratorContext, Object)}.
+ *
+ * <p>During subtask recovery, splits that have been assigned since the last 
checkpoint will be
+ * added back by the source coordinator. This requires the enumerator to 
potentially suspend the
+ * current enumerator, activate previous readers and process the split backlog 
following the
+ * original assignment before resuming with the current underlying enumerator.
+ */
+public class HybridSourceSplitEnumerator<SplitT extends SourceSplit>
+        implements SplitEnumerator<HybridSourceSplit<SplitT>, 
HybridSourceEnumeratorState> {
+    private static final Logger LOG = 
LoggerFactory.getLogger(HybridSourceSplitEnumerator.class);
+
+    private final SplitEnumeratorContext<HybridSourceSplit> context;
+    private final HybridSource.SourceChain<?, SplitT, Object> sourceChain;
+    // TODO: SourceCoordinatorContext does not provide access to current 
assignments
+    private final Map<Integer, List<HybridSourceSplit<SplitT>>> assignments;
+    private final Map<Integer, TreeMap<Integer, 
List<HybridSourceSplit<SplitT>>>> pendingSplits;
+    private final HashSet<Integer> pendingReaders;
+    private int currentSourceIndex;
+    private SplitEnumerator<SplitT, Object> currentEnumerator;
+
+    public HybridSourceSplitEnumerator(
+            SplitEnumeratorContext<HybridSourceSplit> context,
+            HybridSource.SourceChain<?, SplitT, Object> sourceChain) {
+        this(context, sourceChain, 0);
+    }
+
+    public HybridSourceSplitEnumerator(
+            SplitEnumeratorContext<HybridSourceSplit> context,
+            HybridSource.SourceChain<?, SplitT, Object> sourceChain,
+            int initialSourceIndex) {
+        Preconditions.checkArgument(initialSourceIndex < 
sourceChain.sources.size());
+        this.context = context;
+        this.sourceChain = sourceChain;
+        this.currentSourceIndex = initialSourceIndex;
+        this.assignments = new HashMap<>();
+        this.pendingSplits = new HashMap<>();
+        this.pendingReaders = new HashSet<>();
+    }
+
+    @Override
+    public void start() {
+        switchEnumerator();
+    }
+
+    @Override
+    public void handleSplitRequest(int subtaskId, String requesterHostname) {
+        LOG.debug(
+                "handleSplitRequest subtask={} sourceIndex={} 
pendingSplits={}",
+                subtaskId,
+                currentSourceIndex,
+                pendingSplits);
+        // TODO: test coverage for on demand split assignment
+        assignPendingSplits(subtaskId);
+        currentEnumerator.handleSplitRequest(subtaskId, requesterHostname);
+    }
+
+    @Override
+    public void addSplitsBack(List<HybridSourceSplit<SplitT>> splits, int 
subtaskId) {
+        LOG.debug("Adding splits back for subtask={} {}", subtaskId, splits);
+        // Splits returned can belong to multiple sources, after switching 
since last checkpoint
+        TreeMap<Integer, List<HybridSourceSplit<SplitT>>> splitsBySourceIndex 
= new TreeMap<>();
+
+        for (HybridSourceSplit<SplitT> split : splits) {
+            splitsBySourceIndex
+                    .computeIfAbsent(split.sourceIndex(), k -> new 
ArrayList<>())
+                    .add(split);
+        }
+
+        splitsBySourceIndex.forEach(
+                (k, splitsPerSource) -> {
+                    if (k == currentSourceIndex) {
+                        currentEnumerator.addSplitsBack(
+                                
HybridSourceReader.unwrapSplits(splitsPerSource), subtaskId);
+                    } else {
+                        pendingSplits
+                                .computeIfAbsent(subtaskId, sourceIndex -> new 
TreeMap<>())
+                                .put(k, splitsPerSource);
+                        if 
(context.registeredReaders().containsKey(subtaskId)) {
+                            assignPendingSplits(subtaskId);
+                        }
+                    }
+                });
+    }
+
+    @Override
+    public void addReader(int subtaskId) {
+        LOG.debug("addReader subtaskId={}", subtaskId);
+        if (pendingSplits.isEmpty()) {
+            context.sendEventToSourceReader(subtaskId, new 
SwitchSourceEvent(currentSourceIndex));
+            LOG.debug("Adding reader {} to enumerator {}", subtaskId, 
currentSourceIndex);
+            currentEnumerator.addReader(subtaskId);
+        } else {
+            // Defer adding reader to the current enumerator until splits 
belonging to earlier
+            // enumerators that were added back have been processed
+            pendingReaders.add(subtaskId);
+            assignPendingSplits(subtaskId);
+        }
+    }
+
+    private void assignPendingSplits(int subtaskId) {
+        TreeMap<Integer, List<HybridSourceSplit<SplitT>>> splitsBySource =
+                pendingSplits.get(subtaskId);
+        if (splitsBySource != null) {
+            int sourceIndex = splitsBySource.firstKey();
+            List<HybridSourceSplit<SplitT>> splits =
+                    
Preconditions.checkNotNull(splitsBySource.get(sourceIndex));
+            Preconditions.checkState(!splits.isEmpty());
+            LOG.debug("Assigning pending splits subtask={} {}", subtaskId, 
splits);
+            context.sendEventToSourceReader(subtaskId, new 
SwitchSourceEvent(sourceIndex));
+            context.assignSplits(
+                    new SplitsAssignment<HybridSourceSplit>(
+                            Collections.singletonMap(subtaskId, (List) 
splits)));
+            context.signalNoMoreSplits(subtaskId);
+            // Empty collection indicates that splits have been assigned
+            splits.clear();
+        }
+    }
+
+    @Override
+    public HybridSourceEnumeratorState snapshotState(long checkpointId) throws 
Exception {
+        Object enumState = currentEnumerator.snapshotState(checkpointId);
+        return new HybridSourceEnumeratorState(currentSourceIndex, enumState);
+    }
+
+    @Override
+    public void handleSourceEvent(int subtaskId, SourceEvent sourceEvent) {
+        if (sourceEvent instanceof SourceReaderFinishedEvent) {
+            SourceReaderFinishedEvent srfe = (SourceReaderFinishedEvent) 
sourceEvent;

Review comment:
       nit: acronym like `srfe` is not very readable. maybe 
`readerFinishedEvent`?




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

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to