diff --git a/storage/ctc/ctc_srv_mq_module.cc b/storage/ctc/ctc_srv_mq_module.cc index 80a07067d2d61d737e6898a95518bd6e7a9c3835..3de0e94fa6337ae0188589c4b3a0d8d472f0f015 100644 --- a/storage/ctc/ctc_srv_mq_module.cc +++ b/storage/ctc/ctc_srv_mq_module.cc @@ -334,11 +334,6 @@ static void ctc_log_reg_error_by_code(int error_code) EXTER_ATTACK int ctc_mq_deal_func(void *shm_inst, CTC_FUNC_TYPE func_type, void *request, void* msg_buf, uint32_t server_id, uint32_t wait_sec) { - uint64_t start_time = 0; - if (ctc_stats::get_instance().get_statistics_enabled()) { - start_time = my_getsystime() / 10; - } - shm_seg_s *seg = (shm_seg_s *)shm_inst; dsw_message_block_t *msg; bool is_alloc = false; @@ -411,11 +406,6 @@ EXTER_ATTACK int ctc_mq_deal_func(void *shm_inst, CTC_FUNC_TYPE func_type, } shm_free(nullptr, msg); } - - - if (ctc_stats::get_instance().get_statistics_enabled()) { - ctc_stats::get_instance().gather_stats(func_type, my_getsystime() / 10 - start_time); - } return result; } diff --git a/storage/ctc/ctc_stats.cc b/storage/ctc/ctc_stats.cc index a03a218bb16c8f8f388a243561bea203deada015..5a54fd17ed390edce6201d7421c2367cb56d275a 100644 --- a/storage/ctc/ctc_stats.cc +++ b/storage/ctc/ctc_stats.cc @@ -18,78 +18,50 @@ #include "ctc_log.h" #include -const char *ctc_interface_strs[] = { - "CTC_FUNC_TYPE_OPEN_TABLE", - "CTC_FUNC_TYPE_CLOSE_TABLE", - "CTC_FUNC_TYPE_CLOSE_SESSION", - "CTC_FUNC_TYPE_WRITE_ROW", - "CTC_FUNC_TYPE_UPDATE_JOB", - "CTC_FUNC_TYPE_UPDATE_ROW", - "CTC_FUNC_TYPE_DELETE_ROW", - "CTC_FUNC_TYPE_RND_INIT", - "CTC_FUNC_TYPE_RND_END", - "CTC_FUNC_TYPE_RND_NEXT", - "CTC_FUNC_TYPE_RND_PREFETCH", - "CTC_FUNC_TYPE_SCAN_RECORDS", - "CTC_FUNC_TYPE_TRX_COMMIT", - "CTC_FUNC_TYPE_TRX_ROLLBACK", - "CTC_FUNC_TYPE_TRX_BEGIN", - "CTC_FUNC_TYPE_LOCK_TABLE", - "CTC_FUNC_TYPE_UNLOCK_TABLE", - "CTC_FUNC_TYPE_INDEX_END", - "CTC_FUNC_TYPE_SRV_SET_SAVEPOINT", - "CTC_FUNC_TYPE_SRV_ROLLBACK_SAVEPOINT", - "CTC_FUNC_TYPE_SRV_RELEASE_SAVEPOINT", - "CTC_FUNC_TYPE_GENERAL_FETCH", - "CTC_FUNC_TYPE_GENERAL_PREFETCH", - "CTC_FUNC_TYPE_FREE_CURSORS", - "CTC_FUNC_TYPE_GET_INDEX_NAME", - "CTC_FUNC_TYPE_INDEX_READ", - "CTC_FUNC_TYPE_RND_POS", - "CTC_FUNC_TYPE_POSITION", - "CTC_FUNC_TYPE_DELETE_ALL_ROWS", - "CTC_FUNC_TYPE_GET_CBO_STATS", - "CTC_FUNC_TYPE_WRITE_LOB", - "CTC_FUNC_TYPE_READ_LOB", - "CTC_FUNC_TYPE_CREATE_TABLE", - "CTC_FUNC_TYPE_TRUNCATE_TABLE", - "CTC_FUNC_TYPE_TRUNCATE_PARTITION", - "CTC_FUNC_TYPE_RENAME_TABLE", - "CTC_FUNC_TYPE_ALTER_TABLE", - "CTC_FUNC_TYPE_GET_SERIAL_VALUE", - "CTC_FUNC_TYPE_DROP_TABLE", - "CTC_FUNC_TYPE_EXCUTE_MYSQL_DDL_SQL", - "CTC_FUNC_TYPE_BROADCAST_REWRITE_SQL", - "CTC_FUNC_TYPE_CREATE_TABLESPACE", - "CTC_FUNC_TYPE_ALTER_TABLESPACE", - "CTC_FUNC_TYPE_DROP_TABLESPACE", - "CTC_FUNC_TYPE_BULK_INSERT", - "CTC_FUNC_TYPE_ANALYZE", - "CTC_FUNC_TYPE_GET_MAX_SESSIONS", - "CTC_FUNC_LOCK_INSTANCE", - "CTC_FUNC_UNLOCK_INSTANCE", - "CTC_FUNC_CHECK_TABLE_EXIST", - "CTC_FUNC_SEARCH_METADATA_SWITCH", - "CTC_FUNC_QUERY_SHM_USAGE", - "CTC_FUNC_QUERY_CLUSTER_ROLE", - "CTC_FUNC_SET_CLUSTER_ROLE_BY_CANTIAN", - "CTC_FUNC_PRE_CREATE_DB", - "CTC_FUNC_TYPE_DROP_TABLESPACE_AND_USER", - "CTC_FUNC_DROP_DB_PRE_CHECK", - "CTC_FUNC_KILL_CONNECTION", - "CTC_FUNC_TYPE_INVALIDATE_OBJECT", - "CTC_FUNC_TYPE_RECORD_SQL", - "CTC_FUNC_TYPE_REGISTER_INSTANCE", - "CTC_FUNC_QUERY_SHM_FILE_NUM", - "CTC_FUNC_TYPE_WAIT_CONNETOR_STARTUPED", - "CTC_FUNC_TYPE_MYSQL_EXECUTE_UPDATE", - "CTC_FUNC_TYPE_CLOSE_MYSQL_CONNECTION", - "CTC_FUNC_TYPE_LOCK_TABLES", - "CTC_FUNC_TYPE_UNLOCK_TABLES", - "CTC_FUNC_TYPE_EXECUTE_REWRITE_OPEN_CONN", - "CTC_FUNC_TYPE_INVALIDATE_OBJECTS", - "CTC_FUNC_TYPE_INVALIDATE_ALL_OBJECTS", - "CTC_FUNC_TYPE_UPDATE_DDCACHE", +const char *event_tracking_messages[] = { + //DATABASE + "EVENT_TYPE_PRE_CREATE_DB", + "EVENT_TYPE_DROP_DB", + //TABLE + "EVENT_TYPE_OPEN_TABLE", + "EVENT_TYPE_CLOSE_TABLE", + "EVENT_TYPE_CREATE_TABLE", + "EVENT_TYPE_RENAME_TABLE", + "EVENT_TYPE_DROP_TABLE", + "EVENT_TYPE_INPLACE_ALTER_TABLE", + //DML-FETCH + "EVENT_TYPE_RND_INIT", + "EVENT_TYPE_RND_NEXT", + "EVENT_TYPE_POSITION", + "EVENT_TYPE_RND_POS", + "EVENT_TYPE_RND_END", + //DML + "EVENT_TYPE_WRITE_ROW", + "EVENT_TYPE_UPDATE_ROW", + "EVENT_TYPE_DELETE_ROW", + "EVENT_TYPE_DELETE_ALL_ROWS", + //INDEX + "EVENT_TYPE_INDEX_INIT", + "EVENT_TYPE_INDEX_END", + "EVENT_TYPE_INDEX_READ", + "EVENT_TYPE_INDEX_FETCH", + //CBO + "EVENT_TYPE_INITIALIZE_DBO", + "EVENT_TYPE_FREE_CBO", + "EVENT_TYPE_GET_CBO", + "EVENT_TYPE_CBO_ANALYZE", + "EVENT_TYPE_CBO_RECORDS", + "EVENT_TYPE_CBO_RECORDS_IN_RANGE", + //TRANSCTIONS + "EVENT_TYPE_COMMIT", + "EVENT_TYPE_ROLLBACK", + "EVENT_TYPE_BEGIN_TRX", + "EVENT_TYPE_RELEASE_SAVEPOINT", + "EVENT_TYPE_SET_SAVEPOINT", + "EVENT_TYPE_ROLLBACK_SAVEPOINT", + //CONNECTION + "EVENT_TYPE_KILL_CONNECTION", + "EVENT_TYPE_CLOSE_CONNECTION", }; @@ -144,38 +116,55 @@ void ctc_stats::set_statistics_enabled(const bool val) { for (int i = 0; i < CTC_FUNC_TYPE_NUMBER; i++) { m_calls[i] = 0; m_use_time[i] = 0; + initialize_clock_freq(); } } m_statistics_enabled = val; } -void ctc_stats::gather_stats(const enum CTC_FUNC_TYPE& type, const uint64_t use_time) { +void ctc_stats::gather_stats(const enum EVENT_TRACKING type, const uint64_t use_time) { + if (clock_frequency == 0) { + ctc_log_error("[TSE STATS] clock frequency is not initialized."); + return; + } m_calls[type]++; - m_use_time[type] += use_time; + m_use_time[type] += use_time/clock_frequency; } void ctc_stats::print_cost_times(std::string &ctc_srv_monitor_str) { - if ((sizeof(ctc_interface_strs) / sizeof(ctc_interface_strs[0])) != CTC_FUNC_TYPE_NUMBER) { + if ((sizeof(event_tracking_messages) / sizeof(event_tracking_messages[0])) != EVENT_TYPE_COUNT) { ctc_srv_monitor_str += "[CTC_STATS]: ctc_interface_strs number must be same as total ctc interfaces.\n"; return; } - ctc_srv_monitor_str += "\n======================================CTC_STATS=====================================\n"; - ctc_srv_monitor_str += "Interface: Call counter Used Time Average Time\n"; - for (int i = 0; i < CTC_FUNC_TYPE_NUMBER; i++) { + ctc_srv_monitor_str += "\n===================================================CTC_STATS===================================================\n"; + size_t size = EVENT_TYPE_COUNT; + auto longestStr = std::max_element(event_tracking_messages, event_tracking_messages + size, + [](const char* a, const char* b) { + return std::strlen(a) < std::strlen(b); + }); + int interf_width = strlen(*longestStr); + ctc_srv_monitor_str += "Interface" + std::string(interf_width - strlen("Interface"), ' '); + ctc_srv_monitor_str += "Call counter Used Time Average Time\n"; + + for (int i = 0; i < EVENT_TYPE_COUNT; i++) { uint64_t calls = m_calls[i]; uint64_t use_time = m_use_time[i]; if (calls == 0) { continue; } - double average_time = (double) use_time / calls; - ctc_srv_monitor_str += ctc_interface_strs[i]; - ctc_srv_monitor_str += ": " + std::to_string(calls) + " " + std::to_string(use_time) + " "+ std::to_string(average_time)+"\n"; + uint64_t average_time = use_time / calls; + ctc_srv_monitor_str += event_tracking_messages[i]; + std::string calls_str = std::to_string(calls); + std::string total_str = std::to_string(use_time); + std::string avg_str = std::to_string(average_time); + ctc_srv_monitor_str += std::string(interf_width - strlen(event_tracking_messages[i]), ' ') + calls_str + std::string(CTC_STATS_TABLE_COL_WIDTH - calls_str.size(), ' ') + + total_str + std::string(CTC_STATS_TABLE_COL_WIDTH - total_str.size(), ' ') + avg_str + "\n"; } - ctc_srv_monitor_str += "\n======================================CTC_STATS=====================================\n"; + ctc_srv_monitor_str += "\n====================================================CTC_STATS================================================\n"; } #ifndef WITH_CANTIAN @@ -224,3 +213,47 @@ void ctc_stats::print_stats(THD *thd, stat_print_fn *stat_print) { stat_print(thd, "ctc", static_cast(strlen("ctc")), STRING_WITH_LEN(""), ctc_srv_monitor, (uint)ctc_srv_monitor_str.length()); } + +#if (defined __x86_64__) +void ctc_stats::initialize_clock_freq() +{ + FILE *fp = fopen("/proc/cpuinfo", "r"); + if (!fp) { + ctc_log_error("[IO RECORD] Failed to open 'proc/cpuinfo"); + my_error(ER_INTERNAL_ERROR, MYF(0), "Clock frequency initialization failed: unable to open cpuinfo."); + return; + } + char line[100]; + double freq = 0; + while (fgets(line, sizeof(line), fp)) { + if (sscanf(line, "cpu MHz : %lf", &freq) == 1) { + fclose(fp); + clock_frequency = freq; + return; + } + } + ctc_log_error("[io record] failed to get cpu frequency."); + my_error(ER_INTERNAL_ERROR, MYF(0), "Clock frequency initialization failed: unable to get cpu frequency."); +} +#else +void ctc_stats::initialize_clock_freq() +{ + uint64_t freq; + __asm__ volatile("mrs %0, cntfrq_el0" : "=r"(freq)); + clock_frequency = freq; +} +#endif + +#if (defined __x86_64__) +#include +uint64_t rdtsc(){ + return __rdtsc(); +} +#else +uint64_t rdtsc() +{ + uint64_t tsc; + __asm__ volatile ("mrs %0, cntvct_el0" : "=r" (tsc)); + return tsc; +} +#endif diff --git a/storage/ctc/ctc_stats.h b/storage/ctc/ctc_stats.h index 3e2d168e434224506a44fec5d6dc1df75ff21a20..51eb878cb00d6ecd02f919257bd3541829146ef0 100644 --- a/storage/ctc/ctc_stats.h +++ b/storage/ctc/ctc_stats.h @@ -26,6 +26,70 @@ #include "ctc_srv.h" #include "ha_ctc.h" +#define BEGIN_RECORD_STATS \ + uint64_t start_clock = 0; \ + if (ctc_stats::get_instance().get_statistics_enabled()) { \ + start_clock = rdtsc(); \ + } + +#define END_RECORD_STATS(type) \ + if (ctc_stats::get_instance().get_statistics_enabled()) { \ + ctc_stats::get_instance().gather_stats(type, rdtsc() - start_clock); \ + } + +#define CTC_STATS_TABLE_COL_WIDTH 25 + + +enum EVENT_TRACKING { + //DATABASE + EVENT_TYPE_PRE_CREATE_DB, + EVENT_TYPE_DROP_DB, + //TABLE + EVENT_TYPE_OPEN_TABLE, + EVENT_TYPE_CLOSE_TABLE, + EVENT_TYPE_CREATE_TABLE, + EVENT_TYPE_RENAME_TABLE, + EVENT_TYPE_DROP_TABLE, + EVENT_TYPE_INPLACE_ALTER_TABLE, + //DML-FETCH + EVENT_TYPE_RND_INIT, + EVENT_TYPE_RND_NEXT, + EVENT_TYPE_POSITION, + EVENT_TYPE_RND_POS, + EVENT_TYPE_RND_END, + //DML + EVENT_TYPE_WRITE_ROW, + EVENT_TYPE_UPDATE_ROW, + EVENT_TYPE_DELETE_ROW, + EVENT_TYPE_DELETE_ALL_ROWS, + //INDEX + EVENT_TYPE_INDEX_INIT, + EVENT_TYPE_INDEX_END, + EVENT_TYPE_INDEX_READ, + EVENT_TYPE_INDEX_FETCH, + //CBO + EVENT_TYPE_INITIALIZE_DBO, + EVENT_TYPE_FREE_CBO, + EVENT_TYPE_GET_CBO, + EVENT_TYPE_CBO_ANALYZE, + EVENT_TYPE_CBO_RECORDS, + EVENT_TYPE_CBO_RECORDS_IN_RANGE, + //TRANSCTIONS + EVENT_TYPE_COMMIT, + EVENT_TYPE_ROLLBACK, + EVENT_TYPE_BEGIN_TRX, + EVENT_TYPE_RELEASE_SAVEPOINT, + EVENT_TYPE_SET_SAVEPOINT, + EVENT_TYPE_ROLLBACK_SAVEPOINT, + //CONNECTION + EVENT_TYPE_KILL_CONNECTION, + EVENT_TYPE_CLOSE_CONNECTION, + + EVENT_TYPE_COUNT, +}; + + + class ctc_stats { private: ctc_stats(void) = default; @@ -38,15 +102,20 @@ class ctc_stats { static ctc_stats& get_instance(void) noexcept; bool get_statistics_enabled(void); void set_statistics_enabled(const bool val); - void gather_stats(const enum CTC_FUNC_TYPE& type, const uint64_t use_time); + void gather_stats(const enum EVENT_TRACKING type, const uint64_t use_time); void print_stats(THD *thd, stat_print_fn *stat_print); void print_cost_times(std::string &ctc_srv_monitor_str); void print_shm_usage(std::string &ctc_srv_monitor_str); private: bool m_statistics_enabled = false; + uint64_t clock_frequency = 0; - std::atomic_uint64_t m_calls[CTC_FUNC_TYPE_NUMBER]; - std::atomic_uint64_t m_use_time[CTC_FUNC_TYPE_NUMBER]; + void initialize_clock_freq(); + + std::atomic_uint64_t m_calls[EVENT_TYPE_COUNT]; + std::atomic_uint64_t m_use_time[EVENT_TYPE_COUNT]; }; +uint64_t rdtsc(); + #endif diff --git a/storage/ctc/ha_ctc.cc b/storage/ctc/ha_ctc.cc index 6032249fe1662ecdff2860385382399a79d57d13..76161559d4252476903bedca8bd2477a2398ff35 100644 --- a/storage/ctc/ha_ctc.cc +++ b/storage/ctc/ha_ctc.cc @@ -143,9 +143,17 @@ * mysql> SHOW GLOBAL VARIABLES like'%ctc%' */ -static void ctc_statistics_enabled_update(THD *, SYS_VAR *, void *var_ptr, const void *save) { + + + +static void ctc_statistics_enabled_update(THD * thd, SYS_VAR *, void *var_ptr, const void *save) { bool val = *static_cast(var_ptr) = *static_cast(save); ctc_stats::get_instance().set_statistics_enabled(val); + if (val) { + push_warning_printf(thd, Sql_condition::SL_WARNING, ER_DISALLOWED_OPERATION, + "CTC: ONLY FOR developers profiling and testing purposes! \ + Turning on this switch will cause a significant performance DEGRADATION! ."); + } } /* 创库的表空间datafile自动扩展, 默认开 */ @@ -1687,8 +1695,9 @@ static void ctc_free_cursors_no_autocommit(THD *thd, ctc_handler_t *tch, thd_ses */ static int ctc_commit(handlerton *hton, THD *thd, bool commit_trx) { DBUG_TRACE; - + BEGIN_RECORD_STATS if (engine_ddl_passthru(thd) && (is_alter_table_copy(thd) || is_create_table_check(thd) || is_lock_table(thd))) { + END_RECORD_STATS(EVENT_TYPE_COMMIT) return 0; } @@ -1718,6 +1727,7 @@ static int ctc_commit(handlerton *hton, THD *thd, bool commit_trx) { ctc_free_buf(&tch, (uint8_t *)cursors); if (ret != CT_SUCCESS) { ctc_log_error("commit atomic ddl failed with error code: %d", ret); + END_RECORD_STATS(EVENT_TYPE_COMMIT) return convert_ctc_error_code_to_mysql(ret); } if (is_ddl_commit && !engine_skip_ddl(thd)) { @@ -1745,6 +1755,7 @@ static int ctc_commit(handlerton *hton, THD *thd, bool commit_trx) { sess_ctx->sql_stat_start = 1; // indicate cantian for a new sql border tch.sql_stat_start = 1; } + END_RECORD_STATS(EVENT_TYPE_COMMIT) return 0; } @@ -1761,7 +1772,7 @@ static int ctc_commit(handlerton *hton, THD *thd, bool commit_trx) { */ static int ctc_rollback(handlerton *hton, THD *thd, bool rollback_trx) { DBUG_TRACE; - + BEGIN_RECORD_STATS if (thd->lex->sql_command == SQLCOM_DROP_TABLE) { ctc_log_error("[CTC_TRX]:rollback when drop table, rollback_trx=%d", rollback_trx); } @@ -1789,6 +1800,7 @@ static int ctc_rollback(handlerton *hton, THD *thd, bool rollback_trx) { if (ret != CT_SUCCESS) { ctc_free_buf(&tch, (uint8_t *)cursors); ctc_log_error("rollback trx failed with error code: %d", ret); + END_RECORD_STATS(EVENT_TYPE_ROLLBACK) return convert_ctc_error_code_to_mysql(ret); } ctc_free_buf(&tch, (uint8_t *)cursors); @@ -1814,11 +1826,12 @@ static int ctc_rollback(handlerton *hton, THD *thd, bool rollback_trx) { sess_ctx->sql_stat_start = 1; // indicate cantian for a new sql border tch.sql_stat_start = 1; } - + END_RECORD_STATS(EVENT_TYPE_ROLLBACK) return 0; } static int ctc_close_connect(handlerton *hton, THD *thd) { + BEGIN_RECORD_STATS ctc_handler_t tch; CTC_RETURN_IF_NOT_ZERO(get_tch_in_handler_data(hton, thd, tch)); thd_sess_ctx_s *sess_ctx = (thd_sess_ctx_s *)thd_get_ha_data(thd, hton); @@ -1838,10 +1851,12 @@ static int ctc_close_connect(handlerton *hton, THD *thd) { int ret = ctc_close_session(&local_tch); release_sess_ctx(sess_ctx, hton, thd); + END_RECORD_STATS(EVENT_TYPE_CLOSE_CONNECTION) return convert_ctc_error_code_to_mysql((ct_errno_t)ret); } static void ctc_kill_connection(handlerton *hton, THD *thd) { + BEGIN_RECORD_STATS ctc_handler_t tch; int ret = get_tch_in_handler_data(hton, thd, tch); if (ret != CT_SUCCESS) { @@ -1850,10 +1865,12 @@ static void ctc_kill_connection(handlerton *hton, THD *thd) { if (tch.sess_addr == INVALID_VALUE64) { ctc_log_system("[CTC_KILL_SESSION]:trying to kill a thd without session assigned, conn_id=%u, instid=%u", tch.thd_id, tch.inst_id); + END_RECORD_STATS(EVENT_TYPE_KILL_CONNECTION) return; } if (is_ddl_sql_cmd(thd->lex->sql_command)) { + END_RECORD_STATS(EVENT_TYPE_KILL_CONNECTION) return; } @@ -1865,10 +1882,13 @@ static void ctc_kill_connection(handlerton *hton, THD *thd) { ctc_kill_session(&local_tch); ctc_log_system("[CTC_KILL_SESSION]:conn_id:%u, ctc_instance_id:%u", tch.thd_id, tch.inst_id); + END_RECORD_STATS(EVENT_TYPE_KILL_CONNECTION) } static int ctc_pre_create_db4cantian(THD *thd, ctc_handler_t *tch) { + BEGIN_RECORD_STATS if (engine_skip_ddl(thd)) { + END_RECORD_STATS(EVENT_TYPE_PRE_CREATE_DB) return CT_SUCCESS; } char user_name[SMALL_RECORD_SIZE] = { 0 }; @@ -1896,9 +1916,11 @@ static int ctc_pre_create_db4cantian(THD *thd, ctc_handler_t *tch) { /* 如果参天上报tablespace或user已存在,且创库命令包含if not exists关键字,则忽略此错误 */ if (error_code == ERR_USER_NOT_EMPTY_4MYSQL) { if (thd->lex->create_info->options & HA_LEX_CREATE_IF_NOT_EXISTS) { + END_RECORD_STATS(EVENT_TYPE_PRE_CREATE_DB) return CT_SUCCESS; } my_printf_error(ER_DB_CREATE_EXISTS, "Can't create database '%s'; database exists", MYF(0), thd->lex->name.str); + END_RECORD_STATS(EVENT_TYPE_PRE_CREATE_DB) return ER_DB_CREATE_EXISTS; } @@ -1907,6 +1929,7 @@ static int ctc_pre_create_db4cantian(THD *thd, ctc_handler_t *tch) { } } ctc_log_system("[CTC_INIT]:ctc_pre_create_db4cantian end, ret=%d", ret); + END_RECORD_STATS(EVENT_TYPE_PRE_CREATE_DB) return ret; } @@ -2129,7 +2152,7 @@ static const int BASE36 = 36; // 0~9 and a~z, total 36 encoded character */ static int ctc_set_savepoint(handlerton *hton, THD *thd, void *savepoint) { DBUG_TRACE; - + BEGIN_RECORD_STATS char name[MAX_SAVEPOINT_NAME_LEN]; longlong2str((unsigned long long)savepoint, name, BASE36); ctc_handler_t tch; @@ -2142,6 +2165,7 @@ static int ctc_set_savepoint(handlerton *hton, THD *thd, void *savepoint) { if (ret != CT_SUCCESS) { ctc_log_error("set trx savepoint failed with error code: %d", ret); } + END_RECORD_STATS(EVENT_TYPE_SET_SAVEPOINT) return convert_ctc_error_code_to_mysql(ret); } @@ -2154,7 +2178,7 @@ static int ctc_set_savepoint(handlerton *hton, THD *thd, void *savepoint) { */ static int ctc_rollback_savepoint(handlerton *hton, THD *thd, void *savepoint) { DBUG_TRACE; - + BEGIN_RECORD_STATS char name[MAX_SAVEPOINT_NAME_LEN]; longlong2str((unsigned long long)savepoint, name, BASE36); ctc_handler_t tch; @@ -2176,6 +2200,7 @@ static int ctc_rollback_savepoint(handlerton *hton, THD *thd, void *savepoint) { if (ret != CT_SUCCESS) { ctc_log_error("rollback to trx savepoint failed with error code: %d", ret); } + END_RECORD_STATS(EVENT_TYPE_ROLLBACK_SAVEPOINT) return convert_ctc_error_code_to_mysql(ret); } @@ -2188,7 +2213,7 @@ static int ctc_rollback_savepoint(handlerton *hton, THD *thd, void *savepoint) { */ static int ctc_release_savepoint(handlerton *hton, THD *thd, void *savepoint) { DBUG_TRACE; - + BEGIN_RECORD_STATS /** * SQLCOM_SAVEPOINT命令如果发现之前已保存有重名的savepoint,mysql会触发调用ctc_release_savepoint, * 该种场景下就不需要再调用ctc_srv_release_savepoint了,knl_set_savepoint接口内部会去掉重名的savepoint; @@ -2197,6 +2222,7 @@ static int ctc_release_savepoint(handlerton *hton, THD *thd, void *savepoint) { * 与innodb行为不一致,在某些场景下会引发缺陷 */ if (thd->query_plan.get_command() == SQLCOM_SAVEPOINT) { + END_RECORD_STATS(EVENT_TYPE_RELEASE_SAVEPOINT) return 0; } @@ -2208,6 +2234,7 @@ static int ctc_release_savepoint(handlerton *hton, THD *thd, void *savepoint) { if (ret != CT_SUCCESS) { ctc_log_error("release trx savepoint failed with error code: %d", ret); } + END_RECORD_STATS(EVENT_TYPE_RELEASE_SAVEPOINT) return convert_ctc_error_code_to_mysql(ret); } @@ -2429,6 +2456,7 @@ bool ctc_is_temporary(const dd::Table *table_def) { EXTER_ATTACK int ha_ctc::open(const char *name, int, uint test_if_locked, const dd::Table *table_def) { DBUG_TRACE; + BEGIN_RECORD_STATS assert(table_share == table->s); THD *thd = ha_thd(); @@ -2445,15 +2473,18 @@ EXTER_ATTACK int ha_ctc::open(const char *name, int, uint test_if_locked, const if (table_share->m_part_info == nullptr) { free_share(); } + END_RECORD_STATS(EVENT_TYPE_OPEN_TABLE) return convert_ctc_error_code_to_mysql(ret); } } if (is_replay_ddl(thd)) { + END_RECORD_STATS(EVENT_TYPE_OPEN_TABLE) return 0; } // rename table的故障场景下,参天的表不存在,需要忽略open close table的错误 if (engine_ddl_passthru(thd) && (is_alter_table_copy(thd, table->s->table_name.str) || is_lock_table(thd))) { + END_RECORD_STATS(EVENT_TYPE_OPEN_TABLE) return 0; } @@ -2483,6 +2514,7 @@ EXTER_ATTACK int ha_ctc::open(const char *name, int, uint test_if_locked, const free_share(); } + END_RECORD_STATS(EVENT_TYPE_OPEN_TABLE) return convert_ctc_error_code_to_mysql(ret); } @@ -2503,9 +2535,11 @@ EXTER_ATTACK int ha_ctc::open(const char *name, int, uint test_if_locked, const int ha_ctc::close(void) { DBUG_TRACE; + BEGIN_RECORD_STATS THD *thd = ha_thd(); if (get_server_state() != SERVER_OPERATING && thd == nullptr) { + END_RECORD_STATS(EVENT_TYPE_CLOSE_TABLE) return 0; } @@ -2514,23 +2548,27 @@ int ha_ctc::close(void) { } if (is_replay_ddl(thd)) { + END_RECORD_STATS(EVENT_TYPE_CLOSE_TABLE) return 0; } // rename table的故障场景下,参天的表不存在,需要忽略open close table的错误 if (engine_ddl_passthru(thd) && (is_alter_table_copy(thd, table->s->table_name.str))) { + END_RECORD_STATS(EVENT_TYPE_CLOSE_TABLE) return 0; } update_member_tch(m_tch, ctc_hton, thd); if (m_tch.ctx_addr == INVALID_VALUE64) { ctc_log_warning("[CTC_CLOSE_TABLE]:Close a table that is not open."); + END_RECORD_STATS(EVENT_TYPE_CLOSE_TABLE) return 0; } ct_errno_t ret = (ct_errno_t)ctc_close_table(&m_tch); check_error_code_to_mysql(ha_thd(), &ret); m_tch.ctx_addr = INVALID_VALUE64; + END_RECORD_STATS(EVENT_TYPE_CLOSE_TABLE) return convert_ctc_error_code_to_mysql(ret); } @@ -2608,9 +2646,11 @@ EXTER_ATTACK int ha_ctc::write_row(uchar *buf, bool write_through) { EXTER_ATTACK int ha_ctc::write_row(uchar *buf) { #endif DBUG_TRACE; + BEGIN_RECORD_STATS THD *thd = ha_thd(); if (engine_ddl_passthru(thd) && is_create_table_check(thd)) { + END_RECORD_STATS(EVENT_TYPE_WRITE_ROW) return CT_SUCCESS; } @@ -2625,6 +2665,7 @@ EXTER_ATTACK int ha_ctc::write_row(uchar *buf) { if (table->next_number_field && buf == table->record[0]) { error_result = handle_auto_increment(has_explicit_autoinc); if (error_result != CT_SUCCESS) { + END_RECORD_STATS(EVENT_TYPE_WRITE_ROW) return error_result; } auto_inc_used = true; @@ -2639,6 +2680,7 @@ EXTER_ATTACK int ha_ctc::write_row(uchar *buf) { flag.write_through = false; #endif error_result = convert_mysql_record_and_write_to_cantian(buf, &cantian_record_buf_size, &serial_column_offset, flag); + END_RECORD_STATS(EVENT_TYPE_WRITE_ROW) return error_result; } @@ -2648,6 +2690,7 @@ EXTER_ATTACK int ha_ctc::write_row(uchar *buf) { if (error_result != CT_SUCCESS) { delete m_rec_buf_4_writing; m_rec_buf_4_writing = nullptr; + END_RECORD_STATS(EVENT_TYPE_WRITE_ROW) return error_result; } @@ -2667,7 +2710,7 @@ EXTER_ATTACK int ha_ctc::write_row(uchar *buf) { return error_result; } assert(cantian_record_buf_size <= m_cantian_rec_len); - + END_RECORD_STATS(EVENT_TYPE_WRITE_ROW) return CT_SUCCESS; } @@ -2839,13 +2882,14 @@ int ctc_cmp_key_values(TABLE *table, const uchar *old_data, const uchar *new_dat */ EXTER_ATTACK int ha_ctc::update_row(const uchar *old_data, uchar *new_data) { DBUG_TRACE; - + BEGIN_RECORD_STATS THD *thd = ha_thd(); m_is_replace = (thd->lex->sql_command == SQLCOM_REPLACE || thd->lex->sql_command == SQLCOM_REPLACE_SELECT) ? true : m_is_replace; if (thd->lex->sql_command == SQLCOM_REPLACE || thd->lex->sql_command == SQLCOM_REPLACE_SELECT) { uint key_nr = table->file->errkey; if (key_nr < MAX_KEY && ctc_cmp_key_values(table, old_data, new_data, key_nr) != 0) { + END_RECORD_STATS(EVENT_TYPE_UPDATE_ROW) return HA_ERR_KEY_NOT_FOUND; } } @@ -2864,12 +2908,14 @@ EXTER_ATTACK int ha_ctc::update_row(const uchar *old_data, uchar *new_data) { } if (upd_fields.size() == 0) { + END_RECORD_STATS(EVENT_TYPE_UPDATE_ROW) return HA_ERR_RECORD_IS_THE_SAME; } if (m_ctc_buf == nullptr) { m_ctc_buf = ctc_alloc_buf(&m_tch, BIG_RECORD_SIZE); if (m_ctc_buf == nullptr) { + END_RECORD_STATS(EVENT_TYPE_UPDATE_ROW) return convert_ctc_error_code_to_mysql(ERR_ALLOC_MEMORY); } } @@ -2879,10 +2925,12 @@ EXTER_ATTACK int ha_ctc::update_row(const uchar *old_data, uchar *new_data) { ct_errno_t ret = (ct_errno_t)mysql_record_to_cantian_record(*table, &record_buf, m_tch, &serial_column_offset, &upd_fields); if (ret != CT_SUCCESS) { + END_RECORD_STATS(EVENT_TYPE_UPDATE_ROW) return ret; } // return if only update gcol if (upd_fields.size() == 0) { + END_RECORD_STATS(EVENT_TYPE_UPDATE_ROW) return 0; } // (m_ignore_dup && m_is_replace) -> special case for load data ... replace into @@ -2894,6 +2942,7 @@ EXTER_ATTACK int ha_ctc::update_row(const uchar *old_data, uchar *new_data) { ret = (ct_errno_t)ctc_update_row(&m_tch, cantian_new_record_buf_size, m_ctc_buf, &upd_fields[0], upd_fields.size(), flag); check_error_code_to_mysql(thd, &ret); + END_RECORD_STATS(EVENT_TYPE_UPDATE_ROW) return convert_ctc_error_code_to_mysql(ret); } @@ -2919,6 +2968,7 @@ EXTER_ATTACK int ha_ctc::update_row(const uchar *old_data, uchar *new_data) { int ha_ctc::delete_row(const uchar *buf) { DBUG_TRACE; + BEGIN_RECORD_STATS UNUSED_PARAM(buf); ha_statistic_increment(&System_status_var::ha_delete_count); THD *thd = ha_thd(); @@ -2931,6 +2981,7 @@ int ha_ctc::delete_row(const uchar *buf) { } ct_errno_t ret = (ct_errno_t)ctc_delete_row(&m_tch, table->s->reclength, flag); check_error_code_to_mysql(thd, &ret); + END_RECORD_STATS(EVENT_TYPE_DELETE_ROW) return convert_ctc_error_code_to_mysql(ret); } @@ -3128,15 +3179,18 @@ int ctc_fill_conds(ctc_handler_t m_tch, const Item *pushed_cond, Field **field, int ha_ctc::rnd_init(bool) { DBUG_TRACE; + BEGIN_RECORD_STATS m_index_sorted = false; THD *thd = ha_thd(); if (engine_ddl_passthru(thd) && (is_alter_table_copy(thd) || is_create_table_check(thd) || is_alter_table_scan(m_error_if_not_empty))) { + END_RECORD_STATS(EVENT_TYPE_RND_INIT) return 0; } ct_errno_t ret = (ct_errno_t)set_prefetch_buffer(); if (ret != CT_SUCCESS) { + END_RECORD_STATS(EVENT_TYPE_RND_INIT) return ret; } expected_cursor_action_t action = EXP_CURSOR_ACTION_SELECT; @@ -3161,6 +3215,7 @@ int ha_ctc::rnd_init(bool) { check_error_code_to_mysql(ha_thd(), &ret); cnvrt_to_mysql_record = cantian_record_to_mysql_record; reset_rec_buf(); + END_RECORD_STATS(EVENT_TYPE_RND_INIT) return convert_ctc_error_code_to_mysql(ret); } @@ -3203,11 +3258,12 @@ int ha_ctc::ctc_alloc_ctc_buf_4_read() { */ int ha_ctc::rnd_next(uchar *buf) { DBUG_TRACE; - + BEGIN_RECORD_STATS if (unlikely(!m_rec_buf || m_rec_buf->records() == 0)) { THD *thd = ha_thd(); if (engine_ddl_passthru(thd) && (is_alter_table_copy(thd) || is_create_table_check(thd) || is_alter_table_scan(m_error_if_not_empty))) { + END_RECORD_STATS(EVENT_TYPE_RND_NEXT) return HA_ERR_END_OF_FILE; } } @@ -3221,6 +3277,7 @@ int ha_ctc::rnd_next(uchar *buf) { record_info_t record_info = {m_read_buf, 0, nullptr, nullptr}; ct_ret = (ct_errno_t)ctc_rnd_next(&m_tch, &record_info); ret = process_cantian_record(buf, &record_info, ct_ret, HA_ERR_END_OF_FILE); + END_RECORD_STATS(EVENT_TYPE_RND_NEXT) return ret; } @@ -3232,6 +3289,7 @@ int ha_ctc::rnd_next(uchar *buf) { cur_pos_in_buf = INVALID_MAX_UINT32; if (m_rec_buf->is_out_of_range()) { set_my_errno(HA_ERR_END_OF_FILE); + END_RECORD_STATS(EVENT_TYPE_RND_NEXT) return HA_ERR_END_OF_FILE; } } else { @@ -3242,6 +3300,7 @@ int ha_ctc::rnd_next(uchar *buf) { if (cur_pos_in_buf % m_rec_buf->max_records() == 0) { fill_record_to_rec_buffer(); } + END_RECORD_STATS(EVENT_TYPE_RND_NEXT) return 0; } } @@ -3249,8 +3308,10 @@ int ha_ctc::rnd_next(uchar *buf) { int mysql_ret = prefetch_and_fill_record_buffer(buf, ctc_rnd_prefetch); if (mysql_ret != 0) { set_my_errno(mysql_ret); + END_RECORD_STATS(EVENT_TYPE_RND_NEXT) return mysql_ret; } + END_RECORD_STATS(EVENT_TYPE_RND_NEXT) return 0; } @@ -3276,12 +3337,15 @@ int ha_ctc::rnd_next(uchar *buf) { filesort.cc, sql_select.cc, sql_delete.cc and sql_update.cc */ void ha_ctc::position(const uchar *) { + BEGIN_RECORD_STATS if (cur_pos_in_buf == INVALID_MAX_UINT32) { ctc_position(&m_tch, ref, ref_length); + END_RECORD_STATS(EVENT_TYPE_POSITION) return; } assert(cur_pos_in_buf < max_prefetch_num); memcpy(ref, &m_rowids[cur_pos_in_buf], ref_length); + END_RECORD_STATS(EVENT_TYPE_POSITION) } /** @@ -3300,6 +3364,7 @@ void ha_ctc::position(const uchar *) { */ EXTER_ATTACK int ha_ctc::rnd_pos(uchar *buf, uchar *pos) { DBUG_TRACE; + BEGIN_RECORD_STATS ha_statistic_increment(&System_status_var::ha_read_rnd_count); int ret = CT_SUCCESS; ct_errno_t ct_ret = CT_SUCCESS; @@ -3311,6 +3376,7 @@ EXTER_ATTACK int ha_ctc::rnd_pos(uchar *buf, uchar *pos) { } ct_ret = (ct_errno_t)ctc_rnd_pos(&m_tch, key_len, pos, &record_info); ret = process_cantian_record(buf, &record_info, ct_ret, HA_ERR_KEY_NOT_FOUND); + END_RECORD_STATS(EVENT_TYPE_RND_POS) return ret; } @@ -3361,8 +3427,10 @@ void ha_ctc::info_low() { int ha_ctc::info(uint flag) { DBUG_TRACE; + BEGIN_RECORD_STATS THD *thd = ha_thd(); if (engine_ddl_passthru(thd) && (is_alter_table_copy(thd) || is_create_table_check(thd))) { + END_RECORD_STATS(EVENT_TYPE_GET_CBO) return 0; } @@ -3371,6 +3439,7 @@ int ha_ctc::info(uint flag) { if (thd->lex->sql_command == SQLCOM_DELETE && thd->lex->query_block->where_cond() == nullptr) { records(&stats.records); + END_RECORD_STATS(EVENT_TYPE_GET_CBO) return 0; } // analyze..update histogram on colname flag @@ -3378,12 +3447,14 @@ int ha_ctc::info(uint flag) { thd->lex->sql_command == SQLCOM_ANALYZE) { ret = (ct_errno_t)analyze(thd, nullptr); if (ret != CT_SUCCESS) { + END_RECORD_STATS(EVENT_TYPE_GET_CBO) return convert_ctc_error_code_to_mysql(ret); } } ret = (ct_errno_t)get_cbo_stats_4share(); if (ret != CT_SUCCESS) { + END_RECORD_STATS(EVENT_TYPE_GET_CBO) return convert_ctc_error_code_to_mysql(ret); } } @@ -3406,24 +3477,27 @@ int ha_ctc::info(uint flag) { } } if (table->file->errkey == UINT_MAX) { + END_RECORD_STATS(EVENT_TYPE_GET_CBO) return HA_ERR_KEY_NOT_FOUND; } } } - + END_RECORD_STATS(EVENT_TYPE_GET_CBO) return convert_ctc_error_code_to_mysql(ret); } int ha_ctc::analyze(THD *thd, HA_CHECK_OPT *) { - + BEGIN_RECORD_STATS if (engine_ddl_passthru(thd)) { if (m_share) { m_share->need_fetch_cbo = true; } + END_RECORD_STATS(EVENT_TYPE_CBO_ANALYZE) return 0; } if (table->s->tmp_table) { + END_RECORD_STATS(EVENT_TYPE_CBO_ANALYZE) return HA_ADMIN_OK; } @@ -3436,6 +3510,7 @@ int ha_ctc::analyze(THD *thd, HA_CHECK_OPT *) { if (ret == CT_SUCCESS && m_share && table_share->m_part_info == nullptr) { m_share->need_fetch_cbo = true; } + END_RECORD_STATS(EVENT_TYPE_CBO_ANALYZE) return convert_ctc_error_code_to_mysql(ret); } @@ -3524,9 +3599,11 @@ int ha_ctc::reset() { int ha_ctc::rnd_end() { DBUG_TRACE; + BEGIN_RECORD_STATS THD *thd = ha_thd(); if (engine_ddl_passthru(thd) && (is_alter_table_copy(thd) || is_create_table_check(thd) || is_alter_table_scan(m_error_if_not_empty))) { + END_RECORD_STATS(EVENT_TYPE_RND_END) return 0; } @@ -3540,11 +3617,13 @@ int ha_ctc::rnd_end() { m_tch.cursor_valid = false; m_tch.cursor_addr = INVALID_VALUE64; + END_RECORD_STATS(EVENT_TYPE_RND_END) return ret; } int ha_ctc::index_init(uint index, bool sorted) { DBUG_TRACE; + BEGIN_RECORD_STATS ct_errno_t ret = (ct_errno_t)set_prefetch_buffer(); if (ret != CT_SUCCESS) { return ret; @@ -3559,12 +3638,13 @@ int ha_ctc::index_init(uint index, bool sorted) { } m_tch.cursor_ref++; m_tch.cursor_valid = false; + END_RECORD_STATS(EVENT_TYPE_INDEX_INIT) return CT_SUCCESS; } int ha_ctc::index_end() { DBUG_TRACE; - + BEGIN_RECORD_STATS active_index = MAX_KEY; int ret = CT_SUCCESS; @@ -3577,6 +3657,7 @@ int ha_ctc::index_end() { if (m_tch.cursor_ref <= 0) { m_tch.cursor_valid = false; } + END_RECORD_STATS(EVENT_TYPE_INDEX_END) return ret; } @@ -3604,6 +3685,7 @@ int ha_ctc::process_cantian_record(uchar *buf, record_info_t *record_info, ct_er EXTER_ATTACK int ha_ctc::index_read(uchar *buf, const uchar *key, uint key_len, ha_rkey_function find_flag) { DBUG_TRACE; + BEGIN_RECORD_STATS ha_statistic_increment(&System_status_var::ha_read_key_count); // reset prefetch buf if calling multiple index_read continuously without index_init as interval @@ -3648,6 +3730,7 @@ EXTER_ATTACK int ha_ctc::index_read(uchar *buf, const uchar *key, uint key_len, int ret = ctc_fill_index_key_info(table, key, key_len, end_range, &index_key_info, index_key_info.index_skip_scan); if (ret != CT_SUCCESS) { ctc_log_error("ha_ctc::index_read: fill index key info failed, ret(%d).", ret); + END_RECORD_STATS(EVENT_TYPE_INDEX_READ) return ret; } @@ -3657,6 +3740,7 @@ EXTER_ATTACK int ha_ctc::index_read(uchar *buf, const uchar *key, uint key_len, ret = ctc_convert_index_datatype(table, &index_key_info, has_right_key, d4); if (ret != CT_SUCCESS) { ctc_log_error("ha_ctc::index_read: convert data type for index search failed, ret(%d).", ret); + END_RECORD_STATS(EVENT_TYPE_INDEX_READ) return ret; } @@ -3677,6 +3761,7 @@ EXTER_ATTACK int ha_ctc::index_read(uchar *buf, const uchar *key, uint key_len, } ret = process_cantian_record(buf, &record_info, ct_ret, HA_ERR_KEY_NOT_FOUND); + END_RECORD_STATS(EVENT_TYPE_INDEX_READ) return ret; } @@ -3686,6 +3771,7 @@ EXTER_ATTACK int ha_ctc::index_read_last(uchar *buf, const uchar *key_ptr, uint int ha_ctc::index_fetch(uchar *buf) { DBUG_TRACE; + BEGIN_RECORD_STATS int mysql_ret = 0; if (!m_rec_buf || m_rec_buf->max_records() == 0) { @@ -3697,6 +3783,7 @@ int ha_ctc::index_fetch(uchar *buf) { ct_ret = (ct_errno_t)ctc_general_fetch(&m_tch, &record_info); attachable_trx_update_pre_addr(ctc_hton, ha_thd(), &m_tch, false); ret = process_cantian_record(buf, &record_info, ct_ret, HA_ERR_END_OF_FILE); + END_RECORD_STATS(EVENT_TYPE_INDEX_FETCH) return ret; } @@ -3708,6 +3795,7 @@ int ha_ctc::index_fetch(uchar *buf) { cur_pos_in_buf = INVALID_MAX_UINT32; if (m_rec_buf->is_out_of_range()) { set_my_errno(HA_ERR_END_OF_FILE); + END_RECORD_STATS(EVENT_TYPE_INDEX_FETCH) return HA_ERR_END_OF_FILE; } } else { @@ -3718,6 +3806,7 @@ int ha_ctc::index_fetch(uchar *buf) { if (cur_pos_in_buf % m_rec_buf->max_records() == 0) { fill_record_to_rec_buffer(); } + END_RECORD_STATS(EVENT_TYPE_INDEX_FETCH) return CT_SUCCESS; } } @@ -3728,8 +3817,10 @@ int ha_ctc::index_fetch(uchar *buf) { if (mysql_ret != 0) { set_my_errno(mysql_ret); + END_RECORD_STATS(EVENT_TYPE_INDEX_FETCH) return mysql_ret; } + END_RECORD_STATS(EVENT_TYPE_INDEX_FETCH) return CT_SUCCESS; } @@ -3823,6 +3914,7 @@ int ha_ctc::index_last(uchar *buf) { */ int ha_ctc::delete_all_rows() { DBUG_TRACE; + BEGIN_RECORD_STATS THD *thd = ha_thd(); update_member_tch(m_tch, ctc_hton, thd); dml_flag_t flag = ctc_get_dml_flag(thd, false, false, false, false); @@ -3833,8 +3925,10 @@ int ha_ctc::delete_all_rows() { update_sess_ctx_by_tch(m_tch, ctc_hton, thd); check_error_code_to_mysql(thd, &ret); if (thd->lex->is_ignore() && ret == ERR_ROW_IS_REFERENCED) { + END_RECORD_STATS(EVENT_TYPE_DELETE_ALL_ROWS) return 0; } + END_RECORD_STATS(EVENT_TYPE_DELETE_ALL_ROWS) return convert_ctc_error_code_to_mysql(ret); } @@ -4016,6 +4110,7 @@ void ha_ctc::set_ctc_range_key(ctc_key *ctc_key, key_range *mysql_range_key, boo */ ha_rows ha_ctc::records_in_range(uint inx, key_range *min_key, key_range *max_key) { + BEGIN_RECORD_STATS DBUG_TRACE; ctc_key ctc_min_key; ctc_key ctc_max_key; @@ -4034,6 +4129,7 @@ ha_rows ha_ctc::records_in_range(uint inx, key_range *min_key, if (m_share) { if (!m_share->cbo_stats->is_updated) { ctc_log_debug("table %s has not been analyzed", table->alias); + END_RECORD_STATS(EVENT_TYPE_CBO_RECORDS_IN_RANGE) return 1; } density = calc_density_one_table(inx, &key, m_share->cbo_stats->ctc_cbo_stats_table, *table); @@ -4052,6 +4148,7 @@ ha_rows ha_ctc::records_in_range(uint inx, key_range *min_key, if (n_rows == 0) { n_rows = 1; } + END_RECORD_STATS(EVENT_TYPE_CBO_RECORDS_IN_RANGE) return n_rows; } @@ -4084,9 +4181,11 @@ int ha_ctc::records(ha_rows *num_rows) /*!< out: number of rows */ int ha_ctc::records_from_index(ha_rows *num_rows, uint inx) { + BEGIN_RECORD_STATS active_index = inx; int ret = records(num_rows); active_index = MAX_KEY; + END_RECORD_STATS(EVENT_TYPE_CBO_RECORDS) return ret; } @@ -4217,7 +4316,7 @@ int ha_ctc::external_lock(THD *thd, int lock_type) { out they lock meaning. */ DBUG_TRACE; - + BEGIN_RECORD_STATS if (IS_METADATA_NORMALIZATION() && ctc_check_if_log_table(table_share->db.str, table_share->table_name.str)) { is_log_table = true; @@ -4226,22 +4325,27 @@ int ha_ctc::external_lock(THD *thd, int lock_type) { sess_ctx->sql_stat_start = 1; m_tch.sql_stat_start = 1; } + END_RECORD_STATS(EVENT_TYPE_BEGIN_TRX) return 0; } is_log_table = false; if (engine_ddl_passthru(thd) && (is_create_table_check(thd) || is_alter_table_copy(thd))) { + END_RECORD_STATS(EVENT_TYPE_BEGIN_TRX) return 0; } // F_RDLCK:0, F_WRLCK:1, F_UNLCK:2 if (lock_type == F_UNLCK) { m_select_lock = lock_mode::NO_LOCK; + END_RECORD_STATS(EVENT_TYPE_BEGIN_TRX) return 0; } - return start_stmt(thd, TL_IGNORE); + int ret = start_stmt(thd, TL_IGNORE); + END_RECORD_STATS(EVENT_TYPE_BEGIN_TRX) + return ret; } /** @@ -4332,16 +4436,19 @@ static bool ctc_get_tablespace_statistics( } EXTER_ATTACK bool ctc_drop_database_with_err(handlerton *hton, char *path) { + BEGIN_RECORD_STATS THD *thd = current_thd; assert(thd != nullptr); if (engine_ddl_passthru(thd)) { + END_RECORD_STATS(EVENT_TYPE_DROP_DB) return false; } ctc_handler_t tch; int res = get_tch_in_handler_data(hton, thd, tch); if (res != CT_SUCCESS) { + END_RECORD_STATS(EVENT_TYPE_DROP_DB) return true; } @@ -4362,6 +4469,7 @@ EXTER_ATTACK bool ctc_drop_database_with_err(handlerton *hton, char *path) { ctc_log_error("drop database failed with error code: %d", convert_ctc_error_code_to_mysql((ct_errno_t)ret)); cm_assert(0); } + END_RECORD_STATS(EVENT_TYPE_DROP_DB) return false; } @@ -4944,6 +5052,7 @@ void ha_ctc::update_create_info(HA_CREATE_INFO *create_info) { delete_table and ha_create_table() in handler.cc */ EXTER_ATTACK int ha_ctc::delete_table(const char *full_path_name, const dd::Table *table_def) { + BEGIN_RECORD_STATS THD *thd = ha_thd(); ct_errno_t ret = CT_SUCCESS; @@ -4953,11 +5062,13 @@ EXTER_ATTACK int ha_ctc::delete_table(const char *full_path_name, const dd::Tabl close_all_tables_for_name(thd, parent_fk->child_schema_name().c_str(), parent_fk->child_table_name().c_str(), true); } } + END_RECORD_STATS(EVENT_TYPE_DROP_TABLE) return ret; } /* 删除db时 会直接删除参天用户 所有表也会直接被删除 无需再次下发 */ if (thd->lex->sql_command == SQLCOM_DROP_DB) { + END_RECORD_STATS(EVENT_TYPE_DROP_TABLE) return ret; } @@ -4971,10 +5082,12 @@ EXTER_ATTACK int ha_ctc::delete_table(const char *full_path_name, const dd::Tabl ctc_ddl_stack_mem stack_mem(0); int mysql_ret = fill_delete_table_req(full_path_name, table_def, thd, &ddl_ctrl, &stack_mem); if (mysql_ret != CT_SUCCESS) { + END_RECORD_STATS(EVENT_TYPE_DROP_TABLE) return mysql_ret; } void *ctc_ddl_req_msg_mem = stack_mem.get_buf(); if (ctc_ddl_req_msg_mem == nullptr) { + END_RECORD_STATS(EVENT_TYPE_DROP_TABLE) return HA_ERR_OUT_OF_MEM; } ctc_log_note("ctc_drop_table enter"); @@ -4983,6 +5096,7 @@ EXTER_ATTACK int ha_ctc::delete_table(const char *full_path_name, const dd::Tabl ctc_ddl_hook_cantian_error("ctc_drop_table_cantian_error", thd, &ddl_ctrl, &ret); m_tch = ddl_ctrl.tch; update_sess_ctx_by_tch(m_tch, ctc_hton, thd); + END_RECORD_STATS(EVENT_TYPE_DROP_TABLE) return ctc_ddl_handle_fault("ctc_drop_table", thd, &ddl_ctrl, ret, full_path_name, HA_ERR_WRONG_TABLE_NAME); } static map> @@ -5080,10 +5194,12 @@ int ctc_ddl_handle_fault(const char *tag, const THD *thd, */ EXTER_ATTACK int ha_ctc::create(const char *name, TABLE *form, HA_CREATE_INFO *create_info, dd::Table *table_def) { + BEGIN_RECORD_STATS THD *thd = ha_thd(); ct_errno_t ret = CT_SUCCESS; if (check_unsupported_operation(thd, create_info)) { ctc_log_system("Unsupported operation. sql = %s", thd->query().str); + END_RECORD_STATS(EVENT_TYPE_CREATE_TABLE) return HA_ERR_WRONG_COMMAND; } @@ -5095,6 +5211,7 @@ EXTER_ATTACK int ha_ctc::create(const char *name, TABLE *form, HA_CREATE_INFO *c && thd->lex->alter_info) { if (is_tmp_table) { ctc_log_system("Unsupported operation. sql = %s", thd->query().str); + END_RECORD_STATS(EVENT_TYPE_CREATE_TABLE) return HA_ERR_NOT_ALLOWED_COMMAND; } // do not move this under engine_ddl_passthru(thd) function @@ -5102,6 +5219,7 @@ EXTER_ATTACK int ha_ctc::create(const char *name, TABLE *form, HA_CREATE_INFO *c } if (engine_skip_ddl(thd) || engine_ddl_passthru(thd)) { + END_RECORD_STATS(EVENT_TYPE_CREATE_TABLE) return ret; } @@ -5118,10 +5236,13 @@ EXTER_ATTACK int ha_ctc::create(const char *name, TABLE *form, HA_CREATE_INFO *c } if (thd->lex->sql_command == SQLCOM_TRUNCATE) { - return ha_ctc_truncate_table(&m_tch, thd, db_name, table_name, is_tmp_table); + int ret_status = ha_ctc_truncate_table(&m_tch, thd, db_name, table_name, is_tmp_table); + END_RECORD_STATS(EVENT_TYPE_CREATE_TABLE) + return ret_status; } if (get_cantian_record_length(form) > CT_MAX_RECORD_LENGTH) { + END_RECORD_STATS(EVENT_TYPE_CREATE_TABLE) return HA_ERR_TOO_BIG_ROW; } @@ -5144,11 +5265,13 @@ EXTER_ATTACK int ha_ctc::create(const char *name, TABLE *form, HA_CREATE_INFO *c } ret = (ct_errno_t)fill_create_table_req(create_info, table_def, db_name, table_name, form, thd, &ddl_ctrl, &stack_mem); if (ret != CT_SUCCESS) { + END_RECORD_STATS(EVENT_TYPE_CREATE_TABLE) return ret; } void *ctc_ddl_req_msg_mem = stack_mem.get_buf(); if (ctc_ddl_req_msg_mem == nullptr) { + END_RECORD_STATS(EVENT_TYPE_CREATE_TABLE) return HA_ERR_OUT_OF_MEM; } @@ -5161,12 +5284,14 @@ EXTER_ATTACK int ha_ctc::create(const char *name, TABLE *form, HA_CREATE_INFO *c // func_name非空的情况对应default function my_error(ER_DEFAULT_VAL_GENERATED_NAMED_FUNCTION_IS_NOT_ALLOWED, MYF(0), field_name, func_name); + END_RECORD_STATS(EVENT_TYPE_CREATE_TABLE) return CT_ERROR; } } ctc_ddl_hook_cantian_error("ctc_create_table_cantian_error", thd, &ddl_ctrl, &ret); m_tch = ddl_ctrl.tch; update_sess_ctx_by_tch(m_tch, ctc_hton, thd); + END_RECORD_STATS(EVENT_TYPE_CREATE_TABLE) return ctc_ddl_handle_fault("ctc_create_table", thd, &ddl_ctrl, ret); } @@ -5191,10 +5316,12 @@ bool ha_ctc::inplace_alter_table(TABLE *altered_table, const dd::Table *old_table_def, dd::Table *new_table_def) { + BEGIN_RECORD_STATS if (old_table_def == nullptr || new_table_def == nullptr) { ctc_log_error( "inplace_alter_table old_table_def:%p, or new_table_def:%p is NULL", old_table_def, new_table_def); + END_RECORD_STATS(EVENT_TYPE_INPLACE_ALTER_TABLE) return true; } @@ -5204,16 +5331,19 @@ bool ha_ctc::inplace_alter_table(TABLE *altered_table, if (check_unsupported_operation(thd, nullptr)) { ctc_log_system("Unsupported operation. sql = %s", thd->query().str); + END_RECORD_STATS(EVENT_TYPE_INPLACE_ALTER_TABLE) return true; } if (get_cantian_record_length(altered_table) > CT_MAX_RECORD_LENGTH) { + END_RECORD_STATS(EVENT_TYPE_INPLACE_ALTER_TABLE) return true; } /* Nothing to commit/rollback, mark all handlers committed! */ ha_alter_info->group_commit_ctx = nullptr; if (engine_ddl_passthru(thd)) { + END_RECORD_STATS(EVENT_TYPE_INPLACE_ALTER_TABLE) return false; } @@ -5229,11 +5359,13 @@ bool ha_ctc::inplace_alter_table(TABLE *altered_table, &ddl_ctrl, &stack_mem); } if (ret != CT_SUCCESS) { + END_RECORD_STATS(EVENT_TYPE_INPLACE_ALTER_TABLE) return true; } void *ctc_ddl_req_msg_mem = stack_mem.get_buf(); if (ctc_ddl_req_msg_mem == nullptr) { + END_RECORD_STATS(EVENT_TYPE_INPLACE_ALTER_TABLE) return true; } ctc_register_trx(ht, thd); @@ -5245,9 +5377,11 @@ bool ha_ctc::inplace_alter_table(TABLE *altered_table, // 这个地方alter table需要特殊处理返回值 if (ret != CT_SUCCESS) { ctc_alter_table_handle_fault(ret); + END_RECORD_STATS(EVENT_TYPE_INPLACE_ALTER_TABLE) return true; } + END_RECORD_STATS(EVENT_TYPE_INPLACE_ALTER_TABLE) return false; } @@ -5269,15 +5403,18 @@ bool ha_ctc::inplace_alter_table(TABLE *altered_table, EXTER_ATTACK int ha_ctc::rename_table(const char *from, const char *to, const dd::Table *from_table_def, dd::Table *to_table_def) { + BEGIN_RECORD_STATS THD *thd = ha_thd(); ct_errno_t ret = CT_SUCCESS; if (engine_ddl_passthru(thd)) { + END_RECORD_STATS(EVENT_TYPE_RENAME_TABLE) return false; } if (is_dd_table_id(to_table_def->se_private_id())) { my_error(ER_NOT_ALLOWED_COMMAND, MYF(0)); + END_RECORD_STATS(EVENT_TYPE_RENAME_TABLE) return HA_ERR_UNSUPPORTED; } @@ -5291,11 +5428,13 @@ EXTER_ATTACK int ha_ctc::rename_table(const char *from, const char *to, ret = (ct_errno_t)fill_rename_table_req(from, to, from_table_def, to_table_def, thd, &ddl_ctrl, &stack_mem); if (ret != CT_SUCCESS) { + END_RECORD_STATS(EVENT_TYPE_RENAME_TABLE) return ret; } void *ctc_ddl_req_msg_mem = stack_mem.get_buf(); if(ctc_ddl_req_msg_mem == nullptr) { + END_RECORD_STATS(EVENT_TYPE_RENAME_TABLE) return HA_ERR_OUT_OF_MEM; } ctc_register_trx(ht, thd); @@ -5303,6 +5442,7 @@ EXTER_ATTACK int ha_ctc::rename_table(const char *from, const char *to, ctc_ddl_hook_cantian_error("ctc_rename_table_cantian_error", thd, &ddl_ctrl, &ret); m_tch = ddl_ctrl.tch; update_sess_ctx_by_tch(m_tch, ctc_hton, thd); + END_RECORD_STATS(EVENT_TYPE_RENAME_TABLE) return ctc_ddl_handle_fault("ctc_rename_table", thd, &ddl_ctrl, ret, to); } @@ -5417,9 +5557,11 @@ int ha_ctc::initialize_cbo_stats() if (!m_share || m_share->cbo_stats != nullptr) { return CT_SUCCESS; } + BEGIN_RECORD_STATS m_share->cbo_stats = (ctc_cbo_stats_t*)my_malloc(PSI_NOT_INSTRUMENTED, sizeof(ctc_cbo_stats_t), MYF(MY_WME)); if (m_share->cbo_stats == nullptr) { ctc_log_error("alloc mem failed, m_share->cbo_stats size(%lu)", sizeof(ctc_cbo_stats_t)); + END_RECORD_STATS(EVENT_TYPE_INITIALIZE_DBO) return ERR_ALLOC_MEMORY; } *m_share->cbo_stats = {0, 0, 0, 0, 0, nullptr, 0, nullptr, nullptr}; @@ -5427,12 +5569,14 @@ int ha_ctc::initialize_cbo_stats() (ctc_cbo_stats_table_t*)my_malloc(PSI_NOT_INSTRUMENTED, sizeof(ctc_cbo_stats_table_t), MYF(MY_WME)); if (m_share->cbo_stats->ctc_cbo_stats_table == nullptr) { ctc_log_error("alloc mem failed, m_share->cbo_stats->ctc_cbo_stats_table(%lu)", sizeof(ctc_cbo_stats_table_t)); + END_RECORD_STATS(EVENT_TYPE_INITIALIZE_DBO) return ERR_ALLOC_MEMORY; } 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) { ctc_log_error("alloc mem failed, m_share->cbo_stats->ctc_cbo_stats_table->columns size(%lu)", table->s->fields * sizeof(ctc_cbo_stats_column_t)); + END_RECORD_STATS(EVENT_TYPE_INITIALIZE_DBO) return ERR_ALLOC_MEMORY; } for (uint col_id = 0; col_id < table->s->fields; col_id++) { @@ -5448,11 +5592,13 @@ int ha_ctc::initialize_cbo_stats() (uint32_t*)my_malloc(PSI_NOT_INSTRUMENTED, table->s->keys * sizeof(uint32_t) * MAX_KEY_COLUMNS, MYF(MY_WME)); if (m_share->cbo_stats->ndv_keys == nullptr) { ctc_log_error("alloc mem failed, m_share->cbo_stats->ndv_keys size(%lu)", table->s->keys * sizeof(uint32_t) * MAX_KEY_COLUMNS); + END_RECORD_STATS(EVENT_TYPE_INITIALIZE_DBO) return ERR_ALLOC_MEMORY; } 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; + END_RECORD_STATS(EVENT_TYPE_INITIALIZE_DBO) return CT_SUCCESS; } @@ -5518,7 +5664,7 @@ void ha_ctc::free_cbo_stats() if (!m_share || m_share->cbo_stats == nullptr) { return; } - + BEGIN_RECORD_STATS my_free((m_share->cbo_stats->ndv_keys)); m_share->cbo_stats->ndv_keys = nullptr; my_free((m_share->cbo_stats->col_type)); @@ -5531,7 +5677,7 @@ void ha_ctc::free_cbo_stats() m_share->cbo_stats->ctc_cbo_stats_table = nullptr; my_free((uchar *)(m_share->cbo_stats)); m_share->cbo_stats = nullptr; - + END_RECORD_STATS(EVENT_TYPE_FREE_CBO) } /**