2017-04-10 18:59:45 +00:00
|
|
|
// Copyright 2017 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2016-12-22 11:05:24 +00:00
|
|
|
package tsdb
|
|
|
|
|
|
|
|
import (
|
2017-01-16 13:18:25 +00:00
|
|
|
"bufio"
|
2016-12-22 11:05:24 +00:00
|
|
|
"encoding/binary"
|
2017-02-15 23:24:53 +00:00
|
|
|
"hash"
|
2016-12-22 11:05:24 +00:00
|
|
|
"hash/crc32"
|
|
|
|
"io"
|
|
|
|
"math"
|
|
|
|
"os"
|
2017-01-06 16:23:12 +00:00
|
|
|
"sync"
|
2017-01-06 14:18:06 +00:00
|
|
|
"time"
|
2016-12-22 11:05:24 +00:00
|
|
|
|
|
|
|
"github.com/coreos/etcd/pkg/fileutil"
|
2017-01-06 14:18:06 +00:00
|
|
|
"github.com/go-kit/kit/log"
|
2016-12-22 14:18:33 +00:00
|
|
|
"github.com/pkg/errors"
|
2017-04-04 09:27:26 +00:00
|
|
|
"github.com/prometheus/tsdb/labels"
|
2016-12-22 11:05:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// WALEntryType indicates what data a WAL entry contains.
|
|
|
|
type WALEntryType byte
|
|
|
|
|
|
|
|
const (
|
2017-02-14 23:54:52 +00:00
|
|
|
// WALMagic is a 4 byte number every WAL segment file starts with.
|
|
|
|
WALMagic = uint32(0x43AF00EF)
|
2017-02-14 08:24:53 +00:00
|
|
|
|
2017-02-14 23:54:52 +00:00
|
|
|
// WALFormatDefault is the version flag for the default outer segment file format.
|
|
|
|
WALFormatDefault = byte(1)
|
|
|
|
)
|
2017-02-14 08:24:53 +00:00
|
|
|
|
2017-02-14 23:54:52 +00:00
|
|
|
// Entry types in a segment file.
|
|
|
|
const (
|
|
|
|
WALEntrySymbols WALEntryType = 1
|
|
|
|
WALEntrySeries WALEntryType = 2
|
|
|
|
WALEntrySamples WALEntryType = 3
|
2017-05-23 10:45:16 +00:00
|
|
|
WALEntryDeletes WALEntryType = 4
|
2016-12-22 11:05:24 +00:00
|
|
|
)
|
|
|
|
|
2017-05-23 10:45:16 +00:00
|
|
|
// SamplesCB yolo.
|
|
|
|
type SamplesCB func([]RefSample) error
|
|
|
|
|
|
|
|
// SeriesCB yolo.
|
|
|
|
type SeriesCB func([]labels.Labels) error
|
|
|
|
|
|
|
|
// DeletesCB yolo.
|
|
|
|
type DeletesCB func([]stone) error
|
|
|
|
|
2017-05-13 15:09:26 +00:00
|
|
|
// SegmentWAL is a write ahead log for series data.
|
|
|
|
type SegmentWAL struct {
|
2017-01-06 16:23:12 +00:00
|
|
|
mtx sync.Mutex
|
|
|
|
|
2017-02-14 07:53:19 +00:00
|
|
|
dirFile *os.File
|
2017-02-19 12:01:19 +00:00
|
|
|
files []*os.File
|
2017-02-14 07:53:19 +00:00
|
|
|
|
2017-01-06 14:18:06 +00:00
|
|
|
logger log.Logger
|
|
|
|
flushInterval time.Duration
|
2017-02-14 23:54:52 +00:00
|
|
|
segmentSize int64
|
2017-01-06 14:18:06 +00:00
|
|
|
|
2017-02-15 23:24:53 +00:00
|
|
|
crc32 hash.Hash32
|
|
|
|
cur *bufio.Writer
|
|
|
|
curN int64
|
2017-02-14 07:53:19 +00:00
|
|
|
|
2017-01-06 14:18:06 +00:00
|
|
|
stopc chan struct{}
|
|
|
|
donec chan struct{}
|
2016-12-22 11:05:24 +00:00
|
|
|
}
|
|
|
|
|
2017-05-13 15:09:26 +00:00
|
|
|
// WAL is a write ahead log that can log new series labels and samples.
|
|
|
|
// It must be completely read before new entries are logged.
|
|
|
|
type WAL interface {
|
|
|
|
Reader() WALReader
|
2017-05-23 10:45:16 +00:00
|
|
|
LogSeries([]labels.Labels) error
|
|
|
|
LogSamples([]RefSample) error
|
|
|
|
LogDeletes(TombstoneReader) error
|
2017-05-13 15:09:26 +00:00
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
|
|
|
// WALReader reads entries from a WAL.
|
|
|
|
type WALReader interface {
|
2017-05-23 10:45:16 +00:00
|
|
|
Read(SeriesCB, SamplesCB, DeletesCB) error
|
2017-05-13 15:09:26 +00:00
|
|
|
}
|
|
|
|
|
2017-05-12 15:06:26 +00:00
|
|
|
// RefSample is a timestamp/value pair associated with a reference to a series.
|
|
|
|
type RefSample struct {
|
|
|
|
Ref uint64
|
|
|
|
T int64
|
|
|
|
V float64
|
|
|
|
}
|
|
|
|
|
2017-02-14 07:53:19 +00:00
|
|
|
const (
|
2017-03-02 20:53:11 +00:00
|
|
|
walSegmentSizeBytes = 256 * 1024 * 1024 // 256 MB
|
2017-02-14 07:53:19 +00:00
|
|
|
)
|
2017-01-06 08:26:39 +00:00
|
|
|
|
2017-03-20 13:45:27 +00:00
|
|
|
// The table gets initialized with sync.Once but may still cause a race
|
|
|
|
// with any other use of the crc32 package anywhere. Thus we initialize it
|
|
|
|
// before.
|
|
|
|
var castagnoliTable *crc32.Table
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
castagnoliTable = crc32.MakeTable(crc32.Castagnoli)
|
|
|
|
}
|
|
|
|
|
2017-05-13 15:09:26 +00:00
|
|
|
// OpenSegmentWAL opens or creates a write ahead log in the given directory.
|
2016-12-22 14:18:33 +00:00
|
|
|
// The WAL must be read completely before new data is written.
|
2017-05-13 15:09:26 +00:00
|
|
|
func OpenSegmentWAL(dir string, logger log.Logger, flushInterval time.Duration) (*SegmentWAL, error) {
|
2016-12-22 11:05:24 +00:00
|
|
|
if err := os.MkdirAll(dir, 0777); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-02-14 07:53:19 +00:00
|
|
|
df, err := fileutil.OpenDir(dir)
|
2016-12-22 15:14:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-03-14 18:30:23 +00:00
|
|
|
if logger == nil {
|
|
|
|
logger = log.NewNopLogger()
|
|
|
|
}
|
2016-12-22 11:05:24 +00:00
|
|
|
|
2017-05-13 15:09:26 +00:00
|
|
|
w := &SegmentWAL{
|
2017-02-14 07:53:19 +00:00
|
|
|
dirFile: df,
|
2017-03-14 18:30:23 +00:00
|
|
|
logger: logger,
|
2017-01-06 14:18:06 +00:00
|
|
|
flushInterval: flushInterval,
|
|
|
|
donec: make(chan struct{}),
|
|
|
|
stopc: make(chan struct{}),
|
2017-02-14 23:54:52 +00:00
|
|
|
segmentSize: walSegmentSizeBytes,
|
2017-03-20 13:45:27 +00:00
|
|
|
crc32: crc32.New(castagnoliTable),
|
2016-12-22 11:05:24 +00:00
|
|
|
}
|
2017-02-14 07:53:19 +00:00
|
|
|
if err := w.initSegments(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-01-06 14:18:06 +00:00
|
|
|
go w.run(flushInterval)
|
|
|
|
|
2016-12-22 11:05:24 +00:00
|
|
|
return w, nil
|
|
|
|
}
|
|
|
|
|
2017-02-15 05:54:59 +00:00
|
|
|
// Reader returns a new reader over the the write ahead log data.
|
|
|
|
// It must be completely consumed before writing to the WAL.
|
2017-05-13 15:09:26 +00:00
|
|
|
func (w *SegmentWAL) Reader() WALReader {
|
2017-05-12 15:06:26 +00:00
|
|
|
return newWALReader(w, w.logger)
|
2016-12-22 14:18:33 +00:00
|
|
|
}
|
|
|
|
|
2016-12-22 11:05:24 +00:00
|
|
|
// Log writes a batch of new series labels and samples to the log.
|
2017-05-23 10:45:16 +00:00
|
|
|
//func (w *SegmentWAL) Log(series []labels.Labels, samples []RefSample) error {
|
|
|
|
//return nil
|
|
|
|
//}
|
|
|
|
|
|
|
|
// LogSeries writes a batch of new series labels to the log.
|
|
|
|
func (w *SegmentWAL) LogSeries(series []labels.Labels) error {
|
2017-02-14 07:53:19 +00:00
|
|
|
if err := w.encodeSeries(series); err != nil {
|
2016-12-22 11:05:24 +00:00
|
|
|
return err
|
|
|
|
}
|
2017-05-23 10:45:16 +00:00
|
|
|
|
|
|
|
if w.flushInterval <= 0 {
|
|
|
|
return w.Sync()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LogSamples writes a batch of new samples to the log.
|
|
|
|
func (w *SegmentWAL) LogSamples(samples []RefSample) error {
|
2017-02-14 07:53:19 +00:00
|
|
|
if err := w.encodeSamples(samples); err != nil {
|
2016-12-22 11:05:24 +00:00
|
|
|
return err
|
|
|
|
}
|
2017-05-23 10:45:16 +00:00
|
|
|
|
|
|
|
if w.flushInterval <= 0 {
|
|
|
|
return w.Sync()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LogDeletes write a batch of new deletes to the log.
|
|
|
|
func (w *SegmentWAL) LogDeletes(tr TombstoneReader) error {
|
|
|
|
if err := w.encodeDeletes(tr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-01-06 14:18:06 +00:00
|
|
|
if w.flushInterval <= 0 {
|
2017-02-14 07:53:19 +00:00
|
|
|
return w.Sync()
|
2017-01-06 14:18:06 +00:00
|
|
|
}
|
2016-12-22 11:05:24 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-14 07:53:19 +00:00
|
|
|
// initSegments finds all existing segment files and opens them in the
|
|
|
|
// appropriate file modes.
|
2017-05-13 15:09:26 +00:00
|
|
|
func (w *SegmentWAL) initSegments() error {
|
2017-02-14 07:53:19 +00:00
|
|
|
fns, err := sequenceFiles(w.dirFile.Name(), "")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(fns) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2017-04-28 13:41:42 +00:00
|
|
|
// We must open all files in read/write mode as we may have to truncate along
|
2017-03-14 18:30:23 +00:00
|
|
|
// the way and any file may become the tail.
|
|
|
|
for _, fn := range fns {
|
|
|
|
f, err := os.OpenFile(fn, os.O_RDWR, 0666)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-02-14 07:53:19 +00:00
|
|
|
}
|
2017-03-14 18:30:23 +00:00
|
|
|
w.files = append(w.files, f)
|
2017-02-14 07:53:19 +00:00
|
|
|
}
|
|
|
|
|
2017-02-14 08:24:53 +00:00
|
|
|
// Consume and validate meta headers.
|
|
|
|
for _, f := range w.files {
|
|
|
|
metab := make([]byte, 8)
|
|
|
|
|
|
|
|
if n, err := f.Read(metab); err != nil {
|
|
|
|
return errors.Wrapf(err, "validate meta %q", f.Name())
|
|
|
|
} else if n != 8 {
|
|
|
|
return errors.Errorf("invalid header size %d in %q", n, f.Name())
|
|
|
|
}
|
|
|
|
|
|
|
|
if m := binary.BigEndian.Uint32(metab[:4]); m != WALMagic {
|
|
|
|
return errors.Errorf("invalid magic header %x in %q", m, f.Name())
|
|
|
|
}
|
|
|
|
if metab[4] != WALFormatDefault {
|
|
|
|
return errors.Errorf("unknown WAL segment format %d in %q", metab[4], f.Name())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-14 07:53:19 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-04-28 13:41:42 +00:00
|
|
|
// cut finishes the currently active segments and opens the next one.
|
2017-02-14 07:53:19 +00:00
|
|
|
// The encoder is reset to point to the new segment.
|
2017-05-13 15:09:26 +00:00
|
|
|
func (w *SegmentWAL) cut() error {
|
2017-04-28 13:41:42 +00:00
|
|
|
// Sync current tail to disk and close.
|
2017-02-14 07:53:19 +00:00
|
|
|
if tf := w.tail(); tf != nil {
|
2017-02-14 23:54:52 +00:00
|
|
|
if err := w.sync(); err != nil {
|
2017-02-14 07:53:19 +00:00
|
|
|
return err
|
|
|
|
}
|
2017-02-15 05:54:59 +00:00
|
|
|
off, err := tf.Seek(0, os.SEEK_CUR)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := tf.Truncate(off); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-02-14 23:54:52 +00:00
|
|
|
if err := tf.Close(); err != nil {
|
2017-02-14 07:53:19 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
p, _, err := nextSequenceFile(w.dirFile.Name(), "")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-02-19 12:01:19 +00:00
|
|
|
f, err := os.Create(p)
|
2017-02-14 07:53:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-02-19 12:01:19 +00:00
|
|
|
if err = fileutil.Preallocate(f, w.segmentSize, true); err != nil {
|
2017-02-14 07:53:19 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = w.dirFile.Sync(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-02-14 08:24:53 +00:00
|
|
|
// Write header metadata for new file.
|
|
|
|
metab := make([]byte, 8)
|
|
|
|
binary.BigEndian.PutUint32(metab[:4], WALMagic)
|
|
|
|
metab[4] = WALFormatDefault
|
|
|
|
|
|
|
|
if _, err := f.Write(metab); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-02-14 07:53:19 +00:00
|
|
|
w.files = append(w.files, f)
|
|
|
|
w.cur = bufio.NewWriterSize(f, 4*1024*1024)
|
2017-02-14 23:54:52 +00:00
|
|
|
w.curN = 8
|
2017-02-14 07:53:19 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-13 15:09:26 +00:00
|
|
|
func (w *SegmentWAL) tail() *os.File {
|
2017-02-14 07:53:19 +00:00
|
|
|
if len(w.files) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return w.files[len(w.files)-1]
|
|
|
|
}
|
|
|
|
|
2017-03-19 16:05:01 +00:00
|
|
|
// Sync flushes the changes to disk.
|
2017-05-13 15:09:26 +00:00
|
|
|
func (w *SegmentWAL) Sync() error {
|
2017-02-14 07:53:19 +00:00
|
|
|
w.mtx.Lock()
|
|
|
|
defer w.mtx.Unlock()
|
|
|
|
|
|
|
|
return w.sync()
|
|
|
|
}
|
|
|
|
|
2017-05-13 15:09:26 +00:00
|
|
|
func (w *SegmentWAL) sync() error {
|
2017-02-15 05:54:59 +00:00
|
|
|
if w.cur == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2017-02-14 07:53:19 +00:00
|
|
|
if err := w.cur.Flush(); err != nil {
|
2016-12-22 15:14:34 +00:00
|
|
|
return err
|
|
|
|
}
|
2017-02-19 12:01:19 +00:00
|
|
|
return fileutil.Fdatasync(w.tail())
|
2016-12-22 11:05:24 +00:00
|
|
|
}
|
|
|
|
|
2017-05-13 15:09:26 +00:00
|
|
|
func (w *SegmentWAL) run(interval time.Duration) {
|
2017-01-06 14:18:06 +00:00
|
|
|
var tick <-chan time.Time
|
|
|
|
|
|
|
|
if interval > 0 {
|
|
|
|
ticker := time.NewTicker(interval)
|
|
|
|
defer ticker.Stop()
|
|
|
|
tick = ticker.C
|
|
|
|
}
|
|
|
|
defer close(w.donec)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-w.stopc:
|
|
|
|
return
|
|
|
|
case <-tick:
|
2017-02-14 07:53:19 +00:00
|
|
|
if err := w.Sync(); err != nil {
|
2017-01-06 14:18:06 +00:00
|
|
|
w.logger.Log("msg", "sync failed", "err", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-28 13:41:42 +00:00
|
|
|
// Close syncs all data and closes the underlying resources.
|
2017-05-13 15:09:26 +00:00
|
|
|
func (w *SegmentWAL) Close() error {
|
2017-01-06 14:18:06 +00:00
|
|
|
close(w.stopc)
|
|
|
|
<-w.donec
|
|
|
|
|
2017-03-04 15:50:48 +00:00
|
|
|
// Lock mutex and leave it locked so we panic if there's a bug causing
|
|
|
|
// the block to be used afterwards.
|
2017-02-14 07:53:19 +00:00
|
|
|
w.mtx.Lock()
|
|
|
|
|
2016-12-22 11:05:24 +00:00
|
|
|
if err := w.sync(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-02-14 23:54:52 +00:00
|
|
|
// On opening, a WAL must be fully consumed once. Afterwards
|
|
|
|
// only the current segment will still be open.
|
2017-02-15 05:54:59 +00:00
|
|
|
if tf := w.tail(); tf != nil {
|
2017-03-14 18:30:23 +00:00
|
|
|
return errors.Wrapf(tf.Close(), "closing WAL tail %s", tf.Name())
|
2017-02-15 05:54:59 +00:00
|
|
|
}
|
|
|
|
return nil
|
2016-12-22 11:05:24 +00:00
|
|
|
}
|
|
|
|
|
2016-12-22 19:00:24 +00:00
|
|
|
const (
|
|
|
|
minSectorSize = 512
|
|
|
|
|
|
|
|
// walPageBytes is the alignment for flushing records to the backing Writer.
|
|
|
|
// It should be a multiple of the minimum sector size so that WAL can safely
|
|
|
|
// distinguish between torn writes and ordinary data corruption.
|
2017-01-09 17:34:29 +00:00
|
|
|
walPageBytes = 16 * minSectorSize
|
2016-12-22 19:00:24 +00:00
|
|
|
)
|
2016-12-22 15:14:34 +00:00
|
|
|
|
2017-05-13 15:09:26 +00:00
|
|
|
func (w *SegmentWAL) entry(et WALEntryType, flag byte, buf []byte) error {
|
2017-02-14 07:53:19 +00:00
|
|
|
w.mtx.Lock()
|
|
|
|
defer w.mtx.Unlock()
|
2017-01-06 17:36:42 +00:00
|
|
|
|
2017-04-28 13:41:42 +00:00
|
|
|
// Cut to the next segment if the entry exceeds the file size unless it would also
|
2017-02-15 05:54:59 +00:00
|
|
|
// exceed the size of a new segment.
|
|
|
|
var (
|
2017-04-28 13:41:42 +00:00
|
|
|
// 6-byte header + 4-byte CRC32 + buf.
|
2017-02-15 05:54:59 +00:00
|
|
|
sz = int64(6 + 4 + len(buf))
|
|
|
|
newsz = w.curN + sz
|
|
|
|
)
|
2017-03-08 19:52:03 +00:00
|
|
|
// XXX(fabxc): this currently cuts a new file whenever the WAL was newly opened.
|
|
|
|
// Probably fine in general but may yield a lot of short files in some cases.
|
2017-02-15 05:54:59 +00:00
|
|
|
if w.cur == nil || w.curN > w.segmentSize || newsz > w.segmentSize && sz <= w.segmentSize {
|
2017-02-14 07:53:19 +00:00
|
|
|
if err := w.cut(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2017-01-06 17:36:42 +00:00
|
|
|
|
2017-02-15 23:24:53 +00:00
|
|
|
w.crc32.Reset()
|
|
|
|
wr := io.MultiWriter(w.crc32, w.cur)
|
2016-12-22 11:05:24 +00:00
|
|
|
|
|
|
|
b := make([]byte, 6)
|
|
|
|
b[0] = byte(et)
|
|
|
|
b[1] = flag
|
|
|
|
|
2017-01-06 17:36:42 +00:00
|
|
|
binary.BigEndian.PutUint32(b[2:], uint32(len(buf)))
|
2016-12-22 11:05:24 +00:00
|
|
|
|
2017-02-14 07:53:19 +00:00
|
|
|
if _, err := wr.Write(b); err != nil {
|
2016-12-22 11:05:24 +00:00
|
|
|
return err
|
|
|
|
}
|
2017-02-14 07:53:19 +00:00
|
|
|
if _, err := wr.Write(buf); err != nil {
|
2016-12-22 11:05:24 +00:00
|
|
|
return err
|
|
|
|
}
|
2017-02-15 23:24:53 +00:00
|
|
|
if _, err := w.cur.Write(w.crc32.Sum(nil)); err != nil {
|
2016-12-22 11:05:24 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-02-14 07:53:19 +00:00
|
|
|
w.curN += sz
|
|
|
|
|
2017-01-06 17:36:42 +00:00
|
|
|
putWALBuffer(buf)
|
2016-12-22 11:05:24 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
walSeriesSimple = 1
|
|
|
|
walSamplesSimple = 1
|
2017-05-23 10:45:16 +00:00
|
|
|
walDeletesSimple = 1
|
2016-12-22 11:05:24 +00:00
|
|
|
)
|
|
|
|
|
2017-01-06 17:36:42 +00:00
|
|
|
var walBuffers = sync.Pool{}
|
|
|
|
|
|
|
|
func getWALBuffer() []byte {
|
|
|
|
b := walBuffers.Get()
|
|
|
|
if b == nil {
|
|
|
|
return make([]byte, 0, 64*1024)
|
|
|
|
}
|
|
|
|
return b.([]byte)
|
|
|
|
}
|
|
|
|
|
|
|
|
func putWALBuffer(b []byte) {
|
|
|
|
b = b[:0]
|
|
|
|
walBuffers.Put(b)
|
|
|
|
}
|
|
|
|
|
2017-05-13 15:09:26 +00:00
|
|
|
func (w *SegmentWAL) encodeSeries(series []labels.Labels) error {
|
2016-12-22 11:05:24 +00:00
|
|
|
if len(series) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2016-12-22 14:18:33 +00:00
|
|
|
|
|
|
|
b := make([]byte, binary.MaxVarintLen32)
|
2017-01-06 17:36:42 +00:00
|
|
|
buf := getWALBuffer()
|
2016-12-22 11:05:24 +00:00
|
|
|
|
|
|
|
for _, lset := range series {
|
|
|
|
n := binary.PutUvarint(b, uint64(len(lset)))
|
2017-01-06 17:36:42 +00:00
|
|
|
buf = append(buf, b[:n]...)
|
2016-12-22 11:05:24 +00:00
|
|
|
|
|
|
|
for _, l := range lset {
|
|
|
|
n = binary.PutUvarint(b, uint64(len(l.Name)))
|
2017-01-06 17:36:42 +00:00
|
|
|
buf = append(buf, b[:n]...)
|
|
|
|
buf = append(buf, l.Name...)
|
2016-12-22 11:05:24 +00:00
|
|
|
|
|
|
|
n = binary.PutUvarint(b, uint64(len(l.Value)))
|
2017-01-06 17:36:42 +00:00
|
|
|
buf = append(buf, b[:n]...)
|
|
|
|
buf = append(buf, l.Value...)
|
2016-12-22 11:05:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-14 07:53:19 +00:00
|
|
|
return w.entry(WALEntrySeries, walSeriesSimple, buf)
|
2016-12-22 11:05:24 +00:00
|
|
|
}
|
|
|
|
|
2017-05-13 15:09:26 +00:00
|
|
|
func (w *SegmentWAL) encodeSamples(samples []RefSample) error {
|
2016-12-22 11:05:24 +00:00
|
|
|
if len(samples) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2016-12-22 14:18:33 +00:00
|
|
|
|
|
|
|
b := make([]byte, binary.MaxVarintLen64)
|
2017-01-06 17:36:42 +00:00
|
|
|
buf := getWALBuffer()
|
2016-12-22 11:05:24 +00:00
|
|
|
|
|
|
|
// Store base timestamp and base reference number of first sample.
|
|
|
|
// All samples encode their timestamp and ref as delta to those.
|
|
|
|
//
|
|
|
|
// TODO(fabxc): optimize for all samples having the same timestamp.
|
|
|
|
first := samples[0]
|
|
|
|
|
2017-05-12 15:06:26 +00:00
|
|
|
binary.BigEndian.PutUint64(b, first.Ref)
|
2017-01-17 07:40:31 +00:00
|
|
|
buf = append(buf, b[:8]...)
|
2017-05-12 15:06:26 +00:00
|
|
|
binary.BigEndian.PutUint64(b, uint64(first.T))
|
2017-01-06 17:36:42 +00:00
|
|
|
buf = append(buf, b[:8]...)
|
2016-12-22 11:05:24 +00:00
|
|
|
|
|
|
|
for _, s := range samples {
|
2017-05-12 15:06:26 +00:00
|
|
|
n := binary.PutVarint(b, int64(s.Ref)-int64(first.Ref))
|
2017-01-06 17:36:42 +00:00
|
|
|
buf = append(buf, b[:n]...)
|
2016-12-22 11:05:24 +00:00
|
|
|
|
2017-05-12 15:06:26 +00:00
|
|
|
n = binary.PutVarint(b, s.T-first.T)
|
2017-01-06 17:36:42 +00:00
|
|
|
buf = append(buf, b[:n]...)
|
2016-12-22 11:05:24 +00:00
|
|
|
|
2017-05-12 15:06:26 +00:00
|
|
|
binary.BigEndian.PutUint64(b, math.Float64bits(s.V))
|
2017-01-06 17:36:42 +00:00
|
|
|
buf = append(buf, b[:8]...)
|
2016-12-22 11:05:24 +00:00
|
|
|
}
|
|
|
|
|
2017-02-14 07:53:19 +00:00
|
|
|
return w.entry(WALEntrySamples, walSamplesSimple, buf)
|
2016-12-22 11:05:24 +00:00
|
|
|
}
|
|
|
|
|
2017-05-23 10:45:16 +00:00
|
|
|
func (w *SegmentWAL) encodeDeletes(tr TombstoneReader) error {
|
|
|
|
b := make([]byte, 2*binary.MaxVarintLen64)
|
|
|
|
eb := &encbuf{b: b}
|
|
|
|
buf := getWALBuffer()
|
|
|
|
for tr.Next() {
|
|
|
|
eb.reset()
|
|
|
|
s := tr.At()
|
|
|
|
eb.putUvarint32(s.ref)
|
|
|
|
eb.putUvarint(len(s.intervals))
|
|
|
|
buf = append(buf, eb.get()...)
|
|
|
|
for _, itv := range s.intervals {
|
|
|
|
eb.reset()
|
|
|
|
eb.putVarint64(itv.mint)
|
|
|
|
eb.putVarint64(itv.maxt)
|
|
|
|
buf = append(buf, eb.get()...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return w.entry(WALEntryDeletes, walDeletesSimple, buf)
|
|
|
|
}
|
|
|
|
|
2017-05-12 15:06:26 +00:00
|
|
|
// walReader decodes and emits write ahead log entries.
|
|
|
|
type walReader struct {
|
2017-03-14 18:30:23 +00:00
|
|
|
logger log.Logger
|
|
|
|
|
2017-05-13 15:09:26 +00:00
|
|
|
wal *SegmentWAL
|
2017-02-15 23:24:53 +00:00
|
|
|
cur int
|
|
|
|
buf []byte
|
|
|
|
crc32 hash.Hash32
|
2017-02-15 05:54:59 +00:00
|
|
|
|
2017-05-12 15:06:26 +00:00
|
|
|
samples []RefSample
|
2017-05-23 10:45:16 +00:00
|
|
|
series []labels.Labels
|
|
|
|
stones []stone
|
|
|
|
|
|
|
|
samplesFunc SamplesCB
|
|
|
|
seriesFunc SeriesCB
|
|
|
|
deletesFunc DeletesCB
|
|
|
|
|
|
|
|
err error
|
2016-12-22 14:18:33 +00:00
|
|
|
}
|
|
|
|
|
2017-05-13 15:09:26 +00:00
|
|
|
func newWALReader(w *SegmentWAL, l log.Logger) *walReader {
|
2017-05-12 15:06:26 +00:00
|
|
|
if l == nil {
|
|
|
|
l = log.NewNopLogger()
|
2017-03-14 18:30:23 +00:00
|
|
|
}
|
2017-05-12 15:06:26 +00:00
|
|
|
return &walReader{
|
|
|
|
logger: l,
|
2017-03-14 18:30:23 +00:00
|
|
|
wal: w,
|
|
|
|
buf: make([]byte, 0, 128*4096),
|
|
|
|
crc32: crc32.New(crc32.MakeTable(crc32.Castagnoli)),
|
2016-12-22 14:18:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-15 05:54:59 +00:00
|
|
|
// Err returns the last error the reader encountered.
|
2017-05-12 15:06:26 +00:00
|
|
|
func (r *walReader) Err() error {
|
2017-02-15 05:54:59 +00:00
|
|
|
return r.err
|
|
|
|
}
|
|
|
|
|
2017-05-23 10:45:16 +00:00
|
|
|
func (r *walReader) Read(seriesf SeriesCB, samplesf SamplesCB, deletesf DeletesCB) error {
|
|
|
|
r.samplesFunc = samplesf
|
|
|
|
r.seriesFunc = seriesf
|
|
|
|
r.deletesFunc = deletesf
|
|
|
|
|
|
|
|
for r.next() {
|
|
|
|
}
|
|
|
|
|
|
|
|
return r.Err()
|
|
|
|
}
|
|
|
|
|
2017-02-15 05:54:59 +00:00
|
|
|
// nextEntry retrieves the next entry. It is also used as a testing hook.
|
2017-05-12 15:06:26 +00:00
|
|
|
func (r *walReader) nextEntry() (WALEntryType, byte, []byte, error) {
|
2017-03-14 18:30:23 +00:00
|
|
|
if r.cur >= len(r.wal.files) {
|
2017-02-15 05:54:59 +00:00
|
|
|
return 0, 0, nil, io.EOF
|
|
|
|
}
|
2017-03-14 18:30:23 +00:00
|
|
|
cf := r.wal.files[r.cur]
|
2017-02-15 05:54:59 +00:00
|
|
|
|
2017-03-14 18:30:23 +00:00
|
|
|
et, flag, b, err := r.entry(cf)
|
2017-03-08 15:53:07 +00:00
|
|
|
// If we reached the end of the reader, advance to the next one
|
|
|
|
// and close.
|
|
|
|
// Do not close on the last one as it will still be appended to.
|
2017-03-14 18:30:23 +00:00
|
|
|
if err == io.EOF && r.cur < len(r.wal.files)-1 {
|
2017-02-15 05:54:59 +00:00
|
|
|
// Current reader completed, close and move to the next one.
|
2017-03-14 18:30:23 +00:00
|
|
|
if err := cf.Close(); err != nil {
|
2017-02-15 05:54:59 +00:00
|
|
|
return 0, 0, nil, err
|
|
|
|
}
|
|
|
|
r.cur++
|
|
|
|
return r.nextEntry()
|
|
|
|
}
|
|
|
|
return et, flag, b, err
|
|
|
|
}
|
|
|
|
|
2017-05-23 10:45:16 +00:00
|
|
|
// next returns decodes the next entry pair and returns true
|
2017-02-15 05:54:59 +00:00
|
|
|
// if it was succesful.
|
2017-05-23 10:45:16 +00:00
|
|
|
func (r *walReader) next() bool {
|
|
|
|
r.series = r.series[:0]
|
2017-02-15 05:54:59 +00:00
|
|
|
r.samples = r.samples[:0]
|
2017-05-23 10:45:16 +00:00
|
|
|
r.stones = r.stones[:0]
|
2017-02-15 05:54:59 +00:00
|
|
|
|
2017-03-14 18:30:23 +00:00
|
|
|
if r.cur >= len(r.wal.files) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
cf := r.wal.files[r.cur]
|
|
|
|
|
|
|
|
// Save position after last valid entry if we have to truncate the WAL.
|
|
|
|
lastOffset, err := cf.Seek(0, os.SEEK_CUR)
|
2017-02-14 23:54:52 +00:00
|
|
|
if err != nil {
|
2017-03-14 18:30:23 +00:00
|
|
|
r.err = err
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
et, flag, b, err := r.entry(cf)
|
|
|
|
// If we reached the end of the reader, advance to the next one
|
|
|
|
// and close.
|
|
|
|
// Do not close on the last one as it will still be appended to.
|
|
|
|
if err == io.EOF {
|
|
|
|
if r.cur == len(r.wal.files)-1 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// Current reader completed, close and move to the next one.
|
|
|
|
if err := cf.Close(); err != nil {
|
2017-02-15 05:54:59 +00:00
|
|
|
r.err = err
|
2017-03-14 18:30:23 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
r.cur++
|
2017-05-23 10:45:16 +00:00
|
|
|
return r.next()
|
2017-03-14 18:30:23 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
r.err = err
|
|
|
|
|
|
|
|
if _, ok := err.(walCorruptionErr); ok {
|
|
|
|
r.err = r.truncate(lastOffset)
|
2017-02-15 05:54:59 +00:00
|
|
|
}
|
|
|
|
return false
|
2017-02-14 23:54:52 +00:00
|
|
|
}
|
2017-02-15 05:54:59 +00:00
|
|
|
|
2017-03-14 18:30:23 +00:00
|
|
|
// In decoding below we never return a walCorruptionErr for now.
|
|
|
|
// Those should generally be catched by entry decoding before.
|
2017-02-15 05:54:59 +00:00
|
|
|
switch et {
|
2017-02-14 23:54:52 +00:00
|
|
|
case WALEntrySeries:
|
2017-05-23 10:45:16 +00:00
|
|
|
r.err = r.decodeSeries(flag, b)
|
|
|
|
case WALEntrySamples:
|
|
|
|
r.err = r.decodeSamples(flag, b)
|
|
|
|
case WALEntryDeletes:
|
|
|
|
r.err = r.decodeDeletes(flag, b)
|
2017-02-15 05:54:59 +00:00
|
|
|
}
|
|
|
|
return r.err == nil
|
|
|
|
}
|
|
|
|
|
2017-05-12 15:06:26 +00:00
|
|
|
func (r *walReader) current() *os.File {
|
2017-03-14 18:30:23 +00:00
|
|
|
return r.wal.files[r.cur]
|
|
|
|
}
|
|
|
|
|
|
|
|
// truncate the WAL after the last valid entry.
|
2017-05-12 15:06:26 +00:00
|
|
|
func (r *walReader) truncate(lastOffset int64) error {
|
2017-03-14 18:30:23 +00:00
|
|
|
r.logger.Log("msg", "WAL corruption detected; truncating",
|
|
|
|
"err", r.err, "file", r.current().Name(), "pos", lastOffset)
|
|
|
|
|
|
|
|
// Close and delete all files after the current one.
|
|
|
|
for _, f := range r.wal.files[r.cur+1:] {
|
|
|
|
if err := f.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := os.Remove(f.Name()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
r.wal.files = r.wal.files[:r.cur+1]
|
|
|
|
|
|
|
|
// Seek the current file to the last valid offset where we continue writing from.
|
|
|
|
_, err := r.current().Seek(lastOffset, os.SEEK_SET)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// walCorruptionErr is a type wrapper for errors that indicate WAL corruption
|
|
|
|
// and trigger a truncation.
|
|
|
|
type walCorruptionErr error
|
|
|
|
|
|
|
|
func walCorruptionErrf(s string, args ...interface{}) error {
|
|
|
|
return walCorruptionErr(errors.Errorf(s, args...))
|
|
|
|
}
|
|
|
|
|
2017-05-12 15:06:26 +00:00
|
|
|
func (r *walReader) entry(cr io.Reader) (WALEntryType, byte, []byte, error) {
|
2017-02-15 23:24:53 +00:00
|
|
|
r.crc32.Reset()
|
|
|
|
tr := io.TeeReader(cr, r.crc32)
|
2017-02-15 05:54:59 +00:00
|
|
|
|
|
|
|
b := make([]byte, 6)
|
2017-03-14 18:30:23 +00:00
|
|
|
if n, err := tr.Read(b); err != nil {
|
2017-02-15 05:54:59 +00:00
|
|
|
return 0, 0, nil, err
|
2017-03-14 18:30:23 +00:00
|
|
|
} else if n != 6 {
|
|
|
|
return 0, 0, nil, walCorruptionErrf("invalid entry header size %d", n)
|
2017-02-15 05:54:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
etype = WALEntryType(b[0])
|
|
|
|
flag = b[1]
|
|
|
|
length = int(binary.BigEndian.Uint32(b[2:]))
|
|
|
|
)
|
|
|
|
// Exit if we reached pre-allocated space.
|
|
|
|
if etype == 0 {
|
|
|
|
return 0, 0, nil, io.EOF
|
2017-02-14 23:54:52 +00:00
|
|
|
}
|
2017-05-23 10:45:16 +00:00
|
|
|
if etype != WALEntrySeries && etype != WALEntrySamples && etype != WALEntryDeletes {
|
2017-03-14 18:30:23 +00:00
|
|
|
return 0, 0, nil, walCorruptionErrf("invalid entry type %d", etype)
|
|
|
|
}
|
2017-02-15 05:54:59 +00:00
|
|
|
|
|
|
|
if length > len(r.buf) {
|
|
|
|
r.buf = make([]byte, length)
|
|
|
|
}
|
|
|
|
buf := r.buf[:length]
|
|
|
|
|
2017-03-14 18:30:23 +00:00
|
|
|
if n, err := tr.Read(buf); err != nil {
|
2017-02-15 05:54:59 +00:00
|
|
|
return 0, 0, nil, err
|
2017-03-14 18:30:23 +00:00
|
|
|
} else if n != length {
|
|
|
|
return 0, 0, nil, walCorruptionErrf("invalid entry body size %d", n)
|
2017-02-15 05:54:59 +00:00
|
|
|
}
|
2017-03-14 18:30:23 +00:00
|
|
|
|
|
|
|
if n, err := cr.Read(b[:4]); err != nil {
|
2017-02-15 05:54:59 +00:00
|
|
|
return 0, 0, nil, err
|
2017-03-14 18:30:23 +00:00
|
|
|
} else if n != 4 {
|
|
|
|
return 0, 0, nil, walCorruptionErrf("invalid checksum length %d", n)
|
2017-02-15 05:54:59 +00:00
|
|
|
}
|
2017-02-15 23:24:53 +00:00
|
|
|
if exp, has := binary.BigEndian.Uint32(b[:4]), r.crc32.Sum32(); has != exp {
|
2017-03-14 18:30:23 +00:00
|
|
|
return 0, 0, nil, walCorruptionErrf("unexpected CRC32 checksum %x, want %x", has, exp)
|
2017-02-15 05:54:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return etype, flag, buf, nil
|
2017-02-14 23:54:52 +00:00
|
|
|
}
|
|
|
|
|
2017-05-12 15:06:26 +00:00
|
|
|
func (r *walReader) decodeSeries(flag byte, b []byte) error {
|
2016-12-22 14:18:33 +00:00
|
|
|
for len(b) > 0 {
|
|
|
|
l, n := binary.Uvarint(b)
|
|
|
|
if n < 1 {
|
|
|
|
return errors.Wrap(errInvalidSize, "number of labels")
|
|
|
|
}
|
|
|
|
b = b[n:]
|
|
|
|
lset := make(labels.Labels, l)
|
|
|
|
|
|
|
|
for i := 0; i < int(l); i++ {
|
|
|
|
nl, n := binary.Uvarint(b)
|
|
|
|
if n < 1 || len(b) < n+int(nl) {
|
|
|
|
return errors.Wrap(errInvalidSize, "label name")
|
|
|
|
}
|
|
|
|
lset[i].Name = string(b[n : n+int(nl)])
|
|
|
|
b = b[n+int(nl):]
|
|
|
|
|
|
|
|
vl, n := binary.Uvarint(b)
|
|
|
|
if n < 1 || len(b) < n+int(vl) {
|
|
|
|
return errors.Wrap(errInvalidSize, "label value")
|
|
|
|
}
|
|
|
|
lset[i].Value = string(b[n : n+int(vl)])
|
|
|
|
b = b[n+int(vl):]
|
|
|
|
}
|
|
|
|
|
2017-05-23 10:45:16 +00:00
|
|
|
r.series = append(r.series, lset)
|
2016-12-22 14:18:33 +00:00
|
|
|
}
|
2017-05-23 10:45:16 +00:00
|
|
|
return r.seriesFunc(r.series)
|
2016-12-22 14:18:33 +00:00
|
|
|
}
|
|
|
|
|
2017-05-12 15:06:26 +00:00
|
|
|
func (r *walReader) decodeSamples(flag byte, b []byte) error {
|
2017-05-23 10:45:16 +00:00
|
|
|
r.samples = r.samples[:]
|
|
|
|
|
2017-01-17 07:40:31 +00:00
|
|
|
if len(b) < 16 {
|
2016-12-22 14:18:33 +00:00
|
|
|
return errors.Wrap(errInvalidSize, "header length")
|
|
|
|
}
|
|
|
|
var (
|
2017-01-13 15:14:40 +00:00
|
|
|
baseRef = binary.BigEndian.Uint64(b)
|
2017-01-17 07:40:31 +00:00
|
|
|
baseTime = int64(binary.BigEndian.Uint64(b[8:]))
|
2016-12-22 14:18:33 +00:00
|
|
|
)
|
2017-01-17 07:40:31 +00:00
|
|
|
b = b[16:]
|
2016-12-22 14:18:33 +00:00
|
|
|
|
|
|
|
for len(b) > 0 {
|
2017-05-12 15:06:26 +00:00
|
|
|
var smpl RefSample
|
2016-12-22 14:18:33 +00:00
|
|
|
|
|
|
|
dref, n := binary.Varint(b)
|
|
|
|
if n < 1 {
|
|
|
|
return errors.Wrap(errInvalidSize, "sample ref delta")
|
|
|
|
}
|
|
|
|
b = b[n:]
|
2017-01-13 15:14:40 +00:00
|
|
|
|
2017-05-12 15:06:26 +00:00
|
|
|
smpl.Ref = uint64(int64(baseRef) + dref)
|
2016-12-22 14:18:33 +00:00
|
|
|
|
|
|
|
dtime, n := binary.Varint(b)
|
|
|
|
if n < 1 {
|
|
|
|
return errors.Wrap(errInvalidSize, "sample timestamp delta")
|
|
|
|
}
|
|
|
|
b = b[n:]
|
2017-05-12 15:06:26 +00:00
|
|
|
smpl.T = baseTime + dtime
|
2016-12-22 14:18:33 +00:00
|
|
|
|
|
|
|
if len(b) < 8 {
|
|
|
|
return errors.Wrapf(errInvalidSize, "sample value bits %d", len(b))
|
|
|
|
}
|
2017-05-12 15:06:26 +00:00
|
|
|
smpl.V = float64(math.Float64frombits(binary.BigEndian.Uint64(b)))
|
2016-12-22 14:18:33 +00:00
|
|
|
b = b[8:]
|
|
|
|
|
2017-02-15 05:54:59 +00:00
|
|
|
r.samples = append(r.samples, smpl)
|
2016-12-22 14:18:33 +00:00
|
|
|
}
|
2017-05-23 10:45:16 +00:00
|
|
|
return r.samplesFunc(r.samples)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *walReader) decodeDeletes(flag byte, b []byte) error {
|
|
|
|
db := &decbuf{b: b}
|
|
|
|
r.samples = r.samples[:]
|
|
|
|
|
|
|
|
for db.len() > 0 {
|
|
|
|
var s stone
|
|
|
|
s.ref = uint32(db.uvarint())
|
|
|
|
l := db.uvarint()
|
|
|
|
if db.err() != nil {
|
|
|
|
return db.err()
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < l; i++ {
|
|
|
|
s.intervals = append(s.intervals, interval{db.varint64(), db.varint64()})
|
|
|
|
if db.err() != nil {
|
|
|
|
return db.err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
r.stones = append(r.stones, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
return r.deletesFunc(r.stones)
|
2016-12-22 14:18:33 +00:00
|
|
|
}
|