zoukankan      html  css  js  c++  java
  • Percona 8.0 ThreadPool源码解析

    ThreadPool的基本功能在Percona 8.0里面没有太大的变化,只不调用的方式有变化,这里只介绍threadpool插件的初始化过程和调用过程。

    threadpool本身的逻辑可以参考:

    MariaDB · 源码分析 · thread pool

    Percona 5.7线程池源码分析

    threadpool插件初始化过

    线程池默认是关闭的,要开启这个功能,需要启动实例时指定参数--thread-handling=pool-of-threads。这个参数在代码中对应的变量是Connection_handler_manager::thread_handling,其中Connection_handler_manager是一个singleton类,thread_handling是其静态成员变量。Connection_handler_manager是代码中连接模型的抽象,当实例启动后,会有一条线程专门监听指定的TCP端口和Unix Socket上是否有新的连接请求,这部分代码实现在mysqld_main --> Connection_acceptor->connection_event_loop()中,当有连接请求时会调用Connection_handler_manager中相应Connection_handler对象的add_connection()虚方法处理连接,线程池和之前的thread-per-connection模型在这里就会进入不同的代码路径,因为启用线程池时,Connection_handler_manager::handler为Thread_pool_connection_handler对象,而thread-per-connection模型时该成员为Per_thread_connection_handler对象。Connection_handler_manager::handler的确定是在函数Connection_handler_manager::init中,根据Connection_handler_manager::thread_handling决定构造哪个类的对象。

    Connection_handler_manager::init()
    {
      switch (Connection_handler_manager::thread_handling)
      {
      case SCHEDULER_ONE_THREAD_PER_CONNECTION:
        connection_handler= new (std::nothrow) Per_thread_connection_handler();
        break;
      case SCHEDULER_NO_THREADS:
        connection_handler= new (std::nothrow) One_thread_connection_handler();
        break;
      case SCHEDULER_THREAD_POOL:
        connection_handler= new (std::nothrow) Thread_pool_connection_handler();
        break;
      default:
        DBUG_ASSERT(false);
      }
    }
    
    mysqld_main
     ->init_server_components
      ->plugin_register_dynamic_and_init_all
       ->plugin_init_initialize_and_reap
        ->plugin_initialize
         ->threadpool_plugin_init
    	    tp_init  # 初始化threadpool
    	    my_connection_handler_set  # 设置connection_handler
    		 Plugin_connection_handler *conn_handler = new (std::nothrow) Plugin_connection_handler(chf);
    		 Connection_handler_manager::get_instance()->load_connection_handler(conn_handler);
    

    其中tp_init是初始化threadpool的核心入口,初始化 thread_group_t 数组。

    my_connection_handler_set设置连接的处理模式。

    threadpool调用过程

    mysqld_main
     ->connection_event_loop
      ->Connection_handler_manager::process_new_connection
       ->add_connection
    

    其中add_connection就是threadpool的核心入口,主要内容如下:

    创建connection,将connection分配到一个thread_group,插入group队列。

    唤醒或创建一个worker thread,如果group中没有活跃的worker thread。

  • 相关阅读:
    20.C语言_数组参数传递
    19.C语言_取值运算符*
    Google Chrome 解决 “您的连接不是私密连接” 和被毒霸劫持
    教你如何一步步将项目部署到Github
    教你如何把Android手机的网络完全映射到PC上,比如免流给PC用…
    CSS background-size 属性详解
    display:block;是什么意思
    Cookie是储存在电脑文本文件中的数据,用于保存访问者的信息,并可以在下次打开页面时引用。
    Marquee(跑马灯)横向、纵向、无空白的不间断连续循环滚动代码
    Visual Studio中从应用程序中调试SQL脚本
  • 原文地址:https://www.cnblogs.com/jmliao/p/12627860.html
Copyright © 2011-2022 走看看