lib/Kafka/Consumer.pm

filter: Statementstmt 73.6% 95 / 129 Branchbran 55.5% 40 / 72 Conditioncond 42.5% 80 / 188 MC/DCmcdc 21.9% 20 / 91 Subroutinesub 72.7% 16 / 22 Podpod 100% 9 / 9 totaltotal 50.8% 260 / 511 SCAR 70.1
CC
106
Cov
55%
commit_offsets
272.0
fetch_offsets
156.0
fetch
58.4
CRAP 1111.6
« lib/Kafka/IO.pm ↑ Summary blib/lib/Kafka/Consumer.pm »
1 package Kafka::Consumer;
2  
3 =head1 NAME
4  
5 Kafka::Consumer - Perl interface for Kafka consumer client.
6  
7 =head1 VERSION
8  
9 This documentation refers to C<Kafka::Consumer> version 1.07 .
10  
11 =cut
12  
13 5 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 5 our $VERSION = '1.07';
18  
19 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 5 use Params::Util qw(
21 _INSTANCE
22 _NONNEGINT
23 _NUMBER
24 _POSINT
25 _STRING
26 5 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
27 5 use Scalar::Util::Numeric qw(
28 isint
29 5 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 5 use Kafka qw(
32 $BITS64
33 $DEFAULT_MAX_BYTES
34 $DEFAULT_MAX_NUMBER_OF_OFFSETS
35 $DEFAULT_MAX_WAIT_TIME
36 %ERROR
37 $ERROR_CANNOT_GET_METADATA
38 $ERROR_METADATA_ATTRIBUTES
39 $ERROR_MISMATCH_ARGUMENT
40 $ERROR_PARTITION_DOES_NOT_MATCH
41 $ERROR_TOPIC_DOES_NOT_MATCH
42 $MESSAGE_SIZE_OVERHEAD
43 $MIN_BYTES_RESPOND_IMMEDIATELY
44 $RECEIVE_LATEST_OFFSETS
45 $RECEIVE_EARLIEST_OFFSET
46 5 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
47 5 ▶use Kafka::Exceptions;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 5 use Kafka::Internals qw(
49 $APIKEY_FETCH
50 $APIKEY_OFFSET
51 $APIKEY_OFFSETCOMMIT
52 $APIKEY_OFFSETFETCH
53 $MAX_INT32
54 _get_CorrelationId
55 _isbig
56 format_message
57 5 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
58 5 ▶use Kafka::Connection;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 5 ▶use Kafka::Message;
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61 5 ▶if ( !$BITS64 ) { eval 'use Kafka::Int64; 1;' or die "Cannot load Kafka::Int64 : $@"; } ## no critic
Branch: 2 branches
BranchTrueFalseTotal
unless ($Kafka::Consumer::BITS64) 0 5 5
eval "use Kafka::Int64; 1;" or die "Cannot load Kafka::Int64 : $@" 0 0 0
62  
63 =head1 SYNOPSIS
64  
65 use 5.010;
66 use strict;
67 use warnings;
68  
69 use Scalar::Util qw(
70 blessed
71 );
72 use Try::Tiny;
73  
74 use Kafka qw(
75 $DEFAULT_MAX_BYTES
76 $DEFAULT_MAX_NUMBER_OF_OFFSETS
77 $RECEIVE_EARLIEST_OFFSET
78 );
79 use Kafka::Connection;
80 use Kafka::Consumer;
81  
82 my ( $connection, $consumer );
83 try {
84  
85 #-- Connection
86 $connection = Kafka::Connection->new( host => 'localhost' );
87  
88 #-- Consumer
89 $consumer = Kafka::Consumer->new( Connection => $connection );
90  
91 # Get a valid offset before the given time
92 my $offsets = $consumer->offset_before_time(
93 'mytopic', # topic
94 0, # partition
95 (time()-3600) * 1000, # time
96 );
97  
98 if ( @$offsets ) {
99 say "Received offset: $_" foreach @$offsets;
100 } else {
101 warn "Error: Offsets are not received\n";
102 }
103  
104 # Consuming messages
105 my $messages = $consumer->fetch(
106 'mytopic', # topic
107 0, # partition
108 0, # offset
109 $DEFAULT_MAX_BYTES # Maximum size of MESSAGE(s) to receive
110 );
111  
112 if ( $messages ) {
113 foreach my $message ( @$messages ) {
114 if ( $message->valid ) {
115 say 'payload : ', $message->payload;
116 say 'key : ', $message->key;
117 say 'offset : ', $message->offset;
118 say 'next_offset: ', $message->next_offset;
119 } else {
120 say 'error : ', $message->error;
121 }
122 }
123 }
124  
125 } catch {
126 my $error = $_;
127 if ( blessed( $error ) && $error->isa( 'Kafka::Exception' ) ) {
128 warn 'Error: (', $error->code, ') ', $error->message, "\n";
129 exit;
130 } else {
131 die $error;
132 }
133 };
134  
135 # Closes the consumer and cleans up
136 undef $consumer;
137 $connection->close;
138 undef $connection;
139  
140 =head1 DESCRIPTION
141  
142 Kafka consumer API is implemented by C<Kafka::Consumer> class.
143  
144 The main features of the C<Kafka::Consumer> class are:
145  
146 =over 3
147  
148 =item *
149  
150 Provides an object-oriented API for consuming messages.
151  
152 =item *
153  
154 Provides Kafka FETCH and OFFSETS requests.
155  
156 =item *
157  
158 Supports parsing the Apache Kafka 0.9+ Wire Format protocol.
159  
160 =item *
161  
162 Works with 64-bit elements of the Kafka Wire Format protocol
163 on 32 bit systems.
164  
165 =back
166  
167 The Kafka consumer response returns ARRAY references for C<offsets> and
168 C<fetch> methods.
169  
170 Array returned by C<offsets> contains offset integers.
171  
172 Array returned by C<fetch> contains objects of L<Kafka::Message|Kafka::Message> class.
173  
174 =cut
175  
176 #-- constructor ----------------------------------------------------------------
177  
178 =head2 CONSTRUCTOR
179  
180 =head3 C<new>
181  
182 Creates a new consumer client object. Returns the created C<Kafka::Consumer> object.
183  
184 C<new()> takes arguments in key-value pairs. The following arguments are recognized:
185  
186 =over 3
187  
188 =item C<Connection =E<gt> $connection>
189  
190 C<$connection> is the L<Kafka::Connection|Kafka::Connection> object responsible for communication with
191 the Apache Kafka cluster.
192  
193 =item C<ClientId =E<gt> $client_id>
194  
195 This is a user supplied identifier (string) for the client application.
196  
197 C<ClientId> will be auto-assigned if not passed in when creating L<Kafka::Producer|Kafka::Producer> object.
198  
199 =item C<MaxWaitTime =E<gt> $max_time>
200  
201 The maximum amount of time (seconds, may be fractional) to wait when no sufficient data is available at the time the
202 request was issued.
203  
204 Optional, default is C<$DEFAULT_MAX_WAIT_TIME>.
205  
206 C<$DEFAULT_MAX_WAIT_TIME> is the default time that can be imported from the
207 L<Kafka|Kafka> module.
208  
209 The C<$max_time> must be a positive number.
210  
211 =item C<MinBytes =E<gt> $min_bytes>
212  
213 The minimum number of bytes of messages that must be available to give a response.
214 If the client sets this to C<$MIN_BYTES_RESPOND_IMMEDIATELY> the server will always respond
215 immediately. If it is set to C<$MIN_BYTES_RESPOND_HAS_DATA>, the server will respond as soon
216 as at least one partition has at least 1 byte of data or the specified timeout occurs.
217 Setting higher values in combination with the bigger timeouts allows reading larger chunks of data.
218  
219 Optional, int32 signed integer, default is C<$MIN_BYTES_RESPOND_IMMEDIATELY>.
220  
221 C<$MIN_BYTES_RESPOND_IMMEDIATELY>, C<$MIN_BYTES_RESPOND_HAS_DATA> are the defaults that
222 can be imported from the L<Kafka|Kafka> module.
223  
224 The C<$min_bytes> must be a non-negative int32 signed integer.
225  
226 =item C<MaxBytes =E<gt> $max_bytes>
227  
228 The maximum bytes to include in the message set for this partition.
229  
230 Optional, int32 signed integer, default = C<$DEFAULT_MAX_BYTES> (1_000_000).
231  
232 The C<$max_bytes> must be more than C<$MESSAGE_SIZE_OVERHEAD>
233 (size of protocol overhead - data added by Kafka wire protocol to each message).
234  
235 C<$DEFAULT_MAX_BYTES>, C<$MESSAGE_SIZE_OVERHEAD>
236 are the defaults that can be imported from the L<Kafka|Kafka> module.
237  
238 =item C<MaxNumberOfOffsets =E<gt> $max_number>
239  
240 Limit the number of offsets returned by Kafka.
241  
242 That is a non-negative integer.
243  
244 Optional, int32 signed integer, default = C<$DEFAULT_MAX_NUMBER_OF_OFFSETS> (100).
245  
246 C<$DEFAULT_MAX_NUMBER_OF_OFFSETS>
247 is the default that can be imported from the L<Kafka|Kafka> module.
248  
249 =back
250  
251 =cut
252 sub new {
253 179 ▶ my ( $class, %p ) = @_;
Subroutine new: called CC=21 SCAR=30.8
Pod documented
254  
255 179 my $self = bless {
256 Connection => undef,
257 ClientId => undef,
258 MaxWaitTime => $DEFAULT_MAX_WAIT_TIME,
259 MinBytes => $MIN_BYTES_RESPOND_IMMEDIATELY,
260 MaxBytes => $DEFAULT_MAX_BYTES,
261 MaxNumberOfOffsets => $DEFAULT_MAX_NUMBER_OF_OFFSETS,
262 ApiVersion => undef, # undef - allows consumer to choose newest supported
263 }, $class;
264  
265 179 ▶ exists $p{$_} and $self->{$_} = $p{$_} foreach keys %$self;
Branch: 1 branch
BranchTrueFalseTotal
exists $p{$_} and $self->{$_} = $p{$_} 594 659 1253
266  
267 179 ▶ $self->{ClientId} //= 'consumer';
Condition100%2/2
$self->{"ClientId"} //= "consumer"
l!l
8171
MC/DC100%1/1
$self->{"ClientId"} //= "consumer"
$self->{"ClientId"}
Truth table100%2/2
A
A= $self->{"ClientId"}
Aresult
11
00
268  
269 $self->_error( $ERROR_MISMATCH_ARGUMENT, 'Connection' )
270 179 ▶ unless _INSTANCE( $self->{Connection}, 'Kafka::Connection' );
Branch: 1 branch
BranchTrueFalseTotal
unless &_INSTANCE($self->{"Connection"}, "Kafka::Connection") 20 159 179
271 $self->_error( $ERROR_MISMATCH_ARGUMENT, 'ClientId' )
272 159 ▶ unless ( $self->{ClientId} eq q{} || defined( _STRING( $self->{ClientId} ) ) ) && !utf8::is_utf8( $self->{ClientId} );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"ClientId"} eq "" || defined &_STRING($self->{"ClientId"}) and not utf8::is_utf8($self->{"ClientId"}) 8 151 159
Condition83%5/6
$self->{"ClientId"} eq "" || defined &_STRING($self->{"ClientId"}) and not utf8::is_utf8($self->{"ClientId"})
!ll&&!rl&&r
71151
$self->{"ClientId"} eq "" or defined &_STRING($self->{"ClientId"})
l!l&&r!l&&!r
01527
MC/DC66%2/3
$self->{"ClientId"} eq "" || defined &_STRING($self->{"ClientId"}) and not utf8::is_utf8($self->{"ClientId"})
$self->{"ClientId"} eq "" defined &_STRING($self->{"ClientId"}) not utf8::is_utf8($self->{"ClientId"})
Truth table60%3/5
A or B and not(C)
A= $self->{"ClientId"} eq ""B= defined &_STRING($self->{"ClientId"})C= not utf8::is_utf8($self->{"ClientId"})
ABCresult
00X0
1X00
0100
1X11
0111
273 $self->_error( $ERROR_MISMATCH_ARGUMENT, format_message( 'MaxWaitTime (%s)', $self->{MaxWaitTime} ) )
274 151 ▶ unless defined( $self->{MaxWaitTime} ) && defined _NUMBER( $self->{MaxWaitTime} ) && int( $self->{MaxWaitTime} * 1000 ) >= 1 && int( $self->{MaxWaitTime} * 1000 ) <= $MAX_INT32;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"MaxWaitTime"} and defined &_NUMBER($self->{"MaxWaitTime"}) and int $self->{"MaxWaitTime"} * 1000 >= 1 and int $self->{"MaxWaitTime"} * 1000 <= $Kafka::Consumer::MAX_INT32 2 139 141
Condition88%8/9
defined $self->{"MaxWaitTime"} and defined &_NUMBER($self->{"MaxWaitTime"}) and int $self->{"MaxWaitTime"} * 1000 >= 1 and int $self->{"MaxWaitTime"} * 1000 <= $Kafka::Consumer::MAX_INT32
!ll&&!rl&&r
120139
defined $self->{"MaxWaitTime"} and defined &_NUMBER($self->{"MaxWaitTime"}) and int $self->{"MaxWaitTime"} * 1000 >= 1
!ll&&!rl&&r
102139
defined $self->{"MaxWaitTime"} and defined &_NUMBER($self->{"MaxWaitTime"})
!ll&&!rl&&r
19141
MC/DC75%3/4
defined $self->{"MaxWaitTime"} and defined &_NUMBER($self->{"MaxWaitTime"}) and int $self->{"MaxWaitTime"} * 1000 >= 1 and int $self->{"MaxWaitTime"} * 1000 <= $Kafka::Consumer::MAX_INT32
defined $self->{"MaxWaitTime"} defined &_NUMBER($self->{"MaxWaitTime"}) int $self->{"MaxWaitTime"} * 1000 >= 1 int $self->{"MaxWaitTime"} * 1000 <= $Kafka::Consumer::MAX_INT32
Truth table80%4/5
A and B and C and D
A= defined $self->{"MaxWaitTime"}B= defined &_NUMBER($self->{"MaxWaitTime"})C= int $self->{"MaxWaitTime"} * 1000 >= 1D= int $self->{"MaxWaitTime"} * 1000 <= $Kafka::Consumer::MAX_INT32
ABCDresult
0XXX0
10XX0
110X0
11100
11111
275 $self->_error( $ERROR_MISMATCH_ARGUMENT, format_message( 'MinBytes (%s)', $self->{MinBytes} ) )
276 139 ▶ unless ( _isbig( $self->{MinBytes} ) ? ( $self->{MinBytes} >= 0 ) : defined( _NONNEGINT( $self->{MinBytes} ) ) ) && $self->{MinBytes} <= $MAX_INT32;
Branch: 2 branches
BranchTrueFalseTotal
unless _isbig($self->{"MinBytes"}) ? $self->{"MinBytes"} >= 0 : defined &_NONNEGINT($self->{"MinBytes"}) and $self->{"MinBytes"} <= $Kafka::Consumer::MAX_INT32 13 126 139
_isbig($self->{'MinBytes'}) ? : 0 139 139
Condition66%2/3
_isbig($self->{"MinBytes"}) ? $self->{"MinBytes"} >= 0 : defined &_NONNEGINT($self->{"MinBytes"}) and $self->{"MinBytes"} <= $Kafka::Consumer::MAX_INT32
!ll&&!rl&&r
130126
MC/DC50%1/2
_isbig($self->{"MinBytes"}) ? $self->{"MinBytes"} >= 0 : defined &_NONNEGINT($self->{"MinBytes"}) and $self->{"MinBytes"} <= $Kafka::Consumer::MAX_INT32
_isbig($self->{"MinBytes"}) ? $self->{"MinBytes"} >= 0 : defined &_NONNEGINT($self->{"MinBytes"}) $self->{"MinBytes"} <= $Kafka::Consumer::MAX_INT32
Truth table66%2/3
A and B
A= _isbig($self->{"MinBytes"}) ? $self->{"MinBytes"} >= 0 : defined &_NONNEGINT($self->{"MinBytes"})B= $self->{"MinBytes"} <= $Kafka::Consumer::MAX_INT32
ABresult
0X0
100
111
277 $self->_error( $ERROR_MISMATCH_ARGUMENT, format_message( 'MaxBytes (%s)', $self->{MaxBytes} ) )
278 126 ▶ unless ( _isbig( $self->{MaxBytes} ) ? ( $self->{MaxBytes} > 0 ) : _POSINT( $self->{MaxBytes} ) ) && $self->{MaxBytes} >= $MESSAGE_SIZE_OVERHEAD && $self->{MaxBytes} <= $MAX_INT32;
Branch: 2 branches
BranchTrueFalseTotal
unless _isbig($self->{"MaxBytes"}) ? $self->{"MaxBytes"} > 0 : &_POSINT($self->{"MaxBytes"}) and $self->{"MaxBytes"} >= $Kafka::Consumer::MESSAGE_SIZE_OVERHEAD and $self->{"MaxBytes"} <= $Kafka::Consumer::MAX_INT32 1 110 111
_isbig($self->{'MaxBytes'}) ? : 0 126 126
Condition83%5/6
_isbig($self->{"MaxBytes"}) ? $self->{"MaxBytes"} > 0 : &_POSINT($self->{"MaxBytes"}) and $self->{"MaxBytes"} >= $Kafka::Consumer::MESSAGE_SIZE_OVERHEAD and $self->{"MaxBytes"} <= $Kafka::Consumer::MAX_INT32
!ll&&!rl&&r
160110
_isbig($self->{"MaxBytes"}) ? $self->{"MaxBytes"} > 0 : &_POSINT($self->{"MaxBytes"}) and $self->{"MaxBytes"} >= $Kafka::Consumer::MESSAGE_SIZE_OVERHEAD
!ll&&!rl&&r
151110
MC/DC66%2/3
_isbig($self->{"MaxBytes"}) ? $self->{"MaxBytes"} > 0 : &_POSINT($self->{"MaxBytes"}) and $self->{"MaxBytes"} >= $Kafka::Consumer::MESSAGE_SIZE_OVERHEAD and $self->{"MaxBytes"} <= $Kafka::Consumer::MAX_INT32
_isbig($self->{"MaxBytes"}) ? $self->{"MaxBytes"} > 0 : &_POSINT($self->{"MaxBytes"}) $self->{"MaxBytes"} >= $Kafka::Consumer::MESSAGE_SIZE_OVERHEAD $self->{"MaxBytes"} <= $Kafka::Consumer::MAX_INT32
Truth table75%3/4
A and B and C
A= _isbig($self->{"MaxBytes"}) ? $self->{"MaxBytes"} > 0 : &_POSINT($self->{"MaxBytes"})B= $self->{"MaxBytes"} >= $Kafka::Consumer::MESSAGE_SIZE_OVERHEADC= $self->{"MaxBytes"} <= $Kafka::Consumer::MAX_INT32
ABCresult
0XX0
10X0
1100
1111
279 $self->_error( $ERROR_MISMATCH_ARGUMENT, format_message( 'MaxNumberOfOffsets (%s)', $self->{MaxNumberOfOffsets} ) )
280 110 ▶ unless defined( _POSINT( $self->{MaxNumberOfOffsets} ) ) && $self->{MaxNumberOfOffsets} <= $MAX_INT32;
Branch: 1 branch
BranchTrueFalseTotal
unless defined &_POSINT($self->{"MaxNumberOfOffsets"}) and $self->{"MaxNumberOfOffsets"} <= $Kafka::Consumer::MAX_INT32 15 95 110
Condition66%2/3
defined &_POSINT($self->{"MaxNumberOfOffsets"}) and $self->{"MaxNumberOfOffsets"} <= $Kafka::Consumer::MAX_INT32
!ll&&!rl&&r
15095
MC/DC50%1/2
defined &_POSINT($self->{"MaxNumberOfOffsets"}) and $self->{"MaxNumberOfOffsets"} <= $Kafka::Consumer::MAX_INT32
defined &_POSINT($self->{"MaxNumberOfOffsets"}) $self->{"MaxNumberOfOffsets"} <= $Kafka::Consumer::MAX_INT32
Truth table66%2/3
A and B
A= defined &_POSINT($self->{"MaxNumberOfOffsets"})B= $self->{"MaxNumberOfOffsets"} <= $Kafka::Consumer::MAX_INT32
ABresult
0X0
100
111
281  
282 95 return $self;
283 }
284  
285 #-- public attributes ----------------------------------------------------------
286  
287 =head2 METHODS
288  
289 The following methods are defined for the C<Kafka::Consumer> class:
290  
291 =cut
292  
293 #-- public methods -------------------------------------------------------------
294  
295 =head3 C<fetch( $topic, $partition, $start_offset, $max_size )>
296  
297 Get a list of messages to consume one by one up to C<$max_size> bytes.
298  
299 Returns the reference to array of the L<Kafka::Message|Kafka::Message> objects.
300  
301 C<fetch()> takes the following arguments:
302  
303 =over 3
304  
305 =item C<$topic>
306  
307 The C<$topic> must be a normal non-false string of non-zero length.
308  
309 =item C<$partition>
310  
311 The C<$partition> must be a non-negative integer.
312  
313 =item C<$start_offset>
314  
315 Offset in topic and partition to start from (64-bit integer).
316  
317 The argument must be a non-negative integer. The argument may be a
318 L<Math::BigInt|Math::BigInt> integer on 32-bit system.
319  
320 =item C<$max_size>
321  
322 C<$max_size> is the maximum size of the messages set to return. The argument
323 must be a positive int32 signed integer.
324  
325 The maximum size of a request limited by C<MAX_SOCKET_REQUEST_BYTES> that
326 can be imported from L<Kafka|Kafka> module.
327  
328 =back
329  
330 =cut
331 sub fetch {
332 5055 ▶ my ( $self, $topic, $partition, $start_offset, $max_size, $_return_all, $api_version ) = @_;
Subroutine fetch: called CC=32 SCAR=40.7
Pod documented
333 # Special argument: $_return_all - return redundant messages sent out of a compressed package posts
334  
335 5055 ▶ $self->_error( $ERROR_MISMATCH_ARGUMENT, 'topic' )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $topic and $topic eq "" || defined &_STRING($topic) and not utf8::is_utf8($topic) 8 5047 5055
Condition77%7/9
defined $topic and $topic eq "" || defined &_STRING($topic) and not utf8::is_utf8($topic)
!ll&&!rl&&r
715047
defined $topic and $topic eq "" || defined &_STRING($topic)
!ll&&!rl&&r
075048
$topic eq "" or defined &_STRING($topic)
l!l&&r!l&&!r
050487
MC/DC50%2/4
defined $topic and $topic eq "" || defined &_STRING($topic) and not utf8::is_utf8($topic)
defined $topic $topic eq "" defined &_STRING($topic) not utf8::is_utf8($topic)
Truth table50%3/6
A and B or C and not(D)
A= defined $topicB= $topic eq ""C= defined &_STRING($topic)D= not utf8::is_utf8($topic)
ABCDresult
0XXX0
100X0
11X00
10100
11X11
10111
336 unless defined( $topic ) && ( $topic eq q{} || defined( _STRING( $topic ) ) ) && !utf8::is_utf8( $topic );
337 5047 ▶ $self->_error( $ERROR_MISMATCH_ARGUMENT, 'partition' )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $partition and &isint($partition) and $partition >= 0 10 5037 5047
Condition66%4/6
defined $partition and &isint($partition) and $partition >= 0
!ll&&!rl&&r
1005037
defined $partition and &isint($partition)
!ll&&!rl&&r
0105037
MC/DC33%1/3
defined $partition and &isint($partition) and $partition >= 0
defined $partition &isint($partition) $partition >= 0
Truth table50%2/4
A and B and C
A= defined $partitionB= &isint($partition)C= $partition >= 0
ABCresult
0XX0
10X0
1100
1111
338 unless defined( $partition ) && isint( $partition ) && $partition >= 0;
339 5037 ▶ $self->_error( $ERROR_MISMATCH_ARGUMENT, 'offset' )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $start_offset and _isbig($start_offset) && $start_offset >= 0 || defined &_NONNEGINT($start_offset) 13 5024 5037
Condition66%6/9
defined $start_offset and _isbig($start_offset) && $start_offset >= 0 || defined &_NONNEGINT($start_offset)
!ll&&!rl&&r
1125024
_isbig($start_offset) and $start_offset >= 0 or defined &_NONNEGINT($start_offset)
l!l&&r!l&&!r
0502412
_isbig($start_offset) and $start_offset >= 0
!ll&&!rl&&r
503600
MC/DC50%2/4
defined $start_offset and _isbig($start_offset) && $start_offset >= 0 || defined &_NONNEGINT($start_offset)
defined $start_offset _isbig($start_offset) $start_offset >= 0 defined &_NONNEGINT($start_offset)
Truth table50%3/6
A and B and C or D
A= defined $start_offsetB= _isbig($start_offset)C= $start_offset >= 0D= defined &_NONNEGINT($start_offset)
ABCDresult
0XXX0
10X00
11000
111X1
10X11
11011
340 unless defined( $start_offset ) && ( ( _isbig( $start_offset ) && $start_offset >= 0 ) || defined( _NONNEGINT( $start_offset ) ) );
341 5024 ▶ $self->_error( $ERROR_MISMATCH_ARGUMENT, format_message( 'max_size (%s)', $max_size ) )
Branch: 1 branch
BranchTrueFalseTotal
unless not defined $max_size or _isbig($max_size) || &_POSINT($max_size) and $max_size >= $Kafka::Consumer::MESSAGE_SIZE_OVERHEAD and $max_size <= $Kafka::Consumer::MAX_INT32 1 5011 5012
Condition75%9/12
not defined $max_size or _isbig($max_size) || &_POSINT($max_size) and $max_size >= $Kafka::Consumer::MESSAGE_SIZE_OVERHEAD and $max_size <= $Kafka::Consumer::MAX_INT32
l!l&&r!l&&!r
250220
_isbig($max_size) || &_POSINT($max_size) and $max_size >= $Kafka::Consumer::MESSAGE_SIZE_OVERHEAD and $max_size <= $Kafka::Consumer::MAX_INT32
!ll&&!rl&&r
1305009
_isbig($max_size) || &_POSINT($max_size) and $max_size >= $Kafka::Consumer::MESSAGE_SIZE_OVERHEAD
!ll&&!rl&&r
1215009
_isbig($max_size) or &_POSINT($max_size)
l!l&&r!l&&!r
0501012
MC/DC0%0/5
not defined $max_size or _isbig($max_size) || &_POSINT($max_size) and $max_size >= $Kafka::Consumer::MESSAGE_SIZE_OVERHEAD and $max_size <= $Kafka::Consumer::MAX_INT32
not defined $max_size _isbig($max_size) &_POSINT($max_size) $max_size >= $Kafka::Consumer::MESSAGE_SIZE_OVERHEAD $max_size <= $Kafka::Consumer::MAX_INT32
Truth table25%2/8
not(A) or B or C and D and E
A= not defined $max_sizeB= _isbig($max_size)C= &_POSINT($max_size)D= $max_size >= $Kafka::Consumer::MESSAGE_SIZE_OVERHEADE= $max_size <= $Kafka::Consumer::MAX_INT32
ABCDEresult
1XXXX1
01X111
001111
000XX0
01X0X0
0010X0
01X100
001100
342 unless ( !defined( $max_size ) || ( ( _isbig( $max_size ) || _POSINT( $max_size ) ) && $max_size >= $MESSAGE_SIZE_OVERHEAD && $max_size <= $MAX_INT32 ) );
343  
344 my $request = {
345 ApiKey => $APIKEY_FETCH,
346 ApiVersion => $api_version // $self->{ApiVersion},
347 CorrelationId => _get_CorrelationId(),
348 ClientId => $self->{ClientId},
349 MaxWaitTime => int( $self->{MaxWaitTime} * 1000 ),
350 MinBytes => $self->{MinBytes},
351 MaxBytes => $max_size // $self->{MaxBytes},
352 topics => [
353 {
354 TopicName => $topic,
355 partitions => [
356 {
357 Partition => $partition,
358 FetchOffset => $start_offset,
359 MaxBytes => $max_size // $self->{MaxBytes},
360 },
361 5011 ▶ ],
Condition55%5/9
$api_version // $self->{"ApiVersion"}
l!l&&r!l&&!r
005011
$max_size // $self->{"MaxBytes"}
l!l&&r!l&&!r
500920
$max_size // $self->{"MaxBytes"}
l!l&&r!l&&!r
500920
MC/DC0%0/2
$api_version // $self->{"ApiVersion"}
$api_version $self->{"ApiVersion"}
MC/DC0%0/2
$max_size // $self->{"MaxBytes"}
$max_size $self->{"MaxBytes"}
MC/DC0%0/2
$max_size // $self->{"MaxBytes"}
$max_size $self->{"MaxBytes"}
Truth table33%1/3
A // B
A= $api_versionB= $self->{"ApiVersion"}
ABresult
1X1
011
000
Truth table66%2/3
A // B
A= $max_sizeB= $self->{"MaxBytes"}
ABresult
1X1
011
000
Truth table66%2/3
A // B
A= $max_sizeB= $self->{"MaxBytes"}
ABresult
1X1
011
000
362 },
363 ],
364 };
365  
366 5011 my $response = $self->{Connection}->receive_response_to_request( $request, undef, $self->{MaxWaitTime} );
367  
368 5009 my $messages = [];
369 5009 foreach my $received_topic ( @{ $response->{topics} } ) {
370 $received_topic->{TopicName} eq $topic
371 5009 ▶ or $self->_error( $ERROR_TOPIC_DOES_NOT_MATCH, format_message( "'%s' ne '%s'", $topic, $received_topic->{TopicName} ) );
Branch: 1 branch
BranchTrueFalseTotal
$received_topic->{"TopicName"} eq $topic or $self->_error($Kafka::Consumer::ERROR_TOPIC_DOES_NOT_MATCH, format_message("'%s' ne '%s'", $topic, $received_topic->{"TopicName"})) 0 5009 5009
372 5009 foreach my $received_partition ( @{ $received_topic->{partitions} } ) {
373 $received_partition->{Partition} == $partition
374 5009 ▶ or $self->_error( $ERROR_PARTITION_DOES_NOT_MATCH, format_message( '%s != %s', $partition, $received_partition->{Partition} ) );
Branch: 1 branch
BranchTrueFalseTotal
$received_partition->{"Partition"} == $partition or $self->_error($Kafka::Consumer::ERROR_PARTITION_DOES_NOT_MATCH, format_message("%s != %s", $partition, $received_partition->{"Partition"})) 0 5009 5009
375 5009 my $HighwaterMarkOffset = $received_partition->{HighwaterMarkOffset};
376 5009 foreach my $Message ( @{ $received_partition->{MessageSet} } ) {
377 15018 my $offset = $Message->{Offset};
378 15018 my $next_offset;
379 15018 ▶ if ( $BITS64 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Kafka::Consumer::BITS64) { } 15018 0 15018
380 15018 $next_offset += $offset + 1;
381 } else {
382 0 $offset = Kafka::Int64::intsum( $offset, 0 );
383 0 $next_offset = Kafka::Int64::intsum( $offset, 1 );
384 }
385  
386 # skip previous messages of a compressed package posts
387 15018 ▶ next if $offset < $start_offset && !$_return_all;
Branch: 1 branch
BranchTrueFalseTotal
if $offset < $start_offset and not $_return_all 0 15018 15018
Condition33%1/3
$offset < $start_offset and not $_return_all
!ll&&!rl&&r
1501800
MC/DC0%0/2
$offset < $start_offset and not $_return_all
$offset < $start_offset not $_return_all
Truth table33%1/3
A and not(B)
A= $offset < $start_offsetB= not $_return_all
ABresult
0X0
100
111
388  
389 15018 my $message_error = q{};
390 # According to Apache Kafka documentation:
391 # This byte holds metadata attributes about the message. The
392 # lowest 3 bits contain the compression codec used for the
393 # message. The fourth lowest bit represents the timestamp type.
394 # 0 stands for CreateTime and 1 stands for LogAppendTime. The
395 # producer should always set this bit to 0. (since 0.10.0).
396 # All other bits should be set to 0.
397 15018 my $attributes = $Message->{Attributes};
398 # check that attributes is valid
399 $attributes & 0b11110000
400 15018 ▶ and $message_error = $ERROR{ $ERROR_METADATA_ATTRIBUTES };
Branch: 1 branch
BranchTrueFalseTotal
$attributes & 240 and $message_error = $Kafka::Consumer::ERROR{$Kafka::Consumer::ERROR_METADATA_ATTRIBUTES} 0 15018 15018
401  
402 15018 ▶ if (my $compression_codec = $attributes & 0b00000111) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $compression_codec = $attributes & 7) 0 15018 15018
403 0 ▶ unless ( $compression_codec == 1 # GZIP
Branch: 1 branch
BranchTrueFalseTotal
unless ($compression_codec == 1 or $compression_codec == 2) 0 0 0
Condition0%0/3
$compression_codec == 1 or $compression_codec == 2
l!l&&r!l&&!r
000
MC/DC0%0/2
$compression_codec == 1 or $compression_codec == 2
$compression_codec == 1 $compression_codec == 2
Truth table0%0/3
A or B
A= $compression_codec == 1B= $compression_codec == 2
ABresult
1X1
011
000
404 || $compression_codec == 2 # Snappy
405 ) {
406 0 $message_error = $ERROR{ $ERROR_METADATA_ATTRIBUTES };
407 }
408 }
409  
410 push( @$messages, Kafka::Message->new( {
411 Attributes => $Message->{Attributes},
412 Timestamp => $Message->{Timestamp},
413 MagicByte => $Message->{MagicByte},
414 key => $Message->{Key},
415 payload => $Message->{Value},
416 15018 offset => $offset,
417 next_offset => $next_offset,
418 error => $message_error,
419 valid => !$message_error,
420 HighwaterMarkOffset => $HighwaterMarkOffset,
421 } )
422 );
423 }
424 }
425 }
426  
427 5009 return $messages;
428 }
429  
430 =head3 C<offset_at_time( $topic, $partition, $time )>
431  
432 Returns an offset, given a topic, partition and time.
433  
434 The returned offset is the earliest offset whose timestamp is greater than or
435 equal to the given timestamp. The return value is a HashRef, containing
436 C<timestamp> and C<offset> keys.
437  
438 B<WARNING>: this method requires Kafka 0.10.0, and messages with timestamps.
439 Check the configuration of the brokers or topic, specifically
440 C<message.timestamp.type>, and set it either to C<LogAppentTime> to have Kafka
441 automatically set messages timestamps based on the broker clock, or
442 C<CreateTime>, in which case the client populating your topic has to set the
443 timestamps when producing messages.
444  
445 C<offset_at_time()> takes the following arguments:
446  
447 =over 3
448  
449 =item C<$topic>
450  
451 The C<$topics> must be a normal non-false strings of non-zero length.
452  
453 =item C<$partition>
454  
455 The C<$partitions> must be a non-negative integers.
456  
457 =item C<$time>
458  
459 Get offsets before the given time (in milliseconds since UNIX Epoch).
460  
461 The argument must be a positive number.
462  
463 The argument may be a L<Math::BigInt|Math::BigInt> integer on 32 bit
464 system.
465  
466 =back
467  
468 =cut
469  
470 sub offset_at_time {
471 0 ▶ my ( $self, $topic, $partition, $time ) = @_;
Subroutine offset_at_time: not called CC=5 SCAR=34.0
Pod documented
472  
473 # we don't accept special values for $time, we want a real timestamp
474 0 ▶ $self->_error( $ERROR_MISMATCH_ARGUMENT, 'time' )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $time and _isbig($time) || &isint($time) and $time >= 0 0 0 0
Condition0%0/9
defined $time and _isbig($time) || &isint($time) and $time >= 0
!ll&&!rl&&r
000
defined $time and _isbig($time) || &isint($time)
!ll&&!rl&&r
000
_isbig($time) or &isint($time)
l!l&&r!l&&!r
000
MC/DC0%0/4
defined $time and _isbig($time) || &isint($time) and $time >= 0
defined $time _isbig($time) &isint($time) $time >= 0
Truth table0%0/6
A and B or C and D
A= defined $timeB= _isbig($time)C= &isint($time)D= $time >= 0
ABCDresult
0XXX0
100X0
11X00
10100
11X11
10111
475 unless defined( $time ) && ( _isbig( $time ) || isint( $time ) ) && $time >= 0;
476  
477 # no max_number, api version = 1
478 0 return $self->_query_offsets($topic, $partition, $time, undef, 1)->[0];
479 }
480  
481 =head3 C<offset_before_time( $topic, $partition, $time )>
482  
483 Returns an offset, given a topic, partition and time.
484  
485 The returned offset is an offset whose timestamp is guaranteed to be earlier
486 than the given timestamp. The return value is a Number
487  
488 This method works with all version of Kafka, and doesn't require messages with
489 timestamps.
490  
491 C<offset_before_time()> takes the following arguments:
492  
493 =over 3
494  
495 =item C<$topic>
496  
497 The C<$topics> must be a normal non-false strings of non-zero length.
498  
499 =item C<$partition>
500  
501 The C<$partitions> must be a non-negative integers.
502  
503 =item C<$time>
504  
505 Get offsets before the given time (in milliseconds since UNIX Epoch).
506  
507 The argument must be a positive number.
508  
509 The argument may be a L<Math::BigInt|Math::BigInt> integer on 32 bit
510 system.
511  
512 =back
513  
514 =cut
515  
516 sub offset_before_time {
517 0 ▶ my ( $self, $topic, $partition, $time ) = @_;
Subroutine offset_before_time: not called CC=5 SCAR=34.0
Pod documented
518  
519 # we don't accept special values for $time, we want a real timestamp
520 0 ▶ $self->_error( $ERROR_MISMATCH_ARGUMENT, 'time' )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $time and _isbig($time) || &isint($time) and $time >= 0 0 0 0
Condition0%0/9
defined $time and _isbig($time) || &isint($time) and $time >= 0
!ll&&!rl&&r
000
defined $time and _isbig($time) || &isint($time)
!ll&&!rl&&r
000
_isbig($time) or &isint($time)
l!l&&r!l&&!r
000
MC/DC0%0/4
defined $time and _isbig($time) || &isint($time) and $time >= 0
defined $time _isbig($time) &isint($time) $time >= 0
Truth table0%0/6
A and B or C and D
A= defined $timeB= _isbig($time)C= &isint($time)D= $time >= 0
ABCDresult
0XXX0
100X0
11X00
10100
11X11
10111
521 unless defined( $time ) && ( _isbig( $time ) || isint( $time ) ) && $time >= 0;
522 # $self->_error( $ERROR_MISMATCH_ARGUMENT, format_message( 'max_number (%s)', $max_number ) )
523 # unless !defined( $max_number ) || ( _POSINT( $max_number ) && $max_number <= $MAX_INT32 );
524  
525 # max_number = 1, api version = 0
526 0 return $self->_query_offsets($topic, $partition, $time, 1, 0)->[0];
527 }
528  
529 =head3 C<offset_earliest( $topic, $partition )>
530  
531 Returns the earliest offset for a given topic and partition
532  
533 C<offset_earliest()> takes the following arguments:
534  
535 =over 3
536  
537 =item C<$topic>
538  
539 The C<$topics> must be a normal non-false strings of non-zero length.
540  
541 =item C<$partition>
542  
543 The C<$partitions> must be a non-negative integers.
544  
545 =back
546  
547 =cut
548  
549 sub offset_earliest {
550 0 ▶ my ( $self, $topic, $partition ) = @_;
Subroutine offset_earliest: not called CC=1 SCAR=6.9
Pod documented
551  
552 # max_number = 1, api version = 0
553 0 return $self->_query_offsets($topic, $partition, $RECEIVE_EARLIEST_OFFSET, 1, 0)->[0];
554 }
555  
556 =head3 C<offset_latest( $topic, $partition )>
557  
558 Returns the latest offset for a given topic and partition
559  
560 C<offset_latest()> takes the following arguments:
561  
562 =over 3
563  
564 =item C<$topic>
565  
566 The C<$topics> must be a normal non-false strings of non-zero length.
567  
568 =item C<$partition>
569  
570 The C<$partitions> must be a non-negative integers.
571  
572 =back
573  
574 =cut
575  
576 sub offset_latest {
577 0 ▶ my ( $self, $topic, $partition ) = @_;
Subroutine offset_latest: not called CC=1 SCAR=6.9
Pod documented
578  
579 # max_number = 1, api version = 0
580 0 return $self->_query_offsets($topic, $partition, $RECEIVE_LATEST_OFFSETS, 1, 0)->[0];
581 }
582  
583 =head3 C<offsets( $topic, $partition, $time, $max_number )>
584  
585 B<WARNING>: This method is DEPRECATED, please use one of C<offset_at_time()>, C<offset_before_time()>, C<offset_earliest()>, C<offset_latest()>. It is kept for backward compatibility.
586  
587 Returns an ArrayRef of offsets
588  
589 C<offsets()> takes the following arguments:
590  
591 =over 3
592  
593 =item C<$topic>
594  
595 The C<$topics> must be a normal non-false strings of non-zero length.
596  
597 =item C<$partition>
598  
599 The C<$partitions> must be a non-negative integers.
600  
601 =item C<$time>
602  
603 Get offsets before the given time (in milliseconds since UNIX Epoch). It must
604 be a positive number. It may be a L<Math::BigInt|Math::BigInt> integer on 32
605 bit system.
606  
607 The special values C<$RECEIVE_LATEST_OFFSETS> (-1), C<$RECEIVE_EARLIEST_OFFSET>
608 (-2) are allowed. They can be imported from the L<Kafka|Kafka> module.
609  
610 =item C<$max_number>
611  
612 Maximum number of offsets to be returned
613  
614 =back
615  
616 =cut
617  
618 sub offsets {
619 63 ▶ my ( $self, $topic, $partition, $time, $max_number ) = @_;
Subroutine offsets: called CC=8 SCAR=21.2
Pod documented
620  
621 63 ▶ $self->_error( $ERROR_MISMATCH_ARGUMENT, 'time' )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $time and _isbig($time) || &isint($time) and $time >= $Kafka::Consumer::RECEIVE_EARLIEST_OFFSET 11 52 63
Condition77%7/9
defined $time and _isbig($time) || &isint($time) and $time >= $Kafka::Consumer::RECEIVE_EARLIEST_OFFSET
!ll&&!rl&&r
10152
defined $time and _isbig($time) || &isint($time)
!ll&&!rl&&r
01053
_isbig($time) or &isint($time)
l!l&&r!l&&!r
05310
MC/DC50%2/4
defined $time and _isbig($time) || &isint($time) and $time >= $Kafka::Consumer::RECEIVE_EARLIEST_OFFSET
defined $time _isbig($time) &isint($time) $time >= $Kafka::Consumer::RECEIVE_EARLIEST_OFFSET
Truth table50%3/6
A and B or C and D
A= defined $timeB= _isbig($time)C= &isint($time)D= $time >= $Kafka::Consumer::RECEIVE_EARLIEST_OFFSET
ABCDresult
0XXX0
100X0
11X00
10100
11X11
10111
622 unless defined( $time ) && ( _isbig( $time ) || isint( $time ) ) && $time >= $RECEIVE_EARLIEST_OFFSET;
623 52 ▶ $self->_error( $ERROR_MISMATCH_ARGUMENT, format_message( 'max_number (%s)', $max_number ) )
Branch: 1 branch
BranchTrueFalseTotal
unless not defined $max_number or &_POSINT($max_number) and $max_number <= $Kafka::Consumer::MAX_INT32 14 38 52
Condition66%4/6
not defined $max_number or &_POSINT($max_number) and $max_number <= $Kafka::Consumer::MAX_INT32
l!l&&r!l&&!r
2500
&_POSINT($max_number) and $max_number <= $Kafka::Consumer::MAX_INT32
!ll&&!rl&&r
14036
MC/DC0%0/3
not defined $max_number or &_POSINT($max_number) and $max_number <= $Kafka::Consumer::MAX_INT32
not defined $max_number &_POSINT($max_number) $max_number <= $Kafka::Consumer::MAX_INT32
Truth table50%2/4
not(A) or B and C
A= not defined $max_numberB= &_POSINT($max_number)C= $max_number <= $Kafka::Consumer::MAX_INT32
ABCresult
1XX1
0111
00X0
0100
624 unless !defined( $max_number ) || ( _POSINT( $max_number ) && $max_number <= $MAX_INT32 );
625  
626 38 return $self->_query_offsets($topic, $partition, $time, $max_number, 0);
627 }
628  
629 sub _query_offsets {
630 38 ▶ my ( $self, $topic, $partition, $time, $max_number, $api_version ) = @_;
Subroutine _query_offsets: called CC=14 SCAR=28.4
631  
632 38 ▶ $self->_error( $ERROR_MISMATCH_ARGUMENT, 'topic' )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $topic and $topic eq "" || defined &_STRING($topic) and not utf8::is_utf8($topic) 8 30 38
Condition77%7/9
defined $topic and $topic eq "" || defined &_STRING($topic) and not utf8::is_utf8($topic)
!ll&&!rl&&r
7130
defined $topic and $topic eq "" || defined &_STRING($topic)
!ll&&!rl&&r
0731
$topic eq "" or defined &_STRING($topic)
l!l&&r!l&&!r
0317
MC/DC50%2/4
defined $topic and $topic eq "" || defined &_STRING($topic) and not utf8::is_utf8($topic)
defined $topic $topic eq "" defined &_STRING($topic) not utf8::is_utf8($topic)
Truth table50%3/6
A and B or C and not(D)
A= defined $topicB= $topic eq ""C= defined &_STRING($topic)D= not utf8::is_utf8($topic)
ABCDresult
0XXX0
100X0
11X00
10100
11X11
10111
633 unless defined( $topic) && ( $topic eq q{} || defined( _STRING( $topic ) ) ) && !utf8::is_utf8( $topic );
634 30 ▶ $self->_error( $ERROR_MISMATCH_ARGUMENT, 'partition' )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $partition and &isint($partition) and $partition >= 0 10 20 30
Condition66%4/6
defined $partition and &isint($partition) and $partition >= 0
!ll&&!rl&&r
10020
defined $partition and &isint($partition)
!ll&&!rl&&r
01020
MC/DC33%1/3
defined $partition and &isint($partition) and $partition >= 0
defined $partition &isint($partition) $partition >= 0
Truth table50%2/4
A and B and C
A= defined $partitionB= &isint($partition)C= $partition >= 0
ABCresult
0XX0
10X0
1100
1111
635 unless defined( $partition ) && isint( $partition ) && $partition >= 0;
636  
637 20 my $is_v1 = $api_version == 1;
638  
639 my $request = {
640 ApiKey => $APIKEY_OFFSET,
641 ApiVersion => $api_version,
642 CorrelationId => _get_CorrelationId(),
643 ClientId => $self->{ClientId},
644 topics => [
645 {
646 TopicName => $topic,
647 partitions => [
648 {
649 Partition => $partition,
650 Time => $time,
651 MaxNumberOfOffsets => $max_number // $self->{MaxNumberOfOffsets},
652 },
653 20 ▶ ],
Condition66%2/3
$max_number // $self->{"MaxNumberOfOffsets"}
l!l&&r!l&&!r
1820
MC/DC0%0/2
$max_number // $self->{"MaxNumberOfOffsets"}
$max_number $self->{"MaxNumberOfOffsets"}
Truth table66%2/3
A // B
A= $max_numberB= $self->{"MaxNumberOfOffsets"}
ABresult
1X1
011
000
654 },
655 ],
656 };
657  
658 20 my $response = $self->{Connection}->receive_response_to_request( $request );
659  
660 18 my $offsets = [];
661 # because we accepted only one topic and partition, we are sure that the
662 # response is all about this single topic and partition, so we can merge
663 # the offsets.
664 18 ▶ if ($is_v1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_v1) { } 0 18 18
665 0 foreach my $received_topic ( @{ $response->{topics} } ) {
666 0 foreach my $partition_offsets ( @{ $received_topic->{PartitionOffsets} } ) {
667 push @$offsets, { timestamp => $partition_offsets->{Timestamp},
668 0 offset => $partition_offsets->{Offset} };
669 }
670 }
671 } else {
672 18 foreach my $received_topic ( @{ $response->{topics} } ) {
673 18 foreach my $partition_offsets ( @{ $received_topic->{PartitionOffsets} } ) {
674 18 push @$offsets, @{ $partition_offsets->{Offset} };
675 }
676 }
677 }
678  
679 18 return $offsets;
680 }
681  
682 =head3 C<commit_offsets( $topic, $partition, $offset, $group )>
683  
684 Commit offsets using the offset commit/fetch API.
685  
686 Returns a non-blank value (a reference to a hash with server response description)
687 if the message is successfully sent.
688  
689 C<commit_offsets()> takes the following arguments:
690  
691 =over 3
692  
693 =item C<$topic>
694  
695 The C<$topic> must be a normal non-false string of non-zero length.
696  
697 =item C<$partition>
698  
699 The C<$partition> must be a non-negative integer.
700  
701 =item C<$offset>
702  
703 Offset in topic and partition to commit.
704  
705 The argument must be a positive number.
706  
707 The argument may be a L<Math::BigInt|Math::BigInt> integer on 32 bit
708 system.
709  
710 =item C<$group>
711  
712 The name of the consumer group
713  
714 The argument must be a normal non-false string of non-zero length.
715  
716 =back
717  
718 =cut
719 sub commit_offsets {
720 0 ▶ my ( $self, $topic, $partition, $offset, $group ) = @_;
Subroutine commit_offsets: not called CC=16 SCAR=56.1
Pod documented
721  
722  
723 0 ▶ $self->_error( $ERROR_MISMATCH_ARGUMENT, 'topic' )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $topic and $topic eq "" || defined &_STRING($topic) and not utf8::is_utf8($topic) 0 0 0
Condition0%0/9
defined $topic and $topic eq "" || defined &_STRING($topic) and not utf8::is_utf8($topic)
!ll&&!rl&&r
000
defined $topic and $topic eq "" || defined &_STRING($topic)
!ll&&!rl&&r
000
$topic eq "" or defined &_STRING($topic)
l!l&&r!l&&!r
000
MC/DC0%0/4
defined $topic and $topic eq "" || defined &_STRING($topic) and not utf8::is_utf8($topic)
defined $topic $topic eq "" defined &_STRING($topic) not utf8::is_utf8($topic)
Truth table0%0/6
A and B or C and not(D)
A= defined $topicB= $topic eq ""C= defined &_STRING($topic)D= not utf8::is_utf8($topic)
ABCDresult
0XXX0
100X0
11X00
10100
11X11
10111
724 unless defined( $topic ) && ( $topic eq q{} || defined( _STRING( $topic ) ) ) && !utf8::is_utf8( $topic );
725 0 ▶ $self->_error( $ERROR_MISMATCH_ARGUMENT, 'partition' )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $partition and &isint($partition) and $partition >= 0 0 0 0
Condition0%0/6
defined $partition and &isint($partition) and $partition >= 0
!ll&&!rl&&r
000
defined $partition and &isint($partition)
!ll&&!rl&&r
000
MC/DC0%0/3
defined $partition and &isint($partition) and $partition >= 0
defined $partition &isint($partition) $partition >= 0
Truth table0%0/4
A and B and C
A= defined $partitionB= &isint($partition)C= $partition >= 0
ABCresult
0XX0
10X0
1100
1111
726 unless defined( $partition ) && isint( $partition ) && $partition >= 0;
727 0 ▶ $self->_error( $ERROR_MISMATCH_ARGUMENT, 'offset' )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $offset and _isbig($offset) && $offset >= 0 || defined &_NONNEGINT($offset) 0 0 0
Condition0%0/9
defined $offset and _isbig($offset) && $offset >= 0 || defined &_NONNEGINT($offset)
!ll&&!rl&&r
000
_isbig($offset) and $offset >= 0 or defined &_NONNEGINT($offset)
l!l&&r!l&&!r
000
_isbig($offset) and $offset >= 0
!ll&&!rl&&r
000
MC/DC0%0/4
defined $offset and _isbig($offset) && $offset >= 0 || defined &_NONNEGINT($offset)
defined $offset _isbig($offset) $offset >= 0 defined &_NONNEGINT($offset)
Truth table0%0/6
A and B and C or D
A= defined $offsetB= _isbig($offset)C= $offset >= 0D= defined &_NONNEGINT($offset)
ABCDresult
0XXX0
10X00
11000
111X1
10X11
11011
728 unless defined( $offset ) && ( ( _isbig( $offset ) && $offset >= 0 ) || defined( _NONNEGINT( $offset ) ) );
729 0 ▶ $self->_error( $ERROR_MISMATCH_ARGUMENT, 'group' )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $group and $group eq "" || defined &_STRING($group) and not utf8::is_utf8($group) 0 0 0
Condition0%0/9
defined $group and $group eq "" || defined &_STRING($group) and not utf8::is_utf8($group)
!ll&&!rl&&r
000
defined $group and $group eq "" || defined &_STRING($group)
!ll&&!rl&&r
000
$group eq "" or defined &_STRING($group)
l!l&&r!l&&!r
000
MC/DC0%0/4
defined $group and $group eq "" || defined &_STRING($group) and not utf8::is_utf8($group)
defined $group $group eq "" defined &_STRING($group) not utf8::is_utf8($group)
Truth table0%0/6
A and B or C and not(D)
A= defined $groupB= $group eq ""C= defined &_STRING($group)D= not utf8::is_utf8($group)
ABCDresult
0XXX0
100X0
11X00
10100
11X11
10111
730 unless defined( $group ) && ( $group eq q{} || defined( _STRING( $group ) ) ) && !utf8::is_utf8( $group );
731  
732 my $request = {
733 __send_to__ => 'group_coordinator',
734 ApiKey => $APIKEY_OFFSETCOMMIT,
735 CorrelationId => _get_CorrelationId(),
736 ClientId => $self->{ClientId},
737 0 GroupId => $group,
738 topics => [
739 {
740 TopicName => $topic,
741 partitions => [
742 {
743 Partition => $partition,
744 Offset => $offset,
745 Metadata => '',
746 },
747 ],
748 },
749 ],
750 };
751  
752 0 return $self->{Connection}->receive_response_to_request( $request );
753 }
754  
755 =head3 C<fetch_offsets( $topic, $partition, $group )>
756  
757 Fetch Committed offsets using the offset commit/fetch API.
758  
759 Returns a non-blank value (a reference to a hash with server response description)
760 if the message is successfully sent.
761  
762 C<fetch_offsets()> takes the following arguments:
763  
764 =over 3
765  
766 =item C<$topic>
767  
768 The C<$topic> must be a normal non-false string of non-zero length.
769  
770 =item C<$partition>
771  
772 The C<$partition> must be a non-negative integer.
773  
774 =item C<$group>
775  
776 The name of the consumer group
777  
778 The argument must be a normal non-false string of non-zero length.
779  
780 =back
781  
782 =cut
783 sub fetch_offsets {
784 0 ▶ my ( $self, $topic, $partition, $group ) = @_;
Subroutine fetch_offsets: not called CC=12 SCAR=50.5
Pod documented
785  
786  
787 0 ▶ $self->_error( $ERROR_MISMATCH_ARGUMENT, 'topic' )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $topic and $topic eq "" || defined &_STRING($topic) and not utf8::is_utf8($topic) 0 0 0
Condition0%0/9
defined $topic and $topic eq "" || defined &_STRING($topic) and not utf8::is_utf8($topic)
!ll&&!rl&&r
000
defined $topic and $topic eq "" || defined &_STRING($topic)
!ll&&!rl&&r
000
$topic eq "" or defined &_STRING($topic)
l!l&&r!l&&!r
000
MC/DC0%0/4
defined $topic and $topic eq "" || defined &_STRING($topic) and not utf8::is_utf8($topic)
defined $topic $topic eq "" defined &_STRING($topic) not utf8::is_utf8($topic)
Truth table0%0/6
A and B or C and not(D)
A= defined $topicB= $topic eq ""C= defined &_STRING($topic)D= not utf8::is_utf8($topic)
ABCDresult
0XXX0
100X0
11X00
10100
11X11
10111
788 unless defined( $topic ) && ( $topic eq q{} || defined( _STRING( $topic ) ) ) && !utf8::is_utf8( $topic );
789 0 ▶ $self->_error( $ERROR_MISMATCH_ARGUMENT, 'partition' )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $partition and &isint($partition) and $partition >= 0 0 0 0
Condition0%0/6
defined $partition and &isint($partition) and $partition >= 0
!ll&&!rl&&r
000
defined $partition and &isint($partition)
!ll&&!rl&&r
000
MC/DC0%0/3
defined $partition and &isint($partition) and $partition >= 0
defined $partition &isint($partition) $partition >= 0
Truth table0%0/4
A and B and C
A= defined $partitionB= &isint($partition)C= $partition >= 0
ABCresult
0XX0
10X0
1100
1111
790 unless defined( $partition ) && isint( $partition ) && $partition >= 0;
791 0 ▶ $self->_error( $ERROR_MISMATCH_ARGUMENT, 'group' )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $group and $group eq "" || defined &_STRING($group) and not utf8::is_utf8($group) 0 0 0
Condition0%0/9
defined $group and $group eq "" || defined &_STRING($group) and not utf8::is_utf8($group)
!ll&&!rl&&r
000
defined $group and $group eq "" || defined &_STRING($group)
!ll&&!rl&&r
000
$group eq "" or defined &_STRING($group)
l!l&&r!l&&!r
000
MC/DC0%0/4
defined $group and $group eq "" || defined &_STRING($group) and not utf8::is_utf8($group)
defined $group $group eq "" defined &_STRING($group) not utf8::is_utf8($group)
Truth table0%0/6
A and B or C and not(D)
A= defined $groupB= $group eq ""C= defined &_STRING($group)D= not utf8::is_utf8($group)
ABCDresult
0XXX0
100X0
11X00
10100
11X11
10111
792 unless defined( $group ) && ( $group eq q{} || defined( _STRING( $group ) ) ) && !utf8::is_utf8( $group );
793  
794 my $request = {
795 __send_to__ => 'group_coordinator',
796 ApiKey => $APIKEY_OFFSETFETCH,
797 CorrelationId => _get_CorrelationId(),
798 ClientId => $self->{ClientId},
799 0 GroupId => $group,
800 topics => [
801 {
802 TopicName => $topic,
803 partitions => [
804 {
805 Partition => $partition,
806 },
807 ],
808 },
809 ],
810 };
811  
812 0 return $self->{Connection}->receive_response_to_request( $request );
813 }
814  
815 #-- private attributes ---------------------------------------------------------
816  
817 #-- private methods ------------------------------------------------------------
818  
819 # Handler for errors
820 sub _error {
821 171 ▶ my $self = shift;
Subroutine _error: called CC=1 SCAR=0.4
822  
823 171 Kafka::Exception::Consumer->throw( throw_args( @_ ) );
824  
825 0 return;
826 }
827  
828  
829  
830 5 1;
831  
832 __END__
« lib/Kafka/IO.pm ↑ Summary blib/lib/Kafka/Consumer.pm »