From 049cc6c10aed73bafabf2d6bbb9e560838c3fb96 Mon Sep 17 00:00:00 2001 From: MohitGoyal09 Date: Wed, 18 Nov 2026 23:16:00 +0530 Subject: [PATCH 1/7] fix(aof): chunk grown string values into SET and APPEND Write strings above the replay budget as a SET plus APPEND chunks so values past the 256 MiB protocol cap replay, and cover a 260 MiB grown string across a restart. --- CHANGELOG.md | 2 ++ docs/COMPATIBILITY.md | 5 +++-- src/aof.rs | 38 +++++++++++++++++++++++++++++++++++++- tests/aof.rs | 25 +++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5619ab0..3fbe947 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -83,6 +83,8 @@ Because ReRu is pre-1.0, minor versions may include breaking changes. time up to the next second, and `HEXPIRETIME` rounds the absolute second up. A legacy AOF with a larger stored deadline is clamped during replay so the server still starts. +- AOF string values larger than the replay budget are written as a `SET` plus + `APPEND` chunks, so a string grown past the protocol bulk limit replays. - `PUBSUB NUMPAT` reads a reference-counted pattern map instead of rebuilding a set of every listener's patterns on each call. - `BZPOPMIN`, `BZPOPMAX`, and `BZMPOP` stay blocked when a watched key only diff --git a/docs/COMPATIBILITY.md b/docs/COMPATIBILITY.md index 9da2c45..170f4bf 100644 --- a/docs/COMPATIBILITY.md +++ b/docs/COMPATIBILITY.md @@ -159,8 +159,9 @@ returns to `listpack` when the last deadline is cleared, deleted, or expires. keys. `OBJECT FREQ`, `OBJECT IDLETIME`, and `OBJECT HELP` are not implemented. AOF replay rewrites one key per batch. Collection keys are split by argument -count and a 32 MiB payload budget. String values are written as one SET, so a -string grown past the 256 MiB command cap by APPEND or SETRANGE cannot replay. +count and a 32 MiB payload budget. String values larger than that budget are +written as a SET followed by APPEND chunks, so a string grown past the 256 MiB +command cap by APPEND or SETRANGE still replays. A dataset larger than 512 MiB cannot be snapshotted and returns MISCONF. A failed AOF write leaves the in-memory value in place, returns `-MISCONF`, and keeps the connection open. Snapshot key counts are bounded by the payload size instead of a fixed 1,000,000-key cap. diff --git a/src/aof.rs b/src/aof.rs index f9f51de..0bfc19b 100644 --- a/src/aof.rs +++ b/src/aof.rs @@ -221,7 +221,19 @@ where fn append_value(output: &mut Vec, key: &[u8], entry: &Entry) -> io::Result<()> { match &entry.value { - Value::String(value) => command(output, &[b"SET", key, value]), + Value::String(value) => { + if value.len() <= MAX_REPLAY_COMMAND_BYTES { + command(output, &[b"SET", key, value]); + } else { + // A grown string can exceed the protocol bulk limit; emit it in + // replay-sized chunks so the AOF still loads. + let (first, rest) = value.split_at(MAX_REPLAY_COMMAND_BYTES); + command(output, &[b"SET", key, first]); + for chunk in rest.chunks(MAX_REPLAY_COMMAND_BYTES) { + command(output, &[b"APPEND", key, chunk]); + } + } + } Value::List(items) => { chunk_items(items.iter().map(Vec::as_slice), |chunk| { let mut args: Vec<&[u8]> = vec![b"RPUSH", key]; @@ -568,6 +580,30 @@ mod tests { // One 72 MiB command would exceed the 64 MiB replay limit. assert_eq!(commands, 3); assert_eq!(members, 8); + + // Strings above the replay budget become SET plus APPEND chunks. + let entry = Entry { + value: Value::String(vec![b'y'; 70 * 1024 * 1024]), + expires_at: None, + }; + let mut output = Vec::new(); + append_value(&mut output, b"grown", &entry).unwrap(); + let mut cursor = std::io::Cursor::new(output.as_slice()); + let mut commands = 0; + let mut total = 0; + while let Some(command) = protocol::read_command(&mut cursor).unwrap() { + let bytes: usize = command.iter().map(Vec::len).sum(); + assert!(bytes <= MAX_REPLAY_COMMAND_BYTES + 1024, "{bytes} bytes"); + if commands == 0 { + assert_eq!(command[0], b"SET"); + } else { + assert_eq!(command[0], b"APPEND"); + } + total += command[2].len(); + commands += 1; + } + assert_eq!(commands, 3); + assert_eq!(total, 70 * 1024 * 1024); } static TEST_SEQUENCE: AtomicU64 = AtomicU64::new(0); diff --git a/tests/aof.rs b/tests/aof.rs index 69f260e..f4445ee 100644 --- a/tests/aof.rs +++ b/tests/aof.rs @@ -261,3 +261,28 @@ fn aof_clamps_legacy_out_of_range_field_deadlines() { } let _ = std::fs::remove_file(path); } + +#[test] +fn grown_strings_above_the_bulk_limit_survive_restart() { + let path = path(); + let filename = path.to_str().unwrap(); + let chunk = vec![b'x'; 130 * 1024 * 1024]; + { + let server = Server::start_with(&["--appendonly", filename]); + let mut client = Client::new(&server); + assert_eq!( + client.call(&[b"APPEND", b"gk", &chunk]), + integer(130 * 1024 * 1024) + ); + assert_eq!( + client.call(&[b"APPEND", b"gk", &chunk]), + integer(260 * 1024 * 1024) + ); + } + { + let server = Server::start_with(&["--appendonly", filename]); + let mut client = Client::new(&server); + assert_eq!(client.call(&[b"STRLEN", b"gk"]), integer(260 * 1024 * 1024)); + } + let _ = std::fs::remove_file(path); +} From 5cc8f73a4e85474e8d48ef61f23616c9ed5aef4d Mon Sep 17 00:00:00 2001 From: MohitGoyal09 Date: Wed, 18 Nov 2026 23:18:49 +0530 Subject: [PATCH 2/7] test(aof): allow large AOF fsyncs on slow runners --- tests/aof.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/aof.rs b/tests/aof.rs index f4445ee..3c24108 100644 --- a/tests/aof.rs +++ b/tests/aof.rs @@ -269,7 +269,7 @@ fn grown_strings_above_the_bulk_limit_survive_restart() { let chunk = vec![b'x'; 130 * 1024 * 1024]; { let server = Server::start_with(&["--appendonly", filename]); - let mut client = Client::new(&server); + let mut client = Client::with_read_timeout(&server, std::time::Duration::from_secs(30)); assert_eq!( client.call(&[b"APPEND", b"gk", &chunk]), integer(130 * 1024 * 1024) @@ -281,7 +281,7 @@ fn grown_strings_above_the_bulk_limit_survive_restart() { } { let server = Server::start_with(&["--appendonly", filename]); - let mut client = Client::new(&server); + let mut client = Client::with_read_timeout(&server, std::time::Duration::from_secs(30)); assert_eq!(client.call(&[b"STRLEN", b"gk"]), integer(260 * 1024 * 1024)); } let _ = std::fs::remove_file(path); From 6d77ca0f530547d5c7a9cdf3367c39d86a88cdff Mon Sep 17 00:00:00 2001 From: MohitGoyal09 Date: Wed, 18 Nov 2026 23:18:51 +0530 Subject: [PATCH 3/7] ci: retrigger From 3d53ec0a7ec84d7bb3878104bc9d27db39e14a3c Mon Sep 17 00:00:00 2001 From: MohitGoyal09 Date: Wed, 18 Nov 2026 23:28:30 +0530 Subject: [PATCH 4/7] test(aof): assert chunk seams and the 32 MiB boundary --- docs/COMPATIBILITY.md | 4 +++- src/aof.rs | 19 +++++++++++++++++++ tests/aof.rs | 14 +++++++++++--- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/docs/COMPATIBILITY.md b/docs/COMPATIBILITY.md index 170f4bf..50c54e8 100644 --- a/docs/COMPATIBILITY.md +++ b/docs/COMPATIBILITY.md @@ -161,7 +161,9 @@ keys. `OBJECT FREQ`, `OBJECT IDLETIME`, and `OBJECT HELP` are not implemented. AOF replay rewrites one key per batch. Collection keys are split by argument count and a 32 MiB payload budget. String values larger than that budget are written as a SET followed by APPEND chunks, so a string grown past the 256 MiB -command cap by APPEND or SETRANGE still replays. +command cap by APPEND still replays (SETRANGE stops at the 64 MiB cap). A file +written by an older build that stored such a value as one SET still cannot +replay, and a large string is re-encoded on every write until BGREWRITEAOF. A dataset larger than 512 MiB cannot be snapshotted and returns MISCONF. A failed AOF write leaves the in-memory value in place, returns `-MISCONF`, and keeps the connection open. Snapshot key counts are bounded by the payload size instead of a fixed 1,000,000-key cap. diff --git a/src/aof.rs b/src/aof.rs index 0bfc19b..ea898c7 100644 --- a/src/aof.rs +++ b/src/aof.rs @@ -604,6 +604,25 @@ mod tests { } assert_eq!(commands, 3); assert_eq!(total, 70 * 1024 * 1024); + + // Exactly one budget stays a single SET; one extra byte adds an APPEND. + for (size, expected) in [ + (MAX_REPLAY_COMMAND_BYTES, 1), + (MAX_REPLAY_COMMAND_BYTES + 1, 2), + ] { + let entry = Entry { + value: Value::String(vec![b'z'; size]), + expires_at: None, + }; + let mut output = Vec::new(); + append_value(&mut output, b"edge", &entry).unwrap(); + let mut cursor = std::io::Cursor::new(output.as_slice()); + let mut commands = 0; + while let Some(_command) = protocol::read_command(&mut cursor).unwrap() { + commands += 1; + } + assert_eq!(commands, expected, "{size} bytes"); + } } static TEST_SEQUENCE: AtomicU64 = AtomicU64::new(0); diff --git a/tests/aof.rs b/tests/aof.rs index 3c24108..2fcaba4 100644 --- a/tests/aof.rs +++ b/tests/aof.rs @@ -266,16 +266,17 @@ fn aof_clamps_legacy_out_of_range_field_deadlines() { fn grown_strings_above_the_bulk_limit_survive_restart() { let path = path(); let filename = path.to_str().unwrap(); - let chunk = vec![b'x'; 130 * 1024 * 1024]; + let first = vec![b'x'; 130 * 1024 * 1024]; + let second = vec![b'y'; 130 * 1024 * 1024]; { let server = Server::start_with(&["--appendonly", filename]); let mut client = Client::with_read_timeout(&server, std::time::Duration::from_secs(30)); assert_eq!( - client.call(&[b"APPEND", b"gk", &chunk]), + client.call(&[b"APPEND", b"gk", &first]), integer(130 * 1024 * 1024) ); assert_eq!( - client.call(&[b"APPEND", b"gk", &chunk]), + client.call(&[b"APPEND", b"gk", &second]), integer(260 * 1024 * 1024) ); } @@ -283,6 +284,13 @@ fn grown_strings_above_the_bulk_limit_survive_restart() { let server = Server::start_with(&["--appendonly", filename]); let mut client = Client::with_read_timeout(&server, std::time::Duration::from_secs(30)); assert_eq!(client.call(&[b"STRLEN", b"gk"]), integer(260 * 1024 * 1024)); + // Bytes and chunk order survive the split image. + assert_eq!( + client.call(&[b"GETRANGE", b"gk", b"136314878", b"136314881"]), + bulk(b"xxyy") + ); + assert_eq!(client.call(&[b"GETRANGE", b"gk", b"0", b"0"]), bulk(b"x")); + assert_eq!(client.call(&[b"GETRANGE", b"gk", b"-1", b"-1"]), bulk(b"y")); } let _ = std::fs::remove_file(path); } From 1e0c784cfbb92391492db10736797d3a61e30c91 Mon Sep 17 00:00:00 2001 From: MohitGoyal09 Date: Wed, 18 Nov 2026 23:28:32 +0530 Subject: [PATCH 5/7] ci: retrigger From b10397b0ba1082ffd1817fe25b5e62a90a2b87e9 Mon Sep 17 00:00:00 2001 From: MohitGoyal09 Date: Wed, 18 Nov 2026 23:38:05 +0530 Subject: [PATCH 6/7] ci: retrigger From b15882a77ad596e2e51a24194e74e3faff453bdc Mon Sep 17 00:00:00 2001 From: MohitGoyal09 Date: Wed, 18 Nov 2026 23:40:42 +0530 Subject: [PATCH 7/7] test(aof): reassemble distinct bytes from the chunked image --- src/aof.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/aof.rs b/src/aof.rs index ea898c7..b053fcd 100644 --- a/src/aof.rs +++ b/src/aof.rs @@ -581,16 +581,20 @@ mod tests { assert_eq!(commands, 3); assert_eq!(members, 8); - // Strings above the replay budget become SET plus APPEND chunks. + // Strings above the replay budget become SET plus APPEND chunks. Use + // position-distinct bytes so a reordered chunk is visible. + let value: Vec = (0..70 * 1024 * 1024) + .map(|index| (index % 251) as u8) + .collect(); let entry = Entry { - value: Value::String(vec![b'y'; 70 * 1024 * 1024]), + value: Value::String(value.clone()), expires_at: None, }; let mut output = Vec::new(); append_value(&mut output, b"grown", &entry).unwrap(); let mut cursor = std::io::Cursor::new(output.as_slice()); let mut commands = 0; - let mut total = 0; + let mut reassembled = Vec::with_capacity(value.len()); while let Some(command) = protocol::read_command(&mut cursor).unwrap() { let bytes: usize = command.iter().map(Vec::len).sum(); assert!(bytes <= MAX_REPLAY_COMMAND_BYTES + 1024, "{bytes} bytes"); @@ -599,11 +603,11 @@ mod tests { } else { assert_eq!(command[0], b"APPEND"); } - total += command[2].len(); + reassembled.extend_from_slice(&command[2]); commands += 1; } assert_eq!(commands, 3); - assert_eq!(total, 70 * 1024 * 1024); + assert_eq!(reassembled, value); // Exactly one budget stays a single SET; one extra byte adds an APPEND. for (size, expected) in [