Help us shape the future of Go! Take the 2026 Go Developer Survey

wal

package
v0.7.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 8, 2026 License: AGPL-3.0 Imports: 9 Imported by: 0

Documentation

Overview

Package wal is mlfs's local data write-ahead log.

Each in-flight write appends a durable (fsync'd) record describing the slice it produced — (seq, op, ino, indx, off, slice_id, geometry) — BEFORE the metadata commit to PostgreSQL. If the daemon crashes after the data is staged (internal/cache) but before the slice_ref row commits, startup Replay re-runs the metadata write so no acknowledged write is lost. Replay is idempotent via the slice_id natural key (meta.Write uses ON CONFLICT DO NOTHING).

Records are framed [u32 len][u32 crc32(payload)][payload]; a torn tail (short read or bad CRC, only possible at the end of the last segment after a crash) is truncated on Open. Segments roll at a size threshold; Checkpoint drops segments whose records have all been committed.

Index

Constants

View Source
const DefaultSegmentBytes = 64 << 20 // 64 MiB

DefaultSegmentBytes is the size at which a segment rolls.

Variables

This section is empty.

Functions

This section is empty.

Types

type OpType

type OpType uint8

OpType identifies the logged operation.

const (
	OpWrite    OpType = 1 // a slice written over a chunk
	OpTruncate OpType = 2 // a file truncation (length change)
)

type Record

type Record struct {
	Seq      uint64
	Op       OpType
	Ino      uint64
	Indx     uint32
	Off      uint32
	SliceID  uint64
	Size     uint32
	SliceOff uint32
	SliceLen uint32
}

Record is one logged operation. For OpWrite the slice fields describe the data; for OpTruncate, Off carries the new length's low 32 bits and Indx the high bits (callers encode as needed).

type WAL

type WAL struct {
	// contains filtered or unexported fields
}

WAL is an append-only, segmented, fsync'd log. Safe for concurrent use.

func Open

func Open(dir string) (*WAL, error)

Open opens (or creates) the WAL in dir, truncating a torn tail left by a crash and positioning to append after the highest valid record.

func (*WAL) Append

func (w *WAL) Append(rec Record) (uint64, error)

Append assigns a sequence number, writes the record framed + fsync'd, and returns the assigned seq. The record is durable when Append returns.

func (*WAL) Checkpoint

func (w *WAL) Checkpoint(upToSeq uint64) error

Checkpoint drops fully-committed segments: any non-current segment whose highest seq is <= upToSeq is deleted (its records have all landed in SQL).

func (*WAL) Close

func (w *WAL) Close() error

Close fsyncs and closes the current segment.

func (*WAL) LastSeq

func (w *WAL) LastSeq() uint64

LastSeq returns the highest assigned sequence number.

func (*WAL) Replay

func (w *WAL) Replay(fn func(Record) error) error

Replay invokes fn for every valid record across all segments, in seq order. The handler is responsible for idempotency (mlfs's meta.Write is idempotent on slice_id).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL