COPY/RENAME can resurrect deleted subkeys because the destination reuses the source's version
Search before asking
- [x] I searched the issues and found no similar issues.
Version
unstable @ 9cee1f58 (kvrocks unstable (commit 9cee1f58)), default config.
Minimal reproduce step
COPY:
127.0.0.1:6666> HSET src a 1 b 2
(integer) 2
127.0.0.1:6666> COPY src dst
(integer) 1
127.0.0.1:6666> HDEL src a
(integer) 1
127.0.0.1:6666> DEL dst
(integer) 1
127.0.0.1:6666> COPY src dst
(integer) 1
127.0.0.1:6666> HLEN dst
(integer) 1
127.0.0.1:6666> HGETALL dst
1) "a"
2) "1"
3) "b"
4) "2"
RENAME (round trip):
127.0.0.1:6666> HSET src a 1 b 2
(integer) 2
127.0.0.1:6666> RENAME src dst
OK
127.0.0.1:6666> HDEL dst a
(integer) 1
127.0.0.1:6666> RENAME dst src
OK
127.0.0.1:6666> HLEN src
(integer) 1
127.0.0.1:6666> HGETALL src
1) "a"
2) "1"
3) "b"
4) "2"
The same happens with sets (SMEMBERS shows a removed member while SCARD is 1) and sorted sets (ZRANGE and ZRANGEBYSCORE show a removed member while ZCARD is 1). It still happens after COMPACT.
What did you expect to see?
HGETALL returns only b 2, which matches HLEN 1.
What did you see instead?
HGETALL returns a 1 b 2. Field a was deleted, but it comes back, and the key's size counter no longer matches its contents. COMPACT never removes the stale field.
Anything Else?
Root cause: Database::Copy (src/storage/redis_db.cc) backs COPY, RENAME, RENAMENX and MOVEX. It writes the destination metadata as a byte-for-byte copy of the source metadata, and it writes each destination subkey with from_ikey.GetVersion(). As a result, the destination always gets the same version as the source.
Kvrocks deletes lazily. DEL and RENAME remove only the metadata entry, and the subkeys stay on disk until the compaction filter sees that their version no longer matches a live metadata entry. In the steps above:
- The first COPY or RENAME writes
dst|v1|aanddst|v1|bunder the source's versionv1. DEL dst(or the RENAME away from that name) leaves those subkeys orphaned.- The second COPY or RENAME recreates the metadata at that name with the same version
v1. Reads scan thedst|v1|prefix, so the orphanedais visible again. - The compaction filter compares each subkey's version against the live metadata.
v1 == v1, so it keeps the orphan permanently.
Possible fix: give the destination a fresh version in Database::Copy, as every other key creation path does.
Are you willing to submit a PR?
- [x] I'm willing to submit a PR!