martongreber commented on code in PR #8:
URL: 
https://github.com/apache/flink-connector-kudu/pull/8#discussion_r1977724995


##########
flink-connector-kudu/src/main/java/org/apache/flink/connector/kudu/source/enumerator/KuduSourceEnumerator.java:
##########
@@ -0,0 +1,256 @@
+/*
+ * 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.kudu.source.enumerator;
+
+import org.apache.flink.api.connector.source.Boundedness;
+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.connector.kudu.connector.KuduTableInfo;
+import org.apache.flink.connector.kudu.connector.reader.KuduReaderConfig;
+import 
org.apache.flink.connector.kudu.source.config.ContinuousBoundingSettings;
+import org.apache.flink.connector.kudu.source.split.KuduSourceSplit;
+import org.apache.flink.connector.kudu.source.split.SplitFinishedEvent;
+
+import org.apache.kudu.util.HybridTimeUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.Nullable;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.concurrent.TimeUnit;
+import java.util.function.Supplier;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+
+/**
+ * The Kudu source enumerator is responsible for periodically discovering and 
assigning new splits
+ * when possible.
+ *
+ * <p>To provide CDC-like functionality, the enumeration works as follows: 
Initially, we perform a
+ * snapshot read of the table and mark the snapshot time as t0. From that 
point onward, we perform
+ * differential scans in the time intervals t0 - t1, t1 - t2, and so on.
+ *
+ * <p>This approach means that new splits can only be enumerated once the 
current time range is
+ * fully processed.
+ *
+ * <p>The process is controlled as follows:
+ *
+ * <ul>
+ *   <li>Once a set of splits is enumerated for a time range, we track:
+ *       <ul>
+ *         <li><b>Unassigned splits</b>: Discovered but not yet assigned to 
readers.
+ *         <li><b>Pending splits</b>: Assigned but not yet fully processed.
+ *       </ul>
+ *   <li>A new set of splits is generated only when there are no remaining 
unassigned or pending
+ *       splits.
+ * </ul>
+ */
+public class KuduSourceEnumerator
+        implements SplitEnumerator<KuduSourceSplit, KuduSourceEnumeratorState> 
{
+    private static final Logger LOG = 
LoggerFactory.getLogger(KuduSourceEnumerator.class);
+
+    private final SplitEnumeratorContext<KuduSourceSplit> context;
+    private final List<Integer> readersAwaitingSplit;
+    private final KuduSplitGenerator splitGenerator;
+    private final ContinuousBoundingSettings continuousBoundingSettings;
+
+    private long lastEndTimestamp;
+    private final List<KuduSourceSplit> unassigned;
+    private final List<KuduSourceSplit> pending;
+
+    public KuduSourceEnumerator(
+            KuduTableInfo tableInfo,
+            KuduReaderConfig readerConfig,
+            ContinuousBoundingSettings continuousBoundingSettings,
+            SplitEnumeratorContext<KuduSourceSplit> context) {
+        this(
+                tableInfo,
+                readerConfig,
+                continuousBoundingSettings,
+                context,
+                KuduSourceEnumeratorState.empty());
+    }
+
+    public KuduSourceEnumerator(
+            KuduTableInfo tableInfo,
+            KuduReaderConfig readerConfig,
+            ContinuousBoundingSettings continuousBoundingSettings,
+            SplitEnumeratorContext<KuduSourceSplit> context,
+            KuduSourceEnumeratorState enumState) {
+        this.continuousBoundingSettings = continuousBoundingSettings;
+        this.context = checkNotNull(context);
+        this.readersAwaitingSplit = new ArrayList<>();
+        this.unassigned = enumState.getUnassigned();
+        this.pending = enumState.getPending();
+        this.splitGenerator = new KuduSplitGenerator(readerConfig, tableInfo);
+        this.lastEndTimestamp = enumState.getLastEndTimestamp();
+    }
+
+    @Override
+    public void start() {
+        if (continuousBoundingSettings.getBoundedness() == 
Boundedness.CONTINUOUS_UNBOUNDED
+                && Objects.nonNull(continuousBoundingSettings.getPeriod())) {
+            context.callAsync(
+                    () -> enumerateNewSplits(() -> pending.isEmpty() && 
unassigned.isEmpty()),
+                    this::assignSplits,
+                    0,
+                    continuousBoundingSettings.getPeriod().toMillis());
+        } else {
+            List<KuduSourceSplit> splits = enumerateNewSplits(() -> true);
+            if (splits != null) {
+                unassigned.addAll(enumerateNewSplits(() -> true));
+                assignSplitsToReaders();
+            }
+        }
+    }
+
+    @Override
+    public void handleSplitRequest(int subtaskId, @Nullable String 
requesterHostname) {
+        readersAwaitingSplit.add(subtaskId);
+        assignSplitsToReaders();
+    }
+
+    @Override
+    public void addSplitsBack(List<KuduSourceSplit> splits, int subtaskId) {
+        LOG.debug("File Source Enumerator adds splits back: {}", splits);

Review Comment:
   done.



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

Reply via email to