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

Add “minuteQPS" hot-cache rudiment. #157

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
47 changes: 45 additions & 2 deletions groupcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ package groupcache
import (
"context"
"errors"
"math/rand"
"strconv"
"sync"
"sync/atomic"
"time"

pb "github.com/golang/groupcache/groupcachepb"
"github.com/golang/groupcache/lru"
Expand Down Expand Up @@ -102,7 +102,14 @@ func newGroup(name string, cacheBytes int64, getter Getter, peers PeerPicker) *G
peers: peers,
cacheBytes: cacheBytes,
loadGroup: &singleflight.Group{},
Qps: QPS{
span: time.Minute,
queryTimes: make(map[interface{}]AtomicInt, 0),
MiniteQPS: make(map[interface{}]float64, 0),
},
}
// start tick
go g.Qps.samplingQueryTimes()
if fn := newGroupHook; fn != nil {
fn(g)
}
Expand Down Expand Up @@ -171,6 +178,9 @@ type Group struct {

// Stats are statistics on the group.
Stats Stats

// record queryTime
Qps QPS
}

// flightGroup is defined as an interface which flightgroup.Group
Expand All @@ -194,6 +204,33 @@ type Stats struct {
ServerRequests AtomicInt // gets that came over the network from peers
}

type QPS struct {
sync.RWMutex //
span time.Duration
queryTimes map[interface{}]AtomicInt
MiniteQPS map[interface{}]float64
}

// samplingQueryTimes is timed task
func (q *QPS) samplingQueryTimes() {
q.Lock()
defer q.Unlock()
for k, v := range q.queryTimes {
q.MiniteQPS[k] = float64(v) / q.span.Seconds()
q.queryTimes[k] = 0
}
time.AfterFunc(q.span, func() {
q.samplingQueryTimes()
})
}

// GetQPS Get QPS of special key.
func (q *QPS) GetQPS(key interface{}) float64 {
q.RLock()
defer q.RUnlock()
return q.MiniteQPS[key]
}

// Name returns the name of the group.
func (g *Group) Name() string {
return g.name
Expand All @@ -208,6 +245,8 @@ func (g *Group) initPeers() {
func (g *Group) Get(ctx context.Context, key string, dest Sink) error {
g.peersOnce.Do(g.initPeers)
g.Stats.Gets.Add(1)
op := g.Qps.queryTimes[key]
op.Add(1)
if dest == nil {
return errors.New("groupcache: nil dest Sink")
}
Expand Down Expand Up @@ -315,7 +354,11 @@ func (g *Group) getFromPeer(ctx context.Context, peer ProtoGetter, key string) (
// TODO(bradfitz): use res.MinuteQps or something smart to
// conditionally populate hotCache. For now just do it some
// percentage of the time.
if rand.Intn(10) == 0 {
//if rand.Intn(10) == 0 {
// g.populateCache(key, value, &g.hotCache)
//}
threshold := 1000 // just a demo.
if *res.MinuteQps > float64(threshold) {
g.populateCache(key, value, &g.hotCache)
}
return value, nil
Expand Down
4 changes: 3 additions & 1 deletion groupcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ func (p *fakePeer) Get(_ context.Context, in *pb.GetRequest, out *pb.GetResponse
return errors.New("simulated error from peer")
}
out.Value = []byte("got:" + in.GetKey())
out.MinuteQps = new(float64)
return nil
}

Expand Down Expand Up @@ -305,7 +306,8 @@ func TestPeers(t *testing.T) {

// Verify cache was hit. All localHits are gone, and some of
// the peer hits (the ones randomly selected to be maybe hot)
run("cached_base", 200, "localHits = 0, peers = 49 47 48")
//run("cached_base", 200, "localHits = 0, peers = 49 47 48")
// because randomly hotchache is unused.
resetCacheSize(0)

// With one of the peers being down.
Expand Down
3 changes: 2 additions & 1 deletion http.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,9 @@ func (p *HTTPPool) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}

minuteQPS := group.Qps.GetQPS(key)
// Write the value to the response body as a proto message.
body, err := proto.Marshal(&pb.GetResponse{Value: value})
body, err := proto.Marshal(&pb.GetResponse{Value: value, MinuteQps: &minuteQPS})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
Expand Down