archive

package
v0.33.1 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2026 License: AGPL-3.0 Imports: 26 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package archive moves the captured result rows of closed connections from PostgreSQL to an object-storage bucket as Parquet files, and reads them back.

Only result rows are archived (one object per connection that captured any). Statements stay in PostgreSQL until their delete window: they are about 4% of the store, query_rows is 96%.

Index

Constants

View Source
const (
	// MaxConnectionsPerPass bounds the connections archived in one pass.
	MaxConnectionsPerPass = 200
	// MaxBytesPerPass bounds the object bytes written in one pass.
	MaxBytesPerPass int64 = 512 << 20
	// MaxDeletesPerPass bounds the archive objects deleted in one pass.
	MaxDeletesPerPass = 500
	// RowGroupRows caps a row group so the writer's memory is bounded for the 1%
	// of sessions that carry most of the bytes.
	RowGroupRows = 50_000
	// FailureNotifyAfter is how long consecutive failures last before the
	// notifier is called.
	FailureNotifyAfter = time.Hour
)

Bounds of one pass. The first pass after archival is switched on meets the whole history (tens of gigabytes on the measured instance) and must not hold the store for hours: it stops on whichever bound comes first and wakes the sweeper again.

View Source
const (
	MetaSchemaVersion = "dbbat.schema_version"
	MetaVersion       = "dbbat.version"
	MetaConnection    = "dbbat.connection"
	// MetaQueryOffsets maps each statement uid to [first row position, row
	// count] in the file: the rows of one statement are the positions
	// [first, first+count).
	MetaQueryOffsets = "dbbat.query_offsets"
)

File key-value metadata.

View Source
const SchemaVersion = "v1"

SchemaVersion is the "v1" of the object key and the dbbat.schema_version file metadata.

View Source
const WholeObjectLimit = 8 << 20

WholeObjectLimit is the size up to which an object is fetched whole, in one GET, checked against its SHA-256 and read from memory. That is nearly every object (a median connection captures 2 rows). A larger one is read through range requests, which are not checked against the hash: the download is.

Variables

View Source
var (
	ErrObjectMissing = errors.New("archive object is missing from the bucket")
	ErrHashMismatch  = errors.New("archive object does not match the SHA-256 sealed in the audit chain")
	ErrBadMetadata   = errors.New("archive object has bad metadata")
)

Read errors. The API turns each into a 502 that says which, never an empty list.

View Source
var ErrNotConfigured = errors.New("archive is not configured")

ErrNotConfigured is returned when reading an archive while no bucket is configured.

Functions

func Download

func Download(ctx context.Context, b *blob.Bucket, o Object) ([]byte, error)

Download returns the whole object, hash checked.

func Read

func Read[T any](ctx context.Context, b *blob.Bucket, o Object, offset int64, limit int) ([]T, error)

Read returns rows [offset, offset+limit) of one archive object.

func RowsKey

func RowsKey(disconnectedAt time.Time, serverUID, connectionUID uuid.UUID) string

RowsKey is the object key of a connection's rows: v1/rows/date=<UTC date of disconnected_at>/server=<server uid>/<connection uid>.parquet.

A pure function of the connection row, built from a date and uids only (never a server name or a username): a retry overwrites the same object.

func ValidKey

func ValidKey(key string) bool

ValidKey reports whether a key only uses the allowed charset.

Types

type Archiver

type Archiver struct {

	// OnPersistentFailure, when set, is called (at most once per outage) after an
	// hour of consecutive archive failures. Wired to the default notify channel.
	OnPersistentFailure func(err error)
	// contains filtered or unexported fields
}

Archiver writes and deletes archive objects.

func Open

func Open(ctx context.Context, url string, dataStore *store.Store, logger *slog.Logger) (*Archiver, error)

Open opens the archive bucket. An empty URL returns (nil, nil): archival does not exist, which is the default.

func (*Archiver) ArchiveConnection

func (a *Archiver) ArchiveConnection(ctx context.Context, cand store.ArchiveCandidate) (store.ArchiveSeal, error)

ArchiveConnection writes the rows of one claimed connection to the bucket, seals the object in the audit chain and, in one transaction, marks the connection archived and deletes its rows from PostgreSQL. A crash anywhere leaves the rows in PostgreSQL: the lease expires, the next pass redoes the connection and overwrites the same key.

func (*Archiver) Bucket

func (a *Archiver) Bucket() *blob.Bucket

Bucket exposes the bucket for readers.

func (*Archiver) Close

func (a *Archiver) Close() error

Close closes the bucket.

func (*Archiver) Pass

func (a *Archiver) Pass(ctx context.Context) (PassResult, error)

Pass runs the archive phases of a sweeper pass: archive due connections, then delete due archives.

type Object

type Object struct {
	Key    string
	Size   int64
	SHA256 string
}

Object identifies one archive object, as recorded on the connection row.

type PassResult

type PassResult struct {
	Archived int
	Deleted  int
	// Bounded is true when the pass stopped on its bound: more is due.
	Bounded bool
}

PassResult counts what one pass did.

type QueryRows

type QueryRows struct {
	Rows []RowRecord
	// Total is how many rows the statement has in the archive.
	Total int64
}

QueryRows is a page of one statement's rows.

func ReadQueryRows

func ReadQueryRows(
	ctx context.Context, b *blob.Bucket, o Object, queryUID uuid.UUID, offset int64, limit int,
) (QueryRows, error)

ReadQueryRows returns rows [offset, offset+limit) of one statement of an archived connection. The statement's position in the file comes from the file's own metadata. A statement with no rows in the file returns none.

type RowRecord

type RowRecord struct {
	ConnectionUID [16]byte `parquet:"connection_uid,uuid"`
	QueryUID      [16]byte `parquet:"query_uid,uuid"`
	// RowUID is the uid the row chain MAC covers; it is needed to recompute
	// the chain.
	RowUID    [16]byte `parquet:"row_uid,uuid"`
	RowNumber int32    `parquet:"row_number"`
	// RowData is the canonical JSON text as PostgreSQL's jsonb stores it. Every
	// statement has its own columns, so this stays one JSON column.
	RowData      string `parquet:"row_data,json"`
	RowSizeBytes int64  `parquet:"row_size_bytes"`
}

RowRecord is one captured result row in the Parquet file. The file is sorted by statement order (executed_at, uid) then row_number.

There is no MAC column: a MAC is 32 bytes that do not compress and the median row carries 31 bytes of data. The row chain is whole in a file, so a verifier holding the key recomputes it from row_uid, row_number, row_data and row_size_bytes and compares the head with queries.row_chain_mac, which stays in PostgreSQL. The file's own integrity is its SHA-256, sealed in the audit chain.

Jump to

Keyboard shortcuts

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