From cb2055f3494790f78aab253b23e6ac6f482c9990 Mon Sep 17 00:00:00 2001 From: zzg19950727 <1071026277@qq.com> Date: Fri, 1 Sep 2023 07:44:33 +0000 Subject: [PATCH] fix nested sql bug --- src/sql/monitor/ob_sql_plan.cpp | 71 +++++++++++++++++++++++---------- src/sql/monitor/ob_sql_plan.h | 12 ++++-- 2 files changed, 59 insertions(+), 24 deletions(-) diff --git a/src/sql/monitor/ob_sql_plan.cpp b/src/sql/monitor/ob_sql_plan.cpp index c086920dc..b64eb4529 100644 --- a/src/sql/monitor/ob_sql_plan.cpp +++ b/src/sql/monitor/ob_sql_plan.cpp @@ -65,20 +65,12 @@ namespace sql { ObSqlPlan::ObSqlPlan(common::ObIAllocator &allocator) - :allocator_(allocator), - save_nested_count_(-1), - saved_session_(NULL) + :allocator_(allocator) { - } ObSqlPlan::~ObSqlPlan() { - save_nested_count_ = -1; - if (OB_NOT_NULL(saved_session_)) { - allocator_.free(saved_session_); - saved_session_ = NULL; - } } int ObSqlPlan::store_sql_plan(ObLogPlan* log_plan, ObPhysicalPlan* phy_plan) @@ -240,6 +232,9 @@ int ObSqlPlan::inner_store_sql_plan_for_explain(ObExecContext *ctx, ObInnerSQLConnectionPool *pool = NULL; sql::ObSQLSessionInfo *session = NULL; ObInnerSQLConnection *conn = NULL; + ObSQLSessionInfo::StmtSavedValue *saved_session = NULL; + transaction::ObTxDesc *save_tx_desc = NULL; + int64_t save_nested_count = 0; int64_t affected_rows = 0; ObSqlString sql; if (OB_ISNULL(ctx) || @@ -254,7 +249,10 @@ int ObSqlPlan::inner_store_sql_plan_for_explain(ObExecContext *ctx, } else if (OB_ISNULL(conn)) { ret = OB_ERR_UNEXPECTED; LOG_WARN("unexpect null sql connection", K(ret)); - } else if (OB_FAIL(prepare_and_store_session(session))) { + } else if (OB_FAIL(prepare_and_store_session(session, + saved_session, + save_tx_desc, + save_nested_count))) { LOG_WARN("failed to begin nested session", K(ret)); } for (int64_t i = 0; OB_SUCC(ret) && i < sql_plan_infos.count(); ++i) { @@ -392,7 +390,10 @@ int ObSqlPlan::inner_store_sql_plan_for_explain(ObExecContext *ctx, ctx->get_sql_proxy()->close(conn, ret); } if (OB_NOT_NULL(session)) { - int end_ret = restore_session(session); + int end_ret = restore_session(session, + saved_session, + save_tx_desc, + save_nested_count); if (OB_SUCCESS != end_ret) { LOG_WARN("failed to restore session", K(end_ret), K(ret)); if (OB_SUCCESS == ret) { @@ -2346,9 +2347,16 @@ int ObSqlPlan::plan_text_to_strings(PlanText &plan_text, return ret; } -int ObSqlPlan::prepare_and_store_session(ObSQLSessionInfo *session) { +int ObSqlPlan::prepare_and_store_session(ObSQLSessionInfo *session, + ObSQLSessionInfo::StmtSavedValue *&session_value, + transaction::ObTxDesc *&tx_desc, + int64_t &nested_count) +{ int ret = OB_SUCCESS; void *ptr = NULL; + session_value = NULL; + tx_desc = NULL; + nested_count = 0; if (OB_ISNULL(session)) { ret = OB_ERR_UNEXPECTED; LOG_WARN("unexpected session value", K(ret)); @@ -2356,29 +2364,52 @@ int ObSqlPlan::prepare_and_store_session(ObSQLSessionInfo *session) { ret = OB_ALLOCATE_MEMORY_FAILED; LOG_WARN("failed to alloc memory for saved session value", K(ret)); } else { - saved_session_ = new(ptr) sql::ObSQLSessionInfo::StmtSavedValue(); - if (OB_FAIL(session->save_session(*saved_session_))) { + session_value = new(ptr) sql::ObSQLSessionInfo::StmtSavedValue(); + if (OB_FAIL(session->save_session(*session_value))) { LOG_WARN("failed to save session", K(ret)); } else { - save_nested_count_ = session->get_nested_count(); + nested_count = session->get_nested_count(); session->set_query_start_time(ObTimeUtility::current_time()); session->set_inner_session(); session->set_nested_count(-1); + //write plan to plan table + session->set_autocommit(true); + tx_desc = session->get_tx_desc(); + session->get_tx_desc() = NULL; } } return ret; } -int ObSqlPlan::restore_session(ObSQLSessionInfo *session) { +int ObSqlPlan::restore_session(ObSQLSessionInfo *session, + ObSQLSessionInfo::StmtSavedValue *&session_value, + transaction::ObTxDesc *tx_desc, + int64_t nested_count) +{ int ret = OB_SUCCESS; - if (OB_ISNULL(session) || OB_ISNULL(saved_session_)) { + if (OB_ISNULL(session) || OB_ISNULL(session_value)) { ret = OB_ERR_UNEXPECTED; LOG_WARN("unexpected session value or saved session value", K(ret)); - } else if (OB_FAIL(session->restore_session(*saved_session_))) { + } else if (OB_FAIL(session->restore_session(*session_value))) { LOG_WARN("failed to restore session", K(ret)); } else { - session->set_nested_count(save_nested_count_); - saved_session_->reset(); + transaction::ObTxDesc *new_tx_desc = session->get_tx_desc(); + session->set_nested_count(nested_count); + session->get_tx_desc() = tx_desc; + session_value->reset(); + allocator_.free(session_value); + session_value = 0; + // release curr + if (OB_NOT_NULL(new_tx_desc)) { + auto txs = MTL(transaction::ObTransService*); + if (OB_ISNULL(txs)) { + ret = OB_ERR_UNEXPECTED; + LOG_ERROR("can not acquire MTL TransService", KR(ret)); + new_tx_desc->dump_and_print_trace(); + } else if (OB_FAIL(txs->release_tx(*new_tx_desc))) { + LOG_WARN("failed to release tx desc", K(ret)); + } + } } return ret; } diff --git a/src/sql/monitor/ob_sql_plan.h b/src/sql/monitor/ob_sql_plan.h index 18b264b24..24158da5b 100644 --- a/src/sql/monitor/ob_sql_plan.h +++ b/src/sql/monitor/ob_sql_plan.h @@ -263,9 +263,15 @@ private: int refine_buffer(PlanText &plan_text); - int prepare_and_store_session(ObSQLSessionInfo *session); + int prepare_and_store_session(ObSQLSessionInfo *session, + ObSQLSessionInfo::StmtSavedValue *&session_value, + transaction::ObTxDesc *&tx_desc, + int64_t &nested_count); - int restore_session(ObSQLSessionInfo *session); + int restore_session(ObSQLSessionInfo *session, + ObSQLSessionInfo::StmtSavedValue *&session_value, + transaction::ObTxDesc *tx_desc, + int64_t nested_count); public: static int format_one_output_expr(char *buf, @@ -279,8 +285,6 @@ public: private: common::ObIAllocator &allocator_; - int64_t save_nested_count_; - ObSQLSessionInfo::StmtSavedValue *saved_session_; }; } // end of namespace sql