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

region: check if the query startkey and endkey specify an uncovered region #8743

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
25 changes: 23 additions & 2 deletions pkg/core/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -1789,8 +1789,29 @@ func (r *RegionsInfo) GetRegionCount(startKey, endKey []byte) int {
defer r.t.RUnlock()
start := &regionItem{&RegionInfo{meta: &metapb.Region{StartKey: startKey}}}
end := &regionItem{&RegionInfo{meta: &metapb.Region{StartKey: endKey}}}
// it returns 0 if startKey is nil.
_, startIndex := r.tree.tree.GetWithIndex(start)

// check if both keys are in the uncovered ranges.
startItemt, startIndex := r.tree.tree.GetWithIndex(start)
endItemt, eit := r.tree.tree.GetWithIndex(end)
rleungx marked this conversation as resolved.
Show resolved Hide resolved
if startItemt == nil {
startItemt = r.tree.tree.GetAt(startIndex - 1)
}
if endItemt == nil {
endItemt = r.tree.tree.GetAt(eit - 1)
}

startInAnInterval := false
if startItemt != nil {
startInAnInterval = (bytes.Compare(startItemt.GetStartKey(), startKey) <= 0) && (bytes.Compare(startKey, startItemt.GetEndKey()) <= 0)
}
endInAnInterval := false
if endItemt != nil {
endInAnInterval = (bytes.Compare(endItemt.GetStartKey(), endKey) <= 0) && (bytes.Compare(endKey, endItemt.GetEndKey()) <= 0)
}
if startIndex == eit && (!startInAnInterval) && (!endInAnInterval) {
return 0
}

var endIndex int
var item *regionItem
// it should return the length of the tree if endKey is nil.
Expand Down
88 changes: 88 additions & 0 deletions server/api/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,91 @@
}
}
}

func (suite *statsTestSuite) TestRegionStatsHoles() {
statsURL := suite.urlPrefix + "/stats/region"
epoch := &metapb.RegionEpoch{
ConfVer: 1,
Version: 1,
}

re := suite.Require()

// range holes
regions_withholes := []*core.RegionInfo{

Check failure on line 218 in server/api/stats_test.go

View workflow job for this annotation

GitHub Actions / statics

var-naming: don't use underscores in Go names; var regions_withholes should be regionsWithholes (revive)
core.NewRegionInfo(
&metapb.Region{
Id: 1,
StartKey: []byte(""),
EndKey: []byte("c"),
Peers: []*metapb.Peer{
{Id: 101, StoreId: 1},
{Id: 102, StoreId: 2},
{Id: 103, StoreId: 3},
},
RegionEpoch: epoch,
},
&metapb.Peer{Id: 101, StoreId: 1},
core.SetApproximateSize(100),
core.SetApproximateKvSize(80),
core.SetApproximateKeys(50),
),
core.NewRegionInfo(
&metapb.Region{
Id: 2,
StartKey: []byte("h"),
EndKey: []byte("x"),
Peers: []*metapb.Peer{
{Id: 104, StoreId: 1},
{Id: 105, StoreId: 4},
{Id: 106, StoreId: 5},
},
RegionEpoch: epoch,
},
&metapb.Peer{Id: 105, StoreId: 4},
core.SetApproximateSize(200),
core.SetApproximateKvSize(180),
core.SetApproximateKeys(150),
),
core.NewRegionInfo(
&metapb.Region{
Id: 3,
StartKey: []byte("z"),
EndKey: []byte(""),
Peers: []*metapb.Peer{
{Id: 106, StoreId: 1},
{Id: 107, StoreId: 5},
},
RegionEpoch: epoch,
},
&metapb.Peer{Id: 107, StoreId: 5},
core.SetApproximateSize(1),
core.SetApproximateKvSize(1),
core.SetApproximateKeys(1),
),
}

for _, r := range regions_withholes {
mustRegionHeartbeat(re, suite.svr, r)
}

// holes in between :
// | - c| ... |h - x| ... |z - |

startKeys := [4]string{"d", "b", "b", "i"}
endKeys := [4]string{"e", "e", "i", "j"}
ans := [4]int{0, 1, 2, 1}

for i := 0; i < 4; i++ {
startKey := url.QueryEscape(startKeys[i])
endKey := url.QueryEscape(endKeys[i])
args_withholes := fmt.Sprintf("?start_key=%s&end_key=%s&count", startKey, endKey)

Check failure on line 285 in server/api/stats_test.go

View workflow job for this annotation

GitHub Actions / statics

var-naming: don't use underscores in Go names; var args_withholes should be argsWithholes (revive)
res, err := testDialClient.Get(statsURL + args_withholes)
re.NoError(err)
stats := &statistics.RegionStats{}
err = apiutil.ReadJSON(res.Body, stats)
res.Body.Close()
re.NoError(err)
re.Equal(ans[i], stats.Count)
}
}
Loading