forked from thanos-io/thanos
/
cache.go
184 lines (148 loc) · 4.91 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
package store
import (
"sync"
lru "github.com/hashicorp/golang-lru/simplelru"
"github.com/oklog/ulid"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/tsdb/labels"
)
const (
cacheTypePostings = "postings"
cacheTypeSeries = "series"
)
type cacheItem struct {
block ulid.ULID
key interface{}
}
func (c cacheItem) keyType() string {
switch c.key.(type) {
case cacheKeyPostings:
return cacheTypePostings
case cacheKeySeries:
return cacheTypeSeries
}
return "<unknown>"
}
type cacheKeyPostings labels.Label
type cacheKeySeries uint64
type indexCache struct {
mtx sync.Mutex
lru *lru.LRU
maxSize uint64
curSize uint64
requests *prometheus.CounterVec
hits *prometheus.CounterVec
added *prometheus.CounterVec
current *prometheus.GaugeVec
currentSize *prometheus.GaugeVec
}
// newIndexCache creates a new LRU cache for index entries and ensures the total cache
// size approximately does not exceed maxBytes.
func newIndexCache(reg prometheus.Registerer, maxBytes uint64) (*indexCache, error) {
c := &indexCache{
maxSize: maxBytes,
}
evicted := prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "thanos_store_index_cache_items_evicted_total",
Help: "Total number of items that were evicted from the index cache.",
}, []string{"item_type"})
c.added = prometheus.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.requests = prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "thanos_store_index_cache_requests_total",
Help: "Total number of requests to the cache.",
}, []string{"item_type"})
c.hits = prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "thanos_store_index_cache_hits_total",
Help: "Total number of requests to the cache that were a hit.",
}, []string{"item_type"})
c.current = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "thanos_store_index_cache_items",
Help: "Current number of items in the index cache.",
}, []string{"item_type"})
c.currentSize = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "thanos_store_index_cache_items_size_bytes",
Help: "Current byte size of items in the index cache.",
}, []string{"item_type"})
// Initialize eviction metric with 0.
evicted.WithLabelValues(cacheTypePostings).Set(0)
evicted.WithLabelValues(cacheTypeSeries).Set(0)
// Initialize LRU cache with a high size limit since we will manage evictions ourselves
// based on stored size.
onEvict := func(key, val interface{}) {
k := key.(cacheItem).keyType()
v := val.([]byte)
evicted.WithLabelValues(k).Inc()
c.current.WithLabelValues(k).Dec()
c.currentSize.WithLabelValues(k).Sub(float64(len(v)))
c.curSize -= uint64(len(v))
}
l, err := lru.NewLRU(1e12, onEvict)
if err != nil {
return nil, err
}
c.lru = l
if reg != nil {
reg.MustRegister(prometheus.NewGaugeFunc(prometheus.GaugeOpts{
Name: "thanos_store_index_cache_max_size_bytes",
Help: "Maximum number of bytes to be held in the index cache.",
}, func() float64 {
return float64(maxBytes)
}))
reg.MustRegister(c.requests, c.hits, c.added, evicted, c.current, c.currentSize)
}
return c, nil
}
func (c *indexCache) ensureFits(b []byte) {
for c.curSize+uint64(len(b)) > c.maxSize {
c.lru.RemoveOldest()
}
}
func (c *indexCache) setPostings(b ulid.ULID, l labels.Label, v []byte) {
c.added.WithLabelValues(cacheTypePostings).Inc()
c.mtx.Lock()
defer c.mtx.Unlock()
c.ensureFits(v)
// The caller may be passing in a sub-slice of a huge array. Copy the data
// to ensure we don't waste huge amounts of space for something small.
cv := make([]byte, len(v))
copy(cv, v)
c.lru.Add(cacheItem{b, cacheKeyPostings(l)}, cv)
c.currentSize.WithLabelValues(cacheTypePostings).Add(float64(len(v)))
}
func (c *indexCache) postings(b ulid.ULID, l labels.Label) ([]byte, bool) {
c.requests.WithLabelValues(cacheTypePostings).Inc()
c.mtx.Lock()
defer c.mtx.Unlock()
v, ok := c.lru.Get(cacheItem{b, cacheKeyPostings(l)})
if !ok {
return nil, false
}
c.hits.WithLabelValues(cacheTypePostings).Inc()
return v.([]byte), true
}
func (c *indexCache) setSeries(b ulid.ULID, id uint64, v []byte) {
c.added.WithLabelValues(cacheTypeSeries).Inc()
c.mtx.Lock()
defer c.mtx.Unlock()
c.ensureFits(v)
// The caller may be passing in a sub-slice of a huge array. Copy the data
// to ensure we don't waste huge amounts of space for something small.
cv := make([]byte, len(v))
copy(cv, v)
c.lru.Add(cacheItem{b, cacheKeySeries(id)}, cv)
c.currentSize.WithLabelValues(cacheTypeSeries).Add(float64(len(v)))
}
func (c *indexCache) series(b ulid.ULID, id uint64) ([]byte, bool) {
c.requests.WithLabelValues(cacheTypeSeries).Inc()
c.mtx.Lock()
defer c.mtx.Unlock()
v, ok := c.lru.Get(cacheItem{b, cacheKeySeries(id)})
if !ok {
return nil, false
}
c.hits.WithLabelValues(cacheTypeSeries).Inc()
return v.([]byte), true
}