kevy_persist/aof.rs
1//! Append-only command log. Split out from `lib.rs` to keep that file
2//! under the 500-LOC house rule; the snapshot writer/reader stays there.
3
4use std::fs::{File, OpenOptions};
5use std::io::{self, BufWriter, Seek, SeekFrom, Write};
6use std::path::{Path, PathBuf};
7use std::time::{Duration, Instant};
8
9use kevy_resp::ArgvView;
10use kevy_store::Store;
11
12use crate::{
13 dump_store_to_aof, estimate_multibulk_bytes, write_multibulk,
14};
15
16/// 9-byte file-format header written at the start of every kevy-managed
17/// AOF as of v1.2.0. `replay_aof` strips it before parsing RESP, so
18/// non-kevy bytes accidentally written into the AOF path (e.g. a deploy
19/// pipeline redirecting shell stderr into the file) get the same loud
20/// rejection as any other corrupt frame. Pre-1.2 AOFs (no magic) still
21/// replay — the parser only consumes the magic if it sees it.
22pub(crate) const AOF_MAGIC: &[u8; 9] = b"KEVYAOF1\n";
23
24/// When to fsync the AOF to disk.
25#[derive(Debug, Clone, Copy, PartialEq, Eq)]
26pub enum Fsync {
27 /// fsync after every write — safest, slowest.
28 Always,
29 /// fsync at most once per second (call [`Aof::maybe_sync`] periodically).
30 EverySec,
31 /// Never fsync explicitly; leave it to the OS.
32 No,
33}
34
35/// An append-only command log. Each write command is appended as a RESP
36/// multi-bulk frame; [`crate::replay_aof`] re-applies them on startup.
37///
38/// Durability model (paired with snapshots): a snapshot taken at T0 plus
39/// the AOF of writes in (T0, now] reconstructs the current state. `SAVE`
40/// writes the snapshot then [`Aof::truncate`]s the log, so replay never
41/// double-applies.
42///
43/// Sizes (`size_bytes`, `size_at_last_rewrite`) drive auto-trigger of
44/// [`Aof::rewrite_from`] (BGREWRITEAOF) via the
45/// `auto_aof_rewrite_percentage` + `auto_aof_rewrite_min_size` knobs in
46/// `kevy_config`.
47pub struct Aof {
48 file: BufWriter<File>,
49 path: PathBuf,
50 fsync: Fsync,
51 dirty: bool,
52 last_sync: Instant,
53 /// Estimated bytes currently in the AOF file (existing + appended since
54 /// open). Maintained without fstat() syscalls per append.
55 size_bytes: u64,
56 /// File size right after the most recent [`Self::rewrite_from`] (or
57 /// `Self::open` if never rewritten). Anchor for `auto_aof_rewrite_*`.
58 size_at_last_rewrite: u64,
59 /// Total rewrites successfully completed since open. Surfaced via INFO.
60 rewrites_total: u64,
61}
62
63/// Result of an [`Aof::rewrite_from`] call. Surfaced by `BGREWRITEAOF` /
64/// `INFO persistence`.
65#[derive(Debug, Clone, Copy)]
66pub struct RewriteStats {
67 /// Keys dumped into the new AOF.
68 pub keys: u64,
69 /// New AOF size in bytes.
70 pub bytes: u64,
71}
72
73impl Aof {
74 /// Open (creating if needed) `path` for appending. New files get the
75 /// 9-byte [`AOF_MAGIC`] header so replays can identify the file as
76 /// kevy-managed. Pre-existing files (legacy bare-RESP or already-
77 /// magic'd) are left untouched.
78 pub fn open(path: &Path, fsync: Fsync) -> io::Result<Self> {
79 let mut file = OpenOptions::new().create(true).append(true).open(path)?;
80 let mut size = file.metadata().map(|m| m.len()).unwrap_or(0);
81 if size == 0 {
82 // Fresh file: stamp the magic header so the replayer can
83 // distinguish kevy-written AOFs from accidental writes.
84 file.write_all(AOF_MAGIC)?;
85 file.sync_data()?;
86 size = AOF_MAGIC.len() as u64;
87 }
88 Ok(Aof {
89 file: BufWriter::new(file),
90 path: path.to_path_buf(),
91 fsync,
92 dirty: false,
93 last_sync: Instant::now(),
94 size_bytes: size,
95 size_at_last_rewrite: size,
96 rewrites_total: 0,
97 })
98 }
99
100 /// The fsync policy this AOF was opened with (or last switched to).
101 /// Mostly for tests / INFO output; the hot path doesn't read this.
102 #[inline]
103 pub fn fsync_policy(&self) -> Fsync {
104 self.fsync
105 }
106
107 /// Switch the fsync policy at runtime (called by `CONFIG SET
108 /// appendfsync`). When tightening to `Always`, also flushes + fsyncs
109 /// any bytes still in the BufWriter so the new "every write is on
110 /// disk before reply" contract is honoured starting on the next
111 /// append, not after the dirty backlog clears.
112 pub fn set_fsync(&mut self, fsync: Fsync) -> io::Result<()> {
113 let upgrading_to_always = matches!(fsync, Fsync::Always) && !matches!(self.fsync, Fsync::Always);
114 self.fsync = fsync;
115 if upgrading_to_always && self.dirty {
116 self.file.flush()?;
117 self.file.get_ref().sync_data()?;
118 self.dirty = false;
119 self.last_sync = Instant::now();
120 }
121 Ok(())
122 }
123
124 /// Append one command, applying the fsync policy.
125 pub fn append<A: ArgvView + ?Sized>(&mut self, args: &A) -> io::Result<()> {
126 write_multibulk(&mut self.file, args)?;
127 self.size_bytes = self
128 .size_bytes
129 .saturating_add(estimate_multibulk_bytes(args));
130 match self.fsync {
131 Fsync::Always => {
132 self.file.flush()?;
133 self.file.get_ref().sync_data()?;
134 }
135 Fsync::EverySec | Fsync::No => self.dirty = true,
136 }
137 Ok(())
138 }
139
140 /// Flush+fsync if the `EverySec` window has elapsed. Call once per loop tick.
141 pub fn maybe_sync(&mut self) -> io::Result<()> {
142 if matches!(self.fsync, Fsync::EverySec)
143 && self.dirty
144 && self.last_sync.elapsed() >= Duration::from_secs(1)
145 {
146 self.file.flush()?;
147 self.file.get_ref().sync_data()?;
148 self.dirty = false;
149 self.last_sync = Instant::now();
150 }
151 Ok(())
152 }
153
154 /// Empty the log (after a snapshot has captured the full state). The
155 /// post-truncate file keeps the [`AOF_MAGIC`] header so replays of
156 /// the freshly-trimmed log still identify as kevy-managed.
157 pub fn truncate(&mut self) -> io::Result<()> {
158 self.file.flush()?;
159 let f = self.file.get_mut();
160 f.set_len(0)?;
161 f.seek(SeekFrom::Start(0))?; // harmless under O_APPEND; keeps len/pos coherent
162 f.write_all(AOF_MAGIC)?;
163 f.sync_all()?;
164 self.dirty = false;
165 self.size_bytes = AOF_MAGIC.len() as u64;
166 self.size_at_last_rewrite = AOF_MAGIC.len() as u64;
167 Ok(())
168 }
169
170 /// Estimated current AOF size in bytes (file content as of last append).
171 #[inline]
172 pub fn size_bytes(&self) -> u64 {
173 self.size_bytes
174 }
175
176 /// AOF size at the most recent rewrite (or open). Auto-trigger compares
177 /// `(size_bytes - size_at_last_rewrite) * 100 / size_at_last_rewrite` to
178 /// the `auto_aof_rewrite_percentage` knob.
179 #[inline]
180 pub fn size_at_last_rewrite(&self) -> u64 {
181 self.size_at_last_rewrite
182 }
183
184 /// Successful rewrite count since `Self::open`. Surfaced in INFO.
185 #[inline]
186 pub fn rewrites_total(&self) -> u64 {
187 self.rewrites_total
188 }
189
190 /// BGREWRITEAOF: rebuild a compact AOF from `store`'s current state and
191 /// atomically swap it in.
192 ///
193 /// **v1.0 is synchronous** — the calling shard blocks for the rewrite's
194 /// duration. Each shard owns its own AOF, so the shards' rewrites
195 /// proceed independently; per-shard blocking matches Redis's `BGSAVE`
196 /// cost in a typical single-key-per-shard workload. Concurrent
197 /// (rewrite-during-writes) incrementalisation is a v1.x perf item.
198 ///
199 /// Writes to a `<path>.rewrite` temp file with fsync, then `rename(2)`s
200 /// it over the live AOF. The append handle is reopened against the new
201 /// file before this call returns, so subsequent `append` calls land in
202 /// the rewritten log.
203 pub fn rewrite_from(&mut self, store: &Store) -> io::Result<RewriteStats> {
204 // Flush any pending writes to the OLD file first so the snapshot
205 // accounts for everything the caller intended to durabilise.
206 self.file.flush()?;
207
208 let tmp = rewrite_tmp_path(&self.path);
209 let (keys, bytes) = dump_store_to_aof(&tmp, store)?;
210
211 // Atomic replacement. After this, the OLD file descriptor in
212 // `self.file` is open against an unlinked inode; new writes would
213 // go nowhere visible. Reopen against the new path.
214 std::fs::rename(&tmp, &self.path)?;
215 let f = OpenOptions::new().append(true).open(&self.path)?;
216 self.file = BufWriter::new(f);
217 self.size_bytes = bytes;
218 self.size_at_last_rewrite = bytes;
219 self.dirty = false;
220 self.rewrites_total = self.rewrites_total.saturating_add(1);
221 Ok(RewriteStats { keys, bytes })
222 }
223}
224
225/// `<aof>.rewrite` — same-directory temp path so `rename(2)` stays atomic.
226fn rewrite_tmp_path(path: &Path) -> PathBuf {
227 let mut p = path.to_path_buf();
228 let new_name = match path.file_name() {
229 Some(n) => {
230 let mut s = n.to_os_string();
231 s.push(".rewrite");
232 s
233 }
234 None => std::ffi::OsString::from("aof.rewrite"),
235 };
236 p.set_file_name(new_name);
237 p
238}