You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@inlong.apache.org by GitBox <gi...@apache.org> on 2023/01/17 06:23:43 UTC

[GitHub] [inlong] gong commented on a diff in pull request #7253: [INLONG-7249][Sort] JDBC accurate dirty data archive and metric calculation

gong commented on code in PR #7253:
URL: https://github.com/apache/inlong/pull/7253#discussion_r1071781406


##########
inlong-sort/sort-connectors/jdbc/src/main/java/org/apache/inlong/sort/jdbc/internal/TableMetricStatementExecutor.java:
##########
@@ -0,0 +1,172 @@
+/*
+ * 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.jdbc.internal;
+
+import org.apache.flink.connector.jdbc.internal.converter.JdbcRowConverter;
+import org.apache.flink.connector.jdbc.internal.executor.JdbcBatchStatementExecutor;
+import org.apache.flink.connector.jdbc.statement.FieldNamedPreparedStatement;
+import org.apache.flink.connector.jdbc.statement.StatementFactory;
+import org.apache.flink.table.data.RowData;
+import org.apache.inlong.sort.base.dirty.DirtySinkHelper;
+import org.apache.inlong.sort.base.dirty.DirtyType;
+import org.apache.inlong.sort.base.metric.SinkMetricData;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.nio.charset.StandardCharsets;
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+
+/**
+ * A {@link JdbcBatchStatementExecutor} that simply adds the records into batches of {@link
+ * java.sql.PreparedStatement} and doesn't buffer records in memory. Only used in Table/SQL API.
+ */
+public final class TableMetricStatementExecutor implements JdbcBatchStatementExecutor<RowData> {
+
+    private static final Logger LOG = LoggerFactory.getLogger(TableMetricStatementExecutor.class);
+    private final StatementFactory stmtFactory;
+    private final JdbcRowConverter converter;
+    private final List<RowData> batch;
+    private final DirtySinkHelper<Object> dirtySinkHelper;
+    private final SinkMetricData sinkMetricData;
+    private final AtomicInteger counter = new AtomicInteger();
+    private transient FieldNamedPreparedStatement st;
+
+    /**
+     * Keep in mind object reuse: if it's on then key extractor may be required to return new
+     * object.
+     */
+    public TableMetricStatementExecutor(StatementFactory stmtFactory, JdbcRowConverter converter,
+            DirtySinkHelper<Object> dirtySinkHelper, SinkMetricData sinkMetricData) {
+        this.stmtFactory = checkNotNull(stmtFactory);
+        this.converter = checkNotNull(converter);
+        this.batch = new ArrayList<>();
+        this.dirtySinkHelper = dirtySinkHelper;
+        this.sinkMetricData = sinkMetricData;
+    }
+
+    /**
+     * parses an SQL exception message, and returns dirty records
+     *
+     * @param e the exception
+     * @return a string that can be compared with the return value of other parseRecord calls
+     */
+    public static void parseRecord(Exception e, List<Integer> answer) {
+        final Pattern pattern = Pattern.compile("Batch entry (\\d+) ");
+        Matcher matcher = pattern.matcher(e.getMessage());
+        if (matcher.find()) {
+            answer.add(Integer.parseInt(matcher.group(1)));
+        }
+        // if e is sql exciption, identify all dirty data, else identify only one dirty data.
+        if (e instanceof SQLException) {
+            SQLException next = ((SQLException) e).getNextException();
+            if (next != null) {
+                parseRecord(next, answer);
+            }
+        }
+    }
+
+    @Override
+    public void prepareStatements(Connection connection) throws SQLException {
+        st = stmtFactory.createStatement(connection);
+    }
+
+    @Override
+    public void addToBatch(RowData record) throws SQLException {
+        batch.add(record);
+        converter.toExternal(record, st);
+        st.addBatch();
+    }
+
+    @Override
+    public void executeBatch() throws SQLException {
+        try {
+            st.executeBatch();
+            addMetrics();
+        } catch (SQLException e) {
+            if (counter.incrementAndGet() == 3) {
+                // parse record from error and handle
+                List<Integer> dirtyRecords = new ArrayList<>();
+                parseRecord(e, dirtyRecords);
+                handleDirty(dirtyRecords);
+                counter.set(0);
+            } else {
+                throw new SQLException(e);
+            }
+        }
+    }
+
+    private void addMetrics() {
+        Set<RowData> set = new HashSet<>();
+        int rowCount = 0;
+        long rowSize = 0;
+        for (RowData record : batch) {
+            if (!set.contains(record)) {
+                set.add(record);
+                rowCount++;
+                rowSize += record.toString().getBytes(StandardCharsets.UTF_8).length;
+            }
+        }
+        sinkMetricData.invoke(rowCount, rowSize * 8);

Review Comment:
   `*8` why?



-- 
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-unsubscribe@inlong.apache.org

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