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

increase vtctlclient backupShard command success rate #14604

Merged
merged 13 commits into from
Nov 30, 2023
8 changes: 7 additions & 1 deletion go/vt/vtctl/grpcvtctldserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,8 +431,14 @@ func (s *VtctldServer) BackupShard(req *vtctldatapb.BackupShardRequest, stream v
span.Annotate("incremental_from_pos", req.IncrementalFromPos)

tablets, stats, err := reparentutil.ShardReplicationStatuses(ctx, s.ts, s.tmc, req.Keyspace, req.Shard)

// Instead of return on err directly, only return err when no tablets for backup at all
if err != nil {
return err
tablets = reparentutil.GetBackupCandidates(tablets, stats)
// Only return err when no usable tablet
if len(tablets) == 0 {
return err
}
}

var (
Expand Down
17 changes: 17 additions & 0 deletions go/vt/vtctl/reparentutil/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,3 +343,20 @@ func waitForCatchUp(
}
return nil
}

// GetBackupCandidates is used to get a list of healthy tablets for backup
func GetBackupCandidates(tablets []*topo.TabletInfo, stats []*replicationdatapb.Status) (res []*topo.TabletInfo) {
for i, stat := range stats {
// Always include TabletType_PRIMARY
if tablets[i].Type == topodatapb.TabletType_PRIMARY {
res = append(res, tablets[i])
continue
}
// shardTablets[i] and stats[i] is 1:1 mapping
// Healthy shardTablets[i] will be added to tablets
if stat != nil {
res = append(res, tablets[i])
}
Copy link
Contributor

Choose a reason for hiding this comment

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

suggest condensing this into either a switch or if/else and avoiding the continue

Copy link
Contributor

Choose a reason for hiding this comment

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

alternatively:

if tablets[i].Type == topodatapb.TabletType_PRIMARY || stats != nil {
    res = append(...)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good approach.

}
return res
}
102 changes: 102 additions & 0 deletions go/vt/vtctl/reparentutil/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1218,3 +1218,105 @@ func Test_getTabletsWithPromotionRules(t *testing.T) {
})
}
}

func Test_GetBackupCandidates(t *testing.T) {
jwangace marked this conversation as resolved.
Show resolved Hide resolved
var (
primaryTablet = &topo.TabletInfo{
Tablet: &topodatapb.Tablet{
Alias: &topodatapb.TabletAlias{
Cell: "zone1",
Uid: 1,
},
Type: topodatapb.TabletType_PRIMARY,
},
}
replicaTablet = &topo.TabletInfo{
Tablet: &topodatapb.Tablet{
Alias: &topodatapb.TabletAlias{
Cell: "zone1",
Uid: 2,
},
Type: topodatapb.TabletType_REPLICA,
},
}
rdonlyTablet = &topo.TabletInfo{
Tablet: &topodatapb.Tablet{
Alias: &topodatapb.TabletAlias{
Cell: "zone1",
Uid: 3,
},
Type: topodatapb.TabletType_RDONLY,
},
}
spareTablet = &topo.TabletInfo{
Tablet: &topodatapb.Tablet{
Alias: &topodatapb.TabletAlias{
Cell: "zone1",
Uid: 4,
},
Type: topodatapb.TabletType_SPARE,
},
}
)
tests := []struct {
name string
in []*topo.TabletInfo
expected []*topo.TabletInfo
status []*replicationdatapb.Status
}{
{
name: "one primary tablet with status",
in: []*topo.TabletInfo{primaryTablet},
expected: []*topo.TabletInfo{primaryTablet},
status: []*replicationdatapb.Status{{}},
},
{
name: "one primary tablet with no status",
in: []*topo.TabletInfo{primaryTablet},
expected: []*topo.TabletInfo{primaryTablet},
status: []*replicationdatapb.Status{nil},
},
{
name: "4 tablets with no status",
in: []*topo.TabletInfo{primaryTablet, replicaTablet, rdonlyTablet, spareTablet},
expected: []*topo.TabletInfo{primaryTablet},
status: []*replicationdatapb.Status{nil, nil, nil, nil},
},
{
name: "4 tablets with full status",
in: []*topo.TabletInfo{primaryTablet, replicaTablet, rdonlyTablet, spareTablet},
expected: []*topo.TabletInfo{primaryTablet, replicaTablet, rdonlyTablet, spareTablet},
status: []*replicationdatapb.Status{{}, {}, {}, {}},
},
{
name: "4 tablets with no primaryTablet status",
in: []*topo.TabletInfo{primaryTablet, replicaTablet, rdonlyTablet, spareTablet},
expected: []*topo.TabletInfo{primaryTablet, replicaTablet, rdonlyTablet, spareTablet},
status: []*replicationdatapb.Status{nil, {}, {}, {}},
},
{
name: "4 tablets with no replicaTablet status",
in: []*topo.TabletInfo{primaryTablet, replicaTablet, rdonlyTablet, spareTablet},
expected: []*topo.TabletInfo{primaryTablet, rdonlyTablet, spareTablet},
status: []*replicationdatapb.Status{{}, nil, {}, {}},
},
{
name: "4 tablets with no rdonlyTablet status",
in: []*topo.TabletInfo{primaryTablet, replicaTablet, rdonlyTablet, spareTablet},
expected: []*topo.TabletInfo{primaryTablet, replicaTablet, spareTablet},
status: []*replicationdatapb.Status{{}, {}, nil, {}},
},
{
name: "4 tablets with no spareTablet status",
in: []*topo.TabletInfo{primaryTablet, replicaTablet, rdonlyTablet, spareTablet},
expected: []*topo.TabletInfo{primaryTablet, replicaTablet, rdonlyTablet},
status: []*replicationdatapb.Status{{}, {}, {}, nil},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
res := GetBackupCandidates(tt.in, tt.status)
require.EqualValues(t, tt.expected, res)
})
}
}
Loading