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..50c54e8 100644 --- a/docs/COMPATIBILITY.md +++ b/docs/COMPATIBILITY.md @@ -159,8 +159,11 @@ 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 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 f9f51de..b053fcd 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,53 @@ 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. 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(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 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"); + if commands == 0 { + assert_eq!(command[0], b"SET"); + } else { + assert_eq!(command[0], b"APPEND"); + } + reassembled.extend_from_slice(&command[2]); + commands += 1; + } + assert_eq!(commands, 3); + assert_eq!(reassembled, value); + + // 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 69f260e..2fcaba4 100644 --- a/tests/aof.rs +++ b/tests/aof.rs @@ -261,3 +261,36 @@ 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 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", &first]), + integer(130 * 1024 * 1024) + ); + assert_eq!( + client.call(&[b"APPEND", b"gk", &second]), + integer(260 * 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"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); +}