Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

statistics: make hotPeerCache concurrency safe #3460

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 31 additions & 14 deletions server/statistics/hot_peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package statistics

import (
"math"
"sync"
"time"

"github.com/tikv/pd/pkg/movingaverage"
Expand All @@ -27,43 +28,59 @@ const (
)

type dimStat struct {
typ int
Rolling *movingaverage.TimeMedian // it's used to statistic hot degree and average speed.
LastAverage *movingaverage.AvgOverTime // it's used to obtain the average speed in last second as instantaneous speed.
typ int
mu struct {
sync.RWMutex
Rolling *movingaverage.TimeMedian // it's used to statistic hot degree and average speed.
LastAverage *movingaverage.AvgOverTime // it's used to obtain the average speed in last second as instantaneous speed.
}
}

func newDimStat(typ int) *dimStat {
reportInterval := RegionHeartBeatReportInterval * time.Second
return &dimStat{
Yisaer marked this conversation as resolved.
Show resolved Hide resolved
typ: typ,
Rolling: movingaverage.NewTimeMedian(DefaultAotSize, rollingWindowsSize, reportInterval),
LastAverage: movingaverage.NewAvgOverTime(reportInterval),
ds := &dimStat{
typ: typ,
}
ds.mu.Rolling = movingaverage.NewTimeMedian(DefaultAotSize, rollingWindowsSize, reportInterval)
ds.mu.LastAverage = movingaverage.NewAvgOverTime(reportInterval)
return ds
}

func (d *dimStat) Add(delta float64, interval time.Duration) {
d.LastAverage.Add(delta, interval)
d.Rolling.Add(delta, interval)
d.mu.Lock()
defer d.mu.Unlock()
d.mu.LastAverage.Add(delta, interval)
d.mu.Rolling.Add(delta, interval)
}

func (d *dimStat) isLastAverageHot(thresholds [dimLen]float64) bool {
return d.LastAverage.Get() >= thresholds[d.typ]
d.mu.RLock()
defer d.mu.RUnlock()
return d.mu.LastAverage.Get() >= thresholds[d.typ]
}

func (d *dimStat) isHot(thresholds [dimLen]float64) bool {
return d.Rolling.Get() >= thresholds[d.typ]
d.mu.RLock()
defer d.mu.RUnlock()
return d.mu.Rolling.Get() >= thresholds[d.typ]
}

func (d *dimStat) isFull() bool {
return d.LastAverage.IsFull()
d.mu.RLock()
defer d.mu.RUnlock()
return d.mu.LastAverage.IsFull()
}

func (d *dimStat) clearLastAverage() {
d.LastAverage.Clear()
d.mu.Lock()
defer d.mu.Unlock()
d.mu.LastAverage.Clear()
}

func (d *dimStat) Get() float64 {
return d.Rolling.Get()
d.mu.RLock()
defer d.mu.RUnlock()
return d.mu.Rolling.Get()
}

// HotPeerStat records each hot peer's statistics
Expand Down
27 changes: 27 additions & 0 deletions server/statistics/hot_peer_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package statistics

import (
"math/rand"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -218,6 +219,32 @@ func newPeers(n int, pid genID, sid genID) []*metapb.Peer {
return peers
}

func (t *testHotPeerCache) TestUpdateHotPeerStatInConcurrency(c *C) {
cache := NewHotStoresStats(ReadFlow)
// new peer, interval is less than report interval
newItem := &HotPeerStat{needDelete: false, thresholds: [2]float64{0.0, 0.0}}
newItem = cache.updateHotPeerStat(newItem, nil, 60, 60, 30*time.Second)
c.Check(newItem, NotNil)
c.Check(newItem.HotDegree, Equals, 0)
c.Check(newItem.AntiCount, Equals, 0)
// sum of interval is less than report interval
oldItem := newItem
newItem = cache.updateHotPeerStat(newItem, oldItem, 60, 60, 10*time.Second)
c.Check(newItem.HotDegree, Equals, 0)
c.Check(newItem.AntiCount, Equals, 0)
wg := &sync.WaitGroup{}
wg.Add(100)
oldItem = newItem
newItem = cache.updateHotPeerStat(newItem, oldItem, 60, 60, 50*time.Second)
for i := 0; i < 100; i++ {
go func(newItem, oldItem *HotPeerStat, wg *sync.WaitGroup) {
defer wg.Done()
cache.updateHotPeerStat(newItem, oldItem, 60, 60, 50*time.Second)
}(oldItem, newItem, wg)
}
wg.Wait()
}

func (t *testHotPeerCache) TestUpdateHotPeerStat(c *C) {
cache := NewHotStoresStats(ReadFlow)

Expand Down
6 changes: 0 additions & 6 deletions server/statistics/region_collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
package statistics

import (
"testing"
lhy1024 marked this conversation as resolved.
Show resolved Hide resolved

. "github.com/pingcap/check"
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/kvproto/pkg/pdpb"
Expand All @@ -25,10 +23,6 @@ import (
"github.com/tikv/pd/server/schedule/placement"
)

func TestStatistics(t *testing.T) {
Yisaer marked this conversation as resolved.
Show resolved Hide resolved
TestingT(t)
}

var _ = Suite(&testRegionStatisticsSuite{})

type testRegionStatisticsSuite struct {
Expand Down