-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathsyncer.go
More file actions
428 lines (364 loc) · 11.8 KB
/
syncer.go
File metadata and controls
428 lines (364 loc) · 11.8 KB
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
// Copyright (c) The Thanos Authors.
// Licensed under the Apache 2.0 license found in the LICENSE file or at:
// https://opensource.org/licenses/Apache-2.0
package locate
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"net/url"
"os"
"path/filepath"
"sort"
"sync"
"github.com/alecthomas/units"
"github.com/efficientgo/core/errcapture"
"github.com/parquet-go/parquet-go"
"github.com/prometheus/prometheus/tsdb/fileutil"
"github.com/thanos-io/objstore"
"github.com/thanos-io/thanos-parquet-gateway/db"
"github.com/thanos-io/thanos-parquet-gateway/internal/util"
"github.com/thanos-io/thanos-parquet-gateway/schema"
)
type Syncer struct {
bkt objstore.Bucket
blockOpts []BlockOption
metaFilter MetaFilter
concurrency int
shardConcurrency int
mu sync.Mutex
blocks map[schema.ExternalLabelsHash]map[util.Date]*db.Block
cached []*db.Block
}
type SyncerOption func(*syncerConfig)
type syncerConfig struct {
blockOpts []BlockOption
metaFilter MetaFilter
concurrency int
shardConcurrency int
}
func BlockOptions(opts ...BlockOption) SyncerOption {
return func(cfg *syncerConfig) {
cfg.blockOpts = opts
}
}
func FilterMetas(f MetaFilter) SyncerOption {
return func(cfg *syncerConfig) {
cfg.metaFilter = f
}
}
func BlockConcurrency(c int) SyncerOption {
return func(cfg *syncerConfig) {
cfg.concurrency = c
}
}
func ShardConcurrency(c int) SyncerOption {
return func(cfg *syncerConfig) {
cfg.shardConcurrency = c
}
}
type BlockOption func(*blockConfig)
type blockConfig struct {
readBufferSize units.Base2Bytes
labelFilesDir string
}
func ReadBufferSize(sz units.Base2Bytes) BlockOption {
return func(cfg *blockConfig) {
cfg.readBufferSize = sz
}
}
func LabelFilesDir(d string) BlockOption {
return func(cfg *blockConfig) {
cfg.labelFilesDir = d
}
}
func NewSyncer(bkt objstore.Bucket, opts ...SyncerOption) *Syncer {
cfg := syncerConfig{
metaFilter: AllMetasMetaFilter,
concurrency: 1,
shardConcurrency: 0,
}
for _, o := range opts {
o(&cfg)
}
return &Syncer{
bkt: bkt,
blocks: make(map[schema.ExternalLabelsHash]map[util.Date]*db.Block),
blockOpts: cfg.blockOpts,
metaFilter: cfg.metaFilter,
concurrency: cfg.concurrency,
shardConcurrency: cfg.shardConcurrency,
}
}
func (s *Syncer) Blocks() []*db.Block {
s.mu.Lock()
defer s.mu.Unlock()
return s.filterBlocks(s.cached)
}
func (s *Syncer) Sync(ctx context.Context, parquetStreams map[schema.ExternalLabelsHash]schema.ParquetBlocksStream) error {
type blockOrError struct {
blk *db.Block
extLabels schema.ExternalLabels
err error
blockName string
}
type metaHashTuple struct {
streamHash schema.ExternalLabelsHash
m schema.Meta
extLabels schema.ExternalLabels
}
blkC := make(chan blockOrError)
go func() {
defer close(blkC)
workerC := make(chan metaHashTuple, s.concurrency)
go func() {
defer close(workerC)
for streamHash, stream := range parquetStreams {
if _, ok := s.blocks[streamHash]; !ok {
s.blocks[streamHash] = make(map[util.Date]*db.Block)
}
for _, m := range s.filterMetas(stream.Metas) {
if _, ok := s.blocks[streamHash][m.Date]; ok {
continue
}
workerC <- metaHashTuple{
m: m,
extLabels: stream.ExternalLabels,
}
}
}
}()
var wg sync.WaitGroup
defer wg.Wait()
for range s.concurrency {
wg.Add(1)
go func() {
defer wg.Done()
for mh := range workerC {
blk, err := newBlockForMeta(ctx, s.bkt, mh.extLabels, mh.m, s.shardConcurrency, s.blockOpts...)
if err != nil {
blkC <- blockOrError{blk: nil, err: fmt.Errorf("unable to read block %s %q: %w", mh.streamHash.String(), mh.m.Date, err), blockName: mh.streamHash.String()}
} else {
blkC <- blockOrError{blk: blk, extLabels: mh.extLabels}
}
}
}()
}
}()
blocks := make(map[schema.ExternalLabelsHash]map[util.Date]*db.Block, 0)
for b := range blkC {
if b.err != nil {
return fmt.Errorf("unable to read block: %w", b.err)
}
if _, ok := blocks[b.extLabels.Hash()]; !ok {
blocks[b.extLabels.Hash()] = make(map[util.Date]*db.Block)
}
blocks[b.extLabels.Hash()][b.blk.Meta().Date] = b.blk
}
s.mu.Lock()
defer s.mu.Unlock()
var numBlocks int
for stream := range s.blocks {
if _, ok := parquetStreams[stream]; !ok {
delete(s.blocks, stream)
}
}
for stream := range s.blocks {
if _, ok := blocks[stream]; !ok {
continue
}
s.blocks[stream] = blocks[stream]
}
for _, v := range s.blocks {
numBlocks += len(v)
}
allBlocks := make([]*db.Block, 0, numBlocks)
for _, m := range s.blocks {
for _, b := range m {
allBlocks = append(allBlocks, b)
}
}
s.cached = allBlocks
sort.Slice(s.cached, func(i, j int) bool {
ls, _ := s.cached[i].Timerange()
rs, _ := s.cached[j].Timerange()
return ls < rs
})
if len(s.cached) != 0 {
syncMinTime.WithLabelValues(whatSyncer).Set(float64(s.cached[0].Meta().Mint))
syncMaxTime.WithLabelValues(whatSyncer).Set(float64(s.cached[len(s.cached)-1].Meta().Maxt))
}
syncLastSuccessfulTime.WithLabelValues(whatSyncer).SetToCurrentTime()
return nil
}
func (s *Syncer) filterMetas(metas []schema.Meta) []schema.Meta {
return s.metaFilter.filterMetas(metas)
}
func (s *Syncer) filterBlocks(blks []*db.Block) []*db.Block {
return s.metaFilter.filterBlocks(blks)
}
/*
TODO: the following functions should be abstracted into syncer somehow, the syncer should decide
where shards live (s3, disk, memory) and should also do regular maintenance on its on disk state,
i.e. downloading files that it wants on disk, deleting files that are out of retention, etc.
*/
func newBlockForMeta(ctx context.Context, bkt objstore.Bucket, extLabels schema.ExternalLabels, m schema.Meta, shardConcurrency int, opts ...BlockOption) (*db.Block, error) {
cfg := blockConfig{
readBufferSize: 8 * units.MiB,
labelFilesDir: os.TempDir(),
}
for _, o := range opts {
o(&cfg)
}
var shards []*db.Shard
var err error
// Use parallel loading if shardConcurrency > 0, otherwise use sequential (original) loading
if shardConcurrency > 0 {
shards, err = readShardsParallel(ctx, bkt, m, cfg, extLabels.Hash(), shardConcurrency)
} else {
shards, err = readShards(ctx, bkt, m, extLabels.Hash(), cfg)
}
if err != nil {
return nil, fmt.Errorf("unable to read shards: %w", err)
}
return db.NewBlock(m, extLabels, shards...), nil
}
func readShards(ctx context.Context, bkt objstore.Bucket, m schema.Meta, extLabelsHash schema.ExternalLabelsHash, cfg blockConfig) ([]*db.Shard, error) {
shards := make([]*db.Shard, 0, m.Shards)
for sh := range int(m.Shards) {
shard, err := readShard(ctx, bkt, m, extLabelsHash, sh, cfg)
if err != nil {
return nil, fmt.Errorf("unable to read shard %d: %w", sh, err)
}
shards = append(shards, shard)
}
return shards, nil
}
func readShardsParallel(ctx context.Context, bkt objstore.Bucket, m schema.Meta, cfg blockConfig, extLabelsHash schema.ExternalLabelsHash, shardConcurrency int) ([]*db.Shard, error) {
type shardOrError struct {
shard *db.Shard
index int
err error
}
shardC := make(chan shardOrError, int(m.Shards))
var wg sync.WaitGroup
sem := make(chan struct{}, shardConcurrency)
for i := range int(m.Shards) {
wg.Add(1)
go func(shardIndex int) {
defer wg.Done()
sem <- struct{}{}
defer func() { <-sem }()
shard, err := readShard(ctx, bkt, m, extLabelsHash, shardIndex, cfg)
shardC <- shardOrError{shard: shard, index: shardIndex, err: err}
}(i)
}
go func() {
wg.Wait()
close(shardC)
}()
shards := make([]*db.Shard, 0, int(m.Shards))
var failedShards []int
for result := range shardC {
if result.err != nil {
failedShards = append(failedShards, result.index)
continue
}
shards = append(shards, result.shard)
}
// Return error only if ALL shards failed
if len(shards) == 0 {
return nil, fmt.Errorf("all shards failed to load, failed_shards=%v", failedShards)
}
return shards, nil
}
func bucketReaderFromContext(bkt objstore.Bucket, name string) func(context.Context) io.ReaderAt {
return func(ctx context.Context) io.ReaderAt {
return newBucketReaderAt(ctx, bkt, name)
}
}
func readShard(ctx context.Context, bkt objstore.Bucket, m schema.Meta, extLabelsHash schema.ExternalLabelsHash, shard int, cfg blockConfig) (s *db.Shard, err error) {
chunkspfile := schema.ChunksPfileNameForShard(extLabelsHash, m.Date, shard)
attrs, err := bkt.Attributes(ctx, chunkspfile)
if err != nil {
return nil, fmt.Errorf("unable to attr chunks parquet file %q: %w", chunkspfile, err)
}
bktRdrAtFromCtx := bucketReaderFromContext(bkt, chunkspfile)
chunkspf, err := parquet.OpenFile(bktRdrAtFromCtx(ctx), attrs.Size,
parquet.FileReadMode(parquet.ReadModeAsync),
parquet.ReadBufferSize(int(cfg.readBufferSize)),
parquet.SkipMagicBytes(true),
parquet.SkipBloomFilters(true),
parquet.OptimisticRead(true),
)
if err != nil {
return nil, fmt.Errorf("unable to open chunks parquet file %q: %w", chunkspfile, err)
}
labelspfile := schema.LabelsPfileNameForShard(extLabelsHash, m.Date, shard)
labelspfilePath := filepath.Join(cfg.labelFilesDir, url.PathEscape(labelspfile))
// If we were not able to read the file for any reason we delete it and retry.
// This is also executed on paths that have nothing to do with the file, i.e. loading
// its content from object storage, but just makes sure that we dont forget cleanup.
defer func() {
if err != nil {
if cerr := os.RemoveAll(labelspfilePath); cerr != nil {
err = errors.Join(err, fmt.Errorf("unable to remove labels parquet file %q: %w", labelspfilePath, cerr))
}
}
}()
// if the file was corrupted on its way to disk we remove it and will retry downloading it next try
if stat, err := os.Stat(labelspfilePath); err != nil {
if !os.IsNotExist(err) {
return nil, fmt.Errorf("unable to stat label parquet file %q from disk: %w", labelspfile, err)
// file didn't exist - we need to download and save it to disk
}
} else {
f, err := fileutil.OpenMmapFileWithSize(labelspfilePath, int(stat.Size()))
if err != nil {
return nil, fmt.Errorf("unable to mmap label parquet file %q: %w", labelspfile, err)
}
labelspf, err := parquet.OpenFile(bytes.NewReader(f.Bytes()), stat.Size())
if err != nil {
syncCorruptedLabelFile.Add(1)
rerr := fmt.Errorf("unable to read label parquet file %q: %w", labelspfile, err)
if cerr := f.Close(); cerr != nil {
return nil, errors.Join(rerr, fmt.Errorf("unable to close memory mapped parquet file %q: %w", labelspfile, cerr))
}
return nil, rerr
}
return db.NewShard(m, chunkspf, labelspf, bktRdrAtFromCtx), nil
}
rdr, err := bkt.Get(ctx, labelspfile)
if err != nil {
return nil, fmt.Errorf("unable to get %q: %w", labelspfile, err)
}
defer errcapture.Do(&err, rdr.Close, "labels parquet file close")
f, err := os.Create(labelspfilePath)
if err != nil {
return nil, fmt.Errorf("unable to create label parquet file %q on disk: %w", labelspfile, err)
}
defer errcapture.Do(&err, f.Close, "labels parquet file close")
n, err := io.Copy(f, rdr)
if err != nil {
return nil, fmt.Errorf("unable to copy label parquet file %q to disk: %w", labelspfile, err)
}
if err := f.Sync(); err != nil {
return nil, fmt.Errorf("unable to close label parquet file %q: %w", labelspfile, err)
}
mf, err := fileutil.OpenMmapFileWithSize(labelspfilePath, int(n))
if err != nil {
return nil, fmt.Errorf("unable to mmap label parquet file %q: %w", labelspfile, err)
}
labelspf, err := parquet.OpenFile(bytes.NewReader(mf.Bytes()), int64(n))
if err != nil {
syncCorruptedLabelFile.Add(1)
rerr := fmt.Errorf("unable to read label parquet file %q: %w", labelspfile, err)
if cerr := f.Close(); cerr != nil {
return nil, errors.Join(rerr, fmt.Errorf("unable to close memory mapped parquet file %q: %w", labelspfile, cerr))
}
return nil, rerr
}
return db.NewShard(m, chunkspf, labelspf, bktRdrAtFromCtx), nil
}