~drizzle-trunk/drizzle/development

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