Documentation
¶
Overview ¶
Package litefs provides utilities for detecting the role of the current node in a LiteFS cluster (primary vs replica).
Index ¶
Constants ¶
const PrimaryFile = "/litefs/.primary"
PrimaryFile is the standard LiteFS lease file path. This file exists ONLY on replica nodes and contains the primary's hostname. Its ABSENCE means this node IS the primary.
The semantics are inverted from what the filename suggests:
- .primary file EXISTS -> this node is a REPLICA (file contains the primary's address)
- .primary file ABSENT -> this node IS the PRIMARY (we hold the lease)
Variables ¶
This section is empty.
Functions ¶
func IsPrimaryAt ¶
IsPrimaryAt reports whether this node is the LiteFS primary by checking the given path for the lease file. Returns true when the file does NOT exist (absence = primary), false when it does exist (presence = replica).
This is a low-level primitive with no fallback handling; it is exported so the package's external test can exercise the lease-file semantics against arbitrary paths. Production code calls IsPrimaryWithFallback, which adds the fail-safe-to-replica path for ambiguous stat errors and the no-LiteFS env-var fallback.
func IsPrimaryWithFallback ¶
IsPrimaryWithFallback detects primary status with a fallback for environments where LiteFS is not running (e.g., local development).
Detection order:
- If the primary file at path exists, this is a replica (return false).
- If stat fails with anything other than "does not exist" (e.g. a flaky LiteFS FUSE mount returning EIO/EACCES/ENOTDIR), fail SAFE toward replica (return false) and log a WARN. A misclassified replica that declines primary duties is harmless; a misclassified primary runs destructive Schema.Create DDL (WithDropColumn/WithDropIndex).
- If the file is genuinely absent and the parent directory of path exists (LiteFS is mounted), this is the primary (return true).
- If neither exists (no LiteFS), fall back to the environment variable identified by envKey parsed as a boolean. Defaults to true if the env var is unset, matching the common local-dev expectation (single node = primary).
func ValidateEnvFallback ¶ added in v1.23.0
ValidateEnvFallback checks that the environment fallback variable, if set, parses as a boolean. Call once at startup so an operator typo in PDBPLUS_IS_PRIMARY fails fast with a clear message instead of being silently coerced into a role at every scheduler tick. Unset is valid (the documented "default primary" local-dev behaviour).
Types ¶
This section is empty.