dbbranch

package
v1.5.12 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: Apache-2.0 Imports: 9 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 dbbranch holds the lifecycle rules of database branches: states, engine capabilities, quotas and expiry selection.

Index

Constants

View Source
const (
	ModeFull       = "full"
	ModePITR       = "pitr"
	ModeSchema     = "schema"
	ModeAnonymized = "anonymized"
)

Branch modes.

View Source
const (
	StateRequested   = "requested"
	StateCloning     = "cloning"
	StateMasking     = "masking"
	StateCredentials = "credentials"
	StateVerifying   = "verifying"
	StateReady       = "ready"
	StateResetting   = "resetting"
	StatePromoting   = "promoting"
	StatePromoted    = "promoted"
	StateExpiring    = "expiring"
	StateDeleting    = "deleting"
	StateDeleted     = "deleted"
	StateFailed      = "failed"
)

Lifecycle states of a branch.

Variables

View Source
var ErrBadTransition = errors.New("dbbranch: transition not allowed")

ErrBadTransition is returned for a state move the lifecycle does not allow.

View Source
var ErrDumpTruncated = errors.New("dbbranch: dump stream failed or was cut short")

ErrDumpTruncated marks a dump stream that errored, was empty or passed the byte cap.

View Source
var ErrLostOwnership = errors.New("dbbranch: branch row is no longer owned by this clone")

ErrLostOwnership is returned when the branch row no longer belongs to this clone, for example after a delete.

View Source
var ErrQuota = errors.New("dbbranch: branch quota reached")

ErrQuota is returned when a branch limit is reached.

Modes lists every mode in display order.

Functions

func CanTransition

func CanTransition(from, to string) bool

CanTransition reports whether from may move to to. Any step before ready and any in flight step may fail.

func CheckQuota

func CheckQuota(l Limits, r Request, all []store.DatabaseBranch, guard *diskguard.Guard) error

CheckQuota refuses a branch over a limit. It reads a snapshot, so the API slice must serialize creates or insert through store.CreateDatabaseBranchWithinQuota. all is every existing branch row.

func ExpiryFor

func ExpiryFor(now time.Time, ttl time.Duration) string

ExpiryFor returns the expiry time for a new branch: never for a zero ttl, otherwise now plus ttl.

func ModeAvailable

func ModeAvailable(engine, mode string, h Hints) (bool, string)

ModeAvailable reports whether mode may be used and the reason when not.

func Move

func Move(ctx context.Context, s Store, id, from, to, reason, now string) error

Move validates the transition and applies it as a compare and set on (id, state).

func Reachable

func Reachable(state string) bool

Reachable reports whether the branch may serve clients: only ready or promoted.

func SelectExpired

func SelectExpired(rows []store.DatabaseBranch, now time.Time, p store.DuePolicy) []store.DatabaseBranch

SelectExpired picks what the expiry job collects, through the one rule in the store.

Types

type Capability

type Capability struct {
	Engine   string             `json:"engine"`
	Modes    map[string]ModeCap `json:"modes"`
	FastPath bool               `json:"fast_path"`
	Branches bool               `json:"branches"`
}

Capability is what a branch of one engine can do.

func CapabilityFor

func CapabilityFor(engine string, h Hints) Capability

CapabilityFor returns the honest capability of an engine. Unknown engines are refused.

type CloneStore

type CloneStore interface {
	Store
	GetDatabaseBranch(ctx context.Context, name string) (store.DatabaseBranch, error)
	SetDatabaseBranchClone(ctx context.Context, id, clonePath, reason, now string) error
	SetDatabaseBranchSize(ctx context.Context, id string, bytes int64, now string) error
}

CloneStore is the store surface the clone worker needs.

type Cloner

type Cloner struct {
	Store    CloneStore
	Dumper   Dumper
	Restorer Restorer
	// WaitReady blocks until the branch database reports ready.
	WaitReady func(ctx context.Context, databaseName string) error
	// Teardown removes the branch container and volume after a failure.
	Teardown func(ctx context.Context, databaseName string) error
	// Cleanup, when set, runs instead of Teardown after a failure: it removes the container and volume
	// but keeps the database and branch rows, so the reason stays visible until failed_retention.
	Cleanup func(ctx context.Context, databaseName string) error
	// Container maps a database name to its container name.
	Container func(databaseName string) string
	// Count returns the table or key count of a container, used to compare branch with parent before ready.
	Count func(ctx context.Context, engine, containerName string) (int64, error)
	// MaxBytes caps the dump stream; zero or nil means no cap.
	MaxBytes func(ctx context.Context) int64
	// MeasureSize is optional and reports the branch volume use in bytes.
	MeasureSize func(ctx context.Context, databaseName string) (int64, error)
	Timeout     func(ctx context.Context) time.Duration
	Now         func() time.Time
	Log         *slog.Logger
}

Cloner runs the slow path: dump the parent and restore into the branch's own new database. The branch database is created by the caller with no published port and no links, so it is unreachable except by container name until the row is ready.

func (*Cloner) Run

func (c *Cloner) Run(ctx context.Context, b store.DatabaseBranch, engine string) error

Run clones into branch b, whose row is requested (new) or resetting (reset). It records the outcome in the row and returns the error it recorded. State moves go only through Move.

type Dumper

type Dumper interface {
	Dump(ctx context.Context, engine, containerName string, schemaOnly bool) (io.ReadCloser, string, error)
}

Dumper streams a consistent dump of a live parent and labels its consistency.

type ExpiryJob

type ExpiryJob struct {
	Store  ExpiryStore
	Policy func(ctx context.Context) ExpiryPolicy
	// Delete runs the normal teardown and removes the row; it must honor the store guards.
	Delete func(ctx context.Context, b store.DatabaseBranch) error
	// Warn is the existing notification path; optional.
	Warn func(ctx context.Context, b store.DatabaseBranch, expiresAt time.Time)
	Now  func() time.Time
	Log  *slog.Logger
	// contains filtered or unexported fields
}

ExpiryJob deletes due branches through the normal teardown and warns before expiry. Protected rows and rows with live children are never selected (store.SelectDueBranches).

func (*ExpiryJob) Run

func (j *ExpiryJob) Run(ctx context.Context)

Run ticks until ctx ends. The interval is re-read each round so a settings change applies live.

func (*ExpiryJob) Tick

func (j *ExpiryJob) Tick(ctx context.Context)

Tick runs one collection and warning pass.

type ExpiryPolicy

type ExpiryPolicy struct {
	Enabled  bool
	Interval time.Duration
	// Warning is how long before expiry a warning goes out; zero disables it.
	Warning         time.Duration
	FailedRetention time.Duration
	// Retry is how long an expiring or deleting row may sit before the job resumes it.
	Retry time.Duration
	// RequestedTimeout is how long a requested row may wait for its clone to start.
	RequestedTimeout time.Duration
}

ExpiryPolicy is read from the live branches.* settings on every tick.

type ExpiryStore

type ExpiryStore interface {
	Store
	ListAllDatabaseBranches(ctx context.Context) ([]store.DatabaseBranch, error)
}

ExpiryStore is the store surface of the expiry job.

type Hints

type Hints struct {
	// PITREnabled is whether the database reports point in time recovery on.
	PITREnabled bool
	// MySQLPITRRunner is whether this control plane has the MySQL PITR clone runner wired.
	MySQLPITRRunner bool
}

Hints are facts about the one database and this control plane that the engine alone cannot tell. The caller must fill them: a missing MySQL PITR runner is reported as unavailable, never assumed present.

type Limits

type Limits struct {
	MaxPerRoot       int
	MaxTotal         int
	MaxTotalBytes    int64
	MaxDepth         int
	SlowPathMaxBytes int64
	Headroom         float64
}

Limits are the live settings the checks read.

type ModeCap

type ModeCap struct {
	Available bool   `json:"available"`
	Reason    string `json:"reason,omitempty"`
}

ModeCap is whether one mode is available for an engine, with the reason when it is not.

type Request

type Request struct {
	Parent       store.DatabaseBranch
	ParentIsRoot bool
	RootName     string
	ParentBytes  int64
	SlowPath     bool
}

Request is one branch about to be created.

type Restorer

type Restorer interface {
	Restore(ctx context.Context, engine, containerName string, dump io.Reader) error
}

Restorer applies a dump into the branch container.

type Store

type Store interface {
	UpdateDatabaseBranchState(ctx context.Context, id, from, to, reason, now string) error
}

Store is the part of the branch store the lifecycle needs.

Jump to

Keyboard shortcuts

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