~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to sql/rpl_mi.cc

  • Committer: Monty Taylor
  • Date: 2008-07-09 16:42:25 UTC
  • mto: (77.6.1 glibclient-merge)
  • mto: This revision was merged to the branch mainline in revision 112.
  • Revision ID: monty@inaugust.com-20080709164225-2r6n4j98nhxh031l
Moved test to tests... 

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>
17
 
#include <drizzled/replication/mi.h>
18
 
#include <drizzled/gettext.h>
19
 
#include <drizzled/slave.h>
20
 
#include <drizzled/data_home.h>
21
 
 
22
 
#include <iostream>
23
 
#include <fstream>
24
 
 
25
 
using namespace std;
 
16
#include <my_global.h> // For HAVE_REPLICATION
 
17
#include "mysql_priv.h"
 
18
#include <my_dir.h>
 
19
 
 
20
#include "rpl_mi.h"
 
21
 
 
22
#ifdef HAVE_REPLICATION
26
23
 
27
24
#define DEFAULT_CONNECT_RETRY 60
28
25
 
29
26
// Defined in slave.cc
30
27
int init_intvar_from_file(int* var, IO_CACHE* f, int default_val);
31
28
int init_strvar_from_file(char *var, int max_size, IO_CACHE *f,
32
 
                          const char *default_val);
 
29
                          const char *default_val);
33
30
int init_floatvar_from_file(float* var, IO_CACHE* f, float default_val);
34
31
 
35
32
Master_info::Master_info()
36
33
  :Slave_reporting_capability("I/O"),
 
34
   ssl(0), ssl_verify_server_cert(0), fd(-1),  io_thd(0), port(MYSQL_PORT),
37
35
   connect_retry(DEFAULT_CONNECT_RETRY), heartbeat_period(0),
38
36
   received_heartbeats(0), inited(0),
39
37
   abort_slave(0), slave_running(0), slave_run_id(0)
40
38
{
41
39
  host[0] = 0; user[0] = 0; password[0] = 0;
42
 
  io_session= NULL;
43
 
  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;
44
42
 
 
43
  bzero((char*) &file, sizeof(file));
45
44
  pthread_mutex_init(&run_lock, MY_MUTEX_INIT_FAST);
46
45
  pthread_mutex_init(&data_lock, MY_MUTEX_INIT_FAST);
47
46
  pthread_cond_init(&data_cond, NULL);
58
57
  pthread_cond_destroy(&stop_cond);
59
58
}
60
59
 
61
 
bool Master_info::setPassword(const char *pword)
62
 
{
63
 
  password.assign(pword);
64
 
 
65
 
  return true;
66
 
}
67
 
 
68
 
const char *Master_info::getPassword()
69
 
{
70
 
  return password.c_str();
71
 
}
72
 
 
73
 
bool Master_info::setUsername(const char *username)
74
 
{
75
 
  user.assign(username);
76
 
 
77
 
  return true;
78
 
}
79
 
 
80
 
const char *Master_info::getUsername()
81
 
{
82
 
  return user.c_str();
83
 
}
84
 
 
85
 
bool Master_info::setHost(const char *hostname, uint16_t new_port)
86
 
{
87
 
  host.assign(hostname);
88
 
  port= new_port;
89
 
 
90
 
  return true;
91
 
}
92
 
 
93
 
const char *Master_info::getHostname()
94
 
{
95
 
  return host.c_str();
96
 
}
97
 
 
98
 
uint16_t Master_info::getPort()
99
 
{
100
 
  return port;
101
 
}
102
 
 
103
 
off_t Master_info::getLogPosition()
104
 
{
105
 
  return log_pos;
106
 
}
107
 
 
108
 
bool Master_info::setLogPosition(off_t position)
109
 
{
110
 
  log_pos= position;
111
 
 
112
 
  return true;
113
 
}
114
 
 
115
 
void Master_info::incrementLogPosition(off_t position)
116
 
{
117
 
  log_pos+= position;
118
 
}
119
 
 
120
 
const char *Master_info::getLogName()
121
 
{
122
 
  return log_name.c_str();
123
 
}
124
 
 
125
 
bool Master_info::setLogName(const char *name)
126
 
{
127
 
 log_name.assign(name);
128
 
 
129
 
  return true;
130
 
}
131
 
 
132
 
uint32_t Master_info::getConnectionRetry()
133
 
{
134
 
  return connect_retry;
135
 
}
136
 
 
137
 
bool Master_info::setConnectionRetry(uint32_t retry)
138
 
{
139
 
  connect_retry= retry;
140
 
 
141
 
  return true;
142
 
}
143
 
 
144
 
 
145
 
void Master_info::reset()
146
 
{
147
 
  log_name.clear();
148
 
  log_pos= 0;
149
 
}
150
 
 
151
 
 
152
 
int Master_info::init_master_info(const char* master_info_fname,
153
 
                                  const char* slave_info_fname,
154
 
                                  int thread_mask)
155
 
{
156
 
  int error;
157
 
 
158
 
  if (inited)
 
60
 
 
61
void init_master_log_pos(Master_info* mi)
 
62
{
 
63
  DBUG_ENTER("init_master_log_pos");
 
64
 
 
65
  mi->master_log_name[0] = 0;
 
66
  mi->master_log_pos = BIN_LOG_HEADER_SIZE;             // skip magic number
 
67
  /* 
 
68
    always request heartbeat unless master_heartbeat_period is set
 
69
    explicitly zero.  Here is the default value for heartbeat period
 
70
    if CHANGE MASTER did not specify it.  (no data loss in conversion
 
71
    as hb period has a max)
 
72
  */
 
73
  mi->heartbeat_period= (float) min(SLAVE_MAX_HEARTBEAT_PERIOD,
 
74
                                    (slave_net_timeout/2.0));
 
75
  DBUG_ASSERT(mi->heartbeat_period > (float) 0.001
 
76
              || mi->heartbeat_period == 0);
 
77
  DBUG_VOID_RETURN;
 
78
}
 
79
 
 
80
 
 
81
enum {
 
82
  LINES_IN_MASTER_INFO_WITH_SSL= 14,
 
83
 
 
84
  /* 5.1.16 added value of master_ssl_verify_server_cert */
 
85
  LINE_FOR_MASTER_SSL_VERIFY_SERVER_CERT= 15,
 
86
 
 
87
  /* 6.0 added value of master_heartbeat_period */
 
88
  LINE_FOR_MASTER_HEARTBEAT_PERIOD= 16,
 
89
 
 
90
  /* Number of lines currently used when saving master info file */
 
91
  LINES_IN_MASTER_INFO= LINE_FOR_MASTER_HEARTBEAT_PERIOD
 
92
};
 
93
 
 
94
 
 
95
int init_master_info(Master_info* mi, const char* master_info_fname,
 
96
                     const char* slave_info_fname,
 
97
                     bool abort_if_no_master_info_file,
 
98
                     int thread_mask)
 
99
{
 
100
  int fd,error;
 
101
  char fname[FN_REFLEN+128];
 
102
  DBUG_ENTER("init_master_info");
 
103
 
 
104
  if (mi->inited)
159
105
  {
160
106
    /*
161
107
      We have to reset read position of relay-log-bin as we may have
171
117
    */
172
118
    if (thread_mask & SLAVE_SQL)
173
119
    {
174
 
      my_b_seek(rli.cur_log, (my_off_t) 0);
 
120
      my_b_seek(mi->rli.cur_log, (my_off_t) 0);
175
121
    }
176
 
    return(0);
177
 
  }
178
 
 
179
 
  drizzle= 0;
180
 
  file_id= 1;
181
 
  {
182
 
    char fname[FN_REFLEN+128];
183
 
 
184
 
    fn_format(fname, master_info_fname, drizzle_data_home, "", 4+32);
185
 
    info_filename.assign(fname);
186
 
  }
 
122
    DBUG_RETURN(0);
 
123
  }
 
124
 
 
125
  mi->mysql=0;
 
126
  mi->file_id=1;
 
127
  fn_format(fname, master_info_fname, mysql_data_home, "", 4+32);
187
128
 
188
129
  /*
189
130
    We need a mutex while we are changing master info parameters to
190
131
    keep other threads from reading bogus info
191
132
  */
192
133
 
193
 
  pthread_mutex_lock(&data_lock);
 
134
  pthread_mutex_lock(&mi->data_lock);
 
135
  fd = mi->fd;
194
136
 
195
137
  /* does master.info exist ? */
196
138
 
197
 
  if (access(info_filename.c_str(), F_OK))
 
139
  if (access(fname,F_OK))
198
140
  {
199
 
    drizzle::MasterList_Record *record;
200
 
 
201
 
    reset();
202
 
 
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);
212
 
 
213
 
    fstream output(info_filename.c_str(), ios::out | ios::trunc | ios::binary);
214
 
    if (!list.SerializeToOstream(&output))
215
 
    {
216
 
      assert(0);
217
 
      return -1;
218
 
    }
 
141
    if (abort_if_no_master_info_file)
 
142
    {
 
143
      pthread_mutex_unlock(&mi->data_lock);
 
144
      DBUG_RETURN(0);
 
145
    }
 
146
    /*
 
147
      if someone removed the file from underneath our feet, just close
 
148
      the old descriptor and re-create the old file
 
149
    */
 
150
    if (fd >= 0)
 
151
      my_close(fd, MYF(MY_WME));
 
152
    if ((fd = my_open(fname, O_CREAT|O_RDWR|O_BINARY, MYF(MY_WME))) < 0 )
 
153
    {
 
154
      sql_print_error("Failed to create a new master info file (file '%s', errno %d)", fname, my_errno);
 
155
      goto err;
 
156
    }
 
157
    if (init_io_cache(&mi->file, fd, IO_SIZE*2, READ_CACHE, 0L,0,
 
158
                      MYF(MY_WME)))
 
159
    {
 
160
      sql_print_error("Failed to create a cache on master info file (file '%s')", fname);
 
161
      goto err;
 
162
    }
 
163
 
 
164
    mi->fd = fd;
 
165
    init_master_log_pos(mi);
 
166
 
219
167
  }
220
168
  else // file exists
221
169
  {
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))
225
 
    {
226
 
      assert(0);
227
 
      return -1;
228
 
    }
229
 
 
230
 
    /* We do not support multi-master just yet */
231
 
    assert(list.record_size() == 1);
232
 
    const drizzle::MasterList_Record record= list.record(0);
233
 
 
234
 
    if (record.has_username())
235
 
      user= record.username();
236
 
    if (record.has_password())
237
 
      password= record.password();
238
 
    if (record.has_port())
239
 
      port= record.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();
 
170
    if (fd >= 0)
 
171
      reinit_io_cache(&mi->file, READ_CACHE, 0L,0,0);
 
172
    else
 
173
    {
 
174
      if ((fd = my_open(fname, O_RDWR|O_BINARY, MYF(MY_WME))) < 0 )
 
175
      {
 
176
        sql_print_error("Failed to open the existing master info file (file '%s', errno %d)", fname, my_errno);
 
177
        goto err;
 
178
      }
 
179
      if (init_io_cache(&mi->file, fd, IO_SIZE*2, READ_CACHE, 0L,
 
180
                        0, MYF(MY_WME)))
 
181
      {
 
182
        sql_print_error("Failed to create a cache on master info file (file '%s')", fname);
 
183
        goto err;
 
184
      }
 
185
    }
 
186
 
 
187
    mi->fd = fd;
 
188
    int port, connect_retry, master_log_pos, lines;
 
189
    int ssl= 0, ssl_verify_server_cert= 0;
 
190
    float master_heartbeat_period= 0.0;
 
191
    char *first_non_digit;
 
192
 
 
193
    /*
 
194
       Starting from 4.1.x master.info has new format. Now its
 
195
       first line contains number of lines in file. By reading this
 
196
       number we will be always distinguish to which version our
 
197
       master.info corresponds to. We can't simply count lines in
 
198
       file since versions before 4.1.x could generate files with more
 
199
       lines than needed.
 
200
       If first line doesn't contain a number or contain number less than
 
201
       LINES_IN_MASTER_INFO_WITH_SSL then such file is treated like file
 
202
       from pre 4.1.1 version.
 
203
       There is no ambiguity when reading an old master.info, as before
 
204
       4.1.1, the first line contained the binlog's name, which is either
 
205
       empty or has an extension (contains a '.'), so can't be confused
 
206
       with an integer.
 
207
 
 
208
       So we're just reading first line and trying to figure which version
 
209
       is this.
 
210
    */
 
211
 
 
212
    /*
 
213
       The first row is temporarily stored in mi->master_log_name,
 
214
       if it is line count and not binlog name (new format) it will be
 
215
       overwritten by the second row later.
 
216
    */
 
217
    if (init_strvar_from_file(mi->master_log_name,
 
218
                              sizeof(mi->master_log_name), &mi->file,
 
219
                              ""))
 
220
      goto errwithmsg;
 
221
 
 
222
    lines= strtoul(mi->master_log_name, &first_non_digit, 10);
 
223
 
 
224
    if (mi->master_log_name[0]!='\0' &&
 
225
        *first_non_digit=='\0' && lines >= LINES_IN_MASTER_INFO_WITH_SSL)
 
226
    {
 
227
      /* Seems to be new format => read master log name from next line */
 
228
      if (init_strvar_from_file(mi->master_log_name,
 
229
            sizeof(mi->master_log_name), &mi->file, ""))
 
230
        goto errwithmsg;
 
231
    }
 
232
    else
 
233
      lines= 7;
 
234
 
 
235
    if (init_intvar_from_file(&master_log_pos, &mi->file, 4) ||
 
236
        init_strvar_from_file(mi->host, sizeof(mi->host), &mi->file, 0) ||
 
237
        init_strvar_from_file(mi->user, sizeof(mi->user), &mi->file, "test") ||
 
238
        init_strvar_from_file(mi->password, SCRAMBLED_PASSWORD_CHAR_LENGTH+1,
 
239
                              &mi->file, 0 ) ||
 
240
        init_intvar_from_file(&port, &mi->file, MYSQL_PORT) ||
 
241
        init_intvar_from_file(&connect_retry, &mi->file, DEFAULT_CONNECT_RETRY))
 
242
      goto errwithmsg;
 
243
 
 
244
    /*
 
245
      If file has ssl part use it even if we have server without
 
246
      SSL support. But these option will be ignored later when
 
247
      slave will try connect to master, so in this case warning
 
248
      is printed.
 
249
    */
 
250
    if (lines >= LINES_IN_MASTER_INFO_WITH_SSL)
 
251
    {
 
252
      if (init_intvar_from_file(&ssl, &mi->file, 0) ||
 
253
          init_strvar_from_file(mi->ssl_ca, sizeof(mi->ssl_ca),
 
254
                                &mi->file, 0) ||
 
255
          init_strvar_from_file(mi->ssl_capath, sizeof(mi->ssl_capath),
 
256
                                &mi->file, 0) ||
 
257
          init_strvar_from_file(mi->ssl_cert, sizeof(mi->ssl_cert),
 
258
                                &mi->file, 0) ||
 
259
          init_strvar_from_file(mi->ssl_cipher, sizeof(mi->ssl_cipher),
 
260
                                &mi->file, 0) ||
 
261
          init_strvar_from_file(mi->ssl_key, sizeof(mi->ssl_key),
 
262
                               &mi->file, 0))
 
263
      goto errwithmsg;
 
264
 
 
265
      /*
 
266
        Starting from 5.1.16 ssl_verify_server_cert might be
 
267
        in the file
 
268
      */
 
269
      if (lines >= LINE_FOR_MASTER_SSL_VERIFY_SERVER_CERT &&
 
270
          init_intvar_from_file(&ssl_verify_server_cert, &mi->file, 0))
 
271
        goto errwithmsg;
 
272
      /*
 
273
        Starting from 6.0 master_heartbeat_period might be
 
274
        in the file
 
275
      */
 
276
      if (lines >= LINE_FOR_MASTER_HEARTBEAT_PERIOD &&
 
277
          init_floatvar_from_file(&master_heartbeat_period, &mi->file, 0.0))
 
278
        goto errwithmsg;
 
279
    }
 
280
 
 
281
    if (ssl)
 
282
      sql_print_warning("SSL information in the master info file "
 
283
                      "('%s') are ignored because this MySQL slave was "
 
284
                      "compiled without SSL support.", fname);
 
285
 
 
286
    /*
 
287
      This has to be handled here as init_intvar_from_file can't handle
 
288
      my_off_t types
 
289
    */
 
290
    mi->master_log_pos= (my_off_t) master_log_pos;
 
291
    mi->port= (uint) port;
 
292
    mi->connect_retry= (uint) connect_retry;
 
293
    mi->ssl= (my_bool) ssl;
 
294
    mi->ssl_verify_server_cert= ssl_verify_server_cert;
 
295
    mi->heartbeat_period= master_heartbeat_period;
246
296
  }
 
297
  DBUG_PRINT("master_info",("log_file_name: %s  position: %ld",
 
298
                            mi->master_log_name,
 
299
                            (ulong) mi->master_log_pos));
247
300
 
248
 
  rli.mi = this;
249
 
  if (init_relay_log_info(&rli, slave_info_fname))
 
301
  mi->rli.mi = mi;
 
302
  if (init_relay_log_info(&mi->rli, slave_info_fname))
250
303
    goto err;
251
304
 
252
 
  inited= 1;
253
 
  if ((error= test(flush())))
254
 
    sql_print_error(_("Failed to flush master info file"));
255
 
  pthread_mutex_unlock(&data_lock);
256
 
  return(error);
 
305
  mi->inited = 1;
 
306
  // now change cache READ -> WRITE - must do this before flush_master_info
 
307
  reinit_io_cache(&mi->file, WRITE_CACHE, 0L, 0, 1);
 
308
  if ((error=test(flush_master_info(mi, 1))))
 
309
    sql_print_error("Failed to flush master info file");
 
310
  pthread_mutex_unlock(&mi->data_lock);
 
311
  DBUG_RETURN(error);
 
312
 
 
313
errwithmsg:
 
314
  sql_print_error("Error reading master configuration");
257
315
 
258
316
err:
259
 
  pthread_mutex_unlock(&data_lock);
260
 
  return 1;
 
317
  if (fd >= 0)
 
318
  {
 
319
    my_close(fd, MYF(0));
 
320
    end_io_cache(&mi->file);
 
321
  }
 
322
  mi->fd= -1;
 
323
  pthread_mutex_unlock(&mi->data_lock);
 
324
  DBUG_RETURN(1);
261
325
}
262
326
 
263
327
 
267
331
     1 - flush master info failed
268
332
     0 - all ok
269
333
*/
270
 
int Master_info::flush()
 
334
int flush_master_info(Master_info* mi, bool flush_relay_log_cache)
271
335
{
 
336
  IO_CACHE* file = &mi->file;
 
337
  char lbuf[22];
 
338
 
 
339
  DBUG_ENTER("flush_master_info");
 
340
  DBUG_PRINT("enter",("master_pos: %ld", (long) mi->master_log_pos));
 
341
 
272
342
  /*
273
343
    Flush the relay log to disk. If we don't do it, then the relay log while
274
344
    have some part (its last kilobytes) in memory only, so if the slave server
281
351
    When we come to this place in code, relay log may or not be initialized;
282
352
    the caller is responsible for setting 'flush_relay_log_cache' accordingly.
283
353
  */
284
 
 
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);
289
 
 
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);
297
 
 
298
 
  fstream output(info_filename.c_str(), ios::out | ios::trunc | ios::binary);
299
 
  if (!list.SerializeToOstream(&output))
 
354
  if (flush_relay_log_cache &&
 
355
      flush_io_cache(mi->rli.relay_log.get_log_file()))
 
356
    DBUG_RETURN(2);
 
357
 
 
358
  /*
 
359
    We flushed the relay log BEFORE the master.info file, because if we crash
 
360
    now, we will get a duplicate event in the relay log at restart. If we
 
361
    flushed in the other order, we would get a hole in the relay log.
 
362
    And duplicate is better than hole (with a duplicate, in later versions we
 
363
    can add detection and scrap one event; with a hole there's nothing we can
 
364
    do).
 
365
  */
 
366
 
 
367
  /*
 
368
     In certain cases this code may create master.info files that seems
 
369
     corrupted, because of extra lines filled with garbage in the end
 
370
     file (this happens if new contents take less space than previous
 
371
     contents of file). But because of number of lines in the first line
 
372
     of file we don't care about this garbage.
 
373
  */
 
374
  char heartbeat_buf[sizeof(mi->heartbeat_period) * 4]; // buffer to suffice always
 
375
  my_sprintf(heartbeat_buf, (heartbeat_buf, "%.3f", mi->heartbeat_period));
 
376
  my_b_seek(file, 0L);
 
377
  my_b_printf(file,
 
378
              "%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",
 
379
              LINES_IN_MASTER_INFO,
 
380
              mi->master_log_name, llstr(mi->master_log_pos, lbuf),
 
381
              mi->host, mi->user,
 
382
              mi->password, mi->port, mi->connect_retry,
 
383
              (int)(mi->ssl), mi->ssl_ca, mi->ssl_capath, mi->ssl_cert,
 
384
              mi->ssl_cipher, mi->ssl_key, mi->ssl_verify_server_cert,
 
385
              heartbeat_buf);
 
386
  DBUG_RETURN(-flush_io_cache(file));
 
387
}
 
388
 
 
389
 
 
390
void end_master_info(Master_info* mi)
 
391
{
 
392
  DBUG_ENTER("end_master_info");
 
393
 
 
394
  if (!mi->inited)
 
395
    DBUG_VOID_RETURN;
 
396
  end_relay_log_info(&mi->rli);
 
397
  if (mi->fd >= 0)
300
398
  {
301
 
    assert(0);
302
 
    return 1;
 
399
    end_io_cache(&mi->file);
 
400
    (void)my_close(mi->fd, MYF(MY_WME));
 
401
    mi->fd = -1;
303
402
  }
304
 
 
305
 
  return 0;
306
 
}
307
 
 
308
 
 
309
 
void Master_info::end_master_info()
310
 
{
311
 
  if (!inited)
312
 
    return;
313
 
  end_relay_log_info(&rli);
314
 
  inited = 0;
315
 
 
316
 
  return;
317
 
}
 
403
  mi->inited = 0;
 
404
 
 
405
  DBUG_VOID_RETURN;
 
406
}
 
407
 
 
408
 
 
409
#endif /* HAVE_REPLICATION */