cluster.tcl 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. # Primitive tests on cluster-enabled redis with modules using redis-cli
  2. source tests/support/cli.tcl
  3. proc cluster_info {r field} {
  4. if {[regexp "^$field:(.*?)\r\n" [$r cluster info] _ value]} {
  5. set _ $value
  6. }
  7. }
  8. # Provide easy access to CLUSTER INFO properties. Same semantic as "proc s".
  9. proc csi {args} {
  10. set level 0
  11. if {[string is integer [lindex $args 0]]} {
  12. set level [lindex $args 0]
  13. set args [lrange $args 1 end]
  14. }
  15. cluster_info [srv $level "client"] [lindex $args 0]
  16. }
  17. set testmodule [file normalize tests/modules/blockonkeys.so]
  18. set testmodule_nokey [file normalize tests/modules/blockonbackground.so]
  19. # make sure the test infra won't use SELECT
  20. set ::singledb 1
  21. # cluster creation is complicated with TLS, and the current tests don't really need that coverage
  22. tags {tls:skip external:skip cluster modules} {
  23. # start three servers
  24. set base_conf [list cluster-enabled yes cluster-node-timeout 1 loadmodule $testmodule]
  25. start_server [list overrides $base_conf] {
  26. start_server [list overrides $base_conf] {
  27. start_server [list overrides $base_conf] {
  28. set node1 [srv 0 client]
  29. set node2 [srv -1 client]
  30. set node3 [srv -2 client]
  31. set node3_pid [srv -2 pid]
  32. # the "overrides" mechanism can only support one "loadmodule" directive
  33. $node1 module load $testmodule_nokey
  34. $node2 module load $testmodule_nokey
  35. $node3 module load $testmodule_nokey
  36. test {Create 3 node cluster} {
  37. exec src/redis-cli --cluster-yes --cluster create \
  38. 127.0.0.1:[srv 0 port] \
  39. 127.0.0.1:[srv -1 port] \
  40. 127.0.0.1:[srv -2 port]
  41. wait_for_condition 1000 50 {
  42. [csi 0 cluster_state] eq {ok} &&
  43. [csi -1 cluster_state] eq {ok} &&
  44. [csi -2 cluster_state] eq {ok}
  45. } else {
  46. fail "Cluster doesn't stabilize"
  47. }
  48. }
  49. test "Run blocking command (blocked on key) on cluster node3" {
  50. # key9184688 is mapped to slot 10923 (first slot of node 3)
  51. set node3_rd [redis_deferring_client -2]
  52. $node3_rd fsl.bpop key9184688 0
  53. $node3_rd flush
  54. wait_for_condition 50 100 {
  55. [s -2 blocked_clients] eq {1}
  56. } else {
  57. fail "Client executing blocking command (blocked on key) not blocked"
  58. }
  59. }
  60. test "Run blocking command (no keys) on cluster node2" {
  61. set node2_rd [redis_deferring_client -1]
  62. $node2_rd block.block 0
  63. $node2_rd flush
  64. wait_for_condition 50 100 {
  65. [s -1 blocked_clients] eq {1}
  66. } else {
  67. fail "Client executing blocking command (no keys) not blocked"
  68. }
  69. }
  70. test "Perform a Resharding" {
  71. exec src/redis-cli --cluster-yes --cluster reshard 127.0.0.1:[srv -2 port] \
  72. --cluster-to [$node1 cluster myid] \
  73. --cluster-from [$node3 cluster myid] \
  74. --cluster-slots 1
  75. }
  76. test "Verify command (no keys) is unaffected after resharding" {
  77. # verify there are blocked clients on node2
  78. assert_equal [s -1 blocked_clients] {1}
  79. #release client
  80. $node2 block.release 0
  81. }
  82. test "Verify command (blocked on key) got unblocked after resharding" {
  83. # this (read) will wait for the node3 to realize the new topology
  84. assert_error {*MOVED*} {$node3_rd read}
  85. # verify there are no blocked clients
  86. assert_equal [s 0 blocked_clients] {0}
  87. assert_equal [s -1 blocked_clients] {0}
  88. assert_equal [s -2 blocked_clients] {0}
  89. }
  90. test "Wait for cluster to be stable" {
  91. wait_for_condition 1000 50 {
  92. [catch {exec src/redis-cli --cluster \
  93. check 127.0.0.1:[srv 0 port] \
  94. }] == 0
  95. } else {
  96. fail "Cluster doesn't stabilize"
  97. }
  98. }
  99. test "Sanity test push cmd after resharding" {
  100. assert_error {*MOVED*} {$node3 fsl.push key9184688 1}
  101. set node1_rd [redis_deferring_client 0]
  102. $node1_rd fsl.bpop key9184688 0
  103. $node1_rd flush
  104. wait_for_condition 50 100 {
  105. [s 0 blocked_clients] eq {1}
  106. } else {
  107. puts "Client not blocked"
  108. puts "read from blocked client: [$node1_rd read]"
  109. fail "Client not blocked"
  110. }
  111. $node1 fsl.push key9184688 2
  112. assert_equal {2} [$node1_rd read]
  113. }
  114. $node1_rd close
  115. $node2_rd close
  116. $node3_rd close
  117. test "Run blocking command (blocked on key) again on cluster node1" {
  118. $node1 del key9184688
  119. # key9184688 is mapped to slot 10923 which has been moved to node1
  120. set node1_rd [redis_deferring_client 0]
  121. $node1_rd fsl.bpop key9184688 0
  122. $node1_rd flush
  123. wait_for_condition 50 100 {
  124. [s 0 blocked_clients] eq {1}
  125. } else {
  126. fail "Client executing blocking command (blocked on key) again not blocked"
  127. }
  128. }
  129. test "Run blocking command (no keys) again on cluster node2" {
  130. set node2_rd [redis_deferring_client -1]
  131. $node2_rd block.block 0
  132. $node2_rd flush
  133. wait_for_condition 50 100 {
  134. [s -1 blocked_clients] eq {1}
  135. } else {
  136. fail "Client executing blocking command (no keys) again not blocked"
  137. }
  138. }
  139. test "Kill a cluster node and wait for fail state" {
  140. # kill node3 in cluster
  141. exec kill -SIGSTOP $node3_pid
  142. wait_for_condition 1000 50 {
  143. [csi 0 cluster_state] eq {fail} &&
  144. [csi -1 cluster_state] eq {fail}
  145. } else {
  146. fail "Cluster doesn't fail"
  147. }
  148. }
  149. test "Verify command (blocked on key) got unblocked after cluster failure" {
  150. assert_error {*CLUSTERDOWN*} {$node1_rd read}
  151. }
  152. test "Verify command (no keys) got unblocked after cluster failure" {
  153. assert_error {*CLUSTERDOWN*} {$node2_rd read}
  154. # verify there are no blocked clients
  155. assert_equal [s 0 blocked_clients] {0}
  156. assert_equal [s -1 blocked_clients] {0}
  157. }
  158. exec kill -SIGCONT $node3_pid
  159. $node1_rd close
  160. $node2_rd close
  161. # stop three servers
  162. }
  163. }
  164. }
  165. } ;# tags