From 9183f9ebcfb01e4c694863ab2c56801def873ada Mon Sep 17 00:00:00 2001 From: Weizhen Wang Date: Wed, 31 Jan 2024 11:03:55 +0800 Subject: [PATCH 1/6] *: remove redundant go_test Signed-off-by: Weizhen Wang --- internal/locate/region_cache.go | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/internal/locate/region_cache.go b/internal/locate/region_cache.go index e8f652e00f..d598e1b2c3 100644 --- a/internal/locate/region_cache.go +++ b/internal/locate/region_cache.go @@ -42,7 +42,6 @@ import ( "math" "math/rand" "slices" - "sort" "strconv" "strings" "sync" @@ -68,7 +67,6 @@ import ( "github.com/tikv/client-go/v2/tikvrpc" "github.com/tikv/client-go/v2/util" pd "github.com/tikv/pd/client" - atomic2 "go.uber.org/atomic" "go.uber.org/zap" "golang.org/x/sync/singleflight" "google.golang.org/grpc" @@ -220,7 +218,7 @@ type regionStore struct { // accessIndex[tiKVOnly][proxyTiKVIdx] is the index of TiKV that can forward requests to the leader in stores, -1 means not using proxy. proxyTiKVIdx AccessIndex // accessIndex[tiFlashOnly][workTiFlashIdx] is the index of the current working TiFlash in stores. - workTiFlashIdx atomic2.Int32 + workTiFlashIdx atomic.Int32 // buckets is not accurate and it can change even if the region is not changed. // It can be stale and buckets keys can be out of the region range. buckets *metapb.Buckets @@ -1049,13 +1047,13 @@ func (l *KeyLocation) LocateBucket(key []byte) *Bucket { func (l *KeyLocation) locateBucket(key []byte) *Bucket { keys := l.Buckets.GetKeys() searchLen := len(keys) - 1 - i := sort.Search(searchLen, func(i int) bool { - return bytes.Compare(key, keys[i]) < 0 + i, found := slices.BinarySearchFunc(keys, key, func(a, b []byte) int { + return bytes.Compare(a, b) }) // buckets contains region's start/end key, so i==0 means it can't find a suitable bucket // which can happen if the bucket information is stale. - if i == 0 || + if found || i == 0 || // the key isn't located in the last range. (i == searchLen && len(keys[searchLen]) != 0 && bytes.Compare(key, keys[searchLen]) >= 0) { return nil @@ -2512,9 +2510,9 @@ type Store struct { resolveMutex sync.Mutex // protect pd from concurrent init requests epoch uint32 // store fail epoch, see RegionStore.storeEpochs storeType tikvrpc.EndpointType // type of the store - tokenCount atomic2.Int64 // used store token count + tokenCount atomic.Int64 // used store token count - loadStats atomic2.Pointer[storeLoadStats] + loadStats atomic.Pointer[storeLoadStats] // whether the store is unreachable due to some reason, therefore requests to the store needs to be // forwarded by other stores. this is also the flag that a checkUntilHealth goroutine is running for this store. From 002b5272e47e2d0d526940607deedb9f7bc57713 Mon Sep 17 00:00:00 2001 From: Weizhen Wang Date: Sun, 28 Jan 2024 23:27:57 +0800 Subject: [PATCH 2/6] *: clean code Signed-off-by: Weizhen Wang --- internal/locate/region_cache.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/locate/region_cache.go b/internal/locate/region_cache.go index d598e1b2c3..cc500c7309 100644 --- a/internal/locate/region_cache.go +++ b/internal/locate/region_cache.go @@ -1053,7 +1053,7 @@ func (l *KeyLocation) locateBucket(key []byte) *Bucket { // buckets contains region's start/end key, so i==0 means it can't find a suitable bucket // which can happen if the bucket information is stale. - if found || i == 0 || + if !found || i == 0 || // the key isn't located in the last range. (i == searchLen && len(keys[searchLen]) != 0 && bytes.Compare(key, keys[searchLen]) >= 0) { return nil From 664e78af216dde615b7fe898a5fdb559a4f5752c Mon Sep 17 00:00:00 2001 From: Weizhen Wang Date: Tue, 30 Jan 2024 17:38:38 +0800 Subject: [PATCH 3/6] *: remove redundant go_test Signed-off-by: Weizhen Wang --- internal/locate/region_request.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/locate/region_request.go b/internal/locate/region_request.go index efcafbf162..c3bc11dcad 100644 --- a/internal/locate/region_request.go +++ b/internal/locate/region_request.go @@ -1809,7 +1809,7 @@ func (s *RegionRequestSender) releaseStoreToken(st *Store) { count := st.tokenCount.Load() // Decreasing tokenCount is no thread safe, preferring this for avoiding check in loop. if count > 0 { - st.tokenCount.Sub(1) + st.tokenCount.Add(-1) return } logutil.BgLogger().Warn("release store token failed, count equals to 0") From fdb029cbafdfafb611c1a5478f0df74f4dc7bb91 Mon Sep 17 00:00:00 2001 From: Weizhen Wang Date: Wed, 31 Jan 2024 10:55:41 +0800 Subject: [PATCH 4/6] *: remove redundant go_test Signed-off-by: Weizhen Wang --- internal/locate/region_cache.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/locate/region_cache.go b/internal/locate/region_cache.go index cc500c7309..f3f3b1c9e4 100644 --- a/internal/locate/region_cache.go +++ b/internal/locate/region_cache.go @@ -1048,7 +1048,7 @@ func (l *KeyLocation) locateBucket(key []byte) *Bucket { keys := l.Buckets.GetKeys() searchLen := len(keys) - 1 i, found := slices.BinarySearchFunc(keys, key, func(a, b []byte) int { - return bytes.Compare(a, b) + return -bytes.Compare(a, b) }) // buckets contains region's start/end key, so i==0 means it can't find a suitable bucket From 8408cd60c1f0e96b31ad3ba3d45bb8c68cc31c96 Mon Sep 17 00:00:00 2001 From: Weizhen Wang Date: Wed, 31 Jan 2024 10:56:55 +0800 Subject: [PATCH 5/6] *: remove redundant go_test Signed-off-by: Weizhen Wang --- internal/locate/region_cache.go | 9 +++++---- internal/locate/region_request.go | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/internal/locate/region_cache.go b/internal/locate/region_cache.go index f3f3b1c9e4..64856c0d73 100644 --- a/internal/locate/region_cache.go +++ b/internal/locate/region_cache.go @@ -67,6 +67,7 @@ import ( "github.com/tikv/client-go/v2/tikvrpc" "github.com/tikv/client-go/v2/util" pd "github.com/tikv/pd/client" + atomic2 "go.uber.org/atomic" "go.uber.org/zap" "golang.org/x/sync/singleflight" "google.golang.org/grpc" @@ -218,7 +219,7 @@ type regionStore struct { // accessIndex[tiKVOnly][proxyTiKVIdx] is the index of TiKV that can forward requests to the leader in stores, -1 means not using proxy. proxyTiKVIdx AccessIndex // accessIndex[tiFlashOnly][workTiFlashIdx] is the index of the current working TiFlash in stores. - workTiFlashIdx atomic.Int32 + workTiFlashIdx atomic2.Int32 // buckets is not accurate and it can change even if the region is not changed. // It can be stale and buckets keys can be out of the region range. buckets *metapb.Buckets @@ -1048,7 +1049,7 @@ func (l *KeyLocation) locateBucket(key []byte) *Bucket { keys := l.Buckets.GetKeys() searchLen := len(keys) - 1 i, found := slices.BinarySearchFunc(keys, key, func(a, b []byte) int { - return -bytes.Compare(a, b) + return bytes.Compare(a, b) }) // buckets contains region's start/end key, so i==0 means it can't find a suitable bucket @@ -2510,9 +2511,9 @@ type Store struct { resolveMutex sync.Mutex // protect pd from concurrent init requests epoch uint32 // store fail epoch, see RegionStore.storeEpochs storeType tikvrpc.EndpointType // type of the store - tokenCount atomic.Int64 // used store token count + tokenCount atomic2.Int64 // used store token count - loadStats atomic.Pointer[storeLoadStats] + loadStats atomic2.Pointer[storeLoadStats] // whether the store is unreachable due to some reason, therefore requests to the store needs to be // forwarded by other stores. this is also the flag that a checkUntilHealth goroutine is running for this store. diff --git a/internal/locate/region_request.go b/internal/locate/region_request.go index c3bc11dcad..efcafbf162 100644 --- a/internal/locate/region_request.go +++ b/internal/locate/region_request.go @@ -1809,7 +1809,7 @@ func (s *RegionRequestSender) releaseStoreToken(st *Store) { count := st.tokenCount.Load() // Decreasing tokenCount is no thread safe, preferring this for avoiding check in loop. if count > 0 { - st.tokenCount.Add(-1) + st.tokenCount.Sub(1) return } logutil.BgLogger().Warn("release store token failed, count equals to 0") From a6b67c277d97b176f3747d7e81576915086a61d9 Mon Sep 17 00:00:00 2001 From: Weizhen Wang Date: Fri, 23 Feb 2024 11:28:49 +0800 Subject: [PATCH 6/6] 202402022 Signed-off-by: Weizhen Wang --- internal/locate/region_cache.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/locate/region_cache.go b/internal/locate/region_cache.go index 64856c0d73..4b0bf405cc 100644 --- a/internal/locate/region_cache.go +++ b/internal/locate/region_cache.go @@ -1054,7 +1054,7 @@ func (l *KeyLocation) locateBucket(key []byte) *Bucket { // buckets contains region's start/end key, so i==0 means it can't find a suitable bucket // which can happen if the bucket information is stale. - if !found || i == 0 || + if (!found && i == 0) || // the key isn't located in the last range. (i == searchLen && len(keys[searchLen]) != 0 && bytes.Compare(key, keys[searchLen]) >= 0) { return nil