From 90cd712b84d69d64a8e6fa162b560602be87903a Mon Sep 17 00:00:00 2001 From: Rucha Deodhar Date: Mon, 13 Nov 2023 23:21:49 +0530 Subject: [PATCH] MDEV-27087: Add thread ID and database / table, where the error occured to SQL error plugin New plugin variable "with_db_and_thread_info" is added which prints the thread id and databse name to the logfile. the value is stored in variable "with_db_and_thread_info" log_sql_errors() is responsible for printing in the log. If detailed is enabled, print thread id and database name both, otherwise skip it. --- mysql-test/include/read_head.inc | 30 +++++++++++++++++++ mysql-test/suite/plugins/r/mdev_27087.result | 12 ++++++++ .../suite/plugins/r/sql_error_log.result | 1 + mysql-test/suite/plugins/t/mdev_27087.opt | 1 + mysql-test/suite/plugins/t/mdev_27087.test | 18 +++++++++++ plugin/sql_errlog/sql_errlog.c | 24 +++++++++++++-- 6 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 mysql-test/include/read_head.inc create mode 100644 mysql-test/suite/plugins/r/mdev_27087.result create mode 100644 mysql-test/suite/plugins/t/mdev_27087.opt create mode 100644 mysql-test/suite/plugins/t/mdev_27087.test diff --git a/mysql-test/include/read_head.inc b/mysql-test/include/read_head.inc new file mode 100644 index 00000000000..98818d76703 --- /dev/null +++ b/mysql-test/include/read_head.inc @@ -0,0 +1,30 @@ +# Purpose: +# Print first LINES_TO_READ from a file. +# The environment variables SEARCH_FILE and LINES_TO_READ must be set +# before sourcing this routine. +# Use: +# When the test is slow ( example because of ASAN build) then it +# may not flush the lines when 'cat' command is called and the +# test could fail with missing lines. Hence this can be used to +# to print first N lines. +# + +perl; + +use strict; + +my $search_file = $ENV{SEARCH_FILE} or die "SEARCH_FILE not set"; +my $lines_to_read = $ENV{LINES_TO_READ} or die "LINES_TO_READ not set"; + +open(FILE, '<', $search_file) or die "Can't open file $search_file: $!"; + +my $line_count = 0; +while ($line_count < $lines_to_read and my $line = ) +{ + print $line; + $line_count++; +} + +close(FILE); + +EOF diff --git a/mysql-test/suite/plugins/r/mdev_27087.result b/mysql-test/suite/plugins/r/mdev_27087.result new file mode 100644 index 00000000000..9cffceaa4db --- /dev/null +++ b/mysql-test/suite/plugins/r/mdev_27087.result @@ -0,0 +1,12 @@ +show variables like 'sql_error_log%'; +Variable_name Value +sql_error_log_filename sql_errors.log +sql_error_log_rate 1 +sql_error_log_rotate OFF +sql_error_log_rotations 9 +sql_error_log_size_limit 1000000 +sql_error_log_with_db_and_thread_info ON +set global sql_error_log_rate=1; +select * from t_doesnt_exist; +ERROR 42S02: Table 'test.t_doesnt_exist' doesn't exist +THREAD_ID DATABASE_NAME TIME HOSTNAME ERROR 1146: Table 'test.t_doesnt_exist' doesn't exist : select * from t_doesnt_exist diff --git a/mysql-test/suite/plugins/r/sql_error_log.result b/mysql-test/suite/plugins/r/sql_error_log.result index 98dfe0374fd..585457f5ee2 100644 --- a/mysql-test/suite/plugins/r/sql_error_log.result +++ b/mysql-test/suite/plugins/r/sql_error_log.result @@ -8,6 +8,7 @@ sql_error_log_rate 1 sql_error_log_rotate OFF sql_error_log_rotations 9 sql_error_log_size_limit 1000000 +sql_error_log_with_db_and_thread_info OFF set global sql_error_log_rate=1; select * from t_doesnt_exist; ERROR 42S02: Table 'test.t_doesnt_exist' doesn't exist diff --git a/mysql-test/suite/plugins/t/mdev_27087.opt b/mysql-test/suite/plugins/t/mdev_27087.opt new file mode 100644 index 00000000000..53c8ec8f812 --- /dev/null +++ b/mysql-test/suite/plugins/t/mdev_27087.opt @@ -0,0 +1 @@ +--plugin-load-add=$SQL_ERRLOG_SO --sql-error-log-with-db-and-thread-info=1 diff --git a/mysql-test/suite/plugins/t/mdev_27087.test b/mysql-test/suite/plugins/t/mdev_27087.test new file mode 100644 index 00000000000..ddd677b6681 --- /dev/null +++ b/mysql-test/suite/plugins/t/mdev_27087.test @@ -0,0 +1,18 @@ +--source include/not_embedded.inc + +if (!$SQL_ERRLOG_SO) { + skip No SQL_ERROR_LOG plugin; +} + +show variables like 'sql_error_log%'; +set global sql_error_log_rate=1; +--error ER_NO_SUCH_TABLE +select * from t_doesnt_exist; + +let $MYSQLD_DATADIR= `SELECT @@datadir`; +--let SEARCH_FILE= $MYSQLD_DATADIR/sql_errors.log +--let LINES_TO_READ=1 +--replace_regex /[1-9]* test [1-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] [ 0-9][0-9]:[0-9][0-9]:[0-9][0-9] [^E]*/THREAD_ID DATABASE_NAME TIME HOSTNAME / +--source include/read_head.inc + +remove_file $MYSQLD_DATADIR/sql_errors.log; diff --git a/plugin/sql_errlog/sql_errlog.c b/plugin/sql_errlog/sql_errlog.c index e0ebd6b7737..619dc2e227e 100644 --- a/plugin/sql_errlog/sql_errlog.c +++ b/plugin/sql_errlog/sql_errlog.c @@ -39,6 +39,7 @@ static unsigned int rate; static unsigned long long size_limit; static unsigned int rotations; static char rotate; +static char with_db_and_thread_info; static unsigned int count; LOGGER_HANDLE *logfile; @@ -67,12 +68,19 @@ static MYSQL_SYSVAR_STR(filename, filename, "The file to log sql errors to", NULL, NULL, "sql_errors.log"); +static MYSQL_SYSVAR_BOOL(with_db_and_thread_info, with_db_and_thread_info, + PLUGIN_VAR_READONLY | PLUGIN_VAR_OPCMDARG, + "Show details about thread id and database name in the log", + NULL, NULL, + 0); + static struct st_mysql_sys_var* vars[] = { MYSQL_SYSVAR(rate), MYSQL_SYSVAR(size_limit), MYSQL_SYSVAR(rotations), MYSQL_SYSVAR(rotate), MYSQL_SYSVAR(filename), + MYSQL_SYSVAR(with_db_and_thread_info), NULL }; @@ -93,12 +101,24 @@ static void log_sql_errors(MYSQL_THD thd __attribute__((unused)), count = 0; (void) localtime_r(&event_time, &t); - logger_printf(logfile, "%04d-%02d-%02d %2d:%02d:%02d " + if (with_db_and_thread_info) + { + logger_printf(logfile, "%llu %s %04d-%02d-%02d %2d:%02d:%02d " + "%s ERROR %d: %s : %s \n", + event->general_thread_id, event->database.str, t.tm_year + 1900, + t.tm_mon + 1, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec, + event->general_user, event->general_error_code, + event->general_command, event->general_query); + } + else + { + logger_printf(logfile, "%04d-%02d-%02d %2d:%02d:%02d " "%s ERROR %d: %s : %s\n", t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec, event->general_user, event->general_error_code, event->general_command, event->general_query); + } } } } @@ -157,7 +177,7 @@ maria_declare_plugin(sql_errlog) 0x0100, NULL, vars, - "1.0", + "1.1", MariaDB_PLUGIN_MATURITY_STABLE } maria_declare_plugin_end;