From bee90c0a0726ef6e057f346d1f3306fd1239a7f9 Mon Sep 17 00:00:00 2001 From: linusluo Date: Sun, 7 Jun 2026 15:42:44 +0800 Subject: [PATCH 01/14] [bugfix] issue#399 auto-inc tables using parallel copy ddl have duplicate key error. Description =========== Scenario 1: Copy ddl operations are performed with insert operations concurrently, insert operations will report duplicate key error. Scenario 2: Change the primary key column from a non-auto-inc to an autoinc, then insert a new column, duplicate key error will occur. More testcases can be found in parallel_copy_alter_table.test Solution ======== Using ha_write_row instead of row_insert_for_mysql since in ha_write_row, auto increment value will be updated. (cherry picked from commit a8069d7db17b16be31b148eb7c93f338c7875d26) --- mysql-test/r/parallel_copy_alter_table.result | Bin 13932 -> 17366 bytes mysql-test/t/parallel_copy_alter_table.test | 99 ++++++++++ sql/handler.h | 9 - sql/sql_table.cc | 26 ++- storage/innobase/handler/ha_innodb.h | 2 - storage/innobase/handler/handler0alter.cc | 183 ++++++++---------- storage/innobase/include/trx0trx.h | 4 +- 7 files changed, 196 insertions(+), 127 deletions(-) diff --git a/mysql-test/r/parallel_copy_alter_table.result b/mysql-test/r/parallel_copy_alter_table.result index 15bbca0a657b335239ed52e2272abcda52f398a1..e3c6b6e560f58b4eb7d6cf8a1ed483cdca4ad7af 100644 GIT binary patch delta 2401 zcmb7F&2HO95Jp>|fO&8(1=^xH4B~^92&0OWQ#%zJlonZ;Fh#l))yTP2D~hy~P?XCb zM*#}(*i(Q2dXWNsgFZlyJrpSV0)2=Mm!w3>HkuqH?#}Ma&d)dB{OkYu%Ll){X{XZT zui?OUqZlsTzzu2aPQh9F$s*vomMYsS*z$?4LJWk$Jcx-X@&ZgYOU@~m&{GZ7LJb9E z{h53*0C|8)4<|hQpjq}%)&*|5d-;pIZ}V&M-skK4`~CI3{pZE+QX*8y49faY9S~8F z_zoxQwrXwMR4nrxrUJ-{0xn$BaQ*k=PgAyKujeNoI5Qd&vBfhl8Mr@`RN>R^Q0ZayQGoX!XDg=bT-8 zSgx(#HMeO*8qDj`;+OAe5vUitA$X-8xm9nng-M~{lnNg!<6y0u83HjA!UWj@5{BLKm?MF`fAMFBWLUlFoz34dV=V z%S?Vv{ltwvA|k*3t94%_49OMjQ~wox9YJ0RkuM(=@~rZmb#w2tR62T(rqK*LYZ&%% zvd?Xe!umvj86w-lYFQaF1$dEbEtu9;4bfaS82bMZUf|WBu6ecDN(k{=I+Juwqbu7a zz(;x1*9=7+!F;+BiBsK5Ko-?~n_TfJrd2Zrs%2-6naB-gKZ*iKk^qYOojlYr><6+e zKM>%!DjJPOC@QYpX$cK&E}@+yNl@uYE=qITO!Pl?+5#Nb>@1f{G=zR`eM}aMnnTpj zz+E5-C^kQ=GW-Z^y;?zvn+jyM#aLlDcOd(?L=2KfaOoDf1q3Bc;^m`xfPH%l?T%w< z41^og@rF4Kb7X@t)R9idlX1>thkO}NdE$-1^OrQ5>C{^PNJal3^ESds*brLHXQnZqz3kVCkVXFhXAy~4Z5Pm1$QiV#MlnLL-FC@W~ z@kZbzVdw_VYgl@qC^{n*rLUKB@7yxGs*(;Z9DbQ6(G~WBW 0) DO +ALTER TABLE test.t1 ADD n2 BIGINT; +ALTER TABLE test.t1 DROP COLUMN n2; +SET @idx = @idx - 1; +END WHILE; +END| + +#Procedure for INSERT +CREATE PROCEDURE test.sp1 (IN start BIGINT) +BEGIN +SET @idx =start; +WHILE (@idx > 0) DO +INSERT INTO test.t1 (c2,c3) VALUES(repeat('q',10),@idx); +SET @idx = @idx - 1; +END WHILE; +END| + +delimiter ;| + +connect (con1,localhost,root,,); + +connection default; +SET SESSION txsql_parallel_copy_ddl=ON; +CREATE TABLE t1 (c1 INT AUTO_INCREMENT PRIMARY KEY, c2 TEXT(1024), c3 INT); +--send call test.sp2(30); + +connection con1; +--send call test.sp1(300); +--reap + +connection default; +--reap + +CHECK TABLE t1; +DROP TABLE t1; +DROP PROCEDURE test.sp1; +DROP PROCEDURE test.sp2; + +SET SESSION txsql_parallel_copy_ddl=ON; + --echo --echo --echo #Case : test parallel copy ddl with a large amount of data diff --git a/sql/handler.h b/sql/handler.h index 5cb902537b7..3c19f0a4bfc 100644 --- a/sql/handler.h +++ b/sql/handler.h @@ -6284,10 +6284,6 @@ class handler { ha_copy_alter_info, create, found); } - void ha_prepare_copy_alter(Alter_copy_info *ha_copy_alter_info) { - return prepare_copy_alter(ha_copy_alter_info); - } - /** Public function wrapping the actual handler call. Allows us to enforce asserts regardless of handler implementation. @@ -6430,11 +6426,6 @@ class handler { return 0; } - virtual void prepare_copy_alter( - Alter_copy_info *ha_copy_alter_info [[maybe_unused]]) { - return; - } - /** Commit or rollback the changes made during prepare_inplace_alter_table() and inplace_alter_table() inside the storage engine. diff --git a/sql/sql_table.cc b/sql/sql_table.cc index 47da062ac92..f80a4acbadf 100644 --- a/sql/sql_table.cc +++ b/sql/sql_table.cc @@ -18921,13 +18921,6 @@ static int copy_data_between_tables( if (check_if_can_use_parallel_copy_ddl(thd, from, to, alter_ctx, order, is_auto_inc, is_multi_value, table_def, old_table_def)) { - - to->file->ha_prepare_copy_alter(&ha_copy_alter_info); - - ha_copy_alter_info.handler_ctx->init(alter_ctx->new_db, alter_ctx->db, - alter_ctx->table_name, alter_ctx->tmp_name, - alter_ctx->get_path(), - alter_ctx->get_tmp_path()); if (!(error = iterator->Read())) { /* Return error if source table isn't empty. @@ -18944,14 +18937,19 @@ static int copy_data_between_tables( error = 1; goto err; } + ha_copy_alter_info.handler_ctx = new copy_alter_handler_ctx(); + ha_copy_alter_info.handler_ctx->init(alter_ctx->new_db, alter_ctx->db, + alter_ctx->table_name, alter_ctx->tmp_name, + alter_ctx->get_path(), + alter_ctx->get_tmp_path()); + + error = from->file->ha_parallel_copy_data_between_tables(from, to, table_def, + old_table_def, + &ha_copy_alter_info, + create, found_count); + mysql_stage_set_work_completed(psi, found_count); + thd->get_stmt_da()->set_current_row_for_condition(found_count); } - - error = from->file->ha_parallel_copy_data_between_tables(from, to, table_def, - old_table_def, - &ha_copy_alter_info, - create, found_count); - mysql_stage_set_work_completed(psi, found_count); - thd->get_stmt_da()->set_current_row_for_condition(found_count); } else { while (!(error = iterator->Read())) { diff --git a/storage/innobase/handler/ha_innodb.h b/storage/innobase/handler/ha_innodb.h index 8fb93dd7c96..950511d9b9e 100644 --- a/storage/innobase/handler/ha_innodb.h +++ b/storage/innobase/handler/ha_innodb.h @@ -470,8 +470,6 @@ class ha_innobase : public handler { Alter_copy_info *ha_copy_alter_info, List &create, ulong &found) override; - void prepare_copy_alter(Alter_copy_info *ha_copy_alter_info) override; - /** Commit or rollback the changes made during prepare_inplace_alter_table() and inplace_alter_table() inside the storage engine. Note that the allowed level of concurrency diff --git a/storage/innobase/handler/handler0alter.cc b/storage/innobase/handler/handler0alter.cc index 45000665368..68059cd59af 100644 --- a/storage/innobase/handler/handler0alter.cc +++ b/storage/innobase/handler/handler0alter.cc @@ -344,8 +344,6 @@ struct ha_innobase_copy_ctx : public copy_alter_handler_ctx { struct ha_innobase_copy_ctx_t { mem_heap_t * copy_heap; - row_prebuilt_t * copy_prebuilt; - row_prebuilt_t * from_prebuilt; THD * copy_thd; @@ -356,12 +354,10 @@ struct ha_innobase_copy_ctx_t { TABLE * from_table, * to_table; - void init(dict_table_t * new_inno_table, dict_table_t *old_inno_table, - TABLE *from, TABLE *to, - row_prebuilt_t * prebuilt, row_prebuilt_t * old_prebuilt, - THD * thd, dd::Table *new_dd_tab, const dd::Table *old_dd_tab, - ha_innobase_copy_ctx *copy_ctx, List create) { - + void init(dict_table_t *old_inno_table, TABLE *from, TABLE *to, + row_prebuilt_t *old_prebuilt, THD *thd, dd::Table *new_dd_tab, + const dd::Table *old_dd_tab, copy_alter_handler_ctx *copy_ctx, + List create, bool & auto_increment_field_copied) { copy_heap = mem_heap_create(1024, UT_LOCATION_HERE); from_table = open_table_uncached(thd, copy_ctx->get_path(), copy_ctx->db, @@ -380,18 +376,6 @@ struct ha_innobase_copy_ctx_t { to_table->column_bitmaps_set_no_signal(&to_table->s->all_set, &to_table->s->all_set); - copy_prebuilt = row_create_prebuilt(new_inno_table, to->s->reclength); - - copy_prebuilt->default_rec = to->s->default_values; - ut_ad(copy_prebuilt->default_rec); - - /* Use same trx as original prebuilt. */ - copy_prebuilt->trx = prebuilt->trx; - - copy_prebuilt->m_mysql_table = to; - - build_copy_template(copy_prebuilt, true, to_table); - from_prebuilt = row_create_prebuilt(old_inno_table, from->s->reclength); from_prebuilt->default_rec = from->s->default_values; ut_ad(from_prebuilt->default_rec); @@ -402,6 +386,8 @@ struct ha_innobase_copy_ctx_t { build_copy_template(from_prebuilt, true, from_table); + to_table->next_number_field = to_table->found_next_number_field; + copy_thd = thd; /* build copy_field information */ @@ -442,12 +428,17 @@ struct ha_innobase_copy_ctx_t { continue; } if (def->field) { - const char * cmp_name = (*ptr)->field_name; + if (*ptr == to_table->next_number_field) { + auto_increment_field_copied = true; + } + + const char *cmp_name = (*ptr)->field_name; if (def->change) { cmp_name = def->change; } for (Field **from_it = from_table->field; *from_it; from_it++) { - if (my_strcasecmp(system_charset_info, (*from_it)->field_name, cmp_name) == 0) { + if (my_strcasecmp(system_charset_info, (*from_it)->field_name, + cmp_name) == 0) { (copy_end++)->set(*ptr, *from_it); break; } @@ -471,8 +462,6 @@ struct ha_innobase_copy_ctx_t { mem_heap_free(copy_heap); /* Do not close the table. */ - copy_prebuilt->table = nullptr; - row_prebuilt_free(copy_prebuilt, false); from_prebuilt->table = nullptr; row_prebuilt_free(from_prebuilt, false); @@ -1810,87 +1799,69 @@ void ha_innobase::parallel_scan_end(void *parallel_scan_ctx) { ut::delete_(parallel_reader); } -void ha_innobase::prepare_copy_alter(Alter_copy_info *ha_copy_alter_info) { - ha_copy_alter_info->handler_ctx = new (m_user_thd->mem_root) - ha_innobase_copy_ctx(m_prebuilt); - ha_innobase_copy_ctx *ctx [[maybe_unused]] = - static_cast(ha_copy_alter_info->handler_ctx); - assert(ctx); - if (m_prebuilt->mysql_template == nullptr) { - build_template(true); - } - row_get_prebuilt_insert_row(m_prebuilt); -} - /* @return error status (zero on success, HA_ERR_* error code on error) */ int ha_innobase::parallel_copy_data_between_tables( - TABLE *from, TABLE *to, dd::Table *new_dd_tab, - const dd::Table *old_dd_tab, - Alter_copy_info *ha_copy_alter_info, - List &create, ulong &found) { + TABLE *from, TABLE *to, dd::Table *new_dd_tab, const dd::Table *old_dd_tab, + Alter_copy_info *ha_copy_alter_info, List &create, + ulong &found) { dberr_t err = DB_SUCCESS; if (dict_table_is_discarded(m_prebuilt->table)) { return (HA_ERR_NO_SUCH_TABLE); } size_t num_threads; - THD * thd = current_thd; - mem_heap_t * heap = mem_heap_create(1024, UT_LOCATION_HERE); + THD *thd = current_thd; + mem_heap_t *heap = mem_heap_create(1024, UT_LOCATION_HERE); dict_index_t *clust_index = m_prebuilt->table->first_index(); + int constraint_err = 0; ha_innobase_copy_ctx *ctx = static_cast(ha_copy_alter_info->handler_ctx); - row_prebuilt_t * new_prebuilt = ctx->prebuilt; - auto trx = m_prebuilt->trx; - - innobase_register_trx(ht, ha_thd(), trx); - - trx_start_if_not_started_xa(trx, false, UT_LOCATION_HERE); - + trx_start_if_not_started_xa(trx, true, UT_LOCATION_HERE); trx_assign_read_view(trx); if (trx_is_interrupted(trx)) { mem_heap_free(heap); - ib::error(ER_IB_MSG_1004) << "Trx is interrupted, parallel copy data is stopped."; - return (true); + ib::error(ER_IB_MSG_1004) + << "Trx is interrupted, parallel copy data is stopped."; + return (DB_INTERRUPTED); } trx->op_info = "parallel copying data between tables"; - // all threads one trx and one que_thr_t. - // que_thr_t *thr = pars_complete_graph_for_exec(nullptr, trx, heap, m_prebuilt); - num_threads = thd_parallel_read_threads(m_prebuilt->trx->mysql_thd); - num_threads = - Parallel_reader::available_threads(num_threads, false); + num_threads = Parallel_reader::available_threads(num_threads, false); if (num_threads == 0) { mem_heap_free(heap); ib::error(ER_IB_MSG_1004) << "No enough parallel reader threads."; - return (true); + return (DB_INTERRUPTED); } - sql_print_information("[TXSQL Parallel Copy DDL] parallel_threads=%u, "\ - "sql=%s.", num_threads, thd->query().str); + sql_print_information( + "[TXSQL Parallel Copy DDL] parallel_threads=%u, " + "sql=%s.", + num_threads, thd->query().str); + dict_table_t *old_inno_table = dd_table_open_on_id( + old_dd_tab->se_private_id(), thd, nullptr, false, true); - dict_table_t * new_inno_table = dd_table_open_on_id(new_dd_tab->se_private_id(), - thd, nullptr, false, true); - dict_table_t * old_inno_table = dd_table_open_on_id(old_dd_tab->se_private_id(), - thd, nullptr, false, true); - ha_innobase_copy_ctx_t ** ctx_array = ut::new_arr_withkey( - UT_NEW_THIS_FILE_PSI_KEY, ut::Count{num_threads}); + bool auto_increment_field_copied = 0; + + ha_innobase_copy_ctx_t **ctx_array = + ut::new_arr_withkey(UT_NEW_THIS_FILE_PSI_KEY, + ut::Count{num_threads}); for (size_t i = 0; i < num_threads; i++) { - ctx_array[i] = ut::new_withkey(UT_NEW_THIS_FILE_PSI_KEY); - ctx_array[i]->init(new_inno_table, old_inno_table, - from, to, new_prebuilt, m_prebuilt, - thd, new_dd_tab, old_dd_tab, ctx, create); + ctx_array[i] = + ut::new_withkey(UT_NEW_THIS_FILE_PSI_KEY); + ctx_array[i]->init(old_inno_table, from, to, m_prebuilt, thd, new_dd_tab, + old_dd_tab, ctx, create, auto_increment_field_copied); } Parallel_reader::Scan_range full_scan; @@ -1900,9 +1871,8 @@ int ha_innobase::parallel_copy_data_between_tables( std::atomic global_err{DB_SUCCESS}; auto process_row = [&](const Parallel_reader::Ctx *ctx) { - dberr_t error = DB_SUCCESS; + int error; const rec_t *mrec = ctx->m_rec; - // row_ext_t *ext = nullptr; const size_t thread_id = ctx->thread_id(); char my_stack; thd->thread_stack = &my_stack; // remember where our stack is @@ -1916,9 +1886,8 @@ int ha_innobase::parallel_copy_data_between_tables( current_thd = thd; - mem_heap_t * row_heap = ctx_array[thread_id]->copy_heap; - row_prebuilt_t * from_prebuilt = ctx_array[thread_id]->from_prebuilt; - row_prebuilt_t * to_prebuilt = ctx_array[thread_id]->copy_prebuilt; + mem_heap_t *row_heap = ctx_array[thread_id]->copy_heap; + row_prebuilt_t *from_prebuilt = ctx_array[thread_id]->from_prebuilt; Copy_field *copy = ctx_array[thread_id]->copy; Copy_field *copy_end = ctx_array[thread_id]->copy_end; @@ -1926,8 +1895,8 @@ int ha_innobase::parallel_copy_data_between_tables( Field **gen_fields = ctx_array[thread_id]->gen_fields; Field **gen_fields_end = ctx_array[thread_id]->gen_fields_end; - TABLE * from_table = ctx_array[thread_id]->from_table; - TABLE * to_table = ctx_array[thread_id]->to_table; + TABLE *from_table = ctx_array[thread_id]->from_table; + TABLE *to_table = ctx_array[thread_id]->to_table; /* Conver rec_t to MySQL record */ @@ -1936,28 +1905,34 @@ int ha_innobase::parallel_copy_data_between_tables( rec_offs_init(offsets_); - offsets = rec_get_offsets(mrec, clust_index, offsets, - ULINT_UNDEFINED, UT_LOCATION_HERE, &row_heap); + offsets = rec_get_offsets(mrec, clust_index, offsets, ULINT_UNDEFINED, + UT_LOCATION_HERE, &row_heap); - byte * mysql_rec; + byte *mysql_rec; ulint bufsize = std::max(UNIV_PAGE_SIZE, from_prebuilt->mysql_row_len); mysql_rec = static_cast( ut::malloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, bufsize)); - if (!row_sel_store_mysql_rec(mysql_rec, from_prebuilt, mrec, - nullptr, true, clust_index, - clust_index, offsets, false, nullptr, - from_prebuilt->blob_heap)) { - ut::free(mysql_rec); - global_err.store(DB_ERROR); - return DB_ERROR; + if (!row_sel_store_mysql_rec(mysql_rec, from_prebuilt, mrec, nullptr, true, + clust_index, clust_index, offsets, false, + nullptr, from_prebuilt->blob_heap)) { + ut::free(mysql_rec); + global_err.store(DB_ERROR); + return DB_ERROR; } memcpy(from_table->record[0], mysql_rec, from_table->s->reclength); innobase_rec_reset(to_table); + if (to_table->next_number_field) { + if (auto_increment_field_copied) + to_table->autoinc_field_has_explicit_non_null_value = true; + else + to_table->next_number_field->reset(); + } + for (Copy_field *copy_ptr = copy; copy_ptr != copy_end; copy_ptr++) { copy_ptr->invoke_do_copy(); } @@ -1990,35 +1965,43 @@ int ha_innobase::parallel_copy_data_between_tables( } if (thd->is_killed()) { + thd->send_kill_message(); ut::free(mysql_rec); global_err.store(DB_ERROR); return DB_ERROR; } - error = row_insert_for_mysql((byte *)to_table->record[0], to_prebuilt); - int res_code = convert_error_code_to_mysql(error, 0, ha_thd()); - if (res_code) { - if (!to_table->file->is_ignorable_error(res_code)) { + error = to_table->file->ha_write_row(to_table->record[0]); + + to_table->autoinc_field_has_explicit_non_null_value = false; + + if (error) { + if (!to_table->file->is_ignorable_error(error)) { /* Not a duplicate key error. */ - to_table->file->print_error(res_code, MYF(0)); + to_table->file->print_error(error, MYF(0)); } else { /* Report duplicate key error. */ - uint key_nr = to_table->file->get_dup_key(res_code); + uint key_nr = to_table->file->get_dup_key(error); if ((int)key_nr >= 0) { const char *err_msg = ER_THD(thd, ER_DUP_ENTRY_WITH_KEY_NAME); - if (key_nr == 0 && (to_table->key_info[0].key_part[0].field->is_flag_set( - AUTO_INCREMENT_FLAG))) + if (key_nr == 0 && + (to_table->key_info[0].key_part[0].field->is_flag_set( + AUTO_INCREMENT_FLAG))) err_msg = ER_THD(thd, ER_DUP_ENTRY_AUTOINCREMENT_CASE); print_keydup_error( - to_table, key_nr == MAX_KEY ? nullptr : &to_table->key_info[key_nr], err_msg, - MYF(0), from_table->s->table_name.str); + to_table, + key_nr == MAX_KEY ? nullptr : &to_table->key_info[key_nr], + err_msg, MYF(0), from_table->s->table_name.str); } else - to_table->file->print_error(res_code, MYF(0)); + to_table->file->print_error(error, MYF(0)); } } ut::free(mysql_rec); total_recs++; - return error; + if (error) + return DB_ERROR; + else + return DB_SUCCESS; }; err = reader.add_scan(trx, config, process_row); @@ -2028,7 +2011,6 @@ int ha_innobase::parallel_copy_data_between_tables( found = total_recs.load(); - dd_table_close(new_inno_table, thd, nullptr, false); dd_table_close(old_inno_table, thd, nullptr, false); if (ctx_array) { @@ -2043,10 +2025,9 @@ int ha_innobase::parallel_copy_data_between_tables( trx->op_info = ""; if (constraint_err) return constraint_err; - return (convert_error_code_to_mysql(err, 0, ha_thd())); + return (convert_error_code_to_mysql(err, 0, thd)); } - bool ha_innobase::inplace_alter_table(TABLE *altered_table, Alter_inplace_info *ha_alter_info, const dd::Table *old_dd_tab diff --git a/storage/innobase/include/trx0trx.h b/storage/innobase/include/trx0trx.h index ba461d6559c..9e4c995b978 100644 --- a/storage/innobase/include/trx0trx.h +++ b/storage/innobase/include/trx0trx.h @@ -763,7 +763,9 @@ struct trx_t { Therefore in innobase_kill_connection. We don't increment this counter via TrxInInnoDB. */ - uint32_t in_depth; /*!< Track nested TrxInInnoDB + /* [TXSQL Parallel Copy DDL] modify this member into an atomic one + since there are multi threads that will modify it. */ + std::atomic in_depth; /*!< Track nested TrxInInnoDB count */ uint32_t in_innodb; /*!< if the thread is executing From 4735aed49370a1eccc73d3a69d114fe99370cf52 Mon Sep 17 00:00:00 2001 From: dorseywang Date: Mon, 29 May 2023 16:26:29 +0800 Subject: [PATCH 02/14] [bugfix] issue#444 assertion failed in trx rollback with pcddl. Description: =========== Assertion failed in trx0trx.cc:2343:trx->lock.n_active_thrs == 1 when we perform a copy ddl operation with pcddl. This is because that in parallel copy ddl, multi threads will use the same transaction to ensure the same consistency. Then trx->lock.n_active_thrs won't alway be 1. Solution: ======== When we use parallel copy ddl, just skip this assertion check. (cherry picked from commit e6b361e281cd0a03e80e18dab3ee48c960bd5a63) --- sql/sql_thd_internal_api.cc | 7 ++++++- sql/sql_thd_internal_api.h | 2 ++ storage/innobase/que/que0que.cc | 4 ++-- storage/innobase/trx/trx0trx.cc | 2 +- 4 files changed, 11 insertions(+), 4 deletions(-) diff --git a/sql/sql_thd_internal_api.cc b/sql/sql_thd_internal_api.cc index fbe7c2b751b..fc98b2c7b3a 100644 --- a/sql/sql_thd_internal_api.cc +++ b/sql/sql_thd_internal_api.cc @@ -418,4 +418,9 @@ bool thd_is_unnested_single_table_stmt(const THD *thd) { thd->lex->current_query_block()->outer_query_block() == nullptr && select->get_table_list()->is_base_table() && select->leaf_table_count == 1); -} \ No newline at end of file +} + +bool thd_parallel_copy_ddl(THD* thd) { + if (thd == nullptr) return false; + return thd->variables.txsql_parallel_copy_ddl; +} diff --git a/sql/sql_thd_internal_api.h b/sql/sql_thd_internal_api.h index 86b59f7cb3b..552d622de81 100644 --- a/sql/sql_thd_internal_api.h +++ b/sql/sql_thd_internal_api.h @@ -321,4 +321,6 @@ bool add_auto_statistics_task(const char* db_name, */ bool thd_is_unnested_single_table_stmt(const THD *thd); +bool thd_parallel_copy_ddl(THD* thd); + #endif // SQL_THD_INTERNAL_API_INCLUDED diff --git a/storage/innobase/que/que0que.cc b/storage/innobase/que/que0que.cc index 3a626ea6936..e70f9ad9766 100644 --- a/storage/innobase/que/que0que.cc +++ b/storage/innobase/que/que0que.cc @@ -708,7 +708,7 @@ void que_thr_stop_for_mysql(que_thr_t *thr) /*!< in: query thread */ } ut_ad(thr->is_active == true); - ut_ad(trx->lock.n_active_thrs == 1); + ut_ad(thd_parallel_copy_ddl(trx->mysql_thd) || trx->lock.n_active_thrs == 1); ut_ad(thr->graph->n_active_thrs == 1); thr->is_active = false; @@ -746,7 +746,7 @@ void que_thr_move_to_run_state_for_mysql( void que_thr_stop_for_mysql_no_error(que_thr_t *thr, trx_t *trx) { ut_ad(thr->state == QUE_THR_RUNNING); ut_ad(thr->is_active == true); - // ut_ad(trx->lock.n_active_thrs == 1); + ut_ad(thd_parallel_copy_ddl(trx->mysql_thd) || trx->lock.n_active_thrs == 1); ut_ad(thr->graph->n_active_thrs == 1); ut_a(thr->magic_n == QUE_THR_MAGIC_N); diff --git a/storage/innobase/trx/trx0trx.cc b/storage/innobase/trx/trx0trx.cc index b488d9ad50c..2dd516266e5 100644 --- a/storage/innobase/trx/trx0trx.cc +++ b/storage/innobase/trx/trx0trx.cc @@ -2333,7 +2333,7 @@ void trx_commit_or_rollback_prepare(trx_t *trx) /*!< in/out: transaction */ trx->lock.que_state = TRX_QUE_RUNNING; } - ut_a(trx->lock.n_active_thrs == 1); + ut_ad(thd_parallel_copy_ddl(trx->mysql_thd) || trx->lock.n_active_thrs == 1); return; case TRX_STATE_COMMITTED_IN_MEMORY: From f927673f3799bbbf1e6a8100fa13485d0571628a Mon Sep 17 00:00:00 2001 From: linusluo Date: Mon, 19 Feb 2024 14:57:15 +0800 Subject: [PATCH 03/14] [bugfix] prevent from occuring parallel copy ddl crash --- sql/sql_class.h | 3 +++ sql/sql_thd_api.cc | 4 ++++ sql/sql_thd_internal_api.cc | 9 +++++++++ sql/sql_thd_internal_api.h | 4 ++++ storage/innobase/handler/handler0alter.cc | 4 ++++ 5 files changed, 24 insertions(+) diff --git a/sql/sql_class.h b/sql/sql_class.h index 453773dff11..0dceecf532b 100644 --- a/sql/sql_class.h +++ b/sql/sql_class.h @@ -1824,6 +1824,9 @@ class THD : public MDL_context_owner, */ mysql_cond_t COND_thr_lock; + public: + bool is_doing_parallel_copy_data = false; + private: /** Type of current query: COM_STMT_PREPARE, COM_QUERY, etc. diff --git a/sql/sql_thd_api.cc b/sql/sql_thd_api.cc index ba2b7c51fe2..0c6c0042b65 100644 --- a/sql/sql_thd_api.cc +++ b/sql/sql_thd_api.cc @@ -331,6 +331,10 @@ static void set_thd_stage_info(MYSQL_THD thd, const PSI_stage_info *new_stage, const unsigned int calling_line) { if (thd == nullptr) thd = current_thd; + if (thd->is_doing_parallel_copy_data) { + return; + } + thd->enter_stage(new_stage, old_stage, calling_func, calling_file, calling_line); } diff --git a/sql/sql_thd_internal_api.cc b/sql/sql_thd_internal_api.cc index fc98b2c7b3a..0fc371eac88 100644 --- a/sql/sql_thd_internal_api.cc +++ b/sql/sql_thd_internal_api.cc @@ -424,3 +424,12 @@ bool thd_parallel_copy_ddl(THD* thd) { if (thd == nullptr) return false; return thd->variables.txsql_parallel_copy_ddl; } + +void thd_start_parallel_copy_data(THD *thd) { + thd->is_doing_parallel_copy_data = true; +} + +void thd_end_parallel_copy_data(THD *thd) { + thd->is_doing_parallel_copy_data = false; +} + diff --git a/sql/sql_thd_internal_api.h b/sql/sql_thd_internal_api.h index 552d622de81..a8d68ecb096 100644 --- a/sql/sql_thd_internal_api.h +++ b/sql/sql_thd_internal_api.h @@ -323,4 +323,8 @@ bool thd_is_unnested_single_table_stmt(const THD *thd); bool thd_parallel_copy_ddl(THD* thd); +void thd_start_parallel_copy_data(THD *thd); + +void thd_end_parallel_copy_data(THD *thd); + #endif // SQL_THD_INTERNAL_API_INCLUDED diff --git a/storage/innobase/handler/handler0alter.cc b/storage/innobase/handler/handler0alter.cc index 68059cd59af..6d4ce7e37a6 100644 --- a/storage/innobase/handler/handler0alter.cc +++ b/storage/innobase/handler/handler0alter.cc @@ -2005,10 +2005,14 @@ int ha_innobase::parallel_copy_data_between_tables( }; err = reader.add_scan(trx, config, process_row); + thd_start_parallel_copy_data(thd); + if (err == DB_SUCCESS) { err = reader.run(num_threads); } + thd_end_parallel_copy_data(thd); + found = total_recs.load(); dd_table_close(old_inno_table, thd, nullptr, false); From 7dad795b764d41234acbcfd442f21d64a632d540 Mon Sep 17 00:00:00 2001 From: linusluo Date: Fri, 21 Jun 2024 11:30:00 +0800 Subject: [PATCH 04/14] [bugfix] issue#1227 fix push warning concurrently when doing parallel copy DDL (merge request !1657) Problem: ======== Multiple worker threads in parallel copy DDL share a thd. If you encounter datetime conversion warning, you need to use Diagnostics_area in thd. This process has no lock protection, resulting in concurrent writing of memory. Solution: ======== When pushing warning, it is judged that if thd is currently doing parallel copy DDL, unlock it. MR!1657 reviewed by dorseywang. --- mysql-test/t/parallel_copy_alter_table.test | 19 +++++++++++++++++ sql/mysqld.cc | 18 +++++++++------- sql/mysqld.h | 2 ++ sql/sql_class.cc | 23 +++++++++++++++++++-- sql/sql_class.h | 2 ++ 5 files changed, 54 insertions(+), 10 deletions(-) diff --git a/mysql-test/t/parallel_copy_alter_table.test b/mysql-test/t/parallel_copy_alter_table.test index 2fc95795a9a..66a1068bdea 100644 --- a/mysql-test/t/parallel_copy_alter_table.test +++ b/mysql-test/t/parallel_copy_alter_table.test @@ -499,4 +499,23 @@ ALTER TABLE t1 encryption='Y'; --let $count_after = query_get_value(SELECT COUNT(*) FROM t1 WHERE c2 < 4000, COUNT(*), 1) assert($count_before == $count_after); +--echo +--echo #Case : test raise error +--echo +DROP TABLE t1; +CREATE TABLE t1(a varchar(5)); +--let $row_count=5000 +--disable_query_log +while ($row_count) +{ + --eval INSERT INTO t1 VALUES ('kkk'); + dec $row_count; +} +--enable_query_log +--error 1292 +ALTER TABLE t1 MODIFY COLUMN a DATETIME NOT NULL, ALGORITHM=COPY; + +--echo +--echo #Clear environment +--echo DROP TABLE t1; diff --git a/sql/mysqld.cc b/sql/mysqld.cc index a34a2b2797c..3569f5cd27b 100644 --- a/sql/mysqld.cc +++ b/sql/mysqld.cc @@ -1025,6 +1025,8 @@ ulonglong cdb_page_cache_cleaning_window = 16 * 1024 * 1024; bool cdb_page_cache_cleaning_redo = true; bool cdb_page_cache_cleaning_binlog = true; +PSI_mutex_key key_LOCK_push_warning; + #define mysqld_charset &my_charset_latin1 #define mysqld_default_locale_name "en_US" @@ -4020,7 +4022,7 @@ void my_message_sql(uint error, const char *str, myf MyFlags) { assert(strncmp(str, "MyISAM table", 12) == 0); error = ER_UNKNOWN_ERROR; } else if (error == ER_SYNC_TIMEOUT ) { - /* TDSQL: In order to maintain compatibility, the error + /* TDSQL: In order to maintain compatibility, the error code must be the same as ER_XA_RBTIMEOUT. */ error = ER_XA_RBTIMEOUT; } @@ -4686,9 +4688,9 @@ SHOW_VAR com_status_vars[] = { (char*) offsetof(System_status_var, com_stat[(uint) SQLCOM_SHOW_CDB_SQL_FILTERS]), SHOW_LONG_STATUS, SHOW_SCOPE_ALL}, - {"show_cdb_outline_info", - (char*) offsetof(System_status_var, - com_stat[(uint) SQLCOM_SHOW_OUTLINE_INFO]), + {"show_cdb_outline_info", + (char*) offsetof(System_status_var, + com_stat[(uint) SQLCOM_SHOW_OUTLINE_INFO]), SHOW_LONG_STATUS, SHOW_SCOPE_ALL}, {"show_auto_stats_task_status", (char*) offsetof(System_status_var, @@ -4697,7 +4699,7 @@ SHOW_VAR com_status_vars[] = { {"show_auto_stats_node_status", (char*) offsetof(System_status_var, com_stat[(uint) SQLCOM_SHOW_STATS_NODE]), - SHOW_LONG_STATUS, SHOW_SCOPE_ALL}, + SHOW_LONG_STATUS, SHOW_SCOPE_ALL}, {"shutdown", (char *)offsetof(System_status_var, com_stat[(uint)SQLCOM_SHUTDOWN]), SHOW_LONG_STATUS, SHOW_SCOPE_ALL}, @@ -12385,7 +12387,7 @@ static void delete_dictionary_tablespace() { dd::upgrade_57::Upgrade_status().remove(); } -static bool init_outline_builder_to_memory() +static bool init_outline_builder_to_memory() { THD *thd; bool return_val = false; @@ -12426,11 +12428,11 @@ static bool init_outline_builder_to_memory() strlen(get_field(&mem, table_list.table->field[2]))); op.m_outline_query.assign(get_field(&mem, table_list.table->field[3]), strlen(get_field(&mem, table_list.table->field[3]))); - if (cdb_outline_builder.construct_param_positions(thd, op)) + if (cdb_outline_builder.construct_param_positions(thd, op)) break; if (cdb_outline_loader.load_outline_info(digest_hash, op)) break; - } + } iterator.reset(); // table_list.table->m_needs_reopen = true; diff --git a/sql/mysqld.h b/sql/mysqld.h index f0b823691fe..65e69bc2332 100644 --- a/sql/mysqld.h +++ b/sql/mysqld.h @@ -81,6 +81,8 @@ extern int mysql_admin_tool_set_priority(ulonglong os_thread_id, int priority); extern my_thread_os_id_t admin_listener_os_thread_id; extern bool admin_tool_valid; +extern PSI_mutex_key key_LOCK_push_warning; + class Rpl_global_filter; class Rpl_acf_configuration_handler; class Source_IO_monitor; diff --git a/sql/sql_class.cc b/sql/sql_class.cc index b419a3fcebf..647142253a8 100644 --- a/sql/sql_class.cc +++ b/sql/sql_class.cc @@ -825,6 +825,7 @@ THD::THD(bool enable_plugins) mysql_mutex_init(key_LOCK_current_cond, &LOCK_current_cond, MY_MUTEX_INIT_FAST); mysql_cond_init(key_COND_thr_lock, &COND_thr_lock); + mysql_mutex_init(key_LOCK_push_warning, &LOCK_push_warning, MY_MUTEX_INIT_FAST); mysql_mutex_init(key_LOCK_thd_done, &m_thd_lock_done, MY_MUTEX_INIT_FAST); mysql_cond_init(key_COND_thd_done, &m_thd_stage_cond_commit_order); @@ -1068,9 +1069,18 @@ Sql_condition *THD::raise_condition(uint sql_errno, const char *sqlstate, const char *msg, bool fatal_error) { DBUG_TRACE; + if (is_doing_parallel_copy_data) { + mysql_mutex_lock(&LOCK_push_warning); + } + if (!(variables.option_bits & OPTION_SQL_NOTES) && - (level == Sql_condition::SL_NOTE)) + (level == Sql_condition::SL_NOTE)) { + if (is_doing_parallel_copy_data) { + mysql_mutex_unlock(&LOCK_push_warning); + } return nullptr; + } + assert(sql_errno != 0); if (sql_errno == 0) /* Safety in release build */ @@ -1085,7 +1095,12 @@ Sql_condition *THD::raise_condition(uint sql_errno, const char *sqlstate, } MYSQL_LOG_ERROR(sql_errno, PSI_ERROR_OPERATION_RAISED); - if (handle_condition(sql_errno, sqlstate, &level, msg)) return nullptr; + if (handle_condition(sql_errno, sqlstate, &level, msg)) { + if (is_doing_parallel_copy_data) { + mysql_mutex_unlock(&LOCK_push_warning); + } + return nullptr; + } Diagnostics_area *da = get_stmt_da(); if (level == Sql_condition::SL_ERROR) { @@ -1119,6 +1134,9 @@ Sql_condition *THD::raise_condition(uint sql_errno, const char *sqlstate, sql_errno == ER_STD_BAD_ALLOC_ERROR))) { cond = da->push_warning(this, sql_errno, sqlstate, level, msg); } + if (is_doing_parallel_copy_data) { + mysql_mutex_unlock(&LOCK_push_warning); + } return cond; } @@ -1542,6 +1560,7 @@ THD::~THD() { mysql_mutex_destroy(&LOCK_thd_security_ctx); mysql_mutex_destroy(&LOCK_current_cond); mysql_mutex_destroy(&LOCK_group_replication_connection_mutex); + mysql_mutex_destroy(&LOCK_push_warning); mysql_cond_destroy(&COND_thr_lock); mysql_mutex_destroy(&m_thd_lock_done); diff --git a/sql/sql_class.h b/sql/sql_class.h index 0dceecf532b..2e3b8aad26f 100644 --- a/sql/sql_class.h +++ b/sql/sql_class.h @@ -1827,6 +1827,8 @@ class THD : public MDL_context_owner, public: bool is_doing_parallel_copy_data = false; + mysql_mutex_t LOCK_push_warning; + private: /** Type of current query: COM_STMT_PREPARE, COM_QUERY, etc. From 089c67302f3886f03bddb7e1fb7a3d265569c6b1 Mon Sep 17 00:00:00 2001 From: linusluo Date: Thu, 11 Jul 2024 20:24:31 +0800 Subject: [PATCH 05/14] [feature]issue#827 Make parallel copy DDL support partition table (merge request !1718) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Problem: ======== Now parallel copy DDL does not support partition tables. When encountering copy DDL of a partition table, it will use the single-threaded method. So make parallel copy DDL support partition table. Solution: ======== Implement `ha_innopart::parallel_copy_data_between_tables`,use parallel scan interface of `ha_innopart` to do. The implementation in ha_innopart::parallel_copy_data_between_tables is to be compatible with the implementation of non-partitioned tables. A better approach is to mention both implementations to the SQL layer and use the parallel scan interface of the handler. MR!1718 reviewed by dorseywang. --- mysql-test/t/parallel_copy_alter_table.test | 208 +++++++++++++++++++- sql/handler.h | 5 +- sql/sql_table.cc | 22 ++- storage/innobase/handler/ha_innopart.h | 5 + storage/innobase/handler/handler0alter.cc | 198 ++++++++++++++++++- 5 files changed, 415 insertions(+), 23 deletions(-) diff --git a/mysql-test/t/parallel_copy_alter_table.test b/mysql-test/t/parallel_copy_alter_table.test index 66a1068bdea..8b1f5fbc466 100644 --- a/mysql-test/t/parallel_copy_alter_table.test +++ b/mysql-test/t/parallel_copy_alter_table.test @@ -116,7 +116,7 @@ drop table t1; --echo --echo ---echo #Case 11: parallel copy ddl doesn't support partition table now +--echo #Case 11: Test partition table CREATE TABLE t1 (a INT NOT NULL, b INT NOT NULL) PARTITION BY KEY (a) PARTITIONS 2; INSERT INTO t1 VALUES (0,1), (0,2); @@ -189,11 +189,11 @@ partition by range columns(a) partition p2 values less than ('c'), partition p3 values less than ('d')); insert into t1 values ('A'),('a'),('B'),('b'),('C'),('c'); -select * from t1 where a > 'B' collate latin1_bin; -select * from t1 where a <> 'B' collate latin1_bin; +select * from t1 where a > 'B' collate latin1_bin order by a; +select * from t1 where a <> 'B' collate latin1_bin order by a; alter table t1 remove partitioning; -select * from t1 where a > 'B' collate latin1_bin; -select * from t1 where a <> 'B' collate latin1_bin; +select * from t1 where a > 'B' collate latin1_bin order by a; +select * from t1 where a <> 'B' collate latin1_bin order by a; drop table t1; --echo @@ -466,7 +466,7 @@ SET SESSION txsql_parallel_copy_ddl=ON; --echo --echo ---echo #Case : test parallel copy ddl with a large amount of data +--echo #Case 30: test parallel copy ddl with a large amount of data CREATE TABLE t1 (c1 char(127) PRIMARY KEY, c2 int, c3 char(127)) ENGINE=INNODB; let row_count=5000; --disable_query_log @@ -515,7 +515,203 @@ while ($row_count) --error 1292 ALTER TABLE t1 MODIFY COLUMN a DATETIME NOT NULL, ALGORITHM=COPY; +--echo +--echo #Case : test when innodb_parallel_read_threads = 1 +--echo +DROP TABLE t1; +CREATE TABLE t1(a INT, b INT); +INSERT INTO t1 VALUES(1, 10), (2, 20), (3, 30); +SET SESSION txsql_parallel_copy_ddl=ON; +SET SESSION innodb_parallel_read_threads=1; +ALTER TABLE t1 ENGINE=INNODB, ALGORITHM=COPY; +SELECT * FROM t1 ORDER BY a; + --echo --echo #Clear environment --echo DROP TABLE t1; + + +SET SESSION innodb_parallel_read_threads=16; + +--echo +--echo +--echo #Case 31: Test partition table with datetime +CREATE TABLE t1 (c1 INT, c2 DATETIME NOT NULL) PARTITION BY KEY(c1) PARTITIONS 4; +INSERT INTO t1 VALUES(1, '2000-01-01'); +INSERT INTO t1 VALUES(1001, '2001-01-01'); +INSERT INTO t1 VALUES(2001, '2002-01-01'); +ALTER TABLE t1 ADD COLUMN c3 DATETIME, ALGORITHM= COPY; +ALTER TABLE t1 MODIFY c2 DATETIME NOT NULL FIRST, ALGORITHM= COPY; +SELECT * FROM t1; +DROP TABLE t1; + +--echo +--echo +--echo #Case 32: Test partition table change column to different name +CREATE TABLE `t1` ( + `a` INT DEFAULT NULL, + `b` VARCHAR(30) DEFAULT NULL, + `c` DOUBLE DEFAULT NULL +) PARTITION BY KEY(a) PARTITIONS 3; +INSERT INTO t1 VALUES(1, 'abcd', 1.234); +ALTER TABLE t1 RENAME COLUMN b TO c, CHANGE COLUMN c d FLOAT; +SELECT * FROM t1; +DROP TABLE t1; + +--echo +--echo +--echo #Case 32: Test alter table engine=innodb +CREATE TABLE t1 ( + a int PRIMARY KEY, + b VARCHAR(6), + INDEX b3(b(3)) +) PARTITION BY KEY(a) PARTITIONS 4; +INSERT INTO t1 VALUES(1,'foo'),(2,'foobar'); +SELECT * FROM t1 WHERE b LIKE 'foob%'; +ALTER TABLE t1 ENGINE=INNODB, ALGORITHM=COPY; +SELECT * FROM t1 WHERE b LIKE 'foob%'; +DROP TABLE t1; + +--echo +--echo +--echo #Case 32: Test alter table subpartition +CREATE TABLE t1(a int primary key, b int) +PARTITION BY RANGE (a) +SUBPARTITION BY HASH (a) SUBPARTITIONS 2 +( + PARTITION p0 VALUES LESS THAN (10) + (SUBPARTITION sp0, + SUBPARTITION sp1), + PARTITION p1 VALUES LESS THAN (20) + (SUBPARTITION sp2, + SUBPARTITION sp3), + PARTITION p2 VALUES LESS THAN (30) + (SUBPARTITION sp4, + SUBPARTITION sp5) +); +delimiter |; +drop procedure if exists insert_t; +create procedure insert_t(in num int) +begin +declare i int default 0; +set autocommit=0; +repeat +insert into t1 values(i, i); +set i=i+1; +until i=num +end repeat; +commit; +set autocommit=1; +end | +delimiter ;| +CALL insert_t(29); +ALTER TABLE t1 ENGINE=INNODB, ALGORITHM=COPY; +DROP TABLE t1; + +--echo +--echo +--echo #Case 33: Test parallel copy ddl with a large amount of data with partition table +CREATE TABLE t1 (c1 char(127) PRIMARY KEY, c2 int, c3 char(127)) +PARTITION BY KEY(c1) PARTITIONS 4; +let row_count=5000; +--disable_query_log +while ($row_count) +{ + --let $c2=`select 1 + floor(rand()*10000 % 10000)` + --eval INSERT INTO t1 VALUES (LPAD($row_count, 127, 0), $c2, LPAD($row_count, 127, 0)); + dec $row_count; +} +--enable_query_log +--let $count_before = query_get_value(SELECT COUNT(*) FROM t1 WHERE c2 < 4000, COUNT(*), 1) + +set session innodb_parallel_read_threads=16; +ALTER TABLE t1 change column c2 c2 bigint, change column c3 c3 int, drop primary key, add index idx(c2); + +SHOW CREATE TABLE t1; + +--let $count_after = query_get_value(SELECT COUNT(*) FROM t1 WHERE c2 < 4000, COUNT(*), 1) +assert($count_before == $count_after); + +set session innodb_parallel_read_threads=8; +ALTER TABLE t1 change column c2 c2 char(10); + +ALTER TABLE t1 change column c2 c2 bigint; +--let $count_after = query_get_value(SELECT COUNT(*) FROM t1 WHERE c2 < 4000, COUNT(*), 1) +assert($count_before == $count_after); + +set session innodb_parallel_read_threads=5; +ALTER TABLE t1 encryption='Y'; +--let $count_after = query_get_value(SELECT COUNT(*) FROM t1 WHERE c2 < 4000, COUNT(*), 1) +assert($count_before == $count_after); + +DROP TABLE t1; + +--echo +--echo +--echo #Case 33: Test foreign key +CREATE TABLE t1 (id BIGINT PRIMARY KEY, a VARCHAR(255)); +CREATE TABLE t2 (id BIGINT, fid BIGINT, b VARCHAR(255)); +INSERT INTO t1 VALUES(1, 'aaa'), (2, 'bbb'); +INSERT INTO t2 VALUES(1001, 1, 'abcde'), (1002, 2, 'efghi'); +ALTER TABLE t2 ADD FOREIGN KEY fk_t2_fid_t1_id (fid) REFERENCES t1 (id); +SELECT * FROM t2; +DROP TABLE t2; +CREATE TABLE t2 (id BIGINT, fid BIGINT, b VARCHAR(255)); +INSERT INTO t2 VALUES(1001, 1, 'abcde'), (1002, 3, 'efghi'); +--disable_result_log +--error 1452 +ALTER TABLE t2 ADD FOREIGN KEY fk_t2_fid_t1_id (fid) REFERENCES t1 (id); +--enable_result_log +DROP TABLE t2; +CREATE TABLE t2 (id BIGINT, fid BIGINT, b VARCHAR(255)) +PARTITION BY KEY (id) PARTITIONS 4; +INSERT INTO t2 VALUES(1001, 1, 'abcde'), (1002, 2, 'efghi'); +--error 1506 +ALTER TABLE t2 ADD FOREIGN KEY fk_t2_fid_t1_id (fid) REFERENCES t1 (id); +DROP TABLE t1, t2; + +--echo +--echo +--echo #Case 33: Checksum table +SET SESSION txsql_parallel_copy_ddl=OFF; +CREATE TABLE t1 (c1 char(127) PRIMARY KEY, c2 int, c3 char(127)) +PARTITION BY KEY(c1) PARTITIONS 4; +let row_count=5000; +--disable_query_log +while ($row_count) +{ + --let $c2=`select 1 + floor(rand()*10000 % 10000)` + --eval INSERT INTO t1 VALUES (LPAD($row_count, 127, 0), $c2, LPAD($row_count, 127, 0)); + dec $row_count; +} +--enable_query_log +CREATE TABLE t2 LIKE t1; +INSERT INTO t2 SELECT * FROM t1; + +ALTER TABLE t1 change column c2 c2 bigint, change column c3 c3 int, drop primary key, add index idx(c2); +--let $sum11 = query_get_value(CHECKSUM TABLE t1, Checksum, 1) +ALTER TABLE t1 change column c2 c2 char(10); +--let $sum12 = query_get_value(CHECKSUM TABLE t1, Checksum, 1) +ALTER TABLE t1 change column c2 c2 bigint; +--let $sum13 = query_get_value(CHECKSUM TABLE t1, Checksum, 1) +ALTER TABLE t1 encryption='Y'; +--let $sum14 = query_get_value(CHECKSUM TABLE t1, Checksum, 1) + +SET SESSION txsql_parallel_copy_ddl=ON; +SET SESSION innodb_parallel_read_threads=16; +ALTER TABLE t2 change column c2 c2 bigint, change column c3 c3 int, drop primary key, add index idx(c2); +--let $sum21 = query_get_value(CHECKSUM TABLE t2, Checksum, 1) +ALTER TABLE t2 change column c2 c2 char(10); +--let $sum22 = query_get_value(CHECKSUM TABLE t2, Checksum, 1) +ALTER TABLE t2 change column c2 c2 bigint; +--let $sum23 = query_get_value(CHECKSUM TABLE t2, Checksum, 1) +ALTER TABLE t2 encryption='Y'; +--let $sum24 = query_get_value(CHECKSUM TABLE t2, Checksum, 1) +assert($sum11 == $sum21); +assert($sum12 == $sum22); +assert($sum13 == $sum23); +assert($sum14 == $sum24); + +DROP TABLE t1, t2; +DROP PROCEDURE insert_t; diff --git a/sql/handler.h b/sql/handler.h index 3c19f0a4bfc..946421d0f16 100644 --- a/sql/handler.h +++ b/sql/handler.h @@ -3608,7 +3608,7 @@ class Alter_inplace_info { class Alter_copy_info { public: - Alter_copy_info() : handler_ctx(nullptr) {} + Alter_copy_info() : handler_ctx(nullptr), fallback(false) {} ~Alter_copy_info() {} /** @@ -3618,6 +3618,9 @@ class Alter_copy_info { @see copy_alter_handler_ctx for information about object lifecycle. */ copy_alter_handler_ctx *handler_ctx; + + // Whether need fallback from parallel copy ddl to normal way. + bool fallback; }; struct HA_CHECK_OPT { diff --git a/sql/sql_table.cc b/sql/sql_table.cc index f80a4acbadf..1d87d9ba39e 100644 --- a/sql/sql_table.cc +++ b/sql/sql_table.cc @@ -18706,9 +18706,8 @@ static bool check_if_can_use_parallel_copy_ddl(THD *thd, TABLE *from, TABLE *to, to->file->table_type(), "InnoDB") == 0 && !alter_ctx->is_tmp_table() && order == nullptr && !is_auto_inc - && (!from->part_info && !to->part_info) - && table_def->foreign_keys()->empty() - && const_cast(old_table_def)->foreign_keys()->empty() + && (table_def && table_def->foreign_keys()->empty()) + && (old_table_def && old_table_def->foreign_keys().empty()) && !is_multi_value) { return true; } @@ -18944,14 +18943,25 @@ static int copy_data_between_tables( alter_ctx->get_tmp_path()); error = from->file->ha_parallel_copy_data_between_tables(from, to, table_def, - old_table_def, - &ha_copy_alter_info, - create, found_count); + old_table_def, + &ha_copy_alter_info, + create, found_count); + + if (ha_copy_alter_info.fallback) { + if (iterator->Init()) { + error= 1; + goto err; + } + thd->get_stmt_da()->reset_current_row_for_condition(); + goto fallback; + } + mysql_stage_set_work_completed(psi, found_count); thd->get_stmt_da()->set_current_row_for_condition(found_count); } } else { +fallback: while (!(error = iterator->Read())) { if (thd->killed) { thd->send_kill_message(); diff --git a/storage/innobase/handler/ha_innopart.h b/storage/innobase/handler/ha_innopart.h index 30a4e32fbec..9233a3f9efc 100644 --- a/storage/innobase/handler/ha_innopart.h +++ b/storage/innobase/handler/ha_innopart.h @@ -1177,5 +1177,10 @@ class ha_innopart : public ha_innobase, public: bool prepare_backquery(THD *thd, time_t t) override; + + int parallel_copy_data_between_tables(TABLE *from, TABLE *to, + dd::Table *new_dd_tab, const dd::Table *old_dd_tab, + Alter_copy_info *ha_copy_alter_info, + List &create, ulong &found) override; }; #endif /* ha_innopart_h */ diff --git a/storage/innobase/handler/handler0alter.cc b/storage/innobase/handler/handler0alter.cc index 6d4ce7e37a6..f197034470f 100644 --- a/storage/innobase/handler/handler0alter.cc +++ b/storage/innobase/handler/handler0alter.cc @@ -376,15 +376,18 @@ struct ha_innobase_copy_ctx_t { to_table->column_bitmaps_set_no_signal(&to_table->s->all_set, &to_table->s->all_set); - from_prebuilt = row_create_prebuilt(old_inno_table, from->s->reclength); - from_prebuilt->default_rec = from->s->default_values; - ut_ad(from_prebuilt->default_rec); + from_prebuilt = nullptr; + if (old_inno_table) { + from_prebuilt = row_create_prebuilt(old_inno_table, from->s->reclength); + from_prebuilt->default_rec = from->s->default_values; + ut_ad(from_prebuilt->default_rec); - from_prebuilt->trx = old_prebuilt->trx; + from_prebuilt->trx = old_prebuilt->trx; - from_prebuilt->m_mysql_table = from; + from_prebuilt->m_mysql_table = from; - build_copy_template(from_prebuilt, true, from_table); + build_copy_template(from_prebuilt, true, from_table); + } to_table->next_number_field = to_table->found_next_number_field; @@ -462,8 +465,10 @@ struct ha_innobase_copy_ctx_t { mem_heap_free(copy_heap); /* Do not close the table. */ - from_prebuilt->table = nullptr; - row_prebuilt_free(from_prebuilt, false); + if (from_prebuilt) { + from_prebuilt->table = nullptr; + row_prebuilt_free(from_prebuilt, false); + } destroy_array(copy, to_table->s->fields); @@ -1837,9 +1842,13 @@ int ha_innobase::parallel_copy_data_between_tables( num_threads = Parallel_reader::available_threads(num_threads, false); - if (num_threads == 0) { + if (num_threads <= 1) + { mem_heap_free(heap); - ib::error(ER_IB_MSG_1004) << "No enough parallel reader threads."; + if (num_threads == 0) { + ib::warn() << "No enough parallel reader threads."; + } + ha_copy_alter_info->fallback = true; return (DB_INTERRUPTED); } @@ -10884,6 +10893,175 @@ enum_alter_inplace_result ha_innopart::check_if_supported_inplace_alter( ha_alter_info); } +int ha_innopart::parallel_copy_data_between_tables( + TABLE *from, TABLE *to, dd::Table *new_dd_tab, const dd::Table *old_dd_tab, + Alter_copy_info *ha_copy_alter_info, List &create, + ulong &found) { + + if (dict_table_is_discarded(m_prebuilt->table)) { + return (HA_ERR_NO_SUCH_TABLE); + } + + int ret = 0; + THD *thd = current_thd; + copy_alter_handler_ctx *ctx = ha_copy_alter_info->handler_ctx; + + void *scan_ctx; + size_t num_threads; + + if ((ret = parallel_scan_init(scan_ctx, &num_threads, true))) { + return ret; + } + + sql_print_information( + "[TXSQL] parallel_threads=%u, " + "sql=%s.", + num_threads, thd->query().str); + + bool auto_increment_field_copied = 0; + + ha_innobase_copy_ctx_t **ctx_array = + ut::new_arr_withkey(UT_NEW_THIS_FILE_PSI_KEY, + ut::Count{num_threads}); + + for (size_t i = 0; i < num_threads; i++) { + ctx_array[i] = + ut::new_withkey(UT_NEW_THIS_FILE_PSI_KEY); + ctx_array[i]->init(nullptr, from, to, m_prebuilt, thd, new_dd_tab, + old_dd_tab, ctx, create, auto_increment_field_copied); + } + + Parallel_reader_adapter::Init_fn init_fn = + [&thd](void *cookie, ulong ncols, ulong row_len, const ulong *col_offsets, + const ulong *null_byte_offsets, + const ulong *null_bitmasks) -> bool { + current_thd = thd; + return false; + }; + + std::atomic total_recs{0}; + + Parallel_reader_adapter::Load_fn load_fn = + [&auto_increment_field_copied, &total_recs](void *cookie, uint nrows, void *rowdata, + uint64_t partition_id) -> bool { + int error; + + THD *thd = current_thd; + + ha_innobase_copy_ctx_t *ctx = static_cast(cookie); + + Copy_field *copy = ctx->copy; + Copy_field *copy_end = ctx->copy_end; + + Field **gen_fields = ctx->gen_fields; + Field **gen_fields_end = ctx->gen_fields_end; + + TABLE *from_table = ctx->from_table; + TABLE *to_table = ctx->to_table; + + byte *buffer = static_cast(rowdata); + + for (uint i = 0; i < nrows; i++) { + byte *mysql_rec = buffer + from_table->s->reclength * i; + + memcpy(from_table->record[0], mysql_rec, from_table->s->reclength); + + innobase_rec_reset(to_table); + + if (to_table->next_number_field) { + if (auto_increment_field_copied) + to_table->autoinc_field_has_explicit_non_null_value = true; + else + to_table->next_number_field->reset(); + } + + for (Copy_field *copy_ptr = copy; copy_ptr != copy_end; copy_ptr++) { + copy_ptr->invoke_do_copy(); + } + + if (thd->is_error()) { + return true; + } + + for (auto ptr = gen_fields; ptr != gen_fields_end; ptr++) { + Item *expr_item; + if ((*ptr)->is_gcol()) { + expr_item = (*ptr)->gcol_info->expr_item; + } else { + assert((*ptr)->has_insert_default_general_value_expression()); + expr_item = (*ptr)->m_default_val_expr->expr_item; + } + expr_item->save_in_field(*ptr, false); + if (thd->is_error()) { + return true; + } + } + // store the check constraint result error code + if (invoke_table_check_constraints(thd, to_table)) { + return true; + } + + if (thd->is_killed()) { + thd->send_kill_message(); + return true; + } + + error = to_table->file->ha_write_row(to_table->record[0]); + + to_table->autoinc_field_has_explicit_non_null_value = false; + + if (error) { + if (!to_table->file->is_ignorable_error(error)) { + /* Not a duplicate key error. */ + to_table->file->print_error(error, MYF(0)); + } else { + /* Report duplicate key error. */ + uint key_nr = to_table->file->get_dup_key(error); + if ((int)key_nr >= 0) { + const char *err_msg = ER_THD(thd, ER_DUP_ENTRY_WITH_KEY_NAME); + if (key_nr == 0 && + (to_table->key_info[0].key_part[0].field->is_flag_set( + AUTO_INCREMENT_FLAG))) + err_msg = ER_THD(thd, ER_DUP_ENTRY_AUTOINCREMENT_CASE); + print_keydup_error( + to_table, + key_nr == MAX_KEY ? nullptr : &to_table->key_info[key_nr], + err_msg, MYF(0), from_table->s->table_name.str); + } else + to_table->file->print_error(error, MYF(0)); + } + return true; + } + + total_recs++; + } + + return false; + }; + + Parallel_reader_adapter::End_fn end_fn = [](void *cookie) {}; + + if ((ret = parallel_scan(scan_ctx, reinterpret_cast(ctx_array), init_fn, load_fn, end_fn))) { + goto error; + } + + parallel_scan_end(scan_ctx); + + found = total_recs.load(); + +error: + if (ctx_array) { + for (size_t i = 0; i < num_threads; i++) { + ctx_array[i]->destroy(); + ut::delete_(ctx_array[i]); + } + ut::delete_arr(ctx_array); + ctx_array = nullptr; + } + + return ret; +} + /** Prepare in-place ALTER for table. Allows InnoDB to update internal structures with concurrent writes blocked (provided that check_if_supported_inplace_alter() From 6b80da23e504c221cd02fdae74342af8fccc5e87 Mon Sep 17 00:00:00 2001 From: linusluo Date: Tue, 19 Nov 2024 12:39:09 +0000 Subject: [PATCH 06/14] [bugfix] issue#1887 Parallel copy ddl over stack (merge request !2338) Problem: ======== The THD object is generally bound to a thread, but in Parallel Copy DDL, the THD is shared by multiple threads, and each thread sets the thread stack of the THD, which is the top position of the current thread's stack. However, since the stacks of each thread are not contiguous, it will lead to a very large stack space value when judging whether the THD is stack-full later on. Solution: ======== Parallel Copy DDL resets the stack top to its original value after the Parallel Reader thread finishes running. MR!2338 reviewed by dorseywang and characterli. --- storage/innobase/handler/handler0alter.cc | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/storage/innobase/handler/handler0alter.cc b/storage/innobase/handler/handler0alter.cc index f197034470f..bfb348980f1 100644 --- a/storage/innobase/handler/handler0alter.cc +++ b/storage/innobase/handler/handler0alter.cc @@ -1879,6 +1879,8 @@ int ha_innobase::parallel_copy_data_between_tables( std::atomic total_recs{0}; std::atomic global_err{DB_SUCCESS}; + const char *origin_stack = thd->thread_stack; + auto process_row = [&](const Parallel_reader::Ctx *ctx) { int error; const rec_t *mrec = ctx->m_rec; @@ -2021,6 +2023,7 @@ int ha_innobase::parallel_copy_data_between_tables( } thd_end_parallel_copy_data(thd); + thd->thread_stack = origin_stack; found = total_recs.load(); @@ -10941,12 +10944,16 @@ int ha_innopart::parallel_copy_data_between_tables( std::atomic total_recs{0}; + const char *origin_stack = thd->thread_stack; + Parallel_reader_adapter::Load_fn load_fn = [&auto_increment_field_copied, &total_recs](void *cookie, uint nrows, void *rowdata, uint64_t partition_id) -> bool { int error; THD *thd = current_thd; + char my_stack; + thd->thread_stack = &my_stack; // remember where our stack is ha_innobase_copy_ctx_t *ctx = static_cast(cookie); @@ -11041,10 +11048,15 @@ int ha_innopart::parallel_copy_data_between_tables( Parallel_reader_adapter::End_fn end_fn = [](void *cookie) {}; + thd_start_parallel_copy_data(thd); + if ((ret = parallel_scan(scan_ctx, reinterpret_cast(ctx_array), init_fn, load_fn, end_fn))) { goto error; } + thd_end_parallel_copy_data(thd); + thd->thread_stack = origin_stack; + parallel_scan_end(scan_ctx); found = total_recs.load(); From b3b097cc058ce5d44194f00588c5e946ef4be2b9 Mon Sep 17 00:00:00 2001 From: linusluo Date: Mon, 16 Dec 2024 03:43:43 +0000 Subject: [PATCH 07/14] [bugfix] issue#1980 Parallel copy DDL for partition table doesn't release threads with error (merge request !2522) Problem: ======== The parallel copy DDL for the partitioned table does not release the parallel reader threads when an error occurs during execution. Solution: ======== Release parallel reader threads when an error occurs during execution. --story=135473349 MR!2522 reviewed by dorseywang. TAPD: --bug=135473349 --- storage/innobase/handler/handler0alter.cc | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/storage/innobase/handler/handler0alter.cc b/storage/innobase/handler/handler0alter.cc index bfb348980f1..0423ccc050c 100644 --- a/storage/innobase/handler/handler0alter.cc +++ b/storage/innobase/handler/handler0alter.cc @@ -11054,14 +11054,13 @@ int ha_innopart::parallel_copy_data_between_tables( goto error; } + found = total_recs.load(); + +error: thd_end_parallel_copy_data(thd); thd->thread_stack = origin_stack; - parallel_scan_end(scan_ctx); - found = total_recs.load(); - -error: if (ctx_array) { for (size_t i = 0; i < num_threads; i++) { ctx_array[i]->destroy(); From fefe2272eee61184b4dcc01eb39d970502cce366 Mon Sep 17 00:00:00 2001 From: linusluo Date: Mon, 26 May 2025 21:06:58 +0800 Subject: [PATCH 08/14] [bugfix] issue#2550 Fix thread safety issues with the m_internal_handler of THD objects in parallel copy DDL operations Problem: ======== In Parallel Copy DDL operations, multiple threads share the same THD object, but concurrent operations to the m_internal_handler linked list lacks proper synchronization protection. Solution: ======== For threads executing parallel copy DDL operations, accessing THD::m_internal_handler requires lock protection. --bug=142749233 --- mysql-test/r/parallel_copy_alter_table.result | Bin 17366 -> 22428 bytes sql/mysqld.cc | 1 + sql/mysqld.h | 1 + sql/sql_class.cc | 41 +++++++++++++++++- sql/sql_class.h | 2 + 5 files changed, 43 insertions(+), 2 deletions(-) diff --git a/mysql-test/r/parallel_copy_alter_table.result b/mysql-test/r/parallel_copy_alter_table.result index e3c6b6e560f58b4eb7d6cf8a1ed483cdca4ad7af..ade221ce085cbc36beca17211d1a2f66d97490af 100644 GIT binary patch delta 3222 zcmb_e-EZ4e6jy0gYIho?-Djt5hmI~z=e7Q5L2J|v*Kyitsa+fwsD#wnj@z2HcH}tQ z3W&Th@wRbadEo`|!j~#Gi9Z4{9(dq|e}R7hiF2JMjgxLLgcQlS=iGDd`Tm{X9`t?j zUElXVjhwW+S`1Y?iv8FRgU<&~9sD)&`_M3iQN@eE*$124AHSJ8pB{Hp=Pv%i<8kHc zkSxqYjJnB}lVb-X%9EEG>=o6ndC(+7WczsVq9}}5Hg6iMU@40w4Pr6Js-~t`TARSx zaMzC9Z9C%Tve_J~lq}6`b795E7Ypm48l|-r9c;*>tW{jmKsPMV*GeTGl+uzxw8>HO z%gYnfJ3BiW*bF0Z!`=E`02w<2b_`K?4@5R?p8Zpl{D3YDJYo63PueKGmlYOTn9VH+Kj`Bj3}@YBMGd`$O3h2*Rvz=g1dec z276u*cX~9E1U_xl#CR(lvN#XvLV5J&Rr1HJ!G0cG@^onIN)}cX(<)l%4z=O7w$8a? z2eAyZWR*Whz8V^R^yJKdC z56-!VZs%B6VqB4NWfqkP*98%OAPDoxj}aMK0F*`PX%^DjT&bH&CCE2@7e{5O+jSBS z-|DA1{$Y5`dPgzUWyRz$t&rCW%3A5L=GZ mpGNw5B^5Y)4R9k+M5zWss9Vt)7@5 zpPiU_y*tk|xh_+|^xBSFVZ>=ko+Z=FxklSidpE|;jcsQa`zzS;18>0(g0SL1cVhv^ z+TaH zq*T1EL1r@yoyknISS5y@?5Ml9U$hB2UpRC86VDD-t?sl3VRxu-2GKxOBY)41_F>1{ zr=5`sE zMOZGSuT~ZK^b57Bh}mlEg{pXrBAh~rqNDerR+V}*$!Sf}F`BU6drX?`Q8b4IlVdAU zSjEVNu2-o?9{8Kk@b3GyxQ6koc~QI(V;4uB9ebU(4q2QZKv3UnhT8G4oY{)!?%GsQ zl9GzIY1emS5OQqIi@~nPp&Rb)`SAi$BCm>i+2%X+dPqm*T@u_FZ1_C*XdM;u7yN68 zJM|#;ckzJlv0hMtrZgIt=A$jsVWDz+w?W%1X@-MBvu^kmeG+PQ$s>A;`mKTM6x_L%{(K zCCRJT#~TzJY}?KpqG-l(oD4>{sFyX+fg%kp@aQyw?|r%XTJWz94V6PDL;!gW$-~gaOqav)yRIZl?ozOY>ai$Ubb)!sN0^qQ^T>Bz&QJGvcFKsk!y#hpw254fa*T}ERTjXu^3RzO;$k*!So>g_GPa+%Y1$ysfx|NW8 g8pX~g7cO=)AFG!xHyus(=vC8Dwfq|H14kh6H%*^`vj6}9 delta 72 zcmbQUp7B~c*$2^S<5m1LG==I1GtBqrsgD&*ytZ$2mRf@AVQNxR8hQpq5C b^Fb*`HD)V?lF6%l4L4gj&S9E-A%GtMDA61< diff --git a/sql/mysqld.cc b/sql/mysqld.cc index 3569f5cd27b..8c415a25c8a 100644 --- a/sql/mysqld.cc +++ b/sql/mysqld.cc @@ -1026,6 +1026,7 @@ bool cdb_page_cache_cleaning_redo = true; bool cdb_page_cache_cleaning_binlog = true; PSI_mutex_key key_LOCK_push_warning; +PSI_mutex_key key_LOCK_internal_handler; #define mysqld_charset &my_charset_latin1 #define mysqld_default_locale_name "en_US" diff --git a/sql/mysqld.h b/sql/mysqld.h index 65e69bc2332..c153c51e508 100644 --- a/sql/mysqld.h +++ b/sql/mysqld.h @@ -82,6 +82,7 @@ extern my_thread_os_id_t admin_listener_os_thread_id; extern bool admin_tool_valid; extern PSI_mutex_key key_LOCK_push_warning; +extern PSI_mutex_key key_LOCK_internal_handler; class Rpl_global_filter; class Rpl_acf_configuration_handler; diff --git a/sql/sql_class.cc b/sql/sql_class.cc index 647142253a8..ef35aa42cf3 100644 --- a/sql/sql_class.cc +++ b/sql/sql_class.cc @@ -826,6 +826,7 @@ THD::THD(bool enable_plugins) MY_MUTEX_INIT_FAST); mysql_cond_init(key_COND_thr_lock, &COND_thr_lock); mysql_mutex_init(key_LOCK_push_warning, &LOCK_push_warning, MY_MUTEX_INIT_FAST); + mysql_mutex_init(key_LOCK_internal_handler, &LOCK_internal_handler, MY_MUTEX_INIT_FAST); mysql_mutex_init(key_LOCK_thd_done, &m_thd_lock_done, MY_MUTEX_INIT_FAST); mysql_cond_init(key_COND_thd_done, &m_thd_stage_cond_commit_order); @@ -969,30 +970,63 @@ bool THD::set_db(const LEX_CSTRING &new_db) { } void THD::push_internal_handler(Internal_error_handler *handler) { + if (is_doing_parallel_copy_data) { + mysql_mutex_lock(&LOCK_internal_handler); + } + if (m_internal_handler) { handler->m_prev_internal_handler = m_internal_handler; m_internal_handler = handler; } else m_internal_handler = handler; + + if (is_doing_parallel_copy_data) { + mysql_mutex_unlock(&LOCK_internal_handler); + } } bool THD::handle_condition(uint sql_errno, const char *sqlstate, Sql_condition::enum_severity_level *level, const char *msg) { - if (!m_internal_handler) return false; + if (is_doing_parallel_copy_data) { + mysql_mutex_lock(&LOCK_internal_handler); + } + + if (!m_internal_handler) { + if (is_doing_parallel_copy_data) { + mysql_mutex_unlock(&LOCK_internal_handler); + } + return false; + } for (Internal_error_handler *error_handler = m_internal_handler; error_handler; error_handler = error_handler->m_prev_internal_handler) { - if (error_handler->handle_condition(this, sql_errno, sqlstate, level, msg)) + if (error_handler->handle_condition(this, sql_errno, sqlstate, level, msg)) { + if (is_doing_parallel_copy_data) { + mysql_mutex_unlock(&LOCK_internal_handler); + } return true; + } + } + if (is_doing_parallel_copy_data) { + mysql_mutex_unlock(&LOCK_internal_handler); } return false; } Internal_error_handler *THD::pop_internal_handler() { + if (is_doing_parallel_copy_data) { + mysql_mutex_lock(&LOCK_internal_handler); + } + assert(m_internal_handler != nullptr); Internal_error_handler *popped_handler = m_internal_handler; m_internal_handler = m_internal_handler->m_prev_internal_handler; + + if (is_doing_parallel_copy_data) { + mysql_mutex_unlock(&LOCK_internal_handler); + } + return popped_handler; } @@ -1069,6 +1103,8 @@ Sql_condition *THD::raise_condition(uint sql_errno, const char *sqlstate, const char *msg, bool fatal_error) { DBUG_TRACE; + DBUG_EXECUTE_IF("thd_raise_condition_block", sleep(2);); + if (is_doing_parallel_copy_data) { mysql_mutex_lock(&LOCK_push_warning); } @@ -1561,6 +1597,7 @@ THD::~THD() { mysql_mutex_destroy(&LOCK_current_cond); mysql_mutex_destroy(&LOCK_group_replication_connection_mutex); mysql_mutex_destroy(&LOCK_push_warning); + mysql_mutex_destroy(&LOCK_internal_handler); mysql_cond_destroy(&COND_thr_lock); mysql_mutex_destroy(&m_thd_lock_done); diff --git a/sql/sql_class.h b/sql/sql_class.h index 2e3b8aad26f..b8ff138437b 100644 --- a/sql/sql_class.h +++ b/sql/sql_class.h @@ -1829,6 +1829,8 @@ class THD : public MDL_context_owner, mysql_mutex_t LOCK_push_warning; + mysql_mutex_t LOCK_internal_handler; + private: /** Type of current query: COM_STMT_PREPARE, COM_QUERY, etc. From c902926949ed18a5fb87857edf864f86d3cfc329 Mon Sep 17 00:00:00 2001 From: linusluo Date: Sun, 7 Jun 2026 17:44:04 +0800 Subject: [PATCH 09/14] [bugfix] issue#2284 Fix fts_trx create multiple times when executing parallel copy DDL (merge request !3052) Problem: ======== During parallel copy DDL execution, multithreaded concurrency may cause multiple threads to simultaneously reach the logic checking for fts_trx == nullptr. As a result, multiple threads may all determine that trx->fts_trx has not been created yet, leading to the fts_trx_create procedure being executed multiple times. Solution: ======== Use std::call_once to ensure fts_trx_create is executed by only one thread once. --story=140343413 MR!3052 reviewed by dorseywang. --- include/my_dbug.h | 4 ++ ...fts_trx_create_crash_parallel_copy_ddl.csi | 28 +++++++++++++ ...ts_trx_create_crash_parallel_copy_ddl.ncdb | 28 +++++++++++++ ...trx_create_crash_parallel_copy_ddl.ncdb_pq | 28 +++++++++++++ ..._trx_create_crash_parallel_copy_ddl.pq_csi | 28 +++++++++++++ ...ts_trx_create_crash_parallel_copy_ddl.test | 39 +++++++++++++++++++ mysys/dbug.cc | 31 +++++++++++++++ storage/innobase/fts/fts0fts.cc | 8 +++- storage/innobase/handler/handler0alter.cc | 18 +++++++++ storage/innobase/include/trx0trx.h | 1 + storage/innobase/row/row0mysql.cc | 8 +++- storage/innobase/trx/trx0trx.cc | 20 +++++++--- 12 files changed, 233 insertions(+), 8 deletions(-) create mode 100644 mysql-test/r/issue2284_fts_trx_create_crash_parallel_copy_ddl.csi create mode 100644 mysql-test/r/issue2284_fts_trx_create_crash_parallel_copy_ddl.ncdb create mode 100644 mysql-test/r/issue2284_fts_trx_create_crash_parallel_copy_ddl.ncdb_pq create mode 100644 mysql-test/r/issue2284_fts_trx_create_crash_parallel_copy_ddl.pq_csi create mode 100644 mysql-test/t/issue2284_fts_trx_create_crash_parallel_copy_ddl.test diff --git a/include/my_dbug.h b/include/my_dbug.h index 354c4feb8a0..7040f59246d 100644 --- a/include/my_dbug.h +++ b/include/my_dbug.h @@ -93,6 +93,10 @@ extern void _db_unlock_file_(void); extern FILE *_db_fp_(void); extern void _db_flush_(); +extern void parallel_reader_copy_dbug_keyword_list(CODE_STATE **dst_ptr, CODE_STATE **src_ptr); + +extern void parallel_reader_reset_dbug_keyword_list(CODE_STATE **cs_ptr); + #ifdef __cplusplus #if defined(__GNUC__) diff --git a/mysql-test/r/issue2284_fts_trx_create_crash_parallel_copy_ddl.csi b/mysql-test/r/issue2284_fts_trx_create_crash_parallel_copy_ddl.csi new file mode 100644 index 00000000000..86d001bc510 --- /dev/null +++ b/mysql-test/r/issue2284_fts_trx_create_crash_parallel_copy_ddl.csi @@ -0,0 +1,28 @@ +CREATE TABLE articles ( +id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, +title VARCHAR(200), +content TEXT, +FULLTEXT INDEX ft_index (title, content) WITH PARSER ngram +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; +drop procedure if exists populate; +create procedure populate(IN start INT, IN end INT) +begin +declare i int default start; +SET autocommit=0; +repeat +INSERT INTO articles (title, content) VALUES +('MySQL Full-Text Search', 'This article explains how to use full-text search in MySQL.'), +('Understanding MySQL', 'MySQL is a popular relational database management system.'), +('Advanced MySQL Techniques', 'Learn advanced techniques for optimizing MySQL performance.'); +set i=i+1; +until i=end +end repeat; +SET autocommit=1; +end | +CALL populate(0, 10000); +set session debug = '+d,sync_fts_trx_create'; +SET SESSION txsql_parallel_copy_ddl = ON; +ALTER TABLE articles ENGINE=innodb, algorithm=copy; +set session debug = '-d,sync_fts_trx_create'; +drop table articles; +drop procedure populate; diff --git a/mysql-test/r/issue2284_fts_trx_create_crash_parallel_copy_ddl.ncdb b/mysql-test/r/issue2284_fts_trx_create_crash_parallel_copy_ddl.ncdb new file mode 100644 index 00000000000..86d001bc510 --- /dev/null +++ b/mysql-test/r/issue2284_fts_trx_create_crash_parallel_copy_ddl.ncdb @@ -0,0 +1,28 @@ +CREATE TABLE articles ( +id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, +title VARCHAR(200), +content TEXT, +FULLTEXT INDEX ft_index (title, content) WITH PARSER ngram +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; +drop procedure if exists populate; +create procedure populate(IN start INT, IN end INT) +begin +declare i int default start; +SET autocommit=0; +repeat +INSERT INTO articles (title, content) VALUES +('MySQL Full-Text Search', 'This article explains how to use full-text search in MySQL.'), +('Understanding MySQL', 'MySQL is a popular relational database management system.'), +('Advanced MySQL Techniques', 'Learn advanced techniques for optimizing MySQL performance.'); +set i=i+1; +until i=end +end repeat; +SET autocommit=1; +end | +CALL populate(0, 10000); +set session debug = '+d,sync_fts_trx_create'; +SET SESSION txsql_parallel_copy_ddl = ON; +ALTER TABLE articles ENGINE=innodb, algorithm=copy; +set session debug = '-d,sync_fts_trx_create'; +drop table articles; +drop procedure populate; diff --git a/mysql-test/r/issue2284_fts_trx_create_crash_parallel_copy_ddl.ncdb_pq b/mysql-test/r/issue2284_fts_trx_create_crash_parallel_copy_ddl.ncdb_pq new file mode 100644 index 00000000000..86d001bc510 --- /dev/null +++ b/mysql-test/r/issue2284_fts_trx_create_crash_parallel_copy_ddl.ncdb_pq @@ -0,0 +1,28 @@ +CREATE TABLE articles ( +id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, +title VARCHAR(200), +content TEXT, +FULLTEXT INDEX ft_index (title, content) WITH PARSER ngram +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; +drop procedure if exists populate; +create procedure populate(IN start INT, IN end INT) +begin +declare i int default start; +SET autocommit=0; +repeat +INSERT INTO articles (title, content) VALUES +('MySQL Full-Text Search', 'This article explains how to use full-text search in MySQL.'), +('Understanding MySQL', 'MySQL is a popular relational database management system.'), +('Advanced MySQL Techniques', 'Learn advanced techniques for optimizing MySQL performance.'); +set i=i+1; +until i=end +end repeat; +SET autocommit=1; +end | +CALL populate(0, 10000); +set session debug = '+d,sync_fts_trx_create'; +SET SESSION txsql_parallel_copy_ddl = ON; +ALTER TABLE articles ENGINE=innodb, algorithm=copy; +set session debug = '-d,sync_fts_trx_create'; +drop table articles; +drop procedure populate; diff --git a/mysql-test/r/issue2284_fts_trx_create_crash_parallel_copy_ddl.pq_csi b/mysql-test/r/issue2284_fts_trx_create_crash_parallel_copy_ddl.pq_csi new file mode 100644 index 00000000000..86d001bc510 --- /dev/null +++ b/mysql-test/r/issue2284_fts_trx_create_crash_parallel_copy_ddl.pq_csi @@ -0,0 +1,28 @@ +CREATE TABLE articles ( +id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, +title VARCHAR(200), +content TEXT, +FULLTEXT INDEX ft_index (title, content) WITH PARSER ngram +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; +drop procedure if exists populate; +create procedure populate(IN start INT, IN end INT) +begin +declare i int default start; +SET autocommit=0; +repeat +INSERT INTO articles (title, content) VALUES +('MySQL Full-Text Search', 'This article explains how to use full-text search in MySQL.'), +('Understanding MySQL', 'MySQL is a popular relational database management system.'), +('Advanced MySQL Techniques', 'Learn advanced techniques for optimizing MySQL performance.'); +set i=i+1; +until i=end +end repeat; +SET autocommit=1; +end | +CALL populate(0, 10000); +set session debug = '+d,sync_fts_trx_create'; +SET SESSION txsql_parallel_copy_ddl = ON; +ALTER TABLE articles ENGINE=innodb, algorithm=copy; +set session debug = '-d,sync_fts_trx_create'; +drop table articles; +drop procedure populate; diff --git a/mysql-test/t/issue2284_fts_trx_create_crash_parallel_copy_ddl.test b/mysql-test/t/issue2284_fts_trx_create_crash_parallel_copy_ddl.test new file mode 100644 index 00000000000..552c0e230f3 --- /dev/null +++ b/mysql-test/t/issue2284_fts_trx_create_crash_parallel_copy_ddl.test @@ -0,0 +1,39 @@ +--source include/have_debug.inc + +CONNECT (con1,localhost,root,,); + +CREATE TABLE articles ( + id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, + title VARCHAR(200), + content TEXT, + FULLTEXT INDEX ft_index (title, content) WITH PARSER ngram +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; + +delimiter |; +drop procedure if exists populate; +create procedure populate(IN start INT, IN end INT) +begin +declare i int default start; +SET autocommit=0; +repeat +INSERT INTO articles (title, content) VALUES + ('MySQL Full-Text Search', 'This article explains how to use full-text search in MySQL.'), + ('Understanding MySQL', 'MySQL is a popular relational database management system.'), + ('Advanced MySQL Techniques', 'Learn advanced techniques for optimizing MySQL performance.'); +set i=i+1; +until i=end +end repeat; +SET autocommit=1; +end | +delimiter ;| + +CALL populate(0, 10000); + +set session debug = '+d,sync_fts_trx_create'; +SET SESSION txsql_parallel_copy_ddl = ON; + +ALTER TABLE articles ENGINE=innodb, algorithm=copy; + +set session debug = '-d,sync_fts_trx_create'; +drop table articles; +drop procedure populate; diff --git a/mysys/dbug.cc b/mysys/dbug.cc index bbf4fde6ba2..3170ccf2df4 100644 --- a/mysys/dbug.cc +++ b/mysys/dbug.cc @@ -1781,6 +1781,37 @@ int _db_keyword_(CODE_STATE *cs, const char *keyword, int strict) { return result; } +void parallel_reader_copy_dbug_keyword_list(CODE_STATE **dst_ptr, + CODE_STATE **src_ptr) { + if (*dst_ptr == nullptr || *src_ptr == nullptr) { + return; + } + + CODE_STATE *dst = *dst_ptr; + CODE_STATE *src = *src_ptr; + + if (dst->stack == &init_settings) { + PushState(dst); + assert(dst->stack->keywords == nullptr); + + dst->stack->keywords = ListCopy(src->stack->keywords); + dst->stack->flags |= DEBUG_ON; + } +} + +void parallel_reader_reset_dbug_keyword_list(CODE_STATE **cs_ptr) { + if (*cs_ptr == nullptr) { + return; + } + + CODE_STATE *cs = *cs_ptr; + + if (cs->stack != &init_settings) { + FreeState(cs, cs->stack, 1); + cs->stack= &init_settings; + } +} + /* * FUNCTION * diff --git a/storage/innobase/fts/fts0fts.cc b/storage/innobase/fts/fts0fts.cc index 3008ac05948..3711b47e0ed 100644 --- a/storage/innobase/fts/fts0fts.cc +++ b/storage/innobase/fts/fts0fts.cc @@ -29,6 +29,7 @@ this program; if not, write to the Free Software Foundation, Inc., ***********************************************************************/ #include +#include #include #include @@ -2639,7 +2640,12 @@ void fts_trx_add_op(trx_t *trx, dict_table_t *table, doc_id_t doc_id, fts_trx_table_t *stmt_ftt; if (!trx->fts_trx) { - trx->fts_trx = fts_trx_create(trx); + if (current_thd && current_thd->is_doing_parallel_copy_data) { + std::call_once(*trx->fts_trx_create_once_flag, + [&trx]() { trx->fts_trx = fts_trx_create(trx); }); + } else { + trx->fts_trx = fts_trx_create(trx); + } } tran_ftt = fts_trx_init(trx, table, trx->fts_trx->savepoints); diff --git a/storage/innobase/handler/handler0alter.cc b/storage/innobase/handler/handler0alter.cc index 0423ccc050c..a7c3b1b1612 100644 --- a/storage/innobase/handler/handler0alter.cc +++ b/storage/innobase/handler/handler0alter.cc @@ -2014,6 +2014,24 @@ int ha_innobase::parallel_copy_data_between_tables( else return DB_SUCCESS; }; + +#ifndef NDEBUG + CODE_STATE **thread_var_dbug = my_thread_var_dbug(); + + reader.set_start_callback( + [thread_var_dbug](Parallel_reader::Thread_ctx *thread_ctx) -> dberr_t { + parallel_reader_copy_dbug_keyword_list(my_thread_var_dbug(), + thread_var_dbug); + return DB_SUCCESS; + }); + + reader.set_finish_callback( + [](Parallel_reader::Thread_ctx *thread_ctx) -> dberr_t { + parallel_reader_reset_dbug_keyword_list(my_thread_var_dbug()); + return DB_SUCCESS; + }); +#endif /* !NDEBUG */ + err = reader.add_scan(trx, config, process_row); thd_start_parallel_copy_data(thd); diff --git a/storage/innobase/include/trx0trx.h b/storage/innobase/include/trx0trx.h index 9e4c995b978..10562a87fc4 100644 --- a/storage/innobase/include/trx0trx.h +++ b/storage/innobase/include/trx0trx.h @@ -1109,6 +1109,7 @@ struct trx_t { /*------------------------------*/ uint32_t flush_tables; /*!< if "covering" the FLUSH TABLES", count of tables being flushed. */ + std::once_flag *fts_trx_create_once_flag; /*------------------------------*/ bool internal; /*!< true if it is a system/internal diff --git a/storage/innobase/row/row0mysql.cc b/storage/innobase/row/row0mysql.cc index d4baa986f35..a3c293ceeec 100644 --- a/storage/innobase/row/row0mysql.cc +++ b/storage/innobase/row/row0mysql.cc @@ -2540,7 +2540,13 @@ static dberr_t row_insert_for_mysql_using_ins_graph(const byte *mysql_rec, if (table->skip_alter_undo) { if (trx->fts_trx == nullptr) { - trx->fts_trx = fts_trx_create(trx); + DBUG_EXECUTE_IF("sync_fts_trx_create", sleep(1);); + if (current_thd && current_thd->is_doing_parallel_copy_data) { + std::call_once(*trx->fts_trx_create_once_flag, + [&trx]() { trx->fts_trx = fts_trx_create(trx); }); + } else { + trx->fts_trx = fts_trx_create(trx); + } } fts_trx_table_t ftt; diff --git a/storage/innobase/trx/trx0trx.cc b/storage/innobase/trx/trx0trx.cc index 2dd516266e5..814c669c70f 100644 --- a/storage/innobase/trx/trx0trx.cc +++ b/storage/innobase/trx/trx0trx.cc @@ -257,6 +257,11 @@ static void trx_init(trx_t *trx) { trx->lock.hu_rec_id.m_space_id = UINT32_UNDEFINED; trx->lock.hu_rec_id.m_page_no = UINT32_UNDEFINED; trx->lock.hu_rec_id.m_heap_no = UINT32_UNDEFINED; + + if (trx->fts_trx_create_once_flag) { + ut::delete_(trx->fts_trx_create_once_flag); + } + trx->fts_trx_create_once_flag = ut::new_(); } /** For managing the life-cycle of the trx_t instance that we get @@ -272,7 +277,7 @@ struct TrxFactory { ut::zalloc_withkey() in Pool::Pool() which would not call the constructors of the trx_t members. */ new (trx) trx_t(); - + trx->read_view = new ReadView(); ut_a(!trx->rw_trx_hash_pins); @@ -296,7 +301,7 @@ struct TrxFactory { mutex_create(LATCH_ID_TRX, &trx->mutex); mutex_create(LATCH_ID_TRX_UNDO, &trx->undo_mutex); mutex_create(LATCH_ID_TRX_VIEW, &trx->view_mutex); - + lock_trx_alloc_locks(trx); } @@ -324,6 +329,9 @@ struct TrxFactory { ut_a(UT_LIST_GET_LEN(trx->lock.trx_locks) == 0); + ut::delete_(trx->fts_trx_create_once_flag); + trx->fts_trx_create_once_flag = nullptr; + ut::delete_(trx->xid); ut::free(trx->detailed_error); @@ -1055,7 +1063,7 @@ static void trx_resurrect(trx_rseg_t *rseg) { trx_sys->rw_trx_hash.insert(trx); trx_sys->rw_trx_hash.put_pins(trx); } - + trx_resurrect_table_ids(trx, &trx->rsegs.m_redo, undo); } } @@ -1379,7 +1387,7 @@ static void trx_start_low( /* Temporary rseg is assigned only if the transaction updates a temporary table */ DEBUG_SYNC_C("trx_sys_before_assign_id"); - + trx_sys->register_rw(trx); trx_sys_rw_trx_add(trx); @@ -1395,7 +1403,7 @@ static void trx_start_low( if (read_write) { trx_sys->register_rw(trx); trx_sys_rw_trx_add(trx); - } + } } } @@ -3432,7 +3440,7 @@ void trx_set_rw_mode(trx_t *trx) /*!< in/out: transaction that is RW */ ut_ad(trx->rsegs.m_redo.rseg != nullptr); DEBUG_SYNC_C("trx_sys_before_assign_id"); - + trx_sys->register_rw(trx); /* So that we can see our own changes. */ From ccac30e7c4165767b586603885471b1075dcd5c6 Mon Sep 17 00:00:00 2001 From: linusluo Date: Wed, 9 Apr 2025 08:48:40 +0000 Subject: [PATCH 10/14] [bugfix] followup issue#2284 Fill in the missing dbug settings member variables in parallel copy DDL (merge request !3080) Problem: ======== When we create new dbug settings in parallel copy DDL, filling in member variables out_file, functions, p_functions and processes are missing. Solution: ======== Use member variables of init_settings to fill in the new dbug settings we have created. --story=75696212 MR!3080 reviewed by dorseywang. TAPD: --task=075696212 --- mysys/dbug.cc | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/mysys/dbug.cc b/mysys/dbug.cc index 3170ccf2df4..b5d0214613b 100644 --- a/mysys/dbug.cc +++ b/mysys/dbug.cc @@ -1793,6 +1793,14 @@ void parallel_reader_copy_dbug_keyword_list(CODE_STATE **dst_ptr, if (dst->stack == &init_settings) { PushState(dst); assert(dst->stack->keywords == nullptr); + native_rw_rdlock(&THR_LOCK_init_settings); + + dst->stack->out_file = stderr; + dst->stack->functions = ListCopy(init_settings.functions); + dst->stack->p_functions = ListCopy(init_settings.p_functions); + dst->stack->processes = ListCopy(init_settings.processes); + + native_rw_unlock(&THR_LOCK_init_settings); dst->stack->keywords = ListCopy(src->stack->keywords); dst->stack->flags |= DEBUG_ON; From a0aec0146889d2dd57fee804defc655c4b5021af Mon Sep 17 00:00:00 2001 From: linusluo Date: Mon, 9 Feb 2026 17:13:32 +0800 Subject: [PATCH 11/14] [bugfix] issue#3602 Fix memory leak in Parallel Copy DDL --story=130221137 --- sql/sql_table.cc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sql/sql_table.cc b/sql/sql_table.cc index 1d87d9ba39e..c26b4741e1e 100644 --- a/sql/sql_table.cc +++ b/sql/sql_table.cc @@ -18947,6 +18947,8 @@ static int copy_data_between_tables( &ha_copy_alter_info, create, found_count); + delete ha_copy_alter_info.handler_ctx; + if (ha_copy_alter_info.fallback) { if (iterator->Init()) { error= 1; From 3bd7a70b4ec9adbaf864913c10c907fed99421ec Mon Sep 17 00:00:00 2001 From: linusluo Date: Mon, 9 Feb 2026 19:55:35 +0800 Subject: [PATCH 12/14] [bugfix] issue#3628 Fix concurrency issues with trx->error_state during parallel copy DDL MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Description: ======== During parallel copy DDL, since the same trx is shared among multiple threads, concurrent operations on trx->error_state pose a concurrency issue. This variable is responsible for propagating dberr_t across contexts. So during parallel copy DDL, all operations on this variable should be redirected to current_thd->trx_error_state — which is a thread_local variable. --bug=153702123 --- ...pcddl_trx_state_concurrency_issue3628.ncdb | 21 +++++++ ...dl_trx_state_concurrency_issue3628.ncdb_pq | 21 +++++++ ...ddl_trx_state_concurrency_issue3628.result | 21 +++++++ ...pcddl_trx_state_concurrency_issue3628.test | 29 ++++++++++ sql/sql_class.cc | 3 + sql/sql_class.h | 2 + sql/sql_thd_internal_api.cc | 11 ++++ sql/sql_thd_internal_api.h | 5 ++ storage/innobase/api/api0api.cc | 6 +- storage/innobase/api/api0misc.cc | 12 ++-- storage/innobase/ddl/ddl0ddl.cc | 4 +- storage/innobase/ddl/ddl0loader.cc | 2 +- storage/innobase/dict/dict0stats.cc | 4 +- storage/innobase/fts/fts0fts.cc | 10 ++-- storage/innobase/fts/fts0opt.cc | 8 +-- storage/innobase/fts/fts0que.cc | 6 +- storage/innobase/fts/fts0sql.cc | 2 +- storage/innobase/handler/handler0alter.cc | 16 +++--- storage/innobase/handler/i_s.cc | 2 +- storage/innobase/include/trx0trx.h | 21 ++++++- storage/innobase/lock/lock0lock.cc | 6 +- storage/innobase/lock/lock0wait.cc | 20 +++---- storage/innobase/que/que0que.cc | 28 +++++----- storage/innobase/row/row0import.cc | 2 +- storage/innobase/row/row0ins.cc | 10 ++-- storage/innobase/row/row0merge.cc | 6 +- storage/innobase/row/row0mysql.cc | 56 ++++++++++--------- storage/innobase/row/row0px.cc | 2 +- storage/innobase/row/row0sel.cc | 10 ++-- storage/innobase/row/row0undo.cc | 2 +- storage/innobase/row/row0upd.cc | 2 +- storage/innobase/trx/trx0roll.cc | 10 ++-- storage/innobase/trx/trx0trx.cc | 10 ++-- 33 files changed, 251 insertions(+), 119 deletions(-) create mode 100644 mysql-test/r/pcddl_trx_state_concurrency_issue3628.ncdb create mode 100644 mysql-test/r/pcddl_trx_state_concurrency_issue3628.ncdb_pq create mode 100644 mysql-test/r/pcddl_trx_state_concurrency_issue3628.result create mode 100644 mysql-test/t/pcddl_trx_state_concurrency_issue3628.test diff --git a/mysql-test/r/pcddl_trx_state_concurrency_issue3628.ncdb b/mysql-test/r/pcddl_trx_state_concurrency_issue3628.ncdb new file mode 100644 index 00000000000..e0e322c013d --- /dev/null +++ b/mysql-test/r/pcddl_trx_state_concurrency_issue3628.ncdb @@ -0,0 +1,21 @@ +SET SESSION txsql_parallel_copy_ddl=ON; +SET debug="+d,sync_row_mysql_handle_errors"; +CREATE TABLE t1(id int primary key, v int); +drop procedure if exists insert_t1; +create procedure insert_t1(IN start INT, IN end INT) +begin +declare i int default start; +SET autocommit=0; +repeat +INSERT INTO t1 VALUES (i, i); +set i=i+1; +until i=end +end repeat; +SET autocommit=1; +end | +CALL insert_t1(0, 20000); +UPDATE t1 SET v = 2 WHERE id = 9999; +ALTER TABLE t1 ADD UNIQUE INDEX k_1(v), algorithm=copy; +ERROR 23000: Can't write; duplicate key in table '#sql-394247_31' +DROP TABLE t1; +DROP PROCEDURE insert_t1; diff --git a/mysql-test/r/pcddl_trx_state_concurrency_issue3628.ncdb_pq b/mysql-test/r/pcddl_trx_state_concurrency_issue3628.ncdb_pq new file mode 100644 index 00000000000..a477e763cca --- /dev/null +++ b/mysql-test/r/pcddl_trx_state_concurrency_issue3628.ncdb_pq @@ -0,0 +1,21 @@ +SET SESSION txsql_parallel_copy_ddl=ON; +SET debug="+d,sync_row_mysql_handle_errors"; +CREATE TABLE t1(id int primary key, v int); +drop procedure if exists insert_t1; +create procedure insert_t1(IN start INT, IN end INT) +begin +declare i int default start; +SET autocommit=0; +repeat +INSERT INTO t1 VALUES (i, i); +set i=i+1; +until i=end +end repeat; +SET autocommit=1; +end | +CALL insert_t1(0, 20000); +UPDATE t1 SET v = 2 WHERE id = 9999; +ALTER TABLE t1 ADD UNIQUE INDEX k_1(v), algorithm=copy; +ERROR 23000: Can't write; duplicate key in table '#sql-395365_31' +DROP TABLE t1; +DROP PROCEDURE insert_t1; diff --git a/mysql-test/r/pcddl_trx_state_concurrency_issue3628.result b/mysql-test/r/pcddl_trx_state_concurrency_issue3628.result new file mode 100644 index 00000000000..9775f204e32 --- /dev/null +++ b/mysql-test/r/pcddl_trx_state_concurrency_issue3628.result @@ -0,0 +1,21 @@ +SET SESSION txsql_parallel_copy_ddl=ON; +SET debug="+d,sync_row_mysql_handle_errors"; +CREATE TABLE t1(id int primary key, v int); +drop procedure if exists insert_t1; +create procedure insert_t1(IN start INT, IN end INT) +begin +declare i int default start; +SET autocommit=0; +repeat +INSERT INTO t1 VALUES (i, i); +set i=i+1; +until i=end +end repeat; +SET autocommit=1; +end | +CALL insert_t1(0, 20000); +UPDATE t1 SET v = 2 WHERE id = 9999; +ALTER TABLE t1 ADD UNIQUE INDEX k_1(v), algorithm=copy; +ERROR 23000: Can't write; duplicate key in table '#sql-431ed_e' +DROP TABLE t1; +DROP PROCEDURE insert_t1; diff --git a/mysql-test/t/pcddl_trx_state_concurrency_issue3628.test b/mysql-test/t/pcddl_trx_state_concurrency_issue3628.test new file mode 100644 index 00000000000..ed1e74459fa --- /dev/null +++ b/mysql-test/t/pcddl_trx_state_concurrency_issue3628.test @@ -0,0 +1,29 @@ +SET SESSION txsql_parallel_copy_ddl=ON; +SET debug="+d,sync_row_mysql_handle_errors"; +CREATE TABLE t1(id int primary key, v int); + +delimiter |; +drop procedure if exists insert_t1; +create procedure insert_t1(IN start INT, IN end INT) +begin +declare i int default start; +SET autocommit=0; +repeat +INSERT INTO t1 VALUES (i, i); +set i=i+1; +until i=end +end repeat; +SET autocommit=1; +end | +delimiter ;| + +CALL insert_t1(0, 20000); + +# Simulate duplicate key. +UPDATE t1 SET v = 2 WHERE id = 9999; + +--error 1022 +ALTER TABLE t1 ADD UNIQUE INDEX k_1(v), algorithm=copy; + +DROP TABLE t1; +DROP PROCEDURE insert_t1; diff --git a/sql/sql_class.cc b/sql/sql_class.cc index ef35aa42cf3..c904730fc64 100644 --- a/sql/sql_class.cc +++ b/sql/sql_class.cc @@ -3927,3 +3927,6 @@ bool THD::can_delay_commit() const { } /* Changes from TXSQL end. */ + +// 定义 THD 类的静态成员变量 +thread_local uint32 THD::trx_error_state = 10; diff --git a/sql/sql_class.h b/sql/sql_class.h index b8ff138437b..2c185304530 100644 --- a/sql/sql_class.h +++ b/sql/sql_class.h @@ -1827,6 +1827,8 @@ class THD : public MDL_context_owner, public: bool is_doing_parallel_copy_data = false; + static thread_local uint32 trx_error_state; + mysql_mutex_t LOCK_push_warning; mysql_mutex_t LOCK_internal_handler; diff --git a/sql/sql_thd_internal_api.cc b/sql/sql_thd_internal_api.cc index 0fc371eac88..c388aa0070d 100644 --- a/sql/sql_thd_internal_api.cc +++ b/sql/sql_thd_internal_api.cc @@ -433,3 +433,14 @@ void thd_end_parallel_copy_data(THD *thd) { thd->is_doing_parallel_copy_data = false; } +bool thd_is_parallel_copy_data(THD *thd) { + return thd->is_doing_parallel_copy_data; +} + +uint32 thd_get_trx_error_state(THD *thd) { + return thd->trx_error_state; +} + +void thd_set_trx_error_state(THD *thd, uint32 trx_error_state) { + thd->trx_error_state = trx_error_state; +} diff --git a/sql/sql_thd_internal_api.h b/sql/sql_thd_internal_api.h index a8d68ecb096..8ab0c30a457 100644 --- a/sql/sql_thd_internal_api.h +++ b/sql/sql_thd_internal_api.h @@ -327,4 +327,9 @@ void thd_start_parallel_copy_data(THD *thd); void thd_end_parallel_copy_data(THD *thd); +bool thd_is_parallel_copy_data(THD *thd); + +uint32 thd_get_trx_error_state(THD *thd); + +void thd_set_trx_error_state(THD *thd, uint32 trx_error_state); #endif // SQL_THD_INTERNAL_API_INCLUDED diff --git a/storage/innobase/api/api0api.cc b/storage/innobase/api/api0api.cc index 7702e32e306..4c7f9d1694b 100644 --- a/storage/innobase/api/api0api.cc +++ b/storage/innobase/api/api0api.cc @@ -1030,7 +1030,7 @@ static inline ib_err_t ib_insert_row_with_lock_retry( row_ins_step(thr); - err = trx->error_state; + err = get_trx_error_state(trx); if (err != DB_SUCCESS) { que_thr_stop_for_mysql(thr); @@ -1336,7 +1336,7 @@ static inline ib_err_t ib_update_row_with_lock_retry( row_upd_step(thr); - err = trx->error_state; + err = get_trx_error_state(trx); if (err != DB_SUCCESS) { que_thr_stop_for_mysql(thr); @@ -1412,7 +1412,7 @@ static inline ib_err_t ib_execute_update_query_graph( } } else if (err == DB_RECORD_NOT_FOUND) { - trx->error_state = DB_SUCCESS; + set_trx_error_state(trx, DB_SUCCESS); } trx->op_info = ""; diff --git a/storage/innobase/api/api0misc.cc b/storage/innobase/api/api0misc.cc index 5d7b0252dde..608b4bcabc8 100644 --- a/storage/innobase/api/api0misc.cc +++ b/storage/innobase/api/api0misc.cc @@ -66,11 +66,11 @@ bool ib_handle_errors(dberr_t *new_err, trx_t *trx, que_thr_t *thr, trx_savept_t *savept, bool is_sdi) { dberr_t err; handle_new_error: - err = trx->error_state; + err = get_trx_error_state(trx); ut_a(err != DB_SUCCESS); - trx->error_state = DB_SUCCESS; + set_trx_error_state(trx, DB_SUCCESS); switch (err) { case DB_LOCK_WAIT_TIMEOUT: @@ -97,7 +97,7 @@ bool ib_handle_errors(dberr_t *new_err, trx_t *trx, que_thr_t *thr, case DB_LOCK_WAIT: lock_wait_suspend_thread(thr); - if (trx->error_state != DB_SUCCESS) { + if (get_trx_error_state(trx) != DB_SUCCESS) { que_thr_stop_for_mysql(thr); goto handle_new_error; @@ -126,13 +126,13 @@ bool ib_handle_errors(dberr_t *new_err, trx_t *trx, que_thr_t *thr, ut_error; } - if (trx->error_state != DB_SUCCESS) { - *new_err = trx->error_state; + if (get_trx_error_state(trx) != DB_SUCCESS) { + *new_err = get_trx_error_state(trx); } else { *new_err = err; } - trx->error_state = DB_SUCCESS; + set_trx_error_state(trx, DB_SUCCESS); return false; } diff --git a/storage/innobase/ddl/ddl0ddl.cc b/storage/innobase/ddl/ddl0ddl.cc index 246cc2956c5..03965f24141 100644 --- a/storage/innobase/ddl/ddl0ddl.cc +++ b/storage/innobase/ddl/ddl0ddl.cc @@ -230,7 +230,7 @@ dict_index_t *create_index(trx_t *trx, dict_table_t *table, trx_is_strict(trx)); if (err != DB_SUCCESS) { - trx->error_state = err; + set_trx_error_state(trx, err); dict_sys_mutex_enter(); return nullptr; } @@ -248,7 +248,7 @@ dict_index_t *create_index(trx_t *trx, dict_table_t *table, fts_cache_index_cache_remove(table, index); } - trx->error_state = err; + set_trx_error_state(trx, err); return nullptr; } diff --git a/storage/innobase/ddl/ddl0loader.cc b/storage/innobase/ddl/ddl0loader.cc index 9446c9e6397..025b9d3d3e5 100644 --- a/storage/innobase/ddl/ddl0loader.cc +++ b/storage/innobase/ddl/ddl0loader.cc @@ -482,7 +482,7 @@ dberr_t Loader::build_all() noexcept { DBUG_EXECUTE_IF("ib_build_indexes_too_many_concurrent_trxs", err = DB_TOO_MANY_CONCURRENT_TRXS; - m_ctx.m_trx->error_state = err;); + set_trx_error_state(m_ctx.m_trx, err);); if (m_ctx.m_fts.m_ptr != nullptr) { /* Clean up FTS psort related resource */ diff --git a/storage/innobase/dict/dict0stats.cc b/storage/innobase/dict/dict0stats.cc index 53813c34d01..8f2a95c9dc2 100644 --- a/storage/innobase/dict/dict0stats.cc +++ b/storage/innobase/dict/dict0stats.cc @@ -199,7 +199,7 @@ static dberr_t dict_stats_exec_sql(pars_info_t *pinfo, const char *sql, DBUG_EXECUTE_IF( "stats_index_error", if (!trx_started) { err = DB_STATS_DO_NOT_EXIST; - trx->error_state = DB_STATS_DO_NOT_EXIST; + set_trx_error_state(trx, DB_STATS_DO_NOT_EXIST); }); if (!trx_started && err == DB_SUCCESS) { @@ -214,7 +214,7 @@ static dberr_t dict_stats_exec_sql(pars_info_t *pinfo, const char *sql, trx_rollback_to_savepoint(trx, nullptr); trx->dict_operation_lock_mode = 0; trx->op_info = ""; - ut_a(trx->error_state == DB_SUCCESS); + ut_a(get_trx_error_state(trx) == DB_SUCCESS); } if (trx_started) { diff --git a/storage/innobase/fts/fts0fts.cc b/storage/innobase/fts/fts0fts.cc index 3711b47e0ed..8a35cd26db3 100644 --- a/storage/innobase/fts/fts0fts.cc +++ b/storage/innobase/fts/fts0fts.cc @@ -451,7 +451,7 @@ static bool fts_load_user_stopword( ib::warn(ER_IB_MSG_462) << "Lock wait timeout reading user" " stopword table. Retrying!"; - trx->error_state = DB_SUCCESS; + set_trx_error_state(trx, DB_SUCCESS); } else { ib::error(ER_IB_MSG_463) << "Error '" << ut_strerr(error) << "' while reading user stopword" @@ -1850,7 +1850,7 @@ static dict_table_t *fts_create_one_common_table(trx_t *trx, } if (error != DB_SUCCESS) { - trx->error_state = error; + set_trx_error_state(trx, error); new_table = nullptr; ib::warn(ER_IB_MSG_465) << "Failed to create FTS common table " << fts_table_name; @@ -2038,7 +2038,7 @@ static dict_table_t *fts_create_one_index_table(trx_t *trx, } if (error != DB_SUCCESS) { - trx->error_state = error; + set_trx_error_state(trx, error); new_table = nullptr; ib::warn(ER_IB_MSG_466) << "Failed to create FTS index table " << table_name; @@ -2640,7 +2640,7 @@ void fts_trx_add_op(trx_t *trx, dict_table_t *table, doc_id_t doc_id, fts_trx_table_t *stmt_ftt; if (!trx->fts_trx) { - if (current_thd && current_thd->is_doing_parallel_copy_data) { + if (current_thd && thd_is_parallel_copy_data(current_thd)) { std::call_once(*trx->fts_trx_create_once_flag, [&trx]() { trx->fts_trx = fts_trx_create(trx); }); } else { @@ -5018,7 +5018,7 @@ ulint fts_get_rows_count(fts_table_t *fts_table) /*!< in: fts table to read */ ib::warn(ER_IB_MSG_478) << "lock wait timeout reading" " FTS table. Retrying!"; - trx->error_state = DB_SUCCESS; + set_trx_error_state(trx, DB_SUCCESS); } else { ib::error(ER_IB_MSG_479) << "(" << ut_strerr(error) << ") while reading FTS table."; diff --git a/storage/innobase/fts/fts0opt.cc b/storage/innobase/fts/fts0opt.cc index 59148e11888..e8ced9ebfdc 100644 --- a/storage/innobase/fts/fts0opt.cc +++ b/storage/innobase/fts/fts0opt.cc @@ -527,7 +527,7 @@ dberr_t fts_index_fetch_nodes( ib::warn(ER_IB_MSG_486) << "lock wait timeout reading" " FTS index. Retrying!"; - trx->error_state = DB_SUCCESS; + set_trx_error_state(trx, DB_SUCCESS); } else { ib::error(ER_IB_MSG_487) << "(" << ut_strerr(error) << ") while reading FTS index."; @@ -832,7 +832,7 @@ static void fts_zip_deflate_end( deflateEnd(zip->zp); fts_zip_init(zip); - optim->trx->error_state = DB_SUCCESS; + set_trx_error_state(optim->trx, DB_SUCCESS); } else { ib::error(ER_IB_MSG_490) << "(" << ut_strerr(error) << ") while reading document."; @@ -1673,11 +1673,11 @@ static void fts_optimize_words( ib::warn(ER_IB_MSG_495) << "Lock wait timeout during optimize." " Retrying!"; - trx->error_state = DB_SUCCESS; + set_trx_error_state(trx, DB_SUCCESS); } else if (error == DB_DEADLOCK) { ib::warn(ER_IB_MSG_496) << "Deadlock during optimize. Retrying!"; - trx->error_state = DB_SUCCESS; + set_trx_error_state(trx, DB_SUCCESS); } else { optim->done = true; /* Exit the loop. */ } diff --git a/storage/innobase/fts/fts0que.cc b/storage/innobase/fts/fts0que.cc index ae4bfe180cd..6cb265b5300 100644 --- a/storage/innobase/fts/fts0que.cc +++ b/storage/innobase/fts/fts0que.cc @@ -2018,7 +2018,7 @@ fts_query_find_term( ib::warn(ER_IB_MSG_506) << "lock wait timeout reading FTS" " index. Retrying!"; - trx->error_state = DB_SUCCESS; + set_trx_error_state(trx, DB_SUCCESS); } else { ib::error(ER_IB_MSG_507) << error << " while reading FTS index."; @@ -2135,7 +2135,7 @@ fts_query_total_docs_containing_term( ib::warn(ER_IB_MSG_508) << "lock wait timeout reading FTS" " index. Retrying!"; - trx->error_state = DB_SUCCESS; + set_trx_error_state(trx, DB_SUCCESS); } else { ib::error(ER_IB_MSG_509) << error << " while reading FTS index."; @@ -2216,7 +2216,7 @@ fts_query_terms_in_document( ib::warn(ER_IB_MSG_510) << "lock wait timeout reading FTS" " doc id table. Retrying!"; - trx->error_state = DB_SUCCESS; + set_trx_error_state(trx, DB_SUCCESS); } else { ib::error(ER_IB_MSG_511) << error << " while reading FTS" " doc id table."; diff --git a/storage/innobase/fts/fts0sql.cc b/storage/innobase/fts/fts0sql.cc index d68ec767fe7..a993add095f 100644 --- a/storage/innobase/fts/fts0sql.cc +++ b/storage/innobase/fts/fts0sql.cc @@ -256,7 +256,7 @@ dberr_t fts_eval_sql(trx_t *trx, /*!< in: transaction */ que_run_threads(thr); - return (trx->error_state); + return (get_trx_error_state(trx)); } /** Construct the column specification part of the SQL string for selecting the diff --git a/storage/innobase/handler/handler0alter.cc b/storage/innobase/handler/handler0alter.cc index a7c3b1b1612..bdecaf02390 100644 --- a/storage/innobase/handler/handler0alter.cc +++ b/storage/innobase/handler/handler0alter.cc @@ -5466,7 +5466,7 @@ template add_key_nums[a] = index_defs[a].m_key_number; if (!ctx->add_index[a]) { - error = ctx->trx->error_state; + error = get_trx_error_state(ctx->trx); assert(error != DB_SUCCESS); goto error_handling; } @@ -5679,7 +5679,7 @@ template error_handled: ctx->prebuilt->trx->error_index = nullptr; - ctx->trx->error_state = DB_SUCCESS; + set_trx_error_state(ctx->trx, DB_SUCCESS); if (!dict_locked) { row_mysql_lock_data_dictionary(ctx->prebuilt->trx, UT_LOCATION_HERE); @@ -6909,7 +6909,7 @@ bool ha_innobase::inplace_alter_table_impl(TABLE *altered_table, /* prebuilt->table->n_ref_count can be anything here, given that we hold at most a shared lock on the table. */ m_prebuilt->trx->error_index = nullptr; - ctx->trx->error_state = DB_SUCCESS; + set_trx_error_state(ctx->trx, DB_SUCCESS); return true; }; @@ -7338,7 +7338,7 @@ but do not touch the data dictionary cache. } DBUG_EXECUTE_IF("ib_drop_foreign_error", my_error_innodb(DB_OUT_OF_FILE_SPACE, table_name, 0); - trx->error_state = DB_SUCCESS; return true;); + set_trx_error_state(trx, DB_SUCCESS); return true;); return false; } @@ -7626,7 +7626,7 @@ when rebuilding the table. } DBUG_EXECUTE_IF("ib_rename_column_error", my_error_innodb(DB_OUT_OF_FILE_SPACE, table_name, 0); - trx->error_state = DB_SUCCESS; trx->op_info = ""; + set_trx_error_state(trx, DB_SUCCESS); trx->op_info = ""; return true;); DBUG_EXECUTE_IF("ib_ddl_crash_before_rename", DBUG_SUICIDE();); @@ -7777,17 +7777,17 @@ the table. DBUG_EXECUTE_IF("ib_rename_column_error", my_error_innodb(DB_OUT_OF_FILE_SPACE, table_name, 0); - trx->error_state = DB_SUCCESS; trx->op_info = ""; + set_trx_error_state(trx, DB_SUCCESS); trx->op_info = ""; return true;); DBUG_EXECUTE_IF("ib_resize_column_error", my_error_innodb(DB_OUT_OF_FILE_SPACE, table_name, 0); - trx->error_state = DB_SUCCESS; trx->op_info = ""; + set_trx_error_state(trx, DB_SUCCESS); trx->op_info = ""; return true;); DBUG_EXECUTE_IF( "ib_rename_index_fail1", my_error_innodb(DB_DEADLOCK, table_name, 0); - trx->error_state = DB_SUCCESS; trx->op_info = ""; return true;); + set_trx_error_state(trx, DB_SUCCESS); trx->op_info = ""; return true;); return false; } diff --git a/storage/innobase/handler/i_s.cc b/storage/innobase/handler/i_s.cc index 73602efa129..a414e5eb96d 100644 --- a/storage/innobase/handler/i_s.cc +++ b/storage/innobase/handler/i_s.cc @@ -3106,7 +3106,7 @@ static dberr_t i_s_fts_index_table_fill_selected( ib::warn(ER_IB_MSG_601) << "Lock wait timeout reading" " FTS index. Retrying!"; - trx->error_state = DB_SUCCESS; + set_trx_error_state(trx, DB_SUCCESS); } else { ib::error(ER_IB_MSG_602) << "Error occurred while reading" " FTS index: " diff --git a/storage/innobase/include/trx0trx.h b/storage/innobase/include/trx0trx.h index 10562a87fc4..6bfee1473e9 100644 --- a/storage/innobase/include/trx0trx.h +++ b/storage/innobase/include/trx0trx.h @@ -56,6 +56,7 @@ this program; if not, write to the Free Software Foundation, Inc., #include "srv0srv.h" #include "dict0mem.h" +#include "sql_thd_internal_api.h" // Forward declaration struct mtr_t; @@ -1125,7 +1126,7 @@ struct trx_t { const char *start_file; /*!< Filename where it was started */ #endif /* UNIV_DEBUG */ - std::atomic n_ref; /*!< Count of references, protected + std::atomic n_ref; /*!< Count of references, protected by trx_t::mutex. We can't release the locks nor commit the transaction until this reference is 0. We can change @@ -1207,7 +1208,7 @@ struct trx_t { @return true iff in this transaction's isolation level locks on records which do not match the WHERE clause are released */ bool releases_non_matching_rows() const { return skip_gap_locks(); } - + inline uint64_t get_hash_val() const { return (reinterpret_cast(this)) >> 4; } @@ -1669,6 +1670,22 @@ class TrxInInnoDB { trx_t *m_trx; }; +static inline void set_trx_error_state(trx_t *trx, dberr_t error_state) { + if (UNIV_UNLIKELY(current_thd && thd_is_parallel_copy_data(current_thd))) { + thd_set_trx_error_state(current_thd, static_cast(error_state)); + } else { + trx->error_state = error_state; + } +} + +static inline dberr_t get_trx_error_state(const trx_t *trx) { + if (UNIV_UNLIKELY(current_thd && thd_is_parallel_copy_data(current_thd))) { + return (dberr_t)thd_get_trx_error_state(current_thd); + } else { + return trx->error_state; + } +} + /** Check if transaction is internal XA transaction @param[in] trx transaction @return true, iff internal XA transaction. */ diff --git a/storage/innobase/lock/lock0lock.cc b/storage/innobase/lock/lock0lock.cc index eee2853ca03..c0f94bceb94 100644 --- a/storage/innobase/lock/lock0lock.cc +++ b/storage/innobase/lock/lock0lock.cc @@ -2234,7 +2234,7 @@ static const lock_t *lock_rec_has_to_wait_for_granted( for (size_t i = new_granted_index; i < granted.size(); ++i) { const auto granted_lock = granted[i]; - ut_ad(granted_lock->trx->error_state != DB_DEADLOCK); + ut_ad(get_trx_error_state(granted_lock->trx) != DB_DEADLOCK); ut_ad(!granted_lock->trx->lock.was_chosen_as_deadlock_victim); if (lock_has_to_wait(wait_lock, granted_lock)) { @@ -2307,7 +2307,7 @@ static void lock_rec_grant_by_heap_no(lock_t *in_lock, ulint heap_no) { } ut_d(seen_waiting_lock = true); const auto trx = lock->trx; - if (trx->error_state == DB_DEADLOCK || + if (get_trx_error_state(trx) == DB_DEADLOCK || trx->lock.was_chosen_as_deadlock_victim) { return (true); } @@ -4387,7 +4387,7 @@ dberr_t lock_table_for_trx(dict_table_t *table, trx_t *trx, err = lock_table(0, table, mode, thr); - trx->error_state = err; + set_trx_error_state(trx, err); if (err == DB_SUCCESS) { que_thr_stop_for_mysql_no_error(thr, trx); diff --git a/storage/innobase/lock/lock0wait.cc b/storage/innobase/lock/lock0wait.cc index 51956e36988..bd0e0a8ad90 100644 --- a/storage/innobase/lock/lock0wait.cc +++ b/storage/innobase/lock/lock0wait.cc @@ -266,7 +266,7 @@ lock_wait_in_hot_row_update_queue(que_thr_t *thr) trx_mutex_enter(trx); - trx->error_state = DB_SUCCESS; + set_trx_error_state(trx, DB_SUCCESS); slot = lock_wait_table_reserve_slot(thr, std::chrono::seconds{100000000}); @@ -342,7 +342,7 @@ lock_wait_in_hot_row_update_queue(que_thr_t *thr) trx->hot_update_status = HOT_UPDATE_STATUS_RUNNING; trx_mutex_exit(trx); if (trx_is_interrupted(trx)) { - trx->error_state = DB_INTERRUPTED; + set_trx_error_state(trx, DB_INTERRUPTED); } } @@ -369,7 +369,7 @@ void lock_wait_suspend_thread(que_thr_t *thr) { trx_mutex_enter(trx); - trx->error_state = DB_SUCCESS; + set_trx_error_state(trx, DB_SUCCESS); if (thr->state == QUE_THR_RUNNING) { ut_ad(thr->is_active); @@ -378,7 +378,7 @@ void lock_wait_suspend_thread(que_thr_t *thr) { was chosen as a deadlock victim: no need to suspend */ if (trx->lock.was_chosen_as_deadlock_victim) { - trx->error_state = DB_DEADLOCK; + set_trx_error_state(trx, DB_DEADLOCK); trx->lock.was_chosen_as_deadlock_victim = false; ut_d(trx->lock.in_rollback = true); @@ -492,17 +492,17 @@ void lock_wait_suspend_thread(que_thr_t *thr) { } /* The transaction is chosen as deadlock victim during sleep. */ - if (trx->error_state == DB_DEADLOCK) { + if (get_trx_error_state(trx) == DB_DEADLOCK) { ut_d(trx->lock.in_rollback = true); return; } - if (trx->error_state == DB_LOCK_WAIT_TIMEOUT) { + if (get_trx_error_state(trx) == DB_LOCK_WAIT_TIMEOUT) { MONITOR_INC(MONITOR_TIMEOUT); } if (trx_is_interrupted(trx)) { - trx->error_state = DB_INTERRUPTED; + set_trx_error_state(trx, DB_INTERRUPTED); } } @@ -561,7 +561,7 @@ static void lock_wait_release_thread_if_suspended(que_thr_t *thr) { if (thr->slot != nullptr && thr->slot->in_use && thr->slot->thr == thr) { if (trx->lock.was_chosen_as_deadlock_victim) { - trx->error_state = DB_DEADLOCK; + set_trx_error_state(trx, DB_DEADLOCK); trx->lock.was_chosen_as_deadlock_victim = false; ut_d(trx->lock.in_rollback = true); @@ -636,8 +636,8 @@ static void lock_wait_try_cancel(trx_t *trx, bool timeout) { current query. We set error_state to DB_DEADLOCK only: 1) before the transaction reserves a slot. But, we know it's in a slot. 2) when wait_lock is already set to nullptr. But, it's not nullptr. */ - ut_ad(trx->error_state != DB_DEADLOCK); - trx->error_state = DB_LOCK_WAIT_TIMEOUT; + ut_ad(get_trx_error_state(trx) != DB_DEADLOCK); + set_trx_error_state(trx, DB_LOCK_WAIT_TIMEOUT); /* This flag can't be set, as we always call the lock_cancel_waiting_and_release() immediately after setting it, which either prevents the trx from going to sleep or resets the wait_lock, and diff --git a/storage/innobase/que/que0que.cc b/storage/innobase/que/que0que.cc index e70f9ad9766..51ade2b0361 100644 --- a/storage/innobase/que/que0que.cc +++ b/storage/innobase/que/que0que.cc @@ -603,8 +603,8 @@ bool que_thr_stop(que_thr_t *thr) { trx->lock.wait_thr = thr; thr->state = QUE_THR_LOCK_WAIT; - } else if (trx->error_state != DB_SUCCESS && - trx->error_state != DB_LOCK_WAIT) { + } else if (get_trx_error_state(trx) != DB_SUCCESS && + get_trx_error_state(trx) != DB_LOCK_WAIT) { /* Error handling built for the MySQL interface */ thr->state = QUE_THR_COMPLETED; @@ -662,7 +662,7 @@ static void que_thr_dec_refer_count( in this case we have to do it here, otherwise nobody does it. */ - trx->error_state = DB_SUCCESS; + set_trx_error_state(trx, DB_SUCCESS); *next_thr = thr; @@ -692,8 +692,8 @@ void que_thr_stop_for_mysql(que_thr_t *thr) /*!< in: query thread */ trx_mutex_enter(trx); if (thr->state == QUE_THR_RUNNING) { - if (trx->error_state != DB_SUCCESS && trx->error_state != DB_LOCK_WAIT && - trx->error_state != DB_LOCK_WAIT_HOT_ROW_UPDATE) { + if (get_trx_error_state(trx) != DB_SUCCESS && get_trx_error_state(trx) != DB_LOCK_WAIT && + get_trx_error_state(trx) != DB_LOCK_WAIT_HOT_ROW_UPDATE) { /* Error handling built for the MySQL interface */ thr->state = QUE_THR_COMPLETED; } else { @@ -849,7 +849,7 @@ static inline que_thr_t *que_thr_step(que_thr_t *thr) /*!< in: query thread */ trx = thr_get_trx(thr); ut_ad(thr->state == QUE_THR_RUNNING); - ut_a(trx->error_state == DB_SUCCESS); + ut_a(get_trx_error_state(trx) == DB_SUCCESS); thr->resource++; @@ -931,7 +931,7 @@ static inline que_thr_t *que_thr_step(que_thr_t *thr) /*!< in: query thread */ } if (thr) { - ut_a(thr_get_trx(thr)->error_state == DB_SUCCESS); + ut_a(get_trx_error_state(thr_get_trx(thr)) == DB_SUCCESS); } return (thr); @@ -944,7 +944,7 @@ static void que_run_threads_low(que_thr_t *thr) /*!< in: query thread */ que_thr_t *next_thr; ut_ad(thr->state == QUE_THR_RUNNING); - ut_a(thr_get_trx(thr)->error_state == DB_SUCCESS); + ut_a(get_trx_error_state(thr_get_trx(thr)) == DB_SUCCESS); ut_ad(!trx_mutex_own(thr_get_trx(thr))); /* cumul_resource counts how much resources the OS thread (NOT the @@ -969,7 +969,7 @@ static void que_run_threads_low(que_thr_t *thr) /*!< in: query thread */ trx_mutex_enter(trx); - ut_a(next_thr == nullptr || trx->error_state == DB_SUCCESS); + ut_a(next_thr == nullptr || get_trx_error_state(trx) == DB_SUCCESS); if (next_thr != thr) { ut_a(next_thr == nullptr); @@ -997,7 +997,7 @@ void que_run_threads(que_thr_t *thr) /*!< in: query thread */ ut_ad(!trx_mutex_own(thr_get_trx(thr))); loop: - ut_a(thr_get_trx(thr)->error_state == DB_SUCCESS); + ut_a(get_trx_error_state(thr_get_trx(thr)) == DB_SUCCESS); que_run_threads_low(thr); @@ -1015,7 +1015,7 @@ void que_run_threads(que_thr_t *thr) /*!< in: query thread */ ut_a(thr_get_trx(thr)->id != 0); - if (thr_get_trx(thr)->error_state != DB_SUCCESS) { + if (get_trx_error_state(thr_get_trx(thr)) != DB_SUCCESS) { /* thr was chosen as a deadlock victim or there was a lock wait timeout */ @@ -1043,7 +1043,7 @@ dberr_t que_eval_sql(pars_info_t *info, const char *sql, trx_t *trx) { DBUG_TRACE; DBUG_PRINT("que_eval_sql", ("query: %s", sql)); - ut_a(trx->error_state == DB_SUCCESS); + ut_a(get_trx_error_state(trx) == DB_SUCCESS); mutex_enter(&pars_mutex); @@ -1063,9 +1063,9 @@ dberr_t que_eval_sql(pars_info_t *info, const char *sql, trx_t *trx) { que_graph_free(graph); - ut_a(trx->error_state != 0); + ut_a(get_trx_error_state(trx) != 0); - return trx->error_state; + return get_trx_error_state(trx); } /** Initialise the query sub-system. */ diff --git a/storage/innobase/row/row0import.cc b/storage/innobase/row/row0import.cc index bdb290607e4..1038e40fb62 100644 --- a/storage/innobase/row/row0import.cc +++ b/storage/innobase/row/row0import.cc @@ -4853,7 +4853,7 @@ dberr_t row_import_for_mysql(dict_table_t *table, dd::Table *table_def, row_mysql_lock_data_dictionary(trx, UT_LOCATION_HERE); - DBUG_EXECUTE_IF("ib_import_internal_error", trx->error_state = DB_ERROR; + DBUG_EXECUTE_IF("ib_import_internal_error", set_trx_error_state(trx, DB_ERROR); err = DB_ERROR; ib_errf(trx->mysql_thd, IB_LOG_LEVEL_ERROR, ER_INTERNAL_ERROR, "While importing table %s", table->name.m_name); diff --git a/storage/innobase/row/row0ins.cc b/storage/innobase/row/row0ins.cc index 298ea4a6ba1..d10eeeffedb 100644 --- a/storage/innobase/row/row0ins.cc +++ b/storage/innobase/row/row0ins.cc @@ -1699,7 +1699,7 @@ dberr_t row_ins_check_foreign_constraint( when it goes out of this scope. */ ib_dec_in_dtor dec(check_table->n_foreign_key_checks_running); - trx->error_state = err; + set_trx_error_state(trx, err); que_thr_stop_for_mysql(thr); @@ -1712,8 +1712,8 @@ dberr_t row_ins_check_foreign_constraint( lock_wait_suspend_thread(thr); - if (trx->error_state != DB_SUCCESS) { - err = trx->error_state; + if (get_trx_error_state(trx) != DB_SUCCESS) { + err = get_trx_error_state(trx); goto exit_func; } @@ -3600,7 +3600,7 @@ static inline void row_ins_get_row_from_query_block( case DB_SUCCESS: break; case DB_DUPLICATE_KEY: - thr_get_trx(thr)->error_state = DB_DUPLICATE_KEY; + set_trx_error_state(thr_get_trx(thr), DB_DUPLICATE_KEY); thr_get_trx(thr)->error_index = node->index; [[fallthrough]]; default: @@ -3721,7 +3721,7 @@ que_thr_t *row_ins_step(que_thr_t *thr) /*!< in: query thread */ err = row_ins(node, thr); error_handling: - trx->error_state = err; + set_trx_error_state(trx, err); if (err != DB_SUCCESS) { /* err == DB_LOCK_WAIT or SQL error detected */ diff --git a/storage/innobase/row/row0merge.cc b/storage/innobase/row/row0merge.cc index 73e1ea699f5..7eb64584c9b 100644 --- a/storage/innobase/row/row0merge.cc +++ b/storage/innobase/row/row0merge.cc @@ -2373,11 +2373,11 @@ static dberr_t write_merge_buf_to_file( file_mutex.unlock(); if (!row_merge_write(file->fd, my_offset, block)) { - err = DB_TEMP_FILE_WRITE_FAIL; + err = DB_TEMP_FILE_WRITE_FAIL; trx->error_key_num = idx; return err; } - UNIV_MEM_INVALID(&block[0], srv_sort_buf_size); + UNIV_MEM_INVALID(&block[0], srv_sort_buf_size); return err; } @@ -5429,7 +5429,7 @@ dberr_t row_merge_build_indexes( const_cast (sort_idx)->cached_offs_pddl.reset(); #ifdef UNIV_DEBUG_PARALLEL_DDL - ib::info() << "[TXSQL PARALLEL DDL] Build btree finished, costs(" + ib::info() << "[TXSQL PARALLEL DDL] Build btree finished, costs(" << std::chrono::duration_cast( std::chrono::steady_clock::now() - start_build_btree_time).count() << ") ms."; diff --git a/storage/innobase/row/row0mysql.cc b/storage/innobase/row/row0mysql.cc index a3c293ceeec..ba1e6249b07 100644 --- a/storage/innobase/row/row0mysql.cc +++ b/storage/innobase/row/row0mysql.cc @@ -1471,11 +1471,13 @@ bool row_mysql_handle_errors( dberr_t err; handle_new_error: - err = trx->error_state; + DBUG_EXECUTE_IF("sync_row_mysql_handle_errors_issue3628", sleep(5);); + + err = get_trx_error_state(trx); ut_a(err != DB_SUCCESS); - trx->error_state = DB_SUCCESS; + set_trx_error_state(trx, DB_SUCCESS); switch (err) { case DB_LOCK_WAIT_TIMEOUT: @@ -1519,7 +1521,7 @@ bool row_mysql_handle_errors( lock_wait_suspend_thread(thr); - if (trx->error_state != DB_SUCCESS) { + if (get_trx_error_state(trx) != DB_SUCCESS) { que_thr_stop_for_mysql(thr); goto handle_new_error; @@ -1577,13 +1579,13 @@ bool row_mysql_handle_errors( << "Unknown error code " << err << ": " << ut_strerr(err); } - if (trx->error_state != DB_SUCCESS) { - *new_err = trx->error_state; + if (get_trx_error_state(trx) != DB_SUCCESS) { + *new_err = get_trx_error_state(trx); } else { *new_err = err; } - trx->error_state = DB_SUCCESS; + set_trx_error_state(trx, DB_SUCCESS); return (false); } @@ -2058,7 +2060,7 @@ dberr_t row_lock_table_autoinc_for_mysql( err = lock_table(0, prebuilt->table, LOCK_AUTO_INC, thr); - trx->error_state = err; + set_trx_error_state(trx, err); if (err != DB_SUCCESS) { que_thr_stop_for_mysql(thr); @@ -2116,7 +2118,7 @@ dberr_t row_lock_table(row_prebuilt_t *prebuilt) { lock_table(0, prebuilt->table, static_cast(prebuilt->select_lock_type), thr); - trx->error_state = err; + set_trx_error_state(trx, err); if (err != DB_SUCCESS) { que_thr_stop_for_mysql(thr); @@ -2365,7 +2367,7 @@ static dberr_t row_insert_for_mysql_using_cursor(const byte *mysql_rec, } } - thr_get_trx(thr)->error_state = DB_SUCCESS; + set_trx_error_state(thr_get_trx(thr), DB_SUCCESS); return (err); } @@ -2465,7 +2467,7 @@ static dberr_t row_insert_for_mysql_using_ins_graph(const byte *mysql_rec, DEBUG_SYNC_C("ib_after_row_insert_step"); - err = trx->error_state; + err = get_trx_error_state(trx); if (err != DB_SUCCESS) { error_exit: @@ -2502,7 +2504,7 @@ static dberr_t row_insert_for_mysql_using_ins_graph(const byte *mysql_rec, if (doc_id <= 0) { ib::error(ER_IB_MSG_980) << "FTS Doc ID must be large than 0"; err = DB_FTS_INVALID_DOCID; - trx->error_state = DB_FTS_INVALID_DOCID; + set_trx_error_state(trx, DB_FTS_INVALID_DOCID); goto error_exit; } @@ -2515,7 +2517,7 @@ static dberr_t row_insert_for_mysql_using_ins_graph(const byte *mysql_rec, << " for table " << table->name; err = DB_FTS_INVALID_DOCID; - trx->error_state = DB_FTS_INVALID_DOCID; + set_trx_error_state(trx, DB_FTS_INVALID_DOCID); goto error_exit; } @@ -2533,7 +2535,7 @@ static dberr_t row_insert_for_mysql_using_ins_graph(const byte *mysql_rec, " exceed or equal to " << FTS_DOC_ID_MAX_STEP; err = DB_FTS_INVALID_DOCID; - trx->error_state = DB_FTS_INVALID_DOCID; + set_trx_error_state(trx, DB_FTS_INVALID_DOCID); goto error_exit; } } @@ -3134,7 +3136,7 @@ static dberr_t row_del_upd_for_mysql_using_cursor(row_prebuilt_t *prebuilt) { } } - thr_get_trx(thr)->error_state = DB_SUCCESS; + set_trx_error_state(thr_get_trx(thr), DB_SUCCESS); cursors_t::iterator end = delete_entries.end(); for (cursors_t::iterator it = delete_entries.begin(); it != end; ++it) { it->close(); @@ -3242,13 +3244,13 @@ static dberr_t row_update_for_mysql_using_upd_graph(const byte *mysql_rec, thr->fk_cascade_depth = 0; row_upd_step(thr); - err = trx->error_state; + err = get_trx_error_state(trx); if (err != DB_SUCCESS) { que_thr_stop_for_mysql(thr); if (err == DB_RECORD_NOT_FOUND) { - trx->error_state = DB_SUCCESS; + set_trx_error_state(trx, DB_SUCCESS); trx->op_info = ""; if (thr->fk_cascade_depth > 0) { @@ -3491,7 +3493,7 @@ dberr_t row_update_cascade_for_mysql( different row deletes */ thr->fk_cascade_depth = 0; - err = trx->error_state; + err = get_trx_error_state(trx); /* Note that the cascade node is a subnode of another InnoDB query graph node. We do a normal lock wait in this node, but @@ -3508,8 +3510,8 @@ dberr_t row_update_cascade_for_mysql( or this transaction is picked as a victim in selective deadlock resolution */ - if (trx->error_state != DB_SUCCESS) { - return (trx->error_state); + if (get_trx_error_state(trx) != DB_SUCCESS) { + return (get_trx_error_state(trx)); } /* Retry operation after a normal lock wait */ @@ -3646,7 +3648,7 @@ dberr_t row_create_table_for_mysql(dict_table_t *&table, /* Assign table id and build table space. */ err = dict_build_table_def(table, create_info, trx); if (err != DB_SUCCESS) { - trx->error_state = DB_SUCCESS; + set_trx_error_state(trx, DB_SUCCESS); trx->op_info = ""; trx->dict_operation = TRX_DICT_OP_NONE; dict_mem_table_free(table); @@ -3728,7 +3730,7 @@ dberr_t row_create_table_for_mysql(dict_table_t *&table, << " because the tablespace is full"; } - trx->error_state = DB_SUCCESS; + set_trx_error_state(trx, DB_SUCCESS); /* Still do it here so that the table can always be freed */ if (dd_table_open_on_name_in_mem(table->name.m_name, false)) { @@ -3746,7 +3748,7 @@ dberr_t row_create_table_for_mysql(dict_table_t *&table, break; default: - trx->error_state = DB_SUCCESS; + set_trx_error_state(trx, DB_SUCCESS); dict_mem_table_free(table); table = nullptr; break; @@ -4401,11 +4403,11 @@ static dberr_t row_discard_tablespace(trx_t *trx, dict_table_t *table, default: /* We need to rollback the disk changes, something failed. */ - trx->error_state = DB_SUCCESS; + set_trx_error_state(trx, DB_SUCCESS); trx_rollback_to_savepoint(trx, nullptr); - trx->error_state = DB_SUCCESS; + set_trx_error_state(trx, DB_SUCCESS); } return (err); @@ -4506,7 +4508,7 @@ dberr_t row_mysql_lock_table( err = lock_table(0, table, mode, thr); - trx->error_state = err; + set_trx_error_state(trx, err); if (err == DB_SUCCESS) { que_thr_stop_for_mysql_no_error(thr, trx); @@ -5127,7 +5129,7 @@ dberr_t row_rename_table_for_mysql(const char *old_name, const char *new_name, " the table exists, and DROP TABLE will" " succeed."; } - trx->error_state = DB_SUCCESS; + set_trx_error_state(trx, DB_SUCCESS); } else { /* The following call will also rename the .ibd data file if the table is stored in a single-table tablespace */ @@ -5135,7 +5137,7 @@ dberr_t row_rename_table_for_mysql(const char *old_name, const char *new_name, err = dict_table_rename_in_cache(table, new_name, !table->refresh_fk && !new_is_tmp); if (err != DB_SUCCESS) { - trx->error_state = DB_SUCCESS; + set_trx_error_state(trx, DB_SUCCESS); goto funct_exit; } diff --git a/storage/innobase/row/row0px.cc b/storage/innobase/row/row0px.cc index a7466fd052e..f5ef21fcf5a 100644 --- a/storage/innobase/row/row0px.cc +++ b/storage/innobase/row/row0px.cc @@ -1270,7 +1270,7 @@ dberr_t PX_Ctx::row_search_px(uchar *buf, row_prebuilt_t *prebuilt) { lock_wait_or_error: pcur->store_position(&mtr); - prebuilt->trx->error_state = err; + set_trx_error_state(prebuilt->trx, err); mtr_has_extra_clust_latch = false; goto func_exit; diff --git a/storage/innobase/row/row0sel.cc b/storage/innobase/row/row0sel.cc index e970d2d075c..e9145d3c2ae 100644 --- a/storage/innobase/row/row0sel.cc +++ b/storage/innobase/row/row0sel.cc @@ -1008,7 +1008,7 @@ static inline dberr_t sel_set_rtr_rec_lock( case DB_LOCK_WAIT: re_scan: mtr_commit(mtr); - trx->error_state = err; + set_trx_error_state(trx, err); que_thr_stop_for_mysql(thr); thr->lock_state = QUE_THR_LOCK_ROW; if (row_mysql_handle_errors(&err, trx, thr, nullptr)) { @@ -2168,7 +2168,7 @@ que_thr_t *row_sel_step(que_thr_t *thr) /*!< in: query thread */ trx_t *trx; trx = thr_get_trx(thr); - trx->error_state = err; + set_trx_error_state(trx, err); return (nullptr); } @@ -2202,7 +2202,7 @@ que_thr_t *row_sel_step(que_thr_t *thr) /*!< in: query thread */ thr->graph->last_sel_node = node; if (err != DB_SUCCESS) { - thr_get_trx(thr)->error_state = err; + set_trx_error_state(thr_get_trx(thr), err); return (nullptr); } @@ -2252,7 +2252,7 @@ que_thr_t *fetch_step(que_thr_t *thr) /*!< in: query thread */ if (sel_node->state == SEL_NODE_CLOSED) { ib::error(ER_IB_MSG_1027) << "fetch called on a closed cursor"; - thr_get_trx(thr)->error_state = DB_ERROR; + set_trx_error_state(thr_get_trx(thr), DB_ERROR); return (nullptr); } @@ -6373,7 +6373,7 @@ dberr_t row_search_mvcc(byte *buf, page_cur_mode_t mode, mtr_commit(&mtr); mtr_has_extra_clust_latch = false; - trx->error_state = err; + set_trx_error_state(trx, err); /* The following is a patch for MySQL */ diff --git a/storage/innobase/row/row0undo.cc b/storage/innobase/row/row0undo.cc index e2a6f86e84b..398392ae389 100644 --- a/storage/innobase/row/row0undo.cc +++ b/storage/innobase/row/row0undo.cc @@ -361,7 +361,7 @@ que_thr_t *row_undo_step(que_thr_t *thr) /*!< in: query thread */ err = row_undo(node, thr); - trx->error_state = err; + set_trx_error_state(trx, err); if (err != DB_SUCCESS) { /* SQL error detected */ diff --git a/storage/innobase/row/row0upd.cc b/storage/innobase/row/row0upd.cc index d11efae6745..f0059a8a0b2 100644 --- a/storage/innobase/row/row0upd.cc +++ b/storage/innobase/row/row0upd.cc @@ -3299,7 +3299,7 @@ que_thr_t *row_upd_step(que_thr_t *thr) /*!< in: query thread */ err = row_upd(node, thr); error_handling: - trx->error_state = err; + set_trx_error_state(trx, err); if (err != DB_SUCCESS) { return nullptr; diff --git a/storage/innobase/trx/trx0roll.cc b/storage/innobase/trx/trx0roll.cc index 9e634e3ada7..fbbceee72bf 100644 --- a/storage/innobase/trx/trx0roll.cc +++ b/storage/innobase/trx/trx0roll.cc @@ -97,7 +97,7 @@ static void trx_rollback_to_savepoint_low( assert_trx_nonlocking_or_in_list(trx); } - trx->error_state = DB_SUCCESS; + set_trx_error_state(trx, DB_SUCCESS); if (trx_is_rseg_updated(trx)) { ut_ad(trx->rsegs.m_redo.rseg != nullptr || @@ -125,7 +125,7 @@ static void trx_rollback_to_savepoint_low( MONITOR_INC(MONITOR_TRX_ROLLBACK_SAVEPOINT); } - ut_a(trx->error_state == DB_SUCCESS); + ut_a(get_trx_error_state(trx) == DB_SUCCESS); ut_a(trx->lock.que_state == TRX_QUE_RUNNING); mem_heap_free(heap); @@ -150,7 +150,7 @@ dberr_t trx_rollback_to_savepoint( trx_rollback_to_savepoint_low(trx, savept); - return (trx->error_state); + return (get_trx_error_state(trx)); } /** Rollback a transaction used in MySQL. @@ -169,9 +169,9 @@ static dberr_t trx_rollback_for_mysql_low( trx->op_info = ""; - ut_a(trx->error_state == DB_SUCCESS); + ut_a(get_trx_error_state(trx) == DB_SUCCESS); - return (trx->error_state); + return (get_trx_error_state(trx)); } /** Rollback a transaction used in MySQL diff --git a/storage/innobase/trx/trx0trx.cc b/storage/innobase/trx/trx0trx.cc index 814c669c70f..1bac58ecd71 100644 --- a/storage/innobase/trx/trx0trx.cc +++ b/storage/innobase/trx/trx0trx.cc @@ -179,7 +179,7 @@ static void trx_init(trx_t *trx) { trx->ddl_operation = false; - trx->error_state = DB_SUCCESS; + set_trx_error_state(trx, DB_SUCCESS); trx->error_key_num = ULINT_UNDEFINED; @@ -366,7 +366,7 @@ struct TrxFactory { is added to the pool. @return true if all OK */ static bool debug(const trx_t *trx) { - ut_a(trx->error_state == DB_SUCCESS); + ut_a(get_trx_error_state(trx) == DB_SUCCESS); ut_a(trx->magic_n == TRX_MAGIC_N); @@ -1283,7 +1283,7 @@ static void trx_start_low( ut_ad(trx->start_file != nullptr); ut_ad(trx->roll_limit == 0); ut_ad(!trx->lock.in_rollback); - ut_ad(trx->error_state == DB_SUCCESS); + ut_ad(get_trx_error_state(trx) == DB_SUCCESS); ut_ad(trx->rsegs.m_redo.rseg == nullptr); ut_ad(trx->rsegs.m_noredo.rseg == nullptr); ut_ad(trx_state_eq(trx, TRX_STATE_NOT_STARTED)); @@ -1407,7 +1407,7 @@ static void trx_start_low( } } - ut_a(trx->error_state == DB_SUCCESS); + ut_a(get_trx_error_state(trx) == DB_SUCCESS); trx->connect_broken = false; @@ -2091,7 +2091,7 @@ written */ trx_mutex_exit(trx); - ut_a(trx->error_state == DB_SUCCESS); + ut_a(get_trx_error_state(trx) == DB_SUCCESS); } /** Commits a transaction and a mini-transaction. From 242d9626080a25aa460125964208c5ed70a4e4ba Mon Sep 17 00:00:00 2001 From: linusluo Date: Thu, 22 Jan 2026 17:34:05 +0800 Subject: [PATCH 13/14] [bugfix] issue#3625 Disable parallel copy DDL when table has the functional index. Description: ======== Multiple workers of Parallel Copy DDL share the same THD, leading to concurrency issues when operating on the internal handler list. Although locks have been added, they do not resolve the push and pop order problem. Currently, this issue only occurs in scenarios where tables have functional indexes. Therefore, Parallel Copy DDL should be disabled in such cases. --bug=152754102 --- ...copy_ddl_functional_index_issue3625.result | 8 +++++++ ...l_copy_ddl_functional_index_issue3625.test | 20 ++++++++++++++++++ sql/sql_table.cc | 21 +++++++++++++++++-- 3 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 mysql-test/r/disable_parallel_copy_ddl_functional_index_issue3625.result create mode 100644 mysql-test/t/disable_parallel_copy_ddl_functional_index_issue3625.test diff --git a/mysql-test/r/disable_parallel_copy_ddl_functional_index_issue3625.result b/mysql-test/r/disable_parallel_copy_ddl_functional_index_issue3625.result new file mode 100644 index 00000000000..99ce2a805fa --- /dev/null +++ b/mysql-test/r/disable_parallel_copy_ddl_functional_index_issue3625.result @@ -0,0 +1,8 @@ +SET SESSION txsql_parallel_copy_ddl=ON; +CREATE TABLE t1 (fld1 INT, fld2 DATETIME NOT NULL) ENGINE= INNODB; +INSERT INTO t1 VALUES(1, '2000-01-01'); +INSERT INTO t1 VALUES(2, '2001-01-01'); +INSERT INTO t1 VALUES(3, '2002-01-01'); +ALTER TABLE t1 ADD INDEX idx_sale_month ((MONTH(fld2))), ALGORITHM= COPY; +ALTER TABLE t1 DROP INDEX idx_sale_month, ALGORITHM= COPY; +DROP TABLE t1; diff --git a/mysql-test/t/disable_parallel_copy_ddl_functional_index_issue3625.test b/mysql-test/t/disable_parallel_copy_ddl_functional_index_issue3625.test new file mode 100644 index 00000000000..f5f742e94bb --- /dev/null +++ b/mysql-test/t/disable_parallel_copy_ddl_functional_index_issue3625.test @@ -0,0 +1,20 @@ +SET SESSION txsql_parallel_copy_ddl=ON; + +CREATE TABLE t1 (fld1 INT, fld2 DATETIME NOT NULL) ENGINE= INNODB; + +INSERT INTO t1 VALUES(1, '2000-01-01'); +INSERT INTO t1 VALUES(2, '2001-01-01'); +INSERT INTO t1 VALUES(3, '2002-01-01'); +ALTER TABLE t1 ADD INDEX idx_sale_month ((MONTH(fld2))), ALGORITHM= COPY; + +--let SEARCH_FILE= $MYSQLTEST_VARDIR/log/mysqld.1.err +--let SEARCH_PATTERN= Executing non-parallel copy DDL when txsql_parallel_copy_ddl=ON +--source include/search_pattern_in_file.inc + +ALTER TABLE t1 DROP INDEX idx_sale_month, ALGORITHM= COPY; + +--let SEARCH_FILE= $MYSQLTEST_VARDIR/log/mysqld.1.err +--let SEARCH_PATTERN= \[TXSQL\] parallel_threads= +--source include/search_pattern_in_file.inc + +DROP TABLE t1; diff --git a/sql/sql_table.cc b/sql/sql_table.cc index c26b4741e1e..ca04306d83d 100644 --- a/sql/sql_table.cc +++ b/sql/sql_table.cc @@ -18666,6 +18666,17 @@ bool mysql_trans_commit_alter_copy_data(THD *thd) { return error; } +static bool check_if_table_has_functional_index(TABLE *to) { + for (uint i = 0; i < to->s->keys; ++i) { + const KEY &index = to->key_info[i]; + if (index.is_functional_index()) { + return true; + } + } + + return false; +} + /* In the following cases, we don't use parallel copy ddl: 1. Turn off txsql_parallel_copy_ddl; @@ -18678,6 +18689,7 @@ In the following cases, we don't use parallel copy ddl: latch, and during insert, it will do row_ins_check_foreign_constraints, which maybe try to acquire the same latch when the parent is in the same table. In this case, crash will happen. + 8. Table doesn't have functional indexes. Example, CREATE TABLE t3( @@ -18708,7 +18720,7 @@ static bool check_if_can_use_parallel_copy_ddl(THD *thd, TABLE *from, TABLE *to, && order == nullptr && !is_auto_inc && (table_def && table_def->foreign_keys()->empty()) && (old_table_def && old_table_def->foreign_keys().empty()) - && !is_multi_value) { + && !is_multi_value && !check_if_table_has_functional_index(to)) { return true; } return res; @@ -18964,7 +18976,12 @@ static int copy_data_between_tables( } else { fallback: - while (!(error = iterator->Read())) { + if (thd->variables.txsql_parallel_copy_ddl) { + sql_print_information( + "[TXSQL] Executing non-parallel copy DDL when " + "txsql_parallel_copy_ddl=ON."); + } + while (!(error = iterator->Read())) { if (thd->killed) { thd->send_kill_message(); error = 1; From d5a0bd9a62ab54c3209ba3c56c4f3a3a2b5b2f53 Mon Sep 17 00:00:00 2001 From: linusluo Date: Sun, 7 Jun 2026 18:47:13 +0800 Subject: [PATCH 14/14] [bugfix] issue#4088 Fix multiple workers of parallel copy DDL sharing the same Item_func_regexp_instr object causes a data race. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Problem: ======== When performing a copy-style ALTER (going through ha_innobase::parallel_copy_data_between_tables) on a table that contains generated columns or DEFAULT (expr) columns whose expressions use REGEXP_INSTR / REGEXP_LIKE / REGEXP_REPLACE / REGEXP_SUBSTR, mysqld crashes. Root cause: during the parallel copy, each worker creates its own copies of from_table / to_table via open_table_uncached, but the Item tree rooted at Field::gcol_info->expr_item and Field::m_default_val_expr->expr_item is not cloned along with them, so multiple workers share the same Item_func_regexp_instr object. This object holds the member unique_ptr_destroy_only m_facade; when two threads enter set_pattern() → SetupEngine() simultaneously, a data race occurs — the internal ICU URegularExpression* is concurrently reset / accessed, and dereferencing already-freed memory leads to a SIGSEGV. Solution: ======== Before ha_innobase::parallel_copy_data_between_tables enters parallel execution, check whether the target table has any generated columns or newly added columns with a generated default value. If either is present, directly set ha_copy_alter_info->fallback = true and return DB_INTERRUPTED, letting the upper layer fall back to the original single-threaded copy_data_between_tables path. --story=1157611170 --- ..._parallel_copy_ddl_generated_column.result | 13 +++++++ ...l_copy_ddl_functional_index_issue3625.test | 2 +- ...le_parallel_copy_ddl_generated_column.test | 35 +++++++++++++++++++ sql/sql_table.cc | 6 ++++ storage/innobase/include/trx0trx.h | 1 + 5 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 mysql-test/r/disable_parallel_copy_ddl_generated_column.result create mode 100644 mysql-test/t/disable_parallel_copy_ddl_generated_column.test diff --git a/mysql-test/r/disable_parallel_copy_ddl_generated_column.result b/mysql-test/r/disable_parallel_copy_ddl_generated_column.result new file mode 100644 index 00000000000..e2f7c993f78 --- /dev/null +++ b/mysql-test/r/disable_parallel_copy_ddl_generated_column.result @@ -0,0 +1,13 @@ +SET SESSION txsql_parallel_copy_ddl=ON; +CREATE TABLE t1 ( +id INT PRIMARY KEY, +a INT, +b INT GENERATED ALWAYS AS (a + 1) VIRTUAL +) ENGINE=INNODB; +INSERT INTO t1 (id, a) VALUES (1, 10), (2, 20), (3, 30); +ALTER TABLE t1 ADD COLUMN c INT, ALGORITHM=COPY; +DROP TABLE t1; +CREATE TABLE t2 (id INT PRIMARY KEY, a INT) ENGINE=INNODB; +INSERT INTO t2 VALUES (1, 10), (2, 20), (3, 30); +ALTER TABLE t2 ADD COLUMN c INT, ALGORITHM=COPY; +DROP TABLE t2; diff --git a/mysql-test/t/disable_parallel_copy_ddl_functional_index_issue3625.test b/mysql-test/t/disable_parallel_copy_ddl_functional_index_issue3625.test index f5f742e94bb..9c098da34b1 100644 --- a/mysql-test/t/disable_parallel_copy_ddl_functional_index_issue3625.test +++ b/mysql-test/t/disable_parallel_copy_ddl_functional_index_issue3625.test @@ -14,7 +14,7 @@ ALTER TABLE t1 ADD INDEX idx_sale_month ((MONTH(fld2))), ALGORITHM= COPY; ALTER TABLE t1 DROP INDEX idx_sale_month, ALGORITHM= COPY; --let SEARCH_FILE= $MYSQLTEST_VARDIR/log/mysqld.1.err ---let SEARCH_PATTERN= \[TXSQL\] parallel_threads= +--let SEARCH_PATTERN= \[TXSQL Parallel Copy DDL\] parallel_threads= --source include/search_pattern_in_file.inc DROP TABLE t1; diff --git a/mysql-test/t/disable_parallel_copy_ddl_generated_column.test b/mysql-test/t/disable_parallel_copy_ddl_generated_column.test new file mode 100644 index 00000000000..d65efaaeb3a --- /dev/null +++ b/mysql-test/t/disable_parallel_copy_ddl_generated_column.test @@ -0,0 +1,35 @@ +# +# Verify that parallel copy DDL falls back to single-threaded copy when the +# target table contains a generated column. These expressions share a single +# Item tree across per-thread TABLE copies, evaluating them in parallel +# workers causes data races (e.g. Item_func_regexp::m_facade). +# + +SET SESSION txsql_parallel_copy_ddl=ON; + +--let SEARCH_FILE= $MYSQLTEST_VARDIR/log/mysqld.1.err + +CREATE TABLE t1 ( + id INT PRIMARY KEY, + a INT, + b INT GENERATED ALWAYS AS (a + 1) VIRTUAL +) ENGINE=INNODB; + +INSERT INTO t1 (id, a) VALUES (1, 10), (2, 20), (3, 30); +ALTER TABLE t1 ADD COLUMN c INT, ALGORITHM=COPY; + +--let SEARCH_PATTERN= Executing non-parallel copy DDL when txsql_parallel_copy_ddl=ON +--source include/search_pattern_in_file.inc + +DROP TABLE t1; + +# Control: a plain table without any generated column must still take the +# parallel path. +CREATE TABLE t2 (id INT PRIMARY KEY, a INT) ENGINE=INNODB; +INSERT INTO t2 VALUES (1, 10), (2, 20), (3, 30); +ALTER TABLE t2 ADD COLUMN c INT, ALGORITHM=COPY; + +--let SEARCH_PATTERN= \[TXSQL Parallel Copy DDL\] parallel_threads= +--source include/search_pattern_in_file.inc + +DROP TABLE t2; diff --git a/sql/sql_table.cc b/sql/sql_table.cc index ca04306d83d..149a1448650 100644 --- a/sql/sql_table.cc +++ b/sql/sql_table.cc @@ -18721,6 +18721,12 @@ static bool check_if_can_use_parallel_copy_ddl(THD *thd, TABLE *from, TABLE *to, && (table_def && table_def->foreign_keys()->empty()) && (old_table_def && old_table_def->foreign_keys().empty()) && !is_multi_value && !check_if_table_has_functional_index(to)) { + /* Generated columns and generated-default columns share a single + Item tree across per-thread TABLE copies, evaluating them in parallel + workers causes data races. Fall back to single-threaded copy. */ + if (to->s->vfields > 0 || to->s->gen_def_field_count > 0) { + return false; + } return true; } return res; diff --git a/storage/innobase/include/trx0trx.h b/storage/innobase/include/trx0trx.h index 6bfee1473e9..48a003dc4b0 100644 --- a/storage/innobase/include/trx0trx.h +++ b/storage/innobase/include/trx0trx.h @@ -56,6 +56,7 @@ this program; if not, write to the Free Software Foundation, Inc., #include "srv0srv.h" #include "dict0mem.h" +#include "sql/current_thd.h" #include "sql_thd_internal_api.h" // Forward declaration