Skip to content

Commit

Permalink
Reduce locking on MySQL_Logger writes to disk
Browse files Browse the repository at this point in the history
  • Loading branch information
renecannao committed Jul 14, 2019
1 parent 0831740 commit 255d924
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
4 changes: 2 additions & 2 deletions include/MySQL_Logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ class MySQL_Logger {
#else
rwlock_t rwlock;
#endif
void wrlock();
void wrunlock();
void events_close_log_unlocked();
void events_open_log_unlocked();
void audit_close_log_unlocked();
Expand All @@ -88,6 +86,8 @@ class MySQL_Logger {
void log_request(MySQL_Session *, MySQL_Data_Stream *);
void log_audit_entry(log_event_type, MySQL_Session *, MySQL_Data_Stream *, char *e = NULL);
void flush();
void wrlock();
void wrunlock();
};


Expand Down
21 changes: 19 additions & 2 deletions lib/MySQL_Logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ using json = nlohmann::json;
#endif /* DEBUG */
#define PROXYSQL_MYSQL_LOGGER_VERSION "2.0.0714" DEB

extern MySQL_Logger *GloMyLogger;

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

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

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

// for performance reason, we are moving the write lock
// right before the write to disk
GloMyLogger->wrlock();

// write total length , fixed size
f->write((const char *)&total_bytes,sizeof(uint64_t));
//char prefix;
Expand Down Expand Up @@ -389,6 +397,11 @@ uint64_t MySQL_Event::write_query_format_2_json(std::fstream *f) {
char digest_hex[20];
sprintf(digest_hex,"0x%016llX", (long long unsigned int)query_digest);
j["digest"] = digest_hex;

// for performance reason, we are moving the write lock
// right before the write to disk
GloMyLogger->wrlock();

*f << j.dump() << std::endl;
return total_bytes; // always 0
}
Expand Down Expand Up @@ -690,7 +703,9 @@ void MySQL_Logger::log_request(MySQL_Session *sess, MySQL_Data_Stream *myds) {
me.set_server(hid,sa,sl);
}

wrlock();
// for performance reason, we are moving the write lock
// right before the write to disk
//wrlock();

me.write(events.logfile, sess);

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

wrlock();
// for performance reason, we are moving the write lock
// right before the write to disk
//wrlock();

me.write(audit.logfile, sess);

Expand Down

0 comments on commit 255d924

Please sign in to comment.