關於MYSQL 5.7 新連線建立流程原始碼介面(和5.6不同)

gaopengtttt發表於2017-05-29
原創 水平有限

今天在看運維內參第三章原始碼的時候,發現書上很多程式碼入口函式和我用的5.7.14的原始碼對不上,然後再看了一下5.6的原始碼,
書上寫的基本都是5.6的原始碼(書是2015年開始寫的)5.7.14在連線這一塊做了相當大的改動,加了很多抽象類,變得更加複雜了。
當然這也是軟體模組劃分的更加清晰的必然的結果,順便說一下MYSQL在UNIX上使用的select/poll 這種多路I/O轉接模型來監聽
客戶端的連線,監聽的只是讀操作,當客戶端發起連線的時候select/poll透過監聽LISTEN scoket上的是否有讀取資訊來判斷,是否
有新的連線到來,然後呼叫執行緒回撥函式建立新的執行緒來處理這個新的accpet的socket通道(還會判斷是否有空閒的執行緒佇列而不需要
新建立新的執行緒來處理),執行緒的建立我們說是比程式代價小很多,他沒有獨立的記憶體三區(共享區,程式碼段和堆記憶體),但是他也有PCB
有獨立的棧空間所以能節約一點是一點吧。
關於MYSQL的原始碼各種多型太多了,真是眼花繚亂

下面是我使用到一些斷點。

(gdb) info b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x0000000000ebd333 in main(int, char**) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/main.cc:25
        breakpoint already hit 1 time
2       breakpoint     keep y   0x00000000016e350d in inline_mysql_socket_accept(char const*, uint, PSI_socket_key, MYSQL_SOCKET, sockaddr*, socklen_t*) at /root/mysql5.7.14/percona-server-5.7.14-7/include/mysql/psi/mysql_socket.h:1110
        breakpoint already hit 18 times
6       breakpoint     keep y   0x0000000000f1b8d9 in Connection_handler_manager::process_new_connection(Channel_info*) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/conn_handler/connection_handler_manager.cc:289
        breakpoint already hit 15 times
8       breakpoint     keep y   0x00000000016e197f in handle_connection(void*) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/conn_handler/connection_handler_per_thread.cc:239
        breakpoint already hit 6 times
9      breakpoint     keep y   0x0000000000f1b1d5 in Connection_handler_manager::check_and_incr_conn_count(bool) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/conn_handler/connection_handler_manager.cc:105
        breakpoint already hit 1 time
12      breakpoint     keep y   0x00000000016e1f41 in Per_thread_connection_handler::add_connection(Channel_info*) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/conn_handler/connection_handler_per_thread.cc:399


下面按照呼叫流程解釋一下這幾個函式作用,希望幫到在5.7.14下使用GDB除錯MYSQL連線的同學一點幫助
1、不解釋主函式main

2、 inline_mysql_socket_accept
 當然也就是sokcet accept返回新的socket檔案描述符的函式,因為找不到多路I/O轉接 select和poll的入口用了這招去找
  找到多路I/O轉接介面變為了Channel_info* Mysqld_socket_listener::listen_for_connection_event()
  Channel_info就是一個抽象類,這個函式會返回連線socket的相關資訊給 Channel_info這個基類指標
  又有多太發生了擦。
  
這裡貼出一點和運維內參同樣的程式碼,但是入口函式全部變了,而且除了邏輯相似,很多實現都變了
大量的用多型

點選(此處)摺疊或開啟

  1. #ifdef HAVE_POLL //多路I/O轉接判斷是否有寫連線到來
  2.   int retval= poll(&m_poll_info.m_fds[0], m_socket_map.size(), -1);
  3. #else
  4.   m_select_info.m_read_fds= m_select_info.m_client_fds;
  5.   int retval= select((int) m_select_info.m_max_used_connection,
  6.                      &m_select_info.m_read_fds, 0, 0, 0);
  7. #endif


點選(此處)摺疊或開啟

  1. #ifdef HAVE_POLL //多路I/O轉接判斷是否有寫連線到來
  2.   int retval= poll(&m_poll_info.m_fds[0], m_socket_map.size(), -1);
  3. #else
  4.   m_select_info.m_read_fds= m_select_info.m_client_fds;
  5.   int retval= select((int) m_select_info.m_max_used_connection,
  6.                      &m_select_info.m_read_fds, 0, 0, 0);
  7. #endif

點選(此處)摺疊或開啟

  1. for (uint retry= 0; retry < MAX_ACCEPT_RETRY; retry++) //重試次數MAX_ACCEPT_RETRY好像是10
  2.   {
  3.     socket_len_t length= sizeof(struct sockaddr_storage);
  4.     connect_sock= mysql_socket_accept(key_socket_client_connection, listen_sock,
  5.                                       (struct sockaddr *)(&cAddr), &length);//這裡沒什麼好說的返回新的sokcet檔案描述符了
  6.     if (mysql_socket_getfd(connect_sock) != INVALID_SOCKET ||//返回的新的socket檔案描述符無效(-1)
  7.         (socket_errno != SOCKET_EINTR && socket_errno != SOCKET_EAGAIN)) //如果是訊號中斷或者eagain重試沒關係再來
  8.       break;
  9.   }

點選(此處)摺疊或開啟

  1. Channel_info* channel_info= NULL;
  2. //注意下面有多型發生這裡和5.6很多不同了
  3.   if (is_unix_socket) //是否是本地UNIX_DOMAIN SOCKET
  4.     channel_info= new (std::nothrow) Channel_info_local_socket(connect_sock);
  5.   else//不是就是遠端TCP/IP SCOKET
  6.     channel_info= new (std::nothrow)
  7.       Channel_info_tcpip_socket(connect_sock,
  8.                                 (mysql_socket_getfd(listen_sock)
  9.                                  == m_extra_tcp_port_fd)); //根據connect_scok 建立channel_info
  10.  
  11.   if (channel_info == NULL) //分配記憶體是否正常不正常搞下面的。
  12.   {
  13.     (void) mysql_socket_shutdown(connect_sock, SHUT_RDWR);
  14.     (void) mysql_socket_close(connect_sock);
  15.     connection_errors_internal++;
  16.     return NULL;
  17.   }

  18.   return channel_info;

棧幀
#0  inline_mysql_socket_accept (src_file=0x21c5f30 "/root/mysql5.7.14/percona-server-5.7.14-7/sql/conn_handler/socket_connection.cc", src_line=928, key=3, socket_listen=..., addr=0x7fffffffdc50, addr_len=0x7fffffffdcdc)
    at /root/mysql5.7.14/percona-server-5.7.14-7/include/mysql/psi/mysql_socket.h:1110
#1  0x00000000016e4981 in Mysqld_socket_listener::listen_for_connection_event (this=0x2fca760) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/conn_handler/socket_connection.cc:928
#2  0x0000000000ecf016 in Connection_acceptor::connection_event_loop (this=0x2fc9b30) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/conn_handler/connection_acceptor.h:66
#3  0x0000000000ec6a06 in mysqld_main (argc=52, argv=0x2e97438) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/mysqld.cc:5287
#4  0x0000000000ebd344 in main (argc=9, argv=0x7fffffffe418) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/main.cc:25

6、Connection_handler_manager::process_new_connection,透過Channel_info指標指向的SCOKET資訊建立新執行緒

棧幀
#1  0x0000000000f1b9c6 in Connection_handler_manager::process_new_connection (this=0x2e98fc0, channel_info=0x34c4a30) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/conn_handler/connection_handler_manager.cc:301
#2  0x0000000000ecf034 in Connection_acceptor::connection_event_loop (this=0x2fc9b30) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/conn_handler/connection_acceptor.h:68
#3  0x0000000000ec6a06 in mysqld_main (argc=52, argv=0x2e97438) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/mysqld.cc:5287
#4  0x0000000000ebd344 in main (argc=9, argv=0x7fffffffe418) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/main.cc:25

9、 Connection_handler_manager::check_and_incr_conn_count(bool) 
   這裡判斷MAX_CONNECTIONS是否合適這裡也和運維內參說的5.6的程式碼有了
很大的不同,但是邏輯一樣如下:


點選(此處)摺疊或開啟

  1. if (extra_port_connection)
  2.   {
  3.     if (extra_connection_count > extra_max_connections)
  4.     {
  5.       connection_accepted= false;
  6.       m_connection_errors_max_connection++;
  7.     }
  8.     else
  9.     {
  10.       ++extra_connection_count;
  11.     }
  12.   }
  13.   else if (connection_count > max_connections)
  14.   {
  15.     connection_accepted= false;
  16.     m_connection_errors_max_connection++;
  17.   }
GDB如下:

(gdb) n
106       mysql_mutex_lock(&LOCK_connection_count);
(gdb) n
115       if (extra_port_connection)
(gdb) n
127       else if (connection_count > max_connections)
(gdb) P  max_connections
$4 = 6
(gdb) p connection_count
$5 = 7
(gdb) n
129         connection_accepted= false;
(gdb) n
130         m_connection_errors_max_connection++;
(gdb) n
141       mysql_mutex_unlock(&LOCK_connection_count);
(gdb) n
142       return connection_accepted;

棧幀
#0  Connection_handler_manager::check_and_incr_conn_count (this=0x2e98fc0, extra_port_connection=false) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/conn_handler/connection_handler_manager.cc:105
#1  0x0000000000f1b904 in Connection_handler_manager::process_new_connection (this=0x2e98fc0, channel_info=0x34c4a30) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/conn_handler/connection_handler_manager.cc:289
#2  0x0000000000ecf034 in Connection_acceptor::connection_event_loop (this=0x2fc9b30) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/conn_handler/connection_acceptor.h:68
#3  0x0000000000ec6a06 in mysqld_main (argc=52, argv=0x2e97438) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/mysqld.cc:5287
#4  0x0000000000ebd344 in main (argc=9, argv=0x7fffffffe418) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/main.cc:25


12、 Per_thread_connection_handler::add_connection(Channel_info*) 這個函式會判斷連線佇列是否有空閒的,如果沒有呼叫mysql_thread_create建立新的執行緒
擷取如下:

點選(此處)摺疊或開啟

  1. if (!check_idle_thread_and_enqueue_connection(channel_info)) //判斷是否有空閒的連線隊裡
  2.     DBUG_RETURN(false);
  3.   /*
  4.     There are no idle threads avaliable to take up the new
  5.     connection. Create a new thread to handle the connection
  6.   */
  7.   error= mysql_thread_create(key_thread_one_connection, &id, //建立新的執行緒對接
  8.                              &connection_attrib,
  9.                              handle_connection,
  10.                              (void*) channel_info);
棧幀
#0  Per_thread_connection_handler::add_connection (this=0x2f67b10, channel_info=0x34c4a30) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/conn_handler/connection_handler_per_thread.cc:399
#1  0x0000000000f1b9c6 in Connection_handler_manager::process_new_connection (this=0x2e98fc0, channel_info=0x34c4a30) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/conn_handler/connection_handler_manager.cc:301
#2  0x0000000000ecf034 in Connection_acceptor::connection_event_loop (this=0x2fc9b30) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/conn_handler/connection_acceptor.h:68
#3  0x0000000000ec6a06 in mysqld_main (argc=52, argv=0x2e97438) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/mysqld.cc:5287
#4  0x0000000000ebd344 in main (argc=9, argv=0x7fffffffe418) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/main.cc:25

8、 handle_connection(void*)  這裡就是5.7.16真正的回撥函式用於建立執行緒,這裡就不解釋了,因為這裡只是分析流程,以後在詳細研究




這裡終於找到了建立執行緒的回撥函式,除錯有時候不知道入口函式名字也是一種悲劇




作者微信:

               

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/7728585/viewspace-2140043/,如需轉載,請註明出處,否則將追究法律責任。

相關文章