Skip to content

Commit e14accd

Browse files
authored
Merge pull request #3453 from sysown/v2.2.0-3427
Closes #3427: Read/Write Split query rules do not work while using sysbench to evaluate perfromance
2 parents 5a6095c + 3a39483 commit e14accd

9 files changed

+903
-9
lines changed

include/MySQL_PreparedStatement.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,15 @@ class MySQL_STMT_Global_info {
5959
uint16_t num_params;
6060
uint16_t warning_count;
6161
MYSQL_FIELD **fields;
62+
char* first_comment;
6263
// struct {
6364
// int cache_ttl;
6465
// int timeout;
6566
// int delay;
6667
// } properties;
6768
bool is_select_NOT_for_update;
6869
MYSQL_BIND **params; // seems unused (?)
69-
MySQL_STMT_Global_info(uint64_t id, char *u, char *s, char *q, unsigned int ql, MYSQL_STMT *stmt, uint64_t _h);
70+
MySQL_STMT_Global_info(uint64_t id, char *u, char *s, char *q, unsigned int ql, char *fc, MYSQL_STMT *stmt, uint64_t _h);
7071
void update_metadata(MYSQL_STMT *stmt);
7172
~MySQL_STMT_Global_info();
7273
};
@@ -259,7 +260,7 @@ class MySQL_STMT_Manager_v14 {
259260
void unlock() { pthread_rwlock_unlock(&rwlock_); }
260261
void ref_count_client(uint64_t _stmt, int _v, bool lock=true);
261262
void ref_count_server(uint64_t _stmt, int _v, bool lock=true);
262-
MySQL_STMT_Global_info * add_prepared_statement(char *u, char *s, char *q, unsigned int ql, MYSQL_STMT *stmt, bool lock=true);
263+
MySQL_STMT_Global_info * add_prepared_statement(char *u, char *s, char *q, unsigned int ql, char *fc, MYSQL_STMT *stmt, bool lock=true);
263264
void get_metrics(uint64_t *c_unique, uint64_t *c_total, uint64_t *stmt_max_stmt_id, uint64_t *cached, uint64_t *s_unique, uint64_t *s_total);
264265
SQLite3_result * get_prepared_statements_global_infos();
265266
};

lib/MySQL_PreparedStatement.cpp

+12-3
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ void *StmtLongDataHandler::get(uint32_t _stmt_id, uint16_t _param_id,
133133
MySQL_STMT_Global_info::MySQL_STMT_Global_info(uint64_t id,
134134
char *u, char *s, char *q,
135135
unsigned int ql,
136+
char *fc,
136137
MYSQL_STMT *stmt, uint64_t _h) {
137138
pthread_rwlock_init(&rwlock_, NULL);
138139
statement_id = id;
@@ -145,6 +146,11 @@ MySQL_STMT_Global_info::MySQL_STMT_Global_info(uint64_t id,
145146
memcpy(query, q, ql);
146147
query[ql] = '\0'; // add NULL byte
147148
query_length = ql;
149+
if (fc) {
150+
first_comment = strdup(fc);
151+
} else {
152+
first_comment = NULL;
153+
}
148154
MyComQueryCmd = MYSQL_COM_QUERY__UNINITIALIZED;
149155
num_params = stmt->param_count;
150156
num_columns = stmt->field_count;
@@ -476,6 +482,9 @@ MySQL_STMT_Global_info::~MySQL_STMT_Global_info() {
476482
free(username);
477483
free(schemaname);
478484
free(query);
485+
if (first_comment) {
486+
free(first_comment);
487+
}
479488
if (num_columns) {
480489
uint16_t i;
481490
for (i = 0; i < num_columns; i++) {
@@ -812,10 +821,10 @@ bool MySQL_STMTs_local_v14::client_close(uint32_t client_statement_id) {
812821

813822
MySQL_STMT_Global_info *MySQL_STMT_Manager_v14::add_prepared_statement(
814823
char *u, char *s, char *q, unsigned int ql,
815-
MYSQL_STMT *stmt, bool lock) {
824+
char *fc, MYSQL_STMT *stmt, bool lock) {
816825
MySQL_STMT_Global_info *ret = NULL;
817826
uint64_t hash = stmt_compute_hash(
818-
u, s, q, ql); // this identifies the prepared statement
827+
u, s, q, ql); // this identifies the prepared statement
819828
if (lock) {
820829
pthread_rwlock_wrlock(&rwlock_);
821830
}
@@ -847,7 +856,7 @@ MySQL_STMT_Global_info *MySQL_STMT_Manager_v14::add_prepared_statement(
847856

848857
//next_statement_id++;
849858
MySQL_STMT_Global_info *a =
850-
new MySQL_STMT_Global_info(next_id, u, s, q, ql, stmt, hash);
859+
new MySQL_STMT_Global_info(next_id, u, s, q, ql, fc, stmt, hash);
851860
// insert it in both maps
852861
map_stmt_id_to_info.insert(std::make_pair(a->statement_id, a));
853862
map_stmt_hash_to_info.insert(std::make_pair(a->hash, a));

lib/MySQL_Session.cpp

+14-2
Original file line numberDiff line numberDiff line change
@@ -3052,7 +3052,12 @@ void MySQL_Session::handler___status_WAITING_CLIENT_DATA___STATE_SLEEP___MYSQL_C
30523052
if (client_myds->myconn->local_stmts==NULL) {
30533053
client_myds->myconn->local_stmts=new MySQL_STMTs_local_v14(true);
30543054
}
3055-
uint64_t hash=client_myds->myconn->local_stmts->compute_hash((char *)client_myds->myconn->userinfo->username,(char *)client_myds->myconn->userinfo->schemaname,(char *)CurrentQuery.QueryPointer,CurrentQuery.QueryLength);
3055+
uint64_t hash=client_myds->myconn->local_stmts->compute_hash(
3056+
(char *)client_myds->myconn->userinfo->username,
3057+
(char *)client_myds->myconn->userinfo->schemaname,
3058+
(char *)CurrentQuery.QueryPointer,
3059+
CurrentQuery.QueryLength
3060+
);
30563061
MySQL_STMT_Global_info *stmt_info=NULL;
30573062
// we first lock GloStmt
30583063
GloMyStmt->wrlock();
@@ -3137,7 +3142,7 @@ void MySQL_Session::handler___status_WAITING_CLIENT_DATA___STATE_SLEEP___MYSQL_C
31373142
if (thread->variables.stats_time_query_processor) {
31383143
clock_gettime(CLOCK_THREAD_CPUTIME_ID,&begint);
31393144
}
3140-
qpo=GloQPro->process_mysql_query(this,pkt.ptr,pkt.size,&CurrentQuery);
3145+
qpo=GloQPro->process_mysql_query(this,NULL,0,&CurrentQuery);
31413146
if (qpo->max_lag_ms >= 0) {
31423147
thread->status_variables.stvar[st_var_queries_with_max_lag_ms]++;
31433148
}
@@ -3858,6 +3863,7 @@ bool MySQL_Session::handler_rc0_PROCESSING_STMT_PREPARE(enum session_status& st,
38583863
(char *)client_myds->myconn->userinfo->schemaname,
38593864
(char *)CurrentQuery.QueryPointer,
38603865
CurrentQuery.QueryLength,
3866+
CurrentQuery.QueryParserArgs.first_comment,
38613867
CurrentQuery.mysql_stmt,
38623868
false);
38633869
if (CurrentQuery.QueryParserArgs.digest_text) {
@@ -4449,6 +4455,12 @@ int MySQL_Session::handler() {
44494455
stmt_info=GloMyStmt->find_prepared_statement_by_stmt_id(CurrentQuery.stmt_global_id);
44504456
CurrentQuery.QueryLength=stmt_info->query_length;
44514457
CurrentQuery.QueryPointer=(unsigned char *)stmt_info->query;
4458+
// NOTE: Update 'first_comment' with the the from the retrieved
4459+
// 'stmt_info' from the found prepared statement. 'CurrentQuery' requires its
4460+
// own copy of 'first_comment' because it will later be free by 'QueryInfo::end'.
4461+
if (stmt_info->first_comment) {
4462+
CurrentQuery.QueryParserArgs.first_comment=strdup(stmt_info->first_comment);
4463+
}
44524464
previous_status.push(PROCESSING_STMT_EXECUTE);
44534465
NEXT_IMMEDIATE(PROCESSING_STMT_PREPARE);
44544466
if (CurrentQuery.stmt_global_id!=stmt_info->statement_id) {

lib/Query_Processor.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -1319,7 +1319,7 @@ Query_Processor_Output * Query_Processor::process_mysql_query(MySQL_Session *ses
13191319
qp=&stmt_exec_qp;
13201320
qp->digest = qi->stmt_info->digest;
13211321
qp->digest_text = qi->stmt_info->digest_text;
1322-
qp->first_comment = NULL;
1322+
qp->first_comment = qi->stmt_info->first_comment;
13231323
}
13241324
}
13251325
#define stackbuffer_size 128
@@ -1710,7 +1710,9 @@ Query_Processor_Output * Query_Processor::process_mysql_query(MySQL_Session *ses
17101710
if (len < stackbuffer_size) {
17111711
// query is in the stack
17121712
} else {
1713-
l_free(len+1,query);
1713+
if (ptr) {
1714+
l_free(len+1,query);
1715+
}
17141716
}
17151717
if (sess->mirror==false) { // we process comments only on original queries, not on mirrors
17161718
if (qp && qp->first_comment) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
reg_test_3427-stmt_first_comment1-t.cpp

0 commit comments

Comments
 (0)