6
6
"github.com/VictoriaMetrics/fastcache"
7
7
"github.com/line/lbm-sdk/v2/store/cachekv"
8
8
"github.com/line/lbm-sdk/v2/store/types"
9
- "github.com/line/lbm-sdk/v2/telemetry"
10
9
)
11
10
12
11
const (
@@ -27,18 +26,20 @@ type (
27
26
// CommitKVStore and below is completely irrelevant to this layer.
28
27
CommitKVStoreCache struct {
29
28
types.CommitKVStore
30
- cache * fastcache.Cache
31
- prefix []byte
29
+ cache * fastcache.Cache
30
+ prefix []byte
31
+ metrics * Metrics
32
32
}
33
33
34
34
// CommitKVStoreCacheManager maintains a mapping from a StoreKey to a
35
35
// CommitKVStoreCache. Each CommitKVStore, per StoreKey, is meant to be used
36
36
// in an inter-block (persistent) manner and typically provided by a
37
37
// CommitMultiStore.
38
38
CommitKVStoreCacheManager struct {
39
- mutex sync.Mutex
40
- cache * fastcache.Cache
41
- caches map [string ]types.CommitKVStore
39
+ mutex sync.Mutex
40
+ cache * fastcache.Cache
41
+ caches map [string ]types.CommitKVStore
42
+ metrics * Metrics
42
43
43
44
// All cache stores use the unique prefix that has one byte length
44
45
// Contract: The number of all cache stores cannot exceed 127(max byte)
@@ -47,22 +48,25 @@ type (
47
48
}
48
49
)
49
50
50
- func NewCommitKVStoreCache (store types.CommitKVStore , prefix []byte , cache * fastcache.Cache ) * CommitKVStoreCache {
51
+ func NewCommitKVStoreCache (store types.CommitKVStore , prefix []byte , cache * fastcache.Cache ,
52
+ metrics * Metrics ) * CommitKVStoreCache {
51
53
return & CommitKVStoreCache {
52
54
CommitKVStore : store ,
53
55
prefix : prefix ,
54
56
cache : cache ,
57
+ metrics : metrics ,
55
58
}
56
59
}
57
60
58
- func NewCommitKVStoreCacheManager (cacheSize int ) * CommitKVStoreCacheManager {
61
+ func NewCommitKVStoreCacheManager (cacheSize int , provider MetricsProvider ) * CommitKVStoreCacheManager {
59
62
if cacheSize <= 0 {
60
63
// This function was called because it intended to use the inter block cache, creating a cache of minimal size.
61
64
cacheSize = DefaultCommitKVStoreCacheSize
62
65
}
63
66
return & CommitKVStoreCacheManager {
64
67
cache : fastcache .New (cacheSize ),
65
68
caches : make (map [string ]types.CommitKVStore ),
69
+ metrics : provider (),
66
70
prefixMap : make (map [string ][]byte ),
67
71
prefixOrder : 0 ,
68
72
}
@@ -81,7 +85,7 @@ func (cmgr *CommitKVStoreCacheManager) GetStoreCache(key types.StoreKey, store t
81
85
if cmgr .prefixOrder <= 0 {
82
86
panic ("The number of cache stores exceed the maximum(127)" )
83
87
}
84
- cmgr .caches [key .Name ()] = NewCommitKVStoreCache (store , cmgr .prefixMap [key .Name ()], cmgr .cache )
88
+ cmgr .caches [key .Name ()] = NewCommitKVStoreCache (store , cmgr .prefixMap [key .Name ()], cmgr .cache , cmgr . metrics )
85
89
}
86
90
cmgr .mutex .Unlock ()
87
91
}
@@ -123,18 +127,18 @@ func (ckv *CommitKVStoreCache) Get(key []byte) []byte {
123
127
valueI := ckv .cache .Get (nil , prefixedKey )
124
128
if valueI != nil {
125
129
// cache hit
126
- telemetry . IncrCounter ( 1 , "store" , "inter-block-cache" , "hits" )
130
+ ckv . metrics . InterBlockCacheHits . Add ( 1 )
127
131
return valueI
128
132
}
129
133
130
134
// cache miss; write to cache
131
- telemetry . IncrCounter ( 1 , "store" , "inter-block-cache" , "misses" )
135
+ ckv . metrics . InterBlockCacheMisses . Add ( 1 )
132
136
value := ckv .CommitKVStore .Get (key )
133
137
ckv .cache .Set (prefixedKey , value )
134
138
stats := fastcache.Stats {}
135
139
ckv .cache .UpdateStats (& stats )
136
- telemetry . SetGauge ( float32 (stats .EntriesCount ), "store" , "inter-block-cache" , "entries" )
137
- telemetry . SetGauge ( float32 (stats .BytesSize ), "store" , "inter-block-cache" , "bytes" )
140
+ ckv . metrics . InterBlockCacheEntries . Set ( float64 (stats .EntriesCount ))
141
+ ckv . metrics . InterBlockCacheBytes . Set ( float64 (stats .BytesSize ))
138
142
return value
139
143
}
140
144
0 commit comments