diff --git a/CHANGELOG.md b/CHANGELOG.md index faf294f..996c099 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -69,6 +69,12 @@ Because ReRu is pre-1.0, minor versions may include breaking changes. - `GETEX` validates option syntax first, then the key and type, and only then the expiry value, so a missing key replies nil and a wrong type replies WRONGTYPE without value errors. +- `XINFO STREAM` argument-shape errors and per-group `NOGROUP` texts match + Redis: `XGROUP`/`XINFO CONSUMERS` name the missing group, `XPENDING` keeps the + combined key/group text, and malformed `XINFO STREAM` shapes report the + subcommand error after the key and type are resolved. `XGROUP SETID` resolves + the group before parsing the ID, and non-UTF-8 key/group names are echoed as + raw bytes. - Hash field deadlines are capped at the 46-bit millisecond range Redis uses: larger `HEXPIRE`/`HPEXPIRE`/`HEXPIREAT`/`HPEXPIREAT`/`HSETEX`/`HGETEX` values report `invalid expire time in '' command`. `HTTL` rounds the remaining diff --git a/docs/COMPATIBILITY.md b/docs/COMPATIBILITY.md index a793487..74d2cff 100644 --- a/docs/COMPATIBILITY.md +++ b/docs/COMPATIBILITY.md @@ -389,13 +389,12 @@ Stream IDs are parsed as C strings: leading whitespace, a leading `+`, leading zeros, and a NUL terminator are accepted, while trailing bytes are not. Stream subcommand arity errors name the command or `parent|subcommand`, and unknown subcommands are reported. A few stream keyword comparisons remain exact rather -than C-string (`XADD`/`XTRIM`/`XGROUP`/`XAUTOCLAIM` options with a NUL suffix), +than C-string (`XADD`/`XTRIM`/`XGROUP`/`XAUTOCLAIM` and `XINFO` options with a +NUL suffix), `XAUTOCLAIM`'s start ID, `XGROUP SETID`, and `XREAD` IDs do not accept `+`/`-`, `XGROUP DESTROY` on a missing key replies 0, `ENTRIESREAD` uses the generic -integer error and its reporting differs from Redis, `XINFO STREAM ... COUNT` -without `FULL` reports a syntax error, an existing key with a missing group uses -the combined NOGROUP text, and some `XTRIM`/`XREAD` arity and syntax texts still -differ. `XAUTOCLAIM` implements bounded scanning +integer error and its reporting differs from Redis, and some `XTRIM`/`XREAD` +arity and syntax texts still differ. `XAUTOCLAIM` implements bounded scanning (10 x COUNT), a COUNT shared by claims and deleted pending entries, JUSTID, the Redis 7 deleted-ID reply, and registers its consumer even when it claims nothing. These provide at-least-once processing primitives, not exactly-once diff --git a/src/stream_groups.rs b/src/stream_groups.rs index a5a23d0..f91cdb2 100644 --- a/src/stream_groups.rs +++ b/src/stream_groups.rs @@ -104,19 +104,39 @@ fn syntax() -> Result { fn wrongtype() -> Vec { b"-WRONGTYPE Operation against a key holding the wrong kind of value\r\n".to_vec() } +fn push_quoted(out: &mut Vec, bytes: &[u8]) { + // Redis formats these names with %s: stop at NUL, keep other bytes raw. + out.extend( + bytes + .iter() + .take_while(|byte| **byte != 0) + .map(|byte| match byte { + b'\r' | b'\n' => b' ', + _ => *byte, + }), + ); +} + fn nogroup(key: &[u8], group: &[u8], read: bool) -> Vec { - let msg = format!( - "No such key '{}' or consumer group '{}'{}", - String::from_utf8_lossy(key), - String::from_utf8_lossy(group), - if read { - " in XREADGROUP with GROUP option" - } else { - "" - } - ) - .replace(['\r', '\n'], " "); - format!("-NOGROUP {msg}\r\n").into_bytes() + let mut reply = b"-NOGROUP No such key '".to_vec(); + push_quoted(&mut reply, key); + reply.extend_from_slice(b"' or consumer group '"); + push_quoted(&mut reply, group); + reply.extend_from_slice(if read { + b"' in XREADGROUP with GROUP option\r\n" + } else { + b"'\r\n" + }); + reply +} + +fn missing_group(key: &[u8], group: &[u8]) -> Vec { + let mut reply = b"-NOGROUP No such consumer group '".to_vec(); + push_quoted(&mut reply, group); + reply.extend_from_slice(b"' for key name '"); + push_quoted(&mut reply, key); + reply.extend_from_slice(b"'\r\n"); + reply } fn stream_mut<'a>(entries: &'a mut HashMap, Entry>, key: &[u8]) -> Result<&'a mut Stream> { remove_expired(entries, key); @@ -133,7 +153,7 @@ fn group_mut<'a>(stream: &'a mut Stream, key: &[u8], name: &[u8]) -> Result<&'a stream .groups .get_mut(name) - .ok_or_else(|| nogroup(key, name, false)) + .ok_or_else(|| missing_group(key, name)) } fn entry_reply(id: StreamId, entry: Option<&StreamEntry>) -> Vec { array(vec![ @@ -172,9 +192,9 @@ pub fn valid_arity(command: &[Vec]) -> Option { b"XPENDING" => n == 3 || n >= 6, b"XCLAIM" | b"XAUTOCLAIM" => n >= 6, b"XINFO" => match command.get(1) { - Some(sub) if xinfo_sub(sub, b"GROUPS") => n == 3, - Some(sub) if xinfo_sub(sub, b"CONSUMERS") => n == 4, - Some(sub) if xinfo_sub(sub, b"STREAM") => n >= 3, + Some(sub) if sub.eq_ignore_ascii_case(b"GROUPS") => n == 3, + Some(sub) if sub.eq_ignore_ascii_case(b"CONSUMERS") => n == 4, + Some(sub) if sub.eq_ignore_ascii_case(b"STREAM") => n >= 3, _ => false, }, b"XSETID" => n >= 3, @@ -186,12 +206,6 @@ pub fn is_mutating(command: &[Vec]) -> bool { && !command[0].eq_ignore_ascii_case(b"XPENDING") && !command[0].eq_ignore_ascii_case(b"XINFO") } -/// XINFO accepts a subcommand with exactly one trailing NUL, as Redis does. -fn xinfo_sub(sub: &[u8], name: &[u8]) -> bool { - let sub = sub.strip_suffix(b"\0").unwrap_or(sub); - sub.eq_ignore_ascii_case(name) -} - fn sub_token(command: &[Vec]) -> Option<&[u8]> { command .get(1) @@ -209,7 +223,9 @@ fn known_subcommand(command: &[Vec]) -> bool { Some(b"CREATE" | b"SETID" | b"DESTROY" | b"CREATECONSUMER" | b"DELCONSUMER") ), b"XINFO" => command.get(1).is_some_and(|sub| { - xinfo_sub(sub, b"STREAM") || xinfo_sub(sub, b"GROUPS") || xinfo_sub(sub, b"CONSUMERS") + sub.eq_ignore_ascii_case(b"STREAM") + || sub.eq_ignore_ascii_case(b"GROUPS") + || sub.eq_ignore_ascii_case(b"CONSUMERS") }), _ => true, } @@ -304,10 +320,15 @@ fn group_command(c: &[Vec], store: &Store) -> Result> { } else { let _ = stream_mut(&mut entries, key)?; } - // Parse the ID only after the key and type are resolved. - let parsed_id = match raw_id { - Some(raw) => Some(id(raw)?), - None => None, + // CREATE validates the ID before creating an empty stream; SETID resolves + // the group before parsing it. + let parsed_id = if create { + match raw_id { + Some(raw) => Some(id(raw)?), + None => None, + } + } else { + None }; if missing { entries.insert( @@ -320,10 +341,20 @@ fn group_command(c: &[Vec], store: &Store) -> Result> { } let stream = stream_mut(&mut entries, key)?; let reply = if create || setid { + if setid && !stream.groups.contains_key(name) { + return Err(missing_group(key, name)); + } + let last = if create { + parsed_id.unwrap_or(stream.last_generated_id) + } else { + match raw_id { + Some(raw) => id(raw)?, + None => stream.last_generated_id, + } + }; if create && stream.groups.contains_key(name) { return Err(b"-BUSYGROUP Consumer Group name already exists\r\n".to_vec()); } - let last = parsed_id.unwrap_or(stream.last_generated_id); if create { stream.groups.insert( name.clone(), @@ -620,7 +651,11 @@ fn pending(c: &[Vec], store: &Store) -> Result> { if !entries.contains_key(&c[1]) { return Err(nogroup(&c[1], &c[2], false)); } - let group = group_mut(stream_mut(&mut entries, &c[1])?, &c[1], &c[2])?; + let stream = stream_mut(&mut entries, &c[1])?; + let group = stream + .groups + .get_mut(&c[2]) + .ok_or_else(|| nogroup(&c[1], &c[2], false))?; if let Some(((start, exclusive_start), (end, exclusive_end), count, consumer, idle)) = range { let now = now_ms(); return Ok(array( @@ -755,12 +790,12 @@ fn estimate_read( fn info(c: &[Vec], store: &Store) -> Result> { let sub = &c[1]; - if xinfo_sub(sub, b"STREAM") { + if sub.eq_ignore_ascii_case(b"STREAM") { return stream_info(c, store); } let mut entries = store.entries.lock().unwrap(); let stream = stream_mut(&mut entries, &c[2])?; - if xinfo_sub(sub, b"GROUPS") { + if sub.eq_ignore_ascii_case(b"GROUPS") { Ok(array( stream .groups @@ -846,16 +881,21 @@ fn group_lag(stream: &Stream, group: &Group) -> Option { } fn stream_info(c: &[Vec], store: &Store) -> Result> { + let mut entries = store.entries.lock().unwrap(); + let stream = stream_mut(&mut entries, &c[2])?; let full = c.get(3).is_some_and(|v| v.eq_ignore_ascii_case(b"FULL")); let count = match c.len() { 3 => 10, 4 if full => 10, 6 if full && c[4].eq_ignore_ascii_case(b"COUNT") => number(&c[5])? as usize, - _ => return syntax(), + _ => { + return Err(protocol::error(&format!( + "unknown subcommand or wrong number of arguments for '{}'. Try XINFO HELP.", + String::from_utf8_lossy(sub_token(c).unwrap_or(b"")) + ))) + } }; let limit = if count == 0 { usize::MAX } else { count }; - let mut entries = store.entries.lock().unwrap(); - let stream = stream_mut(&mut entries, &c[2])?; let mut fields = vec![ protocol::bulk(b"length"), integer(stream.entries.len() as u64), diff --git a/tests/stream_groups.rs b/tests/stream_groups.rs index 3f58e78..02917c9 100644 --- a/tests/stream_groups.rs +++ b/tests/stream_groups.rs @@ -1641,3 +1641,116 @@ fn xclaim_option_names_are_c_strings() { array(vec![]) ); } + +#[test] +fn stream_group_texts_name_the_group_and_xinfo_stream_shapes() { + let server = Server::start(); + let mut c = Client::new(&server); + assert!(matches!( + c.call(&[b"XADD", b"s", b"1-0", b"f", b"v"]), + Reply::Bulk(Some(_)) + )); + assert_eq!(c.call(&[b"XGROUP", b"CREATE", b"s", b"g", b"0"]), ok()); + + let missing_group = + || Reply::Error(b"NOGROUP No such consumer group 'nogrp' for key name 's'".to_vec()); + assert_eq!( + c.call(&[b"XGROUP", b"SETID", b"s", b"nogrp", b"0"]), + missing_group() + ); + assert_eq!( + c.call(&[b"XGROUP", b"CREATECONSUMER", b"s", b"nogrp", b"c"]), + missing_group() + ); + assert_eq!( + c.call(&[b"XGROUP", b"DELCONSUMER", b"s", b"nogrp", b"c"]), + missing_group() + ); + assert_eq!( + c.call(&[b"XINFO", b"CONSUMERS", b"s", b"nogrp"]), + missing_group() + ); + assert_eq!( + c.call(&[b"XPENDING", b"s", b"nogrp"]), + Reply::Error(b"NOGROUP No such key 's' or consumer group 'nogrp'".to_vec()) + ); + + let shape = |token: &str| { + Reply::Error( + format!( + "ERR unknown subcommand or wrong number of arguments for '{token}'. Try XINFO HELP." + ) + .into_bytes(), + ) + }; + assert_eq!( + c.call(&[b"XINFO", b"STREAM", b"s", b"COUNT", b"1"]), + shape("STREAM") + ); + assert_eq!( + c.call(&[b"XINFO", b"stream", b"s", b"count", b"1"]), + shape("stream") + ); + assert_eq!( + c.call(&[b"XINFO", b"STREAM", b"s", b"FULL", b"COUNT", b"1", b"x"]), + shape("STREAM") + ); + assert_eq!( + c.call(&[b"XINFO", b"STREAM", b"s", b"FULL", b"BAD"]), + shape("STREAM") + ); + assert_eq!( + c.call(&[b"XINFO", b"STREAM", b"s", b"COUNT"]), + shape("STREAM") + ); + + // The key and type are checked before the shape. + assert_eq!( + c.call(&[b"XINFO", b"STREAM", b"missing", b"COUNT", b"1"]), + Reply::Error(b"ERR no such key".to_vec()) + ); + assert_eq!( + c.call(&[b"XINFO", b"STREAM", b"missing", b"FULL", b"COUNT", b"abc"]), + Reply::Error(b"ERR no such key".to_vec()) + ); + assert_eq!(c.call(&[b"SET", b"str", b"x"]), ok()); + assert_eq!( + c.call(&[b"XINFO", b"STREAM", b"str", b"FULL", b"BAD"]), + Reply::Error(b"WRONGTYPE Operation against a key holding the wrong kind of value".to_vec()) + ); + + // SETID resolves the group before parsing the ID; CREATE parses first. + assert_eq!( + c.call(&[b"XGROUP", b"SETID", b"s", b"nogrp", b"badid"]), + missing_group() + ); + assert_eq!( + c.call(&[b"XGROUP", b"SETID", b"s", b"g", b"badid"]), + Reply::Error(b"ERR Invalid stream ID specified as stream command argument".to_vec()) + ); + assert_eq!( + c.call(&[b"XGROUP", b"CREATE", b"s", b"g", b"badid"]), + Reply::Error(b"ERR Invalid stream ID specified as stream command argument".to_vec()) + ); + assert_eq!( + c.call(&[ + b"XGROUP", + b"CREATE", + b"absent", + b"g2", + b"badid", + b"MKSTREAM" + ]), + Reply::Error(b"ERR Invalid stream ID specified as stream command argument".to_vec()) + ); + + // Non-UTF-8 names are echoed as raw bytes, and NUL-suffixed subcommands are unknown. + assert_eq!( + c.call(&[b"XGROUP", b"SETID", b"s", b"no\xffgrp", b"0"]), + Reply::Error(b"NOGROUP No such consumer group 'no\xffgrp' for key name 's'".to_vec()) + ); + assert_eq!( + c.call(&[b"XINFO", b"stream\x00", b"s", b"COUNT", b"1"]), + Reply::Error(b"ERR unknown subcommand 'stream'. Try XINFO HELP.".to_vec()) + ); +}