diff --git a/include/MySQL_Logger.hpp b/include/MySQL_Logger.hpp index f1487b38dd..57f5c4bed7 100644 --- a/include/MySQL_Logger.hpp +++ b/include/MySQL_Logger.hpp @@ -28,9 +28,12 @@ class MySQL_Event { char *extra_info; bool have_affected_rows; bool have_rows_sent; + bool have_gtid; uint64_t affected_rows; + uint64_t last_insert_id; uint64_t rows_sent; uint32_t client_stmt_id; + const char * gtid; public: MySQL_Event(log_event_type _et, uint32_t _thread_id, char * _username, char * _schemaname , uint64_t _start_time , uint64_t _end_time , uint64_t _query_digest, char *_client, size_t _client_len); uint64_t write(std::fstream *f, MySQL_Session *sess); @@ -41,8 +44,9 @@ class MySQL_Event { void set_query(const char *ptr, int len); void set_server(int _hid, const char *ptr, int len); void set_extra_info(char *); - void set_affected_rows(uint64_t ar); + void set_affected_rows(uint64_t ar, uint64_t lid); void set_rows_sent(uint64_t rs); + void set_gtid(MySQL_Session *sess); }; class MySQL_Logger { diff --git a/include/MySQL_Session.h b/include/MySQL_Session.h index a657dc85ce..424d7d6943 100644 --- a/include/MySQL_Session.h +++ b/include/MySQL_Session.h @@ -58,8 +58,9 @@ class Query_Info { enum MYSQL_COM_QUERY_command MyComQueryCmd; bool bool_is_select_NOT_for_update; bool bool_is_select_NOT_for_update_computed; - bool have_affected_rows; + bool have_affected_rows; // if affected rows is set, last_insert_id is set too uint64_t affected_rows; + uint64_t last_insert_id; uint64_t rows_sent; uint64_t waiting_since; std::string show_warnings_prev_query_digest; diff --git a/lib/MySQL_Logger.cpp b/lib/MySQL_Logger.cpp index 9e61c68cc4..b0b93d8e3a 100644 --- a/lib/MySQL_Logger.cpp +++ b/lib/MySQL_Logger.cpp @@ -18,7 +18,7 @@ using json = nlohmann::json; #else #define DEB "" #endif /* DEBUG */ -#define PROXYSQL_MYSQL_LOGGER_VERSION "2.0.0714" DEB +#define PROXYSQL_MYSQL_LOGGER_VERSION "2.5.0421" DEB extern MySQL_Logger *GloMyLogger; @@ -56,18 +56,23 @@ MySQL_Event::MySQL_Event (log_event_type _et, uint32_t _thread_id, char * _usern extra_info = NULL; have_affected_rows=false; affected_rows=0; + last_insert_id = 0; have_rows_sent=false; rows_sent=0; client_stmt_id=0; + gtid = NULL; } void MySQL_Event::set_client_stmt_id(uint32_t client_stmt_id) { this->client_stmt_id = client_stmt_id; } -void MySQL_Event::set_affected_rows(uint64_t ar) { +// if affected rows is set, last_insert_id is set too. +// They are part of the same OK packet +void MySQL_Event::set_affected_rows(uint64_t ar, uint64_t lid) { have_affected_rows=true; affected_rows=ar; + last_insert_id=lid; } void MySQL_Event::set_rows_sent(uint64_t rs) { @@ -75,6 +80,15 @@ void MySQL_Event::set_rows_sent(uint64_t rs) { rows_sent=rs; } +void MySQL_Event::set_gtid(MySQL_Session *sess) { + if (sess != NULL) { + if (sess->gtid_buf[0] != 0) { + have_gtid = true; + gtid = sess->gtid_buf; + } + } +} + void MySQL_Event::set_extra_info(char *_err) { extra_info = _err; } @@ -264,6 +278,7 @@ uint64_t MySQL_Event::write_query_format_1(std::fstream *f) { total_bytes+=mysql_encode_length(end_time,NULL); total_bytes+=mysql_encode_length(client_stmt_id,NULL); total_bytes+=mysql_encode_length(affected_rows,NULL); + total_bytes+=mysql_encode_length(last_insert_id,NULL); // as in MySQL Protocol, last_insert_id is immediately after affected_rows total_bytes+=mysql_encode_length(rows_sent,NULL); total_bytes+=mysql_encode_length(query_digest,NULL); @@ -330,6 +345,10 @@ uint64_t MySQL_Event::write_query_format_1(std::fstream *f) { write_encoded_length(buf,affected_rows,len,buf[0]); f->write((char *)buf,len); + len=mysql_encode_length(last_insert_id,buf); + write_encoded_length(buf,last_insert_id,len,buf[0]); + f->write((char *)buf,len); + len=mysql_encode_length(rows_sent,buf); write_encoded_length(buf,rows_sent,len,buf[0]); f->write((char *)buf,len); @@ -370,26 +389,36 @@ uint64_t MySQL_Event::write_query_format_2_json(std::fstream *f) { } if (username) { j["username"] = username; - } else { - j["username"] = ""; + //} else { + // j["username"] = ""; } if (schemaname) { j["schemaname"] = schemaname; - } else { - j["schemaname"] = ""; + //} else { + // j["schemaname"] = ""; } if (client) { j["client"] = client; - } else { - j["client"] = ""; + //} else { + // j["client"] = ""; } if (hid!=UINT64_MAX) { if (server) { j["server"] = server; } } - j["rows_affected"] = affected_rows; - j["rows_sent"] = rows_sent; + if (have_affected_rows == true) { + // in JSON format we only log rows_affected and last_insert_id + // if they are present (even if 0) + j["rows_affected"] = affected_rows; + j["last_insert_id"] = last_insert_id; + } + if (have_rows_sent == true) { + j["rows_sent"] = rows_sent; + } + if (have_gtid == true) { + j["last_gtid"] = gtid; + } j["query"] = string(query_ptr,query_len); j["starttime_timestamp_us"] = start_time; { @@ -729,9 +758,10 @@ void MySQL_Logger::log_request(MySQL_Session *sess, MySQL_Data_Stream *myds) { } if (sess->CurrentQuery.have_affected_rows) { - me.set_affected_rows(sess->CurrentQuery.affected_rows); + me.set_affected_rows(sess->CurrentQuery.affected_rows, sess->CurrentQuery.last_insert_id); } me.set_rows_sent(sess->CurrentQuery.rows_sent); + me.set_gtid(sess); int sl=0; char *sa=(char *)""; // default diff --git a/lib/MySQL_Protocol.cpp b/lib/MySQL_Protocol.cpp index 756c28f970..589f4e8954 100644 --- a/lib/MySQL_Protocol.cpp +++ b/lib/MySQL_Protocol.cpp @@ -524,8 +524,9 @@ bool MySQL_Protocol::generate_pkt_OK(bool send, void **ptr, unsigned int *len, u break; } if (sess->session_type == PROXYSQL_SESSION_MYSQL) { - sess->CurrentQuery.have_affected_rows = true; + sess->CurrentQuery.have_affected_rows = true; // if affected rows is set, last_insert_id is set too sess->CurrentQuery.affected_rows = affected_rows; + sess->CurrentQuery.last_insert_id = last_insert_id; } } if (*myds && (*myds)->myconn) { diff --git a/lib/MySQL_Session.cpp b/lib/MySQL_Session.cpp index 7c79886589..0a4f8cafc9 100644 --- a/lib/MySQL_Session.cpp +++ b/lib/MySQL_Session.cpp @@ -302,9 +302,10 @@ Query_Info::Query_Info() { stmt_info=NULL; bool_is_select_NOT_for_update=false; bool_is_select_NOT_for_update_computed=false; - have_affected_rows=false; + have_affected_rows=false; // if affected rows is set, last_insert_id is set too waiting_since = 0; affected_rows=0; + last_insert_id = 0; rows_sent=0; start_time=0; end_time=0; @@ -335,9 +336,10 @@ void Query_Info::begin(unsigned char *_p, int len, bool mysql_header) { } bool_is_select_NOT_for_update=false; bool_is_select_NOT_for_update_computed=false; - have_affected_rows=false; + have_affected_rows=false; // if affected rows is set, last_insert_id is set too waiting_since = 0; affected_rows=0; + last_insert_id = 0; rows_sent=0; sess->gtid_hid=-1; stmt_client_id=0; @@ -374,9 +376,10 @@ void Query_Info::init(unsigned char *_p, int len, bool mysql_header) { MyComQueryCmd = MYSQL_COM_QUERY__UNINITIALIZED; bool_is_select_NOT_for_update=false; bool_is_select_NOT_for_update_computed=false; - have_affected_rows=false; + have_affected_rows=false; // if affected rows is set, last_insert_id is set too waiting_since = 0; affected_rows=0; + last_insert_id = 0; rows_sent=0; }