List:Commits« Previous MessageNext Message »
From:Magnus Blåudd Date:March 23 2011 10:02am
Subject:bzr commit into mysql-5.1-telco-7.0 branch (magnus.blaudd:4254)
View as plain text  
#At file:///C:/mysql/7.0-dist-priv/ based on revid:magnus.blaudd@stripped

 4254 Magnus Blåudd	2011-03-23
      ndb
       - refactor the dist_priv code into it's own header file ndb_dist_priv_util.h to avoid code duplication
        and improve encapsulation

    added:
      sql/ndb_dist_priv_util.h
    modified:
      sql/ha_ndbcluster.cc
      sql/ha_ndbcluster_binlog.cc
      sql/ha_ndbcluster_binlog.h
      sql/ha_ndbcluster_tables.h
      storage/ndb/tools/restore/restore_main.cpp
=== modified file 'sql/ha_ndbcluster.cc'
--- a/sql/ha_ndbcluster.cc	2011-03-21 09:00:49 +0000
+++ b/sql/ha_ndbcluster.cc	2011-03-23 10:02:55 +0000
@@ -43,6 +43,7 @@
 #include "ha_ndbcluster_binlog.h"
 #include "ha_ndbcluster_tables.h"
 #include "ha_ndbcluster_connection.h"
+#include "ndb_dist_priv_util.h"
 
 #include <mysql/plugin.h>
 #include <ndb_version.h>
@@ -10130,7 +10131,7 @@ int ndbcluster_discover(handlerton *hton
   }
 #ifdef HAVE_NDB_BINLOG
   if (ndbcluster_check_if_local_table(db, name) &&
-      (! table_in_binlog_exclude_list(db, name)))
+      !Ndb_dist_priv_util::is_distributed_priv_table(db, name))
   {
     DBUG_PRINT("info", ("ndbcluster_discover: Skipping locally defined table '%s.%s'",
                         db, name));

=== modified file 'sql/ha_ndbcluster_binlog.cc'
--- a/sql/ha_ndbcluster_binlog.cc	2011-03-23 07:33:23 +0000
+++ b/sql/ha_ndbcluster_binlog.cc	2011-03-23 10:02:55 +0000
@@ -59,6 +59,8 @@ bool ndb_log_empty_epochs(void);
 static char repdb[]= NDB_REP_DB;
 static char reptable[]= NDB_REP_TABLE;
 
+#include "ndb_dist_priv_util.h"
+
 /*
   Timeout for syncing schema events between
   mysql servers, and between mysql server and the binlog
@@ -740,26 +742,27 @@ ndbcluster_binlog_index_purge_file(THD *
   DBUG_RETURN(0);
 }
 
-static const char* ndbcluster_distributed_tables[] =
-{
-  NDB_USER_TABLE,
-  NDB_DB_TABLE,
-  NDB_TABLES_PRIV_TABLE,
-  NDB_COLUMNS_PRIV_TABLE,
-  NDB_PROCS_PRIV_TABLE
-};
 
+// Determine if privilege tables are distributed, ie. stored in NDB
 static bool
-ndbcluster_distributed_privileges(THD *thd)
+priv_tables_are_in_ndb(THD *thd)
 {
-  Ndb *ndb= check_ndb_in_thd(thd);
-  NDBDICT *dict= ndb->getDictionary();
   bool distributed= false;
+  Ndb_dist_priv_util dist_priv;
   DBUG_ENTER("ndbcluster_distributed_privileges");
-  ndb->setDatabaseName(NDB_PRIV_DB);
-  for(uint i= 0; i < 5; i++)
+
+  Ndb *ndb= check_ndb_in_thd(thd);
+  if (!ndb)
+    DBUG_RETURN(false); // MAGNUS, error message?
+
+  if (ndb->setDatabaseName(dist_priv.database()) != 0)
+    DBUG_RETURN(false);
+
+  const char* table_name;
+  while((table_name= dist_priv.iter_next_table()))
   {
-    Ndb_table_guard ndbtab_g(dict, ndbcluster_distributed_tables[i]);
+    DBUG_PRINT("info", ("table_name: %s", table_name));
+    Ndb_table_guard ndbtab_g(ndb->getDictionary(), table_name);
     const NDBTAB *ndbtab= ndbtab_g.get_table();
     if (ndbtab)
     {
@@ -767,13 +770,16 @@ ndbcluster_distributed_privileges(THD *t
     }
     else if (distributed)
     {
-      sql_print_error("NDB: Inconsistency detected, not all privilege tables have been distributed");
+      sql_print_error("NDB: Inconsistency detected in distributed "
+                      "privilege tables. Table '%s.%s' is not distributed",
+                      dist_priv.database(), table_name);
       DBUG_RETURN(false);
     }
   }
   DBUG_RETURN(distributed);
 }
 
+
 static void
 ndbcluster_binlog_log_query(handlerton *hton, THD *thd, enum_binlog_command binlog_command,
                             const char *query, uint query_length,
@@ -806,7 +812,7 @@ ndbcluster_binlog_log_query(handlerton *
     break;
   case LOGCOM_RENAME_TABLE:
     type= SOT_RENAME_TABLE;
-    //DBUG_ASSERT(FALSE);
+    DBUG_ASSERT(FALSE);
     break;
   case LOGCOM_DROP_TABLE:
     type= SOT_DROP_TABLE;
@@ -826,7 +832,7 @@ ndbcluster_binlog_log_query(handlerton *
     break;
   case LOGCOM_CREATE_USER:
     type= SOT_CREATE_USER;
-    if (ndbcluster_distributed_privileges(thd))
+    if (priv_tables_are_in_ndb(thd))
     {
       DBUG_PRINT("info", ("Privilege tables have been distributed, logging statement"));
       log= 1;
@@ -834,7 +840,7 @@ ndbcluster_binlog_log_query(handlerton *
     break;
   case LOGCOM_DROP_USER:
     type= SOT_DROP_USER;
-    if (ndbcluster_distributed_privileges(thd))
+    if (priv_tables_are_in_ndb(thd))
     {
       DBUG_PRINT("info", ("Privilege tables have been distributed, logging statement"));
       log= 1;
@@ -842,7 +848,7 @@ ndbcluster_binlog_log_query(handlerton *
     break;
   case LOGCOM_RENAME_USER:
     type= SOT_RENAME_USER;
-    if (ndbcluster_distributed_privileges(thd))
+    if (priv_tables_are_in_ndb(thd))
     {
       DBUG_PRINT("info", ("Privilege tables have been distributed, logging statement"));
       log= 1;
@@ -850,7 +856,7 @@ ndbcluster_binlog_log_query(handlerton *
     break;
   case LOGCOM_GRANT:
     type= SOT_GRANT;
-    if (ndbcluster_distributed_privileges(thd))
+    if (priv_tables_are_in_ndb(thd))
     {
       DBUG_PRINT("info", ("Privilege tables have been distributed, logging statement"));
       log= 1;
@@ -858,7 +864,7 @@ ndbcluster_binlog_log_query(handlerton *
     break;
   case LOGCOM_REVOKE:
     type= SOT_REVOKE;
-    if (ndbcluster_distributed_privileges(thd))
+    if (priv_tables_are_in_ndb(thd))
     {
       DBUG_PRINT("info", ("Privilege tables have been distributed, logging statement"));
       log= 1;
@@ -3319,7 +3325,8 @@ ndb_binlog_thread_handle_schema_event_po
         thd_ndb_options.set(TNO_NO_LOCK_SCHEMA_OP);
         mysql_mutex_lock(&LOCK_open);
         if (ndbcluster_check_if_local_table(schema->db, schema->name) &&
-            (!table_in_binlog_exclude_list(schema->db, schema->name)))
+           !Ndb_dist_priv_util::is_distributed_priv_table(schema->db,
+                                                          schema->name))
         {
           sql_print_error("NDB Binlog: Skipping locally defined table '%s.%s' "
                           "from binlog schema event '%s' from node %d.",
@@ -3687,7 +3694,6 @@ add_ndb_binlog_index_err:
   return error;
 }
 
-
 /*********************************************************************
   Functions for start, stop, wait for ndbcluster binlog thread
 *********************************************************************/
@@ -4501,21 +4507,6 @@ ndbcluster_check_if_local_tables_in_db(T
   DBUG_RETURN(false);
 }
 
-bool
-table_in_binlog_exclude_list(const char *db, const char *table)
-{
-  if (strcmp(db, NDB_PRIV_DB) == 0)
-  {
-    if (strcmp(table, NDB_USER_TABLE) == 0 ||
-        strcmp(table, NDB_DB_TABLE) == 0 ||
-        strcmp(table, NDB_TABLES_PRIV_TABLE) == 0 ||
-        strcmp(table, NDB_COLUMNS_PRIV_TABLE) == 0 ||
-        strcmp(table, NDB_PROCS_PRIV_TABLE) == 0)
-      return TRUE;      
-  }
-  return FALSE;
-}
-
 /*
   Common function for setting up everything for logging a table at
   create/discover.
@@ -4541,8 +4532,11 @@ int ndbcluster_create_binlog_setup(THD *
 
   if (share && share_may_exist)
   {
-    if (table_in_binlog_exclude_list(db, table_name))
+    if (Ndb_dist_priv_util::is_distributed_priv_table(db, table_name))
     {
+      // The distributed privilege tables are distributed by writing
+      // the CREATE USER, GRANT, REVOKE etc. to ndb_schema -> no need
+      // to listen to events from this table
       DBUG_PRINT("info", ("Skipping binlogging of table %s/%s", db, table_name));
       set_binlog_nologging(share);
     }

=== modified file 'sql/ha_ndbcluster_binlog.h'
--- a/sql/ha_ndbcluster_binlog.h	2011-03-19 13:19:44 +0000
+++ b/sql/ha_ndbcluster_binlog.h	2011-03-23 10:02:55 +0000
@@ -329,9 +329,6 @@ ndbcluster_check_if_local_table(const ch
 bool
 ndbcluster_check_if_local_tables_in_db(THD *thd, const char *dbname);
 
-bool
-table_in_binlog_exclude_list(const char *db, const char *table);
-
 inline
 Thd_ndb *
 get_thd_ndb(THD *thd)

=== modified file 'sql/ha_ndbcluster_tables.h'
--- a/sql/ha_ndbcluster_tables.h	2011-03-19 13:19:44 +0000
+++ b/sql/ha_ndbcluster_tables.h	2011-03-23 10:02:55 +0000
@@ -22,13 +22,6 @@
 #define NDB_SCHEMA_TABLE "ndb_schema"
 #define NDB_REPLICATION_TABLE "ndb_replication"
 
-#define NDB_PRIV_DB            "mysql"
-#define NDB_USER_TABLE         "user"
-#define NDB_DB_TABLE           "db"
-#define NDB_TABLES_PRIV_TABLE  "tables_priv"
-#define NDB_COLUMNS_PRIV_TABLE "columns_priv"
-#define NDB_PROCS_PRIV_TABLE   "procs_priv"
-
 enum Ndb_binlog_type
 {
   NBT_DEFAULT                   = 0

=== added file 'sql/ndb_dist_priv_util.h'
--- a/sql/ndb_dist_priv_util.h	1970-01-01 00:00:00 +0000
+++ b/sql/ndb_dist_priv_util.h	2011-03-23 10:02:55 +0000
@@ -0,0 +1,67 @@
+/*
+   Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; version 2 of the License.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
+*/
+
+#ifndef NDB_DIST_PRIV_UTIL_H
+#define NDB_DIST_PRIV_UTIL_H
+
+class Ndb_dist_priv_util {
+  size_t m_iter_curr_table;
+public:
+  Ndb_dist_priv_util()
+  {
+    iter_reset();
+  }
+
+  const char* database() const { return "mysql"; }
+
+  // Iterator for distributed priv tables name
+  const char* iter_next_table()
+  {
+    static const char* tables[] =
+      { "user", "db", "tables_priv", "columns_priv", "procs_priv" };
+
+    if (m_iter_curr_table >= (sizeof(tables) / sizeof(tables[0])))
+      return NULL;
+    m_iter_curr_table++;
+    return tables[m_iter_curr_table-1];
+  }
+
+  // Reset iterator to start at first table name
+  void iter_reset() { m_iter_curr_table = 0; }
+
+  // Determine if a given table name is in the list
+  // of distributed priv tables
+  static
+  bool
+  is_distributed_priv_table(const char *db, const char *table)
+  {
+    Ndb_dist_priv_util dist_priv;
+    if (strcmp(db, dist_priv.database()))
+      return false; // Ignore tables not in dist_priv database
+
+    const char* priv_table_name;
+    while((priv_table_name= dist_priv.iter_next_table()))
+    {
+      if (strcmp(table, priv_table_name) == 0)
+        return true;
+    }
+    return false;
+  }
+
+};
+
+#endif

=== modified file 'storage/ndb/tools/restore/restore_main.cpp'
--- a/storage/ndb/tools/restore/restore_main.cpp	2011-03-23 07:39:22 +0000
+++ b/storage/ndb/tools/restore/restore_main.cpp	2011-03-23 10:02:55 +0000
@@ -124,7 +124,7 @@ static const char *opt_include_tables= N
 static const char *opt_exclude_databases= NULL;
 static const char *opt_include_databases= NULL;
 static const char *opt_rewrite_database= NULL;
-static const char *opt_restore_privilege_tables= NULL;
+static bool opt_restore_privilege_tables = false;
 
 static struct my_option my_long_options[] =
 {
@@ -531,40 +531,24 @@ makeExternalTableName(const BaseString &
   return externalName;
 }
 
+#include "../../../../sql/ndb_dist_priv_util.h"
+
+// Exclude privilege tables unless explicitely included
 void
 exclude_privilege_tables()
 {
-  // Exclude privilege tables unless explicitely included
-  BaseString priv_tab_prefix, priv_tab;
-  priv_tab_prefix.append(NDB_PRIV_DB);
-  priv_tab_prefix.append(".");
-  // mysql.user
-  priv_tab.assign(priv_tab_prefix);
-  priv_tab.append(NDB_USER_TABLE);
-  g_exclude_tables.push_back(priv_tab);
-  save_include_exclude(OPT_EXCLUDE_TABLES, (char *)priv_tab.c_str());
-  // mysql.db
-  priv_tab.assign(priv_tab_prefix);
-  priv_tab.append(NDB_DB_TABLE);
-  g_exclude_tables.push_back(priv_tab);
-  save_include_exclude(OPT_EXCLUDE_TABLES, (char *)priv_tab.c_str());
-  // mysql.tables_priv
-  priv_tab.assign(priv_tab_prefix);
-  priv_tab.append(NDB_TABLES_PRIV_TABLE);
-  g_exclude_tables.push_back(priv_tab);
-  save_include_exclude(OPT_EXCLUDE_TABLES, (char *)priv_tab.c_str());
-  // mysql.columns_priv
-  priv_tab.assign(priv_tab_prefix);
-  priv_tab.append(NDB_COLUMNS_PRIV_TABLE);
-  g_exclude_tables.push_back(priv_tab);
-  save_include_exclude(OPT_EXCLUDE_TABLES, (char *)priv_tab.c_str());
-  // mysql.procs_priv
-  priv_tab.assign(priv_tab_prefix);
-  priv_tab.append(NDB_PROCS_PRIV_TABLE);
-  g_exclude_tables.push_back(priv_tab);
-  save_include_exclude(OPT_EXCLUDE_TABLES, (char *)priv_tab.c_str());
+  const char* table_name;
+  Ndb_dist_priv_util dist_priv;
+  while((table_name= dist_priv.iter_next_table()))
+  {
+    BaseString priv_tab;
+    priv_tab.assfmt("%s.%s", dist_priv.database(), table_name);
+    g_exclude_tables.push_back(priv_tab);
+    save_include_exclude(OPT_EXCLUDE_TABLES, (char *)priv_tab.c_str());
+  }
 }
 
+
 bool
 readArguments(int *pargc, char*** pargv) 
 {
@@ -742,7 +726,7 @@ o verify nodegroup mapping
 
   if (ga_restore)
   {
-    // We always exclude privilege tables
+    // Exclude privilege tables unless explicitely included
     if (!opt_restore_privilege_tables)
       exclude_privilege_tables();
     
@@ -1047,6 +1031,7 @@ static bool check_include_exclude(BaseSt
       }
     }
   }
+
   return do_include;
 }
 
@@ -1080,7 +1065,6 @@ checkDbAndTableName(const TableS* table)
     return false;
 
   // If new options are given, ignore the old format
-
   if (opt_include_tables || g_exclude_tables.size() > 0 ||
       opt_include_databases || opt_exclude_databases ) {
     return (checkDoRestore(table));

Attachment: [text/bzr-bundle] bzr/magnus.blaudd@sun.com-20110323100255-qqfxganhc35230v2.bundle
Thread
bzr commit into mysql-5.1-telco-7.0 branch (magnus.blaudd:4254) Magnus Blåudd23 Mar