diff options
author | Timothy Smith <timothy.smith@sun.com> | 2008-12-14 13:42:37 -0700 |
---|---|---|
committer | Timothy Smith <timothy.smith@sun.com> | 2008-12-14 13:42:37 -0700 |
commit | 220ee82cb7248dd9381805f0de3c737f97f3e577 (patch) | |
tree | 6143c8dce388d1d9b50fe12ba3f7f61cce1bbe8a /storage | |
parent | 968608efb7ed797879327d68afe4dc8ad06a1e4f (diff) | |
download | mariadb-git-220ee82cb7248dd9381805f0de3c737f97f3e577.tar.gz |
Apply InnoDB snapshot innodb-5.1-2858, part 7.
A follow-up fix for Bug 38839, which exposed a pre-existing bug in the
autoinc handling.
Detailed revision comments:
r2722 | sunny | 2008-10-04 02:48:04 +0300 (Sat, 04 Oct 2008) | 18 lines
branches/5.1: This bug has always existed but was masked by other errors. The
fix for bug# 38839 triggered this bug. When the offset and increment are > 1
we need to calculate the next value taking into consideration the two
variables. Previously we simply assumed they were 1 particularly offset was
never used. MySQL does its own calculation and that's probably why it seemed
to work in the past. We would return what we thought was the correct next
value and then MySQL would recalculate the actual value from that and return
it to the caller (e.g., handler::write_row()). Several new tests have been
added that try and catch some edge cases. The tests exposed a wrap around
error in MySQL next value calculation which was filed as bug 39828. The tests
will need to be updated once MySQL fix that bug.
One good side effect of this fix is that dict_table_t size has been
reduced by 8 bytes because we have moved the autoinc_increment field to
the row_prebuilt_t structure. See review-board for a detailed discussion.
rb://3
Diffstat (limited to 'storage')
-rw-r--r-- | storage/innobase/dict/dict0mem.c | 4 | ||||
-rw-r--r-- | storage/innobase/handler/ha_innodb.cc | 126 | ||||
-rw-r--r-- | storage/innobase/include/dict0mem.h | 6 | ||||
-rw-r--r-- | storage/innobase/include/row0mysql.h | 11 | ||||
-rw-r--r-- | storage/innobase/row/row0mysql.c | 8 |
5 files changed, 116 insertions, 39 deletions
diff --git a/storage/innobase/dict/dict0mem.c b/storage/innobase/dict/dict0mem.c index 47cf7a0bc9c..f9935b8db19 100644 --- a/storage/innobase/dict/dict0mem.c +++ b/storage/innobase/dict/dict0mem.c @@ -91,10 +91,6 @@ dict_mem_table_create( table->autoinc_inited = FALSE; - /* The actual increment value will be set by MySQL, we simply - default to 1 here.*/ - table->autoinc_increment = 1; - /* The number of transactions that are either waiting on the AUTOINC lock or have been granted the lock. */ table->n_waiting_or_granted_auto_inc_locks = 0; diff --git a/storage/innobase/handler/ha_innodb.cc b/storage/innobase/handler/ha_innodb.cc index 379269ee07f..f79af1e274c 100644 --- a/storage/innobase/handler/ha_innodb.cc +++ b/storage/innobase/handler/ha_innodb.cc @@ -922,6 +922,74 @@ innobase_convert_string( } /************************************************************************* +Compute the next autoinc value. + +For MySQL replication the autoincrement values can be partitioned among +the nodes. The offset is the start or origin of the autoincrement value +for a particular node. For n nodes the increment will be n and the offset +will be in the interval [1, n]. The formula tries to allocate the next +value for a particular node. + +Note: This function is also called with increment set to the number of +values we want to reserve for multi-value inserts e.g., + + INSERT INTO T VALUES(), (), (); + +innobase_next_autoinc() will be called with increment set to +n * 3 where autoinc_lock_mode != TRADITIONAL because we want +to reserve 3 values for the multi-value INSERT above. */ +static +ulonglong +innobase_next_autoinc( +/*==================*/ + /* out: the next value */ + ulonglong current, /* in: Current value */ + ulonglong increment, /* in: increment current by */ + ulonglong offset) /* in: AUTOINC offset */ +{ + ulonglong next_value; + + /* Should never be 0. */ + ut_a(increment > 0); + + if (offset <= 1) { + /* Offset 0 and 1 are the same, because there must be at + least one node in the system. */ + if (~0x0ULL - current <= increment) { + next_value = ~0x0ULL; + } else { + next_value = current + increment; + } + } else { + if (current > offset) { + next_value = ((current - offset) / increment) + 1; + } else { + next_value = ((offset - current) / increment) + 1; + } + + ut_a(increment > 0); + ut_a(next_value > 0); + + /* Check for multiplication overflow. */ + if (increment > (~0x0ULL / next_value)) { + + next_value = ~0x0ULL; + } else { + next_value *= increment; + + /* Check for overflow. */ + if (~0x0ULL - next_value <= offset) { + next_value = ~0x0ULL; + } else { + next_value += offset; + } + } + } + + return(next_value); +} + +/************************************************************************* Gets the InnoDB transaction handle for a MySQL handler object, creates an InnoDB transaction struct if the corresponding MySQL thread struct still lacks one. */ @@ -3565,22 +3633,18 @@ no_commit: update the table upper limit. Note: last_value will be 0 if get_auto_increment() was not called.*/ - if (auto_inc > prebuilt->last_value) { + if (auto_inc > prebuilt->autoinc_last_value) { set_max_autoinc: - ut_a(prebuilt->table->autoinc_increment > 0); + ut_a(prebuilt->autoinc_increment > 0); - ulonglong have; ulonglong need; + ulonglong offset; - /* Check for overflow conditions. */ - need = prebuilt->table->autoinc_increment; - have = ~0x0ULL - auto_inc; - - if (have < need) { - need = have; - } + offset = prebuilt->autoinc_offset; + need = prebuilt->autoinc_increment; - auto_inc += need; + auto_inc = innobase_next_autoinc( + auto_inc, need, offset); err = innobase_set_max_autoinc(auto_inc); @@ -3822,7 +3886,15 @@ ha_innobase::update_row( auto_inc = table->next_number_field->val_int(); if (auto_inc != 0) { - auto_inc += prebuilt->table->autoinc_increment; + + ulonglong need; + ulonglong offset; + + offset = prebuilt->autoinc_offset; + need = prebuilt->autoinc_increment; + + auto_inc = innobase_next_autoinc( + auto_inc, need, offset); error = innobase_set_max_autoinc(auto_inc); } @@ -5844,7 +5916,7 @@ ha_innobase::info( not be updated. This will force write_row() into attempting an update of the table's AUTOINC counter. */ - prebuilt->last_value = 0; + prebuilt->autoinc_last_value = 0; } stats.records = (ha_rows)n_rows; @@ -6474,7 +6546,7 @@ int ha_innobase::reset() it's safer to do it explicitly here. */ /* This is a statement level counter. */ - prebuilt->last_value = 0; + prebuilt->autoinc_last_value = 0; return(0); } @@ -7568,7 +7640,7 @@ ha_innobase::get_auto_increment( set_if_bigger(*first_value, autoinc); /* Not in the middle of a mult-row INSERT. */ - } else if (prebuilt->last_value == 0) { + } else if (prebuilt->autoinc_last_value == 0) { set_if_bigger(*first_value, autoinc); } @@ -7577,35 +7649,33 @@ ha_innobase::get_auto_increment( /* With old style AUTOINC locking we only update the table's AUTOINC counter after attempting to insert the row. */ if (innobase_autoinc_lock_mode != AUTOINC_OLD_STYLE_LOCKING) { - ulonglong have; ulonglong need; + ulonglong next_value; - /* Check for overflow conditions. */ need = *nb_reserved_values * increment; - have = ~0x0ULL - *first_value; - - if (have < need) { - need = have; - } /* Compute the last value in the interval */ - prebuilt->last_value = *first_value + need; + next_value = innobase_next_autoinc(*first_value, need, offset); + + prebuilt->autoinc_last_value = next_value; - ut_a(prebuilt->last_value >= *first_value); + ut_a(prebuilt->autoinc_last_value >= *first_value); /* Update the table autoinc variable */ dict_table_autoinc_update( - prebuilt->table, prebuilt->last_value); + prebuilt->table, prebuilt->autoinc_last_value); } else { /* This will force write_row() into attempting an update of the table's AUTOINC counter. */ - prebuilt->last_value = 0; + prebuilt->autoinc_last_value = 0; } /* The increment to be used to increase the AUTOINC value, we use this in write_row() and update_row() to increase the autoinc counter - for columns that are filled by the user.*/ - prebuilt->table->autoinc_increment = increment; + for columns that are filled by the user. We need the offset and + the increment. */ + prebuilt->autoinc_offset = offset; + prebuilt->autoinc_increment = increment; dict_table_autoinc_unlock(prebuilt->table); } diff --git a/storage/innobase/include/dict0mem.h b/storage/innobase/include/dict0mem.h index 2fe72498989..6e5435493cb 100644 --- a/storage/innobase/include/dict0mem.h +++ b/storage/innobase/include/dict0mem.h @@ -411,11 +411,6 @@ struct dict_table_struct{ SELECT MAX(auto inc column) */ ib_ulonglong autoinc;/* autoinc counter value to give to the next inserted row */ - - ib_longlong autoinc_increment; - /* The increment step of the auto increment - column. Value must be greater than or equal - to 1 */ ulong n_waiting_or_granted_auto_inc_locks; /* This counter is used to track the number of granted and pending autoinc locks on this @@ -425,6 +420,7 @@ struct dict_table_struct{ acquired the AUTOINC lock or not. Of course only one transaction can be granted the lock but there can be multiple waiters. */ + /*----------------------*/ #ifdef UNIV_DEBUG ulint magic_n;/* magic number */ diff --git a/storage/innobase/include/row0mysql.h b/storage/innobase/include/row0mysql.h index ca9d9c6b8f8..dd6b1d00676 100644 --- a/storage/innobase/include/row0mysql.h +++ b/storage/innobase/include/row0mysql.h @@ -683,7 +683,16 @@ struct row_prebuilt_struct { to this heap */ mem_heap_t* old_vers_heap; /* memory heap where a previous version is built in consistent read */ - ulonglong last_value; /* last value of AUTO-INC interval */ + /*----------------------*/ + ulonglong autoinc_last_value;/* last value of AUTO-INC interval */ + ulonglong autoinc_increment;/* The increment step of the auto + increment column. Value must be + greater than or equal to 1. Required to + calculate the next value */ + ulonglong autoinc_offset; /* The offset passed to + get_auto_increment() by MySQL. Required + to calculate the next value */ + /*----------------------*/ ulint magic_n2; /* this should be the same as magic_n */ }; diff --git a/storage/innobase/row/row0mysql.c b/storage/innobase/row/row0mysql.c index 74bf2267a3e..a3e2549f517 100644 --- a/storage/innobase/row/row0mysql.c +++ b/storage/innobase/row/row0mysql.c @@ -661,7 +661,13 @@ row_create_prebuilt( prebuilt->old_vers_heap = NULL; - prebuilt->last_value = 0; + prebuilt->autoinc_offset = 0; + + /* Default to 1, we will set the actual value later in + ha_innobase::get_auto_increment(). */ + prebuilt->autoinc_increment = 1; + + prebuilt->autoinc_last_value = 0; return(prebuilt); } |