[CP] fix write optimizer stats to use the same trans

This commit is contained in:
wangt1xiuyi
2023-07-07 06:18:23 +00:00
committed by ob-robot
parent 0692a9954d
commit a0a29c9034
9 changed files with 85 additions and 78 deletions

View File

@ -334,14 +334,30 @@ int ObDbmsStatsExecutor::set_column_stats(ObExecContext &ctx,
LOG_WARN("failed to do set table stats", K(ret));
} else if (OB_FAIL(column_stats.push_back(col_stat))) {
LOG_WARN("failed to push back column stat", K(ret));
} else if (OB_FAIL(mgr.update_column_stat(ctx.get_virtual_table_ctx().schema_guard_,
param.table_param_.tenant_id_,
column_stats,
true,
CREATE_OBJ_PRINT_PARAM(ctx.get_my_session())))) {
LOG_WARN("failed to update column stats", K(ret));
} else {
LOG_TRACE("end set column stats", K(param), K(*col_stat));
ObMySQLTransaction trans;
if (OB_FAIL(trans.start(ctx.get_sql_proxy(), param.table_param_.tenant_id_))) {
LOG_WARN("fail to start transaction", K(ret));
} else if (OB_FAIL(mgr.update_column_stat(ctx.get_virtual_table_ctx().schema_guard_,
param.table_param_.tenant_id_,
trans,
column_stats,
true,
CREATE_OBJ_PRINT_PARAM(ctx.get_my_session())))) {
LOG_WARN("failed to update column stats", K(ret));
} else {
LOG_TRACE("end set column stats", K(param), K(*col_stat));
}
if (OB_SUCC(ret)) {
if (OB_FAIL(trans.end(true))) {
LOG_WARN("fail to commit transaction", K(ret));
}
} else {
int tmp_ret = OB_SUCCESS;
if (OB_SUCCESS != (tmp_ret = trans.end(false))) {
LOG_WARN("fail to roll back transaction", K(tmp_ret));
}
}
}
}
}

View File

@ -118,6 +118,7 @@ int ObDbmsStatsUtils::check_range_skew(ObHistType hist_type,
int ObDbmsStatsUtils::batch_write(share::schema::ObSchemaGetterGuard *schema_guard,
const uint64_t tenant_id,
ObMySQLTransaction &trans,
ObIArray<ObOptTableStat *> &table_stats,
ObIArray<ObOptColumnStat*> &column_stats,
const int64_t current_time,
@ -129,6 +130,7 @@ int ObDbmsStatsUtils::batch_write(share::schema::ObSchemaGetterGuard *schema_gua
int ret = OB_SUCCESS;
if (OB_FAIL(ObOptStatManager::get_instance().batch_write(schema_guard,
tenant_id,
trans,
table_stats,
column_stats,
current_time,
@ -348,31 +350,6 @@ int ObDbmsStatsUtils::parse_granularity(const ObString &granularity, ObGranulari
}
int ObDbmsStatsUtils::split_batch_write(sql::ObExecContext &ctx,
ObIArray<ObOptTableStat*> &table_stats,
ObIArray<ObOptColumnStat*> &column_stats,
const bool is_index_stat /*default false*/,
const bool is_history_stat /*default false*/,
const bool is_online_stat /*default false*/)
{
int ret = OB_SUCCESS;
if (OB_ISNULL(ctx.get_my_session())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(ret), K(ctx.get_my_session()));
} else if (OB_FAIL(split_batch_write(ctx.get_virtual_table_ctx().schema_guard_,
ctx.get_my_session()->get_effective_tenant_id(),
table_stats,
column_stats,
is_index_stat,
is_history_stat,
is_online_stat,
CREATE_OBJ_PRINT_PARAM(ctx.get_my_session())))) {
LOG_WARN("failed to split batch write", K(ret));
} else {/*do nothing*/}
return ret;
}
int ObDbmsStatsUtils::split_batch_write(share::schema::ObSchemaGetterGuard *schema_guard,
const uint64_t tenant_id,
ObIArray<ObOptTableStat*> &table_stats,
ObIArray<ObOptColumnStat*> &column_stats,
const bool is_index_stat/*default false*/,
@ -388,6 +365,14 @@ int ObDbmsStatsUtils::split_batch_write(share::schema::ObSchemaGetterGuard *sche
LOG_DEBUG("dbms stats write stats", K(table_stats), K(column_stats));
const int64_t MAX_NUM_OF_WRITE_STATS = 2000;
int64_t current_time = ObTimeUtility::current_time();
ObMySQLTransaction trans;
//begin trans before writing stats
if (OB_ISNULL(ctx.get_my_session())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(ret), K(ctx.get_my_session()));
} else if (OB_FAIL(trans.start(ctx.get_sql_proxy(), ctx.get_my_session()->get_effective_tenant_id()))) {
LOG_WARN("fail to start transaction", K(ret));
}
while (OB_SUCC(ret) &&
(idx_tab_stat < table_stats.count() || idx_col_stat < column_stats.count())) {
ObSEArray<ObOptTableStat*, 4> write_table_stats;
@ -424,8 +409,9 @@ int ObDbmsStatsUtils::split_batch_write(share::schema::ObSchemaGetterGuard *sche
}
}
if (OB_SUCC(ret)) {
if (OB_FAIL(ObDbmsStatsUtils::batch_write(schema_guard,
tenant_id,
if (OB_FAIL(ObDbmsStatsUtils::batch_write(ctx.get_virtual_table_ctx().schema_guard_,
ctx.get_my_session()->get_effective_tenant_id(),
trans,
write_table_stats,
write_column_stats,
current_time,
@ -437,6 +423,17 @@ int ObDbmsStatsUtils::split_batch_write(share::schema::ObSchemaGetterGuard *sche
} else {/*do nothing*/}
}
}
//end trans after writing stats.
if (OB_SUCC(ret)) {
if (OB_FAIL(trans.end(true))) {
LOG_WARN("fail to commit transaction", K(ret));
}
} else {
int tmp_ret = OB_SUCCESS;
if (OB_SUCCESS != (tmp_ret = trans.end(false))) {
LOG_WARN("fail to roll back transaction", K(tmp_ret));
}
}
return ret;
}

View File

@ -40,14 +40,6 @@ public:
bool &is_even_distributed);
static int split_batch_write(sql::ObExecContext &ctx,
ObIArray<ObOptTableStat*> &table_stats,
ObIArray<ObOptColumnStat*> &column_stats,
const bool is_index_stat = false,
const bool is_history_stat = false,
const bool is_online_stat = false);
static int split_batch_write(share::schema::ObSchemaGetterGuard *schema_guard,
const uint64_t tenant_id,
ObIArray<ObOptTableStat*> &table_stats,
ObIArray<ObOptColumnStat*> &column_stats,
const bool is_index_stat = false,
@ -135,6 +127,7 @@ public:
private:
static int batch_write(share::schema::ObSchemaGetterGuard *schema_guard,
const uint64_t tenant_id,
ObMySQLTransaction &trans,
ObIArray<ObOptTableStat *> &table_stats,
ObIArray<ObOptColumnStat*> &column_stats,
const int64_t current_time,

View File

@ -324,15 +324,29 @@ int ObIndexStatsEstimator::fast_gather_index_stats(ObExecContext &ctx,
}
}
if (OB_SUCC(ret) && is_continued && !index_table_stats.empty()) {
if (OB_FAIL(mgr.update_table_stat(index_param.tenant_id_,
index_table_stats,
index_param.is_index_stat_))) {
ObMySQLTransaction trans;
if (OB_FAIL(trans.start(ctx.get_sql_proxy(), index_param.tenant_id_))) {
LOG_WARN("fail to start transaction", K(ret));
} else if (OB_FAIL(mgr.update_table_stat(index_param.tenant_id_,
trans,
index_table_stats,
index_param.is_index_stat_))) {
LOG_WARN("failed to update table stats", K(ret));
} else {
is_fast_gather = true;
LOG_TRACE("Succeed to fast gather index stats", K(data_param), K(index_param),
K(index_table_stats));
}
if (OB_SUCC(ret)) {
if (OB_FAIL(trans.end(true))) {
LOG_WARN("fail to commit transaction", K(ret));
}
} else {
int tmp_ret = OB_SUCCESS;
if (OB_SUCCESS != (tmp_ret = trans.end(false))) {
LOG_WARN("fail to roll back transaction", K(tmp_ret));
}
}
}
}
return ret;

View File

@ -239,6 +239,7 @@ int ObOptStatManager::get_table_stat(const uint64_t tenant_id,
int ObOptStatManager::update_column_stat(share::schema::ObSchemaGetterGuard *schema_guard,
const uint64_t tenant_id,
ObMySQLTransaction &trans,
const ObIArray<ObOptColumnStat *> &column_stats,
bool only_update_col_stat /*default false*/,
const ObObjPrintParams &print_params)
@ -250,6 +251,7 @@ int ObOptStatManager::update_column_stat(share::schema::ObSchemaGetterGuard *sch
LOG_WARN("optimizer statistics manager has not been initialized.", K(ret));
} else if (OB_FAIL(stat_service_.get_sql_service().update_column_stat(schema_guard,
tenant_id,
trans,
column_stats,
current_time,
only_update_col_stat,
@ -277,6 +279,7 @@ int ObOptStatManager::update_table_stat(const uint64_t tenant_id,
}
int ObOptStatManager::update_table_stat(const uint64_t tenant_id,
ObMySQLTransaction &trans,
const ObIArray<ObOptTableStat*> &table_stats,
const bool is_index_stat)
{
@ -286,6 +289,7 @@ int ObOptStatManager::update_table_stat(const uint64_t tenant_id,
ret = OB_NOT_INIT;
LOG_WARN("not inited", K(ret));
} else if (OB_FAIL(stat_service_.get_sql_service().update_table_stat(tenant_id,
trans,
table_stats,
current_time,
is_index_stat))) {
@ -371,6 +375,7 @@ int ObOptStatManager::erase_table_stat(const ObOptTableStat::Key &key)
int ObOptStatManager::batch_write(share::schema::ObSchemaGetterGuard *schema_guard,
const uint64_t tenant_id,
ObMySQLTransaction &trans,
ObIArray<ObOptTableStat *> &table_stats,
ObIArray<ObOptColumnStat *> &column_stats,
const int64_t current_time,
@ -385,6 +390,7 @@ int ObOptStatManager::batch_write(share::schema::ObSchemaGetterGuard *schema_gua
} else if (!table_stats.empty() &&
OB_FAIL(stat_service_.get_sql_service().update_table_stat(
tenant_id,
trans,
table_stats,
current_time,
is_index_stat,
@ -393,6 +399,7 @@ int ObOptStatManager::batch_write(share::schema::ObSchemaGetterGuard *schema_gua
} else if (!column_stats.empty() &&
OB_FAIL(stat_service_.get_sql_service().update_column_stat(schema_guard,
tenant_id,
trans,
column_stats,
current_time,
false,

View File

@ -61,6 +61,7 @@ public:
const bool is_index_stat);
int update_table_stat(const uint64_t tenant_id,
ObMySQLTransaction &trans,
const ObIArray<ObOptTableStat*> &table_stats,
const bool is_index_stat);
@ -119,6 +120,7 @@ public:
ObOptColumnStatHandle &handle);
virtual int update_column_stat(share::schema::ObSchemaGetterGuard *schema_guard,
const uint64_t tenant_id,
ObMySQLTransaction &trans,
const common::ObIArray<ObOptColumnStat *> &column_stats,
bool only_update_col_stat = false,
const ObObjPrintParams &print_params = ObObjPrintParams());
@ -153,6 +155,7 @@ public:
int batch_write(share::schema::ObSchemaGetterGuard *schema_guard,
const uint64_t tenant_id,
ObMySQLTransaction &trans,
ObIArray<ObOptTableStat *> &table_stats,
ObIArray<ObOptColumnStat *> &column_stats,
const int64_t current_time,

View File

@ -16,7 +16,6 @@
#include "lib/oblog/ob_log_module.h"
#include "lib/string/ob_sql_string.h"
#include "lib/mysqlclient/ob_mysql_proxy.h"
#include "lib/mysqlclient/ob_mysql_transaction.h"
#include "lib/mysqlclient/ob_mysql_result.h"
#include "lib/mysqlclient/ob_mysql_connection.h"
#include "lib/mysqlclient/ob_mysql_statement.h"
@ -346,7 +345,6 @@ int ObOptStatSqlService::update_table_stat(const uint64_t tenant_id,
ObSqlString table_stat_sql;
ObSqlString tmp;
int64_t current_time = ObTimeUtility::current_time();
uint64_t exec_tenant_id = tenant_id;
int64_t affected_rows = 0;
if (OB_ISNULL(table_stat)) {
ret = OB_ERR_UNEXPECTED;
@ -360,9 +358,9 @@ int ObOptStatSqlService::update_table_stat(const uint64_t tenant_id,
} else {
ObMySQLTransaction trans;
LOG_TRACE("sql string of table stat update", K(table_stat_sql));
if (OB_FAIL(trans.start(mysql_proxy_, exec_tenant_id))) {
LOG_WARN("fail to start transaction", K(ret), K(exec_tenant_id));
} else if (OB_FAIL(trans.write(exec_tenant_id, table_stat_sql.ptr(), affected_rows))) {
if (OB_FAIL(trans.start(mysql_proxy_, tenant_id))) {
LOG_WARN("fail to start transaction", K(ret), K(tenant_id));
} else if (OB_FAIL(trans.write(tenant_id, table_stat_sql.ptr(), affected_rows))) {
LOG_WARN("failed to exec sql", K(ret));
} else {/*do nothing*/}
if (OB_SUCC(ret)) {
@ -380,6 +378,7 @@ int ObOptStatSqlService::update_table_stat(const uint64_t tenant_id,
}
int ObOptStatSqlService::update_table_stat(const uint64_t tenant_id,
ObMySQLTransaction &trans,
const common::ObIArray<ObOptTableStat *> &table_stats,
const int64_t current_time,
const bool is_index_stat,
@ -414,28 +413,16 @@ int ObOptStatSqlService::update_table_stat(const uint64_t tenant_id,
}
if (OB_SUCC(ret)) {
LOG_TRACE("sql string of table stat update", K(table_stat_sql));
ObMySQLTransaction trans;
if (OB_FAIL(trans.start(mysql_proxy_, tenant_id))) {
LOG_WARN("fail to start transaction", K(ret));
} else if (OB_FAIL(trans.write(tenant_id, table_stat_sql.ptr(), affected_rows))) {
if (OB_FAIL(trans.write(tenant_id, table_stat_sql.ptr(), affected_rows))) {
LOG_WARN("failed to exec sql", K(ret));
}
if (OB_SUCC(ret)) {
if (OB_FAIL(trans.end(true))) {
LOG_WARN("fail to commit transaction", K(ret));
}
} else {
int tmp_ret = OB_SUCCESS;
if (OB_SUCCESS != (tmp_ret = trans.end(false))) {
LOG_WARN("fail to roll back transaction", K(tmp_ret));
}
}
}
return ret;
}
int ObOptStatSqlService::update_column_stat(share::schema::ObSchemaGetterGuard *schema_guard,
const uint64_t exec_tenant_id,
ObMySQLTransaction &trans,
const ObIArray<ObOptColumnStat*> &column_stats,
const int64_t current_time,
bool only_update_col_stat /*default false*/,
@ -443,7 +430,6 @@ int ObOptStatSqlService::update_column_stat(share::schema::ObSchemaGetterGuard *
const ObObjPrintParams &print_params)
{
int ret = OB_SUCCESS;
ObMySQLTransaction trans;
int64_t affected_rows = 0;
ObSqlString insert_histogram;
ObSqlString delete_histogram;
@ -482,8 +468,6 @@ int ObOptStatSqlService::update_column_stat(share::schema::ObSchemaGetterGuard *
need_histogram,
print_params))) {
LOG_WARN("failed to construct histogram insert sql", K(ret));
} else if (OB_FAIL(trans.start(mysql_proxy_, exec_tenant_id))) {
LOG_WARN("fail to start transaction", K(ret));
} else if (!only_update_col_stat && !is_history_stat &&
OB_FAIL(trans.write(exec_tenant_id, delete_histogram.ptr(), affected_rows))) {
LOG_WARN("fail to exec sql", K(delete_histogram), K(ret));
@ -493,16 +477,6 @@ int ObOptStatSqlService::update_column_stat(share::schema::ObSchemaGetterGuard *
} else if (OB_FAIL(trans.write(exec_tenant_id, column_stats_sql.ptr(), affected_rows))) {
LOG_WARN("failed to exec sql", K(column_stats_sql), K(ret));
}
if (OB_SUCC(ret)) {
if (OB_FAIL(trans.end(true))) {
LOG_WARN("fail to commit transaction", K(ret));
}
} else {
int tmp_ret = OB_SUCCESS;
if (OB_SUCCESS != (tmp_ret = trans.end(false))) {
LOG_WARN("fail to roll back transaction", K(tmp_ret));
}
}
return ret;
}

View File

@ -17,6 +17,7 @@
#include "share/stat/ob_opt_table_stat.h"
#include "share/stat/ob_stat_define.h"
#include "share/stat/ob_opt_stat_gather_stat.h"
#include "lib/mysqlclient/ob_mysql_transaction.h"
namespace oceanbase {
namespace common {
namespace sqlclient
@ -94,12 +95,14 @@ public:
const ObOptTableStat *tab_stat,
const bool is_index_stat);
int update_table_stat(const uint64_t tenant_id,
ObMySQLTransaction &trans,
const common::ObIArray<ObOptTableStat*> &table_stats,
const int64_t current_time,
const bool is_index_stat,
const bool is_history_stat = false);
int update_column_stat(share::schema::ObSchemaGetterGuard *schema_guard,
const uint64_t exec_tenant_id,
ObMySQLTransaction &trans,
const common::ObIArray<ObOptColumnStat*> &column_stats,
const int64_t current_time,
bool only_update_col_stat = false,

View File

@ -48,7 +48,7 @@ GB_3 [\x30-\x39]
UTF8_GB_CHAR ({U_2}{U}|{U_3}{U}{U}|{U_4}{U}{U}{U}|{GB_1}{GB_2}|{GB_1}{GB_3}{GB_1}{GB_3})
space [ \t\n\r\f]
non_newline [^\n\r]
sql_comment ("--"{space}+{non_newline}*)|(#{non_newline}*)
sql_comment ("--"[ \t]+{non_newline}*)|(#{non_newline}*|"--"[\n\r])
whitespace ({space}+|{sql_comment})
select_hint_begin (select{space}*(\/\*([^+*]|\*+[^*\/])*\*+\/{space}*)*(\/\*\+({space}*hint{space}+)?))
update_hint_begin (update{space}*(\/\*([^+*]|\*+[^*\/])*\*+\/{space}*)*(\/\*\+({space}*hint{space}+)?))