From 0e5193435ae01ae9323448aa32570a50b23c8658 Mon Sep 17 00:00:00 2001 From: Sergey Petrunya Date: Wed, 8 Aug 2012 21:24:00 +0400 Subject: [PATCH] MWL#182: Explain running statements: Address feedback: - Use LEX::value_list instead of LEX::show_explain_for_thread - Factor out common code into find_thread_by_id(ulong id) --- sql/sql_lex.h | 1 - sql/sql_parse.cc | 46 +++++++++++++++++++++++++++++++--------------- sql/sql_show.cc | 21 ++------------------- sql/sql_show.h | 1 + sql/sql_yacc.yy | 5 +++-- 5 files changed, 37 insertions(+), 37 deletions(-) diff --git a/sql/sql_lex.h b/sql/sql_lex.h index eac41a9f0c0..54f8740e210 100644 --- a/sql/sql_lex.h +++ b/sql/sql_lex.h @@ -2360,7 +2360,6 @@ struct LEX: public Query_tables_list char* to_log; /* For PURGE MASTER LOGS TO */ char* x509_subject,*x509_issuer,*ssl_cipher; String *wild; /* Wildcard in SHOW {something} LIKE 'wild'*/ - Item *show_explain_for_thread; /* id in SHOW EXPLAIN FOR id */ sql_exchange *exchange; select_result *result; Item *default_value, *on_update_value; diff --git a/sql/sql_parse.cc b/sql/sql_parse.cc index 73dbe328761..16f195c44cd 100644 --- a/sql/sql_parse.cc +++ b/sql/sql_parse.cc @@ -2165,7 +2165,7 @@ mysql_execute_command(THD *thd) goto error; } - Item **it= &(lex->show_explain_for_thread); + Item **it= lex->value_list.head_ref(); if ((!(*it)->fixed && (*it)->fix_fields(lex->thd, it)) || (*it)->check_cols(1)) { @@ -6585,6 +6585,35 @@ void add_join_natural(TABLE_LIST *a, TABLE_LIST *b, List *using_fields, } +/** + Find a thread by id and return it, locking it LOCK_thd_data + + @param id Identifier of the thread we're looking for + + @return NULL - not found + pointer - thread found, and its LOCK_thd_data is locked. +*/ + +THD *find_thread_by_id(ulong id) +{ + THD *tmp; + mysql_mutex_lock(&LOCK_thread_count); // For unlink from list + I_List_iterator it(threads); + while ((tmp=it++)) + { + if (tmp->command == COM_DAEMON) + continue; + if (tmp->thread_id == id) + { + mysql_mutex_lock(&tmp->LOCK_thd_data); // Lock from delete + break; + } + } + mysql_mutex_unlock(&LOCK_thread_count); + return tmp; +} + + /** kill on thread. @@ -6603,20 +6632,7 @@ uint kill_one_thread(THD *thd, ulong id, killed_state kill_signal) DBUG_ENTER("kill_one_thread"); DBUG_PRINT("enter", ("id: %lu signal: %u", id, (uint) kill_signal)); - mysql_mutex_lock(&LOCK_thread_count); // For unlink from list - I_List_iterator it(threads); - while ((tmp=it++)) - { - if (tmp->command == COM_DAEMON) - continue; - if (tmp->thread_id == id) - { - mysql_mutex_lock(&tmp->LOCK_thd_data); // Lock from delete - break; - } - } - mysql_mutex_unlock(&LOCK_thread_count); - if (tmp) + if ((tmp= find_thread_by_id(id))) { /* If we're SUPER, we can KILL anything, including system-threads. diff --git a/sql/sql_show.cc b/sql/sql_show.cc index b08d49b3f28..167e099baf9 100644 --- a/sql/sql_show.cc +++ b/sql/sql_show.cc @@ -2056,28 +2056,11 @@ int fill_show_explain(THD *thd, TABLE_LIST *table, COND *cond) DBUG_ENTER("fill_show_explain"); DBUG_ASSERT(cond==NULL); - thread_id= thd->lex->show_explain_for_thread->val_int(); + thread_id= thd->lex->value_list.head()->val_int(); calling_user= (thd->security_ctx->master_access & PROCESS_ACL) ? NullS : thd->security_ctx->priv_user; - /* - Find the thread we need EXPLAIN for. Thread search code was copied from - kill_one_thread() - */ - mysql_mutex_lock(&LOCK_thread_count); // For unlink from list - I_List_iterator it(threads); - while ((tmp=it++)) - { - if (tmp->command == COM_DAEMON) - continue; - if (tmp->thread_id == thread_id) - { - mysql_mutex_lock(&tmp->LOCK_thd_data); // Lock from delete - break; - } - } - mysql_mutex_unlock(&LOCK_thread_count); - if (tmp) + if ((tmp= find_thread_by_id(thread_id))) { Security_context *tmp_sctx= tmp->security_ctx; /* diff --git a/sql/sql_show.h b/sql/sql_show.h index 6ccb6e03872..12da9713e21 100644 --- a/sql/sql_show.h +++ b/sql/sql_show.h @@ -132,6 +132,7 @@ enum enum_schema_tables get_schema_table_idx(ST_SCHEMA_TABLE *schema_table); /* These functions were under INNODB_COMPATIBILITY_HOOKS */ int get_quote_char_for_identifier(THD *thd, const char *name, uint length); +THD *find_thread_by_id(ulong id); class select_result_explain_buffer; /* diff --git a/sql/sql_yacc.yy b/sql/sql_yacc.yy index 50d87497d8c..0d9cb9622f1 100644 --- a/sql/sql_yacc.yy +++ b/sql/sql_yacc.yy @@ -11618,10 +11618,11 @@ show_param: } | describe_command FOR_SYM expr { + THD *thd= YYTHD; Lex->sql_command= SQLCOM_SHOW_EXPLAIN; - if (prepare_schema_table(YYTHD, Lex, 0, SCH_EXPLAIN)) + if (prepare_schema_table(thd, Lex, 0, SCH_EXPLAIN)) MYSQL_YYABORT; - Lex->show_explain_for_thread= $3; + add_value_to_list(thd, $3); } ;