Skip to content
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

refine replication/parser and test case #296

Merged
merged 3 commits into from
Jul 25, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions replication/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,22 +84,22 @@ func (p *BinlogParser) ParseFile(name string, offset int64, onEvent OnEventFunc)
}

func (p *BinlogParser) parseFormatDescriptionEvent(r io.Reader, onEvent OnEventFunc) error {
_, err := p.parseSingleEvent(&r, onEvent)
_, err := p.parseSingleEvent(r, onEvent)
return err
}

// ParseSingleEvent parses single binlog event and passes the event to onEvent function.
func (p *BinlogParser) ParseSingleEvent(r *io.Reader, onEvent OnEventFunc) (bool, error) {
func (p *BinlogParser) ParseSingleEvent(r io.Reader, onEvent OnEventFunc) (bool, error) {
return p.parseSingleEvent(r, onEvent)
}

func (p *BinlogParser) parseSingleEvent(r *io.Reader, onEvent OnEventFunc) (bool, error) {
func (p *BinlogParser) parseSingleEvent(r io.Reader, onEvent OnEventFunc) (bool, error) {
var err error
var n int64

headBuf := make([]byte, EventHeaderSize)

if _, err = io.ReadFull(*r, headBuf); err == io.EOF {
if _, err = io.ReadFull(r, headBuf); err == io.EOF {
return true, nil
} else if err != nil {
return false, errors.Trace(err)
Expand All @@ -116,7 +116,7 @@ func (p *BinlogParser) parseSingleEvent(r *io.Reader, onEvent OnEventFunc) (bool
}

var buf bytes.Buffer
if n, err = io.CopyN(&buf, *r, int64(h.EventSize)-int64(EventHeaderSize)); err != nil {
if n, err = io.CopyN(&buf, r, int64(h.EventSize)-int64(EventHeaderSize)); err != nil {
return false, errors.Errorf("get event body err %v, need %d - %d, but got %d", err, h.EventSize, EventHeaderSize, n)
}

Expand Down Expand Up @@ -152,7 +152,7 @@ func (p *BinlogParser) ParseReader(r io.Reader, onEvent OnEventFunc) error {
break
}

done, err := p.parseSingleEvent(&r, onEvent)
done, err := p.parseSingleEvent(r, onEvent)
if err != nil {
if _, ok := err.(errMissingTableMapEvent); ok {
continue
Expand Down
16 changes: 12 additions & 4 deletions replication/replication_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"flag"
"fmt"
"os"
"path"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -394,9 +395,11 @@ func (t *testSyncerSuite) TestMysqlBinlogCodec(c *C) {
t.testSync(c, nil)
}()

os.RemoveAll("./var")
binlogDir := "./var"

err := t.b.StartBackup("./var", mysql.Position{"", uint32(0)}, 2*time.Second)
os.RemoveAll(binlogDir)

err := t.b.StartBackup(binlogDir, mysql.Position{"", uint32(0)}, 2*time.Second)
c.Assert(err, IsNil)

p := NewBinlogParser()
Expand All @@ -409,9 +412,14 @@ func (t *testSyncerSuite) TestMysqlBinlogCodec(c *C) {
return nil
}

err = p.ParseFile("./var/mysql.000001", 0, f)
dir, err := os.Open(binlogDir)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to close the dir?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, a mistake

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

c.Assert(err, IsNil)

err = p.ParseFile("./var/mysql.000002", 0, f)
files, err := dir.Readdirnames(-1)
c.Assert(err, IsNil)

for _, file := range files {
err = p.ParseFile(path.Join(binlogDir, file), 0, f)
c.Assert(err, IsNil)
}
}