|
| 1 | +package tech.ydb.jdbc.context; |
| 2 | + |
| 3 | +import java.sql.Connection; |
| 4 | +import java.sql.DriverManager; |
| 5 | +import java.sql.PreparedStatement; |
| 6 | +import java.sql.ResultSet; |
| 7 | +import java.sql.SQLException; |
| 8 | +import java.sql.Statement; |
| 9 | +import java.util.concurrent.ConcurrentLinkedQueue; |
| 10 | +import java.util.concurrent.atomic.AtomicLong; |
| 11 | + |
| 12 | +import org.junit.jupiter.api.Assertions; |
| 13 | +import org.junit.jupiter.api.BeforeEach; |
| 14 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 15 | +import org.junit.jupiter.params.ParameterizedTest; |
| 16 | +import org.junit.jupiter.params.provider.ValueSource; |
| 17 | + |
| 18 | +import tech.ydb.core.Status; |
| 19 | +import tech.ydb.core.StatusCode; |
| 20 | +import tech.ydb.jdbc.YdbStatement; |
| 21 | +import tech.ydb.jdbc.impl.helper.ExceptionAssert; |
| 22 | +import tech.ydb.jdbc.impl.helper.JdbcUrlHelper; |
| 23 | +import tech.ydb.jdbc.query.YdbQuery; |
| 24 | +import tech.ydb.jdbc.spi.YdbQueryExtentionService; |
| 25 | +import tech.ydb.query.result.QueryStats; |
| 26 | +import tech.ydb.query.settings.ExecuteQuerySettings; |
| 27 | +import tech.ydb.query.settings.QueryStatsMode; |
| 28 | +import tech.ydb.table.query.stats.QueryStatsCollectionMode; |
| 29 | +import tech.ydb.table.settings.ExecuteDataQuerySettings; |
| 30 | +import tech.ydb.test.junit5.YdbHelperExtension; |
| 31 | + |
| 32 | +/** |
| 33 | + * |
| 34 | + * @author Aleksandr Gorshenin |
| 35 | + */ |
| 36 | +public class YdbDriverQuerySpiTest { |
| 37 | + |
| 38 | + @RegisterExtension |
| 39 | + private static final YdbHelperExtension ydb = new YdbHelperExtension(); |
| 40 | + |
| 41 | + private static final JdbcUrlHelper jdbcURL = new JdbcUrlHelper(ydb) |
| 42 | + .withArg("cacheConnectionsInDriver", "false"); |
| 43 | + |
| 44 | + @BeforeEach |
| 45 | + public void clean() { |
| 46 | + EmptiSpi.COUNT.set(0); |
| 47 | + FullStatsSpi.QUEUE.clear(); |
| 48 | + } |
| 49 | + |
| 50 | + @ParameterizedTest |
| 51 | + @ValueSource(strings = {"true", "false"}) |
| 52 | + public void defaultUsageTest(String useQS) throws SQLException { |
| 53 | + ClassLoader prev = Thread.currentThread().getContextClassLoader(); |
| 54 | + Thread.currentThread().setContextClassLoader(new QuerySpiTestLoader(prev, EmptiSpi.class)); |
| 55 | + |
| 56 | + try (Connection conn = DriverManager.getConnection(jdbcURL.withArg("useQueryService", useQS).build())) { |
| 57 | + Assertions.assertEquals(0, EmptiSpi.COUNT.get()); |
| 58 | + |
| 59 | + try (ResultSet rs = conn.createStatement().executeQuery("SELECT 1 + 2")) { |
| 60 | + Assertions.assertTrue(rs.next()); |
| 61 | + Assertions.assertFalse(rs.next()); |
| 62 | + } |
| 63 | + |
| 64 | + Assertions.assertEquals(1, EmptiSpi.COUNT.get()); |
| 65 | + |
| 66 | + try (PreparedStatement ps = conn.prepareStatement("SELECT ? + ?")) { |
| 67 | + ps.setInt(1, 1); |
| 68 | + ps.setInt(2, 2); |
| 69 | + Assertions.assertTrue(ps.execute()); |
| 70 | + |
| 71 | + Assertions.assertEquals(2, EmptiSpi.COUNT.get()); |
| 72 | + |
| 73 | + ps.setInt(1, 2); |
| 74 | + ps.setInt(2, 3); |
| 75 | + ps.addBatch(); |
| 76 | + ps.setLong(1, 2); |
| 77 | + ps.setLong(2, 3); |
| 78 | + ps.addBatch(); |
| 79 | + |
| 80 | + Assertions.assertEquals(2, ps.executeBatch().length); |
| 81 | + Assertions.assertEquals(4, EmptiSpi.COUNT.get()); |
| 82 | + } |
| 83 | + |
| 84 | + try (Statement st = conn.createStatement()) { |
| 85 | + ExceptionAssert.ydbException("code = GENERIC_ERROR", () -> st.executeQuery("SELECT 1 + 'test'u")); |
| 86 | + Assertions.assertEquals(5, EmptiSpi.COUNT.get()); |
| 87 | + } |
| 88 | + } finally { |
| 89 | + Thread.currentThread().setContextClassLoader(prev); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + @ParameterizedTest |
| 94 | + @ValueSource(strings = {"true", "false"}) |
| 95 | + public void enableStatsModeTest(String useQS) throws SQLException { |
| 96 | + ClassLoader prev = Thread.currentThread().getContextClassLoader(); |
| 97 | + Thread.currentThread().setContextClassLoader(new QuerySpiTestLoader(prev, FullStatsSpi.class)); |
| 98 | + |
| 99 | + try (Connection conn = DriverManager.getConnection(jdbcURL.withArg("useQueryService", useQS).build())) { |
| 100 | + Assertions.assertTrue(FullStatsSpi.QUEUE.isEmpty()); |
| 101 | + |
| 102 | + try (ResultSet rs = conn.createStatement().executeQuery("SELECT 1 + 2")) { |
| 103 | + Assertions.assertTrue(rs.next()); |
| 104 | + Assertions.assertFalse(rs.next()); |
| 105 | + } |
| 106 | + |
| 107 | + Assertions.assertFalse(FullStatsSpi.QUEUE.isEmpty()); |
| 108 | + Assertions.assertNotNull(FullStatsSpi.QUEUE.poll().stats); |
| 109 | + Assertions.assertTrue(FullStatsSpi.QUEUE.isEmpty()); |
| 110 | + |
| 111 | + try (PreparedStatement ps = conn.prepareStatement("SELECT ? + ?")) { |
| 112 | + ps.setInt(1, 1); |
| 113 | + ps.setInt(2, 2); |
| 114 | + Assertions.assertTrue(ps.execute()); |
| 115 | + |
| 116 | + Assertions.assertEquals(1, FullStatsSpi.QUEUE.size()); |
| 117 | + FullStatsSpi.Record record = FullStatsSpi.QUEUE.poll(); |
| 118 | + Assertions.assertNotNull(record.stats); |
| 119 | + Assertions.assertEquals(Status.SUCCESS, record.status); |
| 120 | + Assertions.assertNull(record.th); |
| 121 | + |
| 122 | + ps.setInt(1, 2); |
| 123 | + ps.setInt(2, 3); |
| 124 | + ps.addBatch(); |
| 125 | + ps.setLong(1, 2); |
| 126 | + ps.setLong(2, 3); |
| 127 | + ps.addBatch(); |
| 128 | + |
| 129 | + Assertions.assertEquals(2, ps.executeBatch().length); |
| 130 | + |
| 131 | + Assertions.assertEquals(2, FullStatsSpi.QUEUE.size()); |
| 132 | + |
| 133 | + record = FullStatsSpi.QUEUE.poll(); |
| 134 | + Assertions.assertNotNull(record.stats); |
| 135 | + Assertions.assertEquals(Status.SUCCESS, record.status); |
| 136 | + Assertions.assertNull(record.th); |
| 137 | + |
| 138 | + record = FullStatsSpi.QUEUE.poll(); |
| 139 | + Assertions.assertNotNull(record.stats); |
| 140 | + Assertions.assertEquals(Status.SUCCESS, record.status); |
| 141 | + Assertions.assertNull(record.th); |
| 142 | + } |
| 143 | + |
| 144 | + try (Statement st = conn.createStatement()) { |
| 145 | + ExceptionAssert.ydbException("code = GENERIC_ERROR", () -> st.executeQuery("SELECT 1 + 'test'u")); |
| 146 | + |
| 147 | + Assertions.assertEquals(1, FullStatsSpi.QUEUE.size()); |
| 148 | + FullStatsSpi.Record record = FullStatsSpi.QUEUE.poll(); |
| 149 | + Assertions.assertNull(record.stats); |
| 150 | + Assertions.assertEquals(StatusCode.GENERIC_ERROR, record.status.getCode()); |
| 151 | + Assertions.assertNull(record.th); |
| 152 | + } |
| 153 | + } finally { |
| 154 | + Thread.currentThread().setContextClassLoader(prev); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + @ParameterizedTest |
| 159 | + @ValueSource(strings = {"true", "false"}) |
| 160 | + public void validateQueryTest(String useQS) throws SQLException { |
| 161 | + ClassLoader prev = Thread.currentThread().getContextClassLoader(); |
| 162 | + Thread.currentThread().setContextClassLoader(new QuerySpiTestLoader(prev, FullStatsSpi.class, ValidateSpi.class, |
| 163 | + EmptiSpi.class)); |
| 164 | + |
| 165 | + try (Connection conn = DriverManager.getConnection(jdbcURL.withArg("useQueryService", useQS).build())) { |
| 166 | + Assertions.assertTrue(FullStatsSpi.QUEUE.isEmpty()); |
| 167 | + Assertions.assertEquals(0, EmptiSpi.COUNT.get()); |
| 168 | + |
| 169 | + try (ResultSet rs = conn.createStatement().executeQuery("SELECT 1 + 2")) { |
| 170 | + Assertions.assertTrue(rs.next()); |
| 171 | + Assertions.assertFalse(rs.next()); |
| 172 | + } |
| 173 | + |
| 174 | + Assertions.assertEquals(1, FullStatsSpi.QUEUE.size()); |
| 175 | + FullStatsSpi.Record record = FullStatsSpi.QUEUE.poll(); |
| 176 | + Assertions.assertNotNull(record.stats); |
| 177 | + Assertions.assertEquals(Status.SUCCESS, record.status); |
| 178 | + Assertions.assertNull(record.th); |
| 179 | + Assertions.assertEquals(1, EmptiSpi.COUNT.get()); |
| 180 | + |
| 181 | + try (Statement st = conn.createStatement()) { |
| 182 | + ExceptionAssert.sqlException("INVALID QUERY", () -> st.executeQuery("SELECT 2 + 3")); |
| 183 | + } |
| 184 | + |
| 185 | + Assertions.assertEquals(1, FullStatsSpi.QUEUE.size()); |
| 186 | + record = FullStatsSpi.QUEUE.poll(); |
| 187 | + Assertions.assertNull(record.stats); |
| 188 | + Assertions.assertNull(record.status); |
| 189 | + Assertions.assertNotNull(record.th); |
| 190 | + Assertions.assertEquals("INVALID QUERY", record.th.getMessage()); |
| 191 | + |
| 192 | + Assertions.assertEquals(1, EmptiSpi.COUNT.get()); |
| 193 | + } finally { |
| 194 | + Thread.currentThread().setContextClassLoader(prev); |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + public static class EmptiSpi implements YdbQueryExtentionService { |
| 199 | + private static final AtomicLong COUNT = new AtomicLong(0); |
| 200 | + |
| 201 | + @Override |
| 202 | + public QueryCall newDataQuery(YdbStatement statement, YdbQuery query, String yql) { |
| 203 | + COUNT.incrementAndGet(); |
| 204 | + return new YdbQueryExtentionService.QueryCall() { |
| 205 | + }; |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + public static class ValidateSpi implements YdbQueryExtentionService { |
| 210 | + |
| 211 | + @Override |
| 212 | + public QueryCall newDataQuery(YdbStatement statement, YdbQuery query, String yql) throws SQLException { |
| 213 | + if ("SELECT 2 + 3".equals(query.getOriginQuery())) { |
| 214 | + throw new SQLException("INVALID QUERY"); |
| 215 | + } |
| 216 | + |
| 217 | + return new YdbQueryExtentionService.QueryCall() { |
| 218 | + }; |
| 219 | + } |
| 220 | + } |
| 221 | + |
| 222 | + public static class FullStatsSpi implements YdbQueryExtentionService { |
| 223 | + private static final ConcurrentLinkedQueue<Record> QUEUE = new ConcurrentLinkedQueue<>(); |
| 224 | + |
| 225 | + @Override |
| 226 | + public QueryCall newDataQuery(YdbStatement statement, YdbQuery query, String yql) { |
| 227 | + Record r = new Record(); |
| 228 | + QUEUE.add(r); |
| 229 | + return r; |
| 230 | + } |
| 231 | + |
| 232 | + static class Record implements YdbQueryExtentionService.QueryCall { |
| 233 | + |
| 234 | + private QueryStats stats = null; |
| 235 | + private Status status = null; |
| 236 | + private Throwable th = null; |
| 237 | + |
| 238 | + @Override |
| 239 | + public ExecuteQuerySettings.Builder prepareQuerySettings(ExecuteQuerySettings.Builder builder) { |
| 240 | + return builder.withStatsMode(QueryStatsMode.FULL); |
| 241 | + } |
| 242 | + |
| 243 | + @Override |
| 244 | + public ExecuteDataQuerySettings prepareDataQuerySettings(ExecuteDataQuerySettings settings) { |
| 245 | + return settings.setCollectStats(QueryStatsCollectionMode.FULL); |
| 246 | + } |
| 247 | + |
| 248 | + @Override |
| 249 | + public void onQueryStats(QueryStats stats) { |
| 250 | + this.stats = stats; |
| 251 | + } |
| 252 | + |
| 253 | + @Override |
| 254 | + public void onQueryResult(Status status, Throwable th) { |
| 255 | + this.status = status; |
| 256 | + this.th = th; |
| 257 | + } |
| 258 | + } |
| 259 | + } |
| 260 | +} |
0 commit comments