Skip to content

Commit 255d924

Browse files
committed
Reduce locking on MySQL_Logger writes to disk
1 parent 0831740 commit 255d924

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

include/MySQL_Logger.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ class MySQL_Logger {
6666
#else
6767
rwlock_t rwlock;
6868
#endif
69-
void wrlock();
70-
void wrunlock();
7169
void events_close_log_unlocked();
7270
void events_open_log_unlocked();
7371
void audit_close_log_unlocked();
@@ -88,6 +86,8 @@ class MySQL_Logger {
8886
void log_request(MySQL_Session *, MySQL_Data_Stream *);
8987
void log_audit_entry(log_event_type, MySQL_Session *, MySQL_Data_Stream *, char *e = NULL);
9088
void flush();
89+
void wrlock();
90+
void wrunlock();
9191
};
9292

9393

lib/MySQL_Logger.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ using json = nlohmann::json;
1414
#endif /* DEBUG */
1515
#define PROXYSQL_MYSQL_LOGGER_VERSION "2.0.0714" DEB
1616

17+
extern MySQL_Logger *GloMyLogger;
1718

1819
static uint8_t mysql_encode_length(uint64_t len, unsigned char *hd) {
1920
if (len < 251) return 1;
@@ -225,6 +226,9 @@ void MySQL_Event::write_auth(std::fstream *f, MySQL_Session *sess) {
225226
}
226227
j["ssl"] = sess->client_myds->encrypted;
227228
}
229+
// for performance reason, we are moving the write lock
230+
// right before the write to disk
231+
GloMyLogger->wrlock();
228232
*f << j.dump() << std::endl;
229233
}
230234

@@ -253,6 +257,10 @@ uint64_t MySQL_Event::write_query_format_1(std::fstream *f) {
253257

254258
total_bytes+=mysql_encode_length(query_len,NULL)+query_len;
255259

260+
// for performance reason, we are moving the write lock
261+
// right before the write to disk
262+
GloMyLogger->wrlock();
263+
256264
// write total length , fixed size
257265
f->write((const char *)&total_bytes,sizeof(uint64_t));
258266
//char prefix;
@@ -389,6 +397,11 @@ uint64_t MySQL_Event::write_query_format_2_json(std::fstream *f) {
389397
char digest_hex[20];
390398
sprintf(digest_hex,"0x%016llX", (long long unsigned int)query_digest);
391399
j["digest"] = digest_hex;
400+
401+
// for performance reason, we are moving the write lock
402+
// right before the write to disk
403+
GloMyLogger->wrlock();
404+
392405
*f << j.dump() << std::endl;
393406
return total_bytes; // always 0
394407
}
@@ -690,7 +703,9 @@ void MySQL_Logger::log_request(MySQL_Session *sess, MySQL_Data_Stream *myds) {
690703
me.set_server(hid,sa,sl);
691704
}
692705

693-
wrlock();
706+
// for performance reason, we are moving the write lock
707+
// right before the write to disk
708+
//wrlock();
694709

695710
me.write(events.logfile, sess);
696711

@@ -840,7 +855,9 @@ void MySQL_Logger::log_audit_entry(log_event_type _et, MySQL_Session *sess, MySQ
840855
me.set_extra_info(xi);
841856
}
842857

843-
wrlock();
858+
// for performance reason, we are moving the write lock
859+
// right before the write to disk
860+
//wrlock();
844861

845862
me.write(audit.logfile, sess);
846863

0 commit comments

Comments
 (0)