diff --git a/build-utils/lib/XCAT/BuildUtils.pm b/build-utils/lib/XCAT/BuildUtils.pm index 10172d37d1..dfa9834289 100644 --- a/build-utils/lib/XCAT/BuildUtils.pm +++ b/build-utils/lib/XCAT/BuildUtils.pm @@ -667,9 +667,8 @@ sub acquire_lock_dir { $what = $lockdir unless defined $what; $held = '' unless defined $held; - # Three attempts: reaping a stranded borrow takes two, because a borrow with no metadata is - # reclaimed only when two of them find it unchanged, and the take-over takes the third. - # Builds must fail fast rather than queue, so the budget stops there. + # Three attempts. A take-over needs one; the budget leaves two for a contended lock, where + # another build is superseding the same dead owner. Builds must fail fast rather than queue. my $lock = eval { XCAT::NFSLock->acquire($lockdir, retries => 2, label => $what, quiet => 1); }; diff --git a/build-utils/lib/XCAT/NFSLock.pm b/build-utils/lib/XCAT/NFSLock.pm index e55d4dad30..fc83c5c2a6 100644 --- a/build-utils/lib/XCAT/NFSLock.pm +++ b/build-utils/lib/XCAT/NFSLock.pm @@ -19,9 +19,21 @@ 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.borrow .borrow, beside lock.d: borrow generation 0 +# lock.borrow.N borrow generation N, N >= 1 # R, T, δ the options retries, delay and jitter # +# 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. @@ -29,15 +41,15 @@ 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 MAY die at any point, lock.borrow included. The borrow carries the same -# metadata as the lock, so a borrow whose taker is provably dead is recovered by the same -# rule. A borrow with NO metadata is left by the earlier protocol, or by a death between -# mkdir and the first write; no version of this protocol holds one for more than -# milliseconds, so one that two attempts find unchanged is held by nobody. -# A lock.d with no metadata is a different case and acquire never recovers it. 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. Only sweep_stranded removes those, and only when -# the caller claims the root exclusively. +# 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. # 7. All participants follow the protocol. @@ -52,11 +64,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. -# -# One case is not a partial read: a lock.borrow with no metadata FILE, unchanged over two -# attempts. Two attempts of one acquire or release are at least T seconds apart, and no -# participant holds a metadata-less borrow that long, so nobody holds it. +# 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 @@ -77,9 +86,12 @@ package XCAT::NFSLock; # - invalid or missing: retry # - different machine-id: retry # - same host: save the observed ownership identity -# 3. mkdir lock.borrow, then write valid metadata into it -# - EEXIST: recover the borrow if its metadata proves its taker dead, or if it has no -# metadata file and two attempts found it unchanged; then 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 @@ -89,9 +101,8 @@ package XCAT::NFSLock; # 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, then write valid metadata into it -# - EEXIST: recover the borrow by the same rule as step 3 -# - 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 @@ -101,9 +112,9 @@ package XCAT::NFSLock; # lock.d absent => acquirable # invalid metadata => retry only # different machine-id => never recover here -# lock.borrow exists => ownership transition or release in progress, unless its metadata -# proves its taker dead, or it has no metadata file and two attempts -# found it unchanged +# 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. @@ -111,8 +122,9 @@ package XCAT::NFSLock; # Log # Unless quiet => 1, each lock event prints one line to the selected output handle: # [nfslock] pid=