Skip to content

Commit

Permalink
Adds capability to set different BottommostLevelCompaction option for…
Browse files Browse the repository at this point in the history
… MyRocks Manual Compaction

Upstream commit ID : fb-mysql-5.6.35/49e8df3e1ce20d06b41f14ad9d471be9e0235a55
PS-7767 : Merge percona-202103

Summary:
Currently BottommostLevelCompaction is set to default kForceOptimized for
manual compaction. This is fine usually. However, for privacy triggered
compactions, it does not make much sense, because it compacts Lmax SST files
that do not have any overlapping key updates at all. This adds a session
variable to specify the option for bottommost level compaction before
triggering manual compaction.

Reviewed By: yoshinorim, yizhang82

Differential Revision: D27665464

fbshipit-source-id: 2bbdf04ec18
  • Loading branch information
rahku authored and inikep committed Jul 5, 2021
1 parent d8f3a24 commit f4ea4d4
Show file tree
Hide file tree
Showing 8 changed files with 266 additions and 13 deletions.
@@ -0,0 +1,36 @@
CREATE PROCEDURE compact_start()
BEGIN
select variable_value into @c from performance_schema.global_status where variable_name='rocksdb_compact_write_bytes';
END//
CREATE PROCEDURE compact_end()
BEGIN
select case when variable_value-@c > 0 then 'true' else 'false' end as checked from performance_schema.global_status where variable_name='rocksdb_compact_write_bytes';
END//
CREATE TABLE t1 (
a int not null,
b int not null,
primary key (a,b),
key (b)
) ENGINE=RocksDB;
DELETE FROM t1;
set @@session.rocksdb_manual_compaction_bottommost_level='kSkip';
call compact_start();
set @@global.rocksdb_compact_cf = 'default';
call compact_end();
checked
true
set @@session.rocksdb_manual_compaction_bottommost_level='kSkip';
call compact_start();
set @@global.rocksdb_compact_cf = 'default';
call compact_end();
checked
false
set @@session.rocksdb_manual_compaction_bottommost_level='kForceOptimized';
call compact_start();
set @@global.rocksdb_compact_cf = 'default';
call compact_end();
checked
true
DROP PROCEDURE compact_start;
DROP PROCEDURE compact_end;
drop table t1;
1 change: 1 addition & 0 deletions mysql-test/suite/rocksdb/r/rocksdb.result
Expand Up @@ -969,6 +969,7 @@ rocksdb_lock_scanned_rows OFF
rocksdb_lock_wait_timeout 1
rocksdb_log_file_time_to_roll 0
rocksdb_manifest_preallocation_size 4194304
rocksdb_manual_compaction_bottommost_level kForceOptimized
rocksdb_manual_compaction_threads 0
rocksdb_manual_wal_flush ON
rocksdb_master_skip_tx_api OFF
Expand Down
@@ -0,0 +1,2 @@
--loose-rocksdb_max_subcompactions=1
--loose-rocksdb_default_cf_options=target_file_size_base=100k;max_bytes_for_level_multiplier=1;max_bytes_for_level_base=1m;target_file_size_multiplier=1
48 changes: 48 additions & 0 deletions mysql-test/suite/rocksdb/t/manual_compaction_bottommost_level.test
@@ -0,0 +1,48 @@
--source include/have_rocksdb.inc

DELIMITER //;
CREATE PROCEDURE compact_start()
BEGIN
select variable_value into @c from performance_schema.global_status where variable_name='rocksdb_compact_write_bytes';
END//
CREATE PROCEDURE compact_end()
BEGIN
select case when variable_value-@c > 0 then 'true' else 'false' end as checked from performance_schema.global_status where variable_name='rocksdb_compact_write_bytes';
END//
DELIMITER ;//

CREATE TABLE t1 (
a int not null,
b int not null,
primary key (a,b),
key (b)
) ENGINE=RocksDB;

# Populate tables
let $max = 1000;
let $table = t1;
--source ../include/drop_table_repopulate_table.inc

# compact to move all data in files in Lmax level
set @@session.rocksdb_manual_compaction_bottommost_level='kSkip';
call compact_start();
set @@global.rocksdb_compact_cf = 'default';
call compact_end(); # should return true as compaction of lower levels is performed

# skip compaction of files in bottommost level i.e. Lmax->Lmax compaction and ensure rocksdb_compact_write_byte
# does not increase after compaction
set @@session.rocksdb_manual_compaction_bottommost_level='kSkip';
call compact_start();
set @@global.rocksdb_compact_cf = 'default';
call compact_end(); # should return false as files only in bottommost layer

# restore 'kForceOptimized' default setting and check that value of rocksdb_compact_write_byte increses after compaction
set @@session.rocksdb_manual_compaction_bottommost_level='kForceOptimized';
call compact_start();
set @@global.rocksdb_compact_cf = 'default';
call compact_end(); # should return true

# cleanup
DROP PROCEDURE compact_start;
DROP PROCEDURE compact_end;
drop table t1;
@@ -0,0 +1,114 @@
CREATE TABLE valid_values (value varchar(255)) ENGINE=myisam;
INSERT INTO valid_values VALUES('kSkip');
INSERT INTO valid_values VALUES('kIfHaveCompactionFilter');
INSERT INTO valid_values VALUES('kForce');
INSERT INTO valid_values VALUES('kForceOptimized');
CREATE TABLE invalid_values (value varchar(255)) ENGINE=myisam;
INSERT INTO invalid_values VALUES('\'aaa\'');
SET @start_global_value = @@global.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL;
SELECT @start_global_value;
@start_global_value
kForceOptimized
SET @start_session_value = @@session.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL;
SELECT @start_session_value;
@start_session_value
kForceOptimized
'# Setting to valid values in global scope#'
"Trying to set variable @@global.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL to kSkip"
SET @@global.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL = kSkip;
SELECT @@global.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL;
@@global.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL
kSkip
"Setting the global scope variable back to default"
SET @@global.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL = DEFAULT;
SELECT @@global.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL;
@@global.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL
kForceOptimized
"Trying to set variable @@global.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL to kIfHaveCompactionFilter"
SET @@global.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL = kIfHaveCompactionFilter;
SELECT @@global.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL;
@@global.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL
kIfHaveCompactionFilter
"Setting the global scope variable back to default"
SET @@global.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL = DEFAULT;
SELECT @@global.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL;
@@global.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL
kForceOptimized
"Trying to set variable @@global.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL to kForce"
SET @@global.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL = kForce;
SELECT @@global.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL;
@@global.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL
kForce
"Setting the global scope variable back to default"
SET @@global.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL = DEFAULT;
SELECT @@global.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL;
@@global.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL
kForceOptimized
"Trying to set variable @@global.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL to kForceOptimized"
SET @@global.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL = kForceOptimized;
SELECT @@global.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL;
@@global.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL
kForceOptimized
"Setting the global scope variable back to default"
SET @@global.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL = DEFAULT;
SELECT @@global.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL;
@@global.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL
kForceOptimized
'# Setting to valid values in session scope#'
"Trying to set variable @@session.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL to kSkip"
SET @@session.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL = kSkip;
SELECT @@session.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL;
@@session.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL
kSkip
"Setting the session scope variable back to default"
SET @@session.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL = DEFAULT;
SELECT @@session.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL;
@@session.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL
kForceOptimized
"Trying to set variable @@session.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL to kIfHaveCompactionFilter"
SET @@session.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL = kIfHaveCompactionFilter;
SELECT @@session.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL;
@@session.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL
kIfHaveCompactionFilter
"Setting the session scope variable back to default"
SET @@session.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL = DEFAULT;
SELECT @@session.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL;
@@session.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL
kForceOptimized
"Trying to set variable @@session.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL to kForce"
SET @@session.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL = kForce;
SELECT @@session.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL;
@@session.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL
kForce
"Setting the session scope variable back to default"
SET @@session.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL = DEFAULT;
SELECT @@session.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL;
@@session.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL
kForceOptimized
"Trying to set variable @@session.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL to kForceOptimized"
SET @@session.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL = kForceOptimized;
SELECT @@session.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL;
@@session.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL
kForceOptimized
"Setting the session scope variable back to default"
SET @@session.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL = DEFAULT;
SELECT @@session.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL;
@@session.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL
kForceOptimized
'# Testing with invalid values in global scope #'
"Trying to set variable @@global.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL to 'aaa'"
SET @@global.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL = 'aaa';
Got one of the listed errors
SELECT @@global.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL;
@@global.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL
kForceOptimized
SET @@global.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL = @start_global_value;
SELECT @@global.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL;
@@global.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL
kForceOptimized
SET @@session.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL = @start_session_value;
SELECT @@session.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL;
@@session.ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL
kForceOptimized
DROP TABLE valid_values;
DROP TABLE invalid_values;
@@ -0,0 +1,18 @@
--source include/have_rocksdb.inc

CREATE TABLE valid_values (value varchar(255)) ENGINE=myisam;
INSERT INTO valid_values VALUES('kSkip');
INSERT INTO valid_values VALUES('kIfHaveCompactionFilter');
INSERT INTO valid_values VALUES('kForce');
INSERT INTO valid_values VALUES('kForceOptimized');

CREATE TABLE invalid_values (value varchar(255)) ENGINE=myisam;
INSERT INTO invalid_values VALUES('\'aaa\'');

--let $sys_var=ROCKSDB_MANUAL_COMPACTION_BOTTOMMOST_LEVEL
--let $read_only=0
--let $session=1
--source ../include/rocksdb_sys_var.inc

DROP TABLE valid_values;
DROP TABLE invalid_values;
50 changes: 40 additions & 10 deletions storage/rocksdb/ha_rocksdb.cc
Expand Up @@ -155,10 +155,12 @@ static handler *rocksdb_create_handler(my_core::handlerton *hton,
my_core::MEM_ROOT *mem_root);

static rocksdb::CompactRangeOptions getCompactRangeOptions(
int concurrency = 0) {
int concurrency = 0,
rocksdb::BottommostLevelCompaction bottommost_level_compaction =
rocksdb::BottommostLevelCompaction::kForceOptimized) {
rocksdb::CompactRangeOptions compact_range_options;
compact_range_options.bottommost_level_compaction =
rocksdb::BottommostLevelCompaction::kForceOptimized;
bottommost_level_compaction;
compact_range_options.exclusive_manual_compaction = false;
if (concurrency > 0) {
compact_range_options.max_subcompactions = concurrency;
Expand Down Expand Up @@ -911,10 +913,20 @@ static TYPELIB info_log_level_typelib = {
array_elements(info_log_level_names) - 1, "info_log_level_typelib",
info_log_level_names, nullptr};

static void rocksdb_set_rocksdb_info_log_level(THD *const thd,
struct SYS_VAR *const var,
void *const var_ptr,
const void *const save) {
/* This enum needs to be kept up to date with rocksdb::BottommostLevelCompaction
*/
static const char *bottommost_level_compaction_names[] = {
"kSkip", "kIfHaveCompactionFilter", "kForce", "kForceOptimized", NullS};

static TYPELIB bottommost_level_compaction_typelib = {
array_elements(bottommost_level_compaction_names) - 1,
"bottommost_level_compaction_typelib", bottommost_level_compaction_names,
nullptr};

static void rocksdb_set_rocksdb_info_log_level(
THD *const thd MY_ATTRIBUTE((__unused__)),
struct SYS_VAR *const var MY_ATTRIBUTE((__unused__)),
void *const var_ptr MY_ATTRIBUTE((__unused__)), const void *const save) {
DBUG_ASSERT(save != nullptr);

RDB_MUTEX_LOCK_CHECK(rdb_sysvars_mutex);
Expand Down Expand Up @@ -1261,6 +1273,15 @@ static MYSQL_THDVAR_INT(
/* default rocksdb.dboption max_subcompactions */ 0,
/* min */ 0, /* max */ 128, 0);

static MYSQL_THDVAR_ENUM(
manual_compaction_bottommost_level, PLUGIN_VAR_RQCMDARG,
"Option for bottommost level compaction during manual "
"compaction",
nullptr, nullptr,
/* default */
(ulong)rocksdb::BottommostLevelCompaction::kForceOptimized,
&bottommost_level_compaction_typelib);

static MYSQL_SYSVAR_BOOL(
create_if_missing,
*static_cast<bool *>(&rocksdb_db_options->create_if_missing),
Expand Down Expand Up @@ -2444,6 +2465,7 @@ static struct SYS_VAR *rocksdb_system_variables[] = {
MYSQL_SYSVAR(debug_manual_compaction_delay),
MYSQL_SYSVAR(max_manual_compactions),
MYSQL_SYSVAR(manual_compaction_threads),
MYSQL_SYSVAR(manual_compaction_bottommost_level),
MYSQL_SYSVAR(rollback_on_timeout),

MYSQL_SYSVAR(enable_insert_with_update_caching),
Expand Down Expand Up @@ -2477,8 +2499,13 @@ static int rocksdb_compact_column_family(THD *const thd,

auto cfh = cf_manager.get_cf(cf_name);
if (cfh != nullptr && rdb != nullptr) {
rocksdb::BottommostLevelCompaction bottommost_level_compaction =
(rocksdb::BottommostLevelCompaction)THDVAR(
thd, manual_compaction_bottommost_level);

int mc_id = rdb_mc_thread.request_manual_compaction(
cfh, nullptr, nullptr, THDVAR(thd, manual_compaction_threads));
cfh, nullptr, nullptr, THDVAR(thd, manual_compaction_threads),
bottommost_level_compaction);
if (mc_id == -1) {
my_error(ER_INTERNAL_ERROR, MYF(0),
"Can't schedule more manual compactions. "
Expand Down Expand Up @@ -14551,8 +14578,9 @@ void Rdb_manual_compaction_thread::run() {
// it is cancelled by CancelAllBackgroundWork, then status is
// set to shutdownInProgress.
const rocksdb::Status s =
rdb->CompactRange(getCompactRangeOptions(mcr.concurrency), mcr.cf.get(),
mcr.start, mcr.limit);
rdb->CompactRange(getCompactRangeOptions(
mcr.concurrency, mcr.bottommost_level_compaction),
mcr.cf.get(), mcr.start, mcr.limit);

rocksdb_manual_compactions_running--;
if (s.ok()) {
Expand Down Expand Up @@ -14615,7 +14643,8 @@ void Rdb_manual_compaction_thread::clear_manual_compaction_request(

int Rdb_manual_compaction_thread::request_manual_compaction(
std::shared_ptr<rocksdb::ColumnFamilyHandle> cf, rocksdb::Slice *start,
rocksdb::Slice *limit, int concurrency) {
rocksdb::Slice *limit, int concurrency,
rocksdb::BottommostLevelCompaction bottommost_level_compaction) {
int mc_id = -1;
RDB_MUTEX_LOCK_CHECK(m_mc_mutex);
if (m_requests.size() >= rocksdb_max_manual_compactions) {
Expand All @@ -14629,6 +14658,7 @@ int Rdb_manual_compaction_thread::request_manual_compaction(
mcr.start = start;
mcr.limit = limit;
mcr.concurrency = concurrency;
mcr.bottommost_level_compaction = bottommost_level_compaction;
m_requests.insert(std::make_pair(mcr.mc_id, mcr));
RDB_MUTEX_UNLOCK_CHECK(m_mc_mutex);
return mc_id;
Expand Down
10 changes: 7 additions & 3 deletions storage/rocksdb/rdb_threads.h
Expand Up @@ -149,6 +149,8 @@ class Rdb_manual_compaction_thread : public Rdb_thread {
rocksdb::Slice *start;
rocksdb::Slice *limit;
int concurrency = 0;
rocksdb::BottommostLevelCompaction bottommost_level_compaction =
rocksdb::BottommostLevelCompaction::kForceOptimized;
};

int m_latest_mc_id;
Expand All @@ -165,9 +167,11 @@ class Rdb_manual_compaction_thread : public Rdb_thread {
}

virtual void run() override;
int request_manual_compaction(std::shared_ptr<rocksdb::ColumnFamilyHandle> cf,
rocksdb::Slice *start, rocksdb::Slice *limit,
int concurrency = 0);
int request_manual_compaction(
std::shared_ptr<rocksdb::ColumnFamilyHandle> cf, rocksdb::Slice *start,
rocksdb::Slice *limit, int concurrency = 0,
rocksdb::BottommostLevelCompaction bottommost_level_compaction =
rocksdb::BottommostLevelCompaction::kForceOptimized);
bool is_manual_compaction_finished(int mc_id);
void clear_manual_compaction_request(int mc_id, bool init_only = false);
void clear_all_manual_compaction_requests();
Expand Down

0 comments on commit f4ea4d4

Please sign in to comment.