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 <drizzled/server_includes.h>
23
#define DEFAULT_CONNECT_RETRY 60
25
// Defined in slave.cc
26
int init_intvar_from_file(int* var, IO_CACHE* f, int default_val);
27
int init_strvar_from_file(char *var, int max_size, IO_CACHE *f,
28
const char *default_val);
29
int init_floatvar_from_file(float* var, IO_CACHE* f, float default_val);
31
Master_info::Master_info()
32
:Slave_reporting_capability("I/O"),
33
connect_retry(DEFAULT_CONNECT_RETRY), heartbeat_period(0),
34
received_heartbeats(0), inited(0),
35
abort_slave(0), slave_running(0), slave_run_id(0)
37
host[0] = 0; user[0] = 0; password[0] = 0;
41
pthread_mutex_init(&run_lock, MY_MUTEX_INIT_FAST);
42
pthread_mutex_init(&data_lock, MY_MUTEX_INIT_FAST);
43
pthread_cond_init(&data_cond, NULL);
44
pthread_cond_init(&start_cond, NULL);
45
pthread_cond_init(&stop_cond, NULL);
48
Master_info::~Master_info()
50
pthread_mutex_destroy(&run_lock);
51
pthread_mutex_destroy(&data_lock);
52
pthread_cond_destroy(&data_cond);
53
pthread_cond_destroy(&start_cond);
54
pthread_cond_destroy(&stop_cond);
57
bool Master_info::setPassword(const char *pword)
59
password.assign(pword);
64
const char *Master_info::getPassword()
66
return password.c_str();
69
bool Master_info::setUsername(const char *username)
71
user.assign(username);
76
const char *Master_info::getUsername()
81
bool Master_info::setHost(const char *hostname, uint16_t new_port)
83
host.assign(hostname);
89
const char *Master_info::getHostname()
94
uint16_t Master_info::getPort()
99
off_t Master_info::getLogPosition()
104
bool Master_info::setLogPosition(off_t position)
111
void Master_info::incrementLogPosition(off_t position)
116
const char *Master_info::getLogName()
118
return log_name.c_str();
121
bool Master_info::setLogName(const char *name)
123
log_name.assign(name);
128
uint32_t Master_info::getConnectionRetry()
130
return connect_retry;
133
bool Master_info::setConnectionRetry(uint32_t retry)
135
connect_retry= retry;
141
void Master_info::reset()
148
int Master_info::init_master_info(const char* master_info_fname,
149
const char* slave_info_fname,
157
We have to reset read position of relay-log-bin as we may have
158
already been reading from 'hotlog' when the slave was stopped
159
last time. If this case pos_in_file would be set and we would
160
get a crash when trying to read the signature for the binary
163
We only rewind the read position if we are starting the SQL
164
thread. The handle_slave_sql thread assumes that the read
165
position is at the beginning of the file, and will read the
166
"signature" and then fast-forward to the last position read.
168
if (thread_mask & SLAVE_SQL)
170
my_b_seek(rli.cur_log, (my_off_t) 0);
178
char fname[FN_REFLEN+128];
180
fn_format(fname, master_info_fname, mysql_data_home, "", 4+32);
181
info_filename.assign(fname);
185
We need a mutex while we are changing master info parameters to
186
keep other threads from reading bogus info
189
pthread_mutex_lock(&data_lock);
191
/* does master.info exist ? */
193
if (access(info_filename.c_str(), F_OK))
195
drizzle::MasterList_Record *record;
199
/* Write new Master info file here (from info_filename) */
200
record= list.add_record();
201
record->set_hostname(host);
202
record->set_username(user);
203
record->set_password(password);
204
record->set_port(port);
205
record->set_connect_retry(connect_retry);
206
record->set_log_name(log_name);
207
record->set_log_position(log_pos);
209
fstream output(info_filename.c_str(), ios::out | ios::trunc | ios::binary);
210
if (!list.SerializeToOstream(&output))
218
/* Read Master info file here (from info_filename) */
219
fstream input(info_filename.c_str(), ios::in | ios::binary);
220
if (!list.ParseFromIstream(&input))
226
/* We do not support multi-master just yet */
227
assert(list.record_size() == 1);
228
const drizzle::MasterList_Record record= list.record(0);
230
if (record.has_username())
231
user= record.username();
232
if (record.has_password())
233
password= record.password();
234
if (record.has_port())
236
if (record.has_connect_retry())
237
connect_retry= record.connect_retry();
238
if (record.has_log_name())
239
log_name= record.log_name();
240
if (record.has_log_position())
241
log_pos= record.log_position();
245
if (init_relay_log_info(&rli, slave_info_fname))
249
if ((error= test(flush())))
250
sql_print_error(_("Failed to flush master info file"));
251
pthread_mutex_unlock(&data_lock);
255
pthread_mutex_unlock(&data_lock);
262
2 - flush relay log failed
263
1 - flush master info failed
266
int Master_info::flush()
269
Flush the relay log to disk. If we don't do it, then the relay log while
270
have some part (its last kilobytes) in memory only, so if the slave server
271
dies now, with, say, from master's position 100 to 150 in memory only (not
272
on disk), and with position 150 in master.info, then when the slave
273
restarts, the I/O thread will fetch binlogs from 150, so in the relay log
274
we will have "[0, 100] U [150, infinity[" and nobody will notice it, so the
275
SQL thread will jump from 100 to 150, and replication will silently break.
277
When we come to this place in code, relay log may or not be initialized;
278
the caller is responsible for setting 'flush_relay_log_cache' accordingly.
281
/* Write Master info file here (from info_filename) */
282
assert(info_filename.length());
283
assert(list.record_size() == 1);
284
drizzle::MasterList_Record *record= list.mutable_record(0);
286
record->set_hostname(host);
287
record->set_username(user);
288
record->set_password(password);
289
record->set_port(port);
290
record->set_connect_retry(connect_retry);
291
record->set_log_name(log_name);
292
record->set_log_position(log_pos);
294
fstream output(info_filename.c_str(), ios::out | ios::trunc | ios::binary);
295
if (!list.SerializeToOstream(&output))
305
void Master_info::end_master_info()
309
end_relay_log_info(&rli);