Skip to content

Commit

Permalink
MyRocks: Flush memtables on clean shutdown (facebook#127)
Browse files Browse the repository at this point in the history
Summary:
On clean shutdown, memtables were not flushed. This caused
data loss, if WAL was disabled. This diff flushes all
memtables before calling CancelAllBackgroundWork().

Differential Revision: https://reviews.facebook.net/D52257

fbshipit-source-id: 1c87c632249
  • Loading branch information
yoshinorim authored and inikep committed Aug 30, 2021
1 parent 79986a1 commit 82c3727
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 4 deletions.
16 changes: 16 additions & 0 deletions mysql-test/suite/rocksdb/r/insert_optimized_config.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
DROP TABLE IF EXISTS t1;
create table t1(
id bigint not null primary key,
i1 bigint, #unique
i2 bigint, #repeating
c1 varchar(20), #unique
c2 varchar(20), #repeating
index t1_2(i1)
) engine=rocksdb;
select count(*), sum(id), sum(i1), sum(i2) from t1;
count(*) sum(id) sum(i1) sum(i2)
50000 1250025000 1250025000 124980000
select count(*), sum(id), sum(i1), sum(i2) from t1;
count(*) sum(id) sum(i1) sum(i2)
50000 1250025000 1250025000 124980000
drop table t1;
6 changes: 6 additions & 0 deletions mysql-test/suite/rocksdb/t/insert_optimized_config-master.opt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
--rocksdb_write_disable_wal=1
--rocksdb_default_cf_options=write_buffer_size=16k;target_file_size_base=16k;level0_file_num_compaction_trigger=4;level0_slowdown_writes_trigger=256;level0_stop_writes_trigger=256;max_write_buffer_number=16;compression_per_level=kNoCompression;memtable=vector:1024
--rocksdb_override_cf_options=__system__={memtable=skip_list:16}
--rocksdb_compaction_sequential_deletes=0
--rocksdb_compaction_sequential_deletes_window=0

41 changes: 41 additions & 0 deletions mysql-test/suite/rocksdb/t/insert_optimized_config.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
--source include/have_rocksdb.inc

--disable_warnings
DROP TABLE IF EXISTS t1;
--enable_warnings

create table t1(
id bigint not null primary key,
i1 bigint, #unique
i2 bigint, #repeating
c1 varchar(20), #unique
c2 varchar(20), #repeating
index t1_2(i1)
) engine=rocksdb;

--disable_query_log
set rocksdb_bulk_load=1;
let $i=0;
while ($i<50000)
{
inc $i;
eval insert t1(id, i1, i2, c1, c2) values($i, $i, $i div 10, $i, $i div 10);
}
set rocksdb_bulk_load=0;
--enable_query_log

select count(*), sum(id), sum(i1), sum(i2) from t1;

# reload without load optimized config
let $restart_file= $MYSQLTEST_VARDIR/tmp/mysqld.1.expect;
--exec echo "wait" > $restart_file
--shutdown_server 10
--source include/wait_until_disconnected.inc
-- exec echo "restart:--rocksdb_write_disable_wal=0 --rocksdb_default_cf_options=write_buffer_size=64k;target_file_size_base=64k;max_bytes_for_level_base=1m;compression_per_level=kNoCompression;" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
-- enable_reconnect
-- source include/wait_until_connected_again.inc

select count(*), sum(id), sum(i1), sum(i2) from t1;

drop table t1;

17 changes: 13 additions & 4 deletions storage/rocksdb/ha_rocksdb.cc
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,15 @@ background_thread_control bg_control;
// collation check requirement.
std::vector<std::string> collation_exception_list;

static void
rocksdb_flush_all_memtables()
{
Column_family_manager& cf_manager = rocksdb_get_cf_manager();
for (auto cf_handle : cf_manager.get_all_cf()) {
rdb->Flush(rocksdb::FlushOptions(), cf_handle);
}
}

static void
rocksdb_compact_column_family(THD* thd,
struct st_mysql_sys_var* var,
Expand Down Expand Up @@ -232,10 +241,7 @@ rocksdb_force_flush_memtable_now(THD* thd,
const void* save)
{
sql_print_information("RocksDB: Manual memtable flush\n");
Column_family_manager& cf_manager = rocksdb_get_cf_manager();
for (auto cf_handle : cf_manager.get_all_cf()) {
rdb->Flush(rocksdb::FlushOptions(), cf_handle);
}
rocksdb_flush_all_memtables();
}

static void
Expand Down Expand Up @@ -2419,6 +2425,9 @@ static int rocksdb_done_func(void *p)
// signal the drop index thread to stop
signal_drop_index_thread(true);

// Flush all memtables for not lose data, even if WAL is disabled.
rocksdb_flush_all_memtables();

// Stop all rocksdb background work
CancelAllBackgroundWork(rdb->GetBaseDB(), true);

Expand Down

0 comments on commit 82c3727

Please sign in to comment.