1# Optimize CLUSTER NODES command by generating all nodes slot topology firstly
2
3source "../tests/includes/init-tests.tcl"
4
5proc cluster_allocate_with_continuous_slots {n} {
6    set slot 16383
7    set avg [expr ($slot+1) / $n]
8    while {$slot >= 0} {
9        set node [expr $slot/$avg >= $n ? $n-1 : $slot/$avg]
10        lappend slots_$node $slot
11        incr slot -1
12    }
13    for {set j 0} {$j < $n} {incr j} {
14        R $j cluster addslots {*}[set slots_${j}]
15    }
16}
17
18proc cluster_create_with_continuous_slots {masters slaves} {
19    cluster_allocate_with_continuous_slots $masters
20    if {$slaves} {
21        cluster_allocate_slaves $masters $slaves
22    }
23    assert_cluster_state ok
24}
25
26test "Create a 2 nodes cluster" {
27    cluster_create_with_continuous_slots 2 2
28}
29
30test "Cluster should start ok" {
31    assert_cluster_state ok
32}
33
34set master1 [Rn 0]
35set master2 [Rn 1]
36
37test "Continuous slots distribution" {
38    assert_match "* 0-8191*" [$master1 CLUSTER NODES]
39    assert_match "* 8192-16383*" [$master2 CLUSTER NODES]
40    assert_match "*0 8191*" [$master1 CLUSTER SLOTS]
41    assert_match "*8192 16383*" [$master2 CLUSTER SLOTS]
42
43    $master1 CLUSTER DELSLOTS 4096
44    assert_match "* 0-4095 4097-8191*" [$master1 CLUSTER NODES]
45    assert_match "*0 4095*4097 8191*" [$master1 CLUSTER SLOTS]
46
47
48    $master2 CLUSTER DELSLOTS 12288
49    assert_match "* 8192-12287 12289-16383*" [$master2 CLUSTER NODES]
50    assert_match "*8192 12287*12289 16383*" [$master2 CLUSTER SLOTS]
51}
52
53test "Discontinuous slots distribution" {
54    # Remove middle slots
55    $master1 CLUSTER DELSLOTS 4092 4094
56    assert_match "* 0-4091 4093 4095 4097-8191*" [$master1 CLUSTER NODES]
57    assert_match "*0 4091*4093 4093*4095 4095*4097 8191*" [$master1 CLUSTER SLOTS]
58    $master2 CLUSTER DELSLOTS 12284 12286
59    assert_match "* 8192-12283 12285 12287 12289-16383*" [$master2 CLUSTER NODES]
60    assert_match "*8192 12283*12285 12285*12287 12287*12289 16383*" [$master2 CLUSTER SLOTS]
61
62    # Remove head slots
63    $master1 CLUSTER DELSLOTS 0 2
64    assert_match "* 1 3-4091 4093 4095 4097-8191*" [$master1 CLUSTER NODES]
65    assert_match "*1 1*3 4091*4093 4093*4095 4095*4097 8191*" [$master1 CLUSTER SLOTS]
66
67    # Remove tail slots
68    $master2 CLUSTER DELSLOTS 16380 16382 16383
69    assert_match "* 8192-12283 12285 12287 12289-16379 16381*" [$master2 CLUSTER NODES]
70    assert_match "*8192 12283*12285 12285*12287 12287*12289 16379*16381 16381*" [$master2 CLUSTER SLOTS]
71}
72