Skip to content

Commit 655864b

Browse files
committed
Handle empty hash when all fields are expired during RDB load
Signed-off-by: Hanxi Zhang <hanxizh@amazon.com>
1 parent a01a837 commit 655864b

2 files changed

Lines changed: 36 additions & 0 deletions

File tree

src/rdb.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2283,6 +2283,12 @@ robj *rdbLoadObject(int rdbtype, rio *rdb, sds key, int dbid, int *error, int rd
22832283

22842284
/* All pairs should be read by now */
22852285
serverAssert(len == 0);
2286+
2287+
/* Check if hash became empty after skipping all expired fields */
2288+
if (hashTypeLength(o) == 0) {
2289+
decrRefCount(o);
2290+
goto emptykey;
2291+
}
22862292
} else if (rdbtype == RDB_TYPE_LIST_QUICKLIST || rdbtype == RDB_TYPE_LIST_QUICKLIST_2) {
22872293
if ((len = rdbLoadLen(rdb, NULL)) == RDB_LENERR) return NULL;
22882294
if (len == 0) goto emptykey;

tests/unit/hashexpire.tcl

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4616,6 +4616,36 @@ start_server {tags {"hashexpire skip expired fields"}} {
46164616
assert_equal "" [r HGET myhash f3]
46174617
} {} {needs:debug}
46184618

4619+
test {Hash is skipped when all fields are expired during RDB load on primary} {
4620+
r FLUSHALL
4621+
4622+
# Disable active expiration
4623+
r DEBUG SET-ACTIVE-EXPIRE 0
4624+
r HSETEX myhash PX 1 FIELDS 3 f1 v1 f2 v2 f3 v3
4625+
4626+
# No permanent field - all fields will expire
4627+
4628+
# Wait until all fields are expired (HTTL returns -2)
4629+
wait_for_condition 50 100 {
4630+
[lindex [r HTTL myhash FIELDS 1 f1] 0] == -2 &&
4631+
[lindex [r HTTL myhash FIELDS 1 f2] 0] == -2 &&
4632+
[lindex [r HTTL myhash FIELDS 1 f3] 0] == -2
4633+
} else {
4634+
fail "Fields did not expire"
4635+
}
4636+
4637+
r SAVE
4638+
r FLUSHALL
4639+
r DEBUG RELOAD NOSAVE
4640+
4641+
# Re-enable active expiration
4642+
r DEBUG SET-ACTIVE-EXPIRE 1
4643+
4644+
# Verify: key should not exist at all (empty hash skipped)
4645+
assert_equal 0 [r EXISTS myhash]
4646+
assert_equal 0 [r HLEN myhash]
4647+
} {} {needs:debug}
4648+
46194649
test {RESTORE loads expired hash fields} {
46204650
r FLUSHALL
46214651
r DEBUG SET-ACTIVE-EXPIRE 0

0 commit comments

Comments
 (0)