vernedeng commented on code in PR #10004: URL: https://github.com/apache/inlong/pull/10004#discussion_r1570079896
########## inlong-audit/audit-service/src/main/java/org/apache/inlong/audit/cache/RealTimeQuery.java: ########## @@ -0,0 +1,312 @@ +/* + * 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.cache; + +import org.apache.inlong.audit.config.Configuration; +import org.apache.inlong.audit.entities.JdbcConfig; +import org.apache.inlong.audit.entities.StatData; +import org.apache.inlong.audit.service.ConfigService; + +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.sql.ResultSet; +import java.sql.SQLException; +import java.util.LinkedList; +import java.util.List; + +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.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.PREP_STMT_CACHE_SIZE; +import static org.apache.inlong.audit.config.ConfigConstants.PREP_STMT_CACHE_SQL_LIMIT; +import static org.apache.inlong.audit.config.SqlConstants.DEFAULT_SOURCE_QUERY_IDS_SQL; +import static org.apache.inlong.audit.config.SqlConstants.DEFAULT_SOURCE_QUERY_IPS_SQL; +import static org.apache.inlong.audit.config.SqlConstants.DEFAULT_SOURCE_QUERY_MINUTE_SQL; +import static org.apache.inlong.audit.config.SqlConstants.KEY_SOURCE_QUERY_IDS_SQL; +import static org.apache.inlong.audit.config.SqlConstants.KEY_SOURCE_QUERY_IPS_SQL; +import static org.apache.inlong.audit.config.SqlConstants.KEY_SOURCE_QUERY_MINUTE_SQL; + +/** + * Real time query data from audit source. + */ +public class RealTimeQuery { + + private static final Logger LOGGER = LoggerFactory.getLogger(RealTimeQuery.class); + private static volatile RealTimeQuery realTimeQuery = null; + + private final List<DataSource> dataSourceList = new LinkedList<>(); + + private final String queryLogTsSql; + private final String queryIdsByIpSql; + private final String queryReportIpsSql; + + private RealTimeQuery() { + List<JdbcConfig> jdbcConfigList = ConfigService.getInstance().getAllAuditSource(); + for (JdbcConfig jdbcConfig : jdbcConfigList) { + dataSourceList.add(createDataSource(jdbcConfig)); + } + + queryLogTsSql = Configuration.getInstance().get(KEY_SOURCE_QUERY_MINUTE_SQL, + DEFAULT_SOURCE_QUERY_MINUTE_SQL); + queryIdsByIpSql = Configuration.getInstance().get(KEY_SOURCE_QUERY_IDS_SQL, + DEFAULT_SOURCE_QUERY_IDS_SQL); + queryReportIpsSql = Configuration.getInstance().get(KEY_SOURCE_QUERY_IPS_SQL, + DEFAULT_SOURCE_QUERY_IPS_SQL); + } + + public static RealTimeQuery getInstance() { + if (realTimeQuery == null) { + synchronized (Configuration.class) { + if (realTimeQuery == null) { + realTimeQuery = new RealTimeQuery(); + } + } + } + return realTimeQuery; + } + + /** + * Create data source. + */ + private DataSource createDataSource(JdbcConfig jdbcConfig) { + HikariConfig config = new HikariConfig(); + config.setDriverClassName(jdbcConfig.getDriverClass()); + config.setJdbcUrl(jdbcConfig.getJdbcUrl()); + config.setUsername(jdbcConfig.getUserName()); + config.setPassword(jdbcConfig.getPassword()); + config.setConnectionTimeout(Configuration.getInstance().get(KEY_DATASOURCE_CONNECTION_TIMEOUT, + DEFAULT_CONNECTION_TIMEOUT)); + config.addDataSourceProperty(CACHE_PREP_STMTS, + Configuration.getInstance().get(KEY_CACHE_PREP_STMTS, DEFAULT_CACHE_PREP_STMTS)); + config.addDataSourceProperty(PREP_STMT_CACHE_SIZE, + Configuration.getInstance().get(KEY_PREP_STMT_CACHE_SIZE, DEFAULT_PREP_STMT_CACHE_SIZE)); + config.addDataSourceProperty(PREP_STMT_CACHE_SQL_LIMIT, + Configuration.getInstance().get(KEY_PREP_STMT_CACHE_SQL_LIMIT, DEFAULT_PREP_STMT_CACHE_SQL_LIMIT)); + config.setMaximumPoolSize( + Configuration.getInstance().get(KEY_DATASOURCE_POOL_SIZE, + DEFAULT_DATASOURCE_POOL_SIZE)); + return new HikariDataSource(config); + } + + /** + * Query the audit data of log time. + * + * @param startTime + * @param endTime + * @param inlongGroupId + * @param inlongStreamId + * @param auditId + * @return + */ + public List<StatData> queryLogTs(String startTime, String endTime, String inlongGroupId, + String inlongStreamId, String auditId) { + List<StatData> statDataList = new LinkedList<>(); + for (DataSource dataSource : dataSourceList) { + statDataList = + doQueryLogTs(dataSource, startTime, endTime, inlongGroupId, inlongStreamId, auditId); + if (!statDataList.isEmpty()) { + break; + } + LOGGER.info("Change another audit source to query data! Params is: {} {} {} {} {}", + startTime, endTime, inlongGroupId, inlongStreamId, auditId); + } + return statDataList; + } + + /** + * Do query the audit data of log time. + * + * @param dataSource + * @param startTime + * @param endTime + * @param inlongGroupId + * @param inlongStreamId + * @param auditId + * @return + */ + private List<StatData> doQueryLogTs(DataSource dataSource, String startTime, String endTime, String inlongGroupId, + String inlongStreamId, String auditId) { + List<StatData> result = new LinkedList<>(); + try (Connection connection = dataSource.getConnection(); + PreparedStatement pstat = connection.prepareStatement(queryLogTsSql)) { + pstat.setString(1, startTime); + pstat.setString(2, endTime); + pstat.setString(3, inlongGroupId); + pstat.setString(4, inlongStreamId); + pstat.setString(5, auditId); + try (ResultSet resultSet = pstat.executeQuery()) { + while (resultSet.next()) { + StatData data = new StatData(); + data.setLogTs(resultSet.getString(1)); + data.setInlongGroupId(resultSet.getString(2)); + data.setInlongStreamId(resultSet.getString(3)); + data.setAuditId(resultSet.getString(4)); + data.setAuditTag(resultSet.getString(5)); + data.setCount(resultSet.getLong(6)); + data.setSize(resultSet.getLong(7)); + data.setDelay(resultSet.getLong(8)); + result.add(data); + } + } catch (SQLException sqlException) { + LOGGER.error("Query has SQL exception! ", sqlException); Review Comment: Please log the specified data source which throws the exception. ########## inlong-audit/audit-service/src/main/java/org/apache/inlong/audit/cache/RealTimeQuery.java: ########## @@ -0,0 +1,312 @@ +/* + * 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.cache; + +import org.apache.inlong.audit.config.Configuration; +import org.apache.inlong.audit.entities.JdbcConfig; +import org.apache.inlong.audit.entities.StatData; +import org.apache.inlong.audit.service.ConfigService; + +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.sql.ResultSet; +import java.sql.SQLException; +import java.util.LinkedList; +import java.util.List; + +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.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.PREP_STMT_CACHE_SIZE; +import static org.apache.inlong.audit.config.ConfigConstants.PREP_STMT_CACHE_SQL_LIMIT; +import static org.apache.inlong.audit.config.SqlConstants.DEFAULT_SOURCE_QUERY_IDS_SQL; +import static org.apache.inlong.audit.config.SqlConstants.DEFAULT_SOURCE_QUERY_IPS_SQL; +import static org.apache.inlong.audit.config.SqlConstants.DEFAULT_SOURCE_QUERY_MINUTE_SQL; +import static org.apache.inlong.audit.config.SqlConstants.KEY_SOURCE_QUERY_IDS_SQL; +import static org.apache.inlong.audit.config.SqlConstants.KEY_SOURCE_QUERY_IPS_SQL; +import static org.apache.inlong.audit.config.SqlConstants.KEY_SOURCE_QUERY_MINUTE_SQL; + +/** + * Real time query data from audit source. + */ +public class RealTimeQuery { + + private static final Logger LOGGER = LoggerFactory.getLogger(RealTimeQuery.class); + private static volatile RealTimeQuery realTimeQuery = null; + + private final List<DataSource> dataSourceList = new LinkedList<>(); + + private final String queryLogTsSql; + private final String queryIdsByIpSql; + private final String queryReportIpsSql; + + private RealTimeQuery() { + List<JdbcConfig> jdbcConfigList = ConfigService.getInstance().getAllAuditSource(); + for (JdbcConfig jdbcConfig : jdbcConfigList) { + dataSourceList.add(createDataSource(jdbcConfig)); + } + + queryLogTsSql = Configuration.getInstance().get(KEY_SOURCE_QUERY_MINUTE_SQL, + DEFAULT_SOURCE_QUERY_MINUTE_SQL); + queryIdsByIpSql = Configuration.getInstance().get(KEY_SOURCE_QUERY_IDS_SQL, + DEFAULT_SOURCE_QUERY_IDS_SQL); + queryReportIpsSql = Configuration.getInstance().get(KEY_SOURCE_QUERY_IPS_SQL, + DEFAULT_SOURCE_QUERY_IPS_SQL); + } + + public static RealTimeQuery getInstance() { + if (realTimeQuery == null) { + synchronized (Configuration.class) { + if (realTimeQuery == null) { + realTimeQuery = new RealTimeQuery(); + } + } + } + return realTimeQuery; + } + + /** + * Create data source. + */ + private DataSource createDataSource(JdbcConfig jdbcConfig) { + HikariConfig config = new HikariConfig(); + config.setDriverClassName(jdbcConfig.getDriverClass()); + config.setJdbcUrl(jdbcConfig.getJdbcUrl()); + config.setUsername(jdbcConfig.getUserName()); + config.setPassword(jdbcConfig.getPassword()); + config.setConnectionTimeout(Configuration.getInstance().get(KEY_DATASOURCE_CONNECTION_TIMEOUT, + DEFAULT_CONNECTION_TIMEOUT)); + config.addDataSourceProperty(CACHE_PREP_STMTS, + Configuration.getInstance().get(KEY_CACHE_PREP_STMTS, DEFAULT_CACHE_PREP_STMTS)); + config.addDataSourceProperty(PREP_STMT_CACHE_SIZE, + Configuration.getInstance().get(KEY_PREP_STMT_CACHE_SIZE, DEFAULT_PREP_STMT_CACHE_SIZE)); + config.addDataSourceProperty(PREP_STMT_CACHE_SQL_LIMIT, + Configuration.getInstance().get(KEY_PREP_STMT_CACHE_SQL_LIMIT, DEFAULT_PREP_STMT_CACHE_SQL_LIMIT)); + config.setMaximumPoolSize( + Configuration.getInstance().get(KEY_DATASOURCE_POOL_SIZE, + DEFAULT_DATASOURCE_POOL_SIZE)); + return new HikariDataSource(config); + } + + /** + * Query the audit data of log time. + * + * @param startTime + * @param endTime + * @param inlongGroupId + * @param inlongStreamId + * @param auditId + * @return + */ + public List<StatData> queryLogTs(String startTime, String endTime, String inlongGroupId, + String inlongStreamId, String auditId) { + List<StatData> statDataList = new LinkedList<>(); + for (DataSource dataSource : dataSourceList) { + statDataList = + doQueryLogTs(dataSource, startTime, endTime, inlongGroupId, inlongStreamId, auditId); + if (!statDataList.isEmpty()) { + break; + } + LOGGER.info("Change another audit source to query data! Params is: {} {} {} {} {}", + startTime, endTime, inlongGroupId, inlongStreamId, auditId); + } + return statDataList; + } + + /** + * Do query the audit data of log time. + * + * @param dataSource + * @param startTime + * @param endTime + * @param inlongGroupId + * @param inlongStreamId + * @param auditId + * @return + */ + private List<StatData> doQueryLogTs(DataSource dataSource, String startTime, String endTime, String inlongGroupId, + String inlongStreamId, String auditId) { + List<StatData> result = new LinkedList<>(); + try (Connection connection = dataSource.getConnection(); + PreparedStatement pstat = connection.prepareStatement(queryLogTsSql)) { + pstat.setString(1, startTime); + pstat.setString(2, endTime); + pstat.setString(3, inlongGroupId); + pstat.setString(4, inlongStreamId); + pstat.setString(5, auditId); + try (ResultSet resultSet = pstat.executeQuery()) { + while (resultSet.next()) { + StatData data = new StatData(); + data.setLogTs(resultSet.getString(1)); + data.setInlongGroupId(resultSet.getString(2)); + data.setInlongStreamId(resultSet.getString(3)); + data.setAuditId(resultSet.getString(4)); + data.setAuditTag(resultSet.getString(5)); + data.setCount(resultSet.getLong(6)); + data.setSize(resultSet.getLong(7)); + data.setDelay(resultSet.getLong(8)); + result.add(data); + } + } catch (SQLException sqlException) { + LOGGER.error("Query has SQL exception! ", sqlException); + } + } catch (Exception exception) { + LOGGER.error("Query has exception! ", exception); Review Comment: ditto ########## inlong-audit/audit-service/src/main/java/org/apache/inlong/audit/cache/RealTimeQuery.java: ########## @@ -0,0 +1,312 @@ +/* + * 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.cache; + +import org.apache.inlong.audit.config.Configuration; +import org.apache.inlong.audit.entities.JdbcConfig; +import org.apache.inlong.audit.entities.StatData; +import org.apache.inlong.audit.service.ConfigService; + +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.sql.ResultSet; +import java.sql.SQLException; +import java.util.LinkedList; +import java.util.List; + +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.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.PREP_STMT_CACHE_SIZE; +import static org.apache.inlong.audit.config.ConfigConstants.PREP_STMT_CACHE_SQL_LIMIT; +import static org.apache.inlong.audit.config.SqlConstants.DEFAULT_SOURCE_QUERY_IDS_SQL; +import static org.apache.inlong.audit.config.SqlConstants.DEFAULT_SOURCE_QUERY_IPS_SQL; +import static org.apache.inlong.audit.config.SqlConstants.DEFAULT_SOURCE_QUERY_MINUTE_SQL; +import static org.apache.inlong.audit.config.SqlConstants.KEY_SOURCE_QUERY_IDS_SQL; +import static org.apache.inlong.audit.config.SqlConstants.KEY_SOURCE_QUERY_IPS_SQL; +import static org.apache.inlong.audit.config.SqlConstants.KEY_SOURCE_QUERY_MINUTE_SQL; + +/** + * Real time query data from audit source. + */ +public class RealTimeQuery { + + private static final Logger LOGGER = LoggerFactory.getLogger(RealTimeQuery.class); + private static volatile RealTimeQuery realTimeQuery = null; + + private final List<DataSource> dataSourceList = new LinkedList<>(); + + private final String queryLogTsSql; + private final String queryIdsByIpSql; + private final String queryReportIpsSql; + + private RealTimeQuery() { + List<JdbcConfig> jdbcConfigList = ConfigService.getInstance().getAllAuditSource(); + for (JdbcConfig jdbcConfig : jdbcConfigList) { + dataSourceList.add(createDataSource(jdbcConfig)); + } + + queryLogTsSql = Configuration.getInstance().get(KEY_SOURCE_QUERY_MINUTE_SQL, + DEFAULT_SOURCE_QUERY_MINUTE_SQL); + queryIdsByIpSql = Configuration.getInstance().get(KEY_SOURCE_QUERY_IDS_SQL, + DEFAULT_SOURCE_QUERY_IDS_SQL); + queryReportIpsSql = Configuration.getInstance().get(KEY_SOURCE_QUERY_IPS_SQL, + DEFAULT_SOURCE_QUERY_IPS_SQL); + } + + public static RealTimeQuery getInstance() { + if (realTimeQuery == null) { + synchronized (Configuration.class) { + if (realTimeQuery == null) { + realTimeQuery = new RealTimeQuery(); + } + } + } + return realTimeQuery; + } + + /** + * Create data source. + */ + private DataSource createDataSource(JdbcConfig jdbcConfig) { + HikariConfig config = new HikariConfig(); + config.setDriverClassName(jdbcConfig.getDriverClass()); + config.setJdbcUrl(jdbcConfig.getJdbcUrl()); + config.setUsername(jdbcConfig.getUserName()); + config.setPassword(jdbcConfig.getPassword()); + config.setConnectionTimeout(Configuration.getInstance().get(KEY_DATASOURCE_CONNECTION_TIMEOUT, + DEFAULT_CONNECTION_TIMEOUT)); + config.addDataSourceProperty(CACHE_PREP_STMTS, + Configuration.getInstance().get(KEY_CACHE_PREP_STMTS, DEFAULT_CACHE_PREP_STMTS)); + config.addDataSourceProperty(PREP_STMT_CACHE_SIZE, + Configuration.getInstance().get(KEY_PREP_STMT_CACHE_SIZE, DEFAULT_PREP_STMT_CACHE_SIZE)); + config.addDataSourceProperty(PREP_STMT_CACHE_SQL_LIMIT, + Configuration.getInstance().get(KEY_PREP_STMT_CACHE_SQL_LIMIT, DEFAULT_PREP_STMT_CACHE_SQL_LIMIT)); + config.setMaximumPoolSize( + Configuration.getInstance().get(KEY_DATASOURCE_POOL_SIZE, + DEFAULT_DATASOURCE_POOL_SIZE)); + return new HikariDataSource(config); + } + + /** + * Query the audit data of log time. + * + * @param startTime + * @param endTime + * @param inlongGroupId + * @param inlongStreamId + * @param auditId + * @return + */ + public List<StatData> queryLogTs(String startTime, String endTime, String inlongGroupId, + String inlongStreamId, String auditId) { + List<StatData> statDataList = new LinkedList<>(); + for (DataSource dataSource : dataSourceList) { + statDataList = + doQueryLogTs(dataSource, startTime, endTime, inlongGroupId, inlongStreamId, auditId); + if (!statDataList.isEmpty()) { + break; + } + LOGGER.info("Change another audit source to query data! Params is: {} {} {} {} {}", + startTime, endTime, inlongGroupId, inlongStreamId, auditId); + } + return statDataList; + } + + /** + * Do query the audit data of log time. + * + * @param dataSource + * @param startTime + * @param endTime + * @param inlongGroupId + * @param inlongStreamId + * @param auditId + * @return + */ + private List<StatData> doQueryLogTs(DataSource dataSource, String startTime, String endTime, String inlongGroupId, + String inlongStreamId, String auditId) { + List<StatData> result = new LinkedList<>(); + try (Connection connection = dataSource.getConnection(); + PreparedStatement pstat = connection.prepareStatement(queryLogTsSql)) { + pstat.setString(1, startTime); + pstat.setString(2, endTime); + pstat.setString(3, inlongGroupId); + pstat.setString(4, inlongStreamId); + pstat.setString(5, auditId); + try (ResultSet resultSet = pstat.executeQuery()) { + while (resultSet.next()) { + StatData data = new StatData(); + data.setLogTs(resultSet.getString(1)); + data.setInlongGroupId(resultSet.getString(2)); + data.setInlongStreamId(resultSet.getString(3)); + data.setAuditId(resultSet.getString(4)); + data.setAuditTag(resultSet.getString(5)); + data.setCount(resultSet.getLong(6)); + data.setSize(resultSet.getLong(7)); + data.setDelay(resultSet.getLong(8)); + result.add(data); + } + } catch (SQLException sqlException) { + LOGGER.error("Query has SQL exception! ", sqlException); Review Comment: ```suggestion LOGGER.error("Query LogTs has SQL exception!, datasource={} ", datasource, sqlException); ``` ########## inlong-audit/audit-service/src/main/java/org/apache/inlong/audit/service/ConfigService.java: ########## @@ -0,0 +1,228 @@ +/* + * 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.service; + +import org.apache.inlong.audit.config.Configuration; +import org.apache.inlong.audit.entities.JdbcConfig; +import org.apache.inlong.audit.utils.JdbcUtils; + +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.sql.ResultSet; +import java.sql.SQLException; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.CopyOnWriteArrayList; +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_CONFIG_UPDATE_INTERVAL_SECONDS; +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.KEY_CACHE_PREP_STMTS; +import static org.apache.inlong.audit.config.ConfigConstants.KEY_CONFIG_UPDATE_INTERVAL_SECONDS; +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.PREP_STMT_CACHE_SIZE; +import static org.apache.inlong.audit.config.ConfigConstants.PREP_STMT_CACHE_SQL_LIMIT; +import static org.apache.inlong.audit.config.SqlConstants.DEFAULT_MYSQL_QUERY_AUDIT_ID_SQL; +import static org.apache.inlong.audit.config.SqlConstants.DEFAULT_MYSQL_QUERY_AUDIT_SOURCE_SQL; +import static org.apache.inlong.audit.config.SqlConstants.KEY_MYSQL_QUERY_AUDIT_ID_SQL; +import static org.apache.inlong.audit.config.SqlConstants.KEY_MYSQL_QUERY_AUDIT_SOURCE_SQL; + +/** + * ConfigService periodically pull the configuration of audit id and audit source from DB. + */ +public class ConfigService { + + private static final Logger LOGGER = LoggerFactory.getLogger(ConfigService.class); + + private static volatile ConfigService configService = null; + private CopyOnWriteArrayList<String> auditIds = new CopyOnWriteArrayList<>(); + protected final ScheduledExecutorService updateTimer = Executors.newSingleThreadScheduledExecutor(); + private DataSource dataSource; + private ConcurrentHashMap<String, List<JdbcConfig>> auditSources = new ConcurrentHashMap<>(); + private final String queryAuditIdSql; + private final String queryAuditSourceSql; + + public static ConfigService getInstance() { + if (configService == null) { + synchronized (ConfigService.class) { + if (configService == null) { + configService = new ConfigService(); + } + } + } + return configService; + } + + private ConfigService() { + queryAuditIdSql = Configuration.getInstance().get(KEY_MYSQL_QUERY_AUDIT_ID_SQL, + DEFAULT_MYSQL_QUERY_AUDIT_ID_SQL); + queryAuditSourceSql = Configuration.getInstance().get(KEY_MYSQL_QUERY_AUDIT_SOURCE_SQL, + DEFAULT_MYSQL_QUERY_AUDIT_SOURCE_SQL); + } + + public void start() { + createDataSource(); + updateTimer.scheduleWithFixedDelay(new Runnable() { + + @Override + public void run() { + updateAuditIds(); + updateAuditSource(); + } + }, 0, + Configuration.getInstance().get(KEY_CONFIG_UPDATE_INTERVAL_SECONDS, + DEFAULT_CONFIG_UPDATE_INTERVAL_SECONDS), + TimeUnit.SECONDS); + } + + /** + * Create data source. + */ + private void createDataSource() { + JdbcConfig jdbcConfig = JdbcUtils.buildMysqlConfig(); + HikariConfig config = new HikariConfig(); + config.setDriverClassName(jdbcConfig.getDriverClass()); + config.setJdbcUrl(jdbcConfig.getJdbcUrl()); + config.setUsername(jdbcConfig.getUserName()); + config.setPassword(jdbcConfig.getPassword()); + config.setConnectionTimeout(Configuration.getInstance().get(KEY_DATASOURCE_CONNECTION_TIMEOUT, + DEFAULT_CONNECTION_TIMEOUT)); + config.addDataSourceProperty(CACHE_PREP_STMTS, + Configuration.getInstance().get(KEY_CACHE_PREP_STMTS, DEFAULT_CACHE_PREP_STMTS)); + config.addDataSourceProperty(PREP_STMT_CACHE_SIZE, + Configuration.getInstance().get(KEY_PREP_STMT_CACHE_SIZE, DEFAULT_PREP_STMT_CACHE_SIZE)); + config.addDataSourceProperty(PREP_STMT_CACHE_SQL_LIMIT, + Configuration.getInstance().get(KEY_PREP_STMT_CACHE_SQL_LIMIT, DEFAULT_PREP_STMT_CACHE_SQL_LIMIT)); + config.setMaximumPoolSize( + Configuration.getInstance().get(KEY_DATASOURCE_POOL_SIZE, + DEFAULT_DATASOURCE_POOL_SIZE)); + dataSource = new HikariDataSource(config); + } + + /** + * Update audit ids. + */ + private void updateAuditIds() { + try (Connection connection = dataSource.getConnection(); + PreparedStatement pstat = connection.prepareStatement(queryAuditIdSql)) { + if (connection.isClosed()) { + createDataSource(); + } + + try (ResultSet resultSet = pstat.executeQuery()) { + CopyOnWriteArrayList<String> auditIdsTemp = new CopyOnWriteArrayList<>(); + while (resultSet.next()) { + String auditId = resultSet.getString(1); + LOGGER.info("Update audit id {}", auditId); + auditIdsTemp.add(auditId); + } + if (!auditIdsTemp.isEmpty()) { + auditIds = auditIdsTemp; + } + } catch (SQLException sqlException) { + LOGGER.error("Query has SQL exception! ", sqlException); + } + + } catch (Exception exception) { + LOGGER.error("Query has exception! ", exception); + } + } + + /** + * Update audit source. + */ + private void updateAuditSource() { + try (Connection connection = dataSource.getConnection(); + PreparedStatement pstat = connection.prepareStatement(queryAuditSourceSql)) { + if (connection.isClosed()) { + createDataSource(); + } + try (ResultSet resultSet = pstat.executeQuery()) { + ConcurrentHashMap<String, List<JdbcConfig>> auditSourcesTemp = new ConcurrentHashMap<>(); + while (resultSet.next()) { + JdbcConfig data = new JdbcConfig(resultSet.getString(1), + resultSet.getString(2), + resultSet.getString(3), + resultSet.getString(4)); + String serviceId = resultSet.getString(5); + List<JdbcConfig> config = auditSourcesTemp.computeIfAbsent(serviceId, k -> new LinkedList<>()); + config.add(data); + LOGGER.info("Update audit source service id = {}, jdbc config = {}", serviceId, data); + } + if (!auditSourcesTemp.isEmpty()) { + auditSources = auditSourcesTemp; + } + } catch (SQLException sqlException) { + LOGGER.error("Query has SQL exception! ", sqlException); + } + } catch (Exception exception) { + LOGGER.error("Query has exception! ", exception); + } + } + + /** + * Get audit ids. + * + * @return + */ + public List<String> getAuditIds() { + return auditIds = auditIds == null ? new CopyOnWriteArrayList<>() : auditIds; + } + + /** + * Get all audit source. + * + * @return + */ + public List<JdbcConfig> getAllAuditSource() { + List<JdbcConfig> sourceList = new LinkedList<>(); Review Comment: ```suggestion return new LinkedList<>(auditSources.values()); ``` ########## inlong-audit/audit-service/src/main/java/org/apache/inlong/audit/cache/RealTimeQuery.java: ########## @@ -0,0 +1,312 @@ +/* + * 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.cache; + +import org.apache.inlong.audit.config.Configuration; +import org.apache.inlong.audit.entities.JdbcConfig; +import org.apache.inlong.audit.entities.StatData; +import org.apache.inlong.audit.service.ConfigService; + +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.sql.ResultSet; +import java.sql.SQLException; +import java.util.LinkedList; +import java.util.List; + +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.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.PREP_STMT_CACHE_SIZE; +import static org.apache.inlong.audit.config.ConfigConstants.PREP_STMT_CACHE_SQL_LIMIT; +import static org.apache.inlong.audit.config.SqlConstants.DEFAULT_SOURCE_QUERY_IDS_SQL; +import static org.apache.inlong.audit.config.SqlConstants.DEFAULT_SOURCE_QUERY_IPS_SQL; +import static org.apache.inlong.audit.config.SqlConstants.DEFAULT_SOURCE_QUERY_MINUTE_SQL; +import static org.apache.inlong.audit.config.SqlConstants.KEY_SOURCE_QUERY_IDS_SQL; +import static org.apache.inlong.audit.config.SqlConstants.KEY_SOURCE_QUERY_IPS_SQL; +import static org.apache.inlong.audit.config.SqlConstants.KEY_SOURCE_QUERY_MINUTE_SQL; + +/** + * Real time query data from audit source. + */ +public class RealTimeQuery { + + private static final Logger LOGGER = LoggerFactory.getLogger(RealTimeQuery.class); + private static volatile RealTimeQuery realTimeQuery = null; + + private final List<DataSource> dataSourceList = new LinkedList<>(); + + private final String queryLogTsSql; + private final String queryIdsByIpSql; + private final String queryReportIpsSql; + + private RealTimeQuery() { + List<JdbcConfig> jdbcConfigList = ConfigService.getInstance().getAllAuditSource(); + for (JdbcConfig jdbcConfig : jdbcConfigList) { + dataSourceList.add(createDataSource(jdbcConfig)); + } + + queryLogTsSql = Configuration.getInstance().get(KEY_SOURCE_QUERY_MINUTE_SQL, + DEFAULT_SOURCE_QUERY_MINUTE_SQL); + queryIdsByIpSql = Configuration.getInstance().get(KEY_SOURCE_QUERY_IDS_SQL, + DEFAULT_SOURCE_QUERY_IDS_SQL); + queryReportIpsSql = Configuration.getInstance().get(KEY_SOURCE_QUERY_IPS_SQL, + DEFAULT_SOURCE_QUERY_IPS_SQL); + } + + public static RealTimeQuery getInstance() { + if (realTimeQuery == null) { + synchronized (Configuration.class) { + if (realTimeQuery == null) { + realTimeQuery = new RealTimeQuery(); + } + } + } + return realTimeQuery; + } + + /** + * Create data source. + */ + private DataSource createDataSource(JdbcConfig jdbcConfig) { + HikariConfig config = new HikariConfig(); + config.setDriverClassName(jdbcConfig.getDriverClass()); + config.setJdbcUrl(jdbcConfig.getJdbcUrl()); + config.setUsername(jdbcConfig.getUserName()); + config.setPassword(jdbcConfig.getPassword()); + config.setConnectionTimeout(Configuration.getInstance().get(KEY_DATASOURCE_CONNECTION_TIMEOUT, + DEFAULT_CONNECTION_TIMEOUT)); + config.addDataSourceProperty(CACHE_PREP_STMTS, + Configuration.getInstance().get(KEY_CACHE_PREP_STMTS, DEFAULT_CACHE_PREP_STMTS)); + config.addDataSourceProperty(PREP_STMT_CACHE_SIZE, + Configuration.getInstance().get(KEY_PREP_STMT_CACHE_SIZE, DEFAULT_PREP_STMT_CACHE_SIZE)); + config.addDataSourceProperty(PREP_STMT_CACHE_SQL_LIMIT, + Configuration.getInstance().get(KEY_PREP_STMT_CACHE_SQL_LIMIT, DEFAULT_PREP_STMT_CACHE_SQL_LIMIT)); + config.setMaximumPoolSize( + Configuration.getInstance().get(KEY_DATASOURCE_POOL_SIZE, + DEFAULT_DATASOURCE_POOL_SIZE)); + return new HikariDataSource(config); + } + + /** + * Query the audit data of log time. + * + * @param startTime + * @param endTime + * @param inlongGroupId + * @param inlongStreamId + * @param auditId + * @return + */ + public List<StatData> queryLogTs(String startTime, String endTime, String inlongGroupId, + String inlongStreamId, String auditId) { + List<StatData> statDataList = new LinkedList<>(); + for (DataSource dataSource : dataSourceList) { + statDataList = + doQueryLogTs(dataSource, startTime, endTime, inlongGroupId, inlongStreamId, auditId); + if (!statDataList.isEmpty()) { + break; + } + LOGGER.info("Change another audit source to query data! Params is: {} {} {} {} {}", + startTime, endTime, inlongGroupId, inlongStreamId, auditId); + } + return statDataList; + } + + /** + * Do query the audit data of log time. + * + * @param dataSource + * @param startTime + * @param endTime + * @param inlongGroupId + * @param inlongStreamId + * @param auditId + * @return + */ + private List<StatData> doQueryLogTs(DataSource dataSource, String startTime, String endTime, String inlongGroupId, + String inlongStreamId, String auditId) { + List<StatData> result = new LinkedList<>(); + try (Connection connection = dataSource.getConnection(); + PreparedStatement pstat = connection.prepareStatement(queryLogTsSql)) { + pstat.setString(1, startTime); + pstat.setString(2, endTime); + pstat.setString(3, inlongGroupId); + pstat.setString(4, inlongStreamId); + pstat.setString(5, auditId); + try (ResultSet resultSet = pstat.executeQuery()) { + while (resultSet.next()) { + StatData data = new StatData(); + data.setLogTs(resultSet.getString(1)); + data.setInlongGroupId(resultSet.getString(2)); + data.setInlongStreamId(resultSet.getString(3)); + data.setAuditId(resultSet.getString(4)); + data.setAuditTag(resultSet.getString(5)); + data.setCount(resultSet.getLong(6)); + data.setSize(resultSet.getLong(7)); + data.setDelay(resultSet.getLong(8)); + result.add(data); + } + } catch (SQLException sqlException) { + LOGGER.error("Query has SQL exception! ", sqlException); + } + } catch (Exception exception) { + LOGGER.error("Query has exception! ", exception); + } + return result; + } + + /** + * Query InLong group id by report ip. + * + * @param startTime + * @param endTime + * @param ip + * @param auditId + * @return + */ + public List<StatData> queryIdsByIp(String startTime, String endTime, String ip, String auditId) { + List<StatData> statDataList = new LinkedList<>(); + for (DataSource dataSource : dataSourceList) { + statDataList = doQueryIdsByIp(dataSource, startTime, endTime, ip, auditId); + if (!statDataList.isEmpty()) { + break; + } + LOGGER.info("Change another audit source to query data! Params is: {} {} {} {}", + startTime, endTime, ip, auditId); + } + return statDataList; + } + + /** + * Do query InLong group id by report ip. + * + * @param dataSource + * @param startTime + * @param endTime + * @param ip + * @param auditId + * @return + */ + private List<StatData> doQueryIdsByIp(DataSource dataSource, String startTime, String endTime, String ip, + String auditId) { + List<StatData> result = new LinkedList<>(); + try (Connection connection = dataSource.getConnection(); + PreparedStatement pstat = connection.prepareStatement(queryIdsByIpSql)) { + pstat.setString(1, startTime); + pstat.setString(2, endTime); + pstat.setString(3, auditId); + pstat.setString(4, ip); + try (ResultSet resultSet = pstat.executeQuery()) { + while (resultSet.next()) { + StatData data = new StatData(); + data.setInlongGroupId(resultSet.getString(1)); + data.setInlongStreamId(resultSet.getString(2)); + data.setAuditId(resultSet.getString(3)); + data.setAuditTag(resultSet.getString(4)); + data.setCount(resultSet.getLong(5)); + data.setSize(resultSet.getLong(6)); + data.setDelay(resultSet.getLong(7)); + result.add(data); + } + } catch (SQLException sqlException) { + LOGGER.error("Query has SQL exception! ", sqlException); + } + } catch (Exception exception) { + LOGGER.error("Query has exception! ", exception); + } + return result; + } + + /** + * Query report ips. + * + * @param startTime + * @param endTime + * @param inlongGroupId + * @param inlongStreamId + * @param auditId + * @return + */ + public List<StatData> queryIpsById(String startTime, String endTime, String inlongGroupId, + String inlongStreamId, String auditId) { + List<StatData> statDataList = new LinkedList<>(); + for (DataSource dataSource : dataSourceList) { + statDataList = doQueryIpsById(dataSource, startTime, endTime, inlongGroupId, inlongStreamId, auditId); + if (!statDataList.isEmpty()) { + break; + } Review Comment: Why not log like **queryIdsByIp** do -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
