office

package
v0.0.4 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: 13 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 office takes and keeps office for one database on this node: it runs candidacies until one succeeds, serves the database through an engine.Leader while the node holds office, and leaves office when the leader stops, the node is deposed or the caller shuts it down. It is the leader's lifecycle of record 0004 decisions 3, 6 and 9; it serves nothing itself.

Index

Constants

View Source
const (
	DefaultTTL          = 60 * time.Second
	DefaultDrainTimeout = 60 * time.Second
)

Defaults. The records fix no lease timing. The renewal period is a third of the time to live, the cadence at which a candidate renews during its walk, so a leader misses two renewals before its lease expires. The drain timeout is record 0005's deployment budget T at its defaults (decision 7): a statement time of 30 s, the engine's bound R of 20 s and a response deadline of 10 s.

Variables

View Source
var (
	// ErrDeposed means the manifest no longer carries this node's office:
	// another candidacy took it.
	ErrDeposed = errors.New("office: deposed by another candidacy")
	// ErrUnchecked means the loop could not read the manifest for a time to
	// live, so it can no longer tell that it holds office: its lease has
	// expired, and a standby may have taken office.
	ErrUnchecked = errors.New("office: the manifest could not be read for a time to live")
	// ErrShutdown means the loop's context ended.
	ErrShutdown = errors.New("office: shut down")
)

Reasons a loop stops leading, as State.Reason reports them.

Functions

func NewHolder

func NewHolder(pod string) (string, error)

NewHolder returns a holder name for this process: pod, cut to its first 111 characters, then "_" and 16 random hexadecimal digits, so that two processes never share one even when their pod names are cut. pod is 1 or more of A-Z a-z 0-9 . _ -, as a Kubernetes pod name is.

Types

type Config

type Config struct {
	Store  storage.Store
	Keys   format.Keys
	Holder string // the lease's holder name; see NewHolder
	Dir    string // local directory for the leader's files
	Engine engine.Config
	// FollowerReads tails the database while this node stands by.
	FollowerReads bool

	TTL time.Duration // lease time to live; DefaultTTL if 0
	// Renew is the renewal period, TTL/3 if 0. It is at most TTL/3, so that
	// a lease outlives one renewal that hangs: that renewal costs two
	// periods, its timeout and the wait for the next tick.
	Renew time.Duration
	// Drain, if set, runs when the loop's context ends while it leads,
	// after State reports it not leading and before the leader closes. It
	// stops new work on the database and returns once the work already
	// running has ended or ctx is done. Closing the leader cancels every
	// Run still in progress, which can turn a commit in its upload into an
	// unknown outcome.
	Drain func(ctx context.Context)
	// DrainTimeout bounds Drain, DefaultDrainTimeout if 0, by ending its
	// context: the drain is bounded only if Drain honours that context.
	DrainTimeout time.Duration
	Now          func() time.Time // the lease clock; time.Now if nil
	Logger       *slog.Logger     // slog.Default() if nil
}

Config configures a Loop.

type Loop

type Loop struct {
	// contains filtered or unexported fields
}

Loop takes and keeps office for one database. Its methods are safe for concurrent use.

func New

func New(cfg Config) (*Loop, error)

New returns a loop for cfg, with defaults filled in. Run starts it.

func (*Loop) Run

func (l *Loop) Run(ctx context.Context) error

Run runs candidacies until one takes office, keeps office, and starts again whenever it leaves office, until ctx ends. After a failed attempt, and after leaving office, it waits a backoff that doubles from a second, or the renewal period if that is shorter, up to the renewal period, and starts from there again once a leader is in office. A standby never takes over a live lease: it reads the lease at most min(renewal period, 5 s) apart and under a timeout of one renewal period, and runs a candidacy once the lease reads as released or expired, or cannot be read. After each read that finds the lease live it reads the manifest's token, and its body when the token changed, under a timeout of its own, for the grace period of retirement (engine.Config.Seen).

A node in office starts its leader with engine.Lead, which walks the log tail and can take longer than a time to live, and keeps renewing while it runs. Every renewal period, under a timeout of one renewal period, it reads the manifest, then:

  • leaves office if its leader has stopped, closing it without a lease write: a node that fail-stops issues no further write for the database (record 0004 decision 6). That is best effort: a leader that stops between this check and the lease write lets one renewal, or the release on shutdown, out at its old epoch. That is harmless: the lease is never a fence, and the write is no write of data;
  • leaves office, without releasing the lease, if the manifest no longer carries its office, or if it has not read the manifest for a time to live (ErrUnchecked). A lease it retook as it was deposed is left to expire, as a crashed leader's would: only the log and the manifest decide who leads;
  • renews the lease, and calls Retake if another holder holds it, which takes it back only if it reads as released or expired.

It also checks every second whether its leader has stopped, and ends a renewal whose read or write hangs once it has. If Lead fails, or the office is lost while it runs, the loop leaves office, closing any leader Lead returns; it releases the lease after a failed Lead.

When ctx ends, a leader stops reporting itself in State, runs Drain, closes the leader and then releases the lease, unless the leader had stopped. Run returns the error of the release it makes when ctx ends with the lease in hand: in that shutdown, after a Lead that failed, or after a candidacy that took office as ctx ended. It returns nil otherwise.

func (*Loop) State

func (l *Loop) State() State

State returns the loop's current state.

type State

type State struct {
	// Leader serves the database while this node leads it, and is nil otherwise.
	// A leader that has stopped (its Err is set) stays here until the
	// loop notices, within about a second.
	Leader *engine.Leader
	// Follower serves snapshots while this node stands by, if enabled.
	Follower *engine.Follower
	// Office is the office Leader holds, and zero while Leader is nil.
	Office engine.Office
	// Reason is why the loop last stopped leading, or last failed to start
	// a leader after taking office; nil if neither has happened. It wraps
	// engine.ErrStopped, ErrDeposed, ErrUnchecked or ErrShutdown, or is the
	// error of engine.Lead.
	Reason error
	// Changed is closed when the state changes.
	Changed <-chan struct{}
}

State is a loop's leadership at one moment.

Jump to

Keyboard shortcuts

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