~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/log.cc

  • Committer: Brian Aker
  • Date: 2009-12-29 01:38:38 UTC
  • mfrom: (1251.1.1 drizzle)
  • Revision ID: brian@gaz-20091229013838-03kb2z5xbqw03ddt
Merge of Diego fix.

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
 
 
17
 
/**
18
 
  @file
19
 
 
20
 
  @brief
21
 
  logging of commands
22
 
 
23
 
  @todo
24
 
    Abort logging when we get an error in reading or writing log files
25
 
*/
26
 
 
27
 
#include <drizzled/server_includes.h>
28
 
#include "sql_repl.h"
29
 
#include "rpl_filter.h"
30
 
#include "rpl_rli.h"
31
 
 
32
 
#include <mysys/my_dir.h>
33
 
#include <stdarg.h>
34
 
 
35
 
#include <drizzled/plugin.h>
36
 
#include <drizzled/drizzled_error_messages.h>
37
 
 
38
 
/* max size of the log message */
39
 
#define MAX_LOG_BUFFER_SIZE 1024
40
 
#define MAX_USER_HOST_SIZE 512
41
 
#define MAX_TIME_SIZE 32
42
 
#define MY_OFF_T_UNDEF (~(my_off_t)0UL)
43
 
 
44
 
#define FLAGSTR(V,F) ((V)&(F)?#F" ":"")
45
 
 
46
 
LOGGER logger;
47
 
 
48
 
DRIZZLE_BIN_LOG mysql_bin_log;
49
 
ulong sync_binlog_counter= 0;
50
 
 
51
 
static bool test_if_number(const char *str,
52
 
                           long *res, bool allow_wildcards);
53
 
static int binlog_init(void *p);
54
 
static int binlog_close_connection(handlerton *hton, THD *thd);
55
 
static int binlog_savepoint_set(handlerton *hton, THD *thd, void *sv);
56
 
static int binlog_savepoint_rollback(handlerton *hton, THD *thd, void *sv);
57
 
static int binlog_commit(handlerton *hton, THD *thd, bool all);
58
 
static int binlog_rollback(handlerton *hton, THD *thd, bool all);
59
 
static int binlog_prepare(handlerton *hton, THD *thd, bool all);
60
 
 
61
 
 
62
 
sql_print_message_func sql_print_message_handlers[3] =
63
 
{
64
 
  sql_print_information,
65
 
  sql_print_warning,
66
 
  sql_print_error
67
 
};
68
 
 
69
 
 
70
 
char *make_default_log_name(char *buff,const char* log_ext)
71
 
{
72
 
  strmake(buff, pidfile_name, FN_REFLEN-5);
73
 
  return fn_format(buff, buff, mysql_data_home, log_ext,
74
 
                   MYF(MY_UNPACK_FILENAME|MY_REPLACE_EXT));
75
 
}
76
 
 
77
 
/*
78
 
  Helper class to hold a mutex for the duration of the
79
 
  block.
80
 
 
81
 
  Eliminates the need for explicit unlocking of mutexes on, e.g.,
82
 
  error returns.  On passing a null pointer, the sentry will not do
83
 
  anything.
84
 
 */
85
 
class Mutex_sentry
86
 
{
87
 
public:
88
 
  Mutex_sentry(pthread_mutex_t *mutex)
89
 
    : m_mutex(mutex)
90
 
  {
91
 
    if (m_mutex)
92
 
      pthread_mutex_lock(mutex);
93
 
  }
94
 
 
95
 
  ~Mutex_sentry()
96
 
  {
97
 
    if (m_mutex)
98
 
      pthread_mutex_unlock(m_mutex);
99
 
    m_mutex= 0;
100
 
  }
101
 
 
102
 
private:
103
 
  pthread_mutex_t *m_mutex;
104
 
 
105
 
  // It's not allowed to copy this object in any way
106
 
  Mutex_sentry(Mutex_sentry const&);
107
 
  void operator=(Mutex_sentry const&);
108
 
};
109
 
 
110
 
/*
111
 
  Helper class to store binary log transaction data.
112
 
*/
113
 
class binlog_trx_data {
114
 
public:
115
 
  binlog_trx_data()
116
 
    : at_least_one_stmt(0), m_pending(0), before_stmt_pos(MY_OFF_T_UNDEF)
117
 
  {
118
 
    trans_log.end_of_file= max_binlog_cache_size;
119
 
  }
120
 
 
121
 
  ~binlog_trx_data()
122
 
  {
123
 
    assert(pending() == NULL);
124
 
    close_cached_file(&trans_log);
125
 
  }
126
 
 
127
 
  my_off_t position() const {
128
 
    return my_b_tell(&trans_log);
129
 
  }
130
 
 
131
 
  bool empty() const
132
 
  {
133
 
    return pending() == NULL && my_b_tell(&trans_log) == 0;
134
 
  }
135
 
 
136
 
  /*
137
 
    Truncate the transaction cache to a certain position. This
138
 
    includes deleting the pending event.
139
 
   */
140
 
  void truncate(my_off_t pos)
141
 
  {
142
 
    delete pending();
143
 
    set_pending(0);
144
 
    reinit_io_cache(&trans_log, WRITE_CACHE, pos, 0, 0);
145
 
    if (pos < before_stmt_pos)
146
 
      before_stmt_pos= MY_OFF_T_UNDEF;
147
 
 
148
 
    /*
149
 
      The only valid positions that can be truncated to are at the
150
 
      beginning of a statement. We are relying on this fact to be able
151
 
      to set the at_least_one_stmt flag correctly. In other word, if
152
 
      we are truncating to the beginning of the transaction cache,
153
 
      there will be no statements in the cache, otherwhise, we will
154
 
      have at least one statement in the transaction cache.
155
 
     */
156
 
    at_least_one_stmt= (pos > 0);
157
 
  }
158
 
 
159
 
  /*
160
 
    Reset the entire contents of the transaction cache, emptying it
161
 
    completely.
162
 
   */
163
 
  void reset() {
164
 
    if (!empty())
165
 
      truncate(0);
166
 
    before_stmt_pos= MY_OFF_T_UNDEF;
167
 
    trans_log.end_of_file= max_binlog_cache_size;
168
 
  }
169
 
 
170
 
  Rows_log_event *pending() const
171
 
  {
172
 
    return m_pending;
173
 
  }
174
 
 
175
 
  void set_pending(Rows_log_event *const pending)
176
 
  {
177
 
    m_pending= pending;
178
 
  }
179
 
 
180
 
  IO_CACHE trans_log;                         // The transaction cache
181
 
 
182
 
  /**
183
 
    Boolean that is true if there is at least one statement in the
184
 
    transaction cache.
185
 
  */
186
 
  bool at_least_one_stmt;
187
 
 
188
 
private:
189
 
  /*
190
 
    Pending binrows event. This event is the event where the rows are
191
 
    currently written.
192
 
   */
193
 
  Rows_log_event *m_pending;
194
 
 
195
 
public:
196
 
  /*
197
 
    Binlog position before the start of the current statement.
198
 
  */
199
 
  my_off_t before_stmt_pos;
200
 
};
201
 
 
202
 
handlerton *binlog_hton;
203
 
 
204
 
 
205
 
/* Check if a given table is opened log table */
206
 
int check_if_log_table(uint db_len __attribute__((unused)),
207
 
                       const char *db __attribute__((unused)),
208
 
                       uint table_name_len __attribute__((unused)),
209
 
                       const char *table_name __attribute__((unused)),
210
 
                       uint check_if_opened __attribute__((unused)))
211
 
{
212
 
  return 0;
213
 
}
214
 
 
215
 
/* log event handlers */
216
 
 
217
 
bool Log_to_file_event_handler::
218
 
  log_error(enum loglevel level, const char *format,
219
 
            va_list args)
220
 
{
221
 
  return vprint_msg_to_log(level, format, args);
222
 
}
223
 
 
224
 
void Log_to_file_event_handler::init_pthread_objects()
225
 
{
226
 
  mysql_log.init_pthread_objects();
227
 
  mysql_slow_log.init_pthread_objects();
228
 
}
229
 
 
230
 
 
231
 
/** Wrapper around DRIZZLE_LOG::write() for slow log. */
232
 
 
233
 
bool Log_to_file_event_handler::
234
 
  log_slow(THD *thd, time_t current_time, time_t query_start_arg,
235
 
           const char *user_host, uint user_host_len,
236
 
           uint64_t query_utime, uint64_t lock_utime, bool is_command,
237
 
           const char *sql_text, uint sql_text_len)
238
 
{
239
 
  return mysql_slow_log.write(thd, current_time, query_start_arg,
240
 
                              user_host, user_host_len,
241
 
                              query_utime, lock_utime, is_command,
242
 
                              sql_text, sql_text_len);
243
 
}
244
 
 
245
 
 
246
 
/**
247
 
   Wrapper around DRIZZLE_LOG::write() for general log. We need it since we
248
 
   want all log event handlers to have the same signature.
249
 
*/
250
 
 
251
 
bool Log_to_file_event_handler::
252
 
  log_general(THD *thd __attribute__((unused)),
253
 
              time_t event_time, const char *user_host,
254
 
              uint user_host_len, int thread_id,
255
 
              const char *command_type, uint command_type_len,
256
 
              const char *sql_text, uint sql_text_len,
257
 
              const CHARSET_INFO * const client_cs __attribute__((unused)))
258
 
{
259
 
  return mysql_log.write(event_time, user_host, user_host_len,
260
 
                         thread_id, command_type, command_type_len,
261
 
                         sql_text, sql_text_len);
262
 
}
263
 
 
264
 
 
265
 
bool Log_to_file_event_handler::init()
266
 
{
267
 
  if (!is_initialized)
268
 
  {
269
 
    if (opt_slow_log)
270
 
      mysql_slow_log.open_slow_log(sys_var_slow_log_path.value);
271
 
 
272
 
    if (opt_log)
273
 
      mysql_log.open_query_log(sys_var_general_log_path.value);
274
 
 
275
 
    is_initialized= true;
276
 
  }
277
 
 
278
 
  return false;
279
 
}
280
 
 
281
 
 
282
 
void Log_to_file_event_handler::cleanup()
283
 
{
284
 
  mysql_log.cleanup();
285
 
  mysql_slow_log.cleanup();
286
 
}
287
 
 
288
 
void Log_to_file_event_handler::flush()
289
 
{
290
 
  /* reopen log files */
291
 
  if (opt_log)
292
 
    mysql_log.reopen_file();
293
 
  if (opt_slow_log)
294
 
    mysql_slow_log.reopen_file();
295
 
}
296
 
 
297
 
/*
298
 
  Log error with all enabled log event handlers
299
 
 
300
 
  SYNOPSIS
301
 
    error_log_print()
302
 
 
303
 
    level             The level of the error significance: NOTE,
304
 
                      WARNING or ERROR.
305
 
    format            format string for the error message
306
 
    args              list of arguments for the format string
307
 
 
308
 
  RETURN
309
 
    FALSE - OK
310
 
    TRUE - error occured
311
 
*/
312
 
 
313
 
bool LOGGER::error_log_print(enum loglevel level, const char *format,
314
 
                             va_list args)
315
 
{
316
 
  bool error= false;
317
 
  Log_event_handler **current_handler;
318
 
 
319
 
  /* currently we don't need locking here as there is no error_log table */
320
 
  for (current_handler= error_log_handler_list ; *current_handler ;)
321
 
    error= (*current_handler++)->log_error(level, format, args) || error;
322
 
 
323
 
  return error;
324
 
}
325
 
 
326
 
 
327
 
void LOGGER::cleanup_base()
328
 
{
329
 
  assert(inited == 1);
330
 
  rwlock_destroy(&LOCK_logger);
331
 
  if (file_log_handler)
332
 
    file_log_handler->cleanup();
333
 
}
334
 
 
335
 
 
336
 
void LOGGER::cleanup_end()
337
 
{
338
 
  assert(inited == 1);
339
 
  if (file_log_handler)
340
 
    delete file_log_handler;
341
 
}
342
 
 
343
 
 
344
 
/**
345
 
  Perform basic log initialization: create file-based log handler and
346
 
  init error log.
347
 
*/
348
 
void LOGGER::init_base()
349
 
{
350
 
  assert(inited == 0);
351
 
  inited= 1;
352
 
 
353
 
  /*
354
 
    Here we create file log handler. We don't do it for the table log handler
355
 
    here as it cannot be created so early. The reason is THD initialization,
356
 
    which depends on the system variables (parsed later).
357
 
  */
358
 
  if (!file_log_handler)
359
 
    file_log_handler= new Log_to_file_event_handler;
360
 
 
361
 
  /* by default we use traditional error log */
362
 
  init_error_log(LOG_FILE);
363
 
 
364
 
  file_log_handler->init_pthread_objects();
365
 
  my_rwlock_init(&LOCK_logger, NULL);
366
 
}
367
 
 
368
 
 
369
 
bool LOGGER::flush_logs(THD *thd __attribute__((unused)))
370
 
{
371
 
  int rc= 0;
372
 
 
373
 
  /*
374
 
    Now we lock logger, as nobody should be able to use logging routines while
375
 
    log tables are closed
376
 
  */
377
 
  logger.lock_exclusive();
378
 
 
379
 
  /* reopen log files */
380
 
  file_log_handler->flush();
381
 
 
382
 
  /* end of log flush */
383
 
  logger.unlock();
384
 
  return rc;
385
 
}
386
 
 
387
 
 
388
 
/*
389
 
  Log slow query with all enabled log event handlers
390
 
 
391
 
  SYNOPSIS
392
 
    slow_log_print()
393
 
 
394
 
    thd                 THD of the query being logged
395
 
    query               The query being logged
396
 
    query_length        The length of the query string
397
 
    current_utime       Current time in microseconds (from undefined start)
398
 
 
399
 
  RETURN
400
 
    FALSE   OK
401
 
    TRUE    error occured
402
 
*/
403
 
 
404
 
bool LOGGER::slow_log_print(THD *thd, const char *query, uint query_length,
405
 
                            uint64_t current_utime)
406
 
 
407
 
{
408
 
  bool error= false;
409
 
  Log_event_handler **current_handler;
410
 
  bool is_command= false;
411
 
  char user_host_buff[MAX_USER_HOST_SIZE];
412
 
  Security_context *sctx= thd->security_ctx;
413
 
  uint user_host_len= 0;
414
 
  uint64_t query_utime, lock_utime;
415
 
 
416
 
  /*
417
 
    Print the message to the buffer if we have slow log enabled
418
 
  */
419
 
 
420
 
  if (*slow_log_handler_list)
421
 
  {
422
 
    time_t current_time;
423
 
 
424
 
    /* do not log slow queries from replication threads */
425
 
    if (thd->slave_thread && !opt_log_slow_slave_statements)
426
 
      return 0;
427
 
 
428
 
    lock_shared();
429
 
    if (!opt_slow_log)
430
 
    {
431
 
      unlock();
432
 
      return 0;
433
 
    }
434
 
 
435
 
    /* fill in user_host value: the format is "%s[%s] @ %s [%s]" */
436
 
    user_host_len= (strxnmov(user_host_buff, MAX_USER_HOST_SIZE,
437
 
                             sctx->user, "[", sctx->user, "] @ ",
438
 
                             sctx->ip, " [",
439
 
                             sctx->ip, "]", NullS) -
440
 
                    user_host_buff);
441
 
 
442
 
    current_time= my_time_possible_from_micro(current_utime);
443
 
    if (thd->start_utime)
444
 
    {
445
 
      query_utime= (current_utime - thd->start_utime);
446
 
      lock_utime=  (thd->utime_after_lock - thd->start_utime);
447
 
    }
448
 
    else
449
 
    {
450
 
      query_utime= lock_utime= 0;
451
 
    }
452
 
 
453
 
    if (!query)
454
 
    {
455
 
      is_command= true;
456
 
      query= command_name[thd->command].str;
457
 
      query_length= command_name[thd->command].length;
458
 
    }
459
 
 
460
 
    for (current_handler= slow_log_handler_list; *current_handler ;)
461
 
      error= (*current_handler++)->log_slow(thd, current_time, thd->start_time,
462
 
                                            user_host_buff, user_host_len,
463
 
                                            query_utime, lock_utime, is_command,
464
 
                                            query, query_length) || error;
465
 
 
466
 
    unlock();
467
 
  }
468
 
  return error;
469
 
}
470
 
 
471
 
bool LOGGER::general_log_write(THD *thd, enum enum_server_command command,
472
 
                               const char *query, uint query_length)
473
 
{
474
 
  bool error= false;
475
 
  Log_event_handler **current_handler= general_log_handler_list;
476
 
  char user_host_buff[MAX_USER_HOST_SIZE];
477
 
  Security_context *sctx= thd->security_ctx;
478
 
  ulong id;
479
 
  uint user_host_len= 0;
480
 
  time_t current_time;
481
 
 
482
 
  if (thd)
483
 
    id= thd->thread_id;                 /* Normal thread */
484
 
  else
485
 
    id= 0;                              /* Log from connect handler */
486
 
 
487
 
  lock_shared();
488
 
  if (!opt_log)
489
 
  {
490
 
    unlock();
491
 
    return 0;
492
 
  }
493
 
  user_host_len= strxnmov(user_host_buff, MAX_USER_HOST_SIZE,
494
 
                          sctx->user, "[",
495
 
                          sctx->user, "] @ ",
496
 
                          sctx->ip, " [",
497
 
                          sctx->ip, "]", NullS) -
498
 
                                                          user_host_buff;
499
 
 
500
 
  current_time= my_time(0);
501
 
 
502
 
  while (*current_handler)
503
 
    error|= (*current_handler++)->
504
 
      log_general(thd, current_time, user_host_buff,
505
 
                  user_host_len, id,
506
 
                  command_name[(uint) command].str,
507
 
                  command_name[(uint) command].length,
508
 
                  query, query_length,
509
 
                  thd->variables.character_set_client) || error;
510
 
  unlock();
511
 
 
512
 
  return error;
513
 
}
514
 
 
515
 
bool LOGGER::general_log_print(THD *thd, enum enum_server_command command,
516
 
                               const char *format, va_list args)
517
 
{
518
 
  uint message_buff_len= 0;
519
 
  char message_buff[MAX_LOG_BUFFER_SIZE];
520
 
 
521
 
  /* prepare message */
522
 
  if (format)
523
 
    message_buff_len= vsnprintf(message_buff, sizeof(message_buff),
524
 
                                   format, args);
525
 
  else
526
 
    message_buff[0]= '\0';
527
 
 
528
 
  return general_log_write(thd, command, message_buff, message_buff_len);
529
 
}
530
 
 
531
 
void LOGGER::init_error_log(uint error_log_printer)
532
 
{
533
 
  if (error_log_printer & LOG_NONE)
534
 
  {
535
 
    error_log_handler_list[0]= 0;
536
 
    return;
537
 
  }
538
 
 
539
 
  switch (error_log_printer) {
540
 
  case LOG_FILE:
541
 
    error_log_handler_list[0]= file_log_handler;
542
 
    error_log_handler_list[1]= 0;
543
 
    break;
544
 
  }
545
 
}
546
 
 
547
 
void LOGGER::init_slow_log(uint slow_log_printer)
548
 
{
549
 
  if (slow_log_printer & LOG_NONE)
550
 
  {
551
 
    slow_log_handler_list[0]= 0;
552
 
    return;
553
 
  }
554
 
 
555
 
  slow_log_handler_list[0]= file_log_handler;
556
 
  slow_log_handler_list[1]= 0;
557
 
}
558
 
 
559
 
void LOGGER::init_general_log(uint general_log_printer)
560
 
{
561
 
  if (general_log_printer & LOG_NONE)
562
 
  {
563
 
    general_log_handler_list[0]= 0;
564
 
    return;
565
 
  }
566
 
 
567
 
  general_log_handler_list[0]= file_log_handler;
568
 
  general_log_handler_list[1]= 0;
569
 
}
570
 
 
571
 
 
572
 
bool LOGGER::activate_log_handler(THD* thd __attribute__((unused)),
573
 
                                  uint log_type)
574
 
{
575
 
  DRIZZLE_QUERY_LOG *file_log;
576
 
  bool res= false;
577
 
  lock_exclusive();
578
 
  switch (log_type) {
579
 
  case QUERY_LOG_SLOW:
580
 
    if (!opt_slow_log)
581
 
    {
582
 
      file_log= file_log_handler->get_mysql_slow_log();
583
 
 
584
 
      file_log->open_slow_log(sys_var_slow_log_path.value);
585
 
      init_slow_log(log_output_options);
586
 
      opt_slow_log= true;
587
 
    }
588
 
    break;
589
 
  case QUERY_LOG_GENERAL:
590
 
    if (!opt_log)
591
 
    {
592
 
      file_log= file_log_handler->get_mysql_log();
593
 
 
594
 
      file_log->open_query_log(sys_var_general_log_path.value);
595
 
      init_general_log(log_output_options);
596
 
      opt_log= true;
597
 
    }
598
 
    break;
599
 
  default:
600
 
    assert(0);
601
 
  }
602
 
  unlock();
603
 
  return res;
604
 
}
605
 
 
606
 
 
607
 
void LOGGER::deactivate_log_handler(THD *thd __attribute__((unused)),
608
 
                                    uint log_type)
609
 
{
610
 
  bool *tmp_opt= 0;
611
 
  DRIZZLE_LOG *file_log;
612
 
 
613
 
  switch (log_type) {
614
 
  case QUERY_LOG_SLOW:
615
 
    tmp_opt= &opt_slow_log;
616
 
    file_log= file_log_handler->get_mysql_slow_log();
617
 
    break;
618
 
  case QUERY_LOG_GENERAL:
619
 
    tmp_opt= &opt_log;
620
 
    file_log= file_log_handler->get_mysql_log();
621
 
    break;
622
 
  default:
623
 
    assert(0);                                  // Impossible
624
 
  }
625
 
 
626
 
  if (!(*tmp_opt))
627
 
    return;
628
 
 
629
 
  lock_exclusive();
630
 
  file_log->close(0);
631
 
  *tmp_opt= false;
632
 
  unlock();
633
 
}
634
 
 
635
 
int LOGGER::set_handlers(uint error_log_printer,
636
 
                         uint slow_log_printer,
637
 
                         uint general_log_printer)
638
 
{
639
 
  /* error log table is not supported yet */
640
 
  lock_exclusive();
641
 
 
642
 
  init_error_log(error_log_printer);
643
 
  init_slow_log(slow_log_printer);
644
 
  init_general_log(general_log_printer);
645
 
 
646
 
  unlock();
647
 
 
648
 
  return 0;
649
 
}
650
 
 
651
 
 
652
 
 /*
653
 
  Save position of binary log transaction cache.
654
 
 
655
 
  SYNPOSIS
656
 
    binlog_trans_log_savepos()
657
 
 
658
 
    thd      The thread to take the binlog data from
659
 
    pos      Pointer to variable where the position will be stored
660
 
 
661
 
  DESCRIPTION
662
 
 
663
 
    Save the current position in the binary log transaction cache into
664
 
    the variable pointed to by 'pos'
665
 
 */
666
 
 
667
 
static void
668
 
binlog_trans_log_savepos(THD *thd, my_off_t *pos)
669
 
{
670
 
  assert(pos != NULL);
671
 
  if (thd_get_ha_data(thd, binlog_hton) == NULL)
672
 
    thd->binlog_setup_trx_data();
673
 
  binlog_trx_data *const trx_data=
674
 
    (binlog_trx_data*) thd_get_ha_data(thd, binlog_hton);
675
 
  assert(mysql_bin_log.is_open());
676
 
  *pos= trx_data->position();
677
 
  return;
678
 
}
679
 
 
680
 
 
681
 
/*
682
 
  Truncate the binary log transaction cache.
683
 
 
684
 
  SYNPOSIS
685
 
    binlog_trans_log_truncate()
686
 
 
687
 
    thd      The thread to take the binlog data from
688
 
    pos      Position to truncate to
689
 
 
690
 
  DESCRIPTION
691
 
 
692
 
    Truncate the binary log to the given position. Will not change
693
 
    anything else.
694
 
 
695
 
 */
696
 
static void
697
 
binlog_trans_log_truncate(THD *thd, my_off_t pos)
698
 
{
699
 
  assert(thd_get_ha_data(thd, binlog_hton) != NULL);
700
 
  /* Only true if binlog_trans_log_savepos() wasn't called before */
701
 
  assert(pos != ~(my_off_t) 0);
702
 
 
703
 
  binlog_trx_data *const trx_data=
704
 
    (binlog_trx_data*) thd_get_ha_data(thd, binlog_hton);
705
 
  trx_data->truncate(pos);
706
 
  return;
707
 
}
708
 
 
709
 
 
710
 
/*
711
 
  this function is mostly a placeholder.
712
 
  conceptually, binlog initialization (now mostly done in DRIZZLE_BIN_LOG::open)
713
 
  should be moved here.
714
 
*/
715
 
 
716
 
int binlog_init(void *p)
717
 
{
718
 
  binlog_hton= (handlerton *)p;
719
 
  binlog_hton->state=opt_bin_log ? SHOW_OPTION_YES : SHOW_OPTION_NO;
720
 
  binlog_hton->db_type=DB_TYPE_BINLOG;
721
 
  binlog_hton->savepoint_offset= sizeof(my_off_t);
722
 
  binlog_hton->close_connection= binlog_close_connection;
723
 
  binlog_hton->savepoint_set= binlog_savepoint_set;
724
 
  binlog_hton->savepoint_rollback= binlog_savepoint_rollback;
725
 
  binlog_hton->commit= binlog_commit;
726
 
  binlog_hton->rollback= binlog_rollback;
727
 
  binlog_hton->prepare= binlog_prepare;
728
 
  binlog_hton->flags= HTON_NOT_USER_SELECTABLE | HTON_HIDDEN;
729
 
 
730
 
  return 0;
731
 
}
732
 
 
733
 
static int binlog_close_connection(handlerton *hton __attribute__((unused)),
734
 
                                   THD *thd)
735
 
{
736
 
  binlog_trx_data *const trx_data=
737
 
    (binlog_trx_data*) thd_get_ha_data(thd, binlog_hton);
738
 
  assert(trx_data->empty());
739
 
  thd_set_ha_data(thd, binlog_hton, NULL);
740
 
  trx_data->~binlog_trx_data();
741
 
  my_free((uchar*)trx_data, MYF(0));
742
 
  return 0;
743
 
}
744
 
 
745
 
/*
746
 
  End a transaction.
747
 
 
748
 
  SYNOPSIS
749
 
    binlog_end_trans()
750
 
 
751
 
    thd      The thread whose transaction should be ended
752
 
    trx_data Pointer to the transaction data to use
753
 
    end_ev   The end event to use, or NULL
754
 
    all      True if the entire transaction should be ended, false if
755
 
             only the statement transaction should be ended.
756
 
 
757
 
  DESCRIPTION
758
 
 
759
 
    End the currently open transaction. The transaction can be either
760
 
    a real transaction (if 'all' is true) or a statement transaction
761
 
    (if 'all' is false).
762
 
 
763
 
    If 'end_ev' is NULL, the transaction is a rollback of only
764
 
    transactional tables, so the transaction cache will be truncated
765
 
    to either just before the last opened statement transaction (if
766
 
    'all' is false), or reset completely (if 'all' is true).
767
 
 */
768
 
static int
769
 
binlog_end_trans(THD *thd, binlog_trx_data *trx_data,
770
 
                 Log_event *end_ev, bool all)
771
 
{
772
 
  int error=0;
773
 
  IO_CACHE *trans_log= &trx_data->trans_log;
774
 
 
775
 
  /*
776
 
    NULL denotes ROLLBACK with nothing to replicate: i.e., rollback of
777
 
    only transactional tables.  If the transaction contain changes to
778
 
    any non-transactiona tables, we need write the transaction and log
779
 
    a ROLLBACK last.
780
 
  */
781
 
  if (end_ev != NULL)
782
 
  {
783
 
    /*
784
 
      Doing a commit or a rollback including non-transactional tables,
785
 
      i.e., ending a transaction where we might write the transaction
786
 
      cache to the binary log.
787
 
 
788
 
      We can always end the statement when ending a transaction since
789
 
      transactions are not allowed inside stored functions.  If they
790
 
      were, we would have to ensure that we're not ending a statement
791
 
      inside a stored function.
792
 
     */
793
 
    thd->binlog_flush_pending_rows_event(true);
794
 
 
795
 
    error= mysql_bin_log.write(thd, &trx_data->trans_log, end_ev);
796
 
    trx_data->reset();
797
 
 
798
 
    /*
799
 
      We need to step the table map version after writing the
800
 
      transaction cache to disk.
801
 
    */
802
 
    mysql_bin_log.update_table_map_version();
803
 
    statistic_increment(binlog_cache_use, &LOCK_status);
804
 
    if (trans_log->disk_writes != 0)
805
 
    {
806
 
      statistic_increment(binlog_cache_disk_use, &LOCK_status);
807
 
      trans_log->disk_writes= 0;
808
 
    }
809
 
  }
810
 
  else
811
 
  {
812
 
    /*
813
 
      If rolling back an entire transaction or a single statement not
814
 
      inside a transaction, we reset the transaction cache.
815
 
 
816
 
      If rolling back a statement in a transaction, we truncate the
817
 
      transaction cache to remove the statement.
818
 
     */
819
 
    if (all || !(thd->options & (OPTION_BEGIN | OPTION_NOT_AUTOCOMMIT)))
820
 
    {
821
 
      trx_data->reset();
822
 
 
823
 
      assert(!thd->binlog_get_pending_rows_event());
824
 
      thd->clear_binlog_table_maps();
825
 
    }
826
 
    else                                        // ...statement
827
 
      trx_data->truncate(trx_data->before_stmt_pos);
828
 
 
829
 
    /*
830
 
      We need to step the table map version on a rollback to ensure
831
 
      that a new table map event is generated instead of the one that
832
 
      was written to the thrown-away transaction cache.
833
 
    */
834
 
    mysql_bin_log.update_table_map_version();
835
 
  }
836
 
 
837
 
  return(error);
838
 
}
839
 
 
840
 
static int binlog_prepare(handlerton *hton __attribute__((unused)),
841
 
                          THD *thd __attribute__((unused)),
842
 
                          bool all __attribute__((unused)))
843
 
{
844
 
  /*
845
 
    do nothing.
846
 
    just pretend we can do 2pc, so that MySQL won't
847
 
    switch to 1pc.
848
 
    real work will be done in DRIZZLE_BIN_LOG::log_xid()
849
 
  */
850
 
  return 0;
851
 
}
852
 
 
853
 
#define YESNO(X) ((X) ? "yes" : "no")
854
 
 
855
 
/**
856
 
  This function is called once after each statement.
857
 
 
858
 
  It has the responsibility to flush the transaction cache to the
859
 
  binlog file on commits.
860
 
 
861
 
  @param hton  The binlog handlerton.
862
 
  @param thd   The client thread that executes the transaction.
863
 
  @param all   This is @c true if this is a real transaction commit, and
864
 
               @false otherwise.
865
 
 
866
 
  @see handlerton::commit
867
 
*/
868
 
static int binlog_commit(handlerton *hton __attribute__((unused)),
869
 
                         THD *thd, bool all)
870
 
{
871
 
  binlog_trx_data *const trx_data=
872
 
    (binlog_trx_data*) thd_get_ha_data(thd, binlog_hton);
873
 
 
874
 
  if (trx_data->empty())
875
 
  {
876
 
    // we're here because trans_log was flushed in DRIZZLE_BIN_LOG::log_xid()
877
 
    trx_data->reset();
878
 
    return(0);
879
 
  }
880
 
 
881
 
  /*
882
 
    Decision table for committing a transaction. The top part, the
883
 
    *conditions* represent different cases that can occur, and hte
884
 
    bottom part, the *actions*, represent what should be done in that
885
 
    particular case.
886
 
 
887
 
    Real transaction        'all' was true
888
 
 
889
 
    Statement in cache      There were at least one statement in the
890
 
                            transaction cache
891
 
 
892
 
    In transaction          We are inside a transaction
893
 
 
894
 
    Stmt modified non-trans The statement being committed modified a
895
 
                            non-transactional table
896
 
 
897
 
    All modified non-trans  Some statement before this one in the
898
 
                            transaction modified a non-transactional
899
 
                            table
900
 
 
901
 
 
902
 
    =============================  = = = = = = = = = = = = = = = =
903
 
    Real transaction               N N N N N N N N N N N N N N N N
904
 
    Statement in cache             N N N N N N N N Y Y Y Y Y Y Y Y
905
 
    In transaction                 N N N N Y Y Y Y N N N N Y Y Y Y
906
 
    Stmt modified non-trans        N N Y Y N N Y Y N N Y Y N N Y Y
907
 
    All modified non-trans         N Y N Y N Y N Y N Y N Y N Y N Y
908
 
 
909
 
    Action: (C)ommit/(A)ccumulate  C C - C A C - C - - - - A A - A
910
 
    =============================  = = = = = = = = = = = = = = = =
911
 
 
912
 
 
913
 
    =============================  = = = = = = = = = = = = = = = =
914
 
    Real transaction               Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y
915
 
    Statement in cache             N N N N N N N N Y Y Y Y Y Y Y Y
916
 
    In transaction                 N N N N Y Y Y Y N N N N Y Y Y Y
917
 
    Stmt modified non-trans        N N Y Y N N Y Y N N Y Y N N Y Y
918
 
    All modified non-trans         N Y N Y N Y N Y N Y N Y N Y N Y
919
 
 
920
 
    (C)ommit/(A)ccumulate/(-)      - - - - C C - C - - - - C C - C
921
 
    =============================  = = = = = = = = = = = = = = = =
922
 
 
923
 
    In other words, we commit the transaction if and only if both of
924
 
    the following are true:
925
 
     - We are not in a transaction and committing a statement
926
 
 
927
 
     - We are in a transaction and one (or more) of the following are
928
 
       true:
929
 
 
930
 
       - A full transaction is committed
931
 
 
932
 
         OR
933
 
 
934
 
       - A non-transactional statement is committed and there is
935
 
         no statement cached
936
 
 
937
 
    Otherwise, we accumulate the statement
938
 
  */
939
 
  uint64_t const in_transaction=
940
 
    thd->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN);
941
 
  if ((in_transaction && (all || (!trx_data->at_least_one_stmt && thd->transaction.stmt.modified_non_trans_table))) || (!in_transaction && !all))
942
 
  {
943
 
    Query_log_event qev(thd, STRING_WITH_LEN("COMMIT"), true, false);
944
 
    qev.error_code= 0; // see comment in DRIZZLE_LOG::write(THD, IO_CACHE)
945
 
    int error= binlog_end_trans(thd, trx_data, &qev, all);
946
 
    return(error);
947
 
  }
948
 
  return(0);
949
 
}
950
 
 
951
 
/**
952
 
  This function is called when a transaction involving a transactional
953
 
  table is rolled back.
954
 
 
955
 
  It has the responsibility to flush the transaction cache to the
956
 
  binlog file. However, if the transaction does not involve
957
 
  non-transactional tables, nothing needs to be logged.
958
 
 
959
 
  @param hton  The binlog handlerton.
960
 
  @param thd   The client thread that executes the transaction.
961
 
  @param all   This is @c true if this is a real transaction rollback, and
962
 
               @false otherwise.
963
 
 
964
 
  @see handlerton::rollback
965
 
*/
966
 
static int binlog_rollback(handlerton *hton __attribute__((unused)),
967
 
                           THD *thd, bool all)
968
 
{
969
 
  int error=0;
970
 
  binlog_trx_data *const trx_data=
971
 
    (binlog_trx_data*) thd_get_ha_data(thd, binlog_hton);
972
 
 
973
 
  if (trx_data->empty()) {
974
 
    trx_data->reset();
975
 
    return(0);
976
 
  }
977
 
 
978
 
  if ((all && thd->transaction.all.modified_non_trans_table) ||
979
 
      (!all && thd->transaction.stmt.modified_non_trans_table) ||
980
 
      (thd->options & OPTION_KEEP_LOG))
981
 
  {
982
 
    /*
983
 
      We write the transaction cache with a rollback last if we have
984
 
      modified any non-transactional table. We do this even if we are
985
 
      committing a single statement that has modified a
986
 
      non-transactional table since it can have modified a
987
 
      transactional table in that statement as well, which needs to be
988
 
      rolled back on the slave.
989
 
    */
990
 
    Query_log_event qev(thd, STRING_WITH_LEN("ROLLBACK"), true, false);
991
 
    qev.error_code= 0; // see comment in DRIZZLE_LOG::write(THD, IO_CACHE)
992
 
    error= binlog_end_trans(thd, trx_data, &qev, all);
993
 
  }
994
 
  else if ((all && !thd->transaction.all.modified_non_trans_table) ||
995
 
           (!all && !thd->transaction.stmt.modified_non_trans_table))
996
 
  {
997
 
    /*
998
 
      If we have modified only transactional tables, we can truncate
999
 
      the transaction cache without writing anything to the binary
1000
 
      log.
1001
 
     */
1002
 
    error= binlog_end_trans(thd, trx_data, 0, all);
1003
 
  }
1004
 
  return(error);
1005
 
}
1006
 
 
1007
 
/**
1008
 
  @note
1009
 
  How do we handle this (unlikely but legal) case:
1010
 
  @verbatim
1011
 
    [transaction] + [update to non-trans table] + [rollback to savepoint] ?
1012
 
  @endverbatim
1013
 
  The problem occurs when a savepoint is before the update to the
1014
 
  non-transactional table. Then when there's a rollback to the savepoint, if we
1015
 
  simply truncate the binlog cache, we lose the part of the binlog cache where
1016
 
  the update is. If we want to not lose it, we need to write the SAVEPOINT
1017
 
  command and the ROLLBACK TO SAVEPOINT command to the binlog cache. The latter
1018
 
  is easy: it's just write at the end of the binlog cache, but the former
1019
 
  should be *inserted* to the place where the user called SAVEPOINT. The
1020
 
  solution is that when the user calls SAVEPOINT, we write it to the binlog
1021
 
  cache (so no need to later insert it). As transactions are never intermixed
1022
 
  in the binary log (i.e. they are serialized), we won't have conflicts with
1023
 
  savepoint names when using mysqlbinlog or in the slave SQL thread.
1024
 
  Then when ROLLBACK TO SAVEPOINT is called, if we updated some
1025
 
  non-transactional table, we don't truncate the binlog cache but instead write
1026
 
  ROLLBACK TO SAVEPOINT to it; otherwise we truncate the binlog cache (which
1027
 
  will chop the SAVEPOINT command from the binlog cache, which is good as in
1028
 
  that case there is no need to have it in the binlog).
1029
 
*/
1030
 
 
1031
 
static int binlog_savepoint_set(handlerton *hton __attribute__((unused)),
1032
 
                                THD *thd, void *sv)
1033
 
{
1034
 
  binlog_trans_log_savepos(thd, (my_off_t*) sv);
1035
 
  /* Write it to the binary log */
1036
 
  
1037
 
  int const error=
1038
 
    thd->binlog_query(THD::STMT_QUERY_TYPE,
1039
 
                      thd->query, thd->query_length, true, false);
1040
 
  return(error);
1041
 
}
1042
 
 
1043
 
static int binlog_savepoint_rollback(handlerton *hton __attribute__((unused)),
1044
 
                                     THD *thd, void *sv)
1045
 
{
1046
 
  /*
1047
 
    Write ROLLBACK TO SAVEPOINT to the binlog cache if we have updated some
1048
 
    non-transactional table. Otherwise, truncate the binlog cache starting
1049
 
    from the SAVEPOINT command.
1050
 
  */
1051
 
  if (unlikely(thd->transaction.all.modified_non_trans_table || 
1052
 
               (thd->options & OPTION_KEEP_LOG)))
1053
 
  {
1054
 
    int error=
1055
 
      thd->binlog_query(THD::STMT_QUERY_TYPE,
1056
 
                        thd->query, thd->query_length, true, false);
1057
 
    return(error);
1058
 
  }
1059
 
  binlog_trans_log_truncate(thd, *(my_off_t*)sv);
1060
 
  return(0);
1061
 
}
1062
 
 
1063
 
 
1064
 
int check_binlog_magic(IO_CACHE* log, const char** errmsg)
1065
 
{
1066
 
  char magic[4];
1067
 
  assert(my_b_tell(log) == 0);
1068
 
 
1069
 
  if (my_b_read(log, (uchar*) magic, sizeof(magic)))
1070
 
  {
1071
 
    *errmsg = "I/O error reading the header from the binary log";
1072
 
    sql_print_error("%s, errno=%d, io cache code=%d", *errmsg, my_errno,
1073
 
                    log->error);
1074
 
    return 1;
1075
 
  }
1076
 
  if (memcmp(magic, BINLOG_MAGIC, sizeof(magic)))
1077
 
  {
1078
 
    *errmsg = "Binlog has bad magic number;  It's not a binary log file that can be used by this version of MySQL";
1079
 
    return 1;
1080
 
  }
1081
 
  return 0;
1082
 
}
1083
 
 
1084
 
 
1085
 
File open_binlog(IO_CACHE *log, const char *log_file_name, const char **errmsg)
1086
 
{
1087
 
  File file;
1088
 
 
1089
 
  if ((file = my_open(log_file_name, O_RDONLY | O_BINARY | O_SHARE, 
1090
 
                      MYF(MY_WME))) < 0)
1091
 
  {
1092
 
    sql_print_error("Failed to open log (file '%s', errno %d)",
1093
 
                    log_file_name, my_errno);
1094
 
    *errmsg = "Could not open log file";
1095
 
    goto err;
1096
 
  }
1097
 
  if (init_io_cache(log, file, IO_SIZE*2, READ_CACHE, 0, 0,
1098
 
                    MYF(MY_WME|MY_DONT_CHECK_FILESIZE)))
1099
 
  {
1100
 
    sql_print_error("Failed to create a cache on log (file '%s')",
1101
 
                    log_file_name);
1102
 
    *errmsg = "Could not open log file";
1103
 
    goto err;
1104
 
  }
1105
 
  if (check_binlog_magic(log,errmsg))
1106
 
    goto err;
1107
 
  return(file);
1108
 
 
1109
 
err:
1110
 
  if (file >= 0)
1111
 
  {
1112
 
    my_close(file,MYF(0));
1113
 
    end_io_cache(log);
1114
 
  }
1115
 
  return(-1);
1116
 
}
1117
 
 
1118
 
 
1119
 
/**
1120
 
  Find a unique filename for 'filename.#'.
1121
 
 
1122
 
  Set '#' to a number as low as possible.
1123
 
 
1124
 
  @return
1125
 
    nonzero if not possible to get unique filename
1126
 
*/
1127
 
 
1128
 
static int find_uniq_filename(char *name)
1129
 
{
1130
 
  long                  number;
1131
 
  uint                  i;
1132
 
  char                  buff[FN_REFLEN];
1133
 
  struct st_my_dir     *dir_info;
1134
 
  register struct fileinfo *file_info;
1135
 
  ulong                 max_found=0;
1136
 
  size_t                buf_length, length;
1137
 
  char                  *start, *end;
1138
 
 
1139
 
  length= dirname_part(buff, name, &buf_length);
1140
 
  start=  name + length;
1141
 
  end=    strend(start);
1142
 
 
1143
 
  *end='.';
1144
 
  length= (size_t) (end-start+1);
1145
 
 
1146
 
  if (!(dir_info = my_dir(buff,MYF(MY_DONT_SORT))))
1147
 
  {                                             // This shouldn't happen
1148
 
    stpcpy(end,".1");                           // use name+1
1149
 
    return(0);
1150
 
  }
1151
 
  file_info= dir_info->dir_entry;
1152
 
  for (i=dir_info->number_off_files ; i-- ; file_info++)
1153
 
  {
1154
 
    if (memcmp(file_info->name, start, length) == 0 &&
1155
 
        test_if_number(file_info->name+length, &number,0))
1156
 
    {
1157
 
      set_if_bigger(max_found,(ulong) number);
1158
 
    }
1159
 
  }
1160
 
  my_dirend(dir_info);
1161
 
 
1162
 
  *end++='.';
1163
 
  sprintf(end,"%06ld",max_found+1);
1164
 
  return(0);
1165
 
}
1166
 
 
1167
 
 
1168
 
void DRIZZLE_LOG::init(enum_log_type log_type_arg,
1169
 
                     enum cache_type io_cache_type_arg)
1170
 
{
1171
 
  log_type= log_type_arg;
1172
 
  io_cache_type= io_cache_type_arg;
1173
 
  return;
1174
 
}
1175
 
 
1176
 
 
1177
 
/*
1178
 
  Open a (new) log file.
1179
 
 
1180
 
  SYNOPSIS
1181
 
    open()
1182
 
 
1183
 
    log_name            The name of the log to open
1184
 
    log_type_arg        The type of the log. E.g. LOG_NORMAL
1185
 
    new_name            The new name for the logfile. This is only needed
1186
 
                        when the method is used to open the binlog file.
1187
 
    io_cache_type_arg   The type of the IO_CACHE to use for this log file
1188
 
 
1189
 
  DESCRIPTION
1190
 
    Open the logfile, init IO_CACHE and write startup messages
1191
 
    (in case of general and slow query logs).
1192
 
 
1193
 
  RETURN VALUES
1194
 
    0   ok
1195
 
    1   error
1196
 
*/
1197
 
 
1198
 
bool DRIZZLE_LOG::open(const char *log_name, enum_log_type log_type_arg,
1199
 
                     const char *new_name, enum cache_type io_cache_type_arg)
1200
 
{
1201
 
  char buff[FN_REFLEN];
1202
 
  File file= -1;
1203
 
  int open_flags= O_CREAT | O_BINARY;
1204
 
 
1205
 
  write_error= 0;
1206
 
 
1207
 
  init(log_type_arg, io_cache_type_arg);
1208
 
 
1209
 
  if (!(name= my_strdup(log_name, MYF(MY_WME))))
1210
 
  {
1211
 
    name= (char *)log_name; // for the error message
1212
 
    goto err;
1213
 
  }
1214
 
 
1215
 
  if (new_name)
1216
 
    stpcpy(log_file_name, new_name);
1217
 
  else if (generate_new_name(log_file_name, name))
1218
 
    goto err;
1219
 
 
1220
 
  if (io_cache_type == SEQ_READ_APPEND)
1221
 
    open_flags |= O_RDWR | O_APPEND;
1222
 
  else
1223
 
    open_flags |= O_WRONLY | (log_type == LOG_BIN ? 0 : O_APPEND);
1224
 
 
1225
 
  db[0]= 0;
1226
 
 
1227
 
  if ((file= my_open(log_file_name, open_flags,
1228
 
                     MYF(MY_WME | ME_WAITTANG))) < 0 ||
1229
 
      init_io_cache(&log_file, file, IO_SIZE, io_cache_type,
1230
 
                    my_tell(file, MYF(MY_WME)), 0,
1231
 
                    MYF(MY_WME | MY_NABP |
1232
 
                        ((log_type == LOG_BIN) ? MY_WAIT_IF_FULL : 0))))
1233
 
    goto err;
1234
 
 
1235
 
  if (log_type == LOG_NORMAL)
1236
 
  {
1237
 
    char *end;
1238
 
    int len=snprintf(buff, sizeof(buff), "%s, Version: %s (%s). "
1239
 
                     "started with:\nTCP Port: %d, Named Pipe: %s\n",
1240
 
                     my_progname, server_version, DRIZZLE_COMPILATION_COMMENT,
1241
 
                     mysqld_port, ""
1242
 
                     );
1243
 
    end= stpncpy(buff + len, "Time                 Id Command    Argument\n",
1244
 
                 sizeof(buff) - len);
1245
 
    if (my_b_write(&log_file, (uchar*) buff, (uint) (end-buff)) ||
1246
 
        flush_io_cache(&log_file))
1247
 
      goto err;
1248
 
  }
1249
 
 
1250
 
  log_state= LOG_OPENED;
1251
 
  return(0);
1252
 
 
1253
 
err:
1254
 
  sql_print_error("Could not use %s for logging (error %d). \
1255
 
Turning logging off for the whole duration of the MySQL server process. \
1256
 
To turn it on again: fix the cause, \
1257
 
shutdown the MySQL server and restart it.", name, errno);
1258
 
  if (file >= 0)
1259
 
    my_close(file, MYF(0));
1260
 
  end_io_cache(&log_file);
1261
 
  safeFree(name);
1262
 
  log_state= LOG_CLOSED;
1263
 
  return(1);
1264
 
}
1265
 
 
1266
 
DRIZZLE_LOG::DRIZZLE_LOG()
1267
 
  : name(0), write_error(false), inited(false), log_type(LOG_UNKNOWN),
1268
 
    log_state(LOG_CLOSED)
1269
 
{
1270
 
  /*
1271
 
    We don't want to initialize LOCK_Log here as such initialization depends on
1272
 
    safe_mutex (when using safe_mutex) which depends on MY_INIT(), which is
1273
 
    called only in main(). Doing initialization here would make it happen
1274
 
    before main().
1275
 
  */
1276
 
  memset(&log_file, 0, sizeof(log_file));
1277
 
}
1278
 
 
1279
 
void DRIZZLE_LOG::init_pthread_objects()
1280
 
{
1281
 
  assert(inited == 0);
1282
 
  inited= 1;
1283
 
  (void) pthread_mutex_init(&LOCK_log, MY_MUTEX_INIT_SLOW);
1284
 
}
1285
 
 
1286
 
/*
1287
 
  Close the log file
1288
 
 
1289
 
  SYNOPSIS
1290
 
    close()
1291
 
    exiting     Bitmask. For the slow and general logs the only used bit is
1292
 
                LOG_CLOSE_TO_BE_OPENED. This is used if we intend to call
1293
 
                open at once after close.
1294
 
 
1295
 
  NOTES
1296
 
    One can do an open on the object at once after doing a close.
1297
 
    The internal structures are not freed until cleanup() is called
1298
 
*/
1299
 
 
1300
 
void DRIZZLE_LOG::close(uint exiting)
1301
 
{                                       // One can't set log_type here!
1302
 
  if (log_state == LOG_OPENED)
1303
 
  {
1304
 
    end_io_cache(&log_file);
1305
 
 
1306
 
    if (my_sync(log_file.file, MYF(MY_WME)) && ! write_error)
1307
 
    {
1308
 
      write_error= 1;
1309
 
      sql_print_error(ER(ER_ERROR_ON_WRITE), name, errno);
1310
 
    }
1311
 
 
1312
 
    if (my_close(log_file.file, MYF(MY_WME)) && ! write_error)
1313
 
    {
1314
 
      write_error= 1;
1315
 
      sql_print_error(ER(ER_ERROR_ON_WRITE), name, errno);
1316
 
    }
1317
 
  }
1318
 
 
1319
 
  log_state= (exiting & LOG_CLOSE_TO_BE_OPENED) ? LOG_TO_BE_OPENED : LOG_CLOSED;
1320
 
  safeFree(name);
1321
 
  return;
1322
 
}
1323
 
 
1324
 
/** This is called only once. */
1325
 
 
1326
 
void DRIZZLE_LOG::cleanup()
1327
 
{
1328
 
  if (inited)
1329
 
  {
1330
 
    inited= 0;
1331
 
    (void) pthread_mutex_destroy(&LOCK_log);
1332
 
    close(0);
1333
 
  }
1334
 
  return;
1335
 
}
1336
 
 
1337
 
 
1338
 
int DRIZZLE_LOG::generate_new_name(char *new_name, const char *log_name)
1339
 
{
1340
 
  fn_format(new_name, log_name, mysql_data_home, "", 4);
1341
 
  if (log_type == LOG_BIN)
1342
 
  {
1343
 
    if (!fn_ext(log_name)[0])
1344
 
    {
1345
 
      if (find_uniq_filename(new_name))
1346
 
      {
1347
 
        sql_print_error(ER(ER_NO_UNIQUE_LOGFILE), log_name);
1348
 
        return 1;
1349
 
      }
1350
 
    }
1351
 
  }
1352
 
  return 0;
1353
 
}
1354
 
 
1355
 
 
1356
 
/*
1357
 
  Reopen the log file
1358
 
 
1359
 
  SYNOPSIS
1360
 
    reopen_file()
1361
 
 
1362
 
  DESCRIPTION
1363
 
    Reopen the log file. The method is used during FLUSH LOGS
1364
 
    and locks LOCK_log mutex
1365
 
*/
1366
 
 
1367
 
 
1368
 
void DRIZZLE_QUERY_LOG::reopen_file()
1369
 
{
1370
 
  char *save_name;
1371
 
 
1372
 
  if (!is_open())
1373
 
  {
1374
 
    return;
1375
 
  }
1376
 
 
1377
 
  pthread_mutex_lock(&LOCK_log);
1378
 
 
1379
 
  save_name= name;
1380
 
  name= 0;                              // Don't free name
1381
 
  close(LOG_CLOSE_TO_BE_OPENED);
1382
 
 
1383
 
  /*
1384
 
     Note that at this point, log_state != LOG_CLOSED (important for is_open()).
1385
 
  */
1386
 
 
1387
 
  open(save_name, log_type, 0, io_cache_type);
1388
 
  my_free(save_name, MYF(0));
1389
 
 
1390
 
  pthread_mutex_unlock(&LOCK_log);
1391
 
 
1392
 
  return;
1393
 
}
1394
 
 
1395
 
 
1396
 
/*
1397
 
  Write a command to traditional general log file
1398
 
 
1399
 
  SYNOPSIS
1400
 
    write()
1401
 
 
1402
 
    event_time        command start timestamp
1403
 
    user_host         the pointer to the string with user@host info
1404
 
    user_host_len     length of the user_host string. this is computed once
1405
 
                      and passed to all general log  event handlers
1406
 
    thread_id         Id of the thread, issued a query
1407
 
    command_type      the type of the command being logged
1408
 
    command_type_len  the length of the string above
1409
 
    sql_text          the very text of the query being executed
1410
 
    sql_text_len      the length of sql_text string
1411
 
 
1412
 
  DESCRIPTION
1413
 
 
1414
 
   Log given command to to normal (not rotable) log file
1415
 
 
1416
 
  RETURN
1417
 
    FASE - OK
1418
 
    TRUE - error occured
1419
 
*/
1420
 
 
1421
 
bool DRIZZLE_QUERY_LOG::write(time_t event_time,
1422
 
                            const char *user_host __attribute__((unused)),
1423
 
                            uint user_host_len __attribute__((unused)),
1424
 
                            int thread_id,
1425
 
                            const char *command_type, uint command_type_len,
1426
 
                            const char *sql_text, uint sql_text_len)
1427
 
{
1428
 
  char buff[32];
1429
 
  uint length= 0;
1430
 
  char local_time_buff[MAX_TIME_SIZE];
1431
 
  struct tm start;
1432
 
  uint time_buff_len= 0;
1433
 
 
1434
 
  (void) pthread_mutex_lock(&LOCK_log);
1435
 
 
1436
 
  /* Test if someone closed between the is_open test and lock */
1437
 
  if (is_open())
1438
 
  {
1439
 
    /* Note that my_b_write() assumes it knows the length for this */
1440
 
      if (event_time != last_time)
1441
 
      {
1442
 
        last_time= event_time;
1443
 
 
1444
 
        localtime_r(&event_time, &start);
1445
 
 
1446
 
        time_buff_len= snprintf(local_time_buff, MAX_TIME_SIZE,
1447
 
                                "%02d%02d%02d %2d:%02d:%02d",
1448
 
                                start.tm_year % 100, start.tm_mon + 1,
1449
 
                                start.tm_mday, start.tm_hour,
1450
 
                                start.tm_min, start.tm_sec);
1451
 
 
1452
 
        if (my_b_write(&log_file, (uchar*) local_time_buff, time_buff_len))
1453
 
          goto err;
1454
 
      }
1455
 
      else
1456
 
        if (my_b_write(&log_file, (uchar*) "\t\t" ,2) < 0)
1457
 
          goto err;
1458
 
 
1459
 
      /* command_type, thread_id */
1460
 
      length= snprintf(buff, 32, "%5ld ", (long) thread_id);
1461
 
 
1462
 
    if (my_b_write(&log_file, (uchar*) buff, length))
1463
 
      goto err;
1464
 
 
1465
 
    if (my_b_write(&log_file, (uchar*) command_type, command_type_len))
1466
 
      goto err;
1467
 
 
1468
 
    if (my_b_write(&log_file, (uchar*) "\t", 1))
1469
 
      goto err;
1470
 
 
1471
 
    /* sql_text */
1472
 
    if (my_b_write(&log_file, (uchar*) sql_text, sql_text_len))
1473
 
      goto err;
1474
 
 
1475
 
    if (my_b_write(&log_file, (uchar*) "\n", 1) ||
1476
 
        flush_io_cache(&log_file))
1477
 
      goto err;
1478
 
  }
1479
 
 
1480
 
  (void) pthread_mutex_unlock(&LOCK_log);
1481
 
  return false;
1482
 
err:
1483
 
 
1484
 
  if (!write_error)
1485
 
  {
1486
 
    write_error= 1;
1487
 
    sql_print_error(ER(ER_ERROR_ON_WRITE), name, errno);
1488
 
  }
1489
 
  (void) pthread_mutex_unlock(&LOCK_log);
1490
 
  return true;
1491
 
}
1492
 
 
1493
 
 
1494
 
/*
1495
 
  Log a query to the traditional slow log file
1496
 
 
1497
 
  SYNOPSIS
1498
 
    write()
1499
 
 
1500
 
    thd               THD of the query
1501
 
    current_time      current timestamp
1502
 
    query_start_arg   command start timestamp
1503
 
    user_host         the pointer to the string with user@host info
1504
 
    user_host_len     length of the user_host string. this is computed once
1505
 
                      and passed to all general log event handlers
1506
 
    query_utime       Amount of time the query took to execute (in microseconds)
1507
 
    lock_utime        Amount of time the query was locked (in microseconds)
1508
 
    is_command        The flag, which determines, whether the sql_text is a
1509
 
                      query or an administrator command.
1510
 
    sql_text          the very text of the query or administrator command
1511
 
                      processed
1512
 
    sql_text_len      the length of sql_text string
1513
 
 
1514
 
  DESCRIPTION
1515
 
 
1516
 
   Log a query to the slow log file.
1517
 
 
1518
 
  RETURN
1519
 
    FALSE - OK
1520
 
    TRUE - error occured
1521
 
*/
1522
 
 
1523
 
bool DRIZZLE_QUERY_LOG::write(THD *thd, time_t current_time,
1524
 
                            time_t query_start_arg __attribute__((unused)),
1525
 
                            const char *user_host,
1526
 
                            uint user_host_len, uint64_t query_utime,
1527
 
                            uint64_t lock_utime, bool is_command,
1528
 
                            const char *sql_text, uint sql_text_len)
1529
 
{
1530
 
  bool error= 0;
1531
 
 
1532
 
  (void) pthread_mutex_lock(&LOCK_log);
1533
 
 
1534
 
  if (!is_open())
1535
 
  {
1536
 
    (void) pthread_mutex_unlock(&LOCK_log);
1537
 
    return(0);
1538
 
  }
1539
 
 
1540
 
  if (is_open())
1541
 
  {                                             // Safety agains reopen
1542
 
    int tmp_errno= 0;
1543
 
    char buff[80], *end;
1544
 
    char query_time_buff[22+7], lock_time_buff[22+7];
1545
 
    uint buff_len;
1546
 
    end= buff;
1547
 
 
1548
 
    if (!(specialflag & SPECIAL_SHORT_LOG_FORMAT))
1549
 
    {
1550
 
      if (current_time != last_time)
1551
 
      {
1552
 
        last_time= current_time;
1553
 
        struct tm start;
1554
 
        localtime_r(&current_time, &start);
1555
 
 
1556
 
        buff_len= snprintf(buff, sizeof buff,
1557
 
                           "# Time: %02d%02d%02d %2d:%02d:%02d\n",
1558
 
                           start.tm_year % 100, start.tm_mon + 1,
1559
 
                           start.tm_mday, start.tm_hour,
1560
 
                           start.tm_min, start.tm_sec);
1561
 
 
1562
 
        /* Note that my_b_write() assumes it knows the length for this */
1563
 
        if (my_b_write(&log_file, (uchar*) buff, buff_len))
1564
 
          tmp_errno= errno;
1565
 
      }
1566
 
      const uchar uh[]= "# User@Host: ";
1567
 
      if (my_b_write(&log_file, uh, sizeof(uh) - 1))
1568
 
        tmp_errno= errno;
1569
 
      if (my_b_write(&log_file, (uchar*) user_host, user_host_len))
1570
 
        tmp_errno= errno;
1571
 
      if (my_b_write(&log_file, (uchar*) "\n", 1))
1572
 
        tmp_errno= errno;
1573
 
    }
1574
 
    /* For slow query log */
1575
 
    sprintf(query_time_buff, "%.6f", uint64_t2double(query_utime)/1000000.0);
1576
 
    sprintf(lock_time_buff,  "%.6f", uint64_t2double(lock_utime)/1000000.0);
1577
 
    if (my_b_printf(&log_file,
1578
 
                    "# Query_time: %s  Lock_time: %s"
1579
 
                    " Rows_sent: %lu  Rows_examined: %lu\n",
1580
 
                    query_time_buff, lock_time_buff,
1581
 
                    (ulong) thd->sent_row_count,
1582
 
                    (ulong) thd->examined_row_count) == (uint) -1)
1583
 
      tmp_errno= errno;
1584
 
    if (thd->db && strcmp(thd->db, db))
1585
 
    {                                           // Database changed
1586
 
      if (my_b_printf(&log_file,"use %s;\n",thd->db) == (uint) -1)
1587
 
        tmp_errno= errno;
1588
 
      stpcpy(db,thd->db);
1589
 
    }
1590
 
    if (thd->stmt_depends_on_first_successful_insert_id_in_prev_stmt)
1591
 
    {
1592
 
      end=stpcpy(end, ",last_insert_id=");
1593
 
      end=int64_t10_to_str((int64_t)
1594
 
                            thd->first_successful_insert_id_in_prev_stmt_for_binlog,
1595
 
                            end, -10);
1596
 
    }
1597
 
    // Save value if we do an insert.
1598
 
    if (thd->auto_inc_intervals_in_cur_stmt_for_binlog.nb_elements() > 0)
1599
 
    {
1600
 
      if (!(specialflag & SPECIAL_SHORT_LOG_FORMAT))
1601
 
      {
1602
 
        end=stpcpy(end,",insert_id=");
1603
 
        end=int64_t10_to_str((int64_t)
1604
 
                              thd->auto_inc_intervals_in_cur_stmt_for_binlog.minimum(),
1605
 
                              end, -10);
1606
 
      }
1607
 
    }
1608
 
 
1609
 
    /*
1610
 
      This info used to show up randomly, depending on whether the query
1611
 
      checked the query start time or not. now we always write current
1612
 
      timestamp to the slow log
1613
 
    */
1614
 
    end= stpcpy(end, ",timestamp=");
1615
 
    end= int10_to_str((long) current_time, end, 10);
1616
 
 
1617
 
    if (end != buff)
1618
 
    {
1619
 
      *end++=';';
1620
 
      *end='\n';
1621
 
      if (my_b_write(&log_file, (uchar*) "SET ", 4) ||
1622
 
          my_b_write(&log_file, (uchar*) buff + 1, (uint) (end-buff)))
1623
 
        tmp_errno= errno;
1624
 
    }
1625
 
    if (is_command)
1626
 
    {
1627
 
      end= strxmov(buff, "# administrator command: ", NullS);
1628
 
      buff_len= (ulong) (end - buff);
1629
 
      my_b_write(&log_file, (uchar*) buff, buff_len);
1630
 
    }
1631
 
    if (my_b_write(&log_file, (uchar*) sql_text, sql_text_len) ||
1632
 
        my_b_write(&log_file, (uchar*) ";\n",2) ||
1633
 
        flush_io_cache(&log_file))
1634
 
      tmp_errno= errno;
1635
 
    if (tmp_errno)
1636
 
    {
1637
 
      error= 1;
1638
 
      if (! write_error)
1639
 
      {
1640
 
        write_error= 1;
1641
 
        sql_print_error(ER(ER_ERROR_ON_WRITE), name, error);
1642
 
      }
1643
 
    }
1644
 
  }
1645
 
  (void) pthread_mutex_unlock(&LOCK_log);
1646
 
  return(error);
1647
 
}
1648
 
 
1649
 
 
1650
 
/**
1651
 
  @todo
1652
 
  The following should be using fn_format();  We just need to
1653
 
  first change fn_format() to cut the file name if it's too long.
1654
 
*/
1655
 
const char *DRIZZLE_LOG::generate_name(const char *log_name,
1656
 
                                      const char *suffix,
1657
 
                                      bool strip_ext, char *buff)
1658
 
{
1659
 
  if (!log_name || !log_name[0])
1660
 
  {
1661
 
    strmake(buff, pidfile_name, FN_REFLEN - strlen(suffix) - 1);
1662
 
    return (const char *)
1663
 
      fn_format(buff, buff, "", suffix, MYF(MY_REPLACE_EXT|MY_REPLACE_DIR));
1664
 
  }
1665
 
  // get rid of extension if the log is binary to avoid problems
1666
 
  if (strip_ext)
1667
 
  {
1668
 
    char *p= fn_ext(log_name);
1669
 
    uint length= (uint) (p - log_name);
1670
 
    strmake(buff, log_name, min(length, (uint)FN_REFLEN));
1671
 
    return (const char*)buff;
1672
 
  }
1673
 
  return log_name;
1674
 
}
1675
 
 
1676
 
 
1677
 
 
1678
 
DRIZZLE_BIN_LOG::DRIZZLE_BIN_LOG()
1679
 
  :bytes_written(0), prepared_xids(0), file_id(1), open_count(1),
1680
 
   need_start_event(true), m_table_map_version(0),
1681
 
   description_event_for_exec(0), description_event_for_queue(0)
1682
 
{
1683
 
  /*
1684
 
    We don't want to initialize locks here as such initialization depends on
1685
 
    safe_mutex (when using safe_mutex) which depends on MY_INIT(), which is
1686
 
    called only in main(). Doing initialization here would make it happen
1687
 
    before main().
1688
 
  */
1689
 
  index_file_name[0] = 0;
1690
 
  memset(&index_file, 0, sizeof(index_file));
1691
 
}
1692
 
 
1693
 
/* this is called only once */
1694
 
 
1695
 
void DRIZZLE_BIN_LOG::cleanup()
1696
 
{
1697
 
  if (inited)
1698
 
  {
1699
 
    inited= 0;
1700
 
    close(LOG_CLOSE_INDEX|LOG_CLOSE_STOP_EVENT);
1701
 
    delete description_event_for_queue;
1702
 
    delete description_event_for_exec;
1703
 
    (void) pthread_mutex_destroy(&LOCK_log);
1704
 
    (void) pthread_mutex_destroy(&LOCK_index);
1705
 
    (void) pthread_cond_destroy(&update_cond);
1706
 
  }
1707
 
  return;
1708
 
}
1709
 
 
1710
 
 
1711
 
/* Init binlog-specific vars */
1712
 
void DRIZZLE_BIN_LOG::init(bool no_auto_events_arg, ulong max_size_arg)
1713
 
{
1714
 
  no_auto_events= no_auto_events_arg;
1715
 
  max_size= max_size_arg;
1716
 
  return;
1717
 
}
1718
 
 
1719
 
 
1720
 
void DRIZZLE_BIN_LOG::init_pthread_objects()
1721
 
{
1722
 
  assert(inited == 0);
1723
 
  inited= 1;
1724
 
  (void) pthread_mutex_init(&LOCK_log, MY_MUTEX_INIT_SLOW);
1725
 
  (void) pthread_mutex_init(&LOCK_index, MY_MUTEX_INIT_SLOW);
1726
 
  (void) pthread_cond_init(&update_cond, 0);
1727
 
}
1728
 
 
1729
 
 
1730
 
bool DRIZZLE_BIN_LOG::open_index_file(const char *index_file_name_arg,
1731
 
                                const char *log_name)
1732
 
{
1733
 
  File index_file_nr= -1;
1734
 
  assert(!my_b_inited(&index_file));
1735
 
 
1736
 
  /*
1737
 
    First open of this class instance
1738
 
    Create an index file that will hold all file names uses for logging.
1739
 
    Add new entries to the end of it.
1740
 
  */
1741
 
  myf opt= MY_UNPACK_FILENAME;
1742
 
  if (!index_file_name_arg)
1743
 
  {
1744
 
    index_file_name_arg= log_name;    // Use same basename for index file
1745
 
    opt= MY_UNPACK_FILENAME | MY_REPLACE_EXT;
1746
 
  }
1747
 
  fn_format(index_file_name, index_file_name_arg, mysql_data_home,
1748
 
            ".index", opt);
1749
 
  if ((index_file_nr= my_open(index_file_name,
1750
 
                              O_RDWR | O_CREAT | O_BINARY ,
1751
 
                              MYF(MY_WME))) < 0 ||
1752
 
       my_sync(index_file_nr, MYF(MY_WME)) ||
1753
 
       init_io_cache(&index_file, index_file_nr,
1754
 
                     IO_SIZE, WRITE_CACHE,
1755
 
                     my_seek(index_file_nr,0L,MY_SEEK_END,MYF(0)),
1756
 
                        0, MYF(MY_WME | MY_WAIT_IF_FULL)))
1757
 
  {
1758
 
    /*
1759
 
      TODO: all operations creating/deleting the index file or a log, should
1760
 
      call my_sync_dir() or my_sync_dir_by_file() to be durable.
1761
 
      TODO: file creation should be done with my_create() not my_open().
1762
 
    */
1763
 
    if (index_file_nr >= 0)
1764
 
      my_close(index_file_nr,MYF(0));
1765
 
    return true;
1766
 
  }
1767
 
  return false;
1768
 
}
1769
 
 
1770
 
 
1771
 
/**
1772
 
  Open a (new) binlog file.
1773
 
 
1774
 
  - Open the log file and the index file. Register the new
1775
 
  file name in it
1776
 
  - When calling this when the file is in use, you must have a locks
1777
 
  on LOCK_log and LOCK_index.
1778
 
 
1779
 
  @retval
1780
 
    0   ok
1781
 
  @retval
1782
 
    1   error
1783
 
*/
1784
 
 
1785
 
bool DRIZZLE_BIN_LOG::open(const char *log_name,
1786
 
                         enum_log_type log_type_arg,
1787
 
                         const char *new_name,
1788
 
                         enum cache_type io_cache_type_arg,
1789
 
                         bool no_auto_events_arg,
1790
 
                         ulong max_size_arg,
1791
 
                         bool null_created_arg)
1792
 
{
1793
 
  File file= -1;
1794
 
 
1795
 
  write_error=0;
1796
 
 
1797
 
  /* open the main log file */
1798
 
  if (DRIZZLE_LOG::open(log_name, log_type_arg, new_name, io_cache_type_arg))
1799
 
    return(1);                            /* all warnings issued */
1800
 
 
1801
 
  init(no_auto_events_arg, max_size_arg);
1802
 
 
1803
 
  open_count++;
1804
 
 
1805
 
  assert(log_type == LOG_BIN);
1806
 
 
1807
 
  {
1808
 
    bool write_file_name_to_index_file=0;
1809
 
 
1810
 
    if (!my_b_filelength(&log_file))
1811
 
    {
1812
 
      /*
1813
 
        The binary log file was empty (probably newly created)
1814
 
        This is the normal case and happens when the user doesn't specify
1815
 
        an extension for the binary log files.
1816
 
        In this case we write a standard header to it.
1817
 
      */
1818
 
      if (my_b_safe_write(&log_file, (uchar*) BINLOG_MAGIC,
1819
 
                          BIN_LOG_HEADER_SIZE))
1820
 
        goto err;
1821
 
      bytes_written+= BIN_LOG_HEADER_SIZE;
1822
 
      write_file_name_to_index_file= 1;
1823
 
    }
1824
 
 
1825
 
    assert(my_b_inited(&index_file) != 0);
1826
 
    reinit_io_cache(&index_file, WRITE_CACHE,
1827
 
                    my_b_filelength(&index_file), 0, 0);
1828
 
    if (need_start_event && !no_auto_events)
1829
 
    {
1830
 
      /*
1831
 
        In 4.x we set need_start_event=0 here, but in 5.0 we want a Start event
1832
 
        even if this is not the very first binlog.
1833
 
      */
1834
 
      Format_description_log_event s(BINLOG_VERSION);
1835
 
      /*
1836
 
        don't set LOG_EVENT_BINLOG_IN_USE_F for SEQ_READ_APPEND io_cache
1837
 
        as we won't be able to reset it later
1838
 
      */
1839
 
      if (io_cache_type == WRITE_CACHE)
1840
 
        s.flags|= LOG_EVENT_BINLOG_IN_USE_F;
1841
 
      if (!s.is_valid())
1842
 
        goto err;
1843
 
      s.dont_set_created= null_created_arg;
1844
 
      if (s.write(&log_file))
1845
 
        goto err;
1846
 
      bytes_written+= s.data_written;
1847
 
    }
1848
 
    if (description_event_for_queue &&
1849
 
        description_event_for_queue->binlog_version>=4)
1850
 
    {
1851
 
      /*
1852
 
        This is a relay log written to by the I/O slave thread.
1853
 
        Write the event so that others can later know the format of this relay
1854
 
        log.
1855
 
        Note that this event is very close to the original event from the
1856
 
        master (it has binlog version of the master, event types of the
1857
 
        master), so this is suitable to parse the next relay log's event. It
1858
 
        has been produced by
1859
 
        Format_description_log_event::Format_description_log_event(char* buf,).
1860
 
        Why don't we want to write the description_event_for_queue if this
1861
 
        event is for format<4 (3.23 or 4.x): this is because in that case, the
1862
 
        description_event_for_queue describes the data received from the
1863
 
        master, but not the data written to the relay log (*conversion*),
1864
 
        which is in format 4 (slave's).
1865
 
      */
1866
 
      /*
1867
 
        Set 'created' to 0, so that in next relay logs this event does not
1868
 
        trigger cleaning actions on the slave in
1869
 
        Format_description_log_event::apply_event_impl().
1870
 
      */
1871
 
      description_event_for_queue->created= 0;
1872
 
      /* Don't set log_pos in event header */
1873
 
      description_event_for_queue->artificial_event=1;
1874
 
 
1875
 
      if (description_event_for_queue->write(&log_file))
1876
 
        goto err;
1877
 
      bytes_written+= description_event_for_queue->data_written;
1878
 
    }
1879
 
    if (flush_io_cache(&log_file) ||
1880
 
        my_sync(log_file.file, MYF(MY_WME)))
1881
 
      goto err;
1882
 
 
1883
 
    if (write_file_name_to_index_file)
1884
 
    {
1885
 
      /*
1886
 
        As this is a new log file, we write the file name to the index
1887
 
        file. As every time we write to the index file, we sync it.
1888
 
      */
1889
 
      if (my_b_write(&index_file, (uchar*) log_file_name,
1890
 
                     strlen(log_file_name)) ||
1891
 
          my_b_write(&index_file, (uchar*) "\n", 1) ||
1892
 
          flush_io_cache(&index_file) ||
1893
 
          my_sync(index_file.file, MYF(MY_WME)))
1894
 
        goto err;
1895
 
    }
1896
 
  }
1897
 
  log_state= LOG_OPENED;
1898
 
 
1899
 
  return(0);
1900
 
 
1901
 
err:
1902
 
  sql_print_error("Could not use %s for logging (error %d). \
1903
 
Turning logging off for the whole duration of the MySQL server process. \
1904
 
To turn it on again: fix the cause, \
1905
 
shutdown the MySQL server and restart it.", name, errno);
1906
 
  if (file >= 0)
1907
 
    my_close(file,MYF(0));
1908
 
  end_io_cache(&log_file);
1909
 
  end_io_cache(&index_file);
1910
 
  safeFree(name);
1911
 
  log_state= LOG_CLOSED;
1912
 
  return(1);
1913
 
}
1914
 
 
1915
 
 
1916
 
int DRIZZLE_BIN_LOG::get_current_log(LOG_INFO* linfo)
1917
 
{
1918
 
  pthread_mutex_lock(&LOCK_log);
1919
 
  int ret = raw_get_current_log(linfo);
1920
 
  pthread_mutex_unlock(&LOCK_log);
1921
 
  return ret;
1922
 
}
1923
 
 
1924
 
int DRIZZLE_BIN_LOG::raw_get_current_log(LOG_INFO* linfo)
1925
 
{
1926
 
  strmake(linfo->log_file_name, log_file_name, sizeof(linfo->log_file_name)-1);
1927
 
  linfo->pos = my_b_tell(&log_file);
1928
 
  return 0;
1929
 
}
1930
 
 
1931
 
/**
1932
 
  Move all data up in a file in an filename index file.
1933
 
 
1934
 
    We do the copy outside of the IO_CACHE as the cache buffers would just
1935
 
    make things slower and more complicated.
1936
 
    In most cases the copy loop should only do one read.
1937
 
 
1938
 
  @param index_file                     File to move
1939
 
  @param offset                 Move everything from here to beginning
1940
 
 
1941
 
  @note
1942
 
    File will be truncated to be 'offset' shorter or filled up with newlines
1943
 
 
1944
 
  @retval
1945
 
    0   ok
1946
 
*/
1947
 
 
1948
 
#ifdef HAVE_REPLICATION
1949
 
 
1950
 
static bool copy_up_file_and_fill(IO_CACHE *index_file, my_off_t offset)
1951
 
{
1952
 
  int bytes_read;
1953
 
  my_off_t init_offset= offset;
1954
 
  File file= index_file->file;
1955
 
  uchar io_buf[IO_SIZE*2];
1956
 
 
1957
 
  for (;; offset+= bytes_read)
1958
 
  {
1959
 
    (void) my_seek(file, offset, MY_SEEK_SET, MYF(0));
1960
 
    if ((bytes_read= (int) my_read(file, io_buf, sizeof(io_buf), MYF(MY_WME)))
1961
 
        < 0)
1962
 
      goto err;
1963
 
    if (!bytes_read)
1964
 
      break;                                    // end of file
1965
 
    (void) my_seek(file, offset-init_offset, MY_SEEK_SET, MYF(0));
1966
 
    if (my_write(file, io_buf, bytes_read, MYF(MY_WME | MY_NABP)))
1967
 
      goto err;
1968
 
  }
1969
 
  /* The following will either truncate the file or fill the end with \n' */
1970
 
  if (ftruncate(file, offset - init_offset) || my_sync(file, MYF(MY_WME)))
1971
 
    goto err;
1972
 
 
1973
 
  /* Reset data in old index cache */
1974
 
  reinit_io_cache(index_file, READ_CACHE, (my_off_t) 0, 0, 1);
1975
 
  return(0);
1976
 
 
1977
 
err:
1978
 
  return(1);
1979
 
}
1980
 
 
1981
 
#endif /* HAVE_REPLICATION */
1982
 
 
1983
 
/**
1984
 
  Find the position in the log-index-file for the given log name.
1985
 
 
1986
 
  @param linfo          Store here the found log file name and position to
1987
 
                       the NEXT log file name in the index file.
1988
 
  @param log_name       Filename to find in the index file.
1989
 
                       Is a null pointer if we want to read the first entry
1990
 
  @param need_lock      Set this to 1 if the parent doesn't already have a
1991
 
                       lock on LOCK_index
1992
 
 
1993
 
  @note
1994
 
    On systems without the truncate function the file will end with one or
1995
 
    more empty lines.  These will be ignored when reading the file.
1996
 
 
1997
 
  @retval
1998
 
    0                   ok
1999
 
  @retval
2000
 
    LOG_INFO_EOF                End of log-index-file found
2001
 
  @retval
2002
 
    LOG_INFO_IO         Got IO error while reading file
2003
 
*/
2004
 
 
2005
 
int DRIZZLE_BIN_LOG::find_log_pos(LOG_INFO *linfo, const char *log_name,
2006
 
                            bool need_lock)
2007
 
{
2008
 
  int error= 0;
2009
 
  char *fname= linfo->log_file_name;
2010
 
  uint log_name_len= log_name ? (uint) strlen(log_name) : 0;
2011
 
 
2012
 
  /*
2013
 
    Mutex needed because we need to make sure the file pointer does not
2014
 
    move from under our feet
2015
 
  */
2016
 
  if (need_lock)
2017
 
    pthread_mutex_lock(&LOCK_index);
2018
 
  safe_mutex_assert_owner(&LOCK_index);
2019
 
 
2020
 
  /* As the file is flushed, we can't get an error here */
2021
 
  (void) reinit_io_cache(&index_file, READ_CACHE, (my_off_t) 0, 0, 0);
2022
 
 
2023
 
  for (;;)
2024
 
  {
2025
 
    uint length;
2026
 
    my_off_t offset= my_b_tell(&index_file);
2027
 
    /* If we get 0 or 1 characters, this is the end of the file */
2028
 
 
2029
 
    if ((length= my_b_gets(&index_file, fname, FN_REFLEN)) <= 1)
2030
 
    {
2031
 
      /* Did not find the given entry; Return not found or error */
2032
 
      error= !index_file.error ? LOG_INFO_EOF : LOG_INFO_IO;
2033
 
      break;
2034
 
    }
2035
 
 
2036
 
    // if the log entry matches, null string matching anything
2037
 
    if (!log_name ||
2038
 
        (log_name_len == length-1 && fname[log_name_len] == '\n' &&
2039
 
         !memcmp(fname, log_name, log_name_len)))
2040
 
    {
2041
 
      fname[length-1]=0;                        // remove last \n
2042
 
      linfo->index_file_start_offset= offset;
2043
 
      linfo->index_file_offset = my_b_tell(&index_file);
2044
 
      break;
2045
 
    }
2046
 
  }
2047
 
 
2048
 
  if (need_lock)
2049
 
    pthread_mutex_unlock(&LOCK_index);
2050
 
  return(error);
2051
 
}
2052
 
 
2053
 
 
2054
 
/**
2055
 
  Find the position in the log-index-file for the given log name.
2056
 
 
2057
 
  @param
2058
 
    linfo               Store here the next log file name and position to
2059
 
                        the file name after that.
2060
 
  @param
2061
 
    need_lock           Set this to 1 if the parent doesn't already have a
2062
 
                        lock on LOCK_index
2063
 
 
2064
 
  @note
2065
 
    - Before calling this function, one has to call find_log_pos()
2066
 
    to set up 'linfo'
2067
 
    - Mutex needed because we need to make sure the file pointer does not move
2068
 
    from under our feet
2069
 
 
2070
 
  @retval
2071
 
    0                   ok
2072
 
  @retval
2073
 
    LOG_INFO_EOF                End of log-index-file found
2074
 
  @retval
2075
 
    LOG_INFO_IO         Got IO error while reading file
2076
 
*/
2077
 
 
2078
 
int DRIZZLE_BIN_LOG::find_next_log(LOG_INFO* linfo, bool need_lock)
2079
 
{
2080
 
  int error= 0;
2081
 
  uint length;
2082
 
  char *fname= linfo->log_file_name;
2083
 
 
2084
 
  if (need_lock)
2085
 
    pthread_mutex_lock(&LOCK_index);
2086
 
  safe_mutex_assert_owner(&LOCK_index);
2087
 
 
2088
 
  /* As the file is flushed, we can't get an error here */
2089
 
  (void) reinit_io_cache(&index_file, READ_CACHE, linfo->index_file_offset, 0,
2090
 
                         0);
2091
 
 
2092
 
  linfo->index_file_start_offset= linfo->index_file_offset;
2093
 
  if ((length=my_b_gets(&index_file, fname, FN_REFLEN)) <= 1)
2094
 
  {
2095
 
    error = !index_file.error ? LOG_INFO_EOF : LOG_INFO_IO;
2096
 
    goto err;
2097
 
  }
2098
 
  fname[length-1]=0;                            // kill \n
2099
 
  linfo->index_file_offset = my_b_tell(&index_file);
2100
 
 
2101
 
err:
2102
 
  if (need_lock)
2103
 
    pthread_mutex_unlock(&LOCK_index);
2104
 
  return error;
2105
 
}
2106
 
 
2107
 
 
2108
 
/**
2109
 
  Delete all logs refered to in the index file.
2110
 
  Start writing to a new log file.
2111
 
 
2112
 
  The new index file will only contain this file.
2113
 
 
2114
 
  @param thd            Thread
2115
 
 
2116
 
  @note
2117
 
    If not called from slave thread, write start event to new log
2118
 
 
2119
 
  @retval
2120
 
    0   ok
2121
 
  @retval
2122
 
    1   error
2123
 
*/
2124
 
 
2125
 
bool DRIZZLE_BIN_LOG::reset_logs(THD* thd)
2126
 
{
2127
 
  LOG_INFO linfo;
2128
 
  bool error=0;
2129
 
  const char* save_name;
2130
 
 
2131
 
  ha_reset_logs(thd);
2132
 
  /*
2133
 
    We need to get both locks to be sure that no one is trying to
2134
 
    write to the index log file.
2135
 
  */
2136
 
  pthread_mutex_lock(&LOCK_log);
2137
 
  pthread_mutex_lock(&LOCK_index);
2138
 
 
2139
 
  /*
2140
 
    The following mutex is needed to ensure that no threads call
2141
 
    'delete thd' as we would then risk missing a 'rollback' from this
2142
 
    thread. If the transaction involved MyISAM tables, it should go
2143
 
    into binlog even on rollback.
2144
 
  */
2145
 
  VOID(pthread_mutex_lock(&LOCK_thread_count));
2146
 
 
2147
 
  /* Save variables so that we can reopen the log */
2148
 
  save_name=name;
2149
 
  name=0;                                       // Protect against free
2150
 
  close(LOG_CLOSE_TO_BE_OPENED);
2151
 
 
2152
 
  /* First delete all old log files */
2153
 
 
2154
 
  if (find_log_pos(&linfo, NullS, 0))
2155
 
  {
2156
 
    error=1;
2157
 
    goto err;
2158
 
  }
2159
 
 
2160
 
  for (;;)
2161
 
  {
2162
 
    if ((error= my_delete_allow_opened(linfo.log_file_name, MYF(0))) != 0)
2163
 
    {
2164
 
      if (my_errno == ENOENT) 
2165
 
      {
2166
 
        push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
2167
 
                            ER_LOG_PURGE_NO_FILE, ER(ER_LOG_PURGE_NO_FILE),
2168
 
                            linfo.log_file_name);
2169
 
        sql_print_information("Failed to delete file '%s'",
2170
 
                              linfo.log_file_name);
2171
 
        my_errno= 0;
2172
 
        error= 0;
2173
 
      }
2174
 
      else
2175
 
      {
2176
 
        push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
2177
 
                            ER_BINLOG_PURGE_FATAL_ERR,
2178
 
                            "a problem with deleting %s; "
2179
 
                            "consider examining correspondence "
2180
 
                            "of your binlog index file "
2181
 
                            "to the actual binlog files",
2182
 
                            linfo.log_file_name);
2183
 
        error= 1;
2184
 
        goto err;
2185
 
      }
2186
 
    }
2187
 
    if (find_next_log(&linfo, 0))
2188
 
      break;
2189
 
  }
2190
 
 
2191
 
  /* Start logging with a new file */
2192
 
  close(LOG_CLOSE_INDEX);
2193
 
  if ((error= my_delete_allow_opened(index_file_name, MYF(0)))) // Reset (open will update)
2194
 
  {
2195
 
    if (my_errno == ENOENT) 
2196
 
    {
2197
 
      push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
2198
 
                          ER_LOG_PURGE_NO_FILE, ER(ER_LOG_PURGE_NO_FILE),
2199
 
                          index_file_name);
2200
 
      sql_print_information("Failed to delete file '%s'",
2201
 
                            index_file_name);
2202
 
      my_errno= 0;
2203
 
      error= 0;
2204
 
    }
2205
 
    else
2206
 
    {
2207
 
      push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
2208
 
                          ER_BINLOG_PURGE_FATAL_ERR,
2209
 
                          "a problem with deleting %s; "
2210
 
                          "consider examining correspondence "
2211
 
                          "of your binlog index file "
2212
 
                          "to the actual binlog files",
2213
 
                          index_file_name);
2214
 
      error= 1;
2215
 
      goto err;
2216
 
    }
2217
 
  }
2218
 
  if (!thd->slave_thread)
2219
 
    need_start_event=1;
2220
 
  if (!open_index_file(index_file_name, 0))
2221
 
    open(save_name, log_type, 0, io_cache_type, no_auto_events, max_size, 0);
2222
 
  my_free((uchar*) save_name, MYF(0));
2223
 
 
2224
 
err:
2225
 
  VOID(pthread_mutex_unlock(&LOCK_thread_count));
2226
 
  pthread_mutex_unlock(&LOCK_index);
2227
 
  pthread_mutex_unlock(&LOCK_log);
2228
 
  return(error);
2229
 
}
2230
 
 
2231
 
 
2232
 
/**
2233
 
  Delete relay log files prior to rli->group_relay_log_name
2234
 
  (i.e. all logs which are not involved in a non-finished group
2235
 
  (transaction)), remove them from the index file and start on next
2236
 
  relay log.
2237
 
 
2238
 
  IMPLEMENTATION
2239
 
  - Protects index file with LOCK_index
2240
 
  - Delete relevant relay log files
2241
 
  - Copy all file names after these ones to the front of the index file
2242
 
  - If the OS has truncate, truncate the file, else fill it with \n'
2243
 
  - Read the next file name from the index file and store in rli->linfo
2244
 
 
2245
 
  @param rli           Relay log information
2246
 
  @param included     If false, all relay logs that are strictly before
2247
 
                      rli->group_relay_log_name are deleted ; if true, the
2248
 
                      latter is deleted too (i.e. all relay logs
2249
 
                      read by the SQL slave thread are deleted).
2250
 
 
2251
 
  @note
2252
 
    - This is only called from the slave-execute thread when it has read
2253
 
    all commands from a relay log and want to switch to a new relay log.
2254
 
    - When this happens, we can be in an active transaction as
2255
 
    a transaction can span over two relay logs
2256
 
    (although it is always written as a single block to the master's binary
2257
 
    log, hence cannot span over two master's binary logs).
2258
 
 
2259
 
  @retval
2260
 
    0                   ok
2261
 
  @retval
2262
 
    LOG_INFO_EOF                End of log-index-file found
2263
 
  @retval
2264
 
    LOG_INFO_SEEK       Could not allocate IO cache
2265
 
  @retval
2266
 
    LOG_INFO_IO         Got IO error while reading file
2267
 
*/
2268
 
 
2269
 
#ifdef HAVE_REPLICATION
2270
 
 
2271
 
int DRIZZLE_BIN_LOG::purge_first_log(Relay_log_info* rli, bool included)
2272
 
{
2273
 
  int error;
2274
 
 
2275
 
  assert(is_open());
2276
 
  assert(rli->slave_running == 1);
2277
 
  assert(!strcmp(rli->linfo.log_file_name,rli->event_relay_log_name));
2278
 
 
2279
 
  pthread_mutex_lock(&LOCK_index);
2280
 
  pthread_mutex_lock(&rli->log_space_lock);
2281
 
  rli->relay_log.purge_logs(rli->group_relay_log_name, included,
2282
 
                            0, 0, &rli->log_space_total);
2283
 
  // Tell the I/O thread to take the relay_log_space_limit into account
2284
 
  rli->ignore_log_space_limit= 0;
2285
 
  pthread_mutex_unlock(&rli->log_space_lock);
2286
 
 
2287
 
  /*
2288
 
    Ok to broadcast after the critical region as there is no risk of
2289
 
    the mutex being destroyed by this thread later - this helps save
2290
 
    context switches
2291
 
  */
2292
 
  pthread_cond_broadcast(&rli->log_space_cond);
2293
 
  
2294
 
  /*
2295
 
    Read the next log file name from the index file and pass it back to
2296
 
    the caller
2297
 
    If included is true, we want the first relay log;
2298
 
    otherwise we want the one after event_relay_log_name.
2299
 
  */
2300
 
  if ((included && (error=find_log_pos(&rli->linfo, NullS, 0))) ||
2301
 
      (!included &&
2302
 
       ((error=find_log_pos(&rli->linfo, rli->event_relay_log_name, 0)) ||
2303
 
        (error=find_next_log(&rli->linfo, 0)))))
2304
 
  {
2305
 
    char buff[22];
2306
 
    sql_print_error("next log error: %d  offset: %s  log: %s included: %d",
2307
 
                    error,
2308
 
                    llstr(rli->linfo.index_file_offset,buff),
2309
 
                    rli->group_relay_log_name,
2310
 
                    included);
2311
 
    goto err;
2312
 
  }
2313
 
 
2314
 
  /*
2315
 
    Reset rli's coordinates to the current log.
2316
 
  */
2317
 
  rli->event_relay_log_pos= BIN_LOG_HEADER_SIZE;
2318
 
  strmake(rli->event_relay_log_name,rli->linfo.log_file_name,
2319
 
          sizeof(rli->event_relay_log_name)-1);
2320
 
 
2321
 
  /*
2322
 
    If we removed the rli->group_relay_log_name file,
2323
 
    we must update the rli->group* coordinates, otherwise do not touch it as the
2324
 
    group's execution is not finished (e.g. COMMIT not executed)
2325
 
  */
2326
 
  if (included)
2327
 
  {
2328
 
    rli->group_relay_log_pos = BIN_LOG_HEADER_SIZE;
2329
 
    strmake(rli->group_relay_log_name,rli->linfo.log_file_name,
2330
 
            sizeof(rli->group_relay_log_name)-1);
2331
 
    rli->notify_group_relay_log_name_update();
2332
 
  }
2333
 
 
2334
 
  /* Store where we are in the new file for the execution thread */
2335
 
  flush_relay_log_info(rli);
2336
 
 
2337
 
err:
2338
 
  pthread_mutex_unlock(&LOCK_index);
2339
 
  return(error);
2340
 
}
2341
 
 
2342
 
/**
2343
 
  Update log index_file.
2344
 
*/
2345
 
 
2346
 
int DRIZZLE_BIN_LOG::update_log_index(LOG_INFO* log_info, bool need_update_threads)
2347
 
{
2348
 
  if (copy_up_file_and_fill(&index_file, log_info->index_file_start_offset))
2349
 
    return LOG_INFO_IO;
2350
 
 
2351
 
  // now update offsets in index file for running threads
2352
 
  if (need_update_threads)
2353
 
    adjust_linfo_offsets(log_info->index_file_start_offset);
2354
 
  return 0;
2355
 
}
2356
 
 
2357
 
/**
2358
 
  Remove all logs before the given log from disk and from the index file.
2359
 
 
2360
 
  @param to_log       Delete all log file name before this file.
2361
 
  @param included            If true, to_log is deleted too.
2362
 
  @param need_mutex
2363
 
  @param need_update_threads If we want to update the log coordinates of
2364
 
                             all threads. False for relay logs, true otherwise.
2365
 
  @param freed_log_space     If not null, decrement this variable of
2366
 
                             the amount of log space freed
2367
 
 
2368
 
  @note
2369
 
    If any of the logs before the deleted one is in use,
2370
 
    only purge logs up to this one.
2371
 
 
2372
 
  @retval
2373
 
    0                   ok
2374
 
  @retval
2375
 
    LOG_INFO_EOF                to_log not found
2376
 
    LOG_INFO_EMFILE             too many files opened
2377
 
    LOG_INFO_FATAL              if any other than ENOENT error from
2378
 
                                stat() or my_delete()
2379
 
*/
2380
 
 
2381
 
int DRIZZLE_BIN_LOG::purge_logs(const char *to_log, 
2382
 
                          bool included,
2383
 
                          bool need_mutex, 
2384
 
                          bool need_update_threads, 
2385
 
                          uint64_t *decrease_log_space)
2386
 
{
2387
 
  int error;
2388
 
  int ret = 0;
2389
 
  bool exit_loop= 0;
2390
 
  LOG_INFO log_info;
2391
 
 
2392
 
  if (need_mutex)
2393
 
    pthread_mutex_lock(&LOCK_index);
2394
 
  if ((error=find_log_pos(&log_info, to_log, 0 /*no mutex*/)))
2395
 
    goto err;
2396
 
 
2397
 
  /*
2398
 
    File name exists in index file; delete until we find this file
2399
 
    or a file that is used.
2400
 
  */
2401
 
  if ((error=find_log_pos(&log_info, NullS, 0 /*no mutex*/)))
2402
 
    goto err;
2403
 
  while ((strcmp(to_log,log_info.log_file_name) || (exit_loop=included)) &&
2404
 
         !log_in_use(log_info.log_file_name))
2405
 
  {
2406
 
    struct stat s;
2407
 
    if (stat(log_info.log_file_name, &s))
2408
 
    {
2409
 
      if (errno == ENOENT) 
2410
 
      {
2411
 
        /*
2412
 
          It's not fatal if we can't stat a log file that does not exist;
2413
 
          If we could not stat, we won't delete.
2414
 
        */     
2415
 
        push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
2416
 
                            ER_LOG_PURGE_NO_FILE, ER(ER_LOG_PURGE_NO_FILE),
2417
 
                            log_info.log_file_name);
2418
 
        sql_print_information("Failed to execute stat on file '%s'",
2419
 
                              log_info.log_file_name);
2420
 
        my_errno= 0;
2421
 
      }
2422
 
      else
2423
 
      {
2424
 
        /*
2425
 
          Other than ENOENT are fatal
2426
 
        */
2427
 
        push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
2428
 
                            ER_BINLOG_PURGE_FATAL_ERR,
2429
 
                            "a problem with getting info on being purged %s; "
2430
 
                            "consider examining correspondence "
2431
 
                            "of your binlog index file "
2432
 
                            "to the actual binlog files",
2433
 
                            log_info.log_file_name);
2434
 
        error= LOG_INFO_FATAL;
2435
 
        goto err;
2436
 
      }
2437
 
    }
2438
 
    else
2439
 
    {
2440
 
      if (!my_delete(log_info.log_file_name, MYF(0)))
2441
 
      {
2442
 
        if (decrease_log_space)
2443
 
          *decrease_log_space-= s.st_size;
2444
 
      }
2445
 
      else
2446
 
      {
2447
 
        if (my_errno == ENOENT) 
2448
 
        {
2449
 
          push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
2450
 
                              ER_LOG_PURGE_NO_FILE, ER(ER_LOG_PURGE_NO_FILE),
2451
 
                              log_info.log_file_name);
2452
 
          sql_print_information("Failed to delete file '%s'",
2453
 
                                log_info.log_file_name);
2454
 
          my_errno= 0;
2455
 
        }
2456
 
        else
2457
 
        {
2458
 
          push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
2459
 
                              ER_BINLOG_PURGE_FATAL_ERR,
2460
 
                              "a problem with deleting %s; "
2461
 
                              "consider examining correspondence "
2462
 
                              "of your binlog index file "
2463
 
                              "to the actual binlog files",
2464
 
                              log_info.log_file_name);
2465
 
          if (my_errno == EMFILE)
2466
 
          {
2467
 
            error= LOG_INFO_EMFILE;
2468
 
          }
2469
 
          error= LOG_INFO_FATAL;
2470
 
          goto err;
2471
 
        }
2472
 
      }
2473
 
    }
2474
 
 
2475
 
    ha_binlog_index_purge_file(current_thd, log_info.log_file_name);
2476
 
 
2477
 
    if (find_next_log(&log_info, 0) || exit_loop)
2478
 
      break;
2479
 
  }
2480
 
  
2481
 
  /*
2482
 
    If we get killed -9 here, the sysadmin would have to edit
2483
 
    the log index file after restart - otherwise, this should be safe
2484
 
  */
2485
 
  error= update_log_index(&log_info, need_update_threads);
2486
 
  if (error == 0) {
2487
 
    error = ret;
2488
 
  }
2489
 
 
2490
 
err:
2491
 
  if (need_mutex)
2492
 
    pthread_mutex_unlock(&LOCK_index);
2493
 
  return(error);
2494
 
}
2495
 
 
2496
 
/**
2497
 
  Remove all logs before the given file date from disk and from the
2498
 
  index file.
2499
 
 
2500
 
  @param thd            Thread pointer
2501
 
  @param before_date    Delete all log files before given date.
2502
 
 
2503
 
  @note
2504
 
    If any of the logs before the deleted one is in use,
2505
 
    only purge logs up to this one.
2506
 
 
2507
 
  @retval
2508
 
    0                           ok
2509
 
  @retval
2510
 
    LOG_INFO_PURGE_NO_ROTATE    Binary file that can't be rotated
2511
 
    LOG_INFO_FATAL              if any other than ENOENT error from
2512
 
                                stat() or my_delete()
2513
 
*/
2514
 
 
2515
 
int DRIZZLE_BIN_LOG::purge_logs_before_date(time_t purge_time)
2516
 
{
2517
 
  int error;
2518
 
  LOG_INFO log_info;
2519
 
  struct stat stat_area;
2520
 
 
2521
 
  pthread_mutex_lock(&LOCK_index);
2522
 
 
2523
 
  /*
2524
 
    Delete until we find curren file
2525
 
    or a file that is used or a file
2526
 
    that is older than purge_time.
2527
 
  */
2528
 
  if ((error=find_log_pos(&log_info, NullS, 0 /*no mutex*/)))
2529
 
    goto err;
2530
 
 
2531
 
  while (strcmp(log_file_name, log_info.log_file_name) &&
2532
 
         !log_in_use(log_info.log_file_name))
2533
 
  {
2534
 
    if (stat(log_info.log_file_name, &stat_area))
2535
 
    {
2536
 
      if (errno == ENOENT) 
2537
 
      {
2538
 
        /*
2539
 
          It's not fatal if we can't stat a log file that does not exist.
2540
 
        */     
2541
 
        push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
2542
 
                            ER_LOG_PURGE_NO_FILE, ER(ER_LOG_PURGE_NO_FILE),
2543
 
                            log_info.log_file_name);
2544
 
        sql_print_information("Failed to execute stat on file '%s'",
2545
 
                              log_info.log_file_name);
2546
 
        my_errno= 0;
2547
 
      }
2548
 
      else
2549
 
      {
2550
 
        /*
2551
 
          Other than ENOENT are fatal
2552
 
        */
2553
 
        push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
2554
 
                            ER_BINLOG_PURGE_FATAL_ERR,
2555
 
                            "a problem with getting info on being purged %s; "
2556
 
                            "consider examining correspondence "
2557
 
                            "of your binlog index file "
2558
 
                            "to the actual binlog files",
2559
 
                            log_info.log_file_name);
2560
 
        error= LOG_INFO_FATAL;
2561
 
        goto err;
2562
 
      }
2563
 
    }
2564
 
    else
2565
 
    {
2566
 
      if (stat_area.st_mtime >= purge_time)
2567
 
        break;
2568
 
      if (my_delete(log_info.log_file_name, MYF(0)))
2569
 
      {
2570
 
        if (my_errno == ENOENT) 
2571
 
        {
2572
 
          /* It's not fatal even if we can't delete a log file */
2573
 
          push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
2574
 
                              ER_LOG_PURGE_NO_FILE, ER(ER_LOG_PURGE_NO_FILE),
2575
 
                              log_info.log_file_name);
2576
 
          sql_print_information("Failed to delete file '%s'",
2577
 
                                log_info.log_file_name);
2578
 
          my_errno= 0;
2579
 
        }
2580
 
        else
2581
 
        {
2582
 
          push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
2583
 
                              ER_BINLOG_PURGE_FATAL_ERR,
2584
 
                              "a problem with deleting %s; "
2585
 
                              "consider examining correspondence "
2586
 
                              "of your binlog index file "
2587
 
                              "to the actual binlog files",
2588
 
                              log_info.log_file_name);
2589
 
          error= LOG_INFO_FATAL;
2590
 
          goto err;
2591
 
        }
2592
 
      }
2593
 
      ha_binlog_index_purge_file(current_thd, log_info.log_file_name);
2594
 
    }
2595
 
    if (find_next_log(&log_info, 0))
2596
 
      break;
2597
 
  }
2598
 
 
2599
 
  /*
2600
 
    If we get killed -9 here, the sysadmin would have to edit
2601
 
    the log index file after restart - otherwise, this should be safe
2602
 
  */
2603
 
  error= update_log_index(&log_info, 1);
2604
 
 
2605
 
err:
2606
 
  pthread_mutex_unlock(&LOCK_index);
2607
 
  return(error);
2608
 
}
2609
 
#endif /* HAVE_REPLICATION */
2610
 
 
2611
 
 
2612
 
/**
2613
 
  Create a new log file name.
2614
 
 
2615
 
  @param buf            buf of at least FN_REFLEN where new name is stored
2616
 
 
2617
 
  @note
2618
 
    If file name will be longer then FN_REFLEN it will be truncated
2619
 
*/
2620
 
 
2621
 
void DRIZZLE_BIN_LOG::make_log_name(char* buf, const char* log_ident)
2622
 
{
2623
 
  uint dir_len = dirname_length(log_file_name); 
2624
 
  if (dir_len >= FN_REFLEN)
2625
 
    dir_len=FN_REFLEN-1;
2626
 
  stpncpy(buf, log_file_name, dir_len);
2627
 
  strmake(buf+dir_len, log_ident, FN_REFLEN - dir_len -1);
2628
 
}
2629
 
 
2630
 
 
2631
 
/**
2632
 
  Check if we are writing/reading to the given log file.
2633
 
*/
2634
 
 
2635
 
bool DRIZZLE_BIN_LOG::is_active(const char *log_file_name_arg)
2636
 
{
2637
 
  return !strcmp(log_file_name, log_file_name_arg);
2638
 
}
2639
 
 
2640
 
 
2641
 
/*
2642
 
  Wrappers around new_file_impl to avoid using argument
2643
 
  to control locking. The argument 1) less readable 2) breaks
2644
 
  incapsulation 3) allows external access to the class without
2645
 
  a lock (which is not possible with private new_file_without_locking
2646
 
  method).
2647
 
*/
2648
 
 
2649
 
void DRIZZLE_BIN_LOG::new_file()
2650
 
{
2651
 
  new_file_impl(1);
2652
 
}
2653
 
 
2654
 
 
2655
 
void DRIZZLE_BIN_LOG::new_file_without_locking()
2656
 
{
2657
 
  new_file_impl(0);
2658
 
}
2659
 
 
2660
 
 
2661
 
/**
2662
 
  Start writing to a new log file or reopen the old file.
2663
 
 
2664
 
  @param need_lock              Set to 1 if caller has not locked LOCK_log
2665
 
 
2666
 
  @note
2667
 
    The new file name is stored last in the index file
2668
 
*/
2669
 
 
2670
 
void DRIZZLE_BIN_LOG::new_file_impl(bool need_lock)
2671
 
{
2672
 
  char new_name[FN_REFLEN], *new_name_ptr, *old_name;
2673
 
 
2674
 
  if (!is_open())
2675
 
  {
2676
 
    return;
2677
 
  }
2678
 
 
2679
 
  if (need_lock)
2680
 
    pthread_mutex_lock(&LOCK_log);
2681
 
  pthread_mutex_lock(&LOCK_index);
2682
 
 
2683
 
  safe_mutex_assert_owner(&LOCK_log);
2684
 
  safe_mutex_assert_owner(&LOCK_index);
2685
 
 
2686
 
  /*
2687
 
    if binlog is used as tc log, be sure all xids are "unlogged",
2688
 
    so that on recover we only need to scan one - latest - binlog file
2689
 
    for prepared xids. As this is expected to be a rare event,
2690
 
    simple wait strategy is enough. We're locking LOCK_log to be sure no
2691
 
    new Xid_log_event's are added to the log (and prepared_xids is not
2692
 
    increased), and waiting on COND_prep_xids for late threads to
2693
 
    catch up.
2694
 
  */
2695
 
  if (prepared_xids)
2696
 
  {
2697
 
    tc_log_page_waits++;
2698
 
    pthread_mutex_lock(&LOCK_prep_xids);
2699
 
    while (prepared_xids) {
2700
 
      pthread_cond_wait(&COND_prep_xids, &LOCK_prep_xids);
2701
 
    }
2702
 
    pthread_mutex_unlock(&LOCK_prep_xids);
2703
 
  }
2704
 
 
2705
 
  /* Reuse old name if not binlog and not update log */
2706
 
  new_name_ptr= name;
2707
 
 
2708
 
  /*
2709
 
    If user hasn't specified an extension, generate a new log name
2710
 
    We have to do this here and not in open as we want to store the
2711
 
    new file name in the current binary log file.
2712
 
  */
2713
 
  if (generate_new_name(new_name, name))
2714
 
    goto end;
2715
 
  new_name_ptr=new_name;
2716
 
 
2717
 
  if (log_type == LOG_BIN)
2718
 
  {
2719
 
    if (!no_auto_events)
2720
 
    {
2721
 
      /*
2722
 
        We log the whole file name for log file as the user may decide
2723
 
        to change base names at some point.
2724
 
      */
2725
 
      Rotate_log_event r(new_name+dirname_length(new_name),
2726
 
                         0, LOG_EVENT_OFFSET, 0);
2727
 
      r.write(&log_file);
2728
 
      bytes_written += r.data_written;
2729
 
    }
2730
 
    /*
2731
 
      Update needs to be signalled even if there is no rotate event
2732
 
      log rotation should give the waiting thread a signal to
2733
 
      discover EOF and move on to the next log.
2734
 
    */
2735
 
    signal_update();
2736
 
  }
2737
 
  old_name=name;
2738
 
  name=0;                               // Don't free name
2739
 
  close(LOG_CLOSE_TO_BE_OPENED);
2740
 
 
2741
 
  /*
2742
 
     Note that at this point, log_state != LOG_CLOSED (important for is_open()).
2743
 
  */
2744
 
 
2745
 
  /*
2746
 
     new_file() is only used for rotation (in FLUSH LOGS or because size >
2747
 
     max_binlog_size or max_relay_log_size).
2748
 
     If this is a binary log, the Format_description_log_event at the beginning of
2749
 
     the new file should have created=0 (to distinguish with the
2750
 
     Format_description_log_event written at server startup, which should
2751
 
     trigger temp tables deletion on slaves.
2752
 
  */
2753
 
 
2754
 
  open(old_name, log_type, new_name_ptr,
2755
 
       io_cache_type, no_auto_events, max_size, 1);
2756
 
  my_free(old_name,MYF(0));
2757
 
 
2758
 
end:
2759
 
  if (need_lock)
2760
 
    pthread_mutex_unlock(&LOCK_log);
2761
 
  pthread_mutex_unlock(&LOCK_index);
2762
 
 
2763
 
  return;
2764
 
}
2765
 
 
2766
 
 
2767
 
bool DRIZZLE_BIN_LOG::append(Log_event* ev)
2768
 
{
2769
 
  bool error = 0;
2770
 
  pthread_mutex_lock(&LOCK_log);
2771
 
 
2772
 
  assert(log_file.type == SEQ_READ_APPEND);
2773
 
  /*
2774
 
    Log_event::write() is smart enough to use my_b_write() or
2775
 
    my_b_append() depending on the kind of cache we have.
2776
 
  */
2777
 
  if (ev->write(&log_file))
2778
 
  {
2779
 
    error=1;
2780
 
    goto err;
2781
 
  }
2782
 
  bytes_written+= ev->data_written;
2783
 
  if ((uint) my_b_append_tell(&log_file) > max_size)
2784
 
    new_file_without_locking();
2785
 
 
2786
 
err:
2787
 
  pthread_mutex_unlock(&LOCK_log);
2788
 
  signal_update();                              // Safe as we don't call close
2789
 
  return(error);
2790
 
}
2791
 
 
2792
 
 
2793
 
bool DRIZZLE_BIN_LOG::appendv(const char* buf, uint len,...)
2794
 
{
2795
 
  bool error= 0;
2796
 
  va_list(args);
2797
 
  va_start(args,len);
2798
 
 
2799
 
  assert(log_file.type == SEQ_READ_APPEND);
2800
 
 
2801
 
  safe_mutex_assert_owner(&LOCK_log);
2802
 
  do
2803
 
  {
2804
 
    if (my_b_append(&log_file,(uchar*) buf,len))
2805
 
    {
2806
 
      error= 1;
2807
 
      goto err;
2808
 
    }
2809
 
    bytes_written += len;
2810
 
  } while ((buf=va_arg(args,const char*)) && (len=va_arg(args,uint)));
2811
 
  if ((uint) my_b_append_tell(&log_file) > max_size)
2812
 
    new_file_without_locking();
2813
 
 
2814
 
err:
2815
 
  if (!error)
2816
 
    signal_update();
2817
 
  return(error);
2818
 
}
2819
 
 
2820
 
 
2821
 
bool DRIZZLE_BIN_LOG::flush_and_sync()
2822
 
{
2823
 
  int err=0, fd=log_file.file;
2824
 
  safe_mutex_assert_owner(&LOCK_log);
2825
 
  if (flush_io_cache(&log_file))
2826
 
    return 1;
2827
 
  if (++sync_binlog_counter >= sync_binlog_period && sync_binlog_period)
2828
 
  {
2829
 
    sync_binlog_counter= 0;
2830
 
    err=my_sync(fd, MYF(MY_WME));
2831
 
  }
2832
 
  return err;
2833
 
}
2834
 
 
2835
 
void DRIZZLE_BIN_LOG::start_union_events(THD *thd, query_id_t query_id_param)
2836
 
{
2837
 
  assert(!thd->binlog_evt_union.do_union);
2838
 
  thd->binlog_evt_union.do_union= true;
2839
 
  thd->binlog_evt_union.unioned_events= false;
2840
 
  thd->binlog_evt_union.unioned_events_trans= false;
2841
 
  thd->binlog_evt_union.first_query_id= query_id_param;
2842
 
}
2843
 
 
2844
 
void DRIZZLE_BIN_LOG::stop_union_events(THD *thd)
2845
 
{
2846
 
  assert(thd->binlog_evt_union.do_union);
2847
 
  thd->binlog_evt_union.do_union= false;
2848
 
}
2849
 
 
2850
 
bool DRIZZLE_BIN_LOG::is_query_in_union(THD *thd, query_id_t query_id_param)
2851
 
{
2852
 
  return (thd->binlog_evt_union.do_union && 
2853
 
          query_id_param >= thd->binlog_evt_union.first_query_id);
2854
 
}
2855
 
 
2856
 
 
2857
 
/*
2858
 
  These functions are placed in this file since they need access to
2859
 
  binlog_hton, which has internal linkage.
2860
 
*/
2861
 
 
2862
 
int THD::binlog_setup_trx_data()
2863
 
{
2864
 
  binlog_trx_data *trx_data=
2865
 
    (binlog_trx_data*) thd_get_ha_data(this, binlog_hton);
2866
 
 
2867
 
  if (trx_data)
2868
 
    return(0);                             // Already set up
2869
 
 
2870
 
  trx_data= (binlog_trx_data*) my_malloc(sizeof(binlog_trx_data), MYF(MY_ZEROFILL));
2871
 
  if (!trx_data ||
2872
 
      open_cached_file(&trx_data->trans_log, mysql_tmpdir,
2873
 
                       LOG_PREFIX, binlog_cache_size, MYF(MY_WME)))
2874
 
  {
2875
 
    my_free((uchar*)trx_data, MYF(MY_ALLOW_ZERO_PTR));
2876
 
    return(1);                      // Didn't manage to set it up
2877
 
  }
2878
 
  thd_set_ha_data(this, binlog_hton, trx_data);
2879
 
 
2880
 
  trx_data= new (thd_get_ha_data(this, binlog_hton)) binlog_trx_data;
2881
 
 
2882
 
  return(0);
2883
 
}
2884
 
 
2885
 
/*
2886
 
  Function to start a statement and optionally a transaction for the
2887
 
  binary log.
2888
 
 
2889
 
  SYNOPSIS
2890
 
    binlog_start_trans_and_stmt()
2891
 
 
2892
 
  DESCRIPTION
2893
 
 
2894
 
    This function does three things:
2895
 
    - Start a transaction if not in autocommit mode or if a BEGIN
2896
 
      statement has been seen.
2897
 
 
2898
 
    - Start a statement transaction to allow us to truncate the binary
2899
 
      log.
2900
 
 
2901
 
    - Save the currrent binlog position so that we can roll back the
2902
 
      statement by truncating the transaction log.
2903
 
 
2904
 
      We only update the saved position if the old one was undefined,
2905
 
      the reason is that there are some cases (e.g., for CREATE-SELECT)
2906
 
      where the position is saved twice (e.g., both in
2907
 
      select_create::prepare() and THD::binlog_write_table_map()) , but
2908
 
      we should use the first. This means that calls to this function
2909
 
      can be used to start the statement before the first table map
2910
 
      event, to include some extra events.
2911
 
 */
2912
 
 
2913
 
void
2914
 
THD::binlog_start_trans_and_stmt()
2915
 
{
2916
 
  binlog_trx_data *trx_data= (binlog_trx_data*) thd_get_ha_data(this, binlog_hton);
2917
 
 
2918
 
  if (trx_data == NULL ||
2919
 
      trx_data->before_stmt_pos == MY_OFF_T_UNDEF)
2920
 
  {
2921
 
    this->binlog_set_stmt_begin();
2922
 
    if (options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN))
2923
 
      trans_register_ha(this, true, binlog_hton);
2924
 
    trans_register_ha(this, false, binlog_hton);
2925
 
    /*
2926
 
      Mark statement transaction as read/write. We never start
2927
 
      a binary log transaction and keep it read-only,
2928
 
      therefore it's best to mark the transaction read/write just
2929
 
      at the same time we start it.
2930
 
      Not necessary to mark the normal transaction read/write
2931
 
      since the statement-level flag will be propagated automatically
2932
 
      inside ha_commit_trans.
2933
 
    */
2934
 
    ha_data[binlog_hton->slot].ha_info[0].set_trx_read_write();
2935
 
  }
2936
 
  return;
2937
 
}
2938
 
 
2939
 
void THD::binlog_set_stmt_begin() {
2940
 
  binlog_trx_data *trx_data=
2941
 
    (binlog_trx_data*) thd_get_ha_data(this, binlog_hton);
2942
 
 
2943
 
  /*
2944
 
    The call to binlog_trans_log_savepos() might create the trx_data
2945
 
    structure, if it didn't exist before, so we save the position
2946
 
    into an auto variable and then write it into the transaction
2947
 
    data for the binary log (i.e., trx_data).
2948
 
  */
2949
 
  my_off_t pos= 0;
2950
 
  binlog_trans_log_savepos(this, &pos);
2951
 
  trx_data= (binlog_trx_data*) thd_get_ha_data(this, binlog_hton);
2952
 
  trx_data->before_stmt_pos= pos;
2953
 
}
2954
 
 
2955
 
 
2956
 
/*
2957
 
  Write a table map to the binary log.
2958
 
 */
2959
 
 
2960
 
int THD::binlog_write_table_map(TABLE *table, bool is_trans)
2961
 
{
2962
 
  int error;
2963
 
 
2964
 
  /* Pre-conditions */
2965
 
  assert(current_stmt_binlog_row_based && mysql_bin_log.is_open());
2966
 
  assert(table->s->table_map_id != ULONG_MAX);
2967
 
 
2968
 
  Table_map_log_event::flag_set const
2969
 
    flags= Table_map_log_event::TM_NO_FLAGS;
2970
 
 
2971
 
  Table_map_log_event
2972
 
    the_event(this, table, table->s->table_map_id, is_trans, flags);
2973
 
 
2974
 
  if (is_trans && binlog_table_maps == 0)
2975
 
    binlog_start_trans_and_stmt();
2976
 
 
2977
 
  if ((error= mysql_bin_log.write(&the_event)))
2978
 
    return(error);
2979
 
 
2980
 
  binlog_table_maps++;
2981
 
  table->s->table_map_version= mysql_bin_log.table_map_version();
2982
 
  return(0);
2983
 
}
2984
 
 
2985
 
Rows_log_event*
2986
 
THD::binlog_get_pending_rows_event() const
2987
 
{
2988
 
  binlog_trx_data *const trx_data=
2989
 
    (binlog_trx_data*) thd_get_ha_data(this, binlog_hton);
2990
 
  /*
2991
 
    This is less than ideal, but here's the story: If there is no
2992
 
    trx_data, prepare_pending_rows_event() has never been called
2993
 
    (since the trx_data is set up there). In that case, we just return
2994
 
    NULL.
2995
 
   */
2996
 
  return trx_data ? trx_data->pending() : NULL;
2997
 
}
2998
 
 
2999
 
void
3000
 
THD::binlog_set_pending_rows_event(Rows_log_event* ev)
3001
 
{
3002
 
  if (thd_get_ha_data(this, binlog_hton) == NULL)
3003
 
    binlog_setup_trx_data();
3004
 
 
3005
 
  binlog_trx_data *const trx_data=
3006
 
    (binlog_trx_data*) thd_get_ha_data(this, binlog_hton);
3007
 
 
3008
 
  assert(trx_data);
3009
 
  trx_data->set_pending(ev);
3010
 
}
3011
 
 
3012
 
 
3013
 
/*
3014
 
  Moves the last bunch of rows from the pending Rows event to the binlog
3015
 
  (either cached binlog if transaction, or disk binlog). Sets a new pending
3016
 
  event.
3017
 
*/
3018
 
int
3019
 
DRIZZLE_BIN_LOG::flush_and_set_pending_rows_event(THD *thd,
3020
 
                                                Rows_log_event* event)
3021
 
{
3022
 
  assert(mysql_bin_log.is_open());
3023
 
 
3024
 
  int error= 0;
3025
 
 
3026
 
  binlog_trx_data *const trx_data=
3027
 
    (binlog_trx_data*) thd_get_ha_data(thd, binlog_hton);
3028
 
 
3029
 
  assert(trx_data);
3030
 
 
3031
 
  if (Rows_log_event* pending= trx_data->pending())
3032
 
  {
3033
 
    IO_CACHE *file= &log_file;
3034
 
 
3035
 
    /*
3036
 
      Decide if we should write to the log file directly or to the
3037
 
      transaction log.
3038
 
    */
3039
 
    if (pending->get_cache_stmt() || my_b_tell(&trx_data->trans_log))
3040
 
      file= &trx_data->trans_log;
3041
 
 
3042
 
    /*
3043
 
      If we are writing to the log file directly, we could avoid
3044
 
      locking the log. This does not work since we need to step the
3045
 
      m_table_map_version below, and that change has to be protected
3046
 
      by the LOCK_log mutex.
3047
 
    */
3048
 
    pthread_mutex_lock(&LOCK_log);
3049
 
 
3050
 
    /*
3051
 
      Write pending event to log file or transaction cache
3052
 
    */
3053
 
    if (pending->write(file))
3054
 
    {
3055
 
      pthread_mutex_unlock(&LOCK_log);
3056
 
      return(1);
3057
 
    }
3058
 
 
3059
 
    /*
3060
 
      We step the table map version if we are writing an event
3061
 
      representing the end of a statement.  We do this regardless of
3062
 
      wheather we write to the transaction cache or to directly to the
3063
 
      file.
3064
 
 
3065
 
      In an ideal world, we could avoid stepping the table map version
3066
 
      if we were writing to a transaction cache, since we could then
3067
 
      reuse the table map that was written earlier in the transaction
3068
 
      cache.  This does not work since STMT_END_F implies closing all
3069
 
      table mappings on the slave side.
3070
 
 
3071
 
      TODO: Find a solution so that table maps does not have to be
3072
 
      written several times within a transaction.
3073
 
     */
3074
 
    if (pending->get_flags(Rows_log_event::STMT_END_F))
3075
 
      ++m_table_map_version;
3076
 
 
3077
 
    delete pending;
3078
 
 
3079
 
    if (file == &log_file)
3080
 
    {
3081
 
      error= flush_and_sync();
3082
 
      if (!error)
3083
 
      {
3084
 
        signal_update();
3085
 
        rotate_and_purge(RP_LOCK_LOG_IS_ALREADY_LOCKED);
3086
 
      }
3087
 
    }
3088
 
 
3089
 
    pthread_mutex_unlock(&LOCK_log);
3090
 
  }
3091
 
 
3092
 
  thd->binlog_set_pending_rows_event(event);
3093
 
 
3094
 
  return(error);
3095
 
}
3096
 
 
3097
 
/**
3098
 
  Write an event to the binary log.
3099
 
*/
3100
 
 
3101
 
bool DRIZZLE_BIN_LOG::write(Log_event *event_info)
3102
 
{
3103
 
  THD *thd= event_info->thd;
3104
 
  bool error= 1;
3105
 
 
3106
 
  if (thd->binlog_evt_union.do_union)
3107
 
  {
3108
 
    /*
3109
 
      In Stored function; Remember that function call caused an update.
3110
 
      We will log the function call to the binary log on function exit
3111
 
    */
3112
 
    thd->binlog_evt_union.unioned_events= true;
3113
 
    thd->binlog_evt_union.unioned_events_trans |= event_info->cache_stmt;
3114
 
    return(0);
3115
 
  }
3116
 
 
3117
 
  /*
3118
 
    Flush the pending rows event to the transaction cache or to the
3119
 
    log file.  Since this function potentially aquire the LOCK_log
3120
 
    mutex, we do this before aquiring the LOCK_log mutex in this
3121
 
    function.
3122
 
 
3123
 
    We only end the statement if we are in a top-level statement.  If
3124
 
    we are inside a stored function, we do not end the statement since
3125
 
    this will close all tables on the slave.
3126
 
  */
3127
 
  bool const end_stmt= false;
3128
 
  thd->binlog_flush_pending_rows_event(end_stmt);
3129
 
 
3130
 
  pthread_mutex_lock(&LOCK_log);
3131
 
 
3132
 
  /*
3133
 
     In most cases this is only called if 'is_open()' is true; in fact this is
3134
 
     mostly called if is_open() *was* true a few instructions before, but it
3135
 
     could have changed since.
3136
 
  */
3137
 
  if (likely(is_open()))
3138
 
  {
3139
 
    IO_CACHE *file= &log_file;
3140
 
    /*
3141
 
      In the future we need to add to the following if tests like
3142
 
      "do the involved tables match (to be implemented)
3143
 
      binlog_[wild_]{do|ignore}_table?" (WL#1049)"
3144
 
    */
3145
 
    const char *local_db= event_info->get_db();
3146
 
    if ((thd && !(thd->options & OPTION_BIN_LOG)) ||
3147
 
        (!binlog_filter->db_ok(local_db)))
3148
 
    {
3149
 
      VOID(pthread_mutex_unlock(&LOCK_log));
3150
 
      return(0);
3151
 
    }
3152
 
 
3153
 
    /*
3154
 
      Should we write to the binlog cache or to the binlog on disk?
3155
 
      Write to the binlog cache if:
3156
 
      - it is already not empty (meaning we're in a transaction; note that the
3157
 
     present event could be about a non-transactional table, but still we need
3158
 
     to write to the binlog cache in that case to handle updates to mixed
3159
 
     trans/non-trans table types the best possible in binlogging)
3160
 
      - or if the event asks for it (cache_stmt == TRUE).
3161
 
    */
3162
 
    if (opt_using_transactions && thd)
3163
 
    {
3164
 
      if (thd->binlog_setup_trx_data())
3165
 
        goto err;
3166
 
 
3167
 
      binlog_trx_data *const trx_data=
3168
 
        (binlog_trx_data*) thd_get_ha_data(thd, binlog_hton);
3169
 
      IO_CACHE *trans_log= &trx_data->trans_log;
3170
 
      my_off_t trans_log_pos= my_b_tell(trans_log);
3171
 
      if (event_info->get_cache_stmt() || trans_log_pos != 0)
3172
 
      {
3173
 
        if (trans_log_pos == 0)
3174
 
          thd->binlog_start_trans_and_stmt();
3175
 
        file= trans_log;
3176
 
      }
3177
 
      /*
3178
 
        TODO as Mats suggested, for all the cases above where we write to
3179
 
        trans_log, it sounds unnecessary to lock LOCK_log. We should rather
3180
 
        test first if we want to write to trans_log, and if not, lock
3181
 
        LOCK_log.
3182
 
      */
3183
 
    }
3184
 
 
3185
 
    /*
3186
 
      No check for auto events flag here - this write method should
3187
 
      never be called if auto-events are enabled
3188
 
    */
3189
 
 
3190
 
    /*
3191
 
      1. Write first log events which describe the 'run environment'
3192
 
      of the SQL command
3193
 
    */
3194
 
 
3195
 
    /*
3196
 
      If row-based binlogging, Insert_id, Rand and other kind of "setting
3197
 
      context" events are not needed.
3198
 
    */
3199
 
    if (thd)
3200
 
    {
3201
 
      if (!thd->current_stmt_binlog_row_based)
3202
 
      {
3203
 
        if (thd->stmt_depends_on_first_successful_insert_id_in_prev_stmt)
3204
 
        {
3205
 
          Intvar_log_event e(thd,(uchar) LAST_INSERT_ID_EVENT,
3206
 
                             thd->first_successful_insert_id_in_prev_stmt_for_binlog);
3207
 
          if (e.write(file))
3208
 
            goto err;
3209
 
        }
3210
 
        if (thd->auto_inc_intervals_in_cur_stmt_for_binlog.nb_elements() > 0)
3211
 
        {
3212
 
          /*
3213
 
            If the auto_increment was second in a table's index (possible with
3214
 
            MyISAM or BDB) (table->next_number_keypart != 0), such event is
3215
 
            in fact not necessary. We could avoid logging it.
3216
 
          */
3217
 
          Intvar_log_event e(thd, (uchar) INSERT_ID_EVENT,
3218
 
                             thd->auto_inc_intervals_in_cur_stmt_for_binlog.
3219
 
                             minimum());
3220
 
          if (e.write(file))
3221
 
            goto err;
3222
 
        }
3223
 
        if (thd->rand_used)
3224
 
        {
3225
 
          Rand_log_event e(thd,thd->rand_saved_seed1,thd->rand_saved_seed2);
3226
 
          if (e.write(file))
3227
 
            goto err;
3228
 
        }
3229
 
        if (thd->user_var_events.elements)
3230
 
        {
3231
 
          for (uint i= 0; i < thd->user_var_events.elements; i++)
3232
 
          {
3233
 
            BINLOG_USER_VAR_EVENT *user_var_event;
3234
 
            get_dynamic(&thd->user_var_events,(uchar*) &user_var_event, i);
3235
 
            User_var_log_event e(thd, user_var_event->user_var_event->name.str,
3236
 
                                 user_var_event->user_var_event->name.length,
3237
 
                                 user_var_event->value,
3238
 
                                 user_var_event->length,
3239
 
                                 user_var_event->type,
3240
 
                                 user_var_event->charset_number);
3241
 
            if (e.write(file))
3242
 
              goto err;
3243
 
          }
3244
 
        }
3245
 
      }
3246
 
    }
3247
 
 
3248
 
    /*
3249
 
       Write the SQL command
3250
 
     */
3251
 
 
3252
 
    if (event_info->write(file))
3253
 
      goto err;
3254
 
 
3255
 
    if (file == &log_file) // we are writing to the real log (disk)
3256
 
    {
3257
 
      if (flush_and_sync())
3258
 
        goto err;
3259
 
      signal_update();
3260
 
      rotate_and_purge(RP_LOCK_LOG_IS_ALREADY_LOCKED);
3261
 
    }
3262
 
    error=0;
3263
 
 
3264
 
err:
3265
 
    if (error)
3266
 
    {
3267
 
      if (my_errno == EFBIG)
3268
 
        my_message(ER_TRANS_CACHE_FULL, ER(ER_TRANS_CACHE_FULL), MYF(0));
3269
 
      else
3270
 
        my_error(ER_ERROR_ON_WRITE, MYF(0), name, errno);
3271
 
      write_error=1;
3272
 
    }
3273
 
  }
3274
 
 
3275
 
  if (event_info->flags & LOG_EVENT_UPDATE_TABLE_MAP_VERSION_F)
3276
 
    ++m_table_map_version;
3277
 
 
3278
 
  pthread_mutex_unlock(&LOCK_log);
3279
 
  return(error);
3280
 
}
3281
 
 
3282
 
 
3283
 
int error_log_print(enum loglevel level, const char *format,
3284
 
                    va_list args)
3285
 
{
3286
 
  return logger.error_log_print(level, format, args);
3287
 
}
3288
 
 
3289
 
 
3290
 
bool slow_log_print(THD *thd, const char *query, uint query_length,
3291
 
                    uint64_t current_utime)
3292
 
{
3293
 
  return logger.slow_log_print(thd, query, query_length, current_utime);
3294
 
}
3295
 
 
3296
 
 
3297
 
bool LOGGER::log_command(THD *thd, enum enum_server_command command)
3298
 
{
3299
 
  /*
3300
 
    Log command if we have at least one log event handler enabled and want
3301
 
    to log this king of commands
3302
 
  */
3303
 
  if (*general_log_handler_list && (what_to_log & (1L << (uint) command)))
3304
 
  {
3305
 
    if (thd->options & OPTION_LOG_OFF)
3306
 
    {
3307
 
      /* No logging */
3308
 
      return false;
3309
 
    }
3310
 
 
3311
 
    return true;
3312
 
  }
3313
 
 
3314
 
  return false;
3315
 
}
3316
 
 
3317
 
 
3318
 
bool general_log_print(THD *thd, enum enum_server_command command,
3319
 
                       const char *format, ...)
3320
 
{
3321
 
  va_list args;
3322
 
  uint error= 0;
3323
 
 
3324
 
  /* Print the message to the buffer if we want to log this king of commands */
3325
 
  if (! logger.log_command(thd, command))
3326
 
    return false;
3327
 
 
3328
 
  va_start(args, format);
3329
 
  error= logger.general_log_print(thd, command, format, args);
3330
 
  va_end(args);
3331
 
 
3332
 
  return error;
3333
 
}
3334
 
 
3335
 
bool general_log_write(THD *thd, enum enum_server_command command,
3336
 
                       const char *query, uint query_length)
3337
 
{
3338
 
  /* Write the message to the log if we want to log this king of commands */
3339
 
  if (logger.log_command(thd, command))
3340
 
    return logger.general_log_write(thd, command, query, query_length);
3341
 
 
3342
 
  return false;
3343
 
}
3344
 
 
3345
 
void DRIZZLE_BIN_LOG::rotate_and_purge(uint flags)
3346
 
{
3347
 
  if (!(flags & RP_LOCK_LOG_IS_ALREADY_LOCKED))
3348
 
    pthread_mutex_lock(&LOCK_log);
3349
 
  if ((flags & RP_FORCE_ROTATE) ||
3350
 
      (my_b_tell(&log_file) >= (my_off_t) max_size))
3351
 
  {
3352
 
    new_file_without_locking();
3353
 
#ifdef HAVE_REPLICATION
3354
 
    if (expire_logs_days)
3355
 
    {
3356
 
      time_t purge_time= my_time(0) - expire_logs_days*24*60*60;
3357
 
      if (purge_time >= 0)
3358
 
        purge_logs_before_date(purge_time);
3359
 
    }
3360
 
#endif
3361
 
  }
3362
 
  if (!(flags & RP_LOCK_LOG_IS_ALREADY_LOCKED))
3363
 
    pthread_mutex_unlock(&LOCK_log);
3364
 
}
3365
 
 
3366
 
uint DRIZZLE_BIN_LOG::next_file_id()
3367
 
{
3368
 
  uint res;
3369
 
  pthread_mutex_lock(&LOCK_log);
3370
 
  res = file_id++;
3371
 
  pthread_mutex_unlock(&LOCK_log);
3372
 
  return res;
3373
 
}
3374
 
 
3375
 
 
3376
 
/*
3377
 
  Write the contents of a cache to the binary log.
3378
 
 
3379
 
  SYNOPSIS
3380
 
    write_cache()
3381
 
    cache    Cache to write to the binary log
3382
 
    lock_log True if the LOCK_log mutex should be aquired, false otherwise
3383
 
    sync_log True if the log should be flushed and sync:ed
3384
 
 
3385
 
  DESCRIPTION
3386
 
    Write the contents of the cache to the binary log. The cache will
3387
 
    be reset as a READ_CACHE to be able to read the contents from it.
3388
 
 */
3389
 
 
3390
 
int DRIZZLE_BIN_LOG::write_cache(IO_CACHE *cache, bool lock_log, bool sync_log)
3391
 
{
3392
 
  Mutex_sentry sentry(lock_log ? &LOCK_log : NULL);
3393
 
 
3394
 
  if (reinit_io_cache(cache, READ_CACHE, 0, 0, 0))
3395
 
    return ER_ERROR_ON_WRITE;
3396
 
  uint length= my_b_bytes_in_cache(cache), group, carry, hdr_offs;
3397
 
  long val;
3398
 
  uchar header[LOG_EVENT_HEADER_LEN];
3399
 
 
3400
 
  /*
3401
 
    The events in the buffer have incorrect end_log_pos data
3402
 
    (relative to beginning of group rather than absolute),
3403
 
    so we'll recalculate them in situ so the binlog is always
3404
 
    correct, even in the middle of a group. This is possible
3405
 
    because we now know the start position of the group (the
3406
 
    offset of this cache in the log, if you will); all we need
3407
 
    to do is to find all event-headers, and add the position of
3408
 
    the group to the end_log_pos of each event.  This is pretty
3409
 
    straight forward, except that we read the cache in segments,
3410
 
    so an event-header might end up on the cache-border and get
3411
 
    split.
3412
 
  */
3413
 
 
3414
 
  group= (uint)my_b_tell(&log_file);
3415
 
  hdr_offs= carry= 0;
3416
 
 
3417
 
  do
3418
 
  {
3419
 
 
3420
 
    /*
3421
 
      if we only got a partial header in the last iteration,
3422
 
      get the other half now and process a full header.
3423
 
    */
3424
 
    if (unlikely(carry > 0))
3425
 
    {
3426
 
      assert(carry < LOG_EVENT_HEADER_LEN);
3427
 
 
3428
 
      /* assemble both halves */
3429
 
      memcpy(&header[carry], cache->read_pos, LOG_EVENT_HEADER_LEN - carry);
3430
 
 
3431
 
      /* fix end_log_pos */
3432
 
      val= uint4korr(&header[LOG_POS_OFFSET]) + group;
3433
 
      int4store(&header[LOG_POS_OFFSET], val);
3434
 
 
3435
 
      /* write the first half of the split header */
3436
 
      if (my_b_write(&log_file, header, carry))
3437
 
        return ER_ERROR_ON_WRITE;
3438
 
 
3439
 
      /*
3440
 
        copy fixed second half of header to cache so the correct
3441
 
        version will be written later.
3442
 
      */
3443
 
      memcpy(cache->read_pos, &header[carry], LOG_EVENT_HEADER_LEN - carry);
3444
 
 
3445
 
      /* next event header at ... */
3446
 
      hdr_offs = uint4korr(&header[EVENT_LEN_OFFSET]) - carry;
3447
 
 
3448
 
      carry= 0;
3449
 
    }
3450
 
 
3451
 
    /* if there is anything to write, process it. */
3452
 
 
3453
 
    if (likely(length > 0))
3454
 
    {
3455
 
      /*
3456
 
        process all event-headers in this (partial) cache.
3457
 
        if next header is beyond current read-buffer,
3458
 
        we'll get it later (though not necessarily in the
3459
 
        very next iteration, just "eventually").
3460
 
      */
3461
 
 
3462
 
      while (hdr_offs < length)
3463
 
      {
3464
 
        /*
3465
 
          partial header only? save what we can get, process once
3466
 
          we get the rest.
3467
 
        */
3468
 
 
3469
 
        if (hdr_offs + LOG_EVENT_HEADER_LEN > length)
3470
 
        {
3471
 
          carry= length - hdr_offs;
3472
 
          memcpy(header, cache->read_pos + hdr_offs, carry);
3473
 
          length= hdr_offs;
3474
 
        }
3475
 
        else
3476
 
        {
3477
 
          /* we've got a full event-header, and it came in one piece */
3478
 
 
3479
 
          uchar *log_pos= (uchar *)cache->read_pos + hdr_offs + LOG_POS_OFFSET;
3480
 
 
3481
 
          /* fix end_log_pos */
3482
 
          val= uint4korr(log_pos) + group;
3483
 
          int4store(log_pos, val);
3484
 
 
3485
 
          /* next event header at ... */
3486
 
          log_pos= (uchar *)cache->read_pos + hdr_offs + EVENT_LEN_OFFSET;
3487
 
          hdr_offs += uint4korr(log_pos);
3488
 
 
3489
 
        }
3490
 
      }
3491
 
 
3492
 
      /*
3493
 
        Adjust hdr_offs. Note that it may still point beyond the segment
3494
 
        read in the next iteration; if the current event is very long,
3495
 
        it may take a couple of read-iterations (and subsequent adjustments
3496
 
        of hdr_offs) for it to point into the then-current segment.
3497
 
        If we have a split header (!carry), hdr_offs will be set at the
3498
 
        beginning of the next iteration, overwriting the value we set here:
3499
 
      */
3500
 
      hdr_offs -= length;
3501
 
    }
3502
 
 
3503
 
    /* Write data to the binary log file */
3504
 
    if (my_b_write(&log_file, cache->read_pos, length))
3505
 
      return ER_ERROR_ON_WRITE;
3506
 
    cache->read_pos=cache->read_end;            // Mark buffer used up
3507
 
  } while ((length= my_b_fill(cache)));
3508
 
 
3509
 
  assert(carry == 0);
3510
 
 
3511
 
  if (sync_log)
3512
 
    flush_and_sync();
3513
 
 
3514
 
  return 0;                                     // All OK
3515
 
}
3516
 
 
3517
 
/**
3518
 
  Write a cached log entry to the binary log.
3519
 
  - To support transaction over replication, we wrap the transaction
3520
 
  with BEGIN/COMMIT or BEGIN/ROLLBACK in the binary log.
3521
 
  We want to write a BEGIN/ROLLBACK block when a non-transactional table
3522
 
  was updated in a transaction which was rolled back. This is to ensure
3523
 
  that the same updates are run on the slave.
3524
 
 
3525
 
  @param thd
3526
 
  @param cache          The cache to copy to the binlog
3527
 
  @param commit_event   The commit event to print after writing the
3528
 
                        contents of the cache.
3529
 
 
3530
 
  @note
3531
 
    We only come here if there is something in the cache.
3532
 
  @note
3533
 
    The thing in the cache is always a complete transaction.
3534
 
  @note
3535
 
    'cache' needs to be reinitialized after this functions returns.
3536
 
*/
3537
 
 
3538
 
bool DRIZZLE_BIN_LOG::write(THD *thd, IO_CACHE *cache, Log_event *commit_event)
3539
 
{
3540
 
  VOID(pthread_mutex_lock(&LOCK_log));
3541
 
 
3542
 
  /* NULL would represent nothing to replicate after ROLLBACK */
3543
 
  assert(commit_event != NULL);
3544
 
 
3545
 
  assert(is_open());
3546
 
  if (likely(is_open()))                       // Should always be true
3547
 
  {
3548
 
    /*
3549
 
      We only bother to write to the binary log if there is anything
3550
 
      to write.
3551
 
     */
3552
 
    if (my_b_tell(cache) > 0)
3553
 
    {
3554
 
      /*
3555
 
        Log "BEGIN" at the beginning of every transaction.  Here, a
3556
 
        transaction is either a BEGIN..COMMIT block or a single
3557
 
        statement in autocommit mode.
3558
 
      */
3559
 
      Query_log_event qinfo(thd, STRING_WITH_LEN("BEGIN"), true, false);
3560
 
      /*
3561
 
        Imagine this is rollback due to net timeout, after all
3562
 
        statements of the transaction succeeded. Then we want a
3563
 
        zero-error code in BEGIN.  In other words, if there was a
3564
 
        really serious error code it's already in the statement's
3565
 
        events, there is no need to put it also in this internally
3566
 
        generated event, and as this event is generated late it would
3567
 
        lead to false alarms.
3568
 
 
3569
 
        This is safer than thd->clear_error() against kills at shutdown.
3570
 
      */
3571
 
      qinfo.error_code= 0;
3572
 
      /*
3573
 
        Now this Query_log_event has artificial log_pos 0. It must be
3574
 
        adjusted to reflect the real position in the log. Not doing it
3575
 
        would confuse the slave: it would prevent this one from
3576
 
        knowing where he is in the master's binlog, which would result
3577
 
        in wrong positions being shown to the user, MASTER_POS_WAIT
3578
 
        undue waiting etc.
3579
 
      */
3580
 
      if (qinfo.write(&log_file))
3581
 
        goto err;
3582
 
 
3583
 
      if ((write_error= write_cache(cache, false, false)))
3584
 
        goto err;
3585
 
 
3586
 
      if (commit_event && commit_event->write(&log_file))
3587
 
        goto err;
3588
 
      if (flush_and_sync())
3589
 
        goto err;
3590
 
      if (cache->error)                         // Error on read
3591
 
      {
3592
 
        sql_print_error(ER(ER_ERROR_ON_READ), cache->file_name, errno);
3593
 
        write_error=1;                          // Don't give more errors
3594
 
        goto err;
3595
 
      }
3596
 
      signal_update();
3597
 
    }
3598
 
 
3599
 
    /*
3600
 
      if commit_event is Xid_log_event, increase the number of
3601
 
      prepared_xids (it's decreasd in ::unlog()). Binlog cannot be rotated
3602
 
      if there're prepared xids in it - see the comment in new_file() for
3603
 
      an explanation.
3604
 
      If the commit_event is not Xid_log_event (then it's a Query_log_event)
3605
 
      rotate binlog, if necessary.
3606
 
    */
3607
 
    if (commit_event && commit_event->get_type_code() == XID_EVENT)
3608
 
    {
3609
 
      pthread_mutex_lock(&LOCK_prep_xids);
3610
 
      prepared_xids++;
3611
 
      pthread_mutex_unlock(&LOCK_prep_xids);
3612
 
    }
3613
 
    else
3614
 
      rotate_and_purge(RP_LOCK_LOG_IS_ALREADY_LOCKED);
3615
 
  }
3616
 
  VOID(pthread_mutex_unlock(&LOCK_log));
3617
 
 
3618
 
  return(0);
3619
 
 
3620
 
err:
3621
 
  if (!write_error)
3622
 
  {
3623
 
    write_error= 1;
3624
 
    sql_print_error(ER(ER_ERROR_ON_WRITE), name, errno);
3625
 
  }
3626
 
  VOID(pthread_mutex_unlock(&LOCK_log));
3627
 
  return(1);
3628
 
}
3629
 
 
3630
 
 
3631
 
/**
3632
 
  Wait until we get a signal that the relay log has been updated
3633
 
 
3634
 
  @param[in] thd   a THD struct
3635
 
  @note
3636
 
    LOCK_log must be taken before calling this function.
3637
 
    It will be released at the end of the function.
3638
 
*/
3639
 
 
3640
 
void DRIZZLE_BIN_LOG::wait_for_update_relay_log(THD* thd)
3641
 
{
3642
 
  const char *old_msg;
3643
 
  old_msg= thd->enter_cond(&update_cond, &LOCK_log,
3644
 
                           "Slave has read all relay log; " 
3645
 
                           "waiting for the slave I/O "
3646
 
                           "thread to update it" );
3647
 
  pthread_cond_wait(&update_cond, &LOCK_log);
3648
 
  thd->exit_cond(old_msg);
3649
 
  return;
3650
 
}
3651
 
 
3652
 
 
3653
 
/**
3654
 
  Wait until we get a signal that the binary log has been updated.
3655
 
  Applies to master only.
3656
 
     
3657
 
  NOTES
3658
 
  @param[in] thd        a THD struct
3659
 
  @param[in] timeout    a pointer to a timespec;
3660
 
                        NULL means to wait w/o timeout.
3661
 
  @retval    0          if got signalled on update
3662
 
  @retval    non-0      if wait timeout elapsed
3663
 
  @note
3664
 
    LOCK_log must be taken before calling this function.
3665
 
    LOCK_log is being released while the thread is waiting.
3666
 
    LOCK_log is released by the caller.
3667
 
*/
3668
 
 
3669
 
int DRIZZLE_BIN_LOG::wait_for_update_bin_log(THD* thd,
3670
 
                                           const struct timespec *timeout)
3671
 
{
3672
 
  int ret= 0;
3673
 
  const char* old_msg = thd->proc_info;
3674
 
  old_msg= thd->enter_cond(&update_cond, &LOCK_log,
3675
 
                           "Master has sent all binlog to slave; "
3676
 
                           "waiting for binlog to be updated");
3677
 
  if (!timeout)
3678
 
    pthread_cond_wait(&update_cond, &LOCK_log);
3679
 
  else
3680
 
    ret= pthread_cond_timedwait(&update_cond, &LOCK_log,
3681
 
                                const_cast<struct timespec *>(timeout));
3682
 
  return(ret);
3683
 
}
3684
 
 
3685
 
 
3686
 
/**
3687
 
  Close the log file.
3688
 
 
3689
 
  @param exiting     Bitmask for one or more of the following bits:
3690
 
          - LOG_CLOSE_INDEX : if we should close the index file
3691
 
          - LOG_CLOSE_TO_BE_OPENED : if we intend to call open
3692
 
                                     at once after close.
3693
 
          - LOG_CLOSE_STOP_EVENT : write a 'stop' event to the log
3694
 
 
3695
 
  @note
3696
 
    One can do an open on the object at once after doing a close.
3697
 
    The internal structures are not freed until cleanup() is called
3698
 
*/
3699
 
 
3700
 
void DRIZZLE_BIN_LOG::close(uint exiting)
3701
 
{                                       // One can't set log_type here!
3702
 
  if (log_state == LOG_OPENED)
3703
 
  {
3704
 
#ifdef HAVE_REPLICATION
3705
 
    if (log_type == LOG_BIN && !no_auto_events &&
3706
 
        (exiting & LOG_CLOSE_STOP_EVENT))
3707
 
    {
3708
 
      Stop_log_event s;
3709
 
      s.write(&log_file);
3710
 
      bytes_written+= s.data_written;
3711
 
      signal_update();
3712
 
    }
3713
 
#endif /* HAVE_REPLICATION */
3714
 
 
3715
 
    /* don't pwrite in a file opened with O_APPEND - it doesn't work */
3716
 
    if (log_file.type == WRITE_CACHE && log_type == LOG_BIN)
3717
 
    {
3718
 
      my_off_t offset= BIN_LOG_HEADER_SIZE + FLAGS_OFFSET;
3719
 
      uchar flags= 0;            // clearing LOG_EVENT_BINLOG_IN_USE_F
3720
 
      pwrite(log_file.file, &flags, 1, offset);
3721
 
    }
3722
 
 
3723
 
    /* this will cleanup IO_CACHE, sync and close the file */
3724
 
    DRIZZLE_LOG::close(exiting);
3725
 
  }
3726
 
 
3727
 
  /*
3728
 
    The following test is needed even if is_open() is not set, as we may have
3729
 
    called a not complete close earlier and the index file is still open.
3730
 
  */
3731
 
 
3732
 
  if ((exiting & LOG_CLOSE_INDEX) && my_b_inited(&index_file))
3733
 
  {
3734
 
    end_io_cache(&index_file);
3735
 
    if (my_close(index_file.file, MYF(0)) < 0 && ! write_error)
3736
 
    {
3737
 
      write_error= 1;
3738
 
      sql_print_error(ER(ER_ERROR_ON_WRITE), index_file_name, errno);
3739
 
    }
3740
 
  }
3741
 
  log_state= (exiting & LOG_CLOSE_TO_BE_OPENED) ? LOG_TO_BE_OPENED : LOG_CLOSED;
3742
 
  safeFree(name);
3743
 
  return;
3744
 
}
3745
 
 
3746
 
 
3747
 
void DRIZZLE_BIN_LOG::set_max_size(ulong max_size_arg)
3748
 
{
3749
 
  /*
3750
 
    We need to take locks, otherwise this may happen:
3751
 
    new_file() is called, calls open(old_max_size), then before open() starts,
3752
 
    set_max_size() sets max_size to max_size_arg, then open() starts and
3753
 
    uses the old_max_size argument, so max_size_arg has been overwritten and
3754
 
    it's like if the SET command was never run.
3755
 
  */
3756
 
  pthread_mutex_lock(&LOCK_log);
3757
 
  if (is_open())
3758
 
    max_size= max_size_arg;
3759
 
  pthread_mutex_unlock(&LOCK_log);
3760
 
  return;
3761
 
}
3762
 
 
3763
 
 
3764
 
/**
3765
 
  Check if a string is a valid number.
3766
 
 
3767
 
  @param str                    String to test
3768
 
  @param res                    Store value here
3769
 
  @param allow_wildcards        Set to 1 if we should ignore '%' and '_'
3770
 
 
3771
 
  @note
3772
 
    For the moment the allow_wildcards argument is not used
3773
 
    Should be move to some other file.
3774
 
 
3775
 
  @retval
3776
 
    1   String is a number
3777
 
  @retval
3778
 
    0   Error
3779
 
*/
3780
 
 
3781
 
static bool test_if_number(register const char *str,
3782
 
                           long *res, bool allow_wildcards)
3783
 
{
3784
 
  register int flag;
3785
 
  const char *start;
3786
 
 
3787
 
  flag= 0; 
3788
 
  start= str;
3789
 
  while (*str++ == ' ') ;
3790
 
  if (*--str == '-' || *str == '+')
3791
 
    str++;
3792
 
  while (my_isdigit(files_charset_info,*str) ||
3793
 
         (allow_wildcards && (*str == wild_many || *str == wild_one)))
3794
 
  {
3795
 
    flag=1;
3796
 
    str++;
3797
 
  }
3798
 
  if (*str == '.')
3799
 
  {
3800
 
    for (str++ ;
3801
 
         my_isdigit(files_charset_info,*str) ||
3802
 
           (allow_wildcards && (*str == wild_many || *str == wild_one)) ;
3803
 
         str++, flag=1) ;
3804
 
  }
3805
 
  if (*str != 0 || flag == 0)
3806
 
    return(0);
3807
 
  if (res)
3808
 
    *res=atol(start);
3809
 
  return(1);                    /* Number ok */
3810
 
} /* test_if_number */
3811
 
 
3812
 
 
3813
 
void sql_perror(const char *message)
3814
 
{
3815
 
#ifdef HAVE_STRERROR
3816
 
  sql_print_error("%s: %s",message, strerror(errno));
3817
 
#else
3818
 
  perror(message);
3819
 
#endif
3820
 
}
3821
 
 
3822
 
 
3823
 
bool flush_error_log()
3824
 
{
3825
 
  bool result=0;
3826
 
  if (opt_error_log)
3827
 
  {
3828
 
    char err_renamed[FN_REFLEN], *end;
3829
 
    end= strmake(err_renamed,log_error_file,FN_REFLEN-4);
3830
 
    stpcpy(end, "-old");
3831
 
    VOID(pthread_mutex_lock(&LOCK_error_log));
3832
 
    char err_temp[FN_REFLEN+4];
3833
 
    /*
3834
 
     On Windows is necessary a temporary file for to rename
3835
 
     the current error file.
3836
 
    */
3837
 
    strxmov(err_temp, err_renamed,"-tmp",NullS);
3838
 
    (void) my_delete(err_temp, MYF(0)); 
3839
 
    if (freopen(err_temp,"a+",stdout))
3840
 
    {
3841
 
      int fd;
3842
 
      size_t bytes;
3843
 
      uchar buf[IO_SIZE];
3844
 
 
3845
 
      freopen(err_temp,"a+",stderr);
3846
 
      (void) my_delete(err_renamed, MYF(0));
3847
 
      my_rename(log_error_file,err_renamed,MYF(0));
3848
 
      if (freopen(log_error_file,"a+",stdout))
3849
 
        freopen(log_error_file,"a+",stderr);
3850
 
 
3851
 
      if ((fd = my_open(err_temp, O_RDONLY, MYF(0))) >= 0)
3852
 
      {
3853
 
        while ((bytes= my_read(fd, buf, IO_SIZE, MYF(0))) &&
3854
 
               bytes != MY_FILE_ERROR)
3855
 
          my_fwrite(stderr, buf, bytes, MYF(0));
3856
 
        my_close(fd, MYF(0));
3857
 
      }
3858
 
      (void) my_delete(err_temp, MYF(0)); 
3859
 
    }
3860
 
    else
3861
 
     result= 1;
3862
 
    VOID(pthread_mutex_unlock(&LOCK_error_log));
3863
 
  }
3864
 
   return result;
3865
 
}
3866
 
 
3867
 
void DRIZZLE_BIN_LOG::signal_update()
3868
 
{
3869
 
  pthread_cond_broadcast(&update_cond);
3870
 
  return;
3871
 
}
3872
 
 
3873
 
/**
3874
 
  Prints a printf style message to the error log and, under NT, to the
3875
 
  Windows event log.
3876
 
 
3877
 
  This function prints the message into a buffer and then sends that buffer
3878
 
  to other functions to write that message to other logging sources.
3879
 
 
3880
 
  @param event_type          Type of event to write (Error, Warning, or Info)
3881
 
  @param format              Printf style format of message
3882
 
  @param args                va_list list of arguments for the message
3883
 
 
3884
 
  @returns
3885
 
    The function always returns 0. The return value is present in the
3886
 
    signature to be compatible with other logging routines, which could
3887
 
    return an error (e.g. logging to the log tables)
3888
 
*/
3889
 
static void print_buffer_to_file(enum loglevel level,
3890
 
                                 int error_code __attribute__((unused)),
3891
 
                                 const char *buffer,
3892
 
                                 size_t buffer_length __attribute__((unused)))
3893
 
{
3894
 
  time_t skr;
3895
 
  struct tm tm_tmp;
3896
 
  struct tm *start;
3897
 
 
3898
 
  VOID(pthread_mutex_lock(&LOCK_error_log));
3899
 
 
3900
 
  skr= my_time(0);
3901
 
  localtime_r(&skr, &tm_tmp);
3902
 
  start=&tm_tmp;
3903
 
 
3904
 
  fprintf(stderr, "%02d%02d%02d %2d:%02d:%02d [%s] %s\n",
3905
 
          start->tm_year % 100,
3906
 
          start->tm_mon+1,
3907
 
          start->tm_mday,
3908
 
          start->tm_hour,
3909
 
          start->tm_min,
3910
 
          start->tm_sec,
3911
 
          (level == ERROR_LEVEL ? "ERROR" : level == WARNING_LEVEL ?
3912
 
           "Warning" : "Note"),
3913
 
          buffer);
3914
 
 
3915
 
  fflush(stderr);
3916
 
 
3917
 
  VOID(pthread_mutex_unlock(&LOCK_error_log));
3918
 
  return;
3919
 
}
3920
 
 
3921
 
 
3922
 
int vprint_msg_to_log(enum loglevel level, const char *format, va_list args)
3923
 
{
3924
 
  char   buff[1024];
3925
 
  size_t length;
3926
 
  int error_code= errno;
3927
 
 
3928
 
  length= vsnprintf(buff, sizeof(buff), format, args);
3929
 
 
3930
 
  print_buffer_to_file(level, error_code, buff, length);
3931
 
 
3932
 
  return(0);
3933
 
}
3934
 
 
3935
 
 
3936
 
void sql_print_error(const char *format, ...) 
3937
 
{
3938
 
  va_list args;
3939
 
 
3940
 
  va_start(args, format);
3941
 
  error_log_print(ERROR_LEVEL, format, args);
3942
 
  va_end(args);
3943
 
 
3944
 
  return;
3945
 
}
3946
 
 
3947
 
 
3948
 
void sql_print_warning(const char *format, ...) 
3949
 
{
3950
 
  va_list args;
3951
 
 
3952
 
  va_start(args, format);
3953
 
  error_log_print(WARNING_LEVEL, format, args);
3954
 
  va_end(args);
3955
 
 
3956
 
  return;
3957
 
}
3958
 
 
3959
 
 
3960
 
void sql_print_information(const char *format, ...) 
3961
 
{
3962
 
  va_list args;
3963
 
 
3964
 
  va_start(args, format);
3965
 
  error_log_print(INFORMATION_LEVEL, format, args);
3966
 
  va_end(args);
3967
 
 
3968
 
  return;
3969
 
}
3970
 
 
3971
 
 
3972
 
/********* transaction coordinator log for 2pc - mmap() based solution *******/
3973
 
 
3974
 
/*
3975
 
  the log consists of a file, mmapped to a memory.
3976
 
  file is divided on pages of tc_log_page_size size.
3977
 
  (usable size of the first page is smaller because of log header)
3978
 
  there's PAGE control structure for each page
3979
 
  each page (or rather PAGE control structure) can be in one of three
3980
 
  states - active, syncing, pool.
3981
 
  there could be only one page in active or syncing states,
3982
 
  but many in pool - pool is fifo queue.
3983
 
  usual lifecycle of a page is pool->active->syncing->pool
3984
 
  "active" page - is a page where new xid's are logged.
3985
 
  the page stays active as long as syncing slot is taken.
3986
 
  "syncing" page is being synced to disk. no new xid can be added to it.
3987
 
  when the sync is done the page is moved to a pool and an active page
3988
 
  becomes "syncing".
3989
 
 
3990
 
  the result of such an architecture is a natural "commit grouping" -
3991
 
  If commits are coming faster than the system can sync, they do not
3992
 
  stall. Instead, all commit that came since the last sync are
3993
 
  logged to the same page, and they all are synced with the next -
3994
 
  one - sync. Thus, thought individual commits are delayed, throughput
3995
 
  is not decreasing.
3996
 
 
3997
 
  when a xid is added to an active page, the thread of this xid waits
3998
 
  for a page's condition until the page is synced. when syncing slot
3999
 
  becomes vacant one of these waiters is awaken to take care of syncing.
4000
 
  it syncs the page and signals all waiters that the page is synced.
4001
 
  PAGE::waiters is used to count these waiters, and a page may never
4002
 
  become active again until waiters==0 (that is all waiters from the
4003
 
  previous sync have noticed the sync was completed)
4004
 
 
4005
 
  note, that the page becomes "dirty" and has to be synced only when a
4006
 
  new xid is added into it. Removing a xid from a page does not make it
4007
 
  dirty - we don't sync removals to disk.
4008
 
*/
4009
 
 
4010
 
ulong tc_log_page_waits= 0;
4011
 
 
4012
 
#ifdef HAVE_MMAP
4013
 
 
4014
 
#define TC_LOG_HEADER_SIZE (sizeof(tc_log_magic)+1)
4015
 
 
4016
 
static const char tc_log_magic[]={(char) 254, 0x23, 0x05, 0x74};
4017
 
 
4018
 
ulong opt_tc_log_size= TC_LOG_MIN_SIZE;
4019
 
ulong tc_log_max_pages_used=0, tc_log_page_size=0, tc_log_cur_pages_used=0;
4020
 
 
4021
 
int TC_LOG_MMAP::open(const char *opt_name)
4022
 
{
4023
 
  uint i;
4024
 
  bool crashed= false;
4025
 
  PAGE *pg;
4026
 
 
4027
 
  assert(total_ha_2pc > 1);
4028
 
  assert(opt_name && opt_name[0]);
4029
 
 
4030
 
  tc_log_page_size= getpagesize();
4031
 
  assert(TC_LOG_PAGE_SIZE % tc_log_page_size == 0);
4032
 
 
4033
 
  fn_format(logname,opt_name,mysql_data_home,"",MY_UNPACK_FILENAME);
4034
 
  if ((fd= my_open(logname, O_RDWR, MYF(0))) < 0)
4035
 
  {
4036
 
    if (my_errno != ENOENT)
4037
 
      goto err;
4038
 
    if (using_heuristic_recover())
4039
 
      return 1;
4040
 
    if ((fd= my_create(logname, CREATE_MODE, O_RDWR, MYF(MY_WME))) < 0)
4041
 
      goto err;
4042
 
    inited=1;
4043
 
    file_length= opt_tc_log_size;
4044
 
    if (ftruncate(fd, file_length))
4045
 
      goto err;
4046
 
  }
4047
 
  else
4048
 
  {
4049
 
    inited= 1;
4050
 
    crashed= true;
4051
 
    sql_print_information("Recovering after a crash using %s", opt_name);
4052
 
    if (tc_heuristic_recover)
4053
 
    {
4054
 
      sql_print_error("Cannot perform automatic crash recovery when "
4055
 
                      "--tc-heuristic-recover is used");
4056
 
      goto err;
4057
 
    }
4058
 
    file_length= my_seek(fd, 0L, MY_SEEK_END, MYF(MY_WME+MY_FAE));
4059
 
    if (file_length == MY_FILEPOS_ERROR || file_length % tc_log_page_size)
4060
 
      goto err;
4061
 
  }
4062
 
 
4063
 
  data= (uchar *)my_mmap(0, (size_t)file_length, PROT_READ|PROT_WRITE,
4064
 
                        MAP_NOSYNC|MAP_SHARED, fd, 0);
4065
 
  if (data == MAP_FAILED)
4066
 
  {
4067
 
    my_errno=errno;
4068
 
    goto err;
4069
 
  }
4070
 
  inited=2;
4071
 
 
4072
 
  npages=(uint)file_length/tc_log_page_size;
4073
 
  assert(npages >= 3);             // to guarantee non-empty pool
4074
 
  if (!(pages=(PAGE *)my_malloc(npages*sizeof(PAGE), MYF(MY_WME|MY_ZEROFILL))))
4075
 
    goto err;
4076
 
  inited=3;
4077
 
  for (pg=pages, i=0; i < npages; i++, pg++)
4078
 
  {
4079
 
    pg->next=pg+1;
4080
 
    pg->waiters=0;
4081
 
    pg->state=POOL;
4082
 
    pthread_mutex_init(&pg->lock, MY_MUTEX_INIT_FAST);
4083
 
    pthread_cond_init (&pg->cond, 0);
4084
 
    pg->start=(my_xid *)(data + i*tc_log_page_size);
4085
 
    pg->ptr=pg->start;
4086
 
    pg->end=(my_xid *)(pg->start + tc_log_page_size);
4087
 
    pg->size=pg->free=tc_log_page_size/sizeof(my_xid);
4088
 
  }
4089
 
  pages[0].size=pages[0].free=
4090
 
                (tc_log_page_size-TC_LOG_HEADER_SIZE)/sizeof(my_xid);
4091
 
  pages[0].start=pages[0].end-pages[0].size;
4092
 
  pages[npages-1].next=0;
4093
 
  inited=4;
4094
 
 
4095
 
  if (crashed && recover())
4096
 
      goto err;
4097
 
 
4098
 
  memcpy(data, tc_log_magic, sizeof(tc_log_magic));
4099
 
  data[sizeof(tc_log_magic)]= (uchar)total_ha_2pc;
4100
 
  msync(data, tc_log_page_size, MS_SYNC);
4101
 
  my_sync(fd, MYF(0));
4102
 
  inited=5;
4103
 
 
4104
 
  pthread_mutex_init(&LOCK_sync,    MY_MUTEX_INIT_FAST);
4105
 
  pthread_mutex_init(&LOCK_active,  MY_MUTEX_INIT_FAST);
4106
 
  pthread_mutex_init(&LOCK_pool,    MY_MUTEX_INIT_FAST);
4107
 
  pthread_cond_init(&COND_active, 0);
4108
 
  pthread_cond_init(&COND_pool, 0);
4109
 
 
4110
 
  inited=6;
4111
 
 
4112
 
  syncing= 0;
4113
 
  active=pages;
4114
 
  pool=pages+1;
4115
 
  pool_last=pages+npages-1;
4116
 
 
4117
 
  return 0;
4118
 
 
4119
 
err:
4120
 
  close();
4121
 
  return 1;
4122
 
}
4123
 
 
4124
 
/**
4125
 
  there is no active page, let's got one from the pool.
4126
 
 
4127
 
  Two strategies here:
4128
 
    -# take the first from the pool
4129
 
    -# if there're waiters - take the one with the most free space.
4130
 
 
4131
 
  @todo
4132
 
    TODO page merging. try to allocate adjacent page first,
4133
 
    so that they can be flushed both in one sync
4134
 
*/
4135
 
 
4136
 
void TC_LOG_MMAP::get_active_from_pool()
4137
 
{
4138
 
  PAGE **p, **best_p=0;
4139
 
  int best_free;
4140
 
 
4141
 
  if (syncing)
4142
 
    pthread_mutex_lock(&LOCK_pool);
4143
 
 
4144
 
  do
4145
 
  {
4146
 
    best_p= p= &pool;
4147
 
    if ((*p)->waiters == 0) // can the first page be used ?
4148
 
      break;                // yes - take it.
4149
 
 
4150
 
    best_free=0;            // no - trying second strategy
4151
 
    for (p=&(*p)->next; *p; p=&(*p)->next)
4152
 
    {
4153
 
      if ((*p)->waiters == 0 && (*p)->free > best_free)
4154
 
      {
4155
 
        best_free=(*p)->free;
4156
 
        best_p=p;
4157
 
      }
4158
 
    }
4159
 
  }
4160
 
  while ((*best_p == 0 || best_free == 0) && overflow());
4161
 
 
4162
 
  active=*best_p;
4163
 
  if (active->free == active->size) // we've chosen an empty page
4164
 
  {
4165
 
    tc_log_cur_pages_used++;
4166
 
    set_if_bigger(tc_log_max_pages_used, tc_log_cur_pages_used);
4167
 
  }
4168
 
 
4169
 
  if ((*best_p)->next)              // unlink the page from the pool
4170
 
    *best_p=(*best_p)->next;
4171
 
  else
4172
 
    pool_last=*best_p;
4173
 
 
4174
 
  if (syncing)
4175
 
    pthread_mutex_unlock(&LOCK_pool);
4176
 
}
4177
 
 
4178
 
/**
4179
 
  @todo
4180
 
  perhaps, increase log size ?
4181
 
*/
4182
 
int TC_LOG_MMAP::overflow()
4183
 
{
4184
 
  /*
4185
 
    simple overflow handling - just wait
4186
 
    TODO perhaps, increase log size ?
4187
 
    let's check the behaviour of tc_log_page_waits first
4188
 
  */
4189
 
  tc_log_page_waits++;
4190
 
  pthread_cond_wait(&COND_pool, &LOCK_pool);
4191
 
  return 1; // always return 1
4192
 
}
4193
 
 
4194
 
/**
4195
 
  Record that transaction XID is committed on the persistent storage.
4196
 
 
4197
 
    This function is called in the middle of two-phase commit:
4198
 
    First all resources prepare the transaction, then tc_log->log() is called,
4199
 
    then all resources commit the transaction, then tc_log->unlog() is called.
4200
 
 
4201
 
    All access to active page is serialized but it's not a problem, as
4202
 
    we're assuming that fsync() will be a main bottleneck.
4203
 
    That is, parallelizing writes to log pages we'll decrease number of
4204
 
    threads waiting for a page, but then all these threads will be waiting
4205
 
    for a fsync() anyway
4206
 
 
4207
 
   If tc_log == DRIZZLE_LOG then tc_log writes transaction to binlog and
4208
 
   records XID in a special Xid_log_event.
4209
 
   If tc_log = TC_LOG_MMAP then xid is written in a special memory-mapped
4210
 
   log.
4211
 
 
4212
 
  @retval
4213
 
    0  - error
4214
 
  @retval
4215
 
    \# - otherwise, "cookie", a number that will be passed as an argument
4216
 
    to unlog() call. tc_log can define it any way it wants,
4217
 
    and use for whatever purposes. TC_LOG_MMAP sets it
4218
 
    to the position in memory where xid was logged to.
4219
 
*/
4220
 
 
4221
 
int TC_LOG_MMAP::log_xid(THD *thd __attribute__((unused)), my_xid xid)
4222
 
{
4223
 
  int err;
4224
 
  PAGE *p;
4225
 
  ulong cookie;
4226
 
 
4227
 
  pthread_mutex_lock(&LOCK_active);
4228
 
 
4229
 
  /*
4230
 
    if active page is full - just wait...
4231
 
    frankly speaking, active->free here accessed outside of mutex
4232
 
    protection, but it's safe, because it only means we may miss an
4233
 
    unlog() for the active page, and we're not waiting for it here -
4234
 
    unlog() does not signal COND_active.
4235
 
  */
4236
 
  while (unlikely(active && active->free == 0))
4237
 
    pthread_cond_wait(&COND_active, &LOCK_active);
4238
 
 
4239
 
  /* no active page ? take one from the pool */
4240
 
  if (active == 0)
4241
 
    get_active_from_pool();
4242
 
 
4243
 
  p=active;
4244
 
  pthread_mutex_lock(&p->lock);
4245
 
 
4246
 
  /* searching for an empty slot */
4247
 
  while (*p->ptr)
4248
 
  {
4249
 
    p->ptr++;
4250
 
    assert(p->ptr < p->end);               // because p->free > 0
4251
 
  }
4252
 
 
4253
 
  /* found! store xid there and mark the page dirty */
4254
 
  cookie= (ulong)((uchar *)p->ptr - data);      // can never be zero
4255
 
  *p->ptr++= xid;
4256
 
  p->free--;
4257
 
  p->state= DIRTY;
4258
 
 
4259
 
  /* to sync or not to sync - this is the question */
4260
 
  pthread_mutex_unlock(&LOCK_active);
4261
 
  pthread_mutex_lock(&LOCK_sync);
4262
 
  pthread_mutex_unlock(&p->lock);
4263
 
 
4264
 
  if (syncing)
4265
 
  {                                          // somebody's syncing. let's wait
4266
 
    p->waiters++;
4267
 
    /*
4268
 
      note - it must be while (), not do ... while () here
4269
 
      as p->state may be not DIRTY when we come here
4270
 
    */
4271
 
    while (p->state == DIRTY && syncing)
4272
 
      pthread_cond_wait(&p->cond, &LOCK_sync);
4273
 
    p->waiters--;
4274
 
    err= p->state == ERROR;
4275
 
    if (p->state != DIRTY)                   // page was synced
4276
 
    {
4277
 
      if (p->waiters == 0)
4278
 
        pthread_cond_signal(&COND_pool);     // in case somebody's waiting
4279
 
      pthread_mutex_unlock(&LOCK_sync);
4280
 
      goto done;                             // we're done
4281
 
    }
4282
 
  }                                          // page was not synced! do it now
4283
 
  assert(active == p && syncing == 0);
4284
 
  pthread_mutex_lock(&LOCK_active);
4285
 
  syncing=p;                                 // place is vacant - take it
4286
 
  active=0;                                  // page is not active anymore
4287
 
  pthread_cond_broadcast(&COND_active);      // in case somebody's waiting
4288
 
  pthread_mutex_unlock(&LOCK_active);
4289
 
  pthread_mutex_unlock(&LOCK_sync);
4290
 
  err= sync();
4291
 
 
4292
 
done:
4293
 
  return err ? 0 : cookie;
4294
 
}
4295
 
 
4296
 
int TC_LOG_MMAP::sync()
4297
 
{
4298
 
  int err;
4299
 
 
4300
 
  assert(syncing != active);
4301
 
 
4302
 
  /*
4303
 
    sit down and relax - this can take a while...
4304
 
    note - no locks are held at this point
4305
 
  */
4306
 
  err= msync(syncing->start, 1, MS_SYNC);
4307
 
  if(err==0)
4308
 
    err= my_sync(fd, MYF(0));
4309
 
 
4310
 
  /* page is synced. let's move it to the pool */
4311
 
  pthread_mutex_lock(&LOCK_pool);
4312
 
  pool_last->next=syncing;
4313
 
  pool_last=syncing;
4314
 
  syncing->next=0;
4315
 
  syncing->state= err ? ERROR : POOL;
4316
 
  pthread_cond_broadcast(&syncing->cond);    // signal "sync done"
4317
 
  pthread_cond_signal(&COND_pool);           // in case somebody's waiting
4318
 
  pthread_mutex_unlock(&LOCK_pool);
4319
 
 
4320
 
  /* marking 'syncing' slot free */
4321
 
  pthread_mutex_lock(&LOCK_sync);
4322
 
  syncing=0;
4323
 
  pthread_cond_signal(&active->cond);        // wake up a new syncer
4324
 
  pthread_mutex_unlock(&LOCK_sync);
4325
 
  return err;
4326
 
}
4327
 
 
4328
 
/**
4329
 
  erase xid from the page, update page free space counters/pointers.
4330
 
  cookie points directly to the memory where xid was logged.
4331
 
*/
4332
 
 
4333
 
void TC_LOG_MMAP::unlog(ulong cookie, my_xid xid __attribute__((unused)))
4334
 
{
4335
 
  PAGE *p=pages+(cookie/tc_log_page_size);
4336
 
  my_xid *x=(my_xid *)(data+cookie);
4337
 
 
4338
 
  assert(*x == xid);
4339
 
  assert(x >= p->start && x < p->end);
4340
 
  *x=0;
4341
 
 
4342
 
  pthread_mutex_lock(&p->lock);
4343
 
  p->free++;
4344
 
  assert(p->free <= p->size);
4345
 
  set_if_smaller(p->ptr, x);
4346
 
  if (p->free == p->size)               // the page is completely empty
4347
 
    statistic_decrement(tc_log_cur_pages_used, &LOCK_status);
4348
 
  if (p->waiters == 0)                 // the page is in pool and ready to rock
4349
 
    pthread_cond_signal(&COND_pool);   // ping ... for overflow()
4350
 
  pthread_mutex_unlock(&p->lock);
4351
 
}
4352
 
 
4353
 
void TC_LOG_MMAP::close()
4354
 
{
4355
 
  uint i;
4356
 
  switch (inited) {
4357
 
  case 6:
4358
 
    pthread_mutex_destroy(&LOCK_sync);
4359
 
    pthread_mutex_destroy(&LOCK_active);
4360
 
    pthread_mutex_destroy(&LOCK_pool);
4361
 
    pthread_cond_destroy(&COND_pool);
4362
 
  case 5:
4363
 
    data[0]='A'; // garble the first (signature) byte, in case my_delete fails
4364
 
  case 4:
4365
 
    for (i=0; i < npages; i++)
4366
 
    {
4367
 
      if (pages[i].ptr == 0)
4368
 
        break;
4369
 
      pthread_mutex_destroy(&pages[i].lock);
4370
 
      pthread_cond_destroy(&pages[i].cond);
4371
 
    }
4372
 
  case 3:
4373
 
    my_free((uchar*)pages, MYF(0));
4374
 
  case 2:
4375
 
    my_munmap((char*)data, (size_t)file_length);
4376
 
  case 1:
4377
 
    my_close(fd, MYF(0));
4378
 
  }
4379
 
  if (inited>=5) // cannot do in the switch because of Windows
4380
 
    my_delete(logname, MYF(MY_WME));
4381
 
  inited=0;
4382
 
}
4383
 
 
4384
 
int TC_LOG_MMAP::recover()
4385
 
{
4386
 
  HASH xids;
4387
 
  PAGE *p=pages, *end_p=pages+npages;
4388
 
 
4389
 
  if (memcmp(data, tc_log_magic, sizeof(tc_log_magic)))
4390
 
  {
4391
 
    sql_print_error("Bad magic header in tc log");
4392
 
    goto err1;
4393
 
  }
4394
 
 
4395
 
  /*
4396
 
    the first byte after magic signature is set to current
4397
 
    number of storage engines on startup
4398
 
  */
4399
 
  if (data[sizeof(tc_log_magic)] != total_ha_2pc)
4400
 
  {
4401
 
    sql_print_error("Recovery failed! You must enable "
4402
 
                    "exactly %d storage engines that support "
4403
 
                    "two-phase commit protocol",
4404
 
                    data[sizeof(tc_log_magic)]);
4405
 
    goto err1;
4406
 
  }
4407
 
 
4408
 
  if (hash_init(&xids, &my_charset_bin, tc_log_page_size/3, 0,
4409
 
                sizeof(my_xid), 0, 0, MYF(0)))
4410
 
    goto err1;
4411
 
 
4412
 
  for ( ; p < end_p ; p++)
4413
 
  {
4414
 
    for (my_xid *x=p->start; x < p->end; x++)
4415
 
      if (*x && my_hash_insert(&xids, (uchar *)x))
4416
 
        goto err2; // OOM
4417
 
  }
4418
 
 
4419
 
  if (ha_recover(&xids))
4420
 
    goto err2;
4421
 
 
4422
 
  hash_free(&xids);
4423
 
  memset(data, 0, (size_t)file_length);
4424
 
  return 0;
4425
 
 
4426
 
err2:
4427
 
  hash_free(&xids);
4428
 
err1:
4429
 
  sql_print_error("Crash recovery failed. Either correct the problem "
4430
 
                  "(if it's, for example, out of memory error) and restart, "
4431
 
                  "or delete tc log and start mysqld with "
4432
 
                  "--tc-heuristic-recover={commit|rollback}");
4433
 
  return 1;
4434
 
}
4435
 
#endif
4436
 
 
4437
 
TC_LOG *tc_log;
4438
 
TC_LOG_DUMMY tc_log_dummy;
4439
 
TC_LOG_MMAP  tc_log_mmap;
4440
 
 
4441
 
/**
4442
 
  Perform heuristic recovery, if --tc-heuristic-recover was used.
4443
 
 
4444
 
  @note
4445
 
    no matter whether heuristic recovery was successful or not
4446
 
    mysqld must exit. So, return value is the same in both cases.
4447
 
 
4448
 
  @retval
4449
 
    0   no heuristic recovery was requested
4450
 
  @retval
4451
 
    1   heuristic recovery was performed
4452
 
*/
4453
 
 
4454
 
int TC_LOG::using_heuristic_recover()
4455
 
{
4456
 
  if (!tc_heuristic_recover)
4457
 
    return 0;
4458
 
 
4459
 
  sql_print_information("Heuristic crash recovery mode");
4460
 
  if (ha_recover(0))
4461
 
    sql_print_error("Heuristic crash recovery failed");
4462
 
  sql_print_information("Please restart mysqld without --tc-heuristic-recover");
4463
 
  return 1;
4464
 
}
4465
 
 
4466
 
/****** transaction coordinator log for 2pc - binlog() based solution ******/
4467
 
#define TC_LOG_BINLOG DRIZZLE_BIN_LOG
4468
 
 
4469
 
/**
4470
 
  @todo
4471
 
  keep in-memory list of prepared transactions
4472
 
  (add to list in log(), remove on unlog())
4473
 
  and copy it to the new binlog if rotated
4474
 
  but let's check the behaviour of tc_log_page_waits first!
4475
 
*/
4476
 
 
4477
 
int TC_LOG_BINLOG::open(const char *opt_name)
4478
 
{
4479
 
  LOG_INFO log_info;
4480
 
  int      error= 1;
4481
 
 
4482
 
  assert(total_ha_2pc > 1);
4483
 
  assert(opt_name && opt_name[0]);
4484
 
 
4485
 
  pthread_mutex_init(&LOCK_prep_xids, MY_MUTEX_INIT_FAST);
4486
 
  pthread_cond_init (&COND_prep_xids, 0);
4487
 
 
4488
 
  if (!my_b_inited(&index_file))
4489
 
  {
4490
 
    /* There was a failure to open the index file, can't open the binlog */
4491
 
    cleanup();
4492
 
    return 1;
4493
 
  }
4494
 
 
4495
 
  if (using_heuristic_recover())
4496
 
  {
4497
 
    /* generate a new binlog to mask a corrupted one */
4498
 
    open(opt_name, LOG_BIN, 0, WRITE_CACHE, 0, max_binlog_size, 0);
4499
 
    cleanup();
4500
 
    return 1;
4501
 
  }
4502
 
 
4503
 
  if ((error= find_log_pos(&log_info, NullS, 1)))
4504
 
  {
4505
 
    if (error != LOG_INFO_EOF)
4506
 
      sql_print_error("find_log_pos() failed (error: %d)", error);
4507
 
    else
4508
 
      error= 0;
4509
 
    goto err;
4510
 
  }
4511
 
 
4512
 
  {
4513
 
    const char *errmsg;
4514
 
    IO_CACHE    log;
4515
 
    File        file;
4516
 
    Log_event  *ev=0;
4517
 
    Format_description_log_event fdle(BINLOG_VERSION);
4518
 
    char        log_name[FN_REFLEN];
4519
 
 
4520
 
    if (! fdle.is_valid())
4521
 
      goto err;
4522
 
 
4523
 
    do
4524
 
    {
4525
 
      strmake(log_name, log_info.log_file_name, sizeof(log_name)-1);
4526
 
    } while (!(error= find_next_log(&log_info, 1)));
4527
 
 
4528
 
    if (error !=  LOG_INFO_EOF)
4529
 
    {
4530
 
      sql_print_error("find_log_pos() failed (error: %d)", error);
4531
 
      goto err;
4532
 
    }
4533
 
 
4534
 
    if ((file= open_binlog(&log, log_name, &errmsg)) < 0)
4535
 
    {
4536
 
      sql_print_error("%s", errmsg);
4537
 
      goto err;
4538
 
    }
4539
 
 
4540
 
    if ((ev= Log_event::read_log_event(&log, 0, &fdle)) &&
4541
 
        ev->get_type_code() == FORMAT_DESCRIPTION_EVENT &&
4542
 
        ev->flags & LOG_EVENT_BINLOG_IN_USE_F)
4543
 
    {
4544
 
      sql_print_information("Recovering after a crash using %s", opt_name);
4545
 
      error= recover(&log, (Format_description_log_event *)ev);
4546
 
    }
4547
 
    else
4548
 
      error=0;
4549
 
 
4550
 
    delete ev;
4551
 
    end_io_cache(&log);
4552
 
    my_close(file, MYF(MY_WME));
4553
 
 
4554
 
    if (error)
4555
 
      goto err;
4556
 
  }
4557
 
 
4558
 
err:
4559
 
  return error;
4560
 
}
4561
 
 
4562
 
/** This is called on shutdown, after ha_panic. */
4563
 
void TC_LOG_BINLOG::close()
4564
 
{
4565
 
  assert(prepared_xids==0);
4566
 
  pthread_mutex_destroy(&LOCK_prep_xids);
4567
 
  pthread_cond_destroy (&COND_prep_xids);
4568
 
}
4569
 
 
4570
 
/**
4571
 
  @todo
4572
 
  group commit
4573
 
 
4574
 
  @retval
4575
 
    0    error
4576
 
  @retval
4577
 
    1    success
4578
 
*/
4579
 
int TC_LOG_BINLOG::log_xid(THD *thd, my_xid xid)
4580
 
{
4581
 
  Xid_log_event xle(thd, xid);
4582
 
  binlog_trx_data *trx_data=
4583
 
    (binlog_trx_data*) thd_get_ha_data(thd, binlog_hton);
4584
 
  /*
4585
 
    We always commit the entire transaction when writing an XID. Also
4586
 
    note that the return value is inverted.
4587
 
   */
4588
 
  return(!binlog_end_trans(thd, trx_data, &xle, true));
4589
 
}
4590
 
 
4591
 
void TC_LOG_BINLOG::unlog(ulong cookie __attribute__((unused)),
4592
 
                          my_xid xid __attribute__((unused)))
4593
 
{
4594
 
  pthread_mutex_lock(&LOCK_prep_xids);
4595
 
  assert(prepared_xids > 0);
4596
 
  if (--prepared_xids == 0) {
4597
 
    pthread_cond_signal(&COND_prep_xids);
4598
 
  }
4599
 
  pthread_mutex_unlock(&LOCK_prep_xids);
4600
 
  rotate_and_purge(0);     // as ::write() did not rotate
4601
 
}
4602
 
 
4603
 
int TC_LOG_BINLOG::recover(IO_CACHE *log, Format_description_log_event *fdle)
4604
 
{
4605
 
  Log_event  *ev;
4606
 
  HASH xids;
4607
 
  MEM_ROOT mem_root;
4608
 
 
4609
 
  if (! fdle->is_valid() ||
4610
 
      hash_init(&xids, &my_charset_bin, TC_LOG_PAGE_SIZE/3, 0,
4611
 
                sizeof(my_xid), 0, 0, MYF(0)))
4612
 
    goto err1;
4613
 
 
4614
 
  init_alloc_root(&mem_root, TC_LOG_PAGE_SIZE, TC_LOG_PAGE_SIZE);
4615
 
 
4616
 
  fdle->flags&= ~LOG_EVENT_BINLOG_IN_USE_F; // abort on the first error
4617
 
 
4618
 
  while ((ev= Log_event::read_log_event(log,0,fdle)) && ev->is_valid())
4619
 
  {
4620
 
    if (ev->get_type_code() == XID_EVENT)
4621
 
    {
4622
 
      Xid_log_event *xev=(Xid_log_event *)ev;
4623
 
      uchar *x= (uchar *) memdup_root(&mem_root, (uchar*) &xev->xid,
4624
 
                                      sizeof(xev->xid));
4625
 
      if (! x)
4626
 
        goto err2;
4627
 
      my_hash_insert(&xids, x);
4628
 
    }
4629
 
    delete ev;
4630
 
  }
4631
 
 
4632
 
  if (ha_recover(&xids))
4633
 
    goto err2;
4634
 
 
4635
 
  free_root(&mem_root, MYF(0));
4636
 
  hash_free(&xids);
4637
 
  return 0;
4638
 
 
4639
 
err2:
4640
 
  free_root(&mem_root, MYF(0));
4641
 
  hash_free(&xids);
4642
 
err1:
4643
 
  sql_print_error("Crash recovery failed. Either correct the problem "
4644
 
                  "(if it's, for example, out of memory error) and restart, "
4645
 
                  "or delete (or rename) binary log and start mysqld with "
4646
 
                  "--tc-heuristic-recover={commit|rollback}");
4647
 
  return 1;
4648
 
}
4649
 
 
4650
 
 
4651
 
#ifdef INNODB_COMPATIBILITY_HOOKS
4652
 
/**
4653
 
  Get the file name of the MySQL binlog.
4654
 
  @return the name of the binlog file
4655
 
*/
4656
 
extern "C"
4657
 
const char* mysql_bin_log_file_name(void)
4658
 
{
4659
 
  return mysql_bin_log.get_log_fname();
4660
 
}
4661
 
/**
4662
 
  Get the current position of the MySQL binlog.
4663
 
  @return byte offset from the beginning of the binlog
4664
 
*/
4665
 
extern "C"
4666
 
uint64_t mysql_bin_log_file_pos(void)
4667
 
{
4668
 
  return (uint64_t) mysql_bin_log.get_log_file()->pos_in_file;
4669
 
}
4670
 
#endif /* INNODB_COMPATIBILITY_HOOKS */
4671
 
 
4672
 
 
4673
 
mysql_declare_plugin(binlog)
4674
 
{
4675
 
  DRIZZLE_STORAGE_ENGINE_PLUGIN,
4676
 
  "binlog",
4677
 
  "1.0",
4678
 
  "MySQL AB",
4679
 
  "This is a pseudo storage engine to represent the binlog in a transaction",
4680
 
  PLUGIN_LICENSE_GPL,
4681
 
  binlog_init, /* Plugin Init */
4682
 
  NULL, /* Plugin Deinit */
4683
 
  NULL,                       /* status variables                */
4684
 
  NULL,                       /* system variables                */
4685
 
  NULL                        /* config options                  */
4686
 
}
4687
 
mysql_declare_plugin_end;