Documentation
¶
Overview ¶
Package dbbranch holds the lifecycle rules of database branches: states, engine capabilities, quotas and expiry selection.
Index ¶
- Constants
- Variables
- func CanTransition(from, to string) bool
- func CheckQuota(l Limits, r Request, all []store.DatabaseBranch, guard *diskguard.Guard) error
- func ExpiryFor(now time.Time, ttl time.Duration) string
- func ModeAvailable(engine, mode string, h Hints) (bool, string)
- func Move(ctx context.Context, s Store, id, from, to, reason, now string) error
- func Reachable(state string) bool
- func SelectExpired(rows []store.DatabaseBranch, now time.Time, p store.DuePolicy) []store.DatabaseBranch
- type Capability
- type CloneStore
- type Cloner
- type Dumper
- type ExpiryJob
- type ExpiryPolicy
- type ExpiryStore
- type Hints
- type Limits
- type ModeCap
- type Request
- type Restorer
- type Store
Constants ¶
const ( ModeFull = "full" ModePITR = "pitr" ModeSchema = "schema" ModeAnonymized = "anonymized" )
Branch modes.
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 ¶
var ErrBadTransition = errors.New("dbbranch: transition not allowed")
ErrBadTransition is returned for a state move the lifecycle does not allow.
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.
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.
var ErrQuota = errors.New("dbbranch: branch quota reached")
ErrQuota is returned when a branch limit is reached.
var Modes = []string{ModeFull, ModePITR, ModeSchema, ModeAnonymized}
Modes lists every mode in display order.
Functions ¶
func CanTransition ¶
CanTransition reports whether from may move to to. Any step before ready and any in flight step may fail.
func CheckQuota ¶
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 ¶
ExpiryFor returns the expiry time for a new branch: never for a zero ttl, otherwise now plus ttl.
func ModeAvailable ¶
ModeAvailable reports whether mode may be used and the reason when not.
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.
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).
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 ¶
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.