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