1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
use log::warn;
use serde::{Deserialize, Serialize};
use crate::pipe_log::Version;
use crate::{util::ReadableSize, Result};
const MIN_RECOVERY_READ_BLOCK_SIZE: usize = 512;
const MIN_RECOVERY_THREADS: usize = 1;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum RecoveryMode {
AbsoluteConsistency,
#[serde(
alias = "tolerate-corrupted-tail-records",
rename(serialize = "tolerate-corrupted-tail-records")
)]
TolerateTailCorruption,
TolerateAnyCorruption,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[serde(default)]
#[serde(rename_all = "kebab-case")]
pub struct Config {
pub dir: String,
pub recovery_mode: RecoveryMode,
pub recovery_read_block_size: ReadableSize,
pub recovery_threads: usize,
pub batch_compression_threshold: ReadableSize,
pub bytes_per_sync: Option<ReadableSize>,
pub format_version: Version,
pub target_file_size: ReadableSize,
pub purge_threshold: ReadableSize,
pub purge_rewrite_threshold: Option<ReadableSize>,
pub purge_rewrite_garbage_ratio: f64,
pub memory_limit: Option<ReadableSize>,
pub enable_log_recycle: bool,
}
impl Default for Config {
fn default() -> Config {
#[allow(unused_mut)]
let mut cfg = Config {
dir: "".to_owned(),
recovery_mode: RecoveryMode::TolerateTailCorruption,
recovery_read_block_size: ReadableSize::kb(16),
recovery_threads: 4,
batch_compression_threshold: ReadableSize::kb(8),
bytes_per_sync: None,
format_version: Version::V2,
target_file_size: ReadableSize::mb(128),
purge_threshold: ReadableSize::gb(10),
purge_rewrite_threshold: None,
purge_rewrite_garbage_ratio: 0.6,
memory_limit: None,
enable_log_recycle: true,
};
#[cfg(test)]
{
cfg.memory_limit = Some(ReadableSize(0));
cfg.enable_log_recycle = true;
}
cfg
}
}
impl Config {
pub fn sanitize(&mut self) -> Result<()> {
if self.purge_threshold.0 < self.target_file_size.0 {
return Err(box_err!("purge-threshold < target-file-size"));
}
if self.purge_rewrite_threshold.is_none() {
self.purge_rewrite_threshold = Some(ReadableSize(std::cmp::max(
self.purge_threshold.0 / 10,
self.target_file_size.0,
)));
}
if self.bytes_per_sync.is_some() {
warn!("bytes-per-sync has been deprecated.");
}
let min_recovery_read_block_size = ReadableSize(MIN_RECOVERY_READ_BLOCK_SIZE as u64);
if self.recovery_read_block_size < min_recovery_read_block_size {
warn!(
"recovery-read-block-size ({}) is too small, setting it to {}",
self.recovery_read_block_size, min_recovery_read_block_size
);
self.recovery_read_block_size = min_recovery_read_block_size;
}
if self.recovery_threads < MIN_RECOVERY_THREADS {
warn!(
"recovery-threads ({}) is too small, setting it to {}",
self.recovery_threads, MIN_RECOVERY_THREADS
);
self.recovery_threads = MIN_RECOVERY_THREADS;
}
if self.enable_log_recycle && !self.format_version.has_log_signing() {
return Err(box_err!(
"format version {} doesn't support log recycle, use 2 or above",
self.format_version
));
}
#[cfg(not(feature = "swap"))]
if self.memory_limit.is_some() {
warn!("memory-limit will be ignored because swap feature is disabled");
}
Ok(())
}
pub(crate) fn recycle_capacity(&self) -> usize {
if !self.format_version.has_log_signing() {
return 0;
}
if self.enable_log_recycle && self.purge_threshold.0 >= self.target_file_size.0 {
std::cmp::min(
(self.purge_threshold.0 / self.target_file_size.0) as usize,
u32::MAX as usize,
)
} else {
0
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_serde() {
let value = Config::default();
let dump = toml::to_string_pretty(&value).unwrap();
let load = toml::from_str(&dump).unwrap();
assert_eq!(value, load);
}
#[test]
fn test_custom() {
let custom = r#"
dir = "custom_dir"
recovery-mode = "tolerate-tail-corruption"
bytes-per-sync = "2KB"
target-file-size = "1MB"
purge-threshold = "3MB"
format-version = 1
enable-log-recycle = false
"#;
let mut load: Config = toml::from_str(custom).unwrap();
assert_eq!(load.dir, "custom_dir");
assert_eq!(load.recovery_mode, RecoveryMode::TolerateTailCorruption);
assert_eq!(load.bytes_per_sync, Some(ReadableSize::kb(2)));
assert_eq!(load.target_file_size, ReadableSize::mb(1));
assert_eq!(load.purge_threshold, ReadableSize::mb(3));
assert_eq!(load.format_version, Version::V1);
load.sanitize().unwrap();
}
#[test]
fn test_invalid() {
let hard_error = r#"
target-file-size = "5MB"
purge-threshold = "3MB"
"#;
let mut hard_load: Config = toml::from_str(hard_error).unwrap();
assert!(hard_load.sanitize().is_err());
let soft_error = r#"
recovery-read-block-size = "1KB"
recovery-threads = 0
target-file-size = "5000MB"
format-version = 2
enable-log-recycle = true
"#;
let soft_load: Config = toml::from_str(soft_error).unwrap();
let mut soft_sanitized = soft_load;
soft_sanitized.sanitize().unwrap();
assert!(soft_sanitized.recovery_read_block_size.0 >= MIN_RECOVERY_READ_BLOCK_SIZE as u64);
assert!(soft_sanitized.recovery_threads >= MIN_RECOVERY_THREADS);
assert_eq!(
soft_sanitized.purge_rewrite_threshold.unwrap(),
soft_sanitized.target_file_size
);
assert_eq!(soft_sanitized.format_version, Version::V2);
assert!(soft_sanitized.enable_log_recycle);
let recycle_error = r#"
enable-log-recycle = true
format-version = 1
"#;
let mut cfg_load: Config = toml::from_str(recycle_error).unwrap();
assert!(cfg_load.sanitize().is_err());
}
#[test]
fn test_backward_compactibility() {
let old = r#"
recovery-mode = "tolerate-corrupted-tail-records"
"#;
let mut load: Config = toml::from_str(old).unwrap();
load.sanitize().unwrap();
assert!(toml::to_string(&load)
.unwrap()
.contains("tolerate-corrupted-tail-records"));
}
}