Skip to content

Commit

Permalink
Add new option innodb_stats_on_metadata to mysqldump
Browse files Browse the repository at this point in the history
Summary:
As title.

Note the option takes effect only on innodb as default engine.

Reviewed By: teng-li

Differential Revision: D4590297

fbshipit-source-id: 11a4797
  • Loading branch information
tianx authored and facebook-github-bot committed Feb 22, 2017
1 parent 25e1603 commit da207ee
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 7 deletions.
1 change: 1 addition & 0 deletions client/client_priv.h
Expand Up @@ -106,6 +106,7 @@ enum options_client
OPT_TIMEOUT,
OPT_LONG_QUERY_TIME,
OPT_INNODB_OPTIMIZE_KEYS,
OPT_INNODB_STATS_ON_METADATA,
OPT_LRA_SIZE, OPT_LRA_SLEEP, OPT_LRA_N_NODE_RECS_BEFORE_SLEEP,
OPT_RECEIVE_BUFFER_SIZE,
OPT_PRINT_ORDERING_KEY,
Expand Down
35 changes: 28 additions & 7 deletions client/mysqldump.c
Expand Up @@ -99,7 +99,7 @@ static ulong find_set(TYPELIB *lib, const char *x, size_t length,
char **err_pos, uint *err_len);
static char *alloc_query_str(ulong size);

static my_bool default_engine_rocksdb(MYSQL *mysql_con);
static my_bool default_engine(MYSQL *mysql_con, const char *engine);
static void field_escape(DYNAMIC_STRING* in, const char *from);
static my_bool verbose= 0, opt_no_create_info= 0, opt_no_data= 0,
quick= 1, extended_insert= 1,
Expand Down Expand Up @@ -187,6 +187,8 @@ static my_bool server_supports_sql_no_fcache= FALSE;
static my_bool opt_innodb_optimize_keys= FALSE;
static my_bool opt_print_ordering_key= FALSE;

static my_bool opt_innodb_stats_on_metadata= FALSE;

/*
Dynamic_string wrapper functions. In this file use these
wrappers, they will terminate the process if there is
Expand Down Expand Up @@ -445,6 +447,12 @@ static struct my_option my_long_options[] =
"dumping the data.",
&opt_innodb_optimize_keys, &opt_innodb_optimize_keys, 0, GET_BOOL, NO_ARG,
0, 0, 0, 0, 0, 0},
{"innodb-stats-on-metadata", OPT_INNODB_STATS_ON_METADATA,
"Update non-persistent statistics during metadata statements such as SHOW "
"TABLE STATUS, or when accessing the INFORMATION_SCHEMA.TABLES or "
"INFORMATION_SCHEMA.STATISTICS tables",
&opt_innodb_stats_on_metadata, &opt_innodb_stats_on_metadata, 0, GET_BOOL,
NO_ARG, 0, 0, 0, 0, 0, 0},
{"print-ordering-key", OPT_PRINT_ORDERING_KEY,
"Print the key used for ordering rows in the dumpfile",
&opt_print_ordering_key, &opt_print_ordering_key, 0 , GET_BOOL, NO_ARG,
Expand Down Expand Up @@ -1818,6 +1826,15 @@ static int connect_to_db(char *host, char *user,char *passwd)
DBUG_RETURN(1);
}

/* set innodb_stats_on_metadata if the default engine is InnoDB */
if (opt_innodb_stats_on_metadata && default_engine(mysql, "InnoDB"))
{
my_snprintf(buff, sizeof(buff), "SET session innodb_stats_on_metadata=%u",
opt_innodb_stats_on_metadata);
if (mysql_query_with_error_report(mysql, 0, buff))
DBUG_RETURN(1);
}

DBUG_RETURN(0);
} /* connect_to_db */

Expand Down Expand Up @@ -5786,7 +5803,7 @@ static int start_transaction(MYSQL *mysql_con, char* filename_out,
if (!ignore_errors)
exit(EX_MYSQLERR);
}
my_bool use_rocksdb= opt_rocksdb || default_engine_rocksdb(mysql_con);
my_bool use_rocksdb= opt_rocksdb || default_engine(mysql_con, "ROCKSDB");

if (use_rocksdb &&
mysql_query_with_error_report(mysql_con, 0,
Expand Down Expand Up @@ -6231,13 +6248,17 @@ static my_bool add_set_gtid_purged(MYSQL *mysql_con)
return FALSE; /*success */
}

static my_bool default_engine_rocksdb(MYSQL *mysql_con)
/*
* check if the default engine matches $engine
* Return TRUE if it matches, otherwise return FALSE
*/
static my_bool default_engine(MYSQL *mysql_con, const char *engine)
{
MYSQL_RES *res;
MYSQL_ROW row;
char *val= 0;
char buf[32], query[64];
my_bool rocksdb= FALSE;
my_bool match= FALSE;

my_snprintf(query, sizeof(query), "SHOW VARIABLES LIKE %s",
quote_for_like("default_storage_engine", buf));
Expand All @@ -6246,12 +6267,12 @@ static my_bool default_engine_rocksdb(MYSQL *mysql_con)

row = mysql_fetch_row(res);
val = row ? (char*)row[1] : NULL;
if (val && !strcmp(val, "ROCKSDB"))
if (val && !strcmp(val, engine))
{
rocksdb= TRUE;
match= TRUE;
}
mysql_free_result(res);
return rocksdb;
return match;
}

/**
Expand Down
66 changes: 66 additions & 0 deletions mysql-test/r/mysqldump-stats_on_metadata.result
@@ -0,0 +1,66 @@
DROP TABLE IF EXISTS table1;
drop database if exists database1;
SELECT @@global.default_storage_engine INTO @old_engine;
SET GLOBAL default_storage_engine=InnoDB;
CREATE DATABASE database1;
USE database1;
CREATE TABLE table1(a INT);
INSERT INTO table1 VALUES (1);
INSERT INTO table1 VALUES (2);
INSERT INTO table1 VALUES (3);
==== Running 'replace_regex' on Server/mysqldump Versions ====
==== mysqldump with --innodb-stats-on-metadata database1 ====
-- MYSQLDUMP VERSION
--
-- Host: localhost Database: database1
-- ------------------------------------------------------
-- SERVER VERSION
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

--
-- Table structure for table `table1`
--

DROP TABLE IF EXISTS `table1`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `table1` (
`a` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `table1`
--

LOCK TABLES `table1` WRITE;
/*!40000 ALTER TABLE `table1` DISABLE KEYS */;
INSERT INTO `table1` VALUES (1),(2),(3);
/*!40000 ALTER TABLE `table1` ENABLE KEYS */;
UNLOCK TABLES;

--
-- Rows found for table1: 3
--
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed
drop database database1;
SET GLOBAL default_storage_engine=@old_engine;
45 changes: 45 additions & 0 deletions mysql-test/suite/rocksdb/r/mysqldump.result
Expand Up @@ -124,6 +124,51 @@ UNLOCK TABLES;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

2
==== mysqldump with --innodb-stats-on-metadata ====

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
-- CHANGE MASTER TO MASTER_LOG_FILE='master-bin.000001', MASTER_LOG_POS=3893;
DROP TABLE IF EXISTS `r1`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `r1` (
`id1` int(11) NOT NULL DEFAULT '0',
`id2` int(11) NOT NULL DEFAULT '0',
`id3` varchar(100) NOT NULL DEFAULT '',
`id4` int(11) NOT NULL DEFAULT '0',
`value1` int(11) DEFAULT NULL,
`value2` int(11) DEFAULT NULL,
`value3` int(11) DEFAULT NULL,
`value4` int(11) DEFAULT NULL,
PRIMARY KEY (`id1`,`id2`,`id3`,`id4`)
) ENGINE=ROCKSDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
/* ORDERING KEY : (null) */;

LOCK TABLES `r1` WRITE;
/*!40000 ALTER TABLE `r1` DISABLE KEYS */;
INSERT INTO `r1` VALUES (1,1,'1',1,1,1,1,1),(1,1,'1',2,2,2,2,2),(1,1,'2',1,3,3,3,3),(1,1,'2',2,4,4,4,4),(1,2,'1',1,5,5,5,5),(1,2,'1',2,6,6,6,6),(1,2,'2',1,7,7,7,7),(1,2,'2',2,8,8,8,8),(2,1,'1',1,9,9,9,9),(2,1,'1',2,10,10,10,10),(2,1,'2',1,11,11,11,11),(2,1,'2',2,12,12,12,12),(2,2,'1',1,13,13,13,13),(2,2,'1',2,14,14,14,14),(2,2,'2',1,15,15,15,15),(2,2,'2',2,16,16,16,16);
/*!40000 ALTER TABLE `r1` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

SET GLOBAL binlog_format=statement;
SET GLOBAL binlog_format=row;
drop table r1;
Expand Down
3 changes: 3 additions & 0 deletions mysql-test/suite/rocksdb/t/mysqldump.test
Expand Up @@ -51,6 +51,9 @@ SET GLOBAL default_storage_engine=rocksdb;
--exec $MYSQL_DUMP --skip-comments --single-transaction --master-data=2 --print-ordering-key test
--exec grep "START TRANSACTION WITH CONSISTENT ROCKSDB SNAPSHOT" $MYSQLTEST_VARDIR/mysqld.1/mysqld.log | wc -l

# Sanity test mysqldump when the --innodb-stats-on-metadata is specified (no effect)
--echo ==== mysqldump with --innodb-stats-on-metadata ====
--exec $MYSQL_DUMP --skip-comments --single-transaction --master-data=2 --print-ordering-key --innodb-stats-on-metadata test

# wiping general log so that this test case doesn't fail with --repeat
--exec echo "" > $MYSQLTEST_VARDIR/mysqld.1/mysqld.log
Expand Down
31 changes: 31 additions & 0 deletions mysql-test/t/mysqldump-stats_on_metadata.test
@@ -0,0 +1,31 @@
################################################################################
# Testing the mysqldump with --innodb-stats-on-metadata option
################################################################################

--source include/have_innodb.inc

--disable_warnings
DROP TABLE IF EXISTS table1;
drop database if exists database1;
--enable_warnings

SELECT @@global.default_storage_engine INTO @old_engine;
SET GLOBAL default_storage_engine=InnoDB;

CREATE DATABASE database1;
USE database1;
CREATE TABLE table1(a INT);

INSERT INTO table1 VALUES (1);
INSERT INTO table1 VALUES (2);
INSERT INTO table1 VALUES (3);

# Sanity test mysqldump when the option is used
--echo ==== Running 'replace_regex' on Server/mysqldump Versions ====
--echo ==== mysqldump with --innodb-stats-on-metadata database1 ====
--replace_regex /-- Server version.*/-- SERVER VERSION/ /-- MySQL dump.*[)]/-- MYSQLDUMP VERSION/
--exec $MYSQL_DUMP --skip-dump-date --innodb-stats-on-metadata database1

drop database database1;

SET GLOBAL default_storage_engine=@old_engine;

0 comments on commit da207ee

Please sign in to comment.