From 4eec8a8049e1bf044bace3af7c3f3070b1451d16 Mon Sep 17 00:00:00 2001 From: ChangerR Date: Fri, 12 Jul 2024 11:01:09 +0000 Subject: [PATCH] fix table level online_estimate_percent not work --- .../table_load/ob_table_load_client_task.cpp | 2 ++ src/share/ob_upgrade_utils.cpp | 29 +++++++++++++++++++ src/share/ob_upgrade_utils.h | 1 + src/share/stat/ob_dbms_stats_preferences.cpp | 26 +++++++++++++++++ src/share/stat/ob_dbms_stats_preferences.h | 3 ++ src/share/stat/ob_dbms_stats_utils.cpp | 6 +++- src/share/stat/ob_dbms_stats_utils.h | 4 ++- .../engine/cmd/ob_load_data_direct_impl.cpp | 5 ++-- src/sql/optimizer/ob_insert_log_plan.cpp | 26 +++++++++++++++-- src/sql/optimizer/ob_insert_log_plan.h | 1 + 10 files changed, 95 insertions(+), 8 deletions(-) diff --git a/src/observer/table_load/ob_table_load_client_task.cpp b/src/observer/table_load/ob_table_load_client_task.cpp index 601c55cfd3..acbff18419 100644 --- a/src/observer/table_load/ob_table_load_client_task.cpp +++ b/src/observer/table_load/ob_table_load_client_task.cpp @@ -635,6 +635,8 @@ int ObTableLoadClientTask::init_instance() if (OB_SUCC(ret)) { if (online_opt_stat_gather && OB_FAIL(ObDbmsStatsUtils::get_sys_online_estimate_percent(exec_ctx_, + tenant_id, + table_id, online_sample_percent))) { LOG_WARN("failed to get sys online sample percent", K(ret)); } else { diff --git a/src/share/ob_upgrade_utils.cpp b/src/share/ob_upgrade_utils.cpp index d536945385..acf39fd644 100755 --- a/src/share/ob_upgrade_utils.cpp +++ b/src/share/ob_upgrade_utils.cpp @@ -1316,6 +1316,8 @@ int ObUpgradeFor4320Processor::post_upgrade() LOG_WARN("fail to reset compat version", KR(ret)); } else if (OB_FAIL(post_upgrade_for_spm())) { LOG_WARN("failed to post upgrade for spm", KR(ret)); + } else if (OB_FAIL(post_upgrade_for_online_estimate_percent())) { + LOG_WARN("failed to post upgrade for online estimate percent", KR(ret)); } return ret; } @@ -1420,6 +1422,33 @@ int ObUpgradeFor4320Processor::post_upgrade_for_spm() return ret; } + +int ObUpgradeFor4320Processor::post_upgrade_for_online_estimate_percent() +{ + int ret = OB_SUCCESS; + int64_t start = ObTimeUtility::current_time(); + ObSqlString raw_sql; + int64_t affected_rows = 0; + bool is_primary_tenant = false; + if (sql_proxy_ == NULL) { + ret = OB_ERR_UNEXPECTED; + LOG_WARN("sql_proxy is null", K(ret), K(tenant_id_)); + } else if (OB_FAIL(ObAllTenantInfoProxy::is_primary_tenant(sql_proxy_, tenant_id_, is_primary_tenant))) { + LOG_WARN("check is standby tenant failed", K(ret), K(tenant_id_)); + } else if (!is_primary_tenant) { + LOG_INFO("tenant isn't primary standby, no refer to gather stats, skip", K(tenant_id_)); + } else if (OB_FAIL(ObDbmsStatsPreferences::get_online_estimate_percent_for_upgrade(raw_sql))) { + LOG_WARN("failed to get extra stats perfs for upgrade", K(ret)); + } else if (OB_FAIL(sql_proxy_->write(tenant_id_, raw_sql.ptr(), affected_rows))) { + LOG_WARN("failed to write", K(ret)); + } + if (OB_FAIL(ret)) { + LOG_WARN("[UPGRADE] post upgrade for online estimate failed", KR(ret), K_(tenant_id)); + } else { + LOG_INFO("[UPGRADE] post upgrade for online estimate succeed", K_(tenant_id)); + } return ret; +} + /* =========== 4310 upgrade processor end ============= */ /* =========== special upgrade processor end ============= */ diff --git a/src/share/ob_upgrade_utils.h b/src/share/ob_upgrade_utils.h index 7003864178..a2ebc957e8 100755 --- a/src/share/ob_upgrade_utils.h +++ b/src/share/ob_upgrade_utils.h @@ -257,6 +257,7 @@ private: int post_upgrade_for_reset_compat_version(); int try_reset_version(const uint64_t tenant_id, const char *var_name); int post_upgrade_for_spm(); + int post_upgrade_for_online_estimate_percent(); }; DEF_SIMPLE_UPGRARD_PROCESSER(4, 3, 3, 0) diff --git a/src/share/stat/ob_dbms_stats_preferences.cpp b/src/share/stat/ob_dbms_stats_preferences.cpp index 0a5deff82c..fb5c1b3ae6 100644 --- a/src/share/stat/ob_dbms_stats_preferences.cpp +++ b/src/share/stat/ob_dbms_stats_preferences.cpp @@ -41,6 +41,8 @@ namespace common { #define INIT_GLOBAL_PREFS "REPLACE INTO %s(sname, sval1, sval2, spare4) VALUES %s;" +#define UPGRADE_GLOBAL_PREFS "INSERT IGNORE INTO %s(sname, sval1, sval2, spare4) VALUES %s;" + int ObDbmsStatsPreferences::reset_global_pref_defaults(ObExecContext &ctx) { int ret = OB_SUCCESS; @@ -1184,6 +1186,30 @@ int ObOnlineEstimatePercentPrefs::check_pref_value_validity(ObTableStatParam *pa return ret; } +int ObDbmsStatsPreferences::get_online_estimate_percent_for_upgrade(ObSqlString &raw_sql) +{ + int ret = OB_SUCCESS; + const char *null_str = "NULL"; + const char *time_str = "CURRENT_TIMESTAMP"; + ObSqlString value_str; + ObOnlineEstimatePercentPrefs prefs; + if (OB_ISNULL(prefs.get_stat_pref_name()) || OB_ISNULL(prefs.get_stat_pref_for_update())) { + ret = OB_ERR_UNEXPECTED; + LOG_WARN("get unexpected error", K(ret), K(prefs.get_stat_pref_name()), + K(prefs.get_stat_pref_for_update())); + } else if (OB_FAIL(value_str.append_fmt("('%s', %s, %s, '%s')", + prefs.get_stat_pref_name(), + null_str, + time_str, + prefs.get_stat_pref_for_update()))) { + LOG_WARN("failed to append", K(ret)); + } else if (OB_FAIL(raw_sql.append_fmt(UPGRADE_GLOBAL_PREFS, + share::OB_ALL_OPTSTAT_GLOBAL_PREFS_TNAME, + value_str.ptr()))) { + LOG_WARN("failed to append fmt", K(ret)); + } + return ret; +} } // namespace common } // namespace oceanbase diff --git a/src/share/stat/ob_dbms_stats_preferences.h b/src/share/stat/ob_dbms_stats_preferences.h index 66fb91ac6e..140fef096b 100644 --- a/src/share/stat/ob_dbms_stats_preferences.h +++ b/src/share/stat/ob_dbms_stats_preferences.h @@ -238,6 +238,7 @@ class ObOnlineEstimatePercentPrefs : public ObStatPrefs virtual int check_pref_value_validity(ObTableStatParam *param = NULL) override; virtual const char* get_stat_pref_name() const { return "ONLINE_ESTIMATE_PERCENT"; } virtual const char* get_stat_pref_default_value() const { return "1";} + const char* get_stat_pref_for_update() const { return "100"; } }; template @@ -284,6 +285,8 @@ public: bool is_reset_prefs = false, int64_t *expect_affected_rows = NULL); + static int get_online_estimate_percent_for_upgrade(ObSqlString &sql); + private: static int do_get_prefs(ObExecContext &ctx, ObIAllocator *allocator, diff --git a/src/share/stat/ob_dbms_stats_utils.cpp b/src/share/stat/ob_dbms_stats_utils.cpp index 8d73b6f57b..068b6ca617 100644 --- a/src/share/stat/ob_dbms_stats_utils.cpp +++ b/src/share/stat/ob_dbms_stats_utils.cpp @@ -1381,7 +1381,9 @@ int ObDbmsStatsUtils::scale_col_stats(const uint64_t tenant_id, } int ObDbmsStatsUtils::get_sys_online_estimate_percent(sql::ObExecContext &ctx, - double &percent) + const uint64_t tenant_id, + const uint64_t table_id, + double &percent) { int ret = OB_SUCCESS; ObTableStatParam stat_param; @@ -1389,6 +1391,8 @@ int ObDbmsStatsUtils::get_sys_online_estimate_percent(sql::ObExecContext &ctx, ObSEArray stat_prefs; ObOnlineEstimatePercentPrefs *tmp_pref = NULL; stat_param.allocator_ = &tmp_alloc; + stat_param.tenant_id_ = tenant_id; + stat_param.table_id_ = table_id; if (OB_FAIL(new_stat_prefs(*stat_param.allocator_, ctx.get_my_session(), ObString(), tmp_pref))) { LOG_WARN("failed to new stat prefs", K(ret)); } else if (OB_FAIL(stat_prefs.push_back(tmp_pref))) { diff --git a/src/share/stat/ob_dbms_stats_utils.h b/src/share/stat/ob_dbms_stats_utils.h index 916b8acde7..7f966536ef 100644 --- a/src/share/stat/ob_dbms_stats_utils.h +++ b/src/share/stat/ob_dbms_stats_utils.h @@ -192,7 +192,9 @@ public: common::ObIArray &col_stats); static int get_sys_online_estimate_percent(sql::ObExecContext &ctx, - double &percent); + const uint64_t tenant_id, + const uint64_t table_id, + double &percent); private: static int batch_write(share::schema::ObSchemaGetterGuard *schema_guard, const uint64_t tenant_id, diff --git a/src/sql/engine/cmd/ob_load_data_direct_impl.cpp b/src/sql/engine/cmd/ob_load_data_direct_impl.cpp index f71b6bd087..970f7fe2ed 100644 --- a/src/sql/engine/cmd/ob_load_data_direct_impl.cpp +++ b/src/sql/engine/cmd/ob_load_data_direct_impl.cpp @@ -2478,6 +2478,8 @@ int ObLoadDataDirectImpl::init_execute_context() if (OB_SUCC(ret)) { if (execute_param_.online_opt_stat_gather_ && OB_FAIL(ObDbmsStatsUtils::get_sys_online_estimate_percent(*ctx_, + execute_param_.tenant_id_, + execute_param_.table_id_, online_sample_percent))) { LOG_WARN("failed to get sys online sample percent", K(ret)); } else { @@ -2492,9 +2494,6 @@ int ObLoadDataDirectImpl::init_execute_context() } else if (OB_FAIL(ObDDLUtil::get_temp_store_compress_type( table_compressor_type, execute_param_.parallel_, load_param.compressor_type_))) { LOG_WARN("fail to get tmp store compressor type", KR(ret)); - } else if (OB_FAIL(ObDbmsStatsUtils::get_sys_online_estimate_percent(*ctx_, - load_param.online_sample_percent_))) { - LOG_WARN("fail to get sys online estimate percent", KR(ret)); } else if (OB_FAIL(direct_loader_.init(load_param, execute_param_.column_ids_, &execute_ctx_.exec_ctx_))) { LOG_WARN("fail to init direct loader", KR(ret)); diff --git a/src/sql/optimizer/ob_insert_log_plan.cpp b/src/sql/optimizer/ob_insert_log_plan.cpp index 56c038b50d..c912be7916 100644 --- a/src/sql/optimizer/ob_insert_log_plan.cpp +++ b/src/sql/optimizer/ob_insert_log_plan.cpp @@ -102,9 +102,7 @@ int ObInsertLogPlan::generate_normal_raw_plan() if (OB_FAIL(check_need_online_stats_gather(tmp_need_osg))) { LOG_WARN("fail to check wether we need optimizer stats gathering operator", K(ret)); } else if (tmp_need_osg && - get_optimizer_context().get_query_ctx()->optimizer_features_enable_version_ >= COMPAT_VERSION_4_3_2 && - OB_FAIL(ObDbmsStatsUtils::get_sys_online_estimate_percent(*get_optimizer_context().get_exec_ctx(), - online_sample_percent))) { + OB_FAIL(get_online_estimate_percent(online_sample_percent))) { LOG_WARN("failed to get sys online sample percent", K(ret)); } else { if (is_direct_insert()) { @@ -1755,3 +1753,25 @@ int ObInsertLogPlan::candi_allocate_optimizer_stats_merge(OSGShareInfo *osg_info } return ret; } + +int ObInsertLogPlan::get_online_estimate_percent(double &percent) +{ + int ret = OB_SUCCESS; + percent = 100.; + const ObSQLSessionInfo *session_info = NULL; + if (OB_ISNULL(get_optimizer_context().get_exec_ctx()) || + OB_ISNULL(session_info = get_optimizer_context().get_session_info()) || + OB_ISNULL(get_stmt()) || + OB_ISNULL(get_optimizer_context().get_query_ctx())) { + ret = OB_ERR_UNEXPECTED; + LOG_WARN("get unexpected null", K(ret)); + } else if (get_optimizer_context().get_query_ctx()->optimizer_features_enable_version_ < COMPAT_VERSION_4_3_2) { + // do nothing + } else if (OB_FAIL(ObDbmsStatsUtils::get_sys_online_estimate_percent(*get_optimizer_context().get_exec_ctx(), + session_info->get_effective_tenant_id(), + get_stmt()->get_insert_table_info().ref_table_id_, + percent))) { + LOG_WARN("failed to get sys online estimate percent", K(ret)); + } + return ret; +} \ No newline at end of file diff --git a/src/sql/optimizer/ob_insert_log_plan.h b/src/sql/optimizer/ob_insert_log_plan.h index 7cde4c9f03..207ae31829 100644 --- a/src/sql/optimizer/ob_insert_log_plan.h +++ b/src/sql/optimizer/ob_insert_log_plan.h @@ -129,6 +129,7 @@ protected: int check_contain_non_onetime_expr(const ObRawExpr *expr, bool &contain); int check_contain_non_onetime_expr(const ObIArray &exprs, bool &contain); + int get_online_estimate_percent(double &percent); private: int get_index_part_ids(const ObInsertTableInfo& table_info, const ObTableSchema *&data_table_schema, const ObTableSchema *&index_schema, ObIArray &index_part_ids); int generate_osg_share_info(OSGShareInfo *&info);