Skip to content

Commit

Permalink
Use special sentinel for default & handle set 0 differently for direc…
Browse files Browse the repository at this point in the history
… and non-direct IO
  • Loading branch information
hx235 committed Aug 28, 2023
1 parent 310a242 commit 268c2d1
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 28 deletions.
7 changes: 4 additions & 3 deletions db/db_impl/db_impl_open.cc
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,12 @@ DBOptions SanitizeOptions(const std::string& dbname, const DBOptions& src,
result.wal_dir = result.wal_dir.substr(0, result.wal_dir.size() - 1);
}

if (result.compaction_readahead_size == 0) {
if (result.use_direct_reads) {
if (result.compaction_readahead_size == Options().compaction_readahead_size ||
(result.use_direct_reads && result.compaction_readahead_size == 0)) {
if (result.use_direct_reads && result.compaction_readahead_size == 0) {
TEST_SYNC_POINT_CALLBACK("SanitizeOptions:direct_io", nullptr);
}
result.compaction_readahead_size = 1024 * 1024 * 2;
result.compaction_readahead_size = 2 * 1024 * 1024;
}

// Force flush on DB open if 2PC is enabled, since with 2PC we have no
Expand Down
54 changes: 32 additions & 22 deletions db/db_options_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1034,30 +1034,40 @@ TEST_F(DBOptionsTest, SetFIFOCompactionOptions) {
}

TEST_F(DBOptionsTest, CompactionReadaheadSizeChange) {
SpecialEnv env(env_);
Options options;
options.env = &env;

options.compaction_readahead_size = 0;
options.level0_file_num_compaction_trigger = 2;
const std::string kValue(1024, 'v');
Reopen(options);

ASSERT_EQ(1024 * 1024 * 2,
dbfull()->GetDBOptions().compaction_readahead_size);
ASSERT_OK(dbfull()->SetDBOptions({{"compaction_readahead_size", "256"}}));
ASSERT_EQ(256, dbfull()->GetDBOptions().compaction_readahead_size);
for (int i = 0; i < 1024; i++) {
ASSERT_OK(Put(Key(i), kValue));
}
ASSERT_OK(Flush());
for (int i = 0; i < 1024 * 2; i++) {
ASSERT_OK(Put(Key(i), kValue));
for (bool direct_io : {true, false}) {
SpecialEnv env(env_);
Options options;
options.env = &env;
options.level0_file_num_compaction_trigger = 2;
options.use_direct_reads = direct_io;
Reopen(options);
// Verify the default value of `Option::compaction_readahead_size`
ASSERT_EQ(2 * 1024 * 1024,
dbfull()->GetDBOptions().compaction_readahead_size);
Close();
options.compaction_readahead_size = 0;
Reopen(options);
// Verify the effect of setting `Option::compaction_readahead_size` to be
// zero under direct IO and non-Direct IO
ASSERT_EQ(direct_io ? 2 * 1024 * 1024 : 0,
dbfull()->GetDBOptions().compaction_readahead_size);
// Verify `Option::compaction_readahead_size` is set dynamically to positive
// value correctly
ASSERT_OK(dbfull()->SetDBOptions({{"compaction_readahead_size", "256"}}));
ASSERT_EQ(256, dbfull()->GetDBOptions().compaction_readahead_size);
for (int i = 0; i < 1024; i++) {
ASSERT_OK(Put(Key(i), kValue));
}
ASSERT_OK(Flush());
for (int i = 0; i < 1024 * 2; i++) {
ASSERT_OK(Put(Key(i), kValue));
}
ASSERT_OK(Flush());
ASSERT_OK(dbfull()->TEST_WaitForCompact());
ASSERT_EQ(256, env_->compaction_readahead_size_);
Close();
}
ASSERT_OK(Flush());
ASSERT_OK(dbfull()->TEST_WaitForCompact());
ASSERT_EQ(256, env_->compaction_readahead_size_);
Close();
}

TEST_F(DBOptionsTest, FIFOTtlBackwardCompatible) {
Expand Down
9 changes: 6 additions & 3 deletions include/rocksdb/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -952,16 +952,19 @@ struct DBOptions {
AccessHint access_hint_on_compaction_start = NORMAL;

// The size RocksDB uses to perform readahead during compaction read.
// If set zero, RocksDB will sanitize it to be 2MB during db open.
// If you're
// running RocksDB on spinning disks, you should set this to at least 2MB.
// That way RocksDB's compaction is doing sequential instead of random reads.
//
//
// Default: 0
// Default: 2MB
//
// For direct IO, if set 0, RocksDB will sanitize the value to the default.
// For non-direct IO, if set 0, RocksDB will disable compaction readahead
// instead.
//
// Dynamically changeable through SetDBOptions() API.
size_t compaction_readahead_size = 0;
size_t compaction_readahead_size = 0xfffffffffffffffe;

// This is a maximum buffer size that is used by WinMmapReadableFile in
// unbuffered disk I/O mode. We need to maintain an aligned buffer for
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`Options::compaction_readahead_size` 's default value is changed from 0 to 2MB. If it's explictly set 0, for direct IO, RocksDB will sanitize the value to the default, while for non-direct IO, RocksDB will disable compaction readahead instead.

0 comments on commit 268c2d1

Please sign in to comment.