| 1 | package DBIx::QuickDB::Watcher; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 23 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 23 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | 23 | our $VERSION = '0.000047'; | |||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 23 | ▶ | use Carp qw/croak/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 23 | ▶ | use POSIX qw/:sys_wait_h/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 23 | ▶ | use Time::HiRes qw/sleep time/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 23 | ▶ | use Scalar::Util qw/weaken/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 23 | ▶ | use File::Path qw/remove_tree/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 23 | use DBIx::QuickDB::Util::HashBase qw{ | |||||||||||||||||||||||||||||||||||||||||
| 14 | <db <args | ||||||||||||||||||||||||||||||||||||||||||
| 15 | <server_pid | ||||||||||||||||||||||||||||||||||||||||||
| 16 | <watcher_pid | ||||||||||||||||||||||||||||||||||||||||||
| 17 | <master_pid | ||||||||||||||||||||||||||||||||||||||||||
| 18 | <log_file | ||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | <stopped | ||||||||||||||||||||||||||||||||||||||||||
| 21 | <eliminated | ||||||||||||||||||||||||||||||||||||||||||
| 22 | <detached | ||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | <delete_data | ||||||||||||||||||||||||||||||||||||||||||
| 25 | 23 | ▶ | }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | sub init { | ||||||||||||||||||||||||||||||||||||||||||
| 28 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
init: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | 0 | ▶ | $self->{+MASTER_PID} ||= $$; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$self->{"master_pid"} ||= $$
MC/DC0%0/2
$self->{"master_pid"} ||= $$
$self->{"master_pid"}
$$
Truth table0%0/3
A ||= B
A= $self->{"master_pid"}B= $$
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | 0 | $self->{+LOG_FILE} = $self->{+DB}->gen_log; | |||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | $self->start(); | |||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | 0 | ▶ | weaken($self->{+DB}) if $self->{+MASTER_PID} == $$; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | sub start { | ||||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
start: not called CC=10 SCAR=47.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 0 | ▶ | return if $self->{+SERVER_PID}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | 0 | my ($rh, $wh); | |||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | ▶ | pipe($rh, $wh) or die "Could not open pipe: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | my $pid = fork; | |||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | ▶ | die "Could not fork: $!" unless defined $pid; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | ▶ | if ($pid) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | close($wh); | |||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | waitpid($pid, 0); | |||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | chomp($self->{+WATCHER_PID} = <$rh>); | |||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | chomp($self->{+SERVER_PID} = <$rh>); | |||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | close($rh); | |||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | ▶ | die "Did not get watcher pid!" unless $self->{+WATCHER_PID}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | ▶ | die "Did not get server pid!" unless $self->{+SERVER_PID}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | close($rh); | |||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | POSIX::setsid(); | |||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | setpgrp(0, 0); | |||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | $pid = fork; | |||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | ▶ | die "Could not fork: $!" unless defined $pid; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | ▶ | POSIX::_exit(0) if $pid; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | $wh->autoflush(1); | |||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | print $wh "$$\n"; | |||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | # In watcher now | ||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | ▶ | eval { $self->watch($wh); 1 } or POSIX::_exit(1); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | POSIX::_exit(0); | |||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | sub watch { | ||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
watch: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | my ($wh) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | $0 = 'db-quick-watcher'; | |||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | my $kill = ''; | |||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | my $hup = 0; | |||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | local $SIG{TERM} = sub { $kill = 'TERM' }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | ▶ | local $SIG{INT} = sub { $kill = 'INT' }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | ▶ | local $SIG{HUP} = sub { $hup = 1 }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | my $start_pid = $$; | |||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | my $pid = $self->spawn(); | |||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | print $wh "$pid\n"; | |||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | close($wh); | |||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | my $mpid = $self->{+MASTER_PID}; | |||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | ▶ | my $spid = $self->{+SERVER_PID} or die "No server pid"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | my $ddir = $self->{+DB}->dir; | |||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | ▶ | my $ssig = $self->{+DB}->stop_sig // 'TERM'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->{"db"}->stop_sig // "TERM"
MC/DC0%0/1
$self->{"db"}->stop_sig // "TERM"
$self->{"db"}->stop_sig
Truth table0%0/2
A
A= $self->{"db"}->stop_sig
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | # Ignore SIGTERM/SIGINT before exec so the watcher cannot be killed | ||||||||||||||||||||||||||||||||||||||||||
| 99 | # during startup before _do_watch installs its signal handlers. | ||||||||||||||||||||||||||||||||||||||||||
| 100 | # SIG_IGN persists across exec, and any pending signal will be held | ||||||||||||||||||||||||||||||||||||||||||
| 101 | # until _do_watch replaces these with proper handlers. | ||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | $SIG{TERM} = 'IGNORE'; | |||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | $SIG{INT} = 'IGNORE'; | |||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | exec( | |||||||||||||||||||||||||||||||||||||||||
| 106 | $^X, '-Ilib', | ||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | '-e' => "require DBIx::QuickDB::Watcher; DBIx::QuickDB::Watcher->_do_watch()", | ||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | master_pid => $mpid, | ||||||||||||||||||||||||||||||||||||||||||
| 111 | data_dir => $ddir, | ||||||||||||||||||||||||||||||||||||||||||
| 112 | server_pid => $spid, | ||||||||||||||||||||||||||||||||||||||||||
| 113 | signal => $ssig, | ||||||||||||||||||||||||||||||||||||||||||
| 114 | kill => $kill, | ||||||||||||||||||||||||||||||||||||||||||
| 115 | hup => $hup, | ||||||||||||||||||||||||||||||||||||||||||
| 116 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | sub _do_watch { | ||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | ▶ | my $class = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_do_watch: not called CC=15 SCAR=54.8
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | 0 | $0 = 'db-quick-watcher'; | |||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | my %params = @ARGV; | |||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | 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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 127 | 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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | ▶ | local $SIG{TERM} = sub { $kill = 'TERM' }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 129 | 0 | ▶ | local $SIG{INT} = sub { $kill = 'INT' }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 130 | 0 | ▶ | local $SIG{HUP} = sub { $hup = 1 }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | my $blah; | |||||||||||||||||||||||||||||||||||||||||
| 133 | 0 | close(STDIN); | |||||||||||||||||||||||||||||||||||||||||
| 134 | 0 | ▶ | open(STDIN, '<', \$blah) or warn "$!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | 0 | ▶ | my $master_pid = $params{master_pid} or die "No master pid provided"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 137 | 0 | ▶ | my $server_pid = $params{server_pid} or die "No server pid provided"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | ▶ | my $data_dir = $params{data_dir} or die "No data dir provided"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 139 | 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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | 0 | my $hupped = 0; | |||||||||||||||||||||||||||||||||||||||||
| 142 | 0 | ▶ | while (!$kill) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | 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
| |||||||||||||||||||||||||||||||||||||||||||
| 144 | 0 | close(STDOUT); | |||||||||||||||||||||||||||||||||||||||||
| 145 | 0 | ▶ | open(STDOUT, '>', \$blah) or warn "$!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 146 | 0 | close(STDERR); | |||||||||||||||||||||||||||||||||||||||||
| 147 | 0 | ▶ | open(STDERR, '>', \$blah) or warn "$!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||
| 150 | 0 | sleep 0.1; | |||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||
| 152 | 0 | ▶ | next if kill(0, $master_pid); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 153 | 0 | $kill = 'TERM'; | |||||||||||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||
| 156 | 0 | ▶ | unless (eval { $class->_watcher_terminate(send_sig => $signal, got_sig => $kill, pid => $server_pid, dir => $data_dir); 1 }) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 157 | 0 | my $err = $@; | |||||||||||||||||||||||||||||||||||||||||
| 158 | 0 | eval { warn $@ }; | |||||||||||||||||||||||||||||||||||||||||
| 159 | 0 | POSIX::_exit(1); | |||||||||||||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||
| 162 | 0 | POSIX::_exit(0); | |||||||||||||||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||
| 165 | sub spawn { | ||||||||||||||||||||||||||||||||||||||||||
| 166 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
spawn: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||
| 168 | 0 | ▶ | croak "Extra spawn" if $self->{+SERVER_PID}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||
| 170 | 0 | my $db = $self->{+DB}; | |||||||||||||||||||||||||||||||||||||||||
| 171 | 0 | ▶ | my $args = $self->{+ARGS} || []; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->{'args'} || []
MC/DC0%0/1
$self->{'args'} || []
$self->{'args'}
Truth table0%0/2
A
A= $self->{'args'}
| |||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||
| 173 | 0 | my $init_pid = $$; | |||||||||||||||||||||||||||||||||||||||||
| 174 | 0 | my ($pid, $log_file) = $db->run_command([$db->start_command, @$args], {no_wait => 1, log_file => $self->{+LOG_FILE}}); | |||||||||||||||||||||||||||||||||||||||||
| 175 | 0 | $self->{+SERVER_PID} = $pid; | |||||||||||||||||||||||||||||||||||||||||
| 176 | 0 | $self->{+LOG_FILE} = $log_file; | |||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||
| 178 | 0 | return $pid; | |||||||||||||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||
| 181 | sub _watcher_terminate { | ||||||||||||||||||||||||||||||||||||||||||
| 182 | 0 | ▶ | my $class = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_watcher_terminate: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||||||||||||||||
| 183 | 0 | my %params = @_; | |||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||
| 185 | 0 | ▶ | my $pid = $params{pid} or die "No pid"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 186 | 0 | ▶ | my $dir = $params{dir} or die "No dir"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||
| 188 | 0 | my $got_sig = $params{got_sig}; | |||||||||||||||||||||||||||||||||||||||||
| 189 | 0 | ▶ | my $send_sig = $params{send_sig} // $got_sig // 'TERM'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/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 table0%0/3
A // B
A= $params{"send_sig"}B= $got_sig
| |||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||
| 191 | 0 | $class->_watcher_kill($send_sig, $pid); | |||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||
| 193 | 0 | ▶ | if ($got_sig && $got_sig eq 'TERM') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$got_sig and $got_sig eq "TERM"
MC/DC0%0/2
$got_sig and $got_sig eq "TERM"
$got_sig
$got_sig eq "TERM"
Truth table0%0/3
A and B
A= $got_sigB= $got_sig eq "TERM"
| |||||||||||||||||||||||||||||||||||||||||||
| 194 | # Ignore errors here. | ||||||||||||||||||||||||||||||||||||||||||
| 195 | 0 | my $err = []; | |||||||||||||||||||||||||||||||||||||||||
| 196 | 0 | ▶ | remove_tree($dir, {safe => 1, error => \$err}) if -d $dir; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||
| 200 | sub _watcher_kill { | ||||||||||||||||||||||||||||||||||||||||||
| 201 | 0 | ▶ | my $class = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_watcher_kill: not called CC=13 SCAR=52.0
| |||||||||||||||||||||||||||||||||||||||||||
| 202 | 0 | my ($sig, $pid) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||
| 204 | 0 | ▶ | kill($sig, $pid) or die "Could not send kill signal"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||
| 206 | # How long to wait for a graceful shutdown before escalating to SIGKILL, | ||||||||||||||||||||||||||||||||||||||||||
| 207 | # and how much longer before giving up entirely. Keep this generous: a slow | ||||||||||||||||||||||||||||||||||||||||||
| 208 | # or loaded host (e.g. a CPAN smoke box) can need well over a few seconds to | ||||||||||||||||||||||||||||||||||||||||||
| 209 | # finish PostgreSQL's shutdown checkpoint. A premature SIGKILL leaves the | ||||||||||||||||||||||||||||||||||||||||||
| 210 | # data dir in a crash-recovery state, and a clone of that dir then replays | ||||||||||||||||||||||||||||||||||||||||||
| 211 | # WAL on first start, jumping SERIAL sequences forward by SEQ_LOG_VALS (32) | ||||||||||||||||||||||||||||||||||||||||||
| 212 | # -- silently corrupting cloned databases. Tunable via QDB_STOP_GRACE. | ||||||||||||||||||||||||||||||||||||||||||
| 213 | 0 | my $kill_after = $ENV{QDB_STOP_GRACE}; | |||||||||||||||||||||||||||||||||||||||||
| 214 | 0 | ▶ | $kill_after = 4 unless defined($kill_after) && $kill_after =~ /^\d+$/ && $kill_after > 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
defined $kill_after and $kill_after =~ /^\d+$/ and $kill_after > 0
defined $kill_after and $kill_after =~ /^\d+$/
MC/DC0%0/3
defined $kill_after and $kill_after =~ /^\d+$/ and $kill_after > 0
defined $kill_after
$kill_after =~ /^\d+$/
$kill_after > 0
Truth table0%0/4
A and B and C
A= defined $kill_afterB= $kill_after =~ /^\d+$/C= $kill_after > 0
| |||||||||||||||||||||||||||||||||||||||||||
| 215 | 0 | my $give_up = $kill_after * 2; | |||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||
| 217 | 0 | my ($check, $exit, $killed); | |||||||||||||||||||||||||||||||||||||||||
| 218 | 0 | my $start = time; | |||||||||||||||||||||||||||||||||||||||||
| 219 | 0 | ▶ | until ($check) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 220 | 0 | local $?; | |||||||||||||||||||||||||||||||||||||||||
| 221 | 0 | my $delta = time - $start; | |||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||
| 223 | 0 | ▶ | if ($delta >= $kill_after) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 224 | 0 | ▶ | if ($killed) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 225 | 0 | my $delta2 = time - $killed; | |||||||||||||||||||||||||||||||||||||||||
| 226 | 0 | ▶ | next unless $delta2 >= 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||||||
| 229 | 0 | warn "Server taking too long to shut down, sending SIGKILL"; | |||||||||||||||||||||||||||||||||||||||||
| 230 | 0 | $killed = time; | |||||||||||||||||||||||||||||||||||||||||
| 231 | 0 | kill('KILL', $pid); | |||||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||||||
| 233 | 0 | ▶ | last if $delta > $give_up; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||
| 236 | 0 | $check = waitpid($pid, WNOHANG); | |||||||||||||||||||||||||||||||||||||||||
| 237 | 0 | $exit = $?; | |||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||
| 239 | 0 | sleep 0.1; | |||||||||||||||||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||
| 242 | 0 | ▶ | die "PID refused to exit" unless $check; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 243 | 0 | ▶ | die "Something else reaped our process" if $check < 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 244 | 0 | ▶ | die "Reaped the wrong process '$check' instead of '$pid'" if $pid != $check; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||
| 246 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||||||||
| 249 | sub stop { | ||||||||||||||||||||||||||||||||||||||||||
| 250 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
stop: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 251 | 0 | ▶ | return if $self->{+STOPPED}++; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 252 | 0 | ▶ | my $pid = $self->{+WATCHER_PID} or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 253 | 0 | kill('INT', $pid); | |||||||||||||||||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||||||||
| 256 | sub eliminate { | ||||||||||||||||||||||||||||||||||||||||||
| 257 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
eliminate: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 258 | 0 | ▶ | return if $self->{+ELIMINATED}++; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 259 | 0 | ▶ | my $pid = $self->{+WATCHER_PID} or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 260 | 0 | kill('TERM', $pid); | |||||||||||||||||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||
| 263 | sub detach { | ||||||||||||||||||||||||||||||||||||||||||
| 264 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
detach: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 265 | 0 | ▶ | return if $self->{+DETACHED}++; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 266 | 0 | ▶ | my $pid = $self->{+WATCHER_PID} or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 267 | 0 | kill('HUP', $pid); | |||||||||||||||||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||||||
| 270 | sub wait { | ||||||||||||||||||||||||||||||||||||||||||
| 271 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
wait: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 272 | 0 | ▶ | my $pid = $self->{+WATCHER_PID} or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||||||
| 274 | # Give the watcher long enough to finish a graceful shutdown before we | ||||||||||||||||||||||||||||||||||||||||||
| 275 | # SIGKILL it. The watcher escalates to SIGKILL on the server after | ||||||||||||||||||||||||||||||||||||||||||
| 276 | # QDB_STOP_GRACE and gives up at twice that, so this must outlast the | ||||||||||||||||||||||||||||||||||||||||||
| 277 | # watcher's give-up or we would kill it mid-shutdown and orphan a | ||||||||||||||||||||||||||||||||||||||||||
| 278 | # half-stopped server. Defaults to 10s (grace 4 -> give-up 8 -> 10). | ||||||||||||||||||||||||||||||||||||||||||
| 279 | 0 | my $grace = $ENV{QDB_STOP_GRACE}; | |||||||||||||||||||||||||||||||||||||||||
| 280 | 0 | ▶ | $grace = 4 unless defined($grace) && $grace =~ /^\d+$/ && $grace > 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
defined $grace and $grace =~ /^\d+$/ and $grace > 0
defined $grace and $grace =~ /^\d+$/
MC/DC0%0/3
defined $grace and $grace =~ /^\d+$/ and $grace > 0
defined $grace
$grace =~ /^\d+$/
$grace > 0
Truth table0%0/4
A and B and C
A= defined $graceB= $grace =~ /^\d+$/C= $grace > 0
| |||||||||||||||||||||||||||||||||||||||||||
| 281 | 0 | my $timeout = $grace * 2 + 2; | |||||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||||||
| 283 | 0 | my $start = time; | |||||||||||||||||||||||||||||||||||||||||
| 284 | 0 | ▶ | while(kill(0, $pid)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 285 | 0 | my $waited = time - $start; | |||||||||||||||||||||||||||||||||||||||||
| 286 | 0 | ▶ | if ($waited > $timeout) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 287 | 0 | kill('KILL', $pid); | |||||||||||||||||||||||||||||||||||||||||
| 288 | 0 | $start = time; | |||||||||||||||||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||||||||||||||||
| 290 | 0 | sleep 0.02; | |||||||||||||||||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||||||||||
| 294 | sub DESTROY { | ||||||||||||||||||||||||||||||||||||||||||
| 295 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
DESTROY: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||||||||||||
| 297 | 0 | ▶ | if ($self->{+MASTER_PID} == $$) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 298 | 0 | $self->eliminate; | |||||||||||||||||||||||||||||||||||||||||
| 299 | 0 | $self->wait; | |||||||||||||||||||||||||||||||||||||||||
| 300 | } | ||||||||||||||||||||||||||||||||||||||||||
| 301 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 302 | 0 | ▶ | unlink($self->{+LOG_FILE}) if $self->{+LOG_FILE}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||||||||||||||||||||||
| 304 | } | ||||||||||||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||||||||||
| 306 | 23 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||||||||||
| 308 | __END__ | ||||||||||||||||||||||||||||||||||||||||||