Skip to content

Commit 5cc2b25

Browse files
authored
Fix cluster myself CLUSTER SLOTS/NODES wrong port after updating port/tls-port (valkey-io#2186)
When modifying port or tls-port through config set, we need to call clusterUpdateMyselfAnnouncedPorts to update myself's port, otherwise CLUSTER SLOTS/NODES will be old information from myself's perspective. In addition, in some places, such as clusterUpdateMyselfAnnouncedPorts and clusterUpdateMyselfIp, beforeSleep save is added so that the new ip info can be updated to nodes.conf. Remove clearCachedClusterSlotsResponse in updateClusterAnnouncedPort since now we add beforeSleep save in clusterUpdateMyselfAnnouncedPorts, and it will call clearCachedClusterSlotsResponse. Signed-off-by: Binbin <[email protected]>
1 parent 3bc40be commit 5cc2b25

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

src/cluster_legacy.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,6 +1024,7 @@ void clusterUpdateMyselfFlags(void) {
10241024
void clusterUpdateMyselfAnnouncedPorts(void) {
10251025
if (!myself) return;
10261026
deriveAnnouncedPorts(&myself->tcp_port, &myself->tls_port, &myself->cport);
1027+
clusterDoBeforeSleep(CLUSTER_TODO_SAVE_CONFIG);
10271028
}
10281029

10291030
/* We want to take myself->ip in sync with the cluster-announce-ip option.
@@ -1054,6 +1055,7 @@ void clusterUpdateMyselfIp(void) {
10541055
} else {
10551056
myself->ip[0] = '\0'; /* Force autodetection. */
10561057
}
1058+
clusterDoBeforeSleep(CLUSTER_TODO_SAVE_CONFIG);
10571059
}
10581060
}
10591061

src/config.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2473,6 +2473,7 @@ static int updatePort(const char **err) {
24732473
listener->bindaddr_count = server.bindaddr_count;
24742474
listener->port = server.port;
24752475
listener->ct = connectionByType(CONN_TYPE_SOCKET);
2476+
clusterUpdateMyselfAnnouncedPorts();
24762477
if (changeListener(listener) == C_ERR) {
24772478
*err = "Unable to listen on this port. Check server logs.";
24782479
return 0;
@@ -2660,7 +2661,6 @@ int updateClusterFlags(const char **err) {
26602661
static int updateClusterAnnouncedPort(const char **err) {
26612662
UNUSED(err);
26622663
clusterUpdateMyselfAnnouncedPorts();
2663-
clearCachedClusterSlotsResponse();
26642664
return 1;
26652665
}
26662666

@@ -2719,6 +2719,7 @@ static int applyTLSPort(const char **err) {
27192719
listener->bindaddr_count = server.bindaddr_count;
27202720
listener->port = server.tls_port;
27212721
listener->ct = connectionByType(CONN_TYPE_TLS);
2722+
clusterUpdateMyselfAnnouncedPorts();
27222723
if (changeListener(listener) == C_ERR) {
27232724
*err = "Unable to listen on this port. Check server logs.";
27242725
return 0;

tests/unit/cluster/announced-endpoints.tcl

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,43 @@ start_cluster 2 2 {tags {external:skip cluster}} {
5353
R 0 config set cluster-announce-bus-port 0
5454
assert_match "*@$base_bus_port *" [R 0 CLUSTER NODES]
5555
}
56+
57+
test "Test change port and tls-port on runtime" {
58+
if {$::tls} {
59+
set baseport [lindex [R 0 config get tls-port] 1]
60+
} else {
61+
set baseport [lindex [R 0 config get port] 1]
62+
}
63+
set count [expr [llength $::servers] + 1]
64+
set used_port [find_available_port $baseport $count]
65+
66+
# We execute CLUSTER SLOTS command to trigger the `debugServerAssertWithInfo` in `clusterCommandSlots` function, ensuring
67+
# that the cached response is invalidated upon updating any of port or tls-port.
68+
R 0 CLUSTER SLOTS
69+
R 1 CLUSTER SLOTS
70+
71+
# Set port or tls-port to ensure changes are consistent across the cluster.
72+
if {$::tls} {
73+
R 0 config set tls-port $used_port
74+
} else {
75+
R 0 config set port $used_port
76+
}
77+
# Make sure changes in myself node's view are consistent.
78+
assert_match "*:$used_port@*" [R 0 CLUSTER NODES]
79+
assert_match "*$used_port*" [R 0 CLUSTER SLOTS]
80+
# Make sure changes in other node's view are consistent.
81+
wait_for_condition 50 100 {
82+
[string match "*:$used_port@*" [R 1 CLUSTER NODES]] &&
83+
[string match "*$used_port*" [R 1 CLUSTER SLOTS]]
84+
} else {
85+
fail "Node port was not propagated via gossip"
86+
}
87+
88+
# Restore the original configuration item value.
89+
if {$::tls} {
90+
R 0 config set tls-port $baseport
91+
} else {
92+
R 0 config set port $baseport
93+
}
94+
}
5695
}

0 commit comments

Comments
 (0)