Commit fea4742d authored by unknown's avatar unknown

fix for bug#16406 (Events: DROP DATABASE doesn't automatically drop events)

WL#1034
- This changeset also changes the executor so its quite more stable now.
  Stressing test case added that executes ~800 events per second and dropping
  hundreds of events at once using DROP DATABASE.
(with fixes after review of JimW)
(with fixes after review of Serg)


mysql-test/r/events.result:
  update results after TRIGGER_ACL was added
mysql-test/t/events.test:
  -redundant line
sql/event.cc:
  Implemented evex_db_drop_events() which drops all events
  from a specific database. Needed for SQLCOM_DROP_DATABASE
sql/event.h:
  - protect the event better (see the changes to event_executor.cc
    and event.cc). An event object could be used in a spawned thread
    before it's executed but till now the object is marked as being
    executed when the anonymous sp_head is executed. However, there are
    timeframes before and after that during which the event is not marked
    as executed and other thread may delete the object -> so we end with
    a nirvana pointer.
sql/event_executor.cc:
  - extract some of the code executed in the main thread to a function. Too long
    functions are bad for the overview.
  - prepend all information/error messages to the console with "SCHEDULER:" for
    better overview, and easied searching in the log tables.
sql/event_priv.h:
  - change the name, of evex_db_find_event_by_name() and don't
    used C++ features like function overloading
  - define consts for result returned from event_timed::spawn_now()
sql/event_timed.cc:
  - add few methods related to event execution.
    now the event spawns the worker thread and
    passes itself as parameter. This way it locks itself for exectution
    first and then spawning -> no race condition. When the worker thread
    has finished working with the reference it calls back
    event_timed::spawn_thread_finish() to unlock itself.
sql/sql_db.cc:
  - call evex_drop_db_events() on DROP DATABASE
parent 7088b39d
...@@ -64,7 +64,7 @@ SHOW GRANTS; ...@@ -64,7 +64,7 @@ SHOW GRANTS;
Grants for ev_test@localhost Grants for ev_test@localhost
GRANT USAGE ON *.* TO 'ev_test'@'localhost' GRANT USAGE ON *.* TO 'ev_test'@'localhost'
GRANT ALL PRIVILEGES ON `events_test`.* TO 'ev_test'@'localhost' GRANT ALL PRIVILEGES ON `events_test`.* TO 'ev_test'@'localhost'
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, REFERENCES, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE ON `events_test2`.* TO 'ev_test'@'localhost' GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, REFERENCES, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, TRIGGER ON `events_test2`.* TO 'ev_test'@'localhost'
"Here comes an error:"; "Here comes an error:";
SHOW EVENTS; SHOW EVENTS;
ERROR 42000: Access denied for user 'ev_test'@'localhost' to database 'events_test2' ERROR 42000: Access denied for user 'ev_test'@'localhost' to database 'events_test2'
......
CREATE DATABASE IF NOT EXISTS events_test;
CREATE DATABASE events_test2;
USE events_test2;
CREATE EVENT ev_drop1 ON SCHEDULE EVERY 10 MINUTE DISABLE DO SELECT 1;
CREATE EVENT ev_drop2 ON SCHEDULE EVERY 10 MINUTE DISABLE DO SELECT 1;
CREATE EVENT ev_drop3 ON SCHEDULE EVERY 10 MINUTE DISABLE DO SELECT 1;
USE events_test;
SELECT COUNT(*) FROM INFORMATION_SCHEMA.EVENTS WHERE EVENT_SCHEMA='events_test2';
COUNT(*)
3
DROP DATABASE events_test2;
SELECT COUNT(*) FROM INFORMATION_SCHEMA.EVENTS WHERE EVENT_SCHEMA='events_test2';
COUNT(*)
0
"Now testing stability - dropping db -> events while they are running"
CREATE DATABASE events_test2;
USE events_test2;
SELECT COUNT(*) FROM INFORMATION_SCHEMA.EVENTS WHERE EVENT_SCHEMA='events_test2';
COUNT(*)
1000
SET GLOBAL event_scheduler=1;
DROP DATABASE events_test2;
SET GLOBAL event_scheduler=0;
SELECT COUNT(*) FROM INFORMATION_SCHEMA.EVENTS WHERE EVENT_SCHEMA='events_test2';
COUNT(*)
0
CREATE DATABASE events_test3;
USE events_test3;
SELECT COUNT(*) FROM INFORMATION_SCHEMA.EVENTS WHERE EVENT_SCHEMA='events_test3';
COUNT(*)
950
CREATE DATABASE events_test4;
USE events_test4;
CREATE DATABASE events_test2;
USE events_test2;
SELECT COUNT(*) FROM INFORMATION_SCHEMA.EVENTS WHERE EVENT_SCHEMA='events_test2';
COUNT(*)
1050
DROP DATABASE events_test2;
SET GLOBAL event_scheduler=0;
DROP DATABASE events_test3;
SET GLOBAL event_scheduler=1;
DROP DATABASE events_test4;
SET GLOBAL event_scheduler=1;
USE events_test;
DROP DATABASE events_test;
...@@ -109,7 +109,6 @@ drop event one_event; ...@@ -109,7 +109,6 @@ drop event one_event;
create event e_26 on schedule at '2017-01-01 00:00:00' disable do set @a = 5; create event e_26 on schedule at '2017-01-01 00:00:00' disable do set @a = 5;
select db, name, body, definer, convert_tz(execute_at, 'UTC', 'SYSTEM'), on_completion from mysql.event; select db, name, body, definer, convert_tz(execute_at, 'UTC', 'SYSTEM'), on_completion from mysql.event;
drop event e_26; drop event e_26;
......
CREATE DATABASE IF NOT EXISTS events_test;
#
# DROP DATABASE test start (bug #16406)
#
CREATE DATABASE events_test2;
USE events_test2;
CREATE EVENT ev_drop1 ON SCHEDULE EVERY 10 MINUTE DISABLE DO SELECT 1;
CREATE EVENT ev_drop2 ON SCHEDULE EVERY 10 MINUTE DISABLE DO SELECT 1;
CREATE EVENT ev_drop3 ON SCHEDULE EVERY 10 MINUTE DISABLE DO SELECT 1;
USE events_test;
SELECT COUNT(*) FROM INFORMATION_SCHEMA.EVENTS WHERE EVENT_SCHEMA='events_test2';
DROP DATABASE events_test2;
SELECT COUNT(*) FROM INFORMATION_SCHEMA.EVENTS WHERE EVENT_SCHEMA='events_test2';
--echo "Now testing stability - dropping db -> events while they are running"
CREATE DATABASE events_test2;
USE events_test2;
--disable_query_log
let $1= 1000;
while ($1)
{
eval CREATE EVENT ev_drop$1 ON SCHEDULE EVERY 1 SECOND DO SELECT $1;
dec $1;
}
--enable_query_log
SELECT COUNT(*) FROM INFORMATION_SCHEMA.EVENTS WHERE EVENT_SCHEMA='events_test2';
SET GLOBAL event_scheduler=1;
--sleep 4
DROP DATABASE events_test2;
SET GLOBAL event_scheduler=0;
--sleep 2
SELECT COUNT(*) FROM INFORMATION_SCHEMA.EVENTS WHERE EVENT_SCHEMA='events_test2';
CREATE DATABASE events_test3;
USE events_test3;
--disable_query_log
let $1= 950;
while ($1)
{
eval CREATE EVENT ev_drop$1 ON SCHEDULE EVERY 1 SECOND DO SELECT $1;
dec $1;
}
--enable_query_log
SELECT COUNT(*) FROM INFORMATION_SCHEMA.EVENTS WHERE EVENT_SCHEMA='events_test3';
--sleep 3
CREATE DATABASE events_test4;
USE events_test4;
--disable_query_log
let $1= 860;
while ($1)
{
eval CREATE EVENT ev_drop$1 ON SCHEDULE EVERY 1 SECOND DO SELECT $1;
dec $1;
}
--enable_query_log
CREATE DATABASE events_test2;
USE events_test2;
--disable_query_log
let $1= 1050;
while ($1)
{
eval CREATE EVENT ev_drop$1 ON SCHEDULE EVERY 1 SECOND DO SELECT $1;
dec $1;
}
--enable_query_log
SELECT COUNT(*) FROM INFORMATION_SCHEMA.EVENTS WHERE EVENT_SCHEMA='events_test2';
--sleep 6
DROP DATABASE events_test2;
SET GLOBAL event_scheduler=0;
DROP DATABASE events_test3;
SET GLOBAL event_scheduler=1;
DROP DATABASE events_test4;
SET GLOBAL event_scheduler=1;
USE events_test;
#
# DROP DATABASE test end (bug #16406)
#
DROP DATABASE events_test;
This diff is collapsed.
...@@ -79,6 +79,8 @@ class event_timed ...@@ -79,6 +79,8 @@ class event_timed
{ {
event_timed(const event_timed &); /* Prevent use of these */ event_timed(const event_timed &); /* Prevent use of these */
void operator=(event_timed &); void operator=(event_timed &);
my_bool in_spawned_thread;
ulong locked_by_thread_id;
my_bool running; my_bool running;
pthread_mutex_t LOCK_running; pthread_mutex_t LOCK_running;
...@@ -116,9 +118,10 @@ public: ...@@ -116,9 +118,10 @@ public:
bool free_sphead_on_delete; bool free_sphead_on_delete;
uint flags;//all kind of purposes uint flags;//all kind of purposes
event_timed():running(0), status_changed(false), last_executed_changed(false), event_timed():in_spawned_thread(0),locked_by_thread_id(0),
expression(0), created(0), modified(0), running(0), status_changed(false),
on_completion(MYSQL_EVENT_ON_COMPLETION_DROP), last_executed_changed(false), expression(0), created(0),
modified(0), on_completion(MYSQL_EVENT_ON_COMPLETION_DROP),
status(MYSQL_EVENT_ENABLED), sphead(0), dropped(false), status(MYSQL_EVENT_ENABLED), sphead(0), dropped(false),
free_sphead_on_delete(true), flags(0) free_sphead_on_delete(true), flags(0)
...@@ -197,8 +200,45 @@ public: ...@@ -197,8 +200,45 @@ public:
return ret; return ret;
} }
/*
Checks whether the object is being used in a spawned thread.
This method is for very basic checking. Use ::can_spawn_now_n_lock()
for most of the cases.
*/
my_bool
can_spawn_now()
{
my_bool ret;
VOID(pthread_mutex_lock(&this->LOCK_running));
ret= !in_spawned_thread;
VOID(pthread_mutex_unlock(&this->LOCK_running));
return ret;
}
/*
Checks whether this thread can lock the object for modification ->
preventing being spawned for execution, and locks if possible.
use ::can_spawn_now() only for basic checking because a race
condition may occur between the check and eventual modification (deletion)
of the object.
*/
my_bool
can_spawn_now_n_lock(THD *thd);
int
spawn_unlock(THD *thd);
int
spawn_now(void * (*thread_func)(void*));
void free_sp() void
spawn_thread_finish(THD *thd);
void
free_sp()
{ {
delete sphead; delete sphead;
sphead= 0; sphead= 0;
...@@ -221,7 +261,11 @@ evex_drop_event(THD *thd, event_timed *et, bool drop_if_exists, ...@@ -221,7 +261,11 @@ evex_drop_event(THD *thd, event_timed *et, bool drop_if_exists,
int int
evex_open_event_table(THD *thd, enum thr_lock_type lock_type, TABLE **table); evex_open_event_table(THD *thd, enum thr_lock_type lock_type, TABLE **table);
int sortcmp_lex_string(LEX_STRING s, LEX_STRING t, CHARSET_INFO *cs); int
sortcmp_lex_string(LEX_STRING s, LEX_STRING t, CHARSET_INFO *cs);
int
evex_drop_db_events(THD *thd, char *db);
int int
init_events(); init_events();
......
This diff is collapsed.
...@@ -19,6 +19,10 @@ ...@@ -19,6 +19,10 @@
#include "mysql_priv.h" #include "mysql_priv.h"
#define EVENT_EXEC_STARTED 0
#define EVENT_EXEC_ALREADY_EXEC 1
#define EVENT_EXEC_CANT_FORK 2
#define EVEX_USE_QUEUE #define EVEX_USE_QUEUE
#define UNLOCK_MUTEX_AND_BAIL_OUT(__mutex, __label) \ #define UNLOCK_MUTEX_AND_BAIL_OUT(__mutex, __label) \
...@@ -32,10 +36,10 @@ int ...@@ -32,10 +36,10 @@ int
my_time_compare(TIME *a, TIME *b); my_time_compare(TIME *a, TIME *b);
int int
evex_db_find_event_aux(THD *thd, const LEX_STRING dbname, evex_db_find_event_by_name(THD *thd, const LEX_STRING dbname,
const LEX_STRING rname, const LEX_STRING ev_name,
const LEX_STRING definer, const LEX_STRING user_name,
TABLE *table); TABLE *table);
int int
event_timed_compare_q(void *vptr, byte* a, byte *b); event_timed_compare_q(void *vptr, byte* a, byte *b);
......
...@@ -879,11 +879,12 @@ event_timed::drop(THD *thd) ...@@ -879,11 +879,12 @@ event_timed::drop(THD *thd)
TABLE *table; TABLE *table;
int ret= 0; int ret= 0;
DBUG_ENTER("event_timed::drop"); DBUG_ENTER("event_timed::drop");
DBUG_PRINT("info",("%s.%s", dbname.str, name.str));
if (evex_open_event_table(thd, TL_WRITE, &table)) if (evex_open_event_table(thd, TL_WRITE, &table))
DBUG_RETURN(-1); DBUG_RETURN(-1);
if (evex_db_find_event_aux(thd, dbname, name, definer, table)) if (evex_db_find_event_by_name(thd, dbname, name, definer, table))
DBUG_RETURN(-2); DBUG_RETURN(-2);
if ((ret= table->file->ha_delete_row(table->record[0]))) if ((ret= table->file->ha_delete_row(table->record[0])))
...@@ -919,7 +920,7 @@ event_timed::update_fields(THD *thd) ...@@ -919,7 +920,7 @@ event_timed::update_fields(THD *thd)
} }
if ((ret= evex_db_find_event_aux(thd, dbname, name, definer, table))) if ((ret= evex_db_find_event_by_name(thd, dbname, name, definer, table)))
goto done; goto done;
store_record(table,record[1]); store_record(table,record[1]);
...@@ -1059,6 +1060,7 @@ event_timed::compile(THD *thd, MEM_ROOT *mem_root) ...@@ -1059,6 +1060,7 @@ event_timed::compile(THD *thd, MEM_ROOT *mem_root)
MEM_ROOT *tmp_mem_root= 0; MEM_ROOT *tmp_mem_root= 0;
LEX *old_lex= thd->lex, lex; LEX *old_lex= thd->lex, lex;
char *old_db; char *old_db;
int old_db_length;
event_timed *ett; event_timed *ett;
sp_name *spn; sp_name *spn;
char *old_query; char *old_query;
...@@ -1088,7 +1090,9 @@ event_timed::compile(THD *thd, MEM_ROOT *mem_root) ...@@ -1088,7 +1090,9 @@ event_timed::compile(THD *thd, MEM_ROOT *mem_root)
old_query_len= thd->query_length; old_query_len= thd->query_length;
old_query= thd->query; old_query= thd->query;
old_db= thd->db; old_db= thd->db;
old_db_length= thd->db_length;
thd->db= dbname.str; thd->db= dbname.str;
thd->db_length= dbname.length;
thd->query= get_show_create_event(thd, &thd->query_length); thd->query= get_show_create_event(thd, &thd->query_length);
DBUG_PRINT("event_timed::compile", ("query:%s",thd->query)); DBUG_PRINT("event_timed::compile", ("query:%s",thd->query));
...@@ -1148,3 +1152,135 @@ done: ...@@ -1148,3 +1152,135 @@ done:
DBUG_RETURN(ret); DBUG_RETURN(ret);
} }
/*
Checks whether this thread can lock the object for modification ->
preventing being spawned for execution, and locks if possible.
use ::can_spawn_now() only for basic checking because a race
condition may occur between the check and eventual modification (deletion)
of the object.
Returns
true - locked
false - cannot lock
*/
my_bool
event_timed::can_spawn_now_n_lock(THD *thd)
{
my_bool ret= FALSE;
VOID(pthread_mutex_lock(&this->LOCK_running));
if (!in_spawned_thread)
{
in_spawned_thread= TRUE;
ret= TRUE;
locked_by_thread_id= thd->thread_id;
}
VOID(pthread_mutex_unlock(&this->LOCK_running));
return ret;
}
extern pthread_attr_t connection_attrib;
/*
Checks whether is possible and forks a thread. Passes self as argument.
Returns
EVENT_EXEC_STARTED - OK
EVENT_EXEC_ALREADY_EXEC - Thread not forked, already working
EVENT_EXEC_CANT_FORK - Unable to spawn thread (error)
*/
int
event_timed::spawn_now(void * (*thread_func)(void*))
{
int ret= EVENT_EXEC_STARTED;
static uint exec_num= 0;
DBUG_ENTER("event_timed::spawn_now");
DBUG_PRINT("info", ("[%s.%s]", dbname.str, name.str));
VOID(pthread_mutex_lock(&this->LOCK_running));
if (!in_spawned_thread)
{
pthread_t th;
in_spawned_thread= true;
if (pthread_create(&th, &connection_attrib, thread_func, (void*)this))
{
DBUG_PRINT("info", ("problem while spawning thread"));
ret= EVENT_EXEC_CANT_FORK;
in_spawned_thread= false;
}
#ifndef DBUG_OFF
else
{
sql_print_information("SCHEDULER: Started thread %d", ++exec_num);
DBUG_PRINT("info", ("thread spawned"));
}
#endif
}
else
{
DBUG_PRINT("info", ("already in spawned thread. skipping"));
ret= EVENT_EXEC_ALREADY_EXEC;
}
VOID(pthread_mutex_unlock(&this->LOCK_running));
DBUG_RETURN(ret);
}
void
event_timed::spawn_thread_finish(THD *thd)
{
DBUG_ENTER("event_timed::spawn_thread_finish");
VOID(pthread_mutex_lock(&this->LOCK_running));
in_spawned_thread= false;
if ((flags & EVENT_EXEC_NO_MORE) || status == MYSQL_EVENT_DISABLED)
{
DBUG_PRINT("info", ("%s exec no more. to drop=%d", name.str, dropped));
if (dropped)
drop(thd);
VOID(pthread_mutex_unlock(&this->LOCK_running));
delete this;
DBUG_VOID_RETURN;
}
VOID(pthread_mutex_unlock(&this->LOCK_running));
DBUG_VOID_RETURN;
}
/*
Unlocks the object after it has been locked with ::can_spawn_now_n_lock()
Returns
0 - ok
1 - not locked by this thread
*/
int
event_timed::spawn_unlock(THD *thd)
{
int ret= 0;
VOID(pthread_mutex_lock(&this->LOCK_running));
if (!in_spawned_thread)
{
if (locked_by_thread_id == thd->thread_id)
{
in_spawned_thread= FALSE;
locked_by_thread_id= 0;
}
else
{
sql_print_error("A thread tries to unlock when he hasn't locked. "
"thread_id=%ld locked by %ld",
thd->thread_id, locked_by_thread_id);
DBUG_ASSERT(0);
ret= 1;
}
}
VOID(pthread_mutex_unlock(&this->LOCK_running));
return ret;
}
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
#include "mysql_priv.h" #include "mysql_priv.h"
#include <mysys_err.h> #include <mysys_err.h>
#include "sp.h" #include "sp.h"
#include "event.h"
#include <my_dir.h> #include <my_dir.h>
#include <m_ctype.h> #include <m_ctype.h>
#ifdef __WIN__ #ifdef __WIN__
...@@ -748,6 +749,7 @@ bool mysql_rm_db(THD *thd,char *db,bool if_exists, bool silent) ...@@ -748,6 +749,7 @@ bool mysql_rm_db(THD *thd,char *db,bool if_exists, bool silent)
exit: exit:
(void)sp_drop_db_routines(thd, db); /* QQ Ignore errors for now */ (void)sp_drop_db_routines(thd, db); /* QQ Ignore errors for now */
(void)evex_drop_db_events(thd, db); /* QQ Ignore errors for now */
start_waiting_global_read_lock(thd); start_waiting_global_read_lock(thd);
/* /*
If this database was the client's selected database, we silently change the If this database was the client's selected database, we silently change the
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment