Skip to content

Commit

Permalink
fixed the endkey-being-empty bug
Browse files Browse the repository at this point in the history
Signed-off-by: Boyang Lyu <[email protected]>
  • Loading branch information
Jackl9u committed Oct 28, 2024
1 parent 0a57633 commit 6027b94
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
16 changes: 14 additions & 2 deletions pkg/core/region.go
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -1249,11 +1249,11 @@ func (r *RegionsInfo) setRegionLocked(region *RegionInfo, withOverlaps bool, ol

// UpdateSubTree updates the subtree.
func (r *RegionsInfo) UpdateSubTree(region, origin *RegionInfo, overlaps []*RegionInfo, rangeChanged bool) {
failpoint.Inject("UpdateSubTree", func() {
if _, _err_ := failpoint.Eval(_curpkg_("UpdateSubTree")); _err_ == nil {

Check failure on line 1252 in pkg/core/region.go

View workflow job for this annotation

GitHub Actions / tso-function-test

undefined: _curpkg_

Check failure on line 1252 in pkg/core/region.go

View workflow job for this annotation

GitHub Actions / chunks (1, Unit Test(1))

undefined: _curpkg_

Check failure on line 1252 in pkg/core/region.go

View workflow job for this annotation

GitHub Actions / statics

undefined: _curpkg_

Check failure on line 1252 in pkg/core/region.go

View workflow job for this annotation

GitHub Actions / chunks (2, Unit Test(2))

undefined: _curpkg_

Check failure on line 1252 in pkg/core/region.go

View workflow job for this annotation

GitHub Actions / chunks (3, Unit Test(3))

undefined: _curpkg_

Check failure on line 1252 in pkg/core/region.go

View workflow job for this annotation

GitHub Actions / chunks (4, Tests(1))

undefined: _curpkg_

Check failure on line 1252 in pkg/core/region.go

View workflow job for this annotation

GitHub Actions / chunks (5, Tests(2))

undefined: _curpkg_
if origin == nil {
time.Sleep(time.Second)
}
})
}
r.st.Lock()
defer r.st.Unlock()
if origin != nil {
Expand Down Expand Up @@ -1795,9 +1795,13 @@ func (r *RegionsInfo) GetRegionCount(startKey, endKey []byte) int {
endItem, eit := r.tree.tree.GetWithIndex(end)
if startItem == nil {
startItem = r.tree.tree.GetAt(startIndex - 1)
} else {
startIndex += 1
}
if endItem == nil {
endItem = r.tree.tree.GetAt(eit - 1)
} else {
eit += 1
}

startInAnInterval := false
Expand All @@ -1808,6 +1812,13 @@ func (r *RegionsInfo) GetRegionCount(startKey, endKey []byte) int {
if endItem != nil {
endInAnInterval = (bytes.Compare(endItem.GetStartKey(), endKey) <= 0) && (bytes.Compare(endKey, endItem.GetEndKey()) <= 0)
}

if len(endKey) == 0 {
endItem = r.tree.tree.GetAt(r.tree.tree.Len() - 1)
eit = r.tree.tree.Len()
endInAnInterval = (bytes.Compare(endItem.GetEndKey(), endKey) <= 0) && (bytes.Compare(endKey, endItem.GetEndKey()) <= 0)
}

if startIndex == eit && (!startInAnInterval) && (!endInAnInterval) {
return 0
}
Expand All @@ -1824,6 +1835,7 @@ func (r *RegionsInfo) GetRegionCount(startKey, endKey []byte) int {
endIndex--
}
}

return endIndex - startIndex + 1
}

Expand Down
20 changes: 10 additions & 10 deletions server/api/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func TestRegionStatsHoles(t *testing.T) {
core.NewRegionInfo(
&metapb.Region{
Id: 1,
StartKey: []byte(""),
StartKey: []byte("-"),
EndKey: []byte("c"),
Peers: []*metapb.Peer{
{Id: 101, StoreId: 1},
Expand All @@ -245,8 +245,8 @@ func TestRegionStatsHoles(t *testing.T) {
core.NewRegionInfo(
&metapb.Region{
Id: 2,
StartKey: []byte("h"),
EndKey: []byte("x"),
StartKey: []byte("g"),
EndKey: []byte("l"),
Peers: []*metapb.Peer{
{Id: 104, StoreId: 1},
{Id: 105, StoreId: 4},
Expand All @@ -262,8 +262,8 @@ func TestRegionStatsHoles(t *testing.T) {
core.NewRegionInfo(
&metapb.Region{
Id: 3,
StartKey: []byte("z"),
EndKey: []byte(""),
StartKey: []byte("o"),
EndKey: []byte("u"),
Peers: []*metapb.Peer{
{Id: 106, StoreId: 1},
{Id: 107, StoreId: 5},
Expand All @@ -282,13 +282,13 @@ func TestRegionStatsHoles(t *testing.T) {
}

// holes in between :
// | - c| ... |h - x| ... |z - |
// | - c| ... |g - l| ... |o - u| ...

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

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

0 comments on commit 6027b94

Please sign in to comment.