lib/Kafka/IO.pm

filter: Statementstmt 78.9% 266 / 337 Branchbran 49.4% 87 / 176 Conditioncond 36.4% 51 / 140 MC/DCmcdc 12.0% 9 / 75 Subroutinesub 91.8% 34 / 37 Podpod 100% 4 / 4 totaltotal 58.6% 451 / 769 SCAR 71.1
CC
140
Cov
62%
receive
325.1
send
189.7
_connect
45.1
CRAP 1226.7
« lib/Kafka/Connection.pm ↑ Summary lib/Kafka/Consumer.pm »
1 package Kafka::IO;
2  
3 =head1 NAME
4  
5 Kafka::IO - Interface to network communication with the Apache Kafka server.
6  
7 =head1 VERSION
8  
9 This documentation refers to C<Kafka::IO> version 1.07 .
10  
11 =cut
12  
13  
14  
15 15 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19  
20  
21 15 our $DEBUG = 0;
22  
23 15 our $VERSION = '1.07';
24  
25  
26  
27 15 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 15 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 15 ▶use Const::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 15 use Data::Validate::Domain qw(
31 is_hostname
32 15 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
33 15 use Data::Validate::IP qw(
34 is_ipv4
35 is_ipv6
36 15 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
37 15 use Errno qw(
38 EAGAIN
39 ECONNRESET
40 EINTR
41 EWOULDBLOCK
42 ETIMEDOUT
43 15 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
44 15 ▶use Fcntl;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 15 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 15 use Params::Util qw(
47 _STRING
48 15 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
49 15 use POSIX qw(
50 ceil
51 15 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
52 15 use Scalar::Util qw(
53 dualvar
54 15 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
55 15 use Socket qw(
56 AF_INET
57 AF_INET6
58 IPPROTO_TCP
59 MSG_DONTWAIT
60 MSG_PEEK
61 NI_NUMERICHOST
62 NIx_NOSERV
63 PF_INET
64 PF_INET6
65 SOCK_STREAM
66 SOL_SOCKET
67 SO_ERROR
68 SO_RCVTIMEO
69 SO_SNDTIMEO
70 getaddrinfo
71 getnameinfo
72 inet_aton
73 inet_pton
74 inet_ntop
75 pack_sockaddr_in
76 pack_sockaddr_in6
77 15 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
78 15 use Sys::SigAction qw(
79 set_sig_handler
80 15 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
81 15 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
82 15 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
83  
84 15 use Kafka qw(
85 $ERROR_CANNOT_BIND
86 $ERROR_CANNOT_RECV
87 $ERROR_CANNOT_SEND
88 $ERROR_MISMATCH_ARGUMENT
89 $ERROR_INCOMPATIBLE_HOST_IP_VERSION
90 $ERROR_NO_CONNECTION
91 $IP_V4
92 $IP_V6
93 $KAFKA_SERVER_PORT
94 $REQUEST_TIMEOUT
95 15 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
96 15 ▶use Kafka::Exceptions;
Subroutine BEGIN: called CC=1 SCAR=0.0
97 15 use Kafka::Internals qw(
98 $MAX_SOCKET_REQUEST_BYTES
99 debug_level
100 format_message
101 15 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
102  
103  
104  
105 =head1 SYNOPSIS
106  
107 use 5.010;
108 use strict;
109 use warnings;
110  
111 use Scalar::Util qw(
112 blessed
113 );
114 use Try::Tiny;
115  
116 use Kafka::IO;
117  
118 my $io;
119 try {
120 $io = Kafka::IO->new( host => 'localhost' );
121 } catch {
122 my $error = $_;
123 if ( blessed( $error ) && $error->isa( 'Kafka::Exception' ) ) {
124 warn 'Error: (', $error->code, ') ', $error->message, "\n";
125 exit;
126 } else {
127 die $error;
128 }
129 };
130  
131 # Closes and cleans up
132 $io->close;
133 undef $io;
134  
135 =head1 DESCRIPTION
136  
137 This module is private and should not be used directly.
138  
139 In order to achieve better performance, methods of this module do not
140 perform arguments validation.
141  
142 The main features of the C<Kafka::IO> class are:
143  
144 =over 3
145  
146 =item *
147  
148 Provides an object oriented API for communication with Kafka.
149  
150 =item *
151  
152 This class allows you to create Kafka 0.9+ clients.
153  
154 =back
155  
156 =cut
157  
158 # Hard limit of IO operation retry attempts, to prevent high CPU usage in IO retry loop
159 15 const my $MAX_RETRIES => 30;
160  
161 15 our $_hdr;
162  
163 #-- constructor ----------------------------------------------------------------
164  
165 =head2 CONSTRUCTOR
166  
167 =head3 C<new>
168  
169 Establishes TCP connection to given host and port, creates and returns C<Kafka::IO> IO object.
170  
171 C<new()> takes arguments in key-value pairs. The following arguments are currently recognized:
172  
173 =over 3
174  
175 =item C<host =E<gt> $host>
176  
177 C<$host> is Kafka host to connect to. It can be a host name or an IP-address in
178 IPv4 or IPv6 form (for example '127.0.0.1', '0:0:0:0:0:0:0:1' or '::1').
179  
180 =item C<port =E<gt> $port>
181  
182 Optional, default = C<$KAFKA_SERVER_PORT>.
183  
184 C<$port> is integer attribute denoting the port number of to access Apache Kafka.
185  
186 C<$KAFKA_SERVER_PORT> is the default Apache Kafka server port that can be imported
187 from the L<Kafka|Kafka> module.
188  
189 =item C<timeout =E<gt> $timeout>
190  
191 C<$REQUEST_TIMEOUT> is the default timeout that can be imported from the L<Kafka|Kafka> module.
192  
193 Special behavior when C<timeout> is set to C<undef>:
194  
195 =back
196  
197 =over 3
198  
199 =item *
200  
201 Alarms are not used internally (namely when performing C<gethostbyname>).
202  
203 =item *
204  
205 Default C<$REQUEST_TIMEOUT> is used for the rest of IO operations.
206  
207 =back
208  
209 =over 3
210  
211 =item C<ip_version =E<gt> $ip_version>
212  
213 Force version of IP protocol for resolving host name (or interpretation of passed address).
214  
215 Optional, undefined by default, which works in the following way: version of IP address
216 is detected automatically, host name is resolved into IPv4 address.
217  
218 See description of L<$IP_V4|Kafka::IO/$IP_V4>, L<$IP_V6|Kafka::IO/$IP_V6>
219 in C<Kafka> L<EXPORT|Kafka/EXPORT>.
220  
221 =back
222  
223 =cut
224 sub new {
225 13 ▶ my ( $class, %p ) = @_;
Subroutine new: called CC=4 SCAR=13.9
Pod documented
226  
227 13 my $self = bless {
228 host => '',
229 timeout => $REQUEST_TIMEOUT,
230 port => $KAFKA_SERVER_PORT,
231 ip_version => undef,
232 af => '', # Address family constant
233 pf => '', # Protocol family constant
234 ip => '', # Human-readable textual representation of the ip address
235 }, $class;
236  
237 13 ▶ exists $p{$_} and $self->{$_} = $p{$_} foreach keys %$self;
Branch: 1 branch
BranchTrueFalseTotal
exists $p{$_} and $self->{$_} = $p{$_} 39 52 91
238  
239 # we trust it: make it untainted
240 13 ( $self->{host} ) = $self->{host} =~ /\A(.+)\z/;
241 13 ( $self->{port} ) = $self->{port} =~ /\A(.+)\z/;
242  
243 13 $self->{socket} = undef;
244 13 $self->{_io_select} = undef;
245 13 my $error;
246 try {
247 13 ▶ $self->_connect();
Subroutine __ANON__: called CC=1 SCAR=0.0
248 } catch {
249 4 ▶ $error = $_;
Subroutine __ANON__: called CC=1 SCAR=0.0
250 13 };
251  
252 13 ▶ $self->_error( $ERROR_CANNOT_BIND, format_message("Kafka::IO(%s:%s)->new: %s", $self->{host}, $self->{port}, $error ) )
Branch: 1 branch
BranchTrueFalseTotal
if defined $error 4 9 13
253 if defined $error
254 ;
255 9 return $self;
256 }
257  
258 #-- public attributes ----------------------------------------------------------
259  
260 =head2 METHODS
261  
262 The following methods are provided by C<Kafka::IO> class:
263  
264 =cut
265  
266 =head3 C<< send( $message <, $timeout> ) >>
267  
268 Sends a C<$message> to Kafka.
269  
270 The argument must be a bytes string.
271  
272 Use optional C<$timeout> argument to override default timeout for this request only.
273  
274 Returns the number of characters sent.
275  
276 =cut
277 sub send {
278 1 ▶ my ( $self, $message, $timeout ) = @_;
Subroutine send: called CC=32 SCAR=52.5
Pod documented
279 1 ▶ $self->_error( $ERROR_MISMATCH_ARGUMENT, '->send' )
Branch: 1 branch
BranchTrueFalseTotal
unless defined &_STRING($message) 0 1 1
280 unless defined( _STRING( $message ) )
281 ;
282 1 my $length = length( $message );
283 1 ▶ $self->_error( $ERROR_MISMATCH_ARGUMENT, '->send' )
Branch: 1 branch
BranchTrueFalseTotal
unless $length <= $Kafka::IO::MAX_SOCKET_REQUEST_BYTES 0 1 1
284 unless $length <= $MAX_SOCKET_REQUEST_BYTES
285 ;
286 1 ▶ $timeout = $self->{timeout} // $REQUEST_TIMEOUT unless defined $timeout;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $timeout 1 0 1
Condition33%1/3
$self->{"timeout"} // $Kafka::IO::REQUEST_TIMEOUT
l!l&&r!l&&!r
100
MC/DC0%0/2
$self->{"timeout"} // $Kafka::IO::REQUEST_TIMEOUT
$self->{"timeout"} $Kafka::IO::REQUEST_TIMEOUT
Truth table33%1/3
A // B
A= $self->{"timeout"}B= $Kafka::IO::REQUEST_TIMEOUT
ABresult
1X1
011
000
287 1 ▶ $self->_error( $ERROR_MISMATCH_ARGUMENT, '->receive' )
Branch: 1 branch
BranchTrueFalseTotal
unless $timeout > 0 0 1 1
288 unless $timeout > 0
289 ;
290 1 my $select = $self->{_io_select};
291 1 ▶ $self->_error( $ERROR_NO_CONNECTION, 'Attempt to work with a closed socket' ) unless $select;
Branch: 1 branch
BranchTrueFalseTotal
unless $select 0 1 1
292  
293 1 ▶ $self->_debug_msg( $message, 'Request to', 'green' )
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug_level >= 2 0 1 1
294 if $self->debug_level >= 2
295 ;
296 1 my $sent = 0;
297  
298 1 my $started = Time::HiRes::time();
299 1 my $until = $started + $timeout;
300  
301 1 my $error_code;
302 my $errno;
303 1 my $retries = 0;
304 1 my $interrupts = 0;
305 1 ▶ ATTEMPT: while ( $sent < $length && $retries++ < $MAX_RETRIES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sent < $length and $retries++ < $MAX_RETRIES) 1 1 2
Condition66%2/3
$sent < $length and $retries++ < $MAX_RETRIES
!ll&&!rl&&r
101
MC/DC50%1/2
$sent < $length and $retries++ < $MAX_RETRIES
$sent < $length $retries++ < $MAX_RETRIES
Truth table66%2/3
A and B
A= $sent < $lengthB= $retries++ < $MAX_RETRIES
ABresult
0X0
100
111
306 1 my $remaining_time = $until - Time::HiRes::time();
307 1 ▶ last ATTEMPT if $remaining_time <= 0; # timeout expired
Branch: 1 branch
BranchTrueFalseTotal
if $remaining_time <= 0 0 1 1
308  
309 1 undef $!;
310 1 my $can_write = $select->can_write( $remaining_time );
311 1 $errno = $!;
312 1 ▶ if ( $errno ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($errno) 0 1 1
313 0 ▶ if ( $errno == EINTR ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($errno == 4) 0 0 0
314 0 undef $errno;
315 0 --$retries; # this attempt does not count
316 0 ++$interrupts;
317 0 next ATTEMPT;
318 }
319  
320 0 $self->close;
321  
322 0 last ATTEMPT;
323 }
324  
325 1 ▶ if ( $can_write ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($can_write) 1 0 1
326 # check for EOF on the first attempt only
327 1 ▶ if ( $retries == 1 && $self->_is_close_wait ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($retries == 1 and $self->_is_close_wait) 0 1 1
Condition33%1/3
$retries == 1 and $self->_is_close_wait
!ll&&!rl&&r
010
MC/DC0%0/2
$retries == 1 and $self->_is_close_wait
$retries == 1 $self->_is_close_wait
Truth table33%1/3
A and B
A= $retries == 1B= $self->_is_close_wait
ABresult
0X0
100
111
328 0 $self->close;
329 0 $error_code = $ERROR_NO_CONNECTION;
330 0 last ATTEMPT;
331 }
332  
333 1 undef $!;
334 1 my $wrote = CORE::send( $self->{socket}, $message, MSG_DONTWAIT );
335 1 $errno = $!;
336  
337 1 ▶ if( defined $wrote && $wrote > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $wrote and $wrote > 0) 1 0 1
Condition33%1/3
defined $wrote and $wrote > 0
!ll&&!rl&&r
001
MC/DC0%0/2
defined $wrote and $wrote > 0
defined $wrote $wrote > 0
Truth table33%1/3
A and B
A= defined $wroteB= $wrote > 0
ABresult
0X0
100
111
338 1 $sent += $wrote;
339 1 ▶ if ( $sent < $length ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sent < $length) 0 1 1
340 # remove written data from message
341 0 $message = substr( $message, $wrote );
342 }
343 }
344  
345 1 ▶ if( $errno ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($errno) 0 1 1
346 0 ▶ if( $errno == EINTR ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($errno == 4) { } 0 0 0
elsif ($errno != 11 and $errno != 11 and not $errno == 104 && $^O eq 'freebsd') { } 0 0 0
347 0 undef $errno;
348 0 --$retries; # this attempt does not count
349 0 ++$interrupts;
350 0 next ATTEMPT;
351 ▶ } elsif (
Condition0%0/9
$errno != 11 and $errno != 11 and not $errno == 104 && $^O eq "freebsd"
!ll&&!rl&&r
000
$errno != 11 and $errno != 11
!ll&&!rl&&r
000
$errno == 104 && $^O eq 'freebsd'
!ll&&!rl&&r
000
MC/DC0%0/4
$errno != 11 and $errno != 11 and not $errno == 104 && $^O eq "freebsd"
$errno != 11 $errno != 11 $errno == 104 $^O eq 'freebsd'
Truth table0%0/5
A and B and not(C && D)
A= $errno != 11B= $errno != 11C= $errno == 104D= $^O eq 'freebsd'
ABCDresult
0XXX0
10XX0
11110
110X1
11101
352 $errno != EAGAIN
353 && $errno != EWOULDBLOCK
354 ## on freebsd, if we got ECONNRESET, it's a timeout from the other side
355 && !( $errno == ECONNRESET && $^O eq 'freebsd' )
356 ) {
357 0 $self->close;
358 0 last ATTEMPT;
359 }
360 }
361  
362 1 ▶ last ATTEMPT unless defined $wrote;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $wrote 0 1 1
363 }
364 }
365  
366 1 ▶ unless( !$errno && defined( $sent ) && $sent == $length )
Branch: 1 branch
BranchTrueFalseTotal
unless (not $errno and defined $sent and $sent == $length) 0 1 1
Condition33%2/6
not $errno and defined $sent and $sent == $length
!ll&&!rl&&r
001
not $errno and defined $sent
!ll&&!rl&&r
001
MC/DC0%0/3
not $errno and defined $sent and $sent == $length
not $errno defined $sent $sent == $length
Truth table25%1/4
not(A) and B and C
A= not $errnoB= defined $sentC= $sent == $length
ABCresult
0XX0
10X0
1100
1111
367 {
368 $self->_error(
369 $error_code // $ERROR_CANNOT_SEND,
370 format_message( "Kafka::IO(%s)->send: ERRNO=%s ERROR='%s' (length=%s, sent=%s, timeout=%s, retries=%s, interrupts=%s, secs=%.6f)",
371 $self->{host},
372 0 ▶ ( $errno // 0 ) + 0,
Condition0%0/7
$error_code // $Kafka::IO::ERROR_CANNOT_SEND
l!l&&r!l&&!r
000
$errno // 0
l!l
00
$errno // "<none>"
l!l
00
MC/DC0%0/2
$error_code // $Kafka::IO::ERROR_CANNOT_SEND
$error_code $Kafka::IO::ERROR_CANNOT_SEND
MC/DC0%0/1
$errno // 0
$errno
MC/DC0%0/1
$errno // "<none>"
$errno
Truth table0%0/3
A // B
A= $error_codeB= $Kafka::IO::ERROR_CANNOT_SEND
ABresult
1X1
011
000
Truth table0%0/2
A
A= $errno
Aresult
11
00
Truth table0%0/2
A
A= $errno
Aresult
11
00
373 ( $errno // '<none>' ) . '',
374 $length,
375 $sent,
376 $timeout,
377 $retries,
378 $interrupts,
379 Time::HiRes::time() - $started,
380 )
381 );
382 }
383  
384 1 return $sent;
385 }
386  
387 =head3 C<< receive( $length <, $timeout> ) >>
388  
389 Receives a message up to C<$length> size from Kafka.
390  
391 C<$length> argument must be a positive number.
392  
393 Use optional C<$timeout> argument to override default timeout for this call only.
394  
395 Returns a reference to the received message.
396  
397 =cut
398 sub receive {
399 1 ▶ my ( $self, $length, $timeout ) = @_;
Subroutine receive: called CC=35 SCAR=57.8
Pod documented
400 1 ▶ $self->_error( $ERROR_MISMATCH_ARGUMENT, '->receive' )
Branch: 1 branch
BranchTrueFalseTotal
unless $length > 0 0 1 1
401 unless $length > 0
402 ;
403 1 ▶ $timeout = $self->{timeout} // $REQUEST_TIMEOUT unless defined $timeout;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $timeout 1 0 1
Condition33%1/3
$self->{"timeout"} // $Kafka::IO::REQUEST_TIMEOUT
l!l&&r!l&&!r
100
MC/DC0%0/2
$self->{"timeout"} // $Kafka::IO::REQUEST_TIMEOUT
$self->{"timeout"} $Kafka::IO::REQUEST_TIMEOUT
Truth table33%1/3
A // B
A= $self->{"timeout"}B= $Kafka::IO::REQUEST_TIMEOUT
ABresult
1X1
011
000
404 1 ▶ $self->_error( $ERROR_MISMATCH_ARGUMENT, '->receive' )
Branch: 1 branch
BranchTrueFalseTotal
unless $timeout > 0 0 1 1
405 unless $timeout > 0
406 ;
407 1 my $select = $self->{_io_select};
408 1 ▶ $self->_error( $ERROR_NO_CONNECTION, 'Attempt to work with a closed socket' ) unless $select;
Branch: 1 branch
BranchTrueFalseTotal
unless $select 0 1 1
409  
410 1 my $message = '';
411 1 my $len_to_read = $length;
412  
413 1 my $started = Time::HiRes::time();
414 1 my $until = $started + $timeout;
415  
416 1 my $error_code;
417 my $errno;
418 1 my $retries = 0;
419 1 my $interrupts = 0;
420 1 ▶ ATTEMPT: while ( $len_to_read > 0 && $retries++ < $MAX_RETRIES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len_to_read > 0 and $retries++ < $MAX_RETRIES) 1 1 2
Condition66%2/3
$len_to_read > 0 and $retries++ < $MAX_RETRIES
!ll&&!rl&&r
101
MC/DC50%1/2
$len_to_read > 0 and $retries++ < $MAX_RETRIES
$len_to_read > 0 $retries++ < $MAX_RETRIES
Truth table66%2/3
A and B
A= $len_to_read > 0B= $retries++ < $MAX_RETRIES
ABresult
0X0
100
111
421 1 my $remaining_time = $until - Time::HiRes::time();
422 1 ▶ last if $remaining_time <= 0; # timeout expired
Branch: 1 branch
BranchTrueFalseTotal
if $remaining_time <= 0 0 1 1
423  
424 1 undef $!;
425 1 my $can_read = $select->can_read( $remaining_time );
426 1 $errno = $!;
427 1 ▶ if ( $errno ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($errno) 0 1 1
428 0 ▶ if ( $errno == EINTR ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($errno == 4) 0 0 0
429 0 undef $errno;
430 0 --$retries; # this attempt does not count
431 0 ++$interrupts;
432 0 next ATTEMPT;
433 }
434  
435 0 $self->close;
436  
437 0 last ATTEMPT;
438 }
439  
440 1 ▶ if ( $can_read ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($can_read) 1 0 1
441 1 my $buf = '';
442 1 undef $!;
443 1 my $from_recv = CORE::recv( $self->{socket}, $buf, $len_to_read, MSG_DONTWAIT );
444 1 $errno = $!;
445  
446 1 ▶ if ( defined( $from_recv ) && length( $buf ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $from_recv and length $buf) 1 0 1
Condition33%1/3
defined $from_recv and length $buf
!ll&&!rl&&r
001
MC/DC0%0/2
defined $from_recv and length $buf
defined $from_recv length $buf
Truth table33%1/3
A and B
A= defined $from_recvB= length $buf
ABresult
0X0
100
111
447 1 $message .= $buf;
448 1 $len_to_read = $length - length( $message );
449 1 --$retries; # this attempt was successful, don't count as a retry
450 }
451 1 ▶ if ( $errno ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($errno) 0 1 1
452 0 ▶ if ( $errno == EINTR ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($errno == 4) { } 0 0 0
elsif ($errno != 11 and $errno != 11 and not $errno == 104 && $^O eq 'freebsd') { } 0 0 0
453 0 undef $errno;
454 0 --$retries; # this attempt does not count
455 0 ++$interrupts;
456 0 next ATTEMPT;
457 ▶ } elsif (
Condition0%0/9
$errno != 11 and $errno != 11 and not $errno == 104 && $^O eq "freebsd"
!ll&&!rl&&r
000
$errno != 11 and $errno != 11
!ll&&!rl&&r
000
$errno == 104 && $^O eq 'freebsd'
!ll&&!rl&&r
000
MC/DC0%0/4
$errno != 11 and $errno != 11 and not $errno == 104 && $^O eq "freebsd"
$errno != 11 $errno != 11 $errno == 104 $^O eq 'freebsd'
Truth table0%0/5
A and B and not(C && D)
A= $errno != 11B= $errno != 11C= $errno == 104D= $^O eq 'freebsd'
ABCDresult
0XXX0
10XX0
11110
110X1
11101
458 $errno != EAGAIN
459 && $errno != EWOULDBLOCK
460 ## on freebsd, if we got ECONNRESET, it's a timeout from the other side
461 && !( $errno == ECONNRESET && $^O eq 'freebsd' )
462 ) {
463 0 $self->close;
464 0 last ATTEMPT;
465 }
466 }
467  
468 1 ▶ if ( length( $buf ) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $buf == 0) 0 1 1
469 0 ▶ if( defined( $from_recv ) && ! $errno ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $from_recv and not $errno) 0 0 0
Condition0%0/3
defined $from_recv and not $errno
!ll&&!rl&&r
000
MC/DC0%0/2
defined $from_recv and not $errno
defined $from_recv not $errno
Truth table0%0/3
A and not(B)
A= defined $from_recvB= not $errno
ABresult
0X0
100
111
470 # no error and nothing received with select returning "can read" means EOF: other side closed socket
471 0 ▶ $self->_debug_msg( 'EOF on receive attempt, closing socket' )
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug_level 0 0 0
472 if $self->debug_level;
473 0 $self->close;
474  
475 0 ▶ if( length( $message ) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $message == 0) 0 0 0
476 # we did not receive anything yet, so we may (in some cases) reconnect and try again
477 0 $error_code = $ERROR_NO_CONNECTION;
478 }
479  
480 0 last ATTEMPT;
481 }
482 # we did not read anything on this attempt: wait a bit before the next one; should not happen, but just in case...
483 0 ▶ if ( my $remaining_attempts = $MAX_RETRIES - $retries ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $remaining_attempts = $MAX_RETRIES - $retries) 0 0 0
484 0 $remaining_time = $until - Time::HiRes::time();
485 0 my $micro_seconds = int( $remaining_time * 1e6 / $remaining_attempts );
486 0 ▶ if ( $micro_seconds > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($micro_seconds > 0) 0 0 0
487 0 ▶ $micro_seconds = 250_000 if $micro_seconds > 250_000; # prevent long sleeps if total remaining time is big
Branch: 1 branch
BranchTrueFalseTotal
if $micro_seconds > 250000 0 0 0
488 0 ▶ $self->_debug_msg( format_message( 'sleeping (remaining attempts %d, time %.6f): %d microseconds', $remaining_attempts, $remaining_time, $micro_seconds ) )
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug_level 0 0 0
489 if $self->debug_level;
490 0 Time::HiRes::usleep( $micro_seconds );
491 }
492 }
493 }
494 }
495 }
496  
497 1 ▶ unless( !$errno && length( $message ) >= $length )
Branch: 1 branch
BranchTrueFalseTotal
unless (not $errno and length $message >= $length) 0 1 1
Condition33%1/3
not $errno and length $message >= $length
!ll&&!rl&&r
001
MC/DC0%0/2
not $errno and length $message >= $length
not $errno length $message >= $length
Truth table33%1/3
not(A) and B
A= not $errnoB= length $message >= $length
ABresult
0X0
100
111
498 {
499 $self->_error(
500 $error_code // $ERROR_CANNOT_RECV,
501 format_message( "Kafka::IO(%s)->receive: ERRNO=%s ERROR='%s' (length=%s, received=%s, timeout=%s, retries=%s, interrupts=%s, secs=%.6f)",
502 $self->{host},
503 0 ▶ ( $errno // 0 ) + 0,
Condition0%0/7
$error_code // $Kafka::IO::ERROR_CANNOT_RECV
l!l&&r!l&&!r
000
$errno // 0
l!l
00
$errno // "<none>"
l!l
00
MC/DC0%0/2
$error_code // $Kafka::IO::ERROR_CANNOT_RECV
$error_code $Kafka::IO::ERROR_CANNOT_RECV
MC/DC0%0/1
$errno // 0
$errno
MC/DC0%0/1
$errno // "<none>"
$errno
Truth table0%0/3
A // B
A= $error_codeB= $Kafka::IO::ERROR_CANNOT_RECV
ABresult
1X1
011
000
Truth table0%0/2
A
A= $errno
Aresult
11
00
Truth table0%0/2
A
A= $errno
Aresult
11
00
504 ( $errno // '<none>' ) . '',
505 $length,
506 length( $message ),
507 $timeout,
508 $retries,
509 $interrupts,
510 Time::HiRes::time() - $started,
511 ),
512 );
513 }
514 1 ▶ $self->_debug_msg( $message, 'Response from', 'yellow' )
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug_level >= 2 0 1 1
515 if $self->debug_level >= 2;
516  
517 # returns tainted data
518 1 return \$message;
519 }
520  
521 =head3 C<close>
522  
523 Closes connection to Kafka server.
524 Returns true if those operations succeed and if no error was reported by any PerlIO layer.
525  
526 =cut
527 sub close {
528 1 ▶ my ( $self ) = @_;
Subroutine close: called CC=2 SCAR=7.0
Pod documented
529  
530 1 my $ret = 1;
531 1 ▶ if ( $self->{socket} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"socket"}) 1 0 1
532 1 $ret = CORE::close( $self->{socket} );
533 1 $self->{socket} = undef;
534 1 $self->{_io_select} = undef;
535 }
536  
537 1 return $ret;
538 }
539  
540 sub _is_close_wait {
541 1 ▶ my ( $self ) = @_;
Subroutine _is_close_wait: called CC=6 SCAR=29.1
542 1 ▶ return 1 unless $self->{socket} && $self->{_io_select}; # closed already
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"socket"} and $self->{"_io_select"} 0 1 1
Condition33%1/3
$self->{"socket"} and $self->{"_io_select"}
!ll&&!rl&&r
001
MC/DC0%0/2
$self->{"socket"} and $self->{"_io_select"}
$self->{"socket"} $self->{"_io_select"}
Truth table33%1/3
A and B
A= $self->{"socket"}B= $self->{"_io_select"}
ABresult
0X0
100
111
543 # http://stefan.buettcher.org/cs/conn_closed.html
544 # socket is open; check if we can read, and if we can but recv() cannot peek, it means we got EOF
545 1 ▶ return unless $self->{_io_select}->can_read( 0 ); # we cannot read, but may be able to write
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_io_select"}->can_read(0) 1 0 1
546 0 my $buf = '';
547 0 undef $!;
548 0 my $status = CORE::recv( $self->{socket}, $buf, 1, MSG_DONTWAIT | MSG_PEEK ); # peek, do not remove data from queue
549 # EOF when there is no error, status is defined, but result is empty
550 0 ▶ return ! $! && defined $status && length( $buf ) == 0;
Condition0%0/6
!$! && defined $status && length $buf == 0
!ll&&!rl&&r
000
not $! and defined $status
!ll&&!rl&&r
000
MC/DC0%0/3
!$! && defined $status && length $buf == 0
not $! defined $status length $buf == 0
Truth table0%0/4
not(A) and B && C
A= not $!B= defined $statusC= length $buf == 0
ABCresult
0XX0
10X0
1100
1111
551 }
552  
553 # The method verifies if we can connect to a Kafka broker.
554 # This is evil: opens and immediately closes a NEW connection so do not use unless there is a strong reason for it.
555 sub _is_alive {
556 3 ▶ my ( $self ) = @_;
Subroutine _is_alive: called CC=2 SCAR=6.9
557  
558 3 my $socket = $self->{socket};
559 3 ▶ return unless $socket;
Branch: 1 branch
BranchTrueFalseTotal
unless $socket 1 2 3
560  
561 2 socket( my $tmp_socket, $self->{pf}, SOCK_STREAM, IPPROTO_TCP );
562 2 my $is_alive = connect( $tmp_socket, getpeername( $socket ) );
563 2 CORE::close( $tmp_socket );
564  
565 2 return $is_alive;
566 }
567  
568 #-- private attributes ---------------------------------------------------------
569  
570 #-- private methods ------------------------------------------------------------
571  
572 # You need to have access to Kafka instance and be able to connect through TCP.
573 # uses http://devpit.org/wiki/Connect%28%29_with_timeout_%28in_Perl%29
574 sub _connect {
575 13 ▶ my ( $self ) = @_;
Subroutine _connect: called CC=30 SCAR=38.1
576  
577 13 $self->{socket} = undef;
578 13 $self->{_io_select} = undef;
579  
580 13 my $name = $self->{host};
581 13 my $port = $self->{port};
582 13 my $timeout = $self->{timeout};
583  
584 13 my $ip = '';
585 13 ▶ if ( $self->_get_family( $name ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_get_family($name)) { } 2 10 12
586 2 $ip = $self->{ip} = $name;
587 } else {
588 10 ▶ if ( defined $timeout ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $timeout) { } 10 0 10
589 10 my $remaining;
590 10 my $start = time();
591  
592 10 ▶ $self->_debug_msg( format_message( "name = '%s', number of wallclock seconds = %s", $name, ceil( $timeout ) ) )
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug_level 0 10 10
593 if $self->debug_level;
594  
595 # DNS lookup.
596 10 local $@;
597 0 ▶ my $h = set_sig_handler( 'ALRM', sub { die 'alarm clock restarted' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
598 {
599 10 mask => [ 'ALRM' ],
600 safe => 0, # perl 5.8+ uses safe signal delivery so we need unsafe signal for timeout to work
601 }
602 );
603 10 eval {
604 10 $remaining = alarm( ceil( $timeout ) );
605 10 $ip = $self->_gethostbyname( $name );
606 10 alarm 0;
607 };
608 10 alarm 0; # race condition protection
609 10 my $error = $@;
610 10 undef $h;
611  
612 10 ▶ $self->_debug_msg( format_message( "_connect: ip = '%s', error = '%s', \$? = %s, \$! = '%s'", $ip, $error, $?, $! ) )
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug_level 0 10 10
613 if $self->debug_level;
614  
615 10 ▶ die $error if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 10 10
616 10 ▶ die( format_message( "gethostbyname %s: \$? = '%s', \$! = '%s'\n", $name, $?, $! ) ) unless $ip;
Branch: 1 branch
BranchTrueFalseTotal
unless $ip 2 8 10
617  
618 8 my $elapsed = time() - $start;
619 # $SIG{ALRM} restored automatically, but we need to restart previous alarm manually
620  
621 8 ▶ $self->_debug_msg( format_message( '_connect: %s (remaining) - %s (elapsed) = %s', $remaining, $elapsed, $remaining - $elapsed ) )
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug_level 0 8 8
622 if $self->debug_level;
623 8 ▶ if ( $remaining ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remaining) 2 6 8
624 2 ▶ if ( $remaining - $elapsed > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remaining - $elapsed > 0) { } 1 1 2
625 1 ▶ $self->_debug_msg( '_connect: remaining - elapsed > 0 (to alarm restart)' )
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug_level 0 1 1
626 if $self->debug_level;
627 1 alarm( ceil( $remaining - $elapsed ) );
628 } else {
629 1 ▶ $self->_debug_msg( '_connect: remaining - elapsed < 0 (to alarm function call)' )
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug_level 0 1 1
630 if $self->debug_level;
631 # $SIG{ALRM}->();
632 1 kill ALRM => $$;
633 }
634 2 ▶ $self->_debug_msg( "_connect: after alarm 'recalled'" )
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug_level 0 2 2
635 if $self->debug_level;
636 }
637 } else {
638 0 $ip = $self->_gethostbyname( $name );
639 0 ▶ die( format_message( "could not resolve host name to IP address: %s\n", $name ) ) unless $ip;
Branch: 1 branch
BranchTrueFalseTotal
unless $ip 0 0 0
640 }
641 }
642  
643 # Create socket.
644 10 ▶ socket( my $connection, $self->{pf}, SOCK_STREAM, scalar getprotobyname( 'tcp' ) ) or die( "socket: $!\n" );
Branch: 1 branch
BranchTrueFalseTotal
socket my $connection, $self->{"pf"}, 1, scalar getprotobyname "tcp" or die "socket: $!\n" 0 10 10
645  
646 # Set autoflushing.
647 10 my $file_handle = select( $connection ); $| = 1; select $file_handle;
648  
649 # Set FD_CLOEXEC.
650 10 ▶ my $flags = fcntl( $connection, F_GETFL, 0 ) or die "fcntl: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
my $flags = fcntl($connection, 3, 0) or die "fcntl: $!\n" 0 10 10
651 10 ▶ fcntl( $connection, F_SETFL, $flags | FD_CLOEXEC ) or die "fnctl: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
fcntl $connection, 4, $flags | 1 or die "fnctl: $!\n" 0 10 10
652  
653 10 ▶ $flags = fcntl( $connection, F_GETFL, 0 ) or die "fcntl F_GETFL: $!\n"; # 0 for error, 0e0 for 0.
Branch: 1 branch
BranchTrueFalseTotal
$flags = fcntl($connection, 3, 0) or die "fcntl F_GETFL: $!\n" 0 10 10
654 10 ▶ fcntl( $connection, F_SETFL, $flags | O_NONBLOCK ) or die "fcntl F_SETFL O_NONBLOCK: $!\n"; # 0 for error, 0e0 for 0.
Branch: 1 branch
BranchTrueFalseTotal
fcntl $connection, 4, $flags | 2048 or die "fcntl F_SETFL O_NONBLOCK: $!\n" 0 10 10
655  
656 # Connect returns immediately because of O_NONBLOCK.
657 my $sockaddr = $self->{af} eq AF_INET
658 ? pack_sockaddr_in( $port, inet_aton( $ip ) )
659 10 ▶ : pack_sockaddr_in6( $port, inet_pton( $self->{af}, $ip ) )
Branch: 1 branch
BranchTrueFalseTotal
$self->{'af'} eq 2 ? : 9 1 10
660 ;
661 10 ▶ connect( $connection, $sockaddr ) || $!{EINPROGRESS} || die( format_message( "connect ip = %s, port = %s: %s\n", $ip, $port, $! ) );
Branch: 1 branch
BranchTrueFalseTotal
connect $connection, $sockaddr or $!{"EINPROGRESS"} or die format_message("connect ip = %s, port = %s: %s\n", $ip, $port, $!) 0 10 10
Condition33%1/3
connect $connection, $sockaddr or $!{"EINPROGRESS"}
l!l&&r!l&&!r
0100
MC/DC0%0/2
connect $connection, $sockaddr or $!{"EINPROGRESS"}
connect $connection, $sockaddr $!{"EINPROGRESS"}
Truth table33%1/3
A or B
A= connect $connection, $sockaddrB= $!{"EINPROGRESS"}
ABresult
1X1
011
000
662  
663 # Reset O_NONBLOCK.
664 10 ▶ $flags = fcntl( $connection, F_GETFL, 0 ) or die "fcntl F_GETFL: $!\n"; # 0 for error, 0e0 for 0.
Branch: 1 branch
BranchTrueFalseTotal
$flags = fcntl($connection, 3, 0) or die "fcntl F_GETFL: $!\n" 0 10 10
665 10 ▶ fcntl( $connection, F_SETFL, $flags & ~ O_NONBLOCK ) or die "fcntl F_SETFL not O_NONBLOCK: $!\n"; # 0 for error, 0e0 for 0.
Branch: 1 branch
BranchTrueFalseTotal
fcntl $connection, 4, $flags & '18446744073709549567' or die "fcntl F_SETFL not O_NONBLOCK: $!\n" 0 10 10
666  
667 # Use select() to poll for completion or error. When connect succeeds we can write.
668 10 my $vec = '';
669 10 vec( $vec, fileno( $connection ), 1 ) = 1;
670 10 ▶ select( undef, $vec, undef, $timeout // $REQUEST_TIMEOUT );
Condition33%1/3
$timeout // $Kafka::IO::REQUEST_TIMEOUT
l!l&&r!l&&!r
1000
MC/DC0%0/2
$timeout // $Kafka::IO::REQUEST_TIMEOUT
$timeout $Kafka::IO::REQUEST_TIMEOUT
Truth table33%1/3
A // B
A= $timeoutB= $Kafka::IO::REQUEST_TIMEOUT
ABresult
1X1
011
000
671 10 ▶ unless ( vec( $vec, fileno( $connection ), 1 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (vec $vec, fileno $connection, 1) 0 10 10
672 # If no response yet, impose our own timeout.
673 0 $! = ETIMEDOUT;
674 0 die( format_message( "connect ip = %s, port = %s: %s\n", $ip, $port, $! ) );
675 }
676  
677 # This is how we see whether it connected or there was an error. Document Unix, are you kidding?!
678 10 $! = unpack( 'L', getsockopt( $connection, SOL_SOCKET, SO_ERROR ) );
679 10 ▶ die( format_message( "connect ip = %s, port = %s: %s\n", $ip, $port, $! ) ) if $!;
Branch: 1 branch
BranchTrueFalseTotal
if $! 1 9 10
680  
681 # Set timeout on all reads and writes.
682 #
683 # Note the difference between Perl's sysread() and read() calls: sysread()
684 # queries the kernel exactly once, with max delay specified here. read()
685 # queries the kernel repeatedly until there's a read error (such as this
686 # timeout), EOF, or a full buffer. So when using read() with a timeout of one
687 # second, if the remote server sends 1 byte repeatedly at 1 second intervals,
688 # read() will read the whole buffer very slowly and sysread() will return only
689 # the first byte. The print() and syswrite() calls are similarly different.
690 # <> is of course similar to read() but delimited by newlines instead of buffer
691 # sizes.
692 9 ▶ my $timeval = _get_timeval( $timeout // $REQUEST_TIMEOUT );
Condition33%1/3
$timeout // $Kafka::IO::REQUEST_TIMEOUT
l!l&&r!l&&!r
900
MC/DC0%0/2
$timeout // $Kafka::IO::REQUEST_TIMEOUT
$timeout $Kafka::IO::REQUEST_TIMEOUT
Truth table33%1/3
A // B
A= $timeoutB= $Kafka::IO::REQUEST_TIMEOUT
ABresult
1X1
011
000
693 9 ▶ setsockopt( $connection, SOL_SOCKET, SO_SNDTIMEO, $timeval ) // die "setsockopt SOL_SOCKET, SO_SNDTIMEO: $!\n";
Condition50%1/2
setsockopt($connection, 1, 21, $timeval) // die("setsockopt SOL_SOCKET, SO_SNDTIMEO: $!\n")
l!l
90
MC/DC0%0/1
setsockopt($connection, 1, 21, $timeval) // die("setsockopt SOL_SOCKET, SO_SNDTIMEO: $!\n")
setsockopt($connection, 1, 21, $timeval)
Truth table50%1/2
A
A= setsockopt($connection, 1, 21, $timeval)
Aresult
11
00
694 9 ▶ setsockopt( $connection, SOL_SOCKET, SO_RCVTIMEO, $timeval ) // die "setsockopt SOL_SOCKET, SO_RCVTIMEO: $!\n";
Condition50%1/2
setsockopt($connection, 1, 20, $timeval) // die("setsockopt SOL_SOCKET, SO_RCVTIMEO: $!\n")
l!l
90
MC/DC0%0/1
setsockopt($connection, 1, 20, $timeval) // die("setsockopt SOL_SOCKET, SO_RCVTIMEO: $!\n")
setsockopt($connection, 1, 20, $timeval)
Truth table50%1/2
A
A= setsockopt($connection, 1, 20, $timeval)
Aresult
11
00
695  
696 9 $self->{socket} = $connection;
697 9 my $s = $self->{_io_select} = IO::Select->new;
698 9 $s->add( $self->{socket} );
699  
700 9 return $connection;
701 }
702  
703 # Packing timeval
704 # uses http://trinitum.org/wp/packing-timeval/
705 sub _get_timeval {
706 9 ▶ my $timeout = shift;
Subroutine _get_timeval: called CC=6 SCAR=25.4
707  
708 9 my $intval = int( $timeout ); # sec
709 9 my $fraction = int( ( $timeout - $intval ) * 1_000_000 ); # ms
710  
711 9 ▶ if ( $Config{osname} eq 'netbsd' && _major_osvers() >= 6 && $Config{longsize} == 4 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Kafka::IO::Config{'osname'} eq 'netbsd' and _major_osvers() >= 6 and $Kafka::IO::Config{'longsize'} == 4) { } 0 9 9
Condition33%2/6
$Kafka::IO::Config{"osname"} eq "netbsd" and _major_osvers() >= 6 and $Kafka::IO::Config{"longsize"} == 4
!ll&&!rl&&r
900
$Kafka::IO::Config{"osname"} eq "netbsd" and _major_osvers() >= 6
!ll&&!rl&&r
900
MC/DC0%0/3
$Kafka::IO::Config{"osname"} eq "netbsd" and _major_osvers() >= 6 and $Kafka::IO::Config{"longsize"} == 4
$Kafka::IO::Config{"osname"} eq "netbsd" _major_osvers() >= 6 $Kafka::IO::Config{"longsize"} == 4
Truth table25%1/4
A and B and C
A= $Kafka::IO::Config{"osname"} eq "netbsd"B= _major_osvers() >= 6C= $Kafka::IO::Config{"longsize"} == 4
ABCresult
0XX0
10X0
1100
1111
712 0 ▶ if ( defined $Config{use64bitint} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Kafka::IO::Config{'use64bitint'}) { } 0 0 0
713 0 $timeout = pack( 'QL', int( $timeout ), $fraction );
714 } else {
715 $timeout = pack(
716 'LLL',
717 (
718 0 ▶ $Config{byteorder} eq '1234'
Branch: 1 branch
BranchTrueFalseTotal
$Kafka::IO::Config{'byteorder'} eq '1234' ? : 0 0 0
719 ? ( $timeout, 0, $fraction )
720 : ( 0, $timeout, $fraction )
721 )
722 );
723 }
724 } else {
725 9 $timeout = pack( 'L!L!', $timeout, $fraction );
726 }
727  
728 9 return $timeout;
729 }
730  
731 sub _major_osvers {
732 0 ▶ my $osvers = $Config{osvers};
Subroutine _major_osvers: not called CC=1 SCAR=6.9
733 0 my ( $major_osvers ) = $osvers =~ /^(\d+)/;
734 0 $major_osvers += 0;
735  
736 0 return $major_osvers;
737 }
738  
739 sub _gethostbyname {
740 8 ▶ my ( $self, $name ) = @_;
Subroutine _gethostbyname: called CC=10 SCAR=23.1
741  
742 8 my $is_v4_fqdn = 1;
743 8 $self->{ip} = '';
744  
745 8 my $ip_version = $self->{ip_version};
746 8 ▶ if ( defined( $ip_version ) && $ip_version == $IP_V6 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ip_version and $ip_version == $Kafka::IO::IP_V6) 1 7 8
Condition100%3/3
defined $ip_version and $ip_version == $Kafka::IO::IP_V6
!ll&&!rl&&r
611
MC/DC100%2/2
defined $ip_version and $ip_version == $Kafka::IO::IP_V6
defined $ip_version $ip_version == $Kafka::IO::IP_V6
Truth table100%3/3
A and B
A= defined $ip_versionB= $ip_version == $Kafka::IO::IP_V6
ABresult
0X0
100
111
747 1 my ( $err, @addrs ) = getaddrinfo(
748 $name,
749 '', # not interested in the service name
750 {
751 family => AF_INET6,
752 socktype => SOCK_STREAM,
753 protocol => IPPROTO_TCP,
754 },
755 );
756 1 ▶ return( $self->{ip} ) if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 1 1
757  
758 1 $is_v4_fqdn = 0;
759 1 for my $addr ( @addrs ) {
760 1 my ( $err, $ipaddr ) = getnameinfo( $addr->{addr}, NI_NUMERICHOST, NIx_NOSERV );
761 1 ▶ next if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 1 1
762  
763 1 $self->{af} = AF_INET6;
764 1 $self->{pf} = PF_INET6;
765 1 $self->{ip} = $ipaddr;
766 1 last;
767 }
768 }
769  
770 8 ▶ if ( $is_v4_fqdn && ( !defined( $ip_version ) || $ip_version == $IP_V4 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_v4_fqdn and !defined($ip_version) || $ip_version == $Kafka::IO::IP_V4) 7 1 8
Condition66%4/6
$is_v4_fqdn and !defined($ip_version) || $ip_version == $Kafka::IO::IP_V4
!ll&&!rl&&r
107
not defined $ip_version or $ip_version == $Kafka::IO::IP_V4
l!l&&r!l&&!r
610
MC/DC33%1/3
$is_v4_fqdn and !defined($ip_version) || $ip_version == $Kafka::IO::IP_V4
$is_v4_fqdn not defined $ip_version $ip_version == $Kafka::IO::IP_V4
Truth table50%2/4
A and not(B) or C
A= $is_v4_fqdnB= not defined $ip_versionC= $ip_version == $Kafka::IO::IP_V4
ABCresult
0XX0
1000
11X1
1011
771 7 ▶ if ( my $ipaddr = gethostbyname( $name ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ipaddr = gethostbyname $name) 5 2 7
772 5 $self->{ip} = inet_ntop( $self->{af}, $ipaddr );
773 }
774 }
775  
776 8 return $self->{ip};
777 }
778  
779 sub _get_family {
780 13 ▶ my ( $self, $name ) = @_;
Subroutine _get_family: called CC=19 SCAR=32.4
781  
782 13 my $is_ip;
783 13 ▶ my $ip_version = $self->{ip_version} // 0;
Condition100%2/2
$self->{"ip_version"} // 0
l!l
49
MC/DC100%1/1
$self->{"ip_version"} // 0
$self->{"ip_version"}
Truth table100%2/2
A
A= $self->{"ip_version"}
Aresult
11
00
784 13 ▶ if ( ( ( $is_ip = is_ipv6( $name ) ) && !$ip_version ) || $ip_version == $IP_V6 ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($is_ip = is_ipv6($name) and not $ip_version or $ip_version == $Kafka::IO::IP_V6) { } 2 11 13
elsif ($is_ip = is_ipv4($name) and not $ip_version or $ip_version == $Kafka::IO::IP_V4) { } 3 8 11
elsif (not $ip_version) { } 8 0 8
Condition50%3/6
$is_ip = is_ipv6($name) and not $ip_version or $ip_version == $Kafka::IO::IP_V6
l!l&&r!l&&!r
0211
$is_ip = is_ipv6($name) and not $ip_version
!ll&&!rl&&r
1300
MC/DC33%1/3
$is_ip = is_ipv6($name) and not $ip_version or $ip_version == $Kafka::IO::IP_V6
$is_ip = is_ipv6($name) not $ip_version $ip_version == $Kafka::IO::IP_V6
Truth table40%2/5
A and not(B) or C
A= $is_ip = is_ipv6($name)B= not $ip_versionC= $ip_version == $Kafka::IO::IP_V6
ABCresult
11X1
0X11
1011
0X00
1000
785 2 ▶ $self->_error( $ERROR_INCOMPATIBLE_HOST_IP_VERSION, format_message( 'ip_version = %s, host = %s', $ip_version, $name ) )
Branch: 1 branch
BranchTrueFalseTotal
if $ip_version and !$is_ip && is_ipv4($name) || $is_ip && $ip_version == $Kafka::IO::IP_V4 1 1 2
Condition50%6/12
$ip_version and !$is_ip && is_ipv4($name) || $is_ip && $ip_version == $Kafka::IO::IP_V4
!ll&&!rl&&r
020
not $is_ip and is_ipv4($name) or $is_ip and $ip_version == $Kafka::IO::IP_V4
l!l&&r!l&&!r
101
not $is_ip and is_ipv4($name)
!ll&&!rl&&r
011
$is_ip and $ip_version == $Kafka::IO::IP_V4
!ll&&!rl&&r
100
MC/DC0%0/5
$ip_version and !$is_ip && is_ipv4($name) || $is_ip && $ip_version == $Kafka::IO::IP_V4
$ip_version not $is_ip is_ipv4($name) $is_ip $ip_version == $Kafka::IO::IP_V4
Truth table0%0/8
A and not(B) and C or D and E
A= $ip_versionB= not $is_ipC= is_ipv4($name)D= $is_ipE= $ip_version == $Kafka::IO::IP_V4
ABCDEresult
0XXXX0
10X0X0
10X100
1100X0
110100
111XX1
10X111
110111
786 if
787 $ip_version
788 && (
789 ( !$is_ip && is_ipv4( $name ) )
790 || ( $is_ip && $ip_version == $IP_V4 )
791 )
792 ;
793  
794 1 $self->{af} = AF_INET6;
795 1 $self->{pf} = PF_INET6;
796 ▶ } elsif ( ( ( $is_ip = is_ipv4( $name ) ) && !$ip_version ) || $ip_version == $IP_V4 ) {
Condition100%6/6
$is_ip = is_ipv4($name) and not $ip_version or $ip_version == $Kafka::IO::IP_V4
l!l&&r!l&&!r
128
$is_ip = is_ipv4($name) and not $ip_version
!ll&&!rl&&r
911
MC/DC66%2/3
$is_ip = is_ipv4($name) and not $ip_version or $ip_version == $Kafka::IO::IP_V4
$is_ip = is_ipv4($name) not $ip_version $ip_version == $Kafka::IO::IP_V4
Truth table80%4/5
A and not(B) or C
A= $is_ip = is_ipv4($name)B= not $ip_versionC= $ip_version == $Kafka::IO::IP_V4
ABCresult
11X1
0X11
1011
0X00
1000
797 3 ▶ $self->_error( $ERROR_INCOMPATIBLE_HOST_IP_VERSION, format_message( 'ip_version = %s, host = %s', $ip_version, $name ) )
Branch: 1 branch
BranchTrueFalseTotal
if $ip_version and !$is_ip && is_ipv6($name) || $is_ip && $ip_version == $Kafka::IO::IP_V6 0 3 3
Condition58%7/12
$ip_version and !$is_ip && is_ipv6($name) || $is_ip && $ip_version == $Kafka::IO::IP_V6
!ll&&!rl&&r
120
not $is_ip and is_ipv6($name) or $is_ip and $ip_version == $Kafka::IO::IP_V6
l!l&&r!l&&!r
002
not $is_ip and is_ipv6($name)
!ll&&!rl&&r
110
$is_ip and $ip_version == $Kafka::IO::IP_V6
!ll&&!rl&&r
110
MC/DC0%0/5
$ip_version and !$is_ip && is_ipv6($name) || $is_ip && $ip_version == $Kafka::IO::IP_V6
$ip_version not $is_ip is_ipv6($name) $is_ip $ip_version == $Kafka::IO::IP_V6
Truth table25%2/8
A and not(B) and C or D and E
A= $ip_versionB= not $is_ipC= is_ipv6($name)D= $is_ipE= $ip_version == $Kafka::IO::IP_V6
ABCDEresult
0XXXX0
10X0X0
10X100
1100X0
110100
111XX1
10X111
110111
798 if
799 $ip_version
800 && (
801 ( !$is_ip && is_ipv6( $name ) )
802 || ( $is_ip && $ip_version == $IP_V6 )
803 )
804 ;
805  
806 3 $self->{af} = AF_INET;
807 3 $self->{pf} = PF_INET;
808 } elsif ( !$ip_version ) {
809 8 $self->{af} = AF_INET;
810 8 $self->{pf} = PF_INET;
811 }
812  
813 12 return $is_ip;
814 }
815  
816 # Show additional debugging information
817 sub _debug_msg {
818 0 ▶ my ( $self, $message, $header, $colour ) = @_;
Subroutine _debug_msg: not called CC=3 SCAR=24.8
819  
820 0 ▶ if ( $header ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header) { } 0 0 0
821 0 ▶ unless ( $_hdr ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Kafka::IO::_hdr) 0 0 0
822 0 require Data::HexDump::Range;
823 0 $_hdr = Data::HexDump::Range->new(
824 FORMAT => 'ANSI', # 'ANSI'|'ASCII'|'HTML'
825 COLOR => 'bw', # 'bw' | 'cycle'
826 OFFSET_FORMAT => 'hex', # 'hex' | 'dec'
827 DATA_WIDTH => 16, # 16 | 20 | ...
828 DISPLAY_RANGE_NAME => 0,
829 # MAXIMUM_RANGE_NAME_SIZE => 16,
830 DISPLAY_COLUMN_NAMES => 1,
831 DISPLAY_RULER => 1,
832 DISPLAY_OFFSET => 1,
833 # DISPLAY_CUMULATIVE_OFFSET => 1,
834 DISPLAY_ZERO_SIZE_RANGE_WARNING => 0,
835 DISPLAY_ZERO_SIZE_RANGE => 1,
836 DISPLAY_RANGE_NAME => 0,
837 # DISPLAY_RANGE_SIZE => 1,
838 DISPLAY_ASCII_DUMP => 1,
839 DISPLAY_HEX_DUMP => 1,
840 # DISPLAY_DEC_DUMP => 1,
841 # COLOR_NAMES => {},
842 ORIENTATION => 'horizontal',
843 );
844 }
845  
846 say STDERR
847 0 "# $header ", $self->{host}, ':', $self->{port}, "\n",
848 '# Hex Stream: ', unpack( 'H*', $message ), "\n",
849 $_hdr->dump(
850 [
851 [ 'data', length( $message ), $colour ],
852 ],
853 $message
854 )
855 ;
856 } else {
857 0 say STDERR format_message( '[%s] %s', scalar( localtime ), $message );
858 }
859  
860 0 return;
861 }
862  
863 # Handler for errors
864 sub _error {
865 88 ▶ my $self = shift;
Subroutine _error: called CC=2 SCAR=7.0
866 88 my %args = throw_args( @_ );
867 88 ▶ $self->_debug_msg( format_message( 'throwing IO error %s: %s', $args{code}, $args{message} ) )
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug_level 0 88 88
868 if $self->debug_level;
869 88 Kafka::Exception::IO->throw( %args );
870 }
871  
872  
873  
874 15 1;
875  
876 __END__
« lib/Kafka/Connection.pm ↑ Summary lib/Kafka/Consumer.pm »