11// Copyright (c) 2017-present, PingCAP, Inc. Licensed under Apache-2.0.
22
3- use log:: warn;
3+ use log:: { info , warn} ;
44use serde:: { Deserialize , Serialize } ;
55
66use 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}
0 commit comments