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
- Variables
- func Download(ctx context.Context, b *blob.Bucket, o Object) ([]byte, error)
- func Read[T any](ctx context.Context, b *blob.Bucket, o Object, offset int64, limit int) ([]T, error)
- func RowsKey(disconnectedAt time.Time, serverUID, connectionUID uuid.UUID) string
- func ValidKey(key string) bool
- type Archiver
- type Object
- type PassResult
- type QueryRows
- type RowRecord
Constants ¶
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.
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.
const SchemaVersion = "v1"
SchemaVersion is the "v1" of the object key and the dbbat.schema_version file metadata.
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 ¶
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.
var ErrNotConfigured = errors.New("archive is not configured")
ErrNotConfigured is returned when reading an archive while no bucket is configured.
Functions ¶
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.
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.
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.