14
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
16
16
#include <drizzled/server_includes.h>
19
#ifdef HAVE_REPLICATION
17
#include <drizzled/rpl_mi.h>
18
#include <drizzled/gettext.h>
19
#include <drizzled/slave.h>
20
#include <drizzled/data_home.h>
21
27
#define DEFAULT_CONNECT_RETRY 60
23
29
// Defined in slave.cc
24
30
int init_intvar_from_file(int* var, IO_CACHE* f, int default_val);
25
31
int init_strvar_from_file(char *var, int max_size, IO_CACHE *f,
26
const char *default_val);
32
const char *default_val);
27
33
int init_floatvar_from_file(float* var, IO_CACHE* f, float default_val);
29
35
Master_info::Master_info()
30
36
:Slave_reporting_capability("I/O"),
31
ssl(0), ssl_verify_server_cert(0), fd(-1), io_thd(0), port(DRIZZLE_PORT),
32
37
connect_retry(DEFAULT_CONNECT_RETRY), heartbeat_period(0),
33
38
received_heartbeats(0), inited(0),
34
39
abort_slave(0), slave_running(0), slave_run_id(0)
36
41
host[0] = 0; user[0] = 0; password[0] = 0;
37
ssl_ca[0]= 0; ssl_capath[0]= 0; ssl_cert[0]= 0;
38
ssl_cipher[0]= 0; ssl_key[0]= 0;
40
memset(&file, 0, sizeof(file));
41
45
pthread_mutex_init(&run_lock, MY_MUTEX_INIT_FAST);
42
46
pthread_mutex_init(&data_lock, MY_MUTEX_INIT_FAST);
43
47
pthread_cond_init(&data_cond, NULL);
54
58
pthread_cond_destroy(&stop_cond);
58
void init_master_log_pos(Master_info* mi)
60
mi->master_log_name[0] = 0;
61
mi->master_log_pos = BIN_LOG_HEADER_SIZE; // skip magic number
63
always request heartbeat unless master_heartbeat_period is set
64
explicitly zero. Here is the default value for heartbeat period
65
if CHANGE MASTER did not specify it. (no data loss in conversion
66
as hb period has a max)
68
mi->heartbeat_period= (float) min((double)SLAVE_MAX_HEARTBEAT_PERIOD,
69
(slave_net_timeout/2.0));
70
assert(mi->heartbeat_period > (float) 0.001
71
|| mi->heartbeat_period == 0);
77
LINES_IN_MASTER_INFO_WITH_SSL= 14,
79
/* 5.1.16 added value of master_ssl_verify_server_cert */
80
LINE_FOR_MASTER_SSL_VERIFY_SERVER_CERT= 15,
82
/* 6.0 added value of master_heartbeat_period */
83
LINE_FOR_MASTER_HEARTBEAT_PERIOD= 16,
85
/* Number of lines currently used when saving master info file */
86
LINES_IN_MASTER_INFO= LINE_FOR_MASTER_HEARTBEAT_PERIOD
90
int init_master_info(Master_info* mi, const char* master_info_fname,
91
const char* slave_info_fname,
92
bool abort_if_no_master_info_file,
96
char fname[FN_REFLEN+128];
61
bool Master_info::setPassword(const char *pword)
63
password.assign(pword);
68
const char *Master_info::getPassword()
70
return password.c_str();
73
bool Master_info::setUsername(const char *username)
75
user.assign(username);
80
const char *Master_info::getUsername()
85
bool Master_info::setHost(const char *hostname, uint16_t new_port)
87
host.assign(hostname);
93
const char *Master_info::getHostname()
98
uint16_t Master_info::getPort()
103
off_t Master_info::getLogPosition()
108
bool Master_info::setLogPosition(off_t position)
115
void Master_info::incrementLogPosition(off_t position)
120
const char *Master_info::getLogName()
122
return log_name.c_str();
125
bool Master_info::setLogName(const char *name)
127
log_name.assign(name);
132
uint32_t Master_info::getConnectionRetry()
134
return connect_retry;
137
bool Master_info::setConnectionRetry(uint32_t retry)
139
connect_retry= retry;
145
void Master_info::reset()
152
int Master_info::init_master_info(const char* master_info_fname,
153
const char* slave_info_fname,
101
161
We have to reset read position of relay-log-bin as we may have
112
172
if (thread_mask & SLAVE_SQL)
114
my_b_seek(mi->rli.cur_log, (my_off_t) 0);
174
my_b_seek(rli.cur_log, (my_off_t) 0);
121
fn_format(fname, master_info_fname, mysql_data_home, "", 4+32);
182
char fname[FN_REFLEN+128];
184
fn_format(fname, master_info_fname, mysql_data_home, "", 4+32);
185
info_filename.assign(fname);
124
189
We need a mutex while we are changing master info parameters to
125
190
keep other threads from reading bogus info
128
pthread_mutex_lock(&mi->data_lock);
193
pthread_mutex_lock(&data_lock);
131
195
/* does master.info exist ? */
133
if (access(fname,F_OK))
197
if (access(info_filename.c_str(), F_OK))
135
if (abort_if_no_master_info_file)
137
pthread_mutex_unlock(&mi->data_lock);
141
if someone removed the file from underneath our feet, just close
142
the old descriptor and re-create the old file
145
my_close(fd, MYF(MY_WME));
146
if ((fd = my_open(fname, O_CREAT|O_RDWR|O_BINARY, MYF(MY_WME))) < 0 )
148
sql_print_error(_("Failed to create a new master info file (file '%s', errno %d)"), fname, my_errno);
151
if (init_io_cache(&mi->file, fd, IO_SIZE*2, READ_CACHE, 0L,0,
154
sql_print_error(_("Failed to create a cache on master info file (file '%s')"), fname);
159
init_master_log_pos(mi);
199
drizzle::MasterList_Record *record;
203
/* Write new Master info file here (from info_filename) */
204
record= list.add_record();
205
record->set_hostname(host);
206
record->set_username(user);
207
record->set_password(password);
208
record->set_port(port);
209
record->set_connect_retry(connect_retry);
210
record->set_log_name(log_name);
211
record->set_log_position(log_pos);
213
fstream output(info_filename.c_str(), ios::out | ios::trunc | ios::binary);
214
if (!list.SerializeToOstream(&output))
162
220
else // file exists
165
reinit_io_cache(&mi->file, READ_CACHE, 0L,0,0);
168
if ((fd = my_open(fname, O_RDWR|O_BINARY, MYF(MY_WME))) < 0 )
170
sql_print_error(_("Failed to open the existing master info file (file '%s', errno %d)"), fname, my_errno);
173
if (init_io_cache(&mi->file, fd, IO_SIZE*2, READ_CACHE, 0L,
176
sql_print_error(_("Failed to create a cache on master info file (file '%s')"), fname);
182
int port, connect_retry, master_log_pos, lines;
183
int ssl= 0, ssl_verify_server_cert= 0;
184
float master_heartbeat_period= 0.0;
185
char *first_non_digit;
188
Starting from 4.1.x master.info has new format. Now its
189
first line contains number of lines in file. By reading this
190
number we will be always distinguish to which version our
191
master.info corresponds to. We can't simply count lines in
192
file since versions before 4.1.x could generate files with more
194
If first line doesn't contain a number or contain number less than
195
LINES_IN_MASTER_INFO_WITH_SSL then such file is treated like file
196
from pre 4.1.1 version.
197
There is no ambiguity when reading an old master.info, as before
198
4.1.1, the first line contained the binlog's name, which is either
199
empty or has an extension (contains a '.'), so can't be confused
202
So we're just reading first line and trying to figure which version
207
The first row is temporarily stored in mi->master_log_name,
208
if it is line count and not binlog name (new format) it will be
209
overwritten by the second row later.
211
if (init_strvar_from_file(mi->master_log_name,
212
sizeof(mi->master_log_name), &mi->file,
216
lines= strtoul(mi->master_log_name, &first_non_digit, 10);
218
if (mi->master_log_name[0]!='\0' &&
219
*first_non_digit=='\0' && lines >= LINES_IN_MASTER_INFO_WITH_SSL)
221
/* Seems to be new format => read master log name from next line */
222
if (init_strvar_from_file(mi->master_log_name,
223
sizeof(mi->master_log_name), &mi->file, ""))
229
if (init_intvar_from_file(&master_log_pos, &mi->file, 4) ||
230
init_strvar_from_file(mi->host, sizeof(mi->host), &mi->file, 0) ||
231
init_strvar_from_file(mi->user, sizeof(mi->user), &mi->file, "test") ||
232
init_strvar_from_file(mi->password, SCRAMBLED_PASSWORD_CHAR_LENGTH+1,
234
init_intvar_from_file(&port, &mi->file, DRIZZLE_PORT) ||
235
init_intvar_from_file(&connect_retry, &mi->file, DEFAULT_CONNECT_RETRY))
239
If file has ssl part use it even if we have server without
240
SSL support. But these option will be ignored later when
241
slave will try connect to master, so in this case warning
244
if (lines >= LINES_IN_MASTER_INFO_WITH_SSL)
246
if (init_intvar_from_file(&ssl, &mi->file, 0) ||
247
init_strvar_from_file(mi->ssl_ca, sizeof(mi->ssl_ca),
249
init_strvar_from_file(mi->ssl_capath, sizeof(mi->ssl_capath),
251
init_strvar_from_file(mi->ssl_cert, sizeof(mi->ssl_cert),
253
init_strvar_from_file(mi->ssl_cipher, sizeof(mi->ssl_cipher),
255
init_strvar_from_file(mi->ssl_key, sizeof(mi->ssl_key),
260
Starting from 5.1.16 ssl_verify_server_cert might be
263
if (lines >= LINE_FOR_MASTER_SSL_VERIFY_SERVER_CERT &&
264
init_intvar_from_file(&ssl_verify_server_cert, &mi->file, 0))
267
Starting from 6.0 master_heartbeat_period might be
270
if (lines >= LINE_FOR_MASTER_HEARTBEAT_PERIOD &&
271
init_floatvar_from_file(&master_heartbeat_period, &mi->file, 0.0))
276
sql_print_warning(_("SSL information in the master info file "
277
"('%s') are ignored because this MySQL slave was "
278
"compiled without SSL support."), fname);
281
This has to be handled here as init_intvar_from_file can't handle
284
mi->master_log_pos= (my_off_t) master_log_pos;
285
mi->port= (uint) port;
286
mi->connect_retry= (uint) connect_retry;
288
mi->ssl_verify_server_cert= ssl_verify_server_cert;
289
mi->heartbeat_period= master_heartbeat_period;
222
/* Read Master info file here (from info_filename) */
223
fstream input(info_filename.c_str(), ios::in | ios::binary);
224
if (!list.ParseFromIstream(&input))
230
/* We do not support multi-master just yet */
231
assert(list.record_size() == 1);
232
const drizzle::MasterList_Record record= list.record(0);
234
if (record.has_username())
235
user= record.username();
236
if (record.has_password())
237
password= record.password();
238
if (record.has_port())
240
if (record.has_connect_retry())
241
connect_retry= record.connect_retry();
242
if (record.has_log_name())
243
log_name= record.log_name();
244
if (record.has_log_position())
245
log_pos= record.log_position();
293
if (init_relay_log_info(&mi->rli, slave_info_fname))
249
if (init_relay_log_info(&rli, slave_info_fname))
297
// now change cache READ -> WRITE - must do this before flush_master_info
298
reinit_io_cache(&mi->file, WRITE_CACHE, 0L, 0, 1);
299
if ((error=test(flush_master_info(mi, 1))))
253
if ((error= test(flush())))
300
254
sql_print_error(_("Failed to flush master info file"));
301
pthread_mutex_unlock(&mi->data_lock);
255
pthread_mutex_unlock(&data_lock);
305
sql_print_error(_("Error reading master configuration"));
310
my_close(fd, MYF(0));
311
end_io_cache(&mi->file);
314
pthread_mutex_unlock(&mi->data_lock);
259
pthread_mutex_unlock(&data_lock);
339
281
When we come to this place in code, relay log may or not be initialized;
340
282
the caller is responsible for setting 'flush_relay_log_cache' accordingly.
342
if (flush_relay_log_cache &&
343
flush_io_cache(mi->rli.relay_log.get_log_file()))
347
We flushed the relay log BEFORE the master.info file, because if we crash
348
now, we will get a duplicate event in the relay log at restart. If we
349
flushed in the other order, we would get a hole in the relay log.
350
And duplicate is better than hole (with a duplicate, in later versions we
351
can add detection and scrap one event; with a hole there's nothing we can
356
In certain cases this code may create master.info files that seems
357
corrupted, because of extra lines filled with garbage in the end
358
file (this happens if new contents take less space than previous
359
contents of file). But because of number of lines in the first line
360
of file we don't care about this garbage.
362
char heartbeat_buf[sizeof(mi->heartbeat_period) * 4]; // buffer to suffice always
363
sprintf(heartbeat_buf, "%.3f", mi->heartbeat_period);
366
"%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",
367
LINES_IN_MASTER_INFO,
368
mi->master_log_name, llstr(mi->master_log_pos, lbuf),
370
mi->password, mi->port, mi->connect_retry,
371
(int)(mi->ssl), mi->ssl_ca, mi->ssl_capath, mi->ssl_cert,
372
mi->ssl_cipher, mi->ssl_key, mi->ssl_verify_server_cert,
374
return(-flush_io_cache(file));
285
/* Write Master info file here (from info_filename) */
286
assert(info_filename.length());
287
assert(list.record_size() == 1);
288
drizzle::MasterList_Record *record= list.mutable_record(0);
290
record->set_hostname(host);
291
record->set_username(user);
292
record->set_password(password);
293
record->set_port(port);
294
record->set_connect_retry(connect_retry);
295
record->set_log_name(log_name);
296
record->set_log_position(log_pos);
298
fstream output(info_filename.c_str(), ios::out | ios::trunc | ios::binary);
299
if (!list.SerializeToOstream(&output))
378
void end_master_info(Master_info* mi)
309
void Master_info::end_master_info()
382
end_relay_log_info(&mi->rli);
385
end_io_cache(&mi->file);
386
(void)my_close(mi->fd, MYF(MY_WME));
313
end_relay_log_info(&rli);
395
#endif /* HAVE_REPLICATION */