mirror of
https://github.com/MariaDB/server.git
synced 2025-01-18 13:02:28 +01:00
642eda2229
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/log_event.cc: - Initialization of new Rows_log_event members. - Fixing some typos in documentation. In Rows_log_event::do_apply_event: - Set COMPLETE_ROWS_F flag (when master and slave have the same number of columns and all colums are present in the row) - Move initialization of tables write/read sets here, outside the rows processing loop (and out of unpack_row() function). - Remove calls to do_prepare_row() - no longer needed. - Add code managing m_curr_row and m_curr_row_end pointers. - Change signatures of row processing methods of Rows_log_event and it descendants - now most arguments are taken from class members. - Remove do_prepare_row() methods which are no longer used. - The auto_afree_ptr template is moved to rpl_utility.h (so that it can be used in log_event_old.cc). - Removed copy_extra_fields() function - no longer used. In Rows_log_event::write_row (former replace_record): - The old code is moved to log_event_old.cc. - Use prepare_record() and non-destructive unpack_current_row() to fill record with data. - In case a record being inserted already exists on slave and row data is incomplete use the record found and non-destructive unpack_current_row() to combine new column values with existing ones. - More debug info added. In Rows_log_event::find_row (former find_and_fetch_row function): - The old code is moved to log_event_old.cc. - Unpacking of the row is moved here. - In case of search using PK, the key data is prepared here. - More debug info added. - Remove initialization of Rows_log_event::m_after_image buffer which is no longer used. - Use new row unpacking methods in Update_rows_log_event::do_exec_row() to create before and after image. Note: all existing code used by Rows_log_event::do_apply_event() has been moved to log_event_old.cc to be used by *_rows_log_event_old classes. sql/log_event.h: - Add new COMPLETE_ROWS_F flag in Rows_log_event. - Add Rows_log_event members describing the row being processed. - Add a pointer to key buffer which is used in derived classes. - Add new methods: find__row(), write_row() and unpack_current_row(). - Change signatures of do_...() methods (replace method arguments by class members). - Remove do_prepare_row() method which is no longer used. - Update method documentation. - Add Old_rows_log_event class, which contains the old row processing code, as a friend of Rows_log_event so that it can access all members of an event instance. sql/log_event_old.cc: Move here old implementation of Rows_log_event::do_apply_event() and helper methods. sql/log_event_old.h: - Define new class Old_rows_log_event encapsulating old version of Rows_log_event::do_apply_event() and the helper methods. - Add the Old_rows_log_event class as a base for *_old versions of RBR event classes, ensure that the old version of do_apply_event() is called. - For *_old classes, declare the helper methods used in the old version of do_apply_event(). sql/rpl_record.cc: - Make unpack_row non-destructive for columns not present in the row. - Don't fill read/write set here as it is done outside these functions. - Move initialization of a record with default values to a separate function prepare_record(). sql/rpl_record.h: - Change signature of unpack_row(). - Declare function prepare_record(). sql/rpl_utility.cc: Make tabe_def::calc_field_size() a const method. sql/rpl_utility.h: Make table_def::calc_field_size() a const method. Move auto_afree_ptr template here so that it can be re-used (currently in log_event.cc and log_event_old.cc). Similar with DBUG_PRINT_BITSET macro. mysql-test/extra/rpl_tests/rpl_ndb_2multi_basic.test: Modification of rpl_ndb_2multi_eng test. Tests with partitioned tables are removed and a setup with slave having different number of columns than master is added. mysql-test/include/rpl_multi_engine2.inc: Modification of rpl_multi_engine3.inc which operates on more rows and replaces "DELETE FROM t1" with "TRUNCATE TABLE t1" as the first form doesn't replicate in NDB -> non-NDB setting (BUG#28538). mysql-test/suite/rpl_ndb/r/rpl_ndb_2other.result: Results of the test. mysql-test/suite/rpl_ndb/t/rpl_ndb_2other-slave.opt: Test options. --log-slave-updates=0 is compulsory as otherwise non-NDB slave applying row events from NDB master will fail when trying to log them. mysql-test/suite/rpl_ndb/t/rpl_ndb_2other.test: Test replication of NDB table to slave using other engine. The main test is in extra/rpl_tests/rpl_ndb_2multi_basic.test. It is included here several times with different settings of default storage engine on slave.
362 lines
11 KiB
C++
362 lines
11 KiB
C++
/* Copyright 2007 MySQL AB. All rights reserved.
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation; version 2 of the License.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program; if not, write to the Free Software
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
|
|
|
|
#include "mysql_priv.h"
|
|
#include "rpl_rli.h"
|
|
#include "rpl_record.h"
|
|
#include "slave.h" // Need to pull in slave_print_msg
|
|
#include "rpl_utility.h"
|
|
#include "rpl_rli.h"
|
|
|
|
/**
|
|
Pack a record of data for a table into a format suitable for
|
|
transfer via the binary log.
|
|
|
|
The format for a row in transfer with N fields is the following:
|
|
|
|
ceil(N/8) null bytes:
|
|
One null bit for every column *regardless of whether it can be
|
|
null or not*. This simplifies the decoding. Observe that the
|
|
number of null bits is equal to the number of set bits in the
|
|
@c cols bitmap. The number of null bytes is the smallest number
|
|
of bytes necessary to store the null bits.
|
|
|
|
Padding bits are 1.
|
|
|
|
N packets:
|
|
Each field is stored in packed format.
|
|
|
|
|
|
@param table Table describing the format of the record
|
|
|
|
@param cols Bitmap with a set bit for each column that should
|
|
be stored in the row
|
|
|
|
@param row_data Pointer to memory where row will be written
|
|
|
|
@param record Pointer to record that should be packed. It is
|
|
assumed that the pointer refers to either @c
|
|
record[0] or @c record[1], but no such check is
|
|
made since the code does not rely on that.
|
|
|
|
@return The number of bytes written at @c row_data.
|
|
*/
|
|
#if !defined(MYSQL_CLIENT)
|
|
size_t
|
|
pack_row(TABLE *table, MY_BITMAP const* cols,
|
|
uchar *row_data, const uchar *record)
|
|
{
|
|
Field **p_field= table->field, *field;
|
|
int const null_byte_count= (bitmap_bits_set(cols) + 7) / 8;
|
|
uchar *pack_ptr = row_data + null_byte_count;
|
|
uchar *null_ptr = row_data;
|
|
my_ptrdiff_t const rec_offset= record - table->record[0];
|
|
my_ptrdiff_t const def_offset= table->s->default_values - table->record[0];
|
|
|
|
/*
|
|
We write the null bits and the packed records using one pass
|
|
through all the fields. The null bytes are written little-endian,
|
|
i.e., the first fields are in the first byte.
|
|
*/
|
|
unsigned int null_bits= (1U << 8) - 1;
|
|
// Mask to mask out the correct but among the null bits
|
|
unsigned int null_mask= 1U;
|
|
for ( ; (field= *p_field) ; p_field++)
|
|
{
|
|
DBUG_PRINT("debug", ("null_mask=%d; null_ptr=%p; row_data=%p; null_byte_count=%d",
|
|
null_mask, null_ptr, row_data, null_byte_count));
|
|
if (bitmap_is_set(cols, p_field - table->field))
|
|
{
|
|
my_ptrdiff_t offset;
|
|
if (field->is_null(rec_offset))
|
|
{
|
|
offset= def_offset;
|
|
null_bits |= null_mask;
|
|
}
|
|
else
|
|
{
|
|
offset= rec_offset;
|
|
null_bits &= ~null_mask;
|
|
|
|
/*
|
|
We only store the data of the field if it is non-null
|
|
|
|
For big-endian machines, we have to make sure that the
|
|
length is stored in little-endian format, since this is the
|
|
format used for the binlog.
|
|
|
|
We do this by setting the db_low_byte_first, which is used
|
|
inside some store_length() to decide what order to write the
|
|
bytes in.
|
|
|
|
In reality, db_log_byte_first is only set for legacy table
|
|
type Isam, but in the event of a bug, we need to guarantee
|
|
the endianess when writing to the binlog.
|
|
|
|
This is currently broken for NDB due to BUG#29549, so we
|
|
will fix it when NDB has fixed their way of handling BLOBs.
|
|
*/
|
|
#if 0
|
|
bool save= table->s->db_low_byte_first;
|
|
table->s->db_low_byte_first= TRUE;
|
|
#endif
|
|
pack_ptr= field->pack(pack_ptr, field->ptr + offset);
|
|
#if 0
|
|
table->s->db_low_byte_first= save;
|
|
#endif
|
|
}
|
|
|
|
null_mask <<= 1;
|
|
if ((null_mask & 0xFF) == 0)
|
|
{
|
|
DBUG_ASSERT(null_ptr < row_data + null_byte_count);
|
|
null_mask = 1U;
|
|
*null_ptr++ = null_bits;
|
|
null_bits= (1U << 8) - 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
/*
|
|
Write the last (partial) byte, if there is one
|
|
*/
|
|
if ((null_mask & 0xFF) > 1)
|
|
{
|
|
DBUG_ASSERT(null_ptr < row_data + null_byte_count);
|
|
*null_ptr++ = null_bits;
|
|
}
|
|
|
|
/*
|
|
The null pointer should now point to the first byte of the
|
|
packed data. If it doesn't, something is very wrong.
|
|
*/
|
|
DBUG_ASSERT(null_ptr == row_data + null_byte_count);
|
|
|
|
return static_cast<size_t>(pack_ptr - row_data);
|
|
}
|
|
#endif
|
|
|
|
|
|
/**
|
|
Unpack a row into @c table->record[0].
|
|
|
|
The function will always unpack into the @c table->record[0]
|
|
record. This is because there are too many dependencies on where
|
|
the various member functions of Field and subclasses expect to
|
|
write.
|
|
|
|
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.
|
|
|
|
@param rli Relay log info
|
|
@param table Table to unpack into
|
|
@param colcnt Number of columns to read from record
|
|
@param row_data
|
|
Packed row data
|
|
@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
|
|
|
|
@retval 0 No error
|
|
|
|
@retval ER_NO_DEFAULT_FOR_FIELD
|
|
Returned if one of the fields existing on the slave but not on the
|
|
master does not have a default value (and isn't nullable)
|
|
|
|
*/
|
|
#if !defined(MYSQL_CLIENT) && defined(HAVE_REPLICATION)
|
|
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)
|
|
{
|
|
DBUG_ENTER("unpack_row");
|
|
DBUG_ASSERT(row_data);
|
|
size_t const master_null_byte_count= (bitmap_bits_set(cols) + 7) / 8;
|
|
int error= 0;
|
|
|
|
uchar const *null_ptr= row_data;
|
|
uchar const *pack_ptr= row_data + master_null_byte_count;
|
|
|
|
Field **const begin_ptr = table->field;
|
|
Field **field_ptr;
|
|
Field **const end_ptr= begin_ptr + colcnt;
|
|
|
|
DBUG_ASSERT(null_ptr < row_data + master_null_byte_count);
|
|
|
|
// Mask to mask out the correct bit among the null bits
|
|
unsigned int null_mask= 1U;
|
|
// The "current" null bits
|
|
unsigned int null_bits= *null_ptr++;
|
|
uint i= 0;
|
|
table_def *tabledef= ((RELAY_LOG_INFO*)rli)->get_tabledef(table);
|
|
for (field_ptr= begin_ptr ; field_ptr < end_ptr && *field_ptr ; ++field_ptr)
|
|
{
|
|
Field *const f= *field_ptr;
|
|
|
|
/*
|
|
No need to bother about columns that does not exist: they have
|
|
gotten default values when being emptied above.
|
|
*/
|
|
if (bitmap_is_set(cols, field_ptr - begin_ptr))
|
|
{
|
|
if ((null_mask & 0xFF) == 0)
|
|
{
|
|
DBUG_ASSERT(null_ptr < row_data + master_null_byte_count);
|
|
null_mask= 1U;
|
|
null_bits= *null_ptr++;
|
|
}
|
|
|
|
DBUG_ASSERT(null_mask & 0xFF); // One of the 8 LSB should be set
|
|
|
|
/* Field...::unpack() cannot return 0 */
|
|
DBUG_ASSERT(pack_ptr != NULL);
|
|
|
|
if ((null_bits & null_mask) && f->maybe_null())
|
|
f->set_null();
|
|
else
|
|
{
|
|
f->set_notnull();
|
|
|
|
/*
|
|
We only unpack the field if it was non-null.
|
|
Use the master's size information if available else call
|
|
normal unpack operation.
|
|
*/
|
|
#if 0
|
|
bool save= table->s->db_low_byte_first;
|
|
table->s->db_low_byte_first= TRUE;
|
|
#endif
|
|
uint16 const metadata= tabledef->field_metadata(i);
|
|
if (tabledef && metadata)
|
|
pack_ptr= f->unpack(f->ptr, pack_ptr, metadata);
|
|
else
|
|
pack_ptr= f->unpack(f->ptr, pack_ptr);
|
|
#if 0
|
|
table->s->db_low_byte_first= save;
|
|
#endif
|
|
}
|
|
|
|
null_mask <<= 1;
|
|
}
|
|
i++;
|
|
}
|
|
|
|
/*
|
|
throw away master's extra fields
|
|
*/
|
|
uint max_cols= min(tabledef->size(), cols->n_bits);
|
|
for (; i < max_cols; i++)
|
|
{
|
|
if (bitmap_is_set(cols, i))
|
|
{
|
|
if ((null_mask & 0xFF) == 0)
|
|
{
|
|
DBUG_ASSERT(null_ptr < row_data + master_null_byte_count);
|
|
null_mask= 1U;
|
|
null_bits= *null_ptr++;
|
|
}
|
|
DBUG_ASSERT(null_mask & 0xFF); // One of the 8 LSB should be set
|
|
|
|
if (!((null_bits & null_mask) && tabledef->maybe_null(i)))
|
|
pack_ptr+= tabledef->calc_field_size(i, (uchar *) pack_ptr);
|
|
null_mask <<= 1;
|
|
}
|
|
}
|
|
|
|
/*
|
|
We should now have read all the null bytes, otherwise something is
|
|
really wrong.
|
|
*/
|
|
DBUG_ASSERT(null_ptr == row_data + master_null_byte_count);
|
|
|
|
*row_end = pack_ptr;
|
|
if (master_reclength)
|
|
{
|
|
if (*field_ptr)
|
|
*master_reclength = (*field_ptr)->ptr - table->record[0];
|
|
else
|
|
*master_reclength = table->s->reclength;
|
|
}
|
|
|
|
DBUG_RETURN(error);
|
|
}
|
|
|
|
/**
|
|
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 (check && ((f->flags & mask) == mask))
|
|
{
|
|
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",
|
|
f->field_name, table->s->db.str,
|
|
table->s->table_name.str);
|
|
error = ER_NO_DEFAULT_FOR_FIELD;
|
|
}
|
|
else
|
|
f->set_default();
|
|
}
|
|
|
|
DBUG_RETURN(error);
|
|
}
|
|
|
|
#endif // HAVE_REPLICATION
|