diff --git a/lib/XCAT/NFSLock.pm b/lib/XCAT/NFSLock.pm index fc83c5c..e1d0831 100644 --- a/lib/XCAT/NFSLock.pm +++ b/lib/XCAT/NFSLock.pm @@ -19,10 +19,34 @@ package XCAT::NFSLock; # Names in this module # lock.d the lock path given to acquire # lock.d/metadata the metadata of the owner +# 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 @@ -52,6 +76,7 @@ package XCAT::NFSLock; # 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 @@ -95,8 +120,10 @@ package XCAT::NFSLock; # - 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 @@ -110,6 +137,7 @@ 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 # a borrow generation => a transition or a release is in progress, unless the highest @@ -132,6 +160,7 @@ use Cwd qw(abs_path); use File::Find (); use Digest::SHA qw(sha256_hex); use Errno qw(EEXIST ENOENT); +use Fcntl qw(O_CREAT O_EXCL O_WRONLY); use Exporter 'import'; use File::Basename qw(dirname basename); use POSIX qw(strftime); @@ -139,7 +168,7 @@ use Sys::Hostname qw(hostname); use Time::HiRes (); our @EXPORT_OK = qw(this_process format_metadata parse_metadata owner_is_dead process_start - sweep_stranded); + sweep_stranded live_workers); my @IDENTITY = qw(machine-id boot-id pid pstart token); @@ -200,6 +229,7 @@ sub acquire { }, $class; my $here = _here(); my $seen; + my $workers; for (my $count = 0 ; ; $count++) { # Step 1. @@ -248,6 +278,7 @@ sub acquire { _drop_borrow($borrow); die $error unless defined($taken); if ($taken) { + _drop_dead_workers($abs); $self->_log('took-over', 'from dead pid ' . $observed->{pid}); return $self; } @@ -260,7 +291,8 @@ sub acquire { } my $who = _describe($seen); - my $s = $retries == 1 ? 'retry' : 'retries'; + $who .= ", retained for surviving build workers in process group $workers" if defined($workers); + my $s = $retries == 1 ? 'retry' : 'retries'; # Names what happened, so a caller can tell a lock another process still holds from a mkdir # that failed outright. The earlier wording said "Trying to unlock", which is the reverse. die "Cannot lock $abs: still held after $retries $s by $who.\n" @@ -305,6 +337,10 @@ sub release { if (defined($current) && _key($current) eq _key($self->{identity})) { $self->{released} = 1; unlink("$path/metadata"); + # A group that died without a detach leaves its record; drop those. A LIVE one + # is kept, and rmdir then refuses the non-empty directory, so the release fails + # rather than freeing a lock whose workers still run. + _drop_dead_workers($path); my $removed = rmdir($path); my $error = $!; _drop_borrow($borrow); @@ -624,6 +660,140 @@ sub _absolute { #-------------------------------------------------------------------------------- +=head3 attach + + Descriptions: + Record a process group this lock holder started, so the lock is not recoverable while + any process is still in that group. + + An owner is one process and the work it starts is not. A build wrapper runs each command + in its own process group, so SIGKILL on the wrapper leaves that group running, and an + owner proven dead says nothing about the build it started. + Arguments: + $pgid: the process group id, which is the pid of the group leader + Returns: + 1 when the group is recorded, 0 when it names no process or cannot be recorded. + +=cut + +#-------------------------------------------------------------------------------- +sub attach { + my ($self, $pgid) = @_; + return 0 unless defined($pgid) && $pgid =~ /\A[1-9][0-9]*\z/; + # The leader's start time pins the record, so a later group that reuses the pgid gets its own + # entry and cannot be removed by a detach of this one. + my $pstart = process_start($pgid); + return 0 unless defined($pstart); + my $dir = _worker_dir($self->{path}); + unless (mkdir($dir) || $! == EEXIST) { + warn "Cannot create $dir: $!\n"; + return 0; + } + my $file = "$dir/$pgid.$pstart"; + unless (sysopen(my $fh, $file, O_WRONLY | O_CREAT | O_EXCL) || $! == EEXIST) { + warn "Cannot record build worker $pgid: $!\n"; + return 0; + } + $self->{workers}{$pgid} = $pstart; + return 1; +} + +#-------------------------------------------------------------------------------- + +=head3 detach + + Descriptions: + Remove the record of a process group this holder started. Call it once the group is + reaped. + + It removes nothing while any process is still in the group. Reaping the group LEADER does + not end the group: its children inherit it and can outlive it. Refusing here makes the + retention a property of what is on disk rather than of who remembered to call this, so a + caller that detaches too early cannot free a lock whose workers still run. + Arguments: + $pgid: the process group id + Returns: + the number of records removed. + +=cut + +#-------------------------------------------------------------------------------- +sub detach { + my ($self, $pgid) = @_; + return 0 unless defined($pgid) && $pgid =~ /\A[1-9][0-9]*\z/; + delete($self->{workers}{$pgid}); + my $dir = _worker_dir($self->{path}); + opendir(my $dh, $dir) or return 0; + my @mine = grep { /\A\Q$pgid\E\.[0-9]+\z/ } readdir($dh); + closedir($dh); + my $removed = 0; + $removed += unlink("$dir/$_") for @mine; + rmdir($dir); + return $removed; +} + +#-------------------------------------------------------------------------------- + +=head3 live_workers + + Descriptions: + The attached process groups of the lock at $path that still have a process in them. + + kill(0, -pgid) answering ESRCH is the only answer that proves a group gone, and it is the + test the build wrapper's own cancellation path uses before it releases. Success and EPERM + both mean some process is in the group. A recycled pgid therefore reads as live, which + costs a wait; the opposite error admits a second writer. + Arguments: + $path: the lock path + Returns: + A list of hash refs: pgid, pstart, entry. Empty when every attached group is gone. + +=cut + +#-------------------------------------------------------------------------------- +sub live_workers { + my ($path) = @_; + opendir(my $dh, _worker_dir($path)) or return (); + my @live; + while (defined(my $entry = readdir($dh))) { + next unless $entry =~ /\A([1-9][0-9]*)\.([0-9]+)\z/; + my ($pgid, $pstart) = ($1, $2); + next if !kill(0, -$pgid) && $!{ESRCH}; + push @live, { pgid => $pgid, pstart => $pstart, entry => $entry }; + } + closedir($dh); + return @live; +} + +sub _worker_dir { return "$_[0]/workers" } + +# _drop_dead_workers: remove the records of groups that are gone, and leave the rest. +# +# A group that died without a detach leaves its record behind -- the detach runs in the process +# that reaped the group, and a process killed before it reaps nothing. Without this a holder +# could never release its own lock, because rmdir refuses a non-empty directory. A live record +# is kept for that reason: it is what stops the release, and what a later acquire reads. +# +# Removing an entry by name is safe. The name carries the leader's start time, so only the group +# that was recorded can own that name, and a group that is gone creates nothing. +sub _drop_dead_workers { + my ($path) = @_; + my $dir = _worker_dir($path); + opendir(my $dh, $dir) or return 0; + my @entry = grep { /\A[1-9][0-9]*\.[0-9]+\z/ } readdir($dh); + closedir($dh); + my $removed = 0; + for my $entry (@entry) { + my ($pgid) = $entry =~ /\A([1-9][0-9]*)\./; + next unless !kill(0, -$pgid) && $!{ESRCH}; + $removed += unlink("$dir/$entry"); + } + rmdir($dir); + return $removed; +} + +#-------------------------------------------------------------------------------- + =head3 DESTROY Descriptions: @@ -674,6 +844,10 @@ sub DESTROY { live one that has not written yet. Only a caller that claims the root exclusively can say either is abandoned. + A lock with an attached process group that still exists is kept whatever the caller + claims. The claim is an assertion that no build is running, and a live group is evidence + that one is. + Arguments: $root: the directory to walk $here: hash ref with machine-id, boot-id and start_of, as owner_is_dead takes @@ -706,6 +880,8 @@ sub sweep_stranded { my @report; # A borrow before its own lock: removing the borrow is what lets the lock be taken over. + # The lock a path belongs to, so a borrow is judged by its own lock's workers. + my $lock_of = sub { my $p = shift; $p =~ s/\.borrow(?:\.[0-9]+)?\z//; return $p }; for my $d (sort { ($b =~ /\.borrow(?:\.[0-9]+)?\z/ ? 1 : 0) <=> ($a =~ /\.borrow(?:\.[0-9]+)?\z/ ? 1 : 0) || $a cmp $b } @found) { if ($d =~ /\.borrow(?:\.[0-9]+)?\z/) { diff --git a/t/nfslock_borrow_recovery.t b/t/nfslock_borrow_recovery.t index d3001f6..f5ccaf9 100644 --- a/t/nfslock_borrow_recovery.t +++ b/t/nfslock_borrow_recovery.t @@ -426,6 +426,126 @@ sub reap { ok($lock->release, 'which the owning process can then release itself'); } +# ---- 7a. a dead owner whose workers still run does not lose its lock ------------------- +# An owner is one process; the work it starts is not. The build wrapper runs each command in its +# OWN process group, so SIGKILL on the wrapper leaves that group running, and a lock recovered +# from the dead wrapper puts two builds in one checkout. +# +# BOTH halves are asserted. A test with only the blocking half cannot tell this rule from a lock +# that recovers nothing at all, which would satisfy the same assertion and remove the reason the +# recovery exists. +{ + my $path = "$dir/with-workers.lock.d"; + pipe(my $stop_r, my $stop_w) or die "pipe: $!"; + pipe(my $up_r, my $up_w) or die "pipe: $!"; + + # The worker: its own process group, as sh() gives each build command, and it outlives the + # owner that started it. + my $worker = fork(); + die "fork: $!" unless defined $worker; + unless ($worker) { + close($stop_w); close($up_r); + POSIX::setpgid(0, 0); + open(STDOUT, '>', '/dev/null'); + open(STDERR, '>', '/dev/null'); + syswrite($up_w, 'w'); + sysread($stop_r, my $go, 1); + POSIX::_exit(0); + } + close($stop_r); close($up_w); + is(read_bounded($up_r, 1, 20), 'w', 'control: the worker is running in its own group'); + POSIX::setpgid($worker, $worker); + + # The owner takes the lock, attaches the worker, and is then killed with a signal it cannot + # handle, so it runs no handler and detaches nothing. + my $owner = XCAT::NFSLock->acquire($path, retries => 1, label => 'owner', quiet => 1); + is($owner->attach($worker), 1, 'control: the owner records the worker group'); + ok(-e "$path/workers/$worker." . process_start($worker), 'control: the record names the group'); + write_text("$path/metadata", metadata(pid => $gone, pstart => '1')); # the owner is now dead + + my ($blocked, $err) = take($path); + ok(!$blocked, 'a dead owner whose worker group still exists keeps its lock'); + like($err, qr/retained for surviving build workers in process group $worker\b/, + 'and the refusal names the group that holds it'); + ok(-d $path, 'the lock is still there'); + + my %swept = map { $_->{path} => $_ } XCAT::NFSLock::sweep_stranded($dir, undef, exclusive => 1); + is($swept{$path}{action}, 'kept', 'an exclusive claim does not override a live worker group'); + like($swept{$path}{why}, qr/process group $worker still exist/, 'and says which group kept it'); + + # THE OTHER HALF. Let the worker exit, and the same lock must now be recoverable. + syswrite($stop_w, 'g'); + waitpid($worker, 0); + my ($taken, $why) = take($path); + ok($taken, 'once the worker group is gone the lock IS recovered') or diag "acquire died: $why"; + ok(!-d "$path/workers", 'and the take-over clears the records of the owner it replaced'); + undef $taken; + close($up_r); close($stop_w); + $owner->{released} = 1; # the metadata names another acquisition now +} + +# ---- 7b. a group outlives its leader, and the lock reads the GROUP --------------------- +# Reaping the group leader does not end the group: its children inherit it. A liveness test that +# asked about the leader would call this group gone while a build worker still runs in it, which +# is the same substitution the whole finding is about -- asking about one process instead of the +# set that must be dead. +# +# detach is asserted here too. It must refuse while the group exists, so the retention is a +# property of what is on disk and not of which call site remembered to run. +{ + my $path = "$dir/orphan.lock.d"; + pipe(my $kid_stop_r, my $kid_stop_w) or die "pipe: $!"; + pipe(my $leader_stop_r, my $leader_stop_w) or die "pipe: $!"; + pipe(my $up_r, my $up_w) or die "pipe: $!"; + + my $leader = fork(); + die "fork: $!" unless defined $leader; + unless ($leader) { + close($kid_stop_w); close($leader_stop_w); close($up_r); + POSIX::setpgid(0, 0); + open(STDOUT, '>', '/dev/null'); + open(STDERR, '>', '/dev/null'); + # The grandchild inherits the group and outlives the leader. + my $kid = fork(); + if (defined($kid) && !$kid) { + syswrite($up_w, 'k'); + sysread($kid_stop_r, my $go, 1); + POSIX::_exit(0); + } + sysread($leader_stop_r, my $go, 1); + POSIX::_exit(0); + } + close($kid_stop_r); close($leader_stop_r); close($up_w); + POSIX::setpgid($leader, $leader); + is(read_bounded($up_r, 1, 20), 'k', 'control: a grandchild runs in the leader\'s group'); + + my $lock = XCAT::NFSLock->acquire($path, retries => 1, label => 'orphan', quiet => 1); + is($lock->attach($leader), 1, 'control: the group is recorded while its leader is alive'); + + # The leader exits; the group does not. + syswrite($leader_stop_w, 'g'); + waitpid($leader, 0); + is(process_start($leader), undef, 'control: the group LEADER is gone'); + ok(kill(0, -$leader), 'control: the group itself is not'); + + is(scalar(XCAT::NFSLock::live_workers($path)), 1, + 'a group that outlived its leader still counts as live'); + is($lock->detach($leader), 0, 'and detach refuses to drop its record'); + + # The grandchild is not this process's child, so waitpid cannot reap it. Poll for the group + # to empty instead, bounded, so a regression fails rather than hangs. + syswrite($kid_stop_w, 'g'); + my $deadline = Time::HiRes::time() + 20; + while (kill(0, -$leader) && Time::HiRes::time() < $deadline) { + select(undef, undef, undef, 0.01); + } + ok(!kill(0, -$leader), 'control: the group is empty once the grandchild exits'); + is(scalar(XCAT::NFSLock::live_workers($path)), 0, 'and the record then counts as gone'); + is($lock->detach($leader), 1, 'so detach drops it'); + undef $lock; + close($_) for ($up_r, $kid_stop_w, $leader_stop_w); +} + # ---- 8. a take-over whose commit fails returns that error, and cleans up nothing -------- # acquire set identity before writing the lock metadata, so a write that left partial metadata # and threw gave DESTROY a lock to release. release then retried for ever, because lock.d