From bfe01c9f3dbbb3c94aa935d31d4ce59aedf689ee Mon Sep 17 00:00:00 2001 From: lhy1024 Date: Mon, 11 Nov 2024 15:10:38 +0800 Subject: [PATCH] use atomic.Value Signed-off-by: lhy1024 --- pkg/schedule/checker/checker_controller.go | 8 ++--- pkg/utils/typeutil/locked_value.go | 37 ---------------------- server/cluster/cluster.go | 19 +++++------ 3 files changed, 14 insertions(+), 50 deletions(-) delete mode 100644 pkg/utils/typeutil/locked_value.go diff --git a/pkg/schedule/checker/checker_controller.go b/pkg/schedule/checker/checker_controller.go index 40010d763bf..151799f5a81 100644 --- a/pkg/schedule/checker/checker_controller.go +++ b/pkg/schedule/checker/checker_controller.go @@ -19,6 +19,7 @@ import ( "context" "strconv" "sync" + "sync/atomic" "time" "github.com/pingcap/failpoint" @@ -33,7 +34,6 @@ import ( "github.com/tikv/pd/pkg/schedule/placement" "github.com/tikv/pd/pkg/utils/keyutil" "github.com/tikv/pd/pkg/utils/logutil" - "github.com/tikv/pd/pkg/utils/typeutil" "go.uber.org/zap" ) @@ -78,7 +78,7 @@ type Controller struct { // duration is the duration of the last patrol round. // It's exported, so it should be protected by a mutex. - duration typeutil.LockedValue[time.Duration] + duration atomic.Value // Store as time.Duration // interval is the config interval of patrol regions. // It's used to update the ticker, so we need to @@ -208,11 +208,11 @@ func (c *Controller) updatePatrolWorkersIfNeeded() { // GetPatrolRegionsDuration returns the duration of the last patrol region round. func (c *Controller) GetPatrolRegionsDuration() time.Duration { - return c.duration.Get() + return c.duration.Load().(time.Duration) } func (c *Controller) setPatrolRegionsDuration(dur time.Duration) { - c.duration.Set(dur) + c.duration.Store(dur) } func (c *Controller) checkRegions(startKey []byte) (key []byte, regions []*core.RegionInfo) { diff --git a/pkg/utils/typeutil/locked_value.go b/pkg/utils/typeutil/locked_value.go deleted file mode 100644 index 26a42032917..00000000000 --- a/pkg/utils/typeutil/locked_value.go +++ /dev/null @@ -1,37 +0,0 @@ -// Copyright 2024 TiKV Project Authors. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package typeutil - -import "github.com/tikv/pd/pkg/utils/syncutil" - -// LockedValue is a thread-safe value holder. -type LockedValue[T any] struct { - syncutil.Mutex - val T -} - -// Set sets the value. -func (lv *LockedValue[T]) Set(value T) { - lv.Lock() - defer lv.Unlock() - lv.val = value -} - -// Get gets the value. -func (lv *LockedValue[T]) Get() T { - lv.Lock() - defer lv.Unlock() - return lv.val -} diff --git a/server/cluster/cluster.go b/server/cluster/cluster.go index cda91d165bb..040db1dfe2a 100644 --- a/server/cluster/cluster.go +++ b/server/cluster/cluster.go @@ -26,6 +26,7 @@ import ( "strconv" "strings" "sync" + "sync/atomic" "time" "github.com/coreos/go-semver/semver" @@ -157,8 +158,8 @@ type RaftCluster struct { isAPIServiceMode bool meta *metapb.Cluster storage storage.Storage - minResolvedTS typeutil.LockedValue[uint64] - externalTS typeutil.LockedValue[uint64] + minResolvedTS atomic.Value // Store as uint64 + externalTS atomic.Value // Store as uint64 // Keep the previous store limit settings when removing a store. prevStoreLimit map[uint64]map[storelimit.Type]float64 @@ -358,7 +359,7 @@ func (c *RaftCluster) Start(s Server) error { if err != nil { log.Error("load external timestamp meets error", zap.Error(err)) } - c.externalTS.Set(externalTS) + c.externalTS.Store(externalTS) if c.isAPIServiceMode { // bootstrap keyspace group manager after starting other parts successfully. @@ -2269,11 +2270,11 @@ func (c *RaftCluster) CheckAndUpdateMinResolvedTS() (uint64, bool) { newMinResolvedTS = s.GetMinResolvedTS() } } - oldMinResolvedTS := c.minResolvedTS.Get() + oldMinResolvedTS := c.minResolvedTS.Load().(uint64) if newMinResolvedTS == math.MaxUint64 || newMinResolvedTS <= oldMinResolvedTS { return oldMinResolvedTS, false } - c.minResolvedTS.Set(newMinResolvedTS) + c.minResolvedTS.Store(newMinResolvedTS) return newMinResolvedTS, true } @@ -2318,7 +2319,7 @@ func (c *RaftCluster) loadMinResolvedTS() { log.Error("load min resolved ts meet error", errs.ZapError(err)) return } - c.minResolvedTS.Set(minResolvedTS) + c.minResolvedTS.Store(minResolvedTS) } // GetMinResolvedTS returns the min resolved ts of the cluster. @@ -2326,7 +2327,7 @@ func (c *RaftCluster) GetMinResolvedTS() uint64 { if !c.isInitialized() { return math.MaxUint64 } - return c.minResolvedTS.Get() + return c.minResolvedTS.Load().(uint64) } // GetStoreMinResolvedTS returns the min resolved ts of the store. @@ -2361,12 +2362,12 @@ func (c *RaftCluster) GetExternalTS() uint64 { if !c.isInitialized() { return math.MaxUint64 } - return c.externalTS.Get() + return c.externalTS.Load().(uint64) } // SetExternalTS sets the external timestamp. func (c *RaftCluster) SetExternalTS(timestamp uint64) error { - c.externalTS.Set(timestamp) + c.externalTS.Store(timestamp) return c.storage.SaveExternalTS(timestamp) }