MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
rpl_slave.h
Go to the documentation of this file.
1 /* Copyright (c) 2000, 2012, 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 as published by
5  the Free Software Foundation; version 2 of the License.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program; if not, write to the Free Software Foundation,
14  51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
15 
16 #ifndef RPL_SLAVE_H
17 #define RPL_SLAVE_H
18 
19 typedef enum { SLAVE_THD_IO, SLAVE_THD_SQL, SLAVE_THD_WORKER } SLAVE_THD_TYPE;
20 
24 #define MASTER_DELAY_MAX (0x7FFFFFFF)
25 #if INT_MAX < 0x7FFFFFFF
26 #error "don't support platforms where INT_MAX < 0x7FFFFFFF"
27 #endif
28 
44 #define SLAVE_MAX_HEARTBEAT_PERIOD 4294967
45 
46 #ifdef HAVE_REPLICATION
47 
48 #include "log.h"
49 #include "binlog.h"
50 #include "my_list.h"
51 #include "rpl_filter.h"
52 #include "rpl_tblmap.h"
53 
54 #define SLAVE_NET_TIMEOUT 3600
55 
56 #define MAX_SLAVE_ERROR 2000
57 
58 #define MTS_WORKER_UNDEF ((ulong) -1)
59 #define MTS_MAX_WORKERS 1024
60 
61 /*
62  When using tables to store the slave workers bitmaps,
63  we use a BLOB field. The maximum size of a BLOB is:
64 
65  2^16-1 = 65535 bytes => (2^16-1) * 8 = 524280 bits
66 */
67 #define MTS_MAX_BITS_IN_GROUP ((1L << 19) - 8) /* 524280 */
68 
69 // Forward declarations
70 class Relay_log_info;
71 class Master_info;
72 
73 extern bool server_id_supplied;
74 
75 /*****************************************************************************
76 
77  MySQL Replication
78 
79  Replication is implemented via two types of threads:
80 
81  I/O Thread - One of these threads is started for each master server.
82  They maintain a connection to their master server, read log
83  events from the master as they arrive, and queues them into
84  a single, shared relay log file. A Master_info
85  represents each of these threads.
86 
87  SQL Thread - One of these threads is started and reads from the relay log
88  file, executing each event. A Relay_log_info
89  represents this thread.
90 
91  Buffering in the relay log file makes it unnecessary to reread events from
92  a master server across a slave restart. It also decouples the slave from
93  the master where long-running updates and event logging are concerned--ie
94  it can continue to log new events while a slow query executes on the slave.
95 
96 *****************************************************************************/
97 
98 /*
99  MUTEXES in replication:
100 
101  LOCK_active_mi: [note: this was originally meant for multimaster, to switch
102  from a master to another, to protect active_mi] It is used to SERIALIZE ALL
103  administrative commands of replication: START SLAVE, STOP SLAVE, CHANGE
104  MASTER, RESET SLAVE, end_slave() (when mysqld stops) [init_slave() does not
105  need it it's called early]. Any of these commands holds the mutex from the
106  start till the end. This thus protects us against a handful of deadlocks
107  (consider start_slave_thread() which, when starting the I/O thread, releases
108  mi->run_lock, keeps rli->run_lock, and tries to re-acquire mi->run_lock).
109 
110  Currently active_mi never moves (it's created at startup and deleted at
111  shutdown, and not changed: it always points to the same Master_info struct),
112  because we don't have multimaster. So for the moment, mi does not move, and
113  mi->rli does not either.
114 
115  In Master_info: run_lock, data_lock
116  run_lock protects all information about the run state: slave_running, thd
117  and the existence of the I/O thread (to stop/start it, you need this mutex).
118  data_lock protects some moving members of the struct: counters (log name,
119  position) and relay log (MYSQL_BIN_LOG object).
120 
121  In Relay_log_info: run_lock, data_lock
122  see Master_info
123  However, note that run_lock does not protect
124  Relay_log_info.run_state; that is protected by data_lock.
125 
126  In MYSQL_BIN_LOG: LOCK_log, LOCK_index of the binlog and the relay log
127  LOCK_log: when you write to it. LOCK_index: when you create/delete a binlog
128  (so that you have to update the .index file).
129 
130  ==== Order of acquisition ====
131 
132  Here, we list most major functions that acquire multiple locks.
133 
134  Notation: For each function, we list the locks it takes, in the
135  order it takes them. If a function holds lock A while taking lock
136  B, then we write "A, B". If a function locks A, unlocks A, then
137  locks B, then we write "A | B". If function F1 invokes function F2,
138  then we write F2's name in parentheses in the list of locks for F1.
139 
140  show_master_info:
141  mi.data_lock, rli.data_lock, mi.err_lock, rli.err_lock
142 
143  stop_slave:
144  LOCK_active_mi,
145  ( mi.run_lock, thd.LOCK_thd_data
146  | rli.run_lock, thd.LOCK_thd_data
147  | relay.LOCK_log
148  )
149 
150  start_slave:
151  mi.run_lock, rli.run_lock, rli.data_lock, global_sid_lock->wrlock
152 
153  reset_logs:
154  LOCK_thread_count, .LOCK_log, .LOCK_index, global_sid_lock->wrlock
155 
156  purge_relay_logs:
157  rli.data_lock, (relay.reset_logs) LOCK_thread_count,
158  relay.LOCK_log, relay.LOCK_index, global_sid_lock->wrlock
159 
160  reset_master:
161  (binlog.reset_logs) LOCK_thread_count, binlog.LOCK_log,
162  binlog.LOCK_index, global_sid_lock->wrlock
163 
164  reset_slave:
165  mi.run_lock, rli.run_lock, (purge_relay_logs) rli.data_lock,
166  LOCK_thread_count, relay.LOCK_log, relay.LOCK_index,
167  global_sid_lock->wrlock
168 
169  purge_logs:
170  .LOCK_index, LOCK_thread_count, thd.linfo.lock
171 
172  [Note: purge_logs contains a known bug: LOCK_index should not be
173  taken before LOCK_thread_count. This implies that, e.g.,
174  purge_master_logs can deadlock with reset_master. However,
175  although purge_first_log and reset_slave take locks in reverse
176  order, they cannot deadlock because they both first acquire
177  rli.data_lock.]
178 
179  purge_master_logs, purge_master_logs_before_date, purge:
180  (binlog.purge_logs) binlog.LOCK_index, LOCK_thread_count, thd.linfo.lock
181 
182  purge_first_log:
183  rli.data_lock, relay.LOCK_index, rli.log_space_lock,
184  (relay.purge_logs) LOCK_thread_count, thd.linfo.lock
185 
186  MYSQL_BIN_LOG::new_file_impl:
187  .LOCK_log, .LOCK_index,
188  ( [ if binlog: LOCK_prep_xids ]
189  | global_sid_lock->wrlock
190  )
191 
192  rotate_relay_log:
193  (relay.new_file_impl) relay.LOCK_log, relay.LOCK_index,
194  global_sid_lock->wrlock
195 
196  kill_zombie_dump_threads:
197  LOCK_thread_count, thd.LOCK_thd_data
198 
199  init_relay_log_pos:
200  rli.data_lock, relay.log_lock
201 
202  rli_init_info:
203  rli.data_lock,
204  ( relay.log_lock
205  | global_sid_lock->wrlock
206  | (relay.open_binlog)
207  | (init_relay_log_pos) rli.data_lock, relay.log_lock
208  )
209 
210  change_master:
211  mi.run_lock, rli.run_lock, (init_relay_log_pos) rli.data_lock,
212  relay.log_lock
213 
214  So the DAG of lock acquisition order (not counting the buggy
215  purge_logs) is, empirically:
216 
217  LOCK_active_mi, mi.run_lock, rli.run_lock,
218  ( rli.data_lock,
219  ( LOCK_thread_count,
220  (
221  ( binlog.LOCK_log, binlog.LOCK_index
222  | relay.LOCK_log, relay.LOCK_index
223  ),
224  ( rli.log_space_lock | global_sid_lock->wrlock )
225  | binlog.LOCK_log, binlog.LOCK_index, LOCK_prep_xids
226  | thd.LOCK_data
227  )
228  | mi.err_lock, rli.err_lock
229  )
230  )
231  )
232  | mi.data_lock, rli.data_lock
233 */
234 
235 extern ulong master_retry_count;
236 extern MY_BITMAP slave_error_mask;
237 extern char slave_skip_error_names[];
238 extern bool use_slave_mask;
239 extern char *slave_load_tmpdir;
240 extern char *master_info_file, *relay_log_info_file;
241 extern char *opt_relay_logname, *opt_relaylog_index_name;
242 extern char *opt_binlog_index_name;
243 extern my_bool opt_skip_slave_start, opt_reckless_slave;
244 extern my_bool opt_log_slave_updates;
245 extern char *opt_slave_skip_errors;
246 extern ulonglong relay_log_space_limit;
247 
248 extern const char *relay_log_index;
249 extern const char *relay_log_basename;
250 
251 /*
252  3 possible values for Master_info::slave_running and
253  Relay_log_info::slave_running.
254  The values 0,1,2 are very important: to keep the diff small, I didn't
255  substitute places where we use 0/1 with the newly defined symbols. So don't change
256  these values.
257  The same way, code is assuming that in Relay_log_info we use only values
258  0/1.
259  I started with using an enum, but
260  enum_variable=1; is not legal so would have required many line changes.
261 */
262 #define MYSQL_SLAVE_NOT_RUN 0
263 #define MYSQL_SLAVE_RUN_NOT_CONNECT 1
264 #define MYSQL_SLAVE_RUN_CONNECT 2
265 
266 /*
267  If the following is set, if first gives an error, second will be
268  tried. Otherwise, if first fails, we fail.
269 */
270 #define SLAVE_FORCE_ALL 4
271 
272 int start_slave(THD* thd, Master_info* mi, bool net_report);
273 int stop_slave(THD* thd, Master_info* mi, bool net_report);
274 bool change_master(THD* thd, Master_info* mi);
275 int reset_slave(THD *thd, Master_info* mi);
276 int init_slave();
277 int init_recovery(Master_info* mi, const char** errmsg);
278 int global_init_info(Master_info* mi, bool ignore_if_no_info, int thread_mask);
279 void end_info(Master_info* mi);
280 int remove_info(Master_info* mi);
281 int flush_master_info(Master_info* mi, bool force);
282 void add_slave_skip_errors(const char* arg);
283 void set_slave_skip_errors(char** slave_skip_errors_ptr);
284 int register_slave_on_master(MYSQL* mysql);
285 int terminate_slave_threads(Master_info* mi, int thread_mask,
286  bool need_lock_term= true);
287 int start_slave_threads(bool need_lock_slave, bool wait_for_start,
288  Master_info* mi, int thread_mask);
289 /*
290  cond_lock is usually same as start_lock. It is needed for the case when
291  start_lock is 0 which happens if start_slave_thread() is called already
292  inside the start_lock section, but at the same time we want a
293  mysql_cond_wait() on start_cond, start_lock
294 */
295 int start_slave_thread(
296 #ifdef HAVE_PSI_INTERFACE
297  PSI_thread_key thread_key,
298 #endif
299  pthread_handler h_func,
300  mysql_mutex_t *start_lock,
301  mysql_mutex_t *cond_lock,
302  mysql_cond_t *start_cond,
303  volatile uint *slave_running,
304  volatile ulong *slave_run_id,
305  Master_info *mi);
306 
307 /* retrieve table from master and copy to slave*/
308 int fetch_master_table(THD* thd, const char* db_name, const char* table_name,
309  Master_info* mi, MYSQL* mysql, bool overwrite);
310 
311 bool show_slave_status(THD* thd, Master_info* mi);
312 bool rpl_master_has_bug(const Relay_log_info *rli, uint bug_id, bool report,
313  bool (*pred)(const void *), const void *param);
314 bool rpl_master_erroneous_autoinc(THD* thd);
315 
316 const char *print_slave_db_safe(const char *db);
317 void skip_load_data_infile(NET* net);
318 
319 void end_slave(); /* release slave threads */
320 void close_active_mi(); /* clean up slave threads data */
321 void clear_until_condition(Relay_log_info* rli);
322 void clear_slave_error(Relay_log_info* rli);
323 void lock_slave_threads(Master_info* mi);
324 void unlock_slave_threads(Master_info* mi);
325 void init_thread_mask(int* mask,Master_info* mi,bool inverse);
326 void set_slave_thread_options(THD* thd);
327 void set_slave_thread_default_charset(THD *thd, Relay_log_info const *rli);
328 int apply_event_and_update_pos(Log_event* ev, THD* thd, Relay_log_info* rli);
329 int rotate_relay_log(Master_info* mi);
330 
331 pthread_handler_t handle_slave_io(void *arg);
332 pthread_handler_t handle_slave_sql(void *arg);
333 bool net_request_file(NET* net, const char* fname);
334 
335 extern bool volatile abort_loop;
336 extern Master_info *active_mi; /* active_mi for multi-master */
337 extern LIST master_list;
338 extern my_bool replicate_same_server_id;
339 
340 extern int disconnect_slave_event_count, abort_slave_event_count ;
341 
342 /* the master variables are defaults read from my.cnf or command line */
343 extern uint master_port, master_connect_retry, report_port;
344 extern char * master_user, *master_password, *master_host;
345 extern char *master_info_file, *relay_log_info_file, *report_user;
346 extern char *report_host, *report_password;
347 
348 extern my_bool master_ssl;
349 extern char *master_ssl_ca, *master_ssl_capath, *master_ssl_cert;
350 extern char *master_ssl_cipher, *master_ssl_key;
351 
352 int mts_recovery_groups(Relay_log_info *rli);
353 bool mts_checkpoint_routine(Relay_log_info *rli, ulonglong period,
354  bool force, bool need_data_lock);
355 #endif /* HAVE_REPLICATION */
356 
357 /* masks for start/stop operations on io and sql slave threads */
358 #define SLAVE_IO 1
359 #define SLAVE_SQL 2
360 
364 #endif