replicas

package
v2.2.1 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2026 License: AGPL-3.0 Imports: 5 Imported by: 0

Documentation

Overview

Package replicas is how code anywhere in Studio (core or Enterprise) cooperates with the other control-plane replicas sharing its database, without being handed the cluster machinery:

  • IsLeader: whether this replica should run singleton background work (aggregations, alerts, syncs, reports, cleanups). One replica holds the leader lease at a time (pkg/cluster.Leadership).
  • OnLeading: run something when this replica becomes the leader, such as start-up work that was skipped because another replica (or this one's dead predecessor) still held the lease.
  • Signal / OnSignal: tell every other replica that some in-memory state derived from the database is stale ("budgets", "governed_metadata", ...). The replica that changed the data refreshes its own state itself; the signal reaches the others through the cluster event log, at least once, within about a second.

Studio runs one instance per process, so this is process-wide. Until pkg/studio connects a backend (and in tests), this process is the only replica: IsLeader is true and signals go nowhere.

Index

Constants

View Source
const (
	// SignalBudgets: budget limits, periods or the team-budget switch
	// changed; every replica clears its budget caches.
	SignalBudgets = "budgets"
	// SignalGovernedMetadata: metadata schemas or vocabularies changed;
	// every replica drops its resolved-schema cache (Enterprise).
	SignalGovernedMetadata = "governed_metadata"
)

Signal names shared across packages.

Variables

This section is empty.

Functions

func BecameLeader

func BecameLeader()

BecameLeader runs the OnLeading handlers (called by the backend when this replica gains the leader lease).

func Deliver

func Deliver(name string)

Deliver runs the handlers for a signal received from another replica (called by the backend).

func IsLeader

func IsLeader() bool

IsLeader reports whether this replica runs singleton background work.

func OnLeading

func OnLeading(fn func()) (remove func())

OnLeading registers fn to run each time this replica becomes the leader. Leader-only work that checks IsLeader on a timer uses it to catch up at once instead of waiting a whole interval: a replica restarted after a crash may only get the lease a moment after its start-up run was skipped. Register before checking IsLeader, so no change falls between the two. fn runs on the lease's goroutine: it must be quick (wake a loop). The returned function removes it.

func OnSignal

func OnSignal(name string, fn func()) (remove func())

OnSignal registers fn to run when another replica signals name. fn runs on the cluster log's reader goroutine: it must be quick (clear a cache, trigger a background reload). The returned function removes it.

func SetBackend

func SetBackend(b Backend)

SetBackend connects the cluster; nil disconnects it.

func Signal

func Signal(ctx context.Context, name string)

Signal tells the other replicas that the state called name is stale. It does not call this replica's own handlers: the caller has refreshed its state already. Failures are logged; the other replicas then refresh on their own schedule (TTL) if they have one, or serve stale state.

Types

type Backend

type Backend interface {
	IsLeader() bool
	// Signal publishes name to the other replicas.
	Signal(ctx context.Context, name string) error
}

Backend is the cluster behind the package (pkg/studio provides it).

Jump to

Keyboard shortcuts

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