1
/* Copyright (C) 2000-2003 MySQL AB
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
16
#include <my_global.h> // For HAVE_REPLICATION
17
#include "mysql_priv.h"
22
#ifdef HAVE_REPLICATION
24
#define DEFAULT_CONNECT_RETRY 60
26
// Defined in slave.cc
27
int init_intvar_from_file(int* var, IO_CACHE* f, int default_val);
28
int init_strvar_from_file(char *var, int max_size, IO_CACHE *f,
29
const char *default_val);
30
int init_floatvar_from_file(float* var, IO_CACHE* f, float default_val);
32
Master_info::Master_info()
33
:Slave_reporting_capability("I/O"),
34
ssl(0), ssl_verify_server_cert(0), fd(-1), io_thd(0), port(MYSQL_PORT),
35
connect_retry(DEFAULT_CONNECT_RETRY), heartbeat_period(0),
36
received_heartbeats(0), inited(0),
37
abort_slave(0), slave_running(0), slave_run_id(0)
39
host[0] = 0; user[0] = 0; password[0] = 0;
40
ssl_ca[0]= 0; ssl_capath[0]= 0; ssl_cert[0]= 0;
41
ssl_cipher[0]= 0; ssl_key[0]= 0;
43
bzero((char*) &file, sizeof(file));
44
pthread_mutex_init(&run_lock, MY_MUTEX_INIT_FAST);
45
pthread_mutex_init(&data_lock, MY_MUTEX_INIT_FAST);
46
pthread_cond_init(&data_cond, NULL);
47
pthread_cond_init(&start_cond, NULL);
48
pthread_cond_init(&stop_cond, NULL);
51
Master_info::~Master_info()
53
pthread_mutex_destroy(&run_lock);
54
pthread_mutex_destroy(&data_lock);
55
pthread_cond_destroy(&data_cond);
56
pthread_cond_destroy(&start_cond);
57
pthread_cond_destroy(&stop_cond);
61
void init_master_log_pos(Master_info* mi)
63
mi->master_log_name[0] = 0;
64
mi->master_log_pos = BIN_LOG_HEADER_SIZE; // skip magic number
66
always request heartbeat unless master_heartbeat_period is set
67
explicitly zero. Here is the default value for heartbeat period
68
if CHANGE MASTER did not specify it. (no data loss in conversion
69
as hb period has a max)
71
mi->heartbeat_period= (float) min(SLAVE_MAX_HEARTBEAT_PERIOD,
72
(slave_net_timeout/2.0));
73
assert(mi->heartbeat_period > (float) 0.001
74
|| mi->heartbeat_period == 0);
80
LINES_IN_MASTER_INFO_WITH_SSL= 14,
82
/* 5.1.16 added value of master_ssl_verify_server_cert */
83
LINE_FOR_MASTER_SSL_VERIFY_SERVER_CERT= 15,
85
/* 6.0 added value of master_heartbeat_period */
86
LINE_FOR_MASTER_HEARTBEAT_PERIOD= 16,
88
/* Number of lines currently used when saving master info file */
89
LINES_IN_MASTER_INFO= LINE_FOR_MASTER_HEARTBEAT_PERIOD
93
int init_master_info(Master_info* mi, const char* master_info_fname,
94
const char* slave_info_fname,
95
bool abort_if_no_master_info_file,
99
char fname[FN_REFLEN+128];
104
We have to reset read position of relay-log-bin as we may have
105
already been reading from 'hotlog' when the slave was stopped
106
last time. If this case pos_in_file would be set and we would
107
get a crash when trying to read the signature for the binary
110
We only rewind the read position if we are starting the SQL
111
thread. The handle_slave_sql thread assumes that the read
112
position is at the beginning of the file, and will read the
113
"signature" and then fast-forward to the last position read.
115
if (thread_mask & SLAVE_SQL)
117
my_b_seek(mi->rli.cur_log, (my_off_t) 0);
124
fn_format(fname, master_info_fname, mysql_data_home, "", 4+32);
127
We need a mutex while we are changing master info parameters to
128
keep other threads from reading bogus info
131
pthread_mutex_lock(&mi->data_lock);
134
/* does master.info exist ? */
136
if (access(fname,F_OK))
138
if (abort_if_no_master_info_file)
140
pthread_mutex_unlock(&mi->data_lock);
144
if someone removed the file from underneath our feet, just close
145
the old descriptor and re-create the old file
148
my_close(fd, MYF(MY_WME));
149
if ((fd = my_open(fname, O_CREAT|O_RDWR|O_BINARY, MYF(MY_WME))) < 0 )
151
sql_print_error("Failed to create a new master info file (file '%s', errno %d)", fname, my_errno);
154
if (init_io_cache(&mi->file, fd, IO_SIZE*2, READ_CACHE, 0L,0,
157
sql_print_error("Failed to create a cache on master info file (file '%s')", fname);
162
init_master_log_pos(mi);
168
reinit_io_cache(&mi->file, READ_CACHE, 0L,0,0);
171
if ((fd = my_open(fname, O_RDWR|O_BINARY, MYF(MY_WME))) < 0 )
173
sql_print_error("Failed to open the existing master info file (file '%s', errno %d)", fname, my_errno);
176
if (init_io_cache(&mi->file, fd, IO_SIZE*2, READ_CACHE, 0L,
179
sql_print_error("Failed to create a cache on master info file (file '%s')", fname);
185
int port, connect_retry, master_log_pos, lines;
186
int ssl= 0, ssl_verify_server_cert= 0;
187
float master_heartbeat_period= 0.0;
188
char *first_non_digit;
191
Starting from 4.1.x master.info has new format. Now its
192
first line contains number of lines in file. By reading this
193
number we will be always distinguish to which version our
194
master.info corresponds to. We can't simply count lines in
195
file since versions before 4.1.x could generate files with more
197
If first line doesn't contain a number or contain number less than
198
LINES_IN_MASTER_INFO_WITH_SSL then such file is treated like file
199
from pre 4.1.1 version.
200
There is no ambiguity when reading an old master.info, as before
201
4.1.1, the first line contained the binlog's name, which is either
202
empty or has an extension (contains a '.'), so can't be confused
205
So we're just reading first line and trying to figure which version
210
The first row is temporarily stored in mi->master_log_name,
211
if it is line count and not binlog name (new format) it will be
212
overwritten by the second row later.
214
if (init_strvar_from_file(mi->master_log_name,
215
sizeof(mi->master_log_name), &mi->file,
219
lines= strtoul(mi->master_log_name, &first_non_digit, 10);
221
if (mi->master_log_name[0]!='\0' &&
222
*first_non_digit=='\0' && lines >= LINES_IN_MASTER_INFO_WITH_SSL)
224
/* Seems to be new format => read master log name from next line */
225
if (init_strvar_from_file(mi->master_log_name,
226
sizeof(mi->master_log_name), &mi->file, ""))
232
if (init_intvar_from_file(&master_log_pos, &mi->file, 4) ||
233
init_strvar_from_file(mi->host, sizeof(mi->host), &mi->file, 0) ||
234
init_strvar_from_file(mi->user, sizeof(mi->user), &mi->file, "test") ||
235
init_strvar_from_file(mi->password, SCRAMBLED_PASSWORD_CHAR_LENGTH+1,
237
init_intvar_from_file(&port, &mi->file, MYSQL_PORT) ||
238
init_intvar_from_file(&connect_retry, &mi->file, DEFAULT_CONNECT_RETRY))
242
If file has ssl part use it even if we have server without
243
SSL support. But these option will be ignored later when
244
slave will try connect to master, so in this case warning
247
if (lines >= LINES_IN_MASTER_INFO_WITH_SSL)
249
if (init_intvar_from_file(&ssl, &mi->file, 0) ||
250
init_strvar_from_file(mi->ssl_ca, sizeof(mi->ssl_ca),
252
init_strvar_from_file(mi->ssl_capath, sizeof(mi->ssl_capath),
254
init_strvar_from_file(mi->ssl_cert, sizeof(mi->ssl_cert),
256
init_strvar_from_file(mi->ssl_cipher, sizeof(mi->ssl_cipher),
258
init_strvar_from_file(mi->ssl_key, sizeof(mi->ssl_key),
263
Starting from 5.1.16 ssl_verify_server_cert might be
266
if (lines >= LINE_FOR_MASTER_SSL_VERIFY_SERVER_CERT &&
267
init_intvar_from_file(&ssl_verify_server_cert, &mi->file, 0))
270
Starting from 6.0 master_heartbeat_period might be
273
if (lines >= LINE_FOR_MASTER_HEARTBEAT_PERIOD &&
274
init_floatvar_from_file(&master_heartbeat_period, &mi->file, 0.0))
279
sql_print_warning("SSL information in the master info file "
280
"('%s') are ignored because this MySQL slave was "
281
"compiled without SSL support.", fname);
284
This has to be handled here as init_intvar_from_file can't handle
287
mi->master_log_pos= (my_off_t) master_log_pos;
288
mi->port= (uint) port;
289
mi->connect_retry= (uint) connect_retry;
291
mi->ssl_verify_server_cert= ssl_verify_server_cert;
292
mi->heartbeat_period= master_heartbeat_period;
296
if (init_relay_log_info(&mi->rli, slave_info_fname))
300
// now change cache READ -> WRITE - must do this before flush_master_info
301
reinit_io_cache(&mi->file, WRITE_CACHE, 0L, 0, 1);
302
if ((error=test(flush_master_info(mi, 1))))
303
sql_print_error("Failed to flush master info file");
304
pthread_mutex_unlock(&mi->data_lock);
308
sql_print_error("Error reading master configuration");
313
my_close(fd, MYF(0));
314
end_io_cache(&mi->file);
317
pthread_mutex_unlock(&mi->data_lock);
324
2 - flush relay log failed
325
1 - flush master info failed
328
int flush_master_info(Master_info* mi, bool flush_relay_log_cache)
330
IO_CACHE* file = &mi->file;
334
Flush the relay log to disk. If we don't do it, then the relay log while
335
have some part (its last kilobytes) in memory only, so if the slave server
336
dies now, with, say, from master's position 100 to 150 in memory only (not
337
on disk), and with position 150 in master.info, then when the slave
338
restarts, the I/O thread will fetch binlogs from 150, so in the relay log
339
we will have "[0, 100] U [150, infinity[" and nobody will notice it, so the
340
SQL thread will jump from 100 to 150, and replication will silently break.
342
When we come to this place in code, relay log may or not be initialized;
343
the caller is responsible for setting 'flush_relay_log_cache' accordingly.
345
if (flush_relay_log_cache &&
346
flush_io_cache(mi->rli.relay_log.get_log_file()))
350
We flushed the relay log BEFORE the master.info file, because if we crash
351
now, we will get a duplicate event in the relay log at restart. If we
352
flushed in the other order, we would get a hole in the relay log.
353
And duplicate is better than hole (with a duplicate, in later versions we
354
can add detection and scrap one event; with a hole there's nothing we can
359
In certain cases this code may create master.info files that seems
360
corrupted, because of extra lines filled with garbage in the end
361
file (this happens if new contents take less space than previous
362
contents of file). But because of number of lines in the first line
363
of file we don't care about this garbage.
365
char heartbeat_buf[sizeof(mi->heartbeat_period) * 4]; // buffer to suffice always
366
sprintf(heartbeat_buf, "%.3f", mi->heartbeat_period);
369
"%u\n%s\n%s\n%s\n%s\n%s\n%d\n%d\n%d\n%s\n%s\n%s\n%s\n%s\n%d\n%s\n",
370
LINES_IN_MASTER_INFO,
371
mi->master_log_name, llstr(mi->master_log_pos, lbuf),
373
mi->password, mi->port, mi->connect_retry,
374
(int)(mi->ssl), mi->ssl_ca, mi->ssl_capath, mi->ssl_cert,
375
mi->ssl_cipher, mi->ssl_key, mi->ssl_verify_server_cert,
377
return(-flush_io_cache(file));
381
void end_master_info(Master_info* mi)
385
end_relay_log_info(&mi->rli);
388
end_io_cache(&mi->file);
389
(void)my_close(mi->fd, MYF(MY_WME));
398
#endif /* HAVE_REPLICATION */