"Fossies" - the Fresh Open Source Software Archive

Member "redis-6.0.8/tests/cluster/tests/14-consistency-check.tcl" (10 Sep 2020, 3868 Bytes) of package /linux/misc/redis-6.0.8.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 and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "14-consistency-check.tcl": 6.0.7_vs_6.0.8.

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