diff --git a/examples/gcworker/go.mod b/examples/gcworker/go.mod index 711e70930f..8caa42f919 100644 --- a/examples/gcworker/go.mod +++ b/examples/gcworker/go.mod @@ -15,7 +15,7 @@ require ( github.com/docker/go-units v0.5.0 // indirect github.com/gogo/protobuf v1.3.2 // indirect github.com/golang/protobuf v1.5.4 // indirect - github.com/google/btree v1.1.2 // indirect + github.com/tidwall/btree v1.7.0 // indirect github.com/google/uuid v1.6.0 // indirect github.com/grpc-ecosystem/go-grpc-middleware v1.1.0 // indirect github.com/opentracing/opentracing-go v1.2.0 // indirect diff --git a/examples/rawkv/go.mod b/examples/rawkv/go.mod index 39e643a43f..116d48e78e 100644 --- a/examples/rawkv/go.mod +++ b/examples/rawkv/go.mod @@ -15,7 +15,7 @@ require ( github.com/docker/go-units v0.5.0 // indirect github.com/gogo/protobuf v1.3.2 // indirect github.com/golang/protobuf v1.5.4 // indirect - github.com/google/btree v1.1.2 // indirect + github.com/tidwall/btree v1.7.0 // indirect github.com/google/uuid v1.6.0 // indirect github.com/grpc-ecosystem/go-grpc-middleware v1.1.0 // indirect github.com/opentracing/opentracing-go v1.2.0 // indirect diff --git a/examples/txnkv/1pc_txn/go.mod b/examples/txnkv/1pc_txn/go.mod index 179bce58d2..d128b4db11 100644 --- a/examples/txnkv/1pc_txn/go.mod +++ b/examples/txnkv/1pc_txn/go.mod @@ -15,7 +15,7 @@ require ( github.com/docker/go-units v0.5.0 // indirect github.com/gogo/protobuf v1.3.2 // indirect github.com/golang/protobuf v1.5.4 // indirect - github.com/google/btree v1.1.2 // indirect + github.com/tidwall/btree v1.7.0 // indirect github.com/google/uuid v1.6.0 // indirect github.com/grpc-ecosystem/go-grpc-middleware v1.1.0 // indirect github.com/opentracing/opentracing-go v1.2.0 // indirect diff --git a/examples/txnkv/async_commit/go.mod b/examples/txnkv/async_commit/go.mod index c63516097f..b07bc6f9ae 100644 --- a/examples/txnkv/async_commit/go.mod +++ b/examples/txnkv/async_commit/go.mod @@ -15,7 +15,7 @@ require ( github.com/docker/go-units v0.5.0 // indirect github.com/gogo/protobuf v1.3.2 // indirect github.com/golang/protobuf v1.5.4 // indirect - github.com/google/btree v1.1.2 // indirect + github.com/tidwall/btree v1.7.0 // indirect github.com/google/uuid v1.6.0 // indirect github.com/grpc-ecosystem/go-grpc-middleware v1.1.0 // indirect github.com/opentracing/opentracing-go v1.2.0 // indirect diff --git a/examples/txnkv/delete_range/go.mod b/examples/txnkv/delete_range/go.mod index 33036f2a78..6feebdbfb4 100644 --- a/examples/txnkv/delete_range/go.mod +++ b/examples/txnkv/delete_range/go.mod @@ -15,7 +15,7 @@ require ( github.com/docker/go-units v0.5.0 // indirect github.com/gogo/protobuf v1.3.2 // indirect github.com/golang/protobuf v1.5.4 // indirect - github.com/google/btree v1.1.2 // indirect + github.com/tidwall/btree v1.7.0 // indirect github.com/google/uuid v1.6.0 // indirect github.com/grpc-ecosystem/go-grpc-middleware v1.1.0 // indirect github.com/opentracing/opentracing-go v1.2.0 // indirect diff --git a/examples/txnkv/go.mod b/examples/txnkv/go.mod index b70e0325d9..2d11704ad9 100644 --- a/examples/txnkv/go.mod +++ b/examples/txnkv/go.mod @@ -15,7 +15,7 @@ require ( github.com/docker/go-units v0.5.0 // indirect github.com/gogo/protobuf v1.3.2 // indirect github.com/golang/protobuf v1.5.4 // indirect - github.com/google/btree v1.1.2 // indirect + github.com/tidwall/btree v1.7.0 // indirect github.com/google/uuid v1.6.0 // indirect github.com/grpc-ecosystem/go-grpc-middleware v1.1.0 // indirect github.com/opentracing/opentracing-go v1.2.0 // indirect diff --git a/examples/txnkv/pessimistic_txn/go.mod b/examples/txnkv/pessimistic_txn/go.mod index ccefa42c02..74f3a92d60 100644 --- a/examples/txnkv/pessimistic_txn/go.mod +++ b/examples/txnkv/pessimistic_txn/go.mod @@ -15,7 +15,7 @@ require ( github.com/docker/go-units v0.5.0 // indirect github.com/gogo/protobuf v1.3.2 // indirect github.com/golang/protobuf v1.5.4 // indirect - github.com/google/btree v1.1.2 // indirect + github.com/tidwall/btree v1.7.0 // indirect github.com/google/uuid v1.6.0 // indirect github.com/grpc-ecosystem/go-grpc-middleware v1.1.0 // indirect github.com/opentracing/opentracing-go v1.2.0 // indirect diff --git a/examples/txnkv/unsafedestoryrange/go.mod b/examples/txnkv/unsafedestoryrange/go.mod index 9a562abe29..84274e9efd 100644 --- a/examples/txnkv/unsafedestoryrange/go.mod +++ b/examples/txnkv/unsafedestoryrange/go.mod @@ -15,7 +15,7 @@ require ( github.com/docker/go-units v0.5.0 // indirect github.com/gogo/protobuf v1.3.2 // indirect github.com/golang/protobuf v1.5.4 // indirect - github.com/google/btree v1.1.2 // indirect + github.com/tidwall/btree v1.7.0 // indirect github.com/google/uuid v1.6.0 // indirect github.com/grpc-ecosystem/go-grpc-middleware v1.1.0 // indirect github.com/opentracing/opentracing-go v1.2.0 // indirect diff --git a/go.mod b/go.mod index d2ff6fc5e6..0f99cb7e5d 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,6 @@ require ( github.com/docker/go-units v0.5.0 github.com/gogo/protobuf v1.3.2 github.com/golang/protobuf v1.5.4 - github.com/google/btree v1.1.2 github.com/google/uuid v1.6.0 github.com/grpc-ecosystem/go-grpc-middleware v1.1.0 github.com/opentracing/opentracing-go v1.2.0 @@ -22,6 +21,7 @@ require ( github.com/prometheus/client_model v0.5.0 github.com/stretchr/testify v1.8.2 github.com/tiancaiamao/gp v0.0.0-20221230034425-4025bc8a4d4a + github.com/tidwall/btree v1.7.0 github.com/tikv/pd/client v0.0.0-20241111073742-238d4d79ea31 github.com/twmb/murmur3 v1.1.3 go.etcd.io/etcd/api/v3 v3.5.10 diff --git a/go.sum b/go.sum index 60814ed108..a7c74be025 100644 --- a/go.sum +++ b/go.sum @@ -38,8 +38,6 @@ github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps= github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM= github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= -github.com/google/btree v1.1.2 h1:xf4v41cLI2Z6FxbKm+8Bu+m8ifhj15JuZ9sa0jZCMUU= -github.com/google/btree v1.1.2/go.mod h1:qOPhT0dTNdNzV6Z/lhRX0YXUafgPLFUh+gZMl761Gm4= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= @@ -112,6 +110,8 @@ github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/tiancaiamao/gp v0.0.0-20221230034425-4025bc8a4d4a h1:J/YdBZ46WKpXsxsW93SG+q0F8KI+yFrcIDT4c/RNoc4= github.com/tiancaiamao/gp v0.0.0-20221230034425-4025bc8a4d4a/go.mod h1:h4xBhSNtOeEosLJ4P7JyKXX7Cabg7AVkWCK5gV2vOrM= +github.com/tidwall/btree v1.7.0 h1:L1fkJH/AuEh5zBnnBbmTwQ5Lt+bRJ5A8EWecslvo9iI= +github.com/tidwall/btree v1.7.0/go.mod h1:twD9XRA5jj9VUQGELzDO4HPQTNJsoWWfYEL+EUQ2cKY= github.com/tikv/pd/client v0.0.0-20241111073742-238d4d79ea31 h1:oAYc4m5Eu1OY9ogJ103VO47AYPHvhtzbUPD8L8B67Qk= github.com/tikv/pd/client v0.0.0-20241111073742-238d4d79ea31/go.mod h1:W5a0sDadwUpI9k8p7M77d3jo253ZHdmua+u4Ho4Xw8U= github.com/twmb/murmur3 v1.1.3 h1:D83U0XYKcHRYwYIpBKf3Pks91Z0Byda/9SJ8B6EMRcA= diff --git a/integration_tests/go.mod b/integration_tests/go.mod index b1168906fa..ce5085d0e5 100644 --- a/integration_tests/go.mod +++ b/integration_tests/go.mod @@ -81,6 +81,7 @@ require ( github.com/shoenig/go-m1cpu v0.1.6 // indirect github.com/spf13/pflag v1.0.5 // indirect github.com/tiancaiamao/gp v0.0.0-20221230034425-4025bc8a4d4a // indirect + github.com/tidwall/btree v1.7.0 // indirect github.com/tidwall/match v1.1.1 // indirect github.com/tidwall/pretty v1.2.0 // indirect github.com/tklauser/go-sysconf v0.3.12 // indirect diff --git a/internal/locate/region_cache.go b/internal/locate/region_cache.go index c70e137a4e..5704706aaf 100644 --- a/internal/locate/region_cache.go +++ b/internal/locate/region_cache.go @@ -50,7 +50,6 @@ import ( "unsafe" "github.com/gogo/protobuf/proto" - "github.com/google/btree" "github.com/opentracing/opentracing-go" "github.com/pingcap/kvproto/pkg/kvrpcpb" "github.com/pingcap/kvproto/pkg/metapb" @@ -2736,7 +2735,7 @@ func (c *RegionCache) gcRoundFunc(limit int) func(context.Context, time.Time) bo // Only RLock when checking TTL to avoid blocking other readers c.mu.RLock() - c.mu.sorted.b.AscendGreaterOrEqual(cursor, func(item *btreeItem) bool { + c.mu.sorted.b.Ascend(cursor, func(item *btreeItem) bool { count++ if count > limit { cursor = item @@ -2816,10 +2815,6 @@ func newBtreeSearchItem(key []byte) *btreeItem { } } -func (item *btreeItem) Less(other btree.Item) bool { - return bytes.Compare(item.key, other.(*btreeItem).key) < 0 -} - // GetID returns id. func (r *Region) GetID() uint64 { return r.meta.GetId() diff --git a/internal/locate/sorted_btree.go b/internal/locate/sorted_btree.go index ea233cc3ca..aee1f276a5 100644 --- a/internal/locate/sorted_btree.go +++ b/internal/locate/sorted_btree.go @@ -38,7 +38,7 @@ import ( "bytes" "time" - "github.com/google/btree" + "github.com/tidwall/btree" "github.com/tikv/client-go/v2/internal/logutil" "go.uber.org/zap" ) @@ -51,13 +51,13 @@ type SortedRegions struct { // NewSortedRegions returns a new SortedRegions. func NewSortedRegions(btreeDegree int) *SortedRegions { return &SortedRegions{ - b: btree.NewG(btreeDegree, func(a, b *btreeItem) bool { return a.Less(b) }), + b: btree.NewBTreeGOptions(func(a, b *btreeItem) bool { return bytes.Compare(a.key, b.key) < 0 }, btree.Options{Degree: btreeDegree, NoLocks: true}), } } // ReplaceOrInsert inserts a new item into the btree. func (s *SortedRegions) ReplaceOrInsert(cachedRegion *Region) *Region { - old, _ := s.b.ReplaceOrInsert(newBtreeItem(cachedRegion)) + old, _ := s.b.Set(newBtreeItem(cachedRegion)) if old != nil { return old.cachedRegion } @@ -66,7 +66,7 @@ func (s *SortedRegions) ReplaceOrInsert(cachedRegion *Region) *Region { // SearchByKey returns the region which contains the key. Note that the region might be expired and it's caller's duty to check the region TTL. func (s *SortedRegions) SearchByKey(key []byte, isEndKey bool) (r *Region) { - s.b.DescendLessOrEqual(newBtreeSearchItem(key), func(item *btreeItem) bool { + s.b.Descend(newBtreeSearchItem(key), func(item *btreeItem) bool { region := item.cachedRegion if isEndKey && bytes.Equal(region.StartKey(), key) { return true // iterate next item @@ -84,7 +84,7 @@ func (s *SortedRegions) SearchByKey(key []byte, isEndKey bool) (r *Region) { func (s *SortedRegions) AscendGreaterOrEqual(startKey, endKey []byte, limit int) (regions []*Region) { now := time.Now().Unix() lastStartKey := startKey - s.b.AscendGreaterOrEqual(newBtreeSearchItem(startKey), func(item *btreeItem) bool { + s.b.Ascend(newBtreeSearchItem(startKey), func(item *btreeItem) bool { region := item.cachedRegion if len(endKey) > 0 && bytes.Compare(region.StartKey(), endKey) >= 0 { return false @@ -107,7 +107,7 @@ func (s *SortedRegions) AscendGreaterOrEqual(startKey, endKey []byte, limit int) func (s *SortedRegions) removeIntersecting(r *Region, verID RegionVerID) ([]*btreeItem, bool) { var deleted []*btreeItem var stale bool - s.b.AscendGreaterOrEqual(newBtreeSearchItem(r.StartKey()), func(item *btreeItem) bool { + s.b.Ascend(newBtreeSearchItem(r.StartKey()), func(item *btreeItem) bool { if len(r.EndKey()) > 0 && bytes.Compare(item.cachedRegion.StartKey(), r.EndKey()) >= 0 { return false } @@ -132,17 +132,17 @@ func (s *SortedRegions) removeIntersecting(r *Region, verID RegionVerID) ([]*btr // Clear removes all items from the btree. func (s *SortedRegions) Clear() { - s.b.Clear(false) + s.b.Clear() } // ValidRegionsInBtree returns the number of valid regions in the btree. func (s *SortedRegions) ValidRegionsInBtree(ts int64) (len int) { - s.b.Descend(func(item *btreeItem) bool { - r := item.cachedRegion - if !r.checkRegionCacheTTL(ts) { - return true + s.b.Walk(func(items []*btreeItem) bool { + for _, item := range items { + if item.cachedRegion.checkRegionCacheTTL(ts) { + len++ + } } - len++ return true }) return diff --git a/internal/mockstore/mocktikv/mvcc.go b/internal/mockstore/mocktikv/mvcc.go index 1ea3cf5777..04008b1c2a 100644 --- a/internal/mockstore/mocktikv/mvcc.go +++ b/internal/mockstore/mocktikv/mvcc.go @@ -40,7 +40,6 @@ import ( "io" "math" - "github.com/google/btree" "github.com/pingcap/kvproto/pkg/kvrpcpb" "github.com/pkg/errors" "github.com/tikv/client-go/v2/util/codec" @@ -242,8 +241,8 @@ func (l *mvccLock) check(ts uint64, key []byte, resolvedLocks []uint64) (uint64, return 0, l.lockErr(key) } -func (e *mvccEntry) Less(than btree.Item) bool { - return bytes.Compare(e.key, than.(*mvccEntry).key) < 0 +func (e *mvccEntry) Less(than *mvccEntry) bool { + return bytes.Compare(e.key, than.key) < 0 } func (e *mvccEntry) Get(ts uint64, isoLevel kvrpcpb.IsolationLevel, resolvedLocks []uint64) ([]byte, error) {