Skip to content

Commit

Permalink
Add simple unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
archang19 committed Jan 21, 2025
1 parent 221529a commit 7d35663
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
35 changes: 35 additions & 0 deletions db/db_compaction_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10733,6 +10733,41 @@ TEST_F(DBCompactionTest, RecordNewestKeyTimeForTtlCompaction) {
ASSERT_OK(dbfull()->TEST_WaitForCompact());
ASSERT_EQ(NumTableFilesAtLevel(0), 0);
}

TEST_F(DBCompactionTest, RecordNumberCompactionInputIterators) {
Options options;
SetTimeElapseOnlySleepOnReopen(&options);
options.env = CurrentOptions().env;
options.compaction_style = kCompactionStyleFIFO;
options.statistics = ROCKSDB_NAMESPACE::CreateDBStatistics();
options.write_buffer_size = 10 << 10; // 10KB
options.arena_block_size = 4096;
options.compression = kNoCompression;
options.create_if_missing = true;
options.compaction_options_fifo.allow_compaction = true;
options.num_levels = 1;
env_->SetMockSleep();
options.env = env_;
options.ttl = 1 * 60 * 60; // 1 hour
ASSERT_OK(TryReopen(options));

// Generate and flush 4 files, each about 10KB
Random rnd(301);
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 10; j++) {
ASSERT_OK(Put(std::to_string(i * 20 + j), rnd.RandomString(980)));
}
ASSERT_OK(Flush());
env_->MockSleepForSeconds(5);
}
ASSERT_OK(dbfull()->TEST_WaitForCompact());
ASSERT_EQ(NumTableFilesAtLevel(0), 1);
// We expect 4 input iterators (1 per L0 file)
HistogramData num_compaction_input_iterators;
options.statistics->histogramData(NUM_COMPACTION_INPUT_ITERATORS,
&num_compaction_input_iterators);
ASSERT_EQ(num_compaction_input_iterators.sum, 4);
}
} // namespace ROCKSDB_NAMESPACE

int main(int argc, char** argv) {
Expand Down
1 change: 1 addition & 0 deletions db/db_impl/db_impl_compaction_flush.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ bool DBImpl::EnoughRoomForCompaction(
size_t DBImpl::GetNumberCompactionInputIterators(Compaction* c) {
assert(c);
if (c->start_level() == 0) {
assert(0 < c->num_input_levels());
size_t num_l0_files = c->num_input_files(0);
size_t num_non_l0_levels = c->num_input_levels() - 1;
return num_l0_files + num_non_l0_levels;
Expand Down

0 comments on commit 7d35663

Please sign in to comment.