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

Use relay log position in PRS while choosing the new primary #9270

Merged
merged 5 commits into from
Nov 30, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 7 additions & 2 deletions go/vt/vtctl/reparentutil/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,14 @@ func findPositionForTablet(ctx context.Context, tablet *topodatapb.Tablet, logge
return mysql.Position{}, err
}

pos, err := mysql.DecodePosition(status.Position)
// Use the relay log position if available, otherwise use the executed GTID set (binary log position).
positionString := status.Position
if status.RelayLogPosition != "" {
positionString = status.RelayLogPosition
}
pos, err := mysql.DecodePosition(positionString)
if err != nil {
logger.Warningf("cannot decode replica position %v for tablet %v, ignoring tablet: %v", status.Position, topoproto.TabletAliasString(tablet.Alias), err)
logger.Warningf("cannot decode replica position %v for tablet %v, ignoring tablet: %v", positionString, topoproto.TabletAliasString(tablet.Alias), err)
return mysql.Position{}, err
}

Expand Down
155 changes: 155 additions & 0 deletions go/vt/vtctl/reparentutil/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"testing"
"time"

"github.com/stretchr/testify/require"

"github.com/stretchr/testify/assert"

"vitess.io/vitess/go/mysql"
Expand Down Expand Up @@ -128,6 +130,66 @@ func TestChooseNewPrimary(t *testing.T) {
},
shouldErr: false,
},
{
name: "found a replica - more advanced relay log position",
tmc: &chooseNewPrimaryTestTMClient{
// zone1-101 is behind zone1-102
// since the relay log position for zone1-102 is more advanced
replicationStatuses: map[string]*replicationdatapb.Status{
"zone1-0000000101": {
Position: "MySQL56/3E11FA47-71CA-11E1-9E33-C80AA9429562:1-2",
},
"zone1-0000000102": {
Position: "MySQL56/3E11FA47-71CA-11E1-9E33-C80AA9429562:1",
RelayLogPosition: "MySQL56/3E11FA47-71CA-11E1-9E33-C80AA9429562:1-5",
},
},
},
shardInfo: topo.NewShardInfo("testkeyspace", "-", &topodatapb.Shard{
PrimaryAlias: &topodatapb.TabletAlias{
Cell: "zone1",
Uid: 100,
},
}, nil),
tabletMap: map[string]*topo.TabletInfo{
"primary": {
Tablet: &topodatapb.Tablet{
Alias: &topodatapb.TabletAlias{
Cell: "zone1",
Uid: 100,
},
Type: topodatapb.TabletType_PRIMARY,
},
},
"replica1": {
Tablet: &topodatapb.Tablet{
Alias: &topodatapb.TabletAlias{
Cell: "zone1",
Uid: 101,
},
Type: topodatapb.TabletType_REPLICA,
},
},
"replica2": {
Tablet: &topodatapb.Tablet{
Alias: &topodatapb.TabletAlias{
Cell: "zone1",
Uid: 102,
},
Type: topodatapb.TabletType_REPLICA,
},
},
},
avoidPrimaryAlias: &topodatapb.TabletAlias{
Cell: "zone1",
Uid: 0,
},
expected: &topodatapb.TabletAlias{
Cell: "zone1",
Uid: 102,
},
shouldErr: false,
},
{
name: "no active primary in shard",
tmc: &chooseNewPrimaryTestTMClient{
Expand Down Expand Up @@ -346,6 +408,99 @@ func TestChooseNewPrimary(t *testing.T) {
}
}

func TestFindPositionForTablet(t *testing.T) {
t.Parallel()

ctx := context.Background()
logger := logutil.NewMemoryLogger()
tests := []struct {
name string
tmc *testutil.TabletManagerClient
tablet *topodatapb.Tablet
expectedPosition string
expectedErr string
}{
{
name: "executed gtid set",
tmc: &testutil.TabletManagerClient{
ReplicationStatusResults: map[string]struct {
Position *replicationdatapb.Status
Error error
}{
"zone1-0000000100": {
Position: &replicationdatapb.Status{
Position: "MySQL56/3e11fa47-71ca-11e1-9e33-c80aa9429562:1-5",
},
},
},
},
tablet: &topodatapb.Tablet{
Alias: &topodatapb.TabletAlias{
Cell: "zone1",
Uid: 100,
},
},
expectedPosition: "MySQL56/3e11fa47-71ca-11e1-9e33-c80aa9429562:1-5",
}, {
name: "relay log",
tmc: &testutil.TabletManagerClient{
ReplicationStatusResults: map[string]struct {
Position *replicationdatapb.Status
Error error
}{
"zone1-0000000100": {
Position: &replicationdatapb.Status{
Position: "unused",
RelayLogPosition: "MySQL56/3e11fa47-71ca-11e1-9e33-c80aa9429562:1-5",
},
},
},
},
tablet: &topodatapb.Tablet{
Alias: &topodatapb.TabletAlias{
Cell: "zone1",
Uid: 100,
},
},
expectedPosition: "MySQL56/3e11fa47-71ca-11e1-9e33-c80aa9429562:1-5",
}, {
name: "error in parsing position",
tmc: &testutil.TabletManagerClient{
ReplicationStatusResults: map[string]struct {
Position *replicationdatapb.Status
Error error
}{
"zone1-0000000100": {
Position: &replicationdatapb.Status{
Position: "unused",
},
},
},
},
tablet: &topodatapb.Tablet{
Alias: &topodatapb.TabletAlias{
Cell: "zone1",
Uid: 100,
},
},
expectedErr: `parse error: unknown GTIDSet flavor ""`,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
pos, err := findPositionForTablet(ctx, test.tablet, logger, test.tmc, 10*time.Second)
if test.expectedErr != "" {
require.EqualError(t, err, test.expectedErr)
return
}
require.NoError(t, err)
posString := mysql.EncodePosition(pos)
require.Equal(t, test.expectedPosition, posString)
})
}
}

func TestFindCurrentPrimary(t *testing.T) {
t.Parallel()

Expand Down