~drizzle-trunk/drizzle/development

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