Skip to content

Commit

Permalink
verify repair: correctly order series in the index on rewrite
Browse files Browse the repository at this point in the history
When we have label sets that are not in the correct order, fixing that
changes the order of the series in the index.  So the index must be
rewritten in that new order.  This makes this repair tool take up a
bunch more memory, but produces blocks that verify correctly.
  • Loading branch information
jjneely committed Mar 25, 2019
1 parent f0dfe27 commit f09d3f5
Showing 1 changed file with 31 additions and 12 deletions.
43 changes: 31 additions & 12 deletions pkg/block/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,11 @@ OUTER:
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(
Expand Down Expand Up @@ -605,12 +610,12 @@ func rewrite(
postings = index.NewMemPostings()
values = map[string]stringset{}
i = uint64(0)
series = []seriesRepair{}
)

var lset labels.Labels
var chks []chunks.Meta

for all.Next() {
var lset labels.Labels
var chks []chunks.Meta
id := all.At()

if err := indexr.Series(id, &lset, &chks); err != nil {
Expand All @@ -636,34 +641,48 @@ func rewrite(
continue
}

if err := chunkw.WriteChunks(chks...); err != nil {
series = append(series, seriesRepair{
lset: lset,
chks: chks,
})
}

if all.Err() != nil {
return errors.Wrap(all.Err(), "iterate series")
}

// sort the series -- if labels moved around the ordering will be different
sort.Slice(series, func(i, j int) bool {
return labels.Compare(series[i].lset, series[j].lset) < 0
})

// build new TSDB block
for _, s := range series {
if err := chunkw.WriteChunks(s.chks...); err != nil {
return errors.Wrap(err, "write chunks")
}
if err := indexw.AddSeries(i, lset, chks...); err != nil {
if err := indexw.AddSeries(i, s.lset, s.chks...); err != nil {
return errors.Wrap(err, "add series")
}

meta.Stats.NumChunks += uint64(len(chks))
meta.Stats.NumChunks += uint64(len(s.chks))
meta.Stats.NumSeries++

for _, chk := range chks {
for _, chk := range s.chks {
meta.Stats.NumSamples += uint64(chk.Chunk.NumSamples())
}

for _, l := range lset {
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, lset)
postings.Add(i, s.lset)
i++
}
if all.Err() != nil {
return errors.Wrap(all.Err(), "iterate series")
}

s := make([]string, 0, 256)
for n, v := range values {
Expand Down

0 comments on commit f09d3f5

Please sign in to comment.