Skip to content

Commit

Permalink
[fix] fix ttl compact after flushdb
Browse files Browse the repository at this point in the history
  • Loading branch information
VCgege authored and patpatbear committed Dec 18, 2024
1 parent 5a066da commit d07ee94
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 30 deletions.
2 changes: 1 addition & 1 deletion deps/xredis-gtid
Submodule xredis-gtid updated 0 files
2 changes: 0 additions & 2 deletions src/db.c
Original file line number Diff line number Diff line change
Expand Up @@ -496,8 +496,6 @@ long long emptyDb(int dbnum, int flags, void(callback)(void*)) {

if (server.swap_ttl_compact_ctx) {
swapTtlCompactCtxReset(server.swap_ttl_compact_ctx);
/* after flushdb, all sst in data CF is supposed to be deleted. */
server.swap_ttl_compact_ctx->expire_stats->sst_age_limit = 0;
}

/* Flush slots to keys map if enable cluster, we can flush entire
Expand Down
10 changes: 6 additions & 4 deletions src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -2485,10 +2485,12 @@ int serverCron(struct aeEventLoop *eventLoop, long long id, void *clientData) {

run_with_period(1000*(int)server.swap_swap_info_slave_period) {
/* propagate sst age limit */
robj *argv[3];
swapBuildSwapInfoSstAgeLimitCmd(argv, server.swap_ttl_compact_ctx->expire_stats->sst_age_limit);
swapPropagateSwapInfo(3, argv);
swapDestorySwapInfoSstAgeLimitCmd(argv);
if (server.swap_ttl_compact_enabled) {
robj *argv[3];
swapBuildSwapInfoSstAgeLimitCmd(argv, server.swap_ttl_compact_ctx->expire_stats->sst_age_limit);
swapPropagateSwapInfo(3, argv);
swapDestorySwapInfoSstAgeLimitCmd(argv);
}
}

/* Fire the cron loop modules event. */
Expand Down
68 changes: 45 additions & 23 deletions tests/swap/unit/ttl_compact.tcl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
start_server {tags {"ttl compact 0"}
start_server {tags {"ttl-compact"}
overrides {swap-debug-evict-keys {0}
swap-ttl-compact-period {1}
swap-sst-age-limit-refresh-period {1}
Expand Down Expand Up @@ -28,7 +28,7 @@ start_server {tags {"ttl compact 0"}
}
}

start_server {tags {"ttl compact 1"}
start_server {tags {"ttl-compact"}
overrides {swap-debug-evict-keys {0}
swap-ttl-compact-period {1}
swap-sst-age-limit-refresh-period {1}
Expand Down Expand Up @@ -67,7 +67,7 @@ start_server {tags {"ttl compact 1"}
}
}

start_server {tags {"ttl compact 2"}
start_server {tags {"ttl-compact"}
overrides {swap-debug-evict-keys {0}
swap-ttl-compact-period {1}
swap-sst-age-limit-refresh-period {1}
Expand All @@ -87,7 +87,7 @@ start_server {tags {"ttl compact 2"}
# sst in L0 is forced to be compacted to L1
r swap compact

# make sure info property updated
# more than swap-sst-age-limit-refresh-period
after 1200

set request_sst_count [get_info_property r Swap swap_ttl_compact request_sst_count]
Expand All @@ -107,7 +107,7 @@ start_server {tags {"ttl compact 2"}
}
}

start_server {tags {"ttl compact 3"}
start_server {tags {"ttl-compact"}
overrides {swap-debug-evict-keys {0}
swap-ttl-compact-period {1}
swap-sst-age-limit-refresh-period {1}
Expand All @@ -128,7 +128,7 @@ start_server {tags {"ttl compact 3"}
# sst in L0 is forced to be compacted to L1
r swap compact

# 1.2s, bigger than swap-ttl-compact-period
# 1.2s, more than swap-ttl-compact-period
after 1200

set sst_age_limit [get_info_property r Swap swap_ttl_compact sst_age_limit]
Expand Down Expand Up @@ -160,8 +160,8 @@ start_server {tags {"ttl compact 3"}
# sst in L0 is forced to be compacted to L1
r swap compact

# 1.6s, much bigger than ttl compact period
after 1600
# 1.2s, much bigger than ttl compact period
after 1200

set sst_age_limit [get_info_property r Swap swap_ttl_compact sst_age_limit]
assert_range $sst_age_limit 500 1000
Expand All @@ -180,7 +180,7 @@ start_server {tags {"ttl compact 3"}
}
}

start_server {tags {"ttl compact 4"}
start_server {tags {"ttl-compact"}
overrides {swap-debug-evict-keys {0}
swap-ttl-compact-period {1}
swap-sst-age-limit-refresh-period {1}
Expand All @@ -192,6 +192,7 @@ start_server {tags {"ttl compact 4"}

# bitmap is spilt as subkey of 4KB by default
r setbit $mybitmap 32768 1
r pexpire $mybitmap 1000

r swap.evict $mybitmap
wait_key_cold r $mybitmap
Expand All @@ -201,20 +202,17 @@ start_server {tags {"ttl compact 4"}
after 1200

set sst_age_limit [get_info_property r Swap swap_ttl_compact sst_age_limit]
assert_morethan $sst_age_limit 9000000000000000000

# sst_age_limit will be gradually 0 after flushdb if no new keys, just mock that
# stop sst-age-limit refresh
r config set swap-sst-age-limit-refresh-period 3600
assert_range $sst_age_limit 800 1000

# sst_age_limit will be reset after flushdb
r flushdb

set sst_age_limit [get_info_property r Swap swap_ttl_compact sst_age_limit]
assert_equal $sst_age_limit 0
assert_morethan $sst_age_limit 9000000000000000000
}
}

start_server {tags {"ttl compact 5"}
start_server {tags {"ttl-compact"}
overrides {swap-debug-evict-keys {0}
swap-ttl-compact-period {1}
swap-sst-age-limit-refresh-period {1}
Expand Down Expand Up @@ -262,12 +260,11 @@ start_server {tags {"ttl compact 5"}
}
}

start_server {tags {"master propagate expire test"} overrides {save ""}} {
start_server {tags {"ttl-compact master propagate"} overrides {save ""}} {

start_server {overrides {swap-repl-rordb-sync {no}
swap-debug-evict-keys {0}
swap-swap-info-slave-period {1}
swap-sst-age-limit-refresh-period {1000}}} {
swap-ttl-compact-enabled {no} }} {

set master_host [srv 0 host]
set master_port [srv 0 port]
Expand All @@ -279,16 +276,41 @@ start_server {tags {"master propagate expire test"} overrides {save ""}} {
wait_for_sync $slave
test {ttl compact master slave propagate check} {

$master swap.info SST-AGE-LIMIT 1111
# ttl-compact closed, sst_age_limit is initial value
set sst_age_limit1 [get_info_property $master Swap swap_ttl_compact sst_age_limit]
assert_morethan $sst_age_limit1 9000000000000000000

# more than swap-swap-info-slave-period
after 1100
# wait_for_ofs_sync $master $slave
# sst_age_limit in slave is also initial value
set sst_age_limit2 [get_info_property $slave Swap swap_ttl_compact sst_age_limit]
assert_equal $sst_age_limit1 $sst_age_limit2

# master open ttl compact, but not propagate sst_age_limit to slave right now
$master config set swap-swap-info-slave-period 36000
$master config set swap-ttl-compact-enabled yes
$master config set swap-sst-age-limit-refresh-period 1

after 1200

# sst_age_limit in master is set to 0 for the empty dataset
set sst_age_limit1 [get_info_property $master Swap swap_ttl_compact sst_age_limit]
assert_equal $sst_age_limit1 0

# sst_age_limit in slave is still initial value
set sst_age_limit2 [get_info_property $slave Swap swap_ttl_compact sst_age_limit]
assert_morethan $sst_age_limit2 9000000000000000000

# master propagate sst_age_limit to slave, and stop refresh its own
$master config set swap-swap-info-slave-period 1
$master config set swap-sst-age-limit-refresh-period 36000

$master swap.info SST-AGE-LIMIT 1111

set sst_age_limit1 [get_info_property $master Swap swap_ttl_compact sst_age_limit]
assert_equal $sst_age_limit1 1111

after 1200

set sst_age_limit2 [get_info_property $slave Swap swap_ttl_compact sst_age_limit]
assert_equal $sst_age_limit1 $sst_age_limit2
}
}
Expand Down

0 comments on commit d07ee94

Please sign in to comment.