Skip to content

Commit

Permalink
update func GetBackupCandidates and add func Test_GetBackupCandidates
Browse files Browse the repository at this point in the history
Signed-off-by: Jun Wang <[email protected]>
  • Loading branch information
Jun Wang committed Nov 28, 2023
1 parent ddad61a commit d08edca
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 10 deletions.
10 changes: 0 additions & 10 deletions go/vt/vtctl/reparentutil/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,16 +346,6 @@ func waitForCatchUp(

// GetBackupCandidates is used to get a list of healthy tablets for backup
func GetBackupCandidates(tablets []*topo.TabletInfo, stats []*replicationdatapb.Status) (res []*topo.TabletInfo) {
// In case of all tablets has nil stats, return TabletType_PRIMARY
if len(stats) == 0 {
for _, tablet := range tablets {
if tablet.Type == topodatapb.TabletType_PRIMARY {
res = append(res, tablet)
break
}
}
return res
}
for i, stat := range stats {
// Always include TabletType_PRIMARY
if tablets[i].Type == topodatapb.TabletType_PRIMARY {
Expand Down
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) {
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)
})
}
}

0 comments on commit d08edca

Please sign in to comment.