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