/
tscache.go
311 lines (265 loc) · 7.74 KB
/
tscache.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
// Copyright 2022 V Kontakte LLC
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package api
import (
"context"
"math"
"sync"
"time"
"go.uber.org/atomic"
"github.com/vkcom/statshouse/internal/format"
)
const (
maxEvictionSampleSize = 100
invalidateFrom = -48 * time.Hour
invalidateLinger = 15 * time.Second // try to work around ClickHouse table replication race
)
type tsSelectRow struct {
Time int64 `ch:"_time"`
StepSec int64 `ch:"_stepSec"` // TODO - do not get using strange logic in clickhouse, set directly
tsTags
tsValues
}
// all numeric tags are stored as int32 to save space
type tsTags struct {
Tag0 int32 `ch:"key0"`
Tag1 int32 `ch:"key1"`
Tag2 int32 `ch:"key2"`
Tag3 int32 `ch:"key3"`
Tag4 int32 `ch:"key4"`
Tag5 int32 `ch:"key5"`
Tag6 int32 `ch:"key6"`
Tag7 int32 `ch:"key7"`
Tag8 int32 `ch:"key8"`
Tag9 int32 `ch:"key9"`
Tag10 int32 `ch:"key10"`
Tag11 int32 `ch:"key11"`
Tag12 int32 `ch:"key12"`
Tag13 int32 `ch:"key13"`
Tag14 int32 `ch:"key14"`
Tag15 int32 `ch:"key15"`
STag stringFixed `ch:"skey"` // WIP: just to test the speed
}
type tsValues struct {
CountNorm float64 `ch:"_count"`
Val0 float64 `ch:"_val0"`
Val1 float64 `ch:"_val1"`
Val2 float64 `ch:"_val2"`
Val3 float64 `ch:"_val3"`
Val4 float64 `ch:"_val4"`
Val5 float64 `ch:"_val5"`
Val6 float64 `ch:"_val6"`
}
type tsCacheGroup struct {
pointCaches map[string]map[int64]*tsCache
}
func newTSCacheGroup(approxMaxSize int, lodTables map[string]map[int64]string, utcOffset int64, loader tsLoadFunc, dropEvery time.Duration) *tsCacheGroup {
g := &tsCacheGroup{
pointCaches: map[string]map[int64]*tsCache{},
}
for version, tables := range lodTables {
drop := dropEvery
if version == Version2 {
drop = 0
}
g.pointCaches[version] = map[int64]*tsCache{}
for stepSec := range tables {
g.pointCaches[version][stepSec] = newTSCache(approxMaxSize, stepSec, utcOffset, loader, drop)
}
}
return g
}
func (g *tsCacheGroup) Invalidate(lodLevel int64, times []int64) {
g.pointCaches[Version2][lodLevel].invalidate(times)
}
func (g *tsCacheGroup) Get(ctx context.Context, version string, key string, pq *preparedPointsQuery, lod lodInfo) ([][]tsSelectRow, error) {
switch pq.metricID {
case format.BuiltinMetricIDGeneratorConstCounter:
return generateConstCounter(lod)
case format.BuiltinMetricIDGeneratorSinCounter:
return generateSinCounter(lod)
case format.BuiltinMetricIDGeneratorGapsCounter:
return generateGapsCounter(lod)
default:
return g.pointCaches[version][lod.stepSec].get(ctx, key, pq, lod)
}
}
type tsCache struct {
loader tsLoadFunc
size int
approxMaxSize int
stepSec int64
utcOffset int64 // only used in maybeDropCache(); all external timestamps should be rounded with roundTime()
cacheMu sync.RWMutex
cache map[string]*tsEntry
invalidatedAtNano map[int64]int64
lastDrop time.Time
dropEvery time.Duration
}
type tsLoadFunc func(ctx context.Context, pq *preparedPointsQuery, lod lodInfo, ret [][]tsSelectRow, retStartIx int) error
type tsVersionedRows struct {
rows []tsSelectRow
loadedAtNano int64
}
type tsEntry struct {
// Place atomics first to ensure proper alignment, see https://pkg.go.dev/sync/atomic#pkg-note-BUG
lru atomic.Int64
secRows map[int64]*tsVersionedRows
}
func newTSCache(approxMaxSize int, stepSec int64, utcOffset int64, loader tsLoadFunc, dropEvery time.Duration) *tsCache {
now := time.Now()
return &tsCache{
loader: loader,
approxMaxSize: approxMaxSize,
stepSec: stepSec,
utcOffset: utcOffset,
cache: map[string]*tsEntry{},
invalidatedAtNano: map[int64]int64{},
lastDrop: now,
dropEvery: dropEvery,
}
}
func (c *tsCache) maybeDropCache() {
c.cacheMu.Lock()
defer c.cacheMu.Unlock()
if now := time.Now(); now.Sub(c.lastDrop) > c.dropEvery {
fromSec := roundTime(now.Add(invalidateFrom).Unix(), c.stepSec, c.utcOffset)
nowSec := now.Unix()
for _, e := range c.cache {
for t := fromSec; t <= nowSec; t += c.stepSec {
cached, ok := e.secRows[t]
if ok {
c.size -= len(cached.rows)
delete(e.secRows, t)
}
}
}
c.lastDrop = now
}
}
func (c *tsCache) get(ctx context.Context, key string, pq *preparedPointsQuery, lod lodInfo) ([][]tsSelectRow, error) {
if c.dropEvery != 0 {
c.maybeDropCache()
}
ret := make([][]tsSelectRow, lod.getIndexForTimestamp(lod.toSec, 0))
loadFrom, loadTo := c.loadCached(key, lod.fromSec, lod.toSec, ret, 0, lod.location)
if loadFrom == 0 && loadTo == 0 {
return ret, nil
}
realLoadFrom, realLoadTo := c.loadCached(key, loadFrom, loadTo, ret, int((loadFrom-lod.fromSec)/c.stepSec), lod.location)
if realLoadFrom == 0 && realLoadTo == 0 {
return ret, nil
}
loadAtNano := time.Now().UnixNano()
loadLOD := lodInfo{fromSec: realLoadFrom, toSec: realLoadTo, stepSec: c.stepSec, table: lod.table, hasPreKey: lod.hasPreKey, location: lod.location}
err := c.loader(ctx, pq, loadLOD, ret, int((realLoadFrom-lod.fromSec)/c.stepSec))
if err != nil {
return nil, err
}
c.cacheMu.Lock()
defer c.cacheMu.Unlock()
for c.size >= c.approxMaxSize {
c.size -= c.evictLocked()
}
e, ok := c.cache[key]
if !ok {
e = &tsEntry{secRows: map[int64]*tsVersionedRows{}}
c.cache[key] = e
}
e.lru.Store(time.Now().UnixNano())
for t := realLoadFrom; t < realLoadTo; {
var nextRealLoadFrom int64
if c.stepSec == _1M {
nextRealLoadFrom = time.Unix(t, 0).In(lod.location).AddDate(0, 1, 0).Unix()
} else {
nextRealLoadFrom = t + c.stepSec
}
cached, ok := e.secRows[t]
if !ok {
cached = &tsVersionedRows{}
e.secRows[t] = cached
}
if loadAtNano > cached.loadedAtNano {
loadedRows := ret[lod.getIndexForTimestamp(t, 0)]
c.size += len(loadedRows) - len(cached.rows)
cached.loadedAtNano = loadAtNano
cached.rows = loadedRows
}
t = nextRealLoadFrom
}
return ret, nil
}
func (c *tsCache) invalidate(times []int64) {
c.cacheMu.Lock()
defer c.cacheMu.Unlock()
at := time.Now().UnixNano()
for _, sec := range times {
c.invalidatedAtNano[sec] = at
}
i := 0
from := time.Now().Add(invalidateFrom).Unix()
for sec := range c.invalidatedAtNano {
if sec < from {
delete(c.invalidatedAtNano, sec)
}
i++
if i == maxEvictionSampleSize {
break
}
}
}
func (c *tsCache) loadCached(key string, fromSec int64, toSec int64, ret [][]tsSelectRow, retStartIx int, location *time.Location) (int64, int64) {
c.cacheMu.RLock()
defer c.cacheMu.RUnlock()
e, ok := c.cache[key]
if !ok {
return fromSec, toSec
}
e.lru.Store(time.Now().UnixNano())
var loadFrom, loadTo int64
for t, ix := fromSec, retStartIx; t < toSec; ix++ {
var nextStartFrom int64
if c.stepSec == _1M {
nextStartFrom = time.Unix(t, 0).In(location).AddDate(0, 1, 0).Unix()
} else {
nextStartFrom = t + c.stepSec
}
cached, ok := e.secRows[t]
if ok && cached.loadedAtNano >= c.invalidatedAtNano[t]+int64(invalidateLinger) {
ret[ix] = cached.rows
} else {
if loadFrom == 0 {
loadFrom = t
}
loadTo = nextStartFrom
}
t = nextStartFrom
}
return loadFrom, loadTo
}
func (c *tsCache) evictLocked() int {
k := ""
i := 0
t := int64(math.MaxInt64)
for key, e := range c.cache { // "power of N random choices" with map iteration providing randomness
u := e.lru.Load()
if u < t {
k = key
t = u
}
i++
if i == maxEvictionSampleSize {
break
}
}
e := c.cache[k]
n := 0
for _, cached := range e.secRows {
n += len(cached.rows)
}
delete(c.cache, k)
return n
}