slave.h 8.27 KB
Newer Older
1
/* Copyright (C) 2000-2003 MySQL AB
2

3 4
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
unknown's avatar
unknown committed
5
   the Free Software Foundation; version 2 of the License.
6

7 8 9 10
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
11

12 13 14 15
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

16 17 18
#ifndef SLAVE_H
#define SLAVE_H

unknown's avatar
unknown committed
19 20 21 22 23 24
/**
  @defgroup Replication Replication
  @{

  @file
*/
25 26 27
#ifdef HAVE_REPLICATION

#include "log.h"
28
#include "my_list.h"
unknown's avatar
unknown committed
29
#include "rpl_filter.h"
30
#include "rpl_tblmap.h"
31

unknown's avatar
unknown committed
32
#define SLAVE_NET_TIMEOUT  3600
33

34
#define MAX_SLAVE_ERROR    2000
35

36 37

// Forward declarations
38
class Relay_log_info;
39
class Master_info;
40

41

42 43 44 45 46 47 48 49 50
/*****************************************************************************

  MySQL Replication

  Replication is implemented via two types of threads:

    I/O Thread - One of these threads is started for each master server.
                 They maintain a connection to their master server, read log
                 events from the master as they arrive, and queues them into
51
                 a single, shared relay log file.  A Master_info 
52 53 54
                 represents each of these threads.

    SQL Thread - One of these threads is started and reads from the relay log
55
                 file, executing each event.  A Relay_log_info 
56 57 58 59 60 61 62 63
                 represents this thread.

  Buffering in the relay log file makes it unnecessary to reread events from
  a master server across a slave restart.  It also decouples the slave from
  the master where long-running updates and event logging are concerned--ie
  it can continue to log new events while a slow query executes on the slave.

*****************************************************************************/
unknown's avatar
unknown committed
64

65 66 67
/*
  MUTEXES in replication:

68 69 70 71 72 73 74 75 76 77
  LOCK_active_mi: [note: this was originally meant for multimaster, to switch
  from a master to another, to protect active_mi] It is used to SERIALIZE ALL
  administrative commands of replication: START SLAVE, STOP SLAVE, CHANGE
  MASTER, RESET SLAVE, end_slave() (when mysqld stops) [init_slave() does not
  need it it's called early]. Any of these commands holds the mutex from the
  start till the end. This thus protects us against a handful of deadlocks
  (consider start_slave_thread() which, when starting the I/O thread, releases
  mi->run_lock, keeps rli->run_lock, and tries to re-acquire mi->run_lock).

  Currently active_mi never moves (it's created at startup and deleted at
78
  shutdown, and not changed: it always points to the same Master_info struct),
79 80
  because we don't have multimaster. So for the moment, mi does not move, and
  mi->rli does not either.
81

82
  In Master_info: run_lock, data_lock
83 84
  run_lock protects all information about the run state: slave_running, thd
  and the existence of the I/O thread to stop/start it, you need this mutex).
85
  data_lock protects some moving members of the struct: counters (log name,
86
  position) and relay log (MYSQL_BIN_LOG object).
87

88
  In Relay_log_info: run_lock, data_lock
89
  see Master_info
90
  
91 92 93
  Order of acquisition: if you want to have LOCK_active_mi and a run_lock, you
  must acquire LOCK_active_mi first.

94
  In MYSQL_BIN_LOG: LOCK_log, LOCK_index of the binlog and the relay log
95 96 97 98
  LOCK_log: when you write to it. LOCK_index: when you create/delete a binlog
  (so that you have to update the .index file).
*/

99
extern ulong master_retry_count;
100 101
extern MY_BITMAP slave_error_mask;
extern bool use_slave_mask;
102 103 104
extern char *slave_load_tmpdir;
extern char *master_info_file, *relay_log_info_file;
extern char *opt_relay_logname, *opt_relaylog_index_name;
105 106
extern my_bool opt_skip_slave_start, opt_reckless_slave;
extern my_bool opt_log_slave_updates;
107
extern ulonglong relay_log_space_limit;
108

109
/*
110
  3 possible values for Master_info::slave_running and
111
  Relay_log_info::slave_running.
112 113 114
  The values 0,1,2 are very important: to keep the diff small, I didn't
  substitute places where we use 0/1 with the newly defined symbols. So don't change
  these values.
115
  The same way, code is assuming that in Relay_log_info we use only values
116 117 118 119 120 121 122 123
  0/1.
  I started with using an enum, but
  enum_variable=1; is not legal so would have required many line changes.
*/
#define MYSQL_SLAVE_NOT_RUN         0
#define MYSQL_SLAVE_RUN_NOT_CONNECT 1
#define MYSQL_SLAVE_RUN_CONNECT     2

124
#define RPL_LOG_NAME (rli->group_master_log_name[0] ? rli->group_master_log_name :\
125 126
 "FIRST")
#define IO_RPL_LOG_NAME (mi->master_log_name[0] ? mi->master_log_name :\
127 128
 "FIRST")

unknown's avatar
unknown committed
129 130 131 132 133
/*
  If the following is set, if first gives an error, second will be
  tried. Otherwise, if first fails, we fail.
*/
#define SLAVE_FORCE_ALL 4
134

135
int init_slave();
unknown's avatar
unknown committed
136
void init_slave_skip_errors(const char* arg);
137
bool flush_relay_log_info(Relay_log_info* rli);
138
int register_slave_on_master(MYSQL* mysql);
139
int terminate_slave_threads(Master_info* mi, int thread_mask,
140 141
			     bool skip_lock = 0);
int start_slave_threads(bool need_slave_mutex, bool wait_for_start,
142
			Master_info* mi, const char* master_info_fname,
143
			const char* slave_info_fname, int thread_mask);
unknown's avatar
unknown committed
144 145 146 147 148
/*
  cond_lock is usually same as start_lock. It is needed for the case when
  start_lock is 0 which happens if start_slave_thread() is called already
  inside the start_lock section, but at the same time we want a
  pthread_cond_wait() on start_cond,start_lock
149 150 151 152
*/
int start_slave_thread(pthread_handler h_func, pthread_mutex_t* start_lock,
		       pthread_mutex_t *cond_lock,
		       pthread_cond_t* start_cond,
153
		       volatile uint *slave_running,
unknown's avatar
unknown committed
154
		       volatile ulong *slave_run_id,
155
		       Master_info* mi,
156
                       bool high_priority);
157

unknown's avatar
unknown committed
158
/* If fd is -1, dump to NET */
159 160 161
int mysql_table_dump(THD* thd, const char* db,
		     const char* tbl_name, int fd = -1);

162
/* retrieve table from master and copy to slave*/
unknown's avatar
unknown committed
163
int fetch_master_table(THD* thd, const char* db_name, const char* table_name,
164
		       Master_info* mi, MYSQL* mysql, bool overwrite);
165

166
bool show_master_info(THD* thd, Master_info* mi);
unknown's avatar
unknown committed
167
bool show_binlog_info(THD* thd);
168
bool rpl_master_has_bug(Relay_log_info *rli, uint bug_id);
169

170
const char *print_slave_db_safe(const char *db);
171
int check_expected_error(THD* thd, Relay_log_info const *rli, int error_code);
172
void skip_load_data_infile(NET* net);
173

unknown's avatar
unknown committed
174
void end_slave(); /* clean up */
175 176 177
void clear_until_condition(Relay_log_info* rli);
void clear_slave_error(Relay_log_info* rli);
void end_relay_log_info(Relay_log_info* rli);
178 179 180
void lock_slave_threads(Master_info* mi);
void unlock_slave_threads(Master_info* mi);
void init_thread_mask(int* mask,Master_info* mi,bool inverse);
181
int init_relay_log_pos(Relay_log_info* rli,const char* log,ulonglong pos,
182 183
		       bool need_data_lock, const char** errmsg,
                       bool look_for_description_event);
184

185
int purge_relay_logs(Relay_log_info* rli, THD *thd, bool just_reset,
186
		     const char** errmsg);
187
void set_slave_thread_options(THD* thd);
188
void set_slave_thread_default_charset(THD *thd, Relay_log_info const *rli);
189
void rotate_relay_log(Master_info* mi);
190 191
int apply_event_and_update_pos(Log_event* ev, THD* thd, Relay_log_info* rli,
                               bool skip);
192

193 194
pthread_handler_t handle_slave_io(void *arg);
pthread_handler_t handle_slave_sql(void *arg);
195
extern bool volatile abort_loop;
196
extern Master_info main_mi, *active_mi; /* active_mi for multi-master */
197
extern LIST master_list;
198
extern my_bool replicate_same_server_id;
199

200
extern int disconnect_slave_event_count, abort_slave_event_count ;
unknown's avatar
unknown committed
201

unknown's avatar
unknown committed
202
/* the master variables are defaults read from my.cnf or command line */
203
extern uint master_port, master_connect_retry, report_port;
204 205 206
extern char * master_user, *master_password, *master_host;
extern char *master_info_file, *relay_log_info_file, *report_user;
extern char *report_host, *report_password;
207

unknown's avatar
unknown committed
208
extern my_bool master_ssl;
209 210
extern char *master_ssl_ca, *master_ssl_capath, *master_ssl_cert;
extern char *master_ssl_cipher, *master_ssl_key;
unknown's avatar
unknown committed
211
       
212 213
extern I_List<THD> threads;

214 215 216
#endif /* HAVE_REPLICATION */

/* masks for start/stop operations on io and sql slave threads */
217 218
#define SLAVE_IO  1
#define SLAVE_SQL 2
219

unknown's avatar
unknown committed
220 221 222
/**
  @} (end of group Replication)
*/
223

unknown's avatar
unknown committed
224
#endif