From e49dc884ecf7484c3b2932d5563ca0a83fcee469 Mon Sep 17 00:00:00 2001 From: yh263208 Date: Tue, 9 Apr 2024 11:08:43 +0800 Subject: [PATCH] make timeout settings for pldebug can be overwrited by session init script --- .../pldebug/session/AbstractDebugSession.java | 17 ++++++++++++----- .../pldebug/session/DebuggeeSession.java | 18 ++++++++---------- .../pldebug/session/DebuggerSession.java | 12 +++++++----- 3 files changed, 27 insertions(+), 20 deletions(-) diff --git a/server/odc-service/src/main/java/com/oceanbase/odc/service/pldebug/session/AbstractDebugSession.java b/server/odc-service/src/main/java/com/oceanbase/odc/service/pldebug/session/AbstractDebugSession.java index 492090b5e6..64f91e3f2b 100644 --- a/server/odc-service/src/main/java/com/oceanbase/odc/service/pldebug/session/AbstractDebugSession.java +++ b/server/odc-service/src/main/java/com/oceanbase/odc/service/pldebug/session/AbstractDebugSession.java @@ -73,7 +73,6 @@ public abstract class AbstractDebugSession implements AutoCloseable { public abstract boolean detectSessionAlive(); public List executeProcedure(DBProcedure procedure) { - try { // -1 means statement queryTimeout will be default 0, // By default there is no limit on the amount of time allowed for a running statement to complete @@ -109,9 +108,9 @@ protected void acquireNewConnection(ConnectionSession connectionSession, this.connection = newDataSource.getConnection(); } - protected DebugDataSource acquireDataSource(ConnectionSession connectionSession) { + protected DebugDataSource acquireDataSource(ConnectionSession connectionSession, List initSqls) { ConnectionConfig config = (ConnectionConfig) ConnectionSessionUtil.getConnectionConfig(connectionSession); - DebugDataSource dataSource = new DebugDataSource(config); + DebugDataSource dataSource = new DebugDataSource(config, initSqls); String schema = ConnectionSessionUtil.getCurrentSchema(connectionSession); String host; Integer port; @@ -190,18 +189,26 @@ protected void enableDbmsOutput(Statement statement) { static class DebugDataSource extends SingleConnectionDataSource { + private final List initSqls; private final SessionCreatedInitializer initializer; - public DebugDataSource(@NonNull ConnectionConfig connectionConfig) { + public DebugDataSource(@NonNull ConnectionConfig connectionConfig, List initSqls) { + this.initSqls = initSqls; this.initializer = new SessionCreatedInitializer(connectionConfig, true); } @Override protected void prepareConnection(Connection con) throws SQLException { super.prepareConnection(con); + if (CollectionUtils.isNotEmpty(this.initSqls)) { + try (Statement statement = con.createStatement()) { + for (String stmt : this.initSqls) { + statement.execute(stmt); + } + } + } this.initializer.init(con); } - } } diff --git a/server/odc-service/src/main/java/com/oceanbase/odc/service/pldebug/session/DebuggeeSession.java b/server/odc-service/src/main/java/com/oceanbase/odc/service/pldebug/session/DebuggeeSession.java index eced22c5f6..7405771b94 100644 --- a/server/odc-service/src/main/java/com/oceanbase/odc/service/pldebug/session/DebuggeeSession.java +++ b/server/odc-service/src/main/java/com/oceanbase/odc/service/pldebug/session/DebuggeeSession.java @@ -19,7 +19,9 @@ import java.sql.SQLException; import java.sql.SQLSyntaxErrorException; import java.sql.Statement; +import java.util.Arrays; import java.util.Collections; +import java.util.List; import java.util.Objects; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; @@ -58,26 +60,22 @@ public class DebuggeeSession extends AbstractDebugSession { public DebuggeeSession(ConnectionSession connectionSession, ThreadPoolExecutor debugThreadPoolExecutor, StartPLDebugReq req) throws Exception { - - acquireNewConnection(connectionSession, () -> acquireDataSource(connectionSession)); - + // 设置超时时间, 单位:us + // 设置debug工作线程的超时时间,单位:s 2min + List initSqls = Arrays.asList( + String.format("set session ob_query_timeout = %s;", DEBUG_TIMEOUT_MS * 1000), + String.format("select dbms_debug.set_timeout(%s) from dual;", 120)); + acquireNewConnection(connectionSession, () -> acquireDataSource(connectionSession, initSqls)); // OceanBaseConnection can accept null as executor // 0 for timeout means wait infinitely connection.setNetworkTimeout(null, 0); - try (Statement stmt = connection.createStatement()) { - // 设置超时时间, 单位:us - stmt.execute(String.format("set session ob_query_timeout = %s;", DEBUG_TIMEOUT_MS * 1000)); - // 设置debug工作线程的超时时间,单位:s - stmt.execute(String.format("select dbms_debug.set_timeout(%s) from dual;", 120)); // 2min // 设置debug工作线程的超时行为 DBPLParam param = DBPLParam.of("behaviour", DBPLParamMode.IN, "int"); param.setDefaultValue(String.valueOf(DEBUGGEE_TIMEOUT_BEHAVIOUR)); - DBProcedure dbProcedure = DBProcedure.of("DBMS_DEBUG", "SET_TIMEOUT_BEHAVIOUR", Collections.singletonList(param)); executeProcedure(dbProcedure); - // 初始化debug_id stmt.executeQuery("select dbms_debug.initialize() from dual;"); try (ResultSet resultSet = stmt.getResultSet()) { diff --git a/server/odc-service/src/main/java/com/oceanbase/odc/service/pldebug/session/DebuggerSession.java b/server/odc-service/src/main/java/com/oceanbase/odc/service/pldebug/session/DebuggerSession.java index c7fcff5b09..2fe1c90edf 100644 --- a/server/odc-service/src/main/java/com/oceanbase/odc/service/pldebug/session/DebuggerSession.java +++ b/server/odc-service/src/main/java/com/oceanbase/odc/service/pldebug/session/DebuggerSession.java @@ -19,6 +19,7 @@ import java.sql.Statement; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.List; @@ -108,11 +109,12 @@ public DebuggerSession(DebuggeeSession debuggeeSession, StartPLDebugReq req, boo this.syncEnabled = syncEnabled; // Debugger must connect to database host the same as debuggee + // 设置超时时间, 单位:us + List initSqls = Collections.singletonList( + String.format("set session ob_query_timeout = %s;", DEBUG_TIMEOUT_MS * 1000)); acquireNewConnection(debuggeeSession.getConnectionSession(), - () -> cloneDataSource(debuggeeSession.getNewDataSource())); + () -> cloneDataSource(debuggeeSession.getNewDataSource(), initSqls)); try (Statement stmt = connection.createStatement()) { - // 设置超时时间, 单位:us - stmt.execute(String.format("set session ob_query_timeout = %s;", DEBUG_TIMEOUT_MS * 1000)); // 绑定调试目标id stmt.execute(String.format("call dbms_debug.attach_session(%s);", debuggeeSession.getDebugId())); } catch (Exception e) { @@ -178,9 +180,9 @@ public DebuggerSession(DebuggeeSession debuggeeSession, StartPLDebugReq req, boo } } - private DebugDataSource cloneDataSource(DebugDataSource originDataSource) { + private DebugDataSource cloneDataSource(DebugDataSource originDataSource, List initSqls) { ConnectionConfig config = (ConnectionConfig) ConnectionSessionUtil.getConnectionConfig(connectionSession); - DebugDataSource debuggerDataSource = new DebugDataSource(config); + DebugDataSource debuggerDataSource = new DebugDataSource(config, initSqls); debuggerDataSource.setUrl(originDataSource.getUrl()); debuggerDataSource.setUsername(originDataSource.getUsername()); debuggerDataSource.setPassword(originDataSource.getPassword());