From 8a0e1c17482adb615ea30d137b4d8a48b45cf077 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Canna=C3=B2?= Date: Tue, 7 Apr 2020 00:17:22 +0200 Subject: [PATCH] Simplifying MySQL_Thread::run() phase 6 --- include/MySQL_Thread.h | 1 + lib/MySQL_Thread.cpp | 80 ++++++++++++++++++++++-------------------- 2 files changed, 43 insertions(+), 38 deletions(-) diff --git a/include/MySQL_Thread.h b/include/MySQL_Thread.h index d73ac709e..827a5ac8c 100644 --- a/include/MySQL_Thread.h +++ b/include/MySQL_Thread.h @@ -105,6 +105,7 @@ class MySQL_Thread void idle_thread_assigns_sessions_to_worker_thread(MySQL_Thread *thr); void idle_thread_check_if_worker_thread_has_unprocess_resumed_sessions_and_signal_it(MySQL_Thread *thr); void idle_thread_prepares_session_to_send_to_worker_thread(int i); + void idle_thread_to_kill_idle_sessions(); #endif // IDLE_THREADS unsigned int find_session_idx_in_mysql_sessions(MySQL_Session *sess); diff --git a/lib/MySQL_Thread.cpp b/lib/MySQL_Thread.cpp index 31ddc97d8..ebb2efe72 100644 --- a/lib/MySQL_Thread.cpp +++ b/lib/MySQL_Thread.cpp @@ -3848,10 +3848,10 @@ __run_skip_1: tune_timeout_for_session_needs_pause(myds); } } - myds->revents=0; - if (myds->myds_type!=MYDS_LISTENER) { - configure_pollout(myds, n); - } + myds->revents=0; + if (myds->myds_type!=MYDS_LISTENER) { + configure_pollout(myds, n); + } } proxy_debug(PROXY_DEBUG_NET,1,"Poll for DataStream=%p will be called with FD=%d and events=%d\n", mypolls.myds[n], mypolls.fds[n].fd, mypolls.fds[n].events); } @@ -4007,40 +4007,7 @@ __run_skip_1a: } } if (mysql_sessions->len && maintenance_loop) { -#define SESS_TO_SCAN 128 - if (mysess_idx + SESS_TO_SCAN > mysql_sessions->len) { - mysess_idx=0; - } - unsigned int i; - unsigned long long min_idle = 0; - if (curtime > (unsigned long long)mysql_thread___wait_timeout*1000) { - min_idle = curtime - (unsigned long long)mysql_thread___wait_timeout*1000; - } - for (i=0;ilen; i++) { - uint32_t sess_pos=mysess_idx; - MySQL_Session *mysess=(MySQL_Session *)mysql_sessions->index(sess_pos); - if (mysess->idle_since < min_idle) { - mysess->killed=true; - MySQL_Data_Stream *tmp_myds=mysess->client_myds; - int dsidx=tmp_myds->poll_fds_idx; - //fprintf(stderr,"Removing session %p, DS %p idx %d\n",mysess,tmp_myds,dsidx); - mypolls.remove_index_fast(dsidx); - tmp_myds->mypolls=NULL; - mysess->thread=NULL; - // we first delete the association in sessmap - sessmap.erase(mysess->thread_session_id); - if (mysql_sessions->len > 1) { - // take the last element and adjust the map - MySQL_Session *mysess_last=(MySQL_Session *)mysql_sessions->index(mysql_sessions->len-1); - if (mysess->thread_session_id != mysess_last->thread_session_id) - sessmap[mysess_last->thread_session_id]=sess_pos; - } - unregister_session(sess_pos); - resume_mysql_sessions->add(mysess); - epoll_ctl(efd, EPOLL_CTL_DEL, tmp_myds->fd, NULL); - } - mysess_idx++; - } + idle_thread_to_kill_idle_sessions(); } goto __run_skip_2; } @@ -4114,6 +4081,43 @@ unsigned int MySQL_Thread::find_session_idx_in_mysql_sessions(MySQL_Session *ses } #ifdef IDLE_THREADS +void MySQL_Thread::idle_thread_to_kill_idle_sessions() { +#define SESS_TO_SCAN 128 + if (mysess_idx + SESS_TO_SCAN > mysql_sessions->len) { + mysess_idx=0; + } + unsigned int i; + unsigned long long min_idle = 0; + if (curtime > (unsigned long long)mysql_thread___wait_timeout*1000) { + min_idle = curtime - (unsigned long long)mysql_thread___wait_timeout*1000; + } + for (i=0;ilen; i++) { + uint32_t sess_pos=mysess_idx; + MySQL_Session *mysess=(MySQL_Session *)mysql_sessions->index(sess_pos); + if (mysess->idle_since < min_idle) { + mysess->killed=true; + MySQL_Data_Stream *tmp_myds=mysess->client_myds; + int dsidx=tmp_myds->poll_fds_idx; + //fprintf(stderr,"Removing session %p, DS %p idx %d\n",mysess,tmp_myds,dsidx); + mypolls.remove_index_fast(dsidx); + tmp_myds->mypolls=NULL; + mysess->thread=NULL; + // we first delete the association in sessmap + sessmap.erase(mysess->thread_session_id); + if (mysql_sessions->len > 1) { + // take the last element and adjust the map + MySQL_Session *mysess_last=(MySQL_Session *)mysql_sessions->index(mysql_sessions->len-1); + if (mysess->thread_session_id != mysess_last->thread_session_id) + sessmap[mysess_last->thread_session_id]=sess_pos; + } + unregister_session(sess_pos); + resume_mysql_sessions->add(mysess); + epoll_ctl(efd, EPOLL_CTL_DEL, tmp_myds->fd, NULL); + } + mysess_idx++; + } +} + void MySQL_Thread::idle_thread_prepares_session_to_send_to_worker_thread(int i) { // NOTE: not sure why, sometime events returns odd values. If set, we take it out as normal worker threads know how to handle it if (events[i].events) {