List:Commits« Previous MessageNext Message »
From:kpettersson Date:November 23 2007 1:38pm
Subject:bk commit into 5.0 tree (thek:1.2537) BUG#32213
View as plain text  
Below is the list of changes that have just been committed into a local
5.0 repository of thek. When thek does a push these changes will
be propagated to the main repository and, within 24 hours after the
push, to the public repository.
For information on how to access the public repository
see http://dev.mysql.com/doc/mysql/en/installing-source-tree.html

ChangeSet@stripped, 2007-11-23 13:38:13+01:00, thek@adventure.(none) +3 -0
  Bug #32213 Impossible high query cache setting resets cache size to 0 bytes
  
  PREVIEW PATCH 
  
  Setting the query cache size to high will resize the cache to 0.
  Why not leaving the cache size on the original value?
  

  sql/set_var.cc@stripped, 2007-11-23 13:38:12+01:00, thek@adventure.(none) +1 -1
    - Removed ugly dependency on external variable in the query cache code.

  sql/sql_cache.cc@stripped, 2007-11-23 13:38:12+01:00, thek@adventure.(none) +45 -7
    - Removed ugly dependency on external variable in the query cache code.
    - Added comments in doxygen style
    - On failure to allocate memory; try to allocate last known size.

  sql/sql_cache.h@stripped, 2007-11-23 13:38:12+01:00, thek@adventure.(none) +13 -7
    - Added comments and a variable to store total_memory_allocated to work as a 
    bridge between the global query_cache_size variable and the Query_cache::
    query_cache_size member variable.

diff -Nrup a/sql/set_var.cc b/sql/set_var.cc
--- a/sql/set_var.cc	2007-07-30 17:27:30 +02:00
+++ b/sql/set_var.cc	2007-11-23 13:38:12 +01:00
@@ -1333,7 +1333,7 @@ static void fix_query_cache_size(THD *th
 {
 #ifdef HAVE_QUERY_CACHE
   ulong requested= query_cache_size;
-  query_cache.resize(query_cache_size);
+  query_cache_size= query_cache.resize(query_cache_size);
   if (requested != query_cache_size)
     push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
 			ER_WARN_QC_RESIZE, ER(ER_WARN_QC_RESIZE),
diff -Nrup a/sql/sql_cache.cc b/sql/sql_cache.cc
--- a/sql/sql_cache.cc	2007-09-03 13:46:07 +02:00
+++ b/sql/sql_cache.cc	2007-11-23 13:38:12 +01:00
@@ -806,6 +806,21 @@ Query_cache::Query_cache(ulong query_cac
 }
 
 
+/**
+  @brief Set the size of the query cache as close to the requested size as
+    possible.
+
+  This operation will invalidate the current cache.
+  If there isn't enought memory or the new size is too small the cache size
+  will be 0.
+
+  @param query_cache_size_arg The requested total amount of memory available to
+    the cache.
+
+  @return The actual amount of memory used by the cache after byte border
+    alignment.
+*/
+
 ulong Query_cache::resize(ulong query_cache_size_arg)
 {
   DBUG_ENTER("Query_cache::resize");
@@ -813,14 +828,18 @@ ulong Query_cache::resize(ulong query_ca
 			query_cache_size_arg));
   DBUG_ASSERT(initialized);
   STRUCT_LOCK(&structure_guard_mutex);
+  ulong old_memory_size= total_memory_allocated;
   free_cache();
-  query_cache_size= query_cache_size_arg;
-  ::query_cache_size= init_cache();
+  int new_memory_size= init_cache(query_cache_size_arg);
+  if (new_memory_size == 0 && query_cache_size_arg != 0)
+  {
+    /* Memory allocation failed; try to revert to previous size */
+    new_memory_size= init_cache(old_memory_size);
+  }
   STRUCT_UNLOCK(&structure_guard_mutex);
-  DBUG_RETURN(::query_cache_size);
+  DBUG_RETURN(new_memory_size);
 }
 
-
 ulong Query_cache::set_min_res_unit(ulong size)
 {
   if (size < min_allocation_unit)
@@ -1608,7 +1627,24 @@ void Query_cache::init()
 }
 
 
-ulong Query_cache::init_cache()
+/**
+  @brief Initialize the query cache.
+
+  This function adjust the member variable query_cache_size to compensate for
+  byte border alignment, approx_additional_data_size and additional_data_size
+  the latter representing the hash tables and meta data of the cache.
+
+  @param memory_limit The total amount of memory the cache is allowed
+    to use.
+
+  @note The external varaible query_cache_size as shown by the
+    SHOW query_cache_size command is not equal to the query_cache_size member
+    but to the total amount of allocated memory instead.
+
+  @return The total amount of allocated memory used by the query cache.
+*/
+
+ulong Query_cache::init_cache(ulong memory_limit)
 {
   uint mem_bin_count, num, step;
   ulong mem_bin_size, prev_size, inc;
@@ -1616,6 +1652,7 @@ ulong Query_cache::init_cache()
   int align;
 
   DBUG_ENTER("Query_cache::init_cache");
+  query_cache_size= memory_limit;
   approx_additional_data_size = (sizeof(Query_cache) +
 				 sizeof(gptr)*(def_query_hash_size+
 					       def_table_hash_size));
@@ -1771,8 +1808,9 @@ ulong Query_cache::init_cache()
 
   queries_in_cache = 0;
   queries_blocks = 0;
-  DBUG_RETURN(query_cache_size +
-	      additional_data_size + approx_additional_data_size);
+  total_memory_allocated= query_cache_size + additional_data_size +
+                          approx_additional_data_size;
+  DBUG_RETURN(total_memory_allocated);
 
 err:
   make_disabled();
diff -Nrup a/sql/sql_cache.h b/sql/sql_cache.h
--- a/sql/sql_cache.h	2007-08-17 16:55:19 +02:00
+++ b/sql/sql_cache.h	2007-11-23 13:38:12 +01:00
@@ -233,8 +233,18 @@ struct Query_cache_memory_bin_step
 class Query_cache
 {
 public:
-  /* Info */
-  ulong query_cache_size, query_cache_limit;
+  /**
+    The query cache 'free memory' or max size of result sets, queries and
+    tables; but not hash containers
+  */
+  ulong query_cache_size;
+
+  /** Result set limit */
+  ulong query_cache_limit;
+
+  /** The total memory allocated */
+  ulong total_memory_allocated;
+
   /* statistics */
   ulong free_memory, queries_in_cache, hits, inserts, refused,
     free_memory_blocks, total_blocks, lowmem_prunes;
@@ -339,11 +349,7 @@ protected:
 	      Query_cache_block *pprev);
   my_bool join_results(ulong join_limit);
 
-  /*
-    Following function control structure_guard_mutex
-    by themself or don't need structure_guard_mutex
-  */
-  ulong init_cache();
+  ulong init_cache(ulong memory_limit);
   void make_disabled();
   void free_cache();
   Query_cache_block *write_block_data(ulong data_len, gptr data,
Thread
bk commit into 5.0 tree (thek:1.2537) BUG#32213kpettersson23 Nov