Skip to content

Commit

Permalink
Merge pull request #3453 from sysown/v2.2.0-3427
Browse files Browse the repository at this point in the history
Closes #3427: Read/Write Split query rules do not work while using sysbench to evaluate perfromance
  • Loading branch information
renecannao authored May 27, 2021
2 parents 5a6095c + 3a39483 commit e14accd
Show file tree
Hide file tree
Showing 9 changed files with 903 additions and 9 deletions.
5 changes: 3 additions & 2 deletions include/MySQL_PreparedStatement.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,15 @@ class MySQL_STMT_Global_info {
uint16_t num_params;
uint16_t warning_count;
MYSQL_FIELD **fields;
char* first_comment;
// struct {
// int cache_ttl;
// int timeout;
// int delay;
// } properties;
bool is_select_NOT_for_update;
MYSQL_BIND **params; // seems unused (?)
MySQL_STMT_Global_info(uint64_t id, char *u, char *s, char *q, unsigned int ql, MYSQL_STMT *stmt, uint64_t _h);
MySQL_STMT_Global_info(uint64_t id, char *u, char *s, char *q, unsigned int ql, char *fc, MYSQL_STMT *stmt, uint64_t _h);
void update_metadata(MYSQL_STMT *stmt);
~MySQL_STMT_Global_info();
};
Expand Down Expand Up @@ -259,7 +260,7 @@ class MySQL_STMT_Manager_v14 {
void unlock() { pthread_rwlock_unlock(&rwlock_); }
void ref_count_client(uint64_t _stmt, int _v, bool lock=true);
void ref_count_server(uint64_t _stmt, int _v, bool lock=true);
MySQL_STMT_Global_info * add_prepared_statement(char *u, char *s, char *q, unsigned int ql, MYSQL_STMT *stmt, bool lock=true);
MySQL_STMT_Global_info * add_prepared_statement(char *u, char *s, char *q, unsigned int ql, char *fc, MYSQL_STMT *stmt, bool lock=true);
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);
SQLite3_result * get_prepared_statements_global_infos();
};
Expand Down
15 changes: 12 additions & 3 deletions lib/MySQL_PreparedStatement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ void *StmtLongDataHandler::get(uint32_t _stmt_id, uint16_t _param_id,
MySQL_STMT_Global_info::MySQL_STMT_Global_info(uint64_t id,
char *u, char *s, char *q,
unsigned int ql,
char *fc,
MYSQL_STMT *stmt, uint64_t _h) {
pthread_rwlock_init(&rwlock_, NULL);
statement_id = id;
Expand All @@ -145,6 +146,11 @@ MySQL_STMT_Global_info::MySQL_STMT_Global_info(uint64_t id,
memcpy(query, q, ql);
query[ql] = '\0'; // add NULL byte
query_length = ql;
if (fc) {
first_comment = strdup(fc);
} else {
first_comment = NULL;
}
MyComQueryCmd = MYSQL_COM_QUERY__UNINITIALIZED;
num_params = stmt->param_count;
num_columns = stmt->field_count;
Expand Down Expand Up @@ -476,6 +482,9 @@ MySQL_STMT_Global_info::~MySQL_STMT_Global_info() {
free(username);
free(schemaname);
free(query);
if (first_comment) {
free(first_comment);
}
if (num_columns) {
uint16_t i;
for (i = 0; i < num_columns; i++) {
Expand Down Expand Up @@ -812,10 +821,10 @@ bool MySQL_STMTs_local_v14::client_close(uint32_t client_statement_id) {

MySQL_STMT_Global_info *MySQL_STMT_Manager_v14::add_prepared_statement(
char *u, char *s, char *q, unsigned int ql,
MYSQL_STMT *stmt, bool lock) {
char *fc, MYSQL_STMT *stmt, bool lock) {
MySQL_STMT_Global_info *ret = NULL;
uint64_t hash = stmt_compute_hash(
u, s, q, ql); // this identifies the prepared statement
u, s, q, ql); // this identifies the prepared statement
if (lock) {
pthread_rwlock_wrlock(&rwlock_);
}
Expand Down Expand Up @@ -847,7 +856,7 @@ MySQL_STMT_Global_info *MySQL_STMT_Manager_v14::add_prepared_statement(

//next_statement_id++;
MySQL_STMT_Global_info *a =
new MySQL_STMT_Global_info(next_id, u, s, q, ql, stmt, hash);
new MySQL_STMT_Global_info(next_id, u, s, q, ql, fc, stmt, hash);
// insert it in both maps
map_stmt_id_to_info.insert(std::make_pair(a->statement_id, a));
map_stmt_hash_to_info.insert(std::make_pair(a->hash, a));
Expand Down
16 changes: 14 additions & 2 deletions lib/MySQL_Session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3052,7 +3052,12 @@ void MySQL_Session::handler___status_WAITING_CLIENT_DATA___STATE_SLEEP___MYSQL_C
if (client_myds->myconn->local_stmts==NULL) {
client_myds->myconn->local_stmts=new MySQL_STMTs_local_v14(true);
}
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);
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
);
MySQL_STMT_Global_info *stmt_info=NULL;
// we first lock GloStmt
GloMyStmt->wrlock();
Expand Down Expand Up @@ -3137,7 +3142,7 @@ void MySQL_Session::handler___status_WAITING_CLIENT_DATA___STATE_SLEEP___MYSQL_C
if (thread->variables.stats_time_query_processor) {
clock_gettime(CLOCK_THREAD_CPUTIME_ID,&begint);
}
qpo=GloQPro->process_mysql_query(this,pkt.ptr,pkt.size,&CurrentQuery);
qpo=GloQPro->process_mysql_query(this,NULL,0,&CurrentQuery);
if (qpo->max_lag_ms >= 0) {
thread->status_variables.stvar[st_var_queries_with_max_lag_ms]++;
}
Expand Down Expand Up @@ -3858,6 +3863,7 @@ bool MySQL_Session::handler_rc0_PROCESSING_STMT_PREPARE(enum session_status& st,
(char *)client_myds->myconn->userinfo->schemaname,
(char *)CurrentQuery.QueryPointer,
CurrentQuery.QueryLength,
CurrentQuery.QueryParserArgs.first_comment,
CurrentQuery.mysql_stmt,
false);
if (CurrentQuery.QueryParserArgs.digest_text) {
Expand Down Expand Up @@ -4449,6 +4455,12 @@ int MySQL_Session::handler() {
stmt_info=GloMyStmt->find_prepared_statement_by_stmt_id(CurrentQuery.stmt_global_id);
CurrentQuery.QueryLength=stmt_info->query_length;
CurrentQuery.QueryPointer=(unsigned char *)stmt_info->query;
// NOTE: Update 'first_comment' with the the from the retrieved
// 'stmt_info' from the found prepared statement. 'CurrentQuery' requires its
// own copy of 'first_comment' because it will later be free by 'QueryInfo::end'.
if (stmt_info->first_comment) {
CurrentQuery.QueryParserArgs.first_comment=strdup(stmt_info->first_comment);
}
previous_status.push(PROCESSING_STMT_EXECUTE);
NEXT_IMMEDIATE(PROCESSING_STMT_PREPARE);
if (CurrentQuery.stmt_global_id!=stmt_info->statement_id) {
Expand Down
6 changes: 4 additions & 2 deletions lib/Query_Processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1319,7 +1319,7 @@ Query_Processor_Output * Query_Processor::process_mysql_query(MySQL_Session *ses
qp=&stmt_exec_qp;
qp->digest = qi->stmt_info->digest;
qp->digest_text = qi->stmt_info->digest_text;
qp->first_comment = NULL;
qp->first_comment = qi->stmt_info->first_comment;
}
}
#define stackbuffer_size 128
Expand Down Expand Up @@ -1710,7 +1710,9 @@ Query_Processor_Output * Query_Processor::process_mysql_query(MySQL_Session *ses
if (len < stackbuffer_size) {
// query is in the stack
} else {
l_free(len+1,query);
if (ptr) {
l_free(len+1,query);
}
}
if (sess->mirror==false) { // we process comments only on original queries, not on mirrors
if (qp && qp->first_comment) {
Expand Down
Loading

0 comments on commit e14accd

Please sign in to comment.