MySQL 8.4.2
Source Code Documentation
rpl_io_monitor.h
Go to the documentation of this file.
1/* Copyright (c) 2020, 2024, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef RPL_IO_MONITOR_H
25#define RPL_IO_MONITOR_H
26
27#include <atomic>
28#include <vector>
29
32
33class Master_info;
34class THD;
35struct TABLE;
36
37/* Mysql_connection class object */
38using MYSQL_CONN_PTR = std::unique_ptr<Mysql_connection>;
39
40/* mysql connection map key <channel, host, port> */
41using MYSQL_CONN_KEY = std::tuple<std::string, std::string, uint>;
42
43/**
44 Connection managed tuple <channel, host, port, network_namespace, weight,
45 managed_name, primary_weight, secondary_weight>
46*/
48 std::tuple<std::string, std::string, uint, std::string, uint, std::string,
49 uint, uint>;
50
51/* Sql queries tag list */
52enum class enum_sql_query_tag : uint {
58};
59
60/* Configuration mode quorum status */
65};
66
67struct thread_state {
68 /**
69 * @enum thread_state_enum
70 * @brief Maintains thread status
71 */
73 THREAD_NONE = 0, /**< THREAD_NOT_CREATED */
74 THREAD_CREATED, /**< THREAD_CREATED */
75 THREAD_INIT, /**< THREAD_INIT */
76
77 THREAD_RUNNING, /**< THREAD_RUNNING */
78
79 THREAD_TERMINATED, /**< THREAD_EXIT */
80 THREAD_END /**< END OF ENUM */
81 };
82
83 private:
85
86 public:
88
89 void set_running() { thread_state_var = thread_state_enum::THREAD_RUNNING; }
90
92 thread_state_var = thread_state_enum::THREAD_TERMINATED;
93 }
94
95 void set_initialized() { thread_state_var = thread_state_enum::THREAD_INIT; }
96
97 void set_created() { thread_state_var = thread_state_enum::THREAD_CREATED; }
98
99 bool is_initialized() const {
100 return ((thread_state_var >= thread_state_enum::THREAD_INIT) &&
101 (thread_state_var < thread_state_enum::THREAD_TERMINATED));
102 }
103
104 bool is_running() const {
105 return thread_state_var == thread_state_enum::THREAD_RUNNING;
106 }
107
108 bool is_alive_not_running() const {
109 return thread_state_var < thread_state_enum::THREAD_RUNNING;
110 }
111
112 bool is_thread_alive() const {
113 return ((thread_state_var >= thread_state_enum::THREAD_CREATED) &&
114 (thread_state_var < thread_state_enum::THREAD_TERMINATED));
115 }
116
117 bool is_thread_dead() const { return !is_thread_alive(); }
118};
119
120/**
121 @class Source_IO_monitor
122 Class that contains functionality to monitor group member's state, role and
123 quorum changes on all the potential senders in the Sender List, and if it
124 finds any changes or lost quorum it does automatic update of the sender list.
125*/
127 public:
128 /* Source_IO_monitor class constructor */
130
131 /* Source_IO_monitor class destructor */
132 virtual ~Source_IO_monitor();
133
134 /* Source_IO_monitor class copy constructor (restricted) */
136
137 /* Source_IO_monitor class assignment operator (restricted) */
139
140 /**
141 Fetch Source_IO_monitor class instance.
142
143 @return Pointer to the Source_IO_monitor class instance.
144 */
146
147 /**
148 Creates and launches new Monitor IO thread.
149
150 @param[in] thread_key instrumentation key
151
152 @returns false in case of success, or true otherwise.
153 */
155
156 /**
157 Terminate the Monitor IO thread.
158
159 @returns 0 in case of success, or 1 otherwise.
160 */
162
163 /**
164 Check if Monitor IO thread is killed.
165
166 @param[in] thd The thread.
167 @param[in] mi the pointer to the Master_info object.
168 @return true if yes, false otherwise
169 */
170 bool is_monitor_killed(THD *thd, Master_info *mi);
171
172 /**
173 Gets the delay time between each iteration where it fetches group details.
174
175 @return the delay time in seconds.
176 */
177 uint get_monitoring_wait();
178
179 /**
180 Gets the status of monitor IO thread whether its running.
181
182 @return true if monitor IO thread running, false otherwise.
183 */
185
186 /**
187 It gets stored senders details for channel from
188 replication_asynchronous_connection_failover table.
189
190 @param[in] channel_name the channel from which get the senders
191
192 @returns std::tuple<bool, List_of_Tuple> where each element has
193 following meaning:
194
195 first element of tuple is function return value and determines:
196 false Successful
197 true Error
198
199 second element of the tuple contains following details in tuple
200 <channel, host, port, network_namespace, weight,
201 managed_name, primary_weight, secondary_weight>
202 */
203 std::tuple<bool, std::vector<SENDER_CONN_MERGE_TUPLE>> get_senders_details(
204 const std::string &channel_name);
205
206 /**
207 The function started by Monitor IO thread which does monitor group member's
208 state, role and quorum changes on all the potential senders in the Sender
209 List, and if it finds any changes or lost quorum it does automatic update
210 of the sender list. The thread runs in infinite loop till its not killed.
211 */
213
214 /**
215 Sets the delay between each iteration where it fetches group details.
216
217 @param[in] wait_time the delay time in seconds to set.
218 */
220
221 /**
222 Gets the sql query string.
223
224 @param[in] qtag the query to fetch.
225
226 @return the sql query string.
227 */
228 std::string get_query(enum_sql_query_tag qtag);
229
230 private:
231 /* The Monitor IO thread THD object. */
233
234 /* The flag to determine if Monitor IO thread aborted */
235 bool m_abort_monitor{false};
236
237 /* The delay time in seconds */
239
240 /* monitor IO thread lock for thread synchronization */
242
243 /* monitor IO thread condition variable for thread wait. */
245
246 /* monitor IO thread variable used for THD creation. */
248
249 /* Monitor IO thread state */
251
253
254 /* Sql queries result column number */
261 };
262
263 /**
264 It gets stored senders details for channel from
265 replication_asynchronous_connection_failover table, and then connects
266 to it. It also stores client connection object to all the connected
267 stores.
268 Then it gets group membership list from each sender.
269
270 @param[in] thd The thread.
271
272 @return 0 if success, error otherwise.
273 */
274 int sync_senders_details(THD *thd);
275
276 /**
277 It gets stored senders details for channel from
278 replication_asynchronous_connection_failover table, and then connects
279 to it. It also stores client connection object to all the connected
280 stores.
281
282 @param[in] thd The thread.
283 @param[in] channel_name The channel name.
284
285 @return false if success, true otherwise.
286 */
287 int connect_senders(THD *thd, const std::string &channel_name);
288
289 /**
290 It connects to server and runs a simple query.
291
292 @param[in] thd The thread.
293 @param[in] mi The pointer to the Master_info object.
294 @param[in] conn_detail std::tuple containing <channel, host, port,
295 network_namespace, weight, group_name>
296
297 @return true on success
298 false on failure like unable to connect or query fails
299 */
301 RPL_FAILOVER_SOURCE_TUPLE &conn_detail);
302
303 /**
304 It connects to each stored sender in connect_senders() and check for quorum
305 and group replication plugin enabled. It gets group membership list if
306 group replication plugin is enabled and its also has quorum.
307
308 @param[in] thd The thread.
309 @param[in] mi The pointer to the Master_info object.
310 @param[in] conn The Mysql_connection class object to query remote source.
311 @param[in] source_conn_detail std::tuple containing <channel, host, port,
312 network_namespace, weight, group_name,
313 primary_weight, secondary_weight>.
314 @param[out] group_membership_detail std::tuple containing <channel, host,
315 port, network_namespace, weight,
316 group_name>
317 @param[out] curr_highest_group_weight the highest weight of the source for
318 the group
319 @param[out] curr_conn_weight weight for current connected sender
320
321 @returns std::tuple<int, uint, bool, bool,
322 std::tuple<std::string, std::string, uint>> where each
323 element has following meaning:
324
325 first element of tuple is function return value and determines:
326 false Successful
327 true Error
328
329 second element of tuple determine if the current connected member
330 through asynchronous channel has changed the group.
331
332 third element of tuple determine if the current connected member
333 through asynchronous channel has lost quorum.
334
335 fourth element of tuple is also a tuple containing <channel, host,
336 port> of member who lost quorum. It is only useful
337 when fourth element of returned tuple is true.
338 */
339 std::tuple<int, bool, bool, std::tuple<std::string, std::string, uint>>
341 THD *thd, Master_info *mi, const Mysql_connection *conn,
342 SENDER_CONN_MERGE_TUPLE source_conn_detail,
343 std::vector<RPL_FAILOVER_SOURCE_TUPLE> &group_membership_detail,
344 uint &curr_highest_group_weight, uint &curr_conn_weight);
345
346 /**
347 Store gathered membership details to
348 replication_asynchronous_connection_failover table.
349
350 @param[in] channel_name The managed channel for which failover
351 is enabled.
352 @param[in] managed_name The group name UID value of the group.
353 @param[in] source_conn_list The list of std::tuple containing <channel,
354 host, port, network_namespace, weight,
355 managed_name>.
356
357 @return false if success, true otherwise.
358 */
360 std::string channel_name, std::string managed_name,
361 std::vector<RPL_FAILOVER_SOURCE_TUPLE> &source_conn_list);
362
363 /**
364 Delete provided row to the table with commit.
365
366 @param[in] table_op The Rpl_sys_table_access class object.
367 @param[in] table The table object.
368 @param[in] field_name The name of column/field of the table.
369 @param[in] conn_detail std::tuple containing <channel, host, port>
370
371 @returns std::tuple<bool, std::string> where each element has
372 following meaning:
373
374 first element of tuple is function return value and determines:
375 false Successful
376 true Error
377
378 second element of tuple is error message.
379 */
380 std::tuple<bool, std::string> delete_rows(
381 Rpl_sys_table_access &table_op, TABLE *table,
382 std::vector<std::string> field_name,
383 std::tuple<std::string, std::string, uint> conn_detail);
384
385 /**
386 Insert provided row to the table with commit.
387
388 @param[in] table_op The Rpl_sys_table_access class object.
389 @param[in] table The table object.
390 @param[in] field_name The name of column/field of the table.
391 @param[in] conn_detail std::tuple containing <channel, host, port,
392 network_namespace, weight, group_name>
393
394 @returns std::tuple<bool, std::string> where each element has
395 following meaning:
396
397 first element of tuple is function return value and determines:
398 false Successful
399 true Error
400
401 second element of tuple is error message.
402 */
403 std::tuple<bool, std::string> write_rows(
404 Rpl_sys_table_access &table_op, TABLE *table,
405 std::vector<std::string> field_name,
406 RPL_FAILOVER_SOURCE_TUPLE conn_detail);
407
408 /**
409 Checks if primary member has lost contact with majority
410
411 @return status
412 @retval true primary member has lost contact with majority
413 @retval false otherwise
414 */
416
417 /**
418 Gets the Json key for primary weight for the Configuration column of
419 replication_asynchronous_connection_failover_managed table.
420
421 @return the Json key for primary weight for the Configuration column of
422 replication_asynchronous_connection_failover_managed table.
423 */
424 const char *primary_weight_str() { return "Primary_weight"; }
425
426 /**
427 Gets the Json key for secondary weight for the Configuration column of
428 replication_asynchronous_connection_failover_managed table.
429
430 @return the Json key for secondary weight for the Configuration column of
431 replication_asynchronous_connection_failover_managed table.
432 */
433 const char *secondary_weight_str() { return "Secondary_weight"; }
434};
435#endif /* RPL_IO_MONITOR_H */
Definition: rpl_mi.h:87
Mysql client connection wrapper class to connect MySQL, execute SQL query and fetch query results.
Definition: rpl_mysql_connect.h:56
The class are wrappers for handler index and random scan functions to simplify their usage.
Definition: rpl_sys_table_access.h:45
Class that contains functionality to monitor group member's state, role and quorum changes on all the...
Definition: rpl_io_monitor.h:126
void source_monitor_handler()
The function started by Monitor IO thread which does monitor group member's state,...
Definition: rpl_io_monitor.cc:223
std::tuple< int, bool, bool, std::tuple< std::string, std::string, uint > > get_online_members(THD *thd, Master_info *mi, const Mysql_connection *conn, SENDER_CONN_MERGE_TUPLE source_conn_detail, std::vector< RPL_FAILOVER_SOURCE_TUPLE > &group_membership_detail, uint &curr_highest_group_weight, uint &curr_conn_weight)
It connects to each stored sender in connect_senders() and check for quorum and group replication plu...
Definition: rpl_io_monitor.cc:798
void set_monitoring_wait(uint wait_time)
Sets the delay between each iteration where it fetches group details.
Definition: rpl_io_monitor.cc:1080
bool has_primary_lost_contact_with_majority()
Checks if primary member has lost contact with majority.
Definition: rpl_io_monitor.cc:774
thread_state m_monitor_thd_state
Definition: rpl_io_monitor.h:250
std::tuple< bool, std::string > write_rows(Rpl_sys_table_access &table_op, TABLE *table, std::vector< std::string > field_name, RPL_FAILOVER_SOURCE_TUPLE conn_detail)
Insert provided row to the table with commit.
Definition: rpl_io_monitor.cc:317
my_thread_handle m_th
Definition: rpl_io_monitor.h:247
Source_IO_monitor & operator=(const Source_IO_monitor &)=delete
std::string get_query(enum_sql_query_tag qtag)
Gets the sql query string.
Definition: rpl_io_monitor.cc:164
std::tuple< bool, std::vector< SENDER_CONN_MERGE_TUPLE > > get_senders_details(const std::string &channel_name)
It gets stored senders details for channel from replication_asynchronous_connection_failover table.
Definition: rpl_io_monitor.cc:974
virtual ~Source_IO_monitor()
Definition: rpl_io_monitor.cc:180
THD * m_monitor_thd
Definition: rpl_io_monitor.h:232
Source_IO_monitor()
Definition: rpl_io_monitor.cc:170
uint m_retry_monitor_wait
Definition: rpl_io_monitor.h:238
int terminate_monitoring_process()
Terminate the Monitor IO thread.
Definition: rpl_io_monitor.cc:1032
bool m_abort_monitor
Definition: rpl_io_monitor.h:235
bool launch_monitoring_process(PSI_thread_key thread_key)
Creates and launches new Monitor IO thread.
Definition: rpl_io_monitor.cc:193
int connect_senders(THD *thd, const std::string &channel_name)
It gets stored senders details for channel from replication_asynchronous_connection_failover table,...
Definition: rpl_io_monitor.cc:337
Source_IO_monitor(const Source_IO_monitor &)=delete
bool m_primary_lost_contact_with_majority_warning_logged
Definition: rpl_io_monitor.h:252
int sync_senders_details(THD *thd)
It gets stored senders details for channel from replication_asynchronous_connection_failover table,...
Definition: rpl_io_monitor.cc:936
bool is_monitoring_process_running()
Gets the status of monitor IO thread whether its running.
Definition: rpl_io_monitor.cc:1086
mysql_cond_t m_run_cond
Definition: rpl_io_monitor.h:244
enum_res_col
Definition: rpl_io_monitor.h:255
@ COL_STATE
Definition: rpl_io_monitor.h:259
@ COL_ROLE
Definition: rpl_io_monitor.h:260
@ COL_GROUP_NAME
Definition: rpl_io_monitor.h:256
@ COL_HOST
Definition: rpl_io_monitor.h:257
@ COL_PORT
Definition: rpl_io_monitor.h:258
bool is_monitor_killed(THD *thd, Master_info *mi)
Check if Monitor IO thread is killed.
Definition: rpl_io_monitor.cc:186
int save_group_members(std::string channel_name, std::string managed_name, std::vector< RPL_FAILOVER_SOURCE_TUPLE > &source_conn_list)
Store gathered membership details to replication_asynchronous_connection_failover table.
Definition: rpl_io_monitor.cc:608
std::tuple< bool, std::string > delete_rows(Rpl_sys_table_access &table_op, TABLE *table, std::vector< std::string > field_name, std::tuple< std::string, std::string, uint > conn_detail)
Delete provided row to the table with commit.
Definition: rpl_io_monitor.cc:297
static Source_IO_monitor * get_instance()
Fetch Source_IO_monitor class instance.
Definition: rpl_io_monitor.cc:1090
uint get_monitoring_wait()
Gets the delay time between each iteration where it fetches group details.
Definition: rpl_io_monitor.cc:1084
mysql_mutex_t m_run_lock
Definition: rpl_io_monitor.h:241
const char * primary_weight_str()
Gets the Json key for primary weight for the Configuration column of replication_asynchronous_connect...
Definition: rpl_io_monitor.h:424
bool check_connection_and_run_query(THD *thd, Master_info *mi, RPL_FAILOVER_SOURCE_TUPLE &conn_detail)
It connects to server and runs a simple query.
Definition: rpl_io_monitor.cc:584
const char * secondary_weight_str()
Gets the Json key for secondary weight for the Configuration column of replication_asynchronous_conne...
Definition: rpl_io_monitor.h:433
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
unsigned int PSI_thread_key
Instrumented thread key.
Definition: psi_thread_bits.h:50
static int wait_time
Definition: mysql.cc:215
static PFS_engine_table_share_proxy table
Definition: pfs.cc:61
required string managed_name
Definition: replication_asynchronous_connection_failover.proto:36
std::tuple< std::string, std::string, uint, std::string, uint, std::string > RPL_FAILOVER_SOURCE_TUPLE
Definition: rpl_async_conn_failover_table_operations.h:43
enum_sql_query_tag
Definition: rpl_io_monitor.h:52
@ GR_MEMBER_ALL_DETAILS_FETCH_FOR_57
std::unique_ptr< Mysql_connection > MYSQL_CONN_PTR
Definition: rpl_io_monitor.h:38
std::tuple< std::string, std::string, uint > MYSQL_CONN_KEY
Definition: rpl_io_monitor.h:41
std::tuple< std::string, std::string, uint, std::string, uint, std::string, uint, uint > SENDER_CONN_MERGE_TUPLE
Connection managed tuple <channel, host, port, network_namespace, weight, managed_name,...
Definition: rpl_io_monitor.h:49
enum_conf_mode_quorum_status
Definition: rpl_io_monitor.h:61
Definition: table.h:1406
Definition: my_thread_bits.h:58
An instrumented cond structure.
Definition: mysql_cond_bits.h:50
An instrumented mutex structure.
Definition: mysql_mutex_bits.h:50
Definition: plugin_utils.h:48
bool is_thread_alive() const
Definition: rpl_io_monitor.h:112
thread_state()
Definition: rpl_io_monitor.h:87
bool is_initialized() const
Definition: rpl_io_monitor.h:99
void set_initialized()
Definition: rpl_io_monitor.h:95
bool is_thread_dead() const
Definition: rpl_io_monitor.h:117
thread_state_enum
Maintains thread status.
Definition: plugin_utils.h:53
@ THREAD_NONE
THREAD_NOT_CREATED.
Definition: plugin_utils.h:54
@ THREAD_RUNNING
THREAD_RUNNING.
Definition: plugin_utils.h:58
@ THREAD_TERMINATED
THREAD_EXIT.
Definition: plugin_utils.h:60
@ THREAD_INIT
THREAD_INIT.
Definition: plugin_utils.h:56
@ THREAD_END
END OF ENUM.
Definition: plugin_utils.h:61
@ THREAD_CREATED
THREAD_CREATED.
Definition: plugin_utils.h:55
void set_running()
Definition: rpl_io_monitor.h:89
bool is_running() const
Definition: rpl_io_monitor.h:104
void set_created()
Definition: rpl_io_monitor.h:97
thread_state_enum thread_state_var
Definition: plugin_utils.h:65
bool is_alive_not_running() const
Definition: rpl_io_monitor.h:108
void set_terminated()
Definition: rpl_io_monitor.h:91