Skip to content

Commit

Permalink
sql: Avoid time running backwards
Browse files Browse the repository at this point in the history
The value of NOW() and friends is fixed during the execution
of a statement. When other threads update the group record and
increase group_mtime, the current thread might see a group_mtime
greater than its NOW().

The calculations sometimes produced negative delta values and
overflowed when added to a UNSIGNED type.

Make sure we never decrease group_mtime.
  • Loading branch information
donald committed May 10, 2017
1 parent 7276703 commit a054505
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion mysql/create_trigger.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ CREATE TRIGGER mxq_add_group BEFORE INSERT ON mxq_group
DROP TRIGGER IF EXISTS mxq_update_group|
CREATE TRIGGER mxq_update_group BEFORE UPDATE ON mxq_group
FOR EACH ROW BEGIN
SET NEW.group_mtime = NOW();
SET NEW.group_mtime = GREATEST(OLD.group_mtime, NOW());

IF OLD.group_jobs_inq > 0 AND OLD.group_jobs_running = 0 THEN
SET NEW.stats_wait_sec = OLD.stats_wait_sec + (UNIX_TIMESTAMP(NEW.group_mtime) - UNIX_TIMESTAMP(OLD.group_mtime));
Expand Down

0 comments on commit a054505

Please sign in to comment.