Skip to content

Commit

Permalink
use atomic.Value
Browse files Browse the repository at this point in the history
Signed-off-by: lhy1024 <[email protected]>
  • Loading branch information
lhy1024 committed Nov 11, 2024
1 parent aaea03c commit bfe01c9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 50 deletions.
8 changes: 4 additions & 4 deletions pkg/schedule/checker/checker_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"context"
"strconv"
"sync"
"sync/atomic"
"time"

"github.com/pingcap/failpoint"
Expand All @@ -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"
)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
37 changes: 0 additions & 37 deletions pkg/utils/typeutil/locked_value.go

This file was deleted.

19 changes: 10 additions & 9 deletions server/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"strconv"
"strings"
"sync"
"sync/atomic"
"time"

"github.com/coreos/go-semver/semver"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -2318,15 +2319,15 @@ 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.
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.
Expand Down Expand Up @@ -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)
}

Expand Down

0 comments on commit bfe01c9

Please sign in to comment.