"Fossies" - the Fresh Open Source Software Archive

Member "redis-7.0.5/tests/cluster/tests/27-endpoints.tcl" (21 Sep 2022, 8439 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 
    3 # Isolate a node from the cluster and give it a new nodeid
    4 proc isolate_node {id} {
    5     set node_id [R $id CLUSTER MYID]
    6     R 6 CLUSTER RESET HARD
    7     for {set j 0} {$j < 20} {incr j} {
    8         if { $j eq $id } {
    9             continue
   10         }
   11         R $j CLUSTER FORGET $node_id
   12     }
   13 }
   14 
   15 proc get_slot_field {slot_output shard_id node_id attrib_id} {
   16     return [lindex [lindex [lindex $slot_output $shard_id] $node_id] $attrib_id]
   17 }
   18 
   19 test "Create a 6 nodes cluster" {
   20     cluster_create_with_continuous_slots 3 3
   21 }
   22 
   23 test "Cluster should start ok" {
   24     assert_cluster_state ok
   25     wait_for_cluster_propagation
   26 }
   27 
   28 test "Set cluster hostnames and verify they are propagated" {
   29     for {set j 0} {$j < $::cluster_master_nodes + $::cluster_replica_nodes} {incr j} {
   30         R $j config set cluster-announce-hostname "host-$j.com"
   31     }
   32     
   33     wait_for_condition 50 100 {
   34         [are_hostnames_propagated "host-*.com"] eq 1
   35     } else {
   36         fail "cluster hostnames were not propagated"
   37     }
   38 
   39     # Now that everything is propagated, assert everyone agrees
   40     wait_for_cluster_propagation
   41 }
   42 
   43 test "Update hostnames and make sure they are all eventually propagated" {
   44     for {set j 0} {$j < $::cluster_master_nodes + $::cluster_replica_nodes} {incr j} {
   45         R $j config set cluster-announce-hostname "host-updated-$j.com"
   46     }
   47     
   48     wait_for_condition 50 100 {
   49         [are_hostnames_propagated "host-updated-*.com"] eq 1
   50     } else {
   51         fail "cluster hostnames were not propagated"
   52     }
   53 
   54     # Now that everything is propagated, assert everyone agrees
   55     wait_for_cluster_propagation
   56 }
   57 
   58 test "Remove hostnames and make sure they are all eventually propagated" {
   59     for {set j 0} {$j < $::cluster_master_nodes + $::cluster_replica_nodes} {incr j} {
   60         R $j config set cluster-announce-hostname ""
   61     }
   62     
   63     wait_for_condition 50 100 {
   64         [are_hostnames_propagated ""] eq 1
   65     } else {
   66         fail "cluster hostnames were not propagated"
   67     }
   68 
   69     # Now that everything is propagated, assert everyone agrees
   70     wait_for_cluster_propagation
   71 }
   72 
   73 test "Verify cluster-preferred-endpoint-type behavior for redirects and info" {
   74     R 0 config set cluster-announce-hostname "me.com"
   75     R 1 config set cluster-announce-hostname ""
   76     R 2 config set cluster-announce-hostname "them.com"
   77 
   78     wait_for_cluster_propagation
   79 
   80     # Verify default behavior
   81     set slot_result [R 0 cluster slots]
   82     assert_equal "" [lindex [get_slot_field $slot_result 0 2 0] 1]
   83     assert_equal "" [lindex [get_slot_field $slot_result 2 2 0] 1]
   84     assert_equal "hostname" [lindex [get_slot_field $slot_result 0 2 3] 0]
   85     assert_equal "me.com" [lindex [get_slot_field $slot_result 0 2 3] 1]
   86     assert_equal "hostname" [lindex [get_slot_field $slot_result 2 2 3] 0]
   87     assert_equal "them.com" [lindex [get_slot_field $slot_result 2 2 3] 1]
   88 
   89     # Redirect will use the IP address
   90     catch {R 0 set foo foo} redir_err
   91     assert_match "MOVED * 127.0.0.1:*" $redir_err
   92 
   93     # Verify prefer hostname behavior
   94     R 0 config set cluster-preferred-endpoint-type hostname
   95 
   96     set slot_result [R 0 cluster slots]
   97     assert_equal "me.com" [get_slot_field $slot_result 0 2 0]
   98     assert_equal "them.com" [get_slot_field $slot_result 2 2 0]
   99 
  100     # Redirect should use hostname
  101     catch {R 0 set foo foo} redir_err
  102     assert_match "MOVED * them.com:*" $redir_err
  103 
  104     # Redirect to an unknown hostname returns ?
  105     catch {R 0 set barfoo bar} redir_err
  106     assert_match "MOVED * ?:*" $redir_err
  107 
  108     # Verify unknown hostname behavior
  109     R 0 config set cluster-preferred-endpoint-type unknown-endpoint
  110 
  111     # Verify default behavior
  112     set slot_result [R 0 cluster slots]
  113     assert_equal "ip" [lindex [get_slot_field $slot_result 0 2 3] 0]
  114     assert_equal "127.0.0.1" [lindex [get_slot_field $slot_result 0 2 3] 1]
  115     assert_equal "ip" [lindex [get_slot_field $slot_result 2 2 3] 0]
  116     assert_equal "127.0.0.1" [lindex [get_slot_field $slot_result 2 2 3] 1]
  117     assert_equal "ip" [lindex [get_slot_field $slot_result 1 2 3] 0]
  118     assert_equal "127.0.0.1" [lindex [get_slot_field $slot_result 1 2 3] 1]
  119     # Not required by the protocol, but IP comes before hostname
  120     assert_equal "hostname" [lindex [get_slot_field $slot_result 0 2 3] 2]
  121     assert_equal "me.com" [lindex [get_slot_field $slot_result 0 2 3] 3]
  122     assert_equal "hostname" [lindex [get_slot_field $slot_result 2 2 3] 2]
  123     assert_equal "them.com" [lindex [get_slot_field $slot_result 2 2 3] 3]
  124 
  125     # This node doesn't have a hostname
  126     assert_equal 2 [llength [get_slot_field $slot_result 1 2 3]]
  127 
  128     # Redirect should use empty string
  129     catch {R 0 set foo foo} redir_err
  130     assert_match "MOVED * :*" $redir_err
  131 
  132     R 0 config set cluster-preferred-endpoint-type ip
  133 }
  134 
  135 test "Verify the nodes configured with prefer hostname only show hostname for new nodes" {
  136     # Have everyone forget node 6 and isolate it from the cluster.
  137     isolate_node 6
  138 
  139     # Set hostnames for the primaries, now that the node is isolated
  140     R 0 config set cluster-announce-hostname "shard-1.com"
  141     R 1 config set cluster-announce-hostname "shard-2.com"
  142     R 2 config set cluster-announce-hostname "shard-3.com"
  143 
  144     # Prevent Node 0 and Node 6 from properly meeting,
  145     # they'll hang in the handshake phase. This allows us to 
  146     # test the case where we "know" about it but haven't
  147     # successfully retrieved information about it yet.
  148     R 0 DEBUG DROP-CLUSTER-PACKET-FILTER 0
  149     R 6 DEBUG DROP-CLUSTER-PACKET-FILTER 0
  150 
  151     # Have a replica meet the isolated node
  152     R 3 cluster meet 127.0.0.1 [get_instance_attrib redis 6 port]
  153 
  154     # Wait for the isolated node to learn about the rest of the cluster,
  155     # which correspond to a single entry in cluster nodes. Note this
  156     # doesn't mean the isolated node has successfully contacted each
  157     # node.
  158     wait_for_condition 50 100 {
  159         [llength [split [R 6 CLUSTER NODES] "\n"]] eq 21 
  160     } else {
  161         fail "Isolated node didn't learn about the rest of the cluster *"
  162     }
  163 
  164     # Now, we wait until the two nodes that aren't filtering packets
  165     # to accept our isolated nodes connections. At this point they will
  166     # start showing up in cluster slots. 
  167     wait_for_condition 50 100 {
  168         [llength [R 6 CLUSTER SLOTS]] eq 2
  169     } else {
  170         fail "Node did not learn about the 2 shards it can talk to"
  171     }
  172     set slot_result [R 6 CLUSTER SLOTS]
  173     assert_equal [lindex [get_slot_field $slot_result 0 2 3] 1] "shard-2.com"
  174     assert_equal [lindex [get_slot_field $slot_result 1 2 3] 1] "shard-3.com"
  175 
  176     # Also make sure we know about the isolated primary, we 
  177     # just can't reach it.
  178     set primary_id [R 0 CLUSTER MYID]
  179     assert_match "*$primary_id*" [R 6 CLUSTER NODES]
  180 
  181     # Stop dropping cluster packets, and make sure everything
  182     # stabilizes
  183     R 0 DEBUG DROP-CLUSTER-PACKET-FILTER -1
  184     R 6 DEBUG DROP-CLUSTER-PACKET-FILTER -1
  185 
  186     wait_for_condition 50 100 {
  187         [llength [R 6 CLUSTER SLOTS]] eq 3
  188     } else {
  189         fail "Node did not learn about the 2 shards it can talk to"
  190     }
  191     set slot_result [R 6 CLUSTER SLOTS]
  192     assert_equal [lindex [get_slot_field $slot_result 0 2 3] 1] "shard-1.com"
  193     assert_equal [lindex [get_slot_field $slot_result 1 2 3] 1] "shard-2.com"
  194     assert_equal [lindex [get_slot_field $slot_result 2 2 3] 1] "shard-3.com"
  195 }
  196 
  197 test "Test restart will keep hostname information" {
  198     # Set a new hostname, reboot and make sure it sticks
  199     R 0 config set cluster-announce-hostname "restart-1.com"
  200     # Store the hostname in the config
  201     R 0 config rewrite
  202     kill_instance redis 0
  203     restart_instance redis 0
  204     set slot_result [R 0 CLUSTER SLOTS]
  205     assert_equal [lindex [get_slot_field $slot_result 0 2 3] 1] "restart-1.com"
  206 
  207     # As a sanity check, make sure everyone eventually agrees
  208     wait_for_cluster_propagation
  209 }
  210 
  211 test "Test hostname validation" {
  212     catch {R 0 config set cluster-announce-hostname [string repeat x 256]} err
  213     assert_match "*Hostnames must be less than 256 characters*" $err
  214     catch {R 0 config set cluster-announce-hostname "?.com"} err
  215     assert_match "*Hostnames may only contain alphanumeric characters, hyphens or dots*" $err
  216 
  217     # Note this isn't a valid hostname, but it passes our internal validation
  218     R 0 config set cluster-announce-hostname "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-."
  219 }