diff --git a/.github/workflows/genesis-openembedded.yml b/.github/workflows/genesis-openembedded.yml index d29a9b5..8ce2aa5 100644 --- a/.github/workflows/genesis-openembedded.yml +++ b/.github/workflows/genesis-openembedded.yml @@ -86,6 +86,7 @@ jobs: prove -v t/net_dns_rr_types.t prove -v t/repo-lock-race.t prove -v t/nfslock.t + prove -v t/nfslock_borrow_recovery.t prove -v t/genesis_oe_source_dir.t prove -v -It/lib t/genesis_openembedded_release.t sudo -E prove -v -It/lib t/genesis_openembedded_consumer.t diff --git a/lib/XCAT/NFSLock.pm b/lib/XCAT/NFSLock.pm index ed2eff2..b2f6511 100644 --- a/lib/XCAT/NFSLock.pm +++ b/lib/XCAT/NFSLock.pm @@ -2,6 +2,16 @@ package XCAT::NFSLock; # NFS lock protocol # +# Copies +# This module is vendored three times, because no repo installs another's build tooling: +# xcat-dep lib/XCAT/NFSLock.pm (the original) +# xcat-core build-utils/lib/XCAT/NFSLock.pm +# xcat-core-ci-cd ci/lib/XCAT/NFSLock.pm +# The three must stay identical. They have diverged once already: sweep_stranded was added to +# the ci-cd copy alone, so the protocol hole it covers stayed open in the other two. Its only +# caller is still in xcat-core-ci-cd (ci/sweep-stranded-locks.pl); it stays in all three copies +# because a copy that drops it reopens that hole. +# # A lock on a shared, possibly re-exported, NFS tree. flock and fcntl are not # available there. The protocol uses mkdir, rmdir, unlink and plain file # writes. It uses no rename. @@ -9,9 +19,45 @@ package XCAT::NFSLock; # Names in this module # lock.d the lock path given to acquire # lock.d/metadata the metadata of the owner -# lock.borrow .borrow, beside lock.d +# lock.d/workers/ one empty file per attached worker, named . +# lock.borrow .borrow, beside lock.d: borrow generation 0 +# lock.borrow.N borrow generation N, N >= 1 # R, T, δ the options retries, delay and jitter # +# Workers +# The owner of a lock is one process, and what a lock protects usually outlives it. A build +# wrapper starts each command in its OWN process group, so SIGKILL on the wrapper leaves that +# group running: an owner proven dead says nothing about the work it started, and a take-over +# then puts two builds in one checkout. +# +# A caller therefore ATTACHES each process group it starts, and DETACHES it once reaped. +# attach records lock.d/workers/.. An owner is recoverable +# only when it is provably dead AND every attached group is gone. +# +# A group is provably gone when kill(0, -pgid) answers ESRCH, which is the test the build +# wrapper's own cancellation path uses to decide it may release. detach applies the same test, +# so the retention is a property of what is on disk and not of who called what. EPERM, or success, means some +# process is in the group, and then the lock is not recoverable. This is deliberately +# conservative: a recycled pgid reads as live and costs a wait, where the other error admits a +# second writer. +# +# Reuse windows. The entry name carries the leader's start time, so removing an entry by name +# cannot remove the record of a LATER group that reused the pgid. The group test itself cannot +# tell a recycled pgid from the original group, and answers live for both. What neither can +# tell is a worker that left its group by calling setpgid itself; the wrapper's cancellation +# path has that same limit, and the two now agree rather than contradict each other. +# +# Borrow generations +# The borrow is the mutex for an ownership transition. The one safe way to end a transition its +# own taker did not end is to SUPERSEDE it, never to remove it. rmdir names a path and not the +# inode that was validated, so a reaper that validates a borrow and then removes it deletes +# whatever is at that path when it runs, which can be a live taker's borrow. +# +# The taker is the process holding the HIGHEST generation present. A contender supersedes +# generation N by mkdir(lock.borrow.), and only after N's metadata proves its taker dead. +# mkdir is exclusive, so exactly one contender wins a generation. A process removes only a +# generation it created and still holds, so no process can delete another process's claim. +# # Assumptions # 1. mkdir(path) is atomic and exclusive among contenders, and successful # namespace changes eventually become visible. @@ -19,11 +65,18 @@ package XCAT::NFSLock; # Cloned VMs and images can share it by accident unless it is regenerated. # 3. Metadata writes eventually become readable completely and consistently. # 4. A host never declares one of its own live process incarnations dead. -# 5. A process cannot die: -# - after it creates lock.d, until it publishes valid metadata; -# - while it holds lock.borrow, until it removes it. +# 5. A process MAY die at any point, a borrow generation included. The borrow carries the same +# metadata as the lock, so a generation whose taker is provably dead is superseded by the +# next generation. +# A generation with no valid metadata is a taker that died, or paused, between its mkdir and +# its first write. Nothing can prove that taker dead, so it blocks every later acquire, and +# only sweep_stranded removes it, under an exclusive claim on the root. +# A lock.d with no valid metadata blocks the same way, for the same reason. The build lock in +# xcat-core wrote no metadata and held lock.d for the whole build, so no fact on disk tells +# that holder from a dead one. # 6. A crashed worker leaves recoverable state. The owning host eventually # returns and retries. Eventually one worker and its release complete. +# An attached process group that outlives its owner holds the lock until the group is gone. # 7. All participants follow the protocol. # # Metadata @@ -36,7 +89,8 @@ package XCAT::NFSLock; # # If the metadata is missing, cannot be parsed or hashed, or the hash does not # match, assume a partial or inconsistent read and retry. Never infer stale -# ownership from invalid metadata. +# ownership from invalid metadata. A directory unchanged for some time is not evidence either: +# a live participant can pause for any length of time between two writes. # # Retry # R = max retries, T = base delay, δ = jitter @@ -57,18 +111,25 @@ package XCAT::NFSLock; # - invalid or missing: retry # - different machine-id: retry # - same host: save the observed ownership identity -# 3. mkdir lock.borrow -# - failure: retry +# 3. Take the borrow, then write valid metadata into it. Taking it is mkdir of generation 0 +# when no generation exists, and otherwise mkdir of the generation after the highest one +# whose metadata proves its taker dead. +# - the highest generation has no valid metadata, names another machine, or names a live +# process: retry +# - another contender won that generation: retry +# - other failure: retry # 4. Read and validate the metadata again. # - invalid, missing, or ownership identity changed: rmdir lock.borrow, retry -# 5. Prove that the recorded (boot-id, pid, pstart) is dead. -# - not provably dead: rmdir lock.borrow, retry +# 5. Prove that the recorded (boot-id, pid, pstart) is dead, and that every group in +# lock.d/workers is gone. +# - the owner is not provably dead, or any attached group still exists: give up the borrow, +# retry # - dead: continue # 6. Replace the metadata with the identity of this process and a fresh token. # 7. rmdir lock.borrow # 8. Call the worker. -# 9. mkdir lock.borrow -# - failure: sleep(T + rand(-δ, δ)), retry step 9 +# 9. Take the borrow as in step 3, then write valid metadata into it +# - not available: sleep(T + rand(-δ, δ)), retry step 9, R times at most, then give up # 10. unlink lock.d/metadata # 11. rmdir lock.d (the actual unlock) # 12. rmdir lock.borrow @@ -76,9 +137,12 @@ package XCAT::NFSLock; # Core invariants # lock.d exists => locked # lock.d absent => acquirable +# an attached group => locked, whatever the owner's state # invalid metadata => retry only # different machine-id => never recover here -# lock.borrow exists => ownership transition or release in progress +# a borrow generation => a transition or a release is in progress, unless the highest +# generation's metadata proves its taker dead +# removal => a process removes only a generation it created and still holds # # Steps 1 to 7 are acquire, step 8 is the caller, steps 9 to 12 are release. # release does steps 10 and 11 only when the metadata names this acquisition. @@ -86,21 +150,25 @@ package XCAT::NFSLock; # Log # Unless quiet => 1, each lock event prints one line to the selected output handle: # [nfslock] pid=