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
//! The choices a caller makes when opening and replaying a log: when to
//! fsync, what to do at a corrupt record, and whether replay reports on
//! stderr.
/// When to fsync the AOF to disk. The three values of Redis's
/// `appendfsync`.
///
/// ```
/// use kevy_persist::Fsync;
///
/// assert_eq!(Fsync::parse("EverySec"), Some(Fsync::EverySec));
/// assert_eq!(Fsync::Always.as_str(), "always");
/// assert_eq!(Fsync::default(), Fsync::EverySec);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[non_exhaustive]
pub enum Fsync {
/// fsync after every write — safest, slowest.
///
/// ```
/// use kevy_persist::{Aof, Argv, Fsync};
///
/// let path = std::env::temp_dir().join(format!("fsync-always-doc-{}.aof", std::process::id()));
/// let mut aof = Aof::open(&path, Fsync::Always)?;
/// aof.append(&Argv::from(vec![b"SET".to_vec(), b"k".to_vec(), b"v".to_vec()]))?;
/// // on disk before append returned: no tick, no drop needed
/// assert_eq!(std::fs::metadata(&path)?.len(), aof.size_bytes());
/// # drop(aof);
/// # std::fs::remove_file(&path)?;
/// # Ok::<(), std::io::Error>(())
/// ```
Always,
/// fsync about once per second; each [`crate::Aof::tick`] (or
/// [`crate::Aof::maybe_sync`]) writes the buffer into the kernel. A
/// power loss loses about one second plus the time one fsync takes.
/// The default.
///
/// ```
/// use kevy_persist::{Aof, Fsync};
///
/// let path = std::env::temp_dir().join(format!("fsync-everysec-doc-{}.aof", std::process::id()));
/// let aof = Aof::open(&path, Fsync::default())?;
/// assert_eq!(aof.fsync_policy(), Fsync::EverySec);
/// assert_eq!(Fsync::EverySec.as_str(), "everysec");
/// # drop(aof);
/// # std::fs::remove_file(&path)?;
/// # Ok::<(), std::io::Error>(())
/// ```
#[default]
EverySec,
/// Never fsync explicitly: each [`crate::Aof::tick`] writes the buffer
/// into the kernel, and the OS decides when it reaches the disk.
///
/// ```
/// use kevy_persist::{Aof, Fsync};
///
/// let path = std::env::temp_dir().join(format!("fsync-no-doc-{}.aof", std::process::id()));
/// let mut aof = Aof::open(&path, Fsync::Always)?;
/// aof.set_fsync(Fsync::parse("no").unwrap_or_default())?; // CONFIG SET appendfsync no
/// assert_eq!(aof.fsync_policy(), Fsync::No);
/// # drop(aof);
/// # std::fs::remove_file(&path)?;
/// # Ok::<(), std::io::Error>(())
/// ```
No,
}
impl Fsync {
/// The Redis spelling (`always` / `everysec` / `no`), as `CONFIG GET
/// appendfsync` reports it.
///
/// ```
/// assert_eq!(kevy_persist::Fsync::No.as_str(), "no");
/// ```
pub fn as_str(&self) -> &'static str {
match self {
Self::Always => "always",
Self::EverySec => "everysec",
Self::No => "no",
}
}
/// The inverse of [`Self::as_str`], ignoring ASCII case. `None` for
/// any other input.
///
/// ```
/// assert_eq!(kevy_persist::Fsync::parse("ALWAYS"), Some(kevy_persist::Fsync::Always));
/// assert_eq!(kevy_persist::Fsync::parse("sometimes"), None);
/// ```
pub fn parse(s: &str) -> Option<Self> {
[Self::Always, Self::EverySec, Self::No]
.into_iter()
.find(|f| f.as_str().eq_ignore_ascii_case(s))
}
}
/// What a replay (and the open that follows it) does at a corrupt record
/// in the middle of a log.
///
/// ```
/// use kevy_persist::ReplayMode;
///
/// assert_eq!(ReplayMode::default(), ReplayMode::Strict);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[non_exhaustive]
pub enum ReplayMode {
/// Stop at the first corrupt record; the open quarantines and
/// truncates everything from there.
///
/// ```
/// use kevy_persist::{Aof, Argv, Fsync, ReplayMode, ReplaySummary, replay_aof_in_place};
///
/// let path = std::env::temp_dir().join(format!("mode-strict-doc-{}.aof", std::process::id()));
/// let mut aof = Aof::open(&path, Fsync::Always)?;
/// aof.append(&Argv::from(vec![b"SET".to_vec(), b"k".to_vec(), b"v".to_vec()]))?;
/// drop(aof);
/// let mut bytes = std::fs::read(&path)?;
/// *bytes.last_mut().unwrap() ^= 1; // flip a payload bit
/// std::fs::write(&path, &bytes)?;
/// let report = replay_aof_in_place(&path, ReplayMode::Strict, ReplaySummary::Quiet, |_| {})?;
/// assert!(report.corrupt);
/// assert_eq!(report.commands, 0);
/// # std::fs::remove_file(&path)?;
/// # Ok::<(), std::io::Error>(())
/// ```
#[default]
Strict,
/// Hop over a corrupt region to the next record whose checksum holds
/// and keep replaying; the open leaves interior corrupt regions in
/// place and quarantines only the bytes after the last recoverable
/// record. v1 logs have no checksums to anchor on and replay strictly.
///
/// ```
/// use kevy_persist::{Aof, Argv, Fsync, ReplayMode, ReplaySummary, replay_aof_in_place};
///
/// let path = std::env::temp_dir().join(format!("mode-resync-doc-{}.aof", std::process::id()));
/// let mut aof = Aof::open(&path, Fsync::Always)?;
/// for key in [b"a", b"b"] {
/// aof.append(&Argv::from(vec![b"SET".to_vec(), key.to_vec(), b"v".to_vec()]))?;
/// }
/// drop(aof);
/// let mut bytes = std::fs::read(&path)?;
/// bytes[kevy_persist::AOF2_MAGIC.len() + 12] ^= 1; // damage the first record only
/// std::fs::write(&path, &bytes)?;
/// let report = replay_aof_in_place(&path, ReplayMode::Resync, ReplaySummary::Quiet, |_| {})?;
/// assert!(report.corrupt);
/// assert_eq!(report.commands, 1, "the record behind the damage still replays");
/// # std::fs::remove_file(&path)?;
/// # Ok::<(), std::io::Error>(())
/// ```
Resync,
}
/// Whether a replay prints its informational summary lines on stderr. The
/// corrupt-record WARN prints either way: it is an incident signal, not
/// information.
///
/// ```
/// use kevy_persist::ReplaySummary;
///
/// assert_eq!(ReplaySummary::default(), ReplaySummary::Print);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[non_exhaustive]
pub enum ReplaySummary {
/// Print the summary lines.
///
/// ```
/// use kevy_persist::{ReplayMode, ReplaySummary, replay_aof_in_place};
///
/// let path = std::env::temp_dir().join(format!("summary-print-doc-{}.aof", std::process::id()));
/// std::fs::write(&path, kevy_persist::AOF2_MAGIC)?;
/// // prints `replayed 0 commands …` on stderr as it returns
/// let report = replay_aof_in_place(&path, ReplayMode::Strict, ReplaySummary::Print, |_| {})?;
/// assert_eq!(report.commands, 0);
/// # std::fs::remove_file(&path)?;
/// # Ok::<(), std::io::Error>(())
/// ```
#[default]
Print,
/// Suppress them — for a host that receives the same numbers through
/// a metric sink, where the line would be a duplicate.
///
/// ```
/// use kevy_persist::{ReplayMode, ReplaySummary, replay_aof_in_place};
///
/// let path = std::env::temp_dir().join(format!("summary-quiet-doc-{}.aof", std::process::id()));
/// std::fs::write(&path, kevy_persist::AOF2_MAGIC)?;
/// // the same numbers, handed back instead of printed
/// let report = replay_aof_in_place(&path, ReplayMode::Strict, ReplaySummary::Quiet, |_| {})?;
/// assert_eq!((report.commands, report.dropped_bytes), (0, 0));
/// # std::fs::remove_file(&path)?;
/// # Ok::<(), std::io::Error>(())
/// ```
Quiet,
}