List:Commits« Previous MessageNext Message »
From:Petr Chardin Date:December 26 2005 4:47pm
Subject:bk commit into 5.1 tree (petr:1.1964)
View as plain text  
Below is the list of changes that have just been committed into a local
5.1 repository of cps. When cps does a push these changes will
be propagated to the main repository and, within 24 hours after the
push, to the public repository.
For information on how to access the public repository
see http://dev.mysql.com/doc/mysql/en/installing-source-tree.html

ChangeSet
  1.1964 05/12/26 19:47:00 petr@stripped +9 -0
  WL#1019 post-review fixes

  sql/sql_prepare.cc
    1.156 05/12/26 19:46:31 petr@stripped +1 -1
    don't refer to the MYSQL_LOG objects directly, as they are hidden inside of
    the Log_to_file_event_handler

  sql/sql_parse.cc
    1.496 05/12/26 19:46:31 petr@stripped +2 -35
    flush logs was moved to LOGGER

  sql/sql_delete.cc
    1.161 05/12/26 19:46:31 petr@stripped +5 -6
    use logger for all log-related activity.

  sql/sql_class.h
    1.274 05/12/26 19:46:31 petr@stripped +150 -56
    declare common interface for the log event handlers. Fix LOGGER
    to use it.

  sql/mysqld.cc
    1.504 05/12/26 19:46:30 petr@stripped +31 -40
    log initialization fixed: now we initilaize logger, rather then individual
    handlers. Logger, in it's turn initializes log event handlers.

  sql/mysql_priv.h
    1.355 05/12/26 19:46:29 petr@stripped +1 -11
    remove unused functions

  sql/log.cc
    1.184 05/12/26 19:46:29 petr@stripped +316 -170
    refactoring: all log event handlers are now formed into a classes with a
    common interface. They are used by the LOGGER class, which is responsible
    for their initialization, cluanup and managment.

  sql/lock.cc
    1.82 05/12/26 19:46:29 petr@stripped +2 -1
    log THD's are not part of the table log event handler

  mysql-test/t/log_tables.test
    1.7 05/12/26 19:46:28 petr@stripped +7 -5
    adjust error numbers

# This is a BitKeeper patch.  What follows are the unified diffs for the
# set of deltas contained in the patch.  The rest of the patch, the part
# that BitKeeper cares about, is below these diffs.
# User:	petr
# Host:	owlet.
# Root:	/home/cps/mysql/devel/mysql-5.1-logs-merge

--- 1.81/sql/lock.cc	2005-12-06 00:10:37 +03:00
+++ 1.82/sql/lock.cc	2005-12-26 19:46:29 +03:00
@@ -499,7 +499,8 @@
         thd->lex->sql_command != SQLCOM_TRUNCATE &&
         !(thd->lex->sql_command == SQLCOM_FLUSH &&
           thd->lex->type & REFRESH_LOG) &&
-        (thd != general_log_thd) && (thd != slow_log_thd) &&
+        (thd != logger.get_general_log_thd()) &&
+        (thd != logger.get_slow_log_thd()) &&
         (table_ptr[i]->reginfo.lock_type >= TL_READ_NO_INSERT))
     {
       /*

--- 1.183/sql/log.cc	2005-12-13 16:18:15 +03:00
+++ 1.184/sql/log.cc	2005-12-26 19:46:29 +03:00
@@ -39,16 +39,12 @@
 #define MAX_USER_HOST_SIZE 512
 #define MAX_TIME_SIZE 32
 
-/*
-  We create artificial THD for each of the logs. Since they are shared
-  between threads.
-*/
-THD *general_log_thd= NULL, *slow_log_thd= NULL;
-TABLE_LIST general_log, slow_log;
+/* we need this for log files intialization */
+extern char *opt_logname, *opt_slow_logname;
 
 LOGGER logger;
 
-MYSQL_LOG mysql_log, mysql_slow_log, mysql_bin_log;
+MYSQL_LOG mysql_bin_log;
 ulong sync_binlog_counter= 0;
 
 static bool test_if_number(const char *str,
@@ -117,7 +113,7 @@
     TRUE - error occured
 */
 
-bool open_log_table(uint log_type)
+bool Log_to_csv_event_handler::open_log_table(uint log_type)
 {
   THD *log_thd, *curr= current_thd;
   TABLE_LIST *table;
@@ -197,6 +193,38 @@
 }
 
 
+Log_to_csv_event_handler::Log_to_csv_event_handler()
+{
+  /* init artificial THD's */
+  general_log_thd= new THD;
+  /* logger thread always works with mysql database */
+  general_log_thd->db= my_strdup("mysql", MYF(0));
+  general_log_thd->db_length= 5;
+
+  slow_log_thd= new THD;
+  /* logger thread always works with mysql database */
+  slow_log_thd->db= my_strdup("mysql", MYF(0));;
+  slow_log_thd->db_length= 5;
+}
+
+
+Log_to_csv_event_handler::~Log_to_csv_event_handler()
+{
+  /* now cleanup the tables */
+  if (general_log_thd)
+  {
+    delete general_log_thd;
+    general_log_thd= NULL;
+  }
+
+  if (slow_log_thd)
+  {
+    delete slow_log_thd;
+    slow_log_thd= NULL;
+  }
+}
+
+
 /*
   Reopen log table of a given type
 
@@ -220,13 +248,21 @@
     TRUE - open_log_table() returned an error
 */
 
-bool reopen_log_table(uint log_type)
+bool Log_to_csv_event_handler::reopen_log_table(uint log_type)
 {
+  /* don't open the log table, if it wasn't enabled during startup */
   if (!logger.is_log_tables_initialized)
     return FALSE;
   return open_log_table(log_type);
 }
 
+void Log_to_csv_event_handler::cleanup()
+{
+  close_log_table(LOG_GENERAL, FALSE);
+  close_log_table(LOG_SLOW, FALSE);
+  logger.is_log_tables_initialized= FALSE;
+}
+
 /* log event handlers */
 
 /*
@@ -254,10 +290,11 @@
     TRUE - error occured
 */
 
-static bool log_general_to_csv(time_t event_time, const char *user_host,
-                               uint user_host_len, int thread_id,
-                               const char *command_type, uint command_type_len,
-                               const char *sql_text, uint sql_text_len)
+bool Log_to_csv_event_handler::
+  log_general(time_t event_time, const char *user_host,
+              uint user_host_len, int thread_id,
+              const char *command_type, uint command_type_len,
+              const char *sql_text, uint sql_text_len)
 {
   TABLE *table= general_log.table;
 
@@ -308,10 +345,11 @@
     TRUE - error occured
 */
 
-bool log_slow_to_csv(THD *thd, time_t current_time, time_t query_start_arg,
-                     const char *user_host, uint user_host_len,
-                     longlong query_time, longlong lock_time, bool is_command,
-                     const char *sql_text, uint sql_text_len)
+bool Log_to_csv_event_handler::
+  log_slow(THD *thd, time_t current_time, time_t query_start_arg,
+           const char *user_host, uint user_host_len,
+           longlong query_time, longlong lock_time, bool is_command,
+           const char *sql_text, uint sql_text_len)
 {
   /* table variables */
   TABLE *table= slow_log.table;
@@ -382,19 +420,35 @@
   DBUG_RETURN(0);
 }
 
-int log_error_old(enum loglevel level, const char *format,
-                  va_list args)
+bool Log_to_csv_event_handler::
+  log_error(enum loglevel level, const char *format, va_list args)
+{
+  /* No log table is implemented */
+  DBUG_ASSERT(0);
+  return FALSE;
+}
+
+bool Log_to_file_event_handler::
+  log_error(enum loglevel level, const char *format,
+            va_list args)
 {
   return vprint_msg_to_log(level, format, args);
 }
 
+void Log_to_file_event_handler::init_pthread_objects()
+{
+  mysql_log.init_pthread_objects();
+  mysql_slow_log.init_pthread_objects();
+}
+
 
 /* Wrapper around MYSQL_LOG::write() for slow log */
 
-bool log_slow_old(THD *thd, time_t current_time, time_t query_start_arg,
-                  const char *user_host, uint user_host_len,
-                  longlong query_time, longlong lock_time, bool is_command,
-                  const char *sql_text, uint sql_text_len)
+bool Log_to_file_event_handler::
+  log_slow(THD *thd, time_t current_time, time_t query_start_arg,
+           const char *user_host, uint user_host_len,
+           longlong query_time, longlong lock_time, bool is_command,
+           const char *sql_text, uint sql_text_len)
 {
   return mysql_slow_log.write(thd, current_time, query_start_arg,
                               user_host, user_host_len,
@@ -408,16 +462,48 @@
    want all log event handlers to have the same signature.
 */
 
-bool log_general_old(time_t event_time, const char *user_host,
-                     uint user_host_len, int thread_id,
-                     const char *command_type, uint command_type_len,
-                     const char *sql_text, uint sql_text_len)
+bool Log_to_file_event_handler::
+  log_general(time_t event_time, const char *user_host,
+              uint user_host_len, int thread_id,
+              const char *command_type, uint command_type_len,
+              const char *sql_text, uint sql_text_len)
 {
   return mysql_log.write(event_time, user_host, user_host_len,
                          thread_id, command_type, command_type_len,
                          sql_text, sql_text_len);
 }
 
+
+bool Log_to_file_event_handler::init()
+{
+  if (!is_initialized)
+  {
+    if (opt_slow_log)
+      mysql_slow_log.open_slow_log(opt_slow_logname);
+
+    if (opt_log)
+      mysql_log.open_query_log(opt_logname);
+
+    is_initialized= TRUE;
+  }
+
+  return FALSE;
+}
+
+
+void Log_to_file_event_handler::cleanup()
+{
+  mysql_log.cleanup();
+  mysql_slow_log.cleanup();
+}
+
+void Log_to_file_event_handler::flush()
+{
+  /* reopen log files */
+  mysql_log.new_file(1);
+  mysql_slow_log.new_file(1);
+}
+
 /*
   Log error with all enabled log event handlers
 
@@ -435,17 +521,104 @@
 */
 
 bool LOGGER::error_log_print(enum loglevel level, const char *format,
-                            va_list args)
+                             va_list args)
 {
-  uint error= 0;
-  error_log_handler *current_handler= error_log_print_list;
+  bool error= FALSE;
+  Log_event_handler **current_handler= error_log_handler_list;
 
   /* currently we don't need locking here as there is no error_log table */
-
   while (*current_handler)
-    error+= (*current_handler++)(level, format, args);
+    error= (*current_handler++)->log_error(level, format, args) || error;
+
+  return error;
+}
+
+
+void LOGGER::cleanup()
+{
+  DBUG_ASSERT(inited == 1);
+  (void) pthread_mutex_destroy(&LOCK_logger);
+  table_log_handler->cleanup();
+  file_log_handler->cleanup();
+}
+
+
+void LOGGER::close_log_table(uint log_type, bool lock_in_use)
+{
+  table_log_handler->close_log_table(log_type, lock_in_use);
+}
+
+
+void LOGGER::init_base()
+{
+  DBUG_ASSERT(inited == 0);
+  inited= 1;
+
+  /*
+    Here we create file log handler. We don't do it for the table log handler
+    here as it cannot be created so early. The reason is THD initialization,
+    which depends on the system variables (parsed later).
+  */
+  if (!file_log_handler)
+    file_log_handler= new Log_to_file_event_handler;
+
+  file_log_handler->init_pthread_objects();
+  (void) pthread_mutex_init(&LOCK_logger, MY_MUTEX_INIT_SLOW);
+}
+
+
+void LOGGER::init_log_tables()
+{
+  if (!table_log_handler)
+    table_log_handler= new Log_to_csv_event_handler;
+
+  if (!is_log_tables_initialized &&
+      !table_log_handler->init() && !file_log_handler->init())
+    is_log_tables_initialized= TRUE;
+}
 
-  return error != 0;
+
+bool LOGGER::reopen_log_table(uint log_type)
+{
+  return table_log_handler->reopen_log_table(log_type);
+}
+
+
+bool LOGGER::flush_logs(THD *thd)
+{
+  TABLE_LIST close_slow_log, close_general_log;
+
+  /* reopen log tables */
+  bzero((char*) &close_slow_log, sizeof(TABLE_LIST));
+  close_slow_log.alias= close_slow_log.table_name=(char*) "slow_log";
+  close_slow_log.table_name_length= 8;
+  close_slow_log.db= (char*) "mysql";
+  close_slow_log.db_length= 5;
+
+  bzero((char*) &close_general_log, sizeof(TABLE_LIST));
+  close_general_log.alias= close_general_log.table_name=(char*) "general_log";
+  close_general_log.table_name_length= 11;
+  close_general_log.db= (char*) "mysql";
+  close_general_log.db_length= 5;
+
+  /* reopen log files */
+  file_log_handler->flush();
+
+  /*
+    this will lock and wait for all but the logger thread to release the
+    tables. Then we could reopen log tables. Then release the name locks.
+  */
+  lock_and_wait_for_table_name(thd, &close_slow_log);
+  lock_and_wait_for_table_name(thd, &close_general_log);
+
+  /* deny others from logging to general and slow log, while reopening tables */
+  logger.lock();
+
+  table_log_handler->flush(thd, &close_slow_log, &close_general_log);
+
+  /* end of log tables flush */
+  logger.unlock();
+  return FALSE;
 }
 
 
@@ -468,8 +641,8 @@
 bool LOGGER::slow_log_print(THD *thd, const char *query, uint query_length,
                             time_t query_start_arg)
 {
-  uint error= 0;
-  slow_log_handler *current_handler= slow_log_print_list;
+  bool error= FALSE;
+  Log_event_handler **current_handler= slow_log_handler_list;
   bool is_command= FALSE;
 
   char message_buff[MAX_LOG_BUFFER_SIZE];
@@ -481,61 +654,69 @@
   longlong query_time= 0, lock_time= 0;
   longlong last_insert_id= 0, insert_id= 0;
 
-  current_time= time(NULL);
+  /*
+    Print the message to the buffer if we have slow log enabled
+  */
 
-  if (!(thd->options & OPTION_UPDATE_LOG))
-    return 0;
+  if (*slow_log_handler_list)
+  {
+    current_time= time(NULL);
 
+    if (!(thd->options & OPTION_UPDATE_LOG))
+      return 0;
 
-  lock();
+    lock();
 
-  /* fill in user_host value: the format is "%s[%s] @ %s [%s]" */
-  user_host_len= strxnmov(user_host_buff, MAX_USER_HOST_SIZE,
-                          sctx->priv_user ? sctx->priv_user : "", "[",
-                          sctx->user ? sctx->user : "", "] @ ",
-                          sctx->host ? sctx->host : "", " [",
-                          sctx->ip ? sctx->ip : "", "]", NullS) -
-                                                            user_host_buff;
+    /* fill in user_host value: the format is "%s[%s] @ %s [%s]" */
+    user_host_len= strxnmov(user_host_buff, MAX_USER_HOST_SIZE,
+                            sctx->priv_user ? sctx->priv_user : "", "[",
+                            sctx->user ? sctx->user : "", "] @ ",
+                            sctx->host ? sctx->host : "", " [",
+                            sctx->ip ? sctx->ip : "", "]", NullS) -
+      user_host_buff;
 
-  if (query_start_arg)
-  {
-    query_time= (longlong) (current_time - query_start_arg);
-    lock_time= (longlong) (thd->time_after_lock - query_start_arg);
-  }
+    if (query_start_arg)
+    {
+      query_time= (longlong) (current_time - query_start_arg);
+      lock_time= (longlong) (thd->time_after_lock - query_start_arg);
+    }
 
-  if (thd->last_insert_id_used)
-    last_insert_id= (longlong) thd->current_insert_id;
+    if (thd->last_insert_id_used)
+      last_insert_id= (longlong) thd->current_insert_id;
 
-  /* set value if we do an insert on autoincrement column */
-  if (thd->insert_id_used)
-    insert_id= (longlong) thd->last_insert_id;
+    /* set value if we do an insert on autoincrement column */
+    if (thd->insert_id_used)
+      insert_id= (longlong) thd->last_insert_id;
 
-  if (!query)
-  {
-    is_command= TRUE;
-    query= command_name[thd->command].str;
-    query_length= command_name[thd->command].length;
-  }
+    if (!query)
+    {
+      is_command= TRUE;
+      query= command_name[thd->command].str;
+      query_length= command_name[thd->command].length;
+    }
 
-  while (*current_handler)
-    error+= (*current_handler++)(thd, current_time, query_start_arg,
-                                 user_host_buff, user_host_len, query_time,
-                                 lock_time, is_command,
-                                 query, query_length);
+    while (*current_handler)
+      error= (*current_handler++)->log_slow(thd, current_time, query_start_arg,
+                                            user_host_buff, user_host_len,
+                                            query_time, lock_time, is_command,
+                                            query, query_length) || error;
 
-  unlock();
-  return error != 0;
+    unlock();
+  }
+  return error;
 }
 
 bool LOGGER::general_log_print(THD *thd, enum enum_server_command command,
                                const char *format, va_list args)
 {
-  uint error= 0;
-  general_log_handler *current_handler= general_log_print_list;
-
-  /* print the message to the buffer */
+  bool error= FALSE;
+  Log_event_handler **current_handler= general_log_handler_list;
 
-  if (what_to_log & (1L << (uint) command))
+  /*
+    Print the message to the buffer if we have at least one log event handler
+    enabled and want to log this king of commands
+  */
+  if (*general_log_handler_list && (what_to_log & (1L << (uint) command)))
   {
     char message_buff[MAX_LOG_BUFFER_SIZE];
     char user_host_buff[MAX_USER_HOST_SIZE];
@@ -576,25 +757,26 @@
       message_buff[0]= '\0';
 
     while (*current_handler)
-      error+= (*current_handler++) (current_time, user_host_buff,
-                                    user_host_len, id,
-                                    command_name[(uint) command].str,
-                                    command_name[(uint) command].length,
-                                    message_buff, message_buff_len);
+      error+= (*current_handler++)->
+        log_general(current_time, user_host_buff,
+                   user_host_len, id,
+                   command_name[(uint) command].str,
+                   command_name[(uint) command].length,
+                   message_buff, message_buff_len) || error;
     unlock();
   }
-  return error != 0;
+  return error;
 }
 
 void LOGGER::init_error_log(enum enum_printer error_log_printer)
 {
   switch (error_log_printer) {
   case NONE:
-    error_log_print_list[0]= 0;
+    error_log_handler_list[0]= 0;
     break;
   case LEGACY:
-    error_log_print_list[0]= vprint_msg_to_log;
-    error_log_print_list[1]= 0;
+    error_log_handler_list[0]= file_log_handler;
+    error_log_handler_list[1]= 0;
     break;
     /* these two are disabled for now */
   case CSV:
@@ -610,20 +792,20 @@
 {
   switch (slow_log_printer) {
   case NONE:
-    slow_log_print_list[0]= 0;
+    slow_log_handler_list[0]= 0;
     break;
   case LEGACY:
-    slow_log_print_list[0]= log_slow_old;
-    slow_log_print_list[1]= 0;
+    slow_log_handler_list[0]= file_log_handler;
+    slow_log_handler_list[1]= 0;
     break;
   case CSV:
-    slow_log_print_list[0]= log_slow_to_csv;
-    slow_log_print_list[1]= 0;
+    slow_log_handler_list[0]= table_log_handler;
+    slow_log_handler_list[1]= 0;
     break;
   case LEGACY_AND_CSV:
-    slow_log_print_list[0]= log_slow_old;
-    slow_log_print_list[1]= log_slow_to_csv;
-    slow_log_print_list[2]= 0;
+    slow_log_handler_list[0]= file_log_handler;
+    slow_log_handler_list[1]= table_log_handler;
+    slow_log_handler_list[2]= 0;
     break;
   }
 }
@@ -632,66 +814,66 @@
 {
   switch (general_log_printer) {
   case NONE:
-    general_log_print_list[0]= 0;
+    general_log_handler_list[0]= 0;
     break;
   case LEGACY:
-    general_log_print_list[0]= log_general_old;
-    general_log_print_list[1]= 0;
+    general_log_handler_list[0]= file_log_handler;
+    general_log_handler_list[1]= 0;
     break;
   case CSV:
-    general_log_print_list[0]= log_general_to_csv;
-    general_log_print_list[1]= 0;
+    general_log_handler_list[0]= table_log_handler;
+    general_log_handler_list[1]= 0;
     break;
   case LEGACY_AND_CSV:
-    general_log_print_list[0]= log_general_old;
-    general_log_print_list[1]= log_general_to_csv;
-    general_log_print_list[2]= 0;
+    general_log_handler_list[0]= file_log_handler;
+    general_log_handler_list[1]= table_log_handler;
+    general_log_handler_list[2]= 0;
     break;
   }
 }
 
 
-int LOGGER::init(enum enum_printer error_log_printer,
-                 enum enum_printer slow_log_printer,
-                 enum enum_printer general_log_printer)
+bool Log_to_csv_event_handler::flush(THD *thd, TABLE_LIST *close_slow_log,
+                                     TABLE_LIST *close_general_log)
+{
+  VOID(pthread_mutex_lock(&LOCK_open));
+  close_log_table(LOG_GENERAL, TRUE);
+  close_log_table(LOG_SLOW, TRUE);
+  close_general_log->next_local= close_slow_log;
+  query_cache_invalidate3(thd, close_general_log, 0);
+  unlock_table_name(thd, close_slow_log);
+  unlock_table_name(thd, close_general_log);
+  VOID(pthread_mutex_unlock(&LOCK_open));
+  return reopen_log_table(LOG_SLOW) || reopen_log_table(LOG_GENERAL);
+}
+
+/* the parameters are unused for the log tables */
+bool Log_to_csv_event_handler::init()
+{
+  /* we always open log tables. even if the logging is disabled */
+  return (open_log_table(LOG_GENERAL) || open_log_table(LOG_SLOW));
+}
+
+int LOGGER::set_handlers(enum enum_printer error_log_printer,
+                         enum enum_printer slow_log_printer,
+                         enum enum_printer general_log_printer)
 {
   /* error log table is not supported yet */
-  DBUG_ASSERT(error_log_printer != CSV);
+  DBUG_ASSERT(error_log_printer < CSV);
 
   lock();
 
-  init_error_log(error_log_printer);
-
-  if (slow_log_printer >= CSV || general_log_printer >= CSV)
+  if ((slow_log_printer >= CSV || general_log_printer >= CSV) &&
+      !is_log_tables_initialized)
   {
-    if (!general_log_thd)
-    {
-      general_log_thd= new THD;
-      /* logger thread always works with mysql database */
-      general_log_thd->db= my_strdup("mysql", MYF(0));
-      general_log_thd->db_length= 5;
-    }
+    slow_log_printer= LEGACY;
+    general_log_printer= LEGACY;
 
-    if (!slow_log_thd)
-    {
-      slow_log_thd= new THD;
-      /* logger thread always works with mysql database */
-      slow_log_thd->db= my_strdup("mysql", MYF(0));;
-      slow_log_thd->db_length= 5;
-    }
-
-    if (!is_log_tables_initialized && general_log_thd && slow_log_thd &&
-        !open_log_table(LOG_GENERAL) && !open_log_table(LOG_SLOW))
-      is_log_tables_initialized= TRUE;
-    else
-    {
-      sql_print_error("Failed to initialize log tables. "
-                      "Falling back to the old-fashioned logs");
-      /* fallback to old-fashioned logging routines */
-      slow_log_printer= LEGACY;
-      general_log_printer= LEGACY;
-    }
+    sql_print_error("Failed to initialize log tables. "
+                    "Falling back to the old-fashioned logs");
   }
+
+  init_error_log(error_log_printer);
   init_slow_log(slow_log_printer);
   init_general_log(general_log_printer);
 
@@ -717,7 +899,8 @@
     executed) or (2) during shutdown.
 */
 
-void close_log_table(uint log_type, bool lock_in_use)
+void Log_to_csv_event_handler::
+  close_log_table(uint log_type, bool lock_in_use)
 {
   THD *log_thd, *curr= current_thd;
   TABLE_LIST *table;
@@ -761,43 +944,6 @@
     my_pthread_setspecific_ptr(THR_THD,  0);
     my_pthread_setspecific_ptr(THR_MALLOC, 0);
   }
-}
-
-
-/*
-  Close opened log tables and cleanup artifitial log THDs if needed.
-
-  SYNOPSIS
-    mysql_close_log_tables()
-
-  DESCRIPTION
-
-    The function is called during shutdown: we always open all log tables at
-    startup (even if they are not used for logging). Therefore we should close
-    them at shutdown. We also need to free memory consumed by artificial
-    log THDs if they were created.
-*/
-
-void mysql_close_log_tables()
-{
-  logger.lock();
-  close_log_table(LOG_GENERAL, FALSE);
-  close_log_table(LOG_SLOW, FALSE);
-  logger.is_log_tables_initialized= FALSE;
-
-  /* now cleanup the tables */
-  if (general_log_thd)
-  {
-    delete general_log_thd;
-    general_log_thd= NULL;
-  }
-
-  if (slow_log_thd)
-  {
-    delete slow_log_thd;
-    slow_log_thd= NULL;
-  }
-  logger.unlock();
 }
 
 

--- 1.354/sql/mysql_priv.h	2005-12-12 12:10:06 +03:00
+++ 1.355/sql/mysql_priv.h	2005-12-26 19:46:29 +03:00
@@ -1120,15 +1120,6 @@
 #define LOG_SLOW 1
 #define LOG_GENERAL 2
 
-int mysql_init_logs(enum enum_printer error_log_printer,
-                    enum enum_printer slow_log_printer,
-                    enum enum_printer general_log_printer);
-void mysql_init_logger();
-void mysql_cleanup_logger();
-void mysql_close_log_tables();
-bool reopen_log_table(uint log_type);
-void close_log_table(uint log_type, bool lock_in_use);
-
 int error_log_print(enum loglevel level, const char *format,
                     va_list args);
 
@@ -1253,8 +1244,7 @@
 extern my_bool opt_large_pages;
 extern uint opt_large_page_size;
 
-extern MYSQL_LOG mysql_log,mysql_slow_log,mysql_bin_log;
-extern THD *general_log_thd, *slow_log_thd;
+extern MYSQL_LOG mysql_bin_log;
 extern LOGGER logger;
 extern TABLE_LIST general_log, slow_log;
 extern FILE *bootstrap_file;

--- 1.503/sql/mysqld.cc	2005-12-13 16:18:15 +03:00
+++ 1.504/sql/mysqld.cc	2005-12-26 19:46:30 +03:00
@@ -570,6 +570,7 @@
 my_bool master_ssl;
 char *master_ssl_key, *master_ssl_cert;
 char *master_ssl_ca, *master_ssl_capath, *master_ssl_cipher;
+char *opt_logname, *opt_slow_logname;
 
 /* Static variables */
 
@@ -577,8 +578,8 @@
 static my_bool opt_do_pstack, opt_bootstrap, opt_myisam_log;
 static int cleanup_done;
 static ulong opt_specialflag, opt_myisam_block_size;
-static char *opt_logname, *opt_update_logname, *opt_binlog_index_name;
-static char *opt_slow_logname, *opt_tc_heuristic_recover;
+static char *opt_update_logname, *opt_binlog_index_name;
+static char *opt_tc_heuristic_recover;
 static char *mysql_home_ptr, *pidfile_name_ptr;
 static char **defaults_argv;
 static char *opt_bin_logname;
@@ -1105,8 +1106,7 @@
   if (cleanup_done++)
     return; /* purecov: inspected */
 
-  mysql_log.cleanup();
-  mysql_slow_log.cleanup();
+  logger.cleanup();
   mysql_bin_log.cleanup();
 
 #ifdef HAVE_REPLICATION
@@ -2327,12 +2327,10 @@
 #ifdef EXTRA_DEBUG
       sql_print_information("Got signal %d to shutdown mysqld",sig);
 #endif
-      /* close log tables, since we cannot use them during shutdown */
-      mysql_close_log_tables();
       /* switch to the old log message processing */
-      logger.init(opt_error_log ? LEGACY:NONE,
-                  opt_slow_log ? LEGACY:NONE,
-                  opt_log ? LEGACY:NONE);
+      logger.set_handlers(opt_error_log ? LEGACY:NONE,
+                          opt_slow_log ? LEGACY:NONE,
+                          opt_log ? LEGACY:NONE);
       DBUG_PRINT("info",("Got signal: %d  abort_loop: %d",sig,abort_loop));
       if (!abort_loop)
       {
@@ -2361,9 +2359,9 @@
 			     (TABLE_LIST*) 0, &not_used); // Flush logs
       }
       /* reenable logs after the options were reloaded */
-      logger.init(opt_error_log ? LEGACY:NONE,
-                  opt_slow_log ? CSV:NONE,
-                  opt_log ? CSV:NONE);
+      logger.set_handlers(opt_error_log ? LEGACY:NONE,
+                          opt_slow_log ? CSV:NONE,
+                          opt_log ? CSV:NONE);
       break;
 #ifdef USE_ONE_SIGNAL_HAND
     case THR_SERVER_ALARM:
@@ -2628,8 +2626,6 @@
     global MYSQL_LOGs in their constructors, because then they would be inited
     before MY_INIT(). So we do it here.
   */
-  mysql_log.init_pthread_objects();
-  mysql_slow_log.init_pthread_objects();
   mysql_bin_log.init_pthread_objects();
   
   if (gethostname(glob_hostname,sizeof(glob_hostname)-4) < 0)
@@ -2996,33 +2992,27 @@
     }
   }
 
-  /* open old-fashioned slow log */
-  if (opt_slow_log)
-    mysql_slow_log.open_slow_log(opt_slow_logname);
-
-  /* open old-fashioned general log */
-  if (opt_log)
-    mysql_log.open_query_log(opt_logname);
-
 #ifdef WITH_CSV_STORAGE_ENGINE
+  logger.init_log_tables();
+
   if (opt_old_log_format || (have_csv_db != SHOW_OPTION_YES))
-    logger.init(opt_error_log ? LEGACY:NONE,
-                opt_slow_log ? LEGACY:NONE,
-                opt_log ? LEGACY:NONE);
+    logger.set_handlers(opt_error_log ? LEGACY:NONE,
+                        opt_slow_log ? LEGACY:NONE,
+                        opt_log ? LEGACY:NONE);
   else
     if (opt_both_log_formats)
-      logger.init(opt_error_log ? LEGACY:NONE,
-                  opt_slow_log ? LEGACY_AND_CSV:NONE,
-                  opt_log ? LEGACY_AND_CSV:NONE);
+      logger.set_handlers(opt_error_log ? LEGACY:NONE,
+                          opt_slow_log ? LEGACY_AND_CSV:NONE,
+                          opt_log ? LEGACY_AND_CSV:NONE);
     else
       /* the default is CSV log tables */
-      logger.init(opt_error_log ? LEGACY:NONE,
-                  opt_slow_log ? CSV:NONE,
-                  opt_log ? CSV:NONE);
+      logger.set_handlers(opt_error_log ? LEGACY:NONE,
+                          opt_slow_log ? CSV:NONE,
+                          opt_log ? CSV:NONE);
 #else
-  logger.init(opt_error_log ? LEGACY:NONE,
-              opt_slow_log ? LEGACY:NONE,
-              opt_log ? LEGACY:NONE);
+  logger.set_handlers(opt_error_log ? LEGACY:NONE,
+                      opt_slow_log ? LEGACY:NONE,
+                      opt_log ? LEGACY:NONE);
 #endif
 
   if (opt_update_log)
@@ -3339,10 +3329,6 @@
 #endif
 {
   DEBUGGER_OFF;
-  /* init log mutexes */
-  logger.init_pthread_objects();
-  /* Set logs to legacy during startup. In fact this affects only error log */
-  logger.init(LEGACY, LEGACY, LEGACY);
 
   rpl_filter= new Rpl_filter;
   binlog_filter= new Rpl_filter;
@@ -3354,6 +3340,12 @@
 
   MY_INIT(argv[0]);		// init my_sys library & pthreads
 
+  /*
+    Perform basic logger initialization logger. Should be called after
+    MY_INIT, as it initializes mutexes. Log tables are inited later.
+  */
+  logger.init_base();
+
 #ifdef _CUSTOMSTARTUPCONFIG_
   if (_cust_check_startup())
   {
@@ -3494,6 +3486,7 @@
   */
   error_handler_hook= my_message_sql;
   start_signal_handler();				// Creates pidfile
+
   if (acl_init(opt_noacl) ||
       my_tz_init((THD *)0, default_tz_name, opt_bootstrap))
   {
@@ -3615,8 +3608,6 @@
   clean_up_mutexes();
   my_end(opt_endinfo ? MY_CHECK_ERROR | MY_GIVE_INFO : 0);
 
-  /* should be called last as we want to log errors to the very last moment */
-  logger.cleanup();
   exit(0);
   return(0);					/* purecov: deadcode */
 }

--- 1.273/sql/sql_class.h	2005-12-13 16:18:16 +03:00
+++ 1.274/sql/sql_class.h	2005-12-26 19:46:31 +03:00
@@ -173,23 +173,6 @@
 #define RP_LOCK_LOG_IS_ALREADY_LOCKED 1
 #define RP_FORCE_ROTATE               2
 
-/* typedefs for log processing functions */
-typedef int (*error_log_handler)(enum loglevel level, const char *format,
-                                  va_list args);
-
-typedef bool (*slow_log_handler)(THD *thd, time_t current_time,
-                                 time_t query_start_arg, const char *user_host,
-                                 uint user_host_len, longlong query_time,
-                                 longlong lock_time, bool is_command,
-                                 const char *sql_text, uint sql_text_len);
-
-typedef bool (*general_log_handler)(time_t event_time,
-                                    const char *user_host,
-                                    uint user_host_len, int thread_id,
-                                    const char *command_type,
-                                    uint command_type_len,
-                                    const char *sql_text, uint sql_text_len);
-
 /*
   Currently we have only 3 kinds of logging functions: old-fashioned
   logs, stdout and csv logging routines.
@@ -204,45 +187,6 @@
   LEGACY_AND_CSV
 };
 
-/* Class which manages the log tables */
-class LOGGER
-{
-  pthread_mutex_t LOCK_logger;
-  /* flag to check whether logger mutex is initialized */
-  uint inited;
-  /* NULL-terminated arrays of logging functions*/
-  error_log_handler error_log_print_list[MAX_LOG_HANDLERS_NUM + 1];
-  slow_log_handler slow_log_print_list[MAX_LOG_HANDLERS_NUM + 1];
-  general_log_handler general_log_print_list[MAX_LOG_HANDLERS_NUM + 1];
-
-public:
-
-  bool is_log_tables_initialized;
-
-  LOGGER() : inited(0), is_log_tables_initialized(FALSE)
-  {}
-  void lock() { (void) pthread_mutex_lock(&LOCK_logger); }
-  void unlock() { (void) pthread_mutex_unlock(&LOCK_logger); }
-  void init_pthread_objects()
-  {
-    DBUG_ASSERT(inited == 0);
-    inited= 1;
-    (void) pthread_mutex_init(&LOCK_logger, MY_MUTEX_INIT_SLOW);
-  }
-  void cleanup() { (void) pthread_mutex_destroy(&LOCK_logger); }
-  bool error_log_print(enum loglevel level, const char *format,
-                      va_list args);
-  bool slow_log_print(THD *thd, const char *query, uint query_length,
-                      time_t query_start_arg);
-  bool general_log_print(THD *thd,enum enum_server_command command,
-                         const char *format, va_list args);
-  int init(enum enum_printer error_log_printer,
-           enum enum_printer slow_log_printer,
-           enum enum_printer general_log_printer);
-  void init_error_log(enum enum_printer error_log_printer);
-  void init_slow_log(enum enum_printer slow_log_printer);
-  void init_general_log(enum enum_printer general_log_printer);
-};
 
 class Log_event;
 
@@ -434,6 +378,7 @@
 };
 
 
+
 typedef struct st_copy_info {
   ha_rows records;
   ha_rows deleted;
@@ -1688,6 +1633,155 @@
   void restore_sub_statement_state(Sub_statement_state *backup);
   void set_n_backup_active_arena(Query_arena *set, Query_arena *backup);
   void restore_active_arena(Query_arena *set, Query_arena *backup);
+};
+
+
+class Log_event_handler
+{
+public:
+  virtual bool init()= 0;
+  virtual void cleanup()= 0;
+
+  virtual bool log_slow(THD *thd, time_t current_time,
+                        time_t query_start_arg, const char *user_host,
+                        uint user_host_len, longlong query_time,
+                        longlong lock_time, bool is_command,
+                        const char *sql_text, uint sql_text_len)= 0;
+  virtual bool log_error(enum loglevel level, const char *format,
+                         va_list args)= 0;
+  virtual bool log_general(time_t event_time, const char *user_host,
+                           uint user_host_len, int thread_id,
+                           const char *command_type, uint command_type_len,
+                           const char *sql_text, uint sql_text_len)= 0;
+  virtual ~Log_event_handler() {}
+};
+
+
+class Log_to_csv_event_handler: public Log_event_handler
+{
+  /*
+    We create artificial THD for each of the logs. This is to avoid
+    locking issues: we don't want locks on the log tables reside in the
+    THD's of the query. The reason is the locking order and duration.
+  */
+  THD *general_log_thd, *slow_log_thd;
+  friend class LOGGER;
+  TABLE_LIST general_log, slow_log;
+
+private:
+  bool open_log_table(uint log_type);
+
+public:
+  Log_to_csv_event_handler();
+  ~Log_to_csv_event_handler();
+  virtual bool init();
+  virtual void cleanup();
+
+  virtual bool log_slow(THD *thd, time_t current_time,
+                        time_t query_start_arg, const char *user_host,
+                        uint user_host_len, longlong query_time,
+                        longlong lock_time, bool is_command,
+                        const char *sql_text, uint sql_text_len);
+  virtual bool log_error(enum loglevel level, const char *format,
+                         va_list args);
+  virtual bool log_general(time_t event_time, const char *user_host,
+                           uint user_host_len, int thread_id,
+                           const char *command_type, uint command_type_len,
+                           const char *sql_text, uint sql_text_len);
+  bool flush(THD *thd, TABLE_LIST *close_slow_Log,
+             TABLE_LIST* close_general_log);
+  void close_log_table(uint log_type, bool lock_in_use);
+  bool reopen_log_table(uint log_type);
+};
+
+
+class Log_to_file_event_handler: public Log_event_handler
+{
+  MYSQL_LOG mysql_log, mysql_slow_log;
+  bool is_initialized;
+public:
+  Log_to_file_event_handler(): is_initialized(FALSE)
+  {}
+  virtual bool init();
+  virtual void cleanup();
+
+  virtual bool log_slow(THD *thd, time_t current_time,
+                        time_t query_start_arg, const char *user_host,
+                        uint user_host_len, longlong query_time,
+                        longlong lock_time, bool is_command,
+                        const char *sql_text, uint sql_text_len);
+  virtual bool log_error(enum loglevel level, const char *format,
+                         va_list args);
+  virtual bool log_general(time_t event_time, const char *user_host,
+                           uint user_host_len, int thread_id,
+                           const char *command_type, uint command_type_len,
+                           const char *sql_text, uint sql_text_len);
+  void flush();
+  void init_pthread_objects();
+};
+
+
+/* Class which manages slow, general and error log event handlers */
+class LOGGER
+{
+  pthread_mutex_t LOCK_logger;
+  /* flag to check whether logger mutex is initialized */
+  uint inited;
+
+  /* available log handlers */
+  Log_to_csv_event_handler *table_log_handler;
+  Log_to_file_event_handler *file_log_handler;
+
+  /* NULL-terminated arrays of log handlers */
+  Log_event_handler *error_log_handler_list[MAX_LOG_HANDLERS_NUM + 1];
+  Log_event_handler *slow_log_handler_list[MAX_LOG_HANDLERS_NUM + 1];
+  Log_event_handler *general_log_handler_list[MAX_LOG_HANDLERS_NUM + 1];
+
+public:
+
+  bool is_log_tables_initialized;
+
+  LOGGER() : inited(0), table_log_handler(NULL),
+             file_log_handler(NULL), is_log_tables_initialized(FALSE)
+   /* by default we use traditional error log */
+  { init_error_log(LEGACY); }
+  void lock() { (void) pthread_mutex_lock(&LOCK_logger); }
+  void unlock() { (void) pthread_mutex_unlock(&LOCK_logger); }
+  /*
+    We want to initialize all log mutexes as soon as possible,
+    but we cannot do it in constructor, as safe_mutex relies on
+    initialization, performed by MY_INIT(). This why this is done in
+    this function.
+  */
+  void init_base();
+  void init_log_tables();
+  bool flush_logs(THD *thd);
+  THD *get_general_log_thd()
+  {
+    return (THD *) table_log_handler->general_log_thd;
+  }
+  THD *get_slow_log_thd()
+  {
+    return (THD *) table_log_handler->slow_log_thd;
+  }
+  void cleanup();
+  bool error_log_print(enum loglevel level, const char *format,
+                      va_list args);
+  bool slow_log_print(THD *thd, const char *query, uint query_length,
+                      time_t query_start_arg);
+  bool general_log_print(THD *thd,enum enum_server_command command,
+                         const char *format, va_list args);
+
+  void close_log_table(uint log_type, bool lock_in_use);
+  bool reopen_log_table(uint log_type);
+
+  /* we use this function to setup all enabled log event handlers */
+  int set_handlers(enum enum_printer error_log_printer,
+                   enum enum_printer slow_log_printer,
+                   enum enum_printer general_log_printer);
+  void init_error_log(enum enum_printer error_log_printer);
+  void init_slow_log(enum enum_printer slow_log_printer);
+  void init_general_log(enum enum_printer general_log_printer);
 };
 
 

--- 1.160/sql/sql_delete.cc	2005-12-05 22:54:18 +03:00
+++ 1.161/sql/sql_delete.cc	2005-12-26 19:46:31 +03:00
@@ -866,12 +866,11 @@
         || thd->lex->sphead)
       goto trunc_by_del;
 
-    /* lock logger as we may need to deal with log tables */
     if (lock_and_wait_for_table_name(thd, table_list))
       DBUG_RETURN(TRUE);
   }
 
-  /* loop through table_list and close log tables in use */
+  /* close log tables in use */
   if (!my_strcasecmp(system_charset_info, table_list->db, "mysql"))
   {
     if (!my_strcasecmp(system_charset_info, table_list->table_name,
@@ -879,7 +878,7 @@
     {
       lock_logger= 1;
       logger.lock();
-      close_log_table(LOG_GENERAL, FALSE);
+      logger.close_log_table(LOG_GENERAL, FALSE);
       closed_log_tables= closed_log_tables | LOG_GENERAL;
     }
     else
@@ -888,7 +887,7 @@
       {
         lock_logger= 1;
         logger.lock();
-        close_log_table(LOG_SLOW, FALSE);
+        logger.close_log_table(LOG_SLOW, FALSE);
         closed_log_tables= closed_log_tables | LOG_SLOW;
       }
   }
@@ -917,10 +916,10 @@
     VOID(pthread_mutex_unlock(&LOCK_open));
 
     if (closed_log_tables & LOG_SLOW)
-      reopen_log_table(LOG_SLOW);
+      logger.reopen_log_table(LOG_SLOW);
 
     if (closed_log_tables & LOG_GENERAL)
-      reopen_log_table(LOG_GENERAL);
+      logger.reopen_log_table(LOG_GENERAL);
     if (lock_logger)
       logger.unlock();
   }

--- 1.495/sql/sql_parse.cc	2005-12-12 12:10:08 +03:00
+++ 1.496/sql/sql_parse.cc	2005-12-26 19:46:31 +03:00
@@ -6784,7 +6784,6 @@
 #endif
   if (options & REFRESH_LOG)
   {
-    TABLE_LIST close_slow_log, close_general_log;
     /*
       Flush the normal query log, the update log, the binary log,
       the slow query log, the relay log (if it exists) and the log
@@ -6798,47 +6797,15 @@
       than it would help them)
     */
     tmp_write_to_binlog= 0;
-    mysql_log.new_file(1);
-    mysql_slow_log.new_file(1);
     mysql_bin_log.rotate_and_purge(RP_FORCE_ROTATE);
 #ifdef HAVE_REPLICATION
     pthread_mutex_lock(&LOCK_active_mi);
     rotate_relay_log(active_mi);
     pthread_mutex_unlock(&LOCK_active_mi);
 #endif
-    bzero((char*) &close_slow_log, sizeof(TABLE_LIST));
-    close_slow_log.alias= close_slow_log.table_name=(char*) "slow_log";
-    close_slow_log.table_name_length= 8;
-    close_slow_log.db= (char*) "mysql";
-    close_slow_log.db_length= 5;
 
-    bzero((char*) &close_general_log, sizeof(TABLE_LIST));
-    close_general_log.alias= close_general_log.table_name=(char*) "general_log";
-    close_general_log.table_name_length= 11;
-    close_general_log.db= (char*) "mysql";
-    close_general_log.db_length= 5;
-
-    /*
-      this will lock and wait for all but the logger thread to release the
-      tables. Then we could reopen log tables. Then release the name locks.
-    */
-    lock_and_wait_for_table_name(thd, &close_slow_log);
-    lock_and_wait_for_table_name(thd, &close_general_log);
-
-    logger.lock();
-
-    VOID(pthread_mutex_lock(&LOCK_open));
-    close_log_table(LOG_GENERAL, TRUE);
-    close_log_table(LOG_SLOW, TRUE);
-    close_general_log.next_local= &close_slow_log;
-    query_cache_invalidate3(thd, &close_general_log, 0);
-    unlock_table_name(thd, &close_slow_log);
-    unlock_table_name(thd, &close_general_log);
-    VOID(pthread_mutex_unlock(&LOCK_open));
-    reopen_log_table(LOG_SLOW);
-    reopen_log_table(LOG_GENERAL);
-    /* end of log tables flush */
-    logger.unlock();
+    /* flush slow and general logs */
+    logger.flush_logs(thd);
 
     if (ha_flush_logs())
       result=1;

--- 1.6/mysql-test/t/log_tables.test	2005-12-13 16:18:15 +03:00
+++ 1.7/mysql-test/t/log_tables.test	2005-12-26 19:46:28 +03:00
@@ -33,7 +33,9 @@
 
 
 #
-# Check some basic queries interfering with the log tables
+# Check some basic queries interfering with the log tables.
+# In our test we'll use a tbale with verbose comments to the short
+# command type names, used in the tables
 #
 
 create table join_test (verbose_comment varchar (80), command_type varchar(64));
@@ -58,10 +60,10 @@
 # check locking of the log tables
 #
 
---error 1499
+--error 1500
 lock tables mysql.general_log WRITE;
 
---error 1499
+--error 1500
 lock tables mysql.slow_log WRITE;
 
 #
@@ -70,10 +72,10 @@
 # tables are always opened and locked by the logger.
 #
 
---error 1500
+--error 1501
 lock tables mysql.general_log READ;
 
---error 1500
+--error 1501
 lock tables mysql.slow_log READ;
 
 #

--- 1.155/sql/sql_prepare.cc	2005-12-05 22:54:18 +03:00
+++ 1.156/sql/sql_prepare.cc	2005-12-26 19:46:31 +03:00
@@ -2606,7 +2606,7 @@
 {
   /* Setup binary logging */
   if (mysql_bin_log.is_open() && is_update_query(lex->sql_command) ||
-      mysql_log.is_open() || mysql_slow_log.is_open())
+      opt_log || opt_slow_log)
   {
     set_params_from_vars= insert_params_from_vars_with_log;
 #ifndef EMBEDDED_LIBRARY
Thread
bk commit into 5.1 tree (petr:1.1964)Petr Chardin26 Dec