From: Kristofer Pettersson Date: November 13 2009 7:17pm Subject: bzr commit into mysql-5.1-bugteam branch (kristofer.pettersson:3192) Bug#21074 Bug#39253 List-Archive: http://lists.mysql.com/commits/90392 X-Bug: 21074,39253 Message-Id: <0KT2006WXAXRXY20@fe-emea-10.sun.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="Boundary_(ID_OmDmSHdpGoXDQQDRxq8L0Q)" --Boundary_(ID_OmDmSHdpGoXDQQDRxq8L0Q) MIME-version: 1.0 Content-type: text/plain; CHARSET=US-ASCII Content-transfer-encoding: 7BIT Content-disposition: inline #At file:///Users/thek/Development/mysql-5.1-qc/ based on revid:kristofer.pettersson@stripped 3192 Kristofer Pettersson 2009-11-13 Bug#39253 Large query cache still freezes server after fix for bug #21074 When the query cache is fragmented, the size of the free block lists in the memory bins grow which causes the query cache invalidation to become slow. Even if this is countered by defragmentation the hash_delete() operation still creates a bottle necks which can cause the server to hang for a short period of time depending on the size of the cache. This patch introduce a limit on the time the qc can block with a lock on SELECTs. Other operations which causes a change in the table data will still be blocked. @ sql/sql_cache.cc * Introduced a timeout value for the qc lock when entering send_result_to_client() and store_query() methods. @ sql/sql_cache.h * New signature for Query_cache::try_lock() modified: sql/sql_cache.cc sql/sql_cache.h === modified file 'sql/sql_cache.cc' --- a/sql/sql_cache.cc 2009-10-16 10:29:42 +0000 +++ b/sql/sql_cache.cc 2009-11-13 19:17:40 +0000 @@ -421,16 +421,20 @@ TYPELIB query_cache_type_typelib= effect by another thread. This enables a quick path in execution to skip waits when the outcome is known. + The upper limit for time waited is set to one second. + @return @retval FALSE An exclusive lock was taken @retval TRUE The locking attempt failed */ -bool Query_cache::try_lock(void) +bool Query_cache::try_lock(bool use_timeout) { bool interrupt= FALSE; DBUG_ENTER("Query_cache::try_lock"); - + struct timespec waittime; + set_timespec_nsec(waittime,100000000L); /* Wait for 0.1 sec */ + pthread_mutex_lock(&structure_guard_mutex); while (1) { @@ -456,7 +460,24 @@ bool Query_cache::try_lock(void) else { DBUG_ASSERT(m_cache_lock_status == Query_cache::LOCKED); - pthread_cond_wait(&COND_cache_status_changed, &structure_guard_mutex); + /* + To prevent send_result_to_client() and query_cache_insert() from + blocking execution for too long a timeout is put on the lock. + */ + if (use_timeout) + { + int res= pthread_cond_timedwait(&COND_cache_status_changed, + &structure_guard_mutex,&waittime); + if (res == ETIMEDOUT) + { + interrupt= TRUE; + break; + } + } + else + { + pthread_cond_wait(&COND_cache_status_changed, &structure_guard_mutex); + } } } pthread_mutex_unlock(&structure_guard_mutex); @@ -523,6 +544,7 @@ void Query_cache::lock(void) } + /** Set the query cache to UNLOCKED and signal waiting threads. */ @@ -1190,8 +1212,14 @@ def_week_frmt: %lu, in_trans: %d, autoco A table- or a full flush operation can potentially take a long time to finish. We choose not to wait for them and skip caching statements instead. + + In case the wait time can't be determined there is an upper limit which + causes try_lock() to abort with a time out. + + The 'TRUE' parameter indicate that the lock is allowed to timeout + */ - if (try_lock()) + if (try_lock(TRUE)) DBUG_VOID_RETURN; if (query_cache_size == 0) { @@ -1385,8 +1413,10 @@ Query_cache::send_result_to_client(THD * Try to obtain an exclusive lock on the query cache. If the cache is disabled or if a full cache flush is in progress, the attempt to get the lock is aborted. + + The 'TRUE' parameter indicate that the lock is allowed to timeout */ - if (try_lock()) + if (try_lock(TRUE)) goto err; if (query_cache_size == 0) === modified file 'sql/sql_cache.h' --- a/sql/sql_cache.h 2009-06-16 08:34:47 +0000 +++ b/sql/sql_cache.h 2009-11-13 19:17:40 +0000 @@ -485,7 +485,7 @@ protected: const char *name); my_bool in_blocks(Query_cache_block * point); - bool try_lock(void); + bool try_lock(bool use_timeout= FALSE); void lock(void); void lock_and_suspend(void); void unlock(void); --Boundary_(ID_OmDmSHdpGoXDQQDRxq8L0Q) MIME-version: 1.0 Content-type: text/bzr-bundle; CHARSET=US-ASCII; name*0="bzr/kristofer.pettersson@stripped"; name*1=3191740-exxdcyprlg51gb7i.bundle Content-transfer-encoding: 7BIT Content-disposition: inline; filename*0="bzr/kristofer.pettersson@stripped"; filename*1=3191740-exxdcyprlg51gb7i.bundle # Bazaar merge directive format 2 (Bazaar 0.90) # revision_id: kristofer.pettersson@stripped\ # exxdcyprlg51gb7i # target_branch: file:///Users/thek/Development/mysql-5.1-qc/ # testament_sha1: 713b5214d119c5635ba2ce6ff5be557f4e85637e # timestamp: 2009-11-13 20:17:50 +0100 # base_revision_id: kristofer.pettersson@stripped\ # o9l432ub6kszkla0 # # Begin bundle IyBCYXphYXIgcmV2aXNpb24gYnVuZGxlIHY0CiMKQlpoOTFBWSZTWfKvkloAA3JfgFUQXff//38v /oC////6YAiLiH3XXKVSzsA12xXatF2aWtLhkVPSYyET00RpkwTQ9TIDQaBoyGjahoAlEDSYg0JC nhHppNQDRoMgNGQNAHpDjJk0YhpoYCaGJo0yYgZGE0aaYQZMJESCaaTCZPU9J6qeyNCh6j01AGg0 DTQNADjJk0YhpoYCaGJo0yYgZGE0aaYQZMJIgmpgmjRpU8NNTE09ASPSA2ieiYRoDElpkFEIFEs1 ezSRDtG/HNAipD/XmU/Y6A629JHepDQ6nH5Yw7P1f7STh3HhkQ5bJBYKGS8/dKkIh7GcIl1Q0pq7 evvdd5eeDqm38rPX7ckJFzEhpIZzwj2Tr3ajNmD2V96gwGhsG17c3AVNnZFzMO3bJXhbW1MRZSow cOHDseDdVk2O3o8zhnek/fVVz1WG7K19PV3UrXAvwq/24t7MKRSPTi7QczsbNIhk95TF0PBz5f12 FI0u+953g2Jo6lR0VszaLKI+7rLjcqPRqXVmpJfMTMWbd7IZRkeNTqjCzPXa/hBOHB1pOqCkLVHD 9Kvf2uR2DMtoT0HkaGuEoYDq40k4k84bv0a0IcevyeTu5bFuvtMIuJczufjAe2u+IBw8Teg6XO/k 9RA4skc3FaKSMtxM/jG8TgiYW5+TKEiybxffkMdw1yDhN047m4ALs54omwFkggBa3fvpxW+ViNUk cgkyOlC7DLYEvJM/QW82FproDMS4+NOkCh4yNleG9QoaE61RQtpCjRUoDGknhMpJC7lGKin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