"Fossies" - the Fresh Open Source Software Archive

Member "redis-7.0.5/tests/cluster/tests/14-consistency-check.tcl" (21 Sep 2022, 4092 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 source "../tests/includes/init-tests.tcl"
    2 source "../../../tests/support/cli.tcl"
    3 
    4 test "Create a 5 nodes cluster" {
    5     create_cluster 5 5
    6 }
    7 
    8 test "Cluster should start ok" {
    9     assert_cluster_state ok
   10 }
   11 
   12 test "Cluster is writable" {
   13     cluster_write_test 0
   14 }
   15 
   16 proc find_non_empty_master {} {
   17     set master_id_no {}
   18     foreach_redis_id id {
   19         if {[RI $id role] eq {master} && [R $id dbsize] > 0} {
   20             set master_id_no $id
   21             break
   22         }
   23     }
   24     return $master_id_no
   25 }
   26 
   27 proc get_one_of_my_replica {id} {
   28     wait_for_condition 1000 50 {
   29         [llength [lindex [R $id role] 2]] > 0
   30     } else {
   31         fail "replicas didn't connect"
   32     }
   33     set replica_port [lindex [lindex [lindex [R $id role] 2] 0] 1]
   34     set replica_id_num [get_instance_id_by_port redis $replica_port]
   35     return $replica_id_num
   36 }
   37 
   38 proc cluster_write_keys_with_expire {id ttl} {
   39     set prefix [randstring 20 20 alpha]
   40     set port [get_instance_attrib redis $id port]
   41     set cluster [redis_cluster 127.0.0.1:$port]
   42     for {set j 100} {$j < 200} {incr j} {
   43         $cluster setex key_expire.$j $ttl $prefix.$j
   44     }
   45     $cluster close
   46 }
   47 
   48 # make sure that replica who restarts from persistence will load keys
   49 # that have already expired, critical for correct execution of commands
   50 # that arrive from the master
   51 proc test_slave_load_expired_keys {aof} {
   52     test "Slave expired keys is loaded when restarted: appendonly=$aof" {
   53         set master_id [find_non_empty_master]
   54         set replica_id [get_one_of_my_replica $master_id]
   55 
   56         set master_dbsize_0 [R $master_id dbsize]
   57         set replica_dbsize_0 [R $replica_id dbsize]
   58         assert_equal $master_dbsize_0 $replica_dbsize_0
   59 
   60         # config the replica persistency and rewrite the config file to survive restart
   61         # note that this needs to be done before populating the volatile keys since
   62         # that triggers and AOFRW, and we rather the AOF file to have 'SET PXAT' commands
   63         # rather than an RDB with volatile keys
   64         R $replica_id config set appendonly $aof
   65         R $replica_id config rewrite
   66 
   67         # fill with 100 keys with 3 second TTL
   68         set data_ttl 3
   69         cluster_write_keys_with_expire $master_id $data_ttl
   70 
   71         # wait for replica to be in sync with master
   72         wait_for_condition 500 10 {
   73             [R $replica_id dbsize] eq [R $master_id dbsize]
   74         } else {
   75             fail "replica didn't sync"
   76         }
   77         
   78         set replica_dbsize_1 [R $replica_id dbsize]
   79         assert {$replica_dbsize_1 > $replica_dbsize_0}
   80 
   81         # make replica create persistence file
   82         if {$aof == "yes"} {
   83             # we need to wait for the initial AOFRW to be done, otherwise
   84             # kill_instance (which now uses SIGTERM will fail ("Writing initial AOF, can't exit")
   85             wait_for_condition 100 10 {
   86                 [RI $replica_id aof_rewrite_scheduled] eq 0 &&
   87                 [RI $replica_id aof_rewrite_in_progress] eq 0
   88             } else {
   89                 fail "AOFRW didn't finish"
   90             }
   91         } else {
   92             R $replica_id save
   93         }
   94 
   95         # kill the replica (would stay down until re-started)
   96         kill_instance redis $replica_id
   97 
   98         # Make sure the master doesn't do active expire (sending DELs to the replica)
   99         R $master_id DEBUG SET-ACTIVE-EXPIRE 0
  100 
  101         # wait for all the keys to get logically expired
  102         after [expr $data_ttl*1000]
  103 
  104         # start the replica again (loading an RDB or AOF file)
  105         restart_instance redis $replica_id
  106 
  107         # make sure the keys are still there
  108         set replica_dbsize_3 [R $replica_id dbsize]
  109         assert {$replica_dbsize_3 > $replica_dbsize_0}
  110         
  111         # restore settings
  112         R $master_id DEBUG SET-ACTIVE-EXPIRE 1
  113 
  114         # wait for the master to expire all keys and replica to get the DELs
  115         wait_for_condition 500 10 {
  116             [R $replica_id dbsize] eq $master_dbsize_0
  117         } else {
  118             fail "keys didn't expire"
  119         }
  120     }
  121 }
  122 
  123 test_slave_load_expired_keys no
  124 test_slave_load_expired_keys yes