From 33fba7641a37fcc38ab0777fd32a87199c19a27b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Canna=C3=B2?= Date: Fri, 19 Nov 2021 21:39:38 +0100 Subject: [PATCH] Speed up start time with multiple listeners #3699 When multiple listeners are configured start up time is slow. This commit batches together the configuration of all the listeners. --- include/MySQL_Thread.h | 3 +++ lib/MySQL_Thread.cpp | 19 ++++++++++++++++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/include/MySQL_Thread.h b/include/MySQL_Thread.h index 7a5bde801..4c09da3a2 100644 --- a/include/MySQL_Thread.h +++ b/include/MySQL_Thread.h @@ -584,6 +584,9 @@ class MySQL_Threads_Handler std::array p_counter_array {}; std::array p_gauge_array {}; } status_variables; + + std::atomic bootstrapping_listeners; + /** * @brief Update the client host cache with the supplied 'client_sockaddr', * and the supplied 'error' parameter specifying if there was a connection diff --git a/lib/MySQL_Thread.cpp b/lib/MySQL_Thread.cpp index 3a2584c40..0d84ab69f 100644 --- a/lib/MySQL_Thread.cpp +++ b/lib/MySQL_Thread.cpp @@ -1028,6 +1028,7 @@ MySQL_Threads_Handler::MySQL_Threads_Handler() { #endif // IDLE_THREADS stacksize=0; shutdown_=0; + bootstrapping_listeners = true; pthread_rwlock_init(&rwlock,NULL); pthread_attr_init(&attr); // Zero initialize all variables @@ -2368,6 +2369,10 @@ void MySQL_Threads_Handler::shutdown_threads() { } void MySQL_Threads_Handler::start_listeners() { + // we set bootstrapping_listeners to true + // In this way MySQL_Thread will knows there are more listeners to add + // and it will continue looping until all listeners are added + bootstrapping_listeners = true; char *_tmp=NULL; _tmp=GloMTH->get_variable((char *)"interfaces"); if (strlen(_tmp)==0) { @@ -2382,6 +2387,8 @@ void MySQL_Threads_Handler::start_listeners() { listener_add((char *)token); } free_tokenizer( &tok ); + // no more listeners to add + bootstrapping_listeners = false; } void MySQL_Threads_Handler::stop_listeners() { @@ -3104,9 +3111,15 @@ __run_skip_1a: #endif // IDLE_THREADS pthread_mutex_unlock(&thread_mutex); - while ((n=__sync_add_and_fetch(&mypolls.pending_listener_add,0))) { // spin here - poll_listener_add(n); - assert(__sync_bool_compare_and_swap(&mypolls.pending_listener_add,n,0)); + while ( // spin here if ... + (n=__sync_add_and_fetch(&mypolls.pending_listener_add,0)) // there is a new listener to add + || + (GloMTH->bootstrapping_listeners == true) // MySQL_Thread_Handlers has more listeners to configure + ) { + if (n) { + poll_listener_add(n); + assert(__sync_bool_compare_and_swap(&mypolls.pending_listener_add,n,0)); + } } proxy_debug(PROXY_DEBUG_NET, 7, "poll_timeout=%llu\n", mypolls.poll_timeout);