-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.go
492 lines (381 loc) · 13.7 KB
/
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
package mutable
import (
"bytes"
"context"
"encoding/gob"
"sync"
"time"
"github.com/bleemeo/squirreldb/types"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/prometheus/model/labels"
"github.com/rs/zerolog"
)
const (
topicInvalidateAssociatedNames = "invalidate-associated-names"
topicInvalidateAssociatedValues = "invalidate-associated-values"
topicInvalidateMutableLabels = "invalidate-mutable-labels"
cacheCleanupInterval = 24 * time.Hour
cacheExpirationDelay = time.Hour
)
type cache struct {
cluster types.Cluster
metrics *metrics
logger zerolog.Logger
l sync.Mutex
// Name cache indexed by tenant.
nameCache map[string]nameEntry
valuesCache map[LabelKey]valuesEntry
// mutableLabelsCache contains the mutable labels associated to a non mutable label.
mutableLabelsCache map[Label]mutableLabelsEntry
}
type valuesEntry struct {
lastAccess time.Time
// associatedValues[value] contains the non mutable associated values.
// This map must always be reassigned entirely.
// Values() assumes it always contains all possible values.
associatedValues map[string][]string
}
type nameEntry struct {
lastAccess time.Time
// associatedNames[mutable name] contains the associated non mutable name.
// The map must always either contain no key at all, or all names
// present in Cassandra. It must be reassigned entirely when modified.
associatedNames map[string]string
}
type mutableLabelsEntry struct {
lastAccess time.Time
labels labels.Labels
}
func newCache(
ctx context.Context,
reg prometheus.Registerer,
cluster types.Cluster,
logger zerolog.Logger,
) *cache {
c := cache{
cluster: cluster,
metrics: newMetrics(reg),
nameCache: make(map[string]nameEntry),
valuesCache: make(map[LabelKey]valuesEntry),
mutableLabelsCache: make(map[Label]mutableLabelsEntry),
logger: logger,
}
c.cluster.Subscribe(topicInvalidateAssociatedValues, c.invalidateAssociatedValuesListener)
c.cluster.Subscribe(topicInvalidateAssociatedNames, c.invalidateAssociatedNamesListener)
c.cluster.Subscribe(topicInvalidateMutableLabels, c.invalidateMutableLabelsListener)
go c.cleanupScheduler(ctx)
return &c
}
func (c *cache) cleanupScheduler(ctx context.Context) {
cleanupTicker := time.NewTicker(cacheCleanupInterval)
for ctx.Err() == nil {
select {
case <-cleanupTicker.C:
c.cleanup()
case <-ctx.Done():
cleanupTicker.Stop()
return
}
}
cleanupTicker.Stop()
}
func (c *cache) cleanup() {
c.l.Lock()
defer c.l.Unlock()
now := time.Now()
// Delete expired entries in values cache.
for key, entry := range c.valuesCache {
if entry.lastAccess.Add(cacheExpirationDelay).Before(now) {
c.logger.Debug().Msgf("Deleting expired mutable values cache entry %#v", key)
delete(c.valuesCache, key)
}
}
c.metrics.CacheSize.WithLabelValues("values").Set(float64(len(c.valuesCache)))
// Delete expired entries in name cache.
for key, entry := range c.nameCache {
if entry.lastAccess.Add(cacheExpirationDelay).Before(now) {
c.logger.Debug().Msgf("Deleting expired mutable name cache entry %#v", key)
delete(c.nameCache, key)
}
}
c.metrics.CacheSize.WithLabelValues("names").Set(float64(len(c.nameCache)))
// Delete expired entries in mutable labels cache.
for key, entry := range c.mutableLabelsCache {
if entry.lastAccess.Add(cacheExpirationDelay).Before(now) {
c.logger.Debug().Msgf("Deleting expired mutable labels cache entry %#v", key)
delete(c.mutableLabelsCache, key)
}
}
}
// SetAllAssociatedValues sets mutable label values in cache.
// associatedValuesByValue must contain all possible values for this tenant and label name.
func (c *cache) SetAllAssociatedValues(tenant, name string, associatedValuesByValue map[string][]string) {
c.l.Lock()
defer c.l.Unlock()
key := LabelKey{
Tenant: tenant,
Name: name,
}
c.valuesCache[key] = valuesEntry{
lastAccess: time.Now(),
associatedValues: associatedValuesByValue,
}
c.metrics.CacheSize.WithLabelValues("values").Set(float64(len(c.valuesCache)))
}
// AssociatedValues returns the label values associated to a mutable label name and value of a tenant.
// It can return an empty slice if the cache is up to date butno associated values exist for this
// tenant, name and value.
func (c *cache) AssociatedValues(tenant, name, value string) (associatedValues []string, found bool) {
c.l.Lock()
defer c.l.Unlock()
defer func() {
c.metrics.CacheAccess.WithLabelValues("values", metricStatus(found)).Inc()
}()
key := LabelKey{
Tenant: tenant,
Name: name,
}
entry, found := c.valuesCache[key]
if !found {
return nil, false
}
// Update last access.
entry.lastAccess = time.Now()
c.valuesCache[key] = entry
associatedValues = entry.associatedValues[value]
// Always return true here because the cache for a tenant and label name is always in sync
// with Cassandra. If the associated values were not found, it means they are not present
// in Cassandra either, and there is no need to refresh the cache.
return associatedValues, true
}
// InvalidateAssociatedValues invalidates the non mutable values associated to a mutable label,
// and tells other SquirrelDBs in the cluster to do the same.
func (c *cache) InvalidateAssociatedValues(ctx context.Context, keys []LabelKey) {
// Notify the cluster, we also notify ourselves so the cache will be deleted by the listener.
buffer := bytes.NewBuffer(nil)
encoder := gob.NewEncoder(buffer)
if err := encoder.Encode(keys); err != nil {
c.logger.Warn().Err(err).Msg("Failed encode message, the associated values cache won't be invalidated")
}
err := c.cluster.Publish(ctx, topicInvalidateAssociatedValues, buffer.Bytes())
if err != nil {
c.logger.Warn().Err(err).Msg("Failed to publish a message, the associated values cache won't be invalidated")
}
}
// invalidateAssociatedValuesListener listens for messages from the cluster to invalidate the cache.
func (c *cache) invalidateAssociatedValuesListener(buffer []byte) {
c.logger.Debug().Msg("Invalidating mutable labels associated values cache")
var keys []LabelKey
decoder := gob.NewDecoder(bytes.NewReader(buffer))
if err := decoder.Decode(&keys); err != nil {
c.logger.Warn().Err(err).Msg("Unable to decode associated values cache keys, the values cache won't be invalidated")
return
}
c.l.Lock()
defer c.l.Unlock()
for _, key := range keys {
delete(c.valuesCache, key)
}
c.metrics.CacheSize.WithLabelValues("values").Set(float64(len(c.valuesCache)))
}
// Values returns all the mutable label values associated to a tenant and a label name.
func (c *cache) Values(tenant, name string) (values []string, found bool) {
c.l.Lock()
defer c.l.Unlock()
defer func() {
c.metrics.CacheAccess.WithLabelValues("values", metricStatus(found)).Inc()
}()
key := LabelKey{
Tenant: tenant,
Name: name,
}
entry, found := c.valuesCache[key]
if !found {
return nil, false
}
// Update last access.
entry.lastAccess = time.Now()
c.valuesCache[key] = entry
// When not empty, the values cache for a key always contains all rows from Cassandra.
for value := range entry.associatedValues {
values = append(values, value)
}
return values, true
}
// SetAllAssociatedNames sets all mutable label names and their associated non mutable names in cache.
// associatedNames must contain all possible mutable label names for this tenant.
func (c *cache) SetAllAssociatedNames(tenant string, associatedNames map[string]string) {
c.l.Lock()
defer c.l.Unlock()
c.nameCache[tenant] = nameEntry{
lastAccess: time.Now(),
associatedNames: associatedNames,
}
c.metrics.CacheSize.WithLabelValues("names").Set(float64(len(c.nameCache)))
}
// NonMutableName returns the non mutable label name associated to a mutable name and a tenant.
// It can return an empty name if the cache is up to date but the associated name for this
// tenant and name simply doesn't exist.
func (c *cache) NonMutableName(tenant, name string) (nonMutableName string, found bool) {
c.l.Lock()
defer c.l.Unlock()
defer func() {
c.metrics.CacheAccess.WithLabelValues("names", metricStatus(found)).Inc()
}()
if c.nameCache == nil {
return "", false
}
entry, found := c.nameCache[tenant]
if !found {
return "", false
}
// Update last access.
entry.lastAccess = time.Now()
c.nameCache[tenant] = entry
nonMutableName = entry.associatedNames[name]
// Always return true here because the cache for a tenant is always in sync with Cassandra.
// If the associated name was not found, it means it's not present in Cassandra either,
// and there is no need to refresh the cache.
return nonMutableName, true
}
// InvalidateAssociatedNames invalidates the mutable labels names cache and tells other
// SquirrelDBs in the cluster to do the same.
func (c *cache) InvalidateAssociatedNames(ctx context.Context, tenants []string) {
// Notify the cluster, we also notify ourselves so the cache will be deleted by the listener.
buffer := bytes.NewBuffer(nil)
encoder := gob.NewEncoder(buffer)
if err := encoder.Encode(tenants); err != nil {
c.logger.Warn().Err(err).Msg("Failed encode message, the associated names cache won't be invalidated")
}
err := c.cluster.Publish(ctx, topicInvalidateAssociatedNames, buffer.Bytes())
if err != nil {
c.logger.Warn().Err(err).Msg("Failed to publish a message, the associated names cache won't be invalidated")
}
}
// invalidateAssociatedNamesListener listens for messages from the cluster to invalidate the cache.
func (c *cache) invalidateAssociatedNamesListener(buffer []byte) {
c.logger.Debug().Msg("Invalidating mutable labels associated names cache")
var tenants []string
decoder := gob.NewDecoder(bytes.NewReader(buffer))
if err := decoder.Decode(&tenants); err != nil {
c.logger.Warn().Err(err).Msg("Unable to decode tenants, the name cache won't be invalidated")
return
}
c.l.Lock()
defer c.l.Unlock()
for _, tenant := range tenants {
delete(c.nameCache, tenant)
}
c.metrics.CacheSize.WithLabelValues("names").Set(float64(len(c.valuesCache)))
}
// MutableLabelNames returns the mutable label names associated to a name and a tenant.
func (c *cache) MutableLabelNames(tenant, name string) (mutableNames []string, found bool) {
c.l.Lock()
defer c.l.Unlock()
defer func() {
c.metrics.CacheAccess.WithLabelValues("names", metricStatus(found)).Inc()
}()
entry, found := c.nameCache[tenant]
if !found {
return nil, false
}
// Update last access.
entry.lastAccess = time.Now()
c.nameCache[tenant] = entry
for mutableName, nonMutableName := range entry.associatedNames {
if name == nonMutableName {
mutableNames = append(mutableNames, mutableName)
}
}
return mutableNames, true
}
// AllMutableLabelNames returns all the mutable label names in cache for a tenant.
func (c *cache) AllMutableLabelNames(tenant string) (names []string, found bool) {
c.l.Lock()
defer c.l.Unlock()
defer func() {
c.metrics.CacheAccess.WithLabelValues("names", metricStatus(found)).Inc()
}()
entry, found := c.nameCache[tenant]
if !found {
return nil, false
}
// Update last access.
entry.lastAccess = time.Now()
c.nameCache[tenant] = entry
// When not nil, the name cache for a tenant always contains all rows from Cassandra.
for name := range entry.associatedNames {
names = append(names, name)
}
return names, true
}
// MutableLabels returns the mutable labels corresponding to a non mutable label name and value.
func (c *cache) MutableLabels(tenant, name, value string) (lbls labels.Labels, found bool) {
c.l.Lock()
defer c.l.Unlock()
defer func() {
c.metrics.CacheAccess.WithLabelValues("labels", metricStatus(found)).Inc()
}()
key := Label{
Tenant: tenant,
Name: name,
Value: value,
}
entry, found := c.mutableLabelsCache[key]
if !found {
return nil, false
}
// Update last access.
entry.lastAccess = time.Now()
c.mutableLabelsCache[key] = entry
return entry.labels, true
}
// SetMutableLabels sets the mutable labels corresponding to a non mutable label name and value.
func (c *cache) SetMutableLabels(tenant, name, value string, lbls labels.Labels) {
c.l.Lock()
defer c.l.Unlock()
key := Label{
Tenant: tenant,
Name: name,
Value: value,
}
c.mutableLabelsCache[key] = mutableLabelsEntry{
lastAccess: time.Now(),
labels: lbls,
}
c.metrics.CacheSize.WithLabelValues("labels").Set(float64(len(c.mutableLabelsCache)))
}
// InvalidateMutableLabels invalidates the mutable labels cache and tells other
// SquirrelDBs in the cluster to do the same.
func (c *cache) InvalidateMutableLabels(ctx context.Context, tenants []string) {
// Notify the cluster, we also notify ourselves so the cache will be deleted by the listener.
buffer := bytes.NewBuffer(nil)
encoder := gob.NewEncoder(buffer)
if err := encoder.Encode(tenants); err != nil {
c.logger.Warn().Err(err).Msg("Failed encode message, the associated names cache won't be invalidated")
}
err := c.cluster.Publish(ctx, topicInvalidateMutableLabels, buffer.Bytes())
if err != nil {
c.logger.Warn().Err(err).Msg("Failed to publish a message, the associated names cache won't be invalidated")
}
}
// invalidateMutableLabelsListener listens for messages from the cluster to invalidate the cache.
func (c *cache) invalidateMutableLabelsListener(buffer []byte) {
c.logger.Debug().Msg("Invalidating mutable labels cache")
var tenants []string
decoder := gob.NewDecoder(bytes.NewReader(buffer))
if err := decoder.Decode(&tenants); err != nil {
c.logger.Warn().Err(err).Msg("Unable to decode tenants, the mutable labels cache won't be invalidated")
return
}
c.l.Lock()
defer c.l.Unlock()
for _, tenant := range tenants {
for key := range c.mutableLabelsCache {
if key.Tenant == tenant {
delete(c.mutableLabelsCache, key)
}
}
}
c.metrics.CacheSize.WithLabelValues("names").Set(float64(len(c.valuesCache)))
}