"Fossies" - the Fresh Open Source Software Archive

Member "redis-7.0.5/tests/cluster/tests/25-pubsubshard-slot-migration.tcl" (21 Sep 2022, 6106 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. See also the last Fossies "Diffs" side-by-side code changes report for "25-pubsubshard-slot-migration.tcl": 7.0.0_vs_7.0.1.

    1 source "../tests/includes/init-tests.tcl"
    2 
    3 test "Create a 3 nodes cluster" {
    4     cluster_create_with_continuous_slots 3 3
    5 }
    6 
    7 test "Cluster is up" {
    8     assert_cluster_state ok
    9 }
   10 
   11 set cluster [redis_cluster 127.0.0.1:[get_instance_attrib redis 0 port]]
   12 
   13 test "Migrate a slot, verify client receives sunsubscribe on primary serving the slot." {
   14 
   15     # Setup the to and from node
   16     set channelname mychannel
   17     set slot [$cluster cluster keyslot $channelname]
   18     array set nodefrom [$cluster masternode_for_slot $slot]
   19     array set nodeto [$cluster masternode_notfor_slot $slot]
   20 
   21     set subscribeclient [redis_deferring_client_by_addr $nodefrom(host) $nodefrom(port)]
   22 
   23     $subscribeclient deferred 1
   24     $subscribeclient ssubscribe $channelname
   25     $subscribeclient read
   26 
   27     assert_equal {OK} [$nodefrom(link) cluster setslot $slot migrating $nodeto(id)]
   28     assert_equal {OK} [$nodeto(link) cluster setslot $slot importing $nodefrom(id)]
   29 
   30     # Verify subscribe is still valid, able to receive messages.
   31     $nodefrom(link) spublish $channelname hello
   32     assert_equal {smessage mychannel hello} [$subscribeclient read]
   33 
   34     assert_equal {OK} [$nodefrom(link) cluster setslot $slot node $nodeto(id)]
   35    
   36     set msg [$subscribeclient read]
   37     assert {"sunsubscribe" eq [lindex $msg 0]}
   38     assert {$channelname eq [lindex $msg 1]}
   39     assert {"0" eq [lindex $msg 2]}
   40 
   41     assert_equal {OK} [$nodeto(link) cluster setslot $slot node $nodeto(id)]
   42 
   43     $subscribeclient close
   44 }
   45 
   46 test "Client subscribes to multiple channels, migrate a slot, verify client receives sunsubscribe on primary serving the slot." {
   47 
   48     # Setup the to and from node
   49     set channelname ch3
   50     set anotherchannelname ch7
   51     set slot [$cluster cluster keyslot $channelname]
   52     array set nodefrom [$cluster masternode_for_slot $slot]
   53     array set nodeto [$cluster masternode_notfor_slot $slot]
   54 
   55     set subscribeclient [redis_deferring_client_by_addr $nodefrom(host) $nodefrom(port)]
   56 
   57     $subscribeclient deferred 1
   58     $subscribeclient ssubscribe $channelname
   59     $subscribeclient read
   60 
   61     $subscribeclient ssubscribe $anotherchannelname
   62     $subscribeclient read
   63 
   64     assert_equal {OK} [$nodefrom(link) cluster setslot $slot migrating $nodeto(id)]
   65     assert_equal {OK} [$nodeto(link) cluster setslot $slot importing $nodefrom(id)]
   66 
   67     # Verify subscribe is still valid, able to receive messages.
   68     $nodefrom(link) spublish $channelname hello
   69     assert_equal {smessage ch3 hello} [$subscribeclient read]
   70 
   71     assert_equal {OK} [$nodefrom(link) cluster setslot $slot node $nodeto(id)]
   72 
   73     # Verify the client receives sunsubscribe message for the channel(slot) which got migrated.
   74     set msg [$subscribeclient read]
   75     assert {"sunsubscribe" eq [lindex $msg 0]}
   76     assert {$channelname eq [lindex $msg 1]}
   77     assert {"1" eq [lindex $msg 2]}
   78 
   79     assert_equal {OK} [$nodeto(link) cluster setslot $slot node $nodeto(id)]
   80 
   81     $nodefrom(link) spublish $anotherchannelname hello
   82 
   83     # Verify the client is still connected and receives message from the other channel.
   84     set msg [$subscribeclient read]
   85     assert {"smessage" eq [lindex $msg 0]}
   86     assert {$anotherchannelname eq [lindex $msg 1]}
   87     assert {"hello" eq [lindex $msg 2]}
   88 
   89     $subscribeclient close
   90 }
   91 
   92 test "Migrate a slot, verify client receives sunsubscribe on replica serving the slot." {
   93 
   94     # Setup the to and from node
   95     set channelname mychannel1
   96     set slot [$cluster cluster keyslot $channelname]
   97     array set nodefrom [$cluster masternode_for_slot $slot]
   98     array set nodeto [$cluster masternode_notfor_slot $slot]
   99 
  100     # Get replica node serving slot (mychannel) to connect a client.
  101     set replicanodeinfo [$cluster cluster replicas $nodefrom(id)]
  102     set args [split $replicanodeinfo " "]
  103     set addr [lindex [split [lindex $args 1] @] 0]
  104     set replicahost [lindex [split $addr :] 0]
  105     set replicaport [lindex [split $addr :] 1]
  106     set subscribeclient [redis_deferring_client_by_addr $replicahost $replicaport]
  107 
  108     $subscribeclient deferred 1
  109     $subscribeclient ssubscribe $channelname
  110     $subscribeclient read
  111 
  112     assert_equal {OK} [$nodefrom(link) cluster setslot $slot migrating $nodeto(id)]
  113     assert_equal {OK} [$nodeto(link) cluster setslot $slot importing $nodefrom(id)]
  114 
  115     # Verify subscribe is still valid, able to receive messages.
  116     $nodefrom(link) spublish $channelname hello
  117     assert_equal {smessage mychannel1 hello} [$subscribeclient read]
  118 
  119     assert_equal {OK} [$nodefrom(link) cluster setslot $slot node $nodeto(id)]
  120     assert_equal {OK} [$nodeto(link) cluster setslot $slot node $nodeto(id)]
  121 
  122     set msg [$subscribeclient read]
  123     assert {"sunsubscribe" eq [lindex $msg 0]}
  124     assert {$channelname eq [lindex $msg 1]}
  125     assert {"0" eq [lindex $msg 2]}
  126 
  127     $subscribeclient close
  128 }
  129 
  130 test "Delete a slot, verify sunsubscribe message" {
  131     set channelname ch2
  132     set slot [$cluster cluster keyslot $channelname]
  133 
  134     array set primary_client [$cluster masternode_for_slot $slot]
  135 
  136     set subscribeclient [redis_deferring_client_by_addr $primary_client(host) $primary_client(port)]
  137     $subscribeclient deferred 1
  138     $subscribeclient ssubscribe $channelname
  139     $subscribeclient read
  140 
  141     $primary_client(link) cluster DELSLOTS $slot
  142 
  143     set msg [$subscribeclient read]
  144     assert {"sunsubscribe" eq [lindex $msg 0]}
  145     assert {$channelname eq [lindex $msg 1]}
  146     assert {"0" eq [lindex $msg 2]}
  147     
  148     $subscribeclient close
  149 }
  150 
  151 test "Reset cluster, verify sunsubscribe message" {
  152     set channelname ch4
  153     set slot [$cluster cluster keyslot $channelname]
  154 
  155     array set primary_client [$cluster masternode_for_slot $slot]
  156 
  157     set subscribeclient [redis_deferring_client_by_addr $primary_client(host) $primary_client(port)]
  158     $subscribeclient deferred 1
  159     $subscribeclient ssubscribe $channelname
  160     $subscribeclient read
  161 
  162     $cluster cluster reset HARD
  163 
  164     set msg [$subscribeclient read]
  165     assert {"sunsubscribe" eq [lindex $msg 0]}
  166     assert {$channelname eq [lindex $msg 1]}
  167     assert {"0" eq [lindex $msg 2]}
  168     
  169     $cluster close
  170     $subscribeclient close
  171 }