"Fossies" - the Fresh Open Source Software Archive

Member "redis-7.0.5/tests/cluster/tests/05-slave-selection.tcl" (21 Sep 2022, 4885 Bytes) of package /linux/misc/redis-7.0.5.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Tcl/Tk source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 # Slave selection test
    2 # Check the algorithm trying to pick the slave with the most complete history.
    3 
    4 source "../tests/includes/init-tests.tcl"
    5 
    6 # Create a cluster with 5 master and 10 slaves, so that we have 2
    7 # slaves for each master.
    8 test "Create a 5 nodes cluster" {
    9     create_cluster 5 10
   10 }
   11 
   12 test "Cluster is up" {
   13     assert_cluster_state ok
   14 }
   15 
   16 test "The first master has actually two slaves" {
   17     wait_for_condition 1000 50 {
   18         [llength [lindex [R 0 role] 2]] == 2
   19     } else {
   20         fail "replicas didn't connect"
   21     }
   22 }
   23 
   24 test {Slaves of #0 are instance #5 and #10 as expected} {
   25     set port0 [get_instance_attrib redis 0 port]
   26     assert {[lindex [R 5 role] 2] == $port0}
   27     assert {[lindex [R 10 role] 2] == $port0}
   28 }
   29 
   30 test "Instance #5 and #10 synced with the master" {
   31     wait_for_condition 1000 50 {
   32         [RI 5 master_link_status] eq {up} &&
   33         [RI 10 master_link_status] eq {up}
   34     } else {
   35         fail "Instance #5 or #10 master link status is not up"
   36     }
   37 }
   38 
   39 set cluster [redis_cluster 127.0.0.1:[get_instance_attrib redis 0 port]]
   40 
   41 test "Slaves are both able to receive and acknowledge writes" {
   42     for {set j 0} {$j < 100} {incr j} {
   43         $cluster set $j $j
   44     }
   45     assert {[R 0 wait 2 60000] == 2}
   46 }
   47 
   48 test "Write data while slave #10 is paused and can't receive it" {
   49     # Stop the slave with a multi/exec transaction so that the master will
   50     # be killed as soon as it can accept writes again.
   51     R 10 multi
   52     R 10 debug sleep 10
   53     R 10 client kill 127.0.0.1:$port0
   54     R 10 deferred 1
   55     R 10 exec
   56 
   57     # Write some data the slave can't receive.
   58     for {set j 0} {$j < 100} {incr j} {
   59         $cluster set $j $j
   60     }
   61 
   62     # Prevent the master from accepting new slaves.
   63     # Use a large pause value since we'll kill it anyway.
   64     R 0 CLIENT PAUSE 60000
   65 
   66     # Wait for the slave to return available again
   67     R 10 deferred 0
   68     assert {[R 10 read] eq {OK OK}}
   69 
   70     # Kill the master so that a reconnection will not be possible.
   71     kill_instance redis 0
   72 }
   73 
   74 test "Wait for instance #5 (and not #10) to turn into a master" {
   75     wait_for_condition 1000 50 {
   76         [RI 5 role] eq {master}
   77     } else {
   78         fail "No failover detected"
   79     }
   80 }
   81 
   82 test "Wait for the node #10 to return alive before ending the test" {
   83     R 10 ping
   84 }
   85 
   86 test "Cluster should eventually be up again" {
   87     assert_cluster_state ok
   88 }
   89 
   90 test "Node #10 should eventually replicate node #5" {
   91     set port5 [get_instance_attrib redis 5 port]
   92     wait_for_condition 1000 50 {
   93         ([lindex [R 10 role] 2] == $port5) &&
   94         ([lindex [R 10 role] 3] eq {connected})
   95     } else {
   96         fail "#10 didn't became slave of #5"
   97     }
   98 }
   99 
  100 source "../tests/includes/init-tests.tcl"
  101 
  102 # Create a cluster with 3 master and 15 slaves, so that we have 5
  103 # slaves for eatch master.
  104 test "Create a 3 nodes cluster" {
  105     create_cluster 3 15
  106 }
  107 
  108 test "Cluster is up" {
  109     assert_cluster_state ok
  110 }
  111 
  112 test "The first master has actually 5 slaves" {
  113     wait_for_condition 1000 50 {
  114         [llength [lindex [R 0 role] 2]] == 5
  115     } else {
  116         fail "replicas didn't connect"
  117     }
  118 }
  119 
  120 test {Slaves of #0 are instance #3, #6, #9, #12 and #15 as expected} {
  121     set port0 [get_instance_attrib redis 0 port]
  122     assert {[lindex [R 3 role] 2] == $port0}
  123     assert {[lindex [R 6 role] 2] == $port0}
  124     assert {[lindex [R 9 role] 2] == $port0}
  125     assert {[lindex [R 12 role] 2] == $port0}
  126     assert {[lindex [R 15 role] 2] == $port0}
  127 }
  128 
  129 test {Instance #3, #6, #9, #12 and #15 synced with the master} {
  130     wait_for_condition 1000 50 {
  131         [RI 3 master_link_status] eq {up} &&
  132         [RI 6 master_link_status] eq {up} &&
  133         [RI 9 master_link_status] eq {up} &&
  134         [RI 12 master_link_status] eq {up} &&
  135         [RI 15 master_link_status] eq {up}
  136     } else {
  137         fail "Instance #3 or #6 or #9 or #12 or #15 master link status is not up"
  138     }
  139 }
  140 
  141 proc master_detected {instances} {
  142     foreach instance [dict keys $instances] {
  143         if {[RI $instance role] eq {master}} {
  144             return true
  145         }
  146     }
  147 
  148     return false
  149 }
  150 
  151 test "New Master down consecutively" {
  152     set instances [dict create 0 1 3 1 6 1 9 1 12 1 15 1]
  153 
  154     set loops [expr {[dict size $instances]-1}]
  155     for {set i 0} {$i < $loops} {incr i} {
  156         set master_id -1
  157         foreach instance [dict keys $instances] {
  158             if {[RI $instance role] eq {master}} {
  159                 set master_id $instance
  160                 break;
  161             }
  162         }
  163 
  164         if {$master_id eq -1} {
  165             fail "no master detected, #loop $i"
  166         }
  167 
  168         set instances [dict remove $instances $master_id]
  169 
  170         kill_instance redis $master_id
  171         wait_for_condition 1000 50 {
  172             [master_detected $instances]
  173         } else {
  174             fail "No failover detected when master $master_id fails"
  175         }
  176 
  177         assert_cluster_state ok
  178     }
  179 }