|
| 1 | +/* |
| 2 | +Copyright 2024 The Vitess Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package balancer |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/stretchr/testify/assert" |
| 23 | + "github.com/stretchr/testify/require" |
| 24 | + |
| 25 | + "vitess.io/vitess/go/vt/discovery" |
| 26 | + querypb "vitess.io/vitess/go/vt/proto/query" |
| 27 | + topodatapb "vitess.io/vitess/go/vt/proto/topodata" |
| 28 | +) |
| 29 | + |
| 30 | +func TestRandomBalancerUniformDistribution(t *testing.T) { |
| 31 | + // Test with uneven distribution of tablets across cells to verify |
| 32 | + // that random mode ignores cell affinity and treats all tablets equally |
| 33 | + tablets := []*discovery.TabletHealth{ |
| 34 | + createTestTablet("cell1"), |
| 35 | + createTestTablet("cell1"), |
| 36 | + createTestTablet("cell1"), |
| 37 | + createTestTablet("cell2"), |
| 38 | + createTestTablet("cell3"), |
| 39 | + createTestTablet("cell3"), |
| 40 | + } |
| 41 | + |
| 42 | + target := &querypb.Target{Keyspace: "k", Shard: "s", TabletType: topodatapb.TabletType_REPLICA} |
| 43 | + // Use localCell = "cell1" to verify it doesn't get preferential treatment |
| 44 | + b := newRandomBalancer("cell1", []string{}) |
| 45 | + |
| 46 | + const numPicks = 60000 |
| 47 | + pickCounts := make(map[uint32]int) |
| 48 | + |
| 49 | + for i := 0; i < numPicks; i++ { |
| 50 | + th := b.Pick(target, tablets) |
| 51 | + require.NotNil(t, th, "Pick should not return nil") |
| 52 | + pickCounts[th.Tablet.Alias.Uid]++ |
| 53 | + } |
| 54 | + |
| 55 | + // Each individual tablet should be picked with 1/N probability |
| 56 | + expectedPerTablet := numPicks / len(tablets) |
| 57 | + for _, tablet := range tablets { |
| 58 | + count := pickCounts[tablet.Tablet.Alias.Uid] |
| 59 | + assert.InEpsilon(t, expectedPerTablet, count, 0.05, |
| 60 | + "tablet %d in cell %s should receive uniform picks (expected ~%d, got %d)", |
| 61 | + tablet.Tablet.Alias.Uid, tablet.Tablet.Alias.Cell, expectedPerTablet, count) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func TestRandomBalancerPickEmpty(t *testing.T) { |
| 66 | + target := &querypb.Target{Keyspace: "k", Shard: "s", TabletType: topodatapb.TabletType_REPLICA} |
| 67 | + b := newRandomBalancer("cell1", []string{}) |
| 68 | + |
| 69 | + th := b.Pick(target, []*discovery.TabletHealth{}) |
| 70 | + assert.Nil(t, th, "Pick should return nil for empty tablet list") |
| 71 | +} |
| 72 | + |
| 73 | +func TestRandomBalancerPickSingle(t *testing.T) { |
| 74 | + tablets := []*discovery.TabletHealth{ |
| 75 | + createTestTablet("cell1"), |
| 76 | + } |
| 77 | + |
| 78 | + target := &querypb.Target{Keyspace: "k", Shard: "s", TabletType: topodatapb.TabletType_REPLICA} |
| 79 | + b := newRandomBalancer("cell1", []string{}) |
| 80 | + |
| 81 | + // Pick multiple times, should always return the same tablet |
| 82 | + for i := 0; i < 100; i++ { |
| 83 | + th := b.Pick(target, tablets) |
| 84 | + require.NotNil(t, th, "Pick should not return nil") |
| 85 | + assert.Equal(t, tablets[0].Tablet.Alias.Uid, th.Tablet.Alias.Uid, |
| 86 | + "Pick should return the only available tablet") |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func TestRandomBalancerFactory(t *testing.T) { |
| 91 | + // Test that the factory creates a random balancer correctly |
| 92 | + b, err := NewTabletBalancer("random", "cell1", []string{"cell1", "cell2"}) |
| 93 | + require.NoError(t, err) |
| 94 | + require.NotNil(t, b) |
| 95 | + |
| 96 | + // Verify it's actually a randomBalancer |
| 97 | + _, ok := b.(*randomBalancer) |
| 98 | + assert.True(t, ok, "factory should create a randomBalancer") |
| 99 | +} |
| 100 | + |
| 101 | +func TestBalancerFactoryInvalidModes(t *testing.T) { |
| 102 | + // Test that "cell" mode returns an error (should be handled by gateway) |
| 103 | + b, err := NewTabletBalancer("cell", "cell1", []string{}) |
| 104 | + assert.Error(t, err) |
| 105 | + assert.Nil(t, b) |
| 106 | + assert.Contains(t, err.Error(), "cell mode should be handled by the gateway") |
| 107 | + |
| 108 | + // Test that an invalid mode returns an error |
| 109 | + b, err = NewTabletBalancer("invalid", "cell1", []string{}) |
| 110 | + assert.Error(t, err) |
| 111 | + assert.Nil(t, b) |
| 112 | + assert.Contains(t, err.Error(), "unsupported balancer mode") |
| 113 | +} |
| 114 | + |
| 115 | +func TestRandomBalancerCellFiltering(t *testing.T) { |
| 116 | + // Create tablets in multiple cells |
| 117 | + tablets := []*discovery.TabletHealth{ |
| 118 | + createTestTablet("cell1"), |
| 119 | + createTestTablet("cell1"), |
| 120 | + createTestTablet("cell2"), |
| 121 | + createTestTablet("cell2"), |
| 122 | + createTestTablet("cell3"), |
| 123 | + createTestTablet("cell3"), |
| 124 | + } |
| 125 | + |
| 126 | + target := &querypb.Target{Keyspace: "k", Shard: "s", TabletType: topodatapb.TabletType_REPLICA} |
| 127 | + |
| 128 | + // Create balancer that only considers cell1 and cell2 |
| 129 | + b := newRandomBalancer("cell1", []string{"cell1", "cell2"}) |
| 130 | + require.NotNil(t, b) |
| 131 | + |
| 132 | + const numPicks = 10000 |
| 133 | + pickCounts := make(map[string]int) |
| 134 | + |
| 135 | + for i := 0; i < numPicks; i++ { |
| 136 | + th := b.Pick(target, tablets) |
| 137 | + require.NotNil(t, th) |
| 138 | + pickCounts[th.Tablet.Alias.Cell]++ |
| 139 | + } |
| 140 | + |
| 141 | + // Should only pick from cell1 and cell2, never cell3 |
| 142 | + assert.Greater(t, pickCounts["cell1"], 0, "should pick from cell1") |
| 143 | + assert.Greater(t, pickCounts["cell2"], 0, "should pick from cell2") |
| 144 | + assert.Equal(t, 0, pickCounts["cell3"], "should never pick from cell3") |
| 145 | + |
| 146 | + // Each filtered cell should get approximately half the picks |
| 147 | + expectedPerCell := numPicks / 2 |
| 148 | + assert.InEpsilon(t, expectedPerCell, pickCounts["cell1"], 0.1, |
| 149 | + "cell1 should get ~50%% of picks") |
| 150 | + assert.InEpsilon(t, expectedPerCell, pickCounts["cell2"], 0.1, |
| 151 | + "cell2 should get ~50%% of picks") |
| 152 | +} |
0 commit comments