cluster.tcl 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. # Primitive tests on cluster-enabled redis 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. # make sure the test infra won't use SELECT
  18. set ::singledb 1
  19. # cluster creation is complicated with TLS, and the current tests don't really need that coverage
  20. tags {tls:skip external:skip cluster} {
  21. # start three servers
  22. set base_conf [list cluster-enabled yes cluster-node-timeout 1]
  23. start_server [list overrides $base_conf] {
  24. start_server [list overrides $base_conf] {
  25. start_server [list overrides $base_conf] {
  26. set node1 [srv 0 client]
  27. set node2 [srv -1 client]
  28. set node3 [srv -2 client]
  29. set node3_pid [srv -2 pid]
  30. test {Create 3 node cluster} {
  31. exec src/redis-cli --cluster-yes --cluster create \
  32. 127.0.0.1:[srv 0 port] \
  33. 127.0.0.1:[srv -1 port] \
  34. 127.0.0.1:[srv -2 port]
  35. wait_for_condition 1000 50 {
  36. [csi 0 cluster_state] eq {ok} &&
  37. [csi -1 cluster_state] eq {ok} &&
  38. [csi -2 cluster_state] eq {ok}
  39. } else {
  40. fail "Cluster doesn't stabilize"
  41. }
  42. }
  43. test "Run blocking command on cluster node3" {
  44. # key9184688 is mapped to slot 10923 (first slot of node 3)
  45. set node3_rd [redis_deferring_client -2]
  46. $node3_rd brpop key9184688 0
  47. $node3_rd flush
  48. wait_for_condition 50 100 {
  49. [s -2 blocked_clients] eq {1}
  50. } else {
  51. fail "Client not blocked"
  52. }
  53. }
  54. test "Perform a Resharding" {
  55. exec src/redis-cli --cluster-yes --cluster reshard 127.0.0.1:[srv -2 port] \
  56. --cluster-to [$node1 cluster myid] \
  57. --cluster-from [$node3 cluster myid] \
  58. --cluster-slots 1
  59. }
  60. test "Verify command got unblocked after resharding" {
  61. # this (read) will wait for the node3 to realize the new topology
  62. assert_error {*MOVED*} {$node3_rd read}
  63. # verify there are no blocked clients
  64. assert_equal [s 0 blocked_clients] {0}
  65. assert_equal [s -1 blocked_clients] {0}
  66. assert_equal [s -2 blocked_clients] {0}
  67. }
  68. test "Wait for cluster to be stable" {
  69. wait_for_condition 1000 50 {
  70. [catch {exec src/redis-cli --cluster \
  71. check 127.0.0.1:[srv 0 port] \
  72. }] == 0
  73. } else {
  74. fail "Cluster doesn't stabilize"
  75. }
  76. }
  77. test "Sanity test push cmd after resharding" {
  78. assert_error {*MOVED*} {$node3 lpush key9184688 v1}
  79. set node1_rd [redis_deferring_client 0]
  80. $node1_rd brpop key9184688 0
  81. $node1_rd flush
  82. wait_for_condition 50 100 {
  83. [s 0 blocked_clients] eq {1}
  84. } else {
  85. puts "Client not blocked"
  86. puts "read from blocked client: [$node1_rd read]"
  87. fail "Client not blocked"
  88. }
  89. $node1 lpush key9184688 v2
  90. assert_equal {key9184688 v2} [$node1_rd read]
  91. }
  92. $node1_rd close
  93. $node3_rd close
  94. test "Run blocking command again on cluster node1" {
  95. $node1 del key9184688
  96. # key9184688 is mapped to slot 10923 which has been moved to node1
  97. set node1_rd [redis_deferring_client 0]
  98. $node1_rd brpop key9184688 0
  99. $node1_rd flush
  100. wait_for_condition 50 100 {
  101. [s 0 blocked_clients] eq {1}
  102. } else {
  103. fail "Client not blocked"
  104. }
  105. }
  106. test "Kill a cluster node and wait for fail state" {
  107. # kill node3 in cluster
  108. exec kill -SIGSTOP $node3_pid
  109. wait_for_condition 1000 50 {
  110. [csi 0 cluster_state] eq {fail} &&
  111. [csi -1 cluster_state] eq {fail}
  112. } else {
  113. fail "Cluster doesn't fail"
  114. }
  115. }
  116. test "Verify command got unblocked after cluster failure" {
  117. assert_error {*CLUSTERDOWN*} {$node1_rd read}
  118. # verify there are no blocked clients
  119. assert_equal [s 0 blocked_clients] {0}
  120. assert_equal [s -1 blocked_clients] {0}
  121. }
  122. exec kill -SIGCONT $node3_pid
  123. $node1_rd close
  124. # stop three servers
  125. }
  126. }
  127. }
  128. } ;# tags