List:Commits« Previous MessageNext Message »
From:Dao-Gang.Qu Date:July 30 2009 8:23am
Subject:bzr commit into mysql-5.1-bugteam branch (Dao-Gang.Qu:3034) Bug#44331
View as plain text  
#At file:///home/daogangq/mysql/bzrwork/bug44331/5.1-bugteam/ based on revid:epotemkin@stripped

 3034 Dao-Gang.Qu@stripped	2009-07-30
      Bug #44331  Restore of database with events produces warning in replication
      
      The real problem is that if the EVENT is created without DEFINER clause set explicitly
      or the definer is set to CURRENT_USER, the definer is not consistent between master 
      and slave. Because the DEFINER is set to CURRENT_USER of the current thread. 
      The CURRENT_USER is mysqld startup user on master, but the CURRENT_USER is empty 
      on slave for slave SQL thread.
      
      To fix the problem, if definer is not set explicitly, a DEFINER clause
      will be added when writing the query into binlog; if 'CURRENT_USER' is
      used as the DEFINER, it will be replaced with the value of current user
      before writing to binlog.
     @ mysql-test/suite/rpl/r/rpl_drop_if_exists.result
        Updated the result file after fixing bug#44331
     @ mysql-test/suite/rpl/r/rpl_events.result
        Test result of Bug#44331
     @ mysql-test/suite/rpl/r/rpl_innodb_mixed_dml.result
        Updated the result file after fixing bug#44331
     @ mysql-test/suite/rpl/t/rpl_events.test
        Added test to verify if the definer is consistent between master and slave
        when the event is created without DEFINER clause set explicitly or the
        DEFINER is set to CURRENT_USER
     @ sql/events.cc
        The "create_query_string" function is added for creating a new query string 
        and insert the DEFINER clause to it.
        Added a new parameter "definer_using_cur_user" to 'Events::create_event' function for
        indicating the mode of the definer is set.
        The value of CURRENT_USER will be written into the binary log 
        as default definer for slave SQL thread.
     @ sql/sql_lex.h
        Added the offset variable to LEX for recording the current offset address.

    modified:
      mysql-test/suite/rpl/r/rpl_drop_if_exists.result
      mysql-test/suite/rpl/r/rpl_events.result
      mysql-test/suite/rpl/r/rpl_innodb_mixed_dml.result
      mysql-test/suite/rpl/t/rpl_events.test
      sql/events.cc
      sql/events.h
      sql/sql_lex.h
      sql/sql_parse.cc
      sql/sql_yacc.yy
=== modified file 'mysql-test/suite/rpl/r/rpl_drop_if_exists.result'
--- a/mysql-test/suite/rpl/r/rpl_drop_if_exists.result	2009-02-11 17:46:43 +0000
+++ b/mysql-test/suite/rpl/r/rpl_drop_if_exists.result	2009-07-30 08:23:28 +0000
@@ -43,7 +43,7 @@ master-bin.000001	#	Query	#	#	use `test`
 master-bin.000001	#	Query	#	#	DROP DATABASE IF EXISTS db_bug_13684
 master-bin.000001	#	Query	#	#	CREATE DATABASE db_bug_13684
 master-bin.000001	#	Query	#	#	use `test`; CREATE TABLE db_bug_13684.t (a int)
-master-bin.000001	#	Query	#	#	use `test`; CREATE EVENT db_bug_13684.e
+master-bin.000001	#	Query	#	#	use `test`; CREATE DEFINER=`root`@`localhost` EVENT  db_bug_13684.e
 ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 HOUR
 DO
 UPDATE db_bug_13684.t SET a = a + 1
@@ -75,7 +75,7 @@ master-bin.000001	#	Query	#	#	use `test`
 master-bin.000001	#	Query	#	#	DROP DATABASE IF EXISTS db_bug_13684
 master-bin.000001	#	Query	#	#	CREATE DATABASE db_bug_13684
 master-bin.000001	#	Query	#	#	use `test`; CREATE TABLE db_bug_13684.t (a int)
-master-bin.000001	#	Query	#	#	use `test`; CREATE EVENT db_bug_13684.e
+master-bin.000001	#	Query	#	#	use `test`; CREATE DEFINER=`root`@`localhost` EVENT  db_bug_13684.e
 ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 HOUR
 DO
 UPDATE db_bug_13684.t SET a = a + 1

=== modified file 'mysql-test/suite/rpl/r/rpl_events.result'
--- a/mysql-test/suite/rpl/r/rpl_events.result	2009-01-30 13:44:49 +0000
+++ b/mysql-test/suite/rpl/r/rpl_events.result	2009-07-30 08:23:28 +0000
@@ -191,5 +191,25 @@ select * from t28953;
 END;|
 ALTER EVENT event1 RENAME TO event2;
 DROP EVENT event2;
+CREATE TABLE test.t1(details CHAR(30));
+CREATE EVENT event44331_1 ON SCHEDULE AT CURRENT_TIMESTAMP
+ON COMPLETION PRESERVE DISABLE
+DO INSERT INTO test.t1 VALUES('event event44331_1 fired - no definer');
+CREATE DEFINER=CURRENT_USER EVENT event44331_2 ON SCHEDULE AT CURRENT_TIMESTAMP
+ON COMPLETION PRESERVE DISABLE
+DO INSERT INTO test.t1 VALUES('event event44331_2 fired - DEFINER=CURRENT_USER');
+select EVENT_SCHEMA, EVENT_NAME, DEFINER from information_schema.events
+where EVENT_NAME='event44331_1' || EVENT_NAME='event44331_2';
+EVENT_SCHEMA	EVENT_NAME	DEFINER
+test	event44331_1	root@localhost
+test	event44331_2	root@localhost
+select EVENT_SCHEMA, EVENT_NAME, DEFINER from information_schema.events
+where EVENT_NAME='event44331_1' || EVENT_NAME='event44331_2';
+EVENT_SCHEMA	EVENT_NAME	DEFINER
+test	event44331_1	root@localhost
+test	event44331_2	root@localhost
 SET @@global.event_scheduler= @old_event_scheduler;
 DROP TABLE t28953;
+DROP TABLE t1;
+DROP EVENT event44331_1;
+DROP EVENT event44331_2;

=== modified file 'mysql-test/suite/rpl/r/rpl_innodb_mixed_dml.result'
--- a/mysql-test/suite/rpl/r/rpl_innodb_mixed_dml.result	2009-05-31 05:44:41 +0000
+++ b/mysql-test/suite/rpl/r/rpl_innodb_mixed_dml.result	2009-07-30 08:23:28 +0000
@@ -690,7 +690,7 @@ test_rpl	e1	root@localhost	SYSTEM	RECURR
 USE test_rpl;
 SHOW EVENTS;
 Db	Name	Definer	Time zone	Type	Execute at	Interval value	Interval field	Starts	Ends	Status	Originator	character_set_client	collation_connection	Database Collation
-test_rpl	e1	@	SYSTEM	RECURRING	NULL	1	#	#	NULL	SLAVESIDE_DISABLED	1	latin1	latin1_swedish_ci	latin1_swedish_ci
+test_rpl	e1	root@localhost	SYSTEM	RECURRING	NULL	1	#	#	NULL	SLAVESIDE_DISABLED	1	latin1	latin1_swedish_ci	latin1_swedish_ci
 ==========MASTER==========
 SELECT COUNT(*) FROM t1;
 COUNT(*)
@@ -1076,7 +1076,7 @@ master-bin.000001	#	Query	1	#	use `test_
 master-bin.000001	#	Query	1	#	BEGIN
 master-bin.000001	#	Query	1	#	use `test_rpl`; INSERT INTO t1 VALUES(1, 'test1')
 master-bin.000001	#	Xid	1	#	#
-master-bin.000001	#	Query	1	#	use `test_rpl`; CREATE EVENT e1 ON SCHEDULE EVERY '1' SECOND COMMENT 'e_second_comment' DO DELETE FROM t1
+master-bin.000001	#	Query	1	#	use `test_rpl`; CREATE DEFINER=`root`@`localhost` EVENT  e1 ON SCHEDULE EVERY '1' SECOND COMMENT 'e_second_comment' DO DELETE FROM t1
 master-bin.000001	#	Query	1	#	use `test_rpl`; ALTER EVENT e1 RENAME TO e2
 master-bin.000001	#	Query	1	#	use `test_rpl`; DROP EVENT e2
 master-bin.000001	#	Query	1	#	BEGIN

=== modified file 'mysql-test/suite/rpl/t/rpl_events.test'
--- a/mysql-test/suite/rpl/t/rpl_events.test	2009-01-30 13:44:49 +0000
+++ b/mysql-test/suite/rpl/t/rpl_events.test	2009-07-30 08:23:28 +0000
@@ -46,7 +46,29 @@ connection master;
 
 DROP EVENT event2;
 
+#
+# BUG#44331
+# This test verifies if the definer is consistent between master and slave,
+# when the event is created without DEFINER clause set explicitly or the 
+# DEFINER is set to CURRENT_USER
+#
+CREATE TABLE test.t1(details CHAR(30));
+
+CREATE EVENT event44331_1 ON SCHEDULE AT CURRENT_TIMESTAMP
+  ON COMPLETION PRESERVE DISABLE
+  DO INSERT INTO test.t1 VALUES('event event44331_1 fired - no definer');
+
+CREATE DEFINER=CURRENT_USER EVENT event44331_2 ON SCHEDULE AT CURRENT_TIMESTAMP
+  ON COMPLETION PRESERVE DISABLE
+  DO INSERT INTO test.t1 VALUES('event event44331_2 fired - DEFINER=CURRENT_USER');
+
+select EVENT_SCHEMA, EVENT_NAME, DEFINER from information_schema.events
+  where EVENT_NAME='event44331_1' || EVENT_NAME='event44331_2';
+
 sync_slave_with_master;
+connection slave;
+select EVENT_SCHEMA, EVENT_NAME, DEFINER from information_schema.events
+  where EVENT_NAME='event44331_1' || EVENT_NAME='event44331_2';
 
 # Doing cleanup of the table referred to in the event to guarantee
 # that there is no bad timing cauing it to try to access the table.
@@ -54,4 +76,7 @@ sync_slave_with_master;
 connection master;
 SET @@global.event_scheduler= @old_event_scheduler;
 DROP TABLE t28953;
+DROP TABLE t1;
+DROP EVENT event44331_1;
+DROP EVENT event44331_2;
 sync_slave_with_master;

=== modified file 'sql/events.cc'
--- a/sql/events.cc	2009-04-09 06:22:06 +0000
+++ b/sql/events.cc	2009-07-30 08:23:28 +0000
@@ -342,12 +342,43 @@ common_1_lev_code:
 
 
 /**
+  Create a new query string and insert the DEFINER clause to it.
+  
+  @param[in] thd                 Thread handler
+  @param[in] buf                 Query string
+
+  @return
+             0           ok
+             1           error
+*/
+static int
+create_query_string(THD *thd, String *buf)
+{
+  /* Append the "CREATE" part of the query */
+  if (buf->append(STRING_WITH_LEN("CREATE ")))
+    return 1;
+  /* Append definer */
+  append_definer(thd, buf, &(thd->lex->definer->user), &(thd->lex->definer->host));
+  /* Append the "EVENT" part of the query */
+  if (buf->append(STRING_WITH_LEN("EVENT ")))
+    return 1;
+  /* Append the left part of thd->query after event keyword */
+  if (buf->append(thd->lex->offset))
+    return 1;
+
+  return 0;
+}
+
+
+/**
   Create a new event.
 
   @param[in,out]  thd            THD
   @param[in]      parse_data     Event's data from parsing stage
   @param[in]      if_not_exists  Whether IF NOT EXISTS was
                                  specified
+  @param[in]      definer_using_cur_user  The mode of the definer is set
+
   In case there is an event with the same name (db) and
   IF NOT EXISTS is specified, an warning is put into the stack.
   @sa Events::drop_event for the notes about locking, pre-locking
@@ -359,7 +390,7 @@ common_1_lev_code:
 
 bool
 Events::create_event(THD *thd, Event_parse_data *parse_data,
-                     bool if_not_exists)
+                     bool if_not_exists, bool definer_using_cur_user)
 {
   int ret;
   DBUG_ENTER("Events::create_event");
@@ -439,7 +470,21 @@ Events::create_event(THD *thd, Event_par
     {
       /* Binlog the create event. */
       DBUG_ASSERT(thd->query && thd->query_length);
-      write_bin_log(thd, TRUE, thd->query, thd->query_length);
+      String log_query;
+      if (definer_using_cur_user)
+      {
+        if (create_query_string(thd, &log_query))
+        {
+          sql_print_error("Event Error: An error occurred while creating query string, "
+                          "before write it into binary log.");
+          DBUG_RETURN(TRUE);
+        }
+        /* If the definer is not set or set to CURRENT_USER, the value of CURRENT_USER 
+           will be written into the binary log as definer for slave SQL thread. */
+        write_bin_log(thd, TRUE, log_query.c_ptr(), log_query.length());
+      }
+      else
+        write_bin_log(thd, TRUE, thd->query, thd->query_length);
     }
   }
   pthread_mutex_unlock(&LOCK_event_metadata);

=== modified file 'sql/events.h'
--- a/sql/events.h	2007-08-15 15:08:44 +0000
+++ b/sql/events.h	2009-07-30 08:23:28 +0000
@@ -107,7 +107,8 @@ public:
   switch_event_scheduler_state(enum enum_opt_event_scheduler new_state);
 
   static bool
-  create_event(THD *thd, Event_parse_data *parse_data, bool if_exists);
+  create_event(THD *thd, Event_parse_data *parse_data,
+               bool if_exists, bool definer_using_cur_user);
 
   static bool
   update_event(THD *thd, Event_parse_data *parse_data,

=== modified file 'sql/sql_lex.h'
--- a/sql/sql_lex.h	2009-07-10 23:12:13 +0000
+++ b/sql/sql_lex.h	2009-07-30 08:23:28 +0000
@@ -1529,6 +1529,8 @@ typedef struct st_lex : public Query_tab
   SELECT_LEX *current_select;
   /* list of all SELECT_LEX */
   SELECT_LEX *all_selects_list;
+  /* record the current offset address */
+  const char *offset;
 
   char *length,*dec,*change;
   LEX_STRING name;

=== modified file 'sql/sql_parse.cc'
--- a/sql/sql_parse.cc	2009-07-16 12:43:17 +0000
+++ b/sql/sql_parse.cc	2009-07-30 08:23:28 +0000
@@ -3707,6 +3707,13 @@ end_with_restore_list:
                "function calls as part of this statement");
       break;
     }
+ 
+    bool definer_using_cur_user= FALSE;
+    if (!lex->definer ||
+        ((lex->definer->user.str == thd->security_ctx->priv_user) &&
+        (lex->definer->host.str == thd->security_ctx->priv_host)))
+      /* The definer is not set or set to CURRENT_USER */
+      definer_using_cur_user= TRUE;
 
     res= sp_process_definer(thd);
     if (res)
@@ -3717,7 +3724,8 @@ end_with_restore_list:
     {
       bool if_not_exists= (lex->create_info.options &
                            HA_LEX_CREATE_IF_NOT_EXISTS);
-      res= Events::create_event(thd, lex->event_parse_data, if_not_exists);
+      res= Events::create_event(thd, lex->event_parse_data, 
+                                if_not_exists, definer_using_cur_user);
       break;
     }
     case SQLCOM_ALTER_EVENT:

=== modified file 'sql/sql_yacc.yy'
--- a/sql/sql_yacc.yy	2009-07-16 12:43:17 +0000
+++ b/sql/sql_yacc.yy	2009-07-30 08:23:28 +0000
@@ -13014,6 +13014,9 @@ no_definer:
               case).
             */
             YYTHD->lex->definer= 0;
+            /* Record the current offset address */
+            Lex_input_stream *lip= YYLIP;
+            Lex->offset= lip->get_ptr();
           }
         ;
 
@@ -13021,6 +13024,9 @@ definer:
           DEFINER_SYM EQ user
           {
             YYTHD->lex->definer= get_current_user(YYTHD, $3);
+            /* Record the current offset address */
+            Lex_input_stream *lip= YYLIP;
+            Lex->offset= lip->get_ptr();
           }
         ;
 


Attachment: [text/bzr-bundle] bzr/dao-gang.qu@sun.com-20090730082328-jns46161rn8rgw0f.bundle
Thread
bzr commit into mysql-5.1-bugteam branch (Dao-Gang.Qu:3034) Bug#44331Dao-Gang.Qu30 Jul
  • Re: bzr commit into mysql-5.1-bugteam branch (Dao-Gang.Qu:3034)Bug#44331Alfranio Correia30 Jul
    • Re: bzr commit into mysql-5.1-bugteam branch (Dao-Gang.Qu:3034)Bug#44331Daogang Qu31 Jul
    • Re: bzr commit into mysql-5.1-bugteam branch (Dao-Gang.Qu:3034)Bug#44331Daogang Qu3 Aug