e-mhui commented on code in PR #7416:
URL: https://github.com/apache/inlong/pull/7416#discussion_r1117847077


##########
inlong-sort/sort-connectors/cdc-base/src/main/java/org/apache/inlong/sort/cdc/base/source/reader/IncrementalSourceRecordEmitter.java:
##########
@@ -0,0 +1,177 @@
+/*
+ * 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.inlong.sort.cdc.base.source.reader;
+
+import static 
com.ververica.cdc.connectors.base.source.meta.wartermark.WatermarkEvent.isHighWatermarkEvent;
+import static 
com.ververica.cdc.connectors.base.source.meta.wartermark.WatermarkEvent.isWatermarkEvent;
+import static 
com.ververica.cdc.connectors.base.utils.SourceRecordUtils.getFetchTimestamp;
+import static 
com.ververica.cdc.connectors.base.utils.SourceRecordUtils.getHistoryRecord;
+import static 
com.ververica.cdc.connectors.base.utils.SourceRecordUtils.getMessageTimestamp;
+import static 
com.ververica.cdc.connectors.base.utils.SourceRecordUtils.isDataChangeRecord;
+import static 
com.ververica.cdc.connectors.base.utils.SourceRecordUtils.isSchemaChangeEvent;
+
+import com.ververica.cdc.connectors.base.source.meta.offset.Offset;
+import com.ververica.cdc.connectors.base.source.meta.offset.OffsetFactory;
+import com.ververica.cdc.connectors.base.source.meta.split.SourceRecords;
+import com.ververica.cdc.connectors.base.source.meta.split.SourceSplitState;
+import com.ververica.cdc.connectors.base.source.metrics.SourceReaderMetrics;
+import com.ververica.cdc.connectors.base.source.reader.IncrementalSourceReader;
+import io.debezium.document.Array;
+import io.debezium.relational.history.HistoryRecord;
+import io.debezium.relational.history.TableChanges;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import org.apache.flink.api.connector.source.SourceOutput;
+import org.apache.flink.connector.base.source.reader.RecordEmitter;
+import org.apache.flink.util.Collector;
+import org.apache.inlong.sort.cdc.base.debezium.DebeziumDeserializationSchema;
+import 
org.apache.inlong.sort.cdc.base.debezium.history.FlinkJsonTableChangeSerializer;
+import org.apache.kafka.connect.source.SourceRecord;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * The {@link RecordEmitter} implementation for {@link 
IncrementalSourceReader}.
+ *
+ * <p>The {@link RecordEmitter} buffers the snapshot records of split and call 
the stream reader to
+ * emit records rather than emit the records directly.
+ * Copy from com.ververica:flink-cdc-base:2.3.0
+ */
+public class IncrementalSourceRecordEmitter<T>
+        implements
+            RecordEmitter<SourceRecords, T, SourceSplitState> {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(IncrementalSourceRecordEmitter.class);
+    private static final FlinkJsonTableChangeSerializer 
TABLE_CHANGE_SERIALIZER =
+            new FlinkJsonTableChangeSerializer();
+
+    protected final DebeziumDeserializationSchema<T> 
debeziumDeserializationSchema;
+    protected final SourceReaderMetrics sourceReaderMetrics;
+    protected final boolean includeSchemaChanges;
+    protected final OutputCollector<T> outputCollector;
+    protected final OffsetFactory offsetFactory;
+
+    public IncrementalSourceRecordEmitter(
+            DebeziumDeserializationSchema<T> debeziumDeserializationSchema,
+            SourceReaderMetrics sourceReaderMetrics,
+            boolean includeSchemaChanges,
+            OffsetFactory offsetFactory) {
+        this.debeziumDeserializationSchema = debeziumDeserializationSchema;
+        this.sourceReaderMetrics = sourceReaderMetrics;
+        this.includeSchemaChanges = includeSchemaChanges;
+        this.outputCollector = new OutputCollector<>();
+        this.offsetFactory = offsetFactory;
+    }
+
+    @Override
+    public void emitRecord(
+            SourceRecords sourceRecords, SourceOutput<T> output, 
SourceSplitState splitState)
+            throws Exception {
+        final Iterator<SourceRecord> elementIterator = 
sourceRecords.iterator();
+        while (elementIterator.hasNext()) {
+            processElement(elementIterator.next(), output, splitState);
+        }
+    }
+
+    protected void processElement(
+            SourceRecord element, SourceOutput<T> output, SourceSplitState 
splitState)
+            throws Exception {
+        if (isWatermarkEvent(element)) {
+            Offset watermark = getWatermark(element);
+            if (isHighWatermarkEvent(element) && 
splitState.isSnapshotSplitState()) {
+                splitState.asSnapshotSplitState().setHighWatermark(watermark);
+            }
+        } else if (isSchemaChangeEvent(element) && 
splitState.isStreamSplitState()) {
+            HistoryRecord historyRecord = getHistoryRecord(element);
+            Array tableChanges =
+                    
historyRecord.document().getArray(HistoryRecord.Fields.TABLE_CHANGES);
+            TableChanges changes = 
TABLE_CHANGE_SERIALIZER.deserialize(tableChanges, true);
+            for (TableChanges.TableChange tableChange : changes) {
+                
splitState.asStreamSplitState().recordSchema(tableChange.getId(), tableChange);
+            }
+            if (includeSchemaChanges) {
+                emitElement(element, output);
+            }
+        } else if (isDataChangeRecord(element)) {
+            if (splitState.isStreamSplitState()) {
+                Offset position = getOffsetPosition(element);
+                splitState.asStreamSplitState().setStartingOffset(position);
+            }
+            reportMetrics(element);

Review Comment:
   `reportMetircs` use to collect the the `latest process time`, the `fetch 
delay time` and the `emit delay time`. If this method is executed after the 
data is successfully sent, the `latest  process time` will be inaccurate.
   
   ```java
   
       protected void reportMetrics(SourceRecord element) {
           long now = System.currentTimeMillis();
           // record the latest process time
           sourceReaderMetrics.recordProcessTime(now);
           Long messageTimestamp = getMessageTimestamp(element);
   
           if (messageTimestamp != null && messageTimestamp > 0L) {
               // report fetch delay
               Long fetchTimestamp = getFetchTimestamp(element);
               if (fetchTimestamp != null) {
                   sourceReaderMetrics.recordFetchDelay(fetchTimestamp - 
messageTimestamp);
               }
               // report emit delay
               sourceReaderMetrics.recordEmitDelay(now - messageTimestamp);
           }
       }
   
   ```



-- 
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: commits-unsubscr...@inlong.apache.org

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

Reply via email to