@@ -2,11 +2,12 @@ set base_conf [list cluster-enabled yes cluster-node-timeout 1000 cluster-databa
22
33test {Migrate the last slot away from a node using valkey-cli} {
44 start_multiple_servers 4 [list overrides $base_conf ] {
5+
56 # Create a cluster of 3 nodes
67 exec src/valkey-cli --cluster-yes --cluster create \
7- 127.0.0.1:[srv 0 port] \
8- 127.0.0.1:[srv -1 port] \
9- 127.0.0.1:[srv -2 port]
8+ 127.0.0.1:[srv 0 port] \
9+ 127.0.0.1:[srv -1 port] \
10+ 127.0.0.1:[srv -2 port]
1011
1112 wait_for_condition 1000 50 {
1213 [CI 0 cluster_state] eq {ok} &&
@@ -16,11 +17,16 @@ test {Migrate the last slot away from a node using valkey-cli} {
1617 fail " Cluster doesn't stabilize"
1718 }
1819
20+ # Insert some data
21+ assert_equal OK [exec src/valkey-cli -c -p [srv 0 port] SET foo bar]
22+ set slot [exec src/valkey-cli -c -p [srv 0 port] CLUSTER KEYSLOT foo]
23+
1924 # Add new node to the cluster
2025 exec src/valkey-cli --cluster-yes --cluster add-node \
21- 127.0.0.1:[srv -3 port] \
22- 127.0.0.1:[srv 0 port]
26+ 127.0.0.1:[srv -3 port] \
27+ 127.0.0.1:[srv 0 port]
2328
29+ # First we wait for new node to be recognized by entire cluster
2430 wait_for_cluster_size 4
2531
2632 wait_for_condition 1000 50 {
@@ -32,52 +38,75 @@ test {Migrate the last slot away from a node using valkey-cli} {
3238 fail " Cluster doesn't stabilize"
3339 }
3440
41+ # Ensure all node IDs are propagated
42+ wait_for_condition 1000 50 {
43+ [CI 0 cluster_known_nodes] == 4 &&
44+ [CI 1 cluster_known_nodes] == 4 &&
45+ [CI 2 cluster_known_nodes] == 4 &&
46+ [CI 3 cluster_known_nodes] == 4
47+ } else {
48+ fail " Cluster gossip hasn't propagated all node IDs"
49+ }
50+
3551 set newnode_r [valkey_client -3]
3652 set newnode_id [$newnode_r CLUSTER MYID]
37-
38- # Find a node with slots and migrate one slot to the new node
39- set source_r [valkey_client 0]
40- set source_id [$source_r CLUSTER MYID]
41- set slots [$source_r CLUSTER SLOTS]
42- set slot [lindex [lindex $slots 0] 0]
43-
44- # Migrate the slot using valkey-cli
45- exec src/valkey-cli --cluster reshard 127.0.0.1:[srv 0 port] \
46- --cluster-from $source_id \
47- --cluster-to $newnode_id \
48- --cluster-slots 1 \
49- --cluster-yes
5053
54+ # Find out which node has the key "foo" by asking the new node for a
55+ # redirect.
56+ catch { $newnode_r get foo } e
57+ assert_match " MOVED $slot *" $e
58+ lassign [split [lindex $e 2] :] owner_host owner_port
59+ set owner_r [valkey $owner_host $owner_port 0 $::tls ]
60+ set owner_id [$owner_r CLUSTER MYID]
61+
62+ # Move slot to new node using plain commands
63+ assert_equal OK [$newnode_r CLUSTER SETSLOT $slot IMPORTING $owner_id ]
64+ assert_equal OK [$owner_r CLUSTER SETSLOT $slot MIGRATING $newnode_id ]
65+ assert_equal {foo} [$owner_r CLUSTER GETKEYSINSLOT $slot 10]
66+ assert_equal OK [$owner_r MIGRATE 127.0.0.1 [srv -3 port] " " 0 5000 KEYS foo]
67+ assert_equal OK [$newnode_r CLUSTER SETSLOT $slot NODE $newnode_id ]
68+ assert_equal OK [$owner_r CLUSTER SETSLOT $slot NODE $newnode_id ]
69+
70+ # Using --cluster check make sure we won't get `Not all slots are covered by nodes`.
71+ # Wait for the cluster to become stable make sure the cluster is up during MIGRATE.
5172 wait_for_condition 1000 50 {
73+ [catch {exec src/valkey-cli --cluster check 127.0.0.1:[srv 0 port]}] == 0 &&
74+ [catch {exec src/valkey-cli --cluster check 127.0.0.1:[srv -1 port]}] == 0 &&
75+ [catch {exec src/valkey-cli --cluster check 127.0.0.1:[srv -2 port]}] == 0 &&
76+ [catch {exec src/valkey-cli --cluster check 127.0.0.1:[srv -3 port]}] == 0 &&
5277 [CI 0 cluster_state] eq {ok} &&
5378 [CI 1 cluster_state] eq {ok} &&
5479 [CI 2 cluster_state] eq {ok} &&
5580 [CI 3 cluster_state] eq {ok}
5681 } else {
57- fail " Cluster doesn't stabilize after migration "
82+ fail " Cluster doesn't stabilize"
5883 }
5984
60- # Now migrate the last slot away from the new node using valkey-cli
85+ # Move the only slot back to original node using valkey-cli
6186 exec src/valkey-cli --cluster reshard 127.0.0.1:[srv -3 port] \
6287 --cluster-from $newnode_id \
63- --cluster-to $source_id \
88+ --cluster-to $owner_id \
6489 --cluster-slots 1 \
6590 --cluster-yes
6691
92+ # The empty node will become a replica of the new owner before the
93+ # `MOVED` check, so let's wait for the cluster to become stable.
6794 wait_for_condition 1000 50 {
6895 [CI 0 cluster_state] eq {ok} &&
6996 [CI 1 cluster_state] eq {ok} &&
7097 [CI 2 cluster_state] eq {ok} &&
7198 [CI 3 cluster_state] eq {ok}
7299 } else {
73- fail " Cluster doesn't stabilize after last slot migration "
100+ fail " Cluster doesn't stabilize"
74101 }
75102
76- # Verify the node with no slots becomes a replica
77- wait_for_condition 1000 50 {
78- [string match " *slave*" [$newnode_r role]]
79- } else {
80- fail " Empty node didn't become a replica"
81- }
103+ # Check that the key foo has been migrated back to the original owner.
104+ catch { $newnode_r get foo } e
105+ assert_equal " MOVED $slot $owner_host :$owner_port " $e
106+
107+ # Check that the now empty primary node doesn't turn itself into
108+ # a replica of any other nodes
109+ wait_for_cluster_propagation
110+ assert_match *master* [$owner_r role]
82111 }
83112}
0 commit comments