Documentation
¶
Overview ¶
Package lease implements the primary-lease runnable for the instance manager. The lease is a Kubernetes coordination.k8s.io/v1 Lease object named after the cluster. Whoever holds the lease is allowed to run as PostgreSQL primary.
Index ¶
- type Config
- type PingError
- type Runnable
- func (r *Runnable) Acquire(ctx context.Context, config Config) error
- func (r *Runnable) MarkPromoting(ctx context.Context) error
- func (r *Runnable) Position(ctx context.Context) (string, error)
- func (r *Runnable) Release(ctx context.Context) error
- func (r *Runnable) SetMonitorContext(ctx context.Context)
- func (r *Runnable) Start(ctx context.Context) error
- func (r *Runnable) Watchdog() *watchdog.LeaseWatchdog
- func (r *Runnable) Yield(ctx context.Context)
- type SupersededError
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// LeaseDuration is how long a lease is valid before it expires.
LeaseDuration time.Duration
// RenewDeadline is how long the holder tries to renew before giving up.
RenewDeadline time.Duration
// RetryPeriod is how frequently a non-holder retries acquiring the lease.
RetryPeriod time.Duration
// ReleasedLeaseDuration is the TTL written when explicitly releasing the lease.
ReleasedLeaseDuration time.Duration
}
Config holds the tunable timings of the primary lease. They mirror the underlying Kubernetes leader-election parameters and are sourced from the Cluster's `.spec.primaryLease` stanza (falling back to the defaults above when unset).
type PingError ¶
type PingError struct {
Host string `json:"host"`
IP string `json:"ip"`
RequestTimeout int `json:"requestTimeout"`
ConnectionTimeout int `json:"connectionTimeout"`
Err error `json:"error"`
}
PingError is raised when the instance connectivity test failed. It is exported with public, JSON-tagged fields so it can be logged as a structured value.
type Runnable ¶
type Runnable struct {
// contains filtered or unexported fields
}
Runnable manages the primary lease for this instance. It starts idle and enters the acquisition/renewal loop only after Acquire is called.
func New ¶
func New( kubeClient kubernetes.Interface, instance *postgres.Instance, ) *Runnable
New creates a new Runnable.
func (*Runnable) Acquire ¶
Acquire signals the runnable to start competing for the lease using the provided configuration, then blocks until the lease is held or ctx is cancelled. The configuration is only applied by the first call; subsequent calls reuse the timings captured at activation time.
func (*Runnable) MarkPromoting ¶
MarkPromoting marks on the primary lease, before this instance promotes, that it is about to. It must run before pg_ctl promote.
Until the first renewal after the promotion, the annotation is the one of the previous primary: a final position on a timeline that this instance is about to leave behind. If this instance is lost in that window, the next replica would find a clean shutdown that never happened. The renewals leave the marker alone while the instance is in recovery, and replace it as soon as it is a primary.
The marker keeps the final position it replaces, if any, so that the next replica can still check that it has all the data of that primary.
This is a write of its own, not one of the elector: there is no renewal to wait for, and the lock is not safe to share between goroutines. A renewal that conflicts with this write reads the lease again and retries.
func (*Runnable) Position ¶
Position returns the position annotation of the primary lease, empty if there is none. It fails unless this pod holds the lease: only the holder writes the annotation, so the value is what the previous holder left.
func (*Runnable) Release ¶
Release explicitly releases the lease so that a replica can promote without waiting for the TTL to expire. It is a no-op if this pod is not the current holder (including if the lease does not exist yet). Callers must pass a fresh (non-cancelled) context; the previous run context is already cancelled by the time this is called. When invoked from the defer in cmd.go, controller-runtime has already waited for all runnables (including PostgresLifecycle) to finish, so PostgreSQL is down and releasing the lease lets a replica promote at once.
An in-place instance-manager online upgrade is the exception: PostgresLifecycle keeps PostgreSQL running while InstanceManagerIsUpgrading is set, so this instance is still the primary. Releasing then would blank the holder while we keep serving writes, opening a window for a replica to promote. We keep the lease held instead; the Identity is the pod name, which the replacement instance manager reuses, so it re-adopts the lease with no hand-over and no free-lease window.
This guard is reliable on a successful upgrade because reloadInstanceManager replaces the process with syscall.Exec, which discards the deferred reset of InstanceManagerIsUpgrading: the flag therefore stays set whenever this defer can still run. Only a failed exec resets it, and there the operator's online-upgrade failover delay covers the pod restart.
func (*Runnable) SetMonitorContext ¶
SetMonitorContext makes the step-down monitor run until ctx is done, instead of until the runnable's own context is. ctx must end when the process is about to exit, as the lifecycle manager's global context does. It must be called before the runnable starts.
func (*Runnable) Start ¶
Start implements controller-runtime's Runnable interface. The runnable times the lease record while a failover is pending until Acquire is called, then enters the lease acquisition/renewal loop.
func (*Runnable) Watchdog ¶
func (r *Runnable) Watchdog() *watchdog.LeaseWatchdog
Watchdog returns the heartbeat the liveness probe reads.
func (*Runnable) Yield ¶
Yield stops renewing the lease without stopping PostgreSQL, so that a pod that was named target primary and then replaced before it promoted does not keep the new target from taking the lease. The record is not blanked: it keeps naming this pod and the new target takes it over once it has seen it unchanged for a full LeaseDuration, as for any holder that stopped renewing. At most one write already in flight can still land after this returns. The empty-holder fast path stays reserved for a primary that stopped cleanly.
The caller decides when yielding is safe: the instance reconciler calls it from the goroutine that promotes, only for a pod whose PostgreSQL is in recovery and was never asked to promote. The next Acquire undoes it.
type SupersededError ¶
type SupersededError struct {
Host string `json:"host"`
TargetPrimary string `json:"targetPrimary"`
OurIdentity string `json:"ourIdentity"`
}
SupersededError is raised when a reachable peer reports a target primary other than ours: we have already been superseded, regardless of whether we can still reach every peer. It is exported with public, JSON-tagged fields so it can be logged as a structured value.
func (*SupersededError) Error ¶
func (e *SupersededError) Error() string
Error implements the error interface