Skip to content

Commit ddf1594

Browse files
committed
Merge branch 'master' into add-lru-cache
Signed-off-by: Jay Lee <BusyJayLee@gmail.com>
2 parents ce9b1a2 + 39f4db4 commit ddf1594

File tree

2 files changed

+61
-9
lines changed

2 files changed

+61
-9
lines changed

src/config.rs

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright (c) 2017-present, PingCAP, Inc. Licensed under Apache-2.0.
22

3-
use log::warn;
3+
use log::{info, warn};
44
use serde::{Deserialize, Serialize};
55

66
use crate::pipe_log::Version;
@@ -109,6 +109,13 @@ pub struct Config {
109109
/// Default: false
110110
pub prefill_for_recycle: bool,
111111

112+
/// Maximum capacity for preparing log files for recycling when start.
113+
/// If not `None`, its size is equal to `purge-threshold`.
114+
/// Only available for `prefill-for-recycle` is true.
115+
///
116+
/// Default: None
117+
pub prefill_limit: Option<ReadableSize>,
118+
112119
/// Initial cache capacity for entries.
113120
pub cache_capacity: ReadableSize,
114121
}
@@ -133,6 +140,7 @@ impl Default for Config {
133140
enable_log_recycle: false,
134141
prefill_for_recycle: false,
135142
cache_capacity: ReadableSize::mb(256),
143+
prefill_limit: None,
136144
};
137145
// Test-specific configurations.
138146
#[cfg(test)]
@@ -184,6 +192,14 @@ impl Config {
184192
"prefill is not allowed when log recycle is disabled"
185193
));
186194
}
195+
if !self.prefill_for_recycle && self.prefill_limit.is_some() {
196+
warn!("prefill-limit will be ignored when prefill is disabled");
197+
self.prefill_limit = None;
198+
}
199+
if self.prefill_for_recycle && self.prefill_limit.is_none() {
200+
info!("prefill-limit will be calibrated to purge-threshold");
201+
self.prefill_limit = Some(self.purge_threshold);
202+
}
187203
#[cfg(not(feature = "swap"))]
188204
if self.memory_limit.is_some() {
189205
warn!("memory-limit will be ignored because swap feature is disabled");
@@ -211,6 +227,25 @@ impl Config {
211227
0
212228
}
213229
}
230+
231+
/// Returns the capacity for preparing log files for recycling when start.
232+
pub(crate) fn prefill_capacity(&self) -> usize {
233+
// Attention please, log files with Version::V1 could not be recycled, so it's
234+
// useless for prefill.
235+
if !self.enable_log_recycle || !self.format_version.has_log_signing() {
236+
return 0;
237+
}
238+
let prefill_limit = self.prefill_limit.unwrap_or(ReadableSize(0)).0;
239+
if self.prefill_for_recycle && prefill_limit >= self.target_file_size.0 {
240+
// Keep same with the maximum setting of `recycle_capacity`.
241+
std::cmp::min(
242+
(prefill_limit / self.target_file_size.0) as usize + 2,
243+
u32::MAX as usize,
244+
)
245+
} else {
246+
0
247+
}
248+
}
214249
}
215250

216251
#[cfg(test)]
@@ -308,4 +343,24 @@ mod tests {
308343
.unwrap()
309344
.contains("tolerate-corrupted-tail-records"));
310345
}
346+
347+
#[test]
348+
fn test_prefill_for_recycle() {
349+
let default_prefill_v1 = r#"
350+
enable-log-recycle = true
351+
prefill-for-recycle = true
352+
"#;
353+
let mut cfg_load: Config = toml::from_str(default_prefill_v1).unwrap();
354+
assert!(cfg_load.sanitize().is_ok());
355+
assert_eq!(cfg_load.prefill_limit.unwrap(), cfg_load.purge_threshold);
356+
357+
let default_prefill_v2 = r#"
358+
enable-log-recycle = true
359+
prefill-for-recycle = false
360+
prefill-limit = "20GB"
361+
"#;
362+
let mut cfg_load: Config = toml::from_str(default_prefill_v2).unwrap();
363+
assert!(cfg_load.sanitize().is_ok());
364+
assert!(cfg_load.prefill_limit.is_none());
365+
}
311366
}

src/file_pipe_log/pipe_builder.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
//! Helper types to recover in-memory states from log files.
44
5-
use std::cmp;
65
use std::fs::{self, File as StdFile};
76
use std::io::Write;
87
use std::marker::PhantomData;
@@ -32,8 +31,8 @@ use super::pipe::{
3231
};
3332
use super::reader::LogItemBatchFileReader;
3433

34+
/// Maximum size for the buffer for prefilling.
3535
const PREFILL_BUFFER_SIZE: usize = ReadableSize::mb(16).0 as usize;
36-
const MAX_PREFILL_SIZE: usize = ReadableSize::gb(12).0 as usize;
3736

3837
/// `ReplayMachine` is a type of deterministic state machine that obeys
3938
/// associative law.
@@ -477,14 +476,12 @@ impl<F: FileSystem> DualPipesBuilder<F> {
477476

478477
fn initialize_files(&mut self) -> Result<()> {
479478
let target_file_size = self.cfg.target_file_size.0 as usize;
480-
let mut target = if self.cfg.prefill_for_recycle {
479+
let mut target = std::cmp::min(
480+
self.cfg.prefill_capacity(),
481481
self.cfg
482482
.recycle_capacity()
483-
.saturating_sub(self.append_files.len())
484-
} else {
485-
0
486-
};
487-
target = cmp::min(target, MAX_PREFILL_SIZE / target_file_size);
483+
.saturating_sub(self.append_files.len()),
484+
);
488485
let to_create = target.saturating_sub(self.recycled_files.len());
489486
if to_create > 0 {
490487
let now = Instant::now();

0 commit comments

Comments
 (0)