-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8139 from planetscale/errs-as-warns
Improve ScatterErrorsAsWarnings functionality
- Loading branch information
Showing
29 changed files
with
711 additions
and
235 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
185 changes: 185 additions & 0 deletions
185
go/test/endtoend/vtgate/errors_as_warnings/main_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.