2017-04-10 11:59:45 -07: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 03:05:24 -08:00
|
|
|
package tsdb
|
|
|
|
|
|
|
|
import (
|
2017-01-16 05:18:25 -08:00
|
|
|
"bufio"
|
2016-12-22 03:05:24 -08:00
|
|
|
"encoding/binary"
|
2017-09-06 05:59:25 -07:00
|
|
|
"fmt"
|
2017-02-15 15:24:53 -08:00
|
|
|
"hash"
|
2016-12-22 03:05:24 -08:00
|
|
|
"hash/crc32"
|
|
|
|
"io"
|
|
|
|
"math"
|
|
|
|
"os"
|
2017-09-06 05:59:25 -07:00
|
|
|
"path/filepath"
|
2017-01-06 08:23:12 -08:00
|
|
|
"sync"
|
2017-01-06 06:18:06 -08:00
|
|
|
"time"
|
2016-12-22 03:05:24 -08:00
|
|
|
|
2021-06-11 09:17:59 -07:00
|
|
|
"github.com/go-kit/log"
|
|
|
|
"github.com/go-kit/log/level"
|
2016-12-22 06:18:33 -08:00
|
|
|
"github.com/pkg/errors"
|
2017-10-04 12:51:34 -07:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2020-10-22 02:00:08 -07:00
|
|
|
|
2021-11-06 03:10:04 -07:00
|
|
|
"github.com/prometheus/prometheus/storage"
|
|
|
|
"github.com/prometheus/prometheus/tsdb/chunks"
|
2019-08-13 01:34:14 -07:00
|
|
|
"github.com/prometheus/prometheus/tsdb/encoding"
|
|
|
|
"github.com/prometheus/prometheus/tsdb/fileutil"
|
2019-09-19 02:15:41 -07:00
|
|
|
"github.com/prometheus/prometheus/tsdb/record"
|
|
|
|
"github.com/prometheus/prometheus/tsdb/tombstones"
|
2022-10-10 08:08:46 -07:00
|
|
|
"github.com/prometheus/prometheus/tsdb/wlog"
|
2023-03-29 12:34:34 -07:00
|
|
|
"github.com/prometheus/prometheus/util/zeropool"
|
2016-12-22 03:05:24 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
// WALEntryType indicates what data a WAL entry contains.
|
2017-10-06 05:06:39 -07:00
|
|
|
type WALEntryType uint8
|
2016-12-22 03:05:24 -08:00
|
|
|
|
|
|
|
const (
|
2017-02-14 15:54:52 -08:00
|
|
|
// WALMagic is a 4 byte number every WAL segment file starts with.
|
|
|
|
WALMagic = uint32(0x43AF00EF)
|
2017-02-14 00:24:53 -08:00
|
|
|
|
2017-02-14 15:54:52 -08:00
|
|
|
// WALFormatDefault is the version flag for the default outer segment file format.
|
|
|
|
WALFormatDefault = byte(1)
|
|
|
|
)
|
2017-02-14 00:24:53 -08:00
|
|
|
|
2017-02-14 15:54:52 -08:00
|
|
|
// Entry types in a segment file.
|
|
|
|
const (
|
|
|
|
WALEntrySymbols WALEntryType = 1
|
|
|
|
WALEntrySeries WALEntryType = 2
|
|
|
|
WALEntrySamples WALEntryType = 3
|
2017-05-23 03:45:16 -07:00
|
|
|
WALEntryDeletes WALEntryType = 4
|
2016-12-22 03:05:24 -08:00
|
|
|
)
|
|
|
|
|
2017-10-04 12:51:34 -07:00
|
|
|
type walMetrics struct {
|
|
|
|
fsyncDuration prometheus.Summary
|
2017-10-06 04:50:20 -07:00
|
|
|
corruptions prometheus.Counter
|
2017-10-04 12:51:34 -07:00
|
|
|
}
|
|
|
|
|
2021-05-13 02:26:44 -07:00
|
|
|
func newWalMetrics(r prometheus.Registerer) *walMetrics {
|
2017-10-04 12:51:34 -07:00
|
|
|
m := &walMetrics{}
|
|
|
|
|
|
|
|
m.fsyncDuration = prometheus.NewSummary(prometheus.SummaryOpts{
|
2019-06-11 17:12:32 -07:00
|
|
|
Name: "prometheus_tsdb_wal_fsync_duration_seconds",
|
|
|
|
Help: "Duration of WAL fsync.",
|
|
|
|
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
|
2017-10-04 12:51:34 -07:00
|
|
|
})
|
2017-10-06 04:50:20 -07:00
|
|
|
m.corruptions = prometheus.NewCounter(prometheus.CounterOpts{
|
2018-09-18 10:17:41 -07:00
|
|
|
Name: "prometheus_tsdb_wal_corruptions_total",
|
2017-10-06 04:50:20 -07:00
|
|
|
Help: "Total number of WAL corruptions.",
|
|
|
|
})
|
2017-10-04 12:51:34 -07:00
|
|
|
|
|
|
|
if r != nil {
|
|
|
|
r.MustRegister(
|
|
|
|
m.fsyncDuration,
|
2017-10-06 04:50:20 -07:00
|
|
|
m.corruptions,
|
2017-10-04 12:51:34 -07:00
|
|
|
)
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2017-05-13 08:09:26 -07: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.
|
2018-05-27 10:05:11 -07:00
|
|
|
//
|
2022-10-10 08:08:46 -07:00
|
|
|
// DEPRECATED: use wlog pkg combined with the record codex instead.
|
2017-05-13 08:09:26 -07:00
|
|
|
type WAL interface {
|
2017-09-06 05:59:25 -07:00
|
|
|
Reader() WALReader
|
2019-09-19 02:15:41 -07:00
|
|
|
LogSeries([]record.RefSeries) error
|
|
|
|
LogSamples([]record.RefSample) error
|
|
|
|
LogDeletes([]tombstones.Stone) error
|
2017-09-21 02:02:30 -07:00
|
|
|
Truncate(mint int64, keep func(uint64) bool) error
|
2017-05-13 08:09:26 -07:00
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
|
|
|
// WALReader reads entries from a WAL.
|
|
|
|
type WALReader interface {
|
2017-10-06 05:06:39 -07:00
|
|
|
Read(
|
2019-09-19 02:15:41 -07:00
|
|
|
seriesf func([]record.RefSeries),
|
|
|
|
samplesf func([]record.RefSample),
|
|
|
|
deletesf func([]tombstones.Stone),
|
2017-10-06 05:06:39 -07:00
|
|
|
) error
|
2017-05-13 08:09:26 -07:00
|
|
|
}
|
|
|
|
|
2017-09-06 05:59:25 -07:00
|
|
|
// segmentFile wraps a file object of a segment and tracks the highest timestamp
|
|
|
|
// it contains. During WAL truncating, all segments with no higher timestamp than
|
|
|
|
// the truncation threshold can be compacted.
|
2017-08-31 02:39:22 -07:00
|
|
|
type segmentFile struct {
|
2017-09-06 05:59:25 -07:00
|
|
|
*os.File
|
2021-11-06 03:10:04 -07:00
|
|
|
maxTime int64 // highest tombstone or sample timestamp in segment
|
|
|
|
minSeries chunks.HeadSeriesRef // lowerst series ID in segment
|
2017-08-31 02:39:22 -07:00
|
|
|
}
|
|
|
|
|
2017-09-06 05:59:25 -07:00
|
|
|
func newSegmentFile(f *os.File) *segmentFile {
|
|
|
|
return &segmentFile{
|
|
|
|
File: f,
|
|
|
|
maxTime: math.MinInt64,
|
|
|
|
minSeries: math.MaxUint64,
|
|
|
|
}
|
2017-08-31 02:39:22 -07:00
|
|
|
}
|
|
|
|
|
2017-02-13 23:53:19 -08:00
|
|
|
const (
|
2017-03-02 12:53:11 -08:00
|
|
|
walSegmentSizeBytes = 256 * 1024 * 1024 // 256 MB
|
2017-02-13 23:53:19 -08:00
|
|
|
)
|
2017-01-06 00:26:39 -08:00
|
|
|
|
2017-03-20 06:45:27 -07: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-08-26 09:04:00 -07:00
|
|
|
// newCRC32 initializes a CRC32 hash with a preconfigured polynomial, so the
|
|
|
|
// polynomial may be easily changed in one location at a later time, if necessary.
|
|
|
|
func newCRC32() hash.Hash32 {
|
|
|
|
return crc32.New(castagnoliTable)
|
|
|
|
}
|
|
|
|
|
2017-09-06 05:59:25 -07:00
|
|
|
// SegmentWAL is a write ahead log for series data.
|
2018-05-27 10:05:11 -07:00
|
|
|
//
|
2022-10-10 08:08:46 -07:00
|
|
|
// DEPRECATED: use wlog pkg combined with the record coders instead.
|
2017-09-06 05:59:25 -07:00
|
|
|
type SegmentWAL struct {
|
2017-10-06 05:06:39 -07:00
|
|
|
mtx sync.Mutex
|
2017-10-04 12:51:34 -07:00
|
|
|
metrics *walMetrics
|
2017-09-06 05:59:25 -07:00
|
|
|
|
|
|
|
dirFile *os.File
|
|
|
|
files []*segmentFile
|
|
|
|
|
|
|
|
logger log.Logger
|
|
|
|
flushInterval time.Duration
|
|
|
|
segmentSize int64
|
|
|
|
|
|
|
|
crc32 hash.Hash32
|
|
|
|
cur *bufio.Writer
|
|
|
|
curN int64
|
|
|
|
|
|
|
|
stopc chan struct{}
|
|
|
|
donec chan struct{}
|
2017-11-01 10:11:09 -07:00
|
|
|
actorc chan func() error // sequentialized background operations
|
2017-09-06 05:59:25 -07:00
|
|
|
buffers sync.Pool
|
|
|
|
}
|
|
|
|
|
2017-05-13 08:09:26 -07:00
|
|
|
// OpenSegmentWAL opens or creates a write ahead log in the given directory.
|
2016-12-22 06:18:33 -08:00
|
|
|
// The WAL must be read completely before new data is written.
|
2017-10-04 12:51:34 -07:00
|
|
|
func OpenSegmentWAL(dir string, logger log.Logger, flushInterval time.Duration, r prometheus.Registerer) (*SegmentWAL, error) {
|
2021-10-22 01:06:44 -07:00
|
|
|
if err := os.MkdirAll(dir, 0o777); err != nil {
|
2016-12-22 03:05:24 -08:00
|
|
|
return nil, err
|
|
|
|
}
|
2017-02-13 23:53:19 -08:00
|
|
|
df, err := fileutil.OpenDir(dir)
|
2016-12-22 07:14:34 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-03-14 11:30:23 -07:00
|
|
|
if logger == nil {
|
|
|
|
logger = log.NewNopLogger()
|
|
|
|
}
|
2016-12-22 03:05:24 -08:00
|
|
|
|
2017-05-13 08:09:26 -07:00
|
|
|
w := &SegmentWAL{
|
2017-02-13 23:53:19 -08:00
|
|
|
dirFile: df,
|
2017-03-14 11:30:23 -07:00
|
|
|
logger: logger,
|
2017-01-06 06:18:06 -08:00
|
|
|
flushInterval: flushInterval,
|
|
|
|
donec: make(chan struct{}),
|
|
|
|
stopc: make(chan struct{}),
|
2021-12-17 15:35:24 -08:00
|
|
|
actorc: make(chan func() error, 2),
|
2017-02-14 15:54:52 -08:00
|
|
|
segmentSize: walSegmentSizeBytes,
|
2017-08-26 09:04:00 -07:00
|
|
|
crc32: newCRC32(),
|
2016-12-22 03:05:24 -08:00
|
|
|
}
|
2021-05-13 02:26:44 -07:00
|
|
|
w.metrics = newWalMetrics(r)
|
2017-09-06 05:59:25 -07:00
|
|
|
|
|
|
|
fns, err := sequenceFiles(w.dirFile.Name())
|
|
|
|
if err != nil {
|
2017-02-13 23:53:19 -08:00
|
|
|
return nil, err
|
|
|
|
}
|
2017-10-20 03:12:49 -07:00
|
|
|
|
|
|
|
for i, fn := range fns {
|
2017-09-06 05:59:25 -07:00
|
|
|
f, err := w.openSegmentFile(fn)
|
2017-10-20 03:12:49 -07:00
|
|
|
if err == nil {
|
|
|
|
w.files = append(w.files, newSegmentFile(f))
|
|
|
|
continue
|
|
|
|
}
|
2020-04-11 01:22:18 -07:00
|
|
|
level.Warn(logger).Log("msg", "Invalid segment file detected, truncating WAL", "err", err, "file", fn)
|
2017-10-20 03:12:49 -07:00
|
|
|
|
|
|
|
for _, fn := range fns[i:] {
|
|
|
|
if err := os.Remove(fn); err != nil {
|
|
|
|
return w, errors.Wrap(err, "removing segment failed")
|
|
|
|
}
|
2017-09-06 05:59:25 -07:00
|
|
|
}
|
2017-10-20 03:12:49 -07:00
|
|
|
break
|
2017-09-06 05:59:25 -07:00
|
|
|
}
|
2017-02-13 23:53:19 -08:00
|
|
|
|
2017-01-06 06:18:06 -08:00
|
|
|
go w.run(flushInterval)
|
|
|
|
|
2016-12-22 03:05:24 -08:00
|
|
|
return w, nil
|
|
|
|
}
|
|
|
|
|
2017-09-06 05:59:25 -07:00
|
|
|
// repairingWALReader wraps a WAL reader and truncates its underlying SegmentWAL after the last
|
|
|
|
// valid entry if it encounters corruption.
|
|
|
|
type repairingWALReader struct {
|
|
|
|
wal *SegmentWAL
|
|
|
|
r WALReader
|
|
|
|
}
|
|
|
|
|
2017-10-06 05:06:39 -07:00
|
|
|
func (r *repairingWALReader) Read(
|
2019-09-19 02:15:41 -07:00
|
|
|
seriesf func([]record.RefSeries),
|
|
|
|
samplesf func([]record.RefSample),
|
|
|
|
deletesf func([]tombstones.Stone),
|
2017-10-06 05:06:39 -07:00
|
|
|
) error {
|
|
|
|
err := r.r.Read(seriesf, samplesf, deletesf)
|
2017-09-06 05:59:25 -07:00
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2017-10-06 05:06:39 -07:00
|
|
|
cerr, ok := errors.Cause(err).(walCorruptionErr)
|
2017-09-06 05:59:25 -07:00
|
|
|
if !ok {
|
|
|
|
return err
|
|
|
|
}
|
2017-10-06 04:50:20 -07:00
|
|
|
r.wal.metrics.corruptions.Inc()
|
2017-09-06 05:59:25 -07:00
|
|
|
return r.wal.truncate(cerr.err, cerr.file, cerr.lastOffset)
|
|
|
|
}
|
|
|
|
|
|
|
|
// truncate the WAL after the last valid entry.
|
|
|
|
func (w *SegmentWAL) truncate(err error, file int, lastOffset int64) error {
|
2017-09-28 00:19:34 -07:00
|
|
|
level.Error(w.logger).Log("msg", "WAL corruption detected; truncating",
|
2017-09-06 05:59:25 -07:00
|
|
|
"err", err, "file", w.files[file].Name(), "pos", lastOffset)
|
|
|
|
|
|
|
|
// Close and delete all files after the current one.
|
|
|
|
for _, f := range w.files[file+1:] {
|
|
|
|
if err := f.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := os.Remove(f.Name()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
w.mtx.Lock()
|
|
|
|
defer w.mtx.Unlock()
|
|
|
|
|
|
|
|
w.files = w.files[:file+1]
|
|
|
|
|
|
|
|
// Seek the current file to the last valid offset where we continue writing from.
|
2018-03-21 13:39:43 -07:00
|
|
|
_, err = w.files[file].Seek(lastOffset, io.SeekStart)
|
2017-09-06 05:59:25 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-28 06:43:02 -07:00
|
|
|
// Reader returns a new reader over the write ahead log data.
|
2017-02-14 21:54:59 -08:00
|
|
|
// It must be completely consumed before writing to the WAL.
|
2017-09-06 05:59:25 -07:00
|
|
|
func (w *SegmentWAL) Reader() WALReader {
|
|
|
|
return &repairingWALReader{
|
|
|
|
wal: w,
|
|
|
|
r: newWALReader(w.files, w.logger),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-22 09:11:11 -08:00
|
|
|
func (w *SegmentWAL) getBuffer() *encoding.Encbuf {
|
2017-09-06 05:59:25 -07:00
|
|
|
b := w.buffers.Get()
|
|
|
|
if b == nil {
|
2019-02-22 09:11:11 -08:00
|
|
|
return &encoding.Encbuf{B: make([]byte, 0, 64*1024)}
|
2017-09-06 05:59:25 -07:00
|
|
|
}
|
2019-02-22 09:11:11 -08:00
|
|
|
return b.(*encoding.Encbuf)
|
2017-09-06 05:59:25 -07:00
|
|
|
}
|
|
|
|
|
2019-02-22 09:11:11 -08:00
|
|
|
func (w *SegmentWAL) putBuffer(b *encoding.Encbuf) {
|
|
|
|
b.Reset()
|
2017-09-06 05:59:25 -07:00
|
|
|
w.buffers.Put(b)
|
2016-12-22 06:18:33 -08:00
|
|
|
}
|
|
|
|
|
2017-09-21 02:02:30 -07:00
|
|
|
// Truncate deletes the values prior to mint and the series which the keep function
|
2018-12-07 01:24:02 -08:00
|
|
|
// does not indicate to preserve.
|
2021-11-06 03:10:04 -07:00
|
|
|
func (w *SegmentWAL) Truncate(mint int64, keep func(chunks.HeadSeriesRef) bool) error {
|
2017-09-06 05:59:25 -07:00
|
|
|
// The last segment is always active.
|
|
|
|
if len(w.files) < 2 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var candidates []*segmentFile
|
2017-08-31 02:39:22 -07:00
|
|
|
|
|
|
|
// All files have to be traversed as there could be two segments for a block
|
|
|
|
// with first block having times (10000, 20000) and SECOND one having (0, 10000).
|
2017-09-06 05:59:25 -07:00
|
|
|
for _, sf := range w.files[:len(w.files)-1] {
|
|
|
|
if sf.maxTime >= mint {
|
|
|
|
break
|
2017-08-31 02:39:22 -07:00
|
|
|
}
|
2017-09-06 05:59:25 -07:00
|
|
|
// Past WAL files are closed. We have to reopen them for another read.
|
|
|
|
f, err := w.openSegmentFile(sf.Name())
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "open old WAL segment for read")
|
|
|
|
}
|
|
|
|
candidates = append(candidates, &segmentFile{
|
|
|
|
File: f,
|
|
|
|
minSeries: sf.minSeries,
|
|
|
|
maxTime: sf.maxTime,
|
|
|
|
})
|
2017-08-31 02:39:22 -07:00
|
|
|
}
|
2017-09-06 05:59:25 -07:00
|
|
|
if len(candidates) == 0 {
|
2017-08-31 02:39:22 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-06 05:59:25 -07:00
|
|
|
r := newWALReader(candidates, w.logger)
|
2017-08-31 02:39:22 -07:00
|
|
|
|
|
|
|
// Create a new tmp file.
|
2017-09-06 05:59:25 -07:00
|
|
|
f, err := w.createSegmentFile(filepath.Join(w.dirFile.Name(), "compact.tmp"))
|
2017-08-31 02:39:22 -07:00
|
|
|
if err != nil {
|
2017-09-06 05:59:25 -07:00
|
|
|
return errors.Wrap(err, "create compaction segment")
|
2017-08-31 02:39:22 -07:00
|
|
|
}
|
2019-04-26 01:27:36 -07:00
|
|
|
defer func() {
|
|
|
|
if err := os.RemoveAll(f.Name()); err != nil {
|
|
|
|
level.Error(w.logger).Log("msg", "remove tmp file", "err", err.Error())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-09-07 04:04:02 -07:00
|
|
|
var (
|
|
|
|
csf = newSegmentFile(f)
|
|
|
|
crc32 = newCRC32()
|
2019-09-19 02:15:41 -07:00
|
|
|
decSeries = []record.RefSeries{}
|
|
|
|
activeSeries = []record.RefSeries{}
|
2017-09-07 04:04:02 -07:00
|
|
|
)
|
2017-09-06 05:59:25 -07:00
|
|
|
|
|
|
|
for r.next() {
|
|
|
|
rt, flag, byt := r.at()
|
2017-08-31 02:39:22 -07:00
|
|
|
|
|
|
|
if rt != WALEntrySeries {
|
|
|
|
continue
|
|
|
|
}
|
2017-10-06 05:06:39 -07:00
|
|
|
decSeries = decSeries[:0]
|
|
|
|
activeSeries = activeSeries[:0]
|
|
|
|
|
|
|
|
err := r.decodeSeries(flag, byt, &decSeries)
|
2017-08-31 02:39:22 -07:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "decode samples while truncating")
|
|
|
|
}
|
2017-10-06 05:06:39 -07:00
|
|
|
for _, s := range decSeries {
|
2017-09-21 02:02:30 -07:00
|
|
|
if keep(s.Ref) {
|
2017-08-31 02:39:22 -07:00
|
|
|
activeSeries = append(activeSeries, s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-06 05:59:25 -07:00
|
|
|
buf := w.getBuffer()
|
|
|
|
flag = w.encodeSeries(buf, activeSeries)
|
|
|
|
|
2019-02-22 09:11:11 -08:00
|
|
|
_, err = w.writeTo(csf, crc32, WALEntrySeries, flag, buf.Get())
|
2017-09-06 05:59:25 -07:00
|
|
|
w.putBuffer(buf)
|
|
|
|
|
2017-08-31 02:39:22 -07:00
|
|
|
if err != nil {
|
2017-10-31 07:37:41 -07:00
|
|
|
return errors.Wrap(err, "write to compaction segment")
|
2017-08-31 02:39:22 -07:00
|
|
|
}
|
|
|
|
}
|
2017-09-06 05:59:25 -07:00
|
|
|
if r.Err() != nil {
|
|
|
|
return errors.Wrap(r.Err(), "read candidate WAL files")
|
|
|
|
}
|
2017-08-31 02:39:22 -07:00
|
|
|
|
2018-03-21 13:39:43 -07:00
|
|
|
off, err := csf.Seek(0, io.SeekCurrent)
|
2017-09-06 07:20:37 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := csf.Truncate(off); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-08-13 01:34:14 -07:00
|
|
|
if err := csf.Sync(); err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err := csf.Close(); err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2017-09-07 04:04:02 -07:00
|
|
|
|
2019-08-13 01:34:14 -07:00
|
|
|
_ = candidates[0].Close() // need close before remove on platform windows
|
2019-04-26 01:27:36 -07:00
|
|
|
if err := fileutil.Replace(csf.Name(), candidates[0].Name()); err != nil {
|
2017-10-31 07:37:41 -07:00
|
|
|
return errors.Wrap(err, "rename compaction segment")
|
2017-09-06 05:59:25 -07:00
|
|
|
}
|
|
|
|
for _, f := range candidates[1:] {
|
2017-10-31 07:37:41 -07:00
|
|
|
f.Close() // need close before remove on platform windows
|
2017-09-06 05:59:25 -07:00
|
|
|
if err := os.RemoveAll(f.Name()); err != nil {
|
2017-08-31 02:39:22 -07:00
|
|
|
return errors.Wrap(err, "delete WAL segment file")
|
|
|
|
}
|
|
|
|
}
|
2017-09-06 07:20:37 -07:00
|
|
|
if err := w.dirFile.Sync(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-09-07 01:58:34 -07:00
|
|
|
// The file object of csf still holds the name before rename. Recreate it so
|
2018-04-08 02:28:30 -07:00
|
|
|
// subsequent truncations do not look at a non-existent file name.
|
2017-09-07 01:58:34 -07:00
|
|
|
csf.File, err = w.openSegmentFile(candidates[0].Name())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// We don't need it to be open.
|
2019-08-13 01:34:14 -07:00
|
|
|
if err := csf.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-09-07 01:58:34 -07:00
|
|
|
|
2017-09-06 07:20:37 -07:00
|
|
|
w.mtx.Lock()
|
|
|
|
w.files = append([]*segmentFile{csf}, w.files[len(candidates):]...)
|
|
|
|
w.mtx.Unlock()
|
|
|
|
|
2017-08-31 02:39:22 -07:00
|
|
|
return nil
|
|
|
|
}
|
2017-05-23 03:45:16 -07:00
|
|
|
|
|
|
|
// LogSeries writes a batch of new series labels to the log.
|
2017-08-31 02:39:22 -07:00
|
|
|
// The series have to be ordered.
|
2019-09-19 02:15:41 -07:00
|
|
|
func (w *SegmentWAL) LogSeries(series []record.RefSeries) error {
|
2017-09-06 05:59:25 -07:00
|
|
|
buf := w.getBuffer()
|
|
|
|
|
|
|
|
flag := w.encodeSeries(buf, series)
|
2017-09-07 23:48:19 -07:00
|
|
|
|
|
|
|
w.mtx.Lock()
|
|
|
|
defer w.mtx.Unlock()
|
|
|
|
|
2019-02-22 09:11:11 -08:00
|
|
|
err := w.write(WALEntrySeries, flag, buf.Get())
|
2017-09-06 05:59:25 -07:00
|
|
|
|
|
|
|
w.putBuffer(buf)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "log series")
|
|
|
|
}
|
|
|
|
|
|
|
|
tf := w.head()
|
|
|
|
|
|
|
|
for _, s := range series {
|
|
|
|
if tf.minSeries > s.Ref {
|
|
|
|
tf.minSeries = s.Ref
|
|
|
|
}
|
2016-12-22 03:05:24 -08:00
|
|
|
}
|
2017-05-23 03:45:16 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LogSamples writes a batch of new samples to the log.
|
2019-09-19 02:15:41 -07:00
|
|
|
func (w *SegmentWAL) LogSamples(samples []record.RefSample) error {
|
2017-09-06 05:59:25 -07:00
|
|
|
buf := w.getBuffer()
|
|
|
|
|
|
|
|
flag := w.encodeSamples(buf, samples)
|
2017-09-07 23:48:19 -07:00
|
|
|
|
|
|
|
w.mtx.Lock()
|
|
|
|
defer w.mtx.Unlock()
|
|
|
|
|
2019-02-22 09:11:11 -08:00
|
|
|
err := w.write(WALEntrySamples, flag, buf.Get())
|
2017-09-06 05:59:25 -07:00
|
|
|
|
|
|
|
w.putBuffer(buf)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "log series")
|
|
|
|
}
|
|
|
|
tf := w.head()
|
|
|
|
|
|
|
|
for _, s := range samples {
|
|
|
|
if tf.maxTime < s.T {
|
|
|
|
tf.maxTime = s.T
|
|
|
|
}
|
2016-12-22 03:05:24 -08:00
|
|
|
}
|
2017-05-23 03:45:16 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LogDeletes write a batch of new deletes to the log.
|
2019-09-19 02:15:41 -07:00
|
|
|
func (w *SegmentWAL) LogDeletes(stones []tombstones.Stone) error {
|
2017-09-06 05:59:25 -07:00
|
|
|
buf := w.getBuffer()
|
|
|
|
|
|
|
|
flag := w.encodeDeletes(buf, stones)
|
2017-09-07 23:48:19 -07:00
|
|
|
|
|
|
|
w.mtx.Lock()
|
|
|
|
defer w.mtx.Unlock()
|
|
|
|
|
2019-02-22 09:11:11 -08:00
|
|
|
err := w.write(WALEntryDeletes, flag, buf.Get())
|
2017-09-06 05:59:25 -07:00
|
|
|
|
|
|
|
w.putBuffer(buf)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "log series")
|
|
|
|
}
|
|
|
|
tf := w.head()
|
|
|
|
|
|
|
|
for _, s := range stones {
|
2019-09-19 02:15:41 -07:00
|
|
|
for _, iv := range s.Intervals {
|
2017-09-06 05:59:25 -07:00
|
|
|
if tf.maxTime < iv.Maxt {
|
|
|
|
tf.maxTime = iv.Maxt
|
|
|
|
}
|
|
|
|
}
|
2017-05-23 03:45:16 -07:00
|
|
|
}
|
2016-12-22 03:05:24 -08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-06 05:59:25 -07:00
|
|
|
// openSegmentFile opens the given segment file and consumes and validates header.
|
|
|
|
func (w *SegmentWAL) openSegmentFile(name string) (*os.File, error) {
|
2017-04-28 06:41:42 -07:00
|
|
|
// We must open all files in read/write mode as we may have to truncate along
|
2017-09-06 05:59:25 -07:00
|
|
|
// the way and any file may become the head.
|
2021-10-22 01:06:44 -07:00
|
|
|
f, err := os.OpenFile(name, os.O_RDWR, 0o666)
|
2017-09-06 05:59:25 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-02-13 23:53:19 -08:00
|
|
|
}
|
2017-09-06 05:59:25 -07:00
|
|
|
metab := make([]byte, 8)
|
2017-02-13 23:53:19 -08:00
|
|
|
|
2017-10-31 07:37:41 -07:00
|
|
|
// If there is an error, we need close f for platform windows before gc.
|
|
|
|
// Otherwise, file op may fail.
|
|
|
|
hasError := true
|
|
|
|
defer func() {
|
|
|
|
if hasError {
|
|
|
|
f.Close()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-09-06 05:59:25 -07:00
|
|
|
if n, err := f.Read(metab); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "validate meta %q", f.Name())
|
|
|
|
} else if n != 8 {
|
|
|
|
return nil, errors.Errorf("invalid header size %d in %q", n, f.Name())
|
|
|
|
}
|
2017-02-14 00:24:53 -08:00
|
|
|
|
2017-09-06 05:59:25 -07:00
|
|
|
if m := binary.BigEndian.Uint32(metab[:4]); m != WALMagic {
|
|
|
|
return nil, errors.Errorf("invalid magic header %x in %q", m, f.Name())
|
|
|
|
}
|
|
|
|
if metab[4] != WALFormatDefault {
|
|
|
|
return nil, errors.Errorf("unknown WAL segment format %d in %q", metab[4], f.Name())
|
|
|
|
}
|
2017-10-31 07:37:41 -07:00
|
|
|
hasError = false
|
2017-09-06 05:59:25 -07:00
|
|
|
return f, nil
|
|
|
|
}
|
2017-02-14 00:24:53 -08:00
|
|
|
|
2017-09-06 05:59:25 -07:00
|
|
|
// createSegmentFile creates a new segment file with the given name. It preallocates
|
|
|
|
// the standard segment size if possible and writes the header.
|
|
|
|
func (w *SegmentWAL) createSegmentFile(name string) (*os.File, error) {
|
|
|
|
f, err := os.Create(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-02-14 00:24:53 -08:00
|
|
|
}
|
2017-09-06 05:59:25 -07:00
|
|
|
if err = fileutil.Preallocate(f, w.segmentSize, true); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Write header metadata for new file.
|
|
|
|
metab := make([]byte, 8)
|
|
|
|
binary.BigEndian.PutUint32(metab[:4], WALMagic)
|
|
|
|
metab[4] = WALFormatDefault
|
2017-02-14 00:24:53 -08:00
|
|
|
|
2017-09-06 05:59:25 -07:00
|
|
|
if _, err := f.Write(metab); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return f, err
|
2017-02-13 23:53:19 -08:00
|
|
|
}
|
|
|
|
|
2017-04-28 06:41:42 -07:00
|
|
|
// cut finishes the currently active segments and opens the next one.
|
2017-02-13 23:53:19 -08:00
|
|
|
// The encoder is reset to point to the new segment.
|
2017-05-13 08:09:26 -07:00
|
|
|
func (w *SegmentWAL) cut() error {
|
2017-09-06 05:59:25 -07:00
|
|
|
// Sync current head to disk and close.
|
|
|
|
if hf := w.head(); hf != nil {
|
2017-09-08 01:12:28 -07:00
|
|
|
if err := w.flush(); err != nil {
|
2017-02-13 23:53:19 -08:00
|
|
|
return err
|
|
|
|
}
|
2017-09-08 01:12:28 -07:00
|
|
|
// Finish last segment asynchronously to not block the WAL moving along
|
|
|
|
// in the new segment.
|
|
|
|
go func() {
|
2017-10-27 04:47:07 -07:00
|
|
|
w.actorc <- func() error {
|
2018-03-21 13:39:43 -07:00
|
|
|
off, err := hf.Seek(0, io.SeekCurrent)
|
2017-10-27 04:47:07 -07:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "finish old segment %s", hf.Name())
|
|
|
|
}
|
|
|
|
if err := hf.Truncate(off); err != nil {
|
|
|
|
return errors.Wrapf(err, "finish old segment %s", hf.Name())
|
|
|
|
}
|
|
|
|
if err := hf.Sync(); err != nil {
|
|
|
|
return errors.Wrapf(err, "finish old segment %s", hf.Name())
|
|
|
|
}
|
|
|
|
if err := hf.Close(); err != nil {
|
|
|
|
return errors.Wrapf(err, "finish old segment %s", hf.Name())
|
|
|
|
}
|
|
|
|
return nil
|
2017-09-08 01:12:28 -07:00
|
|
|
}
|
|
|
|
}()
|
2017-02-13 23:53:19 -08:00
|
|
|
}
|
|
|
|
|
2017-08-30 09:34:54 -07:00
|
|
|
p, _, err := nextSequenceFile(w.dirFile.Name())
|
2017-02-13 23:53:19 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-09-06 05:59:25 -07:00
|
|
|
f, err := w.createSegmentFile(p)
|
2017-02-13 23:53:19 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-02-14 00:24:53 -08:00
|
|
|
|
2017-09-08 01:12:28 -07:00
|
|
|
go func() {
|
2017-10-27 04:47:07 -07:00
|
|
|
w.actorc <- func() error {
|
|
|
|
return errors.Wrap(w.dirFile.Sync(), "sync WAL directory")
|
2017-09-08 01:12:28 -07:00
|
|
|
}
|
|
|
|
}()
|
2017-02-14 00:24:53 -08:00
|
|
|
|
2017-09-06 05:59:25 -07:00
|
|
|
w.files = append(w.files, newSegmentFile(f))
|
2017-08-31 02:39:22 -07:00
|
|
|
|
|
|
|
// TODO(gouthamve): make the buffer size a constant.
|
2017-09-06 05:59:25 -07:00
|
|
|
w.cur = bufio.NewWriterSize(f, 8*1024*1024)
|
2017-02-14 15:54:52 -08:00
|
|
|
w.curN = 8
|
2017-02-13 23:53:19 -08:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-06 05:59:25 -07:00
|
|
|
func (w *SegmentWAL) head() *segmentFile {
|
2017-02-13 23:53:19 -08:00
|
|
|
if len(w.files) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2017-09-06 05:59:25 -07:00
|
|
|
return w.files[len(w.files)-1]
|
2017-02-13 23:53:19 -08:00
|
|
|
}
|
|
|
|
|
2017-03-19 09:05:01 -07:00
|
|
|
// Sync flushes the changes to disk.
|
2017-05-13 08:09:26 -07:00
|
|
|
func (w *SegmentWAL) Sync() error {
|
2017-09-06 05:59:25 -07:00
|
|
|
var head *segmentFile
|
2017-09-01 00:45:54 -07:00
|
|
|
var err error
|
|
|
|
|
2017-09-06 05:59:25 -07:00
|
|
|
// Flush the writer and retrieve the reference to the head segment under mutex lock.
|
2017-09-01 00:45:54 -07:00
|
|
|
func() {
|
|
|
|
w.mtx.Lock()
|
|
|
|
defer w.mtx.Unlock()
|
|
|
|
if err = w.flush(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2017-09-06 05:59:25 -07:00
|
|
|
head = w.head()
|
2017-09-01 05:38:49 -07:00
|
|
|
}()
|
2017-09-01 00:45:54 -07:00
|
|
|
if err != nil {
|
2017-09-06 05:59:25 -07:00
|
|
|
return errors.Wrap(err, "flush buffer")
|
2017-09-01 00:45:54 -07:00
|
|
|
}
|
2017-09-06 05:59:25 -07:00
|
|
|
if head != nil {
|
|
|
|
// But only fsync the head segment after releasing the mutex as it will block on disk I/O.
|
2017-10-04 12:51:34 -07:00
|
|
|
start := time.Now()
|
|
|
|
err := fileutil.Fdatasync(head.File)
|
|
|
|
w.metrics.fsyncDuration.Observe(time.Since(start).Seconds())
|
|
|
|
return err
|
2017-09-06 05:59:25 -07:00
|
|
|
}
|
|
|
|
return nil
|
2017-02-13 23:53:19 -08:00
|
|
|
}
|
|
|
|
|
2017-05-13 08:09:26 -07:00
|
|
|
func (w *SegmentWAL) sync() error {
|
2017-09-01 00:45:54 -07:00
|
|
|
if err := w.flush(); err != nil {
|
2016-12-22 07:14:34 -08:00
|
|
|
return err
|
|
|
|
}
|
2017-09-07 23:17:01 -07:00
|
|
|
if w.head() == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2017-10-04 12:51:34 -07:00
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
err := fileutil.Fdatasync(w.head().File)
|
|
|
|
w.metrics.fsyncDuration.Observe(time.Since(start).Seconds())
|
|
|
|
return err
|
2016-12-22 03:05:24 -08:00
|
|
|
}
|
|
|
|
|
2017-09-01 00:45:54 -07:00
|
|
|
func (w *SegmentWAL) flush() error {
|
|
|
|
if w.cur == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return w.cur.Flush()
|
|
|
|
}
|
|
|
|
|
2017-05-13 08:09:26 -07:00
|
|
|
func (w *SegmentWAL) run(interval time.Duration) {
|
2017-01-06 06:18:06 -08:00
|
|
|
var tick <-chan time.Time
|
|
|
|
|
|
|
|
if interval > 0 {
|
|
|
|
ticker := time.NewTicker(interval)
|
|
|
|
defer ticker.Stop()
|
|
|
|
tick = ticker.C
|
|
|
|
}
|
|
|
|
defer close(w.donec)
|
|
|
|
|
|
|
|
for {
|
2017-10-27 04:47:07 -07:00
|
|
|
// Processing all enqueued operations has precedence over shutdown and
|
|
|
|
// background syncs.
|
|
|
|
select {
|
|
|
|
case f := <-w.actorc:
|
|
|
|
if err := f(); err != nil {
|
|
|
|
level.Error(w.logger).Log("msg", "operation failed", "err", err)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
default:
|
|
|
|
}
|
2017-01-06 06:18:06 -08:00
|
|
|
select {
|
|
|
|
case <-w.stopc:
|
|
|
|
return
|
2017-10-27 04:47:07 -07:00
|
|
|
case f := <-w.actorc:
|
|
|
|
if err := f(); err != nil {
|
|
|
|
level.Error(w.logger).Log("msg", "operation failed", "err", err)
|
|
|
|
}
|
2017-01-06 06:18:06 -08:00
|
|
|
case <-tick:
|
2017-02-13 23:53:19 -08:00
|
|
|
if err := w.Sync(); err != nil {
|
2017-09-28 00:19:34 -07:00
|
|
|
level.Error(w.logger).Log("msg", "sync failed", "err", err)
|
2017-01-06 06:18:06 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-28 06:41:42 -07:00
|
|
|
// Close syncs all data and closes the underlying resources.
|
2017-05-13 08:09:26 -07:00
|
|
|
func (w *SegmentWAL) Close() error {
|
2018-09-20 22:31:22 -07:00
|
|
|
// Make sure you can call Close() multiple times.
|
|
|
|
select {
|
|
|
|
case <-w.stopc:
|
|
|
|
return nil // Already closed.
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2017-01-06 06:18:06 -08:00
|
|
|
close(w.stopc)
|
|
|
|
<-w.donec
|
|
|
|
|
2017-02-13 23:53:19 -08:00
|
|
|
w.mtx.Lock()
|
2017-08-28 15:39:17 -07:00
|
|
|
defer w.mtx.Unlock()
|
2017-02-13 23:53:19 -08:00
|
|
|
|
2016-12-22 03:05:24 -08:00
|
|
|
if err := w.sync(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-02-14 15:54:52 -08:00
|
|
|
// On opening, a WAL must be fully consumed once. Afterwards
|
|
|
|
// only the current segment will still be open.
|
2017-09-06 05:59:25 -07:00
|
|
|
if hf := w.head(); hf != nil {
|
2018-09-20 22:31:22 -07:00
|
|
|
if err := hf.Close(); err != nil {
|
|
|
|
return errors.Wrapf(err, "closing WAL head %s", hf.Name())
|
|
|
|
}
|
2017-02-14 21:54:59 -08:00
|
|
|
}
|
2017-10-31 07:37:41 -07:00
|
|
|
|
2018-09-20 22:31:22 -07:00
|
|
|
return errors.Wrapf(w.dirFile.Close(), "closing WAL dir %s", w.dirFile.Name())
|
2016-12-22 03:05:24 -08:00
|
|
|
}
|
|
|
|
|
2017-09-06 05:59:25 -07:00
|
|
|
func (w *SegmentWAL) write(t WALEntryType, flag uint8, buf []byte) error {
|
2017-04-28 06:41:42 -07:00
|
|
|
// Cut to the next segment if the entry exceeds the file size unless it would also
|
2017-02-14 21:54:59 -08:00
|
|
|
// exceed the size of a new segment.
|
2017-08-31 02:39:22 -07:00
|
|
|
// TODO(gouthamve): Add a test for this case where the commit is greater than segmentSize.
|
2017-02-14 21:54:59 -08:00
|
|
|
var (
|
2017-09-06 05:59:25 -07:00
|
|
|
sz = int64(len(buf)) + 6
|
2017-02-14 21:54:59 -08:00
|
|
|
newsz = w.curN + sz
|
|
|
|
)
|
2017-03-08 11:52:03 -08: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-14 21:54:59 -08:00
|
|
|
if w.cur == nil || w.curN > w.segmentSize || newsz > w.segmentSize && sz <= w.segmentSize {
|
2017-02-13 23:53:19 -08:00
|
|
|
if err := w.cut(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2017-09-07 04:04:02 -07:00
|
|
|
n, err := w.writeTo(w.cur, w.crc32, t, flag, buf)
|
2017-09-06 05:59:25 -07:00
|
|
|
|
|
|
|
w.curN += int64(n)
|
2017-01-06 09:36:42 -08:00
|
|
|
|
2017-09-06 05:59:25 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-09-07 04:04:02 -07:00
|
|
|
func (w *SegmentWAL) writeTo(wr io.Writer, crc32 hash.Hash, t WALEntryType, flag uint8, buf []byte) (int, error) {
|
2017-09-06 05:59:25 -07:00
|
|
|
if len(buf) == 0 {
|
|
|
|
return 0, nil
|
|
|
|
}
|
2017-09-07 04:04:02 -07:00
|
|
|
crc32.Reset()
|
|
|
|
wr = io.MultiWriter(crc32, wr)
|
2016-12-22 03:05:24 -08:00
|
|
|
|
2017-09-06 05:59:25 -07:00
|
|
|
var b [6]byte
|
|
|
|
b[0] = byte(t)
|
2016-12-22 03:05:24 -08:00
|
|
|
b[1] = flag
|
|
|
|
|
2017-01-06 09:36:42 -08:00
|
|
|
binary.BigEndian.PutUint32(b[2:], uint32(len(buf)))
|
2016-12-22 03:05:24 -08:00
|
|
|
|
2017-09-06 05:59:25 -07:00
|
|
|
n1, err := wr.Write(b[:])
|
|
|
|
if err != nil {
|
|
|
|
return n1, err
|
2016-12-22 03:05:24 -08:00
|
|
|
}
|
2017-09-06 05:59:25 -07:00
|
|
|
n2, err := wr.Write(buf)
|
|
|
|
if err != nil {
|
|
|
|
return n1 + n2, err
|
2016-12-22 03:05:24 -08:00
|
|
|
}
|
2017-09-07 04:04:02 -07:00
|
|
|
n3, err := wr.Write(crc32.Sum(b[:0]))
|
2016-12-22 03:05:24 -08:00
|
|
|
|
2017-09-06 05:59:25 -07:00
|
|
|
return n1 + n2 + n3, err
|
2016-12-22 03:05:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
walSeriesSimple = 1
|
|
|
|
walSamplesSimple = 1
|
2017-05-23 03:45:16 -07:00
|
|
|
walDeletesSimple = 1
|
2016-12-22 03:05:24 -08:00
|
|
|
)
|
|
|
|
|
2019-09-19 02:15:41 -07:00
|
|
|
func (w *SegmentWAL) encodeSeries(buf *encoding.Encbuf, series []record.RefSeries) uint8 {
|
2017-08-31 02:39:22 -07:00
|
|
|
for _, s := range series {
|
2021-11-06 03:10:04 -07:00
|
|
|
buf.PutBE64(uint64(s.Ref))
|
2022-07-26 07:42:00 -07:00
|
|
|
record.EncodeLabels(buf, s.Labels)
|
2016-12-22 03:05:24 -08:00
|
|
|
}
|
2017-09-06 05:59:25 -07:00
|
|
|
return walSeriesSimple
|
2016-12-22 03:05:24 -08:00
|
|
|
}
|
|
|
|
|
2019-09-19 02:15:41 -07:00
|
|
|
func (w *SegmentWAL) encodeSamples(buf *encoding.Encbuf, samples []record.RefSample) uint8 {
|
2016-12-22 03:05:24 -08:00
|
|
|
if len(samples) == 0 {
|
2017-09-06 05:59:25 -07:00
|
|
|
return walSamplesSimple
|
2016-12-22 03:05:24 -08: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]
|
|
|
|
|
2021-11-06 03:10:04 -07:00
|
|
|
buf.PutBE64(uint64(first.Ref))
|
2019-02-22 09:11:11 -08:00
|
|
|
buf.PutBE64int64(first.T)
|
2016-12-22 03:05:24 -08:00
|
|
|
|
|
|
|
for _, s := range samples {
|
2019-02-22 09:11:11 -08:00
|
|
|
buf.PutVarint64(int64(s.Ref) - int64(first.Ref))
|
|
|
|
buf.PutVarint64(s.T - first.T)
|
|
|
|
buf.PutBE64(math.Float64bits(s.V))
|
2016-12-22 03:05:24 -08:00
|
|
|
}
|
2017-09-06 05:59:25 -07:00
|
|
|
return walSamplesSimple
|
2016-12-22 03:05:24 -08:00
|
|
|
}
|
|
|
|
|
2019-09-19 02:15:41 -07:00
|
|
|
func (w *SegmentWAL) encodeDeletes(buf *encoding.Encbuf, stones []tombstones.Stone) uint8 {
|
2017-05-26 08:56:31 -07:00
|
|
|
for _, s := range stones {
|
2019-09-19 02:15:41 -07:00
|
|
|
for _, iv := range s.Intervals {
|
2021-11-06 03:10:04 -07:00
|
|
|
buf.PutBE64(uint64(s.Ref))
|
2019-02-22 09:11:11 -08:00
|
|
|
buf.PutVarint64(iv.Mint)
|
|
|
|
buf.PutVarint64(iv.Maxt)
|
2017-05-23 03:45:16 -07:00
|
|
|
}
|
|
|
|
}
|
2017-09-06 05:59:25 -07:00
|
|
|
return walDeletesSimple
|
2017-05-23 03:45:16 -07:00
|
|
|
}
|
|
|
|
|
2017-05-12 08:06:26 -07:00
|
|
|
// walReader decodes and emits write ahead log entries.
|
|
|
|
type walReader struct {
|
2017-03-14 11:30:23 -07:00
|
|
|
logger log.Logger
|
|
|
|
|
2017-09-06 05:59:25 -07:00
|
|
|
files []*segmentFile
|
2017-02-15 15:24:53 -08:00
|
|
|
cur int
|
|
|
|
buf []byte
|
|
|
|
crc32 hash.Hash32
|
2022-07-26 07:42:00 -07:00
|
|
|
dec record.Decoder
|
2017-02-14 21:54:59 -08:00
|
|
|
|
2017-09-06 05:59:25 -07:00
|
|
|
curType WALEntryType
|
|
|
|
curFlag byte
|
|
|
|
curBuf []byte
|
|
|
|
lastOffset int64 // offset after last successfully read entry
|
2017-05-23 03:45:16 -07:00
|
|
|
|
|
|
|
err error
|
2016-12-22 06:18:33 -08:00
|
|
|
}
|
|
|
|
|
2017-09-06 05:59:25 -07:00
|
|
|
func newWALReader(files []*segmentFile, l log.Logger) *walReader {
|
2017-05-12 08:06:26 -07:00
|
|
|
if l == nil {
|
|
|
|
l = log.NewNopLogger()
|
2017-03-14 11:30:23 -07:00
|
|
|
}
|
2017-05-12 08:06:26 -07:00
|
|
|
return &walReader{
|
|
|
|
logger: l,
|
2017-09-06 05:59:25 -07:00
|
|
|
files: files,
|
2017-03-14 11:30:23 -07:00
|
|
|
buf: make([]byte, 0, 128*4096),
|
2017-08-26 09:04:00 -07:00
|
|
|
crc32: newCRC32(),
|
2016-12-22 06:18:33 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-14 21:54:59 -08:00
|
|
|
// Err returns the last error the reader encountered.
|
2017-05-12 08:06:26 -07:00
|
|
|
func (r *walReader) Err() error {
|
2017-02-14 21:54:59 -08:00
|
|
|
return r.err
|
|
|
|
}
|
|
|
|
|
2017-10-06 05:06:39 -07:00
|
|
|
func (r *walReader) Read(
|
2019-09-19 02:15:41 -07:00
|
|
|
seriesf func([]record.RefSeries),
|
|
|
|
samplesf func([]record.RefSample),
|
|
|
|
deletesf func([]tombstones.Stone),
|
2017-10-06 05:06:39 -07:00
|
|
|
) error {
|
|
|
|
// Concurrency for replaying the WAL is very limited. We at least split out decoding and
|
|
|
|
// processing into separate threads.
|
|
|
|
// Historically, the processing is the bottleneck with reading and decoding using only
|
|
|
|
// 15% of the CPU.
|
|
|
|
var (
|
2023-03-29 12:34:34 -07:00
|
|
|
seriesPool zeropool.Pool[[]record.RefSeries]
|
|
|
|
samplePool zeropool.Pool[[]record.RefSample]
|
|
|
|
deletePool zeropool.Pool[[]tombstones.Stone]
|
2017-10-06 05:06:39 -07:00
|
|
|
)
|
|
|
|
donec := make(chan struct{})
|
2017-10-07 06:55:11 -07:00
|
|
|
datac := make(chan interface{}, 100)
|
2017-10-06 05:06:39 -07:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer close(donec)
|
|
|
|
|
|
|
|
for x := range datac {
|
|
|
|
switch v := x.(type) {
|
2019-09-19 02:15:41 -07:00
|
|
|
case []record.RefSeries:
|
2017-10-06 05:06:39 -07:00
|
|
|
if seriesf != nil {
|
|
|
|
seriesf(v)
|
|
|
|
}
|
|
|
|
seriesPool.Put(v[:0])
|
2019-09-19 02:15:41 -07:00
|
|
|
case []record.RefSample:
|
2017-10-06 05:06:39 -07:00
|
|
|
if samplesf != nil {
|
|
|
|
samplesf(v)
|
|
|
|
}
|
|
|
|
samplePool.Put(v[:0])
|
2019-09-19 02:15:41 -07:00
|
|
|
case []tombstones.Stone:
|
2017-10-06 05:06:39 -07:00
|
|
|
if deletesf != nil {
|
|
|
|
deletesf(v)
|
|
|
|
}
|
|
|
|
deletePool.Put(v[:0])
|
|
|
|
default:
|
|
|
|
level.Error(r.logger).Log("msg", "unexpected data type")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
var err error
|
2017-09-06 05:59:25 -07:00
|
|
|
|
2017-05-23 03:45:16 -07:00
|
|
|
for r.next() {
|
2017-05-26 08:56:31 -07:00
|
|
|
et, flag, b := r.at()
|
2017-10-06 05:06:39 -07:00
|
|
|
|
2017-05-26 08:56:31 -07:00
|
|
|
// In decoding below we never return a walCorruptionErr for now.
|
2019-09-30 08:54:55 -07:00
|
|
|
// Those should generally be caught by entry decoding before.
|
2017-05-26 08:56:31 -07:00
|
|
|
switch et {
|
|
|
|
case WALEntrySeries:
|
2023-03-29 12:34:34 -07:00
|
|
|
series := seriesPool.Get()
|
|
|
|
if series == nil {
|
2019-09-19 02:15:41 -07:00
|
|
|
series = make([]record.RefSeries, 0, 512)
|
2017-05-26 08:56:31 -07:00
|
|
|
}
|
2017-10-06 05:06:39 -07:00
|
|
|
|
2018-05-07 06:02:49 -07:00
|
|
|
err = r.decodeSeries(flag, b, &series)
|
2017-10-06 05:06:39 -07:00
|
|
|
if err != nil {
|
|
|
|
err = errors.Wrap(err, "decode series entry")
|
|
|
|
break
|
2017-09-19 01:20:19 -07:00
|
|
|
}
|
2017-10-06 05:06:39 -07:00
|
|
|
datac <- series
|
2017-09-06 05:59:25 -07:00
|
|
|
|
|
|
|
cf := r.current()
|
|
|
|
for _, s := range series {
|
|
|
|
if cf.minSeries > s.Ref {
|
|
|
|
cf.minSeries = s.Ref
|
|
|
|
}
|
|
|
|
}
|
2017-05-26 08:56:31 -07:00
|
|
|
case WALEntrySamples:
|
2023-03-29 12:34:34 -07:00
|
|
|
samples := samplePool.Get()
|
|
|
|
if samples == nil {
|
2019-09-19 02:15:41 -07:00
|
|
|
samples = make([]record.RefSample, 0, 512)
|
2017-05-26 08:56:31 -07:00
|
|
|
}
|
2017-10-06 05:06:39 -07:00
|
|
|
|
2018-05-07 06:02:49 -07:00
|
|
|
err = r.decodeSamples(flag, b, &samples)
|
2017-10-06 05:06:39 -07:00
|
|
|
if err != nil {
|
|
|
|
err = errors.Wrap(err, "decode samples entry")
|
|
|
|
break
|
2017-09-19 01:20:19 -07:00
|
|
|
}
|
2017-10-06 05:06:39 -07:00
|
|
|
datac <- samples
|
2017-08-31 02:39:22 -07:00
|
|
|
|
2017-09-06 05:59:25 -07:00
|
|
|
// Update the times for the WAL segment file.
|
|
|
|
cf := r.current()
|
|
|
|
for _, s := range samples {
|
|
|
|
if cf.maxTime < s.T {
|
|
|
|
cf.maxTime = s.T
|
2017-08-31 02:39:22 -07:00
|
|
|
}
|
|
|
|
}
|
2017-05-26 08:56:31 -07:00
|
|
|
case WALEntryDeletes:
|
2023-03-29 12:34:34 -07:00
|
|
|
deletes := deletePool.Get()
|
|
|
|
if deletes == nil {
|
2019-09-19 02:15:41 -07:00
|
|
|
deletes = make([]tombstones.Stone, 0, 512)
|
2017-09-06 05:59:25 -07:00
|
|
|
}
|
2017-10-06 05:06:39 -07:00
|
|
|
|
2018-05-07 06:02:49 -07:00
|
|
|
err = r.decodeDeletes(flag, b, &deletes)
|
2017-10-06 05:06:39 -07:00
|
|
|
if err != nil {
|
|
|
|
err = errors.Wrap(err, "decode delete entry")
|
|
|
|
break
|
2017-09-19 01:20:19 -07:00
|
|
|
}
|
2017-10-06 05:06:39 -07:00
|
|
|
datac <- deletes
|
2017-09-06 05:59:25 -07:00
|
|
|
|
2017-10-06 05:06:39 -07:00
|
|
|
// Update the times for the WAL segment file.
|
2017-09-06 05:59:25 -07:00
|
|
|
cf := r.current()
|
2017-10-06 05:06:39 -07:00
|
|
|
for _, s := range deletes {
|
2019-09-19 02:15:41 -07:00
|
|
|
for _, iv := range s.Intervals {
|
2017-09-06 05:59:25 -07:00
|
|
|
if cf.maxTime < iv.Maxt {
|
|
|
|
cf.maxTime = iv.Maxt
|
|
|
|
}
|
|
|
|
}
|
2017-05-26 08:56:31 -07:00
|
|
|
}
|
|
|
|
}
|
2017-05-23 03:45:16 -07:00
|
|
|
}
|
2017-10-06 05:06:39 -07:00
|
|
|
close(datac)
|
|
|
|
<-donec
|
2017-05-23 03:45:16 -07:00
|
|
|
|
2017-10-06 05:06:39 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-02-14 21:54:59 -08:00
|
|
|
}
|
2017-10-06 05:06:39 -07:00
|
|
|
if r.Err() != nil {
|
|
|
|
return errors.Wrap(r.Err(), "read entry")
|
2017-02-14 21:54:59 -08:00
|
|
|
}
|
2017-10-06 05:06:39 -07:00
|
|
|
return nil
|
2017-02-14 21:54:59 -08:00
|
|
|
}
|
|
|
|
|
2017-05-26 08:56:31 -07:00
|
|
|
func (r *walReader) at() (WALEntryType, byte, []byte) {
|
|
|
|
return r.curType, r.curFlag, r.curBuf
|
|
|
|
}
|
|
|
|
|
2017-05-23 03:45:16 -07:00
|
|
|
// next returns decodes the next entry pair and returns true
|
2018-04-08 02:28:30 -07:00
|
|
|
// if it was successful.
|
2017-05-23 03:45:16 -07:00
|
|
|
func (r *walReader) next() bool {
|
2017-09-06 05:59:25 -07:00
|
|
|
if r.cur >= len(r.files) {
|
2017-03-14 11:30:23 -07:00
|
|
|
return false
|
|
|
|
}
|
2017-09-06 05:59:25 -07:00
|
|
|
cf := r.files[r.cur]
|
2017-03-14 11:30:23 -07:00
|
|
|
|
2017-09-06 05:59:25 -07:00
|
|
|
// Remember the offset after the last correctly read entry. If the next one
|
|
|
|
// is corrupted, this is where we can safely truncate.
|
2018-03-21 13:39:43 -07:00
|
|
|
r.lastOffset, r.err = cf.Seek(0, io.SeekCurrent)
|
2017-09-06 05:59:25 -07:00
|
|
|
if r.err != nil {
|
2017-03-14 11:30:23 -07:00
|
|
|
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.
|
2022-12-29 07:23:07 -08:00
|
|
|
if errors.Is(err, io.EOF) {
|
2017-09-06 05:59:25 -07:00
|
|
|
if r.cur == len(r.files)-1 {
|
2017-03-14 11:30:23 -07:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
// Current reader completed, close and move to the next one.
|
|
|
|
if err := cf.Close(); err != nil {
|
2017-02-14 21:54:59 -08:00
|
|
|
r.err = err
|
2017-03-14 11:30:23 -07:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
r.cur++
|
2017-05-23 03:45:16 -07:00
|
|
|
return r.next()
|
2017-03-14 11:30:23 -07:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
r.err = err
|
2017-02-14 21:54:59 -08:00
|
|
|
return false
|
2017-02-14 15:54:52 -08:00
|
|
|
}
|
2017-02-14 21:54:59 -08:00
|
|
|
|
2017-05-26 08:56:31 -07:00
|
|
|
r.curType = et
|
|
|
|
r.curFlag = flag
|
|
|
|
r.curBuf = b
|
2017-02-14 21:54:59 -08:00
|
|
|
return r.err == nil
|
|
|
|
}
|
|
|
|
|
2017-09-06 05:59:25 -07:00
|
|
|
func (r *walReader) current() *segmentFile {
|
|
|
|
return r.files[r.cur]
|
2017-03-14 11:30:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// walCorruptionErr is a type wrapper for errors that indicate WAL corruption
|
|
|
|
// and trigger a truncation.
|
2017-09-06 05:59:25 -07:00
|
|
|
type walCorruptionErr struct {
|
|
|
|
err error
|
|
|
|
file int
|
|
|
|
lastOffset int64
|
|
|
|
}
|
2017-03-14 11:30:23 -07:00
|
|
|
|
2017-09-06 05:59:25 -07:00
|
|
|
func (e walCorruptionErr) Error() string {
|
|
|
|
return fmt.Sprintf("%s <file: %d, lastOffset: %d>", e.err, e.file, e.lastOffset)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *walReader) corruptionErr(s string, args ...interface{}) error {
|
|
|
|
return walCorruptionErr{
|
|
|
|
err: errors.Errorf(s, args...),
|
|
|
|
file: r.cur,
|
|
|
|
lastOffset: r.lastOffset,
|
|
|
|
}
|
2017-03-14 11:30:23 -07:00
|
|
|
}
|
|
|
|
|
2017-05-12 08:06:26 -07:00
|
|
|
func (r *walReader) entry(cr io.Reader) (WALEntryType, byte, []byte, error) {
|
2017-02-15 15:24:53 -08:00
|
|
|
r.crc32.Reset()
|
|
|
|
tr := io.TeeReader(cr, r.crc32)
|
2017-02-14 21:54:59 -08:00
|
|
|
|
|
|
|
b := make([]byte, 6)
|
2017-03-14 11:30:23 -07:00
|
|
|
if n, err := tr.Read(b); err != nil {
|
2017-02-14 21:54:59 -08:00
|
|
|
return 0, 0, nil, err
|
2017-03-14 11:30:23 -07:00
|
|
|
} else if n != 6 {
|
2017-09-06 05:59:25 -07:00
|
|
|
return 0, 0, nil, r.corruptionErr("invalid entry header size %d", n)
|
2017-02-14 21:54:59 -08: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 15:54:52 -08:00
|
|
|
}
|
2017-05-23 03:45:16 -07:00
|
|
|
if etype != WALEntrySeries && etype != WALEntrySamples && etype != WALEntryDeletes {
|
2017-09-06 05:59:25 -07:00
|
|
|
return 0, 0, nil, r.corruptionErr("invalid entry type %d", etype)
|
2017-03-14 11:30:23 -07:00
|
|
|
}
|
2017-02-14 21:54:59 -08:00
|
|
|
|
|
|
|
if length > len(r.buf) {
|
|
|
|
r.buf = make([]byte, length)
|
|
|
|
}
|
|
|
|
buf := r.buf[:length]
|
|
|
|
|
2017-03-14 11:30:23 -07:00
|
|
|
if n, err := tr.Read(buf); err != nil {
|
2017-02-14 21:54:59 -08:00
|
|
|
return 0, 0, nil, err
|
2017-03-14 11:30:23 -07:00
|
|
|
} else if n != length {
|
2017-09-06 05:59:25 -07:00
|
|
|
return 0, 0, nil, r.corruptionErr("invalid entry body size %d", n)
|
2017-02-14 21:54:59 -08:00
|
|
|
}
|
2017-03-14 11:30:23 -07:00
|
|
|
|
|
|
|
if n, err := cr.Read(b[:4]); err != nil {
|
2017-02-14 21:54:59 -08:00
|
|
|
return 0, 0, nil, err
|
2017-03-14 11:30:23 -07:00
|
|
|
} else if n != 4 {
|
2017-09-06 05:59:25 -07:00
|
|
|
return 0, 0, nil, r.corruptionErr("invalid checksum length %d", n)
|
2017-02-14 21:54:59 -08:00
|
|
|
}
|
2017-02-15 15:24:53 -08:00
|
|
|
if exp, has := binary.BigEndian.Uint32(b[:4]), r.crc32.Sum32(); has != exp {
|
2017-09-06 05:59:25 -07:00
|
|
|
return 0, 0, nil, r.corruptionErr("unexpected CRC32 checksum %x, want %x", has, exp)
|
2017-02-14 21:54:59 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return etype, flag, buf, nil
|
2017-02-14 15:54:52 -08:00
|
|
|
}
|
|
|
|
|
2019-09-19 02:15:41 -07:00
|
|
|
func (r *walReader) decodeSeries(flag byte, b []byte, res *[]record.RefSeries) error {
|
2019-02-22 09:11:11 -08:00
|
|
|
dec := encoding.Decbuf{B: b}
|
2017-08-31 02:39:22 -07:00
|
|
|
|
2019-02-22 09:11:11 -08:00
|
|
|
for len(dec.B) > 0 && dec.Err() == nil {
|
2021-11-06 03:10:04 -07:00
|
|
|
ref := chunks.HeadSeriesRef(dec.Be64())
|
2022-07-26 07:42:00 -07:00
|
|
|
lset := r.dec.DecodeLabels(&dec)
|
2016-12-22 06:18:33 -08:00
|
|
|
|
2019-09-19 02:15:41 -07:00
|
|
|
*res = append(*res, record.RefSeries{
|
2017-09-06 05:59:25 -07:00
|
|
|
Ref: ref,
|
|
|
|
Labels: lset,
|
|
|
|
})
|
|
|
|
}
|
2019-02-22 09:11:11 -08:00
|
|
|
if dec.Err() != nil {
|
|
|
|
return dec.Err()
|
2017-09-06 05:59:25 -07:00
|
|
|
}
|
2019-02-22 09:11:11 -08:00
|
|
|
if len(dec.B) > 0 {
|
|
|
|
return errors.Errorf("unexpected %d bytes left in entry", len(dec.B))
|
2016-12-22 06:18:33 -08:00
|
|
|
}
|
2017-10-06 05:06:39 -07:00
|
|
|
return nil
|
2016-12-22 06:18:33 -08:00
|
|
|
}
|
|
|
|
|
2019-09-19 02:15:41 -07:00
|
|
|
func (r *walReader) decodeSamples(flag byte, b []byte, res *[]record.RefSample) error {
|
2017-09-06 05:59:25 -07:00
|
|
|
if len(b) == 0 {
|
2017-10-06 05:06:39 -07:00
|
|
|
return nil
|
2017-09-06 05:59:25 -07:00
|
|
|
}
|
2019-02-22 09:11:11 -08:00
|
|
|
dec := encoding.Decbuf{B: b}
|
2017-05-23 03:45:16 -07:00
|
|
|
|
2016-12-22 06:18:33 -08:00
|
|
|
var (
|
2019-02-22 09:11:11 -08:00
|
|
|
baseRef = dec.Be64()
|
|
|
|
baseTime = dec.Be64int64()
|
2016-12-22 06:18:33 -08:00
|
|
|
)
|
2017-01-13 07:14:40 -08:00
|
|
|
|
2019-02-22 09:11:11 -08:00
|
|
|
for len(dec.B) > 0 && dec.Err() == nil {
|
|
|
|
dref := dec.Varint64()
|
|
|
|
dtime := dec.Varint64()
|
|
|
|
val := dec.Be64()
|
2016-12-22 06:18:33 -08:00
|
|
|
|
2019-09-19 02:15:41 -07:00
|
|
|
*res = append(*res, record.RefSample{
|
2021-11-06 03:10:04 -07:00
|
|
|
Ref: chunks.HeadSeriesRef(int64(baseRef) + dref),
|
2017-09-06 05:59:25 -07:00
|
|
|
T: baseTime + dtime,
|
|
|
|
V: math.Float64frombits(val),
|
|
|
|
})
|
|
|
|
}
|
2016-12-22 06:18:33 -08:00
|
|
|
|
2019-02-22 09:11:11 -08:00
|
|
|
if dec.Err() != nil {
|
|
|
|
return errors.Wrapf(dec.Err(), "decode error after %d samples", len(*res))
|
2017-09-06 05:59:25 -07:00
|
|
|
}
|
2019-02-22 09:11:11 -08:00
|
|
|
if len(dec.B) > 0 {
|
|
|
|
return errors.Errorf("unexpected %d bytes left in entry", len(dec.B))
|
2016-12-22 06:18:33 -08:00
|
|
|
}
|
2017-10-06 05:06:39 -07:00
|
|
|
return nil
|
2017-05-23 03:45:16 -07:00
|
|
|
}
|
|
|
|
|
2019-09-19 02:15:41 -07:00
|
|
|
func (r *walReader) decodeDeletes(flag byte, b []byte, res *[]tombstones.Stone) error {
|
2019-02-22 09:11:11 -08:00
|
|
|
dec := &encoding.Decbuf{B: b}
|
2017-05-23 03:45:16 -07:00
|
|
|
|
2019-02-22 09:11:11 -08:00
|
|
|
for dec.Len() > 0 && dec.Err() == nil {
|
2019-09-19 02:15:41 -07:00
|
|
|
*res = append(*res, tombstones.Stone{
|
2021-11-06 03:10:04 -07:00
|
|
|
Ref: storage.SeriesRef(dec.Be64()),
|
2019-09-19 02:15:41 -07:00
|
|
|
Intervals: tombstones.Intervals{
|
2019-02-22 09:11:11 -08:00
|
|
|
{Mint: dec.Varint64(), Maxt: dec.Varint64()},
|
2017-09-06 05:59:25 -07:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2019-02-22 09:11:11 -08:00
|
|
|
if dec.Err() != nil {
|
|
|
|
return dec.Err()
|
2017-09-06 05:59:25 -07:00
|
|
|
}
|
2019-02-22 09:11:11 -08:00
|
|
|
if len(dec.B) > 0 {
|
|
|
|
return errors.Errorf("unexpected %d bytes left in entry", len(dec.B))
|
2017-05-23 03:45:16 -07:00
|
|
|
}
|
2017-10-06 05:06:39 -07:00
|
|
|
return nil
|
2016-12-22 06:18:33 -08:00
|
|
|
}
|
2018-05-27 10:05:11 -07:00
|
|
|
|
2018-09-17 09:30:56 -07:00
|
|
|
func deprecatedWALExists(logger log.Logger, dir string) (bool, error) {
|
2018-05-27 10:05:11 -07:00
|
|
|
// Detect whether we still have the old WAL.
|
|
|
|
fns, err := sequenceFiles(dir)
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
2018-09-17 09:30:56 -07:00
|
|
|
return false, errors.Wrap(err, "list sequence files")
|
2018-05-27 10:05:11 -07:00
|
|
|
}
|
|
|
|
if len(fns) == 0 {
|
2018-09-17 09:30:56 -07:00
|
|
|
return false, nil // No WAL at all yet.
|
2018-05-27 10:05:11 -07:00
|
|
|
}
|
2018-06-05 01:50:20 -07:00
|
|
|
// Check header of first segment to see whether we are still dealing with an
|
|
|
|
// old WAL.
|
2018-05-27 10:05:11 -07:00
|
|
|
f, err := os.Open(fns[0])
|
|
|
|
if err != nil {
|
2018-09-17 09:30:56 -07:00
|
|
|
return false, errors.Wrap(err, "check first existing segment")
|
2018-05-27 10:05:11 -07:00
|
|
|
}
|
2018-06-05 01:21:27 -07:00
|
|
|
defer f.Close()
|
|
|
|
|
2018-05-27 10:05:11 -07:00
|
|
|
var hdr [4]byte
|
2018-06-05 01:50:20 -07:00
|
|
|
if _, err := f.Read(hdr[:]); err != nil && err != io.EOF {
|
2018-09-17 09:30:56 -07:00
|
|
|
return false, errors.Wrap(err, "read header from first segment")
|
2018-05-27 10:05:11 -07:00
|
|
|
}
|
2018-06-05 01:50:20 -07:00
|
|
|
// If we cannot read the magic header for segments of the old WAL, abort.
|
|
|
|
// Either it's migrated already or there's a corruption issue with which
|
|
|
|
// we cannot deal here anyway. Subsequent attempts to open the WAL will error in that case.
|
2018-05-27 10:05:11 -07:00
|
|
|
if binary.BigEndian.Uint32(hdr[:]) != WALMagic {
|
2018-09-17 09:30:56 -07:00
|
|
|
return false, nil
|
2018-05-27 10:05:11 -07:00
|
|
|
}
|
2018-09-17 09:30:56 -07:00
|
|
|
return true, nil
|
|
|
|
}
|
2018-05-27 10:05:11 -07:00
|
|
|
|
2018-09-17 09:30:56 -07:00
|
|
|
// MigrateWAL rewrites the deprecated write ahead log into the new format.
|
|
|
|
func MigrateWAL(logger log.Logger, dir string) (err error) {
|
|
|
|
if logger == nil {
|
|
|
|
logger = log.NewNopLogger()
|
|
|
|
}
|
|
|
|
if exists, err := deprecatedWALExists(logger, dir); err != nil || !exists {
|
|
|
|
return err
|
|
|
|
}
|
2020-04-11 01:22:18 -07:00
|
|
|
level.Info(logger).Log("msg", "Migrating WAL format")
|
2018-05-27 10:05:11 -07:00
|
|
|
|
|
|
|
tmpdir := dir + ".tmp"
|
|
|
|
if err := os.RemoveAll(tmpdir); err != nil {
|
|
|
|
return errors.Wrap(err, "cleanup replacement dir")
|
|
|
|
}
|
2022-10-10 08:08:46 -07:00
|
|
|
repl, err := wlog.New(logger, nil, tmpdir, false)
|
2018-05-27 10:05:11 -07:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "open new WAL")
|
|
|
|
}
|
2018-09-20 22:31:22 -07:00
|
|
|
|
2018-08-02 14:57:34 -07:00
|
|
|
// It should've already been closed as part of the previous finalization.
|
2018-06-05 01:21:27 -07:00
|
|
|
// Do it once again in case of prior errors.
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
repl.Close()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2018-05-27 10:05:11 -07:00
|
|
|
w, err := OpenSegmentWAL(dir, logger, time.Minute, nil)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "open old WAL")
|
|
|
|
}
|
2018-06-05 01:21:27 -07:00
|
|
|
defer w.Close()
|
|
|
|
|
2018-05-27 10:05:11 -07:00
|
|
|
rdr := w.Reader()
|
|
|
|
|
|
|
|
var (
|
2019-09-19 02:15:41 -07:00
|
|
|
enc record.Encoder
|
2018-05-27 10:05:11 -07:00
|
|
|
b []byte
|
|
|
|
)
|
|
|
|
decErr := rdr.Read(
|
2019-09-19 02:15:41 -07:00
|
|
|
func(s []record.RefSeries) {
|
2018-05-27 10:05:11 -07:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = repl.Log(enc.Series(s, b[:0]))
|
|
|
|
},
|
2019-09-19 02:15:41 -07:00
|
|
|
func(s []record.RefSample) {
|
2018-05-27 10:05:11 -07:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = repl.Log(enc.Samples(s, b[:0]))
|
|
|
|
},
|
2019-09-19 02:15:41 -07:00
|
|
|
func(s []tombstones.Stone) {
|
2018-05-27 10:05:11 -07:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = repl.Log(enc.Tombstones(s, b[:0]))
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if decErr != nil {
|
|
|
|
return errors.Wrap(err, "decode old entries")
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "write new entries")
|
|
|
|
}
|
2018-09-20 22:31:22 -07:00
|
|
|
// We explicitly close even when there is a defer for Windows to be
|
|
|
|
// able to delete it. The defer is in place to close it in-case there
|
|
|
|
// are errors above.
|
|
|
|
if err := w.Close(); err != nil {
|
|
|
|
return errors.Wrap(err, "close old WAL")
|
|
|
|
}
|
2018-05-27 10:05:11 -07:00
|
|
|
if err := repl.Close(); err != nil {
|
|
|
|
return errors.Wrap(err, "close new WAL")
|
|
|
|
}
|
2018-08-03 08:25:27 -07:00
|
|
|
if err := fileutil.Replace(tmpdir, dir); err != nil {
|
2018-05-27 10:05:11 -07:00
|
|
|
return errors.Wrap(err, "replace old WAL")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|