List:Internals« Previous MessageNext Message »
From:sanja Date:November 29 2005 11:17am
Subject:bk commit into 5.0 tree (bell:1.1998) BUG#10100
View as plain text  
Below is the list of changes that have just been committed into a local
5.0 repository of bell. When bell 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
  1.1998 05/11/29 13:17:37 bell@stripped +6 -0
  The dedicated structure (Sp_recursion_info) used to hold information
  about recursion instences of SP (begging/end of the list, first
  free instance, number of running instences). (BUG#10100)

  sql/sql_trigger.cc
    1.36 05/11/29 13:17:33 bell@stripped +2 -1
    Allocation of Sp_recursion_info for triggers.

  sql/sp_head.h
    1.78 05/11/29 13:17:33 bell@stripped +67 -21
    The dedicated structure (Sp_recursion_info) used to hold information
    about recursion instences of SP (begging/end of the list, first
    free instance, number of running instences).

  sql/sp_head.cc
    1.203 05/11/29 13:17:33 bell@stripped +26 -26
    The dedicated structure (Sp_recursion_info) used to hold information
    about recursion instences of SP (begging/end of the list, first
    free instance, number of running instences).

  sql/sp_cache.cc
    1.16 05/11/29 13:17:33 bell@stripped +8 -1
    Allocation of Sp_recursion_info for stored procedures and functions.

  sql/sp.cc
    1.103 05/11/29 13:17:33 bell@stripped +38 -24
    The dedicated structure (Sp_recursion_info) used to hold information
    about recursion instences of SP (begging/end of the list, first
    free instance, number of running instences).

  sql/item_func.cc
    1.271 05/11/29 13:17:33 bell@stripped +4 -2
    The debug output changed

# This is a BitKeeper patch.  What follows are the unified diffs for the
# set of deltas contained in the patch.  The rest of the patch, the part
# that BitKeeper cares about, is below these diffs.
# User:	bell
# Host:	sanja.is.com.ua
# Root:	/home/bell/mysql/bk/work-merge-5.0

--- 1.270/sql/item_func.cc	2005-11-23 01:12:34 +02:00
+++ 1.271/sql/item_func.cc	2005-11-29 13:17:33 +02:00
@@ -4690,10 +4690,12 @@
 {
   Field *field;
   DBUG_ENTER("Item_func_sp::sp_result_field");
-  DBUG_PRINT("info", ("sp: %s, flags: %x, level: %lu",
+  DBUG_PRINT("info", ("sp: %s, flags: %x, running: %lu",
                       (m_sp ? "YES" : "NO"),
                       (m_sp ? m_sp->m_flags : (uint)0),
-                      (m_sp ? m_sp->m_recursion_level : (ulong)0)));
+                      (m_sp ?
+                       m_sp->m_recursion_info->running_instances :
+                       (ulong)0)));
 
   if (!m_sp)
   {

--- 1.35/sql/sql_trigger.cc	2005-11-23 02:49:40 +02:00
+++ 1.36/sql/sql_trigger.cc	2005-11-29 13:17:33 +02:00
@@ -833,7 +833,8 @@
         lex_start(thd, (uchar*)trg_create_str->str, trg_create_str->length);
 
 	thd->spcont= 0;
-        if (yyparse((void *)thd) || thd->is_fatal_error)
+        if (yyparse((void *)thd) || thd->is_fatal_error ||
+            lex.sphead->attach_recursion_info(&table->mem_root))
         {
           /*
             Free lex associated resources.

--- 1.102/sql/sp.cc	2005-11-23 02:49:40 +02:00
+++ 1.103/sql/sp.cc	2005-11-29 13:17:33 +02:00
@@ -923,9 +923,9 @@
                 bool cache_only)
 {
   sp_head *sp;
-  ulong depth= (type == TYPE_ENUM_PROCEDURE ?
-                thd->variables.max_sp_recursion_depth :
-                0);
+  ulong allowed_depth= (type == TYPE_ENUM_PROCEDURE ?
+                        thd->variables.max_sp_recursion_depth :
+                        0);
 
   DBUG_ENTER("sp_find_routine");
   DBUG_PRINT("enter", ("name:  %.*s.%.*s, type: %d, cache only %d",
@@ -935,30 +935,44 @@
 
   if ((sp= sp_cache_lookup(cp, name)))
   {
-    ulong level;
-    DBUG_PRINT("info", ("found: 0x%lx", (ulong)sp));
-    if (sp->m_first_free_instance)
+    Sp_recursion_info *list_root= sp->m_recursion_info;
+    sp_head *free_sp= list_root->first_free_instance;
+    DBUG_PRINT("info", ("found: 0x%lx with root 0x%lx",
+                        (ulong)sp, (ulong)list_root));
+
+    if (free_sp)
     {
       DBUG_PRINT("info", ("first free: 0x%lx, level: %lu, flags %x",
-                          (ulong)sp->m_first_free_instance,
-                          sp->m_first_free_instance->m_recursion_level,
-                          sp->m_first_free_instance->m_flags));
-      DBUG_ASSERT(!(sp->m_first_free_instance->m_flags & sp_head::IS_INVOKED));
-      if (sp->m_first_free_instance->m_recursion_level > depth)
+                          (ulong)free_sp,
+                          list_root->running_instances,
+                          free_sp->m_flags));
+      DBUG_ASSERT(!(free_sp->m_flags & sp_head::IS_INVOKED));
+      /*
+        We compare number ranning instances with depth of recursion wich is
+        not correct in theory, but it is just optimisation, it should be:
+
+        will_be_instances > allowed_instances
+
+        where,
+          will_be_instances= list_root->running_instances + 1
+          allowed_instances= allowed_depth + 1
+
+        so we reduced the expretion on '1' from the both sides
+      */
+      if (list_root->running_instances > allowed_depth)
       {
         sp->recursion_level_error();
         DBUG_RETURN(0);
       }
-      DBUG_RETURN(sp->m_first_free_instance);
+      DBUG_RETURN(free_sp);
     }
-    level= sp->m_last_cached_sp->m_recursion_level + 1;
-    if (level > depth)
+    /* See comment to above allowed_depth comporison */
+    if (list_root->running_instances > allowed_depth)
     {
       sp->recursion_level_error();
       DBUG_RETURN(0);
     }
     {
-      sp_head *new_sp;
       const char *returns= "";
       char definer[HOSTNAME_LENGTH+USERNAME_LENGTH+2];
       String retstr(64);
@@ -969,19 +983,19 @@
         sp_returns_type(thd, retstr, sp);
         returns= retstr.ptr();
       }
-      if (db_load_routine(thd, type, name, &new_sp,
+      if (db_load_routine(thd, type, name, &free_sp,
                           sp->m_sql_mode, sp->m_params.str, returns,
                           sp->m_body.str, *sp->m_chistics, definer,
                           sp->m_created, sp->m_modified) == SP_OK)
       {
-        sp->m_last_cached_sp->m_next_cached_sp= new_sp;
-        new_sp->m_recursion_level= level;
-        new_sp->m_first_instance= sp;
-        sp->m_last_cached_sp= sp->m_first_free_instance= new_sp;
+        list_root->last_cached_sp->m_next_cached_sp= free_sp;
+        free_sp->m_recursion_info= list_root;
+        list_root->last_cached_sp= list_root->first_free_instance= free_sp;
         DBUG_PRINT("info", ("added level: 0x%lx, level: %lu, flags %x",
-                          (ulong)new_sp, new_sp->m_recursion_level,
-                          new_sp->m_flags));
-        DBUG_RETURN(new_sp);
+                          (ulong)free_sp,
+                          list_root->running_instances + 1,
+                          free_sp->m_flags));
+        DBUG_RETURN(free_sp);
       }
       DBUG_RETURN(0);
     }
@@ -992,7 +1006,7 @@
     {
       sp_cache_insert(cp, sp);
       DBUG_PRINT("info", ("added new: 0x%lx, level: %lu, flags %x",
-                          (ulong)sp, sp->m_recursion_level,
+                          (ulong)sp, sp->m_recursion_info->running_instances,
                           sp->m_flags));
     }
   }

--- 1.15/sql/sp_cache.cc	2005-10-07 03:37:20 +03:00
+++ 1.16/sql/sp_cache.cc	2005-11-29 13:17:33 +02:00
@@ -123,19 +123,26 @@
 
 void sp_cache_insert(sp_cache **cp, sp_head *sp)
 {
+  DBUG_ENTER("sp_cache_insert");
+  Sp_recursion_info *list_root;
   sp_cache *c;
   ulong v;
 
   if (!(c= *cp))
   {
     if (!(c= new sp_cache()))
-      return;                                   // End of memory error
+      DBUG_VOID_RETURN;                         // End of memory error
     c->version= Cversion;      // No need to lock when reading long variable
   }
   DBUG_PRINT("info",("sp_cache: inserting: %.*s", sp->m_qname.length,
                      sp->m_qname.str));
+  if (sp->attach_recursion_info())
+    DBUG_VOID_RETURN;                           // End of memory error
+  DBUG_PRINT("info", ("New Sp_recursion_info 0x%lx for 0x%lx ",
+                      (ulong)sp->m_recursion_info, (ulong)sp));
   c->insert(sp);
   *cp= c;                                       // Update *cp if it was NULL
+  DBUG_VOID_RETURN;
 }
 
 

--- 1.202/sql/sp_head.cc	2005-11-23 01:12:35 +02:00
+++ 1.203/sql/sp_head.cc	2005-11-29 13:17:33 +02:00
@@ -478,8 +478,8 @@
 
 sp_head::sp_head()
   :Query_arena(&main_mem_root, INITIALIZED_FOR_SP),
-   m_flags(0), m_returns_cs(NULL), m_recursion_level(0), m_next_cached_sp(0),
-   m_first_instance(this), m_first_free_instance(this), m_last_cached_sp(this)
+   m_flags(0), m_returns_cs(NULL), m_recursion_info(NULL),
+   m_next_cached_sp(NULL)
 {
   extern byte *
     sp_table_key(const byte *ptr, uint *plen, my_bool first);
@@ -949,22 +949,23 @@
   /* init per-instruction memroot */
   init_alloc_root(&execute_mem_root, MEM_ROOT_BLOCK_SIZE, 0);
 
-  DBUG_ASSERT(!(m_flags & IS_INVOKED));
+  DBUG_ASSERT(!(m_flags & IS_INVOKED) &&
+              m_recursion_info->first_free_instance == this);
   m_flags|= IS_INVOKED;
-  m_first_instance->m_first_free_instance= m_next_cached_sp;
-  DBUG_PRINT("info", ("first free for 0x%lx ++: 0x%lx->0x%lx, level: %lu, flags %x",
-                      (ulong)m_first_instance, this, m_next_cached_sp,
-                      m_next_cached_sp->m_recursion_level,
-                      m_next_cached_sp->m_flags));
+  m_recursion_info->first_free_instance= m_next_cached_sp;
+  m_recursion_info->running_instances++;
+  DBUG_PRINT("info",
+             ("first free for 0x%lx ++: 0x%lx->0x%lx, running: %lu, flags
+              %x", (ulong)m_recursion_info, (ulong)this,
+              (ulong)m_next_cached_sp, m_recursion_info->running_instances,
+              (m_next_cached_sp ? m_next_cached_sp->m_flags : 0)));
   /*
     Check that if there are not any instances after this one then
-    pointer to the last instance points on this instance or if there are
-    some instances after this one then recursion level of next instance
-    greater then recursion level of current instance on 1
+    pointer to the last instance points on this instance
   */
   DBUG_ASSERT((m_next_cached_sp == 0 &&
-               m_first_instance->m_last_cached_sp == this) ||
-              (m_recursion_level + 1 == m_next_cached_sp->m_recursion_level));
+               m_recursion_info->last_cached_sp == this) ||
+              m_next_cached_sp != 0);
 
   dbchanged= FALSE;
   if (m_db.length &&
@@ -1139,10 +1140,12 @@
       ret= mysql_change_db(thd, olddb, 1);
   }
   m_flags&= ~IS_INVOKED;
-  DBUG_PRINT("info", ("first free for 0x%lx --: 0x%lx->0x%lx, level: %lu, flags %x",
-                      (ulong)m_first_instance,
-                      m_first_instance->m_first_free_instance, this,
-                      m_recursion_level, m_flags));
+  m_recursion_info->running_instances--;
+  DBUG_PRINT("info",
+             ("first free for 0x%lx --: 0x%lx->0x%lx, running: %lu, flags %x",
+              (ulong)m_recursion_info,
+              (ulong)m_recursion_info->first_free_instance, (ulong)this,
+              m_recursion_info->running_instances, m_flags));
   /*
     Check that we have one of following:
 
@@ -1151,17 +1154,14 @@
     ther are not other instances after this one in the list)
 
     2) There are some free instances which mean that first free instance
-    should go just after this one and recursion level of that free instance
-    should be on 1 more then recursion leven of this instance.
+    should go just after this one.
   */
-  DBUG_ASSERT((m_first_instance->m_first_free_instance == 0 &&
-               this == m_first_instance->m_last_cached_sp &&
+  DBUG_ASSERT((m_recursion_info->first_free_instance == 0 &&
+               this == m_recursion_info->last_cached_sp &&
                m_next_cached_sp == 0) ||
-              (m_first_instance->m_first_free_instance != 0 &&
-               m_first_instance->m_first_free_instance == m_next_cached_sp &&
-               m_first_instance->m_first_free_instance->m_recursion_level ==
-               m_recursion_level + 1));
-  m_first_instance->m_first_free_instance= this;
+              (m_recursion_info->first_free_instance != 0 &&
+               m_recursion_info->first_free_instance == m_next_cached_sp));
+  m_recursion_info->first_free_instance= this;
   DBUG_RETURN(ret);
 }
 

--- 1.77/sql/sp_head.h	2005-11-23 01:12:35 +02:00
+++ 1.78/sql/sp_head.h	2005-11-29 13:17:33 +02:00
@@ -99,6 +99,51 @@
 sp_name *
 sp_name_current_db_new(THD *thd, LEX_STRING name);
 
+/*
+  Sp_recursion_info -- an auxiliary structure for stored procedure cache
+  describing one SP.
+*/
+
+struct Sp_recursion_info: public Sql_alloc
+{
+  /*
+    Pointer to the first element of the list of all cached instances
+    of the this stored routine.
+  */
+  sp_head *first_instance;
+  /*
+    Pointer to the first free (non-INVOKED) routine in the list of
+    cached instances for this SP. This pointer is set only for the first
+    SP in the list of instences (see above first_cached_sp pointer).
+    The pointer equal to 0 if we have no free instances.
+  */
+  sp_head *first_free_instance;
+  /* Pointer to the last element in the list of all instances of the SP. */
+  sp_head *last_cached_sp;
+  /*
+    A number of running instences of current SP.
+
+    It used to check recursion depth. Relation between a number of running
+    instances and recursion depth is shown in the table below:
+
+    Number of running instances | depth (level) of recursion
+    ----------------------------+---------------------------
+                              0 |           no recursion (0)
+                              1 |           no recursion (0)
+                              2 |                          1
+                              3 |                          2
+                               ...
+                              N |                        N-1
+  */
+  ulong running_instances;
+
+  Sp_recursion_info(sp_head *first_instance)
+    :first_instance(first_instance), first_free_instance(first_instance),
+    last_cached_sp(first_instance), running_instances(0)
+  {}
+};
+
+/* sp_head -- a parsed body of a stored procedure */
 
 class sp_head :private Query_arena
 {
@@ -143,32 +188,18 @@
   LEX_STRING m_definer_host;
   longlong m_created;
   longlong m_modified;
-  /* Recursion level of the current SP instance. The levels are numbered from 0 */
-  ulong m_recursion_level;
   /*
-    A list of diferent recursion level instances for the same procedure.
-    For every recursion level we have a sp_head instance. This instances
-    connected in the list. The list ordered by increasing recursion level
-    (m_recursion_level).
+    A list of all instances of this stored procedure. For every recursion
+    level we have an own sp_head instance. These instances are connected in
+    a list. The list is ordered by increasing level of recursion.
   */
   sp_head *m_next_cached_sp;
   /*
-    Pointer to the first element of the above list
+    A common block describing recursion info for a set of instances of the
+    same SP. It's shared between all instances of the SP and is allocated
+    in the memory root of the first instance.
   */
-  sp_head *m_first_instance;
-  /*
-    Pointer to the first free (non-INVOKED) routine in the list of
-    cached instances for this SP. This pointer is set only for the first
-    SP in the list of instences (see above m_first_cached_sp pointer).
-    The pointer equal to 0 if we have no free instances.
-    For non-first instance value of this pointer meanless (point to itself);
-  */
-  sp_head *m_first_free_instance;
-  /*
-    Pointer to the last element in the list of instances of the SP.
-    For non-first instance value of this pointer meanless (point to itself);
-  */
-  sp_head *m_last_cached_sp;
+  Sp_recursion_info *m_recursion_info;
   /*
     Set containing names of stored routines used by this routine.
     Note that unlike elements of similar set for statement elements of this
@@ -336,6 +367,21 @@
 #ifndef DBUG_OFF
   int show_routine_code(THD *thd);
 #endif
+
+
+  bool attach_recursion_info()
+  {
+    return attach_recursion_info(&main_mem_root);
+  }
+
+
+  bool attach_recursion_info(MEM_ROOT *mem_root)
+  {
+    DBUG_ASSERT(m_recursion_info == 0);
+    if (!(m_recursion_info= new(mem_root) Sp_recursion_info(this)))
+       return TRUE;             // End of memory error
+    return FALSE;
+  }
 
 
 private:
Thread
bk commit into 5.0 tree (bell:1.1998) BUG#10100sanja29 Nov