Optimize locking for 'search_rules_fast_routing_dest_hg'

- Add parameter for conditionally locking inside function.
 - Move potential syscalls outside the 'pthread_rwlock_rdlock' section.
 - Reduce number of 'sprintf' calls.
 - Improved PROXYSQLTEST 11/15 with data randomly exceeding function
   default buffer size.
pull/4182/head
Javier Jaramago Fernández 3 years ago
parent 36e5a13b8a
commit 43372bcfc9

@ -384,7 +384,9 @@ class Query_Processor {
* @return Old 'fast_routing_resultset' that has been replaced. Required to be freed by caller.
*/
SQLite3_result* load_fast_routing(const fast_routing_hashmap_t& fast_routing_hashmap);
int search_rules_fast_routing_dest_hg(khash_t(khStrInt)* _rules_fast_routing, const char* u, const char* s, int flagIN);
int search_rules_fast_routing_dest_hg(
khash_t(khStrInt)* _rules_fast_routing, const char* u, const char* s, int flagIN, bool lock
);
SQLite3_result * get_current_query_rules_fast_routing();
SQLite3_result * get_current_query_rules_fast_routing_inner();
int get_current_query_rules_fast_routing_count();

@ -7536,8 +7536,8 @@ unsigned int ProxySQL_Admin::ProxySQL_Test___GenerateRandom_mysql_query_rules_fa
rc=admindb->prepare_v2(a, &statement1);
ASSERT_SQLITE_OK(rc, admindb);
admindb->execute("DELETE FROM mysql_query_rules_fast_routing");
char * username_buf = (char *)malloc(32);
char * schemaname_buf = (char *)malloc(64);
char * username_buf = (char *)malloc(128);
char * schemaname_buf = (char *)malloc(256);
//ui.username = username_buf;
//ui.schemaname = schemaname_buf;
if (empty==false) {
@ -7548,7 +7548,7 @@ unsigned int ProxySQL_Admin::ProxySQL_Test___GenerateRandom_mysql_query_rules_fa
strcpy(schemaname_buf,"shard_name_");
int _k;
for (unsigned int i=0; i<cnt; i++) {
_k = fastrand()%20 + 1;
_k = fastrand()%117 + 1;
if (empty == false) {
for (int _i=0 ; _i<_k ; _i++) {
int b = fastrand()%10;
@ -7556,7 +7556,7 @@ unsigned int ProxySQL_Admin::ProxySQL_Test___GenerateRandom_mysql_query_rules_fa
}
username_buf[10+_k]='\0';
}
_k = fastrand()%30 + 1;
_k = fastrand()%244+ 1;
for (int _i=0 ; _i<_k ; _i++) {
int b = fastrand()%10;
schemaname_buf[11+_i]='0' + b;

@ -932,20 +932,26 @@ SQLite3_result * Query_Processor::get_current_query_rules_fast_routing() {
}
int Query_Processor::search_rules_fast_routing_dest_hg(
khash_t(khStrInt)* _rules_fast_routing, const char* u, const char* s, int flagIN
khash_t(khStrInt)* _rules_fast_routing, const char* u, const char* s, int flagIN, bool lock
) {
int dest_hg = -1;
const size_t u_len = strlen(u);
size_t keylen = u_len+strlen(rand_del)+strlen(s)+30; // 30 is a big number
char keybuf[256];
char * keybuf_ptr = keybuf;
size_t keylen = strlen(u)+strlen(rand_del)+strlen(s)+30; // 30 is a big number
if (keylen > 250) {
if (keylen >= sizeof(keybuf)) {
keybuf_ptr = (char *)malloc(keylen);
}
sprintf(keybuf_ptr,"%s%s%s---%d", u, rand_del, s, flagIN);
if (lock) {
pthread_rwlock_rdlock(&this->rwlock);
}
khiter_t k = kh_get(khStrInt, _rules_fast_routing, keybuf_ptr);
if (k == kh_end(_rules_fast_routing)) {
sprintf(keybuf_ptr,"%s%s---%d", rand_del, s, flagIN);
khiter_t k2 = kh_get(khStrInt, _rules_fast_routing, keybuf_ptr);
khiter_t k2 = kh_get(khStrInt, _rules_fast_routing, keybuf_ptr + u_len);
if (k2 == kh_end(_rules_fast_routing)) {
} else {
dest_hg = kh_val(_rules_fast_routing,k2);
@ -953,7 +959,11 @@ int Query_Processor::search_rules_fast_routing_dest_hg(
} else {
dest_hg = kh_val(_rules_fast_routing,k);
}
if (keylen > 250) {
if (lock) {
pthread_rwlock_unlock(&this->rwlock);
}
if (keylen >= sizeof(keybuf)) {
free(keybuf_ptr);
}
@ -2060,12 +2070,10 @@ __exit_process_mysql_query:
if (_thr_SQP_rules_fast_routing != nullptr) {
proxy_debug(PROXY_DEBUG_MYSQL_QUERY_PROCESSOR, 7, "Searching thread-local 'rules_fast_routing' hashmap with: user='%s', schema='%s', and flagIN='%d'\n", u, s, flagIN);
dst_hg = search_rules_fast_routing_dest_hg(_thr_SQP_rules_fast_routing, u, s, flagIN);
dst_hg = search_rules_fast_routing_dest_hg(_thr_SQP_rules_fast_routing, u, s, flagIN, false);
} else if (rules_fast_routing != nullptr) {
pthread_rwlock_rdlock(&this->rwlock);
proxy_debug(PROXY_DEBUG_MYSQL_QUERY_PROCESSOR, 7, "Searching global 'rules_fast_routing' hashmap with: user='%s', schema='%s', and flagIN='%d'\n", u, s, flagIN);
dst_hg = search_rules_fast_routing_dest_hg(rules_fast_routing, u, s, flagIN);
pthread_rwlock_unlock(&this->rwlock);
dst_hg = search_rules_fast_routing_dest_hg(rules_fast_routing, u, s, flagIN, true);
}
if (dst_hg != -1) {
@ -3191,14 +3199,8 @@ int Query_Processor::testing___find_HG_in_mysql_query_rules_fast_routing_dual(
int ret = -1;
khash_t(khStrInt)* rules_fast_routing = _rules_fast_routing ? _rules_fast_routing : this->rules_fast_routing;
if (lock) {
pthread_rwlock_rdlock(&rwlock);
}
if (rules_fast_routing) {
ret = search_rules_fast_routing_dest_hg(rules_fast_routing, username, schemaname, flagIN);
}
if (lock) {
pthread_rwlock_unlock(&rwlock);
ret = search_rules_fast_routing_dest_hg(rules_fast_routing, username, schemaname, flagIN, lock);
}
return ret;

Loading…
Cancel
Save