lib/Kafka/Internals.pm

filter: Statementstmt 100% 96 / 96 Branchbran 86.3% 19 / 22 Conditioncond 70.5% 12 / 17 MC/DCmcdc 45.4% 5 / 11 Subroutinesub 100% 17 / 17 Podpod 100% 3 / 3 totaltotal 91.5% 152 / 166 SCAR 29.5
CC
19
Cov
94%
format_message
9.9
debug_level
8.0
format_reference
4.0
CRAP 19.1
« blib/lib/Kafka/Int64.pm ↑ Summary blib/lib/Kafka/Exceptions.pm »
1 package Kafka::Internals;
2  
3 =head1 NAME
4  
5 Kafka::Internals - Constants and functions used internally.
6  
7 =head1 VERSION
8  
9 This documentation refers to C<Kafka::Internals> version 1.07 .
10  
11 =cut
12  
13  
14  
15 16 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19  
20  
21 16 our $VERSION = '1.07';
22  
23 16 use Exporter qw(
24 import
25 16 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 16 our @EXPORT_OK = qw(
28 $APIKEY_PRODUCE
29 $APIKEY_FETCH
30 $APIKEY_OFFSET
31 $APIKEY_METADATA
32 $APIKEY_FINDCOORDINATOR
33 $APIKEY_APIVERSIONS
34 $APIKEY_OFFSETCOMMIT
35 $APIKEY_OFFSETFETCH
36 $DEFAULT_RAISE_ERROR
37 $MAX_CORRELATIONID
38 $MAX_INT16
39 $MAX_INT32
40 $MAX_SOCKET_REQUEST_BYTES
41 $PRODUCER_ANY_OFFSET
42 debug_level
43 _isbig
44 _get_CorrelationId
45 format_message
46 format_reference
47 );
48  
49  
50  
51 16 ▶use overload;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 16 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 16 ▶use Const::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 16 ▶use Data::Dumper ();
Subroutine BEGIN: called CC=1 SCAR=0.0
55 16 use Params::Util qw(
56 _INSTANCE
57 16 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
58 16 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
59  
60 16 use Kafka qw(
61 %ERROR
62 $ERROR_MISMATCH_ARGUMENT
63 16 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
64  
65  
66  
67 =head1 SYNOPSIS
68  
69 use 5.010;
70 use strict;
71 use warnings;
72  
73 use Kafka::Internals qw(
74 $MAX_SOCKET_REQUEST_BYTES
75 );
76  
77 my $bin_stream_size = $MAX_SOCKET_REQUEST_BYTES;
78  
79 =head1 DESCRIPTION
80  
81 This module is private and should not be used directly.
82  
83 In order to achieve better performance, functions of this module do
84 not perform arguments validation.
85  
86 =head2 EXPORT
87  
88 The following constants are available for export
89  
90 =cut
91  
92 #-- Api Keys
93  
94 =head3 C<$APIKEY_PRODUCE>
95  
96 The numeric code that the C<ApiKey> in the request take for the C<ProduceRequest> request type.
97  
98 =cut
99 16 const our $APIKEY_PRODUCE => 0;
100  
101 =head3 C<$APIKEY_FETCH>
102  
103 The numeric code that the C<ApiKey> in the request take for the C<FetchRequest> request type.
104  
105 =cut
106 16 const our $APIKEY_FETCH => 1;
107  
108 =head3 C<$APIKEY_OFFSET>
109  
110 The numeric code that the C<ApiKey> in the request take for the C<OffsetRequest> request type.
111  
112 =cut
113 16 const our $APIKEY_OFFSET => 2;
114  
115 =head3 C<$APIKEY_METADATA>
116  
117 The numeric code that the C<ApiKey> in the request take for the C<MetadataRequest> request type.
118  
119 =cut
120 16 const our $APIKEY_METADATA => 3;
121 # The numeric code that the ApiKey in the request take for the C<LeaderAndIsrRequest> request type.
122 16 const our $APIKEY_LEADERANDISR => 4; # Not used now
123 # The numeric code that the ApiKey in the request take for the C<StopReplicaRequest> request type.
124 16 const our $APIKEY_STOPREPLICA => 5; # Not used now
125  
126 =head3 C<$APIKEY_OFFSETCOMMIT>
127  
128 The numeric code that the ApiKey in the request take for the C<OffsetCommitRequest> request type.
129  
130 =cut
131 16 const our $APIKEY_OFFSETCOMMIT => 8;
132  
133 =head3 C<$APIKEY_OFFSETFETCH>
134  
135 The numeric code that the ApiKey in the request take for the C<OffsetFetchRequest> request type.
136  
137 =cut
138 16 const our $APIKEY_OFFSETFETCH => 9;
139  
140 =head3 C<$APIKEY_FINDCOORDINATOR>
141  
142 The numeric code that the C<ApiKey> in the request take for the C<FindCoordinator> request type.
143  
144 =cut
145 16 const our $APIKEY_FINDCOORDINATOR => 10;
146  
147 =head3 C<$APIKEY_APIVERSIONS>
148  
149 The numeric code that the C<ApiKey> in the request take for the C<ApiVersions> request type.
150  
151 =cut
152 16 const our $APIKEY_APIVERSIONS => 18;
153  
154 # Important configuration properties
155  
156 =head3 C<$MAX_SOCKET_REQUEST_BYTES>
157  
158 The maximum number of bytes in a socket request.
159  
160 The maximum size of a request that the socket server will accept.
161 Default limit (as configured in F<server.properties>) is 104857600.
162  
163 =cut
164 16 const our $MAX_SOCKET_REQUEST_BYTES => 100 * 1024 * 1024;
165  
166 =head3 C<$PRODUCER_ANY_OFFSET>
167  
168 According to Apache Kafka documentation: 'When the producer is sending messages it doesn't actually know the offset and can fill in any
169 value here it likes.'
170  
171 =cut
172 16 const our $PRODUCER_ANY_OFFSET => 0;
173  
174 =head3 C<$MAX_CORRELATIONID>
175  
176 Largest positive integer on 32-bit machines.
177  
178 =cut
179 16 const our $MAX_CORRELATIONID => 0x7fffffff;
180  
181 =head3 C<$MAX_INT32>
182  
183 Largest positive integer on 32-bit machines.
184  
185 =cut
186 16 const our $MAX_INT32 => 0x7fffffff;
187  
188 =head3 C<$MAX_INT16>
189  
190 Largest positive int16 value.
191  
192 =cut
193 16 const our $MAX_INT16 => 0x7fff;
194  
195 #-- public functions -----------------------------------------------------------
196  
197 #-- private functions ----------------------------------------------------------
198  
199 # Used to generate a CorrelationId.
200 sub _get_CorrelationId {
201 10174 ▶ return( -int( rand( $MAX_CORRELATIONID ) ) );
Subroutine _get_CorrelationId: called CC=1 SCAR=0.0
202 }
203  
204 # Verifies that the argument is of Math::BigInt type
205 sub _isbig {
206 10386 ▶ my ( $num ) = @_;
Subroutine _isbig: called CC=1 SCAR=0.0
207  
208 10386 return defined _INSTANCE( $num, 'Math::BigInt' );
209 }
210  
211 #-- public attributes ----------------------------------------------------------
212  
213 =head2 METHODS
214  
215 The following methods are defined in the C<Kafka::Internals>:
216  
217 =cut
218  
219 #-- public methods -------------------------------------------------------------
220  
221 =head3 C<debug_level( $flags )>
222  
223 Gets or sets debug level for a particular L<Kafka|Kafka> module, based on environment variable C<PERL_KAFKA_DEBUG> or flags.
224  
225 $flags - (string) argument that can be used to pass coma delimited module names (omit C<Kafka::>).
226  
227 Returns C<$DEBUG> level for the module from which C<debug_level> was called.
228  
229 =cut
230 16 our %_debug_levels; # per-module levels cache to speed-up multiple calls to debug_level()
231 sub debug_level {
232 16 ▶ no strict 'refs'; ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
233  
234 36552 ▶ my $class = ref( $_[0] ) || $_[0];
Subroutine debug_level: called CC=8 SCAR=20.8
Pod documented
Condition66%2/3
ref $_[0] || $_[0]
l!l&&r!l&&!r
15728208240
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
Truth table66%2/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
235  
236 36552 ▶ return ${ $_debug_levels{ $class } } if @_ == 1 && exists $_debug_levels{ $class };
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 and exists $Kafka::Internals::_debug_levels{$class} 36521 31 36552
Condition100%3/3
@_ == 1 and exists $Kafka::Internals::_debug_levels{$class}
!ll&&!rl&&r
82336521
MC/DC100%2/2
@_ == 1 and exists $Kafka::Internals::_debug_levels{$class}
@_ == 1 exists $Kafka::Internals::_debug_levels{$class}
Truth table100%3/3
A and B
A= @_ == 1B= exists $Kafka::Internals::_debug_levels{$class}
ABresult
0X0
100
111
237  
238 31 ▶ my $flags = $_[1] // $ENV{PERL_KAFKA_DEBUG};
Condition100%3/3
$_[1] // $ENV{"PERL_KAFKA_DEBUG"}
l!l&&r!l&&!r
7222
MC/DC100%2/2
$_[1] // $ENV{"PERL_KAFKA_DEBUG"}
$_[1] $ENV{"PERL_KAFKA_DEBUG"}
Truth table100%3/3
A // B
A= $_[1]B= $ENV{"PERL_KAFKA_DEBUG"}
ABresult
1X1
011
000
239 31 ▶ if( defined $flags ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $flags) 9 22 31
240 9 foreach my $spec ( split /\s*,\s*/, $flags ) {
241 12 my @elements = split( /\s*:\s*/, $spec, 2 );
242 12 my( $module_name, $level );
243 12 ▶ if ( scalar( @elements ) > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @elements > 1) { } 8 4 12
244 8 ( $module_name, $level ) = @elements;
245 } else {
246 4 $module_name = ( $class =~ /([^:]+)$/ )[0];
247 4 $level = $spec;
248 }
249  
250 12 *{ "Kafka::${module_name}::DEBUG" } = \$level; ## no critic
251 }
252 }
253 31 $_debug_levels{ $class } = \${ "${class}::DEBUG" }; ## no critic
254  
255 31 return ${ $_debug_levels{ $class } };
256 }
257  
258 =head2 format_reference
259  
260 say format_reference( $object );
261  
262 Dumps reference using preconfigured L<Data::Dumper>. Produces less verbose
263 output than default L<Data::Dumper> settings.
264  
265 =cut
266  
267 16 my $dumper;
268 16 my $empty_array = [];
269  
270 sub format_reference {
271 62 ▶ my ( $value ) = @_;
Subroutine format_reference: called CC=4 SCAR=13.9
Pod documented
272  
273 62 ▶ unless( $dumper ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dumper) 5 57 62
274 5 $dumper = Data::Dumper->new( $empty_array )
275 ->Indent( 0 )
276 ->Terse( 1 )
277 ->Quotekeys( 0 )
278 ->Sortkeys( 1 )
279 ->Useperl( 1 ) # XS version seems to have a bug which sometimes results in modification of original object
280 # ->Sparseseen( 1 ) # speed up since we don't use "Seen" hash
281 ;
282 }
283  
284 62 my $r;
285 62 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (overload::Overloaded($value) and overload::Method($value, '""')) { } 8 54 62
Condition66%2/3
overload::Overloaded($value) and overload::Method($value, "\"\"")
!ll&&!rl&&r
5408
MC/DC50%1/2
overload::Overloaded($value) and overload::Method($value, "\"\"")
overload::Overloaded($value) overload::Method($value, "\"\"")
Truth table66%2/3
A and B
A= overload::Overloaded($value)B= overload::Method($value, "\"\"")
ABresult
0X0
100
111
286 overload::Overloaded( $value ) &&
287 overload::Method( $value, '""' )
288 ) {
289 8 $r = "$value"; # force stringification
290 } else {
291 54 $r = $dumper->Values( [ $value ] )->Dump;
292 54 $dumper->Reset->Values( $empty_array );
293 }
294  
295 62 return $r;
296 }
297  
298 =head2 format_message
299  
300 $string = format_message( 'Object %d loaded. Status: %s', $id, $message );
301  
302 Returns string formatted using printf-style syntax.
303  
304 If there are more than one argument and the first argument contains C<%...>
305 conversions, arguments are converted to a string message using C<sprintf()>. In this case, undefined
306 values are printed as C<< <undef> >> and references are converted to strings using L</format_reference>.
307  
308 =cut
309 sub format_message {
310 825 ▶ my $format = shift // return;
Subroutine format_message: called CC=9 SCAR=22.9
Pod documented
Condition50%1/2
shift() // (return)
l!l
8250
MC/DC0%0/1
shift() // (return)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
311  
312 825 my $got = scalar @_;
313  
314 825 ▶ return $format unless $got && $format =~ /\%/;
Branch: 1 branch
BranchTrueFalseTotal
unless $got and $format =~ /\%/ 0 825 825
Condition33%1/3
$got and $format =~ /\%/
!ll&&!rl&&r
00825
MC/DC0%0/2
$got and $format =~ /\%/
$got $format =~ /\%/
Truth table33%1/3
A and B
A= $gotB= $format =~ /\%/
ABresult
0X0
100
111
315  
316 825 my $expected = 0;
317 825 ▶ while ( $format =~ /(%%|%[^%])/g ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($format =~ /(%%|%[^%])/g) 1821 825 2646
318 1821 ▶ next if $1 eq '%%'; # don't count escape sequence
Branch: 1 branch
BranchTrueFalseTotal
if $1 eq "%%" 0 1821 1821
319 1821 ++$expected;
320 }
321  
322 825 ▶ Carp::cluck "Wrong number of arguments: $expected vs $got" unless $got == $expected;
Branch: 1 branch
BranchTrueFalseTotal
unless $got == $expected 0 825 825
323  
324 return sprintf $format, map {
325 825 ▶ !defined $_
Branch: 2 branches
BranchTrueFalseTotal
!defined($_) ? : 6 1815 1821
ref $_ ? : 62 1753 1815
326 ? '<undef>'
327 : ref $_
328 ? format_reference( $_ )
329 : $_
330 } @_;
331 }
332  
333 #-- private attributes ---------------------------------------------------------
334  
335 #-- private methods ------------------------------------------------------------
336  
337 16 1;
338  
339 __END__
« blib/lib/Kafka/Int64.pm ↑ Summary blib/lib/Kafka/Exceptions.pm »