Bug #44517 MySQL Event scheduler runs the same event even if its previous state is running
Submitted: 28 Apr 2009 14:05 Modified: 30 Apr 2009 16:34
Reporter: Vinod Sugur Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server: Documentation Severity:S2 (Serious)
Version:5.1.33-log MySQL Community Server (GPL) OS:Solaris (64 bit)
Assigned to: Paul DuBois CPU Architecture:Any
Tags: event scheduler

[28 Apr 2009 14:05] Vinod Sugur
Description:
Suppose that event X is scheduled every 1 second, but X is slow, sometimes it takes more than one second to execute. The event scheduler won't detect such cases and starts execution of event X even if its earlier instance is in running state.

Output of show processlist command:

mysql> show processlist;
+-----+-----------------+-------------------+------+---------+------+-----------------------------+------------------+
| Id  | User            | Host              | db   | Command | Time | State                       | Info             |
+-----+-----------------+-------------------+------+---------+------+-----------------------------+------------------+
| 326 | root            | localhost         | test   | Query   |    0 | NULL                        | show processlist | 
| 327 | event_scheduler | localhost         | NULL | Daemon  |    0 | Waiting for next activation | NULL             | 
+-----+-----------------+-------------------+------+---------+------+-----------------------------+------------------+
4 rows in set (0.00 sec)

mysql> 
mysql> show processlist;
+-----+-----------------+-------------------+------+---------+------+-----------------------------+------------------+
| Id  | User            | Host              | db   | Command | Time | State                       | Info             |
+-----+-----------------+-------------------+------+---------+------+-----------------------------+------------------+
| 326 | root            | localhost         | test | Query   |    0 | NULL                        | show processlist | 
| 327 | event_scheduler | localhost         | NULL | Daemon  |    1 | Waiting for next activation | NULL             | 
+-----+-----------------+-------------------+------+---------+------+-----------------------------+------------------+
4 rows in set (0.00 sec)

mysql> show processlist;
+-----+-----------------+-------------------+------+---------+------+-----------------------------+-------------------+
| Id  | User            | Host              | db   | Command | Time | State                       | Info              |
+-----+-----------------+-------------------+------+---------+------+-----------------------------+-------------------+
| 326 | root            | localhost         | test | Query   |    0 | NULL                        | show processlist  | 
| 327 | event_scheduler | localhost         | NULL | Daemon  |    1 | Waiting for next activation | NULL              | 
| 427 | qa              | %                 | test | Connect |   10 | Copying to tmp table        | INSERT INTO Temp  | 
| 428 | qa              | %                 | test | Connect |   10 | Copying to tmp table        | INSERT INTO Temp  | 
| 429 | qa              | %                 | test | Connect |   10 | Copying to tmp table        | INSERT INTO Temp  | 
| 430 | qa              | %                 | test | Connect |    9 | Copying to tmp table        | INSERT INTO Temp  | 
| 431 | qa              | %                 | test | Connect |    8 | Copying to tmp table        | INSERT INTO Temp  | 
| 432 | qa              | %                 | test | Connect |    7 | Copying to tmp table        | INSERT INTO Temp  | 
| 433 | qa              | %                 | test | Connect |    6 | Copying to tmp table        | INSERT INTO Temp  | 
| 434 | qa              | %                 | test | Connect |    5 | Copying to tmp table        | INSERT INTO Temp  | 

How to repeat:
Please execute the code below to repeat this issue:

======================================================================
create table temp
(id int,
name varchar(10))engine = innodb;

Insert into temp
values(rand() * 1000, rand() * 1000000000);

Insert into temp
    select rand() * 10, rand() * 10000000000 from temp;

Insert into temp
    select rand() * 10, rand() * 10000000000 from temp;

Insert into temp
 select rand() * 10, rand() * 10000000000 from temp;

Insert into temp
    select rand() * 10, rand() * 10000000000 from temp;

Insert into temp
    select rand() * 10, rand() * 10000000000 from temp;

Insert into temp
select rand() * 10, rand() * 10000000000 from temp;

Insert into temp
    select rand() * 10, rand() * 10000000000 from temp;

Insert into temp
select rand() * 10, rand() * 10000000000 from temp;

Insert into temp
select rand() * 10, rand() * 10000000000 from temp;

Insert into temp
select rand() * 10, rand() * 10000000000 from temp;

DELIMITER $$

/*
This procedure is only for re-producing the above mentioned scenario 
*/
create procedure sp_temp()
BEGIN
INSERT INTO TEMP
SELECT t.id, t.name
from temp t, temp t1, temp t2;
END $$

DELIMITER ;

DELIMITER $$

DROP EVENT IF EXISTS `ev_test`;
     CREATE EVENT `ev_test`
     ON SCHEDULE EVERY 1 SECOND
     STARTS cast(current_date() as DATETIME) 
     ENDS '2037-01-09 03:14:07'
     ENABLE
     COMMENT 'NULL'
    DO CALL test.sp_temp() $$

DELIMITER ;

set global event_scheduler=1;

======================================================================

Suggested fix:
As per documentation this scenario will not occur:

url :http://dev.mysql.com/tech-resources/articles/mysql-events.html#12
==========================================
What if the event is already running?

Suppose that event X is scheduled every 1 second, but X is slow, sometimes it takes more than one second to execute. The event scheduler will detect such cases. It will skip X and try again a second later. I have found that, even on a system with a fairly light load, even if X does little more than insert into a table, a few 'misses' of 1-second events can occur each day.
==========================================

This scenario should not occur.
[28 Apr 2009 20:23] Sergei Golubchik
as far as I remember the original plan was to not skip events in a case like that. An article is not a documentation, not an authoritative source. I haven't found the description of a correct behavior in the manual.

The manual (and perhaps the code too) should be fixed.
[29 Apr 2009 6:02] Sveta Smirnova
Thank you for the report.

I'd say this is not a bug, because user manual does not promise such behavior. But as we have this article published on our website it is worth adding words about correct behavior in the user manual.
[30 Apr 2009 16:34] Paul DuBois
Thank you for your bug report. This issue has been addressed in the documentation. The updated documentation will appear on our website shortly, and will be included in the next release of the relevant products.

Added a note to:
http://dev.mysql.com/doc/refman/5.1/en/create-event.html
http://dev.mysql.com/doc/refman/5.1/en/events-overview.html

"If a repeating event does not terminate within its scheduling 
interval, the result may be multiple instances of the event executing
simultaneously."