~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to server/rpl_mi.cc

  • Committer: Brian Aker
  • Date: 2008-07-15 06:45:16 UTC
  • Revision ID: brian@tangent.org-20080715064516-fnbq7kowh7w57bxj
Merge Monty's code.

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
   along with this program; if not, write to the Free Software
14
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
15
 
16
 
#include <drizzled/server_includes.h>
 
16
#include <my_global.h> // For HAVE_REPLICATION
 
17
#include "mysql_priv.h"
 
18
#include <my_dir.h>
 
19
 
17
20
#include "rpl_mi.h"
18
 
#include <iostream>
19
 
#include <fstream>
20
21
 
21
 
using namespace std;
 
22
#ifdef HAVE_REPLICATION
22
23
 
23
24
#define DEFAULT_CONNECT_RETRY 60
24
25
 
30
31
 
31
32
Master_info::Master_info()
32
33
  :Slave_reporting_capability("I/O"),
 
34
   ssl(0), ssl_verify_server_cert(0), fd(-1),  io_thd(0), port(MYSQL_PORT),
33
35
   connect_retry(DEFAULT_CONNECT_RETRY), heartbeat_period(0),
34
36
   received_heartbeats(0), inited(0),
35
37
   abort_slave(0), slave_running(0), slave_run_id(0)
36
38
{
37
39
  host[0] = 0; user[0] = 0; password[0] = 0;
38
 
  io_thd= NULL;
39
 
  port= DRIZZLE_PORT;
 
40
  ssl_ca[0]= 0; ssl_capath[0]= 0; ssl_cert[0]= 0;
 
41
  ssl_cipher[0]= 0; ssl_key[0]= 0;
40
42
 
 
43
  bzero((char*) &file, sizeof(file));
41
44
  pthread_mutex_init(&run_lock, MY_MUTEX_INIT_FAST);
42
45
  pthread_mutex_init(&data_lock, MY_MUTEX_INIT_FAST);
43
46
  pthread_cond_init(&data_cond, NULL);
54
57
  pthread_cond_destroy(&stop_cond);
55
58
}
56
59
 
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)
 
60
 
 
61
void init_master_log_pos(Master_info* mi)
 
62
{
 
63
  mi->master_log_name[0] = 0;
 
64
  mi->master_log_pos = BIN_LOG_HEADER_SIZE;             // skip magic number
 
65
  /* 
 
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)
 
70
  */
 
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);
 
75
  return;
 
76
}
 
77
 
 
78
 
 
79
enum {
 
80
  LINES_IN_MASTER_INFO_WITH_SSL= 14,
 
81
 
 
82
  /* 5.1.16 added value of master_ssl_verify_server_cert */
 
83
  LINE_FOR_MASTER_SSL_VERIFY_SERVER_CERT= 15,
 
84
 
 
85
  /* 6.0 added value of master_heartbeat_period */
 
86
  LINE_FOR_MASTER_HEARTBEAT_PERIOD= 16,
 
87
 
 
88
  /* Number of lines currently used when saving master info file */
 
89
  LINES_IN_MASTER_INFO= LINE_FOR_MASTER_HEARTBEAT_PERIOD
 
90
};
 
91
 
 
92
 
 
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,
 
96
                     int thread_mask)
 
97
{
 
98
  int fd,error;
 
99
  char fname[FN_REFLEN+128];
 
100
 
 
101
  if (mi->inited)
155
102
  {
156
103
    /*
157
104
      We have to reset read position of relay-log-bin as we may have
167
114
    */
168
115
    if (thread_mask & SLAVE_SQL)
169
116
    {
170
 
      my_b_seek(rli.cur_log, (my_off_t) 0);
 
117
      my_b_seek(mi->rli.cur_log, (my_off_t) 0);
171
118
    }
172
119
    return(0);
173
120
  }
174
121
 
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
 
  }
 
122
  mi->mysql=0;
 
123
  mi->file_id=1;
 
124
  fn_format(fname, master_info_fname, mysql_data_home, "", 4+32);
183
125
 
184
126
  /*
185
127
    We need a mutex while we are changing master info parameters to
186
128
    keep other threads from reading bogus info
187
129
  */
188
130
 
189
 
  pthread_mutex_lock(&data_lock);
 
131
  pthread_mutex_lock(&mi->data_lock);
 
132
  fd = mi->fd;
190
133
 
191
134
  /* does master.info exist ? */
192
135
 
193
 
  if (access(info_filename.c_str(), F_OK))
 
136
  if (access(fname,F_OK))
194
137
  {
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
 
    }
 
138
    if (abort_if_no_master_info_file)
 
139
    {
 
140
      pthread_mutex_unlock(&mi->data_lock);
 
141
      return(0);
 
142
    }
 
143
    /*
 
144
      if someone removed the file from underneath our feet, just close
 
145
      the old descriptor and re-create the old file
 
146
    */
 
147
    if (fd >= 0)
 
148
      my_close(fd, MYF(MY_WME));
 
149
    if ((fd = my_open(fname, O_CREAT|O_RDWR|O_BINARY, MYF(MY_WME))) < 0 )
 
150
    {
 
151
      sql_print_error("Failed to create a new master info file (file '%s', errno %d)", fname, my_errno);
 
152
      goto err;
 
153
    }
 
154
    if (init_io_cache(&mi->file, fd, IO_SIZE*2, READ_CACHE, 0L,0,
 
155
                      MYF(MY_WME)))
 
156
    {
 
157
      sql_print_error("Failed to create a cache on master info file (file '%s')", fname);
 
158
      goto err;
 
159
    }
 
160
 
 
161
    mi->fd = fd;
 
162
    init_master_log_pos(mi);
 
163
 
215
164
  }
216
165
  else // file exists
217
166
  {
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();
 
167
    if (fd >= 0)
 
168
      reinit_io_cache(&mi->file, READ_CACHE, 0L,0,0);
 
169
    else
 
170
    {
 
171
      if ((fd = my_open(fname, O_RDWR|O_BINARY, MYF(MY_WME))) < 0 )
 
172
      {
 
173
        sql_print_error("Failed to open the existing master info file (file '%s', errno %d)", fname, my_errno);
 
174
        goto err;
 
175
      }
 
176
      if (init_io_cache(&mi->file, fd, IO_SIZE*2, READ_CACHE, 0L,
 
177
                        0, MYF(MY_WME)))
 
178
      {
 
179
        sql_print_error("Failed to create a cache on master info file (file '%s')", fname);
 
180
        goto err;
 
181
      }
 
182
    }
 
183
 
 
184
    mi->fd = fd;
 
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;
 
189
 
 
190
    /*
 
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
 
196
       lines than needed.
 
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
 
203
       with an integer.
 
204
 
 
205
       So we're just reading first line and trying to figure which version
 
206
       is this.
 
207
    */
 
208
 
 
209
    /*
 
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.
 
213
    */
 
214
    if (init_strvar_from_file(mi->master_log_name,
 
215
                              sizeof(mi->master_log_name), &mi->file,
 
216
                              ""))
 
217
      goto errwithmsg;
 
218
 
 
219
    lines= strtoul(mi->master_log_name, &first_non_digit, 10);
 
220
 
 
221
    if (mi->master_log_name[0]!='\0' &&
 
222
        *first_non_digit=='\0' && lines >= LINES_IN_MASTER_INFO_WITH_SSL)
 
223
    {
 
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, ""))
 
227
        goto errwithmsg;
 
228
    }
 
229
    else
 
230
      lines= 7;
 
231
 
 
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,
 
236
                              &mi->file, 0 ) ||
 
237
        init_intvar_from_file(&port, &mi->file, MYSQL_PORT) ||
 
238
        init_intvar_from_file(&connect_retry, &mi->file, DEFAULT_CONNECT_RETRY))
 
239
      goto errwithmsg;
 
240
 
 
241
    /*
 
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
 
245
      is printed.
 
246
    */
 
247
    if (lines >= LINES_IN_MASTER_INFO_WITH_SSL)
 
248
    {
 
249
      if (init_intvar_from_file(&ssl, &mi->file, 0) ||
 
250
          init_strvar_from_file(mi->ssl_ca, sizeof(mi->ssl_ca),
 
251
                                &mi->file, 0) ||
 
252
          init_strvar_from_file(mi->ssl_capath, sizeof(mi->ssl_capath),
 
253
                                &mi->file, 0) ||
 
254
          init_strvar_from_file(mi->ssl_cert, sizeof(mi->ssl_cert),
 
255
                                &mi->file, 0) ||
 
256
          init_strvar_from_file(mi->ssl_cipher, sizeof(mi->ssl_cipher),
 
257
                                &mi->file, 0) ||
 
258
          init_strvar_from_file(mi->ssl_key, sizeof(mi->ssl_key),
 
259
                               &mi->file, 0))
 
260
      goto errwithmsg;
 
261
 
 
262
      /*
 
263
        Starting from 5.1.16 ssl_verify_server_cert might be
 
264
        in the file
 
265
      */
 
266
      if (lines >= LINE_FOR_MASTER_SSL_VERIFY_SERVER_CERT &&
 
267
          init_intvar_from_file(&ssl_verify_server_cert, &mi->file, 0))
 
268
        goto errwithmsg;
 
269
      /*
 
270
        Starting from 6.0 master_heartbeat_period might be
 
271
        in the file
 
272
      */
 
273
      if (lines >= LINE_FOR_MASTER_HEARTBEAT_PERIOD &&
 
274
          init_floatvar_from_file(&master_heartbeat_period, &mi->file, 0.0))
 
275
        goto errwithmsg;
 
276
    }
 
277
 
 
278
    if (ssl)
 
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);
 
282
 
 
283
    /*
 
284
      This has to be handled here as init_intvar_from_file can't handle
 
285
      my_off_t types
 
286
    */
 
287
    mi->master_log_pos= (my_off_t) master_log_pos;
 
288
    mi->port= (uint) port;
 
289
    mi->connect_retry= (uint) connect_retry;
 
290
    mi->ssl= (bool) ssl;
 
291
    mi->ssl_verify_server_cert= ssl_verify_server_cert;
 
292
    mi->heartbeat_period= master_heartbeat_period;
242
293
  }
243
294
 
244
 
  rli.mi = this;
245
 
  if (init_relay_log_info(&rli, slave_info_fname))
 
295
  mi->rli.mi = mi;
 
296
  if (init_relay_log_info(&mi->rli, slave_info_fname))
246
297
    goto err;
247
298
 
248
 
  inited= 1;
249
 
  if ((error= test(flush())))
250
 
    sql_print_error(_("Failed to flush master info file"));
251
 
  pthread_mutex_unlock(&data_lock);
 
299
  mi->inited = 1;
 
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);
252
305
  return(error);
253
306
 
 
307
errwithmsg:
 
308
  sql_print_error("Error reading master configuration");
 
309
 
254
310
err:
255
 
  pthread_mutex_unlock(&data_lock);
256
 
  return 1;
 
311
  if (fd >= 0)
 
312
  {
 
313
    my_close(fd, MYF(0));
 
314
    end_io_cache(&mi->file);
 
315
  }
 
316
  mi->fd= -1;
 
317
  pthread_mutex_unlock(&mi->data_lock);
 
318
  return(1);
257
319
}
258
320
 
259
321
 
263
325
     1 - flush master info failed
264
326
     0 - all ok
265
327
*/
266
 
int Master_info::flush()
 
328
int flush_master_info(Master_info* mi, bool flush_relay_log_cache)
267
329
{
 
330
  IO_CACHE* file = &mi->file;
 
331
  char lbuf[22];
 
332
 
268
333
  /*
269
334
    Flush the relay log to disk. If we don't do it, then the relay log while
270
335
    have some part (its last kilobytes) in memory only, so if the slave server
277
342
    When we come to this place in code, relay log may or not be initialized;
278
343
    the caller is responsible for setting 'flush_relay_log_cache' accordingly.
279
344
  */
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;
 
345
  if (flush_relay_log_cache &&
 
346
      flush_io_cache(mi->rli.relay_log.get_log_file()))
 
347
    return(2);
 
348
 
 
349
  /*
 
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
 
355
    do).
 
356
  */
 
357
 
 
358
  /*
 
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.
 
364
  */
 
365
  char heartbeat_buf[sizeof(mi->heartbeat_period) * 4]; // buffer to suffice always
 
366
  my_sprintf(heartbeat_buf, (heartbeat_buf, "%.3f", mi->heartbeat_period));
 
367
  my_b_seek(file, 0L);
 
368
  my_b_printf(file,
 
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),
 
372
              mi->host, mi->user,
 
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,
 
376
              heartbeat_buf);
 
377
  return(-flush_io_cache(file));
302
378
}
303
379
 
304
380
 
305
 
void Master_info::end_master_info()
 
381
void end_master_info(Master_info* mi)
306
382
{
307
 
  if (!inited)
 
383
  if (!mi->inited)
308
384
    return;
309
 
  end_relay_log_info(&rli);
310
 
  inited = 0;
 
385
  end_relay_log_info(&mi->rli);
 
386
  if (mi->fd >= 0)
 
387
  {
 
388
    end_io_cache(&mi->file);
 
389
    (void)my_close(mi->fd, MYF(MY_WME));
 
390
    mi->fd = -1;
 
391
  }
 
392
  mi->inited = 0;
311
393
 
312
394
  return;
313
395
}
 
396
 
 
397
 
 
398
#endif /* HAVE_REPLICATION */