diff --git a/agents/drivers/dameng/src/main/java/com/dbx/agent/dameng/DamengAgent.java b/agents/drivers/dameng/src/main/java/com/dbx/agent/dameng/DamengAgent.java index 40b020d671..30d44130c2 100644 --- a/agents/drivers/dameng/src/main/java/com/dbx/agent/dameng/DamengAgent.java +++ b/agents/drivers/dameng/src/main/java/com/dbx/agent/dameng/DamengAgent.java @@ -18,6 +18,7 @@ import com.dbx.agent.QueryPageResult; import com.dbx.agent.QueryResult; import com.dbx.agent.TableInfo; import com.dbx.agent.TriggerInfo; +import dm.jdbc.driver.DmdbConnection; import java.io.PrintStream; import java.io.Reader; import java.net.SocketTimeoutException; @@ -49,6 +50,7 @@ import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Set; +import java.util.WeakHashMap; import java.util.logging.Level; import java.util.logging.Logger; @@ -89,6 +91,8 @@ public final class DamengAgent extends AbstractJdbcAgent { """.stripIndent().trim(); private String connectedUsername; private volatile boolean dbmsOutputInitializationSupported = true; + private final Map dbmsOutputInitializedConnections = + Collections.synchronizedMap(new WeakHashMap<>()); @Override protected String driverClass() { @@ -118,11 +122,24 @@ public final class DamengAgent extends AbstractJdbcAgent { } @Override - protected void afterPhysicalConnect(ConnectParams params, Connection connection) throws SQLException { - if (!dbmsOutputInitializationSupported) { + protected void afterPhysicalConnect(ConnectParams params, Connection connection) { + // 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; } + 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); SQLException setupError = null; try (Statement statement = connection.createStatement()) { @@ -161,6 +178,25 @@ public final class DamengAgent extends AbstractJdbcAgent { 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 { try { 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. return executeExplainQuery(explainSql, schema, options); } + uncheckedVoid(() -> initializeDbmsOutputIfNeeded(sql)); return JdbcExecutor.current().execute( requireConnected(), sql, @@ -1776,6 +1813,7 @@ public final class DamengAgent extends AbstractJdbcAgent { false ); } + uncheckedVoid(() -> initializeDbmsOutputIfNeeded(sql)); return JdbcExecutor.current().executePage( requireConnected(), sql, diff --git a/agents/drivers/dameng/src/test/java/com/dbx/agent/dameng/DamengAgentTest.java b/agents/drivers/dameng/src/test/java/com/dbx/agent/dameng/DamengAgentTest.java index 09cd4ce930..c193272c2f 100644 --- a/agents/drivers/dameng/src/test/java/com/dbx/agent/dameng/DamengAgentTest.java +++ b/agents/drivers/dameng/src/test/java/com/dbx/agent/dameng/DamengAgentTest.java @@ -40,7 +40,7 @@ class DamengAgentTest extends JdbcFakeExecutionBehaviorTest { @Override protected String resultSetSql() { - return "CALL SP_SAMPLE()"; + return "VALUES (1)"; } @Test @@ -60,7 +60,7 @@ class DamengAgentTest extends JdbcFakeExecutionBehaviorTest { } @Test - void physicalConnectionsEnableDbmsOutputWithoutChangingUserSql() throws Exception { + void physicalConnectionsDoNotEnableDbmsOutput() throws Exception { List executedSql = new ArrayList<>(); List queryTimeouts = new ArrayList<>(); List networkTimeouts = new ArrayList<>(); @@ -68,77 +68,132 @@ class DamengAgentTest extends JdbcFakeExecutionBehaviorTest { agent.afterPhysicalConnect(null, printMessageConnection(null, executedSql, queryTimeouts, networkTimeouts)); - assertEquals(List.of(5), queryTimeouts); - assertEquals(List.of(5_000, 0), networkTimeouts); - assertEquals(List.of("BEGIN DBMS_OUTPUT.ENABLE(1000000); END;"), executedSql); + assertTrue(queryTimeouts.isEmpty()); + assertTrue(networkTimeouts.isEmpty()); + assertTrue(executedSql.isEmpty()); } @Test - void physicalConnectionsIgnoreUnsupportedOrRestrictedDbmsOutput() { - assertDoesNotThrow(() -> new DamengAgent().afterPhysicalConnect( - null, - failingDbmsOutputConnection(new SQLFeatureNotSupportedException("unsupported", "0A000")) - )); - assertDoesNotThrow(() -> new DamengAgent().afterPhysicalConnect( - null, - failingDbmsOutputConnection(new SQLException("permission denied", "42000")) - )); + void ordinaryQueriesDoNotEnableDbmsOutput() { + List executedSql = new ArrayList<>(); + DamengAgent agent = new DamengAgent(); + TestSupport.setPrivateConnection(agent, printMessageConnection(null, executedSql)); + + agent.executeQuery("SELECT 1 FROM DUAL", null, new ExecuteQueryOptions()); + + assertEquals(List.of("SELECT 1 FROM DUAL"), executedSql); } @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 firstConnectionSql = new ArrayList<>(); + List 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(); SQLException timeout = new SQLException("network communication failed"); timeout.initCause(new SocketTimeoutException("Read timed out")); - List retrySql = new ArrayList<>(); - List networkTimeouts = new ArrayList<>(); + List firstConnectionSql = new ArrayList<>(); + List retryConnectionSql = new ArrayList<>(); + List firstNetworkTimeouts = new ArrayList<>(); - assertSame(timeout, assertThrows( - SQLException.class, - () -> agent.afterPhysicalConnect( - null, - statementConnection(null, new ArrayList<>(), new ArrayList<>(), networkTimeouts, timeout) - ) - )); - assertEquals(List.of(5_000), networkTimeouts); - assertDoesNotThrow(() -> agent.afterPhysicalConnect( + TestSupport.setPrivateConnection(agent, statementConnection( 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 - void physicalConnectionsPropagateConnectionFailures() { - DamengAgent agent = new DamengAgent(); + void dbmsOutputInitializationPropagatesConnectionFailures() { SQLException transientFailure = new SQLTransientConnectionException("connection closed"); SQLException sqlStateFailure = new SQLException("connection failure", "08006"); SQLException wrappedFailure = new SQLException("permission denied", "42000"); wrappedFailure.initCause(new SQLTransientConnectionException("connection closed")); - assertSame(transientFailure, assertThrows( - SQLException.class, - () -> agent.afterPhysicalConnect(null, failingDbmsOutputConnection(transientFailure)) - )); - assertSame(sqlStateFailure, assertThrows( - SQLException.class, - () -> agent.afterPhysicalConnect(null, failingDbmsOutputConnection(sqlStateFailure)) - )); - assertSame(wrappedFailure, assertThrows( - SQLException.class, - () -> agent.afterPhysicalConnect(null, failingDbmsOutputConnection(wrappedFailure)) - )); + assertDbmsOutputFailurePropagates(transientFailure); + assertDbmsOutputFailurePropagates(sqlStateFailure); + assertDbmsOutputFailurePropagates(wrappedFailure); } @Test - void physicalConnectionsPropagateUnrelatedSetupFailures() { - DamengAgent agent = new DamengAgent(); + void dbmsOutputInitializationPropagatesUnrelatedSetupFailures() { SQLException failure = new SQLException("resource busy", "HY000"); - assertSame(failure, assertThrows( - SQLException.class, - () -> agent.afterPhysicalConnect(null, failingDbmsOutputConnection(failure)) - )); + assertDbmsOutputFailurePropagates(failure); } @Test @@ -155,7 +210,29 @@ class DamengAgentTest extends JdbcFakeExecutionBehaviorTest { assertEquals(List.of("Message"), result.getColumns()); 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 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 @@ -404,11 +481,30 @@ class DamengAgentTest extends JdbcFakeExecutionBehaviorTest { List queryTimeouts, List networkTimeouts ) { - return statementConnection(printMessage, executedSql, queryTimeouts, networkTimeouts, null); + return statementConnection( + printMessage, + executedSql, + queryTimeouts, + networkTimeouts, + new Object(), + null + ); } - private static Connection failingDbmsOutputConnection(SQLException failure) { - return statementConnection(null, new ArrayList<>(), new ArrayList<>(), new ArrayList<>(), failure); + private static Connection printMessageConnection( + String printMessage, + List executedSql, + Object physicalConnection, + SQLException dbmsOutputFailure + ) { + return statementConnection( + printMessage, + executedSql, + new ArrayList<>(), + new ArrayList<>(), + physicalConnection, + dbmsOutputFailure + ); } private static Connection statementConnection( @@ -416,15 +512,17 @@ class DamengAgentTest extends JdbcFakeExecutionBehaviorTest { List executedSql, List queryTimeouts, List networkTimeouts, - SQLException executeFailure + Object physicalConnection, + SQLException dbmsOutputFailure ) { InvocationHandler statementHandler = (Object unused, Method method, Object[] args) -> { switch (method.getName()) { case "execute": - if (executeFailure != null) { - throw executeFailure; + String sql = (String) args[0]; + executedSql.add(sql); + if (dbmsOutputFailure != null && sql.contains("DBMS_OUTPUT.ENABLE")) { + throw dbmsOutputFailure; } - executedSql.add((String) args[0]); return false; case "setQueryTimeout": queryTimeouts.add((Integer) args[0]); @@ -451,6 +549,16 @@ class DamengAgentTest extends JdbcFakeExecutionBehaviorTest { case "setNetworkTimeout": networkTimeouts.add((Integer) args[1]); 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: return defaultValue(method.getReturnType()); } @@ -462,6 +570,56 @@ class DamengAgentTest extends JdbcFakeExecutionBehaviorTest { ); } + private static void assertLazilyEnablesDbmsOutput(String sql) { + List 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 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 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 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) { if (type == Boolean.TYPE) return false; if (type == Byte.TYPE) return (byte) 0;