The checkout lock, _build/juggernaut/lock (design notes §7.1): one mutation run per
checkout.
The lock holds {"format", "run_id", "pid", "os_pid", "host", "started_at", "heartbeat_ms"}: the holder's host name and the interval at which it refreshes the
file's modification time (its heartbeat, heartbeat_ms/0). It is
never visible half-written: the record is written to a temporary file, which is then
hard-linked to lock. Linking fails when lock exists, so of two concurrent acquirers
exactly one creates it. A held lock is an infrastructure error (exit 3) naming the
holder. A file system without hard links is an infrastructure error too.
A lock is stale, and is replaced, when:
- it was taken on this host and its OS process is gone; or
- it was taken on another host (for example another
bin/devcontainer, whose process ids this one cannot see) and its last heartbeat is older than twice the interval its record states (one missed beat is tolerated: a slow file system, a busy scheduler); a record without one, from an older Juggernaut, after 5 minutes. A holder callsrefresh/1everyheartbeat_ms/0, so a live holder never looks gone; or - it cannot be read as a lock record and was last modified at least
unreadable_after_msago (default 2 seconds), so a lock still being written by another tool is not mistaken for an abandoned one.
Replacing a stale lock is a compare-and-swap. The acquirer first claims the replacement
of that exact stale record by hard-linking its own record to lock.replace-<hash of the stale record>; of several acquirers that judged the same record stale, only one
succeeds. The winner checks that lock still holds the stale record and renames its
own record over it; the others try again and find the new holder. A replacement claim
left behind by a run that crashed is cleared once it is unreadable_after_ms old.
Whenever a lock is taken over, replaced carries a "takeover" message saying whose
lock it was and why its holder is known to be gone, for the caller to show. With
force: true (--force-unlock) any lock is removed first, whoever holds it: for a
holder the rules above cannot prove gone, such as a container removed with
docker rm -f less than two heartbeats ago.
Summary
Functions
Takes the lock for run_id. Options: :force (remove any existing lock first, see the
module doc); for tests :os_pid, :host, :alive? (fn os_pid -> boolean end),
:now (a DateTime), :heartbeat_ms (the interval this holder states),
:unreadable_after_ms.
How often a holder refreshes the lock (its heartbeat), in milliseconds.
Whether an OS process with this pid exists on this host.
Marks the lock as still in use (updates its modification time) if this holder still
owns it. A lock that is gone or that another run now holds is left alone: refresh/1
never creates the file, so a refresh timer that races release/1 cannot leave an
empty lock behind, nor keep another run's lock looking fresh.
Releases the lock if this holder still owns it; otherwise leaves it alone.
The message to show for a lock that acquire/3 took over (lock.replaced), or nil
when none was.
Types
Functions
@spec acquire(Path.t(), String.t(), keyword()) :: {:ok, t()} | {:error, Juggernaut.Error.t()}
Takes the lock for run_id. Options: :force (remove any existing lock first, see the
module doc); for tests :os_pid, :host, :alive? (fn os_pid -> boolean end),
:now (a DateTime), :heartbeat_ms (the interval this holder states),
:unreadable_after_ms.
Returns the lock; replaced holds the stale holder that was replaced (or
%{"unreadable" => true} for an unreadable lock file), so the caller can warn about it.
root must be a prepared store (Layout.check/1).
@spec heartbeat_ms() :: pos_integer()
How often a holder refreshes the lock (its heartbeat), in milliseconds.
Whether an OS process with this pid exists on this host.
@spec refresh(t()) :: :ok
Marks the lock as still in use (updates its modification time) if this holder still
owns it. A lock that is gone or that another run now holds is left alone: refresh/1
never creates the file, so a refresh timer that races release/1 cannot leave an
empty lock behind, nor keep another run's lock looking fresh.
@spec release(t()) :: :ok
Releases the lock if this holder still owns it; otherwise leaves it alone.
The message to show for a lock that acquire/3 took over (lock.replaced), or nil
when none was.