|
| 1 | +/* |
| 2 | + * Copyright 2023 NAVER Corp. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.navercorp.pinpoint.it.plugin.jdbc.clickhouse; |
| 17 | + |
| 18 | +import com.navercorp.pinpoint.bootstrap.context.DatabaseInfo; |
| 19 | +import com.navercorp.pinpoint.bootstrap.plugin.jdbc.JdbcUrlParserV2; |
| 20 | +import com.navercorp.pinpoint.bootstrap.plugin.test.Expectations; |
| 21 | +import com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier; |
| 22 | +import com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifierHolder; |
| 23 | +import com.navercorp.pinpoint.common.profiler.sql.DefaultSqlNormalizer; |
| 24 | +import com.navercorp.pinpoint.common.profiler.sql.NormalizedSql; |
| 25 | +import com.navercorp.pinpoint.common.profiler.sql.SqlNormalizer; |
| 26 | +import com.navercorp.pinpoint.it.plugin.utils.jdbc.DriverProperties; |
| 27 | +import com.navercorp.pinpoint.it.plugin.utils.jdbc.JDBCApi; |
| 28 | +import com.navercorp.pinpoint.it.plugin.utils.jdbc.JDBCDriverClass; |
| 29 | +import com.navercorp.pinpoint.it.plugin.utils.jdbc.template.DriverManagerDataSource; |
| 30 | +import com.navercorp.pinpoint.plugin.jdbc.clickhouse.ClickHouseConstants; |
| 31 | +import org.apache.logging.log4j.LogManager; |
| 32 | +import org.apache.logging.log4j.Logger; |
| 33 | + |
| 34 | +import javax.sql.DataSource; |
| 35 | +import java.lang.reflect.Method; |
| 36 | +import java.sql.Connection; |
| 37 | +import java.sql.Driver; |
| 38 | +import java.sql.DriverManager; |
| 39 | +import java.sql.PreparedStatement; |
| 40 | +import java.sql.ResultSet; |
| 41 | +import java.sql.SQLException; |
| 42 | +import java.sql.Statement; |
| 43 | +import java.util.Collections; |
| 44 | + |
| 45 | +/** |
| 46 | + * @author intr3p1d |
| 47 | + */ |
| 48 | +public class ClickHouseITBase { |
| 49 | + private final Logger logger = LogManager.getLogger(getClass()); |
| 50 | + private static final String TABLE_NAME = "jdbc_example_basic"; |
| 51 | + |
| 52 | + private static final SqlNormalizer sqlNormalizer = new DefaultSqlNormalizer(); |
| 53 | + |
| 54 | + protected DriverProperties driverProperties; |
| 55 | + protected ClickHouseITHelper clickHouseITHelper; |
| 56 | + |
| 57 | + protected String DB_TYPE = ClickHouseConstants.CLICK_HOUSE.getName(); |
| 58 | + protected String DB_EXECUTE_QUERY = ClickHouseConstants.CLICK_HOUSE_EXECUTE_QUERY.getName(); |
| 59 | + |
| 60 | + protected String jdbcUrl; |
| 61 | + protected String databaseId; |
| 62 | + protected String databaseIdPassword; |
| 63 | + protected String databaseAddress; |
| 64 | + protected String databaseName; |
| 65 | + protected JDBCDriverClass jdbcDriverClass; |
| 66 | + protected JDBCApi jdbcApi; |
| 67 | + private DataSource dataSource; |
| 68 | + |
| 69 | + public void setup( |
| 70 | + DriverProperties driverProperties, |
| 71 | + JdbcUrlParserV2 jdbcUrlParser, |
| 72 | + JDBCDriverClass jdbcDriverClass, |
| 73 | + JDBCApi jdbcApi |
| 74 | + ) { |
| 75 | + this.driverProperties = driverProperties; |
| 76 | + this.clickHouseITHelper = new ClickHouseITHelper(driverProperties); |
| 77 | + |
| 78 | + this.jdbcUrl = driverProperties.getUrl(); |
| 79 | + |
| 80 | + DatabaseInfo databaseInfo = jdbcUrlParser.parse(jdbcUrl); |
| 81 | + |
| 82 | + this.databaseAddress = databaseInfo.getHost().get(0); |
| 83 | + this.databaseName = databaseInfo.getDatabaseId(); |
| 84 | + |
| 85 | + this.databaseId = driverProperties.getUser(); |
| 86 | + this.databaseIdPassword = driverProperties.getPassword(); |
| 87 | + this.dataSource = new DriverManagerDataSource(jdbcUrl, databaseId, databaseIdPassword); |
| 88 | + |
| 89 | + this.jdbcDriverClass = jdbcDriverClass; |
| 90 | + this.jdbcApi = jdbcApi; |
| 91 | + |
| 92 | + try { |
| 93 | + Driver driver = jdbcDriverClass.getDriver().newInstance(); |
| 94 | + DriverManager.registerDriver(driver); |
| 95 | + } catch (Exception e) { |
| 96 | + throw new RuntimeException("driver register error", e); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + private Connection getConnection() throws SQLException { |
| 101 | + return clickHouseITHelper.getConnection(); |
| 102 | + } |
| 103 | + |
| 104 | + public void testStatements() throws SQLException { |
| 105 | + |
| 106 | + PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance(); |
| 107 | + |
| 108 | + Connection conn = getConnection(); |
| 109 | + |
| 110 | + String sql1 = String.format( |
| 111 | + "drop table if exists %1$s; create table %1$s(a String, b Nullable(String)) engine=Memory", |
| 112 | + TABLE_NAME); |
| 113 | + String sql2 = "select * from " + TABLE_NAME; |
| 114 | + String sql3 = "select * from " + TABLE_NAME; |
| 115 | + |
| 116 | + try (Statement stmt = conn.createStatement()) { |
| 117 | + stmt.execute(sql1); |
| 118 | + } |
| 119 | + |
| 120 | + try (Statement stmt = conn.createStatement()) { |
| 121 | + stmt.setMaxRows(3); |
| 122 | + int count = 0; |
| 123 | + try (ResultSet rs = stmt.executeQuery(sql2)) { |
| 124 | + while (rs.next()) { |
| 125 | + count++; |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + try (Statement stmt = conn.createStatement()) { |
| 131 | + stmt.setMaxRows(3); |
| 132 | + int count = stmt.executeUpdate(sql3); |
| 133 | + } |
| 134 | + |
| 135 | + verifier.printCache(); |
| 136 | + |
| 137 | + Method connect = jdbcApi.getDriver().getConnect(); |
| 138 | + verifier.verifyTrace(Expectations.event(DB_TYPE, connect, null, databaseAddress, databaseName, Expectations.cachedArgs(jdbcUrl))); |
| 139 | + |
| 140 | + Method execute = jdbcApi.getStatement().getExecute(); |
| 141 | + verifier.verifyTrace(Expectations.event(DB_EXECUTE_QUERY, execute, null, databaseAddress, databaseName, Expectations.sql(sql1, null))); |
| 142 | + |
| 143 | + Method executeQuery = jdbcApi.getStatement().getExecuteQuery(); |
| 144 | + verifier.verifyTrace(Expectations.event(DB_EXECUTE_QUERY, executeQuery, null, databaseAddress, databaseName, Expectations.sql(sql2, null))); |
| 145 | + |
| 146 | + Method executeUpdate = jdbcApi.getStatement().getExecuteUpdate(); |
| 147 | + verifier.verifyTrace(Expectations.event(DB_EXECUTE_QUERY, executeUpdate, null, databaseAddress, databaseName, Expectations.sql(sql3, null))); |
| 148 | + |
| 149 | + } |
| 150 | + |
| 151 | + public void testPreparedStatements() throws SQLException { |
| 152 | + |
| 153 | + PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance(); |
| 154 | + |
| 155 | + Connection conn = getConnection(); |
| 156 | + |
| 157 | + String sql0 = "INSERT INTO test (name, age) VALUES (?, 5)"; |
| 158 | + |
| 159 | + String sql1 = "drop table if exists t_map;" |
| 160 | + + "CREATE TABLE t_map" |
| 161 | + + "(" |
| 162 | + + " `audit_seq` Int64 CODEC(Delta(8), LZ4)," |
| 163 | + + "`timestamp` Int64 CODEC(Delta(8), LZ4)," |
| 164 | + + "`event_type` LowCardinality(String)," |
| 165 | + + "`event_subtype` LowCardinality(String)," |
| 166 | + + "`actor_type` LowCardinality(String)," |
| 167 | + + "`actor_id` String," |
| 168 | + + "`actor_tenant_id` LowCardinality(String)," |
| 169 | + + "`actor_tenant_name` String," |
| 170 | + + "`actor_firstname` String," |
| 171 | + + "`actor_lastname` String," |
| 172 | + + "`resource_type` LowCardinality(String)," |
| 173 | + + "`resource_id` String," |
| 174 | + + "`resource_container` LowCardinality(String)," |
| 175 | + + "`resource_path` String," |
| 176 | + + "`origin_ip` String," |
| 177 | + + "`origin_app_name` LowCardinality(String)," |
| 178 | + + "`origin_app_instance` String," |
| 179 | + + "`description` String," |
| 180 | + + "`attributes` Map(String, String)" |
| 181 | + + ")" |
| 182 | + + "ENGINE = MergeTree " |
| 183 | + + "ORDER BY (resource_container, event_type, event_subtype) " |
| 184 | + + "SETTINGS index_granularity = 8192"; |
| 185 | + String sql2 = "INSERT INTO t_map SETTINGS async_insert=1,wait_for_async_insert=1 " + |
| 186 | + "VALUES (8481365034795008,1673349039830,'operation-9','a','service', 'bc3e47b8-2b34-4c1a-9004-123656fa0000','b', 'c', 'service-56','d', 'object','e', 'my-value-62', 'mypath', 'some.hostname.address.com', 'app-9', 'instance-6','x', ?)"; |
| 187 | + |
| 188 | + |
| 189 | + try (PreparedStatement stmt = conn.prepareStatement(sql0)) { |
| 190 | + stmt.setString(1, "maru"); |
| 191 | + stmt.execute(); |
| 192 | + } |
| 193 | + |
| 194 | + try (Statement s = conn.createStatement()) { |
| 195 | + s.execute(sql1); |
| 196 | + } |
| 197 | + |
| 198 | + try (PreparedStatement stmt = conn.prepareStatement(sql2)) { |
| 199 | + stmt.setObject(1, Collections.singletonMap("key1", "value1")); |
| 200 | + stmt.execute(); |
| 201 | + } |
| 202 | + |
| 203 | + verifier.printCache(); |
| 204 | + |
| 205 | + Method connect = jdbcApi.getDriver().getConnect(); |
| 206 | + verifier.verifyTrace(Expectations.event(DB_TYPE, connect, null, databaseAddress, databaseName, Expectations.cachedArgs(jdbcUrl))); |
| 207 | + |
| 208 | + NormalizedSql normalizedSql0 = normalize(sql0); |
| 209 | + |
| 210 | + Method prepare1 = jdbcApi.getConnection().getPrepareStatement(); |
| 211 | + verifier.verifyTrace(Expectations.event(DB_TYPE, prepare1, null, databaseAddress, databaseName, |
| 212 | + Expectations.sql(normalizedSql0.getNormalizedSql(), normalizedSql0.getParseParameter()))); |
| 213 | + |
| 214 | + Method executePrepared1 = jdbcApi.getPreparedStatement().getExecute(); |
| 215 | + verifier.verifyTrace(Expectations.event(DB_EXECUTE_QUERY, executePrepared1, null, databaseAddress, databaseName, |
| 216 | + Expectations.sql(normalizedSql0.getNormalizedSql(), normalizedSql0.getParseParameter(), "maru"))); |
| 217 | + |
| 218 | + |
| 219 | + NormalizedSql normalizedSql1 = normalize(sql1); |
| 220 | + Method execute = jdbcApi.getStatement().getExecute(); |
| 221 | + verifier.verifyTrace(Expectations.event(DB_EXECUTE_QUERY, execute, null, databaseAddress, databaseName, |
| 222 | + Expectations.sql(normalizedSql1.getNormalizedSql(), normalizedSql1.getParseParameter()))); |
| 223 | + |
| 224 | + NormalizedSql normalizedSql2 = normalize(sql2); |
| 225 | + Method prepare2 = jdbcApi.getConnection().getPrepareStatement(); |
| 226 | + verifier.verifyTrace(Expectations.event(DB_TYPE, prepare2, null, databaseAddress, databaseName, |
| 227 | + Expectations.sql(normalizedSql2.getNormalizedSql(), normalizedSql2.getParseParameter()))); |
| 228 | + |
| 229 | + Method executePrepared2 = jdbcApi.getPreparedStatement().getExecute(); |
| 230 | + verifier.verifyTrace(Expectations.event(DB_EXECUTE_QUERY, executePrepared2, null, databaseAddress, databaseName, |
| 231 | + Expectations.sql(normalizedSql2.getNormalizedSql(), normalizedSql2.getParseParameter(), "java.util.Collections$SingletonMap"))); |
| 232 | + } |
| 233 | + |
| 234 | + private NormalizedSql normalize(String sql) { |
| 235 | + return sqlNormalizer.normalizeSql(sql); |
| 236 | + } |
| 237 | + |
| 238 | +} |
0 commit comments