1 /* Copyright (c) 2000, 2016, 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 14 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ 15 16 #ifndef SLAVE_H 17 #define SLAVE_H 18 19 /** 20 @defgroup Replication Replication 21 @{ 22 23 @file 24 */ 25 26 /** 27 Some of defines are need in parser even though replication is not 28 compiled in (embedded). 29 */ 30 31 /** 32 The maximum is defined as (ULONG_MAX/1000) with 4 bytes ulong 33 */ 34 #define SLAVE_MAX_HEARTBEAT_PERIOD 4294967 35 36 #ifdef HAVE_REPLICATION 37 38 #include "log.h" 39 #include "my_list.h" 40 #include "rpl_filter.h" 41 #include "rpl_tblmap.h" 42 43 #define SLAVE_NET_TIMEOUT 3600 44 45 #define MAX_SLAVE_ERROR 2000 46 47 // Forward declarations 48 class Relay_log_info; 49 class Master_info; 50 51 52 /***************************************************************************** 53 54 MySQL Replication 55 56 Replication is implemented via two types of threads: 57 58 I/O Thread - One of these threads is started for each master server. 59 They maintain a connection to their master server, read log 60 events from the master as they arrive, and queues them into 61 a single, shared relay log file. A Master_info 62 represents each of these threads. 63 64 SQL Thread - One of these threads is started and reads from the relay log 65 file, executing each event. A Relay_log_info 66 represents this thread. 67 68 Buffering in the relay log file makes it unnecessary to reread events from 69 a master server across a slave restart. It also decouples the slave from 70 the master where long-running updates and event logging are concerned--ie 71 it can continue to log new events while a slow query executes on the slave. 72 73 *****************************************************************************/ 74 75 /* 76 MUTEXES in replication: 77 78 LOCK_active_mi: [note: this was originally meant for multimaster, to switch 79 from a master to another, to protect active_mi] It is used to SERIALIZE ALL 80 administrative commands of replication: START SLAVE, STOP SLAVE, CHANGE 81 MASTER, RESET SLAVE, end_slave() (when mysqld stops) [init_slave() does not 82 need it it's called early]. Any of these commands holds the mutex from the 83 start till the end. This thus protects us against a handful of deadlocks 84 (consider start_slave_thread() which, when starting the I/O thread, releases 85 mi->run_lock, keeps rli->run_lock, and tries to re-acquire mi->run_lock). 86 87 Currently active_mi never moves (it's created at startup and deleted at 88 shutdown, and not changed: it always points to the same Master_info struct), 89 because we don't have multimaster. So for the moment, mi does not move, and 90 mi->rli does not either. 91 92 In Master_info: run_lock, data_lock 93 run_lock protects all information about the run state: slave_running, thd 94 and the existence of the I/O thread to stop/start it, you need this mutex). 95 data_lock protects some moving members of the struct: counters (log name, 96 position) and relay log (MYSQL_BIN_LOG object). 97 98 In Relay_log_info: run_lock, data_lock 99 see Master_info 100 101 Order of acquisition: if you want to have LOCK_active_mi and a run_lock, you 102 must acquire LOCK_active_mi first. 103 104 In MYSQL_BIN_LOG: LOCK_log, LOCK_index of the binlog and the relay log 105 LOCK_log: when you write to it. LOCK_index: when you create/delete a binlog 106 (so that you have to update the .index file). 107 */ 108 109 extern ulong master_retry_count; 110 extern MY_BITMAP slave_error_mask; 111 extern char slave_skip_error_names[]; 112 extern bool use_slave_mask; 113 extern char *slave_load_tmpdir; 114 extern char *master_info_file, *relay_log_info_file; 115 extern char *opt_relay_logname, *opt_relaylog_index_name; 116 extern my_bool opt_skip_slave_start, opt_reckless_slave; 117 extern my_bool opt_log_slave_updates; 118 extern char *opt_slave_skip_errors; 119 extern ulonglong relay_log_space_limit; 120 121 /* 122 3 possible values for Master_info::slave_running and 123 Relay_log_info::slave_running. 124 The values 0,1,2 are very important: to keep the diff small, I didn't 125 substitute places where we use 0/1 with the newly defined symbols. So don't change 126 these values. 127 The same way, code is assuming that in Relay_log_info we use only values 128 0/1. 129 I started with using an enum, but 130 enum_variable=1; is not legal so would have required many line changes. 131 */ 132 #define MYSQL_SLAVE_NOT_RUN 0 133 #define MYSQL_SLAVE_RUN_NOT_CONNECT 1 134 #define MYSQL_SLAVE_RUN_CONNECT 2 135 136 #define RPL_LOG_NAME (rli->group_master_log_name[0] ? rli->group_master_log_name :\ 137 "FIRST") 138 #define IO_RPL_LOG_NAME (mi->master_log_name[0] ? mi->master_log_name :\ 139 "FIRST") 140 141 /* 142 If the following is set, if first gives an error, second will be 143 tried. Otherwise, if first fails, we fail. 144 */ 145 #define SLAVE_FORCE_ALL 4 146 147 int init_slave(); 148 int init_recovery(Master_info* mi, const char** errmsg); 149 void init_slave_skip_errors(const char* arg); 150 bool flush_relay_log_info(Relay_log_info* rli); 151 int register_slave_on_master(MYSQL* mysql); 152 int terminate_slave_threads(Master_info* mi, int thread_mask, 153 bool skip_lock = 0); 154 int start_slave_threads(bool need_slave_mutex, bool wait_for_start, 155 Master_info* mi, const char* master_info_fname, 156 const char* slave_info_fname, int thread_mask); 157 /* 158 cond_lock is usually same as start_lock. It is needed for the case when 159 start_lock is 0 which happens if start_slave_thread() is called already 160 inside the start_lock section, but at the same time we want a 161 mysql_cond_wait() on start_cond, start_lock 162 */ 163 int start_slave_thread( 164 #ifdef HAVE_PSI_INTERFACE 165 PSI_thread_key thread_key, 166 #endif 167 pthread_handler h_func, 168 mysql_mutex_t *start_lock, 169 mysql_mutex_t *cond_lock, 170 mysql_cond_t *start_cond, 171 volatile uint *slave_running, 172 volatile ulong *slave_run_id, 173 Master_info *mi); 174 175 /* If fd is -1, dump to NET */ 176 int mysql_table_dump(THD* thd, const char* db, 177 const char* tbl_name, int fd = -1); 178 179 /* retrieve table from master and copy to slave*/ 180 int fetch_master_table(THD* thd, const char* db_name, const char* table_name, 181 Master_info* mi, MYSQL* mysql, bool overwrite); 182 183 bool show_master_info(THD* thd, Master_info* mi); 184 bool show_binlog_info(THD* thd); 185 bool rpl_master_has_bug(const Relay_log_info *rli, uint bug_id, bool report, 186 bool (*pred)(const void *), const void *param); 187 bool rpl_master_erroneous_autoinc(THD* thd); 188 189 const char *print_slave_db_safe(const char *db); 190 void skip_load_data_infile(NET* net); 191 192 void end_slave(); /* release slave threads */ 193 void close_active_mi(); /* clean up slave threads data */ 194 void clear_until_condition(Relay_log_info* rli); 195 void clear_slave_error(Relay_log_info* rli); 196 void end_relay_log_info(Relay_log_info* rli); 197 void lock_slave_threads(Master_info* mi); 198 void unlock_slave_threads(Master_info* mi); 199 void init_thread_mask(int* mask,Master_info* mi,bool inverse); 200 int init_relay_log_pos(Relay_log_info* rli,const char* log,ulonglong pos, 201 bool need_data_lock, const char** errmsg, 202 bool look_for_description_event); 203 204 int purge_relay_logs(Relay_log_info* rli, THD *thd, bool just_reset, 205 const char** errmsg); 206 void set_slave_thread_options(THD* thd); 207 void set_slave_thread_default_charset(THD *thd, Relay_log_info const *rli); 208 int rotate_relay_log(Master_info* mi); 209 int apply_event_and_update_pos(Log_event* ev, THD* thd, Relay_log_info* rli); 210 211 pthread_handler_t handle_slave_io(void *arg); 212 pthread_handler_t handle_slave_sql(void *arg); 213 bool net_request_file(NET* net, const char* fname); 214 215 extern bool volatile abort_loop; 216 extern Master_info main_mi, *active_mi; /* active_mi for multi-master */ 217 extern LIST master_list; 218 extern my_bool replicate_same_server_id; 219 220 extern int disconnect_slave_event_count, abort_slave_event_count ; 221 222 /* the master variables are defaults read from my.cnf or command line */ 223 extern uint master_port, master_connect_retry, report_port; 224 extern char * master_user, *master_password, *master_host; 225 extern char *master_info_file, *relay_log_info_file, *report_user; 226 extern char *report_host, *report_password; 227 228 extern my_bool master_ssl; 229 extern char *master_ssl_ca, *master_ssl_capath, *master_ssl_cert; 230 extern char *master_ssl_cipher, *master_ssl_key; 231 232 extern I_List<THD> threads; 233 234 #endif /* HAVE_REPLICATION */ 235 236 /* masks for start/stop operations on io and sql slave threads */ 237 #define SLAVE_IO 1 238 #define SLAVE_SQL 2 239 240 /** 241 @} (end of group Replication) 242 */ 243 244 #endif 245