diff --git a/src/sql/engine/ob_physical_plan_ctx.h b/src/sql/engine/ob_physical_plan_ctx.h index faf1406876..309b6f78ae 100644 --- a/src/sql/engine/ob_physical_plan_ctx.h +++ b/src/sql/engine/ob_physical_plan_ctx.h @@ -174,6 +174,15 @@ public: { return consistency_level_; } + bool check_consistency_level_validation(const bool contain_inner_table) + { + bool bool_ret = true; + if (contain_inner_table) { + // Statement which contain inner tables should be strong read; + bool_ret = (consistency_level_ == ObConsistencyLevel::STRONG); + } + return bool_ret; + } void restore_param_store(const int64_t param_count); // param store int reserve_param_space(int64_t param_count); diff --git a/src/sql/ob_sql_trans_control.cpp b/src/sql/ob_sql_trans_control.cpp index f45a68230a..fe8918aa68 100644 --- a/src/sql/ob_sql_trans_control.cpp +++ b/src/sql/ob_sql_trans_control.cpp @@ -730,12 +730,13 @@ int ObSqlTransControl::stmt_sanity_check_(ObSQLSessionInfo *session, int ret = OB_SUCCESS; ObConsistencyLevel current_consist_level = plan_ctx->get_consistency_level(); CK (current_consist_level != ObConsistencyLevel::INVALID_CONSISTENCY); + const bool contain_inner_table = plan->is_contain_inner_table(); // adjust stmt's consistency level if (OB_SUCC(ret)) { // Weak read statement with inner table should be converted to strong read. // For example, schema refresh statement; - if (plan->is_contain_inner_table() || + if (contain_inner_table || (!plan->is_plain_select() && current_consist_level != ObConsistencyLevel::STRONG)) { plan_ctx->set_consistency_level(ObConsistencyLevel::STRONG); } @@ -762,6 +763,13 @@ int ObSqlTransControl::stmt_sanity_check_(ObSQLSessionInfo *session, LOG_USER_ERROR(OB_NOT_SUPPORTED, "weak consistency under SERIALIZABLE and REPEATABLE-READ isolation level"); } } + if (OB_SUCC(ret) + && !plan_ctx->check_consistency_level_validation(contain_inner_table)) { + ret = OB_ERR_UNEXPECTED; + TRANS_LOG(ERROR, "unexpected consistency level", K(ret), K(contain_inner_table), + "current_level", current_consist_level, + "plan_ctx_level", plan_ctx->get_consistency_level()); + } return ret; } diff --git a/tools/deploy/mysql_test/test_suite/trx/r/mysql/trans_weak_read.result b/tools/deploy/mysql_test/test_suite/trx/r/mysql/trans_weak_read.result index 4c0c7784b6..aa0a18fd5b 100644 --- a/tools/deploy/mysql_test/test_suite/trx/r/mysql/trans_weak_read.result +++ b/tools/deploy/mysql_test/test_suite/trx/r/mysql/trans_weak_read.result @@ -12,6 +12,17 @@ Variable_name Value ob_max_read_stale_time -1 alter system set weak_read_version_refresh_interval="6s"; alter system set weak_read_version_refresh_interval="3s"; +use oceanbase; +set ob_read_consistency = 'weak'; +show variables like 'ob_read_consistency'; +Variable_name Value +ob_read_consistency WEAK +select count(1) > 0 from __all_server; +count(1) > 0 +1 +select count(1) from gv$ob_sql_audit where consistency_level=3 and query_sql = 'select count(1) > 0 from __all_server' and query_sql not like '%sql_audit%'; +count(1) +1 set global ob_max_read_stale_time = -1; show global variables like '%stale_time%'; Variable_name Value diff --git a/tools/deploy/mysql_test/test_suite/trx/t/trans_weak_read.test b/tools/deploy/mysql_test/test_suite/trx/t/trans_weak_read.test index 2002d5417c..2510448e5f 100644 --- a/tools/deploy/mysql_test/test_suite/trx/t/trans_weak_read.test +++ b/tools/deploy/mysql_test/test_suite/trx/t/trans_weak_read.test @@ -22,6 +22,13 @@ show global variables like '%stale_time%'; connection conn1; alter system set weak_read_version_refresh_interval="6s"; alter system set weak_read_version_refresh_interval="3s"; + +#测试内部表查询是否为强读 +use oceanbase; +set ob_read_consistency = 'weak'; +show variables like 'ob_read_consistency'; +select count(1) > 0 from __all_server; +select count(1) from gv$ob_sql_audit where consistency_level=3 and query_sql = 'select count(1) > 0 from __all_server' and query_sql not like '%sql_audit%'; --sleep 2 # 0. 禁用ob_max_read_stale_time