Skip to content

Commit 65e0c9b

Browse files
author
Alexey Botchkov
committed
MDEV-18661 loading the audit plugin causes performance regression.
Plugin fixed to not lock the LOCK_operations when not active. Server fixed to lock the LOCK_plugin less - do it once per thread and then only if a plugin was installed/uninstalled.
1 parent 5b65d61 commit 65e0c9b

File tree

9 files changed

+97
-47
lines changed

9 files changed

+97
-47
lines changed

plugin/server_audit/server_audit.c

Lines changed: 68 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616

1717
#define PLUGIN_VERSION 0x104
18-
#define PLUGIN_STR_VERSION "1.4.4"
18+
#define PLUGIN_STR_VERSION "1.4.7"
1919

2020
#define _my_thread_var loc_thread_var
2121

@@ -295,7 +295,7 @@ static unsigned long long file_rotate_size;
295295
static unsigned int rotations;
296296
static my_bool rotate= TRUE;
297297
static char logging;
298-
static int internal_stop_logging= 0;
298+
static volatile int internal_stop_logging= 0;
299299
static char incl_user_buffer[1024];
300300
static char excl_user_buffer[1024];
301301
static char *big_buffer= NULL;
@@ -533,16 +533,20 @@ static struct st_mysql_show_var audit_status[]=
533533
#if defined(HAVE_PSI_INTERFACE) && !defined(FLOGGER_NO_PSI)
534534
/* These belong to the service initialization */
535535
static PSI_mutex_key key_LOCK_operations;
536+
static PSI_mutex_key key_LOCK_atomic;
536537
static PSI_mutex_key key_LOCK_bigbuffer;
537538
static PSI_mutex_info mutex_key_list[]=
538539
{
539540
{ &key_LOCK_operations, "SERVER_AUDIT_plugin::lock_operations",
540541
PSI_FLAG_GLOBAL},
542+
{ &key_LOCK_atomic, "SERVER_AUDIT_plugin::lock_atomic",
543+
PSI_FLAG_GLOBAL},
541544
{ &key_LOCK_bigbuffer, "SERVER_AUDIT_plugin::lock_bigbuffer",
542545
PSI_FLAG_GLOBAL}
543546
};
544547
#endif
545548
static mysql_mutex_t lock_operations;
549+
static mysql_mutex_t lock_atomic;
546550
static mysql_mutex_t lock_bigbuffer;
547551

548552
/* The Percona server and partly MySQL don't support */
@@ -553,6 +557,14 @@ static mysql_mutex_t lock_bigbuffer;
553557
/* worths doing. */
554558
#define CLIENT_ERROR if (!started_mysql) my_printf_error
555559

560+
#define ADD_ATOMIC(x, a) \
561+
do { \
562+
flogger_mutex_lock(&lock_atomic); \
563+
x+= a; \
564+
flogger_mutex_unlock(&lock_atomic); \
565+
} while (0)
566+
567+
556568
static uchar *getkey_user(const char *entry, size_t *length,
557569
my_bool nu __attribute__((unused)) )
558570
{
@@ -731,20 +743,20 @@ static int user_coll_fill(struct user_coll *c, char *users,
731743

732744
if (cmp_user && take_over_cmp)
733745
{
734-
internal_stop_logging= 1;
746+
ADD_ATOMIC(internal_stop_logging, 1);
735747
CLIENT_ERROR(1, "User '%.*s' was removed from the"
736748
" server_audit_excl_users.",
737749
MYF(ME_JUST_WARNING), (int) cmp_length, users);
738-
internal_stop_logging= 0;
750+
ADD_ATOMIC(internal_stop_logging, -1);
739751
blank_user(cmp_user);
740752
refill_cmp_coll= 1;
741753
}
742754
else if (cmp_user)
743755
{
744-
internal_stop_logging= 1;
756+
ADD_ATOMIC(internal_stop_logging, 1);
745757
CLIENT_ERROR(1, "User '%.*s' is in the server_audit_incl_users, "
746758
"so wasn't added.", MYF(ME_JUST_WARNING), (int) cmp_length, users);
747-
internal_stop_logging= 0;
759+
ADD_ATOMIC(internal_stop_logging, -1);
748760
remove_user(users);
749761
continue;
750762
}
@@ -1252,23 +1264,30 @@ static void change_connection(struct connection_info *cn,
12521264
event->ip, event->ip_length);
12531265
}
12541266

1255-
static int write_log(const char *message, int len)
1267+
static int write_log(const char *message, size_t len, int take_lock)
12561268
{
1269+
int result= 0;
1270+
if (take_lock)
1271+
flogger_mutex_lock(&lock_operations);
1272+
12571273
if (output_type == OUTPUT_FILE)
12581274
{
12591275
if (logfile &&
1260-
(is_active= (logger_write(logfile, message, len) == len)))
1261-
return 0;
1276+
(is_active= (logger_write(logfile, message, len) == (int) len)))
1277+
goto exit;
12621278
++log_write_failures;
1263-
return 1;
1279+
result= 1;
12641280
}
12651281
else if (output_type == OUTPUT_SYSLOG)
12661282
{
12671283
syslog(syslog_facility_codes[syslog_facility] |
12681284
syslog_priority_codes[syslog_priority],
1269-
"%s %.*s", syslog_info, len, message);
1285+
"%s %.*s", syslog_info, (int) len, message);
12701286
}
1271-
return 0;
1287+
exit:
1288+
if (take_lock)
1289+
flogger_mutex_unlock(&lock_operations);
1290+
return result;
12721291
}
12731292

12741293

@@ -1327,7 +1346,7 @@ static int log_connection(const struct connection_info *cn,
13271346
csize+= my_snprintf(message+csize, sizeof(message) - 1 - csize,
13281347
",%.*s,,%d", cn->db_length, cn->db, event->status);
13291348
message[csize]= '\n';
1330-
return write_log(message, csize + 1);
1349+
return write_log(message, csize + 1, 1);
13311350
}
13321351

13331352

@@ -1348,7 +1367,7 @@ static int log_connection_event(const struct mysql_event_connection *event,
13481367
csize+= my_snprintf(message+csize, sizeof(message) - 1 - csize,
13491368
",%.*s,,%d", event->database_length, event->database, event->status);
13501369
message[csize]= '\n';
1351-
return write_log(message, csize + 1);
1370+
return write_log(message, csize + 1, 1);
13521371
}
13531372

13541373

@@ -1477,21 +1496,28 @@ static size_t escape_string_hide_passwords(const char *str, unsigned int len,
14771496

14781497

14791498

1480-
static int do_log_user(const char *name)
1499+
static int do_log_user(const char *name, int take_lock)
14811500
{
14821501
size_t len;
1502+
int result;
14831503

14841504
if (!name)
14851505
return 0;
14861506
len= strlen(name);
14871507

1488-
if (incl_user_coll.n_users)
1489-
return coll_search(&incl_user_coll, name, len) != 0;
1508+
if (take_lock)
1509+
flogger_mutex_lock(&lock_operations);
14901510

1491-
if (excl_user_coll.n_users)
1492-
return coll_search(&excl_user_coll, name, len) == 0;
1511+
if (incl_user_coll.n_users)
1512+
result= coll_search(&incl_user_coll, name, len) != 0;
1513+
else if (excl_user_coll.n_users)
1514+
result= coll_search(&excl_user_coll, name, len) == 0;
1515+
else
1516+
result= 1;
14931517

1494-
return 1;
1518+
if (take_lock)
1519+
flogger_mutex_unlock(&lock_operations);
1520+
return result;
14951521
}
14961522

14971523

@@ -1588,7 +1614,7 @@ static int filter_query_type(const char *query, struct sa_keyword *kwd)
15881614
static int log_statement_ex(const struct connection_info *cn,
15891615
time_t ev_time, unsigned long thd_id,
15901616
const char *query, unsigned int query_len,
1591-
int error_code, const char *type)
1617+
int error_code, const char *type, int take_lock)
15921618
{
15931619
size_t csize;
15941620
char message_loc[1024];
@@ -1736,7 +1762,7 @@ static int log_statement_ex(const struct connection_info *cn,
17361762
csize+= my_snprintf(message+csize, message_size - 1 - csize,
17371763
"\',%d", error_code);
17381764
message[csize]= '\n';
1739-
result= write_log(message, csize + 1);
1765+
result= write_log(message, csize + 1, take_lock);
17401766
if (message == big_buffer)
17411767
flogger_mutex_unlock(&lock_bigbuffer);
17421768

@@ -1750,7 +1776,7 @@ static int log_statement(const struct connection_info *cn,
17501776
{
17511777
return log_statement_ex(cn, event->general_time, event->general_thread_id,
17521778
event->general_query, event->general_query_length,
1753-
event->general_error_code, type);
1779+
event->general_error_code, type, 1);
17541780
}
17551781

17561782

@@ -1772,7 +1798,7 @@ static int log_table(const struct connection_info *cn,
17721798
",%.*s,%.*s,",event->database_length, event->database,
17731799
event->table_length, event->table);
17741800
message[csize]= '\n';
1775-
return write_log(message, csize + 1);
1801+
return write_log(message, csize + 1, 1);
17761802
}
17771803

17781804

@@ -1796,7 +1822,7 @@ static int log_rename(const struct connection_info *cn,
17961822
event->new_database_length, event->new_database,
17971823
event->new_table_length, event->new_table);
17981824
message[csize]= '\n';
1799-
return write_log(message, csize + 1);
1825+
return write_log(message, csize + 1, 1);
18001826
}
18011827

18021828

@@ -1988,8 +2014,6 @@ void auditing(MYSQL_THD thd, unsigned int event_class, const void *ev)
19882014
if (!thd || internal_stop_logging)
19892015
return;
19902016

1991-
flogger_mutex_lock(&lock_operations);
1992-
19932017
if (maria_55_started && debug_server_started &&
19942018
event_class == MYSQL_AUDIT_GENERAL_CLASS)
19952019
{
@@ -2024,7 +2048,7 @@ void auditing(MYSQL_THD thd, unsigned int event_class, const void *ev)
20242048
goto exit_func;
20252049

20262050
if (event_class == MYSQL_AUDIT_GENERAL_CLASS && FILTER(EVENT_QUERY) &&
2027-
cn && do_log_user(cn->user))
2051+
cn && do_log_user(cn->user, 1))
20282052
{
20292053
const struct mysql_event_general *event =
20302054
(const struct mysql_event_general *) ev;
@@ -2043,7 +2067,7 @@ void auditing(MYSQL_THD thd, unsigned int event_class, const void *ev)
20432067
{
20442068
const struct mysql_event_table *event =
20452069
(const struct mysql_event_table *) ev;
2046-
if (do_log_user(event->user))
2070+
if (do_log_user(event->user, 1))
20472071
{
20482072
switch (event->event_subclass)
20492073
{
@@ -2109,7 +2133,6 @@ void auditing(MYSQL_THD thd, unsigned int event_class, const void *ev)
21092133
}
21102134
if (cn)
21112135
cn->log_always= 0;
2112-
flogger_mutex_unlock(&lock_operations);
21132136
}
21142137

21152138

@@ -2377,6 +2400,7 @@ static int server_audit_init(void *p __attribute__((unused)))
23772400
PSI_server->register_mutex("server_audit", mutex_key_list, 1);
23782401
#endif
23792402
flogger_mutex_init(key_LOCK_operations, &lock_operations, MY_MUTEX_INIT_FAST);
2403+
flogger_mutex_init(key_LOCK_operations, &lock_atomic, MY_MUTEX_INIT_FAST);
23802404
flogger_mutex_init(key_LOCK_operations, &lock_bigbuffer, MY_MUTEX_INIT_FAST);
23812405

23822406
coll_init(&incl_user_coll);
@@ -2464,6 +2488,7 @@ static int server_audit_deinit(void *p __attribute__((unused)))
24642488

24652489
(void) free(big_buffer);
24662490
flogger_mutex_destroy(&lock_operations);
2491+
flogger_mutex_destroy(&lock_atomic);
24672492
flogger_mutex_destroy(&lock_bigbuffer);
24682493

24692494
error_header();
@@ -2553,10 +2578,10 @@ static void log_current_query(MYSQL_THD thd)
25532578
return;
25542579
cn= get_loc_info(thd);
25552580
if (!ci_needs_setup(cn) && cn->query_length &&
2556-
FILTER(EVENT_QUERY) && do_log_user(cn->user))
2581+
FILTER(EVENT_QUERY) && do_log_user(cn->user, 0))
25572582
{
25582583
log_statement_ex(cn, cn->query_time, thd_get_thread_id(thd),
2559-
cn->query, cn->query_length, 0, "QUERY");
2584+
cn->query, cn->query_length, 0, "QUERY", 0);
25602585
cn->log_always= 1;
25612586
}
25622587
}
@@ -2568,12 +2593,13 @@ static void update_file_path(MYSQL_THD thd,
25682593
{
25692594
char *new_name= (*(char **) save) ? *(char **) save : empty_str;
25702595

2571-
if (!maria_55_started || !debug_server_started)
2572-
flogger_mutex_lock(&lock_operations);
2573-
internal_stop_logging= 1;
2596+
ADD_ATOMIC(internal_stop_logging, 1);
25742597
error_header();
25752598
fprintf(stderr, "Log file name was changed to '%s'.\n", new_name);
25762599

2600+
if (!maria_55_started || !debug_server_started)
2601+
flogger_mutex_lock(&lock_operations);
2602+
25772603
if (logging)
25782604
log_current_query(thd);
25792605

@@ -2582,7 +2608,6 @@ static void update_file_path(MYSQL_THD thd,
25822608
char *sav_path= file_path;
25832609

25842610
file_path= new_name;
2585-
internal_stop_logging= 1;
25862611
stop_logging();
25872612
if (start_logging())
25882613
{
@@ -2598,16 +2623,15 @@ static void update_file_path(MYSQL_THD thd,
25982623
}
25992624
goto exit_func;
26002625
}
2601-
internal_stop_logging= 0;
26022626
}
26032627

26042628
strncpy(path_buffer, new_name, sizeof(path_buffer)-1);
26052629
path_buffer[sizeof(path_buffer)-1]= 0;
26062630
file_path= path_buffer;
26072631
exit_func:
2608-
internal_stop_logging= 0;
26092632
if (!maria_55_started || !debug_server_started)
26102633
flogger_mutex_unlock(&lock_operations);
2634+
ADD_ATOMIC(internal_stop_logging, -1);
26112635
}
26122636

26132637

@@ -2692,8 +2716,8 @@ static void update_output_type(MYSQL_THD thd,
26922716
if (output_type == new_output_type)
26932717
return;
26942718

2719+
ADD_ATOMIC(internal_stop_logging, 1);
26952720
flogger_mutex_lock(&lock_operations);
2696-
internal_stop_logging= 1;
26972721
if (logging)
26982722
{
26992723
log_current_query(thd);
@@ -2707,8 +2731,8 @@ static void update_output_type(MYSQL_THD thd,
27072731

27082732
if (logging)
27092733
start_logging();
2710-
internal_stop_logging= 0;
27112734
flogger_mutex_unlock(&lock_operations);
2735+
ADD_ATOMIC(internal_stop_logging, -1);
27122736
}
27132737

27142738

@@ -2756,9 +2780,9 @@ static void update_logging(MYSQL_THD thd,
27562780
if (new_logging == logging)
27572781
return;
27582782

2783+
ADD_ATOMIC(internal_stop_logging, 1);
27592784
if (!maria_55_started || !debug_server_started)
27602785
flogger_mutex_lock(&lock_operations);
2761-
internal_stop_logging= 1;
27622786
if ((logging= new_logging))
27632787
{
27642788
start_logging();
@@ -2773,9 +2797,9 @@ static void update_logging(MYSQL_THD thd,
27732797
stop_logging();
27742798
}
27752799

2776-
internal_stop_logging= 0;
27772800
if (!maria_55_started || !debug_server_started)
27782801
flogger_mutex_unlock(&lock_operations);
2802+
ADD_ATOMIC(internal_stop_logging, -1);
27792803
}
27802804

27812805

@@ -2787,16 +2811,16 @@ static void update_mode(MYSQL_THD thd __attribute__((unused)),
27872811
if (mode_readonly || new_mode == mode)
27882812
return;
27892813

2814+
ADD_ATOMIC(internal_stop_logging, 1);
27902815
if (!maria_55_started || !debug_server_started)
27912816
flogger_mutex_lock(&lock_operations);
2792-
internal_stop_logging= 1;
27932817
mark_always_logged(thd);
27942818
error_header();
27952819
fprintf(stderr, "Logging mode was changed from %d to %d.\n", mode, new_mode);
27962820
mode= new_mode;
2797-
internal_stop_logging= 0;
27982821
if (!maria_55_started || !debug_server_started)
27992822
flogger_mutex_unlock(&lock_operations);
2823+
ADD_ATOMIC(internal_stop_logging, -1);
28002824
}
28012825

28022826

0 commit comments

Comments
 (0)