Commit 608b0ee5 authored by Sujatha's avatar Sujatha

MDEV-23033: All slaves crash once in ~24 hours and loop restart with signal 11

Problem:
=======
Upon deleting or updating a row in a parent table (with primary key), if
the child table has virtual column and an associated key with ON UPDATE
CASCADE/ON DELETE CASCADE, it will result in slave crash.

Analysis:
========
Tables which are related through foreign key require prelocking similar to
triggers. i.e If a table has triggers/foreign keys we should add all tables
and routines used by them to the prelocking set.  This prelocking happens
during 'open_and_lock_tables' call.  Each table being opened is checked for
foreign key references. If foreign key reference exists then the child
table is opened and it is linked to the table_list. Upon any modification
to  parent table its corresponding child tables are retried from table_list
and they are updated accordingly. This prelocking work fine on master.

On slave  prelocking works for following cases.
 - Statement/mixed based replication
 - In row based replication when trigger execution is enabled through
   'slave_run_triggers_for_rbr=YES/LOGGING/ENFORCE'

Otherwise it results in an assert/crash, as the parent table will not find
the corresponding child table and it will be NULL. Dereferencing NULL
pointer leads to slave server exit.

Fix:
===
Introduce a new 'slave_fk_event_map' flag similar to 'trg_event_map'. This
flag will ensure that when foreign key is enabled in row based replication
all the parent and child tables are prelocked, so that parent is able to
locate the child table.

Note: This issue is specific to slave, hence only slave needs to be
      upgraded.
parent 25db9ffa
This diff is collapsed.
This diff is collapsed.
......@@ -10718,7 +10718,7 @@ int Rows_log_event::do_add_row_data(uchar *row_data, size_t length)
There was the same problem with MERGE MYISAM tables and so here we try to
go the same way.
*/
static void restore_empty_query_table_list(LEX *lex)
inline void restore_empty_query_table_list(LEX *lex)
{
if (lex->first_not_own_table())
(*lex->first_not_own_table()->prev_global)= NULL;
......@@ -10733,6 +10733,8 @@ int Rows_log_event::do_apply_event(rpl_group_info *rgi)
TABLE* table;
DBUG_ENTER("Rows_log_event::do_apply_event(Relay_log_info*)");
int error= 0;
LEX *lex= thd->lex;
uint8 new_trg_event_map= get_trg_event_map();
/*
If m_table_id == ~0ULL, then we have a dummy event that does not
contain any data. In that case, we just remove all tables in the
......@@ -10823,11 +10825,6 @@ int Rows_log_event::do_apply_event(rpl_group_info *rgi)
DBUG_ASSERT(!debug_sync_set_action(thd, STRING_WITH_LEN(action)));
};);
if (slave_run_triggers_for_rbr)
{
LEX *lex= thd->lex;
uint8 new_trg_event_map= get_trg_event_map();
/*
Trigger's procedures work with global table list. So we have to add
rgi->tables_to_lock content there to get trigger's in the list.
......@@ -10840,10 +10837,17 @@ int Rows_log_event::do_apply_event(rpl_group_info *rgi)
for (TABLE_LIST *tables= rgi->tables_to_lock; tables;
tables= tables->next_global)
{
if (slave_run_triggers_for_rbr)
{
tables->trg_event_map= new_trg_event_map;
lex->query_tables_last= &tables->next_global;
}
else if (!WSREP_ON)
{
tables->slave_fk_event_map= new_trg_event_map;
lex->query_tables_last= &tables->next_global;
}
}
if (open_and_lock_tables(thd, rgi->tables_to_lock, FALSE, 0))
{
......@@ -11193,7 +11197,6 @@ int Rows_log_event::do_apply_event(rpl_group_info *rgi)
}
/* remove trigger's tables */
if (slave_run_triggers_for_rbr)
restore_empty_query_table_list(thd->lex);
#if defined(WITH_WSREP) && defined(HAVE_QUERY_CACHE)
......@@ -11212,7 +11215,6 @@ int Rows_log_event::do_apply_event(rpl_group_info *rgi)
DBUG_RETURN(error);
err:
if (slave_run_triggers_for_rbr)
restore_empty_query_table_list(thd->lex);
rgi->slave_close_thread_tables(thd);
DBUG_RETURN(error);
......
......@@ -4341,17 +4341,8 @@ bool table_already_fk_prelocked(TABLE_LIST *tl, LEX_STRING *db,
return false;
}
/**
Defines how prelocking algorithm for DML statements should handle table list
elements:
- If table has triggers we should add all tables and routines
used by them to the prelocking set.
We do not need to acquire metadata locks on trigger names
in DML statements, since all DDL statements
that change trigger metadata always lock their
subject tables.
Extend the table_list to include foreign tables for prelocking.
@param[in] thd Thread context.
@param[in] prelocking_ctx Prelocking context of the statement.
......@@ -4363,27 +4354,11 @@ bool table_already_fk_prelocked(TABLE_LIST *tl, LEX_STRING *db,
@retval FALSE Success.
@retval TRUE Failure (OOM).
*/
bool DML_prelocking_strategy::
handle_table(THD *thd, Query_tables_list *prelocking_ctx,
TABLE_LIST *table_list, bool *need_prelocking)
inline bool
prepare_fk_prelocking_list(THD *thd, Query_tables_list *prelocking_ctx,
TABLE_LIST *table_list, bool *need_prelocking,
uint8 op)
{
/* We rely on a caller to check that table is going to be changed. */
DBUG_ASSERT(table_list->lock_type >= TL_WRITE_ALLOW_WRITE);
if (table_list->trg_event_map)
{
if (table_list->table->triggers)
{
*need_prelocking= TRUE;
if (table_list->table->triggers->
add_tables_and_routines_for_triggers(thd, prelocking_ctx, table_list))
return TRUE;
}
if (table_list->table->file->referenced_by_foreign_key())
{
List <FOREIGN_KEY_INFO> fk_list;
List_iterator<FOREIGN_KEY_INFO> fk_list_it(fk_list);
FOREIGN_KEY_INFO *fk;
......@@ -4405,7 +4380,6 @@ handle_table(THD *thd, Query_tables_list *prelocking_ctx,
{
// FK_OPTION_RESTRICT and FK_OPTION_NO_ACTION only need read access
static bool can_write[]= { true, false, true, true, false, true };
uint8 op= table_list->trg_event_map;
thr_lock_type lock_type;
if ((op & (1 << TRG_EVENT_DELETE) && can_write[fk->delete_method])
......@@ -4427,9 +4401,65 @@ handle_table(THD *thd, Query_tables_list *prelocking_ctx,
}
if (arena)
thd->restore_active_arena(arena, &backup);
return FALSE;
}
/**
Defines how prelocking algorithm for DML statements should handle table list
elements:
- If table has triggers we should add all tables and routines
used by them to the prelocking set.
We do not need to acquire metadata locks on trigger names
in DML statements, since all DDL statements
that change trigger metadata always lock their
subject tables.
@param[in] thd Thread context.
@param[in] prelocking_ctx Prelocking context of the statement.
@param[in] table_list Table list element for table.
@param[in] sp Routine body.
@param[out] need_prelocking Set to TRUE if method detects that prelocking
required, not changed otherwise.
@retval FALSE Success.
@retval TRUE Failure (OOM).
*/
bool DML_prelocking_strategy::
handle_table(THD *thd, Query_tables_list *prelocking_ctx,
TABLE_LIST *table_list, bool *need_prelocking)
{
/* We rely on a caller to check that table is going to be changed. */
DBUG_ASSERT(table_list->lock_type >= TL_WRITE_ALLOW_WRITE);
if (table_list->trg_event_map)
{
if (table_list->table->triggers)
{
*need_prelocking= TRUE;
if (table_list->table->triggers->
add_tables_and_routines_for_triggers(thd, prelocking_ctx, table_list))
return TRUE;
}
if (table_list->table->file->referenced_by_foreign_key())
{
return (prepare_fk_prelocking_list(thd, prelocking_ctx, table_list,
need_prelocking,
table_list->trg_event_map));
}
}
else if (table_list->slave_fk_event_map &&
table_list->table->file->referenced_by_foreign_key())
{
return (prepare_fk_prelocking_list(thd, prelocking_ctx,
table_list, need_prelocking,
table_list->slave_fk_event_map));
}
return FALSE;
}
......
......@@ -2277,8 +2277,12 @@ struct TABLE_LIST
Indicates what triggers we need to pre-load for this TABLE_LIST
when opening an associated TABLE. This is filled after
the parsed tree is created.
slave_fk_event_map is filled on the slave side with bitmaps value
representing row-based event operation to help find and prelock
possible FK constrain-related child tables.
*/
uint8 trg_event_map;
uint8 trg_event_map, slave_fk_event_map;
/* TRUE <=> this table is a const one and was optimized away. */
bool optimized_away;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment