Commit c594ab79 authored by sasha@asksasha.com's avatar sasha@asksasha.com

updated patch for BUG#4680 (incomplete DROP DATABASE breaking replication).

We binlog the DROP TABLE for each table that was actually dropped. Per Sergei's 
suggestion a fixed buffer for the DROP TABLE query is pre-allocated from THD pool, and 
logging now is done in batches - new batch is started if the buffer becomes full.
Reduced memory usage by reusing the table list instead of accumulating a list of 
dropped table names. Also fixed the problem if the table was not actually dropped, eg
due to permissions. Extended the test case to make sure batched query 
logging does work.  
parent 76a59be6
...@@ -1054,3 +1054,5 @@ vio/test-sslclient ...@@ -1054,3 +1054,5 @@ vio/test-sslclient
vio/test-sslserver vio/test-sslserver
vio/viotest-ssl vio/viotest-ssl
ndb/tools/ndb_config ndb/tools/ndb_config
support-files/MacOSX/postflight
support-files/MacOSX/preflight
This diff is collapsed.
...@@ -17,6 +17,19 @@ create table d1.t3 (n int); ...@@ -17,6 +17,19 @@ create table d1.t3 (n int);
drop database d1; drop database d1;
use d1; use d1;
show tables; show tables;
# test the branch of the code that deals with the query buffer overflow
let $1=1000;
while ($1)
{
eval create table d1.t$1(n int);
dec $1;
}
--error 1010
drop database d1;
use d1;
show tables;
use test; use test;
create table t1 (n int); create table t1 (n int);
insert into t1 values (1234); insert into t1 values (1234);
......
...@@ -447,7 +447,7 @@ int mysql_rm_table_part2(THD *thd, TABLE_LIST *tables, bool if_exists, ...@@ -447,7 +447,7 @@ int mysql_rm_table_part2(THD *thd, TABLE_LIST *tables, bool if_exists,
bool drop_temporary, bool log_query); bool drop_temporary, bool log_query);
int mysql_rm_table_part2_with_lock(THD *thd, TABLE_LIST *tables, int mysql_rm_table_part2_with_lock(THD *thd, TABLE_LIST *tables,
bool if_exists, bool drop_temporary, bool if_exists, bool drop_temporary,
bool log_query, List<String> *dropped_tables); bool log_query);
int quick_rm_table(enum db_type base,const char *db, int quick_rm_table(enum db_type base,const char *db,
const char *table_name); const char *table_name);
bool mysql_rename_tables(THD *thd, TABLE_LIST *table_list); bool mysql_rename_tables(THD *thd, TABLE_LIST *table_list);
......
...@@ -25,13 +25,19 @@ ...@@ -25,13 +25,19 @@
#include <direct.h> #include <direct.h>
#endif #endif
#define MAX_DROP_TABLE_Q_LEN 1024
const char *del_exts[]= {".frm", ".BAK", ".TMD",".opt", NullS}; const char *del_exts[]= {".frm", ".BAK", ".TMD",".opt", NullS};
static TYPELIB deletable_extentions= static TYPELIB deletable_extentions=
{array_elements(del_exts)-1,"del_exts", del_exts, NULL}; {array_elements(del_exts)-1,"del_exts", del_exts, NULL};
static long mysql_rm_known_files(THD *thd, MY_DIR *dirp, static long mysql_rm_known_files(THD *thd, MY_DIR *dirp,
const char *db, const char *path, const char *db, const char *path, uint level,
uint level, List<String> *dropped_tables); TABLE_LIST** dropped_tables);
static inline void write_to_binlog(THD* thd, char* query, uint q_len,
char* db, uint db_len);
/* Database options hash */ /* Database options hash */
static HASH dboptions; static HASH dboptions;
...@@ -57,6 +63,19 @@ static byte* dboptions_get_key(my_dbopt_t *opt, uint *length, ...@@ -57,6 +63,19 @@ static byte* dboptions_get_key(my_dbopt_t *opt, uint *length,
return (byte*) opt->name; return (byte*) opt->name;
} }
/*
Helper function to write a query to binlog used by mysql_rm_db()
*/
static inline void write_to_binlog(THD* thd, char* query, uint q_len,
char* db, uint db_len)
{
Query_log_event qinfo(thd, query, q_len, 0, 0);
qinfo.error_code= 0;
qinfo.db= db;
qinfo.db_len= db_len;
mysql_bin_log.write(&qinfo);
}
/* /*
Function to free dboptions hash element Function to free dboptions hash element
...@@ -584,8 +603,8 @@ int mysql_rm_db(THD *thd,char *db,bool if_exists, bool silent) ...@@ -584,8 +603,8 @@ int mysql_rm_db(THD *thd,char *db,bool if_exists, bool silent)
int error= 0; int error= 0;
char path[FN_REFLEN+16], tmp_db[NAME_LEN+1]; char path[FN_REFLEN+16], tmp_db[NAME_LEN+1];
MY_DIR *dirp; MY_DIR *dirp;
List<String> dropped_tables;
uint length; uint length;
TABLE_LIST* dropped_tables= 0;
DBUG_ENTER("mysql_rm_db"); DBUG_ENTER("mysql_rm_db");
VOID(pthread_mutex_lock(&LOCK_mysql_create_db)); VOID(pthread_mutex_lock(&LOCK_mysql_create_db));
...@@ -675,36 +694,44 @@ int mysql_rm_db(THD *thd,char *db,bool if_exists, bool silent) ...@@ -675,36 +694,44 @@ int mysql_rm_db(THD *thd,char *db,bool if_exists, bool silent)
send_ok(thd, (ulong) deleted); send_ok(thd, (ulong) deleted);
thd->server_status&= ~SERVER_STATUS_DB_DROPPED; thd->server_status&= ~SERVER_STATUS_DB_DROPPED;
} }
else if (!dropped_tables.is_empty() && mysql_bin_log.is_open()) else if (mysql_bin_log.is_open())
{
List_iterator<String> it(dropped_tables);
String* dropped_table;
int q_len= 11; /* drop table */
int db_len= strlen(db);
for (;(dropped_table= it++);)
{ {
q_len += dropped_table->length() + 2 + db_len; char* query= thd->alloc(MAX_DROP_TABLE_Q_LEN);
}
q_len--; /* no last comma */
char* query= thd->alloc(q_len);
if (!query) if (!query)
goto exit; /* not much else we can do */ goto exit; /* not much else we can do */
char* p= strmov(query,"drop table "); char* p= strmov(query,"drop table ");
it.rewind(); char* p_end= query + MAX_DROP_TABLE_Q_LEN;
TABLE_LIST* tbl;
bool last_query_needs_write= 0;
uint db_len= strlen(db);
for (tbl= dropped_tables;tbl;tbl= tbl->next)
{
if (!tbl->was_dropped)
continue;
for (;(dropped_table= it++);) /* 3 for the quotes and the comma*/
uint tbl_name_len= strlen(tbl->real_name) + 3;
if (p + tbl_name_len + 1 >= p_end)
{ {
p= strmov(p,db); *--p= 0; /* kill , */
*p++ = '.'; write_to_binlog(thd, query, p - query, db, db_len);
p= strnmov(p,dropped_table->ptr(),dropped_table->length()); p= query + 11; /* reuse the initial "drop table" */
}
*p++ = '`';
p= strmov(p,tbl->real_name);
*p++ = '`';
*p++ = ','; *p++ = ',';
last_query_needs_write= 1;
} }
if (last_query_needs_write)
{
*--p= 0; *--p= 0;
Query_log_event qinfo(thd, query, q_len, 0, 0); write_to_binlog(thd, query, p - query, db, db_len);
qinfo.error_code= 0; }
mysql_bin_log.write(&qinfo);
} }
exit: exit:
...@@ -750,7 +777,7 @@ exit2: ...@@ -750,7 +777,7 @@ exit2:
*/ */
static long mysql_rm_known_files(THD *thd, MY_DIR *dirp, const char *db, static long mysql_rm_known_files(THD *thd, MY_DIR *dirp, const char *db,
const char *org_path, uint level, List<String> *dropped_tables) const char *org_path, uint level, TABLE_LIST** dropped_tables)
{ {
long deleted=0; long deleted=0;
ulong found_other_files=0; ulong found_other_files=0;
...@@ -839,8 +866,7 @@ static long mysql_rm_known_files(THD *thd, MY_DIR *dirp, const char *db, ...@@ -839,8 +866,7 @@ static long mysql_rm_known_files(THD *thd, MY_DIR *dirp, const char *db,
} }
} }
if (thd->killed || if (thd->killed ||
(tot_list && mysql_rm_table_part2_with_lock(thd, tot_list, 1, 0, (tot_list && mysql_rm_table_part2_with_lock(thd, tot_list, 1, 0, 1)))
1,dropped_tables)))
goto err; goto err;
/* Remove RAID directories */ /* Remove RAID directories */
...@@ -853,6 +879,9 @@ static long mysql_rm_known_files(THD *thd, MY_DIR *dirp, const char *db, ...@@ -853,6 +879,9 @@ static long mysql_rm_known_files(THD *thd, MY_DIR *dirp, const char *db,
} }
my_dirend(dirp); my_dirend(dirp);
if (dropped_tables)
*dropped_tables= tot_list;
/* /*
If the directory is a symbolic link, remove the link first, then If the directory is a symbolic link, remove the link first, then
remove the directory the symbolic link pointed at remove the directory the symbolic link pointed at
......
...@@ -156,8 +156,7 @@ int mysql_rm_table(THD *thd,TABLE_LIST *tables, my_bool if_exists, ...@@ -156,8 +156,7 @@ int mysql_rm_table(THD *thd,TABLE_LIST *tables, my_bool if_exists,
int mysql_rm_table_part2_with_lock(THD *thd, int mysql_rm_table_part2_with_lock(THD *thd,
TABLE_LIST *tables, bool if_exists, TABLE_LIST *tables, bool if_exists,
bool drop_temporary, bool dont_log_query, bool drop_temporary, bool dont_log_query)
List<String>* dropped_tables)
{ {
int error; int error;
thd->mysys_var->current_mutex= &LOCK_open; thd->mysys_var->current_mutex= &LOCK_open;
...@@ -166,23 +165,6 @@ int mysql_rm_table_part2_with_lock(THD *thd, ...@@ -166,23 +165,6 @@ int mysql_rm_table_part2_with_lock(THD *thd,
error=mysql_rm_table_part2(thd,tables, if_exists, drop_temporary, error=mysql_rm_table_part2(thd,tables, if_exists, drop_temporary,
dont_log_query); dont_log_query);
/*
For now we assume that if we got success all the tables in the list
were actually dropped, otherwise, assume none were dropped.
TODO: fix it to work with a partial drop - extremely rare case, but
can happen.
*/
if (!error && dropped_tables)
{
TABLE_LIST* tbl;
for (tbl= tables; tbl; tbl= tbl->next)
{
String *dropped_table= new (thd->mem_root)
String(tbl->real_name,&my_charset_latin1);
dropped_tables->push_back(dropped_table);
}
}
pthread_mutex_unlock(&LOCK_open); pthread_mutex_unlock(&LOCK_open);
...@@ -238,6 +220,7 @@ int mysql_rm_table_part2(THD *thd, TABLE_LIST *tables, bool if_exists, ...@@ -238,6 +220,7 @@ int mysql_rm_table_part2(THD *thd, TABLE_LIST *tables, bool if_exists,
for (table=tables ; table ; table=table->next) for (table=tables ; table ; table=table->next)
{ {
char *db=table->db; char *db=table->db;
table->was_dropped= 0;
mysql_ha_flush(thd, table, MYSQL_HA_CLOSE_FINAL); mysql_ha_flush(thd, table, MYSQL_HA_CLOSE_FINAL);
if (!close_temporary_table(thd, db, table->real_name)) if (!close_temporary_table(thd, db, table->real_name))
{ {
...@@ -298,6 +281,8 @@ int mysql_rm_table_part2(THD *thd, TABLE_LIST *tables, bool if_exists, ...@@ -298,6 +281,8 @@ int mysql_rm_table_part2(THD *thd, TABLE_LIST *tables, bool if_exists,
wrong_tables.append(','); wrong_tables.append(',');
wrong_tables.append(String(table->real_name,system_charset_info)); wrong_tables.append(String(table->real_name,system_charset_info));
} }
else
table->was_dropped= 1;
} }
thd->tmp_table_used= tmp_table_deleted; thd->tmp_table_used= tmp_table_deleted;
error= 0; error= 0;
......
...@@ -235,6 +235,9 @@ typedef struct st_table_list ...@@ -235,6 +235,9 @@ typedef struct st_table_list
bool cacheable_table; /* stop PS caching */ bool cacheable_table; /* stop PS caching */
/* used in multi-upd privelege check */ /* used in multi-upd privelege check */
bool table_in_update_from_clause; bool table_in_update_from_clause;
/* used for proper partially successful DROP DATABASE binlogging */
bool was_dropped;
} TABLE_LIST; } TABLE_LIST;
typedef struct st_changed_table_list typedef struct st_changed_table_list
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment