Your SlideShare is downloading. ×
Mysql multi threaded_server_introduction
Mysql multi threaded_server_introduction
Mysql multi threaded_server_introduction
Mysql multi threaded_server_introduction
Mysql multi threaded_server_introduction
Mysql multi threaded_server_introduction
Mysql multi threaded_server_introduction
Mysql multi threaded_server_introduction
Mysql multi threaded_server_introduction
Mysql multi threaded_server_introduction
Mysql multi threaded_server_introduction
Mysql multi threaded_server_introduction
Mysql multi threaded_server_introduction
Mysql multi threaded_server_introduction
Mysql multi threaded_server_introduction
Mysql multi threaded_server_introduction
Mysql multi threaded_server_introduction
Mysql multi threaded_server_introduction
Mysql multi threaded_server_introduction
Mysql multi threaded_server_introduction
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Mysql multi threaded_server_introduction

3,732

Published on

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

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

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

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

×