Skip to content

Commit 4030ad6

Browse files
enjoy-binbinvitarb
authored andcommitted
Fix cluster myself CLUSTER SLOTS/NODES wrong port after updating port/tls-port (#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]> (cherry picked from commit 5cc2b25)
1 parent 116c402 commit 4030ad6

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
@@ -970,6 +970,7 @@ void clusterUpdateMyselfFlags(void) {
970970
void clusterUpdateMyselfAnnouncedPorts(void) {
971971
if (!myself) return;
972972
deriveAnnouncedPorts(&myself->tcp_port, &myself->tls_port, &myself->cport);
973+
clusterDoBeforeSleep(CLUSTER_TODO_SAVE_CONFIG);
973974
}
974975

975976
/* We want to take myself->ip in sync with the cluster-announce-ip option.
@@ -1000,6 +1001,7 @@ void clusterUpdateMyselfIp(void) {
10001001
} else {
10011002
myself->ip[0] = '\0'; /* Force autodetection. */
10021003
}
1004+
clusterDoBeforeSleep(CLUSTER_TODO_SAVE_CONFIG);
10031005
}
10041006
}
10051007

src/config.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2445,6 +2445,7 @@ static int updatePort(const char **err) {
24452445
listener->bindaddr_count = server.bindaddr_count;
24462446
listener->port = server.port;
24472447
listener->ct = connectionByType(CONN_TYPE_SOCKET);
2448+
clusterUpdateMyselfAnnouncedPorts();
24482449
if (changeListener(listener) == C_ERR) {
24492450
*err = "Unable to listen on this port. Check server logs.";
24502451
return 0;
@@ -2632,7 +2633,6 @@ int updateClusterFlags(const char **err) {
26322633
static int updateClusterAnnouncedPort(const char **err) {
26332634
UNUSED(err);
26342635
clusterUpdateMyselfAnnouncedPorts();
2635-
clearCachedClusterSlotsResponse();
26362636
return 1;
26372637
}
26382638

@@ -2691,6 +2691,7 @@ static int applyTLSPort(const char **err) {
26912691
listener->bindaddr_count = server.bindaddr_count;
26922692
listener->port = server.tls_port;
26932693
listener->ct = connectionByType(CONN_TYPE_TLS);
2694+
clusterUpdateMyselfAnnouncedPorts();
26942695
if (changeListener(listener) == C_ERR) {
26952696
*err = "Unable to listen on this port. Check server logs.";
26962697
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)