{"id":365,"date":"2013-08-23T17:59:53","date_gmt":"2013-08-23T09:59:53","guid":{"rendered":"http:\/\/www.gpfeng.com\/?p=365"},"modified":"2013-08-23T23:17:59","modified_gmt":"2013-08-23T15:17:59","slug":"mysql-statement-timeout%e7%89%b9%e6%80%a7","status":"publish","type":"post","link":"http:\/\/www.gpfeng.com\/?p=365","title":{"rendered":"MySQL statement timeout\u7279\u6027"},"content":{"rendered":"<p>\u8fd9\u4e2a\u7279\u6027\u662ftwitter\u5f00\u53d1\u7684\uff0c<a href=\"https:\/\/github.com\/twitter\/mysql\/wiki\/Statement-Timeout\">\u70b9\u51fb\u67e5\u770b\u8be6\u60c5<\/a>\uff0c\u76ee\u524d\u5df2\u7ecfbackport\u5230AliMySQL\u4e2d\uff0c\u6b63\u5728\u6d4b\u8bd5\u4e2d\uff0c\u9996\u5148\u7b80\u5355\u4ecb\u7ecd\u4e00\u4e0b\u529f\u80fd<\/p>\n<h3>\u7279\u6027\u63cf\u8ff0\uff1a<\/h3>\n<p>1\uff09statement timemout\u7279\u6027\u4e2d\uff0cSELECT\u5fc5\u987b\u662ftop-level\uff0c\u4e0d\u80fd\u662f\u5b50\u67e5\u8be2\u6216\u8005union\u4e2d\u7684\u9664\u7b2c\u4e00\u4e2aSELECT\u4e4b\u5916\u7684SELECT<br \/>\n2\uff09\u8bed\u53e5\u7ea7\u522b\uff1a\u4e3aSELECT\u589e\u52a0\u4e00\u4e2ahint: max_statement_time\u6307\u5b9a\u67e5\u8be2\u6700\u5927\u6267\u884c\u65f6\u95f4\uff0c\u5f53\u6267\u884c\u65f6\u95f4\u8d85\u8fc7max_statement_time\u540e\uff0c\u67e5\u8be2\u81ea\u52a8\u4e2d\u65ad\uff08\u8fde\u63a5\u4e0d\u4e2d\u65ad\uff09<br \/>\n3\uff09session\u7ea7\u522b\uff1a\u5141\u8bb8\u8d85\u7ea7\u7528\u6237grant\u5176\u5b83\u7528\u6237\u4e00\u4e2a\u6307\u5b9a\u7684max_statement_time\uff0c\u8be5\u7528\u6237\u8fde\u63a5\u540e\u9ed8\u8ba4\u4f7f\u7528\u8be5\u503c\uff08\u7528\u6237\u53ef\u5728session\u4e2d\u4e34\u65f6\u4fee\u6539\uff09\uff0c\u6bcf\u4e2aSELECT\u6267\u884c\u65f6\u95f4\u90fd\u4e0d\u4f1a\u8d85\u8fc7max_statement_time<br \/>\n4\uff09\u6ca1\u6709grant max_statement_time\u6216grant max_statement_time 0\u7684\u7528\u6237\uff0c\u9664\u975e\u5728query\u4e2d\u6307\u5b9amax_statement_time hint\uff0c\u5426\u5219\u67e5\u8be2\u6267\u884c\u65f6\u95f4\u4e0d\u53d7\u9650\u5236<br \/>\n<!--more--><\/p>\n<h3>\u8fd0\u884c\u6548\u679c<\/h3>\n<pre lang=\"mysql\">mysql> select @@max_statement_time;\r\n+----------------------+\r\n| @@max_statement_time |\r\n+----------------------+\r\n|                 5000 |\r\n+----------------------+\r\n1 row in set (0.00 sec)\r\n\r\nmysql> select * from load_test_infile, t2;\r\nERROR 1722 (70101): Query execution was interrupted, max_statement_time exceeded\r\n\r\nmysql> select MAX_STATEMENT_TIME=1000 * from load_test_infile, t2;\r\nERROR 1722 (70101): Query execution was interrupted, max_statement_time exceeded\r\n\r\nmysql> grant select, insert, update, select, create on *.* to test@'%' with max_statement_time 10000;      \r\nQuery OK, 0 rows affected (0.00 sec)\r\n\r\nmysql> flush privileges;\r\nQuery OK, 0 rows affected (0.00 sec)\r\n\r\nmysql> select user, max_statement_time from mysql.user where user='test';\r\n+------+--------------------+\r\n| user | max_statement_time |\r\n+------+--------------------+\r\n| test |              10000 |\r\n+------+--------------------+\r\n1 row in set (0.00 sec)\r\n\r\nmysql> show status like '%statement_time%';\r\n+-----------------------------+-------+\r\n| Variable_name               | Value |\r\n+-----------------------------+-------+\r\n| Max_statement_time_exceeded | 2     |\r\n| Max_statement_time_failed   | 0     |\r\n| Max_statement_time_set      | 2     |\r\n+-----------------------------+-------+\r\n3 rows in set (0.00 sec)<\/pre>\n<h3>\u5b9e\u73b0\u539f\u7406\uff1a<\/h3>\n<p>\u539f\u7406\u6bd4\u8f83\u7b80\u5355\uff0c\u4f46\u5b9e\u73b0\u7ec6\u8282\u8fd8\u662f\u6bd4\u8f83\u590d\u6742\uff0c\u7b80\u5355\u6982\u62ec\uff1a<br \/>\n1\uff09\u4e3a\u9700\u8981\u4f7f\u7528statement timeout\u7684\u8fde\u63a5\u521b\u5efa\u4e00\u4e2atimer\uff0c\u6267\u884cSELECT\u4e4b\u524d\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\uff0ctimer\u8d85\u65f6\u540e\u53d1\u9001MY_TIMER_EVENT_SIGNO\u4fe1<br \/>\n\u53f7\uff0c\u67e5\u8be2\u7ed3\u675f\u540e\u6267\u884ctimer_reset<br \/>\n2\uff09\u589e\u52a0\u4e00\u4e2a\u540e\u53f0\u7ebf\u7a0b\uff0c\u63a5\u6536\u6765\u81ea\u6240\u6709\u8fde\u63a5tiemr\u7684MY_TIMER_EVENT_SIGNO\u4fe1\u53f7\uff0c\u8c03\u7528THD::awake(THD::KILL_TIMEOUT)\uff0c\u7ed3\u675f\u67e5\u8be2<br \/>\n3\uff09\u5728mysql.user\u8868\u4e2d\u589e\u52a0\u4e00\u5217\uff1amax_statement_time\u4fdd\u5b58grant max_statement_time\u7684\u503c\uff0c\u7528\u6237\u767b\u5f55\u540e\u521d\u59cb\u5316\u4e3a\u8be5\u503c<\/p>\n<h3>\u4e3b\u8981\u4ee3\u7801\u5206\u6790\uff1a<\/h3>\n<p>\u4e3b\u8981\u6570\u636e\u7ed3\u6784st_thd_timer\u548cst_my_timer\uff0c\u524d\u8005\u662f\u5bf9\u540e\u8005\u7684\u5c01\u88c5\u548c\u4fdd\u62a4\uff0c\u5176\u4e2d\u7684THD\u662ftimer\u8d85\u65f6\u9700\u8981kill\u7684session<\/p>\n<pre lang=\"c\">struct st_thd_timer\r\n{\r\n  THD *thd;\r\n  my_timer_t timer;\r\n  pthread_mutex_t mutex;\r\n  bool destroy;\r\n};\r\n\r\ntypedef struct st_my_timer my_timer_t;\r\n\r\n\/** Non-copyable timer object. *\/\r\nstruct st_my_timer\r\n{\r\n  \/** Timer ID used to identify the timer in timer requests. *\/\r\n  os_timer_t id; \r\n\r\n  \/** Timer expiration notification function. *\/\r\n  void (*notify_function)(my_timer_t *); \r\n};<\/pre>\n<p>\u4e3a\u8fde\u63a5\u521b\u5efa\u4e00\u4e2athd_timer\uff0c\u91cc\u9762\u5c01\u88c5\u4e86\u4e00\u4e2aos_timer_t\u6210\u5458\uff0c\u8fd9\u4e2a\u662f\u64cd\u4f5c\u7cfb\u7edf\u63d0\u4f9b\u7684timer\uff0c\u6b64\u51fd\u6570\u88abthd_timer_set\u8c03\u7528\uff0c\u4e00\u53e5\u91cd\u8981\u4ee3\u7801\uff1a<br \/>\nttp->timer.notify_function= timer_callback<\/p>\n<pre lang=\"c\">\/**\r\n  Allocate and initialize a thread timer object.\r\n\r\n  @return NULL on failure.\r\n*\/\r\nstatic thd_timer_t *thd_timer_create(void)\r\n{\r\n  thd_timer_t *ttp;\r\n  DBUG_ENTER(\"thd_timer_create\");\r\n\r\n  ttp= (thd_timer_t *) my_malloc(sizeof(*ttp), MYF(MY_WME | MY_ZEROFILL));\r\n\r\n  if (ttp == NULL)\r\n    DBUG_RETURN(NULL);\r\n\r\n  ttp->timer.notify_function= timer_callback;\r\n  pthread_mutex_init(&ttp->mutex, MY_MUTEX_INIT_FAST);\r\n\r\n  if (! my_timer_create(&ttp->timer))\r\n    DBUG_RETURN(ttp);\r\n\r\n  pthread_mutex_destroy(&ttp->mutex);\r\n  my_free(ttp);\r\n\r\n  DBUG_RETURN(NULL);\r\n}<\/pre>\n<p>timer_callback\u5b9e\u73b0\uff1amy_container_of\u662f\u4e00\u4e2a\u5b8f\uff0c\u901a\u8fc7\u4e00\u4e2a\u7ed3\u6784\u4f53\u4e2d\u67d0\u4e2a\u6210\u5458\u7684\u5730\u5740\u627e\u5230\u8be5\u7ed3\u6784\u4f53\u7684\u8d77\u59cb\u5730\u5740\uff0c\u6b64\u5904\u901a\u8fc7thd_timer_t\u4e2d\u7684timer\u5730\u5740\u627e\u5230thd_timer_t\u7684\u5730\u5740<\/p>\n<pre lang=\"c\">\/**\r\n  Timer expiration notification callback.\r\n\r\n  @param  timer   Timer (mysys) object.\r\n\r\n  @note Invoked in a separate thread of control.\r\n*\/\r\nstatic void timer_callback(my_timer_t *timer)\r\n{\r\n  bool destroy;\r\n  thd_timer_t *ttp;\r\n\r\n  ttp= my_container_of(timer, thd_timer_t, timer);\r\n\r\n  pthread_mutex_lock(&ttp->mutex);\r\n  destroy= timer_notify(ttp);\r\n  pthread_mutex_unlock(&ttp->mutex);\r\n\r\n  if (destroy)\r\n    thd_timer_destroy(ttp);\r\n}<\/pre>\n<p>timer_notify\u51fd\u6570\u8d1f\u8d23\u8c03\u7528THD::awake\u51fd\u6570kill query<\/p>\n<pre lang=\"c\">static void timer_notify(thd_timer_t *ttp)\r\n{\r\n  THD *thd= ttp->thd;\r\n  DBUG_ASSERT(!ttp->destroy || !thd);\r\n\r\n  \/*\r\n    Statement might have finished while the timer notification\r\n    was being delivered. If this is the case, the timer object\r\n    was detached (orphaned) and has no associated session (thd).\r\n  *\/\r\n  if (thd)\r\n  {\r\n    mysql_mutex_lock(&thd->LOCK_thd_data);\r\n    thd->awake(THD::KILL_TIMEOUT);\r\n    mysql_mutex_unlock(&thd->LOCK_thd_data);\r\n  }\r\n\r\n  \/* Mark the object as unreachable. *\/\r\n  ttp->thd= NULL;\r\n  return ttp->destroy;\r\n}<\/pre>\n<p>\u8c03\u7528\u64cd\u4f5c\u7cfb\u7edfAPI\u521b\u5efatimer\uff0c\u6b64\u51fd\u6570\u88abthd_timer_create\u8c03\u7528\uff0c\u8bbe\u7f6etimer\u8d85\u65f6\u65f6\u53d1\u9001MY_TIMER_EVENT_SIGNO\u4fe1\u53f7\uff0c\u540c\u65f6\u901a\u8fc7sigev.sigev_value.sival_ptr= timer\u4fdd\u5b58\u4e86\u6307\u5411my_timer_t(\u5176\u4e2d\u51fd\u6570\u6307\u9488notify_function\u5728thd_timer_create\u4e2d\u88ab\u7f6e\u4e3atimer_callback)\u7684\u6307\u9488\uff0c<\/p>\n<pre lang=\"c\">\/**\r\n  Create a timer object.\r\n\r\n  @param  timer   Location where the timer ID is returned.\r\n\r\n  @return On success, 0.\r\n          On error, -1 is returned, and errno is set to indicate the error.\r\n*\/\r\nint my_timer_create(my_timer_t *timer)\r\n{\r\n  struct sigevent sigev;\r\n\r\n  memset(&sigev, 0, sizeof(sigev));\r\n\r\n  sigev.sigev_value.sival_ptr= timer;\r\n  sigev.sigev_signo= MY_TIMER_EVENT_SIGNO;\r\n  sigev.sigev_notify= SIGEV_SIGNAL | SIGEV_THREAD_ID;\r\n  sigev.sigev_notify_thread_id= thread_id;\r\n\r\n  return timer_create(CLOCK_MONOTONIC, &sigev, &timer->id);\r\n}<\/pre>\n<p>\u540e\u53f0kill\u7ebf\u7a0b\u5b9e\u73b0\uff0cmy_timer_init_ext\u51fd\u6570\u4f1a\u5728server startup\u65f6\u8c03\u7528\uff0c\u521b\u5efa\u4e00\u4e2ahelper_thread\uff0c\u6267\u884c\u4e00\u4e2awhile\u5faa\u73af\uff08\u5f53\u6536\u5230MY_TIMER_EVENT_SIGNO\u4fe1\u53f7\u65f6\uff0c\u6267\u884c\u56de\u8c03\u51fd\u6570timer_callback\uff09<\/p>\n<pre lang=\"c\">int my_timer_init_ext(void)\r\n{\r\n  int rc;\r\n  sigset_t set, old_set;\r\n\r\n  if (sigfillset(&set))\r\n    return -1;\r\n\r\n  \/*\r\n    Temporarily block all signals. New thread will inherit signal\r\n    mask of the current thread.\r\n  *\/\r\n  if (pthread_sigmask(SIG_BLOCK, &set, &old_set))\r\n    return -1;\r\n\r\n  \/* Create a helper thread. *\/\r\n  rc= start_helper_thread();\r\n\r\n  \/* Restore the signal mask. *\/\r\n  pthread_sigmask(SIG_SETMASK, &old_set, NULL);\r\n\r\n  return rc;\r\n}\r\n\r\n\/**\r\n  Create a helper thread to dispatch timer expiration notifications.\r\n\r\n  @return On success, 0. On error, -1 is returned.\r\n*\/\r\nstatic int start_helper_thread(void)\r\n{\r\n  pthread_barrier_t barrier;\r\n\r\n  if (pthread_barrier_init(&barrier, NULL, 2))\r\n    return -1;\r\n\r\n  if (pthread_create(&thread, NULL, timer_notify_thread, &barrier))\r\n    return -1;\r\n\r\n  pthread_barrier_wait(&barrier);\r\n  pthread_barrier_destroy(&barrier);\r\n\r\n  return 0;\r\n}\r\n\r\n\/**\r\n  Timer expiration notification thread.\r\n\r\n  @param  arg   Barrier object.\r\n*\/\r\nstatic void *timer_notify_thread(void *arg)\r\n{\r\n  sigset_t set;\r\n  siginfo_t info;\r\n  pthread_barrier_t *barrier= arg;\r\n\r\n  my_thread_init();\r\n\r\n  sigemptyset(&set);\r\n  sigaddset(&set, MY_TIMER_EVENT_SIGNO);\r\n  sigaddset(&set, MY_TIMER_KILL_SIGNO);\r\n\r\n  \/* Get the thread ID of the current thread. *\/\r\n  thread_id= (pid_t) syscall(SYS_gettid);\r\n\r\n  \/* Wake up parent thread, thread_id is available. *\/\r\n  pthread_barrier_wait(barrier);\r\n\r\n  while (1)\r\n  {\r\n    if (sigwaitinfo(&set, &info) &lt; 0)\r\n      continue;\r\n\r\n    if (info.si_signo == MY_TIMER_EVENT_SIGNO)\r\n      timer_notify_function(info.si_value);\r\n    else if (info.si_signo == MY_TIMER_KILL_SIGNO)\r\n      break;\r\n  }\r\n  my_thread_end();\r\n  return NULL;\r\n}\r\n\r\nstatic void timer_notify_function(sigval_t sigev_value)\r\n{\r\n  my_timer_t *timer= sigev_value.sival_ptr;\r\n  timer->notify_function(timer);\r\n}<\/pre>\n<p>timer_notify_thread\u4e2dsigwaitinfo\u6536\u5230\u6d88\u606f\u7684\u540c\u65f6\uff0cinfo.si_value\u4f1a\u88ab\u7f6e\u4e3asigev.sigev_value.sival_ptr\uff08my_timer_create\u4e2d\uff09\uff0c\u5b83\u6307\u5411\u4e00\u4e2amy_timer_t\uff0c\u901a\u8fc7\u8c03\u7528\u5176\u4e2d\u7684notify_function\uff08timer_callback\uff0c\u5728thd_timer_create\u8bbe\u7f6e\uff09\uff0c\u8fbe\u5230kill query\u7684\u6548\u679c<\/p>\n<h3>\u6574\u4e2a\u6d41\u7a0b\uff1a<\/h3>\n<p>1\uff09server startup: \u8c03\u7528my_timer_init_ext\u542f\u52a8\u540e\u53f0\u76d1\u542c\u7ebf\u7a0b<br \/>\n2\uff09SELECT\u6267\u884c\u8fc7\u7a0b\u4e2d\uff0c\u82e5max_statement_time\u4e0d\u4e3a0\uff0c\u5728mysql_execute_command\u5f00\u59cb\u4e4b\u524d\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\u5e76\u542f\u52a8timer\uff0c\u5728mysql_execute_command\u7ed3\u675f\u4e4b\u524d\u6e05\u9664timer\uff0c\u82e5SELECT\u7531\u4e8e\u8d85\u65f6\u88abkill\u4f1a\u5f88\u4f1a\u6267\u884c\u5230\u8fd9\u91cc<\/p>\n<h3>\u7ed3\u675f\u8bed<\/h3>\n<p>\u5206\u6790\u5230\u6b64\u7ed3\u675f\uff0c\u82e5\u60f3\u8fdb\u4e00\u6b65\u4e86\u89e3\u5b9e\u73b0\u8be6\u60c5\uff0c\u8bf7\u53c2\u8003\uff1a<br \/>\n1\uff09<a href=\"https:\/\/github.com\/twitter\/mysql\/wiki\/Statement-Timeout\">twitter statement timeout github<\/a><br \/>\n2\uff09<a href=\"https:\/\/blueprints.launchpad.net\/percona-server\/+spec\/port-max-statement-time\">percona 5.6 statement timeout blueprint<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u4e2a\u7279\u6027\u662ftwitter\u5f00\u53d1\u7684\uff0c\u70b9\u51fb\u67e5\u770b\u8be6\u60c5\uff0c\u76ee\u524d\u5df2\u7ecfbackport\u5230AliMySQL\u4e2d\uff0c\u6b63\u5728\u6d4b\u8bd5\u4e2d\uff0c\u9996\u5148\u7b80 &hellip; <a href=\"http:\/\/www.gpfeng.com\/?p=365\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">MySQL statement timeout\u7279\u6027<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[1],"tags":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3tPZp-5T","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"http:\/\/www.gpfeng.com\/index.php?rest_route=\/wp\/v2\/posts\/365"}],"collection":[{"href":"http:\/\/www.gpfeng.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.gpfeng.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.gpfeng.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.gpfeng.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=365"}],"version-history":[{"count":18,"href":"http:\/\/www.gpfeng.com\/index.php?rest_route=\/wp\/v2\/posts\/365\/revisions"}],"predecessor-version":[{"id":374,"href":"http:\/\/www.gpfeng.com\/index.php?rest_route=\/wp\/v2\/posts\/365\/revisions\/374"}],"wp:attachment":[{"href":"http:\/\/www.gpfeng.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=365"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.gpfeng.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=365"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.gpfeng.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=365"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}