|
| 1 | +//go:build !windows |
| 2 | + |
| 3 | +/* |
| 4 | +Copyright 2024 The Vitess Authors. |
| 5 | +
|
| 6 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +you may not use this file except in compliance with the License. |
| 8 | +You may obtain a copy of the License at |
| 9 | +
|
| 10 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +
|
| 12 | +Unless required by applicable law or agreed to in writing, software |
| 13 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +See the License for the specific language governing permissions and |
| 16 | +limitations under the License. |
| 17 | +*/ |
| 18 | + |
| 19 | +package reuseport |
| 20 | + |
| 21 | +import ( |
| 22 | + "context" |
| 23 | + _ "embed" |
| 24 | + "flag" |
| 25 | + "os" |
| 26 | + "testing" |
| 27 | + |
| 28 | + "github.com/stretchr/testify/require" |
| 29 | + |
| 30 | + "vitess.io/vitess/go/mysql" |
| 31 | + "vitess.io/vitess/go/test/endtoend/cluster" |
| 32 | + "vitess.io/vitess/go/test/endtoend/utils" |
| 33 | + "vitess.io/vitess/go/vt/log" |
| 34 | +) |
| 35 | + |
| 36 | +var ( |
| 37 | + keyspaceName = "ks" |
| 38 | + cell = "zone-1" |
| 39 | + |
| 40 | + //go:embed schema.sql |
| 41 | + schemaSQL string |
| 42 | +) |
| 43 | + |
| 44 | +func TestMain(m *testing.M) { |
| 45 | + flag.Parse() |
| 46 | + os.Exit(m.Run()) |
| 47 | +} |
| 48 | + |
| 49 | +func setupCluster(t *testing.T) (*cluster.LocalProcessCluster, mysql.ConnParams) { |
| 50 | + clusterInstance := cluster.NewCluster(cell, "localhost") |
| 51 | + |
| 52 | + // Start topo server |
| 53 | + err := clusterInstance.StartTopo() |
| 54 | + require.NoError(t, err) |
| 55 | + |
| 56 | + // Start keyspace |
| 57 | + keyspace := &cluster.Keyspace{ |
| 58 | + Name: keyspaceName, |
| 59 | + SchemaSQL: schemaSQL, |
| 60 | + } |
| 61 | + err = clusterInstance.StartUnshardedKeyspace(*keyspace, 0, false) |
| 62 | + require.NoError(t, err) |
| 63 | + |
| 64 | + // Start vtgate |
| 65 | + clusterInstance.VtGateExtraArgs = append(clusterInstance.VtGateExtraArgs, "--reuse-port") |
| 66 | + err = clusterInstance.StartVtgate() |
| 67 | + require.NoError(t, err) |
| 68 | + |
| 69 | + vtParams := clusterInstance.GetVTParams(keyspaceName) |
| 70 | + return clusterInstance, vtParams |
| 71 | +} |
| 72 | + |
| 73 | +func start(t *testing.T, vtParams mysql.ConnParams) (*mysql.Conn, func()) { |
| 74 | + vtConn, err := mysql.Connect(context.Background(), &vtParams) |
| 75 | + require.NoError(t, err) |
| 76 | + |
| 77 | + deleteAll := func() { |
| 78 | + _, _ = utils.ExecAllowError(t, vtConn, "set workload = oltp") |
| 79 | + |
| 80 | + tables := []string{"t1"} |
| 81 | + for _, table := range tables { |
| 82 | + _, _ = utils.ExecAllowError(t, vtConn, "delete from "+table) |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + deleteAll() |
| 87 | + |
| 88 | + return vtConn, func() { |
| 89 | + deleteAll() |
| 90 | + vtConn.Close() |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func TestReusePort(t *testing.T) { |
| 95 | + clusterInstance, vtParams := setupCluster(t) |
| 96 | + defer clusterInstance.Teardown() |
| 97 | + |
| 98 | + // Create a connection to the first vtgate |
| 99 | + vtConn, closer := start(t, vtParams) |
| 100 | + defer closer() |
| 101 | + |
| 102 | + // Create a second vtgate with the same configuration |
| 103 | + duplicateVtGate := cluster.VtgateProcessInstance( |
| 104 | + clusterInstance.GetAndReservePort(), |
| 105 | + clusterInstance.VtgateGrpcPort, |
| 106 | + clusterInstance.VtgateMySQLPort, |
| 107 | + clusterInstance.Cell, |
| 108 | + clusterInstance.Cell, |
| 109 | + clusterInstance.Hostname, |
| 110 | + "PRIMARY", |
| 111 | + clusterInstance.TopoProcess.Port, |
| 112 | + clusterInstance.TmpDirectory, |
| 113 | + clusterInstance.VtGateExtraArgs, |
| 114 | + clusterInstance.VtGatePlannerVersion) |
| 115 | + // Unix domain sockets do not support multiplexing |
| 116 | + duplicateVtGate.MySQLServerSocketPath = "" |
| 117 | + err := duplicateVtGate.Setup() |
| 118 | + require.NoError(t, err) |
| 119 | + defer func() { |
| 120 | + if err := duplicateVtGate.TearDown(); err != nil { |
| 121 | + log.Errorf("Error in vtgate teardown: %v", err) |
| 122 | + } |
| 123 | + }() |
| 124 | + |
| 125 | + // Should be able to connect to the first vtgate |
| 126 | + _, err = vtConn.ExecuteFetch("select id1 from t1", 1, false) |
| 127 | + require.NoError(t, err) |
| 128 | + |
| 129 | + // Tear down the first vtgate |
| 130 | + err = clusterInstance.VtgateProcess.TearDown() |
| 131 | + require.NoError(t, err) |
| 132 | + require.True(t, clusterInstance.VtgateProcess.IsShutdown()) |
| 133 | + |
| 134 | + // Should fail since the vtgate has stopped |
| 135 | + _, err = vtConn.ExecuteFetch("select id1 from t1", 1, false) |
| 136 | + require.Error(t, err, "first vtgate should be stopped and should not serve requests") |
| 137 | + |
| 138 | + // Create a second connection with the same parameters, which will |
| 139 | + // now go to the duplicate vtgate |
| 140 | + vtConn2, err := mysql.Connect(context.Background(), &vtParams) |
| 141 | + require.NoError(t, err, "second vtgate should handle the connection") |
| 142 | + defer vtConn2.Close() |
| 143 | + |
| 144 | + // Should be able to fetch from the same host:port on the duplicate vtgate |
| 145 | + _, err = vtConn2.ExecuteFetch("select id1 from t1", 1, false) |
| 146 | + require.NoError(t, err, "second vtgate should serve requests") |
| 147 | +} |
0 commit comments