Skip to content

Commit 9c0e14a

Browse files
committed
Replace rand() with lock-free Xoshiro128++ PRNG
1 parent 1251e4d commit 9c0e14a

File tree

4 files changed

+20
-18
lines changed

4 files changed

+20
-18
lines changed

lib/PgSQL_HostGroups_Manager.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -820,7 +820,7 @@ PgSQL_HostGroups_Manager::PgSQL_HostGroups_Manager() {
820820
static const char alphanum[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
821821
rand_del[0] = '-';
822822
for (int i = 1; i < 6; i++) {
823-
rand_del[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
823+
rand_del[i] = alphanum[rand_fast() % (sizeof(alphanum) - 1)];
824824
}
825825
rand_del[6] = '-';
826826
rand_del[7] = 0;
@@ -2208,11 +2208,12 @@ PgSQL_SrvC *PgSQL_HGC::get_random_MySrvC(char * gtid_uuid, uint64_t gtid_trxid,
22082208

22092209

22102210
unsigned int k;
2211-
if (New_sum > 32768) {
2212-
k=rand()%New_sum;
2213-
} else {
2214-
k=fastrand()%New_sum;
2215-
}
2211+
//if (New_sum > 32768) {
2212+
// k=rand()%New_sum;
2213+
//} else {
2214+
// k=fastrand()%New_sum;
2215+
//}
2216+
k = rand_fast() % New_sum;
22162217
k++;
22172218
New_sum=0;
22182219

@@ -2341,11 +2342,12 @@ PgSQL_Connection * PgSQL_SrvConnList::get_random_MyConn(PgSQL_Session *sess, boo
23412342
}
23422343
}
23432344
if (l && ff==false && needs_warming==false) {
2344-
if (l>32768) {
2345-
i=rand()%l;
2346-
} else {
2347-
i=fastrand()%l;
2348-
}
2345+
//if (l>32768) {
2346+
// i=rand()%l;
2347+
//} else {
2348+
// i=fastrand()%l;
2349+
//}
2350+
i = rand_fast() % l;
23492351
if (sess && sess->client_myds && sess->client_myds->myconn && sess->client_myds->myconn->userinfo) {
23502352
PgSQL_Connection * client_conn = sess->client_myds->myconn;
23512353
get_random_MyConn_inner_search(i, l, conn_found_idx, connection_quality_level, number_of_matching_session_variables, client_conn);

lib/PgSQL_Protocol.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ bool PgSQL_Protocol::generate_pkt_initial_handshake(bool send, void** _ptr, unsi
404404
// Fallback method: using a basic pseudo-random generator
405405
srand((unsigned int)time(NULL));
406406
for (size_t i = 0; i < sizeof((*myds)->tmp_login_salt); i++) {
407-
(*myds)->tmp_login_salt[i] = rand() % 256;
407+
(*myds)->tmp_login_salt[i] = rand_fast() % 256;
408408
}
409409
}
410410
pgpkt.write_generic(type, "ib", PG_PKT_AUTH_MD5, (*myds)->tmp_login_salt, sizeof((*myds)->tmp_login_salt));

lib/PgSQL_Thread.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2943,7 +2943,7 @@ void PgSQL_Thread::run() {
29432943
#ifdef IDLE_THREADS
29442944
if (GloVars.global.idle_threads) {
29452945
if (idle_maintenance_thread == false) {
2946-
int r = rand() % (GloPTH->num_threads);
2946+
int r = rand_fast() % (GloPTH->num_threads);
29472947
PgSQL_Thread* thr = GloPTH->pgsql_threads_idles[r].worker;
29482948
worker_thread_assigns_sessions_to_idle_thread(thr);
29492949
worker_thread_gets_sessions_from_idle_thread();
@@ -2967,7 +2967,7 @@ void PgSQL_Thread::run() {
29672967
// The delay for the active-wait is a fraction of 'poll_timeout'. Since other
29682968
// threads may be waiting on poll for further operations, checks are meaningless
29692969
// until that timeout expires (other workers make progress).
2970-
usleep(std::min(std::max(pgsql_thread___poll_timeout/20, 10000), 40000) + (rand() % 2000));
2970+
usleep(std::min(std::max(pgsql_thread___poll_timeout/20, 10000), 40000) + (rand_fast() % 2000));
29712971
}
29722972

29732973
proxy_debug(PROXY_DEBUG_NET, 7, "poll_timeout=%u\n", mypolls.poll_timeout);
@@ -3124,7 +3124,7 @@ void PgSQL_Thread::run() {
31243124
__run_skip_2 :
31253125
if (GloVars.global.idle_threads && idle_maintenance_thread) {
31263126
// this is an idle thread
3127-
unsigned int w = rand() % (GloPTH->num_threads);
3127+
unsigned int w = rand_fast() % (GloPTH->num_threads);
31283128
PgSQL_Thread* thr = GloPTH->pgsql_threads[w].worker;
31293129
if (resume_mysql_sessions->len) {
31303130
idle_thread_assigns_sessions_to_worker_thread(thr);

lib/Query_Processor.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ Query_Processor<QP_DERIVED>::Query_Processor(int _query_rules_fast_routing_algor
228228
rand_del[1] = '-';
229229
rand_del[2] = '-';
230230
for (int i = 3; i < 11; i++) {
231-
rand_del[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
231+
rand_del[i] = alphanum[rand_fast() % (sizeof(alphanum) - 1)];
232232
}
233233
rand_del[11] = '-';
234234
rand_del[12] = '-';
@@ -1878,9 +1878,9 @@ void Query_Processor<QP_DERIVED>::update_query_digest(uint64_t digest_total, uin
18781878
TypeConnInfo* ui, unsigned long long t, unsigned long long n, const char* client_addr, unsigned long long rows_affected,
18791879
unsigned long long rows_sent) {
18801880
QP_query_digest_stats* qds;
1881-
pthread_rwlock_wrlock(&digest_rwlock);
1881+
std::unordered_map<uint64_t, void*>::iterator it;
18821882

1883-
std::unordered_map<uint64_t, void *>::iterator it;
1883+
pthread_rwlock_wrlock(&digest_rwlock);
18841884
it=digest_umap.find(digest_total);
18851885
if (it != digest_umap.end()) {
18861886
// found

0 commit comments

Comments
 (0)