-
Notifications
You must be signed in to change notification settings - Fork 63
Fix database dump silent failures on cursor close errors #1343
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+306
−11
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ca23935
Fix database dump silent failures on cursor close errors
cursoragent 0314cb6
Close dump cursors on early error returns.
no-itsbackpack 0032601
Fix goimports alignment in dump error tests.
no-itsbackpack c144386
Close dump cursor before logging table completion.
no-itsbackpack 1db75b3
Avoid double-closing dump cursors after an explicit close error.
no-itsbackpack File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,273 @@ | ||
| package dumper | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "testing" | ||
|
|
||
| "github.com/planetscale/cli/internal/cmdutil" | ||
| "github.com/xelabs/go-mysqlstack/driver" | ||
| querypb "github.com/xelabs/go-mysqlstack/sqlparser/depends/query" | ||
| "github.com/xelabs/go-mysqlstack/sqlparser/depends/sqltypes" | ||
| "github.com/xelabs/go-mysqlstack/xlog" | ||
|
|
||
| qt "github.com/frankban/quicktest" | ||
| ) | ||
|
|
||
| type errCloseRows struct { | ||
| row []sqltypes.Value | ||
| rowSent bool | ||
| closeErr error | ||
| rowValuesErr error | ||
| closeCalled bool | ||
| } | ||
|
|
||
| func (r *errCloseRows) Next() bool { | ||
| if r.rowSent { | ||
| return false | ||
| } | ||
| r.rowSent = true | ||
| return true | ||
| } | ||
|
|
||
| func (r *errCloseRows) Close() error { | ||
| r.closeCalled = true | ||
| return r.closeErr | ||
| } | ||
|
|
||
| func (r *errCloseRows) Datas() []byte { return nil } | ||
|
|
||
| func (r *errCloseRows) Bytes() int { return 0 } | ||
|
|
||
| func (r *errCloseRows) RowsAffected() uint64 { return 0 } | ||
|
|
||
| func (r *errCloseRows) LastInsertID() uint64 { return 0 } | ||
|
|
||
| func (r *errCloseRows) LastError() error { return nil } | ||
|
|
||
| func (r *errCloseRows) Fields() []*querypb.Field { return nil } | ||
|
|
||
| func (r *errCloseRows) RowValues() ([]sqltypes.Value, error) { | ||
| if r.rowValuesErr != nil { | ||
| return nil, r.rowValuesErr | ||
| } | ||
| return r.row, nil | ||
| } | ||
|
|
||
| type fakeConn struct { | ||
| fieldsResult *sqltypes.Result | ||
| streamRows driver.Rows | ||
| } | ||
|
|
||
| func (f *fakeConn) Ping() error { return nil } | ||
|
|
||
| func (f *fakeConn) Quit() {} | ||
|
|
||
| func (f *fakeConn) Close() error { return nil } | ||
|
|
||
| func (f *fakeConn) Closed() bool { return false } | ||
|
|
||
| func (f *fakeConn) Cleanup() {} | ||
|
|
||
| func (f *fakeConn) NextPacket() ([]byte, error) { return nil, nil } | ||
|
|
||
| func (f *fakeConn) ConnectionID() uint32 { return 0 } | ||
|
|
||
| func (f *fakeConn) InitDB(string) error { return nil } | ||
|
|
||
| func (f *fakeConn) Command(byte) error { return nil } | ||
|
|
||
| func (f *fakeConn) Query(string) (driver.Rows, error) { | ||
| return f.streamRows, nil | ||
| } | ||
|
|
||
| func (f *fakeConn) Exec(string) error { return nil } | ||
|
|
||
| func (f *fakeConn) FetchAll(string, int) (*sqltypes.Result, error) { | ||
| return f.fieldsResult, nil | ||
| } | ||
|
|
||
| func (f *fakeConn) FetchAllWithFunc(string, int, driver.Func) (*sqltypes.Result, error) { | ||
| return f.fieldsResult, nil | ||
| } | ||
|
|
||
| func (f *fakeConn) ComStatementPrepare(string) (*driver.Statement, error) { | ||
| return nil, fmt.Errorf("not implemented") | ||
| } | ||
|
|
||
| func TestDumpTableReportsCursorCloseError(t *testing.T) { | ||
| c := qt.New(t) | ||
|
|
||
| closeErr := errors.New("unexpected EOF") | ||
| fieldsResult := &sqltypes.Result{ | ||
| Fields: []*querypb.Field{ | ||
| {Name: "Field", Type: querypb.Type_VARCHAR}, | ||
| {Name: "Type", Type: querypb.Type_VARCHAR}, | ||
| {Name: "Null", Type: querypb.Type_VARCHAR}, | ||
| {Name: "Key", Type: querypb.Type_VARCHAR}, | ||
| {Name: "Default", Type: querypb.Type_VARCHAR}, | ||
| {Name: "Extra", Type: querypb.Type_VARCHAR}, | ||
| }, | ||
| Rows: [][]sqltypes.Value{ | ||
| testRow("id", ""), | ||
| }, | ||
| } | ||
|
|
||
| conn := &Connection{ | ||
| ID: 0, | ||
| client: &fakeConn{ | ||
| fieldsResult: fieldsResult, | ||
| streamRows: &errCloseRows{ | ||
| row: []sqltypes.Value{ | ||
| sqltypes.MakeTrusted(querypb.Type_INT32, []byte("1")), | ||
| }, | ||
| closeErr: closeErr, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| cfg := NewDefaultConfig() | ||
| cfg.Outdir = c.TempDir() | ||
| cfg.ChunksizeInMB = 128 | ||
| cfg.StmtSize = 1000000 | ||
|
|
||
| d := &Dumper{ | ||
| cfg: cfg, | ||
| log: cmdutil.NewZapLogger(false), | ||
| } | ||
|
|
||
| err := d.dumpTable(context.Background(), conn, "test", "t1") | ||
| c.Assert(err, qt.ErrorIs, closeErr) | ||
| } | ||
|
|
||
| func TestDumpTableClosesCursorOnRowValuesError(t *testing.T) { | ||
| c := qt.New(t) | ||
|
|
||
| rowErr := errors.New("row decode failed") | ||
| fieldsResult := &sqltypes.Result{ | ||
| Fields: []*querypb.Field{ | ||
| {Name: "Field", Type: querypb.Type_VARCHAR}, | ||
| {Name: "Type", Type: querypb.Type_VARCHAR}, | ||
| {Name: "Null", Type: querypb.Type_VARCHAR}, | ||
| {Name: "Key", Type: querypb.Type_VARCHAR}, | ||
| {Name: "Default", Type: querypb.Type_VARCHAR}, | ||
| {Name: "Extra", Type: querypb.Type_VARCHAR}, | ||
| }, | ||
| Rows: [][]sqltypes.Value{ | ||
| testRow("id", ""), | ||
| }, | ||
| } | ||
|
|
||
| rows := &errCloseRows{ | ||
| rowValuesErr: rowErr, | ||
| closeErr: errors.New("should not mask row error"), | ||
| } | ||
|
|
||
| conn := &Connection{ | ||
| ID: 0, | ||
| client: &fakeConn{ | ||
| fieldsResult: fieldsResult, | ||
| streamRows: rows, | ||
| }, | ||
| } | ||
|
|
||
| cfg := NewDefaultConfig() | ||
| cfg.Outdir = c.TempDir() | ||
| cfg.ChunksizeInMB = 128 | ||
| cfg.StmtSize = 1000000 | ||
|
|
||
| d := &Dumper{ | ||
| cfg: cfg, | ||
| log: cmdutil.NewZapLogger(false), | ||
| } | ||
|
|
||
| err := d.dumpTable(context.Background(), conn, "test", "t1") | ||
| c.Assert(err, qt.ErrorIs, rowErr) | ||
| c.Assert(rows.closeCalled, qt.IsTrue) | ||
| } | ||
|
|
||
| func TestRunReturnsDumpError(t *testing.T) { | ||
| c := qt.New(t) | ||
|
|
||
| log := xlog.NewStdLog(xlog.Level(xlog.INFO)) | ||
| fakedbs := driver.NewTestHandler(log) | ||
| server, err := driver.MockMysqlServer(log, fakedbs) | ||
| c.Assert(err, qt.IsNil) | ||
| c.Cleanup(func() { server.Close() }) | ||
|
|
||
| address := server.Addr() | ||
|
|
||
| schemaResult := &sqltypes.Result{ | ||
| Fields: []*querypb.Field{ | ||
| {Name: "Table", Type: querypb.Type_VARCHAR}, | ||
| {Name: "Create Table", Type: querypb.Type_VARCHAR}, | ||
| }, | ||
| Rows: [][]sqltypes.Value{ | ||
| { | ||
| sqltypes.MakeTrusted(querypb.Type_VARCHAR, []byte("t1")), | ||
| sqltypes.MakeTrusted(querypb.Type_VARCHAR, | ||
| []byte("CREATE TABLE `t1` (`id` int) ENGINE=InnoDB")), | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| tablesResult := &sqltypes.Result{ | ||
| Fields: []*querypb.Field{ | ||
| {Name: "Tables_in_test", Type: querypb.Type_VARCHAR}, | ||
| }, | ||
| Rows: [][]sqltypes.Value{ | ||
| {sqltypes.MakeTrusted(querypb.Type_VARCHAR, []byte("t1"))}, | ||
| }, | ||
| } | ||
|
|
||
| viewsResult := &sqltypes.Result{ | ||
| Fields: []*querypb.Field{ | ||
| {Name: "TABLE_NAME", Type: querypb.Type_VARCHAR}, | ||
| }, | ||
| Rows: [][]sqltypes.Value{}, | ||
| } | ||
|
|
||
| fieldsResult := &sqltypes.Result{ | ||
| Fields: []*querypb.Field{ | ||
| {Name: "Field", Type: querypb.Type_VARCHAR}, | ||
| {Name: "Type", Type: querypb.Type_VARCHAR}, | ||
| {Name: "Null", Type: querypb.Type_VARCHAR}, | ||
| {Name: "Key", Type: querypb.Type_VARCHAR}, | ||
| {Name: "Default", Type: querypb.Type_VARCHAR}, | ||
| {Name: "Extra", Type: querypb.Type_VARCHAR}, | ||
| }, | ||
| Rows: [][]sqltypes.Value{ | ||
| testRow("id", ""), | ||
| }, | ||
| } | ||
|
|
||
| dumpErr := errors.New("closed network connection") | ||
| fakedbs.AddQueryPattern("use .*", &sqltypes.Result{}) | ||
| fakedbs.AddQueryPattern("show create table .*", schemaResult) | ||
| fakedbs.AddQueryPattern("show tables from .*", tablesResult) | ||
| fakedbs.AddQueryPattern("select table_name \n\t\t\t from information_schema.tables \n\t\t\t where table_schema like 'test' \n\t\t\t and table_type = 'view'\n\t\t\t", viewsResult) | ||
| fakedbs.AddQueryPattern("show fields from .*", fieldsResult) | ||
| fakedbs.AddQueryErrorPattern("select .* from `test`\\.`t1` .*", dumpErr) | ||
| fakedbs.AddQueryPattern("set .*", &sqltypes.Result{}) | ||
|
|
||
| cfg := &Config{ | ||
| Database: "test", | ||
| Table: "t1", | ||
| Outdir: c.TempDir(), | ||
| User: "mock", | ||
| Password: "mock", | ||
| Address: address, | ||
| ChunksizeInMB: 1, | ||
| Threads: 1, | ||
| StmtSize: 10000, | ||
| IntervalMs: 500, | ||
| } | ||
|
|
||
| d, err := NewDumper(cfg) | ||
| c.Assert(err, qt.IsNil) | ||
|
|
||
| err = d.Run(context.Background()) | ||
| c.Assert(err, qt.IsNotNil) | ||
| c.Assert(err.Error(), qt.Contains, "closed network connection") | ||
| } |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.