{"id":10,"date":"2013-04-12T01:04:16","date_gmt":"2013-04-11T17:04:16","guid":{"rendered":"http:\/\/www.gpfeng.com\/?p=10"},"modified":"2013-08-23T22:31:49","modified_gmt":"2013-08-23T14:31:49","slug":"threadpool%e5%a4%a7%e9%87%8fkilled%e8%bf%9e%e6%8e%a5%e6%97%a0%e6%b3%95%e9%80%80%e5%87%ba","status":"publish","type":"post","link":"http:\/\/www.gpfeng.com\/?p=10","title":{"rendered":"threadpool\u5927\u91cfkilled\u8fde\u63a5\u65e0\u6cd5\u9000\u51fa"},"content":{"rendered":"<h3>\u7ebf\u4e0a\u95ee\u9898<\/h3>\n<p>\u4e3a\u4e86\u63d0\u5347\u9ad8\u5e76\u53d1\u4e0bMySQL\u8bfb\u6027\u80fd\uff0c\u53bb\u5e7411\u6708\u5e95\u6211\u4eec\u5728\u7ebf\u4e0a\u90e8\u7f72\u4e86threadpool\u7248\u672c\u7684mysql(Percona 5.5.18)\uff0cthreadpool patch\u6765\u81eamariadb 5.5.28\uff0c\u4f5c\u4e3a\u53ea\u8bfb\u5907\u5e93\u8fd0\u884c\u4e864\u4e2a\u591a\u6708\uff0c\u4e00\u5207\u90fd\u5f88\u6b63\u5e38\uff0c\u4e0a\u5468\u6709DBA\u5728\u201cshow processlist;\u201d\u65f6\u53d1\u73b0\u5b58\u5728\u5927\u91cf\u5904\u4e8ekilled\u7684\u8fde\u63a5\u5b58\u5728\uff0c\u800c\u4e14killed\u72b6\u6001\u6301\u7eed\u7684\u65f6\u95f4\u5f88\u957f\u3002\u5173\u4e8ethreadpool\u7ebf\u7a0b\u8c03\u5ea6\uff1a<a href=\"http:\/\/blog.chinaunix.net\/uid-28364803-id-3431242.html\">mariadb 5.5 threadpool<\/a>, <a href=\"http:\/\/blog.chinaunix.net\/uid-28364803-id-3427833.html\">mysql thread sheduler<\/a><!--more--><\/p>\n<pre lang=\"mysql\">mysql> show processlist;\r\n+----+------+-----------+------+---------+------+-------+------------------+-----------+---------------+-----------+\r\n| Id | User | Host      | db   | Command | Time | State | Info             | Rows_sent | Rows_examined | Rows_read |\r\n+----+------+-----------+------+---------+------+-------+------------------+-----------+---------------+-----------+\r\n|  3 | root | localhost | NULL | Query   |    0 | NULL  | show processlist |         0 |             0 |         1 |\r\n|  7 | root | localhost | NULL | Killed  | 2304 |       | NULL             |         0 |             0 |         1 |\r\n| 15 | root | localhost | NULL | Killed  | 2285 |       | NULL             |         0 |             0 |         1 |\r\n| 25 | root | localhost | NULL | Killed  | 2258 |       | NULL             |         0 |             0 |         1 |\r\n| 32 | root | localhost | NULL | Killed  | 2240 |       | NULL             |         0 |             0 |         1 |\r\n| 34 | root | localhost | NULL | Killed  | 2232 |       | NULL             |         0 |             0 |         1 |\r\n| 37 | root | localhost | NULL | Killed  | 2221 |       | NULL             |         0 |             0 |         1 |\r\n| 46 | root | localhost | NULL | Killed  | 2191 |       | NULL             |         0 |             0 |         1 |\r\n| 54 | root | localhost | NULL | Killed  | 2178 |       | NULL             |         0 |             0 |         1 |\r\n| 55 | root | localhost | NULL | Killed  | 2172 |       | NULL             |         0 |             0 |         1 |\r\n| 58 | root | localhost | NULL | Killed  | 2165 |       | NULL             |         0 |             0 |         1 |\r\n| 59 | root | localhost | NULL | Killed  | 2159 |       | NULL             |         0 |             0 |         1 |\r\n| 67 | root | localhost | NULL | Killed  | 2145 |       | NULL             |         0 |             0 |         1 |\r\n| 70 | root | localhost | NULL | Killed  | 2136 |       | NULL             |         0 |             0 |         1 |\r\n| 73 | root | localhost | NULL | Killed  | 2126 |       | NULL             |         0 |             0 |         1 |\r\n| 78 | root | localhost | NULL | Killed  | 2114 |       | NULL             |         0 |             0 |         1 |\r\n| 81 | root | localhost | NULL | Sleep   | 2104 |       | NULL             |         0 |             0 |         1 |\r\n+----+------+-----------+------+---------+------+-------+------------------+-----------+---------------+-----------+\r\n17 rows in set (0.00 sec)<\/pre>\n<h3>\u5206\u6790\u8fc7\u7a0b<\/h3>\n<p>\u8fd9\u662f\u4e00\u4e2a\u770b\u8d77\u6765\u6709\u4e9b\u602a\u5f02\u7684\u95ee\u9898\uff0c\u901a\u8fc7\u4ee3\u7801\u67e5\u770b\u201cKILL connection_id\u201d\u7684\u6267\u884c\u6d41\u7a0b\uff1a<\/p>\n<pre lang=\"c\" line=\"1\">sql_kill\r\n  ->THD.awake\r\n    ->thd.killed=KILL_CONNECTION\r\n    ->close_active_vio\r\n      ->vio_close\r\n        ->mysql_socket_shutdown(vio->sd,\u00a0SHUT_RDWR)\r\n        ->closesocket(vio->sd)\r\n    ->post_kill_notification<\/pre>\n<p>\u6a21\u62df\u5927\u91cf\u5e76\u53d1\u957f\u8fde\u63a5\u7684\u573a\u666f\uff0c\u767b\u9646mysql\u6267\u884cKILL\uff0c\u8d77\u521d\u5f88\u96be\u91cd\u73b0\u95ee\u9898\uff0c\u56e0\u4e3a\u4ee3\u7801\u4e2d\u53ef\u4ee5\u770b\u5230SQL\u6267\u884c\u8fc7\u7a0b\u4e2d\u6709\u5f88\u591a\u7684if(thd.killed ==KILL_CONNECTION)\u5224\u65ad\uff0c\u4e00\u65e6\u8fde\u63a5\u5bf9\u5e94\u7684THD\u72b6\u6001\u88ab\u7f6e\u4e3aKILL_CONNECTION\uff0c\u7ebf\u7a0b\u5728\u6267\u884c\u8fc7\u7a0b\u4e2d\u603b\u6709\u4e00\u5904\u4f1a\u68c0\u6d4b\u5230\uff0c\u63a5\u7740\u4f1arollback\/abort\u76f8\u5e94\u7684\u4e8b\u52a1\uff0c\u8fde\u63a5\u4e5f\u4f1a\u987a\u5229\u9000\u51fa<\/p>\n<p>\u91cd\u73b0\u73b0\u573a\u56f0\u96be\u662f\u56e0\u4e3a\u6f0f\u6389\u4e86\u4e00\u79cd\u60c5\u51b5\uff01\u90a3\u5c31\u662f\u5f53\u8fde\u63a5\u5904\u4e8eSleep\u72b6\u6001\uff0c\u6b64\u65f6kill\u540e\uff0c\u201cshow processlist;\u201d\u4f1a\u53d1\u73b0\u5176\u72b6\u6001\u4e3akilled\uff0c\u4f46\u662f\u4f1a\u4e00\u76f4\u5b58\u5728<\/p>\n<p>\u5bf9\u4e8eone thread per connection\u7ebf\u7a0b\u8c03\u5ea6\u7b56\u7565\uff0c\u5f53\u6ca1\u6709\u6570\u636e\u53ef\u8bfb\u65f6\u6267\u884c\u7ebf\u7a0b\u4f1a\u5835\u585e\u5728do_command\u4e2d\u7684my_net_read\u8fd9\u91cc\uff0c\u5f53\u6b64\u8fde\u63a5\u88abKILL\u65f6\uff0c\u5176\u72b6\u6001\u88ab\u4fee\u6539\u6210\u4e86KILL_CONNECTION\uff0cmysql_socket_shutdown\u4e4b\u540e\u4e4b\u524d\u5835\u585e\u7684my_net_read\u4f1a\u8fd4\u56de\u5f02\u5e38\uff0c\u63a5\u7740do_command\u5916\u5c42\u7684\u5faa\u73af\u4f1a\u5bf9thd.killed\u72b6\u6001\u505a\u5224\u65ad\uff0c\u4e4b\u540e\u8fde\u63a5\u5c31\u53ef\u4ee5\u6b63\u5e38\u9000\u51fa\u4e86\uff1b\u7136\u800c\uff0c<strong>\u5bf9\u4e8ethreadpool\u8c03\u5ea6\u7b56\u7565\uff0c\u5f53\u8fde\u63a5\u4e2d\u6ca1\u6709\u6570\u636e\u53ef\u8bfb\u65f6\uff0c\u5176\u5bf9\u5e94\u7684sockfd\u4f1a\u88ab\u6dfb\u52a0\u5230epoll_wait\u4e2d\u7b49\u5f85\u4e8b\u4ef6\u53d1\u751f\uff0c\u800c\u76f8\u5e94\u7684worker\u7ebf\u7a0b\u4f1a\u53bb\u5904\u7406\u5176\u5b83\u8fde\u63a5\u6216\u8005\u8fdb\u5165condition wait\u72b6\u6001<\/strong>\uff0c\u8fd9\u79cd\u8c03\u5ea6\u65b9\u5f0f\u5bf9\u4e8e\u5927\u91cf\u6267\u884c\u65f6\u95f4\u8f83\u77ed\u7684\u8bfb\u4e8b\u52a1\u80fd\u5927\u5927\u63d0\u9ad8\u6027\u80fd\uff0c\u5f0a\u7aef\u5c31\u662f\uff1a\u5728\u5927\u91cf\u5b58\u5728\u51b2\u7a81\u7684\u8bfb\u5199\u4e8b\u52a1\u5e76\u53d1\u6267\u884c\u7684\u573a\u666f\u4e0b\uff0c\u5982\u679cworker\u7ebf\u7a0b\u6570\u76ee\u53d7\u5230\u9650\u5236\u65f6\u53ef\u80fd\u4f1a\u51fa\u73b0\u8c03\u5ea6\u4e0a\u7684\u6b7b\u9501<\/p>\n<p>\u7ee7\u7eed\u5206\u6790\u95ee\u9898\u539f\u56e0\uff0c\u201cshow processlist;\u201d\u7684\u7ed3\u679c\u8868\u660e\u76ee\u6807\u8fde\u63a5\u7684\u72b6\u6001\u5df2\u7ecf\u88ab\u7f6e\u4e3aKILL_CONNECTION\u4e86\uff0c\u4f46\u662f\u8fde\u63a5\u5374\u4e00\u76f4\u65e0\u6cd5\u9000\u51fa\uff0c\u6000\u7591\u8fde\u63a5\u5176\u5b9e\u4e00\u76f4\u90fd\u5904\u4e8eSleep\u72b6\u6001\uff0c\u53ef\u80fd\u7684\u89e3\u91ca\u662fepoll_wait\u76d1\u542c\u4e0d\u5230socket\u65ad\u5f00\u7684\u4e8b\u4ef6\uff0c\u4f46\u662f\u4e8b\u5b9e\u4e0aepoll_wait\u662f\u53ef\u4ee5\u76d1\u542c\u5230shutdown\u7684<\/p>\n<p>\u627e\u5230\u91cd\u73b0\u95ee\u9898\u7684\u65b9\u6cd5\u540e\uff0c\u60f3\u770b\u770b\u6700\u65b0\u7684mariadb\u662f\u5426\u4f1a\u6709\u540c\u6837\u7684\u95ee\u9898\uff0c\u5982\u679c\u4e5f\u5b58\u5728\u5c31\u53ef\u4ee5\u63d0\u4ea4bug\u6c42\u52a9\u793e\u533a\uff0c\u7136\u800cmariadb 5.5.30\u4e0a\u8fd9\u4e2a\u95ee\u9898\u5e76\u6ca1\u6709\u91cd\u65b0\uff0c\u53ea\u80fd\u7528\u6700\u571f\u7684\u529e\u6cd5\u4e86\uff0c\u5bf9\u6bd4\u4e24\u4efd\u4ee3\u7801\uff0cdiff\u6587\u4ef6\u6570\u4e07\u591a\u884c\uff0c\u770b\u6765mariadb\u5728mysql\u4e0a\u786e\u5b9e\u505a\u4e86\u5927\u91cf\u5de5\u4f5c\uff0c\u805a\u7126threadpool\u548csql_kill\u76f8\u5173\u7684\u6587\u4ef6\u4e0a\uff0c\u53d1\u73b0\u4e86\u4e00\u4e2a\u7591\u70b9\uff0cTHD.awake\u51fd\u6570\u6709\u4e9b\u4e0d\u540c\uff0cmariadb 5.5.30\u91cc\u7684\u903b\u8f91\u662f\uff1a<\/p>\n<pre lang=\"c\" line=\"1\">sql_kill\r\n  ->THD.awake\r\n    ->thd.killed=KILL_CONNECTION\r\n    ->vio_shutdown(active_vio, SHUT_RDWR)\r\n      ->vio_socket_shutdown(vio->sd,\u00a0SHUT_RDWR)\r\n    ->post_kill_notification<\/pre>\n<p>\u53d1\u73b0\u53ea\u662fshutdown\uff0c\u5e76\u6ca1\u6709close\uff0c\u5f53\u65f6\u6253threadpool\u7684patch\u662f\u57fa\u4e8emariadb 5.5.28\u7248\u672c\uff0c\u5bf9\u6bd4\u53d1\u73b0\u548c5.5.30\u4e00\u81f4\uff0c\u6000\u7591\u95ee\u9898\u5c31\u5904\u5728\u8fd9\uff0c\u7528gdb\u5728vio_close\u7684shutdown\u548cclose\u4e4b\u95f4\u4e0b\u65ad\u70b9\uff0c\u5f53\u6267\u884c\u5230shutdown\u4e4b\u540e\uff08close\u8fd8\u672a\u6267\u884c\uff09epoll_wait\u6210\u529f\u76d1\u6d4b\u5230\u4e86\u4e8b\u4ef6\uff0c\u8fde\u63a5\u987a\u5229\u9000\u51fa\uff01\u901a\u8fc7google\u5f97\u77e5\uff1aclose\u51fd\u6570\u4f1a\u4f7fepoll_wait\u4e2d\u7684\u76f8\u5173sockfd\u88ab\u6ce8\u9500<\/p>\n<h3>\u539f\u56e0\u603b\u7ed3<\/h3>\n<p>\u7531\u4e8eclose\u7d27\u63a5\u7740shutdown\u6267\u884c\uff0c\u5f02\u6b65\u7684epoll_wait\u5728\u68c0\u6d4b\u5230shutdown\u4e8b\u4ef6\u8fd4\u56de\u524d,sockfd\u5df2\u7ecf\u4ece\u76d1\u542c\u6587\u4ef6\u5217\u8868\u88ab\u79fb\u9664\u4e86\uff01<\/p>\n<p>\u56e0\u800cpatch\u4e5f\u975e\u5e38\u7b80\u5355\uff1a\u5ef6\u8fdfclose sockfd\uff0c\u5728THD.awake\u4e2d\u53ea\u6267\u884cshutdown\uff0c\u53ea\u8981\u80fd\u4eceepoll_wait\u8fd4\u56de\uff0c\u5c31\u80fd\u88abworker\u7ebf\u7a0b\u5904\u7406\uff0c\u5230\u4e86\u5224\u65adthd.killed\u65f6\uff0cclose_connection\u51fd\u6570\u6700\u7ec8\u4f1a\u88ab\u8c03\u7528,\u5176\u4e2d\u4f1aclose sockfd<\/p>\n<pre lang=\"c\" line=\"1\">worker_main\r\n  ->handle_event\r\n    ->connection_abort\r\n      ->threadpool_remove_connection\r\n        ->close_connection\r\n          ->thd->disconnect\r\n            ->thd->close_active_vio<\/pre>\n<h3>\u53c2\u8003<\/h3>\n<p><a href=\"http:\/\/bazaar.launchpad.net\/~maria-captains\/maria\/5.5\/revision\/3282#sql\/sql_class.cc\">mariadb launchpad<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7ebf\u4e0a\u95ee\u9898 \u4e3a\u4e86\u63d0\u5347\u9ad8\u5e76\u53d1\u4e0bMySQL\u8bfb\u6027\u80fd\uff0c\u53bb\u5e7411\u6708\u5e95\u6211\u4eec\u5728\u7ebf\u4e0a\u90e8\u7f72\u4e86threadpool\u7248\u672c\u7684mysql( &hellip; <a href=\"http:\/\/www.gpfeng.com\/?p=10\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">threadpool\u5927\u91cfkilled\u8fde\u63a5\u65e0\u6cd5\u9000\u51fa<\/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":false,"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-a","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"http:\/\/www.gpfeng.com\/index.php?rest_route=\/wp\/v2\/posts\/10"}],"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=10"}],"version-history":[{"count":38,"href":"http:\/\/www.gpfeng.com\/index.php?rest_route=\/wp\/v2\/posts\/10\/revisions"}],"predecessor-version":[{"id":385,"href":"http:\/\/www.gpfeng.com\/index.php?rest_route=\/wp\/v2\/posts\/10\/revisions\/385"}],"wp:attachment":[{"href":"http:\/\/www.gpfeng.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.gpfeng.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.gpfeng.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}