Repository: tikv/client-go
Pull request #1680 (2025-06-27 05:37:23)

(tidb-7.5) Support buckets for `ScanRegions` of mocktikv
ref pingcap/tidb#53850, pingcap/tidb#62040

Cherry pick #1052 to 7.5 and support buckets for `ScanRegions` of mocktikv.

ti-chi-bot[bot]: [APPROVALNOTIFIER] This PR is **NOT APPROVED**

This pull-request has been approved by:
**Once this PR has been reviewed and has the lgtm label**, please assign [myonkeminta](https://github.com/myonkeminta) for approval. For more information see [the Code Review Process](https://book.prow.tidb.net/#/workflows/pr).
**Please ensure that each of them provides their approval before proceeding.**

The full list of commands accepted by this bot can be found [here](https://prow.tidb.net/command-help?repo=tikv%2Fclient-go).

<details open>
Needs approval from an approver in each of these files:

- **[OWNERS](https://github.com/tikv/client-go/blob/tidb-7.5/OWNERS)**

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":["myonkeminta"]} -->
ti-chi-bot[bot]: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *<a href="https://github.com/tikv/client-go/pull/1680#pullrequestreview-2970032404" title="Approved">cfzjywxk</a>*, *<a href="https://github.com/tikv/client-go/pull/1680#pullrequestreview-2969808624" title="Approved">zyguan</a>*

The full list of commands accepted by this bot can be found [here](https://prow.tidb.net/command-help?repo=tikv%2Fclient-go).

The pull request process is described [here](https://book.prow.tidb.net/#/workflows/pr)

<details >
Needs approval from an approver in each of these files:

- ~~[OWNERS](https://github.com/tikv/client-go/blob/tidb-7.5/OWNERS)~~ [cfzjywxk]

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":[]} -->
ti-chi-bot[bot]: [LGTM Timeline notifier]
---
Timeline:

- `2025-06-30 03:45:49.355683522 +0000 UTC m=+1280202.078862497`: :ballot_box_with_check: agreed by [zyguan](https://github.com/zyguan).
- `2025-06-30 06:20:38.838490338 +0000 UTC m=+1289491.561669319`: :ballot_box_with_check: agreed by [cfzjywxk](https://github.com/cfzjywxk).

Patch:
diff --git a/internal/locate/region_cache.go b/internal/locate/region_cache.go
index bea8bec..126f4bf 100644
--- a/internal/locate/region_cache.go
+++ b/internal/locate/region_cache.go
@@ -2518,6 +2518,10 @@ func (r *Region) Contains(key []byte) bool {
 // for the maximum region endKey is empty.
 // startKey < key <= endKey.
 func (r *Region) ContainsByEnd(key []byte) bool {
+	// Only a region's right bound expands to inf contains the point at inf.
+	if len(key) == 0 {
+		return len(r.EndKey()) == 0
+	}
 	return bytes.Compare(r.meta.GetStartKey(), key) < 0 &&
 		(bytes.Compare(key, r.meta.GetEndKey()) <= 0 || len(r.meta.GetEndKey()) == 0)
 }
diff --git a/internal/mockstore/mocktikv/cluster.go b/internal/mockstore/mocktikv/cluster.go
index d6a1df6..22f2260 100644
--- a/internal/mockstore/mocktikv/cluster.go
+++ b/internal/mockstore/mocktikv/cluster.go
@@ -404,6 +404,7 @@ func (c *Cluster) ScanRegions(startKey, endKey []byte, limit int) []*pd.Region {
 			Meta:      proto.Clone(region.Meta).(*metapb.Region),
 			Leader:    leader,
 			DownPeers: c.getDownPeers(region),
+			Buckets:   proto.Clone(region.Buckets).(*metapb.Buckets),
 		}
 		result = append(result, r)
 	}


Test patch:
diff --git a/internal/locate/region_cache_test.go b/internal/locate/region_cache_test.go
index e012dbf..9c1aff6 100644
--- a/internal/locate/region_cache_test.go
+++ b/internal/locate/region_cache_test.go
@@ -1376,9 +1376,9 @@ func (s *testRegionCacheSuite) TestContains() {
 }
 
 func (s *testRegionCacheSuite) TestContainsByEnd() {
-	s.False(createSampleRegion(nil, nil).ContainsByEnd([]byte{}))
+	s.True(createSampleRegion(nil, nil).ContainsByEnd([]byte{}))
 	s.True(createSampleRegion(nil, nil).ContainsByEnd([]byte{10}))
-	s.False(createSampleRegion([]byte{10}, nil).ContainsByEnd([]byte{}))
+	s.True(createSampleRegion([]byte{10}, nil).ContainsByEnd([]byte{}))
 	s.False(createSampleRegion([]byte{10}, nil).ContainsByEnd([]byte{10}))
 	s.True(createSampleRegion([]byte{10}, nil).ContainsByEnd([]byte{11}))
 	s.False(createSampleRegion(nil, []byte{10}).ContainsByEnd([]byte{}))