Skip to content

Commit

Permalink
Vtgateproxy E2E tests (#364)
Browse files Browse the repository at this point in the history
* Add end to end tests for vtgateproxy

* Fix linter warnings and unhandled errors

---------

Signed-off-by: Riley Laine <[email protected]>
Signed-off-by: Esme Lamb <[email protected]>
  • Loading branch information
rjlaine authored and dedelala committed Nov 12, 2024
1 parent 5673ad0 commit 4a53e79
Show file tree
Hide file tree
Showing 9 changed files with 1,261 additions and 24 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ require (
github.com/evanphx/json-patch v4.9.0+incompatible
github.com/fsnotify/fsnotify v1.6.0
github.com/go-martini/martini v0.0.0-20170121215854-22fa46961aab
github.com/go-sql-driver/mysql v1.7.0
github.com/go-sql-driver/mysql v1.8.1
github.com/golang/glog v1.1.2
github.com/golang/mock v1.6.0
github.com/golang/protobuf v1.5.2
Expand Down Expand Up @@ -129,6 +129,7 @@ require (

require (
cloud.google.com/go v0.93.3 // indirect
filippo.io/edwards25519 v1.1.0 // indirect
github.com/go-chi/chi/v5 v5.0.7 // indirect
github.com/rantav/go-grpc-channelz v0.0.4
)
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RX
cloud.google.com/go/storage v1.10.0 h1:STgFzyU5/8miMl0//zKh2aQeTyeaUH3WN9bSUiJ09bA=
cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
github.com/AdaLogics/go-fuzz-headers v0.0.0-20230106234847-43070de90fa1 h1:EKPd1INOIyr5hWOWhvpmQpY6tKjeG0hT1s3AMC/9fic=
github.com/AdaLogics/go-fuzz-headers v0.0.0-20230106234847-43070de90fa1/go.mod h1:VzwV+t+dZ9j/H867F1M2ziD+yLHtB46oM35FxxMJ4d0=
github.com/Azure/azure-pipeline-go v0.2.3 h1:7U9HBg1JFK3jHl5qmo4CTZKFTVgMwdFHMVtCdfBE21U=
Expand Down Expand Up @@ -290,8 +292,8 @@ github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh
github.com/go-openapi/validate v0.18.0/go.mod h1:Uh4HdOzKt19xGIGm1qHf/ofbX1YQ4Y+MYsct2VUrAJ4=
github.com/go-openapi/validate v0.19.2/go.mod h1:1tRCw7m3jtI8eNWEEliiAqUIcBztB2KDnRCRMUi7GTA=
github.com/go-openapi/validate v0.19.5/go.mod h1:8DJv2CVJQ6kGNpFW6eV9N3JviE1C85nY1c2z52x1Gk4=
github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc=
github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y=
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
Expand Down
235 changes: 235 additions & 0 deletions go/test/endtoend/vtgateproxytest/failure_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
/*
Copyright 2024 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
This tests select/insert using the unshared keyspace added in main_test
*/
package vtgateproxytest

import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"strconv"
"testing"

_ "github.com/go-sql-driver/mysql"
"github.com/stretchr/testify/assert"

"vitess.io/vitess/go/mysql"
"vitess.io/vitess/go/test/endtoend/cluster"
"vitess.io/vitess/go/test/endtoend/utils"
"vitess.io/vitess/go/vt/log"
)

func TestVtgateProxyVtgateFailureRoundRobin(t *testing.T) {
defer cluster.PanicHandler(t)

// insert test value
func() {
conn, err := mysql.Connect(context.Background(), &vtParams)
if err != nil {
t.Fatal(err)
}
defer conn.Close()

utils.Exec(t, conn, "insert into customer(id, email) values(1, 'email1')")
}()

const targetAffinity = "use1-az1"
const vtgateCount = 4
const vtgateproxyConnections = 4

vtgates, err := startAdditionalVtgates(vtgateCount)
if err != nil {
t.Fatal(err)
}
defer teardownVtgates(vtgates)

vtgateHostsFile := filepath.Join(clusterInstance.TmpDirectory, "hosts")
var config []map[string]string

for i, vtgate := range vtgates {
config = append(config, map[string]string{
"host": fmt.Sprintf("vtgate%v", i),
"address": clusterInstance.Hostname,
"grpc": strconv.Itoa(vtgate.GrpcPort),
"az_id": targetAffinity,
"type": "pool1",
})
}

b, err := json.Marshal(config)
if err != nil {
t.Fatal(err)
}
if err := os.WriteFile(vtgateHostsFile, b, 0644); err != nil {
t.Fatal(err)
}

vtgateproxyHTTPPort := clusterInstance.GetAndReservePort()
vtgateproxyGrpcPort := clusterInstance.GetAndReservePort()
vtgateproxyMySQLPort := clusterInstance.GetAndReservePort()

vtgateproxyProcInstance := NewVtgateProxyProcess(
clusterInstance.TmpDirectory,
vtgateHostsFile,
targetAffinity,
"round_robin",
vtgateproxyConnections,
vtgateproxyHTTPPort,
vtgateproxyGrpcPort,
vtgateproxyMySQLPort,
)
if err := vtgateproxyProcInstance.Setup(); err != nil {
t.Fatal(err)
}
defer vtgateproxyProcInstance.Teardown()

conn, err := vtgateproxyProcInstance.GetMySQLConn("pool1", targetAffinity)
if err != nil {
t.Fatal(err)
}
defer conn.Close()

if err := conn.Ping(); err != nil {
t.Fatal(err)
}

log.Info("Stopping 1 vtgate")
err = vtgates[0].TearDown()
if err != nil {
t.Fatal(err)
}

log.Info("Reading test value with one stopped vtgate")
for i := 0; i < vtgateproxyConnections; i++ {
result, err := selectHelper[customerEntry](context.Background(), conn, "select id, email from customer")
if err != nil {
t.Fatal(err)
}

assert.Equal(t, []customerEntry{{1, "email1"}}, result)
}
}

func TestVtgateProxyVtgateFailureFirstReady(t *testing.T) {
defer cluster.PanicHandler(t)

// insert test value
func() {
conn, err := mysql.Connect(context.Background(), &vtParams)
if err != nil {
t.Fatal(err)
}
defer conn.Close()

utils.Exec(t, conn, "insert into customer(id, email) values(1, 'email1')")
}()

const targetAffinity = "use1-az1"
const vtgateCount = 4
const vtgateproxyConnections = 4

vtgates, err := startAdditionalVtgates(vtgateCount)
if err != nil {
t.Fatal(err)
}
defer teardownVtgates(vtgates)

vtgateHostsFile := filepath.Join(clusterInstance.TmpDirectory, "hosts")
var config []map[string]string

for i, vtgate := range vtgates {
config = append(config, map[string]string{
"host": fmt.Sprintf("vtgate%v", i),
"address": clusterInstance.Hostname,
"grpc": strconv.Itoa(vtgate.GrpcPort),
"az_id": targetAffinity,
"type": "pool1",
})
}

b, err := json.Marshal(config)
if err != nil {
t.Fatal(err)
}
if err := os.WriteFile(vtgateHostsFile, b, 0644); err != nil {
t.Fatal(err)
}

vtgateproxyHTTPPort := clusterInstance.GetAndReservePort()
vtgateproxyGrpcPort := clusterInstance.GetAndReservePort()
vtgateproxyMySQLPort := clusterInstance.GetAndReservePort()

vtgateproxyProcInstance := NewVtgateProxyProcess(
clusterInstance.TmpDirectory,
vtgateHostsFile,
targetAffinity,
"first_ready",
vtgateproxyConnections,
vtgateproxyHTTPPort,
vtgateproxyGrpcPort,
vtgateproxyMySQLPort,
)
if err := vtgateproxyProcInstance.Setup(); err != nil {
t.Fatal(err)
}
defer vtgateproxyProcInstance.Teardown()

conn, err := vtgateproxyProcInstance.GetMySQLConn("pool1", targetAffinity)
if err != nil {
t.Fatal(err)
}
defer conn.Close()

if err := conn.Ping(); err != nil {
t.Fatal(err)
}

// First send some queries to the active vtgate
for i := 0; i < 10; i++ {
result, err := selectHelper[customerEntry](context.Background(), conn, "select id, email from customer")
if err != nil {
t.Fatal(err)
}

assert.Equal(t, []customerEntry{{1, "email1"}}, result)
}

// Now kill the active vtgate
for i := range vtgates {
queryCount, err := getVtgateQueryCount(vtgates[i])
if err != nil {
t.Fatal(err)
}

if queryCount.Sum() > 0 {
err = vtgates[i].TearDown()
if err != nil {
t.Fatal(err)
}
}
}

log.Info("Reading test value after killing the active vtgate")
result, err := selectHelper[customerEntry](context.Background(), conn, "select id, email from customer")
if err != nil {
t.Fatal(err)
}

assert.Equal(t, []customerEntry{{1, "email1"}}, result)
}
Loading

0 comments on commit 4a53e79

Please sign in to comment.