diff --git a/include/query_processor.h b/include/query_processor.h index dd868f702..a288b10fe 100644 --- a/include/query_processor.h +++ b/include/query_processor.h @@ -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(); diff --git a/lib/ProxySQL_Admin.cpp b/lib/ProxySQL_Admin.cpp index 528b4324a..a1ce30614 100644 --- a/lib/ProxySQL_Admin.cpp +++ b/lib/ProxySQL_Admin.cpp @@ -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 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;