当前位置:网站首页>2022-07-15 MySQL receives new connection processing
2022-07-15 MySQL receives new connection processing
2022-07-22 15:06:00 【Emperor Zunwu world】
Catalog
The main thread receives the request , Create a new thread :
The sub thread starts processing the request :
Abstract :
Brief analysis mysql Processing of receiving new connections
Process handling :
The main thread receives the request , Create a new thread :
(gdb) bt
#0 Per_thread_connection_handler::add_connection (this=0x5e7a4d0, channel_info=0x7dc3430) at /data/jenkins/workspace/stonedb5.7-zsl-centos7.9/sql/conn_handler/connection_handler_per_thread.cc:417
#1 0x0000000001db0f66 in Connection_handler_manager::process_new_connection (this=0x5e277a0, channel_info=0x7dc3430)
at /data/jenkins/workspace/stonedb5.7-zsl-centos7.9/sql/conn_handler/connection_handler_manager.cc:275
#2 0x0000000001d73037 in Connection_acceptor<Mysqld_socket_listener>::connection_event_loop (this=0x7dc18a0)
at /data/jenkins/workspace/stonedb5.7-zsl-centos7.9/sql/conn_handler/connection_acceptor.h:75
#3 0x0000000001d6a47e in mysqld_main (argc=115, argv=0x5e27c48) at /data/jenkins/workspace/stonedb5.7-zsl-centos7.9/sql/mysqld.cc:5199
#4 0x0000000001d61097 in main (argc=8, argv=0x7ffdd55cdb28) at /data/jenkins/workspace/stonedb5.7-zsl-centos7.9/sql/main.cc:32
The sub thread starts processing the request :
(gdb) c
Continuing.
[New Thread 0x7fdc80137700 (LWP 11313)]
[Switching to Thread 0x7fdc80137700 (LWP 11313)]
Breakpoint 2, pfs_spawn_thread (arg=0x99ee3f0) at /data/jenkins/workspace/stonedb5.7-zsl-centos7.9/storage/perfschema/pfs.cc:2197
2197 (*user_start_routine)(user_arg);
(gdb) bt
#0 pfs_spawn_thread (arg=0x99ee3f0) at /data/jenkins/workspace/stonedb5.7-zsl-centos7.9/storage/perfschema/pfs.cc:2197
#1 0x00007fdcd505eea5 in start_thread (arg=0x7fdc80137700) at pthread_create.c:307
#2 0x00007fdcd3495b0d in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:111
(gdb) p user_start_routine
$1 = (void *(*)(void *)) 0x2597a15 <handle_connection(void*)>
(gdb) s
handle_connection (arg=0x7dc3430) at /data/jenkins/workspace/stonedb5.7-zsl-centos7.9/sql/conn_handler/connection_handler_per_thread.cc:251
251 Global_THD_manager *thd_manager= Global_THD_manager::get_instance();
Sequential flow :
Be careful :
- clone The system call returns twice , Parent thread once , Once for the newly created sub thread
- Cool tone is the main thread of processing , Warm colors for new sub thread processing
The key function :
Per_thread_connection_handler::add_connection
bool Per_thread_connection_handler::add_connection(Channel_info* channel_info)
{
int error= 0;
my_thread_handle id;
DBUG_ENTER("Per_thread_connection_handler::add_connection");
// Simulate thread creation for test case before we check thread cache
DBUG_EXECUTE_IF("fail_thread_create", error= 1; goto handle_error;);
if (!check_idle_thread_and_enqueue_connection(channel_info))
DBUG_RETURN(false);
/*
There are no idle threads avaliable to take up the new
connection. Create a new thread to handle the connection
*/
channel_info->set_prior_thr_create_utime();
error= mysql_thread_create(key_thread_one_connection, &id,
&connection_attrib,
handle_connection,
(void*) channel_info);
#ifndef NDEBUG
handle_error:
#endif // !NDEBUG
if (error)
{
connection_errors_internal++;
if (!create_thd_err_log_throttle.log())
sql_print_error("Can't create thread to handle new connection(errno= %d)",
error);
channel_info->send_error_and_close_channel(ER_CANT_CREATE_THREAD,
error, true);
Connection_handler_manager::dec_connection_count();
DBUG_RETURN(true);
}
Global_THD_manager::get_instance()->inc_thread_created();
DBUG_PRINT("info",("Thread created"));
DBUG_RETURN(false);
}
mysql_thread_create
#ifdef HAVE_PSI_THREAD_INTERFACE
#define mysql_thread_create(K, P1, P2, P3, P4) \
inline_mysql_thread_create(K, P1, P2, P3, P4)
#else
#define mysql_thread_create(K, P1, P2, P3, P4) \
my_thread_create(P1, P2, P3, P4)
#endif
int my_thread_create(my_thread_handle *thread, const my_thread_attr_t *attr,
my_start_routine func, void *arg)
{
#ifndef _WIN32
return pthread_create(&thread->thread, attr, func, arg);
#else
struct thread_start_parameter *par;
unsigned int stack_size;
par= (struct thread_start_parameter *)malloc(sizeof(*par));
if (!par)
goto error_return;
par->func= func;
par->arg= arg;
stack_size= attr ? attr->dwStackSize : 0;
thread->handle= (HANDLE)_beginthreadex(NULL, stack_size, win_thread_start,
par, 0, &thread->thread);
if (thread->handle)
{
/* Note that JOINABLE is default, so attr == NULL => JOINABLE. */
if (attr && attr->detachstate == MY_THREAD_CREATE_DETACHED)
{
/*
Close handles for detached threads right away to avoid leaking
handles. For joinable threads we need the handle during
my_thread_join. It will be closed there.
*/
CloseHandle(thread->handle);
thread->handle= NULL;
}
return 0;
}
my_osmaperr(GetLastError());
free(par);
error_return:
thread->thread= 0;
thread->handle= NULL;
return 1;
#endif
}
pfs_spawn_thread
extern "C" void* pfs_spawn_thread(void *arg)
{
PFS_spawn_thread_arg *typed_arg= (PFS_spawn_thread_arg*) arg;
void *user_arg;
void *(*user_start_routine)(void*);
PFS_thread *pfs;
/* First, attach instrumentation to this newly created pthread. */
PFS_thread_class *klass= find_thread_class(typed_arg->m_child_key);
if (likely(klass != NULL))
{
pfs= create_thread(klass, typed_arg->m_child_identity, 0);
if (likely(pfs != NULL))
{
pfs->m_thread_os_id= my_thread_os_id();
clear_thread_account(pfs);
pfs->m_parent_thread_internal_id= typed_arg->m_thread_internal_id;
memcpy(pfs->m_username, typed_arg->m_username, sizeof(pfs->m_username));
pfs->m_username_length= typed_arg->m_username_length;
memcpy(pfs->m_hostname, typed_arg->m_hostname, sizeof(pfs->m_hostname));
pfs->m_hostname_length= typed_arg->m_hostname_length;
set_thread_account(pfs);
}
}
else
{
pfs= NULL;
}
my_thread_set_THR_PFS(pfs);
/*
Secondly, free the memory allocated in spawn_thread_v1().
It is preferable to do this before invoking the user
routine, to avoid memory leaks at shutdown, in case
the server exits without waiting for this thread.
*/
user_start_routine= typed_arg->m_user_start_routine;
user_arg= typed_arg->m_user_arg;
my_free(typed_arg);
/* Then, execute the user code for this thread. */
(*user_start_routine)(user_arg);
return NULL;
}
边栏推荐
- [AR Foundation] AR Session Origin
- [must see for developers] [push kit] collection of typical problems of push service 3
- 召回向排序靠齐:多目标排序蒸馏召回
- MATLAB basic grammar (I)
- 某视频app分析样本
- MSTP&VRRP协议
- 1. Openpyxl open workbook
- Matlab基本语法(一)
- 【独立站运营】Shopify卖家:如何改善店铺体验?两招搞定!
- This time, thoroughly sort out various layout problems
猜你喜欢
[open source diary] dormitory power-off automatic light off equipment (II)
[HMS core] [ml kit] FAQ of machine learning service (II)
[HMS core] [FAQ] collection of typical problems of account kit, MDM capability and push Kit 6
Worthington 细胞分离优化系统(含细胞分离指南)
Hcia-r & s self use notes (15) OSPF foundation, OSPF core workflow, OSPF packet type, OSPF neighbor establishment conditions and process
Silicon Valley classroom notes (Part 2)
2022.7.11-7.17 AI industry weekly (issue 106): just try your best
687. Longest same value path ●●
无动物型胶原酶丨Worthington的多种应用方案
2022-07-18 exists子查询的剔除遍历处理
随机推荐
HCIA-R&S自用笔记(16)DR与BDR、OSPF度量值、OSPF 配置、OSPF综合实验(认证+缺省路由发布)
Configuring laradock under winodos
1382. 将二叉搜索树变平衡 ●●
【独立站运营】Shopify卖家:如何改善店铺体验?两招搞定!
Practice of online problem feedback module (10): realize image preview function
stm32 RT-Thread移植lvgl
HCIA-R&S自用笔记(13)路由典型问题案例分析、动态路由概述
MSTP&VRRP协议
2022-07-18 jenkins流水线使用及创建自己的流水线
Introduction to relevant matters of PMP preparation guide
弹性蛋白酶丨Worthington 核心酶详细参考资料
[MySQL series] addition, deletion, modification and query of MySQL tables (basic)
Hcia-r & s self use notes (15) OSPF foundation, OSPF core workflow, OSPF packet type, OSPF neighbor establishment conditions and process
Hcia-r & s self use notes (13) case analysis of typical routing problems and overview of dynamic routing
Y73. Chapter IV Prometheus big factory monitoring system and actual combat -- blackbox exporter installation and grafana installation (IV)
Practical demonstration: how to make a scrum iteration plan with ones?
2022 - 07 - 18 sous - Requête - optimisation du traitement de l'optimiseur de requête join pour le scénario exists
【MySQL系列】“一劳永逸“ 解决MySQL中 “插入中文数据“出错的问题
【攻防世界WEB】难度二星6分入门题(上):webshell、command_execution、xff_referer、php_rce、Web_php_include、NewsCenter
Recall to order: multi-objective sorting distillation recall