~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/rpl_mi.cc

  • Committer: Monty Taylor
  • Date: 2008-10-16 06:32:59 UTC
  • mfrom: (518 drizzle)
  • mto: (511.1.5 codestyle)
  • mto: This revision was merged to the branch mainline in revision 521.
  • Revision ID: monty@inaugust.com-20081016063259-fwbqogq7lnezct0l
Merged with trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2000-2003 MySQL AB
 
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
15
 
 
16
#include <drizzled/server_includes.h>
 
17
#include "rpl_mi.h"
 
18
#include <iostream>
 
19
#include <fstream>
 
20
 
 
21
using namespace std;
 
22
 
 
23
#define DEFAULT_CONNECT_RETRY 60
 
24
 
 
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);
 
30
 
 
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)
 
36
{
 
37
  host[0] = 0; user[0] = 0; password[0] = 0;
 
38
  io_thd= NULL;
 
39
  port= DRIZZLE_PORT;
 
40
 
 
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);
 
46
}
 
47
 
 
48
Master_info::~Master_info()
 
49
{
 
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);
 
55
}
 
56
 
 
57
bool Master_info::setPassword(const char *pword)
 
58
{
 
59
  password.assign(pword);
 
60
 
 
61
  return true;
 
62
}
 
63
 
 
64
const char *Master_info::getPassword()
 
65
{
 
66
  return password.c_str();
 
67
}
 
68
 
 
69
bool Master_info::setUsername(const char *username)
 
70
{
 
71
  user.assign(username);
 
72
 
 
73
  return true;
 
74
}
 
75
 
 
76
const char *Master_info::getUsername()
 
77
{
 
78
  return user.c_str();
 
79
}
 
80
 
 
81
bool Master_info::setHost(const char *hostname, uint16_t new_port)
 
82
{
 
83
  host.assign(hostname);
 
84
  port= new_port;
 
85
 
 
86
  return true;
 
87
}
 
88
 
 
89
const char *Master_info::getHostname()
 
90
{
 
91
  return host.c_str();
 
92
}
 
93
 
 
94
uint16_t Master_info::getPort()
 
95
{
 
96
  return port;
 
97
}
 
98
 
 
99
off_t Master_info::getLogPosition()
 
100
{
 
101
  return log_pos;
 
102
}
 
103
 
 
104
bool Master_info::setLogPosition(off_t position)
 
105
{
 
106
  log_pos= position;
 
107
 
 
108
  return true;
 
109
}
 
110
 
 
111
void Master_info::incrementLogPosition(off_t position)
 
112
{
 
113
  log_pos+= position;
 
114
}
 
115
 
 
116
const char *Master_info::getLogName()
 
117
{
 
118
  return log_name.c_str();
 
119
}
 
120
 
 
121
bool Master_info::setLogName(const char *name)
 
122
{
 
123
 log_name.assign(name);
 
124
 
 
125
  return true;
 
126
}
 
127
 
 
128
uint32_t Master_info::getConnectionRetry()
 
129
{
 
130
  return connect_retry;
 
131
}
 
132
 
 
133
bool Master_info::setConnectionRetry(uint32_t retry)
 
134
{
 
135
  connect_retry= retry;
 
136
 
 
137
  return true;
 
138
}
 
139
 
 
140
 
 
141
void Master_info::reset()
 
142
{
 
143
  log_name.clear();
 
144
  log_pos= 0; 
 
145
}
 
146
 
 
147
 
 
148
int Master_info::init_master_info(const char* master_info_fname,
 
149
                                  const char* slave_info_fname,
 
150
                                  int thread_mask)
 
151
{
 
152
  int error;
 
153
 
 
154
  if (inited)
 
155
  {
 
156
    /*
 
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
 
161
      relay log.
 
162
 
 
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.
 
167
    */
 
168
    if (thread_mask & SLAVE_SQL)
 
169
    {
 
170
      my_b_seek(rli.cur_log, (my_off_t) 0);
 
171
    }
 
172
    return(0);
 
173
  }
 
174
 
 
175
  drizzle= 0;
 
176
  file_id= 1;
 
177
  {
 
178
    char fname[FN_REFLEN+128];
 
179
 
 
180
    fn_format(fname, master_info_fname, mysql_data_home, "", 4+32);
 
181
    info_filename.assign(fname);
 
182
  }
 
183
 
 
184
  /*
 
185
    We need a mutex while we are changing master info parameters to
 
186
    keep other threads from reading bogus info
 
187
  */
 
188
 
 
189
  pthread_mutex_lock(&data_lock);
 
190
 
 
191
  /* does master.info exist ? */
 
192
 
 
193
  if (access(info_filename.c_str(), F_OK))
 
194
  {
 
195
    drizzle::MasterList_Record *record;
 
196
 
 
197
    reset();
 
198
 
 
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);
 
208
 
 
209
    fstream output(info_filename.c_str(), ios::out | ios::trunc | ios::binary);
 
210
    if (!list.SerializeToOstream(&output)) 
 
211
    { 
 
212
      assert(0);
 
213
      return -1;
 
214
    }
 
215
  }
 
216
  else // file exists
 
217
  {
 
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)) 
 
221
    {
 
222
      assert(0);
 
223
      return -1;
 
224
    }
 
225
 
 
226
    /* We do not support multi-master just yet */
 
227
    assert(list.record_size() == 1);
 
228
    const drizzle::MasterList_Record record= list.record(0);
 
229
 
 
230
    if (record.has_username())
 
231
      user= record.username();
 
232
    if (record.has_password())
 
233
      password= record.password();
 
234
    if (record.has_port())
 
235
      port= record.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();
 
242
  }
 
243
 
 
244
  rli.mi = this;
 
245
  if (init_relay_log_info(&rli, slave_info_fname))
 
246
    goto err;
 
247
 
 
248
  inited= 1;
 
249
  if ((error= test(flush())))
 
250
    sql_print_error(_("Failed to flush master info file"));
 
251
  pthread_mutex_unlock(&data_lock);
 
252
  return(error);
 
253
 
 
254
err:
 
255
  pthread_mutex_unlock(&data_lock);
 
256
  return 1;
 
257
}
 
258
 
 
259
 
 
260
/*
 
261
  RETURN
 
262
     2 - flush relay log failed
 
263
     1 - flush master info failed
 
264
     0 - all ok
 
265
*/
 
266
int Master_info::flush()
 
267
{
 
268
  /*
 
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.
 
276
 
 
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.
 
279
  */
 
280
 
 
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);
 
285
 
 
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);
 
293
 
 
294
  fstream output(info_filename.c_str(), ios::out | ios::trunc | ios::binary);
 
295
  if (!list.SerializeToOstream(&output)) 
 
296
  { 
 
297
    assert(0);
 
298
    return 1;
 
299
  }
 
300
 
 
301
  return 0;
 
302
}
 
303
 
 
304
 
 
305
void Master_info::end_master_info()
 
306
{
 
307
  if (!inited)
 
308
    return;
 
309
  end_relay_log_info(&rli);
 
310
  inited = 0;
 
311
 
 
312
  return;
 
313
}