2005-12-08 00:17:05 +01:00
|
|
|
/* Copyright (C) 2004-2005 MySQL AB
|
2005-12-05 11:45:04 +01:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
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 "event_priv.h"
|
2005-12-12 21:19:19 +01:00
|
|
|
#include "event.h"
|
2005-12-05 11:45:04 +01:00
|
|
|
#include "sp.h"
|
|
|
|
|
2006-02-16 00:43:11 +01:00
|
|
|
#define WAIT_STATUS_READY 0
|
|
|
|
#define WAIT_STATUS_EMPTY_QUEUE 1
|
|
|
|
#define WAIT_STATUS_NEW_TOP_EVENT 2
|
|
|
|
#define WAIT_STATUS_STOP_EXECUTOR 3
|
|
|
|
|
2005-12-08 20:37:54 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
Make this define DBUG_FAULTY_THR to be able to put breakpoints inside
|
|
|
|
code used by the scheduler's thread(s). In this case user connections
|
|
|
|
are not possible because the scheduler thread code is ran inside the
|
|
|
|
main thread (no spawning takes place. If you want to debug client
|
|
|
|
connection then start with --one-thread and make the define
|
2005-12-13 13:21:11 +01:00
|
|
|
DBUG_FAULTY_THR !
|
2005-12-08 20:37:54 +01:00
|
|
|
*/
|
2005-12-05 11:45:04 +01:00
|
|
|
#define DBUG_FAULTY_THR2
|
|
|
|
|
2005-12-07 22:29:00 +01:00
|
|
|
extern ulong thread_created;
|
2005-12-15 14:12:28 +01:00
|
|
|
extern const char *my_localhost;
|
2006-01-31 23:41:21 +01:00
|
|
|
extern pthread_attr_t connection_attrib;
|
2005-12-05 11:45:04 +01:00
|
|
|
|
2006-02-28 14:43:49 +01:00
|
|
|
pthread_mutex_t LOCK_event_arrays, // mutex for when working with the queue
|
|
|
|
LOCK_workers_count, // mutex for when inc/dec uint workers_count
|
|
|
|
LOCK_evex_running; // mutes for managing bool evex_is_running
|
2005-12-05 11:45:04 +01:00
|
|
|
|
|
|
|
|
|
|
|
bool evex_is_running= false;
|
|
|
|
|
2005-12-12 21:19:19 +01:00
|
|
|
ulonglong evex_main_thread_id= 0;
|
2005-12-05 11:45:04 +01:00
|
|
|
ulong opt_event_executor;
|
2006-01-31 23:41:21 +01:00
|
|
|
my_bool event_executor_running_global_var;
|
2006-02-26 14:11:56 +01:00
|
|
|
static my_bool evex_mutexes_initted= FALSE;
|
2005-12-07 22:29:00 +01:00
|
|
|
static uint workers_count;
|
2005-12-05 11:45:04 +01:00
|
|
|
|
|
|
|
static int
|
|
|
|
evex_load_events_from_db(THD *thd);
|
|
|
|
|
2006-02-10 15:02:57 +01:00
|
|
|
bool
|
|
|
|
evex_print_warnings(THD *thd, event_timed *et);
|
2005-12-05 11:45:04 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
TODO Andrey: Check for command line option whether to start
|
|
|
|
the main thread or not.
|
|
|
|
*/
|
|
|
|
|
2005-12-07 22:29:00 +01:00
|
|
|
pthread_handler_t
|
|
|
|
event_executor_worker(void *arg);
|
|
|
|
|
|
|
|
pthread_handler_t
|
|
|
|
event_executor_main(void *arg);
|
2005-12-05 11:45:04 +01:00
|
|
|
|
2006-02-14 16:20:48 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
Returns the seconds difference of 2 TIME structs
|
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
evex_time_diff()
|
|
|
|
a - TIME struct 1
|
|
|
|
b - TIME struct 2
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
the seconds difference
|
|
|
|
*/
|
|
|
|
|
2005-12-28 11:07:57 +01:00
|
|
|
static int
|
|
|
|
evex_time_diff(TIME *a, TIME *b)
|
|
|
|
{
|
|
|
|
return sec_since_epoch_TIME(a) - sec_since_epoch_TIME(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-02-14 16:20:48 +01:00
|
|
|
/*
|
|
|
|
Inits the mutexes used by the scheduler module
|
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
evex_init_mutexes()
|
|
|
|
|
|
|
|
NOTES
|
|
|
|
The mutexes are :
|
|
|
|
LOCK_event_arrays
|
|
|
|
LOCK_workers_count
|
|
|
|
LOCK_evex_running
|
|
|
|
*/
|
|
|
|
|
2005-12-28 11:07:57 +01:00
|
|
|
static void
|
|
|
|
evex_init_mutexes()
|
2005-12-06 16:15:29 +01:00
|
|
|
{
|
|
|
|
if (evex_mutexes_initted)
|
|
|
|
return;
|
2005-12-13 23:10:29 +01:00
|
|
|
|
2006-02-26 14:11:56 +01:00
|
|
|
evex_mutexes_initted= TRUE;
|
2005-12-06 16:15:29 +01:00
|
|
|
pthread_mutex_init(&LOCK_event_arrays, MY_MUTEX_INIT_FAST);
|
|
|
|
pthread_mutex_init(&LOCK_workers_count, MY_MUTEX_INIT_FAST);
|
|
|
|
pthread_mutex_init(&LOCK_evex_running, MY_MUTEX_INIT_FAST);
|
2005-12-13 23:10:29 +01:00
|
|
|
|
|
|
|
event_executor_running_global_var= opt_event_executor;
|
2005-12-06 16:15:29 +01:00
|
|
|
}
|
|
|
|
|
2006-02-14 20:58:19 +01:00
|
|
|
extern TABLE_FIELD_W_TYPE mysql_db_table_fields[];
|
|
|
|
extern time_t mysql_db_table_last_check;
|
2005-12-12 21:19:19 +01:00
|
|
|
|
2006-02-14 16:20:48 +01:00
|
|
|
/*
|
|
|
|
Opens mysql.db and mysql.user and checks whether
|
|
|
|
1. mysql.db has column Event_priv at column 20 (0 based);
|
|
|
|
2. mysql.user has column Event_priv at column 29 (0 based);
|
|
|
|
|
|
|
|
Synopsis
|
|
|
|
evex_check_system_tables()
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
|
|
|
evex_check_system_tables()
|
|
|
|
{
|
|
|
|
THD *thd= current_thd;
|
|
|
|
TABLE_LIST tables;
|
|
|
|
bool not_used;
|
|
|
|
Open_tables_state backup;
|
|
|
|
|
2006-02-28 14:43:49 +01:00
|
|
|
/* thd is 0x0 during boot of the server. Later it's !=0x0 */
|
2006-02-14 16:20:48 +01:00
|
|
|
if (!thd)
|
|
|
|
return;
|
|
|
|
|
|
|
|
thd->reset_n_backup_open_tables_state(&backup);
|
|
|
|
|
|
|
|
bzero((char*) &tables, sizeof(tables));
|
|
|
|
tables.db= (char*) "mysql";
|
|
|
|
tables.table_name= tables.alias= (char*) "db";
|
|
|
|
tables.lock_type= TL_READ;
|
|
|
|
|
|
|
|
if (simple_open_n_lock_tables(thd, &tables))
|
|
|
|
sql_print_error("Cannot open mysql.db");
|
|
|
|
else
|
|
|
|
{
|
|
|
|
table_check_intact(tables.table, MYSQL_DB_FIELD_COUNT, mysql_db_table_fields,
|
2006-02-28 14:43:49 +01:00
|
|
|
&mysql_db_table_last_check,ER_CANNOT_LOAD_FROM_TABLE);
|
2006-02-14 16:20:48 +01:00
|
|
|
close_thread_tables(thd);
|
|
|
|
}
|
|
|
|
|
|
|
|
bzero((char*) &tables, sizeof(tables));
|
|
|
|
tables.db= (char*) "mysql";
|
|
|
|
tables.table_name= tables.alias= (char*) "user";
|
|
|
|
tables.lock_type= TL_READ;
|
|
|
|
|
|
|
|
if (simple_open_n_lock_tables(thd, &tables))
|
|
|
|
sql_print_error("Cannot open mysql.db");
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (tables.table->s->fields < 29 ||
|
|
|
|
strncmp(tables.table->field[29]->field_name,
|
|
|
|
STRING_WITH_LEN("Event_priv")))
|
|
|
|
sql_print_error("mysql.user has no `Event_priv` column at position 29");
|
|
|
|
|
|
|
|
close_thread_tables(thd);
|
|
|
|
}
|
|
|
|
|
|
|
|
thd->restore_backup_open_tables_state(&backup);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
Inits the scheduler. Called on server start and every time the scheduler
|
|
|
|
is started with switching the event_scheduler global variable to TRUE
|
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
init_events()
|
2006-02-28 14:43:49 +01:00
|
|
|
|
2006-02-14 16:20:48 +01:00
|
|
|
NOTES
|
|
|
|
Inits the mutexes used by the scheduler. Done at server start.
|
|
|
|
*/
|
|
|
|
|
2005-12-05 11:45:04 +01:00
|
|
|
int
|
|
|
|
init_events()
|
|
|
|
{
|
|
|
|
pthread_t th;
|
|
|
|
DBUG_ENTER("init_events");
|
|
|
|
|
|
|
|
DBUG_PRINT("info",("Starting events main thread"));
|
2006-02-28 14:43:49 +01:00
|
|
|
|
2006-02-14 16:20:48 +01:00
|
|
|
evex_check_system_tables();
|
2005-12-05 11:45:04 +01:00
|
|
|
|
2005-12-06 16:15:29 +01:00
|
|
|
evex_init_mutexes();
|
2005-12-12 21:19:19 +01:00
|
|
|
|
2005-12-05 11:45:04 +01:00
|
|
|
VOID(pthread_mutex_lock(&LOCK_evex_running));
|
|
|
|
evex_is_running= false;
|
|
|
|
VOID(pthread_mutex_unlock(&LOCK_evex_running));
|
2005-12-20 13:21:02 +01:00
|
|
|
|
|
|
|
if (event_executor_running_global_var)
|
|
|
|
{
|
2005-12-05 11:45:04 +01:00
|
|
|
#ifndef DBUG_FAULTY_THR
|
2006-02-28 14:43:49 +01:00
|
|
|
/* TODO Andrey: Change the error code returned! */
|
2006-01-31 23:41:21 +01:00
|
|
|
if (pthread_create(&th, &connection_attrib, event_executor_main,(void*)NULL))
|
2005-12-20 13:21:02 +01:00
|
|
|
DBUG_RETURN(ER_SLAVE_THREAD);
|
2005-12-05 11:45:04 +01:00
|
|
|
#else
|
2005-12-20 13:21:02 +01:00
|
|
|
event_executor_main(NULL);
|
2005-12-05 11:45:04 +01:00
|
|
|
#endif
|
2005-12-20 13:21:02 +01:00
|
|
|
}
|
2005-12-05 11:45:04 +01:00
|
|
|
|
|
|
|
DBUG_RETURN(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-02-14 16:20:48 +01:00
|
|
|
/*
|
|
|
|
Cleans up scheduler memory. Called on server shutdown.
|
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
shutdown_events()
|
2006-02-28 14:43:49 +01:00
|
|
|
|
2006-02-14 16:20:48 +01:00
|
|
|
NOTES
|
|
|
|
Destroys the mutexes.
|
|
|
|
*/
|
|
|
|
|
2005-12-05 11:45:04 +01:00
|
|
|
void
|
|
|
|
shutdown_events()
|
|
|
|
{
|
|
|
|
DBUG_ENTER("shutdown_events");
|
2006-02-28 14:43:49 +01:00
|
|
|
|
2006-02-26 14:11:56 +01:00
|
|
|
if (evex_mutexes_initted)
|
|
|
|
{
|
2006-02-28 14:43:49 +01:00
|
|
|
evex_mutexes_initted= FALSE;
|
2006-02-26 14:11:56 +01:00
|
|
|
VOID(pthread_mutex_lock(&LOCK_evex_running));
|
|
|
|
VOID(pthread_mutex_unlock(&LOCK_evex_running));
|
2005-12-12 21:19:19 +01:00
|
|
|
|
2006-02-26 14:11:56 +01:00
|
|
|
pthread_mutex_destroy(&LOCK_event_arrays);
|
|
|
|
pthread_mutex_destroy(&LOCK_workers_count);
|
|
|
|
pthread_mutex_destroy(&LOCK_evex_running);
|
|
|
|
}
|
2005-12-05 11:45:04 +01:00
|
|
|
DBUG_VOID_RETURN;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-02-14 16:20:48 +01:00
|
|
|
/*
|
|
|
|
Inits an scheduler thread handler, both the main and a worker
|
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
init_event_thread()
|
|
|
|
thd - the THD of the thread. Has to be allocated by the caller.
|
2006-02-28 14:43:49 +01:00
|
|
|
|
2006-02-14 16:20:48 +01:00
|
|
|
NOTES
|
|
|
|
1. The host of the thead is my_localhost
|
|
|
|
2. thd->net is initted with NULL - no communication.
|
2006-02-28 14:43:49 +01:00
|
|
|
|
2006-02-14 16:20:48 +01:00
|
|
|
Returns
|
|
|
|
0 - OK
|
|
|
|
-1 - Error
|
|
|
|
*/
|
|
|
|
|
2005-12-05 11:45:04 +01:00
|
|
|
static int
|
|
|
|
init_event_thread(THD* thd)
|
|
|
|
{
|
|
|
|
DBUG_ENTER("init_event_thread");
|
|
|
|
thd->client_capabilities= 0;
|
2006-02-10 15:02:57 +01:00
|
|
|
thd->security_ctx->master_access= 0;
|
|
|
|
thd->security_ctx->db_access= 0;
|
2006-02-21 02:40:23 +01:00
|
|
|
thd->security_ctx->host_or_ip= (char*)my_localhost;
|
2005-12-05 11:45:04 +01:00
|
|
|
my_net_init(&thd->net, 0);
|
|
|
|
thd->net.read_timeout = slave_net_timeout;
|
|
|
|
thd->slave_thread= 0;
|
|
|
|
thd->options= OPTION_AUTO_IS_NULL;
|
|
|
|
thd->client_capabilities= CLIENT_LOCAL_FILES;
|
|
|
|
thd->real_id=pthread_self();
|
|
|
|
VOID(pthread_mutex_lock(&LOCK_thread_count));
|
|
|
|
thd->thread_id= thread_id++;
|
|
|
|
VOID(pthread_mutex_unlock(&LOCK_thread_count));
|
|
|
|
|
|
|
|
if (init_thr_lock() || thd->store_globals())
|
|
|
|
{
|
|
|
|
thd->cleanup();
|
|
|
|
delete thd;
|
|
|
|
DBUG_RETURN(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if !defined(__WIN__) && !defined(OS2) && !defined(__NETWARE__)
|
|
|
|
sigset_t set;
|
|
|
|
VOID(sigemptyset(&set)); // Get mask in use
|
|
|
|
VOID(pthread_sigmask(SIG_UNBLOCK,&set,&thd->block_signals));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
thd->proc_info= "Initialized";
|
|
|
|
thd->version= refresh_version;
|
|
|
|
thd->set_time();
|
|
|
|
DBUG_RETURN(0);
|
|
|
|
}
|
|
|
|
|
2006-02-14 16:20:48 +01:00
|
|
|
|
2006-02-16 00:43:11 +01:00
|
|
|
/*
|
|
|
|
This function waits till the time next event in the queue should be
|
|
|
|
executed.
|
2006-02-28 14:43:49 +01:00
|
|
|
|
2006-02-16 00:43:11 +01:00
|
|
|
Returns
|
|
|
|
WAIT_STATUS_READY There is an event to be executed right now
|
|
|
|
WAIT_STATUS_EMPTY_QUEUE No events or the last event was dropped.
|
|
|
|
WAIT_STATUS_NEW_TOP_EVENT New event has entered the queue and scheduled
|
|
|
|
on top. Restart ticking.
|
|
|
|
WAIT_STATUS_STOP_EXECUTOR The thread was killed or SET global event_scheduler=0;
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int
|
|
|
|
executor_wait_till_next_event_exec(THD *thd)
|
|
|
|
{
|
|
|
|
event_timed *et;
|
|
|
|
TIME time_now;
|
|
|
|
int t2sleep;
|
|
|
|
|
|
|
|
DBUG_ENTER("executor_wait_till_next_event_exec");
|
|
|
|
/*
|
|
|
|
now let's see how much time to sleep, we know there is at least 1
|
|
|
|
element in the queue.
|
|
|
|
*/
|
|
|
|
VOID(pthread_mutex_lock(&LOCK_event_arrays));
|
|
|
|
if (!evex_queue_num_elements(EVEX_EQ_NAME))
|
|
|
|
{
|
|
|
|
VOID(pthread_mutex_unlock(&LOCK_event_arrays));
|
2006-02-28 17:02:31 +01:00
|
|
|
DBUG_RETURN(WAIT_STATUS_EMPTY_QUEUE);
|
2006-02-16 00:43:11 +01:00
|
|
|
}
|
|
|
|
et= evex_queue_first_element(&EVEX_EQ_NAME, event_timed*);
|
|
|
|
DBUG_ASSERT(et);
|
|
|
|
if (et->status == MYSQL_EVENT_DISABLED)
|
|
|
|
{
|
|
|
|
DBUG_PRINT("evex main thread",("Now it is disabled-exec no more"));
|
|
|
|
if (et->dropped)
|
|
|
|
et->drop(thd);
|
|
|
|
delete et;
|
2006-02-28 14:43:49 +01:00
|
|
|
evex_queue_delete_element(&EVEX_EQ_NAME, 0);// 0 is top, internally 1
|
2006-02-16 00:43:11 +01:00
|
|
|
VOID(pthread_mutex_unlock(&LOCK_event_arrays));
|
|
|
|
sql_print_information("Event found disabled, dropping.");
|
|
|
|
DBUG_RETURN(1);
|
|
|
|
}
|
2006-02-28 14:43:49 +01:00
|
|
|
|
2006-02-16 00:43:11 +01:00
|
|
|
DBUG_PRINT("evex main thread",("computing time to sleep till next exec"));
|
2006-02-28 14:43:49 +01:00
|
|
|
/* set the internal clock of thd */
|
2006-02-16 00:43:11 +01:00
|
|
|
thd->end_time();
|
|
|
|
my_tz_UTC->gmt_sec_to_TIME(&time_now, thd->query_start());
|
|
|
|
t2sleep= evex_time_diff(&et->execute_at, &time_now);
|
|
|
|
VOID(pthread_mutex_unlock(&LOCK_event_arrays));
|
|
|
|
|
|
|
|
DBUG_PRINT("evex main thread",("unlocked LOCK_event_arrays"));
|
|
|
|
if (t2sleep > 0)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
We sleep t2sleep seconds but we check every second whether this thread
|
|
|
|
has been killed, or there is a new candidate
|
|
|
|
*/
|
|
|
|
while (t2sleep-- && !thd->killed && event_executor_running_global_var &&
|
|
|
|
evex_queue_num_elements(EVEX_EQ_NAME) &&
|
|
|
|
(evex_queue_first_element(&EVEX_EQ_NAME, event_timed*) == et))
|
|
|
|
{
|
|
|
|
DBUG_PRINT("evex main thread",("will sleep a bit more"));
|
|
|
|
my_sleep(1000000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-02-28 17:02:31 +01:00
|
|
|
int ret= WAIT_STATUS_READY;
|
2006-02-16 00:43:11 +01:00
|
|
|
if (!evex_queue_num_elements(EVEX_EQ_NAME))
|
2006-02-28 17:02:31 +01:00
|
|
|
ret= WAIT_STATUS_EMPTY_QUEUE;
|
2006-02-16 00:43:11 +01:00
|
|
|
else if (evex_queue_first_element(&EVEX_EQ_NAME, event_timed*) != et)
|
2006-02-28 17:02:31 +01:00
|
|
|
ret= WAIT_STATUS_NEW_TOP_EVENT;
|
2006-02-16 00:43:11 +01:00
|
|
|
if (thd->killed && event_executor_running_global_var)
|
2006-02-28 17:02:31 +01:00
|
|
|
ret= WAIT_STATUS_STOP_EXECUTOR;
|
2006-02-16 00:43:11 +01:00
|
|
|
|
|
|
|
DBUG_RETURN(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-02-14 16:20:48 +01:00
|
|
|
/*
|
|
|
|
The main scheduler thread. Inits the priority queue on start and
|
|
|
|
destroys it on thread shutdown. Forks child threads for every event
|
|
|
|
execution. Sleeps between thread forking and does not do a busy wait.
|
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
event_executor_main()
|
2006-02-28 14:43:49 +01:00
|
|
|
arg unused
|
|
|
|
|
2006-02-14 16:20:48 +01:00
|
|
|
NOTES
|
|
|
|
1. The host of the thead is my_localhost
|
|
|
|
2. thd->net is initted with NULL - no communication.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2005-12-07 22:29:00 +01:00
|
|
|
pthread_handler_t
|
|
|
|
event_executor_main(void *arg)
|
2005-12-05 11:45:04 +01:00
|
|
|
{
|
|
|
|
THD *thd; /* needs to be first for thread_stack */
|
|
|
|
uint i=0, j=0;
|
2005-12-13 19:16:00 +01:00
|
|
|
my_ulonglong cnt= 0;
|
2006-02-16 00:43:11 +01:00
|
|
|
TIME time_now;
|
2005-12-05 11:45:04 +01:00
|
|
|
|
|
|
|
DBUG_ENTER("event_executor_main");
|
2006-02-28 14:43:49 +01:00
|
|
|
DBUG_PRINT("event_executor_main", ("EVEX thread started"));
|
2005-12-05 11:45:04 +01:00
|
|
|
|
|
|
|
|
2006-02-28 14:43:49 +01:00
|
|
|
/* init memory root */
|
2005-12-05 11:45:04 +01:00
|
|
|
init_alloc_root(&evex_mem_root, MEM_ROOT_BLOCK_SIZE, MEM_ROOT_PREALLOC);
|
|
|
|
|
2006-02-28 14:43:49 +01:00
|
|
|
/* needs to call my_thread_init(), otherwise we get a coredump in DBUG_ stuff*/
|
2005-12-05 11:45:04 +01:00
|
|
|
my_thread_init();
|
2006-01-12 16:51:45 +01:00
|
|
|
|
|
|
|
if (sizeof(my_time_t) != sizeof(time_t))
|
|
|
|
{
|
2006-02-16 00:43:11 +01:00
|
|
|
sql_print_error("SCHEDULER: sizeof(my_time_t) != sizeof(time_t) ."
|
2006-01-12 16:51:45 +01:00
|
|
|
"The scheduler will not work correctly. Stopping.");
|
2006-02-16 00:43:11 +01:00
|
|
|
DBUG_ASSERT(0);
|
2006-01-12 16:51:45 +01:00
|
|
|
goto err_no_thd;
|
|
|
|
}
|
2005-12-05 11:45:04 +01:00
|
|
|
|
2006-02-28 14:43:49 +01:00
|
|
|
/* note that contructor of THD uses DBUG_ ! */
|
|
|
|
if (!(thd = new THD))
|
2005-12-05 11:45:04 +01:00
|
|
|
{
|
2006-02-16 00:43:11 +01:00
|
|
|
sql_print_error("SCHEDULER: Cannot create THD for the main thread.");
|
2005-12-05 11:45:04 +01:00
|
|
|
goto err_no_thd;
|
2006-01-12 16:51:45 +01:00
|
|
|
}
|
2006-02-28 14:43:49 +01:00
|
|
|
thd->thread_stack = (char*)&thd; // remember where our stack is
|
|
|
|
|
2005-12-05 11:45:04 +01:00
|
|
|
pthread_detach_this_thread();
|
|
|
|
|
|
|
|
if (init_event_thread(thd))
|
2006-02-16 00:43:11 +01:00
|
|
|
goto finish;
|
2006-02-14 17:51:22 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
make this thread visible it has no vio -> show processlist won't see it
|
|
|
|
unless it's marked as system thread
|
|
|
|
*/
|
2005-12-06 16:15:29 +01:00
|
|
|
thd->system_thread= 1;
|
2005-12-05 11:45:04 +01:00
|
|
|
|
|
|
|
VOID(pthread_mutex_lock(&LOCK_thread_count));
|
|
|
|
threads.append(thd);
|
|
|
|
thread_count++;
|
|
|
|
thread_running++;
|
|
|
|
VOID(pthread_mutex_unlock(&LOCK_thread_count));
|
|
|
|
|
2006-02-16 00:43:11 +01:00
|
|
|
DBUG_PRINT("EVEX main thread", ("Initing events_queue"));
|
2005-12-05 11:45:04 +01:00
|
|
|
|
2005-12-07 22:29:00 +01:00
|
|
|
/*
|
|
|
|
eventually manifest that we are running, not to crashe because of
|
|
|
|
usage of non-initialized memory structures.
|
|
|
|
*/
|
|
|
|
VOID(pthread_mutex_lock(&LOCK_evex_running));
|
2005-12-13 19:16:00 +01:00
|
|
|
VOID(pthread_mutex_lock(&LOCK_event_arrays));
|
|
|
|
evex_queue_init(&EVEX_EQ_NAME);
|
|
|
|
VOID(pthread_mutex_unlock(&LOCK_event_arrays));
|
2005-12-07 22:29:00 +01:00
|
|
|
evex_is_running= true;
|
|
|
|
VOID(pthread_mutex_unlock(&LOCK_evex_running));
|
|
|
|
|
2005-12-16 12:42:39 +01:00
|
|
|
thd->security_ctx->user= my_strdup("event_scheduler", MYF(0));
|
|
|
|
|
2005-12-05 11:45:04 +01:00
|
|
|
if (evex_load_events_from_db(thd))
|
2006-02-16 00:43:11 +01:00
|
|
|
goto finish;
|
2005-12-05 11:45:04 +01:00
|
|
|
|
2005-12-12 21:19:19 +01:00
|
|
|
evex_main_thread_id= thd->thread_id;
|
2005-12-16 12:42:39 +01:00
|
|
|
|
2006-02-16 00:43:11 +01:00
|
|
|
sql_print_information("SCHEDULER: Main thread started");
|
2005-12-05 11:45:04 +01:00
|
|
|
while (!thd->killed)
|
|
|
|
{
|
|
|
|
TIME time_now;
|
2005-12-12 21:19:19 +01:00
|
|
|
event_timed *et;
|
2006-02-28 14:43:49 +01:00
|
|
|
|
2005-12-13 19:16:00 +01:00
|
|
|
cnt++;
|
2006-01-11 18:09:05 +01:00
|
|
|
DBUG_PRINT("info", ("EVEX External Loop %d thd->k", cnt));
|
2005-12-13 23:10:29 +01:00
|
|
|
|
2005-12-05 11:45:04 +01:00
|
|
|
thd->proc_info = "Sleeping";
|
2006-01-11 18:09:05 +01:00
|
|
|
if (!event_executor_running_global_var)
|
|
|
|
{
|
2006-02-16 00:43:11 +01:00
|
|
|
sql_print_information("SCHEDULER: Asked to stop.");
|
2006-01-11 18:09:05 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!evex_queue_num_elements(EVEX_EQ_NAME))
|
2005-12-12 21:19:19 +01:00
|
|
|
{
|
|
|
|
my_sleep(1000000);// sleep 1s
|
2005-12-05 11:45:04 +01:00
|
|
|
continue;
|
2005-12-12 21:19:19 +01:00
|
|
|
}
|
2006-02-28 14:43:49 +01:00
|
|
|
|
2006-02-16 00:43:11 +01:00
|
|
|
restart_ticking:
|
|
|
|
switch (executor_wait_till_next_event_exec(thd)) {
|
|
|
|
case WAIT_STATUS_READY: // time to execute the event on top
|
|
|
|
DBUG_PRINT("evex main thread",("time to execute an event"));
|
|
|
|
break;
|
|
|
|
case WAIT_STATUS_EMPTY_QUEUE: // no more events
|
|
|
|
DBUG_PRINT("evex main thread",("no more events"));
|
|
|
|
continue;
|
|
|
|
break;
|
2006-02-28 14:43:49 +01:00
|
|
|
case WAIT_STATUS_NEW_TOP_EVENT: // new event on top in the queue
|
2006-02-16 00:43:11 +01:00
|
|
|
DBUG_PRINT("evex main thread",("restart ticking"));
|
|
|
|
goto restart_ticking;
|
|
|
|
case WAIT_STATUS_STOP_EXECUTOR:
|
|
|
|
sql_print_information("SCHEDULER: Asked to stop.");
|
|
|
|
goto finish;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
DBUG_ASSERT(0);
|
2005-12-12 21:19:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
VOID(pthread_mutex_lock(&LOCK_event_arrays));
|
2006-02-16 00:43:11 +01:00
|
|
|
thd->end_time();
|
|
|
|
my_tz_UTC->gmt_sec_to_TIME(&time_now, thd->query_start());
|
2005-12-12 21:19:19 +01:00
|
|
|
|
|
|
|
if (!evex_queue_num_elements(EVEX_EQ_NAME))
|
|
|
|
{
|
|
|
|
VOID(pthread_mutex_unlock(&LOCK_event_arrays));
|
2006-01-11 19:31:21 +01:00
|
|
|
DBUG_PRINT("evex main thread",("empty queue"));
|
2005-12-12 21:19:19 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
et= evex_queue_first_element(&EVEX_EQ_NAME, event_timed*);
|
2006-01-11 19:31:21 +01:00
|
|
|
DBUG_PRINT("evex main thread",("got event from the queue"));
|
2005-12-05 11:45:04 +01:00
|
|
|
|
2006-02-28 11:43:10 +01:00
|
|
|
if (!et->execute_at_null && my_time_compare(&time_now,&et->execute_at) == -1)
|
2005-12-12 21:19:19 +01:00
|
|
|
{
|
2006-01-11 19:31:21 +01:00
|
|
|
DBUG_PRINT("evex main thread",("still not the time for execution"));
|
2005-12-12 21:19:19 +01:00
|
|
|
VOID(pthread_mutex_unlock(&LOCK_event_arrays));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2006-01-11 19:31:21 +01:00
|
|
|
DBUG_PRINT("evex main thread",("it's right time"));
|
2005-12-12 21:19:19 +01:00
|
|
|
if (et->status == MYSQL_EVENT_ENABLED)
|
|
|
|
{
|
2006-02-16 00:43:11 +01:00
|
|
|
int fork_ret_code;
|
2005-12-05 11:45:04 +01:00
|
|
|
|
2006-01-18 20:41:22 +01:00
|
|
|
DBUG_PRINT("evex main thread", ("[%10s] this exec at [%llu]", et->name.str,
|
|
|
|
TIME_to_ulonglong_datetime(&et->execute_at)));
|
|
|
|
et->mark_last_executed(thd);
|
|
|
|
if (et->compute_next_execution_time())
|
|
|
|
{
|
2006-02-16 00:43:11 +01:00
|
|
|
sql_print_error("SCHEDULER: Error while computing time of %s.%s . "
|
2006-01-18 20:41:22 +01:00
|
|
|
"Disabling after execution.",
|
|
|
|
et->dbname.str, et->name.str);
|
|
|
|
et->status= MYSQL_EVENT_DISABLED;
|
|
|
|
}
|
|
|
|
DBUG_PRINT("evex main thread", ("[%10s] next exec at [%llu]", et->name.str,
|
|
|
|
TIME_to_ulonglong_datetime(&et->execute_at)));
|
|
|
|
|
2005-12-28 11:07:57 +01:00
|
|
|
et->update_fields(thd);
|
2005-12-05 11:45:04 +01:00
|
|
|
#ifndef DBUG_FAULTY_THR
|
2006-02-16 00:43:11 +01:00
|
|
|
thread_safe_increment(workers_count, &LOCK_workers_count);
|
|
|
|
switch ((fork_ret_code= et->spawn_now(event_executor_worker))) {
|
|
|
|
case EVENT_EXEC_CANT_FORK:
|
|
|
|
thread_safe_decrement(workers_count, &LOCK_workers_count);
|
|
|
|
sql_print_error("SCHEDULER: Problem while trying to create a thread");
|
|
|
|
UNLOCK_MUTEX_AND_BAIL_OUT(LOCK_event_arrays, finish);
|
|
|
|
case EVENT_EXEC_ALREADY_EXEC:
|
|
|
|
thread_safe_decrement(workers_count, &LOCK_workers_count);
|
|
|
|
sql_print_information("SCHEDULER: %s.%s in execution. Skip this time.",
|
|
|
|
et->dbname.str, et->name.str);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
DBUG_ASSERT(!fork_ret_code);
|
|
|
|
if (fork_ret_code)
|
|
|
|
thread_safe_decrement(workers_count, &LOCK_workers_count);
|
|
|
|
break;
|
2005-12-12 21:19:19 +01:00
|
|
|
}
|
2005-12-05 11:45:04 +01:00
|
|
|
#else
|
2005-12-12 21:19:19 +01:00
|
|
|
event_executor_worker((void *) et);
|
2005-12-05 11:45:04 +01:00
|
|
|
#endif
|
2006-02-28 11:43:10 +01:00
|
|
|
/*
|
|
|
|
1. For one-time event : year is > 0 and expression is 0
|
|
|
|
2. For recurring, expression is != -=> check execute_at_null in this case
|
|
|
|
*/
|
|
|
|
if ((et->execute_at.year && !et->expression) || et->execute_at_null)
|
2005-12-12 21:19:19 +01:00
|
|
|
et->flags |= EVENT_EXEC_NO_MORE;
|
2005-12-05 11:45:04 +01:00
|
|
|
|
2005-12-28 11:07:57 +01:00
|
|
|
if ((et->flags & EVENT_EXEC_NO_MORE) || et->status == MYSQL_EVENT_DISABLED)
|
2006-02-16 00:43:11 +01:00
|
|
|
evex_queue_delete_element(&EVEX_EQ_NAME, 0);// 0 is top, internally 1
|
2005-12-28 11:07:57 +01:00
|
|
|
else
|
|
|
|
evex_queue_first_updated(&EVEX_EQ_NAME);
|
|
|
|
}
|
2006-01-11 19:31:21 +01:00
|
|
|
DBUG_PRINT("evex main thread",("unlocking"));
|
2005-12-05 11:45:04 +01:00
|
|
|
VOID(pthread_mutex_unlock(&LOCK_event_arrays));
|
2006-02-28 14:43:49 +01:00
|
|
|
}/* while */
|
2006-02-16 00:43:11 +01:00
|
|
|
finish:
|
2005-12-05 11:45:04 +01:00
|
|
|
|
2006-02-28 14:43:49 +01:00
|
|
|
/* First manifest that this thread does not work and then destroy */
|
2005-12-05 11:45:04 +01:00
|
|
|
VOID(pthread_mutex_lock(&LOCK_evex_running));
|
2005-12-12 21:19:19 +01:00
|
|
|
evex_is_running= false;
|
|
|
|
evex_main_thread_id= 0;
|
2005-12-05 11:45:04 +01:00
|
|
|
VOID(pthread_mutex_unlock(&LOCK_evex_running));
|
|
|
|
|
2005-12-07 22:29:00 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
TODO: A better will be with a conditional variable
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
Read workers_count without lock, no need for locking.
|
|
|
|
In the worst case we have to wait 1sec more.
|
|
|
|
*/
|
2006-02-16 00:43:11 +01:00
|
|
|
sql_print_information("SCHEDULER: Stopping. Waiting for worker threads to finish.");
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
VOID(pthread_mutex_lock(&LOCK_workers_count));
|
|
|
|
if (!workers_count)
|
|
|
|
{
|
|
|
|
VOID(pthread_mutex_unlock(&LOCK_workers_count));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
VOID(pthread_mutex_unlock(&LOCK_workers_count));
|
2006-02-28 14:43:49 +01:00
|
|
|
my_sleep(1000000);// 1s
|
2006-02-16 00:43:11 +01:00
|
|
|
}
|
2005-12-07 22:29:00 +01:00
|
|
|
|
|
|
|
/*
|
2006-02-16 00:43:11 +01:00
|
|
|
First we free all objects ...
|
|
|
|
Lock because a DROP DATABASE could be running in parallel and it locks on these
|
2005-12-07 22:29:00 +01:00
|
|
|
*/
|
2006-02-16 00:43:11 +01:00
|
|
|
sql_print_information("SCHEDULER: Emptying the queue.");
|
|
|
|
VOID(pthread_mutex_lock(&LOCK_event_arrays));
|
2005-12-13 19:16:00 +01:00
|
|
|
for (i= 0; i < evex_queue_num_elements(EVEX_EQ_NAME); ++i)
|
|
|
|
{
|
|
|
|
event_timed *et= evex_queue_element(&EVEX_EQ_NAME, i, event_timed*);
|
|
|
|
et->free_sp();
|
|
|
|
delete et;
|
|
|
|
}
|
2006-02-16 00:43:11 +01:00
|
|
|
VOID(pthread_mutex_unlock(&LOCK_event_arrays));
|
2006-02-28 14:43:49 +01:00
|
|
|
/* ... then we can thrash the whole queue at once */
|
2005-12-12 21:19:19 +01:00
|
|
|
evex_queue_destroy(&EVEX_EQ_NAME);
|
2006-02-28 14:43:49 +01:00
|
|
|
|
2005-12-05 11:45:04 +01:00
|
|
|
thd->proc_info = "Clearing";
|
|
|
|
DBUG_ASSERT(thd->net.buff != 0);
|
|
|
|
net_end(&thd->net); // destructor will not free it, because we are weird
|
|
|
|
THD_CHECK_SENTRY(thd);
|
2005-12-07 22:29:00 +01:00
|
|
|
|
2005-12-05 11:45:04 +01:00
|
|
|
pthread_mutex_lock(&LOCK_thread_count);
|
|
|
|
thread_count--;
|
|
|
|
thread_running--;
|
2005-12-12 21:19:19 +01:00
|
|
|
#ifndef DBUG_FAULTY_THR
|
2005-12-05 11:45:04 +01:00
|
|
|
THD_CHECK_SENTRY(thd);
|
|
|
|
delete thd;
|
2005-12-12 21:19:19 +01:00
|
|
|
#endif
|
2005-12-05 11:45:04 +01:00
|
|
|
pthread_mutex_unlock(&LOCK_thread_count);
|
|
|
|
|
|
|
|
|
|
|
|
err_no_thd:
|
|
|
|
VOID(pthread_mutex_lock(&LOCK_evex_running));
|
|
|
|
evex_is_running= false;
|
|
|
|
VOID(pthread_mutex_unlock(&LOCK_evex_running));
|
|
|
|
|
|
|
|
free_root(&evex_mem_root, MYF(0));
|
2006-02-16 00:43:11 +01:00
|
|
|
sql_print_information("SCHEDULER: Stopped.");
|
2005-12-05 11:45:04 +01:00
|
|
|
|
2005-12-12 21:19:19 +01:00
|
|
|
#ifndef DBUG_FAULTY_THR
|
2005-12-05 11:45:04 +01:00
|
|
|
my_thread_end();
|
|
|
|
pthread_exit(0);
|
2005-12-12 21:19:19 +01:00
|
|
|
#endif
|
2006-02-28 14:43:49 +01:00
|
|
|
DBUG_RETURN(0); // Can't return anything here
|
2005-12-05 11:45:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-02-14 16:20:48 +01:00
|
|
|
/*
|
|
|
|
Function that executes an event in a child thread. Setups the
|
|
|
|
environment for the event execution and cleans after that.
|
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
event_executor_worker()
|
2006-02-28 14:43:49 +01:00
|
|
|
arg The event_timed object to be processed
|
2006-02-14 16:20:48 +01:00
|
|
|
*/
|
|
|
|
|
2005-12-07 22:29:00 +01:00
|
|
|
pthread_handler_t
|
|
|
|
event_executor_worker(void *event_void)
|
2005-12-05 11:45:04 +01:00
|
|
|
{
|
|
|
|
THD *thd; /* needs to be first for thread_stack */
|
|
|
|
event_timed *event = (event_timed *) event_void;
|
2005-12-07 22:29:00 +01:00
|
|
|
MEM_ROOT worker_mem_root;
|
2005-12-05 11:45:04 +01:00
|
|
|
|
|
|
|
DBUG_ENTER("event_executor_worker");
|
|
|
|
|
2005-12-07 22:29:00 +01:00
|
|
|
init_alloc_root(&worker_mem_root, MEM_ROOT_BLOCK_SIZE, MEM_ROOT_PREALLOC);
|
2005-12-05 11:45:04 +01:00
|
|
|
|
2005-12-12 21:19:19 +01:00
|
|
|
#ifndef DBUG_FAULTY_THR
|
2005-12-05 11:45:04 +01:00
|
|
|
my_thread_init();
|
|
|
|
|
2006-02-28 14:43:49 +01:00
|
|
|
if (!(thd = new THD)) /* note that contructor of THD uses DBUG_ ! */
|
2005-12-05 11:45:04 +01:00
|
|
|
{
|
2006-02-16 00:43:11 +01:00
|
|
|
sql_print_error("SCHEDULER: Cannot create a THD structure in an worker.");
|
2005-12-05 11:45:04 +01:00
|
|
|
goto err_no_thd;
|
|
|
|
}
|
2006-02-28 14:43:49 +01:00
|
|
|
thd->thread_stack = (char*)&thd; // remember where our stack is
|
2005-12-07 22:29:00 +01:00
|
|
|
thd->mem_root= &worker_mem_root;
|
|
|
|
|
2006-01-30 13:09:08 +01:00
|
|
|
pthread_detach_this_thread();
|
fix for bug#16642 (Events: No INFORMATION_SCHEMA.EVENTS table)
post-review change - use pointer instead of copy on the stack.
WL#1034 (Internal CRON)
This patch adds INFORMATION_SCHEMA.EVENTS table with the following format:
EVENT_CATALOG - MYSQL_TYPE_STRING (Always NULL)
EVENT_SCHEMA - MYSQL_TYPE_STRING (the database)
EVENT_NAME - MYSQL_TYPE_STRING (the name)
DEFINER - MYSQL_TYPE_STRING (user@host)
EVENT_BODY - MYSQL_TYPE_STRING (the body from mysql.event)
EVENT_TYPE - MYSQL_TYPE_STRING ("ONE TIME" | "RECURRING")
EXECUTE_AT - MYSQL_TYPE_TIMESTAMP (set for "ONE TIME" otherwise NULL)
INTERVAL_VALUE - MYSQL_TYPE_LONG (set for RECURRING otherwise NULL)
INTERVAL_FIELD - MYSQL_TYPE_STRING (set for RECURRING otherwise NULL)
SQL_MODE - MYSQL_TYPE_STRING (for now NULL)
STARTS - MYSQL_TYPE_TIMESTAMP (starts from mysql.event)
ENDS - MYSQL_TYPE_TIMESTAMP (ends from mysql.event)
STATUS - MYSQL_TYPE_STRING (ENABLED | DISABLED)
ON_COMPLETION - MYSQL_TYPE_STRING (NOT PRESERVE | PRESERVE)
CREATED - MYSQL_TYPE_TIMESTAMP
LAST_ALTERED - MYSQL_TYPE_TIMESTAMP
LAST_EXECUTED - MYSQL_TYPE_TIMESTAMP
EVENT_COMMENT - MYSQL_TYPE_STRING
SQL_MODE is NULL for now, because the value is still not stored in mysql.event .
Support will be added as a fix for another bug.
This patch also adds SHOW [FULL] EVENTS [FROM db] [LIKE pattern]
1. SHOW EVENTS shows always only the events on the same user,
because the PK of mysql.event is (definer, db, name) several
users may have event with the same name -> no information disclosure.
2. SHOW FULL EVENTS - shows the events (in the current db as SHOW EVENTS)
of all users. The user has to have PROCESS privilege, if not then
SHOW FULL EVENTS behave like SHOW EVENTS.
3. If [FROM db] is specified then this db is considered.
4. Event names can be filtered with LIKE pattern.
SHOW EVENTS returns table with the following columns, which are subset of
the data which is returned by SELECT * FROM I_S.EVENTS
Db
Name
Definer
Type
Execute at
Interval value
Interval field
Starts
Ends
Status
mysql-test/lib/init_db.sql:
change the PK - (definer, db, name)
quicker searches when SHOW EVENTS;
allow also different users to have events with the same name ->
no information disclosure
mysql-test/r/events.result:
result of new tests
mysql-test/r/information_schema.result:
result of new tests
mysql-test/r/information_schema_db.result:
result of new tests
mysql-test/r/system_mysql_db.result:
result of new tests
mysql-test/t/events.test:
new tests for information_schema.events
scripts/mysql_create_system_tables.sh:
change the PK of mysql.event to (definer, db, name)
scripts/mysql_fix_privilege_tables.sql:
change the PK of mysql.event to (definer, db, name)
sql/event.cc:
pass around the definer of the event because of the new PK
which is (definer, db, name). It's needed for index searching.
sql/event.h:
- make enum evex_table_field again public so it can be used
in sql_show.cc
- make created and modified ulonglong, because they should be such
- make public evex_open_event_table so it can be used in sql_show.cc
sql/event_executor.cc:
- cosmetics
sql/event_priv.h:
- moved enum evex_table_field and evex_open_event_table()
to event.h (made them therefore public)
sql/event_timed.cc:
- in event_timed::init_definer() always fill this.definer with
the concatenated value of definer_user@definer_host. Makes
later the work easier.
- pass around the definer wherever is needed for searching
(new prototype of evex_db_find_evex_aux)
sql/mysqld.cc:
- add counter for SHOW EVENTS
sql/sql_lex.h:
- register SHOW EVENTS as command
sql/sql_parse.cc:
- handle SCH_EVENTS (I_S.EVENTS like SCH_TRIGGERS)
- make additional check in case of SHOW EVENTS (check for EVENT on
the current database. if it is null check_access() gives appropriate
message back.
sql/sql_show.cc:
- add INFORMATION_SCHEMA.EVENTS and SHOW EVENTS
- I_S.EVENTS.SQL_MODE is NULL for now -> not implemented. Trudy
asked to be added so bug #16642 can be completely closed. There
is another bug report which will fix the lack of storage of
SQL_MODE during event creation.
sql/sql_yacc.yy:
- always call event_timed::init_definer() when CREATE/ALTER/DROP
EVENT but not when just compiling the body of the event because
in this case this operation is not needed, it takes memory and
CPU time and at the end the result is not used. event_timed::definer
is used only on SQLCOM_CREATE/ALTER/DROP_EVENT execution not on
statement compilation.
- add SHOW [FULL] EVENTS [FROM db] [LIKE pattern]
in case of FULL and the user has PROCESS privilege then he will see
also others' events in the current database, otherwise the output
is the same as of SHOW EVENTS. Because the events are per DB only
the events from the current database are shown. pattern is applied
against event name. FROM db is self explanatory.
sql/table.h:
add SCH_EVENTS as part of INFORMATION_SCHEMA
2006-01-30 13:15:23 +01:00
|
|
|
|
2005-12-05 11:45:04 +01:00
|
|
|
if (init_event_thread(thd))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
thd->init_for_queries();
|
|
|
|
|
2006-02-28 14:43:49 +01:00
|
|
|
/* make this thread visible it has no vio -> show processlist needs this flag */
|
2005-12-06 16:15:29 +01:00
|
|
|
thd->system_thread= 1;
|
2005-12-05 11:45:04 +01:00
|
|
|
|
|
|
|
VOID(pthread_mutex_lock(&LOCK_thread_count));
|
|
|
|
threads.append(thd);
|
|
|
|
thread_count++;
|
|
|
|
thread_running++;
|
|
|
|
VOID(pthread_mutex_unlock(&LOCK_thread_count));
|
2005-12-12 21:19:19 +01:00
|
|
|
#else
|
|
|
|
thd= current_thd;
|
|
|
|
#endif
|
2005-12-05 11:45:04 +01:00
|
|
|
|
|
|
|
{
|
|
|
|
int ret;
|
2006-02-15 21:40:38 +01:00
|
|
|
sql_print_information("SCHEDULER: Executing event %s.%s of %s [EXPR:%d]",
|
|
|
|
event->dbname.str, event->name.str,
|
|
|
|
event->definer.str, (int) event->expression);
|
2006-01-10 11:31:45 +01:00
|
|
|
|
2005-12-07 22:29:00 +01:00
|
|
|
ret= event->execute(thd, &worker_mem_root);
|
2006-01-10 11:31:45 +01:00
|
|
|
|
2006-02-15 12:14:23 +01:00
|
|
|
evex_print_warnings(thd, event);
|
2006-02-15 21:40:38 +01:00
|
|
|
sql_print_information("SCHEDULER: Executed event %s.%s of %s [EXPR:%d]. "
|
|
|
|
"RetCode=%d", event->dbname.str, event->name.str,
|
|
|
|
event->definer.str, (int) event->expression, ret);
|
2006-01-20 22:24:58 +01:00
|
|
|
if (ret == EVEX_COMPILE_ERROR)
|
2006-02-15 21:40:38 +01:00
|
|
|
sql_print_information("SCHEDULER: COMPILE ERROR for event %s.%s of",
|
|
|
|
event->dbname.str, event->name.str,
|
|
|
|
event->definer.str);
|
2006-02-20 16:06:05 +01:00
|
|
|
else if (ret == EVEX_MICROSECOND_UNSUP)
|
2006-02-24 14:08:34 +01:00
|
|
|
sql_print_information("SCHEDULER: MICROSECOND is not supported");
|
2005-12-05 11:45:04 +01:00
|
|
|
}
|
2006-02-16 00:43:11 +01:00
|
|
|
event->spawn_thread_finish(thd);
|
2005-12-28 11:07:57 +01:00
|
|
|
|
2005-12-05 11:45:04 +01:00
|
|
|
|
|
|
|
err:
|
|
|
|
VOID(pthread_mutex_lock(&LOCK_thread_count));
|
2005-12-12 21:19:19 +01:00
|
|
|
#ifndef DBUG_FAULTY_THR
|
2005-12-05 11:45:04 +01:00
|
|
|
thread_count--;
|
|
|
|
thread_running--;
|
|
|
|
/*
|
|
|
|
Some extra safety, which should not been needed (normally, event deletion
|
|
|
|
should already have done these assignments (each event which sets these
|
|
|
|
variables is supposed to set them to 0 before terminating)).
|
|
|
|
*/
|
|
|
|
VOID(pthread_mutex_unlock(&LOCK_thread_count));
|
|
|
|
|
|
|
|
thd->proc_info = "Clearing";
|
|
|
|
DBUG_ASSERT(thd->net.buff != 0);
|
|
|
|
net_end(&thd->net); // destructor will not free it, because we are weird
|
|
|
|
THD_CHECK_SENTRY(thd);
|
2006-02-28 14:43:49 +01:00
|
|
|
|
2005-12-05 11:45:04 +01:00
|
|
|
VOID(pthread_mutex_lock(&LOCK_thread_count));
|
|
|
|
THD_CHECK_SENTRY(thd);
|
|
|
|
delete thd;
|
2005-12-12 21:19:19 +01:00
|
|
|
#endif
|
2005-12-05 11:45:04 +01:00
|
|
|
VOID(pthread_mutex_unlock(&LOCK_thread_count));
|
|
|
|
|
|
|
|
err_no_thd:
|
|
|
|
|
2005-12-07 22:29:00 +01:00
|
|
|
free_root(&worker_mem_root, MYF(0));
|
2006-02-16 00:43:11 +01:00
|
|
|
thread_safe_decrement(workers_count, &LOCK_workers_count);
|
2005-12-05 11:45:04 +01:00
|
|
|
|
|
|
|
#ifndef DBUG_FAULTY_THR
|
|
|
|
my_thread_end();
|
|
|
|
pthread_exit(0);
|
|
|
|
#endif
|
|
|
|
DBUG_RETURN(0); // Can't return anything here
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-02-14 16:20:48 +01:00
|
|
|
/*
|
|
|
|
Loads all ENABLED events from mysql.event into the prioritized
|
|
|
|
queue. Called during scheduler main thread initialization. Compiles
|
|
|
|
the events. Creates event_timed instances for every ENABLED event
|
|
|
|
from mysql.event.
|
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
evex_load_events_from_db()
|
|
|
|
thd - Thread context. Used for memory allocation in some cases.
|
|
|
|
|
|
|
|
RETURNS
|
2006-02-28 14:43:49 +01:00
|
|
|
0 OK
|
|
|
|
!0 Error
|
|
|
|
|
2006-02-14 16:20:48 +01:00
|
|
|
NOTES
|
|
|
|
Reports the error to the console
|
|
|
|
*/
|
|
|
|
|
2005-12-05 11:45:04 +01:00
|
|
|
static int
|
|
|
|
evex_load_events_from_db(THD *thd)
|
|
|
|
{
|
|
|
|
TABLE *table;
|
|
|
|
READ_RECORD read_record_info;
|
|
|
|
MYSQL_LOCK *lock;
|
|
|
|
int ret= -1;
|
2005-12-15 14:12:28 +01:00
|
|
|
uint count= 0;
|
2006-02-28 14:43:49 +01:00
|
|
|
|
2005-12-05 11:45:04 +01:00
|
|
|
DBUG_ENTER("evex_load_events_from_db");
|
|
|
|
|
2005-12-16 12:42:39 +01:00
|
|
|
if ((ret= evex_open_event_table(thd, TL_READ, &table)))
|
|
|
|
{
|
2006-02-16 00:43:11 +01:00
|
|
|
sql_print_error("SCHEDULER: Table mysql.event is damaged. Can not open.");
|
2005-12-05 11:45:04 +01:00
|
|
|
DBUG_RETURN(SP_OPEN_TABLE_FAILED);
|
2005-12-16 12:42:39 +01:00
|
|
|
}
|
2005-12-05 11:45:04 +01:00
|
|
|
|
|
|
|
VOID(pthread_mutex_lock(&LOCK_event_arrays));
|
|
|
|
|
|
|
|
init_read_record(&read_record_info, thd, table ,NULL,1,0);
|
|
|
|
while (!(read_record_info.read_record(&read_record_info)))
|
|
|
|
{
|
2005-12-13 19:16:00 +01:00
|
|
|
event_timed *et;
|
2005-12-13 13:21:11 +01:00
|
|
|
if (!(et= new event_timed))
|
2005-12-05 11:45:04 +01:00
|
|
|
{
|
|
|
|
DBUG_PRINT("evex_load_events_from_db", ("Out of memory"));
|
|
|
|
ret= -1;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
DBUG_PRINT("evex_load_events_from_db", ("Loading event from row."));
|
|
|
|
|
2005-12-07 22:29:00 +01:00
|
|
|
if ((ret= et->load_from_row(&evex_mem_root, table)))
|
|
|
|
{
|
2006-02-16 00:43:11 +01:00
|
|
|
sql_print_error("SCHEDULER: Error while loading from mysql.event. "
|
2005-12-07 22:29:00 +01:00
|
|
|
"Table probably corrupted");
|
|
|
|
goto end;
|
|
|
|
}
|
2005-12-12 21:19:19 +01:00
|
|
|
if (et->status != MYSQL_EVENT_ENABLED)
|
|
|
|
{
|
2006-01-10 11:31:45 +01:00
|
|
|
DBUG_PRINT("evex_load_events_from_db",("%s is disabled",et->name.str));
|
2005-12-12 21:19:19 +01:00
|
|
|
delete et;
|
|
|
|
continue;
|
|
|
|
}
|
2006-02-28 14:43:49 +01:00
|
|
|
|
2005-12-05 11:45:04 +01:00
|
|
|
DBUG_PRINT("evex_load_events_from_db",
|
2005-12-08 00:17:05 +01:00
|
|
|
("Event %s loaded from row. Time to compile", et->name.str));
|
2005-12-05 11:45:04 +01:00
|
|
|
|
2006-02-20 16:06:05 +01:00
|
|
|
switch (ret= et->compile(thd, &evex_mem_root)) {
|
|
|
|
case EVEX_MICROSECOND_UNSUP:
|
|
|
|
sql_print_error("SCHEDULER: mysql.event is tampered. MICROSECOND is not "
|
|
|
|
"supported but found in mysql.event");
|
|
|
|
goto end;
|
|
|
|
case EVEX_COMPILE_ERROR:
|
2006-02-16 00:43:11 +01:00
|
|
|
sql_print_error("SCHEDULER: Error while compiling %s.%s. Aborting load.",
|
2005-12-08 00:17:05 +01:00
|
|
|
et->dbname.str, et->name.str);
|
2005-12-07 22:29:00 +01:00
|
|
|
goto end;
|
2006-02-20 16:06:05 +01:00
|
|
|
default:
|
|
|
|
break;
|
2005-12-07 22:29:00 +01:00
|
|
|
}
|
2006-02-28 14:43:49 +01:00
|
|
|
|
|
|
|
/* let's find when to be executed */
|
2006-01-18 20:41:22 +01:00
|
|
|
if (et->compute_next_execution_time())
|
|
|
|
{
|
2006-02-16 00:43:11 +01:00
|
|
|
sql_print_error("SCHEDULER: Error while computing execution time of %s.%s."
|
|
|
|
" Skipping", et->dbname.str, et->name.str);
|
2006-01-18 20:41:22 +01:00
|
|
|
continue;
|
|
|
|
}
|
2005-12-05 11:45:04 +01:00
|
|
|
|
2005-12-12 21:19:19 +01:00
|
|
|
DBUG_PRINT("evex_load_events_from_db", ("Adding to the exec list."));
|
2005-12-13 19:16:00 +01:00
|
|
|
|
|
|
|
evex_queue_insert(&EVEX_EQ_NAME, (EVEX_PTOQEL) et);
|
|
|
|
DBUG_PRINT("evex_load_events_from_db", ("%p %*s",
|
|
|
|
et, et->name.length,et->name.str));
|
2005-12-15 14:12:28 +01:00
|
|
|
count++;
|
2005-12-05 11:45:04 +01:00
|
|
|
}
|
|
|
|
|
2005-12-12 21:19:19 +01:00
|
|
|
ret= 0;
|
|
|
|
|
|
|
|
end:
|
2005-12-05 11:45:04 +01:00
|
|
|
VOID(pthread_mutex_unlock(&LOCK_event_arrays));
|
2005-12-12 21:19:19 +01:00
|
|
|
end_read_record(&read_record_info);
|
2006-02-28 14:43:49 +01:00
|
|
|
|
|
|
|
/* Force close to free memory */
|
|
|
|
thd->version--;
|
2005-12-05 11:45:04 +01:00
|
|
|
|
|
|
|
close_thread_tables(thd);
|
2006-02-20 16:06:05 +01:00
|
|
|
if (!ret)
|
|
|
|
sql_print_information("SCHEDULER: Loaded %d event%s", count, (count == 1)?"":"s");
|
2006-01-10 11:31:45 +01:00
|
|
|
DBUG_PRINT("info", ("Status code %d. Loaded %d event(s)", ret, count));
|
|
|
|
|
2005-12-05 11:45:04 +01:00
|
|
|
DBUG_RETURN(ret);
|
|
|
|
}
|
2005-12-06 16:15:29 +01:00
|
|
|
|
2005-12-07 19:26:44 +01:00
|
|
|
|
2006-02-14 16:20:48 +01:00
|
|
|
/*
|
|
|
|
The update method of the global variable event_scheduler.
|
|
|
|
If event_scheduler is switched from 0 to 1 then the scheduler main
|
|
|
|
thread is started.
|
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
event_executor_worker()
|
|
|
|
thd - Thread context (unused)
|
|
|
|
car - the new value
|
2006-02-28 14:43:49 +01:00
|
|
|
|
2006-02-14 16:20:48 +01:00
|
|
|
Returns
|
2006-02-28 14:43:49 +01:00
|
|
|
0 OK (always)
|
2006-02-14 16:20:48 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
bool
|
|
|
|
sys_var_event_executor::update(THD *thd, set_var *var)
|
2005-12-06 16:15:29 +01:00
|
|
|
{
|
2006-02-28 14:43:49 +01:00
|
|
|
/* here start the thread if not running. */
|
2006-01-11 18:09:05 +01:00
|
|
|
DBUG_ENTER("sys_var_event_executor::update");
|
2005-12-06 16:15:29 +01:00
|
|
|
VOID(pthread_mutex_lock(&LOCK_evex_running));
|
2005-12-13 23:10:29 +01:00
|
|
|
*value= var->save_result.ulong_value;
|
2006-01-11 18:09:05 +01:00
|
|
|
|
|
|
|
DBUG_PRINT("new_value", ("%d", *value));
|
2005-12-13 23:10:29 +01:00
|
|
|
if ((my_bool) *value && !evex_is_running)
|
2005-12-07 22:29:00 +01:00
|
|
|
{
|
2005-12-06 16:15:29 +01:00
|
|
|
VOID(pthread_mutex_unlock(&LOCK_evex_running));
|
|
|
|
init_events();
|
|
|
|
} else
|
|
|
|
VOID(pthread_mutex_unlock(&LOCK_evex_running));
|
2006-01-11 18:09:05 +01:00
|
|
|
|
|
|
|
DBUG_RETURN(0);
|
2005-12-06 16:15:29 +01:00
|
|
|
}
|
|
|
|
|
2006-02-15 12:14:23 +01:00
|
|
|
|
2006-02-10 15:02:57 +01:00
|
|
|
extern LEX_STRING warning_level_names[];
|
|
|
|
|
2006-02-14 17:51:22 +01:00
|
|
|
typedef void (*sql_print_xxx_func)(const char *format, ...);
|
|
|
|
static sql_print_xxx_func sql_print_xxx_handlers[3] =
|
|
|
|
{
|
|
|
|
sql_print_information,
|
|
|
|
sql_print_warning,
|
|
|
|
sql_print_error
|
|
|
|
};
|
|
|
|
|
2006-02-15 12:14:23 +01:00
|
|
|
|
2006-02-10 15:02:57 +01:00
|
|
|
/*
|
|
|
|
Prints the stack of infos, warnings, errors from thd to
|
|
|
|
the console so it can be fetched by the logs-into-tables and
|
|
|
|
checked later.
|
2006-02-28 14:43:49 +01:00
|
|
|
|
2006-02-10 15:02:57 +01:00
|
|
|
Synopsis
|
|
|
|
evex_print_warnings
|
|
|
|
thd - thread used during the execution of the event
|
|
|
|
et - the event itself
|
2006-02-28 14:43:49 +01:00
|
|
|
|
2006-02-10 15:02:57 +01:00
|
|
|
Returns
|
|
|
|
0 - OK (always)
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
bool
|
|
|
|
evex_print_warnings(THD *thd, event_timed *et)
|
2006-02-28 14:43:49 +01:00
|
|
|
{
|
2006-02-10 15:02:57 +01:00
|
|
|
MYSQL_ERROR *err;
|
|
|
|
DBUG_ENTER("evex_show_warnings");
|
|
|
|
char msg_buf[1024];
|
|
|
|
char prefix_buf[512];
|
|
|
|
String prefix(prefix_buf, sizeof(prefix_buf), system_charset_info);
|
|
|
|
prefix.length(0);
|
2006-02-28 14:43:49 +01:00
|
|
|
|
2006-02-10 15:02:57 +01:00
|
|
|
List_iterator_fast<MYSQL_ERROR> it(thd->warn_list);
|
|
|
|
while ((err= it++))
|
|
|
|
{
|
|
|
|
String err_msg(msg_buf, sizeof(msg_buf), system_charset_info);
|
2006-02-28 14:43:49 +01:00
|
|
|
/* set it to 0 or we start adding at the end. That's the trick ;) */
|
|
|
|
err_msg.length(0);
|
2006-02-10 15:02:57 +01:00
|
|
|
if (!prefix.length())
|
|
|
|
{
|
|
|
|
prefix.append("SCHEDULER: [");
|
|
|
|
|
|
|
|
append_identifier(thd,&prefix,et->definer_user.str,et->definer_user.length);
|
|
|
|
prefix.append('@');
|
|
|
|
append_identifier(thd,&prefix,et->definer_host.str,et->definer_host.length);
|
|
|
|
prefix.append("][", 2);
|
|
|
|
append_identifier(thd,&prefix, et->dbname.str, et->dbname.length);
|
|
|
|
prefix.append('.');
|
|
|
|
append_identifier(thd,&prefix, et->name.str, et->name.length);
|
|
|
|
prefix.append("] ", 2);
|
|
|
|
}
|
2006-02-28 14:43:49 +01:00
|
|
|
|
2006-02-10 15:02:57 +01:00
|
|
|
err_msg.append(prefix);
|
|
|
|
err_msg.append(err->msg, strlen(err->msg), system_charset_info);
|
|
|
|
err_msg.append("]");
|
2006-02-14 17:51:22 +01:00
|
|
|
DBUG_ASSERT(err->level < 3);
|
|
|
|
(sql_print_xxx_handlers[err->level])("%*s", err_msg.length(), err_msg.c_ptr());
|
2006-02-10 15:02:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DBUG_RETURN(FALSE);
|
|
|
|
}
|