Commit 9aca45b3 authored by unknown's avatar unknown

Optimization: in the replication slave, we can avoid doing one strlen() per event's execution,

as we already have db_len in Log_event. Only if rewrite_db() changed the db we need a strlen
(so we now do the strlen() in rewrite_db). Plus a test (we had none for --replicate-rewrite-db :( ).


sql/log_event.cc:
  The goal is to get of rid of one strlen() per replication event in slave:
  we don't need to compute strlen(thd->db) as we already have db_len in the event;
  only case where we need to do a strlen() is if rewrite_db() changed the db.
  Note that db_len is always a meaningful value. It's 0 if event's db is 0.
sql/slave.cc:
  rewrite_db now returns the len of the returned db.
  print_slave_db_safe() needn't call rewrite_db() as rewrite_db() is already called by caller.
sql/slave.h:
  declaration updates for slave.cc
parent 7e657194
stop slave;
drop table if exists t1,t2,t3,t4,t5,t6,t7,t8,t9;
reset master;
reset slave;
drop table if exists t1,t2,t3,t4,t5,t6,t7,t8,t9;
start slave;
drop database if exists mysqltest1;
create database mysqltest1;
use mysqltest1;
create table t1 (a int);
insert into t1 values(9);
select * from mysqltest1.t1;
a
9
show databases like 'mysqltest1';
Database (mysqltest1)
mysqltest1
select * from test.t1;
a
9
drop table t1;
drop database mysqltest1;
"--replicate-rewrite-db=mysqltest1->test"
source include/master-slave.inc;
--disable_warnings
drop database if exists mysqltest1;
--enable_warnings
create database mysqltest1;
use mysqltest1;
create table t1 (a int);
insert into t1 values(9);
select * from mysqltest1.t1;
sync_slave_with_master;
show databases like 'mysqltest1'; # should be empty
select * from test.t1;
# cleanup
connection master;
drop table t1;
drop database mysqltest1;
sync_slave_with_master;
......@@ -977,7 +977,8 @@ void Query_log_event::print(FILE* file, bool short_form, char* last_db)
int Query_log_event::exec_event(struct st_relay_log_info* rli)
{
int expected_error,actual_error= 0;
thd->db= (char*) rewrite_db(db); // thd->db_length is set later if needed
thd->db_length= db_len;
thd->db= (char*) rewrite_db(db, &thd->db_length);
/*
InnoDB internally stores the master log position it has processed so far;
......@@ -995,11 +996,6 @@ int Query_log_event::exec_event(struct st_relay_log_info* rli)
if (db_ok(thd->db, replicate_do_db, replicate_ignore_db))
{
thd->set_time((time_t)when);
/*
We cannot use db_len from event to fill thd->db_length, because
rewrite_db() may have changed db.
*/
thd->db_length= thd->db ? strlen(thd->db) : 0;
thd->query_length= q_len;
thd->query = (char*)query;
VOID(pthread_mutex_lock(&LOCK_thread_count));
......@@ -1057,7 +1053,7 @@ Default database: '%s'. Query: '%s'",
expected_error,
actual_error ? thd->net.last_error: "no error",
actual_error,
print_slave_db_safe(db), query);
print_slave_db_safe(thd->db), query);
thd->query_error= 1;
}
/*
......@@ -1078,7 +1074,7 @@ Default database: '%s'. Query: '%s'",
"Error '%s' on query. Default database: '%s'. Query: '%s'",
(actual_error ? thd->net.last_error :
"unexpected success or fatal error"),
print_slave_db_safe(db), query);
print_slave_db_safe(thd->db), query);
thd->query_error= 1;
}
} /* End of if (db_ok(... */
......@@ -1706,7 +1702,8 @@ int Load_log_event::exec_event(NET* net, struct st_relay_log_info* rli,
bool use_rli_only_for_errors)
{
char *load_data_query= 0;
thd->db= (char*) rewrite_db(db); // thd->db_length is set later if needed
thd->db_length= db_len;
thd->db= (char*) rewrite_db(db, &thd->db_length);
DBUG_ASSERT(thd->query == 0);
thd->query_length= 0; // Should not be needed
thd->query_error= 0;
......@@ -1741,7 +1738,6 @@ int Load_log_event::exec_event(NET* net, struct st_relay_log_info* rli,
if (db_ok(thd->db, replicate_do_db, replicate_ignore_db))
{
thd->set_time((time_t)when);
thd->db_length= thd->db ? strlen(thd->db) : 0;
VOID(pthread_mutex_lock(&LOCK_thread_count));
thd->query_id = query_id++;
VOID(pthread_mutex_unlock(&LOCK_thread_count));
......@@ -1847,7 +1843,7 @@ Slave: load data infile on table '%s' at log position %s in log \
(char*) table_name,
llstr(log_pos,llbuff), RPL_LOG_NAME,
(ulong) thd->cuted_fields,
print_slave_db_safe(db));
print_slave_db_safe(thd->db));
}
if (net)
net->pkt_nr= thd->net.pkt_nr;
......@@ -1865,6 +1861,7 @@ Slave: load data infile on table '%s' at log position %s in log \
}
thd->net.vio = 0;
char *save_db= thd->db;
VOID(pthread_mutex_lock(&LOCK_thread_count));
thd->db= 0;
thd->query= 0;
......@@ -1887,7 +1884,7 @@ Slave: load data infile on table '%s' at log position %s in log \
}
slave_print_error(rli,sql_errno,"\
Error '%s' running LOAD DATA INFILE on table '%s'. Default database: '%s'",
err, (char*)table_name, print_slave_db_safe(db));
err, (char*)table_name, print_slave_db_safe(save_db));
free_root(&thd->mem_root,MYF(MY_KEEP_PREALLOC));
return 1;
}
......@@ -1897,7 +1894,7 @@ Error '%s' running LOAD DATA INFILE on table '%s'. Default database: '%s'",
{
slave_print_error(rli,ER_UNKNOWN_ERROR, "\
Fatal error running LOAD DATA INFILE on table '%s'. Default database: '%s'",
(char*)table_name, print_slave_db_safe(db));
(char*)table_name, print_slave_db_safe(save_db));
return 1;
}
......
......@@ -1032,7 +1032,7 @@ bool net_request_file(NET* net, const char* fname)
}
const char *rewrite_db(const char* db)
const char *rewrite_db(const char* db, uint *new_len)
{
if (replicate_rewrite_db.is_empty() || !db)
return db;
......@@ -1042,7 +1042,10 @@ const char *rewrite_db(const char* db)
while ((tmp=it++))
{
if (!strcmp(tmp->key, db))
{
*new_len= strlen(tmp->val);
return tmp->val;
}
}
return db;
}
......@@ -1056,7 +1059,7 @@ const char *rewrite_db(const char* db)
const char *print_slave_db_safe(const char* db)
{
return (db ? rewrite_db(db) : "");
return (db ? db : "");
}
/*
......
......@@ -510,8 +510,8 @@ int add_table_rule(HASH* h, const char* table_spec);
int add_wild_table_rule(DYNAMIC_ARRAY* a, const char* table_spec);
void init_table_rule_hash(HASH* h, bool* h_inited);
void init_table_rule_array(DYNAMIC_ARRAY* a, bool* a_inited);
const char *rewrite_db(const char* db);
const char *print_slave_db_safe(const char* db);
const char *rewrite_db(const char* db, uint *new_db_len);
const char *print_slave_db_safe(const char *db);
int check_expected_error(THD* thd, RELAY_LOG_INFO* rli, int error_code);
void skip_load_data_infile(NET* net);
void slave_print_error(RELAY_LOG_INFO* rli, int err_code, const char* msg, ...);
......
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