Skip to content

Commit

Permalink
Adding last_insert_id and last_gtid to MySQL_Logger
Browse files Browse the repository at this point in the history
  • Loading branch information
renecannao committed May 4, 2023
1 parent cac91e6 commit c0745b4
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 17 deletions.
6 changes: 5 additions & 1 deletion include/MySQL_Logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion include/MySQL_Session.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
52 changes: 41 additions & 11 deletions lib/MySQL_Logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -56,25 +56,39 @@ 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) {
have_rows_sent=true;
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;
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
{
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lib/MySQL_Protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
9 changes: 6 additions & 3 deletions lib/MySQL_Session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit c0745b4

Please sign in to comment.