From 6bab2d7968dd9d71f2ce200386ecf4286e333a76 Mon Sep 17 00:00:00 2001 From: Binbin Date: Thu, 30 May 2024 10:44:12 +0800 Subject: [PATCH] Make sure clear the CLUSTER SLOTS cache on time when updating hostname (#564) In #53, we will cache the CLUSTER SLOTS response to improve the throughput and reduct the latency. In the code snippet below, the second cluster slots will use the old hostname: ``` config set cluster-preferred-endpoint-type hostname config set cluster-announce-hostname old-hostname.com multi cluster slots config set cluster-announce-hostname new-hostname.com cluster slots exec ``` When updating the hostname, in updateAnnouncedHostname, we will set CLUSTER_TODO_SAVE_CONFIG and we will do a clearCachedClusterSlotsResponse in clusterSaveConfigOrDie, so harmless in most cases. Move the clearCachedClusterSlotsResponse call to clusterDoBeforeSleep instead of scheduling it to be called in clusterSaveConfigOrDie. Signed-off-by: Binbin --- src/cluster_legacy.c | 4 +++- tests/unit/cluster/hostnames.tcl | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/cluster_legacy.c b/src/cluster_legacy.c index 63364d359..22fdb20cf 100644 --- a/src/cluster_legacy.c +++ b/src/cluster_legacy.c @@ -752,7 +752,6 @@ void clusterSaveConfigOrDie(int do_fsync) { serverLog(LL_WARNING, "Fatal: can't update cluster config file."); exit(1); } - clearCachedClusterSlotsResponse(); } /* Lock the cluster config using flock(), and retain the file descriptor used to @@ -4847,6 +4846,9 @@ void clusterBeforeSleep(void) { } void clusterDoBeforeSleep(int flags) { + /* Clear the cache if there are config changes here. */ + if (flags & CLUSTER_TODO_SAVE_CONFIG) clearCachedClusterSlotsResponse(); + server.cluster->todo_before_sleep |= flags; } diff --git a/tests/unit/cluster/hostnames.tcl b/tests/unit/cluster/hostnames.tcl index 7be4b42aa..98a6385c6 100644 --- a/tests/unit/cluster/hostnames.tcl +++ b/tests/unit/cluster/hostnames.tcl @@ -73,6 +73,20 @@ test "Verify cluster-preferred-endpoint-type behavior for redirects and info" { # Verify prefer hostname behavior R 0 config set cluster-preferred-endpoint-type hostname + # Make sure the cache is cleared when updating hostname. + R 0 multi + R 0 cluster slots + R 0 config set cluster-announce-hostname "new-me.com" + R 0 cluster slots + set multi_result [R 0 exec] + set slot_result1 [lindex $multi_result 0] + set slot_result2 [lindex $multi_result 2] + assert_equal "me.com" [get_slot_field $slot_result1 0 2 0] + assert_equal "new-me.com" [get_slot_field $slot_result2 0 2 0] + + # Set it back to its original value. + R 0 config set cluster-announce-hostname "me.com" + set slot_result [R 0 cluster slots] assert_equal "me.com" [get_slot_field $slot_result 0 2 0] assert_equal "them.com" [get_slot_field $slot_result 2 2 0]