MySQL multi-threaded server implementation(mysql 5.1.49)<br />
Main topic<br />主线程loop监听连接请求,开启线程处理客户query请求<br />客户处理线程处理query请求,一个线程一次只能服务于一个client,一个线程可能服务多个client(thread pool 机制)<br />
Sample of pstack<br />pstack <pid_mysqld><br />
主线程处理流程<br />主线程处理流程(mysqld.cc main函数)<br />参数初始化 init_common_variables<br />线程处理机制 get_options – 一个线程处理一个连接<br />one_thre...
handle_connections_sockets<br />handle_connections_sockets  loop<br />建立网络连接( socket accept)<br />建立THD, 将thd和新建立的socket连接...
Thread_schedule结构<br />thread_scheduler<br />
构造函数<br />
thread_scheduler初始化Schedule.cc<br />Create_thread_to_handle_connection<br />从thread pool取出一个线程, 或<br />新建一个线程 pthread_crea...
新建线程(pthread_create)<br />create_thread_to_handle_connection<br />刚开始,thread pool是空的,调用pthread_create创建新的线程<br />新线程处理函数:h...
thread_scheduler.end_thread(thd,1)<br />one_thread_per_connection_end<br />cache_thread不返回,直到该线程被主线程唤醒(从thread pool取出线程)<b...
handle_one_connection<br />
线程唤醒<br />create_thread_to_handle_connection<br />如果thread pool里有sleep的线程,则唤醒一个线程处理客户请求,主线程继续监听<br />
Passing thd<br />主线程创建thd, 传递给其他线程<br />新建线程时,通过pthread_create传递thd<br />从线程池唤醒一个线程时<br />主线程将thd放到链表.<br />子线程从链表中取出thd, ...
thd->store_globals<br />
current_thread<br />
主线程loop<br />handle_connections_sockets<br />新建线程或唤醒thread pool的一个线程后,主线程继续监听,等待新的客户<br />while(!abort_loop) {<br />      ...
Do_command<br />Dispatch_command<br />
Mysql multi-threaded server building blocks<br />Tcp/Ip socket, Unix domain socket, named pipes<br />Posix thread(pthread_...
reference<br />http://x.saw-unix.com/mysqld_thread_init.jpg<br />http://forge.mysql.com/wiki/MySQL_Internals_Guided_Tour<b...
Upcoming SlideShare
Loading in...5
×

Mysql multi threaded_server_introduction

3,741

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,741
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
35
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Mysql multi threaded_server_introduction

  1. 1. MySQL multi-threaded server implementation(mysql 5.1.49)<br />
  2. 2. Main topic<br />主线程loop监听连接请求,开启线程处理客户query请求<br />客户处理线程处理query请求,一个线程一次只能服务于一个client,一个线程可能服务多个client(thread pool 机制)<br />
  3. 3. Sample of pstack<br />pstack <pid_mysqld><br />
  4. 4. 主线程处理流程<br />主线程处理流程(mysqld.cc main函数)<br />参数初始化 init_common_variables<br />线程处理机制 get_options – 一个线程处理一个连接<br />one_thread_per_connection_scheduler(&thread_scheduler)<br />网络初始化network_init<br />开启网络监听(tcp/ip socket)<br />开启本地监听(unix domain socket或named pipe)<br />handle_connections_sockets(0),进入主线程loop<br />接受客户端连接请求(tcp/ip socket connect request)<br />开启线程处理客户端query请求(dml,ddl,.etc)<br />
  5. 5. handle_connections_sockets<br />handle_connections_sockets loop<br />建立网络连接( socket accept)<br />建立THD, 将thd和新建立的socket连接关联起来<br />my_net_init(&thd->net,vio_tmp))<br />开启线程服务客户 create_new_thread(thd)<br />create_new_thread调用了thread_scheduler.add_connection(thd), <br />thread_scheduler是一个全局变量,mysqld启动的时候被初始化 (one_thread_per_connection_scheduler(&thread_scheduler)<br />
  6. 6. Thread_schedule结构<br />thread_scheduler<br />
  7. 7. 构造函数<br />
  8. 8. thread_scheduler初始化Schedule.cc<br />Create_thread_to_handle_connection<br />从thread pool取出一个线程, 或<br />新建一个线程 pthread_create<br />One_thread_per_connection_end<br />当前客户退出后,将线程放入thread pool<br />
  9. 9. 新建线程(pthread_create)<br />create_thread_to_handle_connection<br />刚开始,thread pool是空的,调用pthread_create创建新的线程<br />新线程处理函数:handle_one_connection<br />线程初始化 setup_connection_thread_globals<br />登录,用户信息验证 login_connection(thd)<br />进入loop, 处理客户端的query<br />do_command(…)<br />客户端退出后,断开网络连接,将线程放入thread pool<br />断开网络连接 end_connection<br />线程进入thread pool睡眠 thread_scheduler.end_thread(thd,1)<br />
  10. 10. thread_scheduler.end_thread(thd,1)<br />one_thread_per_connection_end<br />cache_thread不返回,直到该线程被主线程唤醒(从thread pool取出线程)<br />唤醒后立刻处理新客户的请求<br />
  11. 11.
  12. 12. handle_one_connection<br />
  13. 13. 线程唤醒<br />create_thread_to_handle_connection<br />如果thread pool里有sleep的线程,则唤醒一个线程处理客户请求,主线程继续监听<br />
  14. 14. Passing thd<br />主线程创建thd, 传递给其他线程<br />新建线程时,通过pthread_create传递thd<br />从线程池唤醒一个线程时<br />主线程将thd放到链表.<br />子线程从链表中取出thd, 存到thread local storage<br />子线程从tls中取出thd<br />传递的是指向THD的指针,thread共享地址空间<br />
  15. 15. thd->store_globals<br />
  16. 16. current_thread<br />
  17. 17. 主线程loop<br />handle_connections_sockets<br />新建线程或唤醒thread pool的一个线程后,主线程继续监听,等待新的客户<br />while(!abort_loop) {<br /> accept(…) // 建立网络连接<br />create_new_thread(thd); //线程处理<br />}<br />
  18. 18. Do_command<br />Dispatch_command<br />
  19. 19. Mysql multi-threaded server building blocks<br />Tcp/Ip socket, Unix domain socket, named pipes<br />Posix thread(pthread_create)<br />Condition variables, mutex locks<br />pthread_cond_wait, pthread_cond_timedwait<br />pthread_cond_signal<br />pthread_cond_broadcast<br />Thread local storage<br />pthread_setspecific<br />pthread_getspecific<br />
  20. 20. reference<br />http://x.saw-unix.com/mysqld_thread_init.jpg<br />http://forge.mysql.com/wiki/MySQL_Internals_Guided_Tour<br />Unix network programming(richardstevens)<br />Advanced programming in the Unix environment (richardstevens)<br />
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×