lib/DBIx/QuickDB/Driver.pm

filter: Statementstmt 64.7% 202 / 312 Branchbran 44.7% 76 / 170 Conditioncond 32.7% 19 / 58 MC/DCmcdc 8.3% 3 / 36 Subroutinesub 63.4% 33 / 52 Podpod 84.8% 28 / 33 totaltotal 54.6% 361 / 661 CC 111 SCAR 30.9
CC
111
Cov
55%
clone
107.9
run_command
57.5
resync
42.0
CRAP 22.1
« blib/lib/DBIx/QuickDB/Driver/PostgreSQL.pm ↑ Summary blib/lib/DBIx/QuickDB/Driver/DuckDB.pm »
1 package DBIx::QuickDB::Driver;
2 61 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 61 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 61 our $VERSION = '0.000066';
6  
7 61 ▶use Carp qw/croak confess/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 61 ▶use File::Temp qw/tempdir/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 61 ▶use POSIX ":sys_wait_h";
Subroutine BEGIN: called CC=1 SCAR=0.0
10 61 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 61 ▶use Time::HiRes qw/sleep time/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 61 ▶use DBIx::QuickDB::Util qw/clone_dir env_timeout remove_tree_or_quarantine disconnect_dbi_handles/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 61 ▶use DBIx::QuickDB::Watcher;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 61 ▶use DBIx::QuickDB::Util::HashBase qw{
Subroutine BEGIN: called CC=1 SCAR=0.0
18 -root_pid
19 -dir
20 -_cleanup
21 -autostop -autostart
22 verbose
23 -_log_id
24 username
25 password
26 env_vars
27 <watcher
28 <cloned_from
29 -fast_destroy
30 -no_disconnect_handles
31 61 };
32  
33 0 ▶sub viable { (0, "viable() is not implemented for the " . $_[0]->name . " driver") }
Subroutine viable: not called CC=1 SCAR=6.9
Pod documented
34  
35 0 ▶sub socket { confess "socket() is not implemented for the " . $_[0]->name . " driver" }
Subroutine socket: not called CC=1 SCAR=6.9
Pod documented
36 0 ▶sub load_sql { confess "load_sql() is not implemented for the " . $_[0]->name . " driver" }
Subroutine load_sql: not called CC=1 SCAR=6.9
Pod documented
37 0 ▶sub bootstrap { confess "bootstrap() is not implemented for the " . $_[0]->name . " driver" }
Subroutine bootstrap: not called CC=1 SCAR=6.9
38 0 ▶sub connect_string { confess "connect_string() is not implemented for the " . $_[0]->name . " driver" }
Subroutine connect_string: not called CC=1 SCAR=6.9
Pod documented
39 0 ▶sub start_command { confess "start_command() is not implemented for the " . $_[0]->name . " driver" }
Subroutine start_command: not called CC=1 SCAR=6.9
Pod documented
40 0 ▶sub shell_command { confess "shell_command() is not implemented for the " . $_[0]->name . " driver" }
Subroutine shell_command: not called CC=1 SCAR=6.9
Pod documented
41  
42 35 ▶sub error_log { undef }
Subroutine error_log: called CC=1 SCAR=0.0
Pod undocumented
43  
44 36 ▶sub read_error_log {
Subroutine read_error_log: called CC=4 SCAR=14.1
Pod undocumented
45 36 my $self = shift;
46 36 ▶ my $log = $self->error_log or return "";
Branch: 1 branch
BranchTrueFalseTotal
my $log = $self->error_log or return "" 1 35 36
47 1 ▶ return "" unless -f $log;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $log 0 1 1
48 1 ▶ open(my $fh, '<', $log) or return "Could not open error log '$log': $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $log or return "Could not open error log '${log}': $!" 1 0 1
49 1 return join "" => <$fh>;
50 }
51  
52 # Slurp a file for diagnostics; returns "" if it is missing or unreadable.
53 36 ▶sub _read_file {
Subroutine _read_file: called CC=4 SCAR=15.2
54 36 my $self = shift;
55 36 my ($file) = @_;
56 36 ▶ return "" unless $file && -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless $file and -f $file 0 36 36
Condition33%1/3
$file and -f $file
!ll&&!rl&&r
0036
MC/DC0%0/2
$file and -f $file
$file -f $file
Truth table33%1/3
A and B
A= $fileB= -f $file
ABresult
0X0
100
111
57 36 ▶ open(my $fh, '<', $file) or return "";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or return "" 36 0 36
58 36 return join "" => <$fh>;
59 }
60  
61 9 ▶sub list_env_vars { qw/DBI_USER DBI_PASS DBI_DSN/ }
Subroutine list_env_vars: called CC=1 SCAR=0.0
Pod documented
62  
63 0 ▶sub version_string { 'unknown' }
Subroutine version_string: not called CC=1 SCAR=6.9
Pod documented
64  
65 8 ▶sub stop_sig { 'TERM' }
Subroutine stop_sig: called CC=1 SCAR=0.0
Pod documented
66  
67 # Signal the watcher sends to the server for fast (disposable) teardown via
68 # destroy_quietly()/fast_destroy. The default SIGKILL is the quickest possible
69 # stop. Drivers whose server leaks an OS resource on a hard kill should override
70 # this with a clean-but-immediate shutdown signal (see PostgreSQL, which returns
71 # 'QUIT' so the postmaster releases its SysV semaphores instead of orphaning
72 # them). The watcher escalates to SIGKILL if the chosen signal does not exit the
73 # server promptly, so teardown always completes.
74 9 ▶sub fast_stop_sig { 'KILL' }
Subroutine fast_stop_sig: called CC=1 SCAR=0.0
Pod documented
75  
76 0 ▶sub write_config {}
Subroutine write_config: not called CC=1 SCAR=0.0
Pod documented
77  
78 0 ▶sub do_in_env {
Subroutine do_in_env: not called CC=2 SCAR=17.9
Pod documented
79 0 my $self = shift;
80 0 my ($code) = @_;
81  
82 0 my $old = $self->mask_env_vars;
83  
84 0 my $ok = eval { $code->(); 1 };
85 0 my $err = $@;
86  
87 0 $self->unmask_env_vars($old);
88  
89 0 ▶ die $err unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 0 0
90  
91 0 return;
92 }
93  
94 9 ▶sub mask_env_vars {
Subroutine mask_env_vars: called CC=6 SCAR=22.0
Pod documented
95 9 my $self = shift;
96  
97 9 my %old;
98  
99 9 for my $var ($self->list_env_vars) {
100 27 ▶ next unless defined $ENV{$var};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ENV{$var} 27 0 27
101 0 $old{$var} = delete $ENV{$var};
102 }
103  
104 9 ▶ my $env_vars = $self->env_vars || {};
Condition50%1/2
$self->env_vars || {}
l!l
90
MC/DC0%0/1
$self->env_vars || {}
$self->env_vars
Truth table50%1/2
A
A= $self->env_vars
Aresult
11
00
105 9 for my $var (keys %$env_vars) {
106 0 ▶ $old{$var} = delete $ENV{$var} unless defined $old{$var};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $old{$var} 0 0 0
107 0 $ENV{$var} = $env_vars->{$var};
108 }
109  
110 9 return \%old;
111 }
112  
113 0 ▶sub unmask_env_vars {
Subroutine unmask_env_vars: not called CC=3 SCAR=24.8
Pod documented
114 0 my $self = shift;
115 0 my ($old) = @_;
116  
117 0 for my $var (keys %$old) {
118 0 my $val = $old->{$var};
119  
120 0 ▶ if (defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) { } 0 0 0
121 0 $ENV{$var} = $val;
122 }
123 else {
124 0 delete $ENV{$var};
125 }
126 }
127  
128 0 return;
129 }
130  
131 4 ▶sub name {
Subroutine name: called CC=2 SCAR=7.4
Pod documented
132 4 my $in = shift;
133 4 ▶ my $type = blessed($in) || $in;
Condition33%1/3
builtin::blessed($in) || $in
l!l&&r!l&&!r
040
MC/DC0%0/2
builtin::blessed($in) || $in
builtin::blessed($in) $in
Truth table33%1/3
A || B
A= builtin::blessed($in)B= $in
ABresult
1X1
011
000
134  
135 4 $type =~ s/^DBIx::QuickDB::Driver:://;
136  
137 4 return $type;
138 }
139  
140 74 ▶sub init {
Subroutine init: called CC=5 SCAR=16.8
Pod documented
141 74 my $self = shift;
142  
143 74 ▶ confess "'dir' is a required attribute" unless $self->{+DIR};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"dir"} 0 74 74
144  
145 74 $self->{+ROOT_PID} = $$;
146 74 $self->{+_CLEANUP} = delete $self->{cleanup};
147  
148 74 ▶ $self->{+USERNAME} = '' unless defined $self->{+USERNAME};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"username"} 74 0 74
149 74 ▶ $self->{+PASSWORD} = '' unless defined $self->{+PASSWORD};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"password"} 74 0 74
150  
151 74 ▶ $self->{+ENV_VARS} ||= {};
Condition50%1/2
$self->{"env_vars"} ||= {}
l!l
074
MC/DC0%0/1
$self->{"env_vars"} ||= {}
$self->{"env_vars"}
Truth table50%1/2
A
A= $self->{"env_vars"}
Aresult
11
00
152  
153 74 return;
154 }
155  
156 0 ▶sub clone_data {
Subroutine clone_data: not called CC=1 SCAR=6.9
Pod documented
157 0 my $self = shift;
158  
159 return (
160 USERNAME() => $self->{+USERNAME},
161 PASSWORD() => $self->{+PASSWORD},
162 VERBOSE() => $self->{+VERBOSE},
163 AUTOSTOP() => $self->{+AUTOSTOP},
164 AUTOSTART() => $self->{+AUTOSTART},
165  
166 cleanup => $self->{+_CLEANUP},
167  
168 FAST_DESTROY() => $self->{+FAST_DESTROY},
169  
170 NO_DISCONNECT_HANDLES() => $self->{+NO_DISCONNECT_HANDLES},
171  
172 0 ENV_VARS() => {%{$self->{+ENV_VARS}}},
173 );
174 }
175  
176 0 ▶sub resync {
Subroutine resync: not called CC=6 SCAR=37.4
Pod documented
177 0 my $self = shift;
178  
179 0 ▶ my $from = $self->{+CLONED_FROM} or croak "No original source to sync from";
Branch: 1 branch
BranchTrueFalseTotal
my $from = $self->{"cloned_from"} or croak("No original source to sync from") 0 0 0
180  
181 0 my $started = $self->started;
182 0 ▶ $self->stop if $started;
Branch: 1 branch
BranchTrueFalseTotal
if $started 0 0 0
183  
184 0 ▶ clone_dir($from, $self->{+DIR}, verbose => (($self->{+VERBOSE} // 0) > 2) ? 1 : 0, checksum => 1);
Branch: 1 branch
BranchTrueFalseTotal
($self->{'verbose'} // 0) > 2 ? : 0 0 0
Condition0%0/2
$self->{"verbose"} // 0
l!l
00
MC/DC0%0/1
$self->{"verbose"} // 0
$self->{"verbose"}
Truth table0%0/2
A
A= $self->{"verbose"}
Aresult
11
00
185  
186 0 $self->write_config();
187  
188 0 ▶ $self->start if $started;
Branch: 1 branch
BranchTrueFalseTotal
if $started 0 0 0
189 }
190  
191 0 ▶sub clone {
Subroutine clone: not called CC=11 SCAR=46.8
Pod documented
192 0 my $self = shift;
193 0 my %params = @_;
194  
195 0 ▶ confess "Cannot clone a started database, please stop it first."
Branch: 1 branch
BranchTrueFalseTotal
if $self->started 0 0 0
196 if $self->started;
197  
198 0 my $orig_dir = $self->{+DIR};
199 # $$ in the template for the same reason as build_db: rand()-derived
200 # names collide across forked/identically-seeded processes.
201 0 ▶ my $new_dir = delete $params{dir} // tempdir("DB-QUICK-CLONE-$$-XXXXXX", CLEANUP => 0, $ENV{QDB_TMPDIR} ? (DIR => $ENV{QDB_TMPDIR}) : (TMPDIR => 1));
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'QDB_TMPDIR'} ? : 0 0 0
Condition0%0/3
delete $params{"dir"} // tempdir("DB-QUICK-CLONE-${$}-XXXXXX", "CLEANUP", 0, $ENV{"QDB_TMPDIR"} ? ("DIR", $ENV{"QDB_TMPDIR"}) : ("TMPDIR", 1))
l!l&&r!l&&!r
000
MC/DC0%0/2
delete $params{"dir"} // tempdir("DB-QUICK-CLONE-${$}-XXXXXX", "CLEANUP", 0, $ENV{"QDB_TMPDIR"} ? ("DIR", $ENV{"QDB_TMPDIR"}) : ("TMPDIR", 1))
delete $params{"dir"} tempdir("DB-QUICK-CLONE-${$}-XXXXXX", "CLEANUP", 0, $ENV{"QDB_TMPDIR"} ? ("DIR", $ENV{"QDB_TMPDIR"}) : ("TMPDIR", 1))
Truth table0%0/3
A // B
A= delete $params{"dir"}B= tempdir("DB-QUICK-CLONE-${$}-XXXXXX", "CLEANUP", 0, $ENV{"QDB_TMPDIR"} ? ("DIR", $ENV{"QDB_TMPDIR"}) : ("TMPDIR", 1))
ABresult
1X1
011
000
202  
203 0 ▶ clone_dir($orig_dir, $new_dir, verbose => (($self->{+VERBOSE} // 0) > 2) ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
($self->{'verbose'} // 0) > 2 ? : 0 0 0
Condition0%0/2
$self->{"verbose"} // 0
l!l
00
MC/DC0%0/1
$self->{"verbose"} // 0
$self->{"verbose"}
Truth table0%0/2
A
A= $self->{"verbose"}
Aresult
11
00
204  
205 0 my $class = ref($self);
206 my %ok = (
207 cleanup => 1,
208 0 map {$_ => 1} DBIx::QuickDB::Util::HashBase::attr_list($class),
209 );
210 0 my @bad = grep { !$ok{$_} } keys %params;
211  
212 0 ▶ confess "Invalid options to clone(): " . join(', ' => @bad)
Branch: 1 branch
BranchTrueFalseTotal
if @bad 0 0 0
213 if @bad;
214  
215 0 my $clone = $class->new(
216 $self->clone_data,
217  
218 %params,
219  
220 DIR() => $new_dir,
221  
222 WATCHER() => undef,
223  
224 CLONED_FROM() => $orig_dir,
225 );
226  
227 # A clone must start with fresh logs. clone_dir copied the source's
228 # error.log and cmd-log-* files along with the data dir, which made a
229 # clone's logs look like they contained the parent's history -- confusing
230 # when debugging the clone (and it can trip naive log scanning).
231 0 ▶ if (my $log = $clone->error_log) { unlink($log) if -f $log }
Branch: 2 branches
BranchTrueFalseTotal
if (my $log = $clone->error_log) 0 0 0
if -f $log 0 0 0
232 0 ▶ unlink($_) for glob("$new_dir/cmd-log-*");
Subroutine BEGIN: called CC=1 SCAR=0.2
233  
234 # Defensive hygiene, not the load-bearing guard: start() scrubs this too. It
235 # keeps a clone that is never started from carrying its template's crash.
236 0 unlink(DBIx::QuickDB::Watcher->server_exit_status_file($new_dir));
237  
238 0 $clone->write_config();
239 0 ▶ $clone->start if $clone->{+AUTOSTART};
Branch: 1 branch
BranchTrueFalseTotal
if $clone->{"autostart"} 0 0 0
240  
241 0 return $clone;
242 }
243  
244 69 ▶sub gen_log {
Subroutine gen_log: called CC=2 SCAR=7.1
Pod undocumented
245 69 my $self = shift;
246 69 ▶ return if $self->no_log(@_);
Branch: 1 branch
BranchTrueFalseTotal
if $self->no_log(@_) 0 69 69
247 69 return $self->{+DIR} . "/cmd-log-$$-" . $self->{+_LOG_ID}++;
248 }
249  
250 87 ▶sub no_log {
Subroutine no_log: called CC=3 SCAR=13.3
Pod documented
251 87 my $self = shift;
252 87 my ($params) = @_;
253 87 ▶ return $self->{+VERBOSE} || $params->{no_log} || $ENV{DB_VERBOSE};
Condition33%2/6
$self->{'verbose'} || $params->{'no_log'} || $ENV{'DB_VERBOSE'}
l!l&&r!l&&!r
0087
$self->{"verbose"} or $params->{"no_log"}
l!l&&r!l&&!r
0087
MC/DC0%0/3
$self->{'verbose'} || $params->{'no_log'} || $ENV{'DB_VERBOSE'}
$self->{"verbose"} $params->{"no_log"} $ENV{'DB_VERBOSE'}
Truth table25%1/4
A or B || C
A= $self->{"verbose"}B= $params->{"no_log"}C= $ENV{'DB_VERBOSE'}
ABCresult
1XX1
01X1
0011
0000
254 }
255  
256 18 ▶sub run_command {
Subroutine run_command: called CC=15 SCAR=40.5
Pod documented
257 18 my $self = shift;
258 18 my ($cmd, $params) = @_;
259  
260 18 my $no_log = $self->no_log($params);
261 18 ▶ my $log_file = $params->{log_file} || ($no_log ? undef : $self->gen_log);
Branch: 1 branch
BranchTrueFalseTotal
$no_log ? : 0 0 0
Condition33%1/3
$params->{'log_file'} || ($no_log ? undef : $self->gen_log)
l!l&&r!l&&!r
1800
MC/DC0%0/2
$params->{'log_file'} || ($no_log ? undef : $self->gen_log)
$params->{'log_file'} ($no_log ? undef : $self->gen_log)
Truth table33%1/3
A || B
A= $params->{'log_file'}B= ($no_log ? undef : $self->gen_log)
ABresult
1X1
011
000
262  
263 18 my $pid = fork();
264 18 ▶ croak "Could not fork" unless defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 18 18
265  
266 18 ▶ if ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) 9 9 18
267 9 local $?;
268 9 ▶ return ($pid, $log_file) if $params->{no_wait};
Branch: 1 branch
BranchTrueFalseTotal
if $params->{"no_wait"} 9 0 9
269 0 my $ret = waitpid($pid, 0);
270 0 my $exit = $?;
271 0 ▶ die "waitpid returned $ret" unless $ret == $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless $ret == $pid 0 0 0
272  
273 0 ▶ return unless $exit;
Branch: 1 branch
BranchTrueFalseTotal
unless $exit 0 0 0
274  
275 0 my $log = "";
276 0 ▶ unless ($no_log) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($no_log) 0 0 0
277 0 ▶ open(my $fh, '<', $log_file) or warn "Failed to open log: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $log_file or warn "Failed to open log: $!" 0 0 0
278 0 $log = eval { join "" => <$fh> };
279 }
280 0 my $error_log = $self->read_error_log;
281 0 ▶ $log .= "\n=== error log ===\n$error_log" if length $error_log;
Branch: 1 branch
BranchTrueFalseTotal
if length $error_log 0 0 0
282 0 croak "Failed to run command '" . join(' ' => @$cmd) . "' ($exit)\n$log";
283 }
284  
285 9 $self->mask_env_vars;
286  
287 9 ▶ unless ($no_log) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($no_log) 9 0 9
288 9 ▶ open(my $log, '>', $log_file) or die "Could not open log file ($log_file): $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $log, ">", $log_file or die "Could not open log file ($log_file): $!" 9 0 9
289 9 close(STDOUT);
290 9 open(STDOUT, '>&', $log);
291 9 close(STDERR);
292 9 open(STDERR, '>&', $log);
293 }
294  
295 9 ▶ if (my $file = $params->{stdin}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $file = $params->{"stdin"}) 0 9 9
296 0 close(STDIN);
297 0 ▶ open(STDIN, '<', $file) or die "Could not open new STDIN ($file): $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDIN, "<", $file or die "Could not open new STDIN ($file): $!" 0 0 0
298 }
299  
300 9 exec(@$cmd);
301 }
302  
303 66 ▶sub should_cleanup { shift->{+_CLEANUP} }
Subroutine should_cleanup: called CC=1 SCAR=0.0
Pod documented
304  
305 # Flush server state durably to disk so a hard kill mid-shutdown cannot leave
306 # the data dir needing crash recovery. Drivers that benefit (e.g. PostgreSQL)
307 # override this; the default is a no-op.
308 0 ▶sub checkpoint { }
Subroutine checkpoint: not called CC=1 SCAR=0.0
Pod undocumented
309  
310 17 ▶sub cleanup {
Subroutine cleanup: called CC=3 SCAR=11.0
Pod documented
311 17 my $self = shift;
312  
313 # Close any of this process's DBI handles to this db BEFORE deleting the
314 # data dir. Essential on Windows: a still-open SQLite database file cannot
315 # be unlinked, which leaves the directory non-empty so its removal fails.
316 # The watcher teardown paths (stop/DESTROY/destroy_quietly) already
317 # disconnect before signalling, but the watcher-less path -- SQLite, whose
318 # stop() is a no-op -- reached cleanup() with handles still open. Idempotent:
319 # a second disconnect simply finds no matching handles.
320 17 $self->_disconnect_handles;
321  
322 # Best-effort, idempotent, and Windows-aware. If retries cannot delete the
323 # tree, retry the quarantine rename with Windows-aware backoff, then retry a
324 # successful quarantine during later cleanups and at process shutdown. This
325 # is essential for SQLite on Windows: a briefly delete-pending file must not
326 # leave the database directory visible after the driver dies.
327 17 my $dir = $self->{+DIR};
328 17 ▶ if (-d $dir) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $dir) 2 15 17
329 2 my $removed = remove_tree_or_quarantine($dir);
330 2 ▶ unless (defined $removed) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $removed) 1 1 2
331 1 my $error_number = 0 + $!;
332 1 my $error = "$!";
333 1 warn "Could not remove or quarantine database directory '$dir' "
334 . "(OS error $error_number: $error)\n";
335 }
336 }
337 17 return;
338 }
339  
340 0 ▶sub connect {
Subroutine connect: not called CC=2 SCAR=17.9
Pod documented
341 0 my $self = shift;
342 0 my ($db_name, %params) = @_;
343  
344 0 ▶ %params = (AutoCommit => 1, RaiseError => 1) unless @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ > 1 0 0 0
345  
346 0 my $dbh;
347 $self->do_in_env(
348 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
349 0 my $cstring = $self->connect_string($db_name);
350 0 require DBI;
351 0 $dbh = DBI->connect($cstring, $self->username, $self->password, \%params);
352 }
353 0 );
354  
355 0 return $dbh;
356 }
357  
358 # Decoded wait status the watcher recorded, or undef while the server runs.
359 7074 ▶sub _server_exit_status {
Subroutine _server_exit_status: called CC=8 SCAR=21.8
360 7074 my $self = shift;
361  
362 7074 my $file = DBIx::QuickDB::Watcher->server_exit_status_file($self->{+DIR});
363 7074 ▶ return undef unless -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 7039 35 7074
364  
365 35 ▶ open(my $fh, '<', $file) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or return undef 35 0 35
366 35 ▶ chomp(my $status = <$fh> // '');
Condition50%1/2
readline $fh // ""
l!l
350
MC/DC0%0/1
readline $fh // ""
readline $fh
Truth table50%1/2
A
A= readline $fh
Aresult
11
00
367 35 close($fh);
368  
369 35 ▶ return undef unless length($status) && $status =~ m/^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless length $status and $status =~ /^\d+$/ 0 35 35
Condition33%1/3
length $status and $status =~ /^\d+$/
!ll&&!rl&&r
0035
MC/DC0%0/2
length $status and $status =~ /^\d+$/
length $status $status =~ /^\d+$/
Truth table33%1/3
A and B
A= length $statusB= $status =~ /^\d+$/
ABresult
0X0
100
111
370  
371 35 ▶ if (my $sig = $status & 127) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sig = $status & 127) 11 24 35
372 11 ▶ return "killed by signal $sig" . (($status & 128) ? ", core dumped" : "");
Branch: 1 branch
BranchTrueFalseTotal
$status & 128 ? : 11 0 11
373 }
374  
375 24 return "exit value " . ($status >> 8);
376 }
377  
378 # viable() reason for server drivers, which all run under the watcher. Only
379 # starting a server needs one.
380 76 ▶sub _watcher_unsupported_reason {
Subroutine _watcher_unsupported_reason: called CC=4 SCAR=13.9
381 76 my $this = shift;
382 76 my ($spec) = @_;
383 76 ▶ return undef unless $^O eq 'MSWin32';
Branch: 1 branch
BranchTrueFalseTotal
unless $^O eq "MSWin32" 68 8 76
384 8 ▶ return undef unless $spec->{bootstrap} || $spec->{autostart};
Branch: 1 branch
BranchTrueFalseTotal
unless $spec->{"bootstrap"} or $spec->{"autostart"} 4 4 8
Condition66%2/3
$spec->{"bootstrap"} or $spec->{"autostart"}
l!l&&r!l&&!r
404
MC/DC50%1/2
$spec->{"bootstrap"} or $spec->{"autostart"}
$spec->{"bootstrap"} $spec->{"autostart"}
Truth table66%2/3
A or B
A= $spec->{"bootstrap"}B= $spec->{"autostart"}
ABresult
1X1
011
000
385 4 return "Starting a " . $this->name . " server is not supported on MSWin32 (Strawberry Perl): the watcher that owns each server needs Unix process control";
386 }
387  
388 2 ▶sub started {
Subroutine started: called CC=3 SCAR=12.0
Pod undocumented
389 2 my $self = shift;
390  
391 2 my $socket = $self->socket;
392 2 ▶ return 1 if $self->{+WATCHER} || -S $socket;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"watcher"} or -S $socket 0 2 2
Condition33%1/3
$self->{"watcher"} or -S $socket
l!l&&r!l&&!r
002
MC/DC0%0/2
$self->{"watcher"} or -S $socket
$self->{"watcher"} -S $socket
Truth table33%1/3
A or B
A= $self->{"watcher"}B= -S $socket
ABresult
1X1
011
000
393 2 return 0;
394 }
395  
396 69 ▶sub start {
Subroutine start: called CC=14 SCAR=34.7
Pod documented
397 69 my $self = shift;
398 69 my @args = @_;
399  
400 69 my $dir = $self->{+DIR};
401 69 my $socket = $self->socket;
402  
403 69 ▶ return if $self->{+WATCHER} || -S $socket;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"watcher"} or -S $socket 0 69 69
Condition33%1/3
$self->{"watcher"} or -S $socket
l!l&&r!l&&!r
0069
MC/DC0%0/2
$self->{"watcher"} or -S $socket
$self->{"watcher"} -S $socket
Truth table33%1/3
A or B
A= $self->{"watcher"}B= -S $socket
ABresult
1X1
011
000
404  
405 # Drop a previous server's recorded crash, or a legitimate restart would
406 # fail instantly for it. Before the watcher exists, so nothing is writing.
407 69 unlink(DBIx::QuickDB::Watcher->server_exit_status_file($dir));
408  
409 69 my $watcher = $self->{+WATCHER} = DBIx::QuickDB::Watcher->new(db => $self, args => \@args);
410  
411 # Defaults to 10s; tunable via QDB_START_TIMEOUT for slow hosts that need
412 # longer to bring a server up (e.g. a clone doing crash recovery).
413 51 my $timeout = env_timeout(QDB_START_TIMEOUT => 10);
414  
415 51 my $start = time;
416 51 ▶ until (-S $socket) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-S $socket) 7073 16 7089
417 7073 my $waited = time - $start;
418  
419 # A server that aborts during startup is dead in milliseconds; there is
420 # nothing left to wait for. Re-check the socket first so one that came
421 # up and then exited still counts as started.
422 7073 ▶ if (my $status = $self->_server_exit_status) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $status = $self->_server_exit_status) 35 7038 7073
423 35 ▶ last if -S $socket;
Branch: 1 branch
BranchTrueFalseTotal
if -S $socket 0 35 35
424  
425 35 my $launch_log = $self->_read_file($watcher->log_file);
426 35 my $error_log = $self->read_error_log;
427  
428 35 $watcher->eliminate();
429  
430 35 my $msg = "Server exited during startup after ${\ sprintf('%.2f', $waited)}s ($status).";
431 35 ▶ $msg .= "\n=== server launch log ===\n$launch_log" if length $launch_log;
Branch: 1 branch
BranchTrueFalseTotal
if length $launch_log 0 35 35
432 35 ▶ $msg .= "\n=== error log ===\n$error_log" if length $error_log;
Branch: 1 branch
BranchTrueFalseTotal
if length $error_log 0 35 35
433 35 confess $msg;
434 }
435  
436 7038 ▶ if ($waited > $timeout) {
Branch: 1 branch
BranchTrueFalseTotal
if ($waited > $timeout) 0 7038 7038
437 # Capture diagnostics BEFORE eliminate() removes the data dir (which
438 # holds both the error log and the watcher's launch log). The server
439 # process's own stdout/stderr go to the watcher's log_file, not the
440 # driver's error_log, so a server that died (or never launched)
441 # before writing error_log leaves error_log showing only inherited
442 # template history -- the real failure is in the launch log. Also
443 # report whether the server pid is still alive: "not running" points
444 # at a launch/early-exit failure, "alive" at a slow or hung startup.
445 # Recorded status before kill(0), which succeeds on a zombie and so
446 # mislabelled a crashed server as a slow start.
447 0 my $spid = $watcher->server_pid;
448 0 my $status = $self->_server_exit_status;
449 0 ▶ my $alive
Branch: 2 branches
BranchTrueFalseTotal
$status ? : 0 0 0
$spid && kill(0, $spid) ? : 0 0 0
Condition0%0/3
$spid and kill 0, $spid
!ll&&!rl&&r
000
MC/DC0%0/2
$spid and kill 0, $spid
$spid kill 0, $spid
Truth table0%0/3
A and B
A= $spidB= kill 0, $spid
ABresult
0X0
100
111
450 = $status ? "not running ($status)"
451 : ($spid && kill(0, $spid)) ? "alive (pid $spid)"
452 : "not running";
453 0 my $error_log = $self->read_error_log;
454 0 my $launch_log = $self->_read_file($watcher->log_file);
455  
456 0 $watcher->eliminate();
457  
458 0 my $msg = "Timed out waiting for server to start after ${timeout}s; server process is $alive.";
459 0 ▶ $msg .= "\n=== server launch log ===\n$launch_log" if length $launch_log;
Branch: 1 branch
BranchTrueFalseTotal
if length $launch_log 0 0 0
460 0 ▶ $msg .= "\n=== error log ===\n$error_log" if length $error_log;
Branch: 1 branch
BranchTrueFalseTotal
if length $error_log 0 0 0
461 0 confess $msg;
462 }
463  
464 7038 sleep 0.01;
465 }
466  
467 16 return;
468 }
469  
470 # The socket file exists before the server accepts on it, so a connect right
471 # after it appears can be refused. Retry a real connection (the base connect,
472 # not a driver's startup-tolerant override) until one works. A failure appends
473 # the launch log too: errors from before a server's own logging starts (such as
474 # resource exhaustion, which test helpers match on) land only there.
475 3 ▶sub _wait_for_connect {
Subroutine _wait_for_connect: called CC=14 SCAR=26.8
476 3 my $self = shift;
477 3 my ($db_name) = @_;
478  
479 3 my $timeout = env_timeout(QDB_START_TIMEOUT => 10);
480 3 my $start = time;
481 3 my ($dbh, $err, $ok);
482 3 while (1) {
483 {
484 3 local $@;
485 3 $ok = eval {
486 3 $dbh = $self->DBIx::QuickDB::Driver::connect(
487 $db_name,
488 AutoCommit => 1,
489 RaiseError => 1,
490 PrintError => 0,
491 );
492 1 1;
493 };
494 3 ▶ $err = $ok ? undef : $@;
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 1 2 3
495 }
496  
497 3 ▶ last if $ok;
Branch: 1 branch
BranchTrueFalseTotal
if $ok 1 2 3
498 2 ▶ last if $self->_server_exit_status;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_server_exit_status 1 1 2
499 1 ▶ last unless -S $self->socket;
Branch: 1 branch
BranchTrueFalseTotal
unless -S $self->socket 1 0 1
500 0 ▶ last if time - $start > $timeout;
Branch: 1 branch
BranchTrueFalseTotal
if &DBIx::QuickDB::Driver::time() - $start > $timeout 0 0 0
501 0 sleep 0.01;
502 }
503  
504 3 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) 1 2 3
505 1 $dbh->disconnect;
506 1 return;
507 }
508  
509 2 my $watcher = $self->{+WATCHER};
510 2 ▶ my $launch_log = $watcher ? $self->_read_file($watcher->log_file) : '';
Branch: 1 branch
BranchTrueFalseTotal
$watcher ? : 1 1 2
511 2 my $error_log = $self->read_error_log;
512  
513 2 my $msg = $err;
514 2 ▶ $msg .= "\n=== server launch log ===\n$launch_log" if length $launch_log;
Branch: 1 branch
BranchTrueFalseTotal
if length $launch_log 1 1 2
515 2 ▶ $msg .= "\n=== error log ===\n$error_log" if length $error_log;
Branch: 1 branch
BranchTrueFalseTotal
if length $error_log 1 1 2
516  
517 # A failed start must not leave the object permanently reporting
518 # started() merely because it still holds a watcher. Stop and reap the
519 # server without stop()'s checkpoint (it never became ready), keeping the
520 # original startup error.
521 2 ▶ if ($watcher) {
Branch: 1 branch
BranchTrueFalseTotal
if ($watcher) 1 1 2
522 1 delete $self->{+WATCHER};
523 1 my $stopped = eval {
524 1 $watcher->stop;
525 1 $watcher->wait;
526 1 my $socket = $self->socket;
527 1 ▶ unlink($socket) if $socket && -S $socket;
Branch: 1 branch
BranchTrueFalseTotal
if $socket and -S $socket 0 1 1
Condition33%1/3
$socket and -S $socket
!ll&&!rl&&r
010
MC/DC0%0/2
$socket and -S $socket
$socket -S $socket
Truth table33%1/3
A and B
A= $socketB= -S $socket
ABresult
0X0
100
111
528 1 1;
529 };
530 1 ▶ warn $@ unless $stopped;
Branch: 1 branch
BranchTrueFalseTotal
unless $stopped 0 1 1
531 }
532  
533 2 die $msg;
534 }
535  
536 # Disconnect this process's DBI handles to this instance's server. Must run
537 # before any teardown signal: a connected client stalls a graceful shutdown
538 # (PostgreSQL's smart shutdown waits for clients INDEFINITELY, mysqld several
539 # seconds), pinning the stop at the full QDB_STOP_GRACE before the watcher
540 # escalates. It also keeps this process from retaining broken handles that
541 # later report "server has gone away". Skipped during global destruction:
542 # DBI's own structures are already being torn down (DBI->visit_handles dies
543 # with "Can't call method visit_child_handles on an undefined value"), and
544 # retaining broken handles no longer matters because the process is exiting.
545 #
546 # Skipped entirely when the no_disconnect_handles attribute is set: some callers
547 # (e.g. a test framework running teardown queries) hold their OWN active handles
548 # to this same db, and ripping those out mid-query surfaces as "Lost connection
549 # ... during query". MySQL/MariaDB shut down fine with clients still connected,
550 # so such callers can opt out. PostgreSQL's smart shutdown waits for clients
551 # indefinitely, so opting out there risks a stalled stop -- caller's choice.
552 68 ▶sub _disconnect_handles {
Subroutine _disconnect_handles: called CC=2 SCAR=7.0
553 68 my $self = shift;
554  
555 68 ▶ return if $self->{+NO_DISCONNECT_HANDLES};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"no_disconnect_handles"} 0 68 68
556  
557 68 disconnect_dbi_handles($self->{+DIR});
558  
559 68 return;
560 }
561  
562 0 ▶sub stop {
Subroutine stop: not called CC=5 SCAR=34.0
Pod documented
563 0 my $self = shift;
564 0 my %params = @_;
565  
566 0 ▶ my $watcher = delete $self->{+WATCHER} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $watcher = delete $self->{"watcher"} or return 0 0 0
567  
568 # Flush a durable checkpoint while the server is still running. If shutdown
569 # is slow enough to be SIGKILLed, this ensures the on-disk state is already
570 # consistent so a later clone does not crash-recover and jump SERIAL
571 # sequences forward (PostgreSQL SEQ_LOG_VALS=32), corrupting the clone.
572 # No-op for drivers that do not need it.
573 0 $self->checkpoint;
574  
575 0 $self->_disconnect_handles;
576  
577 0 $watcher->stop();
578  
579 0 ▶ unless ($params{no_wait}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($params{"no_wait"}) 0 0 0
580 # Blocks until the watcher exits, having done everything it could to
581 # stop the server. If the server somehow survived, wait() spends its
582 # timeout confirming that and then warns. Do not poll the stored server
583 # pid instead: once the watcher is gone that pid may have been recycled.
584 0 $watcher->wait();
585  
586 # Remove a stale unix socket left behind by a hard kill so it does not
587 # confuse callers or a later run that reuses the same directory.
588 0 my $socket = $self->socket;
589 0 ▶ unlink($socket) if $socket && -S $socket;
Branch: 1 branch
BranchTrueFalseTotal
if $socket and -S $socket 0 0 0
Condition0%0/3
$socket and -S $socket
!ll&&!rl&&r
000
MC/DC0%0/2
$socket and -S $socket
$socket -S $socket
Truth table0%0/3
A and B
A= $socketB= -S $socket
ABresult
0X0
100
111
590 }
591  
592 0 return;
593 }
594  
595 # Immediate disposable teardown for clones that are about to be deleted. Unlike
596 # stop()/DESTROY this does NOT checkpoint or attempt a graceful shutdown: it asks
597 # the watcher to SIGKILL+reap the server and remove the data dir. Only safe when
598 # the data dir is disposable (cleanup => 1). Idempotent: after the first call
599 # clears the watcher, later calls and DESTROY become no-ops apart from an
600 # idempotent cleanup().
601 5 ▶sub destroy_quietly {
Subroutine destroy_quietly: called CC=6 SCAR=21.2
Pod documented
602 5 my $self = shift;
603 5 ▶ return unless $self->{+ROOT_PID} && $self->{+ROOT_PID} == $$;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"root_pid"} and $self->{"root_pid"} == $$ 0 5 5
Condition33%1/3
$self->{"root_pid"} and $self->{"root_pid"} == $$
!ll&&!rl&&r
005
MC/DC0%0/2
$self->{"root_pid"} and $self->{"root_pid"} == $$
$self->{"root_pid"} $self->{"root_pid"} == $$
Truth table33%1/3
A and B
A= $self->{"root_pid"}B= $self->{"root_pid"} == $$
ABresult
0X0
100
111
604  
605 5 ▶ if (my $watcher = delete $self->{+WATCHER}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $watcher = delete $self->{'watcher'}) { } 5 0 5
elsif ($self->should_cleanup) { } 0 0 0
606 5 $self->_disconnect_handles;
607  
608 # The watcher is the server's parent, so it is the correct process to
609 # kill and reap it. Do NOT signal the stored server pid directly here.
610 5 $watcher->fast_eliminate();
611 5 $watcher->wait();
612  
613 # The watcher removes the data dir; fallback for one that exited
614 # without doing so.
615 5 ▶ $self->cleanup() if $self->should_cleanup;
Branch: 1 branch
BranchTrueFalseTotal
if $self->should_cleanup 5 0 5
616 }
617 elsif ($self->should_cleanup) {
618 0 $self->cleanup();
619 }
620  
621 5 return;
622 }
623  
624 0 ▶sub shell {
Subroutine shell: not called CC=2 SCAR=17.9
Pod documented
625 0 my $self = shift;
626 0 my ($db_name) = @_;
627 0 ▶ $db_name = 'quickdb' unless defined $db_name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $db_name 0 0 0
628  
629 0 system($self->shell_command($db_name));
630 }
631  
632 60 ▶sub DESTROY {
Subroutine DESTROY: called CC=8 SCAR=20.8
633 60 my $self = shift;
634 60 ▶ return unless $self->{+ROOT_PID} && $self->{+ROOT_PID} == $$;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"root_pid"} and $self->{"root_pid"} == $$ 3 57 60
Condition66%2/3
$self->{"root_pid"} and $self->{"root_pid"} == $$
!ll&&!rl&&r
3057
MC/DC50%1/2
$self->{"root_pid"} and $self->{"root_pid"} == $$
$self->{"root_pid"} $self->{"root_pid"} == $$
Truth table66%2/3
A and B
A= $self->{"root_pid"}B= $self->{"root_pid"} == $$
ABresult
0X0
100
111
635  
636 # Opt-in fast teardown for disposable clones. Only honored when the data dir
637 # is actually disposable (_CLEANUP); a contradictory fast_destroy + cleanup
638 # => 0 falls through to the normal graceful path below so a reusable data dir
639 # is never hard-killed.
640 return $self->destroy_quietly()
641 57 ▶ if $self->{+FAST_DESTROY} && $self->{+_CLEANUP};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"fast_destroy"} and $self->{"_cleanup"} 5 52 57
Condition66%2/3
$self->{"fast_destroy"} and $self->{"_cleanup"}
!ll&&!rl&&r
5205
MC/DC50%1/2
$self->{"fast_destroy"} and $self->{"_cleanup"}
$self->{"fast_destroy"} $self->{"_cleanup"}
Truth table66%2/3
A and B
A= $self->{"fast_destroy"}B= $self->{"_cleanup"}
ABresult
0X0
100
111
642  
643 52 ▶ if (my $watcher = delete $self->{+WATCHER}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $watcher = delete $self->{'watcher'}) { } 47 5 52
elsif ($self->should_cleanup) { } 2 3 5
644 # A still-connected handle stalls the graceful shutdown that
645 # eliminate() requests (PostgreSQL smart shutdown waits for clients
646 # indefinitely) -- without this, every DESTROY of a db with an open
647 # handle sat out the full stop grace and then got escalated.
648 47 $self->_disconnect_handles;
649  
650 # Explicitly wait after signalling. Relying on Watcher::DESTROY to wait
651 # is unsafe during Perl's END phase: destruction of the local watcher
652 # can be deferred until after this Driver::DESTROY returns, allowing
653 # cleanup() below to delete the data dir while the server is still
654 # shutting down. That strands MySQL mid-shutdown until the watcher
655 # escalates to SIGKILL. An explicit wait also handles callers retaining
656 # another watcher reference.
657 47 $watcher->eliminate();
658 47 $watcher->wait();
659  
660 47 ▶ $self->cleanup() if $self->should_cleanup;
Branch: 1 branch
BranchTrueFalseTotal
if $self->should_cleanup 9 38 47
661 }
662 elsif ($self->should_cleanup) {
663 2 $self->cleanup();
664 }
665  
666 52 return;
667 }
668  
669 61 1;
670  
671 __END__
« blib/lib/DBIx/QuickDB/Driver/PostgreSQL.pm ↑ Summary blib/lib/DBIx/QuickDB/Driver/DuckDB.pm »