Line data Source code
1 : use once_cell::sync::OnceCell;
2 : use pageserver_compaction::interface::CompactionLayer;
3 : use pageserver_compaction::simulator::MockTimeline;
4 : use utils::logging;
5 :
6 : static LOG_HANDLE: OnceCell<()> = OnceCell::new();
7 :
8 12 : pub(crate) fn setup_logging() {
9 12 : LOG_HANDLE.get_or_init(|| {
10 12 : logging::init(
11 12 : logging::LogFormat::Test,
12 12 : logging::TracingErrorLayerEnablement::EnableWithRustLogFilter,
13 12 : logging::Output::Stdout,
14 12 : )
15 12 : .expect("Failed to init test logging")
16 12 : });
17 12 : }
18 :
19 : /// Test the extreme case that there are so many updates for a single key that
20 : /// even if we produce an extremely narrow delta layer, spanning just that one
21 : /// key, we still too many records to fit in the target file size. We need to
22 : /// split in the LSN dimension too in that case.
23 : #[tokio::test]
24 6 : async fn test_many_updates_for_single_key() {
25 6 : setup_logging();
26 6 : let mut executor = MockTimeline::new();
27 6 : executor.target_file_size = 1_000_000; // 1 MB
28 6 :
29 6 : // Ingest 10 MB of updates to a single key.
30 6000 : for _ in 1..1000 {
31 5994 : executor.ingest_uniform(100, 10, &(0..100_000)).unwrap();
32 5994 : executor.ingest_uniform(1000, 10, &(0..1)).unwrap();
33 5994 : executor.compact().await.unwrap();
34 6 : }
35 6 :
36 6 : // Check that all the layers are smaller than the target size (with some slop)
37 72 : for l in executor.live_layers.iter() {
38 72 : println!("layer {}: {}", l.short_id(), l.file_size());
39 72 : }
40 72 : for l in executor.live_layers.iter() {
41 72 : assert!(l.file_size() < executor.target_file_size * 2);
42 6 : // Sanity check that none of the delta layers are empty either.
43 72 : if l.is_delta() {
44 72 : assert!(l.file_size() > 0);
45 6 : }
46 6 : }
47 6 : }
48 :
49 : #[tokio::test]
50 6 : async fn test_simple_updates() {
51 6 : setup_logging();
52 6 : let mut executor = MockTimeline::new();
53 6 : executor.target_file_size = 500_000; // 500 KB
54 6 :
55 6 : // Ingest some traffic.
56 2400 : for _ in 1..400 {
57 2394 : executor.ingest_uniform(100, 500, &(0..100_000)).unwrap();
58 2394 : }
59 6 :
60 234 : for l in executor.live_layers.iter() {
61 234 : println!("layer {}: {}", l.short_id(), l.file_size());
62 234 : }
63 6 :
64 6 : println!("Running compaction...");
65 6 : executor.compact().await.unwrap();
66 6 :
67 234 : for l in executor.live_layers.iter() {
68 234 : println!("layer {}: {}", l.short_id(), l.file_size());
69 234 : }
70 6 : }
|