~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/log.cc

  • Committer: Monty Taylor
  • Date: 2008-08-04 19:37:18 UTC
  • mto: (261.2.2 codestyle)
  • mto: This revision was merged to the branch mainline in revision 262.
  • Revision ID: monty@inaugust.com-20080804193718-f0rz13uli4429ozb
Changed gettext_noop() to N_()

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
    Abort logging when we get an error in reading or writing log files
25
25
*/
26
26
 
27
 
#include <drizzled/server_includes.h>
28
 
#include <drizzled/replication/replication.h>
29
 
#include <drizzled/replication/filter.h>
30
 
#include <drizzled/replication/rli.h>
 
27
#include "mysql_priv.h"
 
28
#include "sql_repl.h"
 
29
#include "rpl_filter.h"
 
30
#include "rpl_rli.h"
31
31
 
32
32
#include <mysys/my_dir.h>
33
33
#include <stdarg.h>
34
34
 
35
35
#include <drizzled/plugin.h>
36
 
#include <drizzled/error.h>
37
 
#include <drizzled/gettext.h>
38
 
#include <drizzled/data_home.h>
39
 
#include <drizzled/log_event.h>
40
 
 
41
 
#include <drizzled/errmsg.h>
 
36
#include <drizzled/drizzled_error_messages.h>
42
37
 
43
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
44
42
#define MY_OFF_T_UNDEF (~(my_off_t)0UL)
45
43
 
 
44
#define FLAGSTR(V,F) ((V)&(F)?#F" ":"")
 
45
 
46
46
LOGGER logger;
47
47
 
48
 
DRIZZLE_BIN_LOG drizzle_bin_log;
 
48
MYSQL_BIN_LOG mysql_bin_log;
49
49
ulong sync_binlog_counter= 0;
50
50
 
51
51
static bool test_if_number(const char *str,
52
 
                           long *res, bool allow_wildcards);
 
52
                           long *res, bool allow_wildcards);
53
53
static int binlog_init(void *p);
54
 
static int binlog_close_connection(handlerton *hton, Session *session);
55
 
static int binlog_savepoint_set(handlerton *hton, Session *session, void *sv);
56
 
static int binlog_savepoint_rollback(handlerton *hton, Session *session, void *sv);
57
 
static int binlog_commit(handlerton *hton, Session *session, bool all);
58
 
static int binlog_rollback(handlerton *hton, Session *session, bool all);
59
 
static int binlog_prepare(handlerton *hton, Session *session, bool all);
 
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
60
 
61
61
 
62
62
sql_print_message_func sql_print_message_handlers[3] =
70
70
char *make_default_log_name(char *buff,const char* log_ext)
71
71
{
72
72
  strmake(buff, pidfile_name, FN_REFLEN-5);
73
 
  return fn_format(buff, buff, drizzle_data_home, log_ext,
 
73
  return fn_format(buff, buff, mysql_data_home, log_ext,
74
74
                   MYF(MY_UNPACK_FILENAME|MY_REPLACE_EXT));
75
75
}
76
76
 
202
202
handlerton *binlog_hton;
203
203
 
204
204
 
 
205
/* Check if a given table is opened log table */
 
206
int check_if_log_table(uint db_len __attribute__((unused)),
 
207
                       const char *db __attribute__((unused)),
 
208
                       uint table_name_len __attribute__((unused)),
 
209
                       const char *table_name __attribute__((unused)),
 
210
                       uint check_if_opened __attribute__((unused)))
 
211
{
 
212
  return 0;
 
213
}
 
214
 
 
215
/* log event handlers */
 
216
 
 
217
bool Log_to_file_event_handler::
 
218
  log_error(enum loglevel level, const char *format,
 
219
            va_list args)
 
220
{
 
221
  return vprint_msg_to_log(level, format, args);
 
222
}
 
223
 
 
224
void Log_to_file_event_handler::init_pthread_objects()
 
225
{
 
226
  mysql_log.init_pthread_objects();
 
227
  mysql_slow_log.init_pthread_objects();
 
228
}
 
229
 
 
230
 
 
231
/** Wrapper around MYSQL_LOG::write() for slow log. */
 
232
 
 
233
bool Log_to_file_event_handler::
 
234
  log_slow(THD *thd, time_t current_time, time_t query_start_arg,
 
235
           const char *user_host, uint user_host_len,
 
236
           uint64_t query_utime, uint64_t lock_utime, bool is_command,
 
237
           const char *sql_text, uint sql_text_len)
 
238
{
 
239
  return mysql_slow_log.write(thd, current_time, query_start_arg,
 
240
                              user_host, user_host_len,
 
241
                              query_utime, lock_utime, is_command,
 
242
                              sql_text, sql_text_len);
 
243
}
 
244
 
 
245
 
 
246
/**
 
247
   Wrapper around MYSQL_LOG::write() for general log. We need it since we
 
248
   want all log event handlers to have the same signature.
 
249
*/
 
250
 
 
251
bool Log_to_file_event_handler::
 
252
  log_general(THD *thd __attribute__((unused)),
 
253
              time_t event_time, const char *user_host,
 
254
              uint user_host_len, int thread_id,
 
255
              const char *command_type, uint command_type_len,
 
256
              const char *sql_text, uint sql_text_len,
 
257
              CHARSET_INFO *client_cs __attribute__((unused)))
 
258
{
 
259
  return mysql_log.write(event_time, user_host, user_host_len,
 
260
                         thread_id, command_type, command_type_len,
 
261
                         sql_text, sql_text_len);
 
262
}
 
263
 
 
264
 
 
265
bool Log_to_file_event_handler::init()
 
266
{
 
267
  if (!is_initialized)
 
268
  {
 
269
    if (opt_slow_log)
 
270
      mysql_slow_log.open_slow_log(sys_var_slow_log_path.value);
 
271
 
 
272
    if (opt_log)
 
273
      mysql_log.open_query_log(sys_var_general_log_path.value);
 
274
 
 
275
    is_initialized= true;
 
276
  }
 
277
 
 
278
  return false;
 
279
}
 
280
 
 
281
 
 
282
void Log_to_file_event_handler::cleanup()
 
283
{
 
284
  mysql_log.cleanup();
 
285
  mysql_slow_log.cleanup();
 
286
}
 
287
 
 
288
void Log_to_file_event_handler::flush()
 
289
{
 
290
  /* reopen log files */
 
291
  if (opt_log)
 
292
    mysql_log.reopen_file();
 
293
  if (opt_slow_log)
 
294
    mysql_slow_log.reopen_file();
 
295
}
 
296
 
205
297
/*
206
298
  Log error with all enabled log event handlers
207
299
 
236
328
{
237
329
  assert(inited == 1);
238
330
  rwlock_destroy(&LOCK_logger);
 
331
  if (file_log_handler)
 
332
    file_log_handler->cleanup();
239
333
}
240
334
 
241
335
 
242
336
void LOGGER::cleanup_end()
243
337
{
244
338
  assert(inited == 1);
 
339
  if (file_log_handler)
 
340
    delete file_log_handler;
245
341
}
246
342
 
247
343
 
254
350
  assert(inited == 0);
255
351
  inited= 1;
256
352
 
 
353
  /*
 
354
    Here we create file log handler. We don't do it for the table log handler
 
355
    here as it cannot be created so early. The reason is THD initialization,
 
356
    which depends on the system variables (parsed later).
 
357
  */
 
358
  if (!file_log_handler)
 
359
    file_log_handler= new Log_to_file_event_handler;
 
360
 
257
361
  /* by default we use traditional error log */
258
362
  init_error_log(LOG_FILE);
259
363
 
 
364
  file_log_handler->init_pthread_objects();
260
365
  my_rwlock_init(&LOCK_logger, NULL);
261
366
}
262
367
 
263
368
 
264
 
bool LOGGER::flush_logs(Session *)
 
369
bool LOGGER::flush_logs(THD *thd __attribute__((unused)))
265
370
{
266
371
  int rc= 0;
267
372
 
271
376
  */
272
377
  logger.lock_exclusive();
273
378
 
 
379
  /* reopen log files */
 
380
  file_log_handler->flush();
 
381
 
274
382
  /* end of log flush */
275
383
  logger.unlock();
276
384
  return rc;
277
385
}
278
386
 
279
 
void LOGGER::init_error_log(uint32_t error_log_printer)
 
387
 
 
388
/*
 
389
  Log slow query with all enabled log event handlers
 
390
 
 
391
  SYNOPSIS
 
392
    slow_log_print()
 
393
 
 
394
    thd                 THD of the query being logged
 
395
    query               The query being logged
 
396
    query_length        The length of the query string
 
397
    current_utime       Current time in microseconds (from undefined start)
 
398
 
 
399
  RETURN
 
400
    FALSE   OK
 
401
    TRUE    error occured
 
402
*/
 
403
 
 
404
bool LOGGER::slow_log_print(THD *thd, const char *query, uint query_length,
 
405
                            uint64_t current_utime)
 
406
 
 
407
{
 
408
  bool error= false;
 
409
  Log_event_handler **current_handler;
 
410
  bool is_command= false;
 
411
  char user_host_buff[MAX_USER_HOST_SIZE];
 
412
  Security_context *sctx= thd->security_ctx;
 
413
  uint user_host_len= 0;
 
414
  uint64_t query_utime, lock_utime;
 
415
 
 
416
  /*
 
417
    Print the message to the buffer if we have slow log enabled
 
418
  */
 
419
 
 
420
  if (*slow_log_handler_list)
 
421
  {
 
422
    time_t current_time;
 
423
 
 
424
    /* do not log slow queries from replication threads */
 
425
    if (thd->slave_thread && !opt_log_slow_slave_statements)
 
426
      return 0;
 
427
 
 
428
    lock_shared();
 
429
    if (!opt_slow_log)
 
430
    {
 
431
      unlock();
 
432
      return 0;
 
433
    }
 
434
 
 
435
    /* fill in user_host value: the format is "%s[%s] @ %s [%s]" */
 
436
    user_host_len= (strxnmov(user_host_buff, MAX_USER_HOST_SIZE,
 
437
                             sctx->priv_user ? sctx->priv_user : "", "[",
 
438
                             sctx->user ? sctx->user : "", "] @ ",
 
439
                             sctx->host ? sctx->host : "", " [",
 
440
                             sctx->ip ? sctx->ip : "", "]", NullS) -
 
441
                    user_host_buff);
 
442
 
 
443
    current_time= my_time_possible_from_micro(current_utime);
 
444
    if (thd->start_utime)
 
445
    {
 
446
      query_utime= (current_utime - thd->start_utime);
 
447
      lock_utime=  (thd->utime_after_lock - thd->start_utime);
 
448
    }
 
449
    else
 
450
    {
 
451
      query_utime= lock_utime= 0;
 
452
    }
 
453
 
 
454
    if (!query)
 
455
    {
 
456
      is_command= true;
 
457
      query= command_name[thd->command].str;
 
458
      query_length= command_name[thd->command].length;
 
459
    }
 
460
 
 
461
    for (current_handler= slow_log_handler_list; *current_handler ;)
 
462
      error= (*current_handler++)->log_slow(thd, current_time, thd->start_time,
 
463
                                            user_host_buff, user_host_len,
 
464
                                            query_utime, lock_utime, is_command,
 
465
                                            query, query_length) || error;
 
466
 
 
467
    unlock();
 
468
  }
 
469
  return error;
 
470
}
 
471
 
 
472
bool LOGGER::general_log_write(THD *thd, enum enum_server_command command,
 
473
                               const char *query, uint query_length)
 
474
{
 
475
  bool error= false;
 
476
  Log_event_handler **current_handler= general_log_handler_list;
 
477
  char user_host_buff[MAX_USER_HOST_SIZE];
 
478
  Security_context *sctx= thd->security_ctx;
 
479
  ulong id;
 
480
  uint user_host_len= 0;
 
481
  time_t current_time;
 
482
 
 
483
  if (thd)
 
484
    id= thd->thread_id;                 /* Normal thread */
 
485
  else
 
486
    id= 0;                              /* Log from connect handler */
 
487
 
 
488
  lock_shared();
 
489
  if (!opt_log)
 
490
  {
 
491
    unlock();
 
492
    return 0;
 
493
  }
 
494
  user_host_len= strxnmov(user_host_buff, MAX_USER_HOST_SIZE,
 
495
                          sctx->priv_user ? sctx->priv_user : "", "[",
 
496
                          sctx->user ? sctx->user : "", "] @ ",
 
497
                          sctx->host ? sctx->host : "", " [",
 
498
                          sctx->ip ? sctx->ip : "", "]", NullS) -
 
499
                                                          user_host_buff;
 
500
 
 
501
  current_time= my_time(0);
 
502
 
 
503
  while (*current_handler)
 
504
    error|= (*current_handler++)->
 
505
      log_general(thd, current_time, user_host_buff,
 
506
                  user_host_len, id,
 
507
                  command_name[(uint) command].str,
 
508
                  command_name[(uint) command].length,
 
509
                  query, query_length,
 
510
                  thd->variables.character_set_client) || error;
 
511
  unlock();
 
512
 
 
513
  return error;
 
514
}
 
515
 
 
516
bool LOGGER::general_log_print(THD *thd, enum enum_server_command command,
 
517
                               const char *format, va_list args)
 
518
{
 
519
  uint message_buff_len= 0;
 
520
  char message_buff[MAX_LOG_BUFFER_SIZE];
 
521
 
 
522
  /* prepare message */
 
523
  if (format)
 
524
    message_buff_len= vsnprintf(message_buff, sizeof(message_buff),
 
525
                                   format, args);
 
526
  else
 
527
    message_buff[0]= '\0';
 
528
 
 
529
  return general_log_write(thd, command, message_buff, message_buff_len);
 
530
}
 
531
 
 
532
void LOGGER::init_error_log(uint error_log_printer)
280
533
{
281
534
  if (error_log_printer & LOG_NONE)
282
535
  {
284
537
    return;
285
538
  }
286
539
 
287
 
}
288
 
 
289
 
int LOGGER::set_handlers(uint32_t error_log_printer)
 
540
  switch (error_log_printer) {
 
541
  case LOG_FILE:
 
542
    error_log_handler_list[0]= file_log_handler;
 
543
    error_log_handler_list[1]= 0;
 
544
    break;
 
545
  }
 
546
}
 
547
 
 
548
void LOGGER::init_slow_log(uint slow_log_printer)
 
549
{
 
550
  if (slow_log_printer & LOG_NONE)
 
551
  {
 
552
    slow_log_handler_list[0]= 0;
 
553
    return;
 
554
  }
 
555
 
 
556
  slow_log_handler_list[0]= file_log_handler;
 
557
  slow_log_handler_list[1]= 0;
 
558
}
 
559
 
 
560
void LOGGER::init_general_log(uint general_log_printer)
 
561
{
 
562
  if (general_log_printer & LOG_NONE)
 
563
  {
 
564
    general_log_handler_list[0]= 0;
 
565
    return;
 
566
  }
 
567
 
 
568
  general_log_handler_list[0]= file_log_handler;
 
569
  general_log_handler_list[1]= 0;
 
570
}
 
571
 
 
572
 
 
573
bool LOGGER::activate_log_handler(THD* thd __attribute__((unused)),
 
574
                                  uint log_type)
 
575
{
 
576
  MYSQL_QUERY_LOG *file_log;
 
577
  bool res= false;
 
578
  lock_exclusive();
 
579
  switch (log_type) {
 
580
  case QUERY_LOG_SLOW:
 
581
    if (!opt_slow_log)
 
582
    {
 
583
      file_log= file_log_handler->get_mysql_slow_log();
 
584
 
 
585
      file_log->open_slow_log(sys_var_slow_log_path.value);
 
586
      init_slow_log(log_output_options);
 
587
      opt_slow_log= true;
 
588
    }
 
589
    break;
 
590
  case QUERY_LOG_GENERAL:
 
591
    if (!opt_log)
 
592
    {
 
593
      file_log= file_log_handler->get_mysql_log();
 
594
 
 
595
      file_log->open_query_log(sys_var_general_log_path.value);
 
596
      init_general_log(log_output_options);
 
597
      opt_log= true;
 
598
    }
 
599
    break;
 
600
  default:
 
601
    assert(0);
 
602
  }
 
603
  unlock();
 
604
  return res;
 
605
}
 
606
 
 
607
 
 
608
void LOGGER::deactivate_log_handler(THD *thd __attribute__((unused)),
 
609
                                    uint log_type)
 
610
{
 
611
  bool *tmp_opt= 0;
 
612
  MYSQL_LOG *file_log;
 
613
 
 
614
  switch (log_type) {
 
615
  case QUERY_LOG_SLOW:
 
616
    tmp_opt= &opt_slow_log;
 
617
    file_log= file_log_handler->get_mysql_slow_log();
 
618
    break;
 
619
  case QUERY_LOG_GENERAL:
 
620
    tmp_opt= &opt_log;
 
621
    file_log= file_log_handler->get_mysql_log();
 
622
    break;
 
623
  default:
 
624
    assert(0);                                  // Impossible
 
625
  }
 
626
 
 
627
  if (!(*tmp_opt))
 
628
    return;
 
629
 
 
630
  lock_exclusive();
 
631
  file_log->close(0);
 
632
  *tmp_opt= false;
 
633
  unlock();
 
634
}
 
635
 
 
636
int LOGGER::set_handlers(uint error_log_printer,
 
637
                         uint slow_log_printer,
 
638
                         uint general_log_printer)
290
639
{
291
640
  /* error log table is not supported yet */
292
641
  lock_exclusive();
293
642
 
294
643
  init_error_log(error_log_printer);
 
644
  init_slow_log(slow_log_printer);
 
645
  init_general_log(general_log_printer);
 
646
 
295
647
  unlock();
296
648
 
297
649
  return 0;
304
656
  SYNPOSIS
305
657
    binlog_trans_log_savepos()
306
658
 
307
 
    session      The thread to take the binlog data from
 
659
    thd      The thread to take the binlog data from
308
660
    pos      Pointer to variable where the position will be stored
309
661
 
310
662
  DESCRIPTION
314
666
 */
315
667
 
316
668
static void
317
 
binlog_trans_log_savepos(Session *session, my_off_t *pos)
 
669
binlog_trans_log_savepos(THD *thd, my_off_t *pos)
318
670
{
319
671
  assert(pos != NULL);
320
 
  if (session_get_ha_data(session, binlog_hton) == NULL)
321
 
    session->binlog_setup_trx_data();
 
672
  if (thd_get_ha_data(thd, binlog_hton) == NULL)
 
673
    thd->binlog_setup_trx_data();
322
674
  binlog_trx_data *const trx_data=
323
 
    (binlog_trx_data*) session_get_ha_data(session, binlog_hton);
324
 
  assert(drizzle_bin_log.is_open());
 
675
    (binlog_trx_data*) thd_get_ha_data(thd, binlog_hton);
 
676
  assert(mysql_bin_log.is_open());
325
677
  *pos= trx_data->position();
326
678
  return;
327
679
}
333
685
  SYNPOSIS
334
686
    binlog_trans_log_truncate()
335
687
 
336
 
    session      The thread to take the binlog data from
 
688
    thd      The thread to take the binlog data from
337
689
    pos      Position to truncate to
338
690
 
339
691
  DESCRIPTION
343
695
 
344
696
 */
345
697
static void
346
 
binlog_trans_log_truncate(Session *session, my_off_t pos)
 
698
binlog_trans_log_truncate(THD *thd, my_off_t pos)
347
699
{
348
 
  assert(session_get_ha_data(session, binlog_hton) != NULL);
 
700
  assert(thd_get_ha_data(thd, binlog_hton) != NULL);
349
701
  /* Only true if binlog_trans_log_savepos() wasn't called before */
350
702
  assert(pos != ~(my_off_t) 0);
351
703
 
352
704
  binlog_trx_data *const trx_data=
353
 
    (binlog_trx_data*) session_get_ha_data(session, binlog_hton);
 
705
    (binlog_trx_data*) thd_get_ha_data(thd, binlog_hton);
354
706
  trx_data->truncate(pos);
355
707
  return;
356
708
}
358
710
 
359
711
/*
360
712
  this function is mostly a placeholder.
361
 
  conceptually, binlog initialization (now mostly done in DRIZZLE_BIN_LOG::open)
 
713
  conceptually, binlog initialization (now mostly done in MYSQL_BIN_LOG::open)
362
714
  should be moved here.
363
715
*/
364
716
 
366
718
{
367
719
  binlog_hton= (handlerton *)p;
368
720
  binlog_hton->state=opt_bin_log ? SHOW_OPTION_YES : SHOW_OPTION_NO;
 
721
  binlog_hton->db_type=DB_TYPE_BINLOG;
369
722
  binlog_hton->savepoint_offset= sizeof(my_off_t);
370
723
  binlog_hton->close_connection= binlog_close_connection;
371
724
  binlog_hton->savepoint_set= binlog_savepoint_set;
378
731
  return 0;
379
732
}
380
733
 
381
 
static int binlog_close_connection(handlerton *, Session *session)
 
734
static int binlog_close_connection(handlerton *hton __attribute__((unused)),
 
735
                                   THD *thd)
382
736
{
383
737
  binlog_trx_data *const trx_data=
384
 
    (binlog_trx_data*) session_get_ha_data(session, binlog_hton);
 
738
    (binlog_trx_data*) thd_get_ha_data(thd, binlog_hton);
385
739
  assert(trx_data->empty());
386
 
  session_set_ha_data(session, binlog_hton, NULL);
 
740
  thd_set_ha_data(thd, binlog_hton, NULL);
387
741
  trx_data->~binlog_trx_data();
388
 
  free((unsigned char*)trx_data);
 
742
  my_free((uchar*)trx_data, MYF(0));
389
743
  return 0;
390
744
}
391
745
 
395
749
  SYNOPSIS
396
750
    binlog_end_trans()
397
751
 
398
 
    session      The thread whose transaction should be ended
 
752
    thd      The thread whose transaction should be ended
399
753
    trx_data Pointer to the transaction data to use
400
754
    end_ev   The end event to use, or NULL
401
755
    all      True if the entire transaction should be ended, false if
413
767
    'all' is false), or reset completely (if 'all' is true).
414
768
 */
415
769
static int
416
 
binlog_end_trans(Session *session, binlog_trx_data *trx_data,
 
770
binlog_end_trans(THD *thd, binlog_trx_data *trx_data,
417
771
                 Log_event *end_ev, bool all)
418
772
{
419
773
  int error=0;
437
791
      were, we would have to ensure that we're not ending a statement
438
792
      inside a stored function.
439
793
     */
440
 
    session->binlog_flush_pending_rows_event(true);
 
794
    thd->binlog_flush_pending_rows_event(true);
441
795
 
442
 
    error= drizzle_bin_log.write(session, &trx_data->trans_log, end_ev);
 
796
    error= mysql_bin_log.write(thd, &trx_data->trans_log, end_ev);
443
797
    trx_data->reset();
444
798
 
445
799
    /*
446
800
      We need to step the table map version after writing the
447
801
      transaction cache to disk.
448
802
    */
449
 
    drizzle_bin_log.update_table_map_version();
 
803
    mysql_bin_log.update_table_map_version();
450
804
    statistic_increment(binlog_cache_use, &LOCK_status);
451
805
    if (trans_log->disk_writes != 0)
452
806
    {
463
817
      If rolling back a statement in a transaction, we truncate the
464
818
      transaction cache to remove the statement.
465
819
     */
466
 
    if (all || !(session->options & (OPTION_BEGIN | OPTION_NOT_AUTOCOMMIT)))
 
820
    if (all || !(thd->options & (OPTION_BEGIN | OPTION_NOT_AUTOCOMMIT)))
467
821
    {
468
822
      trx_data->reset();
469
823
 
470
 
      assert(!session->binlog_get_pending_rows_event());
471
 
      session->clear_binlog_table_maps();
 
824
      assert(!thd->binlog_get_pending_rows_event());
 
825
      thd->clear_binlog_table_maps();
472
826
    }
473
827
    else                                        // ...statement
474
828
      trx_data->truncate(trx_data->before_stmt_pos);
478
832
      that a new table map event is generated instead of the one that
479
833
      was written to the thrown-away transaction cache.
480
834
    */
481
 
    drizzle_bin_log.update_table_map_version();
 
835
    mysql_bin_log.update_table_map_version();
482
836
  }
483
837
 
484
838
  return(error);
485
839
}
486
840
 
487
 
static int binlog_prepare(handlerton *, Session *, bool)
 
841
static int binlog_prepare(handlerton *hton __attribute__((unused)),
 
842
                          THD *thd __attribute__((unused)),
 
843
                          bool all __attribute__((unused)))
488
844
{
489
845
  /*
490
846
    do nothing.
491
847
    just pretend we can do 2pc, so that MySQL won't
492
848
    switch to 1pc.
493
 
    real work will be done in DRIZZLE_BIN_LOG::log_xid()
 
849
    real work will be done in MYSQL_BIN_LOG::log_xid()
494
850
  */
495
851
  return 0;
496
852
}
497
853
 
 
854
#define YESNO(X) ((X) ? "yes" : "no")
 
855
 
498
856
/**
499
857
  This function is called once after each statement.
500
858
 
502
860
  binlog file on commits.
503
861
 
504
862
  @param hton  The binlog handlerton.
505
 
  @param session   The client thread that executes the transaction.
 
863
  @param thd   The client thread that executes the transaction.
506
864
  @param all   This is @c true if this is a real transaction commit, and
507
865
               @false otherwise.
508
866
 
509
867
  @see handlerton::commit
510
868
*/
511
 
static int binlog_commit(handlerton *, Session *session, bool all)
 
869
static int binlog_commit(handlerton *hton __attribute__((unused)),
 
870
                         THD *thd, bool all)
512
871
{
513
872
  binlog_trx_data *const trx_data=
514
 
    (binlog_trx_data*) session_get_ha_data(session, binlog_hton);
 
873
    (binlog_trx_data*) thd_get_ha_data(thd, binlog_hton);
515
874
 
516
875
  if (trx_data->empty())
517
876
  {
518
 
    // we're here because trans_log was flushed in DRIZZLE_BIN_LOG::log_xid()
 
877
    // we're here because trans_log was flushed in MYSQL_BIN_LOG::log_xid()
519
878
    trx_data->reset();
520
879
    return(0);
521
880
  }
579
938
    Otherwise, we accumulate the statement
580
939
  */
581
940
  uint64_t const in_transaction=
582
 
    session->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN);
583
 
  if ((in_transaction && (all || (!trx_data->at_least_one_stmt && session->transaction.stmt.modified_non_trans_table))) || (!in_transaction && !all))
 
941
    thd->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN);
 
942
  if ((in_transaction && (all || (!trx_data->at_least_one_stmt && thd->transaction.stmt.modified_non_trans_table))) || (!in_transaction && !all))
584
943
  {
585
 
    Query_log_event qev(session, STRING_WITH_LEN("COMMIT"), true, false);
586
 
    qev.error_code= 0; // see comment in DRIZZLE_LOG::write(Session, IO_CACHE)
587
 
    int error= binlog_end_trans(session, trx_data, &qev, all);
 
944
    Query_log_event qev(thd, STRING_WITH_LEN("COMMIT"), true, false);
 
945
    qev.error_code= 0; // see comment in MYSQL_LOG::write(THD, IO_CACHE)
 
946
    int error= binlog_end_trans(thd, trx_data, &qev, all);
588
947
    return(error);
589
948
  }
590
949
  return(0);
599
958
  non-transactional tables, nothing needs to be logged.
600
959
 
601
960
  @param hton  The binlog handlerton.
602
 
  @param session   The client thread that executes the transaction.
 
961
  @param thd   The client thread that executes the transaction.
603
962
  @param all   This is @c true if this is a real transaction rollback, and
604
963
               @false otherwise.
605
964
 
606
965
  @see handlerton::rollback
607
966
*/
608
 
static int binlog_rollback(handlerton *, Session *session, bool all)
 
967
static int binlog_rollback(handlerton *hton __attribute__((unused)),
 
968
                           THD *thd, bool all)
609
969
{
610
970
  int error=0;
611
971
  binlog_trx_data *const trx_data=
612
 
    (binlog_trx_data*) session_get_ha_data(session, binlog_hton);
 
972
    (binlog_trx_data*) thd_get_ha_data(thd, binlog_hton);
613
973
 
614
974
  if (trx_data->empty()) {
615
975
    trx_data->reset();
616
976
    return(0);
617
977
  }
618
978
 
619
 
  if ((all && session->transaction.all.modified_non_trans_table) ||
620
 
      (!all && session->transaction.stmt.modified_non_trans_table) ||
621
 
      (session->options & OPTION_KEEP_LOG))
 
979
  if ((all && thd->transaction.all.modified_non_trans_table) ||
 
980
      (!all && thd->transaction.stmt.modified_non_trans_table) ||
 
981
      (thd->options & OPTION_KEEP_LOG))
622
982
  {
623
983
    /*
624
984
      We write the transaction cache with a rollback last if we have
628
988
      transactional table in that statement as well, which needs to be
629
989
      rolled back on the slave.
630
990
    */
631
 
    Query_log_event qev(session, STRING_WITH_LEN("ROLLBACK"), true, false);
632
 
    qev.error_code= 0; // see comment in DRIZZLE_LOG::write(Session, IO_CACHE)
633
 
    error= binlog_end_trans(session, trx_data, &qev, all);
 
991
    Query_log_event qev(thd, STRING_WITH_LEN("ROLLBACK"), true, false);
 
992
    qev.error_code= 0; // see comment in MYSQL_LOG::write(THD, IO_CACHE)
 
993
    error= binlog_end_trans(thd, trx_data, &qev, all);
634
994
  }
635
 
  else if ((all && !session->transaction.all.modified_non_trans_table) ||
636
 
           (!all && !session->transaction.stmt.modified_non_trans_table))
 
995
  else if ((all && !thd->transaction.all.modified_non_trans_table) ||
 
996
           (!all && !thd->transaction.stmt.modified_non_trans_table))
637
997
  {
638
998
    /*
639
999
      If we have modified only transactional tables, we can truncate
640
1000
      the transaction cache without writing anything to the binary
641
1001
      log.
642
1002
     */
643
 
    error= binlog_end_trans(session, trx_data, 0, all);
 
1003
    error= binlog_end_trans(thd, trx_data, 0, all);
644
1004
  }
645
1005
  return(error);
646
1006
}
669
1029
  that case there is no need to have it in the binlog).
670
1030
*/
671
1031
 
672
 
static int binlog_savepoint_set(handlerton *, Session *session, void *sv)
 
1032
static int binlog_savepoint_set(handlerton *hton __attribute__((unused)),
 
1033
                                THD *thd, void *sv)
673
1034
{
674
 
  binlog_trans_log_savepos(session, (my_off_t*) sv);
 
1035
  binlog_trans_log_savepos(thd, (my_off_t*) sv);
675
1036
  /* Write it to the binary log */
676
 
 
 
1037
  
677
1038
  int const error=
678
 
    session->binlog_query(Session::STMT_QUERY_TYPE,
679
 
                      session->query, session->query_length, true, false);
 
1039
    thd->binlog_query(THD::STMT_QUERY_TYPE,
 
1040
                      thd->query, thd->query_length, true, false);
680
1041
  return(error);
681
1042
}
682
1043
 
683
 
static int binlog_savepoint_rollback(handlerton *, Session *session, void *sv)
 
1044
static int binlog_savepoint_rollback(handlerton *hton __attribute__((unused)),
 
1045
                                     THD *thd, void *sv)
684
1046
{
685
1047
  /*
686
1048
    Write ROLLBACK TO SAVEPOINT to the binlog cache if we have updated some
687
1049
    non-transactional table. Otherwise, truncate the binlog cache starting
688
1050
    from the SAVEPOINT command.
689
1051
  */
690
 
  if (unlikely(session->transaction.all.modified_non_trans_table || 
691
 
               (session->options & OPTION_KEEP_LOG)))
 
1052
  if (unlikely(thd->transaction.all.modified_non_trans_table || 
 
1053
               (thd->options & OPTION_KEEP_LOG)))
692
1054
  {
693
1055
    int error=
694
 
      session->binlog_query(Session::STMT_QUERY_TYPE,
695
 
                        session->query, session->query_length, true, false);
 
1056
      thd->binlog_query(THD::STMT_QUERY_TYPE,
 
1057
                        thd->query, thd->query_length, true, false);
696
1058
    return(error);
697
1059
  }
698
 
  binlog_trans_log_truncate(session, *(my_off_t*)sv);
 
1060
  binlog_trans_log_truncate(thd, *(my_off_t*)sv);
699
1061
  return(0);
700
1062
}
701
1063
 
705
1067
  char magic[4];
706
1068
  assert(my_b_tell(log) == 0);
707
1069
 
708
 
  if (my_b_read(log, (unsigned char*) magic, sizeof(magic)))
 
1070
  if (my_b_read(log, (uchar*) magic, sizeof(magic)))
709
1071
  {
710
 
    *errmsg = _("I/O error reading the header from the binary log");
 
1072
    *errmsg = "I/O error reading the header from the binary log";
711
1073
    sql_print_error("%s, errno=%d, io cache code=%d", *errmsg, my_errno,
712
1074
                    log->error);
713
1075
    return 1;
714
1076
  }
715
1077
  if (memcmp(magic, BINLOG_MAGIC, sizeof(magic)))
716
1078
  {
717
 
    *errmsg = _("Binlog has bad magic number;  It's not a binary log file "
718
 
                "that can be used by this version of Drizzle");
 
1079
    *errmsg = "Binlog has bad magic number;  It's not a binary log file that can be used by this version of MySQL";
719
1080
    return 1;
720
1081
  }
721
1082
  return 0;
726
1087
{
727
1088
  File file;
728
1089
 
729
 
  if ((file = my_open(log_file_name, O_RDONLY, 
 
1090
  if ((file = my_open(log_file_name, O_RDONLY | O_BINARY | O_SHARE, 
730
1091
                      MYF(MY_WME))) < 0)
731
1092
  {
732
 
    sql_print_error(_("Failed to open log (file '%s', errno %d)"),
 
1093
    sql_print_error("Failed to open log (file '%s', errno %d)",
733
1094
                    log_file_name, my_errno);
734
 
    *errmsg = _("Could not open log file");
 
1095
    *errmsg = "Could not open log file";
735
1096
    goto err;
736
1097
  }
737
1098
  if (init_io_cache(log, file, IO_SIZE*2, READ_CACHE, 0, 0,
738
1099
                    MYF(MY_WME|MY_DONT_CHECK_FILESIZE)))
739
1100
  {
740
 
    sql_print_error(_("Failed to create a cache on log (file '%s')"),
 
1101
    sql_print_error("Failed to create a cache on log (file '%s')",
741
1102
                    log_file_name);
742
 
    *errmsg = _("Could not open log file");
 
1103
    *errmsg = "Could not open log file";
743
1104
    goto err;
744
1105
  }
745
1106
  if (check_binlog_magic(log,errmsg))
768
1129
static int find_uniq_filename(char *name)
769
1130
{
770
1131
  long                  number;
771
 
  uint32_t                  i;
 
1132
  uint                  i;
772
1133
  char                  buff[FN_REFLEN];
773
1134
  struct st_my_dir     *dir_info;
774
1135
  register struct fileinfo *file_info;
778
1139
 
779
1140
  length= dirname_part(buff, name, &buf_length);
780
1141
  start=  name + length;
781
 
  end= strchr(start, '\0');
 
1142
  end=    strend(start);
782
1143
 
783
1144
  *end='.';
784
1145
  length= (size_t) (end-start+1);
785
1146
 
786
1147
  if (!(dir_info = my_dir(buff,MYF(MY_DONT_SORT))))
787
1148
  {                                             // This shouldn't happen
788
 
    my_stpcpy(end,".1");                                // use name+1
 
1149
    strmov(end,".1");                           // use name+1
789
1150
    return(0);
790
1151
  }
791
1152
  file_info= dir_info->dir_entry;
792
1153
  for (i=dir_info->number_off_files ; i-- ; file_info++)
793
1154
  {
794
 
    if (memcmp(file_info->name, start, length) == 0 &&
 
1155
    if (memcmp((uchar*) file_info->name, (uchar*) start, length) == 0 &&
795
1156
        test_if_number(file_info->name+length, &number,0))
796
1157
    {
797
1158
      set_if_bigger(max_found,(ulong) number);
805
1166
}
806
1167
 
807
1168
 
808
 
void DRIZZLE_LOG::init(enum_log_type log_type_arg,
 
1169
void MYSQL_LOG::init(enum_log_type log_type_arg,
809
1170
                     enum cache_type io_cache_type_arg)
810
1171
{
811
1172
  log_type= log_type_arg;
835
1196
    1   error
836
1197
*/
837
1198
 
838
 
bool DRIZZLE_LOG::open(const char *log_name, enum_log_type log_type_arg,
 
1199
bool MYSQL_LOG::open(const char *log_name, enum_log_type log_type_arg,
839
1200
                     const char *new_name, enum cache_type io_cache_type_arg)
840
1201
{
841
1202
  char buff[FN_REFLEN];
842
1203
  File file= -1;
843
 
  int open_flags= O_CREAT;
 
1204
  int open_flags= O_CREAT | O_BINARY;
844
1205
 
845
1206
  write_error= 0;
846
1207
 
853
1214
  }
854
1215
 
855
1216
  if (new_name)
856
 
    my_stpcpy(log_file_name, new_name);
 
1217
    strmov(log_file_name, new_name);
857
1218
  else if (generate_new_name(log_file_name, name))
858
1219
    goto err;
859
1220
 
877
1238
    char *end;
878
1239
    int len=snprintf(buff, sizeof(buff), "%s, Version: %s (%s). "
879
1240
                     "started with:\nTCP Port: %d, Named Pipe: %s\n",
880
 
                     my_progname, server_version, COMPILATION_COMMENT,
881
 
                     drizzled_port, ""
 
1241
                     my_progname, server_version, MYSQL_COMPILATION_COMMENT,
 
1242
                     mysqld_port, ""
882
1243
                     );
883
 
    end= my_stpncpy(buff + len, "Time                 Id Command    Argument\n",
 
1244
    end= strnmov(buff + len, "Time                 Id Command    Argument\n",
884
1245
                 sizeof(buff) - len);
885
 
    if (my_b_write(&log_file, (unsigned char*) buff, (uint) (end-buff)) ||
 
1246
    if (my_b_write(&log_file, (uchar*) buff, (uint) (end-buff)) ||
886
1247
        flush_io_cache(&log_file))
887
1248
      goto err;
888
1249
  }
891
1252
  return(0);
892
1253
 
893
1254
err:
894
 
  sql_print_error(_("Could not use %s for logging (error %d). "
895
 
                    "Turning logging off for the whole duration of the "
896
 
                    "Drizzle server process. "
897
 
                    "To turn it on again: fix the cause, "
898
 
                    "shutdown the Drizzle server and restart it."),
899
 
                    name, errno);
 
1255
  sql_print_error("Could not use %s for logging (error %d). \
 
1256
Turning logging off for the whole duration of the MySQL server process. \
 
1257
To turn it on again: fix the cause, \
 
1258
shutdown the MySQL server and restart it.", name, errno);
900
1259
  if (file >= 0)
901
1260
    my_close(file, MYF(0));
902
1261
  end_io_cache(&log_file);
903
 
  if (name)
904
 
  {
905
 
    free(name);
906
 
    name= NULL;
907
 
  }
 
1262
  safeFree(name);
908
1263
  log_state= LOG_CLOSED;
909
1264
  return(1);
910
1265
}
911
1266
 
912
 
DRIZZLE_LOG::DRIZZLE_LOG()
 
1267
MYSQL_LOG::MYSQL_LOG()
913
1268
  : name(0), write_error(false), inited(false), log_type(LOG_UNKNOWN),
914
1269
    log_state(LOG_CLOSED)
915
1270
{
919
1274
    called only in main(). Doing initialization here would make it happen
920
1275
    before main().
921
1276
  */
922
 
  memset(&log_file, 0, sizeof(log_file));
 
1277
  memset((char*) &log_file, 0, sizeof(log_file));
923
1278
}
924
1279
 
925
 
void DRIZZLE_LOG::init_pthread_objects()
 
1280
void MYSQL_LOG::init_pthread_objects()
926
1281
{
927
1282
  assert(inited == 0);
928
1283
  inited= 1;
943
1298
    The internal structures are not freed until cleanup() is called
944
1299
*/
945
1300
 
946
 
void DRIZZLE_LOG::close(uint32_t exiting)
 
1301
void MYSQL_LOG::close(uint exiting)
947
1302
{                                       // One can't set log_type here!
948
1303
  if (log_state == LOG_OPENED)
949
1304
  {
963
1318
  }
964
1319
 
965
1320
  log_state= (exiting & LOG_CLOSE_TO_BE_OPENED) ? LOG_TO_BE_OPENED : LOG_CLOSED;
966
 
  if (name)
967
 
  {
968
 
    free(name);
969
 
    name= NULL;
970
 
  }
 
1321
  safeFree(name);
971
1322
  return;
972
1323
}
973
1324
 
974
1325
/** This is called only once. */
975
1326
 
976
 
void DRIZZLE_LOG::cleanup()
 
1327
void MYSQL_LOG::cleanup()
977
1328
{
978
1329
  if (inited)
979
1330
  {
985
1336
}
986
1337
 
987
1338
 
988
 
int DRIZZLE_LOG::generate_new_name(char *new_name, const char *log_name)
 
1339
int MYSQL_LOG::generate_new_name(char *new_name, const char *log_name)
989
1340
{
990
 
  fn_format(new_name, log_name, drizzle_data_home, "", 4);
 
1341
  fn_format(new_name, log_name, mysql_data_home, "", 4);
991
1342
  if (log_type == LOG_BIN)
992
1343
  {
993
1344
    if (!fn_ext(log_name)[0])
1003
1354
}
1004
1355
 
1005
1356
 
 
1357
/*
 
1358
  Reopen the log file
 
1359
 
 
1360
  SYNOPSIS
 
1361
    reopen_file()
 
1362
 
 
1363
  DESCRIPTION
 
1364
    Reopen the log file. The method is used during FLUSH LOGS
 
1365
    and locks LOCK_log mutex
 
1366
*/
 
1367
 
 
1368
 
 
1369
void MYSQL_QUERY_LOG::reopen_file()
 
1370
{
 
1371
  char *save_name;
 
1372
 
 
1373
  if (!is_open())
 
1374
  {
 
1375
    return;
 
1376
  }
 
1377
 
 
1378
  pthread_mutex_lock(&LOCK_log);
 
1379
 
 
1380
  save_name= name;
 
1381
  name= 0;                              // Don't free name
 
1382
  close(LOG_CLOSE_TO_BE_OPENED);
 
1383
 
 
1384
  /*
 
1385
     Note that at this point, log_state != LOG_CLOSED (important for is_open()).
 
1386
  */
 
1387
 
 
1388
  open(save_name, log_type, 0, io_cache_type);
 
1389
  my_free(save_name, MYF(0));
 
1390
 
 
1391
  pthread_mutex_unlock(&LOCK_log);
 
1392
 
 
1393
  return;
 
1394
}
 
1395
 
 
1396
 
 
1397
/*
 
1398
  Write a command to traditional general log file
 
1399
 
 
1400
  SYNOPSIS
 
1401
    write()
 
1402
 
 
1403
    event_time        command start timestamp
 
1404
    user_host         the pointer to the string with user@host info
 
1405
    user_host_len     length of the user_host string. this is computed once
 
1406
                      and passed to all general log  event handlers
 
1407
    thread_id         Id of the thread, issued a query
 
1408
    command_type      the type of the command being logged
 
1409
    command_type_len  the length of the string above
 
1410
    sql_text          the very text of the query being executed
 
1411
    sql_text_len      the length of sql_text string
 
1412
 
 
1413
  DESCRIPTION
 
1414
 
 
1415
   Log given command to to normal (not rotable) log file
 
1416
 
 
1417
  RETURN
 
1418
    FASE - OK
 
1419
    TRUE - error occured
 
1420
*/
 
1421
 
 
1422
bool MYSQL_QUERY_LOG::write(time_t event_time,
 
1423
                            const char *user_host __attribute__((unused)),
 
1424
                            uint user_host_len __attribute__((unused)),
 
1425
                            int thread_id,
 
1426
                            const char *command_type, uint command_type_len,
 
1427
                            const char *sql_text, uint sql_text_len)
 
1428
{
 
1429
  char buff[32];
 
1430
  uint length= 0;
 
1431
  char local_time_buff[MAX_TIME_SIZE];
 
1432
  struct tm start;
 
1433
  uint time_buff_len= 0;
 
1434
 
 
1435
  (void) pthread_mutex_lock(&LOCK_log);
 
1436
 
 
1437
  /* Test if someone closed between the is_open test and lock */
 
1438
  if (is_open())
 
1439
  {
 
1440
    /* Note that my_b_write() assumes it knows the length for this */
 
1441
      if (event_time != last_time)
 
1442
      {
 
1443
        last_time= event_time;
 
1444
 
 
1445
        localtime_r(&event_time, &start);
 
1446
 
 
1447
        time_buff_len= snprintf(local_time_buff, MAX_TIME_SIZE,
 
1448
                                "%02d%02d%02d %2d:%02d:%02d",
 
1449
                                start.tm_year % 100, start.tm_mon + 1,
 
1450
                                start.tm_mday, start.tm_hour,
 
1451
                                start.tm_min, start.tm_sec);
 
1452
 
 
1453
        if (my_b_write(&log_file, (uchar*) local_time_buff, time_buff_len))
 
1454
          goto err;
 
1455
      }
 
1456
      else
 
1457
        if (my_b_write(&log_file, (uchar*) "\t\t" ,2) < 0)
 
1458
          goto err;
 
1459
 
 
1460
      /* command_type, thread_id */
 
1461
      length= snprintf(buff, 32, "%5ld ", (long) thread_id);
 
1462
 
 
1463
    if (my_b_write(&log_file, (uchar*) buff, length))
 
1464
      goto err;
 
1465
 
 
1466
    if (my_b_write(&log_file, (uchar*) command_type, command_type_len))
 
1467
      goto err;
 
1468
 
 
1469
    if (my_b_write(&log_file, (uchar*) "\t", 1))
 
1470
      goto err;
 
1471
 
 
1472
    /* sql_text */
 
1473
    if (my_b_write(&log_file, (uchar*) sql_text, sql_text_len))
 
1474
      goto err;
 
1475
 
 
1476
    if (my_b_write(&log_file, (uchar*) "\n", 1) ||
 
1477
        flush_io_cache(&log_file))
 
1478
      goto err;
 
1479
  }
 
1480
 
 
1481
  (void) pthread_mutex_unlock(&LOCK_log);
 
1482
  return false;
 
1483
err:
 
1484
 
 
1485
  if (!write_error)
 
1486
  {
 
1487
    write_error= 1;
 
1488
    sql_print_error(ER(ER_ERROR_ON_WRITE), name, errno);
 
1489
  }
 
1490
  (void) pthread_mutex_unlock(&LOCK_log);
 
1491
  return true;
 
1492
}
 
1493
 
 
1494
 
 
1495
/*
 
1496
  Log a query to the traditional slow log file
 
1497
 
 
1498
  SYNOPSIS
 
1499
    write()
 
1500
 
 
1501
    thd               THD of the query
 
1502
    current_time      current timestamp
 
1503
    query_start_arg   command start timestamp
 
1504
    user_host         the pointer to the string with user@host info
 
1505
    user_host_len     length of the user_host string. this is computed once
 
1506
                      and passed to all general log event handlers
 
1507
    query_utime       Amount of time the query took to execute (in microseconds)
 
1508
    lock_utime        Amount of time the query was locked (in microseconds)
 
1509
    is_command        The flag, which determines, whether the sql_text is a
 
1510
                      query or an administrator command.
 
1511
    sql_text          the very text of the query or administrator command
 
1512
                      processed
 
1513
    sql_text_len      the length of sql_text string
 
1514
 
 
1515
  DESCRIPTION
 
1516
 
 
1517
   Log a query to the slow log file.
 
1518
 
 
1519
  RETURN
 
1520
    FALSE - OK
 
1521
    TRUE - error occured
 
1522
*/
 
1523
 
 
1524
bool MYSQL_QUERY_LOG::write(THD *thd, time_t current_time,
 
1525
                            time_t query_start_arg __attribute__((unused)),
 
1526
                            const char *user_host,
 
1527
                            uint user_host_len, uint64_t query_utime,
 
1528
                            uint64_t lock_utime, bool is_command,
 
1529
                            const char *sql_text, uint sql_text_len)
 
1530
{
 
1531
  bool error= 0;
 
1532
 
 
1533
  (void) pthread_mutex_lock(&LOCK_log);
 
1534
 
 
1535
  if (!is_open())
 
1536
  {
 
1537
    (void) pthread_mutex_unlock(&LOCK_log);
 
1538
    return(0);
 
1539
  }
 
1540
 
 
1541
  if (is_open())
 
1542
  {                                             // Safety agains reopen
 
1543
    int tmp_errno= 0;
 
1544
    char buff[80], *end;
 
1545
    char query_time_buff[22+7], lock_time_buff[22+7];
 
1546
    uint buff_len;
 
1547
    end= buff;
 
1548
 
 
1549
    if (!(specialflag & SPECIAL_SHORT_LOG_FORMAT))
 
1550
    {
 
1551
      if (current_time != last_time)
 
1552
      {
 
1553
        last_time= current_time;
 
1554
        struct tm start;
 
1555
        localtime_r(&current_time, &start);
 
1556
 
 
1557
        buff_len= snprintf(buff, sizeof buff,
 
1558
                           "# Time: %02d%02d%02d %2d:%02d:%02d\n",
 
1559
                           start.tm_year % 100, start.tm_mon + 1,
 
1560
                           start.tm_mday, start.tm_hour,
 
1561
                           start.tm_min, start.tm_sec);
 
1562
 
 
1563
        /* Note that my_b_write() assumes it knows the length for this */
 
1564
        if (my_b_write(&log_file, (uchar*) buff, buff_len))
 
1565
          tmp_errno= errno;
 
1566
      }
 
1567
      const uchar uh[]= "# User@Host: ";
 
1568
      if (my_b_write(&log_file, uh, sizeof(uh) - 1))
 
1569
        tmp_errno= errno;
 
1570
      if (my_b_write(&log_file, (uchar*) user_host, user_host_len))
 
1571
        tmp_errno= errno;
 
1572
      if (my_b_write(&log_file, (uchar*) "\n", 1))
 
1573
        tmp_errno= errno;
 
1574
    }
 
1575
    /* For slow query log */
 
1576
    sprintf(query_time_buff, "%.6f", uint64_t2double(query_utime)/1000000.0);
 
1577
    sprintf(lock_time_buff,  "%.6f", uint64_t2double(lock_utime)/1000000.0);
 
1578
    if (my_b_printf(&log_file,
 
1579
                    "# Query_time: %s  Lock_time: %s"
 
1580
                    " Rows_sent: %lu  Rows_examined: %lu\n",
 
1581
                    query_time_buff, lock_time_buff,
 
1582
                    (ulong) thd->sent_row_count,
 
1583
                    (ulong) thd->examined_row_count) == (uint) -1)
 
1584
      tmp_errno= errno;
 
1585
    if (thd->db && strcmp(thd->db, db))
 
1586
    {                                           // Database changed
 
1587
      if (my_b_printf(&log_file,"use %s;\n",thd->db) == (uint) -1)
 
1588
        tmp_errno= errno;
 
1589
      strmov(db,thd->db);
 
1590
    }
 
1591
    if (thd->stmt_depends_on_first_successful_insert_id_in_prev_stmt)
 
1592
    {
 
1593
      end=strmov(end, ",last_insert_id=");
 
1594
      end=int64_t10_to_str((int64_t)
 
1595
                            thd->first_successful_insert_id_in_prev_stmt_for_binlog,
 
1596
                            end, -10);
 
1597
    }
 
1598
    // Save value if we do an insert.
 
1599
    if (thd->auto_inc_intervals_in_cur_stmt_for_binlog.nb_elements() > 0)
 
1600
    {
 
1601
      if (!(specialflag & SPECIAL_SHORT_LOG_FORMAT))
 
1602
      {
 
1603
        end=strmov(end,",insert_id=");
 
1604
        end=int64_t10_to_str((int64_t)
 
1605
                              thd->auto_inc_intervals_in_cur_stmt_for_binlog.minimum(),
 
1606
                              end, -10);
 
1607
      }
 
1608
    }
 
1609
 
 
1610
    /*
 
1611
      This info used to show up randomly, depending on whether the query
 
1612
      checked the query start time or not. now we always write current
 
1613
      timestamp to the slow log
 
1614
    */
 
1615
    end= strmov(end, ",timestamp=");
 
1616
    end= int10_to_str((long) current_time, end, 10);
 
1617
 
 
1618
    if (end != buff)
 
1619
    {
 
1620
      *end++=';';
 
1621
      *end='\n';
 
1622
      if (my_b_write(&log_file, (uchar*) "SET ", 4) ||
 
1623
          my_b_write(&log_file, (uchar*) buff + 1, (uint) (end-buff)))
 
1624
        tmp_errno= errno;
 
1625
    }
 
1626
    if (is_command)
 
1627
    {
 
1628
      end= strxmov(buff, "# administrator command: ", NullS);
 
1629
      buff_len= (ulong) (end - buff);
 
1630
      my_b_write(&log_file, (uchar*) buff, buff_len);
 
1631
    }
 
1632
    if (my_b_write(&log_file, (uchar*) sql_text, sql_text_len) ||
 
1633
        my_b_write(&log_file, (uchar*) ";\n",2) ||
 
1634
        flush_io_cache(&log_file))
 
1635
      tmp_errno= errno;
 
1636
    if (tmp_errno)
 
1637
    {
 
1638
      error= 1;
 
1639
      if (! write_error)
 
1640
      {
 
1641
        write_error= 1;
 
1642
        sql_print_error(ER(ER_ERROR_ON_WRITE), name, error);
 
1643
      }
 
1644
    }
 
1645
  }
 
1646
  (void) pthread_mutex_unlock(&LOCK_log);
 
1647
  return(error);
 
1648
}
 
1649
 
 
1650
 
1006
1651
/**
1007
1652
  @todo
1008
1653
  The following should be using fn_format();  We just need to
1009
1654
  first change fn_format() to cut the file name if it's too long.
1010
1655
*/
1011
 
const char *DRIZZLE_LOG::generate_name(const char *log_name,
 
1656
const char *MYSQL_LOG::generate_name(const char *log_name,
1012
1657
                                      const char *suffix,
1013
1658
                                      bool strip_ext, char *buff)
1014
1659
{
1022
1667
  if (strip_ext)
1023
1668
  {
1024
1669
    char *p= fn_ext(log_name);
1025
 
    uint32_t length= (uint) (p - log_name);
1026
 
    strmake(buff, log_name, cmin(length, (uint)FN_REFLEN));
 
1670
    uint length= (uint) (p - log_name);
 
1671
    strmake(buff, log_name, min(length, FN_REFLEN));
1027
1672
    return (const char*)buff;
1028
1673
  }
1029
1674
  return log_name;
1031
1676
 
1032
1677
 
1033
1678
 
1034
 
DRIZZLE_BIN_LOG::DRIZZLE_BIN_LOG()
 
1679
MYSQL_BIN_LOG::MYSQL_BIN_LOG()
1035
1680
  :bytes_written(0), prepared_xids(0), file_id(1), open_count(1),
1036
1681
   need_start_event(true), m_table_map_version(0),
1037
1682
   description_event_for_exec(0), description_event_for_queue(0)
1043
1688
    before main().
1044
1689
  */
1045
1690
  index_file_name[0] = 0;
1046
 
  memset(&index_file, 0, sizeof(index_file));
 
1691
  memset((char*) &index_file, 0, sizeof(index_file));
1047
1692
}
1048
1693
 
1049
1694
/* this is called only once */
1050
1695
 
1051
 
void DRIZZLE_BIN_LOG::cleanup()
 
1696
void MYSQL_BIN_LOG::cleanup()
1052
1697
{
1053
1698
  if (inited)
1054
1699
  {
1065
1710
 
1066
1711
 
1067
1712
/* Init binlog-specific vars */
1068
 
void DRIZZLE_BIN_LOG::init(bool no_auto_events_arg, ulong max_size_arg)
 
1713
void MYSQL_BIN_LOG::init(bool no_auto_events_arg, ulong max_size_arg)
1069
1714
{
1070
1715
  no_auto_events= no_auto_events_arg;
1071
1716
  max_size= max_size_arg;
1073
1718
}
1074
1719
 
1075
1720
 
1076
 
void DRIZZLE_BIN_LOG::init_pthread_objects()
 
1721
void MYSQL_BIN_LOG::init_pthread_objects()
1077
1722
{
1078
1723
  assert(inited == 0);
1079
1724
  inited= 1;
1083
1728
}
1084
1729
 
1085
1730
 
1086
 
bool DRIZZLE_BIN_LOG::open_index_file(const char *index_file_name_arg,
 
1731
bool MYSQL_BIN_LOG::open_index_file(const char *index_file_name_arg,
1087
1732
                                const char *log_name)
1088
1733
{
1089
1734
  File index_file_nr= -1;
1100
1745
    index_file_name_arg= log_name;    // Use same basename for index file
1101
1746
    opt= MY_UNPACK_FILENAME | MY_REPLACE_EXT;
1102
1747
  }
1103
 
  fn_format(index_file_name, index_file_name_arg, drizzle_data_home,
 
1748
  fn_format(index_file_name, index_file_name_arg, mysql_data_home,
1104
1749
            ".index", opt);
1105
1750
  if ((index_file_nr= my_open(index_file_name,
1106
 
                              O_RDWR | O_CREAT,
 
1751
                              O_RDWR | O_CREAT | O_BINARY ,
1107
1752
                              MYF(MY_WME))) < 0 ||
1108
1753
       my_sync(index_file_nr, MYF(MY_WME)) ||
1109
1754
       init_io_cache(&index_file, index_file_nr,
1138
1783
    1   error
1139
1784
*/
1140
1785
 
1141
 
bool DRIZZLE_BIN_LOG::open(const char *log_name,
 
1786
bool MYSQL_BIN_LOG::open(const char *log_name,
1142
1787
                         enum_log_type log_type_arg,
1143
1788
                         const char *new_name,
1144
1789
                         enum cache_type io_cache_type_arg,
1151
1796
  write_error=0;
1152
1797
 
1153
1798
  /* open the main log file */
1154
 
  if (DRIZZLE_LOG::open(log_name, log_type_arg, new_name, io_cache_type_arg))
 
1799
  if (MYSQL_LOG::open(log_name, log_type_arg, new_name, io_cache_type_arg))
1155
1800
    return(1);                            /* all warnings issued */
1156
1801
 
1157
1802
  init(no_auto_events_arg, max_size_arg);
1171
1816
        an extension for the binary log files.
1172
1817
        In this case we write a standard header to it.
1173
1818
      */
1174
 
      if (my_b_safe_write(&log_file, (unsigned char*) BINLOG_MAGIC,
 
1819
      if (my_b_safe_write(&log_file, (uchar*) BINLOG_MAGIC,
1175
1820
                          BIN_LOG_HEADER_SIZE))
1176
1821
        goto err;
1177
1822
      bytes_written+= BIN_LOG_HEADER_SIZE;
1242
1887
        As this is a new log file, we write the file name to the index
1243
1888
        file. As every time we write to the index file, we sync it.
1244
1889
      */
1245
 
      if (my_b_write(&index_file, (unsigned char*) log_file_name,
 
1890
      if (my_b_write(&index_file, (uchar*) log_file_name,
1246
1891
                     strlen(log_file_name)) ||
1247
 
          my_b_write(&index_file, (unsigned char*) "\n", 1) ||
 
1892
          my_b_write(&index_file, (uchar*) "\n", 1) ||
1248
1893
          flush_io_cache(&index_file) ||
1249
1894
          my_sync(index_file.file, MYF(MY_WME)))
1250
1895
        goto err;
1255
1900
  return(0);
1256
1901
 
1257
1902
err:
1258
 
  sql_print_error(_("Could not use %s for logging (error %d). "
1259
 
                    "Turning logging off for the whole duration of the "
1260
 
                    "Drizzle server process. "
1261
 
                    "To turn it on again: fix the cause, "
1262
 
                    "shutdown the Drizzle server and restart it."),
1263
 
                    name, errno);
 
1903
  sql_print_error("Could not use %s for logging (error %d). \
 
1904
Turning logging off for the whole duration of the MySQL server process. \
 
1905
To turn it on again: fix the cause, \
 
1906
shutdown the MySQL server and restart it.", name, errno);
1264
1907
  if (file >= 0)
1265
1908
    my_close(file,MYF(0));
1266
1909
  end_io_cache(&log_file);
1267
1910
  end_io_cache(&index_file);
1268
 
  if (name)
1269
 
  {
1270
 
    free(name);
1271
 
    name= NULL;
1272
 
  }
 
1911
  safeFree(name);
1273
1912
  log_state= LOG_CLOSED;
1274
1913
  return(1);
1275
1914
}
1276
1915
 
1277
1916
 
1278
 
int DRIZZLE_BIN_LOG::get_current_log(LOG_INFO* linfo)
 
1917
int MYSQL_BIN_LOG::get_current_log(LOG_INFO* linfo)
1279
1918
{
1280
1919
  pthread_mutex_lock(&LOCK_log);
1281
1920
  int ret = raw_get_current_log(linfo);
1283
1922
  return ret;
1284
1923
}
1285
1924
 
1286
 
int DRIZZLE_BIN_LOG::raw_get_current_log(LOG_INFO* linfo)
 
1925
int MYSQL_BIN_LOG::raw_get_current_log(LOG_INFO* linfo)
1287
1926
{
1288
1927
  strmake(linfo->log_file_name, log_file_name, sizeof(linfo->log_file_name)-1);
1289
1928
  linfo->pos = my_b_tell(&log_file);
1307
1946
    0   ok
1308
1947
*/
1309
1948
 
 
1949
#ifdef HAVE_REPLICATION
 
1950
 
1310
1951
static bool copy_up_file_and_fill(IO_CACHE *index_file, my_off_t offset)
1311
1952
{
1312
1953
  int bytes_read;
1313
1954
  my_off_t init_offset= offset;
1314
1955
  File file= index_file->file;
1315
 
  unsigned char io_buf[IO_SIZE*2];
 
1956
  uchar io_buf[IO_SIZE*2];
1316
1957
 
1317
1958
  for (;; offset+= bytes_read)
1318
1959
  {
1338
1979
  return(1);
1339
1980
}
1340
1981
 
 
1982
#endif /* HAVE_REPLICATION */
 
1983
 
1341
1984
/**
1342
1985
  Find the position in the log-index-file for the given log name.
1343
1986
 
1360
2003
    LOG_INFO_IO         Got IO error while reading file
1361
2004
*/
1362
2005
 
1363
 
int DRIZZLE_BIN_LOG::find_log_pos(LOG_INFO *linfo, const char *log_name,
 
2006
int MYSQL_BIN_LOG::find_log_pos(LOG_INFO *linfo, const char *log_name,
1364
2007
                            bool need_lock)
1365
2008
{
1366
2009
  int error= 0;
1367
2010
  char *fname= linfo->log_file_name;
1368
 
  uint32_t log_name_len= log_name ? (uint) strlen(log_name) : 0;
 
2011
  uint log_name_len= log_name ? (uint) strlen(log_name) : 0;
1369
2012
 
1370
2013
  /*
1371
2014
    Mutex needed because we need to make sure the file pointer does not
1380
2023
 
1381
2024
  for (;;)
1382
2025
  {
1383
 
    uint32_t length;
 
2026
    uint length;
1384
2027
    my_off_t offset= my_b_tell(&index_file);
1385
2028
    /* If we get 0 or 1 characters, this is the end of the file */
1386
2029
 
1433
2076
    LOG_INFO_IO         Got IO error while reading file
1434
2077
*/
1435
2078
 
1436
 
int DRIZZLE_BIN_LOG::find_next_log(LOG_INFO* linfo, bool need_lock)
 
2079
int MYSQL_BIN_LOG::find_next_log(LOG_INFO* linfo, bool need_lock)
1437
2080
{
1438
2081
  int error= 0;
1439
 
  uint32_t length;
 
2082
  uint length;
1440
2083
  char *fname= linfo->log_file_name;
1441
2084
 
1442
2085
  if (need_lock)
1469
2112
 
1470
2113
  The new index file will only contain this file.
1471
2114
 
1472
 
  @param session                Thread
 
2115
  @param thd            Thread
1473
2116
 
1474
2117
  @note
1475
2118
    If not called from slave thread, write start event to new log
1480
2123
    1   error
1481
2124
*/
1482
2125
 
1483
 
bool DRIZZLE_BIN_LOG::reset_logs(Session* session)
 
2126
bool MYSQL_BIN_LOG::reset_logs(THD* thd)
1484
2127
{
1485
2128
  LOG_INFO linfo;
1486
2129
  bool error=0;
1487
2130
  const char* save_name;
1488
2131
 
 
2132
  ha_reset_logs(thd);
1489
2133
  /*
1490
2134
    We need to get both locks to be sure that no one is trying to
1491
2135
    write to the index log file.
1495
2139
 
1496
2140
  /*
1497
2141
    The following mutex is needed to ensure that no threads call
1498
 
    'delete session' as we would then risk missing a 'rollback' from this
 
2142
    'delete thd' as we would then risk missing a 'rollback' from this
1499
2143
    thread. If the transaction involved MyISAM tables, it should go
1500
2144
    into binlog even on rollback.
1501
2145
  */
1502
 
  pthread_mutex_lock(&LOCK_thread_count);
 
2146
  VOID(pthread_mutex_lock(&LOCK_thread_count));
1503
2147
 
1504
2148
  /* Save variables so that we can reopen the log */
1505
2149
  save_name=name;
1508
2152
 
1509
2153
  /* First delete all old log files */
1510
2154
 
1511
 
  if (find_log_pos(&linfo, NULL, 0))
 
2155
  if (find_log_pos(&linfo, NullS, 0))
1512
2156
  {
1513
2157
    error=1;
1514
2158
    goto err;
1520
2164
    {
1521
2165
      if (my_errno == ENOENT) 
1522
2166
      {
1523
 
        push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
2167
        push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
1524
2168
                            ER_LOG_PURGE_NO_FILE, ER(ER_LOG_PURGE_NO_FILE),
1525
2169
                            linfo.log_file_name);
1526
 
        sql_print_information(_("Failed to delete file '%s'"),
 
2170
        sql_print_information("Failed to delete file '%s'",
1527
2171
                              linfo.log_file_name);
1528
2172
        my_errno= 0;
1529
2173
        error= 0;
1530
2174
      }
1531
2175
      else
1532
2176
      {
1533
 
        push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
 
2177
        push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_ERROR,
1534
2178
                            ER_BINLOG_PURGE_FATAL_ERR,
1535
 
                            _("a problem with deleting %s; "
 
2179
                            "a problem with deleting %s; "
1536
2180
                            "consider examining correspondence "
1537
2181
                            "of your binlog index file "
1538
 
                            "to the actual binlog files"),
 
2182
                            "to the actual binlog files",
1539
2183
                            linfo.log_file_name);
1540
2184
        error= 1;
1541
2185
        goto err;
1551
2195
  {
1552
2196
    if (my_errno == ENOENT) 
1553
2197
    {
1554
 
      push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
2198
      push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
1555
2199
                          ER_LOG_PURGE_NO_FILE, ER(ER_LOG_PURGE_NO_FILE),
1556
2200
                          index_file_name);
1557
 
      sql_print_information(_("Failed to delete file '%s'"),
 
2201
      sql_print_information("Failed to delete file '%s'",
1558
2202
                            index_file_name);
1559
2203
      my_errno= 0;
1560
2204
      error= 0;
1561
2205
    }
1562
2206
    else
1563
2207
    {
1564
 
      push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
 
2208
      push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_ERROR,
1565
2209
                          ER_BINLOG_PURGE_FATAL_ERR,
1566
2210
                          "a problem with deleting %s; "
1567
2211
                          "consider examining correspondence "
1572
2216
      goto err;
1573
2217
    }
1574
2218
  }
1575
 
  if (!session->slave_thread)
 
2219
  if (!thd->slave_thread)
1576
2220
    need_start_event=1;
1577
2221
  if (!open_index_file(index_file_name, 0))
1578
2222
    open(save_name, log_type, 0, io_cache_type, no_auto_events, max_size, 0);
1579
 
  free((unsigned char*) save_name);
 
2223
  my_free((uchar*) save_name, MYF(0));
1580
2224
 
1581
2225
err:
1582
 
  pthread_mutex_unlock(&LOCK_thread_count);
 
2226
  VOID(pthread_mutex_unlock(&LOCK_thread_count));
1583
2227
  pthread_mutex_unlock(&LOCK_index);
1584
2228
  pthread_mutex_unlock(&LOCK_log);
1585
2229
  return(error);
1623
2267
    LOG_INFO_IO         Got IO error while reading file
1624
2268
*/
1625
2269
 
 
2270
#ifdef HAVE_REPLICATION
1626
2271
 
1627
 
int DRIZZLE_BIN_LOG::purge_first_log(Relay_log_info* rli, bool included)
 
2272
int MYSQL_BIN_LOG::purge_first_log(Relay_log_info* rli, bool included)
1628
2273
{
1629
2274
  int error;
1630
2275
 
1631
2276
  assert(is_open());
1632
2277
  assert(rli->slave_running == 1);
1633
 
  assert(!strcmp(rli->linfo.log_file_name,rli->event_relay_log_name.c_str()));
 
2278
  assert(!strcmp(rli->linfo.log_file_name,rli->event_relay_log_name));
1634
2279
 
1635
2280
  pthread_mutex_lock(&LOCK_index);
1636
2281
  pthread_mutex_lock(&rli->log_space_lock);
1637
 
  rli->relay_log.purge_logs(rli->group_relay_log_name.c_str(), included,
 
2282
  rli->relay_log.purge_logs(rli->group_relay_log_name, included,
1638
2283
                            0, 0, &rli->log_space_total);
1639
2284
  // Tell the I/O thread to take the relay_log_space_limit into account
1640
2285
  rli->ignore_log_space_limit= 0;
1653
2298
    If included is true, we want the first relay log;
1654
2299
    otherwise we want the one after event_relay_log_name.
1655
2300
  */
1656
 
  if ((included && (error=find_log_pos(&rli->linfo, NULL, 0))) ||
 
2301
  if ((included && (error=find_log_pos(&rli->linfo, NullS, 0))) ||
1657
2302
      (!included &&
1658
 
       ((error=find_log_pos(&rli->linfo, rli->event_relay_log_name.c_str(), 0)) ||
 
2303
       ((error=find_log_pos(&rli->linfo, rli->event_relay_log_name, 0)) ||
1659
2304
        (error=find_next_log(&rli->linfo, 0)))))
1660
2305
  {
1661
2306
    char buff[22];
1662
 
    sql_print_error(_("next log error: %d  offset: %s  log: %s included: %d"),
 
2307
    sql_print_error("next log error: %d  offset: %s  log: %s included: %d",
1663
2308
                    error,
1664
2309
                    llstr(rli->linfo.index_file_offset,buff),
1665
 
                    rli->group_relay_log_name.c_str(),
 
2310
                    rli->group_relay_log_name,
1666
2311
                    included);
1667
2312
    goto err;
1668
2313
  }
1671
2316
    Reset rli's coordinates to the current log.
1672
2317
  */
1673
2318
  rli->event_relay_log_pos= BIN_LOG_HEADER_SIZE;
1674
 
  rli->event_relay_log_name.assign(rli->linfo.log_file_name);
 
2319
  strmake(rli->event_relay_log_name,rli->linfo.log_file_name,
 
2320
          sizeof(rli->event_relay_log_name)-1);
1675
2321
 
1676
2322
  /*
1677
2323
    If we removed the rli->group_relay_log_name file,
1681
2327
  if (included)
1682
2328
  {
1683
2329
    rli->group_relay_log_pos = BIN_LOG_HEADER_SIZE;
1684
 
    rli->group_relay_log_name.assign(rli->linfo.log_file_name);
 
2330
    strmake(rli->group_relay_log_name,rli->linfo.log_file_name,
 
2331
            sizeof(rli->group_relay_log_name)-1);
1685
2332
    rli->notify_group_relay_log_name_update();
1686
2333
  }
1687
2334
 
1697
2344
  Update log index_file.
1698
2345
*/
1699
2346
 
1700
 
int DRIZZLE_BIN_LOG::update_log_index(LOG_INFO* log_info, bool need_update_threads)
 
2347
int MYSQL_BIN_LOG::update_log_index(LOG_INFO* log_info, bool need_update_threads)
1701
2348
{
1702
2349
  if (copy_up_file_and_fill(&index_file, log_info->index_file_start_offset))
1703
2350
    return LOG_INFO_IO;
1732
2379
                                stat() or my_delete()
1733
2380
*/
1734
2381
 
1735
 
int DRIZZLE_BIN_LOG::purge_logs(const char *to_log, 
 
2382
int MYSQL_BIN_LOG::purge_logs(const char *to_log, 
1736
2383
                          bool included,
1737
2384
                          bool need_mutex, 
1738
2385
                          bool need_update_threads, 
1752
2399
    File name exists in index file; delete until we find this file
1753
2400
    or a file that is used.
1754
2401
  */
1755
 
  if ((error=find_log_pos(&log_info, NULL, 0 /*no mutex*/)))
 
2402
  if ((error=find_log_pos(&log_info, NullS, 0 /*no mutex*/)))
1756
2403
    goto err;
1757
2404
  while ((strcmp(to_log,log_info.log_file_name) || (exit_loop=included)) &&
1758
2405
         !log_in_use(log_info.log_file_name))
1766
2413
          It's not fatal if we can't stat a log file that does not exist;
1767
2414
          If we could not stat, we won't delete.
1768
2415
        */     
1769
 
        push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
2416
        push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
1770
2417
                            ER_LOG_PURGE_NO_FILE, ER(ER_LOG_PURGE_NO_FILE),
1771
2418
                            log_info.log_file_name);
1772
 
        sql_print_information(_("Failed to execute stat() on file '%s'"),
 
2419
        sql_print_information("Failed to execute stat on file '%s'",
1773
2420
                              log_info.log_file_name);
1774
2421
        my_errno= 0;
1775
2422
      }
1778
2425
        /*
1779
2426
          Other than ENOENT are fatal
1780
2427
        */
1781
 
        push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
 
2428
        push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_ERROR,
1782
2429
                            ER_BINLOG_PURGE_FATAL_ERR,
1783
 
                            _("a problem with getting info on being purged %s; "
 
2430
                            "a problem with getting info on being purged %s; "
1784
2431
                            "consider examining correspondence "
1785
2432
                            "of your binlog index file "
1786
 
                            "to the actual binlog files"),
 
2433
                            "to the actual binlog files",
1787
2434
                            log_info.log_file_name);
1788
2435
        error= LOG_INFO_FATAL;
1789
2436
        goto err;
1800
2447
      {
1801
2448
        if (my_errno == ENOENT) 
1802
2449
        {
1803
 
          push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
2450
          push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
1804
2451
                              ER_LOG_PURGE_NO_FILE, ER(ER_LOG_PURGE_NO_FILE),
1805
2452
                              log_info.log_file_name);
1806
 
          sql_print_information(_("Failed to delete file '%s'"),
 
2453
          sql_print_information("Failed to delete file '%s'",
1807
2454
                                log_info.log_file_name);
1808
2455
          my_errno= 0;
1809
2456
        }
1810
2457
        else
1811
2458
        {
1812
 
          push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
 
2459
          push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_ERROR,
1813
2460
                              ER_BINLOG_PURGE_FATAL_ERR,
1814
 
                              _("a problem with deleting %s; "
 
2461
                              "a problem with deleting %s; "
1815
2462
                              "consider examining correspondence "
1816
2463
                              "of your binlog index file "
1817
 
                              "to the actual binlog files"),
 
2464
                              "to the actual binlog files",
1818
2465
                              log_info.log_file_name);
1819
2466
          if (my_errno == EMFILE)
1820
2467
          {
1826
2473
      }
1827
2474
    }
1828
2475
 
 
2476
    ha_binlog_index_purge_file(current_thd, log_info.log_file_name);
 
2477
 
1829
2478
    if (find_next_log(&log_info, 0) || exit_loop)
1830
2479
      break;
1831
2480
  }
1849
2498
  Remove all logs before the given file date from disk and from the
1850
2499
  index file.
1851
2500
 
1852
 
  @param session                Thread pointer
 
2501
  @param thd            Thread pointer
1853
2502
  @param before_date    Delete all log files before given date.
1854
2503
 
1855
2504
  @note
1864
2513
                                stat() or my_delete()
1865
2514
*/
1866
2515
 
1867
 
int DRIZZLE_BIN_LOG::purge_logs_before_date(time_t purge_time)
 
2516
int MYSQL_BIN_LOG::purge_logs_before_date(time_t purge_time)
1868
2517
{
1869
2518
  int error;
1870
2519
  LOG_INFO log_info;
1877
2526
    or a file that is used or a file
1878
2527
    that is older than purge_time.
1879
2528
  */
1880
 
  if ((error=find_log_pos(&log_info, NULL, 0 /*no mutex*/)))
 
2529
  if ((error=find_log_pos(&log_info, NullS, 0 /*no mutex*/)))
1881
2530
    goto err;
1882
2531
 
1883
2532
  while (strcmp(log_file_name, log_info.log_file_name) &&
1890
2539
        /*
1891
2540
          It's not fatal if we can't stat a log file that does not exist.
1892
2541
        */     
1893
 
        push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
2542
        push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
1894
2543
                            ER_LOG_PURGE_NO_FILE, ER(ER_LOG_PURGE_NO_FILE),
1895
2544
                            log_info.log_file_name);
1896
 
        sql_print_information(_("Failed to execute stat() on file '%s'"),
 
2545
        sql_print_information("Failed to execute stat on file '%s'",
1897
2546
                              log_info.log_file_name);
1898
2547
        my_errno= 0;
1899
2548
      }
1902
2551
        /*
1903
2552
          Other than ENOENT are fatal
1904
2553
        */
1905
 
        push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
 
2554
        push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_ERROR,
1906
2555
                            ER_BINLOG_PURGE_FATAL_ERR,
1907
 
                            _("a problem with getting info on being purged %s; "
 
2556
                            "a problem with getting info on being purged %s; "
1908
2557
                            "consider examining correspondence "
1909
2558
                            "of your binlog index file "
1910
 
                            "to the actual binlog files"),
 
2559
                            "to the actual binlog files",
1911
2560
                            log_info.log_file_name);
1912
2561
        error= LOG_INFO_FATAL;
1913
2562
        goto err;
1922
2571
        if (my_errno == ENOENT) 
1923
2572
        {
1924
2573
          /* It's not fatal even if we can't delete a log file */
1925
 
          push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
2574
          push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
1926
2575
                              ER_LOG_PURGE_NO_FILE, ER(ER_LOG_PURGE_NO_FILE),
1927
2576
                              log_info.log_file_name);
1928
 
          sql_print_information(_("Failed to delete file '%s'"),
 
2577
          sql_print_information("Failed to delete file '%s'",
1929
2578
                                log_info.log_file_name);
1930
2579
          my_errno= 0;
1931
2580
        }
1932
2581
        else
1933
2582
        {
1934
 
          push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
 
2583
          push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_ERROR,
1935
2584
                              ER_BINLOG_PURGE_FATAL_ERR,
1936
 
                              _("a problem with deleting %s; "
 
2585
                              "a problem with deleting %s; "
1937
2586
                              "consider examining correspondence "
1938
2587
                              "of your binlog index file "
1939
 
                              "to the actual binlog files"),
 
2588
                              "to the actual binlog files",
1940
2589
                              log_info.log_file_name);
1941
2590
          error= LOG_INFO_FATAL;
1942
2591
          goto err;
1943
2592
        }
1944
2593
      }
 
2594
      ha_binlog_index_purge_file(current_thd, log_info.log_file_name);
1945
2595
    }
1946
2596
    if (find_next_log(&log_info, 0))
1947
2597
      break;
1957
2607
  pthread_mutex_unlock(&LOCK_index);
1958
2608
  return(error);
1959
2609
}
 
2610
#endif /* HAVE_REPLICATION */
1960
2611
 
1961
2612
 
1962
2613
/**
1968
2619
    If file name will be longer then FN_REFLEN it will be truncated
1969
2620
*/
1970
2621
 
1971
 
void DRIZZLE_BIN_LOG::make_log_name(char* buf, const char* log_ident)
 
2622
void MYSQL_BIN_LOG::make_log_name(char* buf, const char* log_ident)
1972
2623
{
1973
 
  uint32_t dir_len = dirname_length(log_file_name); 
 
2624
  uint dir_len = dirname_length(log_file_name); 
1974
2625
  if (dir_len >= FN_REFLEN)
1975
2626
    dir_len=FN_REFLEN-1;
1976
 
  my_stpncpy(buf, log_file_name, dir_len);
 
2627
  strnmov(buf, log_file_name, dir_len);
1977
2628
  strmake(buf+dir_len, log_ident, FN_REFLEN - dir_len -1);
1978
2629
}
1979
2630
 
1982
2633
  Check if we are writing/reading to the given log file.
1983
2634
*/
1984
2635
 
1985
 
bool DRIZZLE_BIN_LOG::is_active(const char *log_file_name_arg)
 
2636
bool MYSQL_BIN_LOG::is_active(const char *log_file_name_arg)
1986
2637
{
1987
2638
  return !strcmp(log_file_name, log_file_name_arg);
1988
2639
}
1996
2647
  method).
1997
2648
*/
1998
2649
 
1999
 
void DRIZZLE_BIN_LOG::new_file()
 
2650
void MYSQL_BIN_LOG::new_file()
2000
2651
{
2001
2652
  new_file_impl(1);
2002
2653
}
2003
2654
 
2004
2655
 
2005
 
void DRIZZLE_BIN_LOG::new_file_without_locking()
 
2656
void MYSQL_BIN_LOG::new_file_without_locking()
2006
2657
{
2007
2658
  new_file_impl(0);
2008
2659
}
2017
2668
    The new file name is stored last in the index file
2018
2669
*/
2019
2670
 
2020
 
void DRIZZLE_BIN_LOG::new_file_impl(bool need_lock)
 
2671
void MYSQL_BIN_LOG::new_file_impl(bool need_lock)
2021
2672
{
2022
2673
  char new_name[FN_REFLEN], *new_name_ptr, *old_name;
2023
2674
 
2103
2754
 
2104
2755
  open(old_name, log_type, new_name_ptr,
2105
2756
       io_cache_type, no_auto_events, max_size, 1);
2106
 
  free(old_name);
 
2757
  my_free(old_name,MYF(0));
2107
2758
 
2108
2759
end:
2109
2760
  if (need_lock)
2114
2765
}
2115
2766
 
2116
2767
 
2117
 
bool DRIZZLE_BIN_LOG::append(Log_event* ev)
 
2768
bool MYSQL_BIN_LOG::append(Log_event* ev)
2118
2769
{
2119
2770
  bool error = 0;
2120
2771
  pthread_mutex_lock(&LOCK_log);
2140
2791
}
2141
2792
 
2142
2793
 
2143
 
bool DRIZZLE_BIN_LOG::appendv(const char* buf, uint32_t len,...)
 
2794
bool MYSQL_BIN_LOG::appendv(const char* buf, uint len,...)
2144
2795
{
2145
2796
  bool error= 0;
2146
2797
  va_list(args);
2151
2802
  safe_mutex_assert_owner(&LOCK_log);
2152
2803
  do
2153
2804
  {
2154
 
    if (my_b_append(&log_file,(unsigned char*) buf,len))
 
2805
    if (my_b_append(&log_file,(uchar*) buf,len))
2155
2806
    {
2156
2807
      error= 1;
2157
2808
      goto err;
2168
2819
}
2169
2820
 
2170
2821
 
2171
 
bool DRIZZLE_BIN_LOG::flush_and_sync()
 
2822
bool MYSQL_BIN_LOG::flush_and_sync()
2172
2823
{
2173
2824
  int err=0, fd=log_file.file;
2174
2825
  safe_mutex_assert_owner(&LOCK_log);
2182
2833
  return err;
2183
2834
}
2184
2835
 
2185
 
void DRIZZLE_BIN_LOG::start_union_events(Session *session, query_id_t query_id_param)
2186
 
{
2187
 
  assert(!session->binlog_evt_union.do_union);
2188
 
  session->binlog_evt_union.do_union= true;
2189
 
  session->binlog_evt_union.unioned_events= false;
2190
 
  session->binlog_evt_union.unioned_events_trans= false;
2191
 
  session->binlog_evt_union.first_query_id= query_id_param;
2192
 
}
2193
 
 
2194
 
void DRIZZLE_BIN_LOG::stop_union_events(Session *session)
2195
 
{
2196
 
  assert(session->binlog_evt_union.do_union);
2197
 
  session->binlog_evt_union.do_union= false;
2198
 
}
2199
 
 
2200
 
bool DRIZZLE_BIN_LOG::is_query_in_union(Session *session, query_id_t query_id_param)
2201
 
{
2202
 
  return (session->binlog_evt_union.do_union && 
2203
 
          query_id_param >= session->binlog_evt_union.first_query_id);
 
2836
void MYSQL_BIN_LOG::start_union_events(THD *thd, query_id_t query_id_param)
 
2837
{
 
2838
  assert(!thd->binlog_evt_union.do_union);
 
2839
  thd->binlog_evt_union.do_union= true;
 
2840
  thd->binlog_evt_union.unioned_events= false;
 
2841
  thd->binlog_evt_union.unioned_events_trans= false;
 
2842
  thd->binlog_evt_union.first_query_id= query_id_param;
 
2843
}
 
2844
 
 
2845
void MYSQL_BIN_LOG::stop_union_events(THD *thd)
 
2846
{
 
2847
  assert(thd->binlog_evt_union.do_union);
 
2848
  thd->binlog_evt_union.do_union= false;
 
2849
}
 
2850
 
 
2851
bool MYSQL_BIN_LOG::is_query_in_union(THD *thd, query_id_t query_id_param)
 
2852
{
 
2853
  return (thd->binlog_evt_union.do_union && 
 
2854
          query_id_param >= thd->binlog_evt_union.first_query_id);
2204
2855
}
2205
2856
 
2206
2857
 
2209
2860
  binlog_hton, which has internal linkage.
2210
2861
*/
2211
2862
 
2212
 
int Session::binlog_setup_trx_data()
 
2863
int THD::binlog_setup_trx_data()
2213
2864
{
2214
2865
  binlog_trx_data *trx_data=
2215
 
    (binlog_trx_data*) session_get_ha_data(this, binlog_hton);
 
2866
    (binlog_trx_data*) thd_get_ha_data(this, binlog_hton);
2216
2867
 
2217
2868
  if (trx_data)
2218
2869
    return(0);                             // Already set up
2219
2870
 
2220
2871
  trx_data= (binlog_trx_data*) my_malloc(sizeof(binlog_trx_data), MYF(MY_ZEROFILL));
2221
2872
  if (!trx_data ||
2222
 
      open_cached_file(&trx_data->trans_log, drizzle_tmpdir,
 
2873
      open_cached_file(&trx_data->trans_log, mysql_tmpdir,
2223
2874
                       LOG_PREFIX, binlog_cache_size, MYF(MY_WME)))
2224
2875
  {
2225
 
    free((unsigned char*)trx_data);
 
2876
    my_free((uchar*)trx_data, MYF(MY_ALLOW_ZERO_PTR));
2226
2877
    return(1);                      // Didn't manage to set it up
2227
2878
  }
2228
 
  session_set_ha_data(this, binlog_hton, trx_data);
 
2879
  thd_set_ha_data(this, binlog_hton, trx_data);
2229
2880
 
2230
 
  trx_data= new (session_get_ha_data(this, binlog_hton)) binlog_trx_data;
 
2881
  trx_data= new (thd_get_ha_data(this, binlog_hton)) binlog_trx_data;
2231
2882
 
2232
2883
  return(0);
2233
2884
}
2254
2905
      We only update the saved position if the old one was undefined,
2255
2906
      the reason is that there are some cases (e.g., for CREATE-SELECT)
2256
2907
      where the position is saved twice (e.g., both in
2257
 
      select_create::prepare() and Session::binlog_write_table_map()) , but
 
2908
      select_create::prepare() and THD::binlog_write_table_map()) , but
2258
2909
      we should use the first. This means that calls to this function
2259
2910
      can be used to start the statement before the first table map
2260
2911
      event, to include some extra events.
2261
2912
 */
2262
2913
 
2263
2914
void
2264
 
Session::binlog_start_trans_and_stmt()
 
2915
THD::binlog_start_trans_and_stmt()
2265
2916
{
2266
 
  binlog_trx_data *trx_data= (binlog_trx_data*) session_get_ha_data(this, binlog_hton);
 
2917
  binlog_trx_data *trx_data= (binlog_trx_data*) thd_get_ha_data(this, binlog_hton);
2267
2918
 
2268
2919
  if (trx_data == NULL ||
2269
2920
      trx_data->before_stmt_pos == MY_OFF_T_UNDEF)
2286
2937
  return;
2287
2938
}
2288
2939
 
2289
 
void Session::binlog_set_stmt_begin() {
 
2940
void THD::binlog_set_stmt_begin() {
2290
2941
  binlog_trx_data *trx_data=
2291
 
    (binlog_trx_data*) session_get_ha_data(this, binlog_hton);
 
2942
    (binlog_trx_data*) thd_get_ha_data(this, binlog_hton);
2292
2943
 
2293
2944
  /*
2294
2945
    The call to binlog_trans_log_savepos() might create the trx_data
2298
2949
  */
2299
2950
  my_off_t pos= 0;
2300
2951
  binlog_trans_log_savepos(this, &pos);
2301
 
  trx_data= (binlog_trx_data*) session_get_ha_data(this, binlog_hton);
 
2952
  trx_data= (binlog_trx_data*) thd_get_ha_data(this, binlog_hton);
2302
2953
  trx_data->before_stmt_pos= pos;
2303
2954
}
2304
2955
 
2307
2958
  Write a table map to the binary log.
2308
2959
 */
2309
2960
 
2310
 
int Session::binlog_write_table_map(Table *table, bool is_trans)
 
2961
int THD::binlog_write_table_map(TABLE *table, bool is_trans)
2311
2962
{
2312
2963
  int error;
2313
2964
 
2314
2965
  /* Pre-conditions */
2315
 
  assert(table->s->table_map_id != UINT32_MAX);
 
2966
  assert(current_stmt_binlog_row_based && mysql_bin_log.is_open());
 
2967
  assert(table->s->table_map_id != ULONG_MAX);
2316
2968
 
2317
2969
  Table_map_log_event::flag_set const
2318
2970
    flags= Table_map_log_event::TM_NO_FLAGS;
2323
2975
  if (is_trans && binlog_table_maps == 0)
2324
2976
    binlog_start_trans_and_stmt();
2325
2977
 
2326
 
  if ((error= drizzle_bin_log.write(&the_event)))
 
2978
  if ((error= mysql_bin_log.write(&the_event)))
2327
2979
    return(error);
2328
2980
 
2329
2981
  binlog_table_maps++;
2330
 
  table->s->table_map_version= drizzle_bin_log.table_map_version();
 
2982
  table->s->table_map_version= mysql_bin_log.table_map_version();
2331
2983
  return(0);
2332
2984
}
2333
2985
 
2334
2986
Rows_log_event*
2335
 
Session::binlog_get_pending_rows_event() const
 
2987
THD::binlog_get_pending_rows_event() const
2336
2988
{
2337
2989
  binlog_trx_data *const trx_data=
2338
 
    (binlog_trx_data*) session_get_ha_data(this, binlog_hton);
 
2990
    (binlog_trx_data*) thd_get_ha_data(this, binlog_hton);
2339
2991
  /*
2340
2992
    This is less than ideal, but here's the story: If there is no
2341
2993
    trx_data, prepare_pending_rows_event() has never been called
2346
2998
}
2347
2999
 
2348
3000
void
2349
 
Session::binlog_set_pending_rows_event(Rows_log_event* ev)
 
3001
THD::binlog_set_pending_rows_event(Rows_log_event* ev)
2350
3002
{
2351
 
  if (session_get_ha_data(this, binlog_hton) == NULL)
 
3003
  if (thd_get_ha_data(this, binlog_hton) == NULL)
2352
3004
    binlog_setup_trx_data();
2353
3005
 
2354
3006
  binlog_trx_data *const trx_data=
2355
 
    (binlog_trx_data*) session_get_ha_data(this, binlog_hton);
 
3007
    (binlog_trx_data*) thd_get_ha_data(this, binlog_hton);
2356
3008
 
2357
3009
  assert(trx_data);
2358
3010
  trx_data->set_pending(ev);
2365
3017
  event.
2366
3018
*/
2367
3019
int
2368
 
DRIZZLE_BIN_LOG::flush_and_set_pending_rows_event(Session *session,
 
3020
MYSQL_BIN_LOG::flush_and_set_pending_rows_event(THD *thd,
2369
3021
                                                Rows_log_event* event)
2370
3022
{
2371
 
  assert(drizzle_bin_log.is_open());
 
3023
  assert(mysql_bin_log.is_open());
2372
3024
 
2373
3025
  int error= 0;
2374
3026
 
2375
3027
  binlog_trx_data *const trx_data=
2376
 
    (binlog_trx_data*) session_get_ha_data(session, binlog_hton);
 
3028
    (binlog_trx_data*) thd_get_ha_data(thd, binlog_hton);
2377
3029
 
2378
3030
  assert(trx_data);
2379
3031
 
2438
3090
    pthread_mutex_unlock(&LOCK_log);
2439
3091
  }
2440
3092
 
2441
 
  session->binlog_set_pending_rows_event(event);
 
3093
  thd->binlog_set_pending_rows_event(event);
2442
3094
 
2443
3095
  return(error);
2444
3096
}
2447
3099
  Write an event to the binary log.
2448
3100
*/
2449
3101
 
2450
 
bool DRIZZLE_BIN_LOG::write(Log_event *event_info)
 
3102
bool MYSQL_BIN_LOG::write(Log_event *event_info)
2451
3103
{
2452
 
  Session *session= event_info->session;
 
3104
  THD *thd= event_info->thd;
2453
3105
  bool error= 1;
2454
3106
 
2455
 
  if (session->binlog_evt_union.do_union)
 
3107
  if (thd->binlog_evt_union.do_union)
2456
3108
  {
2457
3109
    /*
2458
3110
      In Stored function; Remember that function call caused an update.
2459
3111
      We will log the function call to the binary log on function exit
2460
3112
    */
2461
 
    session->binlog_evt_union.unioned_events= true;
2462
 
    session->binlog_evt_union.unioned_events_trans |= event_info->cache_stmt;
 
3113
    thd->binlog_evt_union.unioned_events= true;
 
3114
    thd->binlog_evt_union.unioned_events_trans |= event_info->cache_stmt;
2463
3115
    return(0);
2464
3116
  }
2465
3117
 
2474
3126
    this will close all tables on the slave.
2475
3127
  */
2476
3128
  bool const end_stmt= false;
2477
 
  session->binlog_flush_pending_rows_event(end_stmt);
 
3129
  thd->binlog_flush_pending_rows_event(end_stmt);
2478
3130
 
2479
3131
  pthread_mutex_lock(&LOCK_log);
2480
3132
 
2492
3144
      binlog_[wild_]{do|ignore}_table?" (WL#1049)"
2493
3145
    */
2494
3146
    const char *local_db= event_info->get_db();
2495
 
    if ((session && !(session->options & OPTION_BIN_LOG)) ||
 
3147
    if ((thd && !(thd->options & OPTION_BIN_LOG)) ||
2496
3148
        (!binlog_filter->db_ok(local_db)))
2497
3149
    {
2498
 
      pthread_mutex_unlock(&LOCK_log);
 
3150
      VOID(pthread_mutex_unlock(&LOCK_log));
2499
3151
      return(0);
2500
3152
    }
2501
3153
 
2508
3160
     trans/non-trans table types the best possible in binlogging)
2509
3161
      - or if the event asks for it (cache_stmt == TRUE).
2510
3162
    */
2511
 
    if (opt_using_transactions && session)
 
3163
    if (opt_using_transactions && thd)
2512
3164
    {
2513
 
      if (session->binlog_setup_trx_data())
 
3165
      if (thd->binlog_setup_trx_data())
2514
3166
        goto err;
2515
3167
 
2516
3168
      binlog_trx_data *const trx_data=
2517
 
        (binlog_trx_data*) session_get_ha_data(session, binlog_hton);
 
3169
        (binlog_trx_data*) thd_get_ha_data(thd, binlog_hton);
2518
3170
      IO_CACHE *trans_log= &trx_data->trans_log;
2519
3171
      my_off_t trans_log_pos= my_b_tell(trans_log);
2520
3172
      if (event_info->get_cache_stmt() || trans_log_pos != 0)
2521
3173
      {
2522
3174
        if (trans_log_pos == 0)
2523
 
          session->binlog_start_trans_and_stmt();
 
3175
          thd->binlog_start_trans_and_stmt();
2524
3176
        file= trans_log;
2525
3177
      }
2526
3178
      /*
2532
3184
    }
2533
3185
 
2534
3186
    /*
 
3187
      No check for auto events flag here - this write method should
 
3188
      never be called if auto-events are enabled
 
3189
    */
 
3190
 
 
3191
    /*
 
3192
      1. Write first log events which describe the 'run environment'
 
3193
      of the SQL command
 
3194
    */
 
3195
 
 
3196
    /*
 
3197
      If row-based binlogging, Insert_id, Rand and other kind of "setting
 
3198
      context" events are not needed.
 
3199
    */
 
3200
    if (thd)
 
3201
    {
 
3202
      if (!thd->current_stmt_binlog_row_based)
 
3203
      {
 
3204
        if (thd->stmt_depends_on_first_successful_insert_id_in_prev_stmt)
 
3205
        {
 
3206
          Intvar_log_event e(thd,(uchar) LAST_INSERT_ID_EVENT,
 
3207
                             thd->first_successful_insert_id_in_prev_stmt_for_binlog);
 
3208
          if (e.write(file))
 
3209
            goto err;
 
3210
        }
 
3211
        if (thd->auto_inc_intervals_in_cur_stmt_for_binlog.nb_elements() > 0)
 
3212
        {
 
3213
          /*
 
3214
            If the auto_increment was second in a table's index (possible with
 
3215
            MyISAM or BDB) (table->next_number_keypart != 0), such event is
 
3216
            in fact not necessary. We could avoid logging it.
 
3217
          */
 
3218
          Intvar_log_event e(thd, (uchar) INSERT_ID_EVENT,
 
3219
                             thd->auto_inc_intervals_in_cur_stmt_for_binlog.
 
3220
                             minimum());
 
3221
          if (e.write(file))
 
3222
            goto err;
 
3223
        }
 
3224
        if (thd->rand_used)
 
3225
        {
 
3226
          Rand_log_event e(thd,thd->rand_saved_seed1,thd->rand_saved_seed2);
 
3227
          if (e.write(file))
 
3228
            goto err;
 
3229
        }
 
3230
        if (thd->user_var_events.elements)
 
3231
        {
 
3232
          for (uint i= 0; i < thd->user_var_events.elements; i++)
 
3233
          {
 
3234
            BINLOG_USER_VAR_EVENT *user_var_event;
 
3235
            get_dynamic(&thd->user_var_events,(uchar*) &user_var_event, i);
 
3236
            User_var_log_event e(thd, user_var_event->user_var_event->name.str,
 
3237
                                 user_var_event->user_var_event->name.length,
 
3238
                                 user_var_event->value,
 
3239
                                 user_var_event->length,
 
3240
                                 user_var_event->type,
 
3241
                                 user_var_event->charset_number);
 
3242
            if (e.write(file))
 
3243
              goto err;
 
3244
          }
 
3245
        }
 
3246
      }
 
3247
    }
 
3248
 
 
3249
    /*
2535
3250
       Write the SQL command
2536
3251
     */
2537
3252
 
2572
3287
  return logger.error_log_print(level, format, args);
2573
3288
}
2574
3289
 
2575
 
void DRIZZLE_BIN_LOG::rotate_and_purge(uint32_t flags)
 
3290
 
 
3291
bool slow_log_print(THD *thd, const char *query, uint query_length,
 
3292
                    uint64_t current_utime)
 
3293
{
 
3294
  return logger.slow_log_print(thd, query, query_length, current_utime);
 
3295
}
 
3296
 
 
3297
 
 
3298
bool LOGGER::log_command(THD *thd, enum enum_server_command command)
 
3299
{
 
3300
  /*
 
3301
    Log command if we have at least one log event handler enabled and want
 
3302
    to log this king of commands
 
3303
  */
 
3304
  if (*general_log_handler_list && (what_to_log & (1L << (uint) command)))
 
3305
  {
 
3306
    if (thd->options & OPTION_LOG_OFF)
 
3307
    {
 
3308
      /* No logging */
 
3309
      return false;
 
3310
    }
 
3311
 
 
3312
    return true;
 
3313
  }
 
3314
 
 
3315
  return false;
 
3316
}
 
3317
 
 
3318
 
 
3319
bool general_log_print(THD *thd, enum enum_server_command command,
 
3320
                       const char *format, ...)
 
3321
{
 
3322
  va_list args;
 
3323
  uint error= 0;
 
3324
 
 
3325
  /* Print the message to the buffer if we want to log this king of commands */
 
3326
  if (! logger.log_command(thd, command))
 
3327
    return false;
 
3328
 
 
3329
  va_start(args, format);
 
3330
  error= logger.general_log_print(thd, command, format, args);
 
3331
  va_end(args);
 
3332
 
 
3333
  return error;
 
3334
}
 
3335
 
 
3336
bool general_log_write(THD *thd, enum enum_server_command command,
 
3337
                       const char *query, uint query_length)
 
3338
{
 
3339
  /* Write the message to the log if we want to log this king of commands */
 
3340
  if (logger.log_command(thd, command))
 
3341
    return logger.general_log_write(thd, command, query, query_length);
 
3342
 
 
3343
  return false;
 
3344
}
 
3345
 
 
3346
void MYSQL_BIN_LOG::rotate_and_purge(uint flags)
2576
3347
{
2577
3348
  if (!(flags & RP_LOCK_LOG_IS_ALREADY_LOCKED))
2578
3349
    pthread_mutex_lock(&LOCK_log);
2580
3351
      (my_b_tell(&log_file) >= (my_off_t) max_size))
2581
3352
  {
2582
3353
    new_file_without_locking();
 
3354
#ifdef HAVE_REPLICATION
2583
3355
    if (expire_logs_days)
2584
3356
    {
2585
3357
      time_t purge_time= my_time(0) - expire_logs_days*24*60*60;
2586
3358
      if (purge_time >= 0)
2587
3359
        purge_logs_before_date(purge_time);
2588
3360
    }
 
3361
#endif
2589
3362
  }
2590
3363
  if (!(flags & RP_LOCK_LOG_IS_ALREADY_LOCKED))
2591
3364
    pthread_mutex_unlock(&LOCK_log);
2592
3365
}
2593
3366
 
2594
 
uint32_t DRIZZLE_BIN_LOG::next_file_id()
 
3367
uint MYSQL_BIN_LOG::next_file_id()
2595
3368
{
2596
 
  uint32_t res;
 
3369
  uint res;
2597
3370
  pthread_mutex_lock(&LOCK_log);
2598
3371
  res = file_id++;
2599
3372
  pthread_mutex_unlock(&LOCK_log);
2615
3388
    be reset as a READ_CACHE to be able to read the contents from it.
2616
3389
 */
2617
3390
 
2618
 
int DRIZZLE_BIN_LOG::write_cache(IO_CACHE *cache, bool lock_log, bool sync_log)
 
3391
int MYSQL_BIN_LOG::write_cache(IO_CACHE *cache, bool lock_log, bool sync_log)
2619
3392
{
2620
3393
  Mutex_sentry sentry(lock_log ? &LOCK_log : NULL);
2621
3394
 
2622
3395
  if (reinit_io_cache(cache, READ_CACHE, 0, 0, 0))
2623
3396
    return ER_ERROR_ON_WRITE;
2624
 
  uint32_t length= my_b_bytes_in_cache(cache), group, carry, hdr_offs;
 
3397
  uint length= my_b_bytes_in_cache(cache), group, carry, hdr_offs;
2625
3398
  long val;
2626
 
  unsigned char header[LOG_EVENT_HEADER_LEN];
 
3399
  uchar header[LOG_EVENT_HEADER_LEN];
2627
3400
 
2628
3401
  /*
2629
3402
    The events in the buffer have incorrect end_log_pos data
2654
3427
      assert(carry < LOG_EVENT_HEADER_LEN);
2655
3428
 
2656
3429
      /* assemble both halves */
2657
 
      memcpy(&header[carry], cache->read_pos, LOG_EVENT_HEADER_LEN - carry);
 
3430
      memcpy(&header[carry], (char *)cache->read_pos, LOG_EVENT_HEADER_LEN - carry);
2658
3431
 
2659
3432
      /* fix end_log_pos */
2660
3433
      val= uint4korr(&header[LOG_POS_OFFSET]) + group;
2668
3441
        copy fixed second half of header to cache so the correct
2669
3442
        version will be written later.
2670
3443
      */
2671
 
      memcpy(cache->read_pos, &header[carry], LOG_EVENT_HEADER_LEN - carry);
 
3444
      memcpy((char *)cache->read_pos, &header[carry], LOG_EVENT_HEADER_LEN - carry);
2672
3445
 
2673
3446
      /* next event header at ... */
2674
3447
      hdr_offs = uint4korr(&header[EVENT_LEN_OFFSET]) - carry;
2697
3470
        if (hdr_offs + LOG_EVENT_HEADER_LEN > length)
2698
3471
        {
2699
3472
          carry= length - hdr_offs;
2700
 
          memcpy(header, cache->read_pos + hdr_offs, carry);
 
3473
          memcpy(header, (char *)cache->read_pos + hdr_offs, carry);
2701
3474
          length= hdr_offs;
2702
3475
        }
2703
3476
        else
2704
3477
        {
2705
3478
          /* we've got a full event-header, and it came in one piece */
2706
3479
 
2707
 
          unsigned char *log_pos= (unsigned char *)cache->read_pos + hdr_offs + LOG_POS_OFFSET;
 
3480
          uchar *log_pos= (uchar *)cache->read_pos + hdr_offs + LOG_POS_OFFSET;
2708
3481
 
2709
3482
          /* fix end_log_pos */
2710
3483
          val= uint4korr(log_pos) + group;
2711
3484
          int4store(log_pos, val);
2712
3485
 
2713
3486
          /* next event header at ... */
2714
 
          log_pos= (unsigned char *)cache->read_pos + hdr_offs + EVENT_LEN_OFFSET;
 
3487
          log_pos= (uchar *)cache->read_pos + hdr_offs + EVENT_LEN_OFFSET;
2715
3488
          hdr_offs += uint4korr(log_pos);
2716
3489
 
2717
3490
        }
2750
3523
  was updated in a transaction which was rolled back. This is to ensure
2751
3524
  that the same updates are run on the slave.
2752
3525
 
2753
 
  @param session
 
3526
  @param thd
2754
3527
  @param cache          The cache to copy to the binlog
2755
3528
  @param commit_event   The commit event to print after writing the
2756
3529
                        contents of the cache.
2763
3536
    'cache' needs to be reinitialized after this functions returns.
2764
3537
*/
2765
3538
 
2766
 
bool DRIZZLE_BIN_LOG::write(Session *session, IO_CACHE *cache, Log_event *commit_event)
 
3539
bool MYSQL_BIN_LOG::write(THD *thd, IO_CACHE *cache, Log_event *commit_event)
2767
3540
{
2768
 
  pthread_mutex_lock(&LOCK_log);
 
3541
  VOID(pthread_mutex_lock(&LOCK_log));
2769
3542
 
2770
3543
  /* NULL would represent nothing to replicate after ROLLBACK */
2771
3544
  assert(commit_event != NULL);
2784
3557
        transaction is either a BEGIN..COMMIT block or a single
2785
3558
        statement in autocommit mode.
2786
3559
      */
2787
 
      Query_log_event qinfo(session, STRING_WITH_LEN("BEGIN"), true, false);
 
3560
      Query_log_event qinfo(thd, STRING_WITH_LEN("BEGIN"), true, false);
2788
3561
      /*
2789
3562
        Imagine this is rollback due to net timeout, after all
2790
3563
        statements of the transaction succeeded. Then we want a
2794
3567
        generated event, and as this event is generated late it would
2795
3568
        lead to false alarms.
2796
3569
 
2797
 
        This is safer than session->clear_error() against kills at shutdown.
 
3570
        This is safer than thd->clear_error() against kills at shutdown.
2798
3571
      */
2799
3572
      qinfo.error_code= 0;
2800
3573
      /*
2841
3614
    else
2842
3615
      rotate_and_purge(RP_LOCK_LOG_IS_ALREADY_LOCKED);
2843
3616
  }
2844
 
  pthread_mutex_unlock(&LOCK_log);
 
3617
  VOID(pthread_mutex_unlock(&LOCK_log));
2845
3618
 
2846
3619
  return(0);
2847
3620
 
2851
3624
    write_error= 1;
2852
3625
    sql_print_error(ER(ER_ERROR_ON_WRITE), name, errno);
2853
3626
  }
2854
 
  pthread_mutex_unlock(&LOCK_log);
 
3627
  VOID(pthread_mutex_unlock(&LOCK_log));
2855
3628
  return(1);
2856
3629
}
2857
3630
 
2859
3632
/**
2860
3633
  Wait until we get a signal that the relay log has been updated
2861
3634
 
2862
 
  @param[in] session   a Session struct
 
3635
  @param[in] thd   a THD struct
2863
3636
  @note
2864
3637
    LOCK_log must be taken before calling this function.
2865
3638
    It will be released at the end of the function.
2866
3639
*/
2867
3640
 
2868
 
void DRIZZLE_BIN_LOG::wait_for_update_relay_log(Session* session)
 
3641
void MYSQL_BIN_LOG::wait_for_update_relay_log(THD* thd)
2869
3642
{
2870
3643
  const char *old_msg;
2871
 
  old_msg= session->enter_cond(&update_cond, &LOCK_log,
 
3644
  old_msg= thd->enter_cond(&update_cond, &LOCK_log,
2872
3645
                           "Slave has read all relay log; " 
2873
3646
                           "waiting for the slave I/O "
2874
3647
                           "thread to update it" );
2875
3648
  pthread_cond_wait(&update_cond, &LOCK_log);
2876
 
  session->exit_cond(old_msg);
 
3649
  thd->exit_cond(old_msg);
2877
3650
  return;
2878
3651
}
2879
3652
 
2883
3656
  Applies to master only.
2884
3657
     
2885
3658
  NOTES
2886
 
  @param[in] session        a Session struct
 
3659
  @param[in] thd        a THD struct
2887
3660
  @param[in] timeout    a pointer to a timespec;
2888
3661
                        NULL means to wait w/o timeout.
2889
3662
  @retval    0          if got signalled on update
2894
3667
    LOCK_log is released by the caller.
2895
3668
*/
2896
3669
 
2897
 
int DRIZZLE_BIN_LOG::wait_for_update_bin_log(Session* session,
 
3670
int MYSQL_BIN_LOG::wait_for_update_bin_log(THD* thd,
2898
3671
                                           const struct timespec *timeout)
2899
3672
{
2900
3673
  int ret= 0;
2901
 
  const char* old_msg = session->get_proc_info();
2902
 
  old_msg= session->enter_cond(&update_cond, &LOCK_log,
 
3674
  const char* old_msg = thd->proc_info;
 
3675
  old_msg= thd->enter_cond(&update_cond, &LOCK_log,
2903
3676
                           "Master has sent all binlog to slave; "
2904
3677
                           "waiting for binlog to be updated");
2905
3678
  if (!timeout)
2925
3698
    The internal structures are not freed until cleanup() is called
2926
3699
*/
2927
3700
 
2928
 
void DRIZZLE_BIN_LOG::close(uint32_t exiting)
 
3701
void MYSQL_BIN_LOG::close(uint exiting)
2929
3702
{                                       // One can't set log_type here!
2930
3703
  if (log_state == LOG_OPENED)
2931
3704
  {
 
3705
#ifdef HAVE_REPLICATION
2932
3706
    if (log_type == LOG_BIN && !no_auto_events &&
2933
3707
        (exiting & LOG_CLOSE_STOP_EVENT))
2934
3708
    {
2937
3711
      bytes_written+= s.data_written;
2938
3712
      signal_update();
2939
3713
    }
 
3714
#endif /* HAVE_REPLICATION */
2940
3715
 
2941
3716
    /* don't pwrite in a file opened with O_APPEND - it doesn't work */
2942
3717
    if (log_file.type == WRITE_CACHE && log_type == LOG_BIN)
2943
3718
    {
2944
3719
      my_off_t offset= BIN_LOG_HEADER_SIZE + FLAGS_OFFSET;
2945
 
      unsigned char flags= 0;            // clearing LOG_EVENT_BINLOG_IN_USE_F
2946
 
      assert(pwrite(log_file.file, &flags, 1, offset)==1);
 
3720
      uchar flags= 0;            // clearing LOG_EVENT_BINLOG_IN_USE_F
 
3721
      pwrite(log_file.file, &flags, 1, offset);
2947
3722
    }
2948
3723
 
2949
3724
    /* this will cleanup IO_CACHE, sync and close the file */
2950
 
    DRIZZLE_LOG::close(exiting);
 
3725
    MYSQL_LOG::close(exiting);
2951
3726
  }
2952
3727
 
2953
3728
  /*
2965
3740
    }
2966
3741
  }
2967
3742
  log_state= (exiting & LOG_CLOSE_TO_BE_OPENED) ? LOG_TO_BE_OPENED : LOG_CLOSED;
2968
 
  if (name)
2969
 
  {
2970
 
    free(name);
2971
 
    name= NULL;
2972
 
  }
 
3743
  safeFree(name);
2973
3744
  return;
2974
3745
}
2975
3746
 
2976
3747
 
2977
 
void DRIZZLE_BIN_LOG::set_max_size(ulong max_size_arg)
 
3748
void MYSQL_BIN_LOG::set_max_size(ulong max_size_arg)
2978
3749
{
2979
3750
  /*
2980
3751
    We need to take locks, otherwise this may happen:
3042
3813
 
3043
3814
void sql_perror(const char *message)
3044
3815
{
 
3816
#ifdef HAVE_STRERROR
3045
3817
  sql_print_error("%s: %s",message, strerror(errno));
 
3818
#else
 
3819
  perror(message);
 
3820
#endif
3046
3821
}
3047
3822
 
3048
3823
 
3053
3828
  {
3054
3829
    char err_renamed[FN_REFLEN], *end;
3055
3830
    end= strmake(err_renamed,log_error_file,FN_REFLEN-4);
3056
 
    my_stpcpy(end, "-old");
3057
 
    pthread_mutex_lock(&LOCK_error_log);
 
3831
    strmov(end, "-old");
 
3832
    VOID(pthread_mutex_lock(&LOCK_error_log));
3058
3833
    char err_temp[FN_REFLEN+4];
3059
3834
    /*
3060
3835
     On Windows is necessary a temporary file for to rename
3061
3836
     the current error file.
3062
3837
    */
3063
 
    strxmov(err_temp, err_renamed,"-tmp",NULL);
 
3838
    strxmov(err_temp, err_renamed,"-tmp",NullS);
3064
3839
    (void) my_delete(err_temp, MYF(0)); 
3065
3840
    if (freopen(err_temp,"a+",stdout))
3066
3841
    {
3067
3842
      int fd;
3068
3843
      size_t bytes;
3069
 
      unsigned char buf[IO_SIZE];
 
3844
      uchar buf[IO_SIZE];
3070
3845
 
3071
 
      if(freopen(err_temp,"a+",stderr)==NULL)
3072
 
        return 1;
 
3846
      freopen(err_temp,"a+",stderr);
3073
3847
      (void) my_delete(err_renamed, MYF(0));
3074
3848
      my_rename(log_error_file,err_renamed,MYF(0));
3075
 
      if (freopen(log_error_file,"a+",stdout)==NULL)
3076
 
        return 1;
3077
 
      else
3078
 
        if(freopen(log_error_file,"a+",stderr)==NULL)
3079
 
          return 1;
 
3849
      if (freopen(log_error_file,"a+",stdout))
 
3850
        freopen(log_error_file,"a+",stderr);
3080
3851
 
3081
3852
      if ((fd = my_open(err_temp, O_RDONLY, MYF(0))) >= 0)
3082
3853
      {
3089
3860
    }
3090
3861
    else
3091
3862
     result= 1;
3092
 
    pthread_mutex_unlock(&LOCK_error_log);
 
3863
    VOID(pthread_mutex_unlock(&LOCK_error_log));
3093
3864
  }
3094
3865
   return result;
3095
3866
}
3096
3867
 
3097
 
void DRIZZLE_BIN_LOG::signal_update()
 
3868
void MYSQL_BIN_LOG::signal_update()
3098
3869
{
3099
3870
  pthread_cond_broadcast(&update_cond);
3100
3871
  return;
3116
3887
    signature to be compatible with other logging routines, which could
3117
3888
    return an error (e.g. logging to the log tables)
3118
3889
*/
3119
 
static void print_buffer_to_file(enum loglevel level, int,
3120
 
                                 const char *buffer, size_t)
 
3890
static void print_buffer_to_file(enum loglevel level,
 
3891
                                 int error_code __attribute__((unused)),
 
3892
                                 const char *buffer,
 
3893
                                 size_t buffer_length __attribute__((unused)))
3121
3894
{
3122
3895
  time_t skr;
3123
3896
  struct tm tm_tmp;
3124
3897
  struct tm *start;
3125
3898
 
3126
 
  pthread_mutex_lock(&LOCK_error_log);
 
3899
  VOID(pthread_mutex_lock(&LOCK_error_log));
3127
3900
 
3128
3901
  skr= my_time(0);
3129
3902
  localtime_r(&skr, &tm_tmp);
3142
3915
 
3143
3916
  fflush(stderr);
3144
3917
 
3145
 
  pthread_mutex_unlock(&LOCK_error_log);
 
3918
  VOID(pthread_mutex_unlock(&LOCK_error_log));
3146
3919
  return;
3147
3920
}
3148
3921
 
3166
3939
  va_list args;
3167
3940
 
3168
3941
  va_start(args, format);
3169
 
  errmsg_vprintf (current_session, ERROR_LEVEL, format, args);
 
3942
  error_log_print(ERROR_LEVEL, format, args);
3170
3943
  va_end(args);
3171
3944
 
3172
3945
  return;
3178
3951
  va_list args;
3179
3952
 
3180
3953
  va_start(args, format);
3181
 
  errmsg_vprintf (current_session, WARNING_LEVEL, format, args);
 
3954
  error_log_print(WARNING_LEVEL, format, args);
3182
3955
  va_end(args);
3183
3956
 
3184
3957
  return;
3190
3963
  va_list args;
3191
3964
 
3192
3965
  va_start(args, format);
3193
 
  errmsg_vprintf (current_session, INFORMATION_LEVEL, format, args);
 
3966
  error_log_print(INFORMATION_LEVEL, format, args);
3194
3967
  va_end(args);
3195
3968
 
3196
3969
  return;
3248
4021
 
3249
4022
int TC_LOG_MMAP::open(const char *opt_name)
3250
4023
{
3251
 
  uint32_t i;
 
4024
  uint i;
3252
4025
  bool crashed= false;
3253
4026
  PAGE *pg;
3254
4027
 
3258
4031
  tc_log_page_size= getpagesize();
3259
4032
  assert(TC_LOG_PAGE_SIZE % tc_log_page_size == 0);
3260
4033
 
3261
 
  fn_format(logname,opt_name,drizzle_data_home,"",MY_UNPACK_FILENAME);
 
4034
  fn_format(logname,opt_name,mysql_data_home,"",MY_UNPACK_FILENAME);
3262
4035
  if ((fd= my_open(logname, O_RDWR, MYF(0))) < 0)
3263
4036
  {
3264
4037
    if (my_errno != ENOENT)
3276
4049
  {
3277
4050
    inited= 1;
3278
4051
    crashed= true;
3279
 
    sql_print_information(_("Recovering after a crash using %s"), opt_name);
 
4052
    sql_print_information("Recovering after a crash using %s", opt_name);
3280
4053
    if (tc_heuristic_recover)
3281
4054
    {
3282
 
      sql_print_error(_("Cannot perform automatic crash recovery when "
3283
 
                      "--tc-heuristic-recover is used"));
 
4055
      sql_print_error("Cannot perform automatic crash recovery when "
 
4056
                      "--tc-heuristic-recover is used");
3284
4057
      goto err;
3285
4058
    }
3286
4059
    file_length= my_seek(fd, 0L, MY_SEEK_END, MYF(MY_WME+MY_FAE));
3288
4061
      goto err;
3289
4062
  }
3290
4063
 
3291
 
  data= (unsigned char *)my_mmap(0, (size_t)file_length, PROT_READ|PROT_WRITE,
 
4064
  data= (uchar *)my_mmap(0, (size_t)file_length, PROT_READ|PROT_WRITE,
3292
4065
                        MAP_NOSYNC|MAP_SHARED, fd, 0);
3293
4066
  if (data == MAP_FAILED)
3294
4067
  {
3324
4097
      goto err;
3325
4098
 
3326
4099
  memcpy(data, tc_log_magic, sizeof(tc_log_magic));
3327
 
  data[sizeof(tc_log_magic)]= (unsigned char)total_ha_2pc;
3328
 
  // must cast data to (char *) for solaris. Arg1 is (void *) on linux
3329
 
  //   so the cast should be fine. 
3330
 
  msync((char *)data, tc_log_page_size, MS_SYNC);
 
4100
  data[sizeof(tc_log_magic)]= (uchar)total_ha_2pc;
 
4101
  msync(data, tc_log_page_size, MS_SYNC);
3331
4102
  my_sync(fd, MYF(0));
3332
4103
  inited=5;
3333
4104
 
3434
4205
    threads waiting for a page, but then all these threads will be waiting
3435
4206
    for a fsync() anyway
3436
4207
 
3437
 
   If tc_log == DRIZZLE_LOG then tc_log writes transaction to binlog and
 
4208
   If tc_log == MYSQL_LOG then tc_log writes transaction to binlog and
3438
4209
   records XID in a special Xid_log_event.
3439
4210
   If tc_log = TC_LOG_MMAP then xid is written in a special memory-mapped
3440
4211
   log.
3448
4219
    to the position in memory where xid was logged to.
3449
4220
*/
3450
4221
 
3451
 
int TC_LOG_MMAP::log_xid(Session *, my_xid xid)
 
4222
int TC_LOG_MMAP::log_xid(THD *thd __attribute__((unused)), my_xid xid)
3452
4223
{
3453
4224
  int err;
3454
4225
  PAGE *p;
3481
4252
  }
3482
4253
 
3483
4254
  /* found! store xid there and mark the page dirty */
3484
 
  cookie= (ulong)((unsigned char *)p->ptr - data);      // can never be zero
 
4255
  cookie= (ulong)((uchar *)p->ptr - data);      // can never be zero
3485
4256
  *p->ptr++= xid;
3486
4257
  p->free--;
3487
4258
  p->state= DIRTY;
3533
4304
    sit down and relax - this can take a while...
3534
4305
    note - no locks are held at this point
3535
4306
  */
3536
 
  // must cast data to (char *) for solaris. Arg1 is (void *) on linux
3537
 
  //   so the cast should be fine. 
3538
 
  err= msync((char *)syncing->start, 1, MS_SYNC);
 
4307
  err= msync(syncing->start, 1, MS_SYNC);
3539
4308
  if(err==0)
3540
4309
    err= my_sync(fd, MYF(0));
3541
4310
 
3562
4331
  cookie points directly to the memory where xid was logged.
3563
4332
*/
3564
4333
 
3565
 
void TC_LOG_MMAP::unlog(ulong cookie, my_xid xid)
 
4334
void TC_LOG_MMAP::unlog(ulong cookie, my_xid xid __attribute__((unused)))
3566
4335
{
3567
4336
  PAGE *p=pages+(cookie/tc_log_page_size);
3568
4337
  my_xid *x=(my_xid *)(data+cookie);
3584
4353
 
3585
4354
void TC_LOG_MMAP::close()
3586
4355
{
3587
 
  uint32_t i;
 
4356
  uint i;
3588
4357
  switch (inited) {
3589
4358
  case 6:
3590
4359
    pthread_mutex_destroy(&LOCK_sync);
3602
4371
      pthread_cond_destroy(&pages[i].cond);
3603
4372
    }
3604
4373
  case 3:
3605
 
    free((unsigned char*)pages);
 
4374
    my_free((uchar*)pages, MYF(0));
3606
4375
  case 2:
3607
4376
    my_munmap((char*)data, (size_t)file_length);
3608
4377
  case 1:
3620
4389
 
3621
4390
  if (memcmp(data, tc_log_magic, sizeof(tc_log_magic)))
3622
4391
  {
3623
 
    sql_print_error(_("Bad magic header in tc log"));
 
4392
    sql_print_error("Bad magic header in tc log");
3624
4393
    goto err1;
3625
4394
  }
3626
4395
 
3630
4399
  */
3631
4400
  if (data[sizeof(tc_log_magic)] != total_ha_2pc)
3632
4401
  {
3633
 
    sql_print_error(_("Recovery failed! You must enable "
 
4402
    sql_print_error("Recovery failed! You must enable "
3634
4403
                    "exactly %d storage engines that support "
3635
 
                    "two-phase commit protocol"),
 
4404
                    "two-phase commit protocol",
3636
4405
                    data[sizeof(tc_log_magic)]);
3637
4406
    goto err1;
3638
4407
  }
3644
4413
  for ( ; p < end_p ; p++)
3645
4414
  {
3646
4415
    for (my_xid *x=p->start; x < p->end; x++)
3647
 
      if (*x && my_hash_insert(&xids, (unsigned char *)x))
 
4416
      if (*x && my_hash_insert(&xids, (uchar *)x))
3648
4417
        goto err2; // OOM
3649
4418
  }
3650
4419
 
3658
4427
err2:
3659
4428
  hash_free(&xids);
3660
4429
err1:
3661
 
  sql_print_error(_("Crash recovery failed. Either correct the problem "
 
4430
  sql_print_error("Crash recovery failed. Either correct the problem "
3662
4431
                  "(if it's, for example, out of memory error) and restart, "
3663
 
                  "or delete tc log and start drizzled with "
3664
 
                  "--tc-heuristic-recover={commit|rollback}"));
 
4432
                  "or delete tc log and start mysqld with "
 
4433
                  "--tc-heuristic-recover={commit|rollback}");
3665
4434
  return 1;
3666
4435
}
3667
4436
#endif
3688
4457
  if (!tc_heuristic_recover)
3689
4458
    return 0;
3690
4459
 
3691
 
  sql_print_information(_("Heuristic crash recovery mode"));
 
4460
  sql_print_information("Heuristic crash recovery mode");
3692
4461
  if (ha_recover(0))
3693
 
    sql_print_error(_("Heuristic crash recovery failed"));
3694
 
  sql_print_information(_("Please restart mysqld without --tc-heuristic-recover"));
 
4462
    sql_print_error("Heuristic crash recovery failed");
 
4463
  sql_print_information("Please restart mysqld without --tc-heuristic-recover");
3695
4464
  return 1;
3696
4465
}
3697
4466
 
3698
4467
/****** transaction coordinator log for 2pc - binlog() based solution ******/
3699
 
#define TC_LOG_BINLOG DRIZZLE_BIN_LOG
 
4468
#define TC_LOG_BINLOG MYSQL_BIN_LOG
3700
4469
 
3701
4470
/**
3702
4471
  @todo
3732
4501
    return 1;
3733
4502
  }
3734
4503
 
3735
 
  if ((error= find_log_pos(&log_info, NULL, 1)))
 
4504
  if ((error= find_log_pos(&log_info, NullS, 1)))
3736
4505
  {
3737
4506
    if (error != LOG_INFO_EOF)
3738
 
      sql_print_error(_("find_log_pos() failed (error: %d)"), error);
 
4507
      sql_print_error("find_log_pos() failed (error: %d)", error);
3739
4508
    else
3740
4509
      error= 0;
3741
4510
    goto err;
3759
4528
 
3760
4529
    if (error !=  LOG_INFO_EOF)
3761
4530
    {
3762
 
      sql_print_error(_("find_log_pos() failed (error: %d)"), error);
 
4531
      sql_print_error("find_log_pos() failed (error: %d)", error);
3763
4532
      goto err;
3764
4533
    }
3765
4534
 
3773
4542
        ev->get_type_code() == FORMAT_DESCRIPTION_EVENT &&
3774
4543
        ev->flags & LOG_EVENT_BINLOG_IN_USE_F)
3775
4544
    {
3776
 
      sql_print_information(_("Recovering after a crash using %s"), opt_name);
 
4545
      sql_print_information("Recovering after a crash using %s", opt_name);
3777
4546
      error= recover(&log, (Format_description_log_event *)ev);
3778
4547
    }
3779
4548
    else
3808
4577
  @retval
3809
4578
    1    success
3810
4579
*/
3811
 
int TC_LOG_BINLOG::log_xid(Session *session, my_xid xid)
 
4580
int TC_LOG_BINLOG::log_xid(THD *thd, my_xid xid)
3812
4581
{
3813
 
  Xid_log_event xle(session, xid);
 
4582
  Xid_log_event xle(thd, xid);
3814
4583
  binlog_trx_data *trx_data=
3815
 
    (binlog_trx_data*) session_get_ha_data(session, binlog_hton);
 
4584
    (binlog_trx_data*) thd_get_ha_data(thd, binlog_hton);
3816
4585
  /*
3817
4586
    We always commit the entire transaction when writing an XID. Also
3818
4587
    note that the return value is inverted.
3819
4588
   */
3820
 
  return(!binlog_end_trans(session, trx_data, &xle, true));
 
4589
  return(!binlog_end_trans(thd, trx_data, &xle, true));
3821
4590
}
3822
4591
 
3823
 
void TC_LOG_BINLOG::unlog(ulong, my_xid)
 
4592
void TC_LOG_BINLOG::unlog(ulong cookie __attribute__((unused)),
 
4593
                          my_xid xid __attribute__((unused)))
3824
4594
{
3825
4595
  pthread_mutex_lock(&LOCK_prep_xids);
3826
4596
  assert(prepared_xids > 0);
3851
4621
    if (ev->get_type_code() == XID_EVENT)
3852
4622
    {
3853
4623
      Xid_log_event *xev=(Xid_log_event *)ev;
3854
 
      unsigned char *x= (unsigned char *) memdup_root(&mem_root, (unsigned char*) &xev->xid,
 
4624
      uchar *x= (uchar *) memdup_root(&mem_root, (uchar*) &xev->xid,
3855
4625
                                      sizeof(xev->xid));
3856
4626
      if (! x)
3857
4627
        goto err2;
3871
4641
  free_root(&mem_root, MYF(0));
3872
4642
  hash_free(&xids);
3873
4643
err1:
3874
 
  sql_print_error(_("Crash recovery failed. Either correct the problem "
 
4644
  sql_print_error("Crash recovery failed. Either correct the problem "
3875
4645
                  "(if it's, for example, out of memory error) and restart, "
3876
4646
                  "or delete (or rename) binary log and start mysqld with "
3877
 
                  "--tc-heuristic-recover={commit|rollback}"));
 
4647
                  "--tc-heuristic-recover={commit|rollback}");
3878
4648
  return 1;
3879
4649
}
3880
4650
 
3881
4651
 
3882
 
bool DRIZZLE_BIN_LOG::is_table_mapped(Table *table) const
3883
 
{
3884
 
  return table->s->table_map_version == table_map_version();
3885
 
}
3886
 
 
3887
 
 
3888
4652
#ifdef INNODB_COMPATIBILITY_HOOKS
3889
4653
/**
3890
4654
  Get the file name of the MySQL binlog.
3891
4655
  @return the name of the binlog file
3892
4656
*/
3893
4657
extern "C"
3894
 
const char* drizzle_bin_log_file_name(void)
 
4658
const char* mysql_bin_log_file_name(void)
3895
4659
{
3896
 
  return drizzle_bin_log.get_log_fname();
 
4660
  return mysql_bin_log.get_log_fname();
3897
4661
}
3898
4662
/**
3899
4663
  Get the current position of the MySQL binlog.
3900
4664
  @return byte offset from the beginning of the binlog
3901
4665
*/
3902
4666
extern "C"
3903
 
uint64_t drizzle_bin_log_file_pos(void)
 
4667
uint64_t mysql_bin_log_file_pos(void)
3904
4668
{
3905
 
  return (uint64_t) drizzle_bin_log.get_log_file()->pos_in_file;
 
4669
  return (uint64_t) mysql_bin_log.get_log_file()->pos_in_file;
3906
4670
}
3907
4671
#endif /* INNODB_COMPATIBILITY_HOOKS */
3908
4672
 
3909
4673
 
3910
4674
mysql_declare_plugin(binlog)
3911
4675
{
3912
 
  DRIZZLE_STORAGE_ENGINE_PLUGIN,
 
4676
  MYSQL_STORAGE_ENGINE_PLUGIN,
3913
4677
  "binlog",
3914
4678
  "1.0",
3915
4679
  "MySQL AB",