| 1 | package DBIx::QuickDB::Watcher; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 62 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 62 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | 62 | our $VERSION = '0.000066'; | |||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 62 | ▶ | use Carp qw/croak/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 62 | ▶ | use Cwd qw/abs_path/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 62 | ▶ | use Errno qw/ESRCH/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 62 | ▶ | use POSIX qw/:sys_wait_h/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 62 | ▶ | use Time::HiRes qw/sleep time/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 62 | ▶ | use Scalar::Util qw/weaken/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 62 | ▶ | use File::Path qw/remove_tree/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | # Resolved at load time, not at spawn time: $INC{} can be relative, and an | ||||||||||||||||||||||||||||||||||||||||||
| 16 | # application may chdir before starting a database, which would resolve it | ||||||||||||||||||||||||||||||||||||||||||
| 17 | # against the wrong directory and hand the watcher a different Watcher.pm. | ||||||||||||||||||||||||||||||||||||||||||
| 18 | 62 | my $LOADED_FROM = do { | |||||||||||||||||||||||||||||||||||||||||
| 19 | 62 | my $file = $INC{'DBIx/QuickDB/Watcher.pm'}; | |||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | 62 | my $dir; | |||||||||||||||||||||||||||||||||||||||||
| 22 | 62 | ▶ | if (defined($file) && length($file)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $file and length $file
MC/DC0%0/2
defined $file and length $file
defined $file
length $file
Truth table33%1/3
A and B
A= defined $fileB= length $file
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | 62 | my $root = $file; | |||||||||||||||||||||||||||||||||||||||||
| 24 | 62 | $root =~ s{[\\/]?\QDBIx/QuickDB/Watcher.pm\E$}{}; | |||||||||||||||||||||||||||||||||||||||||
| 25 | 62 | ▶ | $dir = abs_path(length($root) ? $root : '.'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 62 | ▶ | (defined($dir) && length($dir) && -d $dir) ? $dir : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
defined $dir and length $dir and -d $dir
defined $dir and length $dir
MC/DC0%0/3
defined $dir and length $dir and -d $dir
defined $dir
length $dir
-d $dir
Truth table25%1/4
A and B and C
A= defined $dirB= length $dirC= -d $dir
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | 62 | ▶ | use DBIx::QuickDB::Util::HashBase qw{ | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | <db <args | ||||||||||||||||||||||||||||||||||||||||||
| 33 | <server_pid | ||||||||||||||||||||||||||||||||||||||||||
| 34 | <watcher_pid | ||||||||||||||||||||||||||||||||||||||||||
| 35 | <master_pid | ||||||||||||||||||||||||||||||||||||||||||
| 36 | <log_file | ||||||||||||||||||||||||||||||||||||||||||
| 37 | <data_dir | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | <stopped | ||||||||||||||||||||||||||||||||||||||||||
| 40 | <eliminated | ||||||||||||||||||||||||||||||||||||||||||
| 41 | <detached | ||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | <delete_data | ||||||||||||||||||||||||||||||||||||||||||
| 44 | <server_reaped | ||||||||||||||||||||||||||||||||||||||||||
| 45 | 62 | }; | |||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | # The watch loop records its reap inside the data dir, which disposable teardown | ||||||||||||||||||||||||||||||||||||||||||
| 48 | # then deletes -- so the proof is gone by the time wait() wants it. Latch it | ||||||||||||||||||||||||||||||||||||||||||
| 49 | # before signalling, while the dir is still there. | ||||||||||||||||||||||||||||||||||||||||||
| 50 | 122 | ▶ | sub _latch_server_reaped { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_latch_server_reaped: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | 122 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | 122 | ▶ | return if $self->{+SERVER_REAPED}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | 77 | ▶ | my $dir = $self->{+DATA_DIR} or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 77 | ▶ | $self->{+SERVER_REAPED} = 1 if -f $self->server_exit_status_file($dir); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | 77 | return; | |||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | 69 | ▶ | sub init { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
init: called CC=4 SCAR=15.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 69 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | 69 | ▶ | $self->{+MASTER_PID} ||= $$; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$self->{"master_pid"} ||= $$
MC/DC0%0/2
$self->{"master_pid"} ||= $$
$self->{"master_pid"}
$$
Truth table33%1/3
A ||= B
A= $self->{"master_pid"}B= $$
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | # Captured up front: wait() needs it after teardown, when the DB reference | ||||||||||||||||||||||||||||||||||||||||||
| 67 | # has been weakened and may already be gone. | ||||||||||||||||||||||||||||||||||||||||||
| 68 | 69 | ▶ | $self->{+DATA_DIR} //= $self->{+DB}->dir; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$self->{"data_dir"} //= $self->{"db"}->dir
MC/DC0%0/2
$self->{"data_dir"} //= $self->{"db"}->dir
$self->{"data_dir"}
$self->{"db"}->dir
Truth table33%1/3
A //= B
A= $self->{"data_dir"}B= $self->{"db"}->dir
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | 69 | $self->{+LOG_FILE} = $self->{+DB}->gen_log; | |||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | 69 | $self->start(); | |||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | 51 | ▶ | weaken($self->{+DB}) if $self->{+MASTER_PID} == $$; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | 70 | ▶ | sub start { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
start: called CC=11 SCAR=25.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | 70 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 79 | 70 | ▶ | return if $self->{+SERVER_PID}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | # Pseudo-fork would keep running the caller's code in a second thread. | ||||||||||||||||||||||||||||||||||||||||||
| 82 | 70 | ▶ | croak "DBIx::QuickDB::Watcher needs Unix process control (setsid, a real fork) and is not supported on MSWin32" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | if $^O eq 'MSWin32'; | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | 69 | my ($rh, $wh); | |||||||||||||||||||||||||||||||||||||||||
| 86 | 69 | ▶ | pipe($rh, $wh) or die "Could not open pipe: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | 69 | my $pid = fork; | |||||||||||||||||||||||||||||||||||||||||
| 89 | 69 | ▶ | die "Could not fork: $!" unless defined $pid; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | 69 | ▶ | if ($pid) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | 51 | close($wh); | |||||||||||||||||||||||||||||||||||||||||
| 93 | 51 | waitpid($pid, 0); | |||||||||||||||||||||||||||||||||||||||||
| 94 | 51 | chomp($self->{+WATCHER_PID} = <$rh>); | |||||||||||||||||||||||||||||||||||||||||
| 95 | 51 | chomp($self->{+SERVER_PID} = <$rh>); | |||||||||||||||||||||||||||||||||||||||||
| 96 | 51 | close($rh); | |||||||||||||||||||||||||||||||||||||||||
| 97 | 51 | ▶ | die "Did not get watcher pid!" unless $self->{+WATCHER_PID}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | 51 | ▶ | die "Did not get server pid!" unless $self->{+SERVER_PID}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | 51 | return; | |||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | 18 | close($rh); | |||||||||||||||||||||||||||||||||||||||||
| 103 | 18 | POSIX::setsid(); | |||||||||||||||||||||||||||||||||||||||||
| 104 | 18 | setpgrp(0, 0); | |||||||||||||||||||||||||||||||||||||||||
| 105 | 18 | $pid = fork; | |||||||||||||||||||||||||||||||||||||||||
| 106 | 18 | ▶ | die "Could not fork: $!" unless defined $pid; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | 18 | ▶ | POSIX::_exit(0) if $pid; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | 18 | $wh->autoflush(1); | |||||||||||||||||||||||||||||||||||||||||
| 110 | 18 | print $wh "$$\n"; | |||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | # In watcher now | ||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | ▶ | eval { $self->watch($wh); 1 } or POSIX::_exit(1); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | POSIX::_exit(0); | |||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | # -I args for the re-exec'd watcher: the directory this module was loaded from, | ||||||||||||||||||||||||||||||||||||||||||
| 118 | # then the rest of @INC. The watcher must load the same Watcher.pm its parent | ||||||||||||||||||||||||||||||||||||||||||
| 119 | # did, so the parent's own resolution is forwarded rather than guessed. | ||||||||||||||||||||||||||||||||||||||||||
| 120 | # | ||||||||||||||||||||||||||||||||||||||||||
| 121 | # -I rather than PERL5LIB, which the MySQLCom and MariaDB drivers deliberately | ||||||||||||||||||||||||||||||||||||||||||
| 122 | # blank so vendor scripts cannot reach application modules. | ||||||||||||||||||||||||||||||||||||||||||
| 123 | # | ||||||||||||||||||||||||||||||||||||||||||
| 124 | # $root is only passed by tests; it defaults to the load-time capture. | ||||||||||||||||||||||||||||||||||||||||||
| 125 | 16 | ▶ | sub _watcher_inc_args { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_watcher_inc_args: called CC=9 SCAR=22.4
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | 16 | ▶ | my ($root) = @_ ? @_ : ($LOADED_FROM); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | 16 | my @dirs; | |||||||||||||||||||||||||||||||||||||||||
| 129 | 16 | ▶ | push @dirs => $root if defined $root; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | # @INC hooks (refs) cannot be expressed as -I. | ||||||||||||||||||||||||||||||||||||||||||
| 132 | 16 | ▶ | push @dirs => grep { !ref($_) && defined($_) && length($_) } @INC; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%4/6
!ref($_) && defined $_ && length $_
not ref $_ and defined $_
MC/DC33%1/3
!ref($_) && defined $_ && length $_
not ref $_
defined $_
length $_
Truth table50%2/4
not(A) and B && C
A= not ref $_B= defined $_C= length $_
| |||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | # Absolutizing the rest is hygiene only -- this runs in the forked watcher, | ||||||||||||||||||||||||||||||||||||||||||
| 135 | # which shares the caller's cwd, so a relative entry would resolve the same | ||||||||||||||||||||||||||||||||||||||||||
| 136 | # either way. $LOADED_FROM above is what actually pins the module. | ||||||||||||||||||||||||||||||||||||||||||
| 137 | 16 | my (@out, %seen); | |||||||||||||||||||||||||||||||||||||||||
| 138 | 16 | for my $dir (@dirs) { | |||||||||||||||||||||||||||||||||||||||||
| 139 | 106 | my $abs = abs_path($dir); | |||||||||||||||||||||||||||||||||||||||||
| 140 | 106 | ▶ | $dir = $abs if defined($abs) && length($abs); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $abs and length $abs
MC/DC0%0/2
defined $abs and length $abs
defined $abs
length $abs
Truth table33%1/3
A and B
A= defined $absB= length $abs
| |||||||||||||||||||||||||||||||||||||||||||
| 141 | 106 | ▶ | next if $seen{$dir}++; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 142 | 94 | push @out => $dir; | |||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | 16 | return map {"-I$_"} @out; | |||||||||||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||
| 148 | 18 | ▶ | sub watch { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
watch: called CC=7 SCAR=19.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 149 | 18 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 150 | 18 | my ($wh) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||
| 152 | 18 | $0 = 'db-quick-watcher'; | |||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||
| 154 | 18 | my $kill = ''; | |||||||||||||||||||||||||||||||||||||||||
| 155 | 18 | my $hup = 0; | |||||||||||||||||||||||||||||||||||||||||
| 156 | 0 | ▶ | local $SIG{TERM} = sub { $kill = 'TERM' }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||
| 157 | 0 | ▶ | local $SIG{INT} = sub { $kill = 'INT' }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||
| 158 | 0 | ▶ | local $SIG{USR1} = sub { $kill = 'FAST_TERM' }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||
| 159 | 0 | ▶ | local $SIG{HUP} = sub { $hup = 1 }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||
| 161 | # Own child reaping before spawning. An inherited SIGCHLD of IGNORE makes | ||||||||||||||||||||||||||||||||||||||||||
| 162 | # the kernel reap the server first, losing its exit status. Protects the | ||||||||||||||||||||||||||||||||||||||||||
| 163 | # window between spawn() and the exec below. | ||||||||||||||||||||||||||||||||||||||||||
| 164 | 18 | local $SIG{CHLD} = 'DEFAULT'; | |||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||
| 166 | 18 | my $start_pid = $$; | |||||||||||||||||||||||||||||||||||||||||
| 167 | 18 | my $pid = $self->spawn(); | |||||||||||||||||||||||||||||||||||||||||
| 168 | 9 | print $wh "$pid\n"; | |||||||||||||||||||||||||||||||||||||||||
| 169 | 9 | close($wh); | |||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||
| 171 | 9 | my $mpid = $self->{+MASTER_PID}; | |||||||||||||||||||||||||||||||||||||||||
| 172 | 9 | ▶ | my $spid = $self->{+SERVER_PID} or die "No server pid"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||
| 174 | 9 | my $ddir = $self->{+DATA_DIR}; | |||||||||||||||||||||||||||||||||||||||||
| 175 | 9 | ▶ | my $ssig = $self->{+DB}->stop_sig // 'TERM'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"db"}->stop_sig // "TERM"
MC/DC0%0/1
$self->{"db"}->stop_sig // "TERM"
$self->{"db"}->stop_sig
Truth table50%1/2
A
A= $self->{"db"}->stop_sig
| |||||||||||||||||||||||||||||||||||||||||||
| 176 | 9 | ▶ | my $fsig = $self->{+DB}->fast_stop_sig // 'KILL'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"db"}->fast_stop_sig // "KILL"
MC/DC0%0/1
$self->{"db"}->fast_stop_sig // "KILL"
$self->{"db"}->fast_stop_sig
Truth table50%1/2
A
A= $self->{"db"}->fast_stop_sig
| |||||||||||||||||||||||||||||||||||||||||||
| 177 | 9 | ▶ | my $delete_data = $self->{+DB}->should_cleanup ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 178 | 9 | ▶ | my $owner_kill = $delete_data && $self->{+DB}->fast_destroy | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$delete_data and $self->{"db"}->fast_destroy
MC/DC100%2/2
$delete_data and $self->{"db"}->fast_destroy
$delete_data
$self->{"db"}->fast_destroy
Truth table100%3/3
A and B
A= $delete_dataB= $self->{"db"}->fast_destroy
| |||||||||||||||||||||||||||||||||||||||||||
| 179 | ? 'FAST_TERM' | ||||||||||||||||||||||||||||||||||||||||||
| 180 | : 'TERM'; | ||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||
| 182 | # Block (rather than ignore) every teardown signal across the exec. A | ||||||||||||||||||||||||||||||||||||||||||
| 183 | # blocked signal stays *pending* instead of being discarded, so a stop(), | ||||||||||||||||||||||||||||||||||||||||||
| 184 | # eliminate(), or fast_eliminate() that races server startup -- arriving | ||||||||||||||||||||||||||||||||||||||||||
| 185 | # after the socket is up (so the caller's start() has returned) but before | ||||||||||||||||||||||||||||||||||||||||||
| 186 | # _do_watch has installed its handlers -- is not lost: _do_watch unblocks | ||||||||||||||||||||||||||||||||||||||||||
| 187 | # them once the handlers are in place and a pending one fires immediately. | ||||||||||||||||||||||||||||||||||||||||||
| 188 | # | ||||||||||||||||||||||||||||||||||||||||||
| 189 | # SIGTERM/SIGINT used to be SIG_IGN'd here instead, on the wrong belief | ||||||||||||||||||||||||||||||||||||||||||
| 190 | # that an ignored signal is "held" across exec -- it is DISCARDED. A | ||||||||||||||||||||||||||||||||||||||||||
| 191 | # stop() landing in the exec window (perl startup + module load in the | ||||||||||||||||||||||||||||||||||||||||||
| 192 | # fresh watcher) was silently dropped, the watcher never learned it should | ||||||||||||||||||||||||||||||||||||||||||
| 193 | # stop, and the caller's wait() eventually gave up and killed the watcher, | ||||||||||||||||||||||||||||||||||||||||||
| 194 | # orphaning a still-running server whose data dir stayed locked. The | ||||||||||||||||||||||||||||||||||||||||||
| 195 | # build-then-immediately-stop pattern (DBIx::QuickDB::Pool) hit that | ||||||||||||||||||||||||||||||||||||||||||
| 196 | # window constantly. | ||||||||||||||||||||||||||||||||||||||||||
| 197 | 9 | POSIX::sigprocmask(POSIX::SIG_BLOCK(), POSIX::SigSet->new(POSIX::SIGUSR1(), POSIX::SIGINT(), POSIX::SIGTERM())); | |||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||
| 199 | 9 | exec( | |||||||||||||||||||||||||||||||||||||||||
| 200 | $^X, _watcher_inc_args(), | ||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||
| 202 | '-e' => "require DBIx::QuickDB::Watcher; DBIx::QuickDB::Watcher->_do_watch()", | ||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||
| 204 | master_pid => $mpid, | ||||||||||||||||||||||||||||||||||||||||||
| 205 | data_dir => $ddir, | ||||||||||||||||||||||||||||||||||||||||||
| 206 | server_pid => $spid, | ||||||||||||||||||||||||||||||||||||||||||
| 207 | signal => $ssig, | ||||||||||||||||||||||||||||||||||||||||||
| 208 | fast_signal => $fsig, | ||||||||||||||||||||||||||||||||||||||||||
| 209 | delete_data => $delete_data, | ||||||||||||||||||||||||||||||||||||||||||
| 210 | owner_kill => $owner_kill, | ||||||||||||||||||||||||||||||||||||||||||
| 211 | kill => $kill, | ||||||||||||||||||||||||||||||||||||||||||
| 212 | hup => $hup, | ||||||||||||||||||||||||||||||||||||||||||
| 213 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||
| 216 | 0 | ▶ | sub _do_watch { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_do_watch: not called CC=19 SCAR=59.4
| |||||||||||||||||||||||||||||||||||||||||||
| 217 | 0 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||
| 219 | 0 | $0 = 'db-quick-watcher'; | |||||||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||||||||
| 221 | 0 | my %params = @ARGV; | |||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||
| 223 | 0 | ▶ | my $kill = $params{kill} // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$params{"kill"} // ""
MC/DC0%0/1
$params{"kill"} // ""
$params{"kill"}
Truth table0%0/2
A
A= $params{"kill"}
| |||||||||||||||||||||||||||||||||||||||||||
| 224 | 0 | ▶ | my $hup = $params{hup} // 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$params{"hup"} // 0
MC/DC0%0/1
$params{"hup"} // 0
$params{"hup"}
Truth table0%0/2
A
A= $params{"hup"}
| |||||||||||||||||||||||||||||||||||||||||||
| 225 | 0 | ▶ | local $SIG{TERM} = sub { $kill = 'TERM' }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 226 | 0 | ▶ | local $SIG{INT} = sub { $kill = 'INT' }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 227 | 0 | ▶ | local $SIG{USR1} = sub { $kill = 'FAST_TERM' }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 228 | 0 | ▶ | local $SIG{HUP} = sub { $hup = 1 }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||
| 230 | # watch() blocked the teardown signals before exec so a stop/eliminate/ | ||||||||||||||||||||||||||||||||||||||||||
| 231 | # fast_eliminate racing startup stays pending rather than being discarded. | ||||||||||||||||||||||||||||||||||||||||||
| 232 | # Now that the handlers above are installed, unblock them -- any pending | ||||||||||||||||||||||||||||||||||||||||||
| 233 | # teardown fires here and sets $kill before we enter the watch loop. | ||||||||||||||||||||||||||||||||||||||||||
| 234 | 0 | POSIX::sigprocmask(POSIX::SIG_UNBLOCK(), POSIX::SigSet->new(POSIX::SIGUSR1(), POSIX::SIGINT(), POSIX::SIGTERM())); | |||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||
| 236 | 0 | my $blah; | |||||||||||||||||||||||||||||||||||||||||
| 237 | 0 | close(STDIN); | |||||||||||||||||||||||||||||||||||||||||
| 238 | 0 | ▶ | open(STDIN, '<', \$blah) or warn "$!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||||||
| 240 | 0 | ▶ | my $master_pid = $params{master_pid} or die "No master pid provided"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 241 | 0 | ▶ | my $server_pid = $params{server_pid} or die "No server pid provided"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 242 | 0 | ▶ | my $data_dir = $params{data_dir} or die "No data dir provided"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 243 | 0 | ▶ | my $signal = $params{signal} // 'TERM'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$params{"signal"} // "TERM"
MC/DC0%0/1
$params{"signal"} // "TERM"
$params{"signal"}
Truth table0%0/2
A
A= $params{"signal"}
| |||||||||||||||||||||||||||||||||||||||||||
| 244 | 0 | ▶ | my $fast_signal = $params{fast_signal} // 'KILL'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$params{"fast_signal"} // "KILL"
MC/DC0%0/1
$params{"fast_signal"} // "KILL"
$params{"fast_signal"}
Truth table0%0/2
A
A= $params{"fast_signal"}
| |||||||||||||||||||||||||||||||||||||||||||
| 245 | 0 | ▶ | my $delete_data = $params{delete_data} // 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$params{"delete_data"} // 0
MC/DC0%0/1
$params{"delete_data"} // 0
$params{"delete_data"}
Truth table0%0/2
A
A= $params{"delete_data"}
| |||||||||||||||||||||||||||||||||||||||||||
| 246 | 0 | ▶ | my $owner_kill = $params{owner_kill} // 'TERM'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$params{"owner_kill"} // "TERM"
MC/DC0%0/1
$params{"owner_kill"} // "TERM"
$params{"owner_kill"}
Truth table0%0/2
A
A= $params{"owner_kill"}
| |||||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||
| 248 | 0 | my $reaped = 0; | |||||||||||||||||||||||||||||||||||||||||
| 249 | 0 | my $hupped = 0; | |||||||||||||||||||||||||||||||||||||||||
| 250 | 0 | ▶ | while (!$kill) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 251 | 0 | ▶ | if ($hup && !$hupped) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$hup and not $hupped
MC/DC0%0/2
$hup and not $hupped
$hup
not $hupped
Truth table0%0/3
A and not(B)
A= $hupB= not $hupped
| |||||||||||||||||||||||||||||||||||||||||||
| 252 | 0 | close(STDOUT); | |||||||||||||||||||||||||||||||||||||||||
| 253 | 0 | ▶ | open(STDOUT, '>', \$blah) or warn "$!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 254 | 0 | close(STDERR); | |||||||||||||||||||||||||||||||||||||||||
| 255 | 0 | ▶ | open(STDERR, '>', \$blah) or warn "$!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 256 | 0 | $hupped = 1; | |||||||||||||||||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||
| 259 | 0 | sleep 0.1; | |||||||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||||||||
| 261 | # Nothing else reaps the server, so a crashed one would linger as a | ||||||||||||||||||||||||||||||||||||||||||
| 262 | # zombie -- and kill(0) succeeds on a zombie, which made start() report | ||||||||||||||||||||||||||||||||||||||||||
| 263 | # it alive and wait out the whole timeout. Keep watching after the reap: | ||||||||||||||||||||||||||||||||||||||||||
| 264 | # exiting here would run teardown and delete the logs start() needs. | ||||||||||||||||||||||||||||||||||||||||||
| 265 | 0 | ▶ | $reaped ||= $class->_reap_server($server_pid, $data_dir); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$reaped ||= $class->_reap_server($server_pid, $data_dir)
MC/DC0%0/2
$reaped ||= $class->_reap_server($server_pid, $data_dir)
$reaped
$class->_reap_server($server_pid, $data_dir)
Truth table0%0/3
A ||= B
A= $reapedB= $class->_reap_server($server_pid, $data_dir)
| |||||||||||||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||||||||||||
| 267 | 0 | ▶ | next if kill(0, $master_pid); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 268 | 0 | $kill = $owner_kill; | |||||||||||||||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||
| 271 | 0 | ▶ | unless (eval { $class->_watcher_terminate(send_sig => $signal, fast_sig => $fast_signal, got_sig => $kill, pid => $server_pid, dir => $data_dir, delete_data => $delete_data, already_reaped => $reaped); 1 }) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 272 | # warn $err, not $@: entering an eval BLOCK clears $@, which stripped | ||||||||||||||||||||||||||||||||||||||||||
| 273 | # the message off every teardown failure. | ||||||||||||||||||||||||||||||||||||||||||
| 274 | 0 | my $err = $@; | |||||||||||||||||||||||||||||||||||||||||
| 275 | 0 | eval { warn $err }; | |||||||||||||||||||||||||||||||||||||||||
| 276 | 0 | POSIX::_exit(1); | |||||||||||||||||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||||||||||
| 279 | 0 | POSIX::_exit(0); | |||||||||||||||||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||||||||||||
| 282 | 7350 | ▶ | sub server_exit_status_file { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
server_exit_status_file: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 283 | 7350 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 284 | 7350 | my ($dir) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 285 | 7350 | return "$dir/server-exit-status"; | |||||||||||||||||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||||||
| 288 | # Non-blocking reap; writes the wait status to server_exit_status_file() and | ||||||||||||||||||||||||||||||||||||||||||
| 289 | # returns true once collected. Never dies -- this runs in the watch loop, and | ||||||||||||||||||||||||||||||||||||||||||
| 290 | # taking the watcher down would orphan the server. | ||||||||||||||||||||||||||||||||||||||||||
| 291 | 165 | ▶ | sub _reap_server { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_reap_server: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 292 | 165 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 293 | 165 | my ($pid, $dir) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||||||
| 295 | 165 | my ($got, $status); | |||||||||||||||||||||||||||||||||||||||||
| 296 | { | ||||||||||||||||||||||||||||||||||||||||||
| 297 | 165 | local $?; | |||||||||||||||||||||||||||||||||||||||||
| 298 | 165 | $got = waitpid($pid, WNOHANG); | |||||||||||||||||||||||||||||||||||||||||
| 299 | 165 | $status = $?; | |||||||||||||||||||||||||||||||||||||||||
| 300 | } | ||||||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||||||
| 302 | # Only a real reap counts. Negative is ECHILD, which yields no status and | ||||||||||||||||||||||||||||||||||||||||||
| 303 | # cannot distinguish "auto-reaped" from "never our child"; teardown keys off | ||||||||||||||||||||||||||||||||||||||||||
| 304 | # this to decide whether it may skip the kill, so do not overclaim. | ||||||||||||||||||||||||||||||||||||||||||
| 305 | 165 | ▶ | return 0 unless $got > 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||||||||
| 307 | 45 | my $file = $class->server_exit_status_file($dir); | |||||||||||||||||||||||||||||||||||||||||
| 308 | 45 | ▶ | if (open(my $fh, '>', $file)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 309 | 30 | print $fh "$status\n"; | |||||||||||||||||||||||||||||||||||||||||
| 310 | 30 | close($fh); | |||||||||||||||||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||||||||||
| 313 | 45 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 314 | } | ||||||||||||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||||||||||||
| 316 | 18 | ▶ | sub spawn { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
spawn: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 317 | 18 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||||||||||
| 319 | 18 | ▶ | croak "Extra spawn" if $self->{+SERVER_PID}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||||||||||||
| 321 | 18 | my $db = $self->{+DB}; | |||||||||||||||||||||||||||||||||||||||||
| 322 | 18 | ▶ | my $args = $self->{+ARGS} || []; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{'args'} || []
MC/DC0%0/1
$self->{'args'} || []
$self->{'args'}
Truth table50%1/2
A
A= $self->{'args'}
| |||||||||||||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||||||||||||
| 324 | 18 | my $init_pid = $$; | |||||||||||||||||||||||||||||||||||||||||
| 325 | 18 | my ($pid, $log_file) = $db->run_command([$db->start_command, @$args], {no_wait => 1, log_file => $self->{+LOG_FILE}}); | |||||||||||||||||||||||||||||||||||||||||
| 326 | 9 | $self->{+SERVER_PID} = $pid; | |||||||||||||||||||||||||||||||||||||||||
| 327 | 9 | $self->{+LOG_FILE} = $log_file; | |||||||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||||||||
| 329 | 9 | return $pid; | |||||||||||||||||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||||||||||
| 332 | 1 | ▶ | sub _watcher_terminate { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_watcher_terminate: called CC=15 SCAR=38.8
| |||||||||||||||||||||||||||||||||||||||||||
| 333 | 1 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 334 | 1 | my %params = @_; | |||||||||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||||||||||
| 336 | 1 | ▶ | my $pid = $params{pid} or die "No pid"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 337 | 1 | ▶ | my $dir = $params{dir} or die "No dir"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||||||||||||
| 339 | 1 | my $got_sig = $params{got_sig}; | |||||||||||||||||||||||||||||||||||||||||
| 340 | 1 | ▶ | my $send_sig = $params{send_sig} // $got_sig // 'TERM'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition40%2/5
($params{"send_sig"} // $got_sig) // "TERM"
$params{"send_sig"} // $got_sig
MC/DC0%0/2
($params{"send_sig"} // $got_sig) // "TERM"
$params{"send_sig"}
$got_sig
Truth table33%1/3
A // B
A= $params{"send_sig"}B= $got_sig
| |||||||||||||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||||||||||||
| 342 | # Already reaped by the watch loop: signalling the pid now could hit an | ||||||||||||||||||||||||||||||||||||||||||
| 343 | # unrelated process that has since been given it. | ||||||||||||||||||||||||||||||||||||||||||
| 344 | 1 | my $reaped = $params{already_reaped}; | |||||||||||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||||||||||||
| 346 | # Removing the data dir is a goal in its own right, not a reward for a | ||||||||||||||||||||||||||||||||||||||||||
| 347 | # successful kill. A server we cannot stop is already pathological, and | ||||||||||||||||||||||||||||||||||||||||||
| 348 | # leaving its dir behind to outlive the process that asked for it is the | ||||||||||||||||||||||||||||||||||||||||||
| 349 | # failure we are here to prevent. On Linux the server's open handles keep | ||||||||||||||||||||||||||||||||||||||||||
| 350 | # working after the unlink; Windows may fare worse, which is accepted for a | ||||||||||||||||||||||||||||||||||||||||||
| 351 | # case this rare. So the kill is best-effort and never blocks the removal. | ||||||||||||||||||||||||||||||||||||||||||
| 352 | 1 | ▶ | if ($got_sig && $got_sig eq 'FAST_TERM') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$got_sig and $got_sig eq "FAST_TERM"
MC/DC0%0/2
$got_sig and $got_sig eq "FAST_TERM"
$got_sig
$got_sig eq "FAST_TERM"
Truth table33%1/3
A and B
A= $got_sigB= $got_sig eq "FAST_TERM"
| |||||||||||||||||||||||||||||||||||||||||||
| 353 | 0 | ▶ | eval { $class->_watcher_kill_fast($pid, $params{fast_sig}) unless $reaped; 1 } or warn $@; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||||||||||||
| 355 | 0 | ▶ | _remove_data_dir($dir) if $params{delete_data}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||||||||||||
| 357 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 358 | } | ||||||||||||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||||||||||||
| 360 | 1 | ▶ | eval { $class->_watcher_kill($send_sig, $pid, $params{fast_sig}) unless $reaped; 1 } or warn $@; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||||||||||||
| 362 | 1 | ▶ | _remove_data_dir($dir) if $params{delete_data} && $got_sig && $got_sig eq 'TERM'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$params{"delete_data"} and $got_sig and $got_sig eq "TERM"
$params{"delete_data"} and $got_sig
MC/DC0%0/3
$params{"delete_data"} and $got_sig and $got_sig eq "TERM"
$params{"delete_data"}
$got_sig
$got_sig eq "TERM"
Truth table25%1/4
A and B and C
A= $params{"delete_data"}B= $got_sigC= $got_sig eq "TERM"
| |||||||||||||||||||||||||||||||||||||||||||
| 363 | } | ||||||||||||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||||||||||
| 365 | # Best-effort, and eval'd: File::Path hard-dies rather than routing through its | ||||||||||||||||||||||||||||||||||||||||||
| 366 | # error handler when the owner deletes this same tree concurrently. | ||||||||||||||||||||||||||||||||||||||||||
| 367 | 1 | ▶ | sub _remove_data_dir { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_remove_data_dir: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||
| 368 | 1 | my ($dir) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||||||||||||||||
| 370 | 1 | ▶ | return unless -d $dir; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||||||||||||||||
| 372 | 1 | my $err = []; | |||||||||||||||||||||||||||||||||||||||||
| 373 | 1 | eval { remove_tree($dir, {safe => 1, error => \$err}) }; | |||||||||||||||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||||||||||||||||
| 375 | 1 | return; | |||||||||||||||||||||||||||||||||||||||||
| 376 | } | ||||||||||||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||||||||||||
| 378 | 3 | ▶ | sub _watcher_kill_fast { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_watcher_kill_fast: called CC=11 SCAR=25.0
| |||||||||||||||||||||||||||||||||||||||||||
| 379 | 3 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 380 | 3 | my ($pid, $sig) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||||||||||||
| 382 | 3 | ▶ | $sig ||= 'KILL'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$sig ||= "KILL"
MC/DC100%1/1
$sig ||= "KILL"
$sig
Truth table100%2/2
A
A= $sig
| |||||||||||||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||||||||||||
| 384 | 3 | ▶ | unless (kill($sig, $pid)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 385 | 0 | ▶ | return if $! == ESRCH; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 386 | 0 | warn "Could not signal server pid $pid with SIG$sig: $!"; | |||||||||||||||||||||||||||||||||||||||||
| 387 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 388 | } | ||||||||||||||||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||||||||||||||||
| 390 | # Reap the server. With SIGKILL this resolves almost immediately. With a | ||||||||||||||||||||||||||||||||||||||||||
| 391 | # clean immediate-shutdown signal (e.g. PostgreSQL's SIGQUIT, chosen so the | ||||||||||||||||||||||||||||||||||||||||||
| 392 | # postmaster releases its SysV semaphores instead of leaking them) the | ||||||||||||||||||||||||||||||||||||||||||
| 393 | # server needs a moment to abort its backends and exit; give it a short | ||||||||||||||||||||||||||||||||||||||||||
| 394 | # window, then escalate to SIGKILL so teardown always completes even if that | ||||||||||||||||||||||||||||||||||||||||||
| 395 | # signal is caught and ignored. After SIGKILL we block on the reap: SIGKILL | ||||||||||||||||||||||||||||||||||||||||||
| 396 | # cannot be ignored, so the only thing that can delay the zombie appearing is | ||||||||||||||||||||||||||||||||||||||||||
| 397 | # transient (kernel delivery, brief uninterruptible IO), and racing a fixed | ||||||||||||||||||||||||||||||||||||||||||
| 398 | # deadline against it only produces a spurious "PID refused to exit" on a | ||||||||||||||||||||||||||||||||||||||||||
| 399 | # loaded host. | ||||||||||||||||||||||||||||||||||||||||||
| 400 | 3 | my $escalated = $sig eq 'KILL'; | |||||||||||||||||||||||||||||||||||||||||
| 401 | 3 | my ($check, $exit); | |||||||||||||||||||||||||||||||||||||||||
| 402 | 3 | my $start = time; | |||||||||||||||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||||||||||||||||
| 404 | 3 | ▶ | until ($check) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 405 | 201 | local $?; | |||||||||||||||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||||||||||||||||
| 407 | 201 | ▶ | if ($escalated) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 408 | 2 | $check = waitpid($pid, 0); | |||||||||||||||||||||||||||||||||||||||||
| 409 | 2 | $exit = $?; | |||||||||||||||||||||||||||||||||||||||||
| 410 | 2 | last; | |||||||||||||||||||||||||||||||||||||||||
| 411 | } | ||||||||||||||||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||||||||||||||||
| 413 | 199 | $check = waitpid($pid, WNOHANG); | |||||||||||||||||||||||||||||||||||||||||
| 414 | 199 | $exit = $?; | |||||||||||||||||||||||||||||||||||||||||
| 415 | 199 | ▶ | last if $check; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||||||||||||||||
| 417 | 198 | ▶ | if (time - $start > 2) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 418 | 1 | kill('KILL', $pid); | |||||||||||||||||||||||||||||||||||||||||
| 419 | 1 | $escalated = 1; | |||||||||||||||||||||||||||||||||||||||||
| 420 | } | ||||||||||||||||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||||||||||||||||
| 422 | 198 | sleep 0.01; | |||||||||||||||||||||||||||||||||||||||||
| 423 | } | ||||||||||||||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||||||||||||
| 425 | 3 | ▶ | die "PID refused to exit after fast kill" unless $check; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 426 | 3 | ▶ | die "Something else reaped our process" if $check < 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 427 | 3 | ▶ | die "Reaped the wrong process '$check' instead of '$pid'" if $pid != $check; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||||||||||||||||
| 429 | 3 | return; | |||||||||||||||||||||||||||||||||||||||||
| 430 | } | ||||||||||||||||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||||||||||||||||
| 432 | # Seconds a graceful stop may take before the watcher escalates (and before | ||||||||||||||||||||||||||||||||||||||||||
| 433 | # wait() starts worrying). The default must comfortably exceed a NORMAL clean | ||||||||||||||||||||||||||||||||||||||||||
| 434 | # shutdown on a loaded host: MySQL 8's InnoDB shutdown and PostgreSQL's | ||||||||||||||||||||||||||||||||||||||||||
| 435 | # shutdown checkpoint both routinely take more than the old default of 4s | ||||||||||||||||||||||||||||||||||||||||||
| 436 | # under a parallel test load, which made escalation (and its warning, and for | ||||||||||||||||||||||||||||||||||||||||||
| 437 | # MySQL a straight SIGKILL plus crash recovery on the next clone start) the | ||||||||||||||||||||||||||||||||||||||||||
| 438 | # common case rather than the pathological one. Historically this was kept | ||||||||||||||||||||||||||||||||||||||||||
| 439 | # small because wedged shutdowns "never finish no matter how long we wait" -- | ||||||||||||||||||||||||||||||||||||||||||
| 440 | # that wedge was stop() signals being lost across the watcher exec, fixed by | ||||||||||||||||||||||||||||||||||||||||||
| 441 | # blocking them; real shutdowns do finish. | ||||||||||||||||||||||||||||||||||||||||||
| 442 | 63 | ▶ | sub _stop_grace { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_stop_grace: called CC=4 SCAR=14.1
| |||||||||||||||||||||||||||||||||||||||||||
| 443 | 63 | my $grace = $ENV{QDB_STOP_GRACE}; | |||||||||||||||||||||||||||||||||||||||||
| 444 | 63 | ▶ | $grace = 30 unless defined($grace) && $grace =~ /^\d+$/ && $grace > 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
defined $grace and $grace =~ /^\d+$/ and $grace > 0
defined $grace and $grace =~ /^\d+$/
MC/DC33%1/3
defined $grace and $grace =~ /^\d+$/ and $grace > 0
defined $grace
$grace =~ /^\d+$/
$grace > 0
Truth table50%2/4
A and B and C
A= defined $graceB= $grace =~ /^\d+$/C= $grace > 0
| |||||||||||||||||||||||||||||||||||||||||||
| 445 | 63 | return $grace; | |||||||||||||||||||||||||||||||||||||||||
| 446 | } | ||||||||||||||||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||||||||||||||||
| 448 | 4 | ▶ | sub _watcher_kill { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_watcher_kill: called CC=16 SCAR=28.1
| |||||||||||||||||||||||||||||||||||||||||||
| 449 | 4 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 450 | 4 | my ($sig, $pid, $fast_sig) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||||||||||||||||
| 452 | 4 | ▶ | $fast_sig ||= 'KILL'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$fast_sig ||= "KILL"
MC/DC100%1/1
$fast_sig ||= "KILL"
$fast_sig
Truth table100%2/2
A
A= $fast_sig
| |||||||||||||||||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||||||||||||||||
| 454 | # A signal that cannot even be delivered is warning-only. The reap failures | ||||||||||||||||||||||||||||||||||||||||||
| 455 | # further down still throw; _watcher_terminate catches those and removes the | ||||||||||||||||||||||||||||||||||||||||||
| 456 | # data dir anyway. | ||||||||||||||||||||||||||||||||||||||||||
| 457 | 4 | ▶ | unless (kill($sig, $pid)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 458 | 2 | my $err = $!; | |||||||||||||||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||||||||||||||||
| 460 | # ESRCH just means it beat us to it. | ||||||||||||||||||||||||||||||||||||||||||
| 461 | 2 | ▶ | return if $err == ESRCH; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||||||||||||||||
| 463 | # Anything else (EINVAL from a bad stop_sig, EPERM from a recycled pid) | ||||||||||||||||||||||||||||||||||||||||||
| 464 | # leaves a server we cannot stop. Worth saying so. | ||||||||||||||||||||||||||||||||||||||||||
| 465 | 0 | warn "Could not signal server pid $pid with SIG$sig: $err"; | |||||||||||||||||||||||||||||||||||||||||
| 466 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 467 | } | ||||||||||||||||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||||||||||||||||
| 469 | # How long to wait for a graceful shutdown before escalating, and how much | ||||||||||||||||||||||||||||||||||||||||||
| 470 | # longer before giving up entirely. Keep this generous: a slow or loaded | ||||||||||||||||||||||||||||||||||||||||||
| 471 | # host (e.g. a CPAN smoke box) can need well over a few seconds to finish | ||||||||||||||||||||||||||||||||||||||||||
| 472 | # PostgreSQL's shutdown checkpoint. A premature hard kill leaves the data dir | ||||||||||||||||||||||||||||||||||||||||||
| 473 | # in a crash-recovery state, and a clone of that dir then replays WAL on | ||||||||||||||||||||||||||||||||||||||||||
| 474 | # first start, jumping SERIAL sequences forward by SEQ_LOG_VALS (32) -- | ||||||||||||||||||||||||||||||||||||||||||
| 475 | # silently corrupting cloned databases. Tunable via QDB_STOP_GRACE. | ||||||||||||||||||||||||||||||||||||||||||
| 476 | 2 | my $kill_after = _stop_grace(); | |||||||||||||||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||||||||||||||||
| 478 | # Two-stage escalation once the graceful signal has not stopped the server by | ||||||||||||||||||||||||||||||||||||||||||
| 479 | # $kill_after. First send the driver's fast-stop signal -- an immediate but | ||||||||||||||||||||||||||||||||||||||||||
| 480 | # *clean* shutdown (e.g. PostgreSQL SIGQUIT) that still lets the server run | ||||||||||||||||||||||||||||||||||||||||||
| 481 | # its exit cleanup and RELEASE OS resources such as SysV semaphores. Only if | ||||||||||||||||||||||||||||||||||||||||||
| 482 | # that is also ignored do we SIGKILL. Going straight to SIGKILL here would | ||||||||||||||||||||||||||||||||||||||||||
| 483 | # orphan those semaphores permanently -- the data dir is about to be deleted | ||||||||||||||||||||||||||||||||||||||||||
| 484 | # so no future server reuses the IPC key -- and a suite that kills many | ||||||||||||||||||||||||||||||||||||||||||
| 485 | # servers this way exhausts the host's SEMMNI/SEMMNS limits. When the driver | ||||||||||||||||||||||||||||||||||||||||||
| 486 | # leaves fast_stop_sig at its 'KILL' default both stages are SIGKILL, which | ||||||||||||||||||||||||||||||||||||||||||
| 487 | # is harmless. | ||||||||||||||||||||||||||||||||||||||||||
| 488 | 2 | ▶ | my $step = $kill_after > 1 ? int($kill_after / 2) : 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 489 | 2 | my $fast_at = $kill_after; | |||||||||||||||||||||||||||||||||||||||||
| 490 | 2 | my $kill_at = $fast_at + $step; | |||||||||||||||||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||||||||||||||||
| 492 | 2 | my ($check, $exit, $sent_fast, $sent_kill); | |||||||||||||||||||||||||||||||||||||||||
| 493 | 2 | my $start = time; | |||||||||||||||||||||||||||||||||||||||||
| 494 | 2 | ▶ | until ($check) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 495 | 123 | local $?; | |||||||||||||||||||||||||||||||||||||||||
| 496 | 123 | my $delta = time - $start; | |||||||||||||||||||||||||||||||||||||||||
| 497 | |||||||||||||||||||||||||||||||||||||||||||
| 498 | 123 | ▶ | if ($delta >= $fast_at && !$sent_fast) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$delta >= $fast_at and not $sent_fast
MC/DC100%2/2
$delta >= $fast_at and not $sent_fast
$delta >= $fast_at
not $sent_fast
Truth table100%3/3
A and not(B)
A= $delta >= $fast_atB= not $sent_fast
| |||||||||||||||||||||||||||||||||||||||||||
| 499 | 2 | warn "Server taking too long to shut down, sending SIG$fast_sig"; | |||||||||||||||||||||||||||||||||||||||||
| 500 | 2 | kill($fast_sig, $pid); | |||||||||||||||||||||||||||||||||||||||||
| 501 | 2 | $sent_fast = 1; | |||||||||||||||||||||||||||||||||||||||||
| 502 | } | ||||||||||||||||||||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||||||||||||||||||||
| 504 | 123 | ▶ | if ($delta >= $kill_at && !$sent_kill) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$delta >= $kill_at and not $sent_kill
MC/DC50%1/2
$delta >= $kill_at and not $sent_kill
$delta >= $kill_at
not $sent_kill
Truth table66%2/3
A and not(B)
A= $delta >= $kill_atB= not $sent_kill
| |||||||||||||||||||||||||||||||||||||||||||
| 505 | 1 | ▶ | warn "Server still running, sending SIGKILL" unless $fast_sig eq 'KILL'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 506 | 1 | kill('KILL', $pid); | |||||||||||||||||||||||||||||||||||||||||
| 507 | 1 | $sent_kill = 1; | |||||||||||||||||||||||||||||||||||||||||
| 508 | } | ||||||||||||||||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||||||||||||||||
| 510 | # SIGKILL cannot be caught or ignored, so the server WILL terminate and | ||||||||||||||||||||||||||||||||||||||||||
| 511 | # become reapable -- block until it does instead of racing a wall-clock | ||||||||||||||||||||||||||||||||||||||||||
| 512 | # deadline. The old give-up window shrank with QDB_STOP_GRACE (only ~1s | ||||||||||||||||||||||||||||||||||||||||||
| 513 | # at grace=1) and a loaded host could take longer than that just to | ||||||||||||||||||||||||||||||||||||||||||
| 514 | # deliver the kill and surface the zombie, tripping a spurious "PID | ||||||||||||||||||||||||||||||||||||||||||
| 515 | # refused to exit". A normal reap returns in well under a second, so the | ||||||||||||||||||||||||||||||||||||||||||
| 516 | # blocking wait still finishes inside Driver::stop's 2*grace+2 wait() | ||||||||||||||||||||||||||||||||||||||||||
| 517 | # budget and the watcher is not killed mid-reap. | ||||||||||||||||||||||||||||||||||||||||||
| 518 | 123 | ▶ | if ($sent_kill) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 519 | 1 | $check = waitpid($pid, 0); | |||||||||||||||||||||||||||||||||||||||||
| 520 | 1 | $exit = $?; | |||||||||||||||||||||||||||||||||||||||||
| 521 | 1 | last; | |||||||||||||||||||||||||||||||||||||||||
| 522 | } | ||||||||||||||||||||||||||||||||||||||||||
| 523 | |||||||||||||||||||||||||||||||||||||||||||
| 524 | 122 | $check = waitpid($pid, WNOHANG); | |||||||||||||||||||||||||||||||||||||||||
| 525 | 122 | $exit = $?; | |||||||||||||||||||||||||||||||||||||||||
| 526 | |||||||||||||||||||||||||||||||||||||||||||
| 527 | 122 | ▶ | last if $check; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 528 | |||||||||||||||||||||||||||||||||||||||||||
| 529 | 121 | sleep 0.1; | |||||||||||||||||||||||||||||||||||||||||
| 530 | } | ||||||||||||||||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||||||||||||||||
| 532 | 2 | ▶ | die "PID refused to exit" unless $check; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 533 | 2 | ▶ | die "Something else reaped our process" if $check < 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 534 | 2 | ▶ | die "Reaped the wrong process '$check' instead of '$pid'" if $pid != $check; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 535 | |||||||||||||||||||||||||||||||||||||||||||
| 536 | 2 | return; | |||||||||||||||||||||||||||||||||||||||||
| 537 | } | ||||||||||||||||||||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||||||||||||||||||||
| 539 | # stop(), eliminate(), and detach() each signal the watcher process by pid. Once | ||||||||||||||||||||||||||||||||||||||||||
| 540 | # ANY terminal teardown has been initiated, the watcher is exiting (or already | ||||||||||||||||||||||||||||||||||||||||||
| 541 | # gone) and the OS may recycle its pid to an unrelated process -- notably a | ||||||||||||||||||||||||||||||||||||||||||
| 542 | # sibling database's postmaster. A second signal would then land on the wrong | ||||||||||||||||||||||||||||||||||||||||||
| 543 | # process and shut down a live server out from under its owner. So once a stop | ||||||||||||||||||||||||||||||||||||||||||
| 544 | # or eliminate has been sent, never signal this pid again. (Data-dir cleanup for | ||||||||||||||||||||||||||||||||||||||||||
| 545 | # a stopped database is handled by Driver::DESTROY, not by a second signal.) | ||||||||||||||||||||||||||||||||||||||||||
| 546 | 0 | ▶ | sub stop { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
stop: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 547 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 548 | 0 | ▶ | return if $self->{+STOPPED}++ || $self->{+ELIMINATED}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->{"stopped"}++ or $self->{"eliminated"}
MC/DC0%0/2
$self->{"stopped"}++ or $self->{"eliminated"}
$self->{"stopped"}++
$self->{"eliminated"}
Truth table0%0/3
A or B
A= $self->{"stopped"}++B= $self->{"eliminated"}
| |||||||||||||||||||||||||||||||||||||||||||
| 549 | 0 | ▶ | my $pid = $self->{+WATCHER_PID} or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 550 | 0 | $self->_latch_server_reaped; | |||||||||||||||||||||||||||||||||||||||||
| 551 | 0 | kill('INT', $pid); | |||||||||||||||||||||||||||||||||||||||||
| 552 | } | ||||||||||||||||||||||||||||||||||||||||||
| 553 | |||||||||||||||||||||||||||||||||||||||||||
| 554 | 154 | ▶ | sub eliminate { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
eliminate: called CC=4 SCAR=14.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 555 | 154 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 556 | 154 | ▶ | return if $self->{+ELIMINATED}++ || $self->{+STOPPED}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"eliminated"}++ or $self->{"stopped"}
MC/DC50%1/2
$self->{"eliminated"}++ or $self->{"stopped"}
$self->{"eliminated"}++
$self->{"stopped"}
Truth table66%2/3
A or B
A= $self->{"eliminated"}++B= $self->{"stopped"}
| |||||||||||||||||||||||||||||||||||||||||||
| 557 | 51 | ▶ | my $pid = $self->{+WATCHER_PID} or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 558 | 51 | $self->_latch_server_reaped; | |||||||||||||||||||||||||||||||||||||||||
| 559 | 51 | kill('TERM', $pid); | |||||||||||||||||||||||||||||||||||||||||
| 560 | } | ||||||||||||||||||||||||||||||||||||||||||
| 561 | |||||||||||||||||||||||||||||||||||||||||||
| 562 | # Like eliminate(), but the watcher kills the server immediately with the | ||||||||||||||||||||||||||||||||||||||||||
| 563 | # driver's fast_stop_sig (SIGKILL by default, or a clean immediate-shutdown | ||||||||||||||||||||||||||||||||||||||||||
| 564 | # signal) rather than attempting a graceful shutdown. Sets ELIMINATED so the | ||||||||||||||||||||||||||||||||||||||||||
| 565 | # normal teardown signals are never also sent to this (possibly | ||||||||||||||||||||||||||||||||||||||||||
| 566 | # soon-to-be-recycled) pid. | ||||||||||||||||||||||||||||||||||||||||||
| 567 | 15 | ▶ | sub fast_eliminate { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
fast_eliminate: called CC=4 SCAR=14.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 568 | 15 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 569 | 15 | ▶ | return if $self->{+ELIMINATED}++ || $self->{+STOPPED}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"eliminated"}++ or $self->{"stopped"}
MC/DC50%1/2
$self->{"eliminated"}++ or $self->{"stopped"}
$self->{"eliminated"}++
$self->{"stopped"}
Truth table66%2/3
A or B
A= $self->{"eliminated"}++B= $self->{"stopped"}
| |||||||||||||||||||||||||||||||||||||||||||
| 570 | 10 | ▶ | my $pid = $self->{+WATCHER_PID} or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 571 | 10 | $self->_latch_server_reaped; | |||||||||||||||||||||||||||||||||||||||||
| 572 | 10 | kill('USR1', $pid); | |||||||||||||||||||||||||||||||||||||||||
| 573 | } | ||||||||||||||||||||||||||||||||||||||||||
| 574 | |||||||||||||||||||||||||||||||||||||||||||
| 575 | 0 | ▶ | sub detach { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
detach: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 576 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 577 | 0 | ▶ | return if $self->{+DETACHED}++; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 578 | 0 | ▶ | return if $self->{+STOPPED} || $self->{+ELIMINATED}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->{"stopped"} or $self->{"eliminated"}
MC/DC0%0/2
$self->{"stopped"} or $self->{"eliminated"}
$self->{"stopped"}
$self->{"eliminated"}
Truth table0%0/3
A or B
A= $self->{"stopped"}B= $self->{"eliminated"}
| |||||||||||||||||||||||||||||||||||||||||||
| 579 | 0 | ▶ | my $pid = $self->{+WATCHER_PID} or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 580 | 0 | kill('HUP', $pid); | |||||||||||||||||||||||||||||||||||||||||
| 581 | } | ||||||||||||||||||||||||||||||||||||||||||
| 582 | |||||||||||||||||||||||||||||||||||||||||||
| 583 | 134 | ▶ | sub wait { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
wait: called CC=14 SCAR=29.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 584 | 134 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 585 | 134 | ▶ | my $pid = $self->{+WATCHER_PID} or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 586 | |||||||||||||||||||||||||||||||||||||||||||
| 587 | # Give the watcher long enough to finish a graceful shutdown. The watcher | ||||||||||||||||||||||||||||||||||||||||||
| 588 | # escalates to SIGKILL on the server after QDB_STOP_GRACE and then BLOCKS | ||||||||||||||||||||||||||||||||||||||||||
| 589 | # until the server is reaped, so this must outlast the watcher's own | ||||||||||||||||||||||||||||||||||||||||||
| 590 | # escalation schedule (grace + grace/2, plus slack). | ||||||||||||||||||||||||||||||||||||||||||
| 591 | 61 | my $timeout = _stop_grace() * 2 + 2; | |||||||||||||||||||||||||||||||||||||||||
| 592 | |||||||||||||||||||||||||||||||||||||||||||
| 593 | # A watcher that outlives $timeout is almost never hung -- the usual | ||||||||||||||||||||||||||||||||||||||||||
| 594 | # cause is a server the kernel has not been able to kill yet (e.g. stuck | ||||||||||||||||||||||||||||||||||||||||||
| 595 | # in disk-sleep under heavy I/O), with the watcher dutifully blocking on | ||||||||||||||||||||||||||||||||||||||||||
| 596 | # the post-SIGKILL reap. Killing the watcher at that point orphans a | ||||||||||||||||||||||||||||||||||||||||||
| 597 | # still-alive server, so stop() would return "success" while the data dir | ||||||||||||||||||||||||||||||||||||||||||
| 598 | # is locked by a live postmaster/mysqld and the next start on that dir | ||||||||||||||||||||||||||||||||||||||||||
| 599 | # fails on the stale lock file. So past $timeout we only warn, keep | ||||||||||||||||||||||||||||||||||||||||||
| 600 | # waiting on a much longer leash, and SIGKILL the watcher purely as a | ||||||||||||||||||||||||||||||||||||||||||
| 601 | # last resort. Tunable via QDB_STOP_LEASH (extra seconds past $timeout). | ||||||||||||||||||||||||||||||||||||||||||
| 602 | 61 | my $extra = $ENV{QDB_STOP_LEASH}; | |||||||||||||||||||||||||||||||||||||||||
| 603 | 61 | ▶ | $extra = 60 unless defined($extra) && $extra =~ /^\d+$/ && $extra > 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
defined $extra and $extra =~ /^\d+$/ and $extra > 0
defined $extra and $extra =~ /^\d+$/
MC/DC0%0/3
defined $extra and $extra =~ /^\d+$/ and $extra > 0
defined $extra
$extra =~ /^\d+$/
$extra > 0
Truth table25%1/4
A and B and C
A= defined $extraB= $extra =~ /^\d+$/C= $extra > 0
| |||||||||||||||||||||||||||||||||||||||||||
| 604 | 61 | my $leash = $timeout + $extra; | |||||||||||||||||||||||||||||||||||||||||
| 605 | |||||||||||||||||||||||||||||||||||||||||||
| 606 | 61 | my ($warned, $nuked); | |||||||||||||||||||||||||||||||||||||||||
| 607 | 61 | my $start = time; | |||||||||||||||||||||||||||||||||||||||||
| 608 | 61 | ▶ | while(kill(0, $pid)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 609 | 56 | my $waited = time - $start; | |||||||||||||||||||||||||||||||||||||||||
| 610 | |||||||||||||||||||||||||||||||||||||||||||
| 611 | 56 | ▶ | if ($waited > $timeout && !$warned++) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$waited > $timeout and not $warned++
MC/DC0%0/2
$waited > $timeout and not $warned++
$waited > $timeout
not $warned++
Truth table33%1/3
A and not(B)
A= $waited > $timeoutB= not $warned++
| |||||||||||||||||||||||||||||||||||||||||||
| 612 | 0 | warn "Watcher (pid $pid) did not finish within ${timeout}s; the server is probably stuck mid-shutdown, waiting up to ${extra}s longer for it to die"; | |||||||||||||||||||||||||||||||||||||||||
| 613 | } | ||||||||||||||||||||||||||||||||||||||||||
| 614 | |||||||||||||||||||||||||||||||||||||||||||
| 615 | 56 | ▶ | if ($waited > $leash && !$nuked++) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$waited > $leash and not $nuked++
MC/DC0%0/2
$waited > $leash and not $nuked++
$waited > $leash
not $nuked++
Truth table33%1/3
A and not(B)
A= $waited > $leashB= not $nuked++
| |||||||||||||||||||||||||||||||||||||||||||
| 616 | 0 | warn "Watcher (pid $pid) still running after ${leash}s, killing it; the server may survive as an orphan"; | |||||||||||||||||||||||||||||||||||||||||
| 617 | 0 | kill('KILL', $pid); | |||||||||||||||||||||||||||||||||||||||||
| 618 | 0 | $start = time; # from here just wait for the SIGKILL to land | |||||||||||||||||||||||||||||||||||||||||
| 619 | } | ||||||||||||||||||||||||||||||||||||||||||
| 620 | |||||||||||||||||||||||||||||||||||||||||||
| 621 | 56 | sleep 0.02; | |||||||||||||||||||||||||||||||||||||||||
| 622 | } | ||||||||||||||||||||||||||||||||||||||||||
| 623 | |||||||||||||||||||||||||||||||||||||||||||
| 624 | # The watcher has exited; forget its pid so no later teardown signal (e.g. | ||||||||||||||||||||||||||||||||||||||||||
| 625 | # from DESTROY) can land on a recycled pid now owned by another process. | ||||||||||||||||||||||||||||||||||||||||||
| 626 | 61 | delete $self->{+WATCHER_PID}; | |||||||||||||||||||||||||||||||||||||||||
| 627 | |||||||||||||||||||||||||||||||||||||||||||
| 628 | # The server may have outlived the watcher, so verify with a read-only | ||||||||||||||||||||||||||||||||||||||||||
| 629 | # kill(0). Never send a real signal here -- the pid may have been recycled, | ||||||||||||||||||||||||||||||||||||||||||
| 630 | # and a false "alive" should cost a warning, not a wrong kill. | ||||||||||||||||||||||||||||||||||||||||||
| 631 | # | ||||||||||||||||||||||||||||||||||||||||||
| 632 | # Skipped only when the watch loop recorded an exit status, which proves it | ||||||||||||||||||||||||||||||||||||||||||
| 633 | # reaped the server; the pid may since have been recycled, and probing it | ||||||||||||||||||||||||||||||||||||||||||
| 634 | # would report a false "alive" and stall for the whole timeout. A missing | ||||||||||||||||||||||||||||||||||||||||||
| 635 | # data dir proves nothing here -- teardown removes a disposable dir whether | ||||||||||||||||||||||||||||||||||||||||||
| 636 | # or not it managed to stop the server. | ||||||||||||||||||||||||||||||||||||||||||
| 637 | 61 | $self->_latch_server_reaped; | |||||||||||||||||||||||||||||||||||||||||
| 638 | |||||||||||||||||||||||||||||||||||||||||||
| 639 | 61 | ▶ | if (!$self->{+SERVER_REAPED} and my $spid = $self->{+SERVER_PID}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $self->{"server_reaped"} and my $spid = $self->{"server_pid"}
MC/DC50%1/2
not $self->{"server_reaped"} and my $spid = $self->{"server_pid"}
not $self->{"server_reaped"}
my $spid = $self->{"server_pid"}
Truth table66%2/3
not(A) and B
A= not $self->{"server_reaped"}B= my $spid = $self->{"server_pid"}
| |||||||||||||||||||||||||||||||||||||||||||
| 640 | 16 | my $sstart = time; | |||||||||||||||||||||||||||||||||||||||||
| 641 | 16 | ▶ | while (kill(0, $spid)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 642 | 2388 | ▶ | if (time - $sstart > $timeout) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 643 | 12 | warn "Server (pid $spid) still appears to be alive after its watcher exited; its data dir may still be locked"; | |||||||||||||||||||||||||||||||||||||||||
| 644 | 12 | last; | |||||||||||||||||||||||||||||||||||||||||
| 645 | } | ||||||||||||||||||||||||||||||||||||||||||
| 646 | 2376 | sleep 0.02; | |||||||||||||||||||||||||||||||||||||||||
| 647 | } | ||||||||||||||||||||||||||||||||||||||||||
| 648 | } | ||||||||||||||||||||||||||||||||||||||||||
| 649 | } | ||||||||||||||||||||||||||||||||||||||||||
| 650 | |||||||||||||||||||||||||||||||||||||||||||
| 651 | 62 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 652 | 62 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 653 | |||||||||||||||||||||||||||||||||||||||||||
| 654 | 62 | ▶ | if ($self->{+MASTER_PID} == $$) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 655 | 61 | $self->eliminate; | |||||||||||||||||||||||||||||||||||||||||
| 656 | 61 | $self->wait; | |||||||||||||||||||||||||||||||||||||||||
| 657 | } | ||||||||||||||||||||||||||||||||||||||||||
| 658 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 659 | 1 | ▶ | unlink($self->{+LOG_FILE}) if $self->{+LOG_FILE}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 660 | } | ||||||||||||||||||||||||||||||||||||||||||
| 661 | } | ||||||||||||||||||||||||||||||||||||||||||
| 662 | |||||||||||||||||||||||||||||||||||||||||||
| 663 | 62 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 664 | |||||||||||||||||||||||||||||||||||||||||||
| 665 | __END__ | ||||||||||||||||||||||||||||||||||||||||||