From a8e8859b798c5ebcc2f4028452aef28f297fd3bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Canna=C3=B2?= Date: Tue, 6 Feb 2018 16:08:28 +0100 Subject: [PATCH] Add rw lock to ensure metadata aren't updated while running STMT_PREPARE_RESPONSE --- include/MySQL_PreparedStatement.h | 1 + lib/MySQL_PreparedStatement.cpp | 3 +++ lib/MySQL_Protocol.cpp | 2 ++ 3 files changed, 6 insertions(+) diff --git a/include/MySQL_PreparedStatement.h b/include/MySQL_PreparedStatement.h index 19dbded56..4f75605cc 100644 --- a/include/MySQL_PreparedStatement.h +++ b/include/MySQL_PreparedStatement.h @@ -42,6 +42,7 @@ class MySQL_STMT_Global_info { private: void compute_hash(); public: + pthread_rwlock_t rwlock_; uint64_t digest; MYSQL_COM_QUERY_command MyComQueryCmd; char * digest_text; diff --git a/lib/MySQL_PreparedStatement.cpp b/lib/MySQL_PreparedStatement.cpp index b5a946875..b7c998012 100644 --- a/lib/MySQL_PreparedStatement.cpp +++ b/lib/MySQL_PreparedStatement.cpp @@ -136,6 +136,7 @@ MySQL_STMT_Global_info::MySQL_STMT_Global_info(uint64_t id, unsigned int h, char *u, char *s, char *q, unsigned int ql, MYSQL_STMT *stmt, uint64_t _h) { + pthread_rwlock_init(&rwlock_, NULL); statement_id = id; hostgroup_id = h; ref_count_client = 0; @@ -221,6 +222,7 @@ MySQL_STMT_Global_info::MySQL_STMT_Global_info(uint64_t id, unsigned int h, void MySQL_STMT_Global_info::update_metadata(MYSQL_STMT *stmt) { int i; bool need_refresh = false; + pthread_rwlock_wrlock(&rwlock_); if ( (num_params != stmt->param_count) || @@ -400,6 +402,7 @@ void MySQL_STMT_Global_info::update_metadata(MYSQL_STMT *stmt) { } // till here is copied from constructor } + pthread_rwlock_unlock(&rwlock_); } MySQL_STMT_Global_info::~MySQL_STMT_Global_info() { diff --git a/lib/MySQL_Protocol.cpp b/lib/MySQL_Protocol.cpp index 9a7646477..eb6ebbaaf 100644 --- a/lib/MySQL_Protocol.cpp +++ b/lib/MySQL_Protocol.cpp @@ -777,6 +777,7 @@ bool MySQL_Protocol::generate_STMT_PREPARE_RESPONSE(uint8_t sequence_id, MySQL_S okpack[4]=0; okpack[13]=0; okpack[15]=0; + pthread_rwlock_rdlock(&stmt_info->rwlock_); if (_stmt_id) { memcpy(okpack+5,&_stmt_id,sizeof(uint32_t)); } else { @@ -817,6 +818,7 @@ bool MySQL_Protocol::generate_STMT_PREPARE_RESPONSE(uint8_t sequence_id, MySQL_S generate_pkt_EOF(true,NULL,NULL,sid,0,setStatus); sid++; } + pthread_rwlock_unlock(&stmt_info->rwlock_); return true; }