Skip to content

Commit

Permalink
fix(tianmu): bug for truncate table failed stoneatom#759
Browse files Browse the repository at this point in the history
[summary]
1. Fix close_all_tables_for_name() before the txn is commited.
2. In file sql_trunction.cc and function end_transaction(), will call CommitTx() twice, In CommitTx()
   skip for the first time, called in trans_commit_stmt(), thd->server_status = SERVER_STATUS_IN_TRANS and all = false
   then called in trans_commit_implicit(), thd->server_status will be set "~SERVER_STATUS_IN_TRANS", and all = true
  • Loading branch information
hustjieke committed Oct 25, 2022
1 parent 5a5d6ff commit 1beaf0f
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 16 deletions.
30 changes: 15 additions & 15 deletions mysql-test/suite/tianmu/r/truncate.result
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use test;
CREATE TABLE `column_type_test` (
`c_tinyint` tinyint(4) DEFAULT NULL COMMENT 'tinyint',
`c_smallint` smallint(6) DEFAULT NULL COMMENT 'smallint',
`c_mediumint` mediumint(9) DEFAULT NULL COMMENT 'mediumint',
`c_int` int(11) DEFAULT NULL COMMENT 'int',
`c_bigint` bigint(20) DEFAULT NULL COMMENT 'bigint',
`c_tinyint` tinyint DEFAULT NULL COMMENT 'tinyint',
`c_smallint` smallint DEFAULT NULL COMMENT 'smallint',
`c_mediumint` mediumint DEFAULT NULL COMMENT 'mediumint',
`c_int` int DEFAULT NULL COMMENT 'int',
`c_bigint` bigint DEFAULT NULL COMMENT 'bigint',
`c_float` float DEFAULT NULL COMMENT 'float',
`c_double` double DEFAULT NULL COMMENT 'double',
`c_decimal` decimal(10,5) DEFAULT NULL COMMENT 'decimal',
Expand All @@ -17,7 +17,7 @@ CREATE TABLE `column_type_test` (
`c_blob` blob COMMENT 'blob',
`c_text` text COMMENT 'text',
`c_longblob` longblob COMMENT 'longblob'
) ENGINE=TIANMU;
) ENGINE=TIANMU DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
load data infile '../../std_data/tianmu/bigdata4load' into table column_type_test fields terminated by '|';
select count(*) from column_type_test;
count(*)
Expand All @@ -26,24 +26,24 @@ truncate table column_type_test;
show create table column_type_test;
Table Create Table
column_type_test CREATE TABLE `column_type_test` (
`c_tinyint` tinyint(4) DEFAULT NULL COMMENT 'tinyint',
`c_smallint` smallint(6) DEFAULT NULL COMMENT 'smallint',
`c_mediumint` mediumint(9) DEFAULT NULL COMMENT 'mediumint',
`c_int` int(11) DEFAULT NULL COMMENT 'int',
`c_bigint` bigint(20) DEFAULT NULL COMMENT 'bigint',
`c_tinyint` tinyint DEFAULT NULL COMMENT 'tinyint',
`c_smallint` smallint DEFAULT NULL COMMENT 'smallint',
`c_mediumint` mediumint DEFAULT NULL COMMENT 'mediumint',
`c_int` int DEFAULT NULL COMMENT 'int',
`c_bigint` bigint DEFAULT NULL COMMENT 'bigint',
`c_float` float DEFAULT NULL COMMENT 'float',
`c_double` double DEFAULT NULL COMMENT 'double',
`c_decimal` decimal(10,5) DEFAULT NULL COMMENT 'decimal',
`c_date` date DEFAULT NULL COMMENT 'date',
`c_datetime` datetime DEFAULT NULL COMMENT 'datetime',
`c_timestamp` timestamp NULL DEFAULT NULL COMMENT 'timestamp',
`c_time` time DEFAULT NULL COMMENT 'time',
`c_char` char(10) DEFAULT NULL COMMENT 'char',
`c_varchar` varchar(10) DEFAULT NULL COMMENT 'varchar',
`c_char` char(10) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'char',
`c_varchar` varchar(10) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'varchar',
`c_blob` blob COMMENT 'blob',
`c_text` text COMMENT 'text',
`c_text` text COLLATE utf8mb4_unicode_ci COMMENT 'text',
`c_longblob` longblob COMMENT 'longblob'
) ENGINE=TIANMU DEFAULT CHARSET=latin1
) ENGINE=TIANMU DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
insert into column_type_test(c_tinyint,c_smallint,c_mediumint,c_int,c_bigint,c_float,c_double,c_decimal,c_date,c_datetime,c_timestamp,c_time,c_char,c_varchar,c_text)
values(105,105,105,105,105,5.2,10.88,105.083,'2016-02-25','2016-02-25 10:20:01','2016-02-25 05:20:01','10:20:01','stoneatom','hello','bcdefghijklmn');
select * from column_type_test;
Expand Down
29 changes: 29 additions & 0 deletions mysql-test/suite/tianmu/t/truncate.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use test;
CREATE TABLE `column_type_test` (
`c_tinyint` tinyint DEFAULT NULL COMMENT 'tinyint',
`c_smallint` smallint DEFAULT NULL COMMENT 'smallint',
`c_mediumint` mediumint DEFAULT NULL COMMENT 'mediumint',
`c_int` int DEFAULT NULL COMMENT 'int',
`c_bigint` bigint DEFAULT NULL COMMENT 'bigint',
`c_float` float DEFAULT NULL COMMENT 'float',
`c_double` double DEFAULT NULL COMMENT 'double',
`c_decimal` decimal(10,5) DEFAULT NULL COMMENT 'decimal',
`c_date` date DEFAULT NULL COMMENT 'date',
`c_datetime` datetime DEFAULT NULL COMMENT 'datetime',
`c_timestamp` timestamp NULL DEFAULT NULL COMMENT 'timestamp',
`c_time` time DEFAULT NULL COMMENT 'time',
`c_char` char(10) DEFAULT NULL COMMENT 'char',
`c_varchar` varchar(10) DEFAULT NULL COMMENT 'varchar',
`c_blob` blob COMMENT 'blob',
`c_text` text COMMENT 'text',
`c_longblob` longblob COMMENT 'longblob'
) ENGINE=TIANMU DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

load data infile '../../std_data/tianmu/bigdata4load' into table column_type_test fields terminated by '|';
select count(*) from column_type_test;
truncate table column_type_test;
show create table column_type_test;
insert into column_type_test(c_tinyint,c_smallint,c_mediumint,c_int,c_bigint,c_float,c_double,c_decimal,c_date,c_datetime,c_timestamp,c_time,c_char,c_varchar,c_text)
values(105,105,105,105,105,5.2,10.88,105.083,'2016-02-25','2016-02-25 10:20:01','2016-02-25 05:20:01','10:20:01','stoneatom','hello','bcdefghijklmn');
select * from column_type_test;
drop table column_type_test;
6 changes: 5 additions & 1 deletion sql/sql_truncate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,11 @@ void Sql_cmd_truncate_table::truncate_base(THD *thd, TABLE_LIST *table_ref) {
This needs to be done even in case of failure so InnoDB SE
properly invalidates its internal cache.
*/
close_all_tables_for_name(thd, table_ref->table->s, false, nullptr);
/* Added by tianmu: check if tianmu table, if so, skip */
/* We should commit first before close table in tianmu truncate. */
if (table_ref->table->s->db_type() != tianmu_hton) {
close_all_tables_for_name(thd, table_ref->table->s, false, nullptr);
}
break;

case Truncate_result::FAILED_OPEN:
Expand Down
8 changes: 8 additions & 0 deletions storage/tianmu/core/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,14 @@ AttributeTypeInfo Engine::GetAttrTypeInfo(const Field &field) {
}

void Engine::CommitTx(THD *thd, bool all) {
TIANMU_LOG(LogCtl_Level::INFO, "commit txn");

// fix bug: issue759, in file sql_trunction.cc and function end_transaction(), will call CommitTx() twice
// skip for first time, called in trans_commit_stmt(), thd->server_status = SERVER_STATUS_IN_TRANS and all = false
// then called in trans_commit_implicit(), thd->server_status will be set "~SERVER_STATUS_IN_TRANS", and all = true
if ((thd->lex->sql_command == SQLCOM_TRUNCATE) && (thd->server_status & SERVER_STATUS_IN_TRANS)) {
return;
}
if (all || !thd_test_options(thd, OPTION_NOT_AUTOCOMMIT)) {
GetTx(thd)->Commit(thd);
}
Expand Down
1 change: 1 addition & 0 deletions storage/tianmu/core/transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ void Transaction::AddTableWRIfNeeded(std::shared_ptr<TableShare> &share) {
}

void Transaction::Commit([[maybe_unused]] THD *thd) {
TIANMU_LOG(LogCtl_Level::INFO, "txn commit, modified tables size in txn: [%d].", m_modified_tables.size());
for (auto const &iter : m_modified_tables) iter.second->CommitVersion();

m_modified_tables.clear();
Expand Down

0 comments on commit 1beaf0f

Please sign in to comment.