lib/Kafka/Producer.pm

filter: Statementstmt 84.8% 73 / 86 Branchbran 61.1% 33 / 54 Conditioncond 61.7% 58 / 94 MC/DCmcdc 34.7% 16 / 46 Subroutinesub 100% 14 / 14 Podpod 100% 2 / 2 totaltotal 66.2% 196 / 296 SCAR 51.8
CC
65
Cov
70%
send
239.0
new
16.6
_error
1.0
CRAP 178.1
« lib/Kafka/Protocol.pm ↑ Summary blib/lib/Kafka/Internals.pm »
1 package Kafka::Producer;
2  
3 =head1 NAME
4  
5 Kafka::Producer - Perl interface for Kafka producer client.
6  
7 =head1 VERSION
8  
9 This documentation refers to C<Kafka::Producer> version 1.07 .
10  
11 =cut
12  
13  
14  
15 6 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19  
20  
21 6 our $VERSION = '1.07';
22  
23  
24  
25 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 6 use Params::Util qw(
27 _ARRAY
28 _INSTANCE
29 _NONNEGINT
30 _NUMBER
31 _STRING
32 _POSINT
33 6 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
34 6 use Scalar::Util qw(
35 blessed
36 6 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
37 6 use Scalar::Util::Numeric qw(
38 isint
39 6 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 6 use Kafka qw(
42 %ERROR
43 $COMPRESSION_GZIP
44 $COMPRESSION_NONE
45 $COMPRESSION_SNAPPY
46 $COMPRESSION_LZ4
47 $ERROR_CANNOT_GET_METADATA
48 $ERROR_MISMATCH_ARGUMENT
49 $REQUEST_TIMEOUT
50 $NOT_SEND_ANY_RESPONSE
51 $WAIT_WRITTEN_TO_LOCAL_LOG
52 $BLOCK_UNTIL_IS_COMMITTED
53 6 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
54 6 ▶use Kafka::Connection;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 6 ▶use Kafka::Exceptions;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 6 use Kafka::Internals qw(
57 $APIKEY_PRODUCE
58 $MAX_CORRELATIONID
59 $MAX_INT16
60 $MAX_INT32
61 $PRODUCER_ANY_OFFSET
62 _get_CorrelationId
63 format_message
64 6 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
65  
66  
67  
68 =head1 SYNOPSIS
69  
70 use 5.010;
71 use strict;
72 use warnings;
73  
74 use Scalar::Util qw(
75 blessed
76 );
77 use Try::Tiny;
78  
79 use Kafka::Connection;
80 use Kafka::Producer;
81  
82 my ( $connection, $producer );
83 try {
84  
85 #-- Connection
86 $connection = Kafka::Connection->new( host => 'localhost' );
87  
88 #-- Producer
89 $producer = Kafka::Producer->new( Connection => $connection );
90  
91 # Sending a single message
92 my $response = $producer->send(
93 'mytopic', # topic
94 0, # partition
95 'Single message' # message
96 );
97  
98 # Sending a series of messages
99 $response = $producer->send(
100 'mytopic', # topic
101 0, # partition
102 [ # messages
103 'The first message',
104 'The second message',
105 'The third message',
106 ]
107 );
108  
109 } catch {
110 my $error = $_;
111 if ( blessed( $error ) && $error->isa( 'Kafka::Exception' ) ) {
112 warn 'Error: (', $error->code, ') ', $error->message, "\n";
113 exit;
114 } else {
115 die $error;
116 }
117 };
118  
119 # Closes the producer and cleans up
120 undef $producer;
121 $connection->close;
122 undef $connection;
123  
124 =head1 DESCRIPTION
125  
126 Kafka producer API is implemented by C<Kafka::Producer> class.
127  
128 The main features of the C<Kafka::Producer> class are:
129  
130 =over 3
131  
132 =item *
133  
134 Provides object-oriented API for producing messages.
135  
136 =item *
137  
138 Provides Kafka PRODUCE requests.
139  
140 =back
141  
142 =cut
143  
144 6 my %known_compression_codecs = map { $_ => 1 } (
145 $COMPRESSION_NONE,
146 $COMPRESSION_GZIP,
147 $COMPRESSION_SNAPPY,
148 $COMPRESSION_LZ4,
149 );
150  
151 #-- constructor ----------------------------------------------------------------
152  
153 =head2 CONSTRUCTOR
154  
155 =head3 C<new>
156  
157 Creates new producer client object.
158  
159 C<new()> takes arguments in key-value pairs. The following arguments are currently recognized:
160  
161 =over 3
162  
163 =item C<Connection =E<gt> $connection>
164  
165 C<$connection> is the L<Kafka::Connection|Kafka::Connection> object responsible for communication with
166 the Apache Kafka cluster.
167  
168 =item C<ClientId =E<gt> $client_id>
169  
170 This is a user supplied identifier (string) for the client application.
171  
172 If C<ClientId> is not passed to constructor, its value will be automatically assigned
173 (to string C<'producer'>).
174  
175 =item C<RequiredAcks =E<gt> $acks>
176  
177 The C<$acks> should be an int16 signed integer.
178  
179 Indicates how many acknowledgements the servers should receive before responding to the request.
180  
181 If it is C<$NOT_SEND_ANY_RESPONSE> the server does not send any response.
182  
183 If it is C<$WAIT_WRITTEN_TO_LOCAL_LOG>, (default)
184 the server will wait until the data is written to the local log before sending a response.
185  
186 If it is C<$BLOCK_UNTIL_IS_COMMITTED>
187 the server will block until the message is committed by all in sync replicas before sending a response.
188  
189 C<$NOT_SEND_ANY_RESPONSE>, C<$WAIT_WRITTEN_TO_LOCAL_LOG>, C<$BLOCK_UNTIL_IS_COMMITTED>
190 can be imported from the L<Kafka|Kafka> module.
191  
192 =item C<Timeout =E<gt> $timeout>
193  
194 This provides a maximum time the server can await the receipt
195 of the number of acknowledgements in C<RequiredAcks>.
196  
197 The C<$timeout> in seconds, could be any integer or floating-point type not bigger than int32 positive integer.
198  
199 Optional, default = C<$REQUEST_TIMEOUT>.
200  
201 C<$REQUEST_TIMEOUT> is the default timeout that can be imported from the
202 L<Kafka|Kafka> module.
203  
204 =back
205  
206 =cut
207 sub new {
208 108 ▶ my ( $class, %p ) = @_;
Subroutine new: called CC=16 SCAR=28.1
Pod documented
209  
210 108 my $self = bless {
211 Connection => undef,
212 ClientId => undef,
213 RequiredAcks => $WAIT_WRITTEN_TO_LOCAL_LOG,
214 Timeout => $REQUEST_TIMEOUT,
215 }, $class;
216  
217 108 ▶ exists $p{$_} and $self->{$_} = $p{$_} foreach keys %$self;
Branch: 1 branch
BranchTrueFalseTotal
exists $p{$_} and $self->{$_} = $p{$_} 253 179 432
218  
219 108 ▶ $self->{ClientId} //= 'producer';
Condition100%2/2
$self->{"ClientId"} //= "producer"
l!l
4761
MC/DC100%1/1
$self->{"ClientId"} //= "producer"
$self->{"ClientId"}
Truth table100%2/2
A
A= $self->{"ClientId"}
Aresult
11
00
220  
221 $self->_error( $ERROR_MISMATCH_ARGUMENT, 'Connection' )
222 108 ▶ unless _INSTANCE( $self->{Connection}, 'Kafka::Connection' );
Branch: 1 branch
BranchTrueFalseTotal
unless &_INSTANCE($self->{"Connection"}, "Kafka::Connection") 20 88 108
223 $self->_error( $ERROR_MISMATCH_ARGUMENT, 'ClientId' )
224 88 ▶ unless ( $self->{ClientId} eq '' || 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 80 88
Condition83%5/6
$self->{"ClientId"} eq "" || defined &_STRING($self->{"ClientId"}) and not utf8::is_utf8($self->{"ClientId"})
!ll&&!rl&&r
7180
$self->{"ClientId"} eq "" or defined &_STRING($self->{"ClientId"})
l!l&&r!l&&!r
0817
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
225 $self->_error( $ERROR_MISMATCH_ARGUMENT, format_message( 'Timeout (%s)', $self->{Timeout} ) )
226 80 ▶ unless defined _NUMBER( $self->{Timeout} ) && int( $self->{Timeout} * 1000 ) >= 1 && int( $self->{Timeout} * 1000 ) <= $MAX_INT32;
Branch: 1 branch
BranchTrueFalseTotal
unless defined &_NUMBER($self->{"Timeout"}) and int $self->{"Timeout"} * 1000 >= 1 and int $self->{"Timeout"} * 1000 <= $Kafka::Producer::MAX_INT32 0 70 70
Condition66%4/6
defined &_NUMBER($self->{"Timeout"}) and int $self->{"Timeout"} * 1000 >= 1 and int $self->{"Timeout"} * 1000 <= $Kafka::Producer::MAX_INT32
!ll&&!rl&&r
10070
defined &_NUMBER($self->{"Timeout"}) and int $self->{"Timeout"} * 1000 >= 1
!ll&&!rl&&r
10070
MC/DC33%1/3
defined &_NUMBER($self->{"Timeout"}) and int $self->{"Timeout"} * 1000 >= 1 and int $self->{"Timeout"} * 1000 <= $Kafka::Producer::MAX_INT32
defined &_NUMBER($self->{"Timeout"}) int $self->{"Timeout"} * 1000 >= 1 int $self->{"Timeout"} * 1000 <= $Kafka::Producer::MAX_INT32
Truth table50%2/4
A and B and C
A= defined &_NUMBER($self->{"Timeout"})B= int $self->{"Timeout"} * 1000 >= 1C= int $self->{"Timeout"} * 1000 <= $Kafka::Producer::MAX_INT32
ABCresult
0XX0
10X0
1100
1111
227  
228 70 my $required_acks = $self->{RequiredAcks};
229 70 ▶ $self->_error( $ERROR_MISMATCH_ARGUMENT, 'RequiredAcks' )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $required_acks and &isint($required_acks) and $required_acks == $Kafka::Producer::NOT_SEND_ANY_RESPONSE || $required_acks == $Kafka::Producer::WAIT_WRITTEN_TO_LOCAL_LOG || $required_acks == $Kafka::Producer::BLOCK_UNTIL_IS_COMMITTED 10 60 70
Condition83%10/12
defined $required_acks and &isint($required_acks) and $required_acks == $Kafka::Producer::NOT_SEND_ANY_RESPONSE || $required_acks == $Kafka::Producer::WAIT_WRITTEN_TO_LOCAL_LOG || $required_acks == $Kafka::Producer::BLOCK_UNTIL_IS_COMMITTED
!ll&&!rl&&r
10555
defined $required_acks and &isint($required_acks)
!ll&&!rl&&r
01060
$required_acks == $Kafka::Producer::NOT_SEND_ANY_RESPONSE or $required_acks == $Kafka::Producer::WAIT_WRITTEN_TO_LOCAL_LOG or $required_acks == $Kafka::Producer::BLOCK_UNTIL_IS_COMMITTED
l!l&&r!l&&!r
5910
$required_acks == $Kafka::Producer::NOT_SEND_ANY_RESPONSE or $required_acks == $Kafka::Producer::WAIT_WRITTEN_TO_LOCAL_LOG
l!l&&r!l&&!r
2571
MC/DC20%1/5
defined $required_acks and &isint($required_acks) and $required_acks == $Kafka::Producer::NOT_SEND_ANY_RESPONSE || $required_acks == $Kafka::Producer::WAIT_WRITTEN_TO_LOCAL_LOG || $required_acks == $Kafka::Producer::BLOCK_UNTIL_IS_COMMITTED
defined $required_acks &isint($required_acks) $required_acks == $Kafka::Producer::NOT_SEND_ANY_RESPONSE $required_acks == $Kafka::Producer::WAIT_WRITTEN_TO_LOCAL_LOG $required_acks == $Kafka::Producer::BLOCK_UNTIL_IS_COMMITTED
Truth table50%3/6
A and B and C or D or E
A= defined $required_acksB= &isint($required_acks)C= $required_acks == $Kafka::Producer::NOT_SEND_ANY_RESPONSED= $required_acks == $Kafka::Producer::WAIT_WRITTEN_TO_LOCAL_LOGE= $required_acks == $Kafka::Producer::BLOCK_UNTIL_IS_COMMITTED
ABCDEresult
0XXXX0
10XXX0
110000
111XX1
1101X1
110011
230 unless
231 defined( $required_acks )
232 && isint( $required_acks )
233 && (
234 $required_acks == $NOT_SEND_ANY_RESPONSE
235 || $required_acks == $WAIT_WRITTEN_TO_LOCAL_LOG
236 || $required_acks == $BLOCK_UNTIL_IS_COMMITTED
237 )
238 ;
239  
240 60 return $self;
241 }
242  
243 #-- public attributes ----------------------------------------------------------
244  
245 =head2 METHODS
246  
247 The following methods are defined for the C<Kafka::Producer> class:
248  
249 =cut
250  
251 #-- public methods -------------------------------------------------------------
252  
253 =head3 C<send( $topic, $partition, $messages, $keys, $compression_codec )>
254  
255 Sends a messages on a L<Kafka::Connection|Kafka::Connection> object.
256  
257 Returns a non-blank value (a reference to a hash with server response description)
258 if the message is successfully sent.
259  
260 C<send()> takes the following arguments:
261  
262 =over 3
263  
264 =item C<$topic>
265  
266 The C<$topic> must be a normal non-false string of non-zero length.
267  
268 =item C<$partition>
269  
270 The C<$partition> must be a non-negative integer.
271  
272 =item C<$messages>
273  
274 The C<$messages> is an arbitrary amount of data (a simple data string or
275 a reference to an array of the data strings).
276  
277 =item C<$keys>
278  
279 The C<$keys> are optional message keys, for partitioning with each message,
280 so the consumer knows the partitioning key.
281 This argument should be either a single string (common key for all messages),
282 or an array of strings with length matching messages array.
283  
284 =item C<$compression_codec>
285  
286 Optional.
287  
288 C<$compression_codec> sets the required type of C<$messages> compression,
289 if the compression is desirable.
290  
291 Supported codecs:
292 C<$COMPRESSION_NONE>,
293 C<$COMPRESSION_GZIP>,
294 C<$COMPRESSION_SNAPPY>,
295 C<$COMPRESSION_LZ4>.
296 The defaults that can be imported from the L<Kafka|Kafka> module.
297  
298 =item C<$timestamps>
299  
300 Optional.
301  
302 This is the timestamps of the C<$messages>.
303  
304 This argument should be either a single number (common timestamp for all messages),
305 or an array of integers with length matching messages array.
306  
307 Unit is milliseconds since beginning of the epoch (midnight Jan 1, 1970 (UTC)).
308  
309 B<WARNING>: timestamps supported since Kafka 0.10.0.
310  
311  
312 Do not use C<$Kafka::SEND_MAX_ATTEMPTS> in C<Kafka::Producer-<gt>send> request to prevent duplicates.
313  
314 =back
315  
316 =cut
317 sub send {
318 5070 ▶ my ( $self, $topic, $partition, $messages, $keys, $compression_codec, $timestamps ) = @_;
Subroutine send: called CC=50 SCAR=54.8
Pod documented
319  
320 5070 ▶ $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 5062 5070
Condition77%7/9
defined $topic and $topic eq "" || defined &_STRING($topic) and not utf8::is_utf8($topic)
!ll&&!rl&&r
715062
defined $topic and $topic eq "" || defined &_STRING($topic)
!ll&&!rl&&r
075063
$topic eq "" or defined &_STRING($topic)
l!l&&r!l&&!r
050637
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
321 unless defined( $topic ) && ( $topic eq '' || defined( _STRING( $topic ) ) ) && !utf8::is_utf8( $topic );
322 5062 ▶ $self->_error( $ERROR_MISMATCH_ARGUMENT, 'partition' )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $partition and &isint($partition) and $partition >= 0 10 5052 5062
Condition66%4/6
defined $partition and &isint($partition) and $partition >= 0
!ll&&!rl&&r
1005052
defined $partition and &isint($partition)
!ll&&!rl&&r
0105052
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
323 unless defined( $partition ) && isint( $partition ) && $partition >= 0;
324 5052 ▶ $self->_error( $ERROR_MISMATCH_ARGUMENT, 'messages' )
Branch: 1 branch
BranchTrueFalseTotal
unless defined &_STRING($messages) or &_ARRAY($messages) 5 5047 5052
Condition100%3/3
defined &_STRING($messages) or &_ARRAY($messages)
l!l&&r!l&&!r
2650215
MC/DC100%2/2
defined &_STRING($messages) or &_ARRAY($messages)
defined &_STRING($messages) &_ARRAY($messages)
Truth table100%3/3
A or B
A= defined &_STRING($messages)B= &_ARRAY($messages)
ABresult
1X1
011
000
325 unless defined( _STRING( $messages ) ) || _ARRAY( $messages );
326 5047 ▶ $self->_error( $ERROR_MISMATCH_ARGUMENT, 'keys' )
Branch: 1 branch
BranchTrueFalseTotal
unless not defined $keys or defined &_STRING($keys) or &_ARRAY($keys) 6 5041 5047
Condition83%5/6
not defined $keys or defined &_STRING($keys) or &_ARRAY($keys)
l!l&&r!l&&!r
504106
not defined $keys or defined &_STRING($keys)
l!l&&r!l&&!r
5019226
MC/DC66%2/3
not defined $keys or defined &_STRING($keys) or &_ARRAY($keys)
not defined $keys defined &_STRING($keys) &_ARRAY($keys)
Truth table75%3/4
not(A) or B or C
A= not defined $keysB= defined &_STRING($keys)C= &_ARRAY($keys)
ABCresult
1XX1
01X1
0011
0000
327 unless ( !defined( $keys ) || defined( _STRING( $keys ) ) || _ARRAY( $keys ) );
328 $self->_error( $ERROR_MISMATCH_ARGUMENT, 'compression_codec' )
329 5041 ▶ unless ( !defined( $compression_codec ) || $known_compression_codecs{ $compression_codec } );
Branch: 1 branch
BranchTrueFalseTotal
unless not defined $compression_codec or $known_compression_codecs{$compression_codec} 12 5029 5041
Condition100%3/3
not defined $compression_codec or $known_compression_codecs{$compression_codec}
l!l&&r!l&&!r
5026312
MC/DC100%2/2
not defined $compression_codec or $known_compression_codecs{$compression_codec}
not defined $compression_codec $known_compression_codecs{$compression_codec}
Truth table100%3/3
not(A) or B
A= not defined $compression_codecB= $known_compression_codecs{$compression_codec}
ABresult
1X1
011
000
330 5029 ▶ $self->_error( $ERROR_MISMATCH_ARGUMENT, 'timestamps' )
Branch: 1 branch
BranchTrueFalseTotal
unless not defined $timestamps or defined &_POSINT($timestamps) or &_ARRAY($timestamps) 0 5029 5029
Condition33%2/6
not defined $timestamps or defined &_POSINT($timestamps) or &_ARRAY($timestamps)
l!l&&r!l&&!r
502900
not defined $timestamps or defined &_POSINT($timestamps)
l!l&&r!l&&!r
502900
MC/DC0%0/3
not defined $timestamps or defined &_POSINT($timestamps) or &_ARRAY($timestamps)
not defined $timestamps defined &_POSINT($timestamps) &_ARRAY($timestamps)
Truth table25%1/4
not(A) or B or C
A= not defined $timestampsB= defined &_POSINT($timestamps)C= &_ARRAY($timestamps)
ABCresult
1XX1
01X1
0011
0000
331 unless ( !defined( $timestamps ) || defined( _POSINT( $timestamps ) ) || _ARRAY( $timestamps ) );
332  
333 5029 ▶ $messages = [ $messages ] unless ref( $messages );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $messages 8 5021 5029
334 5029 foreach my $message ( @$messages ) {
335 10042 ▶ $self->_error( $ERROR_MISMATCH_ARGUMENT, format_message( 'message = %s', $message ) )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $message and $message eq "" || defined &_STRING($message) && !utf8::is_utf8($message) 9 10033 10042
Condition88%8/9
defined $message and $message eq "" || defined &_STRING($message) && !utf8::is_utf8($message)
!ll&&!rl&&r
1210039
$message eq "" or defined &_STRING($message) and not utf8::is_utf8($message)
l!l&&r!l&&!r
0100392
defined &_STRING($message) and not utf8::is_utf8($message)
!ll&&!rl&&r
6210033
MC/DC50%2/4
defined $message and $message eq "" || defined &_STRING($message) && !utf8::is_utf8($message)
defined $message $message eq "" defined &_STRING($message) not utf8::is_utf8($message)
Truth table60%3/5
A and B or C and not(D)
A= defined $messageB= $message eq ""C= defined &_STRING($message)D= not utf8::is_utf8($message)
ABCDresult
0XXX0
100X0
10100
11XX1
10111
336 unless defined( $message ) && ( $message eq '' || ( defined( _STRING( $message ) ) && !utf8::is_utf8( $message ) ) );
337 }
338  
339 5020 my $common_key;
340  
341 5020 ▶ if( _ARRAY( $keys ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (&_ARRAY($keys)) { } 0 5020 5020
elsif (defined $keys) { } 1 5019 5020
342 # ensure that keys array maytches messages array
343 0 ▶ $self->_error( $ERROR_MISMATCH_ARGUMENT, 'keys' )
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @$keys == scalar @$messages 0 0 0
344 unless scalar( @$keys ) == scalar( @$messages );
345  
346 0 foreach my $key ( @$keys ) {
347 0 ▶ $self->_error( $ERROR_MISMATCH_ARGUMENT, format_message( 'key = %s', $key ) )
Branch: 1 branch
BranchTrueFalseTotal
unless not defined $key or $key eq "" or defined &_STRING($key) and not utf8::is_utf8($key) 0 0 0
Condition0%0/9
not defined $key or $key eq "" or defined &_STRING($key) and not utf8::is_utf8($key)
l!l&&r!l&&!r
000
not defined $key or $key eq ""
l!l&&r!l&&!r
000
defined &_STRING($key) and not utf8::is_utf8($key)
!ll&&!rl&&r
000
MC/DC0%0/4
not defined $key or $key eq "" or defined &_STRING($key) and not utf8::is_utf8($key)
not defined $key $key eq "" defined &_STRING($key) not utf8::is_utf8($key)
Truth table0%0/5
not(A) or B or C and not(D)
A= not defined $keyB= $key eq ""C= defined &_STRING($key)D= not utf8::is_utf8($key)
ABCDresult
1XXX1
01XX1
00111
000X0
00100
348 unless !defined( $key ) || $key eq '' || ( defined( _STRING( $key ) ) && !utf8::is_utf8( $key ) );
349 }
350 }
351 elsif( defined $keys ) {
352 1 ▶ $self->_error( $ERROR_MISMATCH_ARGUMENT, format_message( 'key = %s', $keys ) )
Branch: 1 branch
BranchTrueFalseTotal
unless $keys eq "" or defined &_STRING($keys) and not utf8::is_utf8($keys) 1 0 1
Condition33%2/6
$keys eq "" or defined &_STRING($keys) and not utf8::is_utf8($keys)
l!l&&r!l&&!r
001
defined &_STRING($keys) and not utf8::is_utf8($keys)
!ll&&!rl&&r
010
MC/DC0%0/3
$keys eq "" or defined &_STRING($keys) and not utf8::is_utf8($keys)
$keys eq "" defined &_STRING($keys) not utf8::is_utf8($keys)
Truth table25%1/4
A or B and not(C)
A= $keys eq ""B= defined &_STRING($keys)C= not utf8::is_utf8($keys)
ABCresult
1XX1
0111
00X0
0100
353 unless $keys eq '' || ( defined( _STRING( $keys ) ) && !utf8::is_utf8( $keys ) );
354 0 $common_key = $keys;
355 }
356 else {
357 5019 $common_key = '';
358 }
359  
360 5019 my ($common_ts, $use_ts);
361  
362 5019 ▶ if( _ARRAY( $timestamps ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (&_ARRAY($timestamps)) { } 0 5019 5019
elsif (defined $timestamps) { } 0 5019 5019
363 # ensure that timestamps array maytches messages array
364 0 ▶ $self->_error( $ERROR_MISMATCH_ARGUMENT, 'timestamps' )
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @$timestamps == scalar @$messages 0 0 0
365 unless scalar( @$timestamps ) == scalar( @$messages );
366  
367 0 foreach my $ts ( @$timestamps ) {
368 0 ▶ $self->_error( $ERROR_MISMATCH_ARGUMENT, format_message( 'timestamp = %s', $ts ) )
Branch: 1 branch
BranchTrueFalseTotal
unless not defined $ts or defined &_POSINT($ts) 0 0 0
Condition0%0/3
not defined $ts or defined &_POSINT($ts)
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $ts or defined &_POSINT($ts)
not defined $ts defined &_POSINT($ts)
Truth table0%0/3
not(A) or B
A= not defined $tsB= defined &_POSINT($ts)
ABresult
1X1
011
000
369 unless !defined( $ts ) || defined( _POSINT( $ts ) );
370 }
371 0 $use_ts = 1;
372 }
373 elsif( defined $timestamps ) {
374 0 ▶ $self->_error( $ERROR_MISMATCH_ARGUMENT, format_message( 'timestamp = %s', $timestamps ) )
Branch: 1 branch
BranchTrueFalseTotal
unless defined &_POSINT($timestamps) 0 0 0
375 unless defined( _POSINT( $timestamps ));
376 0 $common_ts = $timestamps;
377 0 $use_ts = 1;
378 }
379  
380 5019 my $MessageSet = [];
381 my $request = {
382 ApiKey => $APIKEY_PRODUCE,
383 CorrelationId => _get_CorrelationId(),
384 ClientId => $self->{ClientId},
385 RequiredAcks => $self->{RequiredAcks},
386 5019 Timeout => int( $self->{Timeout} * 1000 ),
387 topics => [
388 {
389 TopicName => $topic,
390 partitions => [
391 {
392 Partition => $partition,
393 MessageSet => $MessageSet,
394 },
395 ],
396 },
397 ],
398 };
399 5019 ▶ if ( ( defined $compression_codec and $compression_codec == $COMPRESSION_LZ4 ) or defined $timestamps ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $compression_codec and $compression_codec == $Kafka::Producer::COMPRESSION_LZ4 or defined $timestamps) 0 5019 5019
Condition50%3/6
defined $compression_codec and $compression_codec == $Kafka::Producer::COMPRESSION_LZ4 or defined $timestamps
l!l&&r!l&&!r
005019
defined $compression_codec and $compression_codec == $Kafka::Producer::COMPRESSION_LZ4
!ll&&!rl&&r
501630
MC/DC0%0/3
defined $compression_codec and $compression_codec == $Kafka::Producer::COMPRESSION_LZ4 or defined $timestamps
defined $compression_codec $compression_codec == $Kafka::Producer::COMPRESSION_LZ4 defined $timestamps
Truth table40%2/5
A and B or C
A= defined $compression_codecB= $compression_codec == $Kafka::Producer::COMPRESSION_LZ4C= defined $timestamps
ABCresult
11X1
0X11
1011
0X00
1000
400 0 $request->{ApiVersion} = 2;
401 }
402  
403 5019 my $key_index = 0;
404 5019 foreach my $message ( @$messages ) {
405 10032 ▶ push @$MessageSet, {
Branch: 3 branches
BranchTrueFalseTotal
defined $common_key ? : 10032 0 10032
$use_ts ? : 0 10032 10032
defined $common_ts ? : 0 0 0
Condition0%0/2
$keys->[$key_index] // ""
l!l
00
MC/DC0%0/1
$keys->[$key_index] // ""
$keys->[$key_index]
Truth table0%0/2
A
A= $keys->[$key_index]
Aresult
11
00
406 Offset => $PRODUCER_ANY_OFFSET,
407 Key => defined $common_key ? $common_key : ( $keys->[ $key_index ] // '' ),
408 Value => $message,
409 $use_ts ? ( Timestamp => defined $common_ts ? $common_ts : $timestamps->[$key_index] ) : (),
410 };
411 10032 ++$key_index;
412 }
413  
414 5019 my $result = $self->{Connection}->receive_response_to_request( $request, $compression_codec, $self->{Timeout} );
415 5017 return $result;
416 }
417  
418 #-- private attributes ---------------------------------------------------------
419  
420 #-- private methods ------------------------------------------------------------
421  
422 # Handler for errors
423 sub _error {
424 99 ▶ my $self = shift;
Subroutine _error: called CC=1 SCAR=0.4
425  
426 99 Kafka::Exception::Producer->throw( throw_args( @_ ) );
427  
428 0 return;
429 }
430  
431  
432  
433 6 1;
434  
435 __END__
« lib/Kafka/Protocol.pm ↑ Summary blib/lib/Kafka/Internals.pm »