@cryptotaxi247 / kubo / commits / 3251c29f1

metrics: add hit counter for ARC and bloom caches

License: MIT Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>

Jakub Sztandera committed Sep 5, 2016 at 17:04 UTC 3251c29f15d8b01769935a82f58e236057dd5d77
4 files changed +28 -7
blocks/blockstore/arc_cache.go
+13 -3
@@ -1,9 +1,11 @@
1 package blockstore
2
3 import (
4 - "github.com/ipfs/go-ipfs/blocks"
4 key "gx/ipfs/Qmce4Y4zg3sYr7xKM5UueS67vhNni6EeWgCRnb7MbLJMew/go-key"
5
6 + "github.com/ipfs/go-ipfs/blocks"
7 +
8 + "gx/ipfs/QmRg1gKTHzc3CZXSKzem8aR4E3TubFhbgXwfVuWnSK5CC5/go-metrics-interface"
9 lru "gx/ipfs/QmVYxfoJQiZijTgPNHCHgHELvQpbsJNTg6Crmc3dQkj3yy/golang-lru"
10 context "gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context"
11 ds "gx/ipfs/QmbzuUusHqaLLoNTDEVLcSF6vZDHZDLPC7p4bztRvvkXxU/go-datastore"
@@ -12,15 +14,21 @@ import (
14 type arccache struct {
15 arc *lru.ARCCache
16 blockstore Blockstore
17 +
18 + hits metrics.Counter
19 + total metrics.Counter
20 }
21
17 -func arcCached(bs Blockstore, lruSize int) (*arccache, error) {
22 +func newARCCachedBS(bs Blockstore, ctx context.Context, lruSize int) (*arccache, error) {
23 arc, err := lru.NewARC(lruSize)
24 if err != nil {
25 return nil, err
26 }
27 + c := &arccache{arc: arc, blockstore: bs}
28 + c.hits = metrics.NewCtx(ctx, "arc.hits_total", "Number of ARC cache hits").Counter()
29 + c.total = metrics.NewCtx(ctx, "arc_total", "Total number of ARC cache requests").Counter()
30
23 - return &arccache{arc: arc, blockstore: bs}, nil
31 + return c, nil
32 }
33
34 func (b *arccache) DeleteBlock(k key.Key) error {
@@ -42,6 +50,7 @@ func (b *arccache) DeleteBlock(k key.Key) error {
50 // if ok == false has is inconclusive
51 // if ok == true then has respons to question: is it contained
52 func (b *arccache) hasCached(k key.Key) (has bool, ok bool) {
53 + b.total.Inc()
54 if k == "" {
55 // Return cache invalid so the call to blockstore happens
56 // in case of invalid key and correct error is created.
@@ -50,6 +59,7 @@ func (b *arccache) hasCached(k key.Key) (has bool, ok bool) {
59
60 h, ok := b.arc.Get(k)
61 if ok {
62 + b.hits.Inc()
63 return h.(bool), true
64 }
65 return false, false
blocks/blockstore/arc_cache_test.go
+1 -1
@@ -140,7 +140,7 @@ func TestGetAndDeleteFalseShortCircuit(t *testing.T) {
140 }
141
142 func TestArcCreationFailure(t *testing.T) {
143 - if arc, err := arcCached(nil, -1); arc != nil || err == nil {
143 + if arc, err := newARCCachedBS(nil, context.TODO(), -1); arc != nil || err == nil {
144 t.Fatal("expected error and no cache")
145 }
146 }
blocks/blockstore/bloom_cache.go
+9 -2
@@ -6,6 +6,7 @@ import (
6 "github.com/ipfs/go-ipfs/blocks"
7 key "gx/ipfs/Qmce4Y4zg3sYr7xKM5UueS67vhNni6EeWgCRnb7MbLJMew/go-key"
8
9 + "gx/ipfs/QmVWBQQAz4Cd2XgW9KgQoqXXrU8KJoCb9WCrhWRFVBKvFe/go-metrics-interface"
10 bloom "gx/ipfs/QmWQ2SJisXwcCLsUXLwYCKSfyExXjFRW2WbBH5sqCUnwX5/bbloom"
11 context "gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context"
12 )
@@ -18,6 +19,10 @@ func bloomCached(bs Blockstore, ctx context.Context, bloomSize, hashCount int) (
19 return nil, err
20 }
21 bc := &bloomcache{blockstore: bs, bloom: bl}
22 + bc.hits = metrics.NewCtx(ctx, "bloom.hits_total",
23 + "Number of cache hits in bloom cache").Counter()
24 + bc.total = metrics.NewCtx(ctx, "bloom_total",
25 + "Total number of requests to bloom cache").Counter()
26 bc.Invalidate()
27 go bc.Rebuild(ctx)
28
@@ -33,8 +38,8 @@ type bloomcache struct {
38 blockstore Blockstore
39
40 // Statistics
36 - hits uint64
37 - misses uint64
41 + hits metrics.Counter
42 + total metrics.Counter
43 }
44
45 func (b *bloomcache) Invalidate() {
@@ -84,6 +89,7 @@ func (b *bloomcache) DeleteBlock(k key.Key) error {
89 // if ok == false has is inconclusive
90 // if ok == true then has respons to question: is it contained
91 func (b *bloomcache) hasCached(k key.Key) (has bool, ok bool) {
92 + b.total.Inc()
93 if k == "" {
94 // Return cache invalid so call to blockstore
95 // in case of invalid key is forwarded deeper
@@ -92,6 +98,7 @@ func (b *bloomcache) hasCached(k key.Key) (has bool, ok bool) {
98 if b.BloomActive() {
99 blr := b.bloom.HasTS([]byte(k))
100 if blr == false { // not contained in bloom is only conclusive answer bloom gives
101 + b.hits.Inc()
102 return false, true
103 }
104 }
blocks/blockstore/caching.go
+5 -1
@@ -3,6 +3,7 @@ package blockstore
3 import (
4 "errors"
5
6 + "gx/ipfs/QmVWBQQAz4Cd2XgW9KgQoqXXrU8KJoCb9WCrhWRFVBKvFe/go-metrics-interface"
7 context "gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context"
8 )
9
@@ -33,11 +34,14 @@ func CachedBlockstore(bs GCBlockstore,
34 if opts.HasBloomFilterSize != 0 && opts.HasBloomFilterHashes == 0 {
35 return nil, errors.New("bloom filter hash count can't be 0 when there is size set")
36 }
37 +
38 + ctx = metrics.CtxSubScope(ctx, "bs.cache")
39 +
40 if opts.HasBloomFilterSize != 0 {
41 cbs, err = bloomCached(cbs, ctx, opts.HasBloomFilterSize, opts.HasBloomFilterHashes)
42 }
43 if opts.HasARCCacheSize > 0 {
40 - cbs, err = arcCached(cbs, opts.HasARCCacheSize)
44 + cbs, err = newARCCachedBS(cbs, ctx, opts.HasARCCacheSize)
45 }
46
47 return cbs, err