From f8b64e17f94923ef421469d648c31c0f06d2cf96 Mon Sep 17 00:00:00 2001 From: "rafal@quant.(none)" <> Date: Sun, 26 Aug 2007 14:31:10 +0200 Subject: BUG#21842 (Cluster fails to replicate to innodb or myisam with err 134 using TPC-B): Problem: A RBR event can contain incomplete row data (only key value and fields which have been changed). In that case, when the row is unpacked into record and written to a table, the missing fields get incorrect NULL values leading to master-slave inconsistency. Solution: Use values found in slave's table for columns which are not given in the rows event. The code for writing a single row uses the following algorithm: 1. unpack row_data into table->record[0], 2. try to insert record, 3. if duplicate record found, fetch it into table->record[0], 4. unpack row_data into table->record[0], 5. write table->record[0] into the table. Where row_data is the row as stored in the data area of a rows event. Thus: a) unpacking of row_data happens at the time when row is written into a table, b) when unpacking (in step 4), only columns present in row_data are overwritten - all other columns remain as they were found in the table. Since all data needed for the above algorithm is stored inside Rows_log_event class, functions which locate and write rows are turned into methods of that class. replace_record() -> Rows_log_event::write_row() find_and_fetch_row() -> Rows_log_event::find_row() Both methods take row data from event's data buffer - the row being processed is pointed by m_curr_row. They unpack the data as needed into table's record buffers record[0] or record[1]. When row is unpacked, m_curr_row_end is set to point at next row in the data buffer. Other changes introduced in this changeset: - Change signature of unpack_row(): don't report errors and don't setup table's rw_set here. Errors can happen only when setting default values in prepare_record() function and are detected there. - In Rows_log_event and derived classes, don't pass arguments to the execution primitives (do_...() member functions) but use class members instead. - Move old row handling code into log_event_old.cc to be used by *_rows_log_event_old classes. Also, a new test rpl_ndb_2other is added which tests basic replication from master using ndb tables to slave storing the same tables using (possibly) different engine (myisam,innodb). Test is based on existing tests rpl_ndb_2myisam and rpl_ndb_2innodb. However, these tests doesn't work for various reasons and currently are disabled (see BUG#19227). The new test differs from the ones it is based on as follows: 1. Single test tests replication with different storage engines on slave (myisam, innodb, ndb). 2. Include file extra/rpl_tests/rpl_ndb_2multi_eng.test containing original tests is replaced by extra/rpl_tests/rpl_ndb_2multi_basic.test which doesn't contain tests using partitioned tables as these don't work currently. Instead, it tests replication to a slave which has more or less columns than master. 3. Include file include/rpl_multi_engine3.inc is replaced with include/rpl_multi_engine2.inc. The later differs by performing slightly different operations (updating more than one row in the table) and clearing table with "TRUNCATE TABLE" statement instead of "DELETE FROM" as replication of "DELETE" doesn't work well in this setting. 4. Slave must use option --log-slave-updates=0 as otherwise execution of replication events generated by ndb fails if table uses a different storage engine on slave (see BUG#29569). --- sql/rpl_record.cc | 78 ++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 48 insertions(+), 30 deletions(-) (limited to 'sql/rpl_record.cc') diff --git a/sql/rpl_record.cc b/sql/rpl_record.cc index 36dcedb3b88..de4f6e0c337 100644 --- a/sql/rpl_record.cc +++ b/sql/rpl_record.cc @@ -157,9 +157,8 @@ pack_row(TABLE *table, MY_BITMAP const* cols, the various member functions of Field and subclasses expect to write. - The row is assumed to only consist of the fields for which the - bitset represented by @c arr and @c bits; the other parts of the - record are left alone. + The row is assumed to only consist of the fields for which the corresponding + bit in bitset @c cols is set; the other parts of the record are left alone. At most @c colcnt columns are read: if the table is larger than that, the remaining fields are not filled in. @@ -169,15 +168,12 @@ pack_row(TABLE *table, MY_BITMAP const* cols, @param colcnt Number of columns to read from record @param row_data Packed row data - @param cols Pointer to columns data to fill in + @param cols Pointer to bitset describing columns to fill in @param row_end Pointer to variable that will hold the value of the one-after-end position for the row @param master_reclength Pointer to variable that will be set to the length of the record on the master side - @param rw_set Pointer to bitmap that holds either the read_set or the - write_set of the table - @retval 0 No error @@ -191,8 +187,7 @@ int unpack_row(RELAY_LOG_INFO const *rli, TABLE *table, uint const colcnt, uchar const *const row_data, MY_BITMAP const *cols, - uchar const **const row_end, ulong *const master_reclength, - MY_BITMAP* const rw_set, Log_event_type const event_type) + uchar const **const row_end, ulong *const master_reclength) { DBUG_ENTER("unpack_row"); DBUG_ASSERT(row_data); @@ -202,10 +197,6 @@ unpack_row(RELAY_LOG_INFO const *rli, uchar const *null_ptr= row_data; uchar const *pack_ptr= row_data + master_null_byte_count; - bitmap_clear_all(rw_set); - - empty_record(table); - Field **const begin_ptr = table->field; Field **field_ptr; Field **const end_ptr= begin_ptr + colcnt; @@ -265,7 +256,6 @@ unpack_row(RELAY_LOG_INFO const *rli, #endif } - bitmap_set_bit(rw_set, f->field_index); null_mask <<= 1; } i++; @@ -307,30 +297,58 @@ unpack_row(RELAY_LOG_INFO const *rli, else *master_reclength = table->s->reclength; } + + DBUG_RETURN(error); +} - /* - Set properties for remaining columns, if there are any. We let the - corresponding bit in the write_set be set, to write the value if - it was not there already. We iterate over all remaining columns, - even if there were an error, to get as many error messages as - possible. We are still able to return a pointer to the next row, - so redo that. - - This generation of error messages is only relevant when inserting - new rows. - */ - for ( ; *field_ptr ; ++field_ptr) +/** + Fills @c table->record[0] with default values. + + First @c empty_record() is called and then, additionally, fields are + initialized explicitly with a call to @c set_default(). + + For optimization reasons, the explicit initialization can be skipped for + first @c skip fields. This is useful if later we are going to fill these + fields from other source (e.g. from a Rows replication event). + + If @c check is true, fields are explicitly initialized only if they have + default value or can be NULL. Otherwise error is reported. + + @param log Used to report errors. + @param table Table whose record[0] buffer is prepared. + @param skip Number of columns for which default value initialization + should be skipped. + @param check Indicates if errors should be checked when setting default + values. + + @returns 0 on success. + */ +int prepare_record(const Slave_reporting_capability *const log, + TABLE *const table, + const uint skip, const bool check) +{ + DBUG_ENTER("prepare_record"); + + int error= 0; + empty_record(table); + + if (skip >= table->s->fields) // nothing to do + DBUG_RETURN(0); + + /* Explicit initialization of fields */ + + for (Field **field_ptr= table->field+skip ; *field_ptr ; ++field_ptr) { uint32 const mask= NOT_NULL_FLAG | NO_DEFAULT_VALUE_FLAG; Field *const f= *field_ptr; - if (event_type == WRITE_ROWS_EVENT && - ((*field_ptr)->flags & mask) == mask) + if (check && ((f->flags & mask) == mask)) { - rli->report(ERROR_LEVEL, ER_NO_DEFAULT_FOR_FIELD, + DBUG_ASSERT(log); + log->report(ERROR_LEVEL, ER_NO_DEFAULT_FOR_FIELD, "Field `%s` of table `%s`.`%s` " "has no default value and cannot be NULL", - (*field_ptr)->field_name, table->s->db.str, + f->field_name, table->s->db.str, table->s->table_name.str); error = ER_NO_DEFAULT_FOR_FIELD; } -- cgit v1.2.1