|
| 1 | +/* |
| 2 | +Copyright 2025 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 sessionbalancer |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/stretchr/testify/require" |
| 24 | + |
| 25 | + "vitess.io/vitess/go/mysql" |
| 26 | +) |
| 27 | + |
| 28 | +// TestSessionBalancer validates that the session balancer consistently routes |
| 29 | +// queries for the same session to the same tablet. |
| 30 | +func TestSessionBalancer(t *testing.T) { |
| 31 | + // Get connections that route to different tablets |
| 32 | + conn1, conn2, id1, id2 := connections(t) |
| 33 | + |
| 34 | + defer conn1.Close() |
| 35 | + defer conn2.Close() |
| 36 | + |
| 37 | + // Validate that each connection consistently returns the same server ID |
| 38 | + for range 20 { |
| 39 | + newID1 := serverID(t, conn1) |
| 40 | + require.Equal(t, id1, newID1) |
| 41 | + |
| 42 | + newID2 := serverID(t, conn2) |
| 43 | + require.Equal(t, id2, newID2) |
| 44 | + |
| 45 | + require.NotEqual(t, newID2, newID1) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +// connections returns two connections that should route to different servers. |
| 50 | +func connections(t *testing.T) (conn1, conn2 *mysql.Conn, id1, id2 string) { |
| 51 | + t.Helper() |
| 52 | + |
| 53 | + // Keep creating connections until we find two that route to different server IDs |
| 54 | + vtParams.DbName = uks + "@replica" |
| 55 | + for { |
| 56 | + conn1, err := mysql.Connect(context.Background(), &vtParams) |
| 57 | + require.NoError(t, err) |
| 58 | + |
| 59 | + conn2, err = mysql.Connect(context.Background(), &vtParams) |
| 60 | + require.NoError(t, err) |
| 61 | + |
| 62 | + id1 = serverID(t, conn1) |
| 63 | + id2 = serverID(t, conn2) |
| 64 | + |
| 65 | + // Break if we found connections with different server IDs |
| 66 | + if id1 != id2 { |
| 67 | + return conn1, conn2, id1, id2 |
| 68 | + } |
| 69 | + |
| 70 | + // If not, close the connections and try again |
| 71 | + conn1.Close() |
| 72 | + conn2.Close() |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +// serverID runs a `SELECT @@server_id` on the given connection and returns the |
| 77 | +// server's ID. |
| 78 | +func serverID(t *testing.T, conn *mysql.Conn) string { |
| 79 | + t.Helper() |
| 80 | + |
| 81 | + result1, err := conn.ExecuteFetch("SELECT @@server_uuid", 1, false) |
| 82 | + require.NoError(t, err) |
| 83 | + |
| 84 | + tablet1Bytes, err := result1.Rows[0][0].ToBytes() |
| 85 | + require.NoError(t, err) |
| 86 | + |
| 87 | + return string(tablet1Bytes) |
| 88 | +} |
0 commit comments