mirror of
https://github.com/MariaDB/server.git
synced 2025-01-24 07:44:22 +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.
824 lines
22 KiB
C++
824 lines
22 KiB
C++
/* Copyright (C) 2004-2006 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 */
|
|
|
|
#include "sql_priv.h"
|
|
#include "unireg.h"
|
|
#include "event_queue.h"
|
|
#include "event_data_objects.h"
|
|
#include "event_db_repository.h"
|
|
#include "events.h"
|
|
#include "sql_audit.h"
|
|
#include "tztime.h" // my_tz_find, my_tz_OFFSET0, struct Time_zone
|
|
#include "log.h" // sql_print_error
|
|
#include "sql_class.h" // struct THD
|
|
|
|
/**
|
|
@addtogroup Event_Scheduler
|
|
@{
|
|
*/
|
|
|
|
#define EVENT_QUEUE_INITIAL_SIZE 30
|
|
#define EVENT_QUEUE_EXTENT 30
|
|
|
|
#ifdef __GNUC__
|
|
#if __GNUC__ >= 2
|
|
#define SCHED_FUNC __FUNCTION__
|
|
#endif
|
|
#else
|
|
#define SCHED_FUNC "<unknown>"
|
|
#endif
|
|
|
|
#define LOCK_QUEUE_DATA() lock_data(SCHED_FUNC, __LINE__)
|
|
#define UNLOCK_QUEUE_DATA() unlock_data(SCHED_FUNC, __LINE__)
|
|
|
|
/*
|
|
Compares the execute_at members of two Event_queue_element instances.
|
|
Used as callback for the prioritized queue when shifting
|
|
elements inside.
|
|
|
|
SYNOPSIS
|
|
event_queue_element_data_compare_q()
|
|
vptr Not used (set it to NULL)
|
|
a First Event_queue_element object
|
|
b Second Event_queue_element object
|
|
|
|
RETURN VALUE
|
|
-1 a->execute_at < b->execute_at
|
|
0 a->execute_at == b->execute_at
|
|
1 a->execute_at > b->execute_at
|
|
|
|
NOTES
|
|
execute_at.second_part is not considered during comparison
|
|
*/
|
|
|
|
extern "C" int event_queue_element_compare_q(void *, uchar *, uchar *);
|
|
|
|
int event_queue_element_compare_q(void *vptr, uchar* a, uchar *b)
|
|
{
|
|
Event_queue_element *left = (Event_queue_element *)a;
|
|
Event_queue_element *right = (Event_queue_element *)b;
|
|
my_time_t lhs = left->execute_at;
|
|
my_time_t rhs = right->execute_at;
|
|
|
|
if (left->status == Event_parse_data::DISABLED)
|
|
return right->status != Event_parse_data::DISABLED;
|
|
|
|
if (right->status == Event_parse_data::DISABLED)
|
|
return 1;
|
|
|
|
return (lhs < rhs ? -1 : (lhs > rhs ? 1 : 0));
|
|
}
|
|
|
|
|
|
/*
|
|
Constructor of class Event_queue.
|
|
|
|
SYNOPSIS
|
|
Event_queue::Event_queue()
|
|
*/
|
|
|
|
Event_queue::Event_queue()
|
|
:next_activation_at(0),
|
|
mutex_last_locked_at_line(0),
|
|
mutex_last_unlocked_at_line(0),
|
|
mutex_last_attempted_lock_at_line(0),
|
|
mutex_last_locked_in_func("n/a"),
|
|
mutex_last_unlocked_in_func("n/a"),
|
|
mutex_last_attempted_lock_in_func("n/a"),
|
|
mutex_queue_data_locked(FALSE),
|
|
mutex_queue_data_attempting_lock(FALSE),
|
|
waiting_on_cond(FALSE)
|
|
{
|
|
mysql_mutex_init(key_LOCK_event_queue, &LOCK_event_queue, MY_MUTEX_INIT_FAST);
|
|
mysql_cond_init(key_COND_queue_state, &COND_queue_state, NULL);
|
|
}
|
|
|
|
|
|
Event_queue::~Event_queue()
|
|
{
|
|
deinit_queue();
|
|
mysql_mutex_destroy(&LOCK_event_queue);
|
|
mysql_cond_destroy(&COND_queue_state);
|
|
}
|
|
|
|
|
|
/*
|
|
This is a queue's constructor. Until this method is called, the
|
|
queue is unusable. We don't use a C++ constructor instead in
|
|
order to be able to check the return value. The queue is
|
|
initialized once at server startup. Initialization can fail in
|
|
case of a failure reading events from the database or out of
|
|
memory.
|
|
|
|
SYNOPSIS
|
|
Event_queue::init()
|
|
|
|
RETURN VALUE
|
|
FALSE OK
|
|
TRUE Error
|
|
*/
|
|
|
|
bool
|
|
Event_queue::init_queue(THD *thd)
|
|
{
|
|
DBUG_ENTER("Event_queue::init_queue");
|
|
DBUG_PRINT("enter", ("this: 0x%lx", (long) this));
|
|
|
|
LOCK_QUEUE_DATA();
|
|
|
|
if (init_queue_ex(&queue, EVENT_QUEUE_INITIAL_SIZE , 0 /*offset*/,
|
|
0 /*max_on_top*/, event_queue_element_compare_q,
|
|
NULL, EVENT_QUEUE_EXTENT))
|
|
{
|
|
sql_print_error("Event Scheduler: Can't initialize the execution queue");
|
|
goto err;
|
|
}
|
|
|
|
UNLOCK_QUEUE_DATA();
|
|
DBUG_RETURN(FALSE);
|
|
|
|
err:
|
|
UNLOCK_QUEUE_DATA();
|
|
DBUG_RETURN(TRUE);
|
|
}
|
|
|
|
|
|
/*
|
|
Deinits the queue. Remove all elements from it and destroys them
|
|
too.
|
|
|
|
SYNOPSIS
|
|
Event_queue::deinit_queue()
|
|
*/
|
|
|
|
void
|
|
Event_queue::deinit_queue()
|
|
{
|
|
DBUG_ENTER("Event_queue::deinit_queue");
|
|
|
|
LOCK_QUEUE_DATA();
|
|
empty_queue();
|
|
delete_queue(&queue);
|
|
UNLOCK_QUEUE_DATA();
|
|
|
|
DBUG_VOID_RETURN;
|
|
}
|
|
|
|
|
|
/**
|
|
Adds an event to the queue.
|
|
|
|
Compute the next execution time for an event, and if it is still
|
|
active, add it to the queue. Otherwise delete it.
|
|
The object is left intact in case of an error. Otherwise
|
|
the queue container assumes ownership of it.
|
|
|
|
@param[in] thd thread handle
|
|
@param[in] new_element a new element to add to the queue
|
|
@param[out] created set to TRUE if no error and the element is
|
|
added to the queue, FALSE otherwise
|
|
|
|
@retval TRUE an error occured. The value of created is undefined,
|
|
the element was not deleted.
|
|
@retval FALSE success
|
|
*/
|
|
|
|
bool
|
|
Event_queue::create_event(THD *thd, Event_queue_element *new_element,
|
|
bool *created)
|
|
{
|
|
DBUG_ENTER("Event_queue::create_event");
|
|
DBUG_PRINT("enter", ("thd: 0x%lx et=%s.%s", (long) thd,
|
|
new_element->dbname.str, new_element->name.str));
|
|
|
|
/* Will do nothing if the event is disabled */
|
|
new_element->compute_next_execution_time();
|
|
if (new_element->status != Event_parse_data::ENABLED)
|
|
{
|
|
delete new_element;
|
|
*created= FALSE;
|
|
DBUG_RETURN(FALSE);
|
|
}
|
|
|
|
DBUG_PRINT("info", ("new event in the queue: 0x%lx", (long) new_element));
|
|
|
|
LOCK_QUEUE_DATA();
|
|
*created= (queue_insert_safe(&queue, (uchar *) new_element) == FALSE);
|
|
dbug_dump_queue(thd->query_start());
|
|
mysql_cond_broadcast(&COND_queue_state);
|
|
UNLOCK_QUEUE_DATA();
|
|
|
|
DBUG_RETURN(!*created);
|
|
}
|
|
|
|
|
|
/*
|
|
Updates an event from the scheduler queue
|
|
|
|
SYNOPSIS
|
|
Event_queue::update_event()
|
|
thd Thread
|
|
dbname Schema of the event
|
|
name Name of the event
|
|
new_schema New schema, in case of RENAME TO, otherwise NULL
|
|
new_name New name, in case of RENAME TO, otherwise NULL
|
|
*/
|
|
|
|
void
|
|
Event_queue::update_event(THD *thd, LEX_STRING dbname, LEX_STRING name,
|
|
Event_queue_element *new_element)
|
|
{
|
|
DBUG_ENTER("Event_queue::update_event");
|
|
DBUG_PRINT("enter", ("thd: 0x%lx et=[%s.%s]", (long) thd, dbname.str, name.str));
|
|
|
|
if ((new_element->status == Event_parse_data::DISABLED) ||
|
|
(new_element->status == Event_parse_data::SLAVESIDE_DISABLED))
|
|
{
|
|
DBUG_PRINT("info", ("The event is disabled."));
|
|
/*
|
|
Destroy the object but don't skip to end: because we may have to remove
|
|
object from the cache.
|
|
*/
|
|
delete new_element;
|
|
new_element= NULL;
|
|
}
|
|
else
|
|
new_element->compute_next_execution_time();
|
|
|
|
LOCK_QUEUE_DATA();
|
|
find_n_remove_event(dbname, name);
|
|
|
|
/* If not disabled event */
|
|
if (new_element)
|
|
{
|
|
DBUG_PRINT("info", ("new event in the queue: 0x%lx", (long) new_element));
|
|
queue_insert_safe(&queue, (uchar *) new_element);
|
|
mysql_cond_broadcast(&COND_queue_state);
|
|
}
|
|
|
|
dbug_dump_queue(thd->query_start());
|
|
UNLOCK_QUEUE_DATA();
|
|
|
|
DBUG_VOID_RETURN;
|
|
}
|
|
|
|
|
|
/*
|
|
Drops an event from the queue
|
|
|
|
SYNOPSIS
|
|
Event_queue::drop_event()
|
|
thd Thread
|
|
dbname Schema of the event to drop
|
|
name Name of the event to drop
|
|
*/
|
|
|
|
void
|
|
Event_queue::drop_event(THD *thd, LEX_STRING dbname, LEX_STRING name)
|
|
{
|
|
DBUG_ENTER("Event_queue::drop_event");
|
|
DBUG_PRINT("enter", ("thd: 0x%lx db :%s name: %s", (long) thd,
|
|
dbname.str, name.str));
|
|
|
|
LOCK_QUEUE_DATA();
|
|
find_n_remove_event(dbname, name);
|
|
dbug_dump_queue(thd->query_start());
|
|
UNLOCK_QUEUE_DATA();
|
|
|
|
/*
|
|
We don't signal here because the scheduler will catch the change
|
|
next time it wakes up.
|
|
*/
|
|
|
|
DBUG_VOID_RETURN;
|
|
}
|
|
|
|
|
|
/*
|
|
Drops all events from the in-memory queue and disk that match
|
|
certain pattern evaluated by a comparator function
|
|
|
|
SYNOPSIS
|
|
Event_queue::drop_matching_events()
|
|
thd THD
|
|
pattern A pattern string
|
|
comparator The function to use for comparing
|
|
|
|
RETURN VALUE
|
|
>=0 Number of dropped events
|
|
|
|
NOTE
|
|
Expected is the caller to acquire lock on LOCK_event_queue
|
|
*/
|
|
|
|
void
|
|
Event_queue::drop_matching_events(THD *thd, LEX_STRING pattern,
|
|
bool (*comparator)(LEX_STRING, Event_basic *))
|
|
{
|
|
uint i= 0;
|
|
DBUG_ENTER("Event_queue::drop_matching_events");
|
|
DBUG_PRINT("enter", ("pattern=%s", pattern.str));
|
|
|
|
while (i < queue.elements)
|
|
{
|
|
Event_queue_element *et= (Event_queue_element *) queue_element(&queue, i);
|
|
DBUG_PRINT("info", ("[%s.%s]?", et->dbname.str, et->name.str));
|
|
if (comparator(pattern, et))
|
|
{
|
|
/*
|
|
The queue is ordered. If we remove an element, then all elements
|
|
after it will shift one position to the left, if we imagine it as
|
|
an array from left to the right. In this case we should not
|
|
increment the counter and the (i < queue.elements) condition is ok.
|
|
*/
|
|
queue_remove(&queue, i);
|
|
delete et;
|
|
}
|
|
else
|
|
i++;
|
|
}
|
|
/*
|
|
We don't call mysql_cond_broadcast(&COND_queue_state);
|
|
If we remove the top event:
|
|
1. The queue is empty. The scheduler will wake up at some time and
|
|
realize that the queue is empty. If create_event() comes inbetween
|
|
it will signal the scheduler
|
|
2. The queue is not empty, but the next event after the previous top,
|
|
won't be executed any time sooner than the element we removed. Hence,
|
|
we may not notify the scheduler and it will realize the change when it
|
|
wakes up from timedwait.
|
|
*/
|
|
|
|
DBUG_VOID_RETURN;
|
|
}
|
|
|
|
|
|
/*
|
|
Drops all events from the in-memory queue and disk that are from
|
|
certain schema.
|
|
|
|
SYNOPSIS
|
|
Event_queue::drop_schema_events()
|
|
thd HD
|
|
schema The schema name
|
|
*/
|
|
|
|
void
|
|
Event_queue::drop_schema_events(THD *thd, LEX_STRING schema)
|
|
{
|
|
DBUG_ENTER("Event_queue::drop_schema_events");
|
|
LOCK_QUEUE_DATA();
|
|
drop_matching_events(thd, schema, event_basic_db_equal);
|
|
UNLOCK_QUEUE_DATA();
|
|
DBUG_VOID_RETURN;
|
|
}
|
|
|
|
|
|
/*
|
|
Searches for an event in the queue
|
|
|
|
SYNOPSIS
|
|
Event_queue::find_n_remove_event()
|
|
db The schema of the event to find
|
|
name The event to find
|
|
|
|
NOTE
|
|
The caller should do the locking also the caller is responsible for
|
|
actual signalling in case an event is removed from the queue.
|
|
*/
|
|
|
|
void
|
|
Event_queue::find_n_remove_event(LEX_STRING db, LEX_STRING name)
|
|
{
|
|
uint i;
|
|
DBUG_ENTER("Event_queue::find_n_remove_event");
|
|
|
|
for (i= 0; i < queue.elements; ++i)
|
|
{
|
|
Event_queue_element *et= (Event_queue_element *) queue_element(&queue, i);
|
|
DBUG_PRINT("info", ("[%s.%s]==[%s.%s]?", db.str, name.str,
|
|
et->dbname.str, et->name.str));
|
|
if (event_basic_identifier_equal(db, name, et))
|
|
{
|
|
queue_remove(&queue, i);
|
|
delete et;
|
|
break;
|
|
}
|
|
}
|
|
|
|
DBUG_VOID_RETURN;
|
|
}
|
|
|
|
|
|
/*
|
|
Recalculates activation times in the queue. There is one reason for
|
|
that. Because the values (execute_at) by which the queue is ordered are
|
|
changed by calls to compute_next_execution_time() on a request from the
|
|
scheduler thread, if it is not running then the values won't be updated.
|
|
Once the scheduler is started again the values has to be recalculated
|
|
so they are right for the current time.
|
|
|
|
SYNOPSIS
|
|
Event_queue::recalculate_activation_times()
|
|
thd Thread
|
|
*/
|
|
|
|
void
|
|
Event_queue::recalculate_activation_times(THD *thd)
|
|
{
|
|
uint i;
|
|
DBUG_ENTER("Event_queue::recalculate_activation_times");
|
|
|
|
LOCK_QUEUE_DATA();
|
|
DBUG_PRINT("info", ("%u loaded events to be recalculated", queue.elements));
|
|
for (i= 0; i < queue.elements; i++)
|
|
{
|
|
((Event_queue_element*)queue_element(&queue, i))->compute_next_execution_time();
|
|
}
|
|
queue_fix(&queue);
|
|
/*
|
|
The disabled elements are moved to the end during the `fix`.
|
|
Start from the end and remove all of the elements which are
|
|
disabled. When we find the first non-disabled one we break, as we
|
|
have removed all. The queue has been ordered in a way the disabled
|
|
events are at the end.
|
|
*/
|
|
for (i= queue.elements; i > 0; i--)
|
|
{
|
|
Event_queue_element *element = (Event_queue_element*)queue_element(&queue, i - 1);
|
|
if (element->status != Event_parse_data::DISABLED)
|
|
break;
|
|
/*
|
|
This won't cause queue re-order, because we remove
|
|
always the last element.
|
|
*/
|
|
queue_remove(&queue, i - 1);
|
|
delete element;
|
|
}
|
|
UNLOCK_QUEUE_DATA();
|
|
|
|
/*
|
|
XXX: The events are dropped only from memory and not from disk
|
|
even if `drop_list[j]->dropped` is TRUE. There will be still on the
|
|
disk till next server restart.
|
|
Please add code here to do it.
|
|
*/
|
|
|
|
DBUG_VOID_RETURN;
|
|
}
|
|
|
|
|
|
/*
|
|
Empties the queue and destroys the Event_queue_element objects in the
|
|
queue.
|
|
|
|
SYNOPSIS
|
|
Event_queue::empty_queue()
|
|
|
|
NOTE
|
|
Should be called with LOCK_event_queue locked
|
|
*/
|
|
|
|
void
|
|
Event_queue::empty_queue()
|
|
{
|
|
uint i;
|
|
DBUG_ENTER("Event_queue::empty_queue");
|
|
DBUG_PRINT("enter", ("Purging the queue. %u element(s)", queue.elements));
|
|
sql_print_information("Event Scheduler: Purging the queue. %u events",
|
|
queue.elements);
|
|
/* empty the queue */
|
|
for (i= 0; i < queue.elements; ++i)
|
|
{
|
|
Event_queue_element *et= (Event_queue_element *) queue_element(&queue, i);
|
|
delete et;
|
|
}
|
|
resize_queue(&queue, 0);
|
|
DBUG_VOID_RETURN;
|
|
}
|
|
|
|
|
|
/*
|
|
Dumps the queue to the trace log.
|
|
|
|
SYNOPSIS
|
|
Event_queue::dbug_dump_queue()
|
|
now Current timestamp
|
|
*/
|
|
|
|
void
|
|
Event_queue::dbug_dump_queue(time_t now)
|
|
{
|
|
#ifndef DBUG_OFF
|
|
Event_queue_element *et;
|
|
uint i;
|
|
DBUG_ENTER("Event_queue::dbug_dump_queue");
|
|
DBUG_PRINT("info", ("Dumping queue . Elements=%u", queue.elements));
|
|
for (i = 0; i < queue.elements; i++)
|
|
{
|
|
et= ((Event_queue_element*)queue_element(&queue, i));
|
|
DBUG_PRINT("info", ("et: 0x%lx name: %s.%s", (long) et,
|
|
et->dbname.str, et->name.str));
|
|
DBUG_PRINT("info", ("exec_at: %lu starts: %lu ends: %lu execs_so_far: %u "
|
|
"expr: %ld et.exec_at: %ld now: %ld "
|
|
"(et.exec_at - now): %d if: %d",
|
|
(long) et->execute_at, (long) et->starts,
|
|
(long) et->ends, et->execution_count,
|
|
(long) et->expression, (long) et->execute_at,
|
|
(long) now, (int) (et->execute_at - now),
|
|
et->execute_at <= now));
|
|
}
|
|
DBUG_VOID_RETURN;
|
|
#endif
|
|
}
|
|
|
|
static const char *queue_empty_msg= "Waiting on empty queue";
|
|
static const char *queue_wait_msg= "Waiting for next activation";
|
|
|
|
/*
|
|
Checks whether the top of the queue is elligible for execution and
|
|
returns an Event_job_data instance in case it should be executed.
|
|
`now` is compared against `execute_at` of the top element in the queue.
|
|
|
|
SYNOPSIS
|
|
Event_queue::get_top_for_execution_if_time()
|
|
thd [in] Thread
|
|
event_name [out] The object to execute
|
|
|
|
RETURN VALUE
|
|
FALSE No error. event_name != NULL
|
|
TRUE Serious error
|
|
*/
|
|
|
|
bool
|
|
Event_queue::get_top_for_execution_if_time(THD *thd,
|
|
Event_queue_element_for_exec **event_name)
|
|
{
|
|
bool ret= FALSE;
|
|
*event_name= NULL;
|
|
my_time_t last_executed;
|
|
int status;
|
|
DBUG_ENTER("Event_queue::get_top_for_execution_if_time");
|
|
|
|
LOCK_QUEUE_DATA();
|
|
for (;;)
|
|
{
|
|
Event_queue_element *top= NULL;
|
|
|
|
/* Break loop if thd has been killed */
|
|
if (thd->killed)
|
|
{
|
|
DBUG_PRINT("info", ("thd->killed=%d", thd->killed));
|
|
goto end;
|
|
}
|
|
|
|
if (!queue.elements)
|
|
{
|
|
/* There are no events in the queue */
|
|
next_activation_at= 0;
|
|
|
|
/* Release any held audit resources before waiting */
|
|
mysql_audit_release(thd);
|
|
|
|
/* Wait on condition until signaled. Release LOCK_queue while waiting. */
|
|
cond_wait(thd, NULL, queue_empty_msg, SCHED_FUNC, __LINE__);
|
|
|
|
continue;
|
|
}
|
|
|
|
top= ((Event_queue_element*) queue_element(&queue, 0));
|
|
|
|
thd->set_current_time(); /* Get current time */
|
|
|
|
next_activation_at= top->execute_at;
|
|
if (next_activation_at > thd->query_start())
|
|
{
|
|
/*
|
|
Not yet time for top event, wait on condition with
|
|
time or until signaled. Release LOCK_queue while waiting.
|
|
*/
|
|
struct timespec top_time;
|
|
set_timespec(top_time, next_activation_at - thd->query_start());
|
|
|
|
/* Release any held audit resources before waiting */
|
|
mysql_audit_release(thd);
|
|
|
|
cond_wait(thd, &top_time, queue_wait_msg, SCHED_FUNC, __LINE__);
|
|
|
|
continue;
|
|
}
|
|
|
|
if (!(*event_name= new Event_queue_element_for_exec()) ||
|
|
(*event_name)->init(top->dbname, top->name))
|
|
{
|
|
ret= TRUE;
|
|
break;
|
|
}
|
|
|
|
DBUG_PRINT("info", ("Ready for execution"));
|
|
top->mark_last_executed(thd);
|
|
if (top->compute_next_execution_time())
|
|
top->status= Event_parse_data::DISABLED;
|
|
DBUG_PRINT("info", ("event %s status is %d", top->name.str, top->status));
|
|
|
|
top->execution_count++;
|
|
(*event_name)->dropped= top->dropped;
|
|
/*
|
|
Save new values of last_executed timestamp and event status on stack
|
|
in order to be able to update event description in system table once
|
|
QUEUE_DATA lock is released.
|
|
*/
|
|
last_executed= top->last_executed;
|
|
status= top->status;
|
|
|
|
if (top->status == Event_parse_data::DISABLED)
|
|
{
|
|
DBUG_PRINT("info", ("removing from the queue"));
|
|
sql_print_information("Event Scheduler: Last execution of %s.%s. %s",
|
|
top->dbname.str, top->name.str,
|
|
top->dropped? "Dropping.":"");
|
|
delete top;
|
|
queue_remove(&queue, 0);
|
|
}
|
|
else
|
|
queue_replaced(&queue);
|
|
|
|
dbug_dump_queue(thd->query_start());
|
|
break;
|
|
}
|
|
end:
|
|
UNLOCK_QUEUE_DATA();
|
|
|
|
DBUG_PRINT("info", ("returning %d et_new: 0x%lx ",
|
|
ret, (long) *event_name));
|
|
|
|
if (*event_name)
|
|
{
|
|
DBUG_PRINT("info", ("db: %s name: %s",
|
|
(*event_name)->dbname.str, (*event_name)->name.str));
|
|
|
|
Event_db_repository *db_repository= Events::get_db_repository();
|
|
(void) db_repository->update_timing_fields_for_event(thd,
|
|
(*event_name)->dbname, (*event_name)->name,
|
|
last_executed, (ulonglong) status);
|
|
}
|
|
|
|
DBUG_RETURN(ret);
|
|
}
|
|
|
|
|
|
/*
|
|
Auxiliary function for locking LOCK_event_queue. Used by the
|
|
LOCK_QUEUE_DATA macro
|
|
|
|
SYNOPSIS
|
|
Event_queue::lock_data()
|
|
func Which function is requesting mutex lock
|
|
line On which line mutex lock is requested
|
|
*/
|
|
|
|
void
|
|
Event_queue::lock_data(const char *func, uint line)
|
|
{
|
|
DBUG_ENTER("Event_queue::lock_data");
|
|
DBUG_PRINT("enter", ("func=%s line=%u", func, line));
|
|
mutex_last_attempted_lock_in_func= func;
|
|
mutex_last_attempted_lock_at_line= line;
|
|
mutex_queue_data_attempting_lock= TRUE;
|
|
mysql_mutex_lock(&LOCK_event_queue);
|
|
mutex_last_attempted_lock_in_func= "";
|
|
mutex_last_attempted_lock_at_line= 0;
|
|
mutex_queue_data_attempting_lock= FALSE;
|
|
|
|
mutex_last_locked_in_func= func;
|
|
mutex_last_locked_at_line= line;
|
|
mutex_queue_data_locked= TRUE;
|
|
|
|
DBUG_VOID_RETURN;
|
|
}
|
|
|
|
|
|
/*
|
|
Auxiliary function for unlocking LOCK_event_queue. Used by the
|
|
UNLOCK_QUEUE_DATA macro
|
|
|
|
SYNOPSIS
|
|
Event_queue::unlock_data()
|
|
func Which function is requesting mutex unlock
|
|
line On which line mutex unlock is requested
|
|
*/
|
|
|
|
void
|
|
Event_queue::unlock_data(const char *func, uint line)
|
|
{
|
|
DBUG_ENTER("Event_queue::unlock_data");
|
|
DBUG_PRINT("enter", ("func=%s line=%u", func, line));
|
|
mutex_last_unlocked_at_line= line;
|
|
mutex_queue_data_locked= FALSE;
|
|
mutex_last_unlocked_in_func= func;
|
|
mysql_mutex_unlock(&LOCK_event_queue);
|
|
DBUG_VOID_RETURN;
|
|
}
|
|
|
|
|
|
/*
|
|
Wrapper for mysql_cond_wait/timedwait
|
|
|
|
SYNOPSIS
|
|
Event_queue::cond_wait()
|
|
thd Thread (Could be NULL during shutdown procedure)
|
|
msg Message for thd->proc_info
|
|
abstime If not null then call mysql_cond_timedwait()
|
|
func Which function is requesting cond_wait
|
|
line On which line cond_wait is requested
|
|
*/
|
|
|
|
void
|
|
Event_queue::cond_wait(THD *thd, struct timespec *abstime, const char* msg,
|
|
const char *func, uint line)
|
|
{
|
|
DBUG_ENTER("Event_queue::cond_wait");
|
|
waiting_on_cond= TRUE;
|
|
mutex_last_unlocked_at_line= line;
|
|
mutex_queue_data_locked= FALSE;
|
|
mutex_last_unlocked_in_func= func;
|
|
|
|
thd->enter_cond(&COND_queue_state, &LOCK_event_queue, msg);
|
|
|
|
if (!thd->killed)
|
|
{
|
|
if (!abstime)
|
|
mysql_cond_wait(&COND_queue_state, &LOCK_event_queue);
|
|
else
|
|
mysql_cond_timedwait(&COND_queue_state, &LOCK_event_queue, abstime);
|
|
}
|
|
|
|
mutex_last_locked_in_func= func;
|
|
mutex_last_locked_at_line= line;
|
|
mutex_queue_data_locked= TRUE;
|
|
waiting_on_cond= FALSE;
|
|
|
|
/*
|
|
This will free the lock so we need to relock. Not the best thing to
|
|
do but we need to obey cond_wait()
|
|
*/
|
|
thd->exit_cond("");
|
|
lock_data(func, line);
|
|
|
|
DBUG_VOID_RETURN;
|
|
}
|
|
|
|
|
|
/*
|
|
Dumps the internal status of the queue
|
|
|
|
SYNOPSIS
|
|
Event_queue::dump_internal_status()
|
|
*/
|
|
|
|
void
|
|
Event_queue::dump_internal_status()
|
|
{
|
|
DBUG_ENTER("Event_queue::dump_internal_status");
|
|
|
|
/* element count */
|
|
puts("");
|
|
puts("Event queue status:");
|
|
printf("Element count : %u\n", queue.elements);
|
|
printf("Data locked : %s\n", mutex_queue_data_locked? "YES":"NO");
|
|
printf("Attempting lock : %s\n", mutex_queue_data_attempting_lock? "YES":"NO");
|
|
printf("LLA : %s:%u\n", mutex_last_locked_in_func,
|
|
mutex_last_locked_at_line);
|
|
printf("LUA : %s:%u\n", mutex_last_unlocked_in_func,
|
|
mutex_last_unlocked_at_line);
|
|
if (mutex_last_attempted_lock_at_line)
|
|
printf("Last lock attempt at: %s:%u\n", mutex_last_attempted_lock_in_func,
|
|
mutex_last_attempted_lock_at_line);
|
|
printf("WOC : %s\n", waiting_on_cond? "YES":"NO");
|
|
|
|
MYSQL_TIME time;
|
|
my_tz_OFFSET0->gmt_sec_to_TIME(&time, next_activation_at);
|
|
if (time.year != 1970)
|
|
printf("Next activation : %04d-%02d-%02d %02d:%02d:%02d\n",
|
|
time.year, time.month, time.day, time.hour, time.minute, time.second);
|
|
else
|
|
printf("Next activation : never");
|
|
|
|
DBUG_VOID_RETURN;
|
|
}
|
|
|
|
/**
|
|
@} (End of group Event_Scheduler)
|
|
*/
|