From 7fd999d76a3ac7a2fba4aa10eb52b571aa30fd9f Mon Sep 17 00:00:00 2001 From: wangbowen Date: Tue, 31 Dec 2024 15:21:37 +0800 Subject: [PATCH 1/2] fix synchronous of cardinality --- mysql-test/enableCases.list | 1 + .../suite/ctc/r/ctc_index_cardinality.result | 102 ++++++++++++++++++ .../suite/ctc/t/ctc_index_cardinality.test | 66 ++++++++++++ storage/ctc/ctc_cbo.cc | 40 ++++--- storage/ctc/ctc_cbo.h | 1 + storage/ctc/ha_ctc.cc | 9 +- storage/ctc/ha_ctcpart.cc | 12 ++- 7 files changed, 201 insertions(+), 30 deletions(-) create mode 100644 mysql-test/suite/ctc/r/ctc_index_cardinality.result create mode 100644 mysql-test/suite/ctc/t/ctc_index_cardinality.test diff --git a/mysql-test/enableCases.list b/mysql-test/enableCases.list index c6fe92b..870404c 100644 --- a/mysql-test/enableCases.list +++ b/mysql-test/enableCases.list @@ -21,6 +21,7 @@ ctc_enum_analyze ctc_bit_analyze ctc_string_analyze ctc_dml_estimate_rows +ctc_index_cardinality # ------ END TEST CASES OF DML ------ # ------ BEGIN TEST CASES OF TRANSACTION ------ diff --git a/mysql-test/suite/ctc/r/ctc_index_cardinality.result b/mysql-test/suite/ctc/r/ctc_index_cardinality.result new file mode 100644 index 0000000..2194215 --- /dev/null +++ b/mysql-test/suite/ctc/r/ctc_index_cardinality.result @@ -0,0 +1,102 @@ +DROP DATABASE IF EXISTS db1; +CREATE DATABASE db1; +DROP TABLE IF EXISTS db1.t1; +Warnings: +Note 1051 Unknown table 'db1.t1' +CREATE TABLE db1.t1(a INT, b INT, c INT, d INT); +INSERT INTO db1.t1 VALUES(1,1,1,1),(1,1,1,2), (1,2,2,3), (1,2,3,4); +CREATE INDEX idx_abcd ON db1.t1 (a, b, c, d); +ANALYZE TABLE db1.t1; +Table Op Msg_type Msg_text +db1.t1 analyze status OK +SHOW INDEX FROM db1.t1; +Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Visible Expression +t1 1 idx_abcd 1 a A 1 NULL NULL YES BTREE YES NULL +t1 1 idx_abcd 2 b A 2 NULL NULL YES BTREE YES NULL +t1 1 idx_abcd 3 c A 3 NULL NULL YES BTREE YES NULL +t1 1 idx_abcd 4 d A 4 NULL NULL YES BTREE YES NULL +CREATE INDEX idx_a ON db1.t1 (a); +ANALYZE TABLE db1.t1; +Table Op Msg_type Msg_text +db1.t1 analyze status OK +SHOW INDEX FROM db1.t1; +Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Visible Expression +t1 1 idx_abcd 1 a A 1 NULL NULL YES BTREE YES NULL +t1 1 idx_abcd 2 b A 2 NULL NULL YES BTREE YES NULL +t1 1 idx_abcd 3 c A 3 NULL NULL YES BTREE YES NULL +t1 1 idx_abcd 4 d A 4 NULL NULL YES BTREE YES NULL +t1 1 idx_a 1 a A 1 NULL NULL YES BTREE YES NULL +DROP TABLE IF EXISTS db1.t1; +CREATE TABLE db1.t1 ( +a INT, +b INT, +c INT, +d INT +) +PARTITION BY RANGE (a) ( +PARTITION p0 VALUES LESS THAN (10), +PARTITION p1 VALUES LESS THAN (20), +PARTITION p2 VALUES LESS THAN (30), +PARTITION p3 VALUES LESS THAN (40) +); +INSERT INTO db1.t1 VALUES(1,1,1,1),(11,1,1,2), (21,2,2,3), (31,2,3,4), +(1,1,1,2), (1,2,2,3), (1,2,3,4); +CREATE INDEX idx_abcd ON db1.t1 (a, b, c, d); +ANALYZE TABLE db1.t1; +Table Op Msg_type Msg_text +db1.t1 analyze status OK +SHOW INDEX FROM db1.t1; +Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Visible Expression +t1 1 idx_abcd 1 a A 4 NULL NULL YES BTREE YES NULL +t1 1 idx_abcd 2 b A 5 NULL NULL YES BTREE YES NULL +t1 1 idx_abcd 3 c A 6 NULL NULL YES BTREE YES NULL +t1 1 idx_abcd 4 d A 7 NULL NULL YES BTREE YES NULL +CREATE INDEX idx_a ON db1.t1 (a); +ANALYZE TABLE db1.t1; +Table Op Msg_type Msg_text +db1.t1 analyze status OK +SHOW INDEX FROM db1.t1; +Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Visible Expression +t1 1 idx_abcd 1 a A 4 NULL NULL YES BTREE YES NULL +t1 1 idx_abcd 2 b A 5 NULL NULL YES BTREE YES NULL +t1 1 idx_abcd 3 c A 6 NULL NULL YES BTREE YES NULL +t1 1 idx_abcd 4 d A 7 NULL NULL YES BTREE YES NULL +t1 1 idx_a 1 a A 4 NULL NULL YES BTREE YES NULL +DROP TABLE IF EXISTS db1.t1; +CREATE TABLE db1.t1 ( +a INT, +b INT, +c INT, +d INT +) +PARTITION BY RANGE (a) +SUBPARTITION BY HASH (a) SUBPARTITIONS 4 ( +PARTITION p0 VALUES LESS THAN (10), +PARTITION p1 VALUES LESS THAN (20), +PARTITION p2 VALUES LESS THAN (30), +PARTITION p3 VALUES LESS THAN (40) +); +INSERT INTO db1.t1 VALUES(1,1,1,1),(11,1,1,2), (21,2,2,3), (31,2,3,4), +(1,1,1,2), (1,2,2,3), (1,2,3,4); +CREATE INDEX idx_abcd ON db1.t1 (a, b, c, d); +ANALYZE TABLE db1.t1; +Table Op Msg_type Msg_text +db1.t1 analyze status OK +SHOW INDEX FROM db1.t1; +Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Visible Expression +t1 1 idx_abcd 1 a A 4 NULL NULL YES BTREE YES NULL +t1 1 idx_abcd 2 b A 5 NULL NULL YES BTREE YES NULL +t1 1 idx_abcd 3 c A 6 NULL NULL YES BTREE YES NULL +t1 1 idx_abcd 4 d A 7 NULL NULL YES BTREE YES NULL +CREATE INDEX idx_a ON db1.t1 (a); +ANALYZE TABLE db1.t1; +Table Op Msg_type Msg_text +db1.t1 analyze status OK +SHOW INDEX FROM db1.t1; +Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Visible Expression +t1 1 idx_abcd 1 a A 4 NULL NULL YES BTREE YES NULL +t1 1 idx_abcd 2 b A 5 NULL NULL YES BTREE YES NULL +t1 1 idx_abcd 3 c A 6 NULL NULL YES BTREE YES NULL +t1 1 idx_abcd 4 d A 7 NULL NULL YES BTREE YES NULL +t1 1 idx_a 1 a A 4 NULL NULL YES BTREE YES NULL +DROP DATABASE IF EXISTS db1; diff --git a/mysql-test/suite/ctc/t/ctc_index_cardinality.test b/mysql-test/suite/ctc/t/ctc_index_cardinality.test new file mode 100644 index 0000000..c8cf6b0 --- /dev/null +++ b/mysql-test/suite/ctc/t/ctc_index_cardinality.test @@ -0,0 +1,66 @@ +--disable_warnings +DROP DATABASE IF EXISTS db1; +--enable_warnings + +CREATE DATABASE db1; + +# normal table + +DROP TABLE IF EXISTS db1.t1; +CREATE TABLE db1.t1(a INT, b INT, c INT, d INT); +INSERT INTO db1.t1 VALUES(1,1,1,1),(1,1,1,2), (1,2,2,3), (1,2,3,4); +CREATE INDEX idx_abcd ON db1.t1 (a, b, c, d);# Compound index +ANALYZE TABLE db1.t1; +SHOW INDEX FROM db1.t1; +CREATE INDEX idx_a ON db1.t1 (a); # normal index +ANALYZE TABLE db1.t1; +SHOW INDEX FROM db1.t1; + +# part table +DROP TABLE IF EXISTS db1.t1; +CREATE TABLE db1.t1 ( + a INT, + b INT, + c INT, + d INT +) +PARTITION BY RANGE (a) ( + PARTITION p0 VALUES LESS THAN (10), + PARTITION p1 VALUES LESS THAN (20), + PARTITION p2 VALUES LESS THAN (30), + PARTITION p3 VALUES LESS THAN (40) +); +INSERT INTO db1.t1 VALUES(1,1,1,1),(11,1,1,2), (21,2,2,3), (31,2,3,4), + (1,1,1,2), (1,2,2,3), (1,2,3,4); +CREATE INDEX idx_abcd ON db1.t1 (a, b, c, d);# Compound index +ANALYZE TABLE db1.t1; +SHOW INDEX FROM db1.t1; +CREATE INDEX idx_a ON db1.t1 (a); # normal index +ANALYZE TABLE db1.t1; +SHOW INDEX FROM db1.t1; + +#sub_part table +DROP TABLE IF EXISTS db1.t1; +CREATE TABLE db1.t1 ( + a INT, + b INT, + c INT, + d INT +) +PARTITION BY RANGE (a) +SUBPARTITION BY HASH (a) SUBPARTITIONS 4 ( + PARTITION p0 VALUES LESS THAN (10), + PARTITION p1 VALUES LESS THAN (20), + PARTITION p2 VALUES LESS THAN (30), + PARTITION p3 VALUES LESS THAN (40) +); +INSERT INTO db1.t1 VALUES(1,1,1,1),(11,1,1,2), (21,2,2,3), (31,2,3,4), + (1,1,1,2), (1,2,2,3), (1,2,3,4); +CREATE INDEX idx_abcd ON db1.t1 (a, b, c, d);# Compound index +ANALYZE TABLE db1.t1; +SHOW INDEX FROM db1.t1; +CREATE INDEX idx_a ON db1.t1 (a); # normal index +ANALYZE TABLE db1.t1; +SHOW INDEX FROM db1.t1; + +DROP DATABASE IF EXISTS db1; diff --git a/storage/ctc/ctc_cbo.cc b/storage/ctc/ctc_cbo.cc index 29a3054..89f7593 100644 --- a/storage/ctc/ctc_cbo.cc +++ b/storage/ctc/ctc_cbo.cc @@ -795,13 +795,23 @@ double calc_density_one_table(uint16_t idx_id, ctc_range_key *key, return density; } +static inline bool check_index_is_contain_null(ctc_cbo_stats_t *cbo_stats, uint32_t fld_idx) +{ + uint32_t table_part_num = cbo_stats->part_cnt == 0 ? PART_NUM_OF_NORMAL_TABLE : cbo_stats->part_cnt; + for (uint32 k = 0; k < table_part_num; k++) { + if (cbo_stats->ctc_cbo_stats_table[k].columns[fld_idx].num_null) { + return true; + } + } + return false; +} + void ctc_index_stats_update(TABLE *table, ctc_cbo_stats_t *cbo_stats) { rec_per_key_t rec_per_key = 0.0f; KEY sk; uint32_t *n_diff = cbo_stats->ndv_keys; - uint32_t records; - uint32_t table_part_num = cbo_stats->part_cnt == 0 ? 1 : cbo_stats->part_cnt; + uint32_t records = cbo_stats->records; uint32_t acc_gcol_num[CTC_MAX_COLUMNS] = {0}; calc_accumulate_gcol_num(table->s->fields, table->s->field, acc_gcol_num); if (cbo_stats->records == 0) { @@ -811,28 +821,16 @@ void ctc_index_stats_update(TABLE *table, ctc_cbo_stats_t *cbo_stats) for (uint32 i = 0; i < table->s->keys; i++) { sk = table->key_info[i]; for (uint32 j = 0; j < sk.actual_key_parts; j++) { - bool all_n_diff_is_zero = true; rec_per_key = 0.0f; uint32 fld_idx = sk.key_part[j].field->field_index(); fld_idx = fld_idx - acc_gcol_num[fld_idx]; - for (uint32 k = 0; k < table_part_num; k++) { - records = cbo_stats->ctc_cbo_stats_table[k].estimate_rows; - uint32 has_null = cbo_stats->ctc_cbo_stats_table[k].columns[fld_idx].num_null ? 1 : 0; - uint32 n_diff_part = *(n_diff + i * MAX_KEY_COLUMNS + j); - do { - if (!n_diff_part) { - break; - } - rec_per_key += static_cast(records) / static_cast(n_diff_part + has_null); - all_n_diff_is_zero = false; - } while(0); - } - - // if all n_diff(s) values 0, take records itself as rec_per_key - if (all_n_diff_is_zero) { - for (uint32 k = 0; k < table_part_num; k++) { - rec_per_key += static_cast(cbo_stats->ctc_cbo_stats_table[k].estimate_rows); - } + uint32 has_null = check_index_is_contain_null(cbo_stats, fld_idx) ? 1 : 0; + uint32 n_diff_part = *(n_diff + i * MAX_KEY_COLUMNS + j); + if (n_diff_part) { + rec_per_key = static_cast(records) / static_cast(n_diff_part + has_null); + } else { + // if all n_diff(s) values 0, take records itself as rec_per_key + rec_per_key = static_cast(records); } if (rec_per_key < 1.0) { diff --git a/storage/ctc/ctc_cbo.h b/storage/ctc/ctc_cbo.h index 3bc9688..2ea0620 100644 --- a/storage/ctc/ctc_cbo.h +++ b/storage/ctc/ctc_cbo.h @@ -24,6 +24,7 @@ #include "srv_mq_msg.h" #define REAL_EPSINON 0.00001 +#define PART_NUM_OF_NORMAL_TABLE 1 typedef enum en_ctc_compare_type { GREAT = 0, diff --git a/storage/ctc/ha_ctc.cc b/storage/ctc/ha_ctc.cc index 7a19d97..c2faf7f 100644 --- a/storage/ctc/ha_ctc.cc +++ b/storage/ctc/ha_ctc.cc @@ -5437,6 +5437,8 @@ int ha_ctc::initialize_cbo_stats() END_RECORD_STATS(EVENT_TYPE_INITIALIZE_DBO) return ERR_ALLOC_MEMORY; } + memset(m_share->cbo_stats->ctc_cbo_stats_table, 0, sizeof(ctc_cbo_stats_table_t)); + m_share->cbo_stats->ctc_cbo_stats_table->columns = (ctc_cbo_stats_column_t*)my_malloc(PSI_NOT_INSTRUMENTED, table->s->fields * sizeof(ctc_cbo_stats_column_t), MYF(MY_WME)); if (m_share->cbo_stats->ctc_cbo_stats_table->columns == nullptr) { @@ -5444,10 +5446,8 @@ int ha_ctc::initialize_cbo_stats() END_RECORD_STATS(EVENT_TYPE_INITIALIZE_DBO) return ERR_ALLOC_MEMORY; } - for (uint col_id = 0; col_id < table->s->fields; col_id++) { - m_share->cbo_stats->ctc_cbo_stats_table->columns[col_id].hist_count = 0; - } - + memset(m_share->cbo_stats->ctc_cbo_stats_table->columns, 0, table->s->fields * sizeof(ctc_cbo_stats_column_t)); + ct_errno_t ret = (ct_errno_t)alloc_str_mysql_mem(m_share->cbo_stats, 1, table); if (ret != CT_SUCCESS) { ctc_log_error("m_share:ctc alloc str mysql mem failed, ret:%d", ret); @@ -5460,6 +5460,7 @@ int ha_ctc::initialize_cbo_stats() END_RECORD_STATS(EVENT_TYPE_INITIALIZE_DBO) return ERR_ALLOC_MEMORY; } + memset(m_share->cbo_stats->ndv_keys, 0, table->s->keys * sizeof(uint32_t) * MAX_KEY_COLUMNS); m_share->cbo_stats->msg_len = table->s->fields * sizeof(ctc_cbo_stats_column_t); m_share->cbo_stats->key_len = table->s->keys * sizeof(uint32_t) * MAX_KEY_COLUMNS; diff --git a/storage/ctc/ha_ctcpart.cc b/storage/ctc/ha_ctcpart.cc index e05ff7b..0db2188 100644 --- a/storage/ctc/ha_ctcpart.cc +++ b/storage/ctc/ha_ctcpart.cc @@ -1047,23 +1047,25 @@ int ha_ctcpart::initialize_cbo_stats() { ctc_log_error("alloc mem failed, m_part_share->cbo_stats->ctc_cbo_stats_table size(%lu)", part_num * sizeof(ctc_cbo_stats_table_t)); return ERR_ALLOC_MEMORY; } + memset(m_part_share->cbo_stats->ctc_cbo_stats_table, 0, part_num * sizeof(ctc_cbo_stats_table_t)); + m_part_share->cbo_stats->ndv_keys = (uint32_t*)my_malloc(PSI_NOT_INSTRUMENTED, table->s->keys * sizeof(uint32_t) * MAX_KEY_COLUMNS, MYF(MY_WME)); if (m_part_share->cbo_stats->ndv_keys == nullptr) { ctc_log_error("alloc mem failed, m_part_share->cbo_stats->ndv_keys size(%lu)", table->s->keys * sizeof(uint32_t) * MAX_KEY_COLUMNS); return ERR_ALLOC_MEMORY; } + memset(m_part_share->cbo_stats->ndv_keys, 0, table->s->keys * sizeof(uint32_t) * MAX_KEY_COLUMNS); + for (uint i = 0; i < part_num; i++) { m_part_share->cbo_stats->ctc_cbo_stats_table[i].estimate_rows = 0; m_part_share->cbo_stats->ctc_cbo_stats_table[i].columns = (ctc_cbo_stats_column_t*)my_malloc(PSI_NOT_INSTRUMENTED, table->s->fields * sizeof(ctc_cbo_stats_column_t), MYF(MY_WME)); if (m_part_share->cbo_stats->ctc_cbo_stats_table[i].columns == nullptr) { - ctc_log_error("alloc mem failed, m_part_share->cbo_stats->ctc_cbo_stats_table size(%lu)", table->s->fields * sizeof(ctc_cbo_stats_column_t)); - return ERR_ALLOC_MEMORY; - } - for (uint col_id = 0; col_id < table->s->fields; col_id++) { - m_part_share->cbo_stats->ctc_cbo_stats_table[i].columns[col_id].hist_count = 0; + ctc_log_error("alloc mem failed, m_part_share->cbo_stats->ctc_cbo_stats_table size(%lu)", table->s->fields * sizeof(ctc_cbo_stats_column_t)); + return ERR_ALLOC_MEMORY; } + memset(m_part_share->cbo_stats->ctc_cbo_stats_table[i].columns, 0, table->s->fields * sizeof(ctc_cbo_stats_column_t)); } ct_errno_t ret = (ct_errno_t)alloc_str_mysql_mem(m_part_share->cbo_stats, part_num, table); -- Gitee From f8ae7da4e58469378ecd18ba51d9c4eac16b75db Mon Sep 17 00:00:00 2001 From: pengbingjie Date: Mon, 6 Jan 2025 19:57:24 +0800 Subject: [PATCH 2/2] add log --- storage/ctc/ctc_ddl_rewriter_plugin.cc | 76 ++++++++++++++++++++------ 1 file changed, 58 insertions(+), 18 deletions(-) diff --git a/storage/ctc/ctc_ddl_rewriter_plugin.cc b/storage/ctc/ctc_ddl_rewriter_plugin.cc index 15ddec1..661505f 100644 --- a/storage/ctc/ctc_ddl_rewriter_plugin.cc +++ b/storage/ctc/ctc_ddl_rewriter_plugin.cc @@ -93,12 +93,16 @@ int check_default_engine(set_var *setvar, bool &need_forward MY_ATTRIBUTE((unuse my_printf_error(ER_DISALLOWED_OPERATION, "%s", MYF(0), "Once the CTC is loaded, it must be set as the default engine. To modify the setting, uninstall the CTC first."); + ctc_log_error("Once the CTC is loaded, it must be set as the default engine." + "To modify the setting, uninstall the CTC first."); return -1; } if (strcasecmp(setvar->value->item_name.ptr(), ctc_hton_name) != 0) { my_printf_error(ER_DISALLOWED_OPERATION, "%s", MYF(0), "Once the CTC is loaded, it must be set as the default engine. To modify the setting, uninstall the CTC first."); + ctc_log_error("Once the CTC is loaded, it must be set as the default engine. Current engine is %s." + "To modify the setting, uninstall the CTC first.", setvar->value->item_name.ptr()); return -1; } return 0; @@ -112,6 +116,8 @@ int check_session_pool_volume(set_var *setvar, bool &need_forward MY_ATTRIBUTE(( uint max_sessions; if (ctc_get_max_sessions_per_node(&max_sessions)) { my_printf_error(ER_DISALLOWED_OPERATION, "%s", MYF(0), "Get max connections in Cantian failed"); + ctc_log_error("Get max connections in Cantian failed." + "The value of the max connection number is %u.", max_sessions); return -1; } @@ -122,15 +128,21 @@ int check_session_pool_volume(set_var *setvar, bool &need_forward MY_ATTRIBUTE(( if (!isdigit(*user_val_str.c_str())) { my_printf_error(ER_DISALLOWED_OPERATION, "[CTC]:Please make sure value is digits.", MYF(0)); + ctc_log_error("[CTC]:Please make sure value is digits."); return -1; } int tmp_max_connection = atoi(user_val_str.c_str()); if (tmp_max_connection > (int)max_sessions) { - my_printf_error(ER_DISALLOWED_OPERATION, "Current SE can only provide %d connections for one mysql-server", MYF(0), max_sessions); + my_printf_error(ER_DISALLOWED_OPERATION, "Current SE can only provide %u connections for one mysql-server", + MYF(0), max_sessions); + ctc_log_error("Current SE can only provide %u connections for one mysql-server. " + "The value of current connection number is %d.", max_sessions, tmp_max_connection); return -1; } else if (tmp_max_connection < 1) { my_printf_error(ER_DISALLOWED_OPERATION, "Current SE cannot provide less than one connection.", MYF(0)); + ctc_log_error("Current SE cannot provide less than one connection. " + "The value of current connection number is %d.", tmp_max_connection); return -1; } return 0; @@ -138,10 +150,13 @@ int check_session_pool_volume(set_var *setvar, bool &need_forward MY_ATTRIBUTE(( int num_max_conns = atoi(setvar->value->item_name.ptr()); if (num_max_conns > (int)max_sessions) { - my_printf_error(ER_DISALLOWED_OPERATION, "Current SE can only provide %d connections for one mysql-server", MYF(0), max_sessions); + my_printf_error(ER_DISALLOWED_OPERATION, "Current SE can only provide %u connections for one mysql-server", + MYF(0), max_sessions); + ctc_log_error("Current SE can only provide %u connections for one mysql-server.", max_sessions); return -1; } else if (num_max_conns < 1) { my_printf_error(ER_DISALLOWED_OPERATION, "Current SE cannot provide less than one connection.", MYF(0)); + ctc_log_error("Current SE cannot provide less than one connection."); return -1; } return 0; @@ -154,6 +169,7 @@ int not_allow_modify(set_var *setvar, bool &need_forward MY_ATTRIBUTE((unused)), } my_printf_error(ER_DISALLOWED_OPERATION, "%s", MYF(0), "CTC doesn't support modifying the variable"); + ctc_log_error("CTC doesn't support modifying the variable"); return -1; } @@ -180,6 +196,7 @@ int unsupport_tx_isolation_level(set_var *setvar, bool &need_forward MY_ATTRIBUT my_printf_error(ER_DISALLOWED_OPERATION, "%s", MYF(0), "CTC STORAGE ENGINE ONLY SUPPORT READ_COMMITTED TRANSACTION ISOLATION LEVEL."); + ctc_log_error("CTC STORAGE ENGINE ONLY SUPPORT READ_COMMITTED TRANSACTION ISOLATION LEVEL."); return -1; } @@ -188,7 +205,7 @@ int unsupport_tx_isolation_level(set_var *setvar, bool &need_forward MY_ATTRIBUT return 0; } else if (strcasecmp(setvar->value->item_name.ptr(), "repeatable-read") == 0) { push_warning_printf(current_thd, Sql_condition::SL_WARNING, ER_DISALLOWED_OPERATION, - "CTC: The Function of REPEATABLE READ transaction isolation is in progress."); + "CTC: The Function of REPEATABLE READ transaction isolation is in progress."); return 0; } } else { @@ -198,13 +215,14 @@ int unsupport_tx_isolation_level(set_var *setvar, bool &need_forward MY_ATTRIBUT return 0; } else if (tx_isol == ISO_REPEATABLE_READ) { push_warning_printf(current_thd, Sql_condition::SL_WARNING, ER_DISALLOWED_OPERATION, - "CTC: The Function of REPEATABLE READ transaction isolation is in progress."); + "CTC: The Function of REPEATABLE READ transaction isolation is in progress."); return 0; } } my_printf_error(ER_DISALLOWED_OPERATION, "%s", MYF(0), "CTC STORAGE ENGINE ONLY SUPPORT READ_COMMITTED TRANSACTION ISOLATION LEVEL."); + ctc_log_error("CTC STORAGE ENGINE ONLY SUPPORT READ_COMMITTED TRANSACTION ISOLATION LEVEL."); return -1; } @@ -274,6 +292,7 @@ static int ctc_check_dcl(string &, MYSQL_THD thd, bool &need_forward) { } if (allow_sqlcmd(thd, "ctc_dcl_disabled") != 0) { my_printf_error(ER_DISALLOWED_OPERATION, "%s", MYF(0), "DCL query is not allowed (ctc_dcl_disabled = true)"); + ctc_log_error("DCL query is not allowed (ctc_dcl_disabled = true)"); return -1; } return 0; @@ -579,6 +598,7 @@ static int ctc_set_user_var_flag(MYSQL_THD thd, string name, string value) { } else { my_printf_error(ER_UNKNOWN_COM_ERROR, "Invalid variable value for '%s': '%s'", MYF(0), name.c_str(), value.c_str()); + ctc_log_error("Invalid variable value for '%s': '%s'", name.c_str(), value.c_str()); return -1; } } @@ -611,6 +631,7 @@ static int check_non_system_var(set_var_base *var, bool& need_forward, MYSQL_THD setvar_user->print(thd, &set_str); if (set_str.ptr() == nullptr || set_str.length() == 0) { my_printf_error(ER_NOT_ALLOWED_COMMAND, "%s", MYF(0), "The used command is not allowed"); + ctc_log_error("The used command is not allowed"); return -1; } string str(set_str.ptr(), set_str.length()); @@ -675,11 +696,13 @@ static int check_system_var(set_var_base *var, string &sql_str, MYSQL_THD thd, if (strlen(val_str.c_str()) > MAX_DDL_SQL_LEN) { my_printf_error(ER_DISALLOWED_OPERATION, "Set the variable '%s' failed: value is too long", MYF(0), name_str.c_str()); + ctc_log_error("Set the variable '%s' failed: value is too long", name_str.c_str()); return -1; } if (need_forward && allow_sqlcmd(thd, "ctc_setopt_disabled") != 0) { my_printf_error(ER_DISALLOWED_OPERATION, "%s", MYF(0), "Set global variable query is not allowed (ctc_setopt_disabled = true)"); + ctc_log_error("Set global variable query is not allowed (ctc_setopt_disabled = true)"); return -1; } @@ -738,6 +761,7 @@ static int ctc_check_set_opt(string &sql_str, MYSQL_THD thd, bool &need_forward) if (!variables_info.empty()) { if (variables_info.size() > CTC_MAX_SET_VAR_NUM) { my_printf_error(ER_DISALLOWED_OPERATION, "Only %d variables can be set at a time", MYF(0), CTC_MAX_SET_VAR_NUM); + ctc_log_error("Only %d variables can be set at a time", CTC_MAX_SET_VAR_NUM); return -1; } ctc_set_opt_request set_opt_request; @@ -745,6 +769,7 @@ static int ctc_check_set_opt(string &sql_str, MYSQL_THD thd, bool &need_forward) if (ret != 0 && set_opt_request.err_code != 0) { string err_msg = set_opt_request.err_msg; my_printf_error(set_opt_request.err_code, "%s", MYF(0), err_msg.c_str()); + ctc_log_error("%s", err_msg.c_str()); return ret; } } @@ -757,7 +782,10 @@ static int ctc_check_set_opt(string &sql_str, MYSQL_THD thd, bool &need_forward) static int is_system_db(const char *ddl_db) { if (mysql_system_db.find(ddl_db) != mysql_system_db.end()) { my_printf_error(ER_DISALLOWED_OPERATION, "%s", MYF(0), - "Once the CTC is loaded, it must be used as the default engine. To specify other engine for table, uninstall the CTC first."); + "Once the CTC is loaded, it must be used as the default engine." + "To specify other engine for table, uninstall the CTC first."); + ctc_log_error("Once the CTC is loaded, it must be used as the default engine." + "To specify other engine for table, uninstall the CTC first."); return -1; } return 0; @@ -776,21 +804,21 @@ static int ctc_check_ddl_engine(string &, MYSQL_THD thd, bool &need_forward) { } // 检查ddl语句是否显示指定非CTC - if (thd->lex->create_info != nullptr && - thd->lex->create_info->db_type != nullptr && + if (thd->lex->create_info != nullptr && thd->lex->create_info->db_type != nullptr && thd->lex->create_info->db_type != ctc_handlerton && !(thd->lex->create_info->options & HA_LEX_CREATE_TMP_TABLE)) { my_printf_error(ER_DISALLOWED_OPERATION, "%s", MYF(0), - "Once the CTC is loaded, it must be used as the default engine. To specify other engine for table, uninstall the CTC first."); + "Once the CTC is loaded, it must be used as the default engine." + "To specify other engine for table, uninstall the CTC first."); + ctc_log_error("Once the CTC is loaded, it must be used as the default engine." + "To specify other engine for table, uninstall the CTC first."); return -1; } if (!IS_METADATA_NORMALIZATION()) { // create like table 检查是否是系统库 - if (thd->lex->query_tables != nullptr && - thd->lex->query_tables->next_global != nullptr && - thd->lex->create_info != nullptr && - thd->lex->create_info->options & HA_LEX_CREATE_TABLE_LIKE && + if (thd->lex->query_tables != nullptr && thd->lex->query_tables->next_global != nullptr && + thd->lex->create_info != nullptr && thd->lex->create_info->options & HA_LEX_CREATE_TABLE_LIKE && !(thd->lex->create_info->options & HA_LEX_CREATE_TMP_TABLE) && !thd->lex->drop_temporary) { const char *ddl_db = thd->lex->query_tables->next_global->db; @@ -801,11 +829,13 @@ static int ctc_check_ddl_engine(string &, MYSQL_THD thd, bool &need_forward) { // create tablespace 检查是否为engine=Innodb情况 if (thd->lex->sql_command == SQLCOM_ALTER_TABLESPACE) { const Sql_cmd_tablespace *sct = dynamic_cast(thd->lex->m_sql_cmd); - if (sct != nullptr && - sct->get_options().engine_name.str != nullptr && + if (sct != nullptr && sct->get_options().engine_name.str != nullptr && strcmp(sct->get_options().engine_name.str, ctc_name.str) != 0) { my_printf_error(ER_DISALLOWED_OPERATION, "%s", MYF(0), - "Once the CTC is loaded, it must be used as the default engine. To specify other engine for table, uninstall the CTC first."); + "Once the CTC is loaded, it must be used as the default engine." + "To specify other engine for table, uninstall the CTC first."); + ctc_log_error("Once the CTC is loaded, it must be used as the default engine." + "To specify other engine for table, uninstall the CTC first."); return -1; } } @@ -830,6 +860,7 @@ static int ctc_check_ddl(string &, MYSQL_THD, bool &need_forward) { static int ctc_check_unspport_ddl(string &, MYSQL_THD, bool &) { my_printf_error(ER_DISALLOWED_OPERATION, "%s", MYF(0), "Cantian doesn't support current operation"); + ctc_log_error("Cantian doesn't support current operation"); return -1; } @@ -849,6 +880,7 @@ static int ctc_lock_tables_ddl(string &, MYSQL_THD thd, bool &) { if (pre_lock_ret != 0) { ctc_lock_table_post(thd, ticket_list); my_printf_error(ER_LOCK_WAIT_TIMEOUT, "[CTC_DDL_REWRITE]: LOCK TABLE FAILED", MYF(0)); + ctc_log_error("[CTC_DDL_REWRITE]: LOCK TABLE FAILED"); return ER_LOCK_WAIT_TIMEOUT; } #ifdef FEATURE_X_FOR_MYSQL_32 @@ -1093,7 +1125,8 @@ static void ctc_ddl_rewrite_handle_error(MYSQL_THD thd, int ret, ctc_ddl_broadca if (ret == CTC_DDL_VERSION_NOT_MATCH) { broadcast_req.err_code = ER_DISALLOWED_OPERATION; my_printf_error(ER_DISALLOWED_OPERATION, "Version not match. Please make sure cluster on the same version.", MYF(0)); - ctc_log_system("[CTC_DDL_REWRITE]: Version not match, sql=%s", sql_without_plaintext_password(&broadcast_req).c_str()); + ctc_log_system("[CTC_DDL_REWRITE]: Version not match, sql=%s", + sql_without_plaintext_password(&broadcast_req).c_str()); return; } @@ -1101,7 +1134,8 @@ static void ctc_ddl_rewrite_handle_error(MYSQL_THD thd, int ret, ctc_ddl_broadca broadcast_req.err_code, broadcast_req.err_msg); ctc_log_error("[CTC_DDL_REWRITE]:Got error on remote mysql, query:%s, user_name:%s, err_code:%d, err_msg:%s", - sql_without_plaintext_password(&broadcast_req).c_str(), broadcast_req.user_name, broadcast_req.err_code, broadcast_req.err_msg); + sql_without_plaintext_password(&broadcast_req).c_str(), broadcast_req.user_name, + broadcast_req.err_code, broadcast_req.err_msg); // unlock when lock instance failed if (sql_cmd == SQLCOM_LOCK_INSTANCE) { @@ -1141,6 +1175,7 @@ int ddl_broadcast_and_wait(MYSQL_THD thd, string &query_str, if (pre_lock_ret != 0) { ctc_lock_table_post(thd, ticket_list); my_printf_error(ER_LOCK_WAIT_TIMEOUT, "[CTC_DDL_REWRITE]: LOCK TABLE FAILED", MYF(0)); + ctc_log_error("[CTC_DDL_REWRITE]: LOCK TABLE FAILED"); return ER_LOCK_WAIT_TIMEOUT; } } @@ -1197,6 +1232,9 @@ bool check_agent_connection(MYSQL_THD thd) { "%s@%s is not allowed for DDL remote execution!", MYF(0), thd->m_main_security_ctx.priv_user().str, thd->m_main_security_ctx.priv_host().str); + ctc_log_error("%s@%s is not allowed for DDL remote execution!", + thd->m_main_security_ctx.priv_user().str, + thd->m_main_security_ctx.priv_host().str); return true; } @@ -1205,6 +1243,7 @@ bool check_agent_connection(MYSQL_THD thd) { if (ctc_init_agent_client(agent_conn) != 0) { my_printf_error(ER_DISALLOWED_OPERATION, "Failed to establish connection for DDL remote execution!", MYF(0)); + ctc_log_error("Failed to establish connection for DDL remote execution!"); ctc_close_mysql_conn(&agent_conn); return true; } @@ -1278,6 +1317,7 @@ bool plugin_ddl_block(MYSQL_THD thd, // disallow ddl query if ctc_concurrent_ddl=OFF and ctc_enable_ddl not set if (!ddl_enabled_normal(thd)) { my_printf_error(ER_DISALLOWED_OPERATION, "%s", MYF(0), "DDL not allowed in this mode, Please check the value of @@ctc_concurrent_ddl."); + ctc_log_error("DDL not allowed in this mode, Please check the value of @@ctc_concurrent_ddl."); return true; } @@ -1361,7 +1401,7 @@ static int ctc_check_metadata_switch() { return 1; case metadata_switchs::CLUSTER_NOT_READY: ctc_log_error("[CTC_META]: Cantian cluster not ready"); - my_printf_error(ER_DISALLOWED_OPERATION, "%s", MYF(0), "CANTIAN cluster not read."); + my_printf_error(ER_DISALLOWED_OPERATION, "%s", MYF(0), "CANTIAN cluster not ready."); return -1; case metadata_switchs::NOT_MATCH: ctc_log_error("[CTC_META]: The metadata switch of CTC and CANTIAN not match"); -- Gitee