mirror of
https://github.com/MariaDB/server.git
synced 2025-01-27 01:04:19 +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.
301 lines
9 KiB
C++
301 lines
9 KiB
C++
/* Copyright (C) 2006 MySQL AB
|
|
|
|
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
|
|
|
|
#ifndef RPL_UTILITY_H
|
|
#define RPL_UTILITY_H
|
|
|
|
#ifndef __cplusplus
|
|
#error "Don't include this C++ header file from a non-C++ file!"
|
|
#endif
|
|
|
|
#include "mysql_priv.h"
|
|
|
|
struct st_relay_log_info;
|
|
typedef st_relay_log_info RELAY_LOG_INFO;
|
|
|
|
|
|
/**
|
|
A table definition from the master.
|
|
|
|
The responsibilities of this class is:
|
|
- Extract and decode table definition data from the table map event
|
|
- Check if table definition in table map is compatible with table
|
|
definition on slave
|
|
|
|
Currently, the only field type data available is an array of the
|
|
type operators that are present in the table map event.
|
|
|
|
@todo Add type operands to this structure to allow detection of
|
|
difference between, e.g., BIT(5) and BIT(10).
|
|
*/
|
|
|
|
class table_def
|
|
{
|
|
public:
|
|
/**
|
|
Convenience declaration of the type of the field type data in a
|
|
table map event.
|
|
*/
|
|
typedef unsigned char field_type;
|
|
|
|
/**
|
|
Constructor.
|
|
|
|
@param types Array of types
|
|
@param size Number of elements in array 'types'
|
|
@param field_metadata Array of extra information about fields
|
|
@param metadata_size Size of the field_metadata array
|
|
@param null_bitmap The bitmap of fields that can be null
|
|
*/
|
|
table_def(field_type *types, ulong size, uchar *field_metadata,
|
|
int metadata_size, uchar *null_bitmap)
|
|
: m_size(size), m_type(0),
|
|
m_field_metadata(0), m_null_bits(0), m_memory(NULL)
|
|
{
|
|
m_memory= (uchar *)my_multi_malloc(MYF(MY_WME),
|
|
&m_type, size,
|
|
&m_field_metadata,
|
|
size * sizeof(uint16),
|
|
&m_null_bits, (size + 7) / 8,
|
|
NULL);
|
|
|
|
bzero(m_field_metadata, size * sizeof(uint16));
|
|
|
|
if (m_type)
|
|
memcpy(m_type, types, size);
|
|
else
|
|
m_size= 0;
|
|
/*
|
|
Extract the data from the table map into the field metadata array
|
|
iff there is field metadata. The variable metadata_size will be
|
|
0 if we are replicating from an older version server since no field
|
|
metadata was written to the table map. This can also happen if
|
|
there were no fields in the master that needed extra metadata.
|
|
*/
|
|
if (m_size && metadata_size)
|
|
{
|
|
int index= 0;
|
|
for (unsigned int i= 0; i < m_size; i++)
|
|
{
|
|
switch (m_type[i]) {
|
|
case MYSQL_TYPE_TINY_BLOB:
|
|
case MYSQL_TYPE_BLOB:
|
|
case MYSQL_TYPE_MEDIUM_BLOB:
|
|
case MYSQL_TYPE_LONG_BLOB:
|
|
case MYSQL_TYPE_DOUBLE:
|
|
case MYSQL_TYPE_FLOAT:
|
|
{
|
|
/*
|
|
These types store a single byte.
|
|
*/
|
|
m_field_metadata[i]= (uchar)field_metadata[index];
|
|
index++;
|
|
break;
|
|
}
|
|
case MYSQL_TYPE_SET:
|
|
case MYSQL_TYPE_ENUM:
|
|
case MYSQL_TYPE_STRING:
|
|
{
|
|
short int x= field_metadata[index++] << 8U; // real_type
|
|
x = x + field_metadata[index++]; // pack or field length
|
|
m_field_metadata[i]= x;
|
|
break;
|
|
}
|
|
case MYSQL_TYPE_BIT:
|
|
{
|
|
short int x= field_metadata[index++];
|
|
x = x + (field_metadata[index++] << 8U);
|
|
m_field_metadata[i]= x;
|
|
break;
|
|
}
|
|
case MYSQL_TYPE_VARCHAR:
|
|
{
|
|
/*
|
|
These types store two bytes.
|
|
*/
|
|
char *ptr= (char *)&field_metadata[index];
|
|
m_field_metadata[i]= sint2korr(ptr);
|
|
index= index + 2;
|
|
break;
|
|
}
|
|
case MYSQL_TYPE_NEWDECIMAL:
|
|
{
|
|
short int x= field_metadata[index++] << 8U; // precision
|
|
x = x + field_metadata[index++]; // decimals
|
|
m_field_metadata[i]= x;
|
|
break;
|
|
}
|
|
default:
|
|
m_field_metadata[i]= 0;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
if (m_size && null_bitmap)
|
|
memcpy(m_null_bits, null_bitmap, (m_size + 7) / 8);
|
|
}
|
|
|
|
~table_def() {
|
|
my_free(m_memory, MYF(0));
|
|
#ifndef DBUG_OFF
|
|
m_type= 0;
|
|
m_size= 0;
|
|
#endif
|
|
}
|
|
|
|
/**
|
|
Return the number of fields there is type data for.
|
|
|
|
@return The number of fields that there is type data for.
|
|
*/
|
|
ulong size() const { return m_size; }
|
|
|
|
|
|
/*
|
|
Return a representation of the type data for one field.
|
|
|
|
@param index Field index to return data for
|
|
|
|
@return Will return a representation of the type data for field
|
|
<code>index</code>. Currently, only the type identifier is
|
|
returned.
|
|
*/
|
|
field_type type(ulong index) const
|
|
{
|
|
DBUG_ASSERT(index < m_size);
|
|
return m_type[index];
|
|
}
|
|
|
|
|
|
/*
|
|
This function allows callers to get the extra field data from the
|
|
table map for a given field. If there is no metadata for that field
|
|
or there is no extra metadata at all, the function returns 0.
|
|
|
|
The function returns the value for the field metadata for column at
|
|
position indicated by index. As mentioned, if the field was a type
|
|
that stores field metadata, that value is returned else zero (0) is
|
|
returned. This method is used in the unpack() methods of the
|
|
corresponding fields to properly extract the data from the binary log
|
|
in the event that the master's field is smaller than the slave.
|
|
*/
|
|
uint16 field_metadata(uint index) const
|
|
{
|
|
DBUG_ASSERT(index < m_size);
|
|
if (m_field_metadata)
|
|
return m_field_metadata[index];
|
|
else
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
This function returns whether the field on the master can be null.
|
|
This value is derived from field->maybe_null().
|
|
*/
|
|
my_bool maybe_null(uint index) const
|
|
{
|
|
DBUG_ASSERT(index < m_size);
|
|
return ((m_null_bits[(index / 8)] &
|
|
(1 << (index % 8))) == (1 << (index %8)));
|
|
}
|
|
|
|
/*
|
|
This function returns the field size in raw bytes based on the type
|
|
and the encoded field data from the master's raw data. This method can
|
|
be used for situations where the slave needs to skip a column (e.g.,
|
|
WL#3915) or needs to advance the pointer for the fields in the raw
|
|
data from the master to a specific column.
|
|
*/
|
|
uint32 calc_field_size(uint col, uchar *master_data) const;
|
|
|
|
/**
|
|
Decide if the table definition is compatible with a table.
|
|
|
|
Compare the definition with a table to see if it is compatible
|
|
with it.
|
|
|
|
A table definition is compatible with a table if:
|
|
- the columns types of the table definition is a (not
|
|
necessarily proper) prefix of the column type of the table, or
|
|
- the other way around
|
|
|
|
@param rli Pointer to relay log info
|
|
@param table Pointer to table to compare with.
|
|
|
|
@retval 1 if the table definition is not compatible with @c table
|
|
@retval 0 if the table definition is compatible with @c table
|
|
*/
|
|
int compatible_with(RELAY_LOG_INFO const *rli, TABLE *table) const;
|
|
|
|
private:
|
|
ulong m_size; // Number of elements in the types array
|
|
field_type *m_type; // Array of type descriptors
|
|
uint16 *m_field_metadata;
|
|
uchar *m_null_bits;
|
|
uchar *m_memory;
|
|
};
|
|
|
|
/**
|
|
Extend the normal table list with a few new fields needed by the
|
|
slave thread, but nowhere else.
|
|
*/
|
|
struct RPL_TABLE_LIST
|
|
: public TABLE_LIST
|
|
{
|
|
bool m_tabledef_valid;
|
|
table_def m_tabledef;
|
|
};
|
|
|
|
|
|
/* Anonymous namespace for template functions/classes */
|
|
namespace {
|
|
|
|
/*
|
|
Smart pointer that will automatically call my_afree (a macro) when
|
|
the pointer goes out of scope. This is used so that I do not have
|
|
to remember to call my_afree() before each return. There is no
|
|
overhead associated with this, since all functions are inline.
|
|
|
|
I (Matz) would prefer to use the free function as a template
|
|
parameter, but that is not possible when the "function" is a
|
|
macro.
|
|
*/
|
|
template <class Obj>
|
|
class auto_afree_ptr
|
|
{
|
|
Obj* m_ptr;
|
|
public:
|
|
auto_afree_ptr(Obj* ptr) : m_ptr(ptr) { }
|
|
~auto_afree_ptr() { if (m_ptr) my_afree(m_ptr); }
|
|
void assign(Obj* ptr) {
|
|
/* Only to be called if it hasn't been given a value before. */
|
|
DBUG_ASSERT(m_ptr == NULL);
|
|
m_ptr= ptr;
|
|
}
|
|
Obj* get() { return m_ptr; }
|
|
};
|
|
|
|
}
|
|
|
|
#define DBUG_PRINT_BITSET(N,FRM,BS) \
|
|
do { \
|
|
char buf[256]; \
|
|
for (uint i = 0 ; i < (BS)->n_bits ; ++i) \
|
|
buf[i] = bitmap_is_set((BS), i) ? '1' : '0'; \
|
|
buf[(BS)->n_bits] = '\0'; \
|
|
DBUG_PRINT((N), ((FRM), buf)); \
|
|
} while (0)
|
|
|
|
#endif /* RPL_UTILITY_H */
|