|
24 | 24 | #include "random/generators.h"
|
25 | 25 | #include "reflection/adl.h"
|
26 | 26 | #include "storage/batch_cache.h"
|
| 27 | +#include "storage/disk_log_impl.h" |
27 | 28 | #include "storage/log_manager.h"
|
28 | 29 | #include "storage/log_reader.h"
|
29 | 30 | #include "storage/ntp_config.h"
|
@@ -5179,3 +5180,87 @@ FIXTURE_TEST(test_offset_range_size_incremental, storage_test_fixture) {
|
5179 | 5180 | }
|
5180 | 5181 | }
|
5181 | 5182 | };
|
| 5183 | + |
| 5184 | +FIXTURE_TEST(dirty_ratio, storage_test_fixture) { |
| 5185 | + auto cfg = default_log_config(test_dir); |
| 5186 | + cfg.max_compacted_segment_size = config::mock_binding<size_t>(100_MiB); |
| 5187 | + cfg.cache = storage::with_cache::yes; |
| 5188 | + storage::ntp_config::default_overrides overrides; |
| 5189 | + overrides.cleanup_policy_bitflags |
| 5190 | + = model::cleanup_policy_bitflags::compaction; |
| 5191 | + |
| 5192 | + ss::abort_source as; |
| 5193 | + storage::log_manager mgr = make_log_manager(cfg); |
| 5194 | + auto deferred = ss::defer([&mgr]() mutable { mgr.stop().get(); }); |
| 5195 | + auto ntp = model::ntp("default", "test", 0); |
| 5196 | + auto log = mgr |
| 5197 | + .manage(storage::ntp_config( |
| 5198 | + ntp, |
| 5199 | + mgr.config().base_dir, |
| 5200 | + std::make_unique<storage::ntp_config::default_overrides>( |
| 5201 | + overrides))) |
| 5202 | + .get(); |
| 5203 | + |
| 5204 | + auto* disk_log = static_cast<storage::disk_log_impl*>(log.get()); |
| 5205 | + |
| 5206 | + // add a segment with random keys until a certain size |
| 5207 | + auto add_segment = [log](size_t size, model::term_id term) { |
| 5208 | + do { |
| 5209 | + append_single_record_batch(log, 1, term, 16_KiB, true); |
| 5210 | + } while (log->segments().back()->size_bytes() < size); |
| 5211 | + }; |
| 5212 | + |
| 5213 | + static constexpr double tolerance = 1.0e-6; |
| 5214 | + uint64_t closed_segments_size_bytes = 0; |
| 5215 | + |
| 5216 | + auto assert_on_new_segment = [&disk_log, &closed_segments_size_bytes, &as]( |
| 5217 | + size_t index) { |
| 5218 | + auto new_segment_size_bytes = disk_log->segments()[index]->size_bytes(); |
| 5219 | + closed_segments_size_bytes += new_segment_size_bytes; |
| 5220 | + auto expected_dirty_ratio = static_cast<double>(new_segment_size_bytes) |
| 5221 | + / static_cast<double>( |
| 5222 | + closed_segments_size_bytes); |
| 5223 | + BOOST_REQUIRE_EQUAL( |
| 5224 | + disk_log->dirty_segment_bytes(), new_segment_size_bytes); |
| 5225 | + BOOST_REQUIRE_EQUAL( |
| 5226 | + disk_log->closed_segment_bytes(), closed_segments_size_bytes); |
| 5227 | + BOOST_REQUIRE_CLOSE( |
| 5228 | + disk_log->dirty_ratio(), expected_dirty_ratio, tolerance); |
| 5229 | + |
| 5230 | + // Perform sliding window compaction, which will fully cleanly compact |
| 5231 | + // the log. |
| 5232 | + static const storage::compaction_config compact_cfg( |
| 5233 | + model::offset::max(), std::nullopt, ss::default_priority_class(), as); |
| 5234 | + disk_log->sliding_window_compact(compact_cfg).get(); |
| 5235 | + |
| 5236 | + BOOST_REQUIRE_EQUAL(disk_log->dirty_segment_bytes(), 0); |
| 5237 | + BOOST_REQUIRE_EQUAL( |
| 5238 | + disk_log->closed_segment_bytes(), closed_segments_size_bytes); |
| 5239 | + BOOST_REQUIRE_CLOSE(disk_log->dirty_ratio(), 0.0, tolerance); |
| 5240 | + }; |
| 5241 | + |
| 5242 | + add_segment(2_MiB, model::term_id(1)); |
| 5243 | + disk_log->force_roll(ss::default_priority_class()).get(); |
| 5244 | + BOOST_REQUIRE_EQUAL(disk_log->segment_count(), 2); |
| 5245 | + assert_on_new_segment(0); |
| 5246 | + |
| 5247 | + add_segment(2_MiB, model::term_id(1)); |
| 5248 | + disk_log->force_roll(ss::default_priority_class()).get(); |
| 5249 | + BOOST_REQUIRE_EQUAL(disk_log->segment_count(), 3); |
| 5250 | + assert_on_new_segment(1); |
| 5251 | + |
| 5252 | + add_segment(5_MiB, model::term_id(1)); |
| 5253 | + disk_log->force_roll(ss::default_priority_class()).get(); |
| 5254 | + BOOST_REQUIRE_EQUAL(disk_log->segment_count(), 4); |
| 5255 | + assert_on_new_segment(2); |
| 5256 | + |
| 5257 | + add_segment(16_KiB, model::term_id(1)); |
| 5258 | + disk_log->force_roll(ss::default_priority_class()).get(); |
| 5259 | + BOOST_REQUIRE_EQUAL(disk_log->segment_count(), 5); |
| 5260 | + assert_on_new_segment(3); |
| 5261 | + |
| 5262 | + add_segment(16_KiB, model::term_id(1)); |
| 5263 | + disk_log->force_roll(ss::default_priority_class()).get(); |
| 5264 | + BOOST_REQUIRE_EQUAL(disk_log->segment_count(), 6); |
| 5265 | + assert_on_new_segment(4); |
| 5266 | +} |
0 commit comments