new configuration: audit_json_file_bufsize . Controls the buffer size used when logging to a file. Value of 0 means default size, value of 1 means no buffering.

pull/141/head
Guy Lichtman 2015-03-09 10:37:31 +02:00
parent 8d28867ee5
commit 24f157beb2
3 changed files with 38 additions and 2 deletions

View File

@ -554,7 +554,7 @@ class Audit_file_handler: public Audit_io_handler
public: public:
Audit_file_handler() : Audit_file_handler() :
m_sync_period(0), m_log_file(NULL), m_sync_counter(0) m_sync_period(0), m_log_file(NULL), m_sync_counter(0), m_bufsize(0)
{ {
m_io_type = "file"; m_io_type = "file";
} }
@ -571,6 +571,11 @@ public:
*/ */
unsigned int m_sync_period; unsigned int m_sync_period;
/**
* The buf size used by the file stream. 0 = use default, negative or 1 = no buffering
*/
long m_bufsize;
/** /**
* Write function we pass to formatter * Write function we pass to formatter
*/ */

View File

@ -21,6 +21,7 @@
#include "audit_handler.h" #include "audit_handler.h"
//for definition of sockaddr_un //for definition of sockaddr_un
#include <sys/un.h> #include <sys/un.h>
#include <stdio_ext.h>
#include "static_assert.h" #include "static_assert.h"
//utility macro to log also with a date as a prefix //utility macro to log also with a date as a prefix
@ -198,7 +199,7 @@ int Audit_file_handler::open(const char * io_dest, bool log_errors)
{ {
char format_name[FN_REFLEN]; char format_name[FN_REFLEN];
fn_format(format_name, io_dest, "", "", MY_UNPACK_FILENAME); fn_format(format_name, io_dest, "", "", MY_UNPACK_FILENAME);
m_log_file = my_fopen(format_name, O_RDWR | O_APPEND, MYF(0)); m_log_file = my_fopen(format_name, O_WRONLY | O_APPEND| O_CREAT, MYF(0));
if (!m_log_file) if (!m_log_file)
{ {
if(log_errors) if(log_errors)
@ -209,6 +210,31 @@ int Audit_file_handler::open(const char * io_dest, bool log_errors)
} }
return -1; return -1;
} }
ssize_t bufsize = BUFSIZ;
int res =0;
//0 -> use default, 1 or negative -> disabled
if(m_bufsize > 1)
{
bufsize = m_bufsize;
}
if(1 == m_bufsize || m_bufsize < 0)
{
//disabled
res = setvbuf(m_log_file, NULL, _IONBF, 0);
}
else
{
res = setvbuf(m_log_file, NULL, _IOFBF, bufsize);
}
if(res)
{
sql_print_error(
"%s unable to set bufzie [%zd (%ld)] for file %s: %s.",
AUDIT_LOG_PREFIX, bufsize, m_bufsize, m_io_dest, strerror(errno));
}
sql_print_information("%s bufsize for file [%s]: %zd. Value of json_file_bufsize: %ld.", AUDIT_LOG_PREFIX, m_io_dest,
__fbufsize(m_log_file), m_bufsize);
return 0; return 0;
} }

View File

@ -1591,6 +1591,10 @@ static MYSQL_SYSVAR_STR(json_log_file, json_file_handler.m_io_dest,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_MEMALLOC, PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_MEMALLOC,
"AUDIT plugin json log file name", "AUDIT plugin json log file name",
NULL, NULL, "mysql-audit.json"); NULL, NULL, "mysql-audit.json");
static MYSQL_SYSVAR_LONG(json_file_bufsize, json_file_handler.m_bufsize,
PLUGIN_VAR_RQCMDARG,
"AUDIT plugin json log file buffer size. Buffer size in bytes (lager size may improve performance). 0 = use default size, 1 = no buffering. If changed during runtime need to perform a flush for the new value to take affect.",
NULL, NULL, 0, 1, 262144, 0);
static MYSQL_SYSVAR_UINT(json_file_sync, json_file_handler.m_sync_period, static MYSQL_SYSVAR_UINT(json_file_sync, json_file_handler.m_sync_period,
PLUGIN_VAR_RQCMDARG, PLUGIN_VAR_RQCMDARG,
@ -1713,6 +1717,7 @@ static struct st_mysql_sys_var* audit_system_variables[] =
MYSQL_SYSVAR(header_msg), MYSQL_SYSVAR(header_msg),
MYSQL_SYSVAR(force_record_logins), MYSQL_SYSVAR(force_record_logins),
MYSQL_SYSVAR(json_log_file), MYSQL_SYSVAR(json_log_file),
MYSQL_SYSVAR(json_file_bufsize),
MYSQL_SYSVAR(json_file_sync), MYSQL_SYSVAR(json_file_sync),
MYSQL_SYSVAR(json_file_retry), MYSQL_SYSVAR(json_file_retry),
MYSQL_SYSVAR(json_socket_retry), MYSQL_SYSVAR(json_socket_retry),