Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
3pointer committed Dec 16, 2021
1 parent 46f8615 commit 3f76869
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 3 deletions.
6 changes: 3 additions & 3 deletions br/pkg/restore/split.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,10 +339,10 @@ func (rs *RegionSplitter) ScatterRegions(ctx context.Context, newRegions []*Regi
})
}

func checkRegionConsistency(startKey, endKey []byte, regions []*RegionInfo) error {
func CheckRegionConsistency(startKey, endKey []byte, regions []*RegionInfo) error {
// current pd can't guarantee the consistency of returned regions
if len(regions) == 0 {
return errors.Annotatef(berrors.ErrPDBatchScanRegion, "scan region return empty result, startKey: %s, endkey: %s",
return errors.Annotatef(berrors.ErrPDBatchScanRegion, "scan region return empty result, startKey: %s, endKey: %s",
redact.Key(startKey), redact.Key(endKey))
}

Expand Down Expand Up @@ -398,7 +398,7 @@ func PaginateScanRegion(
break
}
}
if err := checkRegionConsistency(startKey, endKey, regions); err != nil {
if err := CheckRegionConsistency(startKey, endKey, regions); err != nil {
log.Warn("failed to scan region, retrying", logutil.ShortError(err))
return err
}
Expand Down
67 changes: 67 additions & 0 deletions br/pkg/restore/split_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,3 +445,70 @@ func (s *testRangeSuite) TestNeedSplit(c *C) {
// Out of region
c.Assert(restore.NeedSplit([]byte("e"), regions), IsNil)
}

func (s *testRangeSuite) TestRegionConsistency(c *C) {
cases := []struct {
startKey []byte
endKey []byte
err string
regions []*restore.RegionInfo
}{
{
codec.EncodeBytes([]byte{}, []byte("a")),
codec.EncodeBytes([]byte{}, []byte("a")),
"scan region return empty result, startKey: (.*?), endKey: (.*?)",
[]*restore.RegionInfo{},
},
{
codec.EncodeBytes([]byte{}, []byte("a")),
codec.EncodeBytes([]byte{}, []byte("a")),
"first region's startKey > startKey, startKey: (.*?), regionStartKey: (.*?)",
[]*restore.RegionInfo{
{
Region: &metapb.Region{
StartKey: codec.EncodeBytes([]byte{}, []byte("b")),
EndKey: codec.EncodeBytes([]byte{}, []byte("d")),
},
},
},
},
{
codec.EncodeBytes([]byte{}, []byte("b")),
codec.EncodeBytes([]byte{}, []byte("e")),
"last region's endKey < endKey, endKey: (.*?), regionEndKey: (.*?)",
[]*restore.RegionInfo{
{
Region: &metapb.Region{
StartKey: codec.EncodeBytes([]byte{}, []byte("b")),
EndKey: codec.EncodeBytes([]byte{}, []byte("d")),
},
},
},
},
{
codec.EncodeBytes([]byte{}, []byte("c")),
codec.EncodeBytes([]byte{}, []byte("e")),
"region endKey not equal to next region startKey(.*?)",
[]*restore.RegionInfo{
{
Region: &metapb.Region{
StartKey: codec.EncodeBytes([]byte{}, []byte("b")),
EndKey: codec.EncodeBytes([]byte{}, []byte("d")),
},
},
{
Region: &metapb.Region{
StartKey: codec.EncodeBytes([]byte{}, []byte("e")),
EndKey: codec.EncodeBytes([]byte{}, []byte("f")),
},
},
},
},
}
for _, ca := range cases {
c.Assert(
restore.CheckRegionConsistency(ca.startKey, ca.endKey, ca.regions),
ErrorMatches,
ca.err)
}
}

0 comments on commit 3f76869

Please sign in to comment.