job

package
v0.1.0-beta.2 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: Apache-2.0 Imports: 23 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 job runs the backup test pipeline and builds its result.

Index

Constants

View Source
const (
	ProblemMissing         = "missing"
	ProblemRowCountChanged = "row_count_changed"
)

Problems reported for a single table.

Variables

This section is empty.

Functions

func PlainTableKey

func PlainTableKey(table TableRef) string

PlainTableKey identifies a table by its readable name.

func WorkFileFailure

func WorkFileFailure(err error) error

WorkFileFailure explains why the runner couldn't write to its work directory.

Types

type Backup

type Backup struct {
	File            string         `json:"file"`
	ObjectKey       string         `json:"object_key,omitempty"`
	SizeBytes       int64          `json:"size_bytes"`
	Time            time.Time      `json:"time,omitzero"`
	Format          archive.Format `json:"format,omitempty"`
	Compression     string         `json:"compression,omitempty"`
	Encryption      string         `json:"encryption,omitempty"`
	PostgresVersion string         `json:"postgres_version,omitempty"`
	PostgresMajor   int            `json:"postgres_major,omitempty"`
	VersionSource   VersionSource  `json:"version_source,omitempty"`
	RestoredMajor   int            `json:"restored_major,omitempty"`
}

Backup describes the file that was tested.

type CheckName

type CheckName string

CheckName identifies a built-in check.

const (
	CheckTablesExist     CheckName = "tables_exist"
	CheckRowCounts       CheckName = "row_counts"
	CheckLatestTimestamp CheckName = "latest_timestamp"
)

Built-in checks.

type CheckResult

type CheckResult struct {
	Name     CheckName      `json:"name"`
	Status   CheckStatus    `json:"status"`
	Code     string         `json:"code,omitempty"`
	Reason   string         `json:"reason,omitempty"`
	Measured string         `json:"measured,omitempty"`
	Expected string         `json:"expected,omitempty"`
	Tables   []TableFinding `json:"tables,omitempty"`
}

CheckResult reports one check. Table names stay in Tables, so they can be hashed before the result leaves the runner, and never appear in Reason.

type CheckStatus

type CheckStatus string

CheckStatus is the outcome of one check.

const (
	CheckPassed  CheckStatus = "pass"
	CheckFailed  CheckStatus = "fail"
	CheckSkipped CheckStatus = "skipped"
)

Check outcomes.

type Checks

type Checks struct {
	// ExpectedTables, Baseline and TableTolerancePercent identify tables by table key.
	ExpectedTables []string
	// Baseline holds the row counts of the last passing run. It is empty on a source's first run.
	Baseline              []KeyedCount
	TolerancePercent      float64
	TableTolerancePercent map[string]float64
	EstimateRows          bool
	LatestTimestamp       *TimestampCheck
	// TableKey defaults to PlainTableKey.
	TableKey TableKeyFunc
	// BackupTime is when the backup was made. The file's modification time is used when it is zero.
	BackupTime time.Time
}

Checks configures how a restored backup is judged.

type Dump

type Dump struct {
	Path   string
	Format archive.Format
}

Dump is a backup file that is ready to restore.

type Engine

type Engine interface {
	Ready(ctx context.Context) error
	DetectVersion(ctx context.Context, jobID string, dump Dump) (Version, error)
	Start(ctx context.Context, jobID string, version Version) (Instance, error)
	Cleanup(ctx context.Context, jobID string) error
	RemoveOrphans(ctx context.Context, keepJobIDs ...string) error
}

Engine restores dumps of one database type.

type Failure

type Failure struct {
	Status Status
	Code   string
	Reason string
	// Stage overrides the running stage when one stage covers several steps.
	Stage Stage
	// contains filtered or unexported fields
}

Failure explains why a job didn't pass. Code and Reason must never contain SQL or row contents.

func Fail

func Fail(code, reason string) *Failure

Fail reports that the backup is bad.

func RunnerError

func RunnerError(code, reason string, cause error) *Failure

RunnerError reports that the runner couldn't do its job.

func (*Failure) Error

func (f *Failure) Error() string

Error returns the reason shown to the user.

func (*Failure) Unwrap

func (f *Failure) Unwrap() error

Unwrap returns the underlying cause, if there is one.

type Instance

type Instance interface {
	Major() int
	Restore(ctx context.Context, dump Dump) error
	Tables(ctx context.Context, estimate bool) ([]TableCount, error)
	LatestTimestamp(ctx context.Context, check TimestampCheck) (TimestampReading, error)
}

Instance is a throwaway database server.

type KeyedCount

type KeyedCount struct {
	Key  string
	Rows int64
}

KeyedCount is the number of rows in the table with this key.

type Object

type Object struct {
	Key          string
	Size         int64
	LastModified time.Time
}

Object is a backup stored in a source.

type Options

type Options struct {
	Checks   Checks
	WorkDir  string
	SourceID string
	Secrets  config.SourceSecrets
	// DiskMargin is how many times the backup's size must be free before downloading it.
	DiskMargin float64
	// PostgresHint is the major version to use when the backup doesn't say which one made it.
	PostgresHint int
	// OnStage is told when each stage starts.
	OnStage func(Stage)
	// RunningJobs lists the other jobs in progress, whose containers and files must be kept.
	RunningJobs func() []string
}

Options configures one job.

type Result

type Result struct {
	SchemaVersion int           `json:"schema_version"`
	Status        Status        `json:"status"`
	Stage         Stage         `json:"stage,omitempty"`
	ReasonCode    string        `json:"reason_code,omitempty"`
	Reason        string        `json:"reason,omitempty"`
	Backup        Backup        `json:"backup"`
	Stages        []StageTiming `json:"stages"`
	Tables        []TableCount  `json:"tables"`
	Checks        []CheckResult `json:"checks"`
	Notes         []string      `json:"notes,omitempty"`
	// contains filtered or unexported fields
}

Result is the report of one job.

func ErrorResult

func ErrorResult(failure *Failure) Result

ErrorResult reports a job that couldn't start.

func RunLocal

func RunLocal(ctx context.Context, engine Engine, logger *slog.Logger, jobID, path string, options Options) Result

RunLocal tests a backup file on this machine and always removes what it created.

func RunSource

func RunSource(ctx context.Context, engine Engine, source Source, logger *slog.Logger, jobID string, options Options) Result

RunSource tests the latest backup of a source and always removes what it created.

type Source

type Source interface {
	Latest(ctx context.Context) (Object, error)
	Download(ctx context.Context, object Object, path string) error
}

Source finds and downloads backups.

type Stage

type Stage string

Stage names a step of the pipeline.

const (
	StageResolve       Stage = "resolve"
	StagePreflight     Stage = "preflight"
	StageDownload      Stage = "download"
	StageDecrypt       Stage = "decrypt"
	StageDecompress    Stage = "decompress"
	StageDetectVersion Stage = "detect_version"
	StageRestore       Stage = "restore"
	StageCheck         Stage = "check"
	StageCleanup       Stage = "cleanup"
)

Pipeline stages, in the order they run.

type StageTiming

type StageTiming struct {
	Name       Stage `json:"name"`
	DurationMS int64 `json:"duration_ms"`
}

StageTiming records how long a stage took.

type Status

type Status string

Status is the outcome of a job.

const (
	StatusPass  Status = "pass"
	StatusFail  Status = "fail"
	StatusError Status = "error"
)

A failed backup and a runner that couldn't do its job trigger different alerts.

type TableCount

type TableCount struct {
	Schema string `json:"schema"`
	Name   string `json:"name"`
	Rows   int64  `json:"rows"`
}

TableCount is the number of rows in a restored table.

type TableFinding

type TableFinding struct {
	Key              string   `json:"table_key"`
	Problem          string   `json:"problem"`
	MeasuredRows     *int64   `json:"measured_rows,omitempty"`
	ExpectedRows     *int64   `json:"expected_rows,omitempty"`
	ChangePercent    *float64 `json:"change_percent,omitempty"`
	TolerancePercent *float64 `json:"tolerance_percent,omitempty"`
}

TableFinding describes what is wrong with one table.

type TableKeyFunc

type TableKeyFunc func(TableRef) string

TableKeyFunc turns a table into the key that identifies it outside the restored database.

func HashedTableKey

func HashedTableKey(salt config.Secret) TableKeyFunc

HashedTableKey identifies tables without revealing their names. The salt stops anyone from recovering names by hashing common ones, and the NUL byte keeps schema and table apart.

type TableRef

type TableRef struct {
	Schema string
	Name   string
}

TableRef names a table.

type TimestampCheck

type TimestampCheck struct {
	Table  TableRef
	Column string
	MaxAge time.Duration
}

TimestampCheck requires a recent row in the chosen column.

type TimestampReading

type TimestampReading struct {
	State TimestampState
	Value time.Time
}

TimestampReading is the newest value in the chosen column.

type TimestampState

type TimestampState int

TimestampState says what was found in the chosen column.

const (
	TimestampFound TimestampState = iota
	TimestampNoColumn
	TimestampNoRows
)

States of the chosen timestamp column.

type Version

type Version struct {
	Full   string
	Major  int
	Source VersionSource
}

Version is the Postgres version a dump was made with.

type VersionSource

type VersionSource string

VersionSource says where the detected Postgres version came from.

const (
	VersionFromDump VersionSource = "dump"
	VersionFromHint VersionSource = "hint"
	VersionDefault  VersionSource = "default"
)

Places a Postgres version can come from.

Jump to

Keyboard shortcuts

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