Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(pl-debug): make timeout settings can be overwritten by session init script #2179

Merged
merged 1 commit into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ public abstract class AbstractDebugSession implements AutoCloseable {
public abstract boolean detectSessionAlive();

public List<DBPLParam> 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
Expand Down Expand Up @@ -109,9 +108,9 @@ protected void acquireNewConnection(ConnectionSession connectionSession,
this.connection = newDataSource.getConnection();
}

protected DebugDataSource acquireDataSource(ConnectionSession connectionSession) {
protected DebugDataSource acquireDataSource(ConnectionSession connectionSession, List<String> 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;
Expand Down Expand Up @@ -190,18 +189,26 @@ protected void enableDbmsOutput(Statement statement) {

static class DebugDataSource extends SingleConnectionDataSource {

private final List<String> initSqls;
private final SessionCreatedInitializer initializer;

public DebugDataSource(@NonNull ConnectionConfig connectionConfig) {
public DebugDataSource(@NonNull ConnectionConfig connectionConfig, List<String> 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);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> 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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> 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) {
Expand Down Expand Up @@ -178,9 +180,9 @@ public DebuggerSession(DebuggeeSession debuggeeSession, StartPLDebugReq req, boo
}
}

private DebugDataSource cloneDataSource(DebugDataSource originDataSource) {
private DebugDataSource cloneDataSource(DebugDataSource originDataSource, List<String> 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());
Expand Down
Loading