statestore

package
v0.44.0 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2026 License: Apache-2.0 Imports: 14 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 statestore keeps the desired state of plexd's last successful state pull in the data directory, so that a node restarting while the control plane is unreachable can apply it before its first pull.

The stored set is what a running node holds for its tunnels and firewall: the peers, the edge-PSK envelopes as the control plane served them (sealed under the node secret key), the compiled policy, the bridge block with the secret inventory its parts take their Secrets' versions from (names and versions, never a value), and the shared_subnets, failure_detection and area blocks peers are programmed from. The dns block, the delivery queues (executions, sessions, hooks) and the state and reports blocks, which the node API cache persists on its own, are not stored.

Index

Constants

View Source
const (
	// FileName is the stored state's file in the data directory.
	FileName = "desired-state.json"
	// MarkerName is the file that marks a restore in progress.
	MarkerName = "desired-state.restoring"
	// Version is the version of File this binary writes and restores.
	Version = 1
)

Variables

View Source
var (
	// ErrNoState is returned by Load when no state is stored.
	ErrNoState = errors.New("statestore: no stored state")
	// ErrInterrupted is returned by Load when the previous restore did not
	// complete: the process died while it applied the stored state, which
	// may well be the cause, so Load removes the file and the marker.
	ErrInterrupted = errors.New("statestore: the previous restore did not complete")
	// ErrForeign is returned by Load when the stored state names another
	// node: it was written under an identity the node no longer holds.
	ErrForeign = errors.New("statestore: stored state belongs to another node")
)

Functions

This section is empty.

Types

type File

type File struct {
	// Version is Version. A file of another version is not restored, and the
	// next successful pull overwrites it.
	Version int `json:"version"`
	// NodeID is the node the state was pulled for.
	NodeID string `json:"node_id"`
	// SavedAt is when the file was written, in UTC. Under a maximum age the
	// file is written on every successful pull, so it is the time of the
	// last one.
	SavedAt time.Time `json:"saved_at"`
	// MaxAgeSeconds is the maximum age the control plane set, as
	// reconcile.Reconciler.MaxAgeSeconds returns it; 0 for none.
	MaxAgeSeconds int64               `json:"max_age_seconds,omitempty"`
	Peers         []api.SnapshotPeer  `json:"peers"`
	Policy        *api.PolicySnapshot `json:"policy"`
	Bridge        *api.BridgeSnapshot `json:"bridge"`
	// Secrets is the pull's secret inventory, names and versions only, which
	// the bridge's ingress, user access and site-to-site managers take before
	// a restore applies the bridge block, so a part whose Secret the control
	// plane can serve comes up at once. Left out while no pull carried one.
	Secrets          *[]api.NodeStateSecret      `json:"secrets,omitempty"`
	SharedSubnets    []api.NodeStateSharedSubnet `json:"shared_subnets"`
	FailureDetection json.RawMessage             `json:"failure_detection,omitempty"`
	EdgePSKs         []wireguard.SealedEdgePSK   `json:"edge_psks,omitempty"`
	// Area is the pull's area block. Null is a state here, not "keep what
	// you hold": a pull with a nil block writes no member, and nothing
	// carries an earlier block over. A file without the member, such as one
	// an earlier release wrote, restores no Area route.
	Area *api.NodeStateArea `json:"area,omitempty"`
}

File is the stored state, the JSON document in FileName.

type Store

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

Store reads and writes the stored state of one node. Load, BeginRestore and EndRestore run before the reconciler does, on the goroutine that then runs it, and the observer runs on the reconcile goroutine, so a Store needs no lock.

func New

func New(dataDir, nodeID string, logger *slog.Logger) *Store

New returns the Store of the node nodeID under dataDir.

func (*Store) BeginRestore

func (s *Store) BeginRestore() error

BeginRestore writes the restore marker, an empty file, before the stored state is applied. A start that finds it knows the previous restore did not complete (see Load).

func (*Store) EndRestore

func (s *Store) EndRestore() error

EndRestore removes the restore marker once the stored state is applied.

func (*Store) Load

func (s *Store) Load() (*File, error)

Load reads the stored state. It returns ErrNoState when there is none, ErrInterrupted when the restore marker exists, after removing the file and the marker, and ErrForeign when the file names another node. Any read, decode or version error is returned wrapped as "statestore: load: ...". Only ErrInterrupted removes anything. A loaded file becomes the Store's record of what is on disk.

func (*Store) Observer

func (s *Store) Observer(sealed func() []wireguard.SealedEdgePSK, maxAgeSeconds func() int64) reconcile.DispatchHandler

Observer returns the reconciler's cycle observer (see reconcile.Reconciler.SetCycleObserver), which stores each successful pull. It builds a File from the pull, the edge-PSK envelopes sealed returns (a nil sealed stores none) and the maximum age maxAgeSeconds returns. A pull whose failure_detection is absent or null carries the stored block over, as the WireGuard manager keeps the value it holds, and so does a pull whose secrets block is null, as the bridge managers keep the inventory they hold. The area block is stored as the pull carries it: its null is a state, so a pull without it stores none, whatever the file held before.

It writes the file, mode 0600, when the content, compared without saved_at, differs from what is on disk, and on every pull while maxAgeSeconds is above 0, so that under a limit saved_at is the time of the last successful pull. A failed write logs stored state not written at Warn once per run of failures and stored state written again at Info when one succeeds; it never fails the cycle, and the next pull tries again.

Jump to

Keyboard shortcuts

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