blib/lib/Kafka/Producer/Avro.pm

filter: Statementstmt 23.4% 34 / 145 Branchbran 2.6% 2 / 76 Conditioncond 1.0% 1 / 97 MC/DCmcdc 0.0% 0 / 38 Subroutinesub 50.0% 10 / 20 Podpod 100% 5 / 5 totaltotal 13.6% 52 / 381 CC 63 SCAR 59.8
CC
63
Cov
12%
send
650.0
bulk_send
600.0
_get_avro_schema
156.0
CRAP 397.3
↑ Summary
1 package Kafka::Producer::Avro;
2  
3 =pod
4  
5 =head1 NAME
6  
7 Kafka::Producer::Avro - Avro message producer for Apache Kafka.
8  
9 =head1 SYNOPSIS
10  
11 use Kafka::Connection;
12 use Kafka::Producer::Avro;
13  
14 my $connection = Kafka::Connection->new( host => 'localhost' );
15  
16 my $producer = Kafka::Producer::Avro->new( Connection => $connection );
17  
18 # Do some interactions with Avro & SchemaRegistry before sending messages
19  
20 # Sending a single message
21 my $response = $producer->send(...);
22  
23 # Sending a series of messages
24 $response = $producer->send(...);
25  
26 # Closes the producer and cleans up
27 undef $producer;
28 $connection->close;
29 undef $connection;
30  
31 =head1 DESCRIPTION
32  
33 C<Kafka::Producer::Avro> main feature is to provide object-oriented API to
34 produce messages according to I<Confluent SchemaRegistry> and I<Avro> serialization.
35  
36 C<Kafka::Producer::Avro> inerhits from and extends L<Kafka::Producer|Kafka::Producer>.
37  
38 =cut
39  
40 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 1 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 1 ▶use base 'Kafka::Producer';
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 1 ▶use Avro::BinaryEncoder;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 1 ▶use Avro::Schema;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 1 ▶use Confluent::SchemaRegistry;
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 1 ▶use constant MAGIC_BYTE => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
53  
54 1 our $VERSION = '0.02';
55  
56  
57 =head2 CONSTRUCTOR
58  
59 =head3 C<new>
60  
61 Creates new producer client object.
62  
63 C<new()> takes arguments in key-value pairs as described in L<Kafka::Producer|Kafka::Producer> from which it inherits.
64  
65 In addition, takes in the following arguments:
66  
67 =over 3
68  
69 =item C<SchemaRegistry =E<gt> $schema_registry> (B<mandatory>)
70  
71 Is a L<Confluent::SchemaRegistry|Confluent::SchemaRegistry> instance.
72  
73 =back
74  
75 =cut
76  
77 sub new {
78 1 ▶ my $this = shift;
Subroutine new: called CC=4 SCAR=17.3
Pod documented
79 1 ▶ my $class = ref($this) || $this;
Condition33%1/3
ref $this || $this
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table33%1/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
80 1 my $schema_registry_class = 'Confluent::SchemaRegistry';
81 1 my %params = @_;
82  
83 # Check SchemaRegistry param
84 die "Missing SchemaRegistry param"
85 1 ▶ unless exists $params{SchemaRegistry};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $params{"SchemaRegistry"} 0 1 1
86 die "SchemaRegistry param must be a $schema_registry_class instance object"
87 1 ▶ unless ref($params{SchemaRegistry}) eq $schema_registry_class;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $params{"SchemaRegistry"} eq $schema_registry_class 1 0 1
88 0 my $schema_registry = delete $params{SchemaRegistry};
89
90 # Use parent class constructor
91 0 my $self = $class->SUPER::new(%params);
92
93 # Add ans internal reference to SchemaRegistry
94 0 $self->{__SCHEMA_REGISTRY} = $schema_registry;
95
96 0 return bless($self, $class);
97 }
98  
99  
100  
101 ##### Class methods
102  
103 # Encode $payload in Avro format according to an Avro schema
104 sub _encode {
105 0 ▶ my $schema_ref = shift;
Subroutine _encode: not called CC=2 SCAR=17.9
106 0 my $payload = shift;
107 return undef
108 0 ▶ unless defined $payload;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $payload 0 0 0
109 0 my $encoded = pack('bN', &MAGIC_BYTE, $schema_ref->{id});
110 Avro::BinaryEncoder->encode(
111 schema => $schema_ref->{schema},
112 data => $payload,
113 emit_cb => sub {
114 0 ▶ $encoded .= ${ $_[0] };
Subroutine __ANON__: not called CC=1 SCAR=6.9
115 }
116 0 );
117 0 return $encoded;
118 }
119  
120  
121 ##### Private methods
122  
123 0 ▶sub _clear_error { $_[0]->_set_error() }
Subroutine _clear_error: not called CC=1 SCAR=6.9
124 0 ▶sub _set_error { $_[0]->{__ERROR} = $_[1] }
Subroutine _set_error: not called CC=1 SCAR=6.9
125 0 ▶sub _get_error { $_[0]->{__ERROR} }
Subroutine _get_error: not called CC=1 SCAR=6.9
126  
127 # Interact with Schema Registry
128 sub _get_avro_schema {
129 0 ▶ my $self = shift;
Subroutine _get_avro_schema: not called CC=12 SCAR=50.5
130 0 my $topic = shift;
131 0 my $type = shift;
132 0 my $supplied_schema = shift;
133
134 0 my $subject = $topic . '-' . $type;
135 0 my $sr = $self->schema_registry();
136 0 my ($schema_id, $avro_schema);
137
138 # FIXME need to use caching w/ TTL to avoid these checks for every call
139
140 # If a schema is supplied...
141 0 ▶ if ($supplied_schema) {
Branch: 1 branch
BranchTrueFalseTotal
if ($supplied_schema) { } 0 0 0
142
143 # If the subject exixts...
144 0 my $subjects = $sr->get_subjects();
145 0 ▶ if (grep(/^$subject$/, @$subjects) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep /^$subject$/, @$subjects) 0 0 0
146
147 # ...check if it already exists in registry
148 0 my $schema_info = $sr->check_schema(
149 SUBJECT => $topic,
150 TYPE => $type,
151 SCHEMA => $supplied_schema
152 );
153 0 ▶ if ( defined $schema_info ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $schema_info) { } 0 0 0
154
155 0 $schema_id = $schema_info->{id};
156 0 $avro_schema = $schema_info->{schema};
157  
158 # ...if it does not already exist in the registry....
159 } else {
160
161 # ...test new schema compliancy against latest version
162 0 my $compliant = $sr->test_schema(
163 SUBJECT => $topic,
164 TYPE => $type,
165 SCHEMA => $supplied_schema
166 );
167 0 ▶ $self->_set_error('Schema not compliant with latest one from registry') &&
Branch: 1 branch
BranchTrueFalseTotal
unless $compliant 0 0 0
Condition0%0/5
$compliant || $self->_set_error('Schema not compliant with latest one from registry') && (return undef)
l!l&&r!l&&!r
000
$self->_set_error("Schema not compliant with latest one from registry") and return undef
!ll
00
MC/DC0%0/2
$compliant || $self->_set_error('Schema not compliant with latest one from registry') && (return undef)
$compliant $self->_set_error("Schema not compliant with latest one from registry")
Truth table0%0/3
A || B
A= $compliantB= $self->_set_error("Schema not compliant with latest one from registry")
ABresult
1X1
011
000
168 return undef
169 unless $compliant;
170
171 }
172
173 }
174
175 # ...if a previous id for the schema is not available, try to add the one supplied to the registry....
176 0 ▶ unless ($schema_id) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($schema_id) 0 0 0
177
178 # ...procede adding it to the registry
179 0 $schema_id = $sr->add_schema(
180 SUBJECT => $topic,
181 TYPE => $type,
182 SCHEMA => $supplied_schema
183 );
184 0 ▶ $self->_set_error('Error adding schema to registry: ' . encode_json($sr->get_error())) &&
Branch: 1 branch
BranchTrueFalseTotal
unless $schema_id 0 0 0
Condition0%0/5
$schema_id || $self->_set_error('Error adding schema to registry: ' . &encode_json(scalar $sr->get_error)) && (return undef)
l!l&&r!l&&!r
000
$self->_set_error('Error adding schema to registry: ' . &encode_json(scalar $sr->get_error)) and return undef
!ll
00
MC/DC0%0/2
$schema_id || $self->_set_error('Error adding schema to registry: ' . &encode_json(scalar $sr->get_error)) && (return undef)
$schema_id $self->_set_error('Error adding schema to registry: ' . &encode_json(scalar $sr->get_error))
Truth table0%0/3
A || B
A= $schema_idB= $self->_set_error('Error adding schema to registry: ' . &encode_json(scalar $sr->get_error))
ABresult
1X1
011
000
185 return undef
186 unless $schema_id;
187
188 # ...and bless new schema into an Avro schema object
189 0 $avro_schema = Avro::Schema->parse($supplied_schema);
190
191 }
192
193 } else {
194
195 # retreive latest schema for the topic value
196 0 my $schema_info = $sr->get_schema(
197 SUBJECT => $topic,
198 TYPE => $type
199 );
200 0 ▶ if ( defined $schema_info ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $schema_info) { } 0 0 0
201
202 0 $schema_id = $schema_info->{id};
203 0 $avro_schema = $schema_info->{schema};
204
205 } else {
206 0 ▶ $self->_set_error("No schema in registry for subject " . $topic . '-' . 'value') &&
Branch: 1 branch
BranchTrueFalseTotal
unless $schema_info 0 0 0
Condition0%0/5
$schema_info || $self->_set_error('No schema in registry for subject ' . $topic . '-' . 'value') && (return undef)
l!l&&r!l&&!r
000
$self->_set_error('No schema in registry for subject ' . $topic . '-' . "value") and return undef
!ll
00
MC/DC0%0/2
$schema_info || $self->_set_error('No schema in registry for subject ' . $topic . '-' . 'value') && (return undef)
$schema_info $self->_set_error('No schema in registry for subject ' . $topic . '-' . "value")
Truth table0%0/3
A || B
A= $schema_infoB= $self->_set_error('No schema in registry for subject ' . $topic . '-' . "value")
ABresult
1X1
011
000
207 return undef
208 unless $schema_info;
209 }
210
211 }
212
213 0 return ($schema_id, $avro_schema);
214 }
215  
216  
217  
218  
219  
220 ##### Public methods
221  
222 =head2 METHODS
223  
224 The following methods are defined for the C<Kafka::Avro::Producer> class:
225  
226 =cut
227  
228  
229 =head3 C<schema_registry>()
230  
231 Returns the L<Confluent::SchemaRegistry|Confluent::SchemaRegistry> instance supplied to the construcor.
232  
233 =cut
234  
235 0 ▶sub schema_registry { $_[0]->{__SCHEMA_REGISTRY} }
Subroutine schema_registry: not called CC=1 SCAR=6.9
Pod documented
236  
237  
238 =head3 C<get_error>()
239  
240 Returns a string containing last error message.
241  
242 =cut
243  
244 0 ▶sub get_error { $_[0]->_get_error() }
Subroutine get_error: not called CC=1 SCAR=6.9
Pod documented
245  
246  
247 =head3 C<send( %params )>
248  
249 Sends a messages on a L<Kafka::Connection|Kafka::Connection> object.
250  
251 Returns a non-blank value (a reference to a hash with server response description)
252 if the message is successfully sent.
253  
254 Despite L<Kafka::Producer|Kafka::Producer-E<gt>send()> method that expects positional arguments,
255 C<Kafka::Producer::Avro->send()> method looks for named parameters:
256  
257 $producer->send(
258 topic => $topic, # scalar
259 partition => $partition, # scalar
260 messages => $messages, # scalar | array
261 keys => $keys, # scalar | array
262 compression_codec => $compression_codec, # scalar
263 key_schema => $key_schema, # optional JSON-string
264 value_schema => $value_schema # optional JSON-string
265 );
266  
267 Extra arguments may be suggested:
268  
269 =over 3
270  
271 =item C<key_schema =E<gt> $key_schema> and C<value_schema =E<gt> $value_schema>
272  
273 Both C<$key_schema> and C<$value_schema> parameters are optional and provide JSON strings that
274 represent Avro schemas to use to validate and serialize key(s) and value(s).
275  
276 These schemas are validated against C<schema_registry> and, if compliant, they are added to the registry
277 under the C<$topic+'key'> or C<$topic+'value'> subjects.
278  
279 If an expected schema isn't provided, latest version from Schema Registry is used accordingly to the
280 subject (key or value).
281  
282 =back
283  
284 =cut
285  
286 sub send {
287 0 ▶ my $self = shift;
Subroutine send: not called CC=25 SCAR=64.8
Pod documented
288 0 my %params = @_;
289 0 my $avro_schemas = {
290 key => {
291 id => undef,
292 schema => undef
293 },
294 value => {
295 id => undef,
296 schema => undef
297 }
298 };
299 0 my ($keys, $messages);
300  
301 0 $self->_clear_error();
302
303 $self->_set_error('Missing topic param')
304 and return undef
305 0 ▶ unless defined $params{topic};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"topic"} 0 0 0
Condition0%0/5
defined $params{'topic'} || $self->_set_error('Missing topic param') && (return undef)
l!l&&r!l&&!r
000
$self->_set_error("Missing topic param") and return undef
!ll
00
MC/DC0%0/2
defined $params{'topic'} || $self->_set_error('Missing topic param') && (return undef)
defined $params{'topic'} $self->_set_error("Missing topic param")
Truth table0%0/3
A || B
A= defined $params{'topic'}B= $self->_set_error("Missing topic param")
ABresult
1X1
011
000
306 $self->_set_error('Missing partition param')
307 and return undef
308 0 ▶ unless defined $params{partition};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"partition"} 0 0 0
Condition0%0/5
defined $params{'partition'} || $self->_set_error('Missing partition param') && (return undef)
l!l&&r!l&&!r
000
$self->_set_error("Missing partition param") and return undef
!ll
00
MC/DC0%0/2
defined $params{'partition'} || $self->_set_error('Missing partition param') && (return undef)
defined $params{'partition'} $self->_set_error("Missing partition param")
Truth table0%0/3
A || B
A= defined $params{'partition'}B= $self->_set_error("Missing partition param")
ABresult
1X1
011
000
309 $self->_set_error('Missing messages')
310 and return undef
311 0 ▶ unless defined $params{messages};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"messages"} 0 0 0
Condition0%0/5
defined $params{'messages'} || $self->_set_error('Missing messages') && (return undef)
l!l&&r!l&&!r
000
$self->_set_error("Missing messages") and return undef
!ll
00
MC/DC0%0/2
defined $params{'messages'} || $self->_set_error('Missing messages') && (return undef)
defined $params{'messages'} $self->_set_error("Missing messages")
Truth table0%0/3
A || B
A= defined $params{'messages'}B= $self->_set_error("Missing messages")
ABresult
1X1
011
000
312
313 # Get Avro schema for keys and values
314 0 foreach my $type (qw/key value/) {
315 0 ($avro_schemas->{$type}->{id}, $avro_schemas->{$type}->{schema}) = $self->_get_avro_schema($params{topic}, $type, $params{"${type}_schema"});
316 }
317  
318 0 ▶ if ($params{keys}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"keys"}) 0 0 0
319
320 # Return if key Avro schema was not found
321 $self->_set_error('No key Avro schema found')
322 and return undef
323 0 ▶ unless defined $avro_schemas->{key}->{id} && defined $avro_schemas->{key}->{schema};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $avro_schemas->{"key"}{"id"} and defined $avro_schemas->{"key"}{"schema"} 0 0 0
Condition0%0/8
defined $avro_schemas->{'key'}{'id'} && defined $avro_schemas->{'key'}{'schema'} || $self->_set_error('No key Avro schema found') && (return undef)
l!l&&r!l&&!r
000
defined $avro_schemas->{"key"}{"id"} and defined $avro_schemas->{"key"}{"schema"}
!ll&&!rl&&r
000
$self->_set_error("No key Avro schema found") and return undef
!ll
00
MC/DC0%0/3
defined $avro_schemas->{'key'}{'id'} && defined $avro_schemas->{'key'}{'schema'} || $self->_set_error('No key Avro schema found') && (return undef)
defined $avro_schemas->{"key"}{"id"} defined $avro_schemas->{"key"}{"schema"} $self->_set_error("No key Avro schema found")
Truth table0%0/5
A and B || C
A= defined $avro_schemas->{"key"}{"id"}B= defined $avro_schemas->{"key"}{"schema"}C= $self->_set_error("No key Avro schema found")
ABCresult
11X1
0X11
1011
0X00
1000
324
325 # Avro encoding of keys
326 0 ▶ if (ref($params{keys}) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params{'keys'} eq 'ARRAY') { } 0 0 0
327 $keys = [
328 map {
329 0 _encode($avro_schemas->{key}, $_);
330 0 } @{$params{keys}}
331 ];
332 } else {
333 0 $keys = _encode($avro_schemas->{key}, $params{keys});
334 }
335
336 }
337  
338 # Return if value Avro schema was not found
339 $self->_set_error('No value Avro schema found')
340 and return undef
341 0 ▶ unless defined $avro_schemas->{value}->{id} && defined $avro_schemas->{value}->{schema};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $avro_schemas->{"value"}{"id"} and defined $avro_schemas->{"value"}{"schema"} 0 0 0
Condition0%0/8
defined $avro_schemas->{'value'}{'id'} && defined $avro_schemas->{'value'}{'schema'} || $self->_set_error('No value Avro schema found') && (return undef)
l!l&&r!l&&!r
000
defined $avro_schemas->{"value"}{"id"} and defined $avro_schemas->{"value"}{"schema"}
!ll&&!rl&&r
000
$self->_set_error("No value Avro schema found") and return undef
!ll
00
MC/DC0%0/3
defined $avro_schemas->{'value'}{'id'} && defined $avro_schemas->{'value'}{'schema'} || $self->_set_error('No value Avro schema found') && (return undef)
defined $avro_schemas->{"value"}{"id"} defined $avro_schemas->{"value"}{"schema"} $self->_set_error("No value Avro schema found")
Truth table0%0/5
A and B || C
A= defined $avro_schemas->{"value"}{"id"}B= defined $avro_schemas->{"value"}{"schema"}C= $self->_set_error("No value Avro schema found")
ABCresult
11X1
0X11
1011
0X00
1000
342
343 # Avro encoding of messages
344 0 ▶ if (ref($params{messages}) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params{'messages'} eq 'ARRAY') { } 0 0 0
345 $messages = [
346 map {
347 0 _encode($avro_schemas->{value}, $_);
348 0 } @{$params{messages}}
349 ];
350 } else {
351 0 $messages = _encode($avro_schemas->{value}, $params{messages});
352 }
353
354 # Return in case of mismatch between keys and messages
355 0 ▶ if ($avro_schemas->{key}->{id}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($avro_schemas->{"key"}{"id"}) 0 0 0
356 0 ▶ $self->_set_error('Keys/messages format mismatch')
Branch: 1 branch
BranchTrueFalseTotal
if ref $keys eq "ARRAY" and ref $messages ne "ARRAY" 0 0 0
Condition0%0/8
ref $keys eq 'ARRAY' && ref $messages ne 'ARRAY' && ($self->_set_error('Keys/messages format mismatch') && (return undef))
!ll&&!rl&&r
000
ref $keys eq "ARRAY" and ref $messages ne "ARRAY"
!ll&&!rl&&r
000
$self->_set_error("Keys/messages format mismatch") and return undef
!ll
00
MC/DC0%0/3
ref $keys eq 'ARRAY' && ref $messages ne 'ARRAY' && ($self->_set_error('Keys/messages format mismatch') && (return undef))
ref $keys eq "ARRAY" ref $messages ne "ARRAY" $self->_set_error("Keys/messages format mismatch")
Truth table0%0/4
A and B && C
A= ref $keys eq "ARRAY"B= ref $messages ne "ARRAY"C= $self->_set_error("Keys/messages format mismatch")
ABCresult
0XX0
10X0
1100
1111
357 and return undef
358 if ref($keys) eq 'ARRAY' && ref($messages) ne 'ARRAY';
359 0 ▶ $self->_set_error('Keys/messages count mismatch')
Branch: 1 branch
BranchTrueFalseTotal
if ref $keys eq "ARRAY" and ref $messages eq "ARRAY" and $keys->$#* != $messages->$#* 0 0 0
Condition0%0/11
ref $keys eq 'ARRAY' && ref $messages eq 'ARRAY' && $keys->$#* != $messages->$#* && ($self->_set_error('Keys/messages count mismatch') && (return undef))
!ll&&!rl&&r
000
ref $keys eq "ARRAY" and ref $messages eq "ARRAY" and $keys->$#* != $messages->$#*
!ll&&!rl&&r
000
ref $keys eq "ARRAY" and ref $messages eq "ARRAY"
!ll&&!rl&&r
000
$self->_set_error("Keys/messages count mismatch") and return undef
!ll
00
MC/DC0%0/4
ref $keys eq 'ARRAY' && ref $messages eq 'ARRAY' && $keys->$#* != $messages->$#* && ($self->_set_error('Keys/messages count mismatch') && (return undef))
ref $keys eq "ARRAY" ref $messages eq "ARRAY" $keys->$#* != $messages->$#* $self->_set_error("Keys/messages count mismatch")
Truth table0%0/5
A and B and C && D
A= ref $keys eq "ARRAY"B= ref $messages eq "ARRAY"C= $keys->$#* != $messages->$#*D= $self->_set_error("Keys/messages count mismatch")
ABCDresult
0XXX0
10XX0
110X0
11100
11111
360 and return undef
361 if ref($keys) eq 'ARRAY' && ref($messages) eq 'ARRAY' && $#$keys != $#$messages;
362 }
363
364 # Send messages through Kafka::Producer parent class
365 return $self->SUPER::send(
366 $params{topic},
367 $params{partition},
368 $messages,
369 $keys,
370 $params{compression_codec}
371 0 );
372
373 }
374  
375  
376 =head3 C<bulk_send( %params )>
377  
378 Similar to C<send> but uses bulks to avoid memory leaking.
379  
380 Extra named parameters are expected:
381  
382 =over 3
383  
384 =item C<size =E<gt> $size>
385  
386 The size of the bulk
387  
388 =item C<on_before_send_bulk =E<gt> sub {...} > (optional)
389  
390 A code block that will be executed before the sending of each bulk.
391  
392 The block will receive the following positional parameters:
393  
394 =over 3
395  
396 =item C<$bulk_num> the number of the bulk
397  
398 =item C<$bulk_messages> the number of messages in the bulk
399  
400 =item C<$bulk_keys> the number of keys in the bulk
401  
402 =item C<$index_from> the absolute index of the first message in the bulk
403  
404 =item C<$index_to> the absolute index of the last message in the bulk
405  
406 =back
407  
408 =item C<on_after_send_bulk =E<gt> sub {...} > (optional)
409  
410 A code block that will be executed after the sending of each bulk.
411  
412 The block will receive the following positional parameters:
413  
414 =over 3
415  
416 =item C<$sent> the number of sent messages in the bulk
417  
418 =item C<$total_sent> the total number of messages sent
419  
420 =back
421  
422 =item C<on_init =E<gt> sub {...} > (optional)
423  
424 A code block that will be executed only once before at the beginning of the cycle.
425  
426 The block will receive the following positional parameters:
427  
428 =over 3
429  
430 =item C<$to_send> the total number of messages to send
431  
432 =item C<$bulk_size> the size of the bulk
433  
434 =back
435  
436 =item C<on_complete =E<gt> sub {...} > (optional)
437  
438 A code block that will be executed only once after the end of the cycle.
439  
440 The block will receive the following positional parameters:
441  
442 =over 3
443  
444 =item C<$to_send> the total number of messages to send
445  
446 =item C<$total_sent> the total number of messages sent
447  
448 =item C<$errors> the number bulks sent with errors
449  
450 =back
451  
452 =item C<on_send_error =E<gt> sub {...} > (optional)
453  
454 A code block that will be executed when a bulk registers an error.
455  
456 =back
457  
458 =cut
459  
460 sub bulk_send {
461 0 ▶ my $self = shift;
Subroutine bulk_send: not called CC=24 SCAR=64.0
Pod documented
462 0 my %params = @_;
463
464 0 $self->_clear_error();
465
466 # If there is no an array of messages use send()
467 return $self->send(%params)
468 0 ▶ unless ref($params{messages}) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $params{"messages"} eq "ARRAY" 0 0 0
469
470 $self->_set_error('Missing bulk size')
471 and return undef
472 0 ▶ unless defined $params{size};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"size"} 0 0 0
Condition0%0/5
defined $params{'size'} || $self->_set_error('Missing bulk size') && (return undef)
l!l&&r!l&&!r
000
$self->_set_error("Missing bulk size") and return undef
!ll
00
MC/DC0%0/2
defined $params{'size'} || $self->_set_error('Missing bulk size') && (return undef)
defined $params{'size'} $self->_set_error("Missing bulk size")
Truth table0%0/3
A || B
A= defined $params{'size'}B= $self->_set_error("Missing bulk size")
ABresult
1X1
011
000
473 $self->_set_error('Bad bulk size')
474 and return undef
475 0 ▶ unless $params{size} =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $params{"size"} =~ /^\d+$/ 0 0 0
Condition0%0/5
$params{'size'} =~ /^\d+$/ || $self->_set_error('Bad bulk size') && (return undef)
l!l&&r!l&&!r
000
$self->_set_error("Bad bulk size") and return undef
!ll
00
MC/DC0%0/2
$params{'size'} =~ /^\d+$/ || $self->_set_error('Bad bulk size') && (return undef)
$params{'size'} =~ /^\d+$/ $self->_set_error("Bad bulk size")
Truth table0%0/3
A || B
A= $params{'size'} =~ /^\d+$/B= $self->_set_error("Bad bulk size")
ABresult
1X1
011
000
476  
477 0 my $messages = $params{messages};
478 0 my $keys = $params{keys};
479 0 ▶ $self->_set_error('Keys/messages format mismatch')
Branch: 1 branch
BranchTrueFalseTotal
if ref $keys eq "ARRAY" and ref $messages ne "ARRAY" 0 0 0
Condition0%0/8
ref $keys eq 'ARRAY' && ref $messages ne 'ARRAY' && ($self->_set_error('Keys/messages format mismatch') && (return undef))
!ll&&!rl&&r
000
ref $keys eq "ARRAY" and ref $messages ne "ARRAY"
!ll&&!rl&&r
000
$self->_set_error("Keys/messages format mismatch") and return undef
!ll
00
MC/DC0%0/3
ref $keys eq 'ARRAY' && ref $messages ne 'ARRAY' && ($self->_set_error('Keys/messages format mismatch') && (return undef))
ref $keys eq "ARRAY" ref $messages ne "ARRAY" $self->_set_error("Keys/messages format mismatch")
Truth table0%0/4
A and B && C
A= ref $keys eq "ARRAY"B= ref $messages ne "ARRAY"C= $self->_set_error("Keys/messages format mismatch")
ABCresult
0XX0
10X0
1100
1111
480 and return undef
481 if ref($keys) eq 'ARRAY' && ref($messages) ne 'ARRAY';
482 0 ▶ $self->_set_error('Keys/messages count mismatch')
Branch: 1 branch
BranchTrueFalseTotal
if ref $keys eq "ARRAY" and ref $messages eq "ARRAY" and $keys->$#* != $messages->$#* 0 0 0
Condition0%0/11
ref $keys eq 'ARRAY' && ref $messages eq 'ARRAY' && $keys->$#* != $messages->$#* && ($self->_set_error('Keys/messages count mismatch') && (return undef))
!ll&&!rl&&r
000
ref $keys eq "ARRAY" and ref $messages eq "ARRAY" and $keys->$#* != $messages->$#*
!ll&&!rl&&r
000
ref $keys eq "ARRAY" and ref $messages eq "ARRAY"
!ll&&!rl&&r
000
$self->_set_error("Keys/messages count mismatch") and return undef
!ll
00
MC/DC0%0/4
ref $keys eq 'ARRAY' && ref $messages eq 'ARRAY' && $keys->$#* != $messages->$#* && ($self->_set_error('Keys/messages count mismatch') && (return undef))
ref $keys eq "ARRAY" ref $messages eq "ARRAY" $keys->$#* != $messages->$#* $self->_set_error("Keys/messages count mismatch")
Truth table0%0/5
A and B and C && D
A= ref $keys eq "ARRAY"B= ref $messages eq "ARRAY"C= $keys->$#* != $messages->$#*D= $self->_set_error("Keys/messages count mismatch")
ABCDresult
0XXX0
10XX0
110X0
11100
11111
483 and return undef
484 if ref($keys) eq 'ARRAY' && ref($messages) eq 'ARRAY' && $#$keys != $#$messages;
485
486 0 my @messages = @$messages; # duplicate messages array to preserve external array
487 # Check for key(s) format (ARRAY vs. SCALAR)
488 0 my (@keys, $key);
489 0 ▶ if (ref($params{keys}) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params{'keys'} eq 'ARRAY') { } 0 0 0
490 0 @keys = @$keys;
491 } else {
492 0 $key = $keys;
493 }
494 0 my $message_count = scalar(@messages);
495 0 my $bulk_size = $params{size};
496 0 my $sent = 0;
497 0 my $errors = 0;
498 0 my $bulk_num = 0;
499
500 0 ▶ if (ref($params{on_init}) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params{"on_init"} eq "CODE") 0 0 0
501 0 $params{on_init}->($message_count, $bulk_size);
502 }
503 0 ▶ for (my $i=1; $i<=$message_count; $i+=$bulk_size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $message_count) 0 0 0
504 0 $bulk_num++;
505 0 my @bulk = splice @messages, 0, $bulk_size;
506 0 my $bulk_keys = $key;
507 0 ▶ unless ($bulk_keys) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($bulk_keys) 0 0 0
508 0 $bulk_keys = [ splice(@keys, 0, $bulk_size) ];
509 }
510 0 ▶ if (ref($params{on_before_send_bulk}) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params{"on_before_send_bulk"} eq "CODE") 0 0 0
511 0 ▶ $params{on_before_send_bulk}->($bulk_num, \@bulk, $bulk_keys, $i, ($i+$bulk_size<$message_count ? $i+$bulk_size-1 : $message_count));
Branch: 1 branch
BranchTrueFalseTotal
$i + $bulk_size < $message_count ? : 0 0 0
512 }
513 my $res = $self->send(
514 'topic' => $params{topic},
515 'partition' => 0,
516 'messages' => [ @bulk ],
517 'keys' => $bulk_keys,
518 'compressione_codec' => undef,
519 'key_schema' => $params{key_schema},
520 'value_schema' => $params{value_schema}
521 0 );
522 0 ▶ if (defined $res) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $res) { } 0 0 0
523 0 $sent += scalar(@bulk);
524 0 ▶ if (ref($params{on_after_send_bulk}) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params{"on_after_send_bulk"} eq "CODE") 0 0 0
525 0 $params{on_after_send_bulk}->(scalar(@bulk), $sent);
526 }
527 } else {
528 0 $errors++;
529 0 ▶ if (ref($params{on_send_error}) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params{"on_send_error"} eq "CODE") 0 0 0
530 0 ▶ $params{on_send_error}->($self->get_error(), $bulk_num, \@bulk, $bulk_keys, $i, ($i+$bulk_size<$message_count ? $i+$bulk_size-1 : $message_count));
Branch: 1 branch
BranchTrueFalseTotal
$i + $bulk_size < $message_count ? : 0 0 0
531 }
532 }
533 }
534 0 ▶ if (ref($params{on_complete}) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params{"on_complete"} eq "CODE") 0 0 0
535 0 $params{on_complete}->($message_count, $sent, $errors);
536 }
537 0 return $sent;
538
539 }
540  
541 =head1 AUTHOR
542  
543 Alvaro Livraghi, E<lt>alvarol@cpan.orgE<gt>
544  
545 =head1 CONTRIBUTE
546  
547 L<https://github.com/alivraghi/Kafka-Producer-Avro>
548  
549 =head1 BUGS
550  
551 Please use GitHub project link above to report problems or contact authors.
552  
553 =head1 COPYRIGHT AND LICENSE
554  
555 Copyright 2018 by Alvaro Livraghi
556  
557 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
558  
559 =cut
560  
561 1 1;
↑ Summary