XDEL of several IDs recomputes first/last entry from a stale snapshot, leaving stream metadata on a deleted entry (XINFO STREAM "no such key", XTRIM MINID deletes a survivor)
Search before asking
- [x] I had searched in the issues and found no similar issues.
Version
- Kvrocks 2.17.0, from the published container image
apache/kvrocks:2.17.0(digestsha256:a1e99b3653fb900d5a17b80a9487f74079edc6d5e9b70e3103c01cbfc0d5146a).INFO serverreportskvrocks_version:2.17.0,kvrocks_git_sha1:185d4221(185d4221df20af8ed3416e4dec4de8917312a2e9, tagv2.17.0),kvrocks_mode:standalone. - OS: Ubuntu 24.04.5 LTS, kernel
7.0.0-38-generic(x86_64), Docker. The container reportsos:Linux 7.0.0-38-generic x86_64.
Minimal reproduce step
docker run -d -p 6401:6666 apache/kvrocks:2.17.0
From a clean keyspace (FLUSHALL), on one connection. The IDs are passed to XDEL in descending
order, which is what matters:
XADD s 1-0 f v -> "1-0"
XADD s 2-0 f v -> "2-0"
XADD s 3-0 f v -> "3-0"
XDEL s 2-0 1-0 -> (integer) 2 correct: two entries deleted
XINFO STREAM s -> (error) ERR no such key
XTRIM s MINID 3-0 -> (integer) 1 deletes the only surviving entry
XRANGE s - + -> (empty array) the stream is empty
The stream is left with its first-entry-id pointing at 2-0, an entry deleted by that same XDEL.
XINFO STREAM fails because it looks up that entry and cannot find it, and XTRIM MINID 3-0 — which
should delete nothing, since 3-0 is the only entry and is not below the threshold — walks from the
bogus first entry and removes it. The surviving entry is lost.
The mirror case leaves last-entry-id on a deleted entry:
FLUSHALL
XADD s 1-0 f v ; XADD s 2-0 f v ; XADD s 3-0 f v
XDEL s 2-0 3-0 -> (integer) 2
XINFO STREAM s -> (error) ERR no such key
A self-contained Python 3 reproducer (standard library only) that runs every case against Kvrocks and
reference servers and prints a comparison table is attached as repro.py; it exits 0 when the bug
reproduces.
What did you expect to see?
After XDEL s 2-0 1-0 the stream holds exactly 3-0, so:
-
XINFO STREAM sshould succeed, reportinglength 1withfirst-entry/last-entry=3-0; -
XTRIM s MINID 3-0should reply0and delete nothing, because XTRIM documentsMINIDasFor
MINID:thresholdis a stream ID. All entries whose IDs are less thanthresholdare trimmed. All entries with IDs greater than or equal tothresholdare kept. -
XRANGE s - +should still return3-0.
That is what Redis 8.10.2, KeyDB 6.3.4 and Dragonfly 2.0.2 all do.
What did you see instead?
Each row is an independent sequence from a clean keyspace holding 1-0, 2-0, 3-0.
| case | expected | Redis 8.10.2 | KeyDB | Dragonfly | Kvrocks 2.17.0 |
|---|---|---|---|---|---|
XDEL s 2-0 1-0 (the count is right) |
2 | 2 | 2 | 2 | 2 |
then XLEN s |
1 | 1 | 1 | 1 | 1 |
then XINFO STREAM s |
length 1, first-entry 3-0 |
ok | ok | ok | ERR no such key |
then XTRIM s MINID 3-0 |
0 | 0 | 0 | 0 | 1 |
then XRANGE s - + |
[3-0] |
[3-0] |
[3-0] |
[3-0] |
[] |
XDEL s 2-0 3-0, then XINFO STREAM s |
length 1, first/last 1-0 |
ok | ok | ok | ERR no such key |
control: XDEL s 1-0 2-0 (ascending), XINFO STREAM s |
ok | ok | ok | ok | ok |
control: two separate XDELs, XINFO STREAM s |
ok | ok | ok | ok | ok |
Note the last two rows: the reply count and XLEN are correct here, and ascending order or two
separate calls are both fine. What breaks is the recomputed first_entry_id/last_entry_id when one
XDEL deletes several IDs and a later-deleted entry is used as the replacement.
Anything Else?
Potential Cause
In Stream::DeleteEntries (src/types/redis_stream.cc:1003-1073) the iterator used to find the new
first/last entry is created from the command's read snapshot, before any delete is staged:
auto batch = storage_->GetWriteBatchBase(); // :1003
...
rocksdb::ReadOptions read_options = ctx.DefaultScanOptions(); // :1012
...
auto iter = util::UniqueIterator(ctx, read_options, stream_cf_handle_); // :1018
for (const auto &id : ids) {
std::string entry_key = internalKeyFromEntryID(ns_key, metadata, id);
std::string value;
s = storage_->Get(ctx, read_options, stream_cf_handle_, entry_key, &value);
if (s.ok()) {
*deleted_cnt += 1;
s = batch->Delete(stream_cf_handle_, entry_key); // :1026 staged only
...
if (id == metadata.first_entry_id) {
iter->Seek(entry_key);
iter->Next(); // :1042
if (iter->Valid()) {
metadata.first_entry_id = entryIDFromInternalKey(iter->key()); // :1044
metadata.recorded_first_entry_id = metadata.first_entry_id;
} else { ... }
}
if (id == metadata.last_entry_id) {
iter->Seek(entry_key);
iter->Prev(); // :1054
if (iter->Valid()) {
metadata.last_entry_id = entryIDFromInternalKey(iter->key()); // :1056
} else { ... }
}
}
}
Walk XDEL s 2-0 1-0:
2-0is deleted (staged). It is neither first nor last, so no recompute.1-0is deleted (staged). It isfirst_entry_id, so:1041-1042seeks to1-0and steps forward. The iterator sees the snapshot, where2-0is still present — theDeletefrom step 1 lives only in the localbatch— sometadata.first_entry_idbecomes2-0, an entry this same command deletes.
XINFO STREAM then looks up first_entry_id and fails with ERR no such key, and XTRIM MINID
starts its scan from 2-0 and goes on to remove 3-0. XDEL s 2-0 3-0 is the same story through
the last_entry_id branch at :1052-1059.
Outside a MULTI, Storage::Get consults only txn_write_batch_ or ctx.batch
(src/storage/storage.cc:627-631), never this function's local batch, and UniqueIterator is
built from the same snapshot — so neither the existence check nor the first/last walk can see the
command's own pending deletes.
Relationship to #3700
#3700 reports the other half of the same blind spot:
because the existence check at :1023 does not see the staged delete, a repeated ID is counted
and subtracted twice, desyncing and then underflowing metadata.size. Same function, same cause
(reads that miss the local write batch), different victim: #3700 corrupts the length, this one
corrupts the first/last entry IDs. A fix that makes DeleteEntries aware of what it has already
staged would address both; a fix for only one would leave the other.
Suggested fix
Make the deletes visible to the function's own reads, or avoid needing them to be:
- Collect the IDs that will actually be deleted first (a
std::set<StreamEntryID>also deduplicates, which is #3700), then recomputefirst_entry_id/last_entry_idonce, after the loop, skipping any key in that set while stepping the iterator. - Or use a batch-aware read (
GetFromBatchAndDBagainst the localbatch, asStorage::Getdoes fortxn_write_batch_) for both the existence check and the first/last walk.
A regression test with XDEL of several IDs in descending order, asserting XINFO STREAM and a
following XTRIM MINID, would pin it.
Are you willing to submit a PR?
- [ ] I'm willing to submit a PR!
Thank you @abhiramaab for picking it up. I do not have the authorization to assign issues to anyone, hopefully a maintainer will see this and assign you!
Hi @PDGGK I saw your PR for this was closed. Are you planning to reopen it? If not, I'd like to pick this up (and I'll credit your analysis of the seen_ids approach).
I can reproduce it on unstable (d56980a8): after XADD 1-0/2-0/3-0, XDEL s 2-0 1-0
makes XINFO STREAM s return "ERR no such key" instead of 3-0 as first/last entry.
Not planning to reopen it, so please go ahead. No need to credit me.