mirror of
https://github.com/MariaDB/server.git
synced 2025-08-01 03:47:19 +03:00
Added new options to KILL. New syntax is KILL [HARD|SOFT] [CONNECTION|QUERY] [ID | USER user_name]
- If USER is given, all threads for that user is signaled - If SOFT is used then the KILL will not be sent to the handler. This can be used to not interrupt critical things in the handler like 'REPAIR'. Internally added more kill signals. This gives us more information of why a query/connection was killed. - KILL_SERVER is used when server is going down. In this case the users gets ER_SHUTDOWN as the reason connection was killed. - Changed signals to number in correct order, which makes it easier to test how the signal should affect the code. - New error message ER_CONNECTION_KILLED if connection was killed by 'KILL CONNECTION'. Before we got error ER_SHUTDOWN. Changed names of not used parameters KILL_QUERY & KILL_CONNCTION to mysql_kill() to not conflict with defines in the server include/mysql.h.pp: Updated file include/mysql_com.h: Changed names of not used parameters KILL_QUERY & KILL_CONNCTION to mysql_kill() to not conflict with defines in the server mysql-test/r/kill.result: Added test of KILL USER mysql-test/suite/rpl/r/rpl_stm_000001.result: Updated error code mysql-test/suite/rpl/t/rpl_stm_000001.test: Updated error codes mysql-test/t/flush_read_lock_kill.test: Updated error codes mysql-test/t/kill.test: Added test of KILL USER plugin/handler_socket/handlersocket/database.cpp: Removed THD:: from KILL sql/debug_sync.cc: Removed THD:: from KILL sql/event_scheduler.cc: Removed THD:: from KILL sql/filesort.cc: Removed THD:: from KILL sql/ha_ndbcluster_binlog.cc: Removed THD:: from KILL sql/handler.cc: Removed THD:: from KILL Simplify code. sql/lex.h: Added new keywords HARD | SOFT sql/log.cc: Removed THD:: from KILL Added testing of new error ER_CONNECTION_KILLED sql/log_event.cc: Removed THD:: from KILL Added testing of new error ER_CONNECTION_KILLED sql/mysql_priv.h: Added new prototypes sql/mysqld.cc: Removed THD:: from KILL Use KILL_SERVER_HARD signal on shutdown. sql/scheduler.cc: Removed THD:: from KILL Simplify test if connection should be killed sql/share/errmsg.txt: New error message ER_CONNECTION_KILLED sql/slave.cc: Removed THD:: from KILL sql/sp_head.cc: Removed THD:: from KILL sql/sql_base.cc: Removed THD:: from KILL sql/sql_cache.cc: Removed THD:: from KILL sql/sql_class.cc: Removed THD:: from KILL Added killed_errno() Only signal kill to storage engine if HARD bit is set. sql/sql_class.h: Move KILL options out from THD to make them easier to use in sql_yacc.yy sql/sql_connect.cc: Removed THD:: from KILL sql/sql_delete.cc: Removed THD:: from KILL sql/sql_error.cc: Removed THD:: from KILL sql/sql_insert.cc: Removed THD:: from KILL Simplifed testing if thread is killed. sql/sql_lex.h: Added kill options to st_lex sql/sql_load.cc: Removed THD:: from KILL sql/sql_parse.cc: Added kill options to st_lex Simplifed and optimzed testing of thd->killed at end of query Added support for KILL USER Extended sql_kill() to allow use of more kill signals. sql/sql_repl.cc: Removed THD:: from KILL sql/sql_show.cc: Removed THD:: from KILL Simplied testing if query/connection was killed sql/sql_table.cc: Removed THD:: from KILL sql/sql_update.cc: Removed THD:: from KILL sql/sql_yacc.yy: Added support for new KILL syntax: KILL [HARD|SOFT] [CONNECTION|QUERY] [ID | USER user_name] storage/archive/ha_archive.cc: Simplify compilation storage/maria/ha_maria.cc: Removed THD:: from KILL
This commit is contained in:
140
sql/sql_parse.cc
140
sql/sql_parse.cc
@ -793,7 +793,7 @@ int end_trans(THD *thd, enum enum_mysql_completiontype completion)
|
||||
my_error(thd->killed_errno(), MYF(0));
|
||||
else if ((res == 0) && do_release)
|
||||
{
|
||||
thd->killed= THD::KILL_CONNECTION;
|
||||
thd->killed= KILL_CONNECTION;
|
||||
if (global_system_variables.log_warnings > 3)
|
||||
{
|
||||
Security_context *sctx= &thd->main_security_ctx;
|
||||
@ -1530,7 +1530,7 @@ bool dispatch_command(enum enum_server_command command, THD *thd,
|
||||
{
|
||||
status_var_increment(thd->status_var.com_stat[SQLCOM_KILL]);
|
||||
ulong id=(ulong) uint4korr(packet);
|
||||
sql_kill(thd,id,false);
|
||||
sql_kill(thd,id, KILL_CONNECTION_HARD);
|
||||
break;
|
||||
}
|
||||
case COM_SET_OPTION:
|
||||
@ -1572,15 +1572,18 @@ bool dispatch_command(enum enum_server_command command, THD *thd,
|
||||
}
|
||||
|
||||
/* report error issued during command execution */
|
||||
if (thd->killed_errno())
|
||||
if (thd->killed)
|
||||
{
|
||||
if (! thd->main_da.is_set())
|
||||
thd->send_kill_message();
|
||||
}
|
||||
if (thd->killed == THD::KILL_QUERY || thd->killed == THD::KILL_BAD_DATA)
|
||||
{
|
||||
thd->killed= THD::NOT_KILLED;
|
||||
thd->mysys_var->abort= 0;
|
||||
if (thd->killed_errno())
|
||||
{
|
||||
if (! thd->main_da.is_set())
|
||||
thd->send_kill_message();
|
||||
}
|
||||
if (thd->killed < KILL_CONNECTION)
|
||||
{
|
||||
thd->killed= NOT_KILLED;
|
||||
thd->mysys_var->abort= 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* If commit fails, we should be able to reset the OK status. */
|
||||
@ -4038,8 +4041,6 @@ end_with_restore_list:
|
||||
}
|
||||
case SQLCOM_KILL:
|
||||
{
|
||||
Item *it= (Item *)lex->value_list.head();
|
||||
|
||||
if (lex->table_or_sp_used())
|
||||
{
|
||||
my_error(ER_NOT_SUPPORTED_YET, MYF(0), "Usage of subqueries or stored "
|
||||
@ -4047,13 +4048,20 @@ end_with_restore_list:
|
||||
break;
|
||||
}
|
||||
|
||||
if ((!it->fixed && it->fix_fields(lex->thd, &it)) || it->check_cols(1))
|
||||
if (lex->kill_type == KILL_TYPE_ID)
|
||||
{
|
||||
my_message(ER_SET_CONSTANTS_ONLY, ER(ER_SET_CONSTANTS_ONLY),
|
||||
MYF(0));
|
||||
goto error;
|
||||
Item *it= (Item *)lex->value_list.head();
|
||||
if ((!it->fixed && it->fix_fields(lex->thd, &it)) || it->check_cols(1))
|
||||
{
|
||||
my_message(ER_SET_CONSTANTS_ONLY, ER(ER_SET_CONSTANTS_ONLY),
|
||||
MYF(0));
|
||||
goto error;
|
||||
}
|
||||
sql_kill(thd, (ulong) it->val_int(), lex->kill_signal);
|
||||
}
|
||||
sql_kill(thd, (ulong)it->val_int(), lex->type & ONLY_KILL_QUERY);
|
||||
else
|
||||
sql_kill_user(thd, get_current_user(thd, lex->users_list.head()),
|
||||
lex->kill_signal);
|
||||
break;
|
||||
}
|
||||
#ifndef NO_EMBEDDED_ACCESS_CHECKS
|
||||
@ -7174,12 +7182,13 @@ bool reload_acl_and_cache(THD *thd, ulong options, TABLE_LIST *tables,
|
||||
This is written such that we have a short lock on LOCK_thread_count
|
||||
*/
|
||||
|
||||
uint kill_one_thread(THD *thd, ulong id, bool only_kill_query)
|
||||
uint kill_one_thread(THD *thd, ulong id, killed_state kill_signal)
|
||||
{
|
||||
THD *tmp;
|
||||
uint error=ER_NO_SUCH_THREAD;
|
||||
DBUG_ENTER("kill_one_thread");
|
||||
DBUG_PRINT("enter", ("id=%lu only_kill=%d", id, only_kill_query));
|
||||
DBUG_PRINT("enter", ("id: %lu signal: %u", id, (uint) kill_signal));
|
||||
|
||||
VOID(pthread_mutex_lock(&LOCK_thread_count)); // For unlink from list
|
||||
I_List_iterator<THD> it(threads);
|
||||
while ((tmp=it++))
|
||||
@ -7219,7 +7228,7 @@ uint kill_one_thread(THD *thd, ulong id, bool only_kill_query)
|
||||
if ((thd->security_ctx->master_access & SUPER_ACL) ||
|
||||
thd->security_ctx->user_matches(tmp->security_ctx))
|
||||
{
|
||||
tmp->awake(only_kill_query ? THD::KILL_QUERY : THD::KILL_CONNECTION);
|
||||
tmp->awake(kill_signal);
|
||||
error=0;
|
||||
}
|
||||
else
|
||||
@ -7231,6 +7240,76 @@ uint kill_one_thread(THD *thd, ulong id, bool only_kill_query)
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
kill all threads from one user
|
||||
|
||||
@param thd Thread class
|
||||
@param user_name User name for threads we should kill
|
||||
@param only_kill_query Should it kill the query or the connection
|
||||
|
||||
@note
|
||||
This is written such that we have a short lock on LOCK_thread_count
|
||||
|
||||
If we can't kill all threads because of security issues, no threads
|
||||
are killed.
|
||||
*/
|
||||
|
||||
static uint kill_threads_for_user(THD *thd, LEX_USER *user,
|
||||
killed_state kill_signal, ha_rows *rows)
|
||||
{
|
||||
THD *tmp;
|
||||
List<THD> threads_to_kill;
|
||||
DBUG_ENTER("kill_threads_for_user");
|
||||
|
||||
*rows= 0;
|
||||
|
||||
if (thd->is_fatal_error) // If we run out of memory
|
||||
DBUG_RETURN(ER_OUT_OF_RESOURCES);
|
||||
|
||||
DBUG_PRINT("enter", ("user: %s signal: %u", user->user.str,
|
||||
(uint) kill_signal));
|
||||
|
||||
VOID(pthread_mutex_lock(&LOCK_thread_count)); // For unlink from list
|
||||
I_List_iterator<THD> it(threads);
|
||||
while ((tmp=it++))
|
||||
{
|
||||
if (tmp->command == COM_DAEMON)
|
||||
continue;
|
||||
/*
|
||||
Check that hostname (if given) and user name matches.
|
||||
|
||||
host.str[0] == '%' means that host name was not given. See sql_yacc.yy
|
||||
*/
|
||||
if (((user->host.str[0] == '%' && !user->host.str[1]) ||
|
||||
!strcmp(tmp->security_ctx->host, user->host.str)) &&
|
||||
!strcmp(tmp->security_ctx->user, user->user.str))
|
||||
{
|
||||
if (!(thd->security_ctx->master_access & SUPER_ACL) &&
|
||||
!thd->security_ctx->user_matches(tmp->security_ctx))
|
||||
{
|
||||
VOID(pthread_mutex_unlock(&LOCK_thread_count));
|
||||
DBUG_RETURN(ER_KILL_DENIED_ERROR);
|
||||
}
|
||||
if (!threads_to_kill.push_back(tmp, tmp->mem_root))
|
||||
pthread_mutex_lock(&tmp->LOCK_thd_data); // Lock from delete
|
||||
}
|
||||
}
|
||||
VOID(pthread_mutex_unlock(&LOCK_thread_count));
|
||||
if (!threads_to_kill.is_empty())
|
||||
{
|
||||
List_iterator_fast<THD> it(threads_to_kill);
|
||||
THD *ptr;
|
||||
while ((ptr= it++))
|
||||
{
|
||||
ptr->awake(kill_signal);
|
||||
pthread_mutex_unlock(&ptr->LOCK_thd_data);
|
||||
(*rows)++;
|
||||
}
|
||||
}
|
||||
DBUG_RETURN(0);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
kills a thread and sends response
|
||||
|
||||
@ -7241,16 +7320,33 @@ uint kill_one_thread(THD *thd, ulong id, bool only_kill_query)
|
||||
only_kill_query Should it kill the query or the connection
|
||||
*/
|
||||
|
||||
void sql_kill(THD *thd, ulong id, bool only_kill_query)
|
||||
void sql_kill(THD *thd, ulong id, killed_state state)
|
||||
{
|
||||
uint error;
|
||||
if (!(error= kill_one_thread(thd, id, only_kill_query)))
|
||||
if (!(error= kill_one_thread(thd, id, state)))
|
||||
my_ok(thd);
|
||||
else
|
||||
my_error(error, MYF(0), id);
|
||||
}
|
||||
|
||||
|
||||
void sql_kill_user(THD *thd, LEX_USER *user, killed_state state)
|
||||
{
|
||||
uint error;
|
||||
ha_rows rows;
|
||||
if (!(error= kill_threads_for_user(thd, user, state, &rows)))
|
||||
my_ok(thd, rows);
|
||||
else
|
||||
{
|
||||
/*
|
||||
This is probably ER_OUT_OF_RESOURCES, but in the future we may
|
||||
want to write the name of the user we tried to kill
|
||||
*/
|
||||
my_error(error, MYF(0), user->host.str, user->user.str);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/** If pointer is not a null pointer, append filename to it. */
|
||||
|
||||
bool append_file_to_dir(THD *thd, const char **filename_ptr,
|
||||
|
Reference in New Issue
Block a user