MySQL  8.0.18
Source Code Documentation
recovery_state_transfer.h
Go to the documentation of this file.
1 /* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
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 RECOVERY_STATE_TRANSFER_INCLUDE
24 #define RECOVERY_STATE_TRANSFER_INCLUDE
25 
27 #include <string>
28 #include <vector>
29 
30 #include "compression.h"
31 #include "my_io.h"
36 
38  public:
39  /**
40  Recovery state transfer constructor
41  @param recovery_channel_name The channel name to be used
42  @param member_uuid This member uuid
43  @param channel_obsr_mngr The channel state observer manager
44  */
46  const std::string &member_uuid,
47  Channel_observation_manager *channel_obsr_mngr);
48 
50 
51  // Base methods: init / abort / end
52 
53  /**
54  Initialize the state transfer class and reset the class flags
55 
56  @param rec_view_id The view id to use on this round
57  */
58  void initialize(const std::string &rec_view_id);
59 
60  /** Abort the state transfer */
61  void abort_state_transfer();
62 
63  /**
64  Signals that the data was received so the process can end.
65  */
66  void end_state_transfer();
67 
68  // Methods for variable updates
69 
70  /** Sets the number of times recovery tries to connect to a given donor */
73  }
74 
75  /** Sets the sleep time between connection attempts to all possible donors */
76  void set_recovery_donor_reconnect_interval(ulong reconnect_interval) {
77  donor_reconnect_interval = reconnect_interval;
78  }
79 
80  /**
81  Sets all the SSL option to use on recovery.
82 
83  @param use_ssl force the use of SSL on recovery connections
84  @param ssl_ca SSL trusted certificate authorities file
85  @param ssl_capath a directory with trusted CA files
86  @param ssl_cert the certificate file for secure connections
87  @param ssl_cipher the list of ciphers to use
88  @param ssl_key the SSL key file
89  @param ssl_crl SSL revocation list file
90  @param ssl_crlpath path with revocation list files
91  @param ssl_verify_server_cert verify the hostname against the certificate
92  */
93  void set_recovery_ssl_options(bool use_ssl, const char *ssl_ca,
94  const char *ssl_capath, const char *ssl_cert,
95  const char *ssl_cipher, const char *ssl_key,
96  const char *ssl_crl, const char *ssl_crlpath,
97  bool ssl_verify_server_cert) {
98  recovery_use_ssl = use_ssl;
99  if (ssl_ca != NULL) set_recovery_ssl_ca(ssl_ca);
100  if (ssl_capath != NULL) set_recovery_ssl_capath(ssl_capath);
101  if (ssl_cert != NULL) set_recovery_ssl_cert(ssl_cert);
102  if (ssl_cipher != NULL) set_recovery_ssl_cipher(ssl_cipher);
103  if (ssl_key != NULL) set_recovery_ssl_key(ssl_key);
104  if (ssl_crl != NULL) set_recovery_ssl_crl(ssl_crl);
105  if (ssl_crlpath != NULL) set_recovery_ssl_crl(ssl_crlpath);
106  recovery_ssl_verify_server_cert = ssl_verify_server_cert;
107  }
108 
109  /** Set the option that forces the use of SSL on recovery connections */
110  void set_recovery_use_ssl(char use_ssl) { this->recovery_use_ssl = use_ssl; }
111 
112  /** Set a SSL trusted certificate authorities file */
113  void set_recovery_ssl_ca(const char *ssl_ca) {
114  memcpy(recovery_ssl_ca, ssl_ca, strlen(ssl_ca) + 1);
115  }
116 
117  /** Set a folder with SSL trusted CA files */
118  void set_recovery_ssl_capath(const char *ssl_capath) {
119  memcpy(recovery_ssl_capath, ssl_capath, strlen(ssl_capath) + 1);
120  }
121 
122  /** Set a SSL certificate for connection */
123  void set_recovery_ssl_cert(const char *ssl_cert) {
124  memcpy(recovery_ssl_cert, ssl_cert, strlen(ssl_cert) + 1);
125  }
126 
127  /** Set a SSL ciphers to be used */
128  void set_recovery_ssl_cipher(const char *ssl_cipher) {
129  memcpy(recovery_ssl_cipher, ssl_cipher, strlen(ssl_cipher) + 1);
130  }
131 
132  /** Set a SSL key for connections */
133  void set_recovery_ssl_key(const char *ssl_key) {
134  memcpy(recovery_ssl_key, ssl_key, strlen(ssl_key) + 1);
135  }
136 
137  /** Set a SSL revocation list file*/
138  void set_recovery_ssl_crl(const char *ssl_crl) {
139  memcpy(recovery_ssl_crl, ssl_crl, strlen(ssl_crl) + 1);
140  }
141 
142  /** Set a folder with SSL revocation list files*/
143  void set_recovery_ssl_crlpath(const char *ssl_crlpath) {
144  memcpy(recovery_ssl_crlpath, ssl_crlpath, strlen(ssl_crlpath) + 1);
145  }
146 
147  /** Set if recovery shall compare the used hostname against the certificate */
148  void set_recovery_ssl_verify_server_cert(char ssl_verify_server_cert) {
149  this->recovery_ssl_verify_server_cert = ssl_verify_server_cert;
150  }
151 
152  /**
153  @return Is recovery configured to use SSL
154  */
155  bool get_recovery_use_ssl() { return this->recovery_use_ssl; }
156 
157  /**
158  Get SSL options configured for recovery
159 
160  @param[out] ssl_ca the ssl ca
161  @param[out] ssl_cert the ssl cert
162  @param[out] ssl_key the ssl key
163  */
164  void get_recovery_base_ssl_options(std::string *ssl_ca, std::string *ssl_cert,
165  std::string *ssl_key) {
166  ssl_ca->assign(recovery_ssl_ca);
167  ssl_cert->assign(recovery_ssl_cert);
168  ssl_key->assign(recovery_ssl_key);
169  }
170 
171  /**
172  Sets the recovery shutdown timeout.
173 
174  @param[in] timeout the timeout
175  */
176  void set_stop_wait_timeout(ulong timeout) {
178  }
179 
180  /** Set a public key file*/
181  void set_recovery_public_key_path(const char *public_key_path) {
182  if (public_key_path != NULL) {
183  memcpy(recovery_public_key_path, public_key_path,
184  strlen(public_key_path) + 1);
185  }
186  }
187 
188  /** Get preference to get public key */
190 
191  /** Set compression algorithm */
193  memcpy(recovery_compression_algorithm, name, strlen(name) + 1);
194  }
195 
196  /** Set compression level */
199  }
200 
201  // Methods that update the state transfer process
202 
203  /** This method initializes the group membership info */
204  void initialize_group_info();
205 
206  /**
207  This method decides what action to take when a member exits the group.
208  If the donor left, and the state transfer is still ongoing, then pick a
209  new one and restart the transfer.
210 
211  @param did_members_left states if members left the view
212 
213  @return the operation status
214  @retval 0 OK
215  @retval !=0 Error
216  */
217  int update_recovery_process(bool did_members_left);
218 
219  /**
220  Method that informs recovery that the donor channel applier was stopped.
221 
222  @param thread_id the applier thread id
223  @param aborted if the applier was aborted or stopped
224  */
225  void inform_of_applier_stop(my_thread_id thread_id, bool aborted);
226 
227  /**
228  Method that informs recovery that the donor channel receiver was stopped.
229 
230  @param thread_id the applier thread id
231  */
233 
234  // Status methods
235 
236  /**
237  Checks if the given id matches the recovery applier thread
238  @param id the thread id
239 
240  @return if it belongs to a thread
241  @retval true the id matches a SQL or worker thread
242  @retval false the id doesn't match any thread
243  */
245 
246  /**
247  Checks to see if the recovery IO/SQL thread is still running, probably caused
248  by an timeout on shutdown.
249  If the threads are still running, we try to stop them again.
250  If not possible, an error is reported.
251 
252  @return are the threads stopped
253  @retval 0 All is stopped.
254  @retval !=0 Threads are still running
255  */
257 
258  // class core method
259 
260  /**
261  Execute state transfer
262  @param stage_handler Stage handler to update the system tables
263 
264  @return the operation status
265  @retval 0 OK
266  @retval !=0 Recovery state transfer failed
267  */
268  int state_transfer(Plugin_stage_monitor_handler &stage_handler);
269 
270  private:
271  /**
272  Removes the old list of group members and enquires about the current members
273 
274  @param[in] update_donor update the selected donor pointer when updating
275  */
276  void update_group_membership(bool update_donor);
277 
278  /**
279  Based on the group list, build a random order list with all suitable donors.
280 
281  @param selected_donor the current selected donor to update its pointer
282  */
283  void build_donor_list(std::string *selected_donor);
284 
285  /** Method that sets the failover status to true and awakes recovery */
286  void donor_failover();
287 
288  /**
289  Establish a master/slave connection to the selected donor.
290 
291  @return the operation status
292  @retval 0 OK
293  @retval !=0 Error
294  */
296 
297  /**
298  Initializes the structures for the donor connection threads.
299  Recovery channel is always purged.
300 
301  @return the operation status
302  @retval 0 OK
303  @retval !=0 Error
304  */
306 
307  /**
308  Initializes the connection parameters for the donor connection.
309 
310  @return
311  @retval false Everything OK
312  @retval true In case of the selected donor is not available
313  */
315 
316  /**
317  Starts the recovery slave threads to receive data from the donor.
318 
319  @return the operation status
320  @retval 0 OK
321  @retval !=0 Error
322  */
324 
325  /**
326  Terminates the connection to the donor
327 
328  @param purge_logs purge recovery logs
329 
330  @return the operation status
331  @retval 0 OK
332  @retval !=0 Error
333  */
334  int terminate_recovery_slave_threads(bool purge_logs = true);
335 
336  /**
337  Purges relay logs and the master info object
338 
339  @return the operation status
340  @retval 0 OK
341  @retval REPLICATION_THREAD_REPOSITORY_RL_PURGE_ERROR
342  Error when purging the relay logs
343  @retval REPLICATION_THREAD_REPOSITORY_MI_PURGE_ERROR
344  Error when cleaning the master info repository
345  */
347 
348  private:
349  /* The member uuid*/
350  std::string member_uuid;
351  /* The associated view id for the current recovery session */
352  std::string view_id;
353 
354  /* The selected donor member*/
356  /* The selected donor member hostname */
358  /* Vector with group members info*/
359  std::vector<Group_member_info *> *group_members;
360  /* Member with suitable donors for use on recovery*/
361  std::vector<Group_member_info *> suitable_donors;
362 
363  /* Retry count on donor connections*/
365 
366  /* Recovery abort flag */
368  /* Flag that signals when the donor transfered all it's data */
370  /* Are we successfully connected to a donor*/
372  /* Are we on failover mode*/
374  /* Did an error happened in one of the threads*/
376 
377  // Recovery connection related structures
378 
379  /** Interface class to interact with the donor connection threads*/
381 
382  /* The plugin's control module for channel status observation */
384 
385  /* The recovery channel state observer */
387 
388  /** If the use of SSL is obligatory on recovery connections */
390  /** Get public key */
392  /** The configured SSL trusted certificate authorities file */
394  /** The configured directory that contains trusted SSL CA files*/
396  /** The configured SSL certificate file to use for a secure connection*/
398  /** The configured SSL list of permissible ciphers to use for encryption.*/
400  /** The configured SSL key file to use for establishing a secure connection.*/
402  /** The configured SSL file containing certificate revocation lists*/
404  /** The configured directory that contains certificate revocation list files*/
406  /** If the server's Common Name value checks against donor sent certificate.*/
408  /** Public key information */
410 
411  /* The lock for the recovery wait condition */
413  /* The condition for the recovery wait */
416 
417  /* Recovery max number of retries due to failures*/
419  /* Sleep time between connection attempts to all possible donors*/
421  /* compression algorithm to be used for communication */
423  /* compression level to be used for compression */
425 };
426 #endif /* RECOVERY_INCLUDE */
void inform_of_receiver_stop(my_thread_id thread_id)
Method that informs recovery that the donor channel receiver was stopped.
Definition: recovery_state_transfer.cc:136
Replication_thread_api donor_connection_interface
Interface class to interact with the donor connection threads.
Definition: recovery_state_transfer.h:380
static char recovery_channel_name[]
The relay log name.
Definition: recovery.cc:44
char recovery_compression_algorithm[COMPRESSION_ALGORITHM_NAME_LENGTH_MAX]
Definition: recovery_state_transfer.h:422
void set_recovery_ssl_cipher(const char *ssl_cipher)
Set a SSL ciphers to be used.
Definition: recovery_state_transfer.h:128
bool recovery_ssl_verify_server_cert
If the server&#39;s Common Name value checks against donor sent certificate.
Definition: recovery_state_transfer.h:407
void donor_failover()
Method that sets the failover status to true and awakes recovery.
Definition: recovery_state_transfer.cc:291
const string name("\ame\)
void set_recovery_donor_reconnect_interval(ulong reconnect_interval)
Sets the sleep time between connection attempts to all possible donors.
Definition: recovery_state_transfer.h:76
An instrumented cond structure.
Definition: mysql_cond_bits.h:49
bool recovery_aborted
Definition: recovery_state_transfer.h:367
long donor_connection_retry_count
Definition: recovery_state_transfer.h:364
void abort_state_transfer()
Abort the state transfer.
Definition: recovery_state_transfer.cc:198
void set_recovery_ssl_ca(const char *ssl_ca)
Set a SSL trusted certificate authorities file.
Definition: recovery_state_transfer.h:113
bool get_recovery_use_ssl()
Definition: recovery_state_transfer.h:155
std::vector< Group_member_info * > * group_members
Definition: recovery_state_transfer.h:359
Channel_state_observer * recovery_channel_observer
Definition: recovery_state_transfer.h:386
std::string view_id
Definition: recovery_state_transfer.h:352
char recovery_ssl_cert[FN_REFLEN]
The configured SSL certificate file to use for a secure connection.
Definition: recovery_state_transfer.h:397
A class to register observers for channel state events.
Definition: channel_observation_manager.h:130
void initialize_group_info()
This method initializes the group membership info.
Definition: recovery_state_transfer.cc:158
int check_recovery_thread_status()
Checks to see if the recovery IO/SQL thread is still running, probably caused by an timeout on shutd...
Definition: recovery_state_transfer.cc:301
std::string selected_donor_hostname
Definition: recovery_state_transfer.h:357
Definition: member_info.h:78
void end_state_transfer()
Signals that the data was received so the process can end.
Definition: recovery_state_transfer.cc:282
Group_member_info * selected_donor
Definition: recovery_state_transfer.h:355
char recovery_ssl_ca[FN_REFLEN]
The configured SSL trusted certificate authorities file.
Definition: recovery_state_transfer.h:393
bool is_own_event_channel(my_thread_id id)
Checks if the given id matches the recovery applier thread.
Definition: recovery_state_transfer.cc:312
char recovery_ssl_key[FN_REFLEN]
The configured SSL key file to use for establishing a secure connection.
Definition: recovery_state_transfer.h:401
void set_recovery_ssl_verify_server_cert(char ssl_verify_server_cert)
Set if recovery shall compare the used hostname against the certificate.
Definition: recovery_state_transfer.h:148
long donor_reconnect_interval
Definition: recovery_state_transfer.h:420
void set_recovery_ssl_crl(const char *ssl_crl)
Set a SSL revocation list file.
Definition: recovery_state_transfer.h:138
int terminate_recovery_slave_threads(bool purge_logs=true)
Terminates the connection to the donor.
Definition: recovery_state_transfer.cc:573
void inform_of_applier_stop(my_thread_id thread_id, bool aborted)
Method that informs recovery that the donor channel applier was stopped.
Definition: recovery_state_transfer.cc:110
bool connected_to_donor
Definition: recovery_state_transfer.h:371
void set_recovery_get_public_key(bool set)
Get preference to get public key.
Definition: recovery_state_transfer.h:189
int state_transfer(Plugin_stage_monitor_handler &stage_handler)
Execute state transfer.
Definition: recovery_state_transfer.cc:612
int purge_recovery_slave_threads_repos()
Purges relay logs and the master info object.
Definition: recovery_state_transfer.cc:594
char recovery_public_key_path[FN_REFLEN]
Public key information.
Definition: recovery_state_transfer.h:409
Definition: replication_threads_api.h:36
int establish_donor_connection()
Establish a master/slave connection to the selected donor.
Definition: recovery_state_transfer.cc:362
bool donor_channel_thread_error
Definition: recovery_state_transfer.h:375
bool donor_transfer_finished
Definition: recovery_state_transfer.h:369
bool recovery_use_ssl
If the use of SSL is obligatory on recovery connections.
Definition: recovery_state_transfer.h:389
void set_recovery_use_ssl(char use_ssl)
Set the option that forces the use of SSL on recovery connections.
Definition: recovery_state_transfer.h:110
unsigned int uint
Definition: uca-dump.cc:29
#define FN_REFLEN
Definition: my_io.h:82
void set_recovery_ssl_cert(const char *ssl_cert)
Set a SSL certificate for connection.
Definition: recovery_state_transfer.h:123
bool recovery_get_public_key
Get public key.
Definition: recovery_state_transfer.h:391
mysql_mutex_t recovery_lock
Definition: recovery_state_transfer.h:412
char recovery_ssl_capath[FN_REFLEN]
The configured directory that contains trusted SSL CA files.
Definition: recovery_state_transfer.h:395
void set_recovery_ssl_crlpath(const char *ssl_crlpath)
Set a folder with SSL revocation list files.
Definition: recovery_state_transfer.h:143
std::vector< Group_member_info * > suitable_donors
Definition: recovery_state_transfer.h:361
void set_recovery_ssl_key(const char *ssl_key)
Set a SSL key for connections.
Definition: recovery_state_transfer.h:133
void set_recovery_compression_algorithm(const char *name)
Set compression algorithm.
Definition: recovery_state_transfer.h:192
void set_recovery_zstd_compression_level(uint level)
Set compression level.
Definition: recovery_state_transfer.h:197
void initialize(const std::string &rec_view_id)
Initialize the state transfer class and reset the class flags.
Definition: recovery_state_transfer.cc:92
mysql_cond_t recovery_condition
Definition: recovery_state_transfer.h:414
Definition: recovery_state_transfer.h:37
char recovery_ssl_crl[FN_REFLEN]
The configured SSL file containing certificate revocation lists.
Definition: recovery_state_transfer.h:403
void set_recovery_donor_retry_count(ulong retry_count)
Sets the number of times recovery tries to connect to a given donor.
Definition: recovery_state_transfer.h:71
void set_stop_wait_timeout(ulong timeout)
Sets the threads shutdown timeout.
Definition: replication_threads_api.h:284
bool on_failover
Definition: recovery_state_transfer.h:373
Definition: stage_monitor_handler.h:29
~Recovery_state_transfer()
Definition: recovery_state_transfer.cc:76
mysql_mutex_t donor_selection_lock
Definition: recovery_state_transfer.h:415
uint32 my_thread_id
Definition: my_thread_local.h:33
int initialize_donor_connection()
Initializes the structures for the donor connection threads.
Definition: recovery_state_transfer.cc:459
void set_recovery_ssl_options(bool use_ssl, const char *ssl_ca, const char *ssl_capath, const char *ssl_cert, const char *ssl_cipher, const char *ssl_key, const char *ssl_crl, const char *ssl_crlpath, bool ssl_verify_server_cert)
Sets all the SSL option to use on recovery.
Definition: recovery_state_transfer.h:93
void set_recovery_ssl_capath(const char *ssl_capath)
Set a folder with SSL trusted CA files.
Definition: recovery_state_transfer.h:118
uint recovery_zstd_compression_level
Definition: recovery_state_transfer.h:424
Channel_observation_manager * channel_observation_manager
Definition: recovery_state_transfer.h:383
An instrumented mutex structure.
Definition: mysql_mutex_bits.h:49
void build_donor_list(std::string *selected_donor)
Based on the group list, build a random order list with all suitable donors.
Definition: recovery_state_transfer.cc:317
Common #defines and includes for file and socket I/O.
Recovery_state_transfer(char *recovery_channel_name, const std::string &member_uuid, Channel_observation_manager *channel_obsr_mngr)
Recovery state transfer constructor.
Definition: recovery_state_transfer.cc:38
#define NULL
Definition: types.h:55
long max_connection_attempts_to_donors
Definition: recovery_state_transfer.h:418
A interface class to code channel state response methods.
Definition: channel_observation_manager.h:37
void update_group_membership(bool update_donor)
Removes the old list of group members and enquires about the current members.
Definition: recovery_state_transfer.cc:169
void set_stop_wait_timeout(ulong timeout)
Sets the recovery shutdown timeout.
Definition: recovery_state_transfer.h:176
void get_recovery_base_ssl_options(std::string *ssl_ca, std::string *ssl_cert, std::string *ssl_key)
Get SSL options configured for recovery.
Definition: recovery_state_transfer.h:164
std::string member_uuid
Definition: recovery_state_transfer.h:350
char recovery_ssl_crlpath[FN_REFLEN]
The configured directory that contains certificate revocation list files.
Definition: recovery_state_transfer.h:405
int start_recovery_donor_threads()
Starts the recovery slave threads to receive data from the donor.
Definition: recovery_state_transfer.cc:498
#define COMPRESSION_ALGORITHM_NAME_LENGTH_MAX
Definition: compression.h:39
int update_recovery_process(bool did_members_left)
This method decides what action to take when a member exits the group.
Definition: recovery_state_transfer.cc:208
void set_recovery_public_key_path(const char *public_key_path)
Set a public key file.
Definition: recovery_state_transfer.h:181
unsigned long ulong
Definition: my_inttypes.h:48
bool initialize_connection_parameters()
Initializes the connection parameters for the donor connection.
char recovery_ssl_cipher[FN_REFLEN]
The configured SSL list of permissible ciphers to use for encryption.
Definition: recovery_state_transfer.h:399
static my_thread_id thread_id
Definition: my_thr_init.cc:62