forked from grafana/loki
/
memstore.go
373 lines (316 loc) · 10.6 KB
/
memstore.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
package ruler
import (
"context"
"errors"
"sync"
"time"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/promql"
"github.com/prometheus/prometheus/rules"
"github.com/prometheus/prometheus/storage"
"github.com/grafana/loki/pkg/querier/series"
"github.com/grafana/loki/pkg/util"
)
const (
AlertForStateMetricName = "ALERTS_FOR_STATE"
statusSuccess = "success"
statusFailure = "failure"
)
func ForStateMetric(base labels.Labels, alertName string) labels.Labels {
b := labels.NewBuilder(base)
b.Set(labels.MetricName, AlertForStateMetricName)
b.Set(labels.AlertName, alertName)
return b.Labels()
}
type memstoreMetrics struct {
evaluations *prometheus.CounterVec
samples prometheus.Gauge // in memory samples
cacheHits *prometheus.CounterVec // cache hits on in memory samples
}
func newMemstoreMetrics(r prometheus.Registerer) *memstoreMetrics {
return &memstoreMetrics{
evaluations: promauto.With(r).NewCounterVec(prometheus.CounterOpts{
Namespace: "loki",
Name: "ruler_memory_for_state_evaluations_total",
}, []string{"status", "tenant"}),
samples: promauto.With(r).NewGauge(prometheus.GaugeOpts{
Namespace: "loki",
Name: "ruler_memory_samples",
}),
cacheHits: promauto.With(r).NewCounterVec(prometheus.CounterOpts{
Namespace: "loki",
Name: "ruler_memory_for_state_cache_hits_total",
}, []string{"tenant"}),
}
}
type RuleIter interface {
AlertingRules() []*rules.AlertingRule
}
type MemStore struct {
mtx sync.Mutex
userID string
queryFunc rules.QueryFunc
metrics *memstoreMetrics
mgr RuleIter
logger log.Logger
rules map[string]*RuleCache
initiated chan struct{}
done chan struct{}
cleanupInterval time.Duration
}
func NewMemStore(userID string, queryFunc rules.QueryFunc, metrics *memstoreMetrics, cleanupInterval time.Duration, logger log.Logger) *MemStore {
s := &MemStore{
userID: userID,
metrics: metrics,
queryFunc: queryFunc,
logger: log.With(logger, "subcomponent", "MemStore", "user", userID),
cleanupInterval: cleanupInterval,
rules: make(map[string]*RuleCache),
initiated: make(chan struct{}), // blocks execution until Start() is called
done: make(chan struct{}),
}
return s
}
// Calling Start will set the RuleIter, unblock the MemStore, and start the run() function in a separate goroutine.
func (m *MemStore) Start(iter RuleIter) {
m.mgr = iter
close(m.initiated)
go m.run()
}
func (m *MemStore) Stop() {
select {
case <-m.initiated:
default:
// If initiated is blocked, the MemStore has yet to start: easy no-op.
return
}
// Need to nil all series & decrement gauges
m.mtx.Lock()
defer m.mtx.Unlock()
select {
// ensures Stop() is idempotent
case <-m.done:
return
default:
for ruleKey, cache := range m.rules {
// Force cleanup of all samples older than time.Now (all of them).
_ = cache.CleanupOldSamples(time.Now())
delete(m.rules, ruleKey)
}
close(m.done)
}
}
// run periodically cleans up old series/samples to ensure memory consumption doesn't grow unbounded.
func (m *MemStore) run() {
<-m.initiated
t := time.NewTicker(m.cleanupInterval)
for {
select {
case <-m.done:
t.Stop()
return
case <-t.C:
m.mtx.Lock()
holdDurs := make(map[string]time.Duration)
for _, rule := range m.mgr.AlertingRules() {
holdDurs[rule.Name()] = rule.HoldDuration()
}
for ruleKey, cache := range m.rules {
dur, ok := holdDurs[ruleKey]
// rule is no longer being tracked, remove it
if !ok {
_ = cache.CleanupOldSamples(time.Now())
delete(m.rules, ruleKey)
continue
}
// trim older samples out of tracking bounds, doubled to buffer.
if empty := cache.CleanupOldSamples(time.Now().Add(-2 * dur)); empty {
delete(m.rules, ruleKey)
}
}
m.mtx.Unlock()
}
}
}
// implement storage.Queryable. It is only called with the desired ts as maxtime. Mint is
// parameterized via the outage tolerance, but since we're synthetically generating these,
// we only care about the desired time.
func (m *MemStore) Querier(ctx context.Context, mint, maxt int64) (storage.Querier, error) {
<-m.initiated
return &memStoreQuerier{
ts: util.TimeFromMillis(maxt),
MemStore: m,
ctx: ctx,
}, nil
}
type memStoreQuerier struct {
ts time.Time
ctx context.Context
*MemStore
}
// Select implements storage.Querier but takes advantage of the fact that it's only called when restoring for state
// in order to lookup & cache previous rule evaluations. This results in a sort of synthetic metric store.
func (m *memStoreQuerier) Select(sortSeries bool, params *storage.SelectHints, matchers ...*labels.Matcher) storage.SeriesSet {
b := labels.NewBuilder(nil)
var ruleKey string
for _, matcher := range matchers {
// Since Select is only called to restore the for state of an alert, we can deduce two things:
// 1) The matchers will all be in the form {foo="bar"}. This means we can construct the cache entry from these matchers.
// 2) The alertname label value can be used to discover the rule this query is associated with.
b.Set(matcher.Name, matcher.Value)
if matcher.Name == labels.AlertName && matcher.Type == labels.MatchEqual {
ruleKey = matcher.Value
}
}
ls := b.Labels()
if ruleKey == "" {
level.Error(m.logger).Log("msg", "Select called in an unexpected fashion without alertname or ALERTS_FOR_STATE labels")
return storage.NoopSeriesSet()
}
var rule *rules.AlertingRule
// go fetch the rule via the alertname
for _, x := range m.mgr.AlertingRules() {
if x.Name() == ruleKey {
rule = x
break
}
}
// should not happen
if rule == nil {
level.Error(m.logger).Log("msg", "failure trying to restore for state for untracked alerting rule", "name", ruleKey)
return storage.NoopSeriesSet()
}
level.Debug(m.logger).Log("msg", "restoring for state via evaluation", "rule", ruleKey)
m.mtx.Lock()
defer m.mtx.Unlock()
cache, ok := m.rules[ruleKey]
// no timestamp results are cached for this rule at all; Create it.
if !ok {
cache = NewRuleCache(m.metrics)
m.rules[ruleKey] = cache
}
smpl, cached := cache.Get(m.ts, ls)
if cached {
m.metrics.cacheHits.WithLabelValues(m.userID).Inc()
level.Debug(m.logger).Log("msg", "result cached", "rule", ruleKey)
// Assuming the result is cached but the desired series is not in the result, it wouldn't be considered active.
if smpl == nil {
return storage.NoopSeriesSet()
}
// If the labelset is cached we can consider it active. Return the for state sample active immediately.
return series.NewConcreteSeriesSet(
[]storage.Series{
series.NewConcreteSeries(smpl.Metric, []model.SamplePair{
{Timestamp: model.Time(util.TimeToMillis(m.ts)), Value: model.SampleValue(smpl.V)},
}),
},
)
}
// see if alert condition had any inhabitants at ts-forDuration. We can assume it's still firing because
// that's the only condition under which this is queried (via RestoreForState).
checkTime := m.ts.Add(-rule.HoldDuration())
vec, err := m.queryFunc(m.ctx, rule.Query().String(), checkTime)
if err != nil {
level.Info(m.logger).Log("msg", "error querying for rule", "rule", ruleKey, "err", err.Error())
m.metrics.evaluations.WithLabelValues(statusFailure, m.userID).Inc()
return storage.NoopSeriesSet()
}
m.metrics.evaluations.WithLabelValues(statusSuccess, m.userID).Inc()
level.Debug(m.logger).Log("msg", "rule state successfully restored", "rule", ruleKey, "len", len(vec))
// translate the result into the ALERTS_FOR_STATE series for caching,
// considered active & written at the timetamp requested
forStateVec := make(promql.Vector, 0, len(vec))
for _, smpl := range vec {
ts := util.TimeToMillis(m.ts)
forStateVec = append(forStateVec, promql.Sample{
Metric: ForStateMetric(smpl.Metric, rule.Name()),
Point: promql.Point{
T: ts,
V: float64(checkTime.Unix()),
},
})
}
// cache the result of the evaluation at this timestamp
cache.Set(m.ts, forStateVec)
// Finally return the series if it exists.
// Calling cache.Get leverages the existing code to return only single sample.
smpl, ok = cache.Get(m.ts, ls)
if !ok || smpl == nil {
return storage.NoopSeriesSet()
}
// If the labelset is cached we can consider it active. Return the for state sample active immediately.
return series.NewConcreteSeriesSet(
[]storage.Series{
series.NewConcreteSeries(smpl.Metric, []model.SamplePair{
{Timestamp: model.Time(util.TimeToMillis(m.ts)), Value: model.SampleValue(smpl.V)},
}),
},
)
}
// LabelValues returns all potential values for a label name.
func (*memStoreQuerier) LabelValues(name string, matchers ...*labels.Matcher) ([]string, storage.Warnings, error) {
return nil, nil, errors.New("unimplemented")
}
// LabelNames returns all the unique label names present in the block in sorted order.
func (*memStoreQuerier) LabelNames(matchers ...*labels.Matcher) ([]string, storage.Warnings, error) {
return nil, nil, errors.New("unimplemented")
}
// Close releases the resources of the Querier.
func (*memStoreQuerier) Close() error { return nil }
type RuleCache struct {
mtx sync.Mutex
metrics *memstoreMetrics
data map[int64]map[uint64]promql.Sample
}
func NewRuleCache(metrics *memstoreMetrics) *RuleCache {
return &RuleCache{
data: make(map[int64]map[uint64]promql.Sample),
metrics: metrics,
}
}
func (c *RuleCache) Set(ts time.Time, vec promql.Vector) {
c.mtx.Lock()
defer c.mtx.Unlock()
tsMap, ok := c.data[ts.UnixNano()]
if !ok {
tsMap = make(map[uint64]promql.Sample)
c.data[ts.UnixNano()] = tsMap
}
for _, sample := range vec {
tsMap[sample.Metric.Hash()] = sample
}
c.metrics.samples.Add(float64(len(vec)))
}
// Get returns ok if that timestamp's result is cached.
func (c *RuleCache) Get(ts time.Time, ls labels.Labels) (*promql.Sample, bool) {
c.mtx.Lock()
defer c.mtx.Unlock()
match, ok := c.data[ts.UnixNano()]
if !ok {
return nil, false
}
smp, ok := match[ls.Hash()]
if !ok {
return nil, true
}
return &smp, true
}
// CleanupOldSamples removes samples that are outside of the rule's `For` duration.
func (c *RuleCache) CleanupOldSamples(olderThan time.Time) (empty bool) {
c.mtx.Lock()
defer c.mtx.Unlock()
ns := olderThan.UnixNano()
// This could be more efficient (logarithmic instead of linear)
for ts, tsMap := range c.data {
if ts < ns {
delete(c.data, ts)
c.metrics.samples.Add(-float64(len(tsMap)))
}
}
return len(c.data) == 0
}