2006-12-31 02:29:11 +01:00
|
|
|
/* Copyright (C) 2006 MySQL AB
|
2006-05-03 15:00:38 +02:00
|
|
|
|
|
|
|
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
|
2006-12-27 02:23:51 +01:00
|
|
|
the Free Software Foundation; version 2 of the License.
|
2006-05-03 15:00:38 +02:00
|
|
|
|
|
|
|
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
|
2006-12-31 02:29:11 +01:00
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
|
2006-05-03 15:00:38 +02:00
|
|
|
|
|
|
|
#include "rpl_utility.h"
|
2007-06-11 22:15:39 +02:00
|
|
|
#include "rpl_rli.h"
|
2006-05-03 15:00:38 +02:00
|
|
|
|
2007-07-30 00:10:42 +02:00
|
|
|
/*********************************************************************
|
|
|
|
* table_def member definitions *
|
|
|
|
*********************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
This function returns the field size in raw bytes based on the type
|
|
|
|
and the encoded field data from the master's raw data.
|
|
|
|
*/
|
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).
2007-08-26 14:31:10 +02:00
|
|
|
uint32 table_def::calc_field_size(uint col, uchar *master_data) const
|
2006-05-03 15:00:38 +02:00
|
|
|
{
|
|
|
|
uint32 length;
|
|
|
|
|
2007-07-30 00:10:42 +02:00
|
|
|
switch (type(col)) {
|
2006-05-03 15:00:38 +02:00
|
|
|
case MYSQL_TYPE_NEWDECIMAL:
|
2007-07-30 00:10:42 +02:00
|
|
|
length= my_decimal_get_binary_size(m_field_metadata[col] >> 8,
|
2007-09-14 17:22:41 +02:00
|
|
|
m_field_metadata[col] & 0xff);
|
2007-07-30 00:10:42 +02:00
|
|
|
break;
|
|
|
|
case MYSQL_TYPE_DECIMAL:
|
|
|
|
case MYSQL_TYPE_FLOAT:
|
|
|
|
case MYSQL_TYPE_DOUBLE:
|
|
|
|
length= m_field_metadata[col];
|
|
|
|
break;
|
2007-09-14 17:22:41 +02:00
|
|
|
/*
|
|
|
|
The cases for SET and ENUM are include for completeness, however
|
|
|
|
both are mapped to type MYSQL_TYPE_STRING and their real types
|
|
|
|
are encoded in the field metadata.
|
|
|
|
*/
|
2007-07-30 00:10:42 +02:00
|
|
|
case MYSQL_TYPE_SET:
|
|
|
|
case MYSQL_TYPE_ENUM:
|
|
|
|
case MYSQL_TYPE_STRING:
|
|
|
|
{
|
2007-09-14 17:22:41 +02:00
|
|
|
uchar type= m_field_metadata[col] >> 8U;
|
|
|
|
if ((type == MYSQL_TYPE_SET) || (type == MYSQL_TYPE_ENUM))
|
2007-07-30 00:10:42 +02:00
|
|
|
length= m_field_metadata[col] & 0x00ff;
|
|
|
|
else
|
|
|
|
{
|
2007-09-14 17:22:41 +02:00
|
|
|
/*
|
|
|
|
We are reading the actual size from the master_data record
|
|
|
|
because this field has the actual lengh stored in the first
|
|
|
|
byte.
|
|
|
|
*/
|
|
|
|
length= (uint) *master_data + 1;
|
|
|
|
DBUG_ASSERT(length != 0);
|
2007-07-30 00:10:42 +02:00
|
|
|
}
|
2006-05-03 15:00:38 +02:00
|
|
|
break;
|
2007-07-30 00:10:42 +02:00
|
|
|
}
|
2006-05-03 15:00:38 +02:00
|
|
|
case MYSQL_TYPE_YEAR:
|
|
|
|
case MYSQL_TYPE_TINY:
|
|
|
|
length= 1;
|
|
|
|
break;
|
|
|
|
case MYSQL_TYPE_SHORT:
|
|
|
|
length= 2;
|
|
|
|
break;
|
|
|
|
case MYSQL_TYPE_INT24:
|
|
|
|
length= 3;
|
|
|
|
break;
|
|
|
|
case MYSQL_TYPE_LONG:
|
|
|
|
length= 4;
|
|
|
|
break;
|
|
|
|
#ifdef HAVE_LONG_LONG
|
|
|
|
case MYSQL_TYPE_LONGLONG:
|
|
|
|
length= 8;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case MYSQL_TYPE_NULL:
|
|
|
|
length= 0;
|
|
|
|
break;
|
|
|
|
case MYSQL_TYPE_NEWDATE:
|
|
|
|
length= 3;
|
|
|
|
break;
|
|
|
|
case MYSQL_TYPE_DATE:
|
|
|
|
case MYSQL_TYPE_TIME:
|
|
|
|
length= 3;
|
|
|
|
break;
|
|
|
|
case MYSQL_TYPE_TIMESTAMP:
|
|
|
|
length= 4;
|
|
|
|
break;
|
|
|
|
case MYSQL_TYPE_DATETIME:
|
|
|
|
length= 8;
|
|
|
|
break;
|
|
|
|
case MYSQL_TYPE_BIT:
|
2007-07-30 00:10:42 +02:00
|
|
|
{
|
2007-09-14 17:22:41 +02:00
|
|
|
/*
|
|
|
|
Decode the size of the bit field from the master.
|
|
|
|
from_len is the length in bytes from the master
|
|
|
|
from_bit_len is the number of extra bits stored in the master record
|
|
|
|
If from_bit_len is not 0, add 1 to the length to account for accurate
|
|
|
|
number of bytes needed.
|
|
|
|
*/
|
2007-07-30 00:10:42 +02:00
|
|
|
uint from_len= (m_field_metadata[col] >> 8U) & 0x00ff;
|
|
|
|
uint from_bit_len= m_field_metadata[col] & 0x00ff;
|
2007-08-03 17:12:00 +02:00
|
|
|
DBUG_ASSERT(from_bit_len <= 7);
|
2007-07-30 00:10:42 +02:00
|
|
|
length= from_len + ((from_bit_len > 0) ? 1 : 0);
|
2006-05-03 15:00:38 +02:00
|
|
|
break;
|
2007-07-30 00:10:42 +02:00
|
|
|
}
|
2006-05-03 15:00:38 +02:00
|
|
|
case MYSQL_TYPE_VARCHAR:
|
2007-07-30 18:55:26 +02:00
|
|
|
{
|
2007-07-30 00:10:42 +02:00
|
|
|
length= m_field_metadata[col] > 255 ? 2 : 1; // c&p of Field_varstring::data_length()
|
2007-07-30 18:55:26 +02:00
|
|
|
DBUG_ASSERT(uint2korr(master_data) > 0);
|
2007-07-30 00:10:42 +02:00
|
|
|
length+= length == 1 ? (uint32) *master_data : uint2korr(master_data);
|
2006-05-03 15:00:38 +02:00
|
|
|
break;
|
2007-07-30 18:55:26 +02:00
|
|
|
}
|
2006-05-03 15:00:38 +02:00
|
|
|
case MYSQL_TYPE_TINY_BLOB:
|
|
|
|
case MYSQL_TYPE_MEDIUM_BLOB:
|
|
|
|
case MYSQL_TYPE_LONG_BLOB:
|
|
|
|
case MYSQL_TYPE_BLOB:
|
|
|
|
case MYSQL_TYPE_GEOMETRY:
|
2007-07-30 00:10:42 +02:00
|
|
|
{
|
2007-08-02 22:15:52 +02:00
|
|
|
#if 1
|
|
|
|
/*
|
|
|
|
BUG#29549:
|
|
|
|
This is currently broken for NDB, which is using big-endian
|
|
|
|
order when packing length of BLOB. Once they have decided how to
|
|
|
|
fix the issue, we can enable the code below to make sure to
|
|
|
|
always read the length in little-endian order.
|
|
|
|
*/
|
2007-07-30 00:10:42 +02:00
|
|
|
Field_blob fb(m_field_metadata[col]);
|
2007-08-02 22:15:52 +02:00
|
|
|
length= fb.get_packed_size(master_data, TRUE);
|
|
|
|
#else
|
|
|
|
/*
|
|
|
|
Compute the length of the data. We cannot use get_length() here
|
|
|
|
since it is dependent on the specific table (and also checks the
|
|
|
|
packlength using the internal 'table' pointer) and replication
|
|
|
|
is using a fixed format for storing data in the binlog.
|
|
|
|
*/
|
|
|
|
switch (m_field_metadata[col]) {
|
|
|
|
case 1:
|
|
|
|
length= *master_data;
|
|
|
|
break;
|
|
|
|
case 2:
|
2007-09-14 17:22:41 +02:00
|
|
|
length= uint2korr(master_data);
|
2007-08-02 22:15:52 +02:00
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
length= uint3korr(master_data);
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
length= uint4korr(master_data);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
DBUG_ASSERT(0); // Should not come here
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
length+= m_field_metadata[col];
|
|
|
|
#endif
|
2006-05-03 15:00:38 +02:00
|
|
|
break;
|
|
|
|
}
|
2007-07-30 00:10:42 +02:00
|
|
|
default:
|
2008-01-30 17:35:25 +01:00
|
|
|
length= ~(uint32) 0;
|
2007-07-30 00:10:42 +02:00
|
|
|
}
|
2006-09-08 10:20:14 +02:00
|
|
|
return length;
|
2006-05-03 15:00:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Is the definition compatible with a table?
|
|
|
|
|
|
|
|
*/
|
|
|
|
int
|
2007-08-16 07:37:50 +02:00
|
|
|
table_def::compatible_with(Relay_log_info const *rli_arg, TABLE *table)
|
2006-05-03 15:00:38 +02:00
|
|
|
const
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
We only check the initial columns for the tables.
|
|
|
|
*/
|
|
|
|
uint const cols_to_check= min(table->s->fields, size());
|
|
|
|
int error= 0;
|
2007-08-16 07:37:50 +02:00
|
|
|
Relay_log_info const *rli= const_cast<Relay_log_info*>(rli_arg);
|
2006-05-03 15:00:38 +02:00
|
|
|
|
|
|
|
TABLE_SHARE const *const tsh= table->s;
|
|
|
|
|
|
|
|
for (uint col= 0 ; col < cols_to_check ; ++col)
|
|
|
|
{
|
BUG#37426: RBR breaks for CHAR() UTF-8 fields > 85 chars
In order to handle CHAR() fields, 8 bits were reserved for
the size of the CHAR field. However, instead of denoting the
number of characters in the field, field_length was used which
denotes the number of bytes in the field.
Since UTF-8 fields can have three bytes per character (and
has been extended to have four bytes per character in 6.0),
an extra two bits have been encoded in the field metadata
work for fields of type Field_string (i.e., CHAR fields).
Since the metadata word is filled, the extra bits have been
encoded in the upper 4 bits of the real type (the most
significant byte of the metadata word) by computing the
bitwise xor of the extra two bits. Since the upper 4 bits
of the real type always is 1111 for Field_string, this
means that for fields of length <256, the encoding is
identical to the encoding used in pre-5.1.26 servers, but
for lengths of 256 or more, an unrecognized type is formed,
causing an old slave (that does not handle lengths of 256
or more) to stop.
2008-06-30 22:11:18 +02:00
|
|
|
Field *const field= table->field[col];
|
|
|
|
if (field->type() != type(col))
|
2006-05-03 15:00:38 +02:00
|
|
|
{
|
|
|
|
DBUG_ASSERT(col < size() && col < tsh->fields);
|
|
|
|
DBUG_ASSERT(tsh->db.str && tsh->table_name.str);
|
|
|
|
error= 1;
|
2007-06-09 07:19:37 +02:00
|
|
|
char buf[256];
|
|
|
|
my_snprintf(buf, sizeof(buf), "Column %d type mismatch - "
|
|
|
|
"received type %d, %s.%s has type %d",
|
|
|
|
col, type(col), tsh->db.str, tsh->table_name.str,
|
BUG#37426: RBR breaks for CHAR() UTF-8 fields > 85 chars
In order to handle CHAR() fields, 8 bits were reserved for
the size of the CHAR field. However, instead of denoting the
number of characters in the field, field_length was used which
denotes the number of bytes in the field.
Since UTF-8 fields can have three bytes per character (and
has been extended to have four bytes per character in 6.0),
an extra two bits have been encoded in the field metadata
work for fields of type Field_string (i.e., CHAR fields).
Since the metadata word is filled, the extra bits have been
encoded in the upper 4 bits of the real type (the most
significant byte of the metadata word) by computing the
bitwise xor of the extra two bits. Since the upper 4 bits
of the real type always is 1111 for Field_string, this
means that for fields of length <256, the encoding is
identical to the encoding used in pre-5.1.26 servers, but
for lengths of 256 or more, an unrecognized type is formed,
causing an old slave (that does not handle lengths of 256
or more) to stop.
2008-06-30 22:11:18 +02:00
|
|
|
field->type());
|
2007-06-09 07:19:37 +02:00
|
|
|
rli->report(ERROR_LEVEL, ER_BINLOG_ROW_WRONG_TABLE_DEF,
|
|
|
|
ER(ER_BINLOG_ROW_WRONG_TABLE_DEF), buf);
|
2006-05-03 15:00:38 +02:00
|
|
|
}
|
2007-08-10 18:48:01 +02:00
|
|
|
/*
|
|
|
|
Check the slave's field size against that of the master.
|
|
|
|
*/
|
BUG#37426: RBR breaks for CHAR() UTF-8 fields > 85 chars
In order to handle CHAR() fields, 8 bits were reserved for
the size of the CHAR field. However, instead of denoting the
number of characters in the field, field_length was used which
denotes the number of bytes in the field.
Since UTF-8 fields can have three bytes per character (and
has been extended to have four bytes per character in 6.0),
an extra two bits have been encoded in the field metadata
work for fields of type Field_string (i.e., CHAR fields).
Since the metadata word is filled, the extra bits have been
encoded in the upper 4 bits of the real type (the most
significant byte of the metadata word) by computing the
bitwise xor of the extra two bits. Since the upper 4 bits
of the real type always is 1111 for Field_string, this
means that for fields of length <256, the encoding is
identical to the encoding used in pre-5.1.26 servers, but
for lengths of 256 or more, an unrecognized type is formed,
causing an old slave (that does not handle lengths of 256
or more) to stop.
2008-06-30 22:11:18 +02:00
|
|
|
if (!error &&
|
2010-03-17 15:28:49 +01:00
|
|
|
!field->compatible_field_size(field_metadata(col), rli_arg, m_flags))
|
2007-08-10 18:48:01 +02:00
|
|
|
{
|
|
|
|
error= 1;
|
|
|
|
char buf[256];
|
|
|
|
my_snprintf(buf, sizeof(buf), "Column %d size mismatch - "
|
|
|
|
"master has size %d, %s.%s on slave has size %d."
|
|
|
|
" Master's column size should be <= the slave's "
|
|
|
|
"column size.", col,
|
BUG#37426: RBR breaks for CHAR() UTF-8 fields > 85 chars
In order to handle CHAR() fields, 8 bits were reserved for
the size of the CHAR field. However, instead of denoting the
number of characters in the field, field_length was used which
denotes the number of bytes in the field.
Since UTF-8 fields can have three bytes per character (and
has been extended to have four bytes per character in 6.0),
an extra two bits have been encoded in the field metadata
work for fields of type Field_string (i.e., CHAR fields).
Since the metadata word is filled, the extra bits have been
encoded in the upper 4 bits of the real type (the most
significant byte of the metadata word) by computing the
bitwise xor of the extra two bits. Since the upper 4 bits
of the real type always is 1111 for Field_string, this
means that for fields of length <256, the encoding is
identical to the encoding used in pre-5.1.26 servers, but
for lengths of 256 or more, an unrecognized type is formed,
causing an old slave (that does not handle lengths of 256
or more) to stop.
2008-06-30 22:11:18 +02:00
|
|
|
field->pack_length_from_metadata(m_field_metadata[col]),
|
|
|
|
tsh->db.str, tsh->table_name.str,
|
|
|
|
field->row_pack_length());
|
2007-08-10 18:48:01 +02:00
|
|
|
rli->report(ERROR_LEVEL, ER_BINLOG_ROW_WRONG_TABLE_DEF,
|
|
|
|
ER(ER_BINLOG_ROW_WRONG_TABLE_DEF), buf);
|
|
|
|
}
|
2006-05-03 15:00:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|