-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
index.go
770 lines (657 loc) · 21 KB
/
index.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
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
package block
import (
"encoding/json"
"fmt"
"hash/crc32"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
"sort"
"strings"
"time"
"github.com/thanos-io/thanos/pkg/block/metadata"
"github.com/prometheus/prometheus/tsdb/fileutil"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/oklog/ulid"
"github.com/pkg/errors"
"github.com/prometheus/prometheus/tsdb"
"github.com/prometheus/prometheus/tsdb/chunks"
"github.com/prometheus/prometheus/tsdb/index"
"github.com/prometheus/prometheus/tsdb/labels"
"github.com/thanos-io/thanos/pkg/runutil"
)
const (
// IndexCacheVersion is a enumeration of index cache versions supported by Thanos.
IndexCacheVersion1 = iota + 1
)
type postingsRange struct {
Name, Value string
Start, End int64
}
type indexCache struct {
Version int
CacheVersion int
Symbols map[uint32]string
LabelValues map[string][]string
Postings []postingsRange
}
type realByteSlice []byte
func (b realByteSlice) Len() int {
return len(b)
}
func (b realByteSlice) Range(start, end int) []byte {
return b[start:end]
}
func (b realByteSlice) Sub(start, end int) index.ByteSlice {
return b[start:end]
}
func getSymbolTable(b index.ByteSlice) (map[uint32]string, error) {
version := int(b.Range(4, 5)[0])
if version != 1 && version != 2 {
return nil, errors.Errorf("unknown index file version %d", version)
}
toc, err := index.NewTOCFromByteSlice(b)
if err != nil {
return nil, errors.Wrap(err, "read TOC")
}
symbolsV2, symbolsV1, err := index.ReadSymbols(b, version, int(toc.Symbols))
if err != nil {
return nil, errors.Wrap(err, "read symbols")
}
symbolsTable := make(map[uint32]string, len(symbolsV1)+len(symbolsV2))
for o, s := range symbolsV1 {
symbolsTable[o] = s
}
for o, s := range symbolsV2 {
symbolsTable[uint32(o)] = s
}
return symbolsTable, nil
}
// WriteIndexCache writes a cache file containing the first lookup stages
// for an index file.
func WriteIndexCache(logger log.Logger, indexFn string, fn string) error {
indexFile, err := fileutil.OpenMmapFile(indexFn)
if err != nil {
return errors.Wrapf(err, "open mmap index file %s", indexFn)
}
defer runutil.CloseWithLogOnErr(logger, indexFile, "close index cache mmap file from %s", indexFn)
b := realByteSlice(indexFile.Bytes())
indexr, err := index.NewReader(b)
if err != nil {
return errors.Wrap(err, "open index reader")
}
defer runutil.CloseWithLogOnErr(logger, indexr, "load index cache reader")
// We assume reader verified index already.
symbols, err := getSymbolTable(b)
if err != nil {
return err
}
f, err := os.Create(fn)
if err != nil {
return errors.Wrap(err, "create index cache file")
}
defer runutil.CloseWithLogOnErr(logger, f, "index cache writer")
v := indexCache{
Version: indexr.Version(),
CacheVersion: IndexCacheVersion1,
Symbols: symbols,
LabelValues: map[string][]string{},
}
// Extract label value indices.
lnames, err := indexr.LabelIndices()
if err != nil {
return errors.Wrap(err, "read label indices")
}
for _, lns := range lnames {
if len(lns) != 1 {
continue
}
ln := lns[0]
tpls, err := indexr.LabelValues(ln)
if err != nil {
return errors.Wrap(err, "get label values")
}
vals := make([]string, 0, tpls.Len())
for i := 0; i < tpls.Len(); i++ {
v, err := tpls.At(i)
if err != nil {
return errors.Wrap(err, "get label value")
}
if len(v) != 1 {
return errors.Errorf("unexpected tuple length %d", len(v))
}
vals = append(vals, v[0])
}
v.LabelValues[ln] = vals
}
// Extract postings ranges.
pranges, err := indexr.PostingsRanges()
if err != nil {
return errors.Wrap(err, "read postings ranges")
}
for l, rng := range pranges {
v.Postings = append(v.Postings, postingsRange{
Name: l.Name,
Value: l.Value,
Start: rng.Start,
End: rng.End,
})
}
if err := json.NewEncoder(f).Encode(&v); err != nil {
return errors.Wrap(err, "encode file")
}
return nil
}
// ReadIndexCache reads an index cache file.
func ReadIndexCache(logger log.Logger, fn string) (
version int,
symbols []string,
lvals map[string][]string,
postings map[labels.Label]index.Range,
err error,
) {
f, err := os.Open(fn)
if err != nil {
return 0, nil, nil, nil, errors.Wrap(err, "open file")
}
defer runutil.CloseWithLogOnErr(logger, f, "index reader")
var v indexCache
bytes, err := ioutil.ReadFile(fn)
if err != nil {
return 0, nil, nil, nil, errors.Wrap(err, "read file")
}
if err = json.Unmarshal(bytes, &v); err != nil {
return 0, nil, nil, nil, errors.Wrap(err, "unmarshal index cache")
}
strs := map[string]string{}
lvals = make(map[string][]string, len(v.LabelValues))
postings = make(map[labels.Label]index.Range, len(v.Postings))
var maxSymbolID uint32
for o := range v.Symbols {
if o > maxSymbolID {
maxSymbolID = o
}
}
symbols = make([]string, maxSymbolID+1)
// Most strings we encounter are duplicates. Dedup string objects that we keep
// around after the function returns to reduce total memory usage.
// NOTE(fabxc): it could even make sense to deduplicate globally.
getStr := func(s string) string {
if cs, ok := strs[s]; ok {
return cs
}
strs[s] = s
return s
}
for o, s := range v.Symbols {
symbols[o] = getStr(s)
}
for ln, vals := range v.LabelValues {
for i := range vals {
vals[i] = getStr(vals[i])
}
lvals[getStr(ln)] = vals
}
for _, e := range v.Postings {
l := labels.Label{
Name: getStr(e.Name),
Value: getStr(e.Value),
}
postings[l] = index.Range{Start: e.Start, End: e.End}
}
return v.Version, symbols, lvals, postings, nil
}
// VerifyIndex does a full run over a block index and verifies that it fulfills the order invariants.
func VerifyIndex(logger log.Logger, fn string, minTime int64, maxTime int64) error {
stats, err := GatherIndexIssueStats(logger, fn, minTime, maxTime)
if err != nil {
return err
}
return stats.AnyErr()
}
type Stats struct {
// TotalSeries represents total number of series in block.
TotalSeries int
// OutOfOrderSeries represents number of series that have out of order chunks.
OutOfOrderSeries int
// OutOfOrderChunks represents number of chunks that are out of order (older time range is after younger one).
OutOfOrderChunks int
// DuplicatedChunks represents number of chunks with same time ranges within same series, potential duplicates.
DuplicatedChunks int
// OutsideChunks represents number of all chunks that are before or after time range specified in block meta.
OutsideChunks int
// CompleteOutsideChunks is subset of OutsideChunks that will be never accessed. They are completely out of time range specified in block meta.
CompleteOutsideChunks int
// Issue347OutsideChunks represents subset of OutsideChunks that are outsiders caused by https://github.com/prometheus/tsdb/issues/347
// and is something that Thanos handle.
//
// Specifically we mean here chunks with minTime == block.maxTime and maxTime > block.MaxTime. These are
// are segregated into separate counters. These chunks are safe to be deleted, since they are duplicated across 2 blocks.
Issue347OutsideChunks int
// OutOfOrderLabels represents the number of postings that contained out
// of order labels, a bug present in Prometheus 2.8.0 and below.
OutOfOrderLabels int
}
// PrometheusIssue5372Err returns an error if the Stats object indicates
// postings with out of order labels. This is corrected by Prometheus Issue
// #5372 and affects Prometheus versions 2.8.0 and below.
func (i Stats) PrometheusIssue5372Err() error {
if i.OutOfOrderLabels > 0 {
return errors.Errorf("index contains %d postings with out of order labels",
i.OutOfOrderLabels)
}
return nil
}
// Issue347OutsideChunksErr returns error if stats indicates issue347 block issue, that is repaired explicitly before compaction (on plan block).
func (i Stats) Issue347OutsideChunksErr() error {
if i.Issue347OutsideChunks > 0 {
return errors.Errorf("found %d chunks outside the block time range introduced by https://github.com/prometheus/tsdb/issues/347", i.Issue347OutsideChunks)
}
return nil
}
// CriticalErr returns error if stats indicates critical block issue, that might solved only by manual repair procedure.
func (i Stats) CriticalErr() error {
var errMsg []string
if i.OutOfOrderSeries > 0 {
errMsg = append(errMsg, fmt.Sprintf(
"%d/%d series have an average of %.3f out-of-order chunks: "+
"%.3f of these are exact duplicates (in terms of data and time range)",
i.OutOfOrderSeries,
i.TotalSeries,
float64(i.OutOfOrderChunks)/float64(i.OutOfOrderSeries),
float64(i.DuplicatedChunks)/float64(i.OutOfOrderChunks),
))
}
n := i.OutsideChunks - (i.CompleteOutsideChunks + i.Issue347OutsideChunks)
if n > 0 {
errMsg = append(errMsg, fmt.Sprintf("found %d chunks non-completely outside the block time range", n))
}
if i.CompleteOutsideChunks > 0 {
errMsg = append(errMsg, fmt.Sprintf("found %d chunks completely outside the block time range", i.CompleteOutsideChunks))
}
if len(errMsg) > 0 {
return errors.New(strings.Join(errMsg, ", "))
}
return nil
}
// AnyErr returns error if stats indicates any block issue.
func (i Stats) AnyErr() error {
var errMsg []string
if err := i.CriticalErr(); err != nil {
errMsg = append(errMsg, err.Error())
}
if err := i.Issue347OutsideChunksErr(); err != nil {
errMsg = append(errMsg, err.Error())
}
if err := i.PrometheusIssue5372Err(); err != nil {
errMsg = append(errMsg, err.Error())
}
if len(errMsg) > 0 {
return errors.New(strings.Join(errMsg, ", "))
}
return nil
}
// GatherIndexIssueStats returns useful counters as well as outsider chunks (chunks outside of block time range) that
// helps to assess index health.
// It considers https://github.com/prometheus/tsdb/issues/347 as something that Thanos can handle.
// See Stats.Issue347OutsideChunks for details.
func GatherIndexIssueStats(logger log.Logger, fn string, minTime int64, maxTime int64) (stats Stats, err error) {
r, err := index.NewFileReader(fn)
if err != nil {
return stats, errors.Wrap(err, "open index file")
}
defer runutil.CloseWithErrCapture(&err, r, "gather index issue file reader")
p, err := r.Postings(index.AllPostingsKey())
if err != nil {
return stats, errors.Wrap(err, "get all postings")
}
var (
lastLset labels.Labels
lset labels.Labels
chks []chunks.Meta
)
// Per series.
for p.Next() {
lastLset = append(lastLset[:0], lset...)
id := p.At()
stats.TotalSeries++
if err := r.Series(id, &lset, &chks); err != nil {
return stats, errors.Wrap(err, "read series")
}
if len(lset) == 0 {
return stats, errors.Errorf("empty label set detected for series %d", id)
}
if lastLset != nil && labels.Compare(lastLset, lset) >= 0 {
return stats, errors.Errorf("series %v out of order; previous %v", lset, lastLset)
}
l0 := lset[0]
for _, l := range lset[1:] {
if l.Name < l0.Name {
stats.OutOfOrderLabels++
level.Warn(logger).Log("msg",
"out-of-order label set: known bug in Prometheus 2.8.0 and below",
"labelset", lset.String(),
"series", fmt.Sprintf("%d", id),
)
}
l0 = l
}
if len(chks) == 0 {
return stats, errors.Errorf("empty chunks for series %d", id)
}
ooo := 0
// Per chunk in series.
for i, c := range chks {
// Chunk vs the block ranges.
if c.MinTime < minTime || c.MaxTime > maxTime {
stats.OutsideChunks++
if c.MinTime > maxTime || c.MaxTime < minTime {
stats.CompleteOutsideChunks++
} else if c.MinTime == maxTime {
stats.Issue347OutsideChunks++
}
}
if i == 0 {
continue
}
c0 := chks[i-1]
// Chunk order within block.
if c.MinTime > c0.MaxTime {
continue
}
if c.MinTime == c0.MinTime && c.MaxTime == c0.MaxTime {
// TODO(bplotka): Calc and check checksum from chunks itself.
// The chunks can overlap 1:1 in time, but does not have same data.
// We assume same data for simplicity, but it can be a symptom of error.
stats.DuplicatedChunks++
continue
}
// Chunks partly overlaps or out of order.
ooo++
}
if ooo > 0 {
stats.OutOfOrderSeries++
stats.OutOfOrderChunks += ooo
}
}
if p.Err() != nil {
return stats, errors.Wrap(err, "walk postings")
}
return stats, nil
}
type ignoreFnType func(mint, maxt int64, prev *chunks.Meta, curr *chunks.Meta) (bool, error)
// Repair open the block with given id in dir and creates a new one with fixed data.
// It:
// - removes out of order duplicates
// - all "complete" outsiders (they will not accessed anyway)
// - removes all near "complete" outside chunks introduced by https://github.com/prometheus/tsdb/issues/347.
// Fixable inconsistencies are resolved in the new block.
// TODO(bplotka): https://github.com/thanos-io/thanos/issues/378.
func Repair(logger log.Logger, dir string, id ulid.ULID, source metadata.SourceType, ignoreChkFns ...ignoreFnType) (resid ulid.ULID, err error) {
if len(ignoreChkFns) == 0 {
return resid, errors.New("no ignore chunk function specified")
}
bdir := filepath.Join(dir, id.String())
entropy := rand.New(rand.NewSource(time.Now().UnixNano()))
resid = ulid.MustNew(ulid.Now(), entropy)
meta, err := metadata.Read(bdir)
if err != nil {
return resid, errors.Wrap(err, "read meta file")
}
if meta.Thanos.Downsample.Resolution > 0 {
return resid, errors.New("cannot repair downsampled block")
}
b, err := tsdb.OpenBlock(logger, bdir, nil)
if err != nil {
return resid, errors.Wrap(err, "open block")
}
defer runutil.CloseWithErrCapture(&err, b, "repair block reader")
indexr, err := b.Index()
if err != nil {
return resid, errors.Wrap(err, "open index")
}
defer runutil.CloseWithErrCapture(&err, indexr, "repair index reader")
chunkr, err := b.Chunks()
if err != nil {
return resid, errors.Wrap(err, "open chunks")
}
defer runutil.CloseWithErrCapture(&err, chunkr, "repair chunk reader")
resdir := filepath.Join(dir, resid.String())
chunkw, err := chunks.NewWriter(filepath.Join(resdir, ChunksDirname))
if err != nil {
return resid, errors.Wrap(err, "open chunk writer")
}
defer runutil.CloseWithErrCapture(&err, chunkw, "repair chunk writer")
indexw, err := index.NewWriter(filepath.Join(resdir, IndexFilename))
if err != nil {
return resid, errors.Wrap(err, "open index writer")
}
defer runutil.CloseWithErrCapture(&err, indexw, "repair index writer")
// TODO(fabxc): adapt so we properly handle the version once we update to an upstream
// that has multiple.
resmeta := *meta
resmeta.ULID = resid
resmeta.Stats = tsdb.BlockStats{} // Reset stats.
resmeta.Thanos.Source = source // Update source.
if err := rewrite(logger, indexr, chunkr, indexw, chunkw, &resmeta, ignoreChkFns); err != nil {
return resid, errors.Wrap(err, "rewrite block")
}
if err := metadata.Write(logger, resdir, &resmeta); err != nil {
return resid, err
}
// TSDB may rewrite metadata in bdir.
// TODO: This is not needed in newer TSDB code. See https://github.com/prometheus/tsdb/pull/637.
if err := metadata.Write(logger, bdir, meta); err != nil {
return resid, err
}
return resid, nil
}
var castagnoli = crc32.MakeTable(crc32.Castagnoli)
func IgnoreCompleteOutsideChunk(mint int64, maxt int64, _ *chunks.Meta, curr *chunks.Meta) (bool, error) {
if curr.MinTime > maxt || curr.MaxTime < mint {
// "Complete" outsider. Ignore.
return true, nil
}
return false, nil
}
func IgnoreIssue347OutsideChunk(_ int64, maxt int64, _ *chunks.Meta, curr *chunks.Meta) (bool, error) {
if curr.MinTime == maxt {
// "Near" outsider from issue https://github.com/prometheus/tsdb/issues/347. Ignore.
return true, nil
}
return false, nil
}
func IgnoreDuplicateOutsideChunk(_ int64, _ int64, last *chunks.Meta, curr *chunks.Meta) (bool, error) {
if last == nil {
return false, nil
}
if curr.MinTime > last.MaxTime {
return false, nil
}
// Verify that the overlapping chunks are exact copies so we can safely discard
// the current one.
if curr.MinTime != last.MinTime || curr.MaxTime != last.MaxTime {
return false, errors.Errorf("non-sequential chunks not equal: [%d, %d] and [%d, %d]",
last.MinTime, last.MaxTime, curr.MinTime, curr.MaxTime)
}
ca := crc32.Checksum(last.Chunk.Bytes(), castagnoli)
cb := crc32.Checksum(curr.Chunk.Bytes(), castagnoli)
if ca != cb {
return false, errors.Errorf("non-sequential chunks not equal: %x and %x", ca, cb)
}
return true, nil
}
// sanitizeChunkSequence ensures order of the input chunks and drops any duplicates.
// It errors if the sequence contains non-dedupable overlaps.
func sanitizeChunkSequence(chks []chunks.Meta, mint int64, maxt int64, ignoreChkFns []ignoreFnType) ([]chunks.Meta, error) {
if len(chks) == 0 {
return nil, nil
}
// First, ensure that chunks are ordered by their start time.
sort.Slice(chks, func(i, j int) bool {
return chks[i].MinTime < chks[j].MinTime
})
// Remove duplicates, complete outsiders and near outsiders.
repl := make([]chunks.Meta, 0, len(chks))
var last *chunks.Meta
OUTER:
// This compares the current chunk to the chunk from the last iteration
// by pointers. If we use "i, c := range chks" the variable c is a new
// variable who's address doesn't change through the entire loop.
// The current element of the chks slice is copied into it. We must take
// the address of the indexed slice instead.
for i := range chks {
for _, ignoreChkFn := range ignoreChkFns {
ignore, err := ignoreChkFn(mint, maxt, last, &chks[i])
if err != nil {
return nil, errors.Wrap(err, "ignore function")
}
if ignore {
continue OUTER
}
}
last = &chks[i]
repl = append(repl, chks[i])
}
return repl, nil
}
type seriesRepair struct {
lset labels.Labels
chks []chunks.Meta
}
// rewrite writes all data from the readers back into the writers while cleaning
// up mis-ordered and duplicated chunks.
func rewrite(
logger log.Logger,
indexr tsdb.IndexReader, chunkr tsdb.ChunkReader,
indexw tsdb.IndexWriter, chunkw tsdb.ChunkWriter,
meta *metadata.Meta,
ignoreChkFns []ignoreFnType,
) error {
symbols, err := indexr.Symbols()
if err != nil {
return err
}
if err := indexw.AddSymbols(symbols); err != nil {
return err
}
all, err := indexr.Postings(index.AllPostingsKey())
if err != nil {
return err
}
all = indexr.SortedPostings(all)
// We fully rebuild the postings list index from merged series.
var (
postings = index.NewMemPostings()
values = map[string]stringset{}
i = uint64(0)
series = []seriesRepair{}
)
for all.Next() {
var lset labels.Labels
var chks []chunks.Meta
id := all.At()
if err := indexr.Series(id, &lset, &chks); err != nil {
return err
}
// Make sure labels are in sorted order.
sort.Sort(lset)
for i, c := range chks {
chks[i].Chunk, err = chunkr.Chunk(c.Ref)
if err != nil {
return err
}
}
chks, err := sanitizeChunkSequence(chks, meta.MinTime, meta.MaxTime, ignoreChkFns)
if err != nil {
return err
}
if len(chks) == 0 {
continue
}
series = append(series, seriesRepair{
lset: lset,
chks: chks,
})
}
if all.Err() != nil {
return errors.Wrap(all.Err(), "iterate series")
}
// Sort the series, if labels are re-ordered then the ordering of series
// will be different.
sort.Slice(series, func(i, j int) bool {
return labels.Compare(series[i].lset, series[j].lset) < 0
})
lastSet := labels.Labels{}
// Build a new TSDB block.
for _, s := range series {
// The TSDB library will throw an error if we add a series with
// identical labels as the last series. This means that we have
// discovered a duplicate time series in the old block. We drop
// all duplicate series preserving the first one.
// TODO: Add metric to count dropped series if repair becomes a daemon
// rather than a batch job.
if labels.Compare(lastSet, s.lset) == 0 {
level.Warn(logger).Log("msg",
"dropping duplicate series in tsdb block found",
"labelset", s.lset.String(),
)
continue
}
if err := chunkw.WriteChunks(s.chks...); err != nil {
return errors.Wrap(err, "write chunks")
}
if err := indexw.AddSeries(i, s.lset, s.chks...); err != nil {
return errors.Wrap(err, "add series")
}
meta.Stats.NumChunks += uint64(len(s.chks))
meta.Stats.NumSeries++
for _, chk := range s.chks {
meta.Stats.NumSamples += uint64(chk.Chunk.NumSamples())
}
for _, l := range s.lset {
valset, ok := values[l.Name]
if !ok {
valset = stringset{}
values[l.Name] = valset
}
valset.set(l.Value)
}
postings.Add(i, s.lset)
i++
lastSet = s.lset
}
s := make([]string, 0, 256)
for n, v := range values {
s = s[:0]
for x := range v {
s = append(s, x)
}
if err := indexw.WriteLabelIndex([]string{n}, s); err != nil {
return errors.Wrap(err, "write label index")
}
}
for _, l := range postings.SortedKeys() {
if err := indexw.WritePostings(l.Name, l.Value, postings.Get(l.Name, l.Value)); err != nil {
return errors.Wrap(err, "write postings")
}
}
return nil
}
type stringset map[string]struct{}
func (ss stringset) set(s string) {
ss[s] = struct{}{}
}
func (ss stringset) String() string {
return strings.Join(ss.slice(), ",")
}
func (ss stringset) slice() []string {
slice := make([]string, 0, len(ss))
for k := range ss {
slice = append(slice, k)
}
sort.Strings(slice)
return slice
}