feat(oceanbase-oracle): split execution time from client wait
This commit is contained in:
1 parent
1294708eea
commit
33afb0b432
58 files changed
+843
-11
No files matched your search
+101
@@ -1,10 +1,12 @@
|
||||
package com.dbx.agent.oceanbaseoracle;
|
||||
|
||||
import com.dbx.agent.ColumnInfo;
|
||||
import com.dbx.agent.AgentProtocol;
|
||||
import com.dbx.agent.ConfiguredJdbcAgent;
|
||||
import com.dbx.agent.ConnectParams;
|
||||
import com.dbx.agent.DatabaseInfo;
|
||||
import com.dbx.agent.DdlBuilder;
|
||||
import com.dbx.agent.ExecuteQueryOptions;
|
||||
import com.dbx.agent.ForeignKeyInfo;
|
||||
import com.dbx.agent.IndexInfo;
|
||||
import com.dbx.agent.JdbcAgentProfile;
|
||||
@@ -16,12 +18,17 @@ import com.dbx.agent.ObjectInfo;
|
||||
import com.dbx.agent.ObjectSource;
|
||||
import com.dbx.agent.OracleObjectPrivilege;
|
||||
import com.dbx.agent.PartitionInfo;
|
||||
import com.dbx.agent.QueryPageOptions;
|
||||
import com.dbx.agent.QueryPageResult;
|
||||
import com.dbx.agent.QueryResult;
|
||||
import com.dbx.agent.TableInfo;
|
||||
import com.dbx.agent.TriggerInfo;
|
||||
|
||||
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;
|
||||
@@ -51,6 +58,9 @@ 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",
|
||||
@@ -70,6 +80,97 @@ public final class OceanBaseOracleAgent extends ConfiguredJdbcAgent {
|
||||
super(OCEANBASE_ORACLE_PROFILE);
|
||||
}
|
||||
|
||||
@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())
|
||||
));
|
||||
}
|
||||
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();
|
||||
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.
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String buildJdbcUrl(ConnectParams params) {
|
||||
return buildUrl(params);
|
||||
|
||||
+108
@@ -1,5 +1,6 @@
|
||||
package com.dbx.agent.oceanbaseoracle;
|
||||
|
||||
import com.dbx.agent.AgentProtocol;
|
||||
import com.dbx.agent.ColumnInfo;
|
||||
import com.dbx.agent.ConnectParams;
|
||||
import com.dbx.agent.ExecuteQueryOptions;
|
||||
@@ -7,6 +8,7 @@ import com.dbx.agent.MetadataListConstraints;
|
||||
import com.dbx.agent.ObjectInfo;
|
||||
import com.dbx.agent.ObjectSource;
|
||||
import com.dbx.agent.QueryPageOptions;
|
||||
import com.dbx.agent.QueryPageResult;
|
||||
import com.dbx.agent.QueryResult;
|
||||
import com.dbx.agent.TableInfo;
|
||||
import com.dbx.agent.test.TestSupport;
|
||||
@@ -133,6 +135,68 @@ 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));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void doesNotAuditAnOpenCursorAfterAnotherSessionMethodCanReplaceItsTrace() {
|
||||
int[] row = {-1};
|
||||
ResultSetMetaData meta = proxy(ResultSetMetaData.class, (method, args) -> {
|
||||
if ("getColumnCount".equals(method.getName())) return 1;
|
||||
if ("getColumnLabel".equals(method.getName())) return "N";
|
||||
if ("getColumnType".equals(method.getName())) return Types.INTEGER;
|
||||
if ("getColumnTypeName".equals(method.getName())) return "NUMBER";
|
||||
return defaultValue(method.getReturnType());
|
||||
});
|
||||
ResultSet cursor = proxy(ResultSet.class, (method, args) -> {
|
||||
if ("next".equals(method.getName())) return ++row[0] < 2;
|
||||
if ("getMetaData".equals(method.getName())) return meta;
|
||||
if ("getObject".equals(method.getName()) || "getInt".equals(method.getName())) return row[0] + 1;
|
||||
return defaultValue(method.getReturnType());
|
||||
});
|
||||
int[] statementsCreated = {0};
|
||||
Statement statement = proxy(Statement.class, (method, args) -> {
|
||||
if ("execute".equals(method.getName())) return !String.valueOf(args[0]).startsWith("ALTER SESSION");
|
||||
if ("getResultSet".equals(method.getName())) return cursor;
|
||||
return defaultValue(method.getReturnType());
|
||||
});
|
||||
Connection connection = proxy(Connection.class, (method, args) -> {
|
||||
if ("createStatement".equals(method.getName())) {
|
||||
statementsCreated[0]++;
|
||||
return statement;
|
||||
}
|
||||
if ("isClosed".equals(method.getName())) return false;
|
||||
return defaultValue(method.getReturnType());
|
||||
});
|
||||
OceanBaseOracleAgent agent = new OceanBaseOracleAgent();
|
||||
TestSupport.setPrivateConnection(agent, connection);
|
||||
QueryPageResult first = agent.executeQueryPage("SELECT N FROM T", null, new QueryPageOptions(1, null, 10, 5));
|
||||
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");
|
||||
}
|
||||
|
||||
@Test
|
||||
void treatsZeroQueryTimeoutAsUnlimitedForOceanBaseSession() {
|
||||
List<String> sql = new ArrayList<>();
|
||||
@@ -1198,6 +1262,50 @@ class OceanBaseOracleAgentTest {
|
||||
});
|
||||
}
|
||||
|
||||
private static Connection auditTimingConnection(int auditRows, long returnedRows, boolean denied,
|
||||
List<String> sql, List<String> parameters, List<Integer> maxRows) {
|
||||
ResultSet trace = resultSet(new String[]{"TRACE_ID"}, new Object[][]{{"trace-1"}});
|
||||
int[] auditIndex = {-1};
|
||||
ResultSet audit = proxy(ResultSet.class, (method, args) -> {
|
||||
switch (method.getName()) {
|
||||
case "next":
|
||||
auditIndex[0]++;
|
||||
return auditIndex[0] < auditRows;
|
||||
case "getLong":
|
||||
return ((Number) args[0]).intValue() == 1 ? 370L : returnedRows;
|
||||
default:
|
||||
return defaultValue(method.getReturnType());
|
||||
}
|
||||
});
|
||||
Statement traceStatement = proxy(Statement.class, (method, args) -> {
|
||||
if ("setQueryTimeout".equals(method.getName())) Assertions.assertEquals(1, args[0]);
|
||||
if ("setMaxRows".equals(method.getName())) maxRows.add((Integer) args[0]);
|
||||
if ("executeQuery".equals(method.getName())) {
|
||||
sql.add(String.valueOf(args[0]));
|
||||
return trace;
|
||||
}
|
||||
return defaultValue(method.getReturnType());
|
||||
});
|
||||
PreparedStatement auditStatement = proxy(PreparedStatement.class, (method, args) -> {
|
||||
if ("setQueryTimeout".equals(method.getName())) Assertions.assertEquals(1, args[0]);
|
||||
if ("setMaxRows".equals(method.getName())) maxRows.add((Integer) args[0]);
|
||||
if ("setString".equals(method.getName())) parameters.add(String.valueOf(args[1]));
|
||||
if ("executeQuery".equals(method.getName())) {
|
||||
if (denied) throw new SQLException("audit access denied", "42000", 1044);
|
||||
return audit;
|
||||
}
|
||||
return defaultValue(method.getReturnType());
|
||||
});
|
||||
return proxy(Connection.class, (method, args) -> {
|
||||
if ("createStatement".equals(method.getName())) return traceStatement;
|
||||
if ("prepareStatement".equals(method.getName())) {
|
||||
sql.add(String.valueOf(args[0]));
|
||||
return auditStatement;
|
||||
}
|
||||
return defaultValue(method.getReturnType());
|
||||
});
|
||||
}
|
||||
|
||||
private static ResultSet blobResultSet() {
|
||||
String[] columns = {"PAYLOAD", "EMPTY_PAYLOAD", "DESCRIPTION"};
|
||||
int[] sqlTypes = {Types.BLOB, Types.BLOB, Types.VARCHAR};
|
||||
|
||||
Reference in new issue
Block a user