You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@inlong.apache.org by "doleyzi (via GitHub)" <gi...@apache.org> on 2024/04/09 09:00:21 UTC
[PR] [INLONG-9943][Audit] Audit-service add codes of jdbc sink [inlong]
doleyzi opened a new pull request, #9949:
URL: https://github.com/apache/inlong/pull/9949
### Prepare a Pull Request
- Fixes #9943
### Motivation
*Audit-service add codes of jdbc sink for aggregate the data from the data source and store the aggregated data to the target storage.*
### Modifications
*Implement data conversion from clickhouse to mysql and mysql to mysql.*
--
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
Re: [PR] [INLONG-9943][Audit] Audit-service add codes of jdbc sink [inlong]
Posted by "justinwwhuang (via GitHub)" <gi...@apache.org>.
justinwwhuang commented on code in PR #9949:
URL: https://github.com/apache/inlong/pull/9949#discussion_r1557283674
##########
inlong-audit/audit-service/src/main/java/org/apache/inlong/audit/sink/JdbcSink.java:
##########
@@ -0,0 +1,164 @@
+/*
+ * 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.audit.sink;
+
+import org.apache.inlong.audit.channel.DataQueue;
+import org.apache.inlong.audit.config.Configuration;
+import org.apache.inlong.audit.entities.SinkConfig;
+import org.apache.inlong.audit.entities.StatData;
+
+import com.zaxxer.hikari.HikariConfig;
+import com.zaxxer.hikari.HikariDataSource;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.sql.DataSource;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+
+import static org.apache.inlong.audit.config.ConfigConstants.CACHE_PREP_STMTS;
+import static org.apache.inlong.audit.config.ConfigConstants.DEFAULT_CACHE_PREP_STMTS;
+import static org.apache.inlong.audit.config.ConfigConstants.DEFAULT_CONNECTION_TIMEOUT;
+import static org.apache.inlong.audit.config.ConfigConstants.DEFAULT_DATASOURCE_POOL_SIZE;
+import static org.apache.inlong.audit.config.ConfigConstants.DEFAULT_PREP_STMT_CACHE_SIZE;
+import static org.apache.inlong.audit.config.ConfigConstants.DEFAULT_PREP_STMT_CACHE_SQL_LIMIT;
+import static org.apache.inlong.audit.config.ConfigConstants.DEFAULT_QUEUE_PULL_TIMEOUT;
+import static org.apache.inlong.audit.config.ConfigConstants.DEFAULT_SOURCE_DB_SINK_BATCH;
+import static org.apache.inlong.audit.config.ConfigConstants.DEFAULT_SOURCE_DB_SINK_INTERVAL;
+import static org.apache.inlong.audit.config.ConfigConstants.KEY_CACHE_PREP_STMTS;
+import static org.apache.inlong.audit.config.ConfigConstants.KEY_DATASOURCE_CONNECTION_TIMEOUT;
+import static org.apache.inlong.audit.config.ConfigConstants.KEY_DATASOURCE_POOL_SIZE;
+import static org.apache.inlong.audit.config.ConfigConstants.KEY_PREP_STMT_CACHE_SIZE;
+import static org.apache.inlong.audit.config.ConfigConstants.KEY_PREP_STMT_CACHE_SQL_LIMIT;
+import static org.apache.inlong.audit.config.ConfigConstants.KEY_QUEUE_PULL_TIMEOUT;
+import static org.apache.inlong.audit.config.ConfigConstants.KEY_SOURCE_DB_SINK_BATCH;
+import static org.apache.inlong.audit.config.ConfigConstants.KEY_SOURCE_DB_SINK_INTERVAL;
+import static org.apache.inlong.audit.config.ConfigConstants.PREP_STMT_CACHE_SIZE;
+import static org.apache.inlong.audit.config.ConfigConstants.PREP_STMT_CACHE_SQL_LIMIT;
+
+/**
+ * Jdbc sink
+ */
+public class JdbcSink implements AutoCloseable {
+
+ private static final Logger LOG = LoggerFactory.getLogger(JdbcSink.class);
+ private final ScheduledExecutorService sinkTimer = Executors.newSingleThreadScheduledExecutor();
+ private final DataQueue dataQueue;
+ private final int insertBatch;
+ private final int pullTimeOut;
+ private final SinkConfig sinkConfig;
+ private DataSource dataSource;
+
+ public JdbcSink(DataQueue dataQueue, SinkConfig sinkConfig) {
+ this.dataQueue = dataQueue;
+ this.sinkConfig = sinkConfig;
+
+ insertBatch = Configuration.getInstance().get(KEY_SOURCE_DB_SINK_BATCH,
+ DEFAULT_SOURCE_DB_SINK_BATCH);
+
+ pullTimeOut = Configuration.getInstance().get(KEY_QUEUE_PULL_TIMEOUT,
+ DEFAULT_QUEUE_PULL_TIMEOUT);
+ }
+
+ /**
+ * start
+ */
+ public void start() {
+ createDataSource();
+
+ sinkTimer.scheduleWithFixedDelay(this::process,
+ 0,
+ Configuration.getInstance().get(KEY_SOURCE_DB_SINK_INTERVAL,
+ DEFAULT_SOURCE_DB_SINK_INTERVAL),
+ TimeUnit.MILLISECONDS);
+ }
+
+ /**
+ * Process
+ */
+ private void process() {
+ try (Connection connection = dataSource.getConnection();
+ PreparedStatement preparedStatement = connection.prepareStatement(sinkConfig.getInsertSql())) {
+ if (connection.isClosed()) {
+ createDataSource();
+ }
+ int counter = 0;
+ StatData data = dataQueue.pull(pullTimeOut, TimeUnit.MICROSECONDS);
+ while (data != null) {
+ preparedStatement.setString(1, data.getLogTs());
+ preparedStatement.setString(2, data.getInlongGroupId());
+ preparedStatement.setString(3, data.getInlongStreamId());
+ preparedStatement.setString(4, data.getAuditId());
+ preparedStatement.setString(5, data.getAuditTag());
+ preparedStatement.setLong(6, data.getCount());
+ preparedStatement.setLong(7, data.getSize());
+ preparedStatement.setLong(8, data.getDelay());
+ preparedStatement.addBatch();
+
+ if (++counter >= insertBatch) {
+ preparedStatement.executeBatch();
+ preparedStatement.clearBatch();
+ counter = 0;
+ }
+ data = dataQueue.pull(pullTimeOut, TimeUnit.MICROSECONDS);
+ }
+ if (counter > 0) {
+ preparedStatement.executeBatch();
Review Comment:
If the connection is disconnected, will there be a problem here
--
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
Re: [PR] [INLONG-9943][Audit] Audit-service add codes of jdbc sink [inlong]
Posted by "aloyszhang (via GitHub)" <gi...@apache.org>.
aloyszhang commented on code in PR #9949:
URL: https://github.com/apache/inlong/pull/9949#discussion_r1557300732
##########
inlong-audit/audit-service/src/main/java/org/apache/inlong/audit/entities/SinkConfig.java:
##########
@@ -0,0 +1,45 @@
+/*
+ * 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.audit.entities;
+
+import lombok.Data;
+
+/**
+ * Source config
+ */
+@Data
+public class SinkConfig {
+
+ private String insertSql;
+ private final String driverClassName;
+ private final String jdbcUrl;
+ private final String username;
+ private final String password;
+
+ public SinkConfig(String insertSql,
+ String driverClassName,
+ String jdbcUrl,
+ String username,
+ String password) {
+ this.insertSql = insertSql;
+ this.driverClassName = driverClassName;
+ this.jdbcUrl = jdbcUrl;
+ this.username = username;
+ this.password = password;
+ }
Review Comment:
can be replaced with `lombok.AllArgsConstructor` annotation
--
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
Re: [PR] [INLONG-9943][Audit] Audit-service add codes of jdbc sink [inlong]
Posted by "luchunliang (via GitHub)" <gi...@apache.org>.
luchunliang merged PR #9949:
URL: https://github.com/apache/inlong/pull/9949
--
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
Re: [PR] [INLONG-9943][Audit] Audit-service add codes of jdbc sink [inlong]
Posted by "doleyzi (via GitHub)" <gi...@apache.org>.
doleyzi commented on code in PR #9949:
URL: https://github.com/apache/inlong/pull/9949#discussion_r1557309491
##########
inlong-audit/audit-service/src/main/java/org/apache/inlong/audit/sink/JdbcSink.java:
##########
@@ -0,0 +1,164 @@
+/*
+ * 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.audit.sink;
+
+import org.apache.inlong.audit.channel.DataQueue;
+import org.apache.inlong.audit.config.Configuration;
+import org.apache.inlong.audit.entities.SinkConfig;
+import org.apache.inlong.audit.entities.StatData;
+
+import com.zaxxer.hikari.HikariConfig;
+import com.zaxxer.hikari.HikariDataSource;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.sql.DataSource;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+
+import static org.apache.inlong.audit.config.ConfigConstants.CACHE_PREP_STMTS;
+import static org.apache.inlong.audit.config.ConfigConstants.DEFAULT_CACHE_PREP_STMTS;
+import static org.apache.inlong.audit.config.ConfigConstants.DEFAULT_CONNECTION_TIMEOUT;
+import static org.apache.inlong.audit.config.ConfigConstants.DEFAULT_DATASOURCE_POOL_SIZE;
+import static org.apache.inlong.audit.config.ConfigConstants.DEFAULT_PREP_STMT_CACHE_SIZE;
+import static org.apache.inlong.audit.config.ConfigConstants.DEFAULT_PREP_STMT_CACHE_SQL_LIMIT;
+import static org.apache.inlong.audit.config.ConfigConstants.DEFAULT_QUEUE_PULL_TIMEOUT;
+import static org.apache.inlong.audit.config.ConfigConstants.DEFAULT_SOURCE_DB_SINK_BATCH;
+import static org.apache.inlong.audit.config.ConfigConstants.DEFAULT_SOURCE_DB_SINK_INTERVAL;
+import static org.apache.inlong.audit.config.ConfigConstants.KEY_CACHE_PREP_STMTS;
+import static org.apache.inlong.audit.config.ConfigConstants.KEY_DATASOURCE_CONNECTION_TIMEOUT;
+import static org.apache.inlong.audit.config.ConfigConstants.KEY_DATASOURCE_POOL_SIZE;
+import static org.apache.inlong.audit.config.ConfigConstants.KEY_PREP_STMT_CACHE_SIZE;
+import static org.apache.inlong.audit.config.ConfigConstants.KEY_PREP_STMT_CACHE_SQL_LIMIT;
+import static org.apache.inlong.audit.config.ConfigConstants.KEY_QUEUE_PULL_TIMEOUT;
+import static org.apache.inlong.audit.config.ConfigConstants.KEY_SOURCE_DB_SINK_BATCH;
+import static org.apache.inlong.audit.config.ConfigConstants.KEY_SOURCE_DB_SINK_INTERVAL;
+import static org.apache.inlong.audit.config.ConfigConstants.PREP_STMT_CACHE_SIZE;
+import static org.apache.inlong.audit.config.ConfigConstants.PREP_STMT_CACHE_SQL_LIMIT;
+
+/**
+ * Jdbc sink
+ */
+public class JdbcSink implements AutoCloseable {
+
+ private static final Logger LOG = LoggerFactory.getLogger(JdbcSink.class);
+ private final ScheduledExecutorService sinkTimer = Executors.newSingleThreadScheduledExecutor();
+ private final DataQueue dataQueue;
+ private final int insertBatch;
+ private final int pullTimeOut;
+ private final SinkConfig sinkConfig;
+ private DataSource dataSource;
+
+ public JdbcSink(DataQueue dataQueue, SinkConfig sinkConfig) {
+ this.dataQueue = dataQueue;
+ this.sinkConfig = sinkConfig;
+
+ insertBatch = Configuration.getInstance().get(KEY_SOURCE_DB_SINK_BATCH,
+ DEFAULT_SOURCE_DB_SINK_BATCH);
+
+ pullTimeOut = Configuration.getInstance().get(KEY_QUEUE_PULL_TIMEOUT,
+ DEFAULT_QUEUE_PULL_TIMEOUT);
+ }
+
+ /**
+ * start
+ */
+ public void start() {
+ createDataSource();
+
+ sinkTimer.scheduleWithFixedDelay(this::process,
+ 0,
+ Configuration.getInstance().get(KEY_SOURCE_DB_SINK_INTERVAL,
+ DEFAULT_SOURCE_DB_SINK_INTERVAL),
+ TimeUnit.MILLISECONDS);
+ }
+
+ /**
+ * Process
+ */
+ private void process() {
+ try (Connection connection = dataSource.getConnection();
+ PreparedStatement preparedStatement = connection.prepareStatement(sinkConfig.getInsertSql())) {
+ if (connection.isClosed()) {
+ createDataSource();
+ }
+ int counter = 0;
+ StatData data = dataQueue.pull(pullTimeOut, TimeUnit.MICROSECONDS);
+ while (data != null) {
+ preparedStatement.setString(1, data.getLogTs());
+ preparedStatement.setString(2, data.getInlongGroupId());
+ preparedStatement.setString(3, data.getInlongStreamId());
+ preparedStatement.setString(4, data.getAuditId());
+ preparedStatement.setString(5, data.getAuditTag());
+ preparedStatement.setLong(6, data.getCount());
+ preparedStatement.setLong(7, data.getSize());
+ preparedStatement.setLong(8, data.getDelay());
+ preparedStatement.addBatch();
+
+ if (++counter >= insertBatch) {
+ preparedStatement.executeBatch();
+ preparedStatement.clearBatch();
+ counter = 0;
+ }
+ data = dataQueue.pull(pullTimeOut, TimeUnit.MICROSECONDS);
+ }
+ if (counter > 0) {
+ preparedStatement.executeBatch();
Review Comment:
Each cycle uses the latest data to overwrite the old data, so even if the connection is disconnected, it will not be affected.
--
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