Skip to content

Commit 974564a

Browse files
rvrangeltanjinx
authored andcommitted
fixing issue with xtrabackup and long gtids (vitessio#16304)
Signed-off-by: Renan Rangel <rrangel@slack-corp.com>
1 parent aac4574 commit 974564a

File tree

2 files changed

+53
-26
lines changed

2 files changed

+53
-26
lines changed

go/vt/mysqlctl/xtrabackupengine.go

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ const (
7171
writerBufferSize = 2 * 1024 * 1024 /*2 MiB*/
7272
xtrabackupBinaryName = "xtrabackup"
7373
xtrabackupEngineName = "xtrabackup"
74+
xtrabackupInfoFile = "xtrabackup_info"
7475
xbstream = "xbstream"
7576
)
7677

@@ -292,14 +293,14 @@ func (be *XtrabackupEngine) backupFiles(
292293
numStripes int,
293294
flavor string,
294295
) (replicationPosition replication.Position, finalErr error) {
295-
296296
backupProgram := path.Join(xtrabackupEnginePath, xtrabackupBinaryName)
297297
flagsToExec := []string{"--defaults-file=" + params.Cnf.Path,
298298
"--backup",
299299
"--socket=" + params.Cnf.SocketFile,
300300
"--slave-info",
301301
"--user=" + xtrabackupUser,
302302
"--target-dir=" + params.Cnf.TmpDir,
303+
"--extra-lsndir=" + params.Cnf.TmpDir,
303304
}
304305
if xtrabackupStreamMode != "" {
305306
flagsToExec = append(flagsToExec, "--stream="+xtrabackupStreamMode)
@@ -398,27 +399,14 @@ func (be *XtrabackupEngine) backupFiles(
398399
// the replication position. Note that if we don't read stderr as we go, the
399400
// xtrabackup process gets blocked when the write buffer fills up.
400401
stderrBuilder := &strings.Builder{}
401-
posBuilder := &strings.Builder{}
402402
stderrDone := make(chan struct{})
403403
go func() {
404404
defer close(stderrDone)
405405

406406
scanner := bufio.NewScanner(backupErr)
407-
capture := false
408407
for scanner.Scan() {
409408
line := scanner.Text()
410409
params.Logger.Infof("xtrabackup stderr: %s", line)
411-
412-
// Wait until we see the first line of the binlog position.
413-
// Then capture all subsequent lines. We need multiple lines since
414-
// the value we're looking for has newlines in it.
415-
if !capture {
416-
if !strings.Contains(line, "MySQL binlog position") {
417-
continue
418-
}
419-
capture = true
420-
}
421-
fmt.Fprintln(posBuilder, line)
422410
}
423411
if err := scanner.Err(); err != nil {
424412
params.Logger.Errorf("error reading from xtrabackup stderr: %v", err)
@@ -462,8 +450,7 @@ func (be *XtrabackupEngine) backupFiles(
462450
return replicationPosition, vterrors.Wrap(err, fmt.Sprintf("xtrabackup failed with error. Output=%s", sterrOutput))
463451
}
464452

465-
posOutput := posBuilder.String()
466-
replicationPosition, rerr := findReplicationPosition(posOutput, flavor, params.Logger)
453+
replicationPosition, rerr := findReplicationPositionFromXtrabackupInfo(params.Cnf.TmpDir, flavor, params.Logger)
467454
if rerr != nil {
468455
return replicationPosition, vterrors.Wrap(rerr, "backup failed trying to find replication position")
469456
}
@@ -751,6 +738,22 @@ func (be *XtrabackupEngine) extractFiles(ctx context.Context, logger logutil.Log
751738
return nil
752739
}
753740

741+
func findReplicationPositionFromXtrabackupInfo(directory, flavor string, logger logutil.Logger) (replication.Position, error) {
742+
f, err := os.Open(path.Join(directory, xtrabackupInfoFile))
743+
if err != nil {
744+
return replication.Position{}, vterrors.Errorf(vtrpc.Code_INVALID_ARGUMENT,
745+
"couldn't open %q to read GTID position", path.Join(directory, xtrabackupInfoFile))
746+
}
747+
defer f.Close()
748+
749+
contents, err := io.ReadAll(f)
750+
if err != nil {
751+
return replication.Position{}, vterrors.Errorf(vtrpc.Code_INVALID_ARGUMENT, "couldn't read GTID position from %q", f.Name())
752+
}
753+
754+
return findReplicationPosition(string(contents), flavor, logger)
755+
}
756+
754757
var xtrabackupReplicationPositionRegexp = regexp.MustCompile(`GTID of the last change '([^']*)'`)
755758

756759
func findReplicationPosition(input, flavor string, logger logutil.Logger) (replication.Position, error) {

go/vt/mysqlctl/xtrabackupengine_test.go

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"bytes"
2121
"io"
2222
"math/rand"
23+
"os"
24+
"path"
2325
"testing"
2426

2527
"github.com/stretchr/testify/assert"
@@ -54,26 +56,48 @@ func TestFindReplicationPosition(t *testing.T) {
5456
}
5557
}
5658

57-
func TestFindReplicationPositionNoMatch(t *testing.T) {
59+
func TestFindReplicationPositionFromXtrabackupInfo(t *testing.T) {
60+
input := `tool_version = 8.0.35-30
61+
binlog_pos = filename 'vt-0476396352-bin.000005', position '310088991', GTID of the last change '145e508e-ae54-11e9-8ce6-46824dd1815e:1-3,
62+
1e51f8be-ae54-11e9-a7c6-4280a041109b:1-3,
63+
47b59de1-b368-11e9-b48b-624401d35560:1-152981,
64+
557def0a-b368-11e9-84ed-f6fffd91cc57:1-3,
65+
599ef589-ae55-11e9-9688-ca1f44501925:1-14857169,
66+
b9ce485d-b36b-11e9-9b17-2a6e0a6011f4:1-371262'
67+
format = xbstream
68+
`
69+
want := "145e508e-ae54-11e9-8ce6-46824dd1815e:1-3,1e51f8be-ae54-11e9-a7c6-4280a041109b:1-3,47b59de1-b368-11e9-b48b-624401d35560:1-152981,557def0a-b368-11e9-84ed-f6fffd91cc57:1-3,599ef589-ae55-11e9-9688-ca1f44501925:1-14857169,b9ce485d-b36b-11e9-9b17-2a6e0a6011f4:1-371262"
70+
71+
tmp, err := os.MkdirTemp(t.TempDir(), "test")
72+
assert.NoError(t, err)
73+
74+
f, err := os.Create(path.Join(tmp, xtrabackupInfoFile))
75+
assert.NoError(t, err)
76+
_, err = f.WriteString(input)
77+
assert.NoError(t, err)
78+
assert.NoError(t, f.Close())
79+
80+
pos, err := findReplicationPositionFromXtrabackupInfo(tmp, "MySQL56", logutil.NewConsoleLogger())
81+
assert.NoError(t, err)
82+
assert.Equal(t, want, pos.String())
83+
}
84+
85+
func TestFindReplicationPositionNoMatchFromXtrabackupInfo(t *testing.T) {
5886
// Make sure failure to find a match triggers an error.
5987
input := `nothing`
6088

61-
_, err := findReplicationPosition(input, "MySQL56", logutil.NewConsoleLogger())
62-
if err == nil {
63-
t.Fatalf("expected error from findReplicationPosition but got nil")
64-
}
89+
_, err := findReplicationPositionFromXtrabackupInfo(input, "MySQL56", logutil.NewConsoleLogger())
90+
assert.Error(t, err)
6591
}
6692

67-
func TestFindReplicationPositionEmptyMatch(t *testing.T) {
93+
func TestFindReplicationPositionEmptyMatchFromXtrabackupInfo(t *testing.T) {
6894
// Make sure failure to find a match triggers an error.
6995
input := `GTID of the last change '
7096
7197
'`
7298

73-
_, err := findReplicationPosition(input, "MySQL56", logutil.NewConsoleLogger())
74-
if err == nil {
75-
t.Fatalf("expected error from findReplicationPosition but got nil")
76-
}
99+
_, err := findReplicationPositionFromXtrabackupInfo(input, "MySQL56", logutil.NewConsoleLogger())
100+
assert.Error(t, err)
77101
}
78102

79103
func TestStripeRoundTrip(t *testing.T) {

0 commit comments

Comments
 (0)