-
Notifications
You must be signed in to change notification settings - Fork 781
/
inmemory_index_cache.go
230 lines (193 loc) · 8.85 KB
/
inmemory_index_cache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package tsdb
import (
"context"
"unsafe"
"github.com/VictoriaMetrics/fastcache"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/oklog/ulid"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/storage"
storecache "github.com/thanos-io/thanos/pkg/store/cache"
"github.com/thanos-io/thanos/pkg/tenancy"
)
type InMemoryIndexCache struct {
logger log.Logger
cache *fastcache.Cache
maxItemSizeBytes uint64
added *prometheus.CounterVec
overflow *prometheus.CounterVec
commonMetrics *storecache.CommonMetrics
}
// NewInMemoryIndexCacheWithConfig creates a new thread-safe cache for index entries. It relies on the cache library
// (fastcache) to ensures the total cache size approximately does not exceed maxBytes.
func NewInMemoryIndexCacheWithConfig(logger log.Logger, commonMetrics *storecache.CommonMetrics, reg prometheus.Registerer, config storecache.InMemoryIndexCacheConfig) (*InMemoryIndexCache, error) {
if config.MaxItemSize > config.MaxSize {
return nil, errors.Errorf("max item size (%v) cannot be bigger than overall cache size (%v)", config.MaxItemSize, config.MaxSize)
}
// fastcache will panic if MaxSize <= 0.
if config.MaxSize <= 0 {
config.MaxSize = storecache.DefaultInMemoryIndexCacheConfig.MaxSize
}
if commonMetrics == nil {
commonMetrics = storecache.NewCommonMetrics(reg)
}
c := &InMemoryIndexCache{
logger: logger,
maxItemSizeBytes: uint64(config.MaxItemSize),
commonMetrics: commonMetrics,
}
c.added = promauto.With(reg).NewCounterVec(prometheus.CounterOpts{
Name: "thanos_store_index_cache_items_added_total",
Help: "Total number of items that were added to the index cache.",
}, []string{"item_type"})
c.added.WithLabelValues(cacheTypePostings)
c.added.WithLabelValues(cacheTypeSeries)
c.added.WithLabelValues(cacheTypeExpandedPostings)
c.commonMetrics.RequestTotal.WithLabelValues(cacheTypePostings, tenancy.DefaultTenant)
c.commonMetrics.RequestTotal.WithLabelValues(cacheTypeSeries, tenancy.DefaultTenant)
c.commonMetrics.RequestTotal.WithLabelValues(cacheTypeExpandedPostings, tenancy.DefaultTenant)
c.overflow = promauto.With(reg).NewCounterVec(prometheus.CounterOpts{
Name: "thanos_store_index_cache_items_overflowed_total",
Help: "Total number of items that could not be added to the cache due to being too big.",
}, []string{"item_type"})
c.overflow.WithLabelValues(cacheTypePostings)
c.overflow.WithLabelValues(cacheTypeSeries)
c.overflow.WithLabelValues(cacheTypeExpandedPostings)
c.commonMetrics.HitsTotal.WithLabelValues(cacheTypePostings, tenancy.DefaultTenant)
c.commonMetrics.HitsTotal.WithLabelValues(cacheTypeSeries, tenancy.DefaultTenant)
c.commonMetrics.HitsTotal.WithLabelValues(cacheTypeExpandedPostings, tenancy.DefaultTenant)
c.cache = fastcache.New(int(config.MaxSize))
level.Info(logger).Log(
"msg", "created in-memory index cache",
"maxItemSizeBytes", c.maxItemSizeBytes,
"maxSizeBytes", config.MaxSize,
)
return c, nil
}
func (c *InMemoryIndexCache) get(key storecache.CacheKey) ([]byte, bool) {
k := yoloBuf(key.String())
resp := c.cache.GetBig(nil, k)
if len(resp) == 0 {
return nil, false
}
return resp, true
}
func (c *InMemoryIndexCache) set(typ string, key storecache.CacheKey, val []byte) {
k := yoloBuf(key.String())
r := c.cache.GetBig(nil, k)
// item exists, no need to set it again.
if r != nil {
return
}
size := uint64(len(k) + len(val))
if size > c.maxItemSizeBytes {
level.Info(c.logger).Log(
"msg", "item bigger than maxItemSizeBytes. Ignoring..",
"maxItemSizeBytes", c.maxItemSizeBytes,
"cacheType", typ,
)
c.overflow.WithLabelValues(typ).Inc()
return
}
c.cache.SetBig(k, val)
c.added.WithLabelValues(typ).Inc()
}
func yoloBuf(s string) []byte {
return *(*[]byte)(unsafe.Pointer(&s))
}
func copyString(s string) string {
return string(unsafe.Slice(unsafe.StringData(s), len(s)))
}
// copyToKey is required as underlying strings might be memory-mapped.
func copyToKey(l labels.Label) storecache.CacheKeyPostings {
return storecache.CacheKeyPostings(labels.Label{Value: copyString(l.Value), Name: copyString(l.Name)})
}
// StorePostings sets the postings identified by the ulid and label to the value v,
// if the postings already exists in the cache it is not mutated.
func (c *InMemoryIndexCache) StorePostings(blockID ulid.ULID, l labels.Label, v []byte, tenant string) {
c.commonMetrics.DataSizeBytes.WithLabelValues(cacheTypePostings, tenant).Observe(float64(len(v)))
c.set(cacheTypePostings, storecache.CacheKey{Block: blockID.String(), Key: copyToKey(l)}, v)
}
// FetchMultiPostings fetches multiple postings - each identified by a label -
// and returns a map containing cache hits, along with a list of missing keys.
func (c *InMemoryIndexCache) FetchMultiPostings(ctx context.Context, blockID ulid.ULID, keys []labels.Label, tenant string) (hits map[labels.Label][]byte, misses []labels.Label) {
timer := prometheus.NewTimer(c.commonMetrics.FetchLatency.WithLabelValues(cacheTypePostings, tenant))
defer timer.ObserveDuration()
hits = map[labels.Label][]byte{}
blockIDKey := blockID.String()
requests := 0
hit := 0
for _, key := range keys {
if ctx.Err() != nil {
c.commonMetrics.RequestTotal.WithLabelValues(cacheTypePostings, tenant).Add(float64(requests))
c.commonMetrics.HitsTotal.WithLabelValues(cacheTypePostings, tenant).Add(float64(hit))
return hits, misses
}
requests++
if b, ok := c.get(storecache.CacheKey{Block: blockIDKey, Key: storecache.CacheKeyPostings(key)}); ok {
hit++
hits[key] = b
continue
}
misses = append(misses, key)
}
c.commonMetrics.RequestTotal.WithLabelValues(cacheTypePostings, tenant).Add(float64(requests))
c.commonMetrics.HitsTotal.WithLabelValues(cacheTypePostings, tenant).Add(float64(hit))
return hits, misses
}
// StoreExpandedPostings stores expanded postings for a set of label matchers.
func (c *InMemoryIndexCache) StoreExpandedPostings(blockID ulid.ULID, matchers []*labels.Matcher, v []byte, tenant string) {
c.commonMetrics.DataSizeBytes.WithLabelValues(cacheTypeExpandedPostings, tenant).Observe(float64(len(v)))
c.set(cacheTypeExpandedPostings, storecache.CacheKey{Block: blockID.String(), Key: storecache.CacheKeyExpandedPostings(storecache.LabelMatchersToString(matchers))}, v)
}
// FetchExpandedPostings fetches expanded postings and returns cached data and a boolean value representing whether it is a cache hit or not.
func (c *InMemoryIndexCache) FetchExpandedPostings(ctx context.Context, blockID ulid.ULID, matchers []*labels.Matcher, tenant string) ([]byte, bool) {
timer := prometheus.NewTimer(c.commonMetrics.FetchLatency.WithLabelValues(cacheTypeExpandedPostings, tenant))
defer timer.ObserveDuration()
if ctx.Err() != nil {
return nil, false
}
c.commonMetrics.RequestTotal.WithLabelValues(cacheTypeExpandedPostings, tenant).Inc()
if b, ok := c.get(storecache.CacheKey{Block: blockID.String(), Key: storecache.CacheKeyExpandedPostings(storecache.LabelMatchersToString(matchers))}); ok {
c.commonMetrics.HitsTotal.WithLabelValues(cacheTypeExpandedPostings, tenant).Inc()
return b, true
}
return nil, false
}
// StoreSeries sets the series identified by the ulid and id to the value v,
// if the series already exists in the cache it is not mutated.
func (c *InMemoryIndexCache) StoreSeries(blockID ulid.ULID, id storage.SeriesRef, v []byte, tenant string) {
c.commonMetrics.DataSizeBytes.WithLabelValues(cacheTypeSeries, tenant).Observe(float64(len(v)))
c.set(cacheTypeSeries, storecache.CacheKey{Block: blockID.String(), Key: storecache.CacheKeySeries(id)}, v)
}
// FetchMultiSeries fetches multiple series - each identified by ID - from the cache
// and returns a map containing cache hits, along with a list of missing IDs.
func (c *InMemoryIndexCache) FetchMultiSeries(ctx context.Context, blockID ulid.ULID, ids []storage.SeriesRef, tenant string) (hits map[storage.SeriesRef][]byte, misses []storage.SeriesRef) {
timer := prometheus.NewTimer(c.commonMetrics.FetchLatency.WithLabelValues(cacheTypeSeries, tenant))
defer timer.ObserveDuration()
hits = map[storage.SeriesRef][]byte{}
blockIDKey := blockID.String()
requests := 0
hit := 0
for _, id := range ids {
if ctx.Err() != nil {
c.commonMetrics.RequestTotal.WithLabelValues(cacheTypeSeries, tenant).Add(float64(requests))
c.commonMetrics.HitsTotal.WithLabelValues(cacheTypeSeries, tenant).Add(float64(hit))
return hits, misses
}
requests++
if b, ok := c.get(storecache.CacheKey{Block: blockIDKey, Key: storecache.CacheKeySeries(id)}); ok {
hit++
hits[id] = b
continue
}
misses = append(misses, id)
}
c.commonMetrics.RequestTotal.WithLabelValues(cacheTypeSeries, tenant).Add(float64(requests))
c.commonMetrics.HitsTotal.WithLabelValues(cacheTypeSeries, tenant).Add(float64(hit))
return hits, misses
}