mirror of
https://github.com/MariaDB/server.git
synced 2025-01-17 12:32:27 +01:00
6bf6272fda
bug #57006 "Deadlock between HANDLER and FLUSH TABLES WITH READ LOCK" and bug #54673 "It takes too long to get readlock for 'FLUSH TABLES WITH READ LOCK'". The first bug manifested itself as a deadlock which occurred when a connection, which had some table open through HANDLER statement, tried to update some data through DML statement while another connection tried to execute FLUSH TABLES WITH READ LOCK concurrently. What happened was that FTWRL in the second connection managed to perform first step of GRL acquisition and thus blocked all upcoming DML. After that it started to wait for table open through HANDLER statement to be flushed. When the first connection tried to execute DML it has started to wait for GRL/the second connection creating deadlock. The second bug manifested itself as starvation of FLUSH TABLES WITH READ LOCK statements in cases when there was a constant stream of concurrent DML statements (in two or more connections). This has happened because requests for protection against GRL which were acquired by DML statements were ignoring presence of pending GRL and thus the latter was starved. This patch solves both these problems by re-implementing GRL using metadata locks. Similar to the old implementation acquisition of GRL in new implementation is two-step. During the first step we block all concurrent DML and DDL statements by acquiring global S metadata lock (each DML and DDL statement acquires global IX lock for its duration). During the second step we block commits by acquiring global S lock in COMMIT namespace (commit code acquires global IX lock in this namespace). Note that unlike in old implementation acquisition of protection against GRL in DML and DDL is semi-automatic. We assume that any statement which should be blocked by GRL will either open and acquires write-lock on tables or acquires metadata locks on objects it is going to modify. For any such statement global IX metadata lock is automatically acquired for its duration. The first problem is solved because waits for GRL become visible to deadlock detector in metadata locking subsystem and thus deadlocks like one in the first bug become impossible. The second problem is solved because global S locks which are used for GRL implementation are given preference over IX locks which are acquired by concurrent DML (and we can switch to fair scheduling in future if needed). Important change: FTWRL/GRL no longer blocks DML and DDL on temporary tables. Before this patch behavior was not consistent in this respect: in some cases DML/DDL statements on temporary tables were blocked while in others they were not. Since the main use cases for FTWRL are various forms of backups and temporary tables are not preserved during backups we have opted for consistently allowing DML/DDL on temporary tables during FTWRL/GRL. Important change: This patch changes thread state names which are used when DML/DDL of FTWRL is waiting for global read lock. It is now either "Waiting for global read lock" or "Waiting for commit lock" depending on the stage on which FTWRL is. Incompatible change: To solve deadlock in events code which was exposed by this patch we have to replace LOCK_event_metadata mutex with metadata locks on events. As result we have to prohibit DDL on events under LOCK TABLES. This patch also adds extensive test coverage for interaction of DML/DDL and FTWRL. Performance of new and old global read lock implementations in sysbench tests were compared. There were no significant difference between new and old implementations. mysql-test/include/check_ftwrl_compatible.inc: Added helper script which allows to check that a statement is compatible with FLUSH TABLES WITH READ LOCK. mysql-test/include/check_ftwrl_incompatible.inc: Added helper script which allows to check that a statement is incompatible with FLUSH TABLES WITH READ LOCK. mysql-test/include/handler.inc: Adjusted test case to the fact that now DROP TABLE closes open HANDLERs for the table to be dropped before checking if there active FTWRL in this connection. mysql-test/include/wait_show_condition.inc: Fixed small error in the timeout message. The correct name of variable used as parameter for this script is "$condition" and not "$wait_condition". mysql-test/r/delayed.result: Added test coverage for scenario which triggered assert in metadata locking subsystem. mysql-test/r/events_2.result: Updated test results after prohibiting event DDL operations under LOCK TABLES. mysql-test/r/flush.result: Added test coverage for bug #57006 "Deadlock between HANDLER and FLUSH TABLES WITH READ LOCK". mysql-test/r/flush_read_lock.result: Added test coverage for various aspects of FLUSH TABLES WITH READ LOCK functionality. mysql-test/r/flush_read_lock_kill.result: Adjusted test case after replacing custom global read lock implementation with one based on metadata locks. Use new debug_sync point. Do not disable concurrent inserts as now InnoDB we always use InnoDB table. mysql-test/r/handler_innodb.result: Adjusted test case to the fact that now DROP TABLE closes open HANDLERs for the table to be dropped before checking if there active FTWRL in this connection. mysql-test/r/handler_myisam.result: Adjusted test case to the fact that now DROP TABLE closes open HANDLERs for the table to be dropped before checking if there active FTWRL in this connection. mysql-test/r/mdl_sync.result: Adjusted test case after replacing custom global read lock implementation with one based on metadata locks. Replaced usage of GRL-specific debug_sync's with appropriate sync points in MDL subsystem. mysql-test/suite/perfschema/r/dml_setup_instruments.result: Updated test results after removing global COND_global_read_lock condition variable. mysql-test/suite/perfschema/r/func_file_io.result: Ensure that this test doesn't affect subsequent tests. At the end of its execution enable back P_S instrumentation which this test disables at some point. mysql-test/suite/perfschema/r/func_mutex.result: Ensure that this test doesn't affect subsequent tests. At the end of its execution enable back P_S instrumentation which this test disables at some point. mysql-test/suite/perfschema/r/global_read_lock.result: Adjusted test case to take into account that new GRL implementation is based on MDL. mysql-test/suite/perfschema/r/server_init.result: Adjusted test case after replacing custom global read lock implementation with one based on MDL and replacing LOCK_event_metadata mutex with metadata lock. mysql-test/suite/perfschema/t/func_file_io.test: Ensure that this test doesn't affect subsequent tests. At the end of its execution enable back P_S instrumentation which this test disables at some point. mysql-test/suite/perfschema/t/func_mutex.test: Ensure that this test doesn't affect subsequent tests. At the end of its execution enable back P_S instrumentation which this test disables at some point. mysql-test/suite/perfschema/t/global_read_lock.test: Adjusted test case to take into account that new GRL implementation is based on MDL. mysql-test/suite/perfschema/t/server_init.test: Adjusted test case after replacing custom global read lock implementation with one based on MDL and replacing LOCK_event_metadata mutex with metadata lock. mysql-test/suite/rpl/r/rpl_tmp_table_and_DDL.result: Updated test results after prohibiting event DDL under LOCK TABLES. mysql-test/t/delayed.test: Added test coverage for scenario which triggered assert in metadata locking subsystem. mysql-test/t/events_2.test: Updated test case after prohibiting event DDL operations under LOCK TABLES. mysql-test/t/flush.test: Added test coverage for bug #57006 "Deadlock between HANDLER and FLUSH TABLES WITH READ LOCK". mysql-test/t/flush_block_commit.test: Adjusted test case after changing thread state name which is used when COMMIT waits for FLUSH TABLES WITH READ LOCK from "Waiting for release of readlock" to "Waiting for commit lock". mysql-test/t/flush_block_commit_notembedded.test: Adjusted test case after changing thread state name which is used when DML waits for FLUSH TABLES WITH READ LOCK. Now we use "Waiting for global read lock" in this case. mysql-test/t/flush_read_lock.test: Added test coverage for various aspects of FLUSH TABLES WITH READ LOCK functionality. mysql-test/t/flush_read_lock_kill-master.opt: We no longer need to use make_global_read_lock_block_commit_loop debug tag in this test. Instead we rely on an appropriate debug_sync point in MDL code. mysql-test/t/flush_read_lock_kill.test: Adjusted test case after replacing custom global read lock implementation with one based on metadata locks. Use new debug_sync point. Do not disable concurrent inserts as now InnoDB we always use InnoDB table. mysql-test/t/lock_multi.test: Adjusted test case after changing thread state names which are used when DML or DDL waits for FLUSH TABLES WITH READ LOCK to "Waiting for global read lock". mysql-test/t/mdl_sync.test: Adjusted test case after replacing custom global read lock implementation with one based on metadata locks. Replaced usage of GRL-specific debug_sync's with appropriate sync points in MDL subsystem. Updated thread state names which are used when DDL waits for FTWRL. mysql-test/t/trigger_notembedded.test: Adjusted test case after changing thread state names which are used when DML or DDL waits for FLUSH TABLES WITH READ LOCK to "Waiting for global read lock". sql/event_data_objects.cc: Removed Event_queue_element::status/last_executed_changed members and Event_queue_element::update_timing_fields() method. We no longer use this class for updating mysql.events once event is chosen for execution. Accesses to instances of this class in scheduler thread require protection by Event_queue::LOCK_event_queue mutex and we try to avoid updating table while holding this lock. sql/event_data_objects.h: Removed Event_queue_element::status/last_executed_changed members and Event_queue_element::update_timing_fields() method. We no longer use this class for updating mysql.events once event is chosen for execution. Accesses to instances of this class in scheduler thread require protection by Event_queue::LOCK_event_queue mutex and we try to avoid updating table while holding this lock. sql/event_db_repository.cc: - Changed Event_db_repository methods to not release all metadata locks once they are done updating mysql.events table. This allows to keep metadata lock protecting against GRL and lock protecting particular event around until corresponding DDL statement is written to the binary log. - Removed logic for conditional update of "status" and "last_executed" fields from update_timing_fields_for_event() method. In the only case when this method is called now "last_executed" is always modified and tracking change of "status" is too much hassle. sql/event_db_repository.h: Removed logic for conditional update of "status" and "last_executed" fields from Event_db_repository:: update_timing_fields_for_event() method. In the only case when this method is called now "last_executed" is always modified and tracking change of "status" field is too much hassle. sql/event_queue.cc: Changed event scheduler code not to update mysql.events table while holding Event_queue::LOCK_event_queue mutex. Doing so led to a deadlock with a new GRL implementation. This deadlock didn't occur with old implementation due to fact that code acquiring protection against GRL ignored pending GRL requests (which lead to GRL starvation). One of goals of new implementation is to disallow GRL starvation and so we have to solve problem with this deadlock in a different way. sql/events.cc: Changed methods of Events class to acquire protection against GRL while perfoming DDL statement and keep it until statement is written to the binary log. Unfortunately this step together with new GRL implementation exposed deadlock involving Events::LOCK_event_metadata and GRL. To solve it Events::LOCK_event_metadata mutex was replaced with a metadata lock on event. As a side-effect events DDL has to be prohibited under LOCK TABLES even in cases when mysql.events table was explicitly locked for write. sql/events.h: Replaced Events::LOCK_event_metadata mutex with a metadata lock on event. sql/ha_ndbcluster.cc: Updated code after replacing custom global read lock implementation with one based on MDL. Since MDL subsystem should now be able to detect deadlocks involving metadata locks and GRL there is no need for special handling of active GRL. sql/handler.cc: Replaced custom implementation of global read lock with one based on metadata locks. Consequently when doing commit instead of calling method of Global_read_lock class to acquire protection against GRL we simply acquire IX in COMMIT namespace. sql/lock.cc: Replaced custom implementation of global read lock with one based on metadata locks. This step allows to expose wait for GRL to deadlock detector of MDL subsystem and thus succesfully resolve deadlocks similar to one behind bug #57006 "Deadlock between HANDLER and FLUSH TABLES WITH READ LOCK". It also solves problem with GRL starvation described in bug #54673 "It takes too long to get readlock for 'FLUSH TABLES WITH READ LOCK'" since metadata locks used by GRL give preference to FTWRL statement instead of DML statements (if needed in future this can be changed to fair scheduling). Similar to old implementation of acquisition of GRL is two-step. During the first step we block all concurrent DML and DDL statements by acquiring global S metadata lock (each DML and DDL statement acquires global IX lock for its duration). During the second step we block commits by acquiring global S lock in COMMIT namespace (commit code acquires global IX lock in this namespace). Note that unlike in old implementation acquisition of protection against GRL in DML and DDL is semi-automatic. We assume that any statement which should be blocked by GRL will either open and acquires write-lock on tables or acquires metadata locks on objects it is going to modify. For any such statement global IX metadata lock is automatically acquired for its duration. To support this change: - Global_read_lock::lock/unlock_global_read_lock and make_global_read_lock_block_commit methods were changed accordingly. - Global_read_lock::wait_if_global_read_lock() and start_waiting_global_read_lock() methods were dropped. It is now responsibility of code acquiring metadata locks opening tables to acquire protection against GRL by explicitly taking global IX lock with statement duration. - Global variables, mutex and condition variable used by old implementation was removed. - lock_routine_name() was changed to use statement duration for its global IX lock. It was also renamed to lock_object_name() as it now also used to take metadata locks on events. - Global_read_lock::set_explicit_lock_duration() was added which allows not to release locks used for GRL when leaving prelocked mode. sql/lock.h: - Renamed lock_routine_name() to lock_object_name() and changed its signature to allow its usage for events. - Removed broadcast_refresh() function. It is no longer needed with new GRL implementation. sql/log_event.cc: Release metadata locks with statement duration at the end of processing legacy event for LOAD DATA. This ensures that replication thread processing such event properly releases its protection against global read lock. sql/mdl.cc: Changed MDL subsystem to support new MDL-based implementation of global read lock. Added COMMIT and EVENTS namespaces for metadata locks. Changed thread state name for GLOBAL namespace to "Waiting for global read lock". Optimized MDL_map::find_or_insert() method to avoid taking m_mutex mutex when looking up MDL_lock objects for GLOBAL or COMMIT namespaces. We keep pre-created MDL_lock objects for these namespaces around and simply return pointers to these global objects when needed. Changed MDL_lock/MDL_scoped_lock to properly handle notification of insert delayed handler threads when FTWRL takes global S lock. Introduced concept of lock duration. In addition to locks with transaction duration which work in the way which is similar to how locks worked before (i.e. they are released at the end of transaction), locks with statement and explicit duration were introduced. Locks with statement duration are automatically released at the end of statement. Locks with explicit duration require explicit release and obsolete concept of transactional sentinel. * Changed MDL_request and MDL_ticket classes to support notion of duration. * Changed MDL_context to keep locks with different duration in different lists. Changed code handling ticket list to take this into account. * Changed methods responsible for releasing locks to take into account duration of tickets. Particularly public MDL_context::release_lock() method now only can release tickets with explicit duration (there is still internal method which allows to specify duration). To release locks with statement or transaction duration one have to use release_statement/transactional_locks() methods. * Concept of savepoint for MDL subsystem now has to take into account locks with statement duration. Consequently MDL_savepoint class was introduced and methods working with savepoints were updated accordingly. * Added methods which allow to set duration for one or all locks in the context. sql/mdl.h: Changed MDL subsystem to support new MDL-based implementation of global read lock. Added COMMIT and EVENTS namespaces for metadata locks. Introduced concept of lock duration. In addition to locks with transaction duration which work in the way which is similar to how locks worked before (i.e. they are released at the end of transaction), locks with statement and explicit duration were introduced. Locks with statement duration are automatically released at the end of statement. Locks with explicit duration require explicit release and obsolete concept of transactional sentinel. * Changed MDL_request and MDL_ticket classes to support notion of duration. * Changed MDL_context to keep locks with different duration in different lists. Changed code handling ticket list to take this into account. * Changed methods responsible for releasing locks to take into account duration of tickets. Particularly public MDL_context::release_lock() method now only can release tickets with explicit duration (there is still internal method which allows to specify duration). To release locks with statement or transaction duration one have to use release_statement/transactional_locks() methods. * Concept of savepoint for MDL subsystem now has to take into account locks with statement duration. Consequently MDL_savepoint class was introduced and methods working with savepoints were updated accordingly. * Added methods which allow to set duration for one or all locks in the context. sql/mysqld.cc: Removed global mutex and condition variables which were used by old implementation of GRL. Also we no longer need to initialize Events::LOCK_event_metadata mutex as it was replaced with metadata locks on events. sql/mysqld.h: Removed global variable, mutex and condition variables which were used by old implementation of GRL. sql/rpl_rli.cc: When slave thread closes tables which were open for handling of RBR events ensure that it releases global IX lock which was acquired as protection against GRL. sql/sp.cc: Adjusted code to the new signature of lock_object/routine_name(), to the fact that one now needs specify duration of lock when initializing MDL_request and to the fact that savepoints for MDL subsystem are now represented by MDL_savepoint class. sql/sp_head.cc: Ensure that statements in stored procedures release statement metadata locks and thus release their protectiong against GRL in proper moment in time. Adjusted code to the fact that one now needs specify duration of lock when initializing MDL_request. sql/sql_admin.cc: Adjusted code to the fact that one now needs specify duration of lock when initializing MDL_request. sql/sql_base.cc: - Implemented support for new approach to acquiring protection against global read lock. We no longer acquire such protection explicitly on the basis of statement flags. Instead we always rely on code which is responsible for acquiring metadata locks on object to be changed acquiring this protection. This is achieved by acquiring global IX metadata lock with statement duration. Code doing this also responsible for checking that current connection has no active GRL by calling an Global_read_lock::can_acquire_protection() method. Changed code in open_table() and lock_table_names() accordingly. Note that as result of this change DDL and DML on temporary tables is always compatible with GRL (before it was incompatible in some cases and compatible in other cases). - To speed-up code acquiring protection against GRL introduced m_has_protection_against_grl member in Open_table_context class. It indicates that protection was already acquired sometime during open_tables() execution and new attempts can be skipped. - Thanks to new GRL implementation calls to broadcast_refresh() became unnecessary and were removed. - Adjusted code to the fact that one now needs specify duration of lock when initializing MDL_request and to the fact that savepoints for MDL subsystem are now represented by MDL_savepoint class. sql/sql_base.h: Adjusted code to the fact that savepoints for MDL subsystem are now represented by MDL_savepoint class. Also introduced Open_table_context::m_has_protection_against_grl member which allows to avoid acquiring protection against GRL while opening tables if such protection was already acquired. sql/sql_class.cc: Changed THD::leave_locked_tables_mode() after transactional sentinel for metadata locks was obsoleted by introduction of locks with explicit duration. sql/sql_class.h: - Adjusted code to the fact that savepoints for MDL subsystem are now represented by MDL_savepoint class. - Changed Global_read_lock class according to changes in global read lock implementation: * wait_if_global_read_lock and start_waiting_global_read_lock are now gone. Instead code needing protection against GRL has to acquire global IX metadata lock with statement duration itself. To help it new can_acquire_protection() was introduced. Also as result of the above change m_protection_count member is gone too. * Added m_mdl_blocks_commits_lock member to store metadata lock blocking commits. * Adjusted code to the fact that concept of transactional sentinel was obsoleted by concept of lock duration. - Removed CF_PROTECT_AGAINST_GRL flag as it is no longer necessary. New GRL implementation acquires protection against global read lock automagically when statement acquires metadata locks on tables or other objects it is going to change. sql/sql_db.cc: Adjusted code to the fact that one now needs specify duration of lock when initializing MDL_request. sql/sql_handler.cc: Removed call to broadcast_refresh() function. It is no longer needed with new GRL implementation. Adjusted code after introducing duration concept for metadata locks. Particularly to the fact transactional sentinel was replaced with explicit duration. sql/sql_handler.h: Renamed mysql_ha_move_tickets_after_trans_sentinel() to mysql_ha_set_explicit_lock_duration() after transactional sentinel was obsoleted by locks with explicit duration. sql/sql_insert.cc: Adjusted code handling delaying inserts after switching to new GRL implementation. Now connection thread initiating delayed insert has to acquire global IX lock in addition to metadata lock on table being inserted into. This IX lock protects against GRL and similarly to SW lock on table being inserted into has to be passed to handler thread in order to avoid deadlocks. sql/sql_lex.cc: LEX::protect_against_global_read_lock member is no longer necessary since protection against GRL is automatically taken by code acquiring metadata locks/opening tables. sql/sql_lex.h: LEX::protect_against_global_read_lock member is no longer necessary since protection against GRL is automatically taken by code acquiring metadata locks/opening tables. sql/sql_parse.cc: - Implemented support for new approach to acquiring protection against global read lock. We no longer acquire such protection explicitly on the basis of statement flags. Instead we always rely on code which is responsible for acquiring metadata locks on object to be changed acquiring this protection. This is achieved by acquiring global IX metadata lock with statement duration. This lock is automatically released at the end of statement execution. - Changed implementation of CREATE/DROP PROCEDURE/FUNCTION not to release metadata locks and thus protection against of GRL in the middle of statement execution. - Adjusted code to the fact that one now needs specify duration of lock when initializing MDL_request and to the fact that savepoints for MDL subsystem are now represented by MDL_savepoint class. sql/sql_prepare.cc: Adjusted code to the to the fact that savepoints for MDL subsystem are now represented by MDL_savepoint class. sql/sql_rename.cc: With new GRL implementation there is no need to explicitly acquire protection against GRL before renaming tables. This happens automatically in code which acquires metadata locks on tables being renamed. sql/sql_show.cc: Adjusted code to the fact that one now needs specify duration of lock when initializing MDL_request and to the fact that savepoints for MDL subsystem are now represented by MDL_savepoint class. sql/sql_table.cc: - With new GRL implementation there is no need to explicitly acquire protection against GRL before dropping tables. This happens automatically in code which acquires metadata locks on tables being dropped. - Changed mysql_alter_table() not to release lock on new table name explicitly and to rely on automatic release of locks at the end of statement instead. This was necessary since now MDL_context::release_lock() is supported only for locks for explicit duration. sql/sql_trigger.cc: With new GRL implementation there is no need to explicitly acquire protection against GRL before changing table triggers. This happens automatically in code which acquires metadata locks on tables which triggers are to be changed. sql/sql_update.cc: Fix bug exposed by GRL testing. During prepare phase acquire only S metadata locks instead of SW locks to keep prepare of multi-UPDATE compatible with concurrent LOCK TABLES WRITE and global read lock. sql/sql_view.cc: With new GRL implementation there is no need to explicitly acquire protection against GRL before creating view. This happens automatically in code which acquires metadata lock on view to be created. sql/sql_yacc.yy: LEX::protect_against_global_read_lock member is no longer necessary since protection against GRL is automatically taken by code acquiring metadata locks/opening tables. sql/table.cc: Adjusted code to the fact that one now needs specify duration of lock when initializing MDL_request. sql/table.h: Adjusted code to the fact that one now needs specify duration of lock when initializing MDL_request. sql/transaction.cc: Replaced custom implementation of global read lock with one based on metadata locks. Consequently when doing commit instead of calling method of Global_read_lock class to acquire protection against GRL we simply acquire IX in COMMIT namespace. Also adjusted code to the fact that MDL savepoint is now represented by MDL_savepoint class.
994 lines
28 KiB
C++
994 lines
28 KiB
C++
/* Copyright (C) 2000-2004 MySQL AB, 2008-2009 Sun Microsystems, Inc
|
|
|
|
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 */
|
|
|
|
|
|
/* HANDLER ... commands - direct access to ISAM */
|
|
|
|
/* TODO:
|
|
HANDLER blabla OPEN [ AS foobar ] [ (column-list) ]
|
|
|
|
the most natural (easiest, fastest) way to do it is to
|
|
compute List<Item> field_list not in mysql_ha_read
|
|
but in mysql_ha_open, and then store it in TABLE structure.
|
|
|
|
The problem here is that mysql_parse calls free_item to free all the
|
|
items allocated at the end of every query. The workaround would to
|
|
keep two item lists per THD - normal free_list and handler_items.
|
|
The second is to be freeed only on thread end. mysql_ha_open should
|
|
then do { handler_items=concat(handler_items, free_list); free_list=0; }
|
|
|
|
But !!! do_command calls free_root at the end of every query and frees up
|
|
all the sql_alloc'ed memory. It's harder to work around...
|
|
*/
|
|
|
|
/*
|
|
The information about open HANDLER objects is stored in a HASH.
|
|
It holds objects of type TABLE_LIST, which are indexed by table
|
|
name/alias, and allows us to quickly find a HANDLER table for any
|
|
operation at hand - be it HANDLER READ or HANDLER CLOSE.
|
|
|
|
It also allows us to maintain an "open" HANDLER even in cases
|
|
when there is no physically open cursor. E.g. a FLUSH TABLE
|
|
statement in this or some other connection demands that all open
|
|
HANDLERs against the flushed table are closed. In order to
|
|
preserve the information about an open HANDLER, we don't perform
|
|
a complete HANDLER CLOSE, but only close the TABLE object. The
|
|
corresponding TABLE_LIST is kept in the cache with 'table'
|
|
pointer set to NULL. The table will be reopened on next access
|
|
(this, however, leads to loss of cursor position, unless the
|
|
cursor points at the first record).
|
|
*/
|
|
|
|
#include "sql_priv.h"
|
|
#include "sql_handler.h"
|
|
#include "unireg.h" // REQUIRED: for other includes
|
|
#include "sql_base.h" // close_thread_tables
|
|
#include "lock.h" // mysql_unlock_tables
|
|
#include "key.h" // key_copy
|
|
#include "sql_base.h" // insert_fields
|
|
#include "sql_select.h"
|
|
#include "transaction.h"
|
|
|
|
#define HANDLER_TABLES_HASH_SIZE 120
|
|
|
|
static enum enum_ha_read_modes rkey_to_rnext[]=
|
|
{ RNEXT_SAME, RNEXT, RPREV, RNEXT, RPREV, RNEXT, RPREV, RPREV };
|
|
|
|
/*
|
|
Get hash key and hash key length.
|
|
|
|
SYNOPSIS
|
|
mysql_ha_hash_get_key()
|
|
tables Pointer to the hash object.
|
|
key_len_p (out) Pointer to the result for key length.
|
|
first Unused.
|
|
|
|
DESCRIPTION
|
|
The hash object is an TABLE_LIST struct.
|
|
The hash key is the alias name.
|
|
The hash key length is the alias name length plus one for the
|
|
terminateing NUL character.
|
|
|
|
RETURN
|
|
Pointer to the TABLE_LIST struct.
|
|
*/
|
|
|
|
static char *mysql_ha_hash_get_key(TABLE_LIST *tables, size_t *key_len_p,
|
|
my_bool first __attribute__((unused)))
|
|
{
|
|
*key_len_p= strlen(tables->alias) + 1 ; /* include '\0' in comparisons */
|
|
return tables->alias;
|
|
}
|
|
|
|
|
|
/*
|
|
Free an hash object.
|
|
|
|
SYNOPSIS
|
|
mysql_ha_hash_free()
|
|
tables Pointer to the hash object.
|
|
|
|
DESCRIPTION
|
|
The hash object is an TABLE_LIST struct.
|
|
|
|
RETURN
|
|
Nothing
|
|
*/
|
|
|
|
static void mysql_ha_hash_free(TABLE_LIST *tables)
|
|
{
|
|
my_free(tables);
|
|
}
|
|
|
|
/**
|
|
Close a HANDLER table.
|
|
|
|
@param thd Thread identifier.
|
|
@param tables A list of tables with the first entry to close.
|
|
|
|
@note Though this function takes a list of tables, only the first list entry
|
|
will be closed.
|
|
@note Broadcasts refresh if it closed a table with old version.
|
|
*/
|
|
|
|
static void mysql_ha_close_table(THD *thd, TABLE_LIST *tables)
|
|
{
|
|
|
|
if (tables->table && !tables->table->s->tmp_table)
|
|
{
|
|
/* Non temporary table. */
|
|
tables->table->file->ha_index_or_rnd_end();
|
|
tables->table->open_by_handler= 0;
|
|
(void) close_thread_table(thd, &tables->table);
|
|
thd->mdl_context.release_lock(tables->mdl_request.ticket);
|
|
}
|
|
else if (tables->table)
|
|
{
|
|
/* Must be a temporary table */
|
|
TABLE *table= tables->table;
|
|
table->file->ha_index_or_rnd_end();
|
|
table->query_id= thd->query_id;
|
|
table->open_by_handler= 0;
|
|
mark_tmp_table_for_reuse(table);
|
|
}
|
|
|
|
/* Mark table as closed, ready for re-open if necessary. */
|
|
tables->table= NULL;
|
|
/* Safety, cleanup the pointer to satisfy MDL assertions. */
|
|
tables->mdl_request.ticket= NULL;
|
|
}
|
|
|
|
/*
|
|
Open a HANDLER table.
|
|
|
|
SYNOPSIS
|
|
mysql_ha_open()
|
|
thd Thread identifier.
|
|
tables A list of tables with the first entry to open.
|
|
reopen Re-open a previously opened handler table.
|
|
|
|
DESCRIPTION
|
|
Though this function takes a list of tables, only the first list entry
|
|
will be opened.
|
|
'reopen' is set when a handler table is to be re-opened. In this case,
|
|
'tables' is the pointer to the hashed TABLE_LIST object which has been
|
|
saved on the original open.
|
|
'reopen' is also used to suppress the sending of an 'ok' message.
|
|
|
|
RETURN
|
|
FALSE OK
|
|
TRUE Error
|
|
*/
|
|
|
|
bool mysql_ha_open(THD *thd, TABLE_LIST *tables, bool reopen)
|
|
{
|
|
TABLE_LIST *hash_tables = NULL;
|
|
char *db, *name, *alias;
|
|
uint dblen, namelen, aliaslen, counter;
|
|
bool error;
|
|
TABLE *backup_open_tables;
|
|
MDL_savepoint mdl_savepoint;
|
|
DBUG_ENTER("mysql_ha_open");
|
|
DBUG_PRINT("enter",("'%s'.'%s' as '%s' reopen: %d",
|
|
tables->db, tables->table_name, tables->alias,
|
|
(int) reopen));
|
|
|
|
if (thd->locked_tables_mode)
|
|
{
|
|
my_error(ER_LOCK_OR_ACTIVE_TRANSACTION, MYF(0));
|
|
DBUG_RETURN(TRUE);
|
|
}
|
|
if (tables->schema_table)
|
|
{
|
|
my_error(ER_WRONG_USAGE, MYF(0), "HANDLER OPEN",
|
|
INFORMATION_SCHEMA_NAME.str);
|
|
DBUG_PRINT("exit",("ERROR"));
|
|
DBUG_RETURN(TRUE);
|
|
}
|
|
|
|
if (! my_hash_inited(&thd->handler_tables_hash))
|
|
{
|
|
/*
|
|
HASH entries are of type TABLE_LIST.
|
|
*/
|
|
if (my_hash_init(&thd->handler_tables_hash, &my_charset_latin1,
|
|
HANDLER_TABLES_HASH_SIZE, 0, 0,
|
|
(my_hash_get_key) mysql_ha_hash_get_key,
|
|
(my_hash_free_key) mysql_ha_hash_free, 0))
|
|
{
|
|
DBUG_PRINT("exit",("ERROR"));
|
|
DBUG_RETURN(TRUE);
|
|
}
|
|
}
|
|
else if (! reopen) /* Otherwise we have 'tables' already. */
|
|
{
|
|
if (my_hash_search(&thd->handler_tables_hash, (uchar*) tables->alias,
|
|
strlen(tables->alias) + 1))
|
|
{
|
|
DBUG_PRINT("info",("duplicate '%s'", tables->alias));
|
|
DBUG_PRINT("exit",("ERROR"));
|
|
my_error(ER_NONUNIQ_TABLE, MYF(0), tables->alias);
|
|
DBUG_RETURN(TRUE);
|
|
}
|
|
}
|
|
|
|
if (! reopen)
|
|
{
|
|
/* copy the TABLE_LIST struct */
|
|
dblen= strlen(tables->db) + 1;
|
|
namelen= strlen(tables->table_name) + 1;
|
|
aliaslen= strlen(tables->alias) + 1;
|
|
if (!(my_multi_malloc(MYF(MY_WME),
|
|
&hash_tables, (uint) sizeof(*hash_tables),
|
|
&db, (uint) dblen,
|
|
&name, (uint) namelen,
|
|
&alias, (uint) aliaslen,
|
|
NullS)))
|
|
{
|
|
DBUG_PRINT("exit",("ERROR"));
|
|
DBUG_RETURN(TRUE);
|
|
}
|
|
/* structure copy */
|
|
*hash_tables= *tables;
|
|
hash_tables->db= db;
|
|
hash_tables->table_name= name;
|
|
hash_tables->alias= alias;
|
|
memcpy(hash_tables->db, tables->db, dblen);
|
|
memcpy(hash_tables->table_name, tables->table_name, namelen);
|
|
memcpy(hash_tables->alias, tables->alias, aliaslen);
|
|
/*
|
|
We can't request lock with explicit duration for this table
|
|
right from the start as open_tables() can't handle properly
|
|
back-off for such locks.
|
|
*/
|
|
hash_tables->mdl_request.init(MDL_key::TABLE, db, name, MDL_SHARED,
|
|
MDL_TRANSACTION);
|
|
/* for now HANDLER can be used only for real TABLES */
|
|
hash_tables->required_type= FRMTYPE_TABLE;
|
|
|
|
/* add to hash */
|
|
if (my_hash_insert(&thd->handler_tables_hash, (uchar*) hash_tables))
|
|
{
|
|
my_free(hash_tables);
|
|
DBUG_PRINT("exit",("ERROR"));
|
|
DBUG_RETURN(TRUE);
|
|
}
|
|
}
|
|
else
|
|
hash_tables= tables;
|
|
|
|
/*
|
|
Save and reset the open_tables list so that open_tables() won't
|
|
be able to access (or know about) the previous list. And on return
|
|
from open_tables(), thd->open_tables will contain only the opened
|
|
table.
|
|
|
|
See open_table() back-off comments for more details.
|
|
*/
|
|
backup_open_tables= thd->open_tables;
|
|
thd->set_open_tables(NULL);
|
|
mdl_savepoint= thd->mdl_context.mdl_savepoint();
|
|
|
|
/*
|
|
open_tables() will set 'hash_tables->table' if successful.
|
|
It must be NULL for a real open when calling open_tables().
|
|
*/
|
|
DBUG_ASSERT(! hash_tables->table);
|
|
|
|
/*
|
|
We use open_tables() here, rather than, say,
|
|
open_ltable() or open_table() because we would like to be able
|
|
to open a temporary table.
|
|
*/
|
|
error= open_tables(thd, &hash_tables, &counter, 0);
|
|
|
|
if (! error &&
|
|
! (hash_tables->table->file->ha_table_flags() & HA_CAN_SQL_HANDLER))
|
|
{
|
|
my_error(ER_ILLEGAL_HA, MYF(0), tables->alias);
|
|
error= TRUE;
|
|
}
|
|
if (!error &&
|
|
hash_tables->mdl_request.ticket &&
|
|
thd->mdl_context.has_lock(mdl_savepoint,
|
|
hash_tables->mdl_request.ticket))
|
|
{
|
|
/* The ticket returned is within a savepoint. Make a copy. */
|
|
error= thd->mdl_context.clone_ticket(&hash_tables->mdl_request);
|
|
hash_tables->table->mdl_ticket= hash_tables->mdl_request.ticket;
|
|
}
|
|
if (error)
|
|
{
|
|
/*
|
|
No need to rollback statement transaction, it's not started.
|
|
If called with reopen flag, no need to rollback either,
|
|
it will be done at statement end.
|
|
*/
|
|
DBUG_ASSERT(thd->transaction.stmt.is_empty());
|
|
close_thread_tables(thd);
|
|
thd->mdl_context.rollback_to_savepoint(mdl_savepoint);
|
|
thd->set_open_tables(backup_open_tables);
|
|
if (!reopen)
|
|
my_hash_delete(&thd->handler_tables_hash, (uchar*) hash_tables);
|
|
else
|
|
{
|
|
hash_tables->table= NULL;
|
|
/* Safety, cleanup the pointer to satisfy MDL assertions. */
|
|
hash_tables->mdl_request.ticket= NULL;
|
|
}
|
|
DBUG_PRINT("exit",("ERROR"));
|
|
DBUG_RETURN(TRUE);
|
|
}
|
|
thd->set_open_tables(backup_open_tables);
|
|
if (hash_tables->mdl_request.ticket)
|
|
{
|
|
thd->mdl_context.set_lock_duration(hash_tables->mdl_request.ticket,
|
|
MDL_EXPLICIT);
|
|
thd->mdl_context.set_needs_thr_lock_abort(TRUE);
|
|
}
|
|
|
|
/*
|
|
Assert that the above check prevents opening of views and merge tables.
|
|
For temporary tables, TABLE::next can be set even if only one table
|
|
was opened for HANDLER as it is used to link them together
|
|
(see thd->temporary_tables).
|
|
*/
|
|
DBUG_ASSERT(hash_tables->table->next == NULL ||
|
|
hash_tables->table->s->tmp_table);
|
|
/*
|
|
If it's a temp table, don't reset table->query_id as the table is
|
|
being used by this handler. For non-temp tables we use this flag
|
|
in asserts.
|
|
*/
|
|
hash_tables->table->open_by_handler= 1;
|
|
|
|
if (! reopen)
|
|
my_ok(thd);
|
|
DBUG_PRINT("exit",("OK"));
|
|
DBUG_RETURN(FALSE);
|
|
}
|
|
|
|
|
|
/*
|
|
Close a HANDLER table by alias or table name
|
|
|
|
SYNOPSIS
|
|
mysql_ha_close()
|
|
thd Thread identifier.
|
|
tables A list of tables with the first entry to close.
|
|
|
|
DESCRIPTION
|
|
Closes the table that is associated (on the handler tables hash) with the
|
|
name (table->alias) of the specified table.
|
|
|
|
RETURN
|
|
FALSE ok
|
|
TRUE error
|
|
*/
|
|
|
|
bool mysql_ha_close(THD *thd, TABLE_LIST *tables)
|
|
{
|
|
TABLE_LIST *hash_tables;
|
|
DBUG_ENTER("mysql_ha_close");
|
|
DBUG_PRINT("enter",("'%s'.'%s' as '%s'",
|
|
tables->db, tables->table_name, tables->alias));
|
|
|
|
if (thd->locked_tables_mode)
|
|
{
|
|
my_error(ER_LOCK_OR_ACTIVE_TRANSACTION, MYF(0));
|
|
DBUG_RETURN(TRUE);
|
|
}
|
|
if ((hash_tables= (TABLE_LIST*) my_hash_search(&thd->handler_tables_hash,
|
|
(uchar*) tables->alias,
|
|
strlen(tables->alias) + 1)))
|
|
{
|
|
mysql_ha_close_table(thd, hash_tables);
|
|
my_hash_delete(&thd->handler_tables_hash, (uchar*) hash_tables);
|
|
}
|
|
else
|
|
{
|
|
my_error(ER_UNKNOWN_TABLE, MYF(0), tables->alias, "HANDLER");
|
|
DBUG_PRINT("exit",("ERROR"));
|
|
DBUG_RETURN(TRUE);
|
|
}
|
|
|
|
/*
|
|
Mark MDL_context as no longer breaking protocol if we have
|
|
closed last HANDLER.
|
|
*/
|
|
if (! thd->handler_tables_hash.records)
|
|
thd->mdl_context.set_needs_thr_lock_abort(FALSE);
|
|
|
|
my_ok(thd);
|
|
DBUG_PRINT("exit", ("OK"));
|
|
DBUG_RETURN(FALSE);
|
|
}
|
|
|
|
|
|
/**
|
|
A helper class to process an error from mysql_lock_tables().
|
|
HANDLER READ statement's attempt to lock the subject table
|
|
may get aborted if there is a pending DDL. In that case
|
|
we close the table, reopen it, and try to read again.
|
|
This is implicit and obscure, since HANDLER position
|
|
is lost in the process, but it's the legacy server
|
|
behaviour we should preserve.
|
|
*/
|
|
|
|
class Sql_handler_lock_error_handler: public Internal_error_handler
|
|
{
|
|
public:
|
|
virtual
|
|
bool handle_condition(THD *thd,
|
|
uint sql_errno,
|
|
const char *sqlstate,
|
|
MYSQL_ERROR::enum_warning_level level,
|
|
const char* msg,
|
|
MYSQL_ERROR **cond_hdl);
|
|
|
|
bool need_reopen() const { return m_need_reopen; };
|
|
void init() { m_need_reopen= FALSE; };
|
|
private:
|
|
bool m_need_reopen;
|
|
};
|
|
|
|
|
|
/**
|
|
Handle an error from mysql_lock_tables().
|
|
Ignore ER_LOCK_ABORTED errors.
|
|
*/
|
|
|
|
bool
|
|
Sql_handler_lock_error_handler::
|
|
handle_condition(THD *thd,
|
|
uint sql_errno,
|
|
const char *sqlstate,
|
|
MYSQL_ERROR::enum_warning_level level,
|
|
const char* msg,
|
|
MYSQL_ERROR **cond_hdl)
|
|
{
|
|
*cond_hdl= NULL;
|
|
if (sql_errno == ER_LOCK_ABORTED)
|
|
m_need_reopen= TRUE;
|
|
|
|
return m_need_reopen;
|
|
}
|
|
|
|
|
|
/*
|
|
Read from a HANDLER table.
|
|
|
|
SYNOPSIS
|
|
mysql_ha_read()
|
|
thd Thread identifier.
|
|
tables A list of tables with the first entry to read.
|
|
mode
|
|
keyname
|
|
key_expr
|
|
ha_rkey_mode
|
|
cond
|
|
select_limit_cnt
|
|
offset_limit_cnt
|
|
|
|
RETURN
|
|
FALSE ok
|
|
TRUE error
|
|
*/
|
|
|
|
bool mysql_ha_read(THD *thd, TABLE_LIST *tables,
|
|
enum enum_ha_read_modes mode, char *keyname,
|
|
List<Item> *key_expr,
|
|
enum ha_rkey_function ha_rkey_mode, Item *cond,
|
|
ha_rows select_limit_cnt, ha_rows offset_limit_cnt)
|
|
{
|
|
TABLE_LIST *hash_tables;
|
|
TABLE *table, *backup_open_tables;
|
|
MYSQL_LOCK *lock;
|
|
List<Item> list;
|
|
Protocol *protocol= thd->protocol;
|
|
char buff[MAX_FIELD_WIDTH];
|
|
String buffer(buff, sizeof(buff), system_charset_info);
|
|
int error, keyno= -1;
|
|
uint num_rows;
|
|
uchar *UNINIT_VAR(key);
|
|
uint UNINIT_VAR(key_len);
|
|
Sql_handler_lock_error_handler sql_handler_lock_error;
|
|
DBUG_ENTER("mysql_ha_read");
|
|
DBUG_PRINT("enter",("'%s'.'%s' as '%s'",
|
|
tables->db, tables->table_name, tables->alias));
|
|
|
|
if (thd->locked_tables_mode)
|
|
{
|
|
my_error(ER_LOCK_OR_ACTIVE_TRANSACTION, MYF(0));
|
|
DBUG_RETURN(TRUE);
|
|
}
|
|
|
|
thd->lex->select_lex.context.resolve_in_table_list_only(tables);
|
|
list.push_front(new Item_field(&thd->lex->select_lex.context,
|
|
NULL, NULL, "*"));
|
|
List_iterator<Item> it(list);
|
|
it++;
|
|
|
|
retry:
|
|
if ((hash_tables= (TABLE_LIST*) my_hash_search(&thd->handler_tables_hash,
|
|
(uchar*) tables->alias,
|
|
strlen(tables->alias) + 1)))
|
|
{
|
|
table= hash_tables->table;
|
|
DBUG_PRINT("info-in-hash",("'%s'.'%s' as '%s' table: 0x%lx",
|
|
hash_tables->db, hash_tables->table_name,
|
|
hash_tables->alias, (long) table));
|
|
if (!table)
|
|
{
|
|
/*
|
|
The handler table has been closed. Re-open it.
|
|
*/
|
|
if (mysql_ha_open(thd, hash_tables, 1))
|
|
{
|
|
DBUG_PRINT("exit",("reopen failed"));
|
|
goto err0;
|
|
}
|
|
|
|
table= hash_tables->table;
|
|
DBUG_PRINT("info",("re-opened '%s'.'%s' as '%s' tab %p",
|
|
hash_tables->db, hash_tables->table_name,
|
|
hash_tables->alias, table));
|
|
}
|
|
}
|
|
else
|
|
table= NULL;
|
|
|
|
if (!table)
|
|
{
|
|
my_error(ER_UNKNOWN_TABLE, MYF(0), tables->alias, "HANDLER");
|
|
goto err0;
|
|
}
|
|
|
|
/* save open_tables state */
|
|
backup_open_tables= thd->open_tables;
|
|
/* Always a one-element list, see mysql_ha_open(). */
|
|
DBUG_ASSERT(hash_tables->table->next == NULL ||
|
|
hash_tables->table->s->tmp_table);
|
|
/*
|
|
mysql_lock_tables() needs thd->open_tables to be set correctly to
|
|
be able to handle aborts properly.
|
|
*/
|
|
thd->set_open_tables(hash_tables->table);
|
|
|
|
|
|
sql_handler_lock_error.init();
|
|
thd->push_internal_handler(&sql_handler_lock_error);
|
|
|
|
lock= mysql_lock_tables(thd, &thd->open_tables, 1, 0);
|
|
|
|
thd->pop_internal_handler();
|
|
/*
|
|
In 5.1 and earlier, mysql_lock_tables() could replace the TABLE
|
|
object with another one (reopen it). This is no longer the case
|
|
with new MDL.
|
|
*/
|
|
DBUG_ASSERT(hash_tables->table == thd->open_tables);
|
|
/* Restore previous context. */
|
|
thd->set_open_tables(backup_open_tables);
|
|
|
|
if (sql_handler_lock_error.need_reopen())
|
|
{
|
|
DBUG_ASSERT(!lock && !thd->is_error());
|
|
/*
|
|
Always close statement transaction explicitly,
|
|
so that the engine doesn't have to count locks.
|
|
*/
|
|
trans_rollback_stmt(thd);
|
|
mysql_ha_close_table(thd, hash_tables);
|
|
goto retry;
|
|
}
|
|
|
|
if (!lock)
|
|
goto err0; // mysql_lock_tables() printed error message already
|
|
|
|
// Always read all columns
|
|
hash_tables->table->read_set= &hash_tables->table->s->all_set;
|
|
tables->table= hash_tables->table;
|
|
|
|
if (cond)
|
|
{
|
|
if (table->query_id != thd->query_id)
|
|
cond->cleanup(); // File was reopened
|
|
if ((!cond->fixed &&
|
|
cond->fix_fields(thd, &cond)) || cond->check_cols(1))
|
|
goto err;
|
|
}
|
|
|
|
if (keyname)
|
|
{
|
|
if ((keyno=find_type(keyname, &table->s->keynames, 1+2)-1)<0)
|
|
{
|
|
my_error(ER_KEY_DOES_NOT_EXITS, MYF(0), keyname, tables->alias);
|
|
goto err;
|
|
}
|
|
/* Check if the same index involved. */
|
|
if ((uint) keyno != table->file->get_index())
|
|
{
|
|
if (mode == RNEXT)
|
|
mode= RFIRST;
|
|
else if (mode == RPREV)
|
|
mode= RLAST;
|
|
}
|
|
}
|
|
|
|
if (insert_fields(thd, &thd->lex->select_lex.context,
|
|
tables->db, tables->alias, &it, 0))
|
|
goto err;
|
|
|
|
protocol->send_result_set_metadata(&list, Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF);
|
|
|
|
/*
|
|
In ::external_lock InnoDB resets the fields which tell it that
|
|
the handle is used in the HANDLER interface. Tell it again that
|
|
we are using it for HANDLER.
|
|
*/
|
|
|
|
table->file->init_table_handle_for_HANDLER();
|
|
|
|
for (num_rows=0; num_rows < select_limit_cnt; )
|
|
{
|
|
switch (mode) {
|
|
case RNEXT:
|
|
if (table->file->inited != handler::NONE)
|
|
{
|
|
if (keyname)
|
|
{
|
|
/* Check if we read from the same index. */
|
|
DBUG_ASSERT((uint) keyno == table->file->get_index());
|
|
error= table->file->index_next(table->record[0]);
|
|
}
|
|
else
|
|
{
|
|
error= table->file->rnd_next(table->record[0]);
|
|
}
|
|
break;
|
|
}
|
|
/* else fall through */
|
|
case RFIRST:
|
|
if (keyname)
|
|
{
|
|
table->file->ha_index_or_rnd_end();
|
|
table->file->ha_index_init(keyno, 1);
|
|
error= table->file->index_first(table->record[0]);
|
|
}
|
|
else
|
|
{
|
|
table->file->ha_index_or_rnd_end();
|
|
if (!(error= table->file->ha_rnd_init(1)))
|
|
error= table->file->rnd_next(table->record[0]);
|
|
}
|
|
mode=RNEXT;
|
|
break;
|
|
case RPREV:
|
|
DBUG_ASSERT(keyname != 0);
|
|
/* Check if we read from the same index. */
|
|
DBUG_ASSERT((uint) keyno == table->file->get_index());
|
|
if (table->file->inited != handler::NONE)
|
|
{
|
|
error=table->file->index_prev(table->record[0]);
|
|
break;
|
|
}
|
|
/* else fall through */
|
|
case RLAST:
|
|
DBUG_ASSERT(keyname != 0);
|
|
table->file->ha_index_or_rnd_end();
|
|
table->file->ha_index_init(keyno, 1);
|
|
error= table->file->index_last(table->record[0]);
|
|
mode=RPREV;
|
|
break;
|
|
case RNEXT_SAME:
|
|
/* Continue scan on "(keypart1,keypart2,...)=(c1, c2, ...) */
|
|
DBUG_ASSERT(keyname != 0);
|
|
error= table->file->index_next_same(table->record[0], key, key_len);
|
|
break;
|
|
case RKEY:
|
|
{
|
|
DBUG_ASSERT(keyname != 0);
|
|
KEY *keyinfo=table->key_info+keyno;
|
|
KEY_PART_INFO *key_part=keyinfo->key_part;
|
|
if (key_expr->elements > keyinfo->key_parts)
|
|
{
|
|
my_error(ER_TOO_MANY_KEY_PARTS, MYF(0), keyinfo->key_parts);
|
|
goto err;
|
|
}
|
|
List_iterator<Item> it_ke(*key_expr);
|
|
Item *item;
|
|
key_part_map keypart_map;
|
|
for (keypart_map= key_len=0 ; (item=it_ke++) ; key_part++)
|
|
{
|
|
my_bitmap_map *old_map;
|
|
// 'item' can be changed by fix_fields() call
|
|
if ((!item->fixed &&
|
|
item->fix_fields(thd, it_ke.ref())) ||
|
|
(item= *it_ke.ref())->check_cols(1))
|
|
goto err;
|
|
if (item->used_tables() & ~RAND_TABLE_BIT)
|
|
{
|
|
my_error(ER_WRONG_ARGUMENTS,MYF(0),"HANDLER ... READ");
|
|
goto err;
|
|
}
|
|
old_map= dbug_tmp_use_all_columns(table, table->write_set);
|
|
(void) item->save_in_field(key_part->field, 1);
|
|
dbug_tmp_restore_column_map(table->write_set, old_map);
|
|
key_len+=key_part->store_length;
|
|
keypart_map= (keypart_map << 1) | 1;
|
|
}
|
|
|
|
if (!(key= (uchar*) thd->calloc(ALIGN_SIZE(key_len))))
|
|
goto err;
|
|
table->file->ha_index_or_rnd_end();
|
|
table->file->ha_index_init(keyno, 1);
|
|
key_copy(key, table->record[0], table->key_info + keyno, key_len);
|
|
error= table->file->index_read_map(table->record[0],
|
|
key, keypart_map, ha_rkey_mode);
|
|
mode=rkey_to_rnext[(int)ha_rkey_mode];
|
|
break;
|
|
}
|
|
default:
|
|
my_message(ER_ILLEGAL_HA, ER(ER_ILLEGAL_HA), MYF(0));
|
|
goto err;
|
|
}
|
|
|
|
if (error)
|
|
{
|
|
if (error == HA_ERR_RECORD_DELETED)
|
|
continue;
|
|
if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
|
|
{
|
|
sql_print_error("mysql_ha_read: Got error %d when reading table '%s'",
|
|
error, tables->table_name);
|
|
table->file->print_error(error,MYF(0));
|
|
goto err;
|
|
}
|
|
goto ok;
|
|
}
|
|
if (cond && !cond->val_int())
|
|
{
|
|
if (thd->is_error())
|
|
goto err;
|
|
continue;
|
|
}
|
|
if (num_rows >= offset_limit_cnt)
|
|
{
|
|
protocol->prepare_for_resend();
|
|
|
|
if (protocol->send_result_set_row(&list))
|
|
goto err;
|
|
|
|
protocol->write();
|
|
}
|
|
num_rows++;
|
|
}
|
|
ok:
|
|
/*
|
|
Always close statement transaction explicitly,
|
|
so that the engine doesn't have to count locks.
|
|
*/
|
|
trans_commit_stmt(thd);
|
|
mysql_unlock_tables(thd,lock);
|
|
my_eof(thd);
|
|
DBUG_PRINT("exit",("OK"));
|
|
DBUG_RETURN(FALSE);
|
|
|
|
err:
|
|
trans_rollback_stmt(thd);
|
|
mysql_unlock_tables(thd,lock);
|
|
err0:
|
|
DBUG_PRINT("exit",("ERROR"));
|
|
DBUG_RETURN(TRUE);
|
|
}
|
|
|
|
|
|
/**
|
|
Scan the handler tables hash for matching tables.
|
|
|
|
@param thd Thread identifier.
|
|
@param tables The list of tables to remove.
|
|
|
|
@return Pointer to head of linked list (TABLE_LIST::next_local) of matching
|
|
TABLE_LIST elements from handler_tables_hash. Otherwise, NULL if no
|
|
table was matched.
|
|
*/
|
|
|
|
static TABLE_LIST *mysql_ha_find(THD *thd, TABLE_LIST *tables)
|
|
{
|
|
TABLE_LIST *hash_tables, *head= NULL, *first= tables;
|
|
DBUG_ENTER("mysql_ha_find");
|
|
|
|
/* search for all handlers with matching table names */
|
|
for (uint i= 0; i < thd->handler_tables_hash.records; i++)
|
|
{
|
|
hash_tables= (TABLE_LIST*) my_hash_element(&thd->handler_tables_hash, i);
|
|
for (tables= first; tables; tables= tables->next_local)
|
|
{
|
|
if ((! *tables->db ||
|
|
! my_strcasecmp(&my_charset_latin1, hash_tables->db, tables->db)) &&
|
|
! my_strcasecmp(&my_charset_latin1, hash_tables->table_name,
|
|
tables->table_name))
|
|
break;
|
|
}
|
|
if (tables)
|
|
{
|
|
hash_tables->next_local= head;
|
|
head= hash_tables;
|
|
}
|
|
}
|
|
|
|
DBUG_RETURN(head);
|
|
}
|
|
|
|
|
|
/**
|
|
Remove matching tables from the HANDLER's hash table.
|
|
|
|
@param thd Thread identifier.
|
|
@param tables The list of tables to remove.
|
|
|
|
@note Broadcasts refresh if it closed a table with old version.
|
|
*/
|
|
|
|
void mysql_ha_rm_tables(THD *thd, TABLE_LIST *tables)
|
|
{
|
|
TABLE_LIST *hash_tables, *next;
|
|
DBUG_ENTER("mysql_ha_rm_tables");
|
|
|
|
DBUG_ASSERT(tables);
|
|
|
|
hash_tables= mysql_ha_find(thd, tables);
|
|
|
|
while (hash_tables)
|
|
{
|
|
next= hash_tables->next_local;
|
|
if (hash_tables->table)
|
|
mysql_ha_close_table(thd, hash_tables);
|
|
my_hash_delete(&thd->handler_tables_hash, (uchar*) hash_tables);
|
|
hash_tables= next;
|
|
}
|
|
|
|
/*
|
|
Mark MDL_context as no longer breaking protocol if we have
|
|
closed last HANDLER.
|
|
*/
|
|
if (! thd->handler_tables_hash.records)
|
|
thd->mdl_context.set_needs_thr_lock_abort(FALSE);
|
|
|
|
DBUG_VOID_RETURN;
|
|
}
|
|
|
|
|
|
/**
|
|
Close cursors of matching tables from the HANDLER's hash table.
|
|
|
|
@param thd Thread identifier.
|
|
@param tables The list of tables to flush.
|
|
*/
|
|
|
|
void mysql_ha_flush_tables(THD *thd, TABLE_LIST *all_tables)
|
|
{
|
|
DBUG_ENTER("mysql_ha_flush_tables");
|
|
|
|
for (TABLE_LIST *table_list= all_tables; table_list;
|
|
table_list= table_list->next_global)
|
|
{
|
|
TABLE_LIST *hash_tables= mysql_ha_find(thd, table_list);
|
|
/* Close all aliases of the same table. */
|
|
while (hash_tables)
|
|
{
|
|
TABLE_LIST *next_local= hash_tables->next_local;
|
|
if (hash_tables->table)
|
|
mysql_ha_close_table(thd, hash_tables);
|
|
hash_tables= next_local;
|
|
}
|
|
}
|
|
|
|
DBUG_VOID_RETURN;
|
|
}
|
|
|
|
|
|
/**
|
|
Flush (close and mark for re-open) all tables that should be should
|
|
be reopen.
|
|
|
|
@param thd Thread identifier.
|
|
|
|
@note Broadcasts refresh if it closed a table with old version.
|
|
*/
|
|
|
|
void mysql_ha_flush(THD *thd)
|
|
{
|
|
TABLE_LIST *hash_tables;
|
|
DBUG_ENTER("mysql_ha_flush");
|
|
|
|
mysql_mutex_assert_not_owner(&LOCK_open);
|
|
|
|
/*
|
|
Don't try to flush open HANDLERs when we're working with
|
|
system tables. The main MDL context is backed up and we can't
|
|
properly release HANDLER locks stored there.
|
|
*/
|
|
if (thd->state_flags & Open_tables_state::BACKUPS_AVAIL)
|
|
DBUG_VOID_RETURN;
|
|
|
|
for (uint i= 0; i < thd->handler_tables_hash.records; i++)
|
|
{
|
|
hash_tables= (TABLE_LIST*) my_hash_element(&thd->handler_tables_hash, i);
|
|
/*
|
|
TABLE::mdl_ticket is 0 for temporary tables so we need extra check.
|
|
*/
|
|
if (hash_tables->table &&
|
|
((hash_tables->table->mdl_ticket &&
|
|
hash_tables->table->mdl_ticket->has_pending_conflicting_lock()) ||
|
|
(!hash_tables->table->s->tmp_table &&
|
|
hash_tables->table->s->has_old_version())))
|
|
mysql_ha_close_table(thd, hash_tables);
|
|
}
|
|
|
|
DBUG_VOID_RETURN;
|
|
}
|
|
|
|
|
|
/**
|
|
Close all HANDLER's tables.
|
|
|
|
@param thd Thread identifier.
|
|
|
|
@note Broadcasts refresh if it closed a table with old version.
|
|
*/
|
|
|
|
void mysql_ha_cleanup(THD *thd)
|
|
{
|
|
TABLE_LIST *hash_tables;
|
|
DBUG_ENTER("mysql_ha_cleanup");
|
|
|
|
for (uint i= 0; i < thd->handler_tables_hash.records; i++)
|
|
{
|
|
hash_tables= (TABLE_LIST*) my_hash_element(&thd->handler_tables_hash, i);
|
|
if (hash_tables->table)
|
|
mysql_ha_close_table(thd, hash_tables);
|
|
}
|
|
|
|
my_hash_free(&thd->handler_tables_hash);
|
|
|
|
DBUG_VOID_RETURN;
|
|
}
|
|
|
|
|
|
/**
|
|
Set explicit duration for metadata locks corresponding to open HANDLERs
|
|
to protect them from being released at the end of transaction.
|
|
|
|
@param thd Thread identifier.
|
|
*/
|
|
|
|
void mysql_ha_set_explicit_lock_duration(THD *thd)
|
|
{
|
|
TABLE_LIST *hash_tables;
|
|
DBUG_ENTER("mysql_ha_set_explicit_lock_duration");
|
|
|
|
for (uint i= 0; i < thd->handler_tables_hash.records; i++)
|
|
{
|
|
hash_tables= (TABLE_LIST*) my_hash_element(&thd->handler_tables_hash, i);
|
|
if (hash_tables->table && hash_tables->table->mdl_ticket)
|
|
thd->mdl_context.set_lock_duration(hash_tables->table->mdl_ticket,
|
|
MDL_EXPLICIT);
|
|
}
|
|
DBUG_VOID_RETURN;
|
|
}
|
|
|