From 26c9f1da82906532d5f132b5fa9f4ab9425d687e Mon Sep 17 00:00:00 2001
From: unknown <chuck.bell@oracle.com>
Date: Thu, 15 Dec 2011 16:16:48 -0500
Subject: [PATCH] BUG#12969301 : mysql_plugin: enable is ignored if plugin
 exists

This patch changes the mechanism by which the client enables a
plugin. Instead of using INSERT IGNORE to reload a plugin library,
it now uses REPLACE INTO. This allows users to load a library
multiple times replacing the existing values in the mysql.plugin
table. This allows users to replace the symbol reference to a
different dl name in the table. Thus permitting enabling of
multiple versions of the same library without first disabling
the old version.

A regression test was added to ensure this feature works.
---
 client/mysql_plugin.c                    | 10 ++-
 mysql-test/include/libdaemon_example.ini |  9 +++
 mysql-test/t/mysql_plugin.test           | 77 +++++++++++++++++++++++-
 3 files changed, 91 insertions(+), 5 deletions(-)
 create mode 100644 mysql-test/include/libdaemon_example.ini

diff --git a/client/mysql_plugin.c b/client/mysql_plugin.c
index fb2a031bb8e..15f67e2b3e7 100644
--- a/client/mysql_plugin.c
+++ b/client/mysql_plugin.c
@@ -1018,7 +1018,7 @@ static int find_plugin(char *tp_path)
   Build the boostrap file.
   
   Create a new file and populate it with SQL commands to ENABLE or DISABLE
-  the plugin via INSERT and DELETE operations on the mysql.plugin table.
+  the plugin via REPLACE and DELETE operations on the mysql.plugin table.
 
   param[in]  operation  The type of operation (ENABLE or DISABLE)
   param[out] bootstrap  A FILE* pointer
@@ -1035,12 +1035,16 @@ static int build_bootstrap_file(char *operation, char *bootstrap)
     Perform plugin operation : ENABLE or DISABLE
 
     The following creates a temporary bootstrap file and populates it with
-    the appropriate SQL commands for the operation. For ENABLE, INSERT
+    the appropriate SQL commands for the operation. For ENABLE, REPLACE
     statements are created. For DISABLE, DELETE statements are created. The
     values for these statements are derived from the plugin_data read from the
     <plugin_name>.ini configuration file. Once the file is built, a call to
     mysqld is made in read only, bootstrap modes to read the SQL statements
     and execute them.
+    
+    Note: Replace was used so that if a user loads a newer version of a
+          library with a different library name, the new library name is
+          used for symbols that match. 
   */
   if ((error= make_tempfile(bootstrap, "sql")))
   {
@@ -1057,7 +1061,7 @@ static int build_bootstrap_file(char *operation, char *bootstrap)
   if (strcasecmp(operation, "enable") == 0)
   {
     int i= 0;
-    fprintf(file, "INSERT IGNORE INTO mysql.plugin VALUES ");
+    fprintf(file, "REPLACE INTO mysql.plugin VALUES ");
     for (i= 0; i < (int)array_elements(plugin_data.components); i++)
     {
       /* stop when we read the end of the symbol list - marked with NULL */
diff --git a/mysql-test/include/libdaemon_example.ini b/mysql-test/include/libdaemon_example.ini
new file mode 100644
index 00000000000..7e7df5bd2a2
--- /dev/null
+++ b/mysql-test/include/libdaemon_example.ini
@@ -0,0 +1,9 @@
+#
+# Plugin configuration file. Place the following on a separate line:
+#
+# library binary file name (without .so or .dll)
+# component_name
+# [component_name] - additional components in plugin
+#
+liblibdaemon_example
+daemon_example
diff --git a/mysql-test/t/mysql_plugin.test b/mysql-test/t/mysql_plugin.test
index c5968df85f8..71617b86330 100644
--- a/mysql-test/t/mysql_plugin.test
+++ b/mysql-test/t/mysql_plugin.test
@@ -155,6 +155,74 @@ eval INSERT INTO mysql.plugin VALUES ('wonky', '$DAEMONEXAMPLE');
 --replace_regex /\.dll/.so/
 SELECT * FROM mysql.plugin WHERE dl like 'libdaemon%' ORDER BY name;
 
+# MTR will remove this file later, but this might be too late.
+--error 0,1
+--remove_file $expect_file
+--write_file $expect_file
+wait
+EOF
+--shutdown_server 10
+--source include/wait_until_disconnected.inc
+
+#
+# Disable the plugin - to remove winky, wonky entries
+#
+--exec $MYSQL_PLUGIN_CMD DISABLE daemon_example
+
+#
+# Enable the plugin again
+#
+--exec $MYSQL_PLUGIN_CMD ENABLE daemon_example
+
+#
+# Restart the server
+#
+--append_file $expect_file
+restart
+EOF
+--enable_reconnect
+--source include/wait_until_connected_again.inc
+
+--echo #
+--echo # Ensure the plugin is loaded.
+--echo #
+--replace_regex /\.dll/.so/
+SELECT * FROM mysql.plugin WHERE dl like '%libdaemon%' ORDER BY name;
+
+# MTR will remove this file later, but this might be too late.
+--error 0,1
+--remove_file $expect_file
+--write_file $expect_file
+wait
+EOF
+--shutdown_server 10
+--source include/wait_until_disconnected.inc
+
+# To test the case where the same plugin is reloaded with a different soname,
+# we must copy the example daemon to a new location renaming it.
+
+let $DAEMON_RELOAD = lib$DAEMONEXAMPLE;
+--copy_file $PLUGIN_DIR/$DAEMONEXAMPLE $PLUGIN_DIR/$DAEMON_RELOAD
+--copy_file include/libdaemon_example.ini $PLUGIN_DIR/libdaemon_example.ini
+
+# Now reload it and see that it is a different name.
+--exec $MYSQL_PLUGIN_CMD ENABLE libdaemon_example 
+
+#
+# Restart the server
+#
+--append_file $expect_file
+restart
+EOF
+--enable_reconnect
+--source include/wait_until_connected_again.inc
+
+--echo #
+--echo # Ensure the plugin is replaced.
+--echo #
+--replace_regex /\.dll/.so/
+SELECT * FROM mysql.plugin WHERE dl like '%libdaemon%' ORDER BY name;
+
 --echo #
 --echo # Disable the plugin...
 --echo #
@@ -170,7 +238,12 @@ EOF
 #
 # Disable the plugin
 #
---exec $MYSQL_PLUGIN_CMD DISABLE daemon_example
+--exec $MYSQL_PLUGIN_CMD DISABLE libdaemon_example
+
+# Remove files for last test case.
+
+--remove_file $PLUGIN_DIR/$DAEMON_RELOAD
+--remove_file $DAEMONEXAMPLE_DIR/libdaemon_example.ini
 
 #
 # Restart the server
@@ -184,7 +257,7 @@ EOF
 --echo #
 --echo # Ensure the plugin isn't loaded.
 --echo #
-SELECT * FROM mysql.plugin WHERE dl like 'libdaemon%' ORDER BY name;
+SELECT * FROM mysql.plugin WHERE dl like '%libdaemon%' ORDER BY name;
 
 #
 # Stop the server for error conditions
-- 
2.30.9