fix(dameng): initialize DBMS output lazily

Closes #6400
This commit is contained in:
t8y2 committed 2026-08-18 16:55:30 +00:00
1 parent 43c62d85ad
commit ca8f566674
2 files changed
+254 -58

No files matched your search

@@ -18,6 +18,7 @@ import com.dbx.agent.QueryPageResult;
import com.dbx.agent.QueryResult; import com.dbx.agent.QueryResult;
import com.dbx.agent.TableInfo; import com.dbx.agent.TableInfo;
import com.dbx.agent.TriggerInfo; import com.dbx.agent.TriggerInfo;
import dm.jdbc.driver.DmdbConnection;
import java.io.PrintStream; import java.io.PrintStream;
import java.io.Reader; import java.io.Reader;
import java.net.SocketTimeoutException; import java.net.SocketTimeoutException;
@@ -49,6 +50,7 @@ import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.WeakHashMap;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@@ -89,6 +91,8 @@ public final class DamengAgent extends AbstractJdbcAgent {
""".stripIndent().trim(); """.stripIndent().trim();
private String connectedUsername; private String connectedUsername;
private volatile boolean dbmsOutputInitializationSupported = true; private volatile boolean dbmsOutputInitializationSupported = true;
private final Map<Object, Boolean> dbmsOutputInitializedConnections =
Collections.synchronizedMap(new WeakHashMap<>());
@Override @Override
protected String driverClass() { protected String driverClass() {
@@ -118,11 +122,24 @@ public final class DamengAgent extends AbstractJdbcAgent {
} }
@Override @Override
protected void afterPhysicalConnect(ConnectParams params, Connection connection) throws SQLException { protected void afterPhysicalConnect(ConnectParams params, Connection connection) {
if (!dbmsOutputInitializationSupported) { // DBMS_OUTPUT is initialized lazily because some DM versions can block here indefinitely.
}
private void initializeDbmsOutputIfNeeded(String sql) throws SQLException {
if (!dbmsOutputInitializationSupported || !isDbmsOutputStatement(sql)) {
return; return;
} }
Connection connection = requireConnected();
Object physicalConnection = connection.unwrap(DmdbConnection.class);
synchronized (dbmsOutputInitializedConnections) {
if (dbmsOutputInitializedConnections.containsKey(physicalConnection)) {
return;
}
dbmsOutputInitializedConnections.put(physicalConnection, Boolean.TRUE);
}
Integer originalNetworkTimeout = applyDbmsOutputNetworkTimeout(connection); Integer originalNetworkTimeout = applyDbmsOutputNetworkTimeout(connection);
SQLException setupError = null; SQLException setupError = null;
try (Statement statement = connection.createStatement()) { try (Statement statement = connection.createStatement()) {
@@ -161,6 +178,25 @@ public final class DamengAgent extends AbstractJdbcAgent {
throw setupError; throw setupError;
} }
private static boolean isDbmsOutputStatement(String sql) {
if (sql == null) {
return false;
}
int start = skipSqlTrivia(sql, 0);
return startsWithKeyword(sql, start, "CALL")
|| startsWithKeyword(sql, start, "BEGIN")
|| startsWithKeyword(sql, start, "DECLARE")
|| startsWithKeyword(sql, start, "EXEC")
|| startsWithKeyword(sql, start, "EXECUTE");
}
private static boolean startsWithKeyword(String sql, int start, String keyword) {
int end = start + keyword.length();
return end <= sql.length()
&& sql.regionMatches(true, start, keyword, 0, keyword.length())
&& (end == sql.length() || !isIdentifierPart(sql.charAt(end)));
}
private static Integer applyDbmsOutputNetworkTimeout(Connection connection) throws SQLException { private static Integer applyDbmsOutputNetworkTimeout(Connection connection) throws SQLException {
try { try {
int originalNetworkTimeout = connection.getNetworkTimeout(); int originalNetworkTimeout = connection.getNetworkTimeout();
@@ -1653,6 +1689,7 @@ public final class DamengAgent extends AbstractJdbcAgent {
// DM JDBC reports raw EXPLAIN as an update count; its driver API is the only source of plan rows. // DM JDBC reports raw EXPLAIN as an update count; its driver API is the only source of plan rows.
return executeExplainQuery(explainSql, schema, options); return executeExplainQuery(explainSql, schema, options);
} }
uncheckedVoid(() -> initializeDbmsOutputIfNeeded(sql));
return JdbcExecutor.current().execute( return JdbcExecutor.current().execute(
requireConnected(), requireConnected(),
sql, sql,
@@ -1776,6 +1813,7 @@ public final class DamengAgent extends AbstractJdbcAgent {
false false
); );
} }
uncheckedVoid(() -> initializeDbmsOutputIfNeeded(sql));
return JdbcExecutor.current().executePage( return JdbcExecutor.current().executePage(
requireConnected(), requireConnected(),
sql, sql,
@@ -40,7 +40,7 @@ class DamengAgentTest extends JdbcFakeExecutionBehaviorTest {
@Override @Override
protected String resultSetSql() { protected String resultSetSql() {
return "CALL SP_SAMPLE()"; return "VALUES (1)";
} }
@Test @Test
@@ -60,7 +60,7 @@ class DamengAgentTest extends JdbcFakeExecutionBehaviorTest {
} }
@Test @Test
void physicalConnectionsEnableDbmsOutputWithoutChangingUserSql() throws Exception { void physicalConnectionsDoNotEnableDbmsOutput() throws Exception {
List<String> executedSql = new ArrayList<>(); List<String> executedSql = new ArrayList<>();
List<Integer> queryTimeouts = new ArrayList<>(); List<Integer> queryTimeouts = new ArrayList<>();
List<Integer> networkTimeouts = new ArrayList<>(); List<Integer> networkTimeouts = new ArrayList<>();
@@ -68,77 +68,132 @@ class DamengAgentTest extends JdbcFakeExecutionBehaviorTest {
agent.afterPhysicalConnect(null, printMessageConnection(null, executedSql, queryTimeouts, networkTimeouts)); agent.afterPhysicalConnect(null, printMessageConnection(null, executedSql, queryTimeouts, networkTimeouts));
assertEquals(List.of(5), queryTimeouts); assertTrue(queryTimeouts.isEmpty());
assertEquals(List.of(5_000, 0), networkTimeouts); assertTrue(networkTimeouts.isEmpty());
assertEquals(List.of("BEGIN DBMS_OUTPUT.ENABLE(1000000); END;"), executedSql); assertTrue(executedSql.isEmpty());
} }
@Test @Test
void physicalConnectionsIgnoreUnsupportedOrRestrictedDbmsOutput() { void ordinaryQueriesDoNotEnableDbmsOutput() {
assertDoesNotThrow(() -> new DamengAgent().afterPhysicalConnect( List<String> executedSql = new ArrayList<>();
null, DamengAgent agent = new DamengAgent();
failingDbmsOutputConnection(new SQLFeatureNotSupportedException("unsupported", "0A000")) TestSupport.setPrivateConnection(agent, printMessageConnection(null, executedSql));
));
assertDoesNotThrow(() -> new DamengAgent().afterPhysicalConnect( agent.executeQuery("SELECT 1 FROM DUAL", null, new ExecuteQueryOptions());
null,
failingDbmsOutputConnection(new SQLException("permission denied", "42000")) assertEquals(List.of("SELECT 1 FROM DUAL"), executedSql);
));
} }
@Test @Test
void physicalConnectionsDisableDbmsOutputAfterInitializationTimeout() { void outputStatementsEnableDbmsOutputAfterCommentsWithKeywordBoundaries() {
assertLazilyEnablesDbmsOutput("CALL LOG_ONLY_PROCEDURE()");
assertLazilyEnablesDbmsOutput(" -- leading comment\nBEGIN NULL; END;");
assertLazilyEnablesDbmsOutput("/* leading block */ DECLARE value INT; BEGIN NULL; END;");
assertLazilyEnablesDbmsOutput("EXEC LOG_ONLY_PROCEDURE");
assertLazilyEnablesDbmsOutput("EXECUTE LOG_ONLY_PROCEDURE");
assertDoesNotEnableDbmsOutput("CALLBACK()");
assertDoesNotEnableDbmsOutput("BEGINNING SELECT 1");
assertDoesNotEnableDbmsOutput("DECLARE_VALUE INT");
assertDoesNotEnableDbmsOutput("EXECUTOR RUN");
assertDoesNotEnableDbmsOutput("EXECUTE_IMMEDIATE 'SELECT 1'");
assertDoesNotEnableDbmsOutput("/* CALL hidden in a comment */ SELECT 1");
}
@Test
void outputStatementsEnableDbmsOutputOncePerPhysicalConnection() {
List<String> firstConnectionSql = new ArrayList<>();
List<String> secondConnectionSql = new ArrayList<>();
Object firstPhysicalConnection = new Object();
Object secondPhysicalConnection = new Object();
DamengAgent agent = new DamengAgent();
TestSupport.setPrivateConnection(
agent,
printMessageConnection(null, firstConnectionSql, firstPhysicalConnection, null)
);
agent.executeQuery("CALL FIRST_PROCEDURE()", null, new ExecuteQueryOptions());
TestSupport.setPrivateConnection(
agent,
printMessageConnection(null, firstConnectionSql, firstPhysicalConnection, null)
);
agent.executeQuery("BEGIN NULL; END;", null, new ExecuteQueryOptions());
TestSupport.setPrivateConnection(
agent,
printMessageConnection(null, secondConnectionSql, secondPhysicalConnection, null)
);
agent.executeQuery("EXEC SECOND_PROCEDURE", null, new ExecuteQueryOptions());
assertEquals(List.of(
"BEGIN DBMS_OUTPUT.ENABLE(1000000); END;",
"CALL FIRST_PROCEDURE()",
"BEGIN NULL; END;"
), firstConnectionSql);
assertEquals(List.of(
"BEGIN DBMS_OUTPUT.ENABLE(1000000); END;",
"EXEC SECOND_PROCEDURE"
), secondConnectionSql);
}
@Test
void unsupportedOrRestrictedDbmsOutputDoesNotBlockUserSql() {
assertDbmsOutputFailureFallsBack(new SQLFeatureNotSupportedException("unsupported", "0A000"));
assertDbmsOutputFailureFallsBack(new SQLException("permission denied", "42000"));
}
@Test
void dbmsOutputTimeoutDisablesFutureAttempts() {
DamengAgent agent = new DamengAgent(); DamengAgent agent = new DamengAgent();
SQLException timeout = new SQLException("network communication failed"); SQLException timeout = new SQLException("network communication failed");
timeout.initCause(new SocketTimeoutException("Read timed out")); timeout.initCause(new SocketTimeoutException("Read timed out"));
List<String> retrySql = new ArrayList<>(); List<String> firstConnectionSql = new ArrayList<>();
List<Integer> networkTimeouts = new ArrayList<>(); List<String> retryConnectionSql = new ArrayList<>();
List<Integer> firstNetworkTimeouts = new ArrayList<>();
assertSame(timeout, assertThrows( TestSupport.setPrivateConnection(agent, statementConnection(
SQLException.class,
() -> agent.afterPhysicalConnect(
null,
statementConnection(null, new ArrayList<>(), new ArrayList<>(), networkTimeouts, timeout)
)
));
assertEquals(List.of(5_000), networkTimeouts);
assertDoesNotThrow(() -> agent.afterPhysicalConnect(
null, null,
printMessageConnection(null, retrySql) firstConnectionSql,
new ArrayList<>(),
firstNetworkTimeouts,
new Object(),
timeout
)); ));
assertTrue(retrySql.isEmpty()); RuntimeException error = assertThrows(
RuntimeException.class,
() -> agent.executeQuery("CALL FIRST_PROCEDURE()", null, new ExecuteQueryOptions())
);
assertSame(timeout, error.getCause());
assertEquals(List.of("BEGIN DBMS_OUTPUT.ENABLE(1000000); END;"), firstConnectionSql);
assertEquals(List.of(5_000), firstNetworkTimeouts);
TestSupport.setPrivateConnection(agent, printMessageConnection(
null,
retryConnectionSql,
new Object(),
null
));
agent.executeQuery("CALL RETRY_PROCEDURE()", null, new ExecuteQueryOptions());
assertEquals(List.of("CALL RETRY_PROCEDURE()"), retryConnectionSql);
} }
@Test @Test
void physicalConnectionsPropagateConnectionFailures() { void dbmsOutputInitializationPropagatesConnectionFailures() {
DamengAgent agent = new DamengAgent();
SQLException transientFailure = new SQLTransientConnectionException("connection closed"); SQLException transientFailure = new SQLTransientConnectionException("connection closed");
SQLException sqlStateFailure = new SQLException("connection failure", "08006"); SQLException sqlStateFailure = new SQLException("connection failure", "08006");
SQLException wrappedFailure = new SQLException("permission denied", "42000"); SQLException wrappedFailure = new SQLException("permission denied", "42000");
wrappedFailure.initCause(new SQLTransientConnectionException("connection closed")); wrappedFailure.initCause(new SQLTransientConnectionException("connection closed"));
assertSame(transientFailure, assertThrows( assertDbmsOutputFailurePropagates(transientFailure);
SQLException.class, assertDbmsOutputFailurePropagates(sqlStateFailure);
() -> agent.afterPhysicalConnect(null, failingDbmsOutputConnection(transientFailure)) assertDbmsOutputFailurePropagates(wrappedFailure);
));
assertSame(sqlStateFailure, assertThrows(
SQLException.class,
() -> agent.afterPhysicalConnect(null, failingDbmsOutputConnection(sqlStateFailure))
));
assertSame(wrappedFailure, assertThrows(
SQLException.class,
() -> agent.afterPhysicalConnect(null, failingDbmsOutputConnection(wrappedFailure))
));
} }
@Test @Test
void physicalConnectionsPropagateUnrelatedSetupFailures() { void dbmsOutputInitializationPropagatesUnrelatedSetupFailures() {
DamengAgent agent = new DamengAgent();
SQLException failure = new SQLException("resource busy", "HY000"); SQLException failure = new SQLException("resource busy", "HY000");
assertSame(failure, assertThrows( assertDbmsOutputFailurePropagates(failure);
SQLException.class,
() -> agent.afterPhysicalConnect(null, failingDbmsOutputConnection(failure))
));
} }
@Test @Test
@@ -155,7 +210,29 @@ class DamengAgentTest extends JdbcFakeExecutionBehaviorTest {
assertEquals(List.of("Message"), result.getColumns()); assertEquals(List.of("Message"), result.getColumns());
assertEquals(List.of(List.of("first"), List.of("中文日志")), result.getRows()); assertEquals(List.of(List.of("first"), List.of("中文日志")), result.getRows());
assertEquals(List.of("CALL LOG_ONLY_PROCEDURE('input')"), executedSql); assertEquals(List.of(
"BEGIN DBMS_OUTPUT.ENABLE(1000000); END;",
"CALL LOG_ONLY_PROCEDURE('input')"
), executedSql);
}
@Test
void pagedOutputStatementsEnableDbmsOutputLazily() {
List<String> executedSql = new ArrayList<>();
DamengAgent agent = new DamengAgent();
TestSupport.setPrivateConnection(agent, printMessageConnection("paged message", executedSql));
QueryPageResult result = agent.executeQueryPage(
"/* output */ CALL PAGED_LOG_PROCEDURE()",
null,
new QueryPageOptions(100, 100, 1000)
);
assertEquals(List.of(
"BEGIN DBMS_OUTPUT.ENABLE(1000000); END;",
"/* output */ CALL PAGED_LOG_PROCEDURE()"
), executedSql);
assertEquals(List.of(List.of("paged message")), result.getRows());
} }
@Test @Test
@@ -404,11 +481,30 @@ class DamengAgentTest extends JdbcFakeExecutionBehaviorTest {
List<Integer> queryTimeouts, List<Integer> queryTimeouts,
List<Integer> networkTimeouts List<Integer> networkTimeouts
) { ) {
return statementConnection(printMessage, executedSql, queryTimeouts, networkTimeouts, null); return statementConnection(
printMessage,
executedSql,
queryTimeouts,
networkTimeouts,
new Object(),
null
);
} }
private static Connection failingDbmsOutputConnection(SQLException failure) { private static Connection printMessageConnection(
return statementConnection(null, new ArrayList<>(), new ArrayList<>(), new ArrayList<>(), failure); String printMessage,
List<String> executedSql,
Object physicalConnection,
SQLException dbmsOutputFailure
) {
return statementConnection(
printMessage,
executedSql,
new ArrayList<>(),
new ArrayList<>(),
physicalConnection,
dbmsOutputFailure
);
} }
private static Connection statementConnection( private static Connection statementConnection(
@@ -416,15 +512,17 @@ class DamengAgentTest extends JdbcFakeExecutionBehaviorTest {
List<String> executedSql, List<String> executedSql,
List<Integer> queryTimeouts, List<Integer> queryTimeouts,
List<Integer> networkTimeouts, List<Integer> networkTimeouts,
SQLException executeFailure Object physicalConnection,
SQLException dbmsOutputFailure
) { ) {
InvocationHandler statementHandler = (Object unused, Method method, Object[] args) -> { InvocationHandler statementHandler = (Object unused, Method method, Object[] args) -> {
switch (method.getName()) { switch (method.getName()) {
case "execute": case "execute":
if (executeFailure != null) { String sql = (String) args[0];
throw executeFailure; executedSql.add(sql);
if (dbmsOutputFailure != null && sql.contains("DBMS_OUTPUT.ENABLE")) {
throw dbmsOutputFailure;
} }
executedSql.add((String) args[0]);
return false; return false;
case "setQueryTimeout": case "setQueryTimeout":
queryTimeouts.add((Integer) args[0]); queryTimeouts.add((Integer) args[0]);
@@ -451,6 +549,16 @@ class DamengAgentTest extends JdbcFakeExecutionBehaviorTest {
case "setNetworkTimeout": case "setNetworkTimeout":
networkTimeouts.add((Integer) args[1]); networkTimeouts.add((Integer) args[1]);
return null; return null;
case "isWrapperFor":
return ((Class<?>) args[0]).getName().equals("dm.jdbc.driver.DmdbConnection");
case "unwrap":
return ((Class<?>) args[0]).getName().equals("dm.jdbc.driver.DmdbConnection")
? physicalConnection
: null;
case "equals":
return unused == args[0];
case "hashCode":
return System.identityHashCode(unused);
default: default:
return defaultValue(method.getReturnType()); return defaultValue(method.getReturnType());
} }
@@ -462,6 +570,56 @@ class DamengAgentTest extends JdbcFakeExecutionBehaviorTest {
); );
} }
private static void assertLazilyEnablesDbmsOutput(String sql) {
List<String> executedSql = new ArrayList<>();
DamengAgent agent = new DamengAgent();
TestSupport.setPrivateConnection(agent, printMessageConnection(null, executedSql));
agent.executeQuery(sql, null, new ExecuteQueryOptions());
assertEquals(2, executedSql.size());
assertEquals("BEGIN DBMS_OUTPUT.ENABLE(1000000); END;", executedSql.get(0));
}
private static void assertDoesNotEnableDbmsOutput(String sql) {
List<String> executedSql = new ArrayList<>();
DamengAgent agent = new DamengAgent();
TestSupport.setPrivateConnection(agent, printMessageConnection(null, executedSql));
agent.executeQuery(sql, null, new ExecuteQueryOptions());
assertEquals(List.of(sql), executedSql);
}
private static void assertDbmsOutputFailureFallsBack(SQLException failure) {
List<String> executedSql = new ArrayList<>();
DamengAgent agent = new DamengAgent();
TestSupport.setPrivateConnection(agent, printMessageConnection(null, executedSql, new Object(), failure));
assertDoesNotThrow(() -> agent.executeQuery("CALL LOG_ONLY_PROCEDURE()", null, new ExecuteQueryOptions()));
assertDoesNotThrow(() -> agent.executeQuery("BEGIN NULL; END;", null, new ExecuteQueryOptions()));
assertEquals(List.of(
"BEGIN DBMS_OUTPUT.ENABLE(1000000); END;",
"CALL LOG_ONLY_PROCEDURE()",
"BEGIN NULL; END;"
), executedSql);
}
private static void assertDbmsOutputFailurePropagates(SQLException failure) {
List<String> executedSql = new ArrayList<>();
DamengAgent agent = new DamengAgent();
TestSupport.setPrivateConnection(agent, printMessageConnection(null, executedSql, new Object(), failure));
RuntimeException error = assertThrows(
RuntimeException.class,
() -> agent.executeQuery("CALL LOG_ONLY_PROCEDURE()", null, new ExecuteQueryOptions())
);
assertSame(failure, error.getCause());
assertEquals(List.of("BEGIN DBMS_OUTPUT.ENABLE(1000000); END;"), executedSql);
}
private static Object defaultValue(Class<?> type) { private static Object defaultValue(Class<?> type) {
if (type == Boolean.TYPE) return false; if (type == Boolean.TYPE) return false;
if (type == Byte.TYPE) return (byte) 0; if (type == Byte.TYPE) return (byte) 0;