Skip to content

Commit

Permalink
MYSQL-15: Implement server-side statement timeout support
Browse files Browse the repository at this point in the history
Set an appropriate error status when a statement is killed. The
error status to be set varies depending on the killed state.
  • Loading branch information
Davi Arnaut committed Mar 6, 2012
1 parent 2bb4adb commit d17ccb9
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 3 deletions.
7 changes: 7 additions & 0 deletions include/mysql/plugin.h
Expand Up @@ -583,6 +583,13 @@ MYSQL_THD thd_get_current_thd();
int thd_killed(const MYSQL_THD thd);


/**
Set the killed status of the current statement.
@param thd user thread connection handle
*/
void thd_set_kill_status(const MYSQL_THD thd);

/**
Return the thread id of a user thread
Expand Down
1 change: 1 addition & 0 deletions include/mysql/plugin_audit.h.pp
Expand Up @@ -189,6 +189,7 @@
int mysql_tmpfile(const char *prefix);
void* thd_get_current_thd();
int thd_killed(const void* thd);
void thd_set_kill_status(const void* thd);
unsigned long thd_get_thread_id(const void* thd);
void thd_get_xid(const void* thd, MYSQL_XID *xid);
void mysql_query_cache_invalidate4(void* thd,
Expand Down
1 change: 1 addition & 0 deletions include/mysql/plugin_auth.h.pp
Expand Up @@ -189,6 +189,7 @@
int mysql_tmpfile(const char *prefix);
void* thd_get_current_thd();
int thd_killed(const void* thd);
void thd_set_kill_status(const void* thd);
unsigned long thd_get_thread_id(const void* thd);
void thd_get_xid(const void* thd, MYSQL_XID *xid);
void mysql_query_cache_invalidate4(void* thd,
Expand Down
1 change: 1 addition & 0 deletions include/mysql/plugin_ftparser.h.pp
Expand Up @@ -142,6 +142,7 @@
int mysql_tmpfile(const char *prefix);
void* thd_get_current_thd();
int thd_killed(const void* thd);
void thd_set_kill_status(const void* thd);
unsigned long thd_get_thread_id(const void* thd);
void thd_get_xid(const void* thd, MYSQL_XID *xid);
void mysql_query_cache_invalidate4(void* thd,
Expand Down
14 changes: 14 additions & 0 deletions mysql-test/r/max_statement_time.result
Expand Up @@ -110,3 +110,17 @@ WHERE VARIABLE_NAME = 'MAX_STATEMENT_TIME_EXCEEDED'
AND CONVERT(VARIABLE_VALUE, UNSIGNED) > @time_exceeded;
STATUS
1

# Check that the appropriate error status is set.

CREATE TABLE t1 (a INT) ENGINE=InnoDB;
INSERT INTO t1 VALUES (1);
START TRANSACTION;
SELECT * FROM t1 FOR UPDATE;
a
1
SET @@SESSION.max_statement_time = 500;
UPDATE t1 SET a = 2;
ERROR 70100: Query execution was interrupted, max_statement_time exceeded
ROLLBACK;
DROP TABLE t1;
22 changes: 21 additions & 1 deletion mysql-test/t/max_statement_time.test
Expand Up @@ -4,6 +4,7 @@

--source include/have_statement_timeout.inc
--source include/not_embedded.inc
--source include/have_innodb.inc

--echo
--echo # Test MAX_STATEMENT_TIME option syntax.
Expand Down Expand Up @@ -79,7 +80,6 @@ DROP TABLE t1;
--echo # Test the MAX_STATEMENT_TIME option with SF.
--echo


CREATE TABLE t1 (a INT, b VARCHAR(300));

INSERT INTO t1 VALUES (1, 'string');
Expand Down Expand Up @@ -159,3 +159,23 @@ SELECT 1 AS STATUS FROM INFORMATION_SCHEMA.GLOBAL_STATUS
SELECT 1 AS STATUS FROM INFORMATION_SCHEMA.GLOBAL_STATUS
WHERE VARIABLE_NAME = 'MAX_STATEMENT_TIME_EXCEEDED'
AND CONVERT(VARIABLE_VALUE, UNSIGNED) > @time_exceeded;

--echo
--echo # Check that the appropriate error status is set.
--echo

CREATE TABLE t1 (a INT) ENGINE=InnoDB;
INSERT INTO t1 VALUES (1);

START TRANSACTION;
SELECT * FROM t1 FOR UPDATE;

connect (con1,localhost,root,,test,,);
SET @@SESSION.max_statement_time = 500;
--error ER_QUERY_TIMEOUT
UPDATE t1 SET a = 2;
disconnect con1;

connection default;
ROLLBACK;
DROP TABLE t1;
10 changes: 10 additions & 0 deletions sql/sql_class.cc
Expand Up @@ -3490,6 +3490,16 @@ extern "C" int thd_killed(const MYSQL_THD thd)
return(thd->killed);
}

/**
Set the killed status of the current statement.
@param thd user thread connection handle
*/
extern "C" void thd_set_kill_status(const MYSQL_THD thd)
{
thd->send_kill_message();
}

/**
Return the thread id of a user thread
@param thd user thread
Expand Down
4 changes: 2 additions & 2 deletions storage/innobase/handler/ha_innodb.cc
Expand Up @@ -950,7 +950,7 @@ convert_error_code_to_mysql(
return(0);

case DB_INTERRUPTED:
my_error(ER_QUERY_INTERRUPTED, MYF(0));
thd_set_kill_status(thd ? thd : thd_get_current_thd());
/* fall through */

case DB_FOREIGN_EXCEED_MAX_CASCADE:
Expand Down Expand Up @@ -8687,7 +8687,7 @@ ha_innobase::check(

prebuilt->trx->op_info = "";
if (thd_killed(user_thd)) {
my_error(ER_QUERY_INTERRUPTED, MYF(0));
thd_set_kill_status(user_thd);
}

DBUG_RETURN(is_ok ? HA_ADMIN_OK : HA_ADMIN_CORRUPT);
Expand Down

0 comments on commit d17ccb9

Please sign in to comment.