From 7823072dad68af8ea575612f971acd444490fbe2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Canna=C3=B2?= Date: Mon, 17 Apr 2017 10:11:43 +0000 Subject: [PATCH] Unbalanced traffic with high weights #975 If total mysql_servers.weight per hostgroup was higher than 64K, traffic was unbalanced Conflicts: include/gen_utils.h lib/MySQL_HostGroups_Manager.cpp --- include/gen_utils.h | 2 +- lib/MySQL_HostGroups_Manager.cpp | 13 +++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/include/gen_utils.h b/include/gen_utils.h index 86b99cdf8..3b2f0384a 100644 --- a/include/gen_utils.h +++ b/include/gen_utils.h @@ -15,7 +15,7 @@ static unsigned int l_near_pow_2 (unsigned int n) { #ifndef def_fastrand inline int fastrand() { g_seed = (214013*g_seed+2531011); - return (g_seed>>16); + return (g_seed>>16)&0x7FFF; } #define def_fastrand #endif diff --git a/lib/MySQL_HostGroups_Manager.cpp b/lib/MySQL_HostGroups_Manager.cpp index 14032de9f..4ef1d34e6 100644 --- a/lib/MySQL_HostGroups_Manager.cpp +++ b/lib/MySQL_HostGroups_Manager.cpp @@ -1147,7 +1147,12 @@ MySrvC *MyHGC::get_random_MySrvC() { return NULL; // if we reach here, we couldn't find any target } - unsigned int k=fastrand()%New_sum; + unsigned int k; + if (New_sum > 32768) { + k=rand()%New_sum; + } else { + k=fastrand()%New_sum; + } k++; New_sum=0; @@ -1184,7 +1189,11 @@ MySQL_Connection * MySrvConnList::get_random_MyConn() { unsigned int i; unsigned int l=conns_length(); if (l) { - i=fastrand()%l; + if (l>32768) { + i=rand()%l; + } else { + i=fastrand()%l; + } conn=(MySQL_Connection *)conns->remove_index_fast(i); proxy_debug(PROXY_DEBUG_MYSQL_CONNPOOL, 7, "Returning MySQL Connection %p, server %s:%d\n", conn, conn->parent->address, conn->parent->port); return conn;