-
Notifications
You must be signed in to change notification settings - Fork 2k
/
lazy_binary_reader.go
321 lines (269 loc) · 9.5 KB
/
lazy_binary_reader.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
// Copyright (c) The Thanos Authors.
// Licensed under the Apache License 2.0.
package indexheader
import (
"context"
"os"
"path/filepath"
"sync"
"time"
"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/tsdb/index"
"github.com/thanos-io/objstore"
"go.uber.org/atomic"
"github.com/thanos-io/thanos/pkg/block"
)
var (
errNotIdle = errors.New("the reader is not idle")
errUnloadedWhileLoading = errors.New("the index-header has been concurrently unloaded")
)
// LazyBinaryReaderMetrics holds metrics tracked by LazyBinaryReader.
type LazyBinaryReaderMetrics struct {
loadCount prometheus.Counter
loadFailedCount prometheus.Counter
unloadCount prometheus.Counter
unloadFailedCount prometheus.Counter
loadDuration prometheus.Histogram
}
// NewLazyBinaryReaderMetrics makes new LazyBinaryReaderMetrics.
func NewLazyBinaryReaderMetrics(reg prometheus.Registerer) *LazyBinaryReaderMetrics {
return &LazyBinaryReaderMetrics{
loadCount: promauto.With(reg).NewCounter(prometheus.CounterOpts{
Name: "indexheader_lazy_load_total",
Help: "Total number of index-header lazy load operations.",
}),
loadFailedCount: promauto.With(reg).NewCounter(prometheus.CounterOpts{
Name: "indexheader_lazy_load_failed_total",
Help: "Total number of failed index-header lazy load operations.",
}),
unloadCount: promauto.With(reg).NewCounter(prometheus.CounterOpts{
Name: "indexheader_lazy_unload_total",
Help: "Total number of index-header lazy unload operations.",
}),
unloadFailedCount: promauto.With(reg).NewCounter(prometheus.CounterOpts{
Name: "indexheader_lazy_unload_failed_total",
Help: "Total number of failed index-header lazy unload operations.",
}),
loadDuration: promauto.With(reg).NewHistogram(prometheus.HistogramOpts{
Name: "indexheader_lazy_load_duration_seconds",
Help: "Duration of the index-header lazy loading in seconds.",
Buckets: []float64{0.01, 0.02, 0.05, 0.1, 0.2, 0.5, 1, 2, 5, 15, 30, 60, 90, 120, 300},
}),
}
}
// LazyBinaryReader wraps BinaryReader and loads (mmap) the index-header only upon
// the first Reader function is called.
type LazyBinaryReader struct {
ctx context.Context
logger log.Logger
bkt objstore.BucketReader
dir string
id ulid.ULID
postingOffsetsInMemSampling int
metrics *LazyBinaryReaderMetrics
binaryReaderMetrics *BinaryReaderMetrics
onClosed func(*LazyBinaryReader)
readerMx sync.RWMutex
reader *BinaryReader
readerErr error
// Keep track of the last time it was used.
usedAt *atomic.Int64
// If true, index header will be downloaded at query time rather than initialization time.
lazyDownload bool
}
// NewLazyBinaryReader makes a new LazyBinaryReader. If the index-header does not exist
// on the local disk at dir location, this function will build it downloading required
// sections from the full index stored in the bucket. However, this function doesn't load
// (mmap) the index-header; it will be loaded at first Reader function call.
func NewLazyBinaryReader(
ctx context.Context,
logger log.Logger,
bkt objstore.BucketReader,
dir string,
id ulid.ULID,
postingOffsetsInMemSampling int,
metrics *LazyBinaryReaderMetrics,
binaryReaderMetrics *BinaryReaderMetrics,
onClosed func(*LazyBinaryReader),
lazyDownload bool,
) (*LazyBinaryReader, error) {
if dir != "" && !lazyDownload {
indexHeaderFile := filepath.Join(dir, id.String(), block.IndexHeaderFilename)
// If the index-header doesn't exist we should download it.
if _, err := os.Stat(indexHeaderFile); err != nil {
if !os.IsNotExist(err) {
return nil, errors.Wrap(err, "read index header")
}
level.Debug(logger).Log("msg", "the index-header doesn't exist on disk; recreating", "path", indexHeaderFile)
start := time.Now()
if _, err := WriteBinary(ctx, bkt, id, indexHeaderFile); err != nil {
return nil, errors.Wrap(err, "write index header")
}
level.Debug(logger).Log("msg", "built index-header file", "path", indexHeaderFile, "elapsed", time.Since(start))
binaryReaderMetrics.downloadDuration.Observe(time.Since(start).Seconds())
}
}
return &LazyBinaryReader{
ctx: ctx,
logger: logger,
bkt: bkt,
dir: dir,
id: id,
postingOffsetsInMemSampling: postingOffsetsInMemSampling,
metrics: metrics,
binaryReaderMetrics: binaryReaderMetrics,
usedAt: atomic.NewInt64(time.Now().UnixNano()),
onClosed: onClosed,
lazyDownload: lazyDownload,
}, nil
}
// Close implements Reader. It unloads the index-header from memory (releasing the mmap
// area), but a subsequent call to any other Reader function will automatically reload it.
func (r *LazyBinaryReader) Close() error {
if r.onClosed != nil {
defer r.onClosed(r)
}
// Unload without checking if idle.
return r.unloadIfIdleSince(0)
}
// IndexVersion implements Reader.
func (r *LazyBinaryReader) IndexVersion() (int, error) {
r.readerMx.RLock()
defer r.readerMx.RUnlock()
if err := r.load(); err != nil {
return 0, err
}
r.usedAt.Store(time.Now().UnixNano())
return r.reader.IndexVersion()
}
// PostingsOffsets implements Reader.
func (r *LazyBinaryReader) PostingsOffsets(name string, values ...string) ([]index.Range, error) {
r.readerMx.RLock()
defer r.readerMx.RUnlock()
if err := r.load(); err != nil {
return nil, err
}
r.usedAt.Store(time.Now().UnixNano())
return r.reader.PostingsOffsets(name, values...)
}
// PostingsOffset implements Reader.
func (r *LazyBinaryReader) PostingsOffset(name, value string) (index.Range, error) {
r.readerMx.RLock()
defer r.readerMx.RUnlock()
if err := r.load(); err != nil {
return index.Range{}, err
}
r.usedAt.Store(time.Now().UnixNano())
return r.reader.PostingsOffset(name, value)
}
// LookupSymbol implements Reader.
func (r *LazyBinaryReader) LookupSymbol(ctx context.Context, o uint32) (string, error) {
r.readerMx.RLock()
defer r.readerMx.RUnlock()
if err := r.load(); err != nil {
return "", err
}
r.usedAt.Store(time.Now().UnixNano())
return r.reader.LookupSymbol(ctx, o)
}
// LabelValues implements Reader.
func (r *LazyBinaryReader) LabelValues(name string) ([]string, error) {
r.readerMx.RLock()
defer r.readerMx.RUnlock()
if err := r.load(); err != nil {
return nil, err
}
r.usedAt.Store(time.Now().UnixNano())
return r.reader.LabelValues(name)
}
// LabelNames implements Reader.
func (r *LazyBinaryReader) LabelNames() ([]string, error) {
r.readerMx.RLock()
defer r.readerMx.RUnlock()
if err := r.load(); err != nil {
return nil, err
}
r.usedAt.Store(time.Now().UnixNano())
return r.reader.LabelNames()
}
// load ensures the underlying binary index-header reader has been successfully loaded. Returns
// an error on failure. This function MUST be called with the read lock already acquired.
func (r *LazyBinaryReader) load() (returnErr error) {
// Nothing to do if we already tried loading it.
if r.reader != nil {
return nil
}
if r.readerErr != nil {
return r.readerErr
}
// Take the write lock to ensure we'll try to load it only once. Take again
// the read lock once done.
r.readerMx.RUnlock()
r.readerMx.Lock()
defer func() {
r.readerMx.Unlock()
r.readerMx.RLock()
// Between the write unlock and the subsequent read lock, the unload() may have run,
// so we make sure to catch this edge case.
if returnErr == nil && r.reader == nil {
returnErr = errUnloadedWhileLoading
}
}()
// Ensure none else tried to load it in the meanwhile.
if r.reader != nil {
return nil
}
if r.readerErr != nil {
return r.readerErr
}
level.Debug(r.logger).Log("msg", "lazy loading index-header", "block", r.id)
r.metrics.loadCount.Inc()
startTime := time.Now()
reader, err := NewBinaryReader(r.ctx, r.logger, r.bkt, r.dir, r.id, r.postingOffsetsInMemSampling, r.binaryReaderMetrics)
if err != nil {
r.metrics.loadFailedCount.Inc()
r.readerErr = err
return errors.Wrapf(err, "lazy load index-header for block %s", r.id)
}
r.reader = reader
level.Debug(r.logger).Log("msg", "lazy loaded index-header", "block", r.id, "elapsed", time.Since(startTime))
r.metrics.loadDuration.Observe(time.Since(startTime).Seconds())
return nil
}
// unloadIfIdleSince closes underlying BinaryReader if the reader is idle since given time (as unix nano). If idleSince is 0,
// the check on the last usage is skipped. Calling this function on a already unloaded reader is a no-op.
func (r *LazyBinaryReader) unloadIfIdleSince(ts int64) error {
r.readerMx.Lock()
defer r.readerMx.Unlock()
// Nothing to do if already unloaded.
if r.reader == nil {
return nil
}
// Do not unloadIfIdleSince if not idle.
if ts > 0 && r.usedAt.Load() > ts {
return errNotIdle
}
r.metrics.unloadCount.Inc()
if err := r.reader.Close(); err != nil {
r.metrics.unloadFailedCount.Inc()
return err
}
r.reader = nil
return nil
}
// isIdleSince returns true if the reader is idle since given time (as unix nano).
func (r *LazyBinaryReader) isIdleSince(ts int64) bool {
if r.usedAt.Load() > ts {
return false
}
// A reader can be considered idle only if it's loaded.
r.readerMx.RLock()
loaded := r.reader != nil
r.readerMx.RUnlock()
return loaded
}