Skip to content

Commit 197a5e1

Browse files
committed
WL#6606: Offload THD initialization and network initialization
to worker thread Follow-up patch. This patch improves the separation between thread_pool (plugins) and the server. Instead of having thread_pool extend a pure virtual superclass from the server, a special plugin connection handler now exists in the server. This connection handler is instantiated by the thread_pool with a set of pointer to global, free functions. These functions are then called by the server e.g. when a new client connects. The patch also reduces the scope of LOCK_thread_count inside thread_pool and makes Connection_handler_mananager::max_threads static and public so that it can be accessed without valid manager instance.
1 parent 3727638 commit 197a5e1

18 files changed

+288
-143
lines changed

include/mysql/plugin_audit.h.pp

+6-5
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,15 @@
5353
void thd_wait_begin(void* thd, int wait_type);
5454
void thd_wait_end(void* thd);
5555
#include <mysql/service_thread_scheduler.h>
56-
struct Connection_handler_callback;
56+
struct Connection_handler_functions;
57+
struct THD_event_functions;
5758
extern struct my_thread_scheduler_service {
58-
int (*connection_handler_set)(void* conn_handler,
59-
struct Connection_handler_callback *cb);
59+
int (*connection_handler_set)(struct Connection_handler_functions *,
60+
struct THD_event_functions *);
6061
int (*connection_handler_reset)();
6162
} *my_thread_scheduler_service;
62-
int my_connection_handler_set(void* conn_handler,
63-
struct Connection_handler_callback *cb);
63+
int my_connection_handler_set(struct Connection_handler_functions *chf,
64+
struct THD_event_functions *tef);
6465
int my_connection_handler_reset();
6566
#include <mysql/service_my_plugin_log.h>
6667
enum plugin_log_level

include/mysql/plugin_auth.h.pp

+6-5
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,15 @@
5353
void thd_wait_begin(void* thd, int wait_type);
5454
void thd_wait_end(void* thd);
5555
#include <mysql/service_thread_scheduler.h>
56-
struct Connection_handler_callback;
56+
struct Connection_handler_functions;
57+
struct THD_event_functions;
5758
extern struct my_thread_scheduler_service {
58-
int (*connection_handler_set)(void* conn_handler,
59-
struct Connection_handler_callback *cb);
59+
int (*connection_handler_set)(struct Connection_handler_functions *,
60+
struct THD_event_functions *);
6061
int (*connection_handler_reset)();
6162
} *my_thread_scheduler_service;
62-
int my_connection_handler_set(void* conn_handler,
63-
struct Connection_handler_callback *cb);
63+
int my_connection_handler_set(struct Connection_handler_functions *chf,
64+
struct THD_event_functions *tef);
6465
int my_connection_handler_reset();
6566
#include <mysql/service_my_plugin_log.h>
6667
enum plugin_log_level

include/mysql/plugin_ftparser.h.pp

+6-5
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,15 @@
5353
void thd_wait_begin(void* thd, int wait_type);
5454
void thd_wait_end(void* thd);
5555
#include <mysql/service_thread_scheduler.h>
56-
struct Connection_handler_callback;
56+
struct Connection_handler_functions;
57+
struct THD_event_functions;
5758
extern struct my_thread_scheduler_service {
58-
int (*connection_handler_set)(void* conn_handler,
59-
struct Connection_handler_callback *cb);
59+
int (*connection_handler_set)(struct Connection_handler_functions *,
60+
struct THD_event_functions *);
6061
int (*connection_handler_reset)();
6162
} *my_thread_scheduler_service;
62-
int my_connection_handler_set(void* conn_handler,
63-
struct Connection_handler_callback *cb);
63+
int my_connection_handler_set(struct Connection_handler_functions *chf,
64+
struct THD_event_functions *tef);
6465
int my_connection_handler_reset();
6566
#include <mysql/service_my_plugin_log.h>
6667
enum plugin_log_level

include/mysql/service_thread_scheduler.h

+44-15
Original file line numberDiff line numberDiff line change
@@ -18,36 +18,65 @@
1818
#ifndef SERVICE_THREAD_SCHEDULER_INCLUDED
1919
#define SERVICE_THREAD_SCHEDULER_INCLUDED
2020

21-
#ifdef __cplusplus
22-
class Connection_handler;
23-
#define MYSQL_CONNECTION_HANDLER Connection_handler*
24-
#else
25-
#define MYSQL_CONNECTION_HANDLER void*
26-
#endif
27-
2821
#ifdef __cplusplus
2922
extern "C" {
3023
#endif
3124

32-
struct Connection_handler_callback;
25+
struct Connection_handler_functions;
26+
struct THD_event_functions;
3327

3428
extern struct my_thread_scheduler_service {
35-
int (*connection_handler_set)(MYSQL_CONNECTION_HANDLER conn_handler,
36-
struct Connection_handler_callback *cb);
29+
int (*connection_handler_set)(struct Connection_handler_functions *,
30+
struct THD_event_functions *);
3731
int (*connection_handler_reset)();
3832
} *my_thread_scheduler_service;
33+
34+
3935
#ifdef MYSQL_DYNAMIC_PLUGIN
4036

41-
#define my_connection_handler_set(F, M) my_thread_scheduler_service->connection_handler_set((F), (M))
42-
#define my_connection_handler_reset() my_thread_scheduler_service->connection_handler_reset()
37+
#define my_connection_handler_set(F, M) \
38+
my_thread_scheduler_service->connection_handler_set((F), (M))
39+
#define my_connection_handler_reset() \
40+
my_thread_scheduler_service->connection_handler_reset()
4341

4442
#else
4543

46-
int my_connection_handler_set(MYSQL_CONNECTION_HANDLER conn_handler,
47-
struct Connection_handler_callback *cb);
44+
/**
45+
Instantiates Plugin_connection_handler based on the supplied
46+
Conection_handler_functions and sets it as the current
47+
connection handler.
48+
49+
Also sets the THD_event_functions functions which will
50+
be called by the server when e.g. begining a wait.
51+
52+
Remembers the existing connection handler so that it can be restored later.
53+
54+
@param chf struct with functions to be called when e.g. handling
55+
new clients.
56+
@param tef struct with functions to be called when events
57+
(e.g. lock wait) happens.
58+
59+
@note Both pointers (i.e. not the structs themselves) will be copied,
60+
so the structs must not disappear.
61+
62+
@note We don't support dynamically loading more than one connection handler.
63+
64+
@retval 1 failure
65+
@retval 0 success
66+
*/
67+
int my_connection_handler_set(struct Connection_handler_functions *chf,
68+
struct THD_event_functions *tef);
69+
70+
/**
71+
Destroys the current connection handler and restores the previous.
72+
Should only be called after calling my_connection_handler_set().
73+
74+
@retval 1 failure
75+
@retval 0 success
76+
*/
4877
int my_connection_handler_reset();
4978

50-
#endif
79+
#endif /* MYSQL_DYNAMIC_PLUGIN */
5180

5281
#ifdef __cplusplus
5382
}

include/mysql/thread_pool_priv.h

+70-12
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,75 @@
4040

4141
typedef std::set<THD*>::iterator Thread_iterator;
4242

43+
#ifdef __cplusplus
44+
extern "C" {
45+
#endif
46+
47+
/**
48+
This structure must be populated by plugins which implement connection
49+
handlers and passed as an argument to my_connection_handler_set() in
50+
order to activate the connection handler.
51+
52+
The structure contains pointers to plugin functions which the server
53+
will call when a new client connects, an existing client disconnects
54+
or when the connection handler is unloaded. It also containts the
55+
maximum number of threads the connection handler will create.
56+
*/
57+
struct Connection_handler_functions
58+
{
59+
/**
60+
The maximum number of threads this connection handler will create.
61+
*/
62+
uint max_threads;
63+
64+
/**
65+
Called by the server when a new client connects.
66+
67+
@param channel_info Pointer to object containing information
68+
about the new connection.
69+
70+
@retval true failure
71+
@retval false success
72+
*/
73+
bool (*add_connection)(Channel_info *channel_info);
74+
75+
/**
76+
Called by the server when an existing client disconnects.
77+
78+
@param thd Thread descriptor.
79+
80+
@note This function pointer can be NULL if the plugin connection
81+
handler takes no action on disconnects.
82+
83+
@retval true failure
84+
@retval false success
85+
*/
86+
bool (*remove_connection)(THD *thd);
87+
88+
/**
89+
Called by the server when the connection handler is destroyed.
90+
*/
91+
void (*end)(void);
92+
};
93+
4394
/* create thd from channel_info object */
4495
THD* create_thd(Channel_info* channel_info);
4596
/* destroy channel_info object */
4697
void destroy_channel_info(Channel_info* channel_info);
98+
/* Decrement connection counter */
99+
void dec_connection_count();
100+
/*
101+
thread_created is maintained by thread pool when activated since
102+
user threads are created by the thread pool (and also special
103+
threads to maintain the thread pool). This is done through
104+
inc_thread_created.
105+
*/
106+
void inc_thread_created();
107+
void inc_aborted_connects();
108+
109+
#ifdef __cplusplus
110+
}
111+
#endif
47112

48113
/* Needed to get access to scheduler variables */
49114
void* thd_get_scheduler_data(THD *thd);
@@ -112,8 +177,6 @@ void close_connection(THD *thd, uint errcode);
112177
void end_connection(THD *thd);
113178
/* Release resources of the THD object */
114179
void thd_release_resources(THD *thd);
115-
/* Decrement connection counter */
116-
void dec_connection_count();
117180
/* Reset the context associated with the thread */
118181
void restore_globals(THD *thd);
119182
/* Destroy THD object */
@@ -122,21 +185,16 @@ void destroy_thd(THD *thd);
122185
void remove_global_thread(THD *thd);
123186

124187
/*
125-
thread_created is maintained by thread pool when activated since
126-
user threads are created by the thread pool (and also special
127-
threads to maintain the thread pool). This is done through
128-
inc_thread_created.
129-
130188
max_connections is needed to calculate the maximum number of threads
131189
that is allowed to be started by the thread pool. The method
132190
get_max_connections() gets reference to this variable.
133-
191+
*/
192+
ulong get_max_connections(void);
193+
/*
134194
connection_attrib is the thread attributes for connection threads,
135195
the method get_connection_attrib provides a reference to these
136196
attributes.
137197
*/
138-
void inc_thread_created(void);
139-
void inc_aborted_connects(void);
140-
ulong get_max_connections(void);
141198
pthread_attr_t *get_connection_attrib(void);
142-
#endif
199+
200+
#endif // THREAD_POOL_PRIV_INCLUDED

libmysqld/lib_sql.cc

-4
Original file line numberDiff line numberDiff line change
@@ -425,9 +425,7 @@ static void emb_free_embedded_thd(MYSQL *mysql)
425425
thd->store_globals();
426426
thd->release_resources();
427427

428-
mysql_mutex_lock(&LOCK_thread_count);
429428
remove_global_thread(thd);
430-
mysql_mutex_unlock(&LOCK_thread_count);
431429

432430
delete thd;
433431
my_pthread_setspecific_ptr(THR_THD, 0);
@@ -738,9 +736,7 @@ void *create_embedded_thd(int client_flag)
738736
thd->data_tail= &thd->first_data;
739737
memset(&thd->net, 0, sizeof(thd->net));
740738

741-
mysql_mutex_lock(&LOCK_thread_count);
742739
add_global_thread(thd);
743-
mysql_mutex_unlock(&LOCK_thread_count);
744740
thd->mysys_var= 0;
745741
return thd;
746742
err:

0 commit comments

Comments
 (0)