Skip to content

Commit

Permalink
Rounds chunk bytes to kb in tsdb and includes benchmarking script (#5479
Browse files Browse the repository at this point in the history
)

* rounds chunk bytes to kb in tsdb and includes benchmarking script

* shellcheck

* fix querier test
  • Loading branch information
owen-d authored Feb 25, 2022
1 parent 6c84304 commit 0a5e01b
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 20 deletions.
8 changes: 2 additions & 6 deletions pkg/storage/tsdb/index/chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,8 @@ type ChunkMeta struct {

MinTime, MaxTime int64

// Bytes use an uint64 as an uint32 can only hold [0,4GB)
// While this is well within current chunk guidelines (1.5MB being "standard"),
// I (owen-d) prefer to overallocate here
// Since TSDB accesses are seeked rather than scanned, this choice
// should have little effect as long as there is enough memory available
Bytes uint64
// Bytes stored, rounded to nearest KB
KB uint32

Entries uint32
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/storage/tsdb/index/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ func (w *Writer) AddSeries(ref storage.SeriesRef, lset labels.Labels, chunks ...
c := chunks[0]
w.buf2.PutVarint64(c.MinTime)
w.buf2.PutUvarint64(uint64(c.MaxTime - c.MinTime))
w.buf2.PutUvarint64(c.Bytes)
w.buf2.PutUvarint32(c.KB)
w.buf2.PutUvarint32(c.Entries)
w.buf2.PutBE32(c.Checksum)
t0 := c.MaxTime
Expand All @@ -468,7 +468,7 @@ func (w *Writer) AddSeries(ref storage.SeriesRef, lset labels.Labels, chunks ...
// instead of uvarint because chunks may overlap
w.buf2.PutVarint64(c.MinTime - t0)
w.buf2.PutUvarint64(uint64(c.MaxTime - c.MinTime))
w.buf2.PutUvarint64(c.Bytes)
w.buf2.PutUvarint32(c.KB)
w.buf2.PutUvarint32(c.Entries)
t0 = c.MaxTime

Expand Down Expand Up @@ -1869,15 +1869,15 @@ func (dec *Decoder) Series(b []byte, lbls *labels.Labels, chks *[]ChunkMeta) err

t0 := d.Varint64()
maxt := int64(d.Uvarint64()) + t0
nBytes := d.Uvarint64()
kb := uint32(d.Uvarint())
entries := uint32(d.Uvarint64())
checksum := d.Be32()

*chks = append(*chks, ChunkMeta{
Checksum: checksum,
MinTime: t0,
MaxTime: maxt,
Bytes: nBytes,
KB: kb,
Entries: entries,
})
t0 = maxt
Expand All @@ -1887,7 +1887,7 @@ func (dec *Decoder) Series(b []byte, lbls *labels.Labels, chks *[]ChunkMeta) err
// instead of uvarint because chunks may overlap
mint := d.Varint64() + t0
maxt := int64(d.Uvarint64()) + mint
nBytes := d.Uvarint64()
kb := uint32(d.Uvarint())
entries := uint32(d.Uvarint64())
checksum := d.Be32()
t0 = maxt
Expand All @@ -1900,7 +1900,7 @@ func (dec *Decoder) Series(b []byte, lbls *labels.Labels, chks *[]ChunkMeta) err
Checksum: checksum,
MinTime: mint,
MaxTime: maxt,
Bytes: nBytes,
KB: kb,
Entries: entries,
})
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/storage/tsdb/querier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ func TestQueryIndex(t *testing.T) {
Checksum: 1,
MinTime: 1,
MaxTime: 10,
Bytes: 10,
KB: 10,
Entries: 10,
},
{
Checksum: 2,
MinTime: 5,
MaxTime: 15,
Bytes: 10,
KB: 10,
Entries: 10,
},
},
Expand All @@ -52,14 +52,14 @@ func TestQueryIndex(t *testing.T) {
Checksum: 3,
MinTime: 20,
MaxTime: 30,
Bytes: 10,
KB: 10,
Entries: 10,
},
{
Checksum: 4,
MinTime: 40,
MaxTime: 50,
Bytes: 10,
KB: 10,
Entries: 10,
},
},
Expand All @@ -71,14 +71,14 @@ func TestQueryIndex(t *testing.T) {
Checksum: 1,
MinTime: 1,
MaxTime: 10,
Bytes: 10,
KB: 10,
Entries: 10,
},
{
Checksum: 2,
MinTime: 5,
MaxTime: 15,
Bytes: 10,
KB: 10,
Entries: 10,
},
},
Expand Down
18 changes: 18 additions & 0 deletions tools/tsdb/tsdb-map/diff.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env bash

old=$1
new=$2

echo benchmarks:
echo

benchstat \
<(LOKI_TSDB_PATH="${old}" go test github.com/grafana/loki/tools/tsdb/tsdb-map -bench=BenchmarkQuery -run '^$' -benchmem) \
<(LOKI_TSDB_PATH="${new}" go test github.com/grafana/loki/tools/tsdb/tsdb-map -bench=BenchmarkQuery -run '^$' -benchmem)

echo
echo sizing:
echo

ls -lh "${old}"
ls -lh "${new}"
4 changes: 2 additions & 2 deletions tools/tsdb/tsdb-map/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ func main() {
Checksum: extractChecksumFromChunkID(entry.ChunkID),
MinTime: int64(entry.From),
MaxTime: int64(entry.Through),
Bytes: (3 << 20) / 4, // guess: 0.75mb, 1/2 of the max size
Entries: 10000, // guess: 10k entries
KB: ((3 << 20) / 4) / 1024, // guess: 0.75mb, 1/2 of the max size, rounded to KB
Entries: 10000, // guess: 10k entries
}})
}

Expand Down

0 comments on commit 0a5e01b

Please sign in to comment.