SeriesWriter -> ChunkWriter

This commit simplifies a SeriesWriter into a ChunkWriter and detaches
it entirely from the notion of a series and the series index.
This commit is contained in:
Fabian Reinartz 2017-02-19 11:27:31 +01:00
parent a3d042b54e
commit c808928b90
2 changed files with 21 additions and 25 deletions

View file

@ -177,7 +177,7 @@ func (c *compactor) compact(dir string, blocks ...Block) (err error) {
} }
indexw := newIndexWriter(indexf) indexw := newIndexWriter(indexf)
chunkw := newSeriesWriter(chunkf, indexw) chunkw := newChunkWriter(chunkf)
if err = c.write(dir, blocks, indexw, chunkw); err != nil { if err = c.write(dir, blocks, indexw, chunkw); err != nil {
return errors.Wrap(err, "write compaction") return errors.Wrap(err, "write compaction")
@ -204,7 +204,7 @@ func (c *compactor) compact(dir string, blocks ...Block) (err error) {
return nil return nil
} }
func (c *compactor) write(dir string, blocks []Block, indexw IndexWriter, chunkw SeriesWriter) error { func (c *compactor) write(dir string, blocks []Block, indexw IndexWriter, chunkw ChunkWriter) error {
var set compactionSet var set compactionSet
for i, b := range blocks { for i, b := range blocks {
@ -238,10 +238,12 @@ func (c *compactor) write(dir string, blocks []Block, indexw IndexWriter, chunkw
for set.Next() { for set.Next() {
lset, chunks := set.At() lset, chunks := set.At()
if err := chunkw.WriteSeries(i, lset, chunks); err != nil { if err := chunkw.WriteChunks(chunks...); err != nil {
return err return err
} }
indexw.AddSeries(i, lset, chunks...)
meta.Stats.NumChunks += uint64(len(chunks)) meta.Stats.NumChunks += uint64(len(chunks))
meta.Stats.NumSeries++ meta.Stats.NumSeries++

View file

@ -25,12 +25,13 @@ const (
const compactionPageBytes = minSectorSize * 64 const compactionPageBytes = minSectorSize * 64
// SeriesWriter serializes a time block of chunked series data. // ChunkWriter serializes a time block of chunked series data.
type SeriesWriter interface { type ChunkWriter interface {
// WriteSeries writes the time series data chunks for a single series. // WriteChunks writes several chunks. The data field of the ChunkMetas
// The reference is used to resolve the correct series in the written index. // must be populated.
// It only has to be valid for the duration of the write. // After returning successfully, the Ref fields in the ChunkMetas
WriteSeries(ref uint32, l labels.Labels, chunks []ChunkMeta) error // is set and can be used to retrieve the chunks from the written data.
WriteChunks(chunks ...ChunkMeta) error
// Size returns the size of the data written so far. // Size returns the size of the data written so far.
Size() int64 Size() int64
@ -40,35 +41,32 @@ type SeriesWriter interface {
Close() error Close() error
} }
// seriesWriter implements the SeriesWriter interface for the standard // chunkWriter implements the ChunkWriter interface for the standard
// serialization format. // serialization format.
type seriesWriter struct { type chunkWriter struct {
ow io.Writer ow io.Writer
w *bufio.Writer w *bufio.Writer
n int64 n int64
c int c int
crc32 hash.Hash crc32 hash.Hash
index IndexWriter
} }
func newSeriesWriter(w io.Writer, index IndexWriter) *seriesWriter { func newChunkWriter(w io.Writer) *chunkWriter {
return &seriesWriter{ return &chunkWriter{
ow: w, ow: w,
w: bufio.NewWriterSize(w, 1*1024*1024), w: bufio.NewWriterSize(w, 1*1024*1024),
n: 0, n: 0,
crc32: crc32.New(crc32.MakeTable(crc32.Castagnoli)), crc32: crc32.New(crc32.MakeTable(crc32.Castagnoli)),
index: index,
} }
} }
func (w *seriesWriter) write(wr io.Writer, b []byte) error { func (w *chunkWriter) write(wr io.Writer, b []byte) error {
n, err := wr.Write(b) n, err := wr.Write(b)
w.n += int64(n) w.n += int64(n)
return err return err
} }
func (w *seriesWriter) writeMeta() error { func (w *chunkWriter) writeMeta() error {
b := [8]byte{} b := [8]byte{}
binary.BigEndian.PutUint32(b[:4], MagicSeries) binary.BigEndian.PutUint32(b[:4], MagicSeries)
@ -77,7 +75,7 @@ func (w *seriesWriter) writeMeta() error {
return w.write(w.w, b[:]) return w.write(w.w, b[:])
} }
func (w *seriesWriter) WriteSeries(ref uint32, lset labels.Labels, chks []ChunkMeta) error { func (w *chunkWriter) WriteChunks(chks ...ChunkMeta) error {
// Initialize with meta data. // Initialize with meta data.
if w.n == 0 { if w.n == 0 {
if err := w.writeMeta(); err != nil { if err := w.writeMeta(); err != nil {
@ -122,18 +120,14 @@ func (w *seriesWriter) WriteSeries(ref uint32, lset labels.Labels, chks []ChunkM
if err := w.write(w.w, w.crc32.Sum(nil)); err != nil { if err := w.write(w.w, w.crc32.Sum(nil)); err != nil {
return err return err
} }
if w.index != nil {
w.index.AddSeries(ref, lset, chks...)
}
return nil return nil
} }
func (w *seriesWriter) Size() int64 { func (w *chunkWriter) Size() int64 {
return w.n return w.n
} }
func (w *seriesWriter) Close() error { func (w *chunkWriter) Close() error {
// Initialize block in case no data was written to it. // Initialize block in case no data was written to it.
if w.n == 0 { if w.n == 0 {
if err := w.writeMeta(); err != nil { if err := w.writeMeta(); err != nil {