Skip to content

Commit

Permalink
Merge pull request #8139 from planetscale/errs-as-warns
Browse files Browse the repository at this point in the history
Improve ScatterErrorsAsWarnings functionality
  • Loading branch information
harshit-gangal authored May 20, 2021
2 parents a0670da + 7c85ced commit d51a7ee
Show file tree
Hide file tree
Showing 29 changed files with 711 additions and 235 deletions.
15 changes: 14 additions & 1 deletion go/test/endtoend/mysqlserver/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,14 @@ var (
keyspace_id bigint(20) unsigned NOT NULL,
data longblob,
primary key (id)
) Engine=InnoDB`
) Engine=InnoDB;
`
createProcSQL = `use vt_test_keyspace;
CREATE PROCEDURE testing()
BEGIN
delete from vt_insert_test;
END;
`
)

func TestMain(m *testing.M) {
Expand Down Expand Up @@ -98,6 +105,7 @@ func TestMain(m *testing.M) {
"-mysql_auth_server_impl", "static",
"-mysql_auth_server_static_file", clusterInstance.TmpDirectory + mysqlAuthServerStatic,
"-mysql_server_version", "8.0.16-7",
"-warn_sharded_only=true",
}

clusterInstance.VtTabletExtraArgs = []string{
Expand Down Expand Up @@ -126,6 +134,11 @@ func TestMain(m *testing.M) {
Pass: "testpassword1",
}

masterProcess := clusterInstance.Keyspaces[0].Shards[0].MasterTablet().VttabletProcess
if _, err := masterProcess.QueryTablet(createProcSQL, keyspaceName, false); err != nil {
return 1, err
}

return m.Run(), nil
}()
if err != nil {
Expand Down
39 changes: 19 additions & 20 deletions go/test/endtoend/mysqlserver/mysql_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,40 +142,39 @@ func TestWarnings(t *testing.T) {
ctx := context.Background()

conn, err := mysql.Connect(ctx, &vtParams)
require.Nilf(t, err, "unable to connect mysql: %v", err)
require.NoError(t, err)
defer conn.Close()

// validate warning with invalid_field error as warning
qr, err := conn.ExecuteFetch("SELECT /*vt+ SCATTER_ERRORS_AS_WARNINGS */ invalid_field from vt_insert_test;", 1, false)
require.Nilf(t, err, "select error : %v", err)
// using CALL will produce a warning saying this only works in unsharded
qr, err := conn.ExecuteFetch("CALL testing()", 1, false)
require.NoError(t, err)
assert.Empty(t, qr.Rows, "number of rows")

qr, err = conn.ExecuteFetch("SHOW WARNINGS;", 1, false)
require.Nilf(t, err, "SHOW WARNINGS; execution failed: %v", err)
require.NoError(t, err, "SHOW WARNINGS")
assert.EqualValues(t, 1, len(qr.Rows), "number of rows")
assert.Contains(t, qr.Rows[0][0].String(), "VARCHAR(\"Warning\")", qr.Rows)
assert.Contains(t, qr.Rows[0][1].String(), "UINT16(1054)", qr.Rows)
assert.Contains(t, qr.Rows[0][2].String(), "Unknown column", qr.Rows)
assert.Contains(t, qr.Rows[0][1].String(), "UINT16(1235)", qr.Rows)
assert.Contains(t, qr.Rows[0][2].String(), "'CALL' not supported in sharded mode", qr.Rows)

// validate warning with query_timeout error as warning
qr, err = conn.ExecuteFetch("SELECT /*vt+ SCATTER_ERRORS_AS_WARNINGS QUERY_TIMEOUT_MS=1 */ sleep(1) from vt_insert_test;", 1, false)
require.Nilf(t, err, "insertion error : %v", err)
assert.Empty(t, qr.Rows, "number of rows")
// validate with 0 warnings
_, err = conn.ExecuteFetch("SELECT 1 from vt_insert_test limit 1", 1, false)
require.NoError(t, err)

qr, err = conn.ExecuteFetch("SHOW WARNINGS;", 1, false)
require.Nilf(t, err, "SHOW WARNINGS; execution failed: %v", err)
assert.EqualValues(t, 1, len(qr.Rows), "number of rows")
assert.Contains(t, qr.Rows[0][0].String(), "VARCHAR(\"Warning\")", qr.Rows)
assert.Contains(t, qr.Rows[0][1].String(), "UINT16(1317)", qr.Rows)
assert.Contains(t, qr.Rows[0][2].String(), "context deadline exceeded", qr.Rows)
require.NoError(t, err)
assert.Empty(t, qr.Rows)

// validate with 0 warnings
// verify that show warnings are empty if another statement is run before calling it
qr, err = conn.ExecuteFetch("CALL testing()", 1, false)
require.NoError(t, err)
assert.Empty(t, qr.Rows, "number of rows")
_, err = conn.ExecuteFetch("SELECT 1 from vt_insert_test limit 1", 1, false)
require.Nilf(t, err, "select error: %v", err)
require.NoError(t, err)

qr, err = conn.ExecuteFetch("SHOW WARNINGS;", 1, false)
require.Nilf(t, err, "SHOW WARNINGS; execution failed: %v", err)
assert.Empty(t, len(qr.Rows), "number of rows")
require.NoError(t, err)
assert.Empty(t, qr.Rows)
}

// TestSelectWithUnauthorizedUser verifies that an unauthorized user
Expand Down
185 changes: 185 additions & 0 deletions go/test/endtoend/vtgate/errors_as_warnings/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
/*
Copyright 2021 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.
*/

package vtgate

import (
"context"
"flag"
"fmt"
"os"
"strings"
"testing"

"github.com/google/go-cmp/cmp"

"github.com/stretchr/testify/require"

"vitess.io/vitess/go/mysql"
"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/test/endtoend/cluster"
)

var (
clusterInstance *cluster.LocalProcessCluster
vtParams mysql.ConnParams
KeyspaceName = "ks"
Cell = "test"
SchemaSQL = `create table t1(
id1 bigint,
id2 bigint,
primary key(id1)
) Engine=InnoDB;`

VSchema = `
{
"sharded": true,
"vindexes": {
"hash": {
"type": "hash"
}
},
"tables": {
"t1": {
"column_vindexes": [
{
"column": "id1",
"name": "hash"
}
]
}
}
}`
)

func TestMain(m *testing.M) {
defer cluster.PanicHandler(nil)
flag.Parse()

exitCode := func() int {
clusterInstance = cluster.NewCluster(Cell, "localhost")
defer clusterInstance.Teardown()

// Start topo server
err := clusterInstance.StartTopo()
if err != nil {
return 1
}

// Start keyspace
keyspace := &cluster.Keyspace{
Name: KeyspaceName,
SchemaSQL: SchemaSQL,
VSchema: VSchema,
}
if err := clusterInstance.StartKeyspace(*keyspace, []string{"-80", "80-"}, 1, false); err != nil {
return 1
}

// Start vtgate
if err := clusterInstance.StartVtgate(); err != nil {
return 1
}

vtParams = mysql.ConnParams{
Host: clusterInstance.Hostname,
Port: clusterInstance.VtgateMySQLPort,
}
return m.Run()
}()
os.Exit(exitCode)
}

func TestScatterErrsAsWarns(t *testing.T) {
oltp, err := mysql.Connect(context.Background(), &vtParams)
require.NoError(t, err)
defer oltp.Close()

olap, err := mysql.Connect(context.Background(), &vtParams)
require.NoError(t, err)
defer olap.Close()

checkedExec(t, oltp, `insert into t1(id1, id2) values (1, 1), (2, 2), (3, 3), (4, 4), (5, 5)`)
defer func() {
checkedExec(t, oltp, "use @master")
checkedExec(t, oltp, `delete from t1`)
}()

query1 := `select /*vt+ SCATTER_ERRORS_AS_WARNINGS */ id1 from t1`
query2 := `select /*vt+ SCATTER_ERRORS_AS_WARNINGS */ id1 from t1 order by id1`
showQ := "show warnings"

// stop the mysql on one tablet, query will fail at vttablet level
require.NoError(t,
clusterInstance.Keyspaces[0].Shards[0].Replica().MysqlctlProcess.Stop())

modes := []struct {
conn *mysql.Conn
m string
}{
{m: "oltp", conn: oltp},
{m: "olap", conn: olap},
}

for _, mode := range modes {
t.Run(mode.m, func(t *testing.T) {
// connection setup
checkedExec(t, mode.conn, "use @replica")
checkedExec(t, mode.conn, fmt.Sprintf("set workload = %s", mode.m))

assertMatches(t, mode.conn, query1, `[[INT64(4)]]`)
assertContainsOneOf(t, mode.conn, showQ, "no valid tablet", "no healthy tablet", "mysql.sock: connect: no such file or directory")
assertMatches(t, mode.conn, query2, `[[INT64(4)]]`)
assertContainsOneOf(t, mode.conn, showQ, "no valid tablet", "no healthy tablet", "mysql.sock: connect: no such file or directory")

// invalid_field should throw error and not warning
_, err = mode.conn.ExecuteFetch("SELECT /*vt+ SCATTER_ERRORS_AS_WARNINGS */ invalid_field from t1;", 1, false)
require.Error(t, err)
serr := mysql.NewSQLErrorFromError(err).(*mysql.SQLError)
require.Equal(t, 1054, serr.Number())
})
}
}

func checkedExec(t *testing.T, conn *mysql.Conn, query string) *sqltypes.Result {
t.Helper()
qr, err := conn.ExecuteFetch(query, 1000, true)
require.NoError(t, err)
return qr
}

func assertMatches(t *testing.T, conn *mysql.Conn, query, expected string) {
t.Helper()
qr := checkedExec(t, conn, query)
got := fmt.Sprintf("%v", qr.Rows)
diff := cmp.Diff(expected, got)
if diff != "" {
t.Errorf("Query: %s (-want +got):\n%s\n%s", query, diff, got)
}
}

func assertContainsOneOf(t *testing.T, conn *mysql.Conn, query string, expected ...string) {
t.Helper()
qr := checkedExec(t, conn, query)
got := fmt.Sprintf("%v", qr.Rows)
for _, s := range expected {
if strings.Contains(got, s) {
return
}
}

t.Errorf("%s\n did not match any of %v", got, expected)
}
1 change: 1 addition & 0 deletions go/test/endtoend/vtgate/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ func TestMain(m *testing.M) {
if err != nil {
return 1
}

vtParams = mysql.ConnParams{
Host: clusterInstance.Hostname,
Port: clusterInstance.VtgateMySQLPort,
Expand Down
2 changes: 2 additions & 0 deletions go/vt/sqlparser/ast_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1259,6 +1259,8 @@ func (ty ShowCommandType) ToString() string {
return VGtidExecGlobalStr
case VitessMigrations:
return VitessMigrationsStr
case Warnings:
return WarningsStr
case Keyspace:
return KeyspaceStr
default:
Expand Down
2 changes: 2 additions & 0 deletions go/vt/sqlparser/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ const (
VGtidExecGlobalStr = " global vgtid_executed"
KeyspaceStr = " keyspaces"
VitessMigrationsStr = " vitess_migrations"
WarningsStr = " warnings"

// DropKeyType strings
PrimaryKeyTypeStr = "primary key"
Expand Down Expand Up @@ -498,6 +499,7 @@ const (
VariableSession
VGtidExecGlobal
VitessMigrations
Warnings
Keyspace
)

Expand Down
3 changes: 1 addition & 2 deletions go/vt/sqlparser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1613,8 +1613,7 @@ var (
}, {
input: "alter vitess_migration cancel all",
}, {
input: "show warnings",
output: "show warnings",
input: "show warnings",
}, {
input: "select warnings from t",
output: "select `warnings` from t",
Expand Down
2 changes: 1 addition & 1 deletion go/vt/sqlparser/sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion go/vt/sqlparser/sql.y
Original file line number Diff line number Diff line change
Expand Up @@ -2612,7 +2612,7 @@ show_statement:
}
| SHOW WARNINGS
{
$$ = &Show{&ShowLegacy{Type: string($2), Scope: ImplicitScope}}
$$ = &Show{&ShowBasic{Command: Warnings}}
}
/* vitess_topo supports SHOW VITESS_SHARDS / SHOW VITESS_TABLETS */
| SHOW vitess_topo like_or_where_opt
Expand Down
14 changes: 13 additions & 1 deletion go/vt/vtgate/engine/cached_size.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit d51a7ee

Please sign in to comment.