• 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。

  • 相关阅读:
    SQL多表合并查询结果
    无法访问请求的页面,因为该页的数据的相关配置数据无效
    网站运行的时候编译成功但是程序浏览器的页面不会显示怎么回事啊
    EasyUI 在textbox里面输入数据敲回车后查询和普通在textbox输入数据敲回车的区别
    搭建Go开发及调试环境(LiteIDE + GoClipse) -- Windows篇
    Python开发工具安装
    jQuery 插件写法2
    jQuery插件面向对象开发
    Jquery插件学习
    Maven 仓库
  • 原文地址:https://www.cnblogs.com/jmliao/p/12627860.html
Copyright © 2020-2023  润新知