feat(query): cross-database timing details, drop OceanBase audit query
This commit is contained in:
1 parent
4b9a1c4d5c
commit
d0497e0ca6
69 files changed
+973
-180
No files matched your search
+15
-82
@@ -27,6 +27,7 @@ import com.dbx.agent.PartitionInfo;
|
||||
import com.dbx.agent.QueryPageOptions;
|
||||
import com.dbx.agent.QueryPageResult;
|
||||
import com.dbx.agent.QueryResult;
|
||||
import com.dbx.agent.QueryTiming;
|
||||
import com.dbx.agent.TableInfo;
|
||||
import com.dbx.agent.TriggerInfo;
|
||||
|
||||
@@ -34,7 +35,6 @@ import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.sql.Types;
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.ArrayList;
|
||||
@@ -66,9 +66,6 @@ public final class OceanBaseOracleAgent extends ConfiguredJdbcAgent {
|
||||
"GGSYS", "FLOWS_FILES", "APEX_PUBLIC_USER", "GSMROOTUSER", "SYSRAC"
|
||||
);
|
||||
private boolean queryTimeoutChanged;
|
||||
private String auditEligibleCursorId;
|
||||
private static final String SQL_AUDIT_BY_TRACE = "SELECT EXECUTE_TIME, RETURN_ROWS FROM SYS.GV$OB_SQL_AUDIT "
|
||||
+ "WHERE TRACE_ID = ? AND IS_INNER_SQL = 0 AND IS_EXECUTOR_RPC = 0 AND ROWNUM <= 2";
|
||||
|
||||
public static final JdbcAgentProfile OCEANBASE_ORACLE_PROFILE = new JdbcAgentProfile(
|
||||
"com.oceanbase.jdbc.Driver",
|
||||
@@ -88,97 +85,33 @@ public final class OceanBaseOracleAgent extends ConfiguredJdbcAgent {
|
||||
super(OCEANBASE_ORACLE_PROFILE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supportsQueryTiming() { return true; }
|
||||
|
||||
@Override
|
||||
public QueryResult executeQuery(String sql, String schema, ExecuteQueryOptions options) {
|
||||
auditEligibleCursorId = null;
|
||||
QueryResult result = super.executeQuery(sql, schema, options);
|
||||
if (!result.getTruncated() && !result.getColumns().isEmpty()) {
|
||||
result.setServer_execute_time_us(availableServerExecuteTimeUs(
|
||||
result.getRows().size(), JdbcExecutor.statementMaxRows(options.getMaxRows())
|
||||
));
|
||||
try (QueryTiming timing = QueryTiming.begin()) {
|
||||
QueryResult result = super.executeQuery(sql, schema, options);
|
||||
result.setQuery_timings_ms(timing.finish());
|
||||
return result;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public QueryPageResult executeQueryPage(String sql, String schema, QueryPageOptions options) {
|
||||
auditEligibleCursorId = null;
|
||||
QueryPageResult result = super.executeQueryPage(sql, schema, options);
|
||||
if (result.getHas_more()) {
|
||||
auditEligibleCursorId = result.getSession_id();
|
||||
try (QueryTiming timing = QueryTiming.begin()) {
|
||||
QueryPageResult result = super.executeQueryPage(sql, schema, options);
|
||||
result.setQuery_timings_ms(timing.finish());
|
||||
return result;
|
||||
}
|
||||
return withCompletedCursorServerTiming(result);
|
||||
}
|
||||
|
||||
@Override
|
||||
public QueryPageResult fetchQueryPage(String sessionId, int pageSize) {
|
||||
boolean auditEligible = sessionId != null && sessionId.equals(auditEligibleCursorId);
|
||||
QueryPageResult result = super.fetchQueryPage(sessionId, pageSize);
|
||||
if (!result.getHas_more()) auditEligibleCursorId = null;
|
||||
return auditEligible ? withCompletedCursorServerTiming(result) : result;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void beforeAgentMethod(String method, String querySessionId) {
|
||||
if (!AgentProtocol.METHOD_FETCH_QUERY_PAGE.equals(method)
|
||||
|| auditEligibleCursorId == null
|
||||
|| !auditEligibleCursorId.equals(querySessionId)) {
|
||||
auditEligibleCursorId = null;
|
||||
}
|
||||
}
|
||||
|
||||
private QueryPageResult withCompletedCursorServerTiming(QueryPageResult result) {
|
||||
// JdbcExecutor closes the ResultSet and Statement before returning a terminal
|
||||
// page. Never issue audit SQL while Connector/J still owns an open cursor.
|
||||
// executePage limits rows while reading, without Statement.setMaxRows.
|
||||
// Preserve its JDBC default of 0 here; QueryPageOptions.maxRows is a
|
||||
// client-side cap, not the statement limit used by Connector/J.
|
||||
if (!result.getHas_more() && !result.getTruncated() && !result.getColumns().isEmpty()) {
|
||||
result.setServer_execute_time_us(availableServerExecuteTimeUs(result.getCursor_rows_read(), 0));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private Long availableServerExecuteTimeUs(long expectedRows, int statementMaxRows) {
|
||||
try {
|
||||
return serverExecuteTimeUs(requireConnected(), expectedRows, statementMaxRows);
|
||||
} catch (RuntimeException ignored) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
static Long serverExecuteTimeUs(Connection connection, long expectedRows, int statementMaxRows) {
|
||||
try {
|
||||
String traceId;
|
||||
try (Statement statement = connection.createStatement()) {
|
||||
// Connector/J resets the session's SQL_SELECT_LIMIT in execute prolog
|
||||
// when this differs from the previous statement. That SET would
|
||||
// replace the query's LAST_TRACE_ID before we can read it.
|
||||
statement.setMaxRows(statementMaxRows);
|
||||
statement.setQueryTimeout(1);
|
||||
try (ResultSet traces = statement.executeQuery("SELECT LAST_TRACE_ID() FROM DUAL")) {
|
||||
traceId = traces.next() ? traces.getString(1) : null;
|
||||
}
|
||||
}
|
||||
if (traceId == null || traceId.isBlank()) return null;
|
||||
try (PreparedStatement statement = connection.prepareStatement(SQL_AUDIT_BY_TRACE)) {
|
||||
statement.setMaxRows(statementMaxRows);
|
||||
statement.setQueryTimeout(1);
|
||||
statement.setString(1, traceId);
|
||||
try (ResultSet audit = statement.executeQuery()) {
|
||||
if (!audit.next()) return null;
|
||||
long executionUs = audit.getLong(1);
|
||||
if (audit.wasNull() || executionUs < 0) return null;
|
||||
long returnedRows = audit.getLong(2);
|
||||
if (audit.wasNull() || returnedRows != expectedRows || audit.next()) return null;
|
||||
return executionUs;
|
||||
}
|
||||
}
|
||||
} catch (SQLException | RuntimeException ignored) {
|
||||
// SQL Audit can be disabled, unavailable to this account, or evicted.
|
||||
// The user's query result remains valid; no wall-clock substitute is used.
|
||||
return null;
|
||||
try (QueryTiming timing = QueryTiming.begin()) {
|
||||
QueryPageResult result = super.fetchQueryPage(sessionId, pageSize);
|
||||
result.setQuery_timings_ms(timing.finish());
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+7
-26
@@ -1,6 +1,5 @@
|
||||
package com.dbx.agent.oceanbaseoracle;
|
||||
|
||||
import com.dbx.agent.AgentProtocol;
|
||||
import com.dbx.agent.ColumnInfo;
|
||||
import com.dbx.agent.CompletionAssistantMatchMode;
|
||||
import com.dbx.agent.CompletionAssistantObjectKind;
|
||||
@@ -141,28 +140,9 @@ class OceanBaseOracleAgentTest {
|
||||
Assertions.assertThrows(IllegalArgumentException.class, () -> OceanBaseOracleAgent.queryTimeoutSql(-1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void readsServerExecutionOnlyFromOneMatchingAuditRow() {
|
||||
List<String> statements = new ArrayList<>();
|
||||
List<String> parameters = new ArrayList<>();
|
||||
List<Integer> maxRows = new ArrayList<>();
|
||||
Assertions.assertEquals(370L, OceanBaseOracleAgent.serverExecuteTimeUs(
|
||||
auditTimingConnection(1, 500, false, statements, parameters, maxRows), 500, 1001
|
||||
));
|
||||
Assertions.assertEquals(List.of(1001, 1001), maxRows,
|
||||
"trace and audit statements must preserve the target statement's session row limit");
|
||||
Assertions.assertEquals(List.of("trace-1"), parameters);
|
||||
Assertions.assertTrue(statements.get(1).contains("IS_INNER_SQL = 0 AND IS_EXECUTOR_RPC = 0"));
|
||||
Assertions.assertEquals(null, OceanBaseOracleAgent.serverExecuteTimeUs(auditTimingConnection(0, 500, false, new ArrayList<>(), new ArrayList<>(), new ArrayList<>()), 500, 0));
|
||||
Assertions.assertEquals(null, OceanBaseOracleAgent.serverExecuteTimeUs(auditTimingConnection(2, 500, false, new ArrayList<>(), new ArrayList<>(), new ArrayList<>()), 500, 0));
|
||||
Assertions.assertEquals(null, OceanBaseOracleAgent.serverExecuteTimeUs(auditTimingConnection(1, 1, false, new ArrayList<>(), new ArrayList<>(), new ArrayList<>()), 500, 0));
|
||||
Assertions.assertEquals(null, OceanBaseOracleAgent.serverExecuteTimeUs(auditTimingConnection(1, 500, true, new ArrayList<>(), new ArrayList<>(), new ArrayList<>()), 500, 0));
|
||||
}
|
||||
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"10, 1000, false", "1, 1000, false", "1, 1, true", "2, 2, false"})
|
||||
void preservesTheCursorStatementLimitWhenSamplingItsTerminalPage(int pageSize, int maxRows, boolean truncated) {
|
||||
void returnsCursorRowsWithoutAdditionalAuditQueries(int pageSize, int maxRows, boolean truncated) {
|
||||
List<Integer> auditLimits = new ArrayList<>();
|
||||
List<String> auditSql = new ArrayList<>();
|
||||
Connection auditConnection = auditTimingConnection(1, 2, false, auditSql, new ArrayList<>(), auditLimits);
|
||||
@@ -219,12 +199,13 @@ class OceanBaseOracleAgentTest {
|
||||
Assertions.assertEquals(truncated, result.getTruncated());
|
||||
Assertions.assertEquals(truncated ? List.of(List.of(1)) : List.of(List.of(1), List.of(2)), rows);
|
||||
Assertions.assertEquals(0, queryLimit[0], "the paging cap must not change the JDBC statement limit");
|
||||
Assertions.assertEquals(truncated ? List.of() : List.of(0, 0), auditLimits);
|
||||
Assertions.assertEquals(truncated ? null : Long.valueOf(370), result.getServer_execute_time_us());
|
||||
Assertions.assertTrue(auditSql.isEmpty(), "completed queries must not read trace or audit records");
|
||||
Assertions.assertTrue(auditLimits.isEmpty());
|
||||
Assertions.assertNull(result.getServer_execute_time_us());
|
||||
}
|
||||
|
||||
@Test
|
||||
void doesNotAuditAnOpenCursorAfterAnotherSessionMethodCanReplaceItsTrace() {
|
||||
void finishesCursorWithoutCreatingDiagnosticStatements() {
|
||||
int[] row = {-1};
|
||||
ResultSetMetaData meta = proxy(ResultSetMetaData.class, (method, args) -> {
|
||||
if ("getColumnCount".equals(method.getName())) return 1;
|
||||
@@ -259,11 +240,11 @@ class OceanBaseOracleAgentTest {
|
||||
Assertions.assertTrue(first.getHas_more());
|
||||
Assertions.assertEquals(2, statementsCreated[0]);
|
||||
|
||||
agent.beforeAgentMethod(AgentProtocol.METHOD_LIST_TABLES, null);
|
||||
|
||||
QueryPageResult last = agent.fetchQueryPage(first.getSession_id(), 1);
|
||||
Assertions.assertFalse(last.getHas_more());
|
||||
Assertions.assertNull(last.getServer_execute_time_us());
|
||||
Assertions.assertEquals(2, statementsCreated[0], "no LAST_TRACE_ID query may run for an invalidated cursor");
|
||||
Assertions.assertEquals(2, statementsCreated[0], "no diagnostic query may run when the cursor finishes");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in new issue
Block a user