blib/lib/DBD/Gofer/Transport/stream.pm

filter: Statementstmt 65.2% 77 / 118 Branchbran 34.6% 18 / 52 Conditioncond 33.3% 10 / 30 MC/DCmcdc 5.2% 1 / 19 Subroutinesub 61.1% 11 / 18 Podpod 0.0% 0 / 3 totaltotal 48.7% 117 / 240 CC 38 SCAR 29.2
CC
38
Cov
52%
receive_response_by_transport
60.1
_make_connection
10.2
_connection_get
9.0
CRAP 18.6
« blib/lib/DBI/SQL/Nano.pm ↑ Summary blib/lib/DBD/Gofer.pm »
1 package DBD::Gofer::Transport::stream;
2  
3 # $Id: stream.pm 14598 2010-12-21 22:53:25Z Tim $
4 #
5 # Copyright (c) 2007, Tim Bunce, Ireland
6 #
7 # You may distribute under the terms of either the GNU General Public
8 # License or the Artistic License, as specified in the Perl README file.
9  
10 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 8 ▶use base qw(DBD::Gofer::Transport::pipeone);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 8 our $VERSION = "0.014599";
18  
19 8 __PACKAGE__->mk_accessors(qw(
20 go_persist
21 ));
22  
23 8 my $persist_all = 5;
24 8 my %persist;
25  
26  
27 148 ▶sub _connection_key {
Subroutine _connection_key: called CC=3 SCAR=11.7
28 148 my ($self) = @_;
29 148 ▶ return join "~", $self->go_url||"", @{ $self->go_perl || [] };
Condition50%2/4
$self->go_url || ''
l!l
0148
$self->go_perl || []
l!l
1480
MC/DC0%0/1
$self->go_url || ''
$self->go_url
MC/DC0%0/1
$self->go_perl || []
$self->go_perl
Truth table50%1/2
A
A= $self->go_url
Aresult
11
00
Truth table50%1/2
A
A= $self->go_perl
Aresult
11
00
30 }
31  
32  
33 148 ▶sub _connection_get {
Subroutine _connection_get: called CC=8 SCAR=22.0
34 148 my ($self) = @_;
35  
36 148 my $persist = $self->go_persist; # = 0 can force non-caching
37 148 ▶ $persist //= $persist_all;
Condition33%1/3
$persist //= $persist_all
l!l&&r!l&&!r
01480
MC/DC0%0/2
$persist //= $persist_all
$persist $persist_all
Truth table33%1/3
A //= B
A= $persistB= $persist_all
ABresult
1X1
011
000
38 148 ▶ my $key = ($persist) ? $self->_connection_key : '';
Branch: 1 branch
BranchTrueFalseTotal
$persist ? : 148 0 148
39 148 ▶ if ($persist{$key} && $self->_connection_check($persist{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($persist{$key} and $self->_connection_check($persist{$key})) 140 8 148
Condition66%2/3
$persist{$key} and $self->_connection_check($persist{$key})
!ll&&!rl&&r
80140
MC/DC50%1/2
$persist{$key} and $self->_connection_check($persist{$key})
$persist{$key} $self->_connection_check($persist{$key})
Truth table66%2/3
A and B
A= $persist{$key}B= $self->_connection_check($persist{$key})
ABresult
0X0
100
111
40 140 ▶ $self->trace_msg("reusing persistent connection $key\n",0) if $self->trace >= 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->trace >= 1 0 140 140
41 140 return $persist{$key};
42 }
43  
44 8 my $connection = $self->_make_connection;
45  
46 8 ▶ if ($key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key) 8 0 8
47 8 ▶ %persist = () if keys %persist > $persist_all; # XXX quick hack to limit subprocesses
Branch: 1 branch
BranchTrueFalseTotal
if keys %persist > $persist_all 0 8 8
48 8 $persist{$key} = $connection;
49 }
50  
51 8 return $connection;
52 }
53  
54  
55 140 ▶sub _connection_check {
Subroutine _connection_check: called CC=3 SCAR=11.6
56 140 my ($self, $connection) = @_;
57 140 ▶ $connection ||= $self->connection_info;
Condition33%1/3
$connection ||= $self->connection_info
l!l&&r!l&&!r
14000
MC/DC0%0/2
$connection ||= $self->connection_info
$connection $self->connection_info
Truth table33%1/3
A ||= B
A= $connectionB= $self->connection_info
ABresult
1X1
011
000
58 140 my $pid = $connection->{pid};
59 140 my $ok = (kill 0, $pid);
60 140 ▶ $self->trace_msg("_connection_check: $ok (pid $$)\n",0) if $self->trace;
Branch: 1 branch
BranchTrueFalseTotal
if $self->trace 0 140 140
61 140 return $ok;
62 }
63  
64  
65 0 ▶sub _connection_kill {
Subroutine _connection_kill: not called CC=2 SCAR=17.9
66 0 my ($self) = @_;
67 0 my $connection = $self->connection_info;
68 0 my ($pid, $wfh, $rfh, $efh) = @{$connection}{qw(pid wfh rfh efh)};
69 0 ▶ $self->trace_msg("_connection_kill: closing write handle\n",0) if $self->trace;
Branch: 1 branch
BranchTrueFalseTotal
if $self->trace 0 0 0
70 # closing the write file handle should be enough, generally
71 0 close $wfh;
72 # in future we may want to be more aggressive
73 #close $rfh; close $efh; kill 15, $pid
74 # but deleting from the persist cache...
75 0 delete $persist{ $self->_connection_key };
76 # ... and removing the connection_info should suffice
77 0 $self->connection_info( undef );
78 0 return;
79 }
80  
81  
82 8 ▶sub _make_connection {
Subroutine _make_connection: called CC=5 SCAR=23.2
83 8 my ($self) = @_;
84  
85 8 my $go_perl = $self->go_perl;
86 8 my $cmd = [ @$go_perl, qw(-MDBI::Gofer::Transport::stream -e run_stdio_hex)];
87  
88 #push @$cmd, "DBI_TRACE=2=/tmp/goferstream.log", "sh", "-c";
89 8 ▶ if (my $url = $self->go_url) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $url = $self->go_url) 0 8 8
90 0 ▶ die "Only 'ssh:user\@host' style url supported by this transport"
Branch: 1 branch
BranchTrueFalseTotal
unless $url =~ s/^ssh:// 0 0 0
91 unless $url =~ s/^ssh://;
92 0 my $ssh = $url;
93 0 my $setup_env = join "||", map { "source $_ 2>/dev/null" } qw(.bash_profile .bash_login .profile);
94 0 my $setup = $setup_env.q{; exec "$@"};
95 # don't use $^X on remote system by default as it's possibly wrong
96 0 ▶ $cmd->[0] = 'perl' if "@$go_perl" eq $^X;
Branch: 1 branch
BranchTrueFalseTotal
if join($", @$go_perl) eq $^X 0 0 0
97 # -x not only 'Disables X11 forwarding' but also makes connections *much* faster
98 0 unshift @$cmd, qw(ssh -xq), split(' ', $ssh), qw(bash -c), $setup;
99 }
100  
101 8 ▶ $self->trace_msg("new connection: @$cmd\n",0) if $self->trace;
Branch: 1 branch
BranchTrueFalseTotal
if $self->trace 0 8 8
102  
103 # XXX add a handshake - some message from DBI::Gofer::Transport::stream that's
104 # sent as soon as it starts that we can wait for to report success - and soak up
105 # and report useful warnings etc from ssh before we get it? Increases latency though.
106 8 my $connection = $self->start_pipe_command($cmd);
107 8 return $connection;
108 }
109  
110  
111 380 ▶sub transmit_request_by_transport {
Subroutine transmit_request_by_transport: called CC=5 SCAR=17.0
Pod undocumented
112 380 my ($self, $request) = @_;
113 380 my $trace = $self->trace;
114  
115 380 ▶ my $connection = $self->connection_info || do {
Condition66%2/3
$self->connection_info || do { my $con = $self->_connection_get; $self->connection_info($con); $con }
l!l&&r!l&&!r
2321480
MC/DC0%0/2
$self->connection_info || do { my $con = $self->_connection_get; $self->connection_info($con); $con }
$self->connection_info do { my $con = $self->_connection_get; $self->connection_info($con); $con }
Truth table66%2/3
A || B
A= $self->connection_infoB= do { my $con = $self->_connection_get; $self->connection_info($con); $con }
ABresult
1X1
011
000
116 148 my $con = $self->_connection_get;
117 148 $self->connection_info( $con );
118 148 $con;
119 };
120  
121 380 my $encoded_request = unpack("H*", $self->freeze_request($request));
122 380 $encoded_request .= "\015\012";
123  
124 380 my $wfh = $connection->{wfh};
125 380 ▶ $self->trace_msg(sprintf("transmit_request_by_transport: to fh %s fd%d\n", $wfh, fileno($wfh)),0)
Branch: 1 branch
BranchTrueFalseTotal
if $trace >= 4 0 380 380
126 if $trace >= 4;
127  
128 # send frozen request
129 380 local $\;
130 $wfh->print($encoded_request) # autoflush enabled
131 380 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
$wfh->print($encoded_request) or do { my $err = $!; $self->_connection_kill; die "Error sending request: $err" } 380 0 380
132 0 my $err = $!;
133 # XXX could/should make new connection and retry
134 0 $self->_connection_kill;
135 0 die "Error sending request: $err";
136 };
137 380 ▶ $self->trace_msg("Request sent: $encoded_request\n",0) if $trace >= 4;
Branch: 1 branch
BranchTrueFalseTotal
if $trace >= 4 0 380 380
138  
139 380 return undef; # indicate no response yet (so caller calls receive_response_by_transport)
140 }
141  
142  
143 380 ▶sub receive_response_by_transport {
Subroutine receive_response_by_transport: called CC=13 SCAR=41.0
Pod undocumented
144 380 my $self = shift;
145 380 my $trace = $self->trace;
146  
147 380 ▶ $self->trace_msg("receive_response_by_transport: awaiting response\n",0) if $trace >= 4;
Branch: 1 branch
BranchTrueFalseTotal
if $trace >= 4 0 380 380
148 380 ▶ my $connection = $self->connection_info || die;
Condition50%1/2
$self->connection_info || die()
l!l
3800
MC/DC0%0/1
$self->connection_info || die()
$self->connection_info
Truth table50%1/2
A
A= $self->connection_info
Aresult
11
00
149 380 my ($pid, $rfh, $efh, $cmd) = @{$connection}{qw(pid rfh efh cmd)};
150  
151 380 my $errno = 0;
152 380 my $encoded_response;
153 my $stderr_msg;
154  
155 $self->read_response_from_fh( {
156 $efh => {
157 0 ▶ error => sub { warn "error reading response stderr: $!"; $errno||=$!; 1 },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Condition0%0/3
$errno ||= $!
l!l&&r!l&&!r
000
MC/DC0%0/2
$errno ||= $!
$errno $!
Truth table0%0/3
A ||= B
A= $errnoB= $!
ABresult
1X1
011
000
158 0 ▶ eof => sub { warn "eof reading efh" if $trace >= 4; 1 },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if $trace >= 4 0 0 0
159 0 ▶ read => sub { $stderr_msg .= $_; 0 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
160 },
161 $rfh => {
162 0 ▶ error => sub { warn "error reading response: $!"; $errno||=$!; 1 },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Condition0%0/3
$errno ||= $!
l!l&&r!l&&!r
000
MC/DC0%0/2
$errno ||= $!
$errno $!
Truth table0%0/3
A ||= B
A= $errnoB= $!
ABresult
1X1
011
000
163 0 ▶ eof => sub { warn "eof reading rfh" if $trace >= 4; 1 },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if $trace >= 4 0 0 0
164 380 ▶ read => sub { $encoded_response .= $_; ($encoded_response=~s/\015\012$//) ? 1 : 0 },
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
$encoded_response =~ s/\015\012$// ? : 380 0 380
165 },
166 380 });
167  
168 # if we got no output on stdout at all then the command has
169 # probably exited, possibly with an error to stderr.
170 # Turn this situation into a reasonably useful DBI error.
171 380 ▶ if (not $encoded_response) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($encoded_response) 0 380 380
172 0 my @msg;
173 0 ▶ push @msg, "error while reading response: $errno" if $errno;
Branch: 1 branch
BranchTrueFalseTotal
if $errno 0 0 0
174 0 ▶ if ($stderr_msg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stderr_msg) 0 0 0
175 0 chomp $stderr_msg;
176 0 ▶ push @msg, sprintf "error reported by \"%s\" (pid %d%s): %s",
Branch: 1 branch
BranchTrueFalseTotal
kill(0, $pid) ? : 0 0 0
177 $self->cmd_as_string,
178 $pid, ((kill 0, $pid) ? "" : ", exited"),
179 $stderr_msg;
180 }
181 0 die join(", ", "No response received", @msg)."\n";
182 }
183  
184 380 ▶ $self->trace_msg("Response received: $encoded_response\n",0)
Branch: 1 branch
BranchTrueFalseTotal
if $trace >= 4 0 380 380
185 if $trace >= 4;
186  
187 380 ▶ $self->trace_msg("Gofer stream stderr message: $stderr_msg\n",0)
Branch: 1 branch
BranchTrueFalseTotal
if $stderr_msg and $trace 0 380 380
Condition33%1/3
$stderr_msg and $trace
!ll&&!rl&&r
38000
MC/DC0%0/2
$stderr_msg and $trace
$stderr_msg $trace
Truth table33%1/3
A and B
A= $stderr_msgB= $trace
ABresult
0X0
100
111
188 if $stderr_msg && $trace;
189  
190 380 my $frozen_response = pack("H*", $encoded_response);
191  
192 # XXX need to be able to detect and deal with corruption
193 380 my $response = $self->thaw_response($frozen_response);
194  
195 380 ▶ if ($stderr_msg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stderr_msg) 0 380 380
196 # add stderr messages as warnings (for PrintWarn)
197 0 ▶ $response->add_err(0, $stderr_msg, undef, $trace)
Branch: 1 branch
BranchTrueFalseTotal
unless $stderr_msg =~ /^Using .*blib/ and join($", @$cmd) =~ /-Mblib/ 0 0 0
Condition0%0/3
$stderr_msg =~ /^Using .*blib/ and join($", @$cmd) =~ /-Mblib/
!ll&&!rl&&r
000
MC/DC0%0/2
$stderr_msg =~ /^Using .*blib/ and join($", @$cmd) =~ /-Mblib/
$stderr_msg =~ /^Using .*blib/ join($", @$cmd) =~ /-Mblib/
Truth table0%0/3
A and B
A= $stderr_msg =~ /^Using .*blib/B= join($", @$cmd) =~ /-Mblib/
ABresult
0X0
100
111
198 # but ignore warning from old version of blib
199 unless $stderr_msg =~ /^Using .*blib/ && "@$cmd" =~ /-Mblib/;
200 }
201  
202 380 return $response;
203 }
204  
205 0 ▶sub transport_timedout {
Subroutine transport_timedout: not called CC=1 SCAR=6.9
Pod undocumented
206 0 my $self = shift;
207 0 $self->_connection_kill;
208 0 return $self->SUPER::transport_timedout(@_);
209 }
210  
211 8 1;
212  
213 __END__
« blib/lib/DBI/SQL/Nano.pm ↑ Summary blib/lib/DBD/Gofer.pm »