Documentation
¶
Overview ¶
Package wal implements a segmented write-ahead log for crash-safe persistence of time-series data.
WAL design informed by Prometheus tsdb/wal. See /NOTICE.md.
Index ¶
- Variables
- func EncodeRecord(dst []byte, typ RecordType, payload []byte) []byte
- func EncodeSamplesRecord(dst []byte, samples []RefSample) []byte
- func EncodeSeriesRecord(dst []byte, rec SeriesRecord) []byte
- func RecordSize(payloadLen int) int
- type Options
- type Reader
- type Record
- type RecordType
- type RefSample
- type SeriesRecord
- type WAL
- func (w *WAL) Close() error
- func (w *WAL) LastSegment() int
- func (w *WAL) LastSyncDuration() float64
- func (w *WAL) Log(typ RecordType, payload []byte) error
- func (w *WAL) LogSamples(samples []RefSample) error
- func (w *WAL) LogSeries(recs []SeriesRecord) error
- func (w *WAL) Replay() (*Reader, error)
- func (w *WAL) Sync() error
- func (w *WAL) Truncate(below int) error
Constants ¶
This section is empty.
Variables ¶
var ( ErrInvalidRecord = errors.New("wal: invalid record") ErrCorruptRecord = errors.New("wal: corrupt record (CRC mismatch)") )
var ErrShortPayload = errors.New("wal: payload too short")
Functions ¶
func EncodeRecord ¶
func EncodeRecord(dst []byte, typ RecordType, payload []byte) []byte
EncodeRecord appends a framed record (type + len + payload + crc32c) to dst and returns the extended slice.
func EncodeSamplesRecord ¶
EncodeSamplesRecord appends the encoded samples record to dst.
nsamples(4) | for each: ref(8) t(8) v(8)
func EncodeSeriesRecord ¶
func EncodeSeriesRecord(dst []byte, rec SeriesRecord) []byte
EncodeSeriesRecord appends the encoded series record to dst.
ref(8) | nlabels(4) | for each: namelen(2) name valuelen(2) value
func RecordSize ¶
RecordSize returns the total on-disk size of a record with the given payload length.
Types ¶
type Options ¶
type Options struct {
// SegmentMaxSize is the maximum size of a single segment file in bytes.
// A new segment is created when the current one would exceed this.
// Default: 128 MiB.
SegmentMaxSize int
// SyncInterval controls background fsync frequency.
// Default (zero): 1s. Negative: sync on every Log call.
SyncInterval time.Duration
}
Options configures WAL behavior.
type Reader ¶
type Reader struct {
// contains filtered or unexported fields
}
Reader scans WAL segments sequentially, validating CRC on each record. It follows the iterator pattern: Next() advances, Record() returns the current record, Err() returns any error after Next() returns false.
func (*Reader) Err ¶
Err returns the error encountered during reading, if any. A nil error after Next() returns false means all records were read cleanly.
type Record ¶
type Record struct {
Type RecordType
Data []byte // raw payload; decode with DecodeSeriesRecord / DecodeSamplesRecord
}
Record is a decoded WAL record returned by the Reader.
type RecordType ¶
type RecordType byte
RecordType identifies the kind of record stored in the WAL.
const ( RecordSeries RecordType = 1 RecordSamples RecordType = 2 )
func DecodeRecord ¶
func DecodeRecord(b []byte) (typ RecordType, payload []byte, consumed int, err error)
DecodeRecord parses a framed record from b. It returns the record type, the payload slice (a sub-slice of b), the total number of bytes consumed, and any error. On success, consumed == RecordSize(len(payload)).
type RefSample ¶
RefSample is a single sample keyed by series ref.
func DecodeSamplesRecord ¶
DecodeSamplesRecord decodes a samples payload.
type SeriesRecord ¶
SeriesRecord is a WAL record that registers a new series.
func DecodeSeriesRecord ¶
func DecodeSeriesRecord(data []byte) (SeriesRecord, error)
DecodeSeriesRecord decodes a series payload. The returned Labels hold copies of the strings (not sub-slices of data).
type WAL ¶
type WAL struct {
// contains filtered or unexported fields
}
WAL is a segmented write-ahead log.
func Open ¶
Open opens or creates a WAL in dir. If segments already exist, it runs recovery (truncating at the first corrupt record) before returning.
func (*WAL) LastSegment ¶
LastSegment returns the index of the current active segment.
func (*WAL) LastSyncDuration ¶
LastSyncDuration returns the duration of the most recent fsync in seconds.
func (*WAL) Log ¶
func (w *WAL) Log(typ RecordType, payload []byte) error
Log writes a framed record to the WAL. The payload is wrapped with the record envelope (type + length + CRC).
func (*WAL) LogSamples ¶
LogSamples encodes and writes a samples record.
func (*WAL) LogSeries ¶
func (w *WAL) LogSeries(recs []SeriesRecord) error
LogSeries encodes and writes a series record.