blib/lib/Kafka/Producer/Avro.pm

filter: Statementstmt 18.4% 31 / 168 Branchbran 0.0% 0 / 88 Conditioncond 0.0% 0 / 97 MC/DCmcdc 0.0% 0 / 38 Subroutinesub 47.6% 10 / 21 Podpod 100% 5 / 5 totaltotal 11.0% 46 / 417 CC 69 SCAR 62.7
CC
69
Cov
9%
send
992.0
bulk_send
600.0
_get_avro_schema
156.0
CRAP 529.6
↑ 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 , SchemaRegistry => Confluent::SchemaRegistry->new() );
17
18 # Set Avro schema for message key (valid JSON-string)
19 my $key_schema = <<KEY_SCHEMA;
20 {
21 "type": "long",
22 "name": "_id"
23 }
24 KEY_SCHEMA
25 # Set Avro schema for message value (payload) (valid JSON-string)
26 my $value_schema = <<VALUE_SCHEMA;
27 {
28 "type": "record",
29 "name": "myrecord",
30 "fields": [
31 {
32 "name": "f1",
33 "type": "string"
34 }
35 ]
36 }
37 VALUE_SCHEMA
38
39 # Sending a single message
40 my $response = $producer->send(
41 'mytopic', # topic
42 0, # partition
43 'Single message', # message
44 undef, # key
45 undef, # compression_codec
46 undef, # timestamps
47 $key_schema, # key_schema
48 $value_schema # value_schema
49 );
50
51 # Sending a series of messages
52 $response = $producer->send(
53 'mytopic', # topic
54 0, # partition
55 [ # messages
56 'The first message',
57 'The second message',
58 'The third message',
59 ],
60 undef, # key(s)
61 undef, # compression_codec
62 undef, # timestamp(s)
63 $key_schema, # key_schema
64 $value_schema # value_schema
65 );
66
67 # ...or use named parameters
68
69 $producer->send(
70 topic => $topic,
71 partition => $partition,
72 messages => $messages,
73 keys => $keys,
74 compression_codec => $compression_codec,
75 timestamps => $timestamps,
76 key_schema => $key_schema,
77 value_schema => $value_schema
78 );
79
80 # Closes the producer and cleans up
81 undef $producer;
82 $connection->close;
83 undef $connection;
84  
85 =head1 DESCRIPTION
86  
87 C<Kafka::Producer::Avro> main feature is to provide object-oriented API to
88 produce messages according to I<Confluent SchemaRegistry> and I<Avro> serialization.
89  
90 C<Kafka::Producer::Avro> inerhits from and extends L<Kafka::Producer|Kafka::Producer>.
91  
92 =cut
93  
94 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
95 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
96 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
97  
98 1 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
99  
100 1 ▶use base 'Kafka::Producer';
Subroutine BEGIN: called CC=1 SCAR=0.0
101  
102 1 ▶use Avro::BinaryEncoder;
Subroutine BEGIN: called CC=1 SCAR=0.0
103 1 ▶use Avro::Schema;
Subroutine BEGIN: called CC=1 SCAR=0.0
104 1 ▶use Confluent::SchemaRegistry;
Subroutine BEGIN: called CC=1 SCAR=0.0
105  
106 1 ▶use constant MAGIC_BYTE => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
107  
108 1 ▶use version; our $VERSION = version->declare('v1.0.0');
Subroutine BEGIN: called CC=1 SCAR=0.0
109  
110 =head1 INSTALL
111  
112 Installation of C<Kafka::Producer::Avro> is a canonical:
113  
114 perl Makefile.PL
115 make
116 make test
117 make install
118  
119 =head2 TEST NOTES
120  
121 Tests are focused on verifying Avro-formatted messages and theirs interactions with Confluent Schema Registry and are intended to extend C<Kafka::Producer> test suite.
122  
123 They expect that in the target are listening Apache Kafka and Schema Registry services, respectively listening on C<localhost:9092> and C<http://localhost:8081>.
124  
125 You can alternatively set a different URLs by exporting the following environment variable:
126  
127 =over 3
128  
129 =item C<KAFKA_HOST>
130  
131 =item C<KAFKA_PORT>
132  
133 =item C<CONFLUENT_SCHEMA_REGISTY_URL>
134  
135 =back
136  
137 For example:
138  
139 export KAFKA_HOST=my-kafka-host.my-domain.org
140 export FALFA_PORT=9092
141 export CONFLUENT_SCHEMA_REGISTY_URL=http://my-schema-registry-host.my-domain.org
142  
143 =head1 USAGE
144  
145 =head2 CONSTRUCTOR
146  
147 =head3 C<new>
148  
149 Creates new producer client object.
150  
151 C<new()> takes arguments in key-value pairs as described in L<Kafka::Producer|Kafka::Producer> from which it inherits.
152  
153 In addition, takes in the following arguments:
154  
155 =over 3
156  
157 =item C<SchemaRegistry =E<gt> $schema_registry> (B<mandatory>)
158  
159 Is a L<Confluent::SchemaRegistry|Confluent::SchemaRegistry> instance.
160  
161 =back
162  
163 =cut
164  
165 sub new {
166 0 ▶ my $this = shift;
Subroutine new: not called CC=4 SCAR=30.0
Pod documented
167 0 ▶ my $class = ref($this) || $this;
Condition0%0/3
ref $this || $this
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table0%0/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
168 0 my $schema_registry_class = 'Confluent::SchemaRegistry';
169 0 my %params = @_;
170  
171 # Check SchemaRegistry param
172 die "Missing SchemaRegistry param"
173 0 ▶ unless exists $params{SchemaRegistry};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $params{"SchemaRegistry"} 0 0 0
174 die "SchemaRegistry param must be a $schema_registry_class instance object"
175 0 ▶ unless ref($params{SchemaRegistry}) eq $schema_registry_class;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $params{"SchemaRegistry"} eq $schema_registry_class 0 0 0
176 0 my $schema_registry = delete $params{SchemaRegistry};
177
178 # Use parent class constructor
179 0 my $self = $class->SUPER::new(%params);
180
181 # Add ans internal reference to SchemaRegistry
182 0 $self->{__SCHEMA_REGISTRY} = $schema_registry;
183
184 0 return bless($self, $class);
185 }
186  
187  
188  
189 ##### Class methods
190  
191 # Encode $payload in Avro format according to an Avro schema
192 sub _encode {
193 0 ▶ my $schema_ref = shift;
Subroutine _encode: not called CC=2 SCAR=17.9
194 0 my $payload = shift;
195 return undef
196 0 ▶ unless defined $payload;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $payload 0 0 0
197 0 my $encoded = pack('bN', &MAGIC_BYTE, $schema_ref->{id});
198 Avro::BinaryEncoder->encode(
199 schema => $schema_ref->{schema},
200 data => $payload,
201 emit_cb => sub {
202 0 ▶ $encoded .= ${ $_[0] };
Subroutine __ANON__: not called CC=1 SCAR=6.9
203 }
204 0 );
205 0 return $encoded;
206 }
207  
208  
209 ##### Private methods
210  
211 0 ▶sub _clear_error { $_[0]->_set_error() }
Subroutine _clear_error: not called CC=1 SCAR=6.9
212 0 ▶sub _set_error { $_[0]->{__ERROR} = $_[1] }
Subroutine _set_error: not called CC=1 SCAR=6.9
213 0 ▶sub _get_error { $_[0]->{__ERROR} }
Subroutine _get_error: not called CC=1 SCAR=6.9
214  
215 # Interact with Schema Registry
216 sub _get_avro_schema {
217 0 ▶ my $self = shift;
Subroutine _get_avro_schema: not called CC=12 SCAR=50.5
218 0 my $topic = shift;
219 0 my $type = shift;
220 0 my $supplied_schema = shift;
221
222 0 my $subject = $topic . '-' . $type;
223 0 my $sr = $self->schema_registry();
224 0 my ($schema_id, $avro_schema);
225
226 # FIXME need to use caching w/ TTL to avoid these checks for every call
227
228 # If a schema is supplied...
229 0 ▶ if ($supplied_schema) {
Branch: 1 branch
BranchTrueFalseTotal
if ($supplied_schema) { } 0 0 0
230
231 # If the subject exixts...
232 0 my $subjects = $sr->get_subjects();
233 0 ▶ if (grep(/^$subject$/, @$subjects) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep /^$subject$/, @$subjects) 0 0 0
234
235 # ...check if it already exists in registry
236 0 my $schema_info = $sr->check_schema(
237 SUBJECT => $topic,
238 TYPE => $type,
239 SCHEMA => $supplied_schema
240 );
241 0 ▶ if ( defined $schema_info ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $schema_info) { } 0 0 0
242
243 0 $schema_id = $schema_info->{id};
244 0 $avro_schema = $schema_info->{schema};
245  
246 # ...if it does not already exist in the registry....
247 } else {
248
249 # ...test new schema compliancy against latest version
250 0 my $compliant = $sr->test_schema(
251 SUBJECT => $topic,
252 TYPE => $type,
253 SCHEMA => $supplied_schema
254 );
255 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
256 return undef
257 unless $compliant;
258
259 }
260
261 }
262
263 # ...if a previous id for the schema is not available, try to add the one supplied to the registry....
264 0 ▶ unless ($schema_id) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($schema_id) 0 0 0
265
266 # ...procede adding it to the registry
267 0 $schema_id = $sr->add_schema(
268 SUBJECT => $topic,
269 TYPE => $type,
270 SCHEMA => $supplied_schema
271 );
272 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
273 return undef
274 unless $schema_id;
275
276 # ...and bless new schema into an Avro schema object
277 0 $avro_schema = Avro::Schema->parse($supplied_schema);
278
279 }
280
281 } else {
282
283 # retreive latest schema for the topic value
284 0 my $schema_info = $sr->get_schema(
285 SUBJECT => $topic,
286 TYPE => $type
287 );
288 0 ▶ if ( defined $schema_info ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $schema_info) { } 0 0 0
289
290 0 $schema_id = $schema_info->{id};
291 0 $avro_schema = $schema_info->{schema};
292
293 } else {
294 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
295 return undef
296 unless $schema_info;
297 }
298
299 }
300
301 0 return ($schema_id, $avro_schema);
302 }
303  
304  
305  
306  
307  
308 ##### Public methods
309  
310 =head2 METHODS
311  
312 The following methods are defined for the C<Kafka::Avro::Producer> class:
313  
314 =cut
315  
316  
317 =head3 C<schema_registry>()
318  
319 Returns the L<Confluent::SchemaRegistry|Confluent::SchemaRegistry> instance supplied to the construcor.
320  
321 =cut
322  
323 0 ▶sub schema_registry { $_[0]->{__SCHEMA_REGISTRY} }
Subroutine schema_registry: not called CC=1 SCAR=6.9
Pod documented
324  
325  
326 =head3 C<get_error>()
327  
328 Returns a string containing last error message.
329  
330 =cut
331  
332 0 ▶sub get_error { $_[0]->_get_error() }
Subroutine get_error: not called CC=1 SCAR=6.9
Pod documented
333  
334  
335 =head3 C<send( $topic, $partition, $messages, $keys, $compression_codec, $timestamps, $key_schema, $value_schema )>
336  
337 =head3 C<send( %named_params )>
338  
339 Sends Avro-formatted messages on a L<Kafka::Connection|Kafka::Connection> object.
340  
341 Returns a non-blank value (a reference to a hash with server response description)
342 if the message is successfully sent.
343  
344 In order to handle Avro format, C<Kafka::Producer|Kafka::Producer> C<send()> method is extended
345 with two more positional arguments, C<$key_schema> and C<$value_schema>:
346  
347 $producer->send(
348 $topic, # scalar
349 $partition, # scalar
350 $messages, # scalar | array
351 $keys, # (optional) undef | scalar | array
352 $compression_codec, # (optional) undef | scalar
353 $timestamps, # (optional) undef | scalar | array
354 $key_schema, # (optional) undef | JSON-string
355 $value_schema # (optional) undef | JSON-string
356 );
357  
358 Both C<$key_schema> and C<$value_schema> parameters are optional and must provide JSON strings that
359 represent Avro schemas to use to validate and serialize key(s) and value(s).
360  
361 These schemas are validated against C<$schema_registry> and, if compliant, they are added to the registry
362 under the C<$topic+'key'> or C<$topic+'value'> Schema Registry's subjects.
363  
364 If an expected schema isn't provided, latest version from Schema Registry is used accordingly to the
365 (topic + key/value) subject.
366  
367 Alternatively, for ease of use, the C<send()> method may be also used by suggesting named parameters:
368  
369 $producer->send(
370 topic => $topic, # scalar
371 partition => $partition, # scalar
372 messages => $messages, # scalar | array
373 keys => $keys, # (optional) undef | scalar | array
374 compression_codec => $compression_codec, # (optional) undef | scalar
375 timestamps => $timestamps, # (optional) undef | scalar | array
376 key_schema => $key_schema, # (optional) undef | JSON-string
377 value_schema => $value_schema # (optional) undef | JSON-string
378 );
379  
380 =cut
381  
382 sub send {
383 0 ▶ my $self = shift;
Subroutine send: not called CC=31 SCAR=69.0
Pod documented
384 0 my %params = (
385 'topic' => undef,
386 'partition' => undef,
387 'messages' => undef,
388 'keys' => undef, # optional in Kafka::Producer
389 'compression_codec' => undef, # optional in Kafka::Producer
390 'timestamps' => undef, # optional in Kafka::Producer
391 'key_schema' => undef, # optional in Kafka::Producer::Avro
392 'value_schema' => undef # optional in Kafka::Producer::Avro
393 );
394 0 my @p = @_;
395 0 my $is_positional = 0;
396 0 ▶ if ( scalar(@p) % 2 == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar(@p) % 2 == 0) { } 0 0 0
397 0 ▶ for ( my $i = 0 ; $i < scalar(@p)-1 ; $i += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar(@p) - 1) 0 0 0
398 0 ▶ if (ref($p[$i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $p[$i]) 0 0 0
399 0 $is_positional = 1;
400 0 last;
401 }
402 0 ▶ if ( grep /^$p[$i]$/, keys(%params) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep /^$p[$i]$/, keys %params) 0 0 0
403 0 $params{ $p[$i] } = $p[ $i + 1 ];
404 }
405 }
406 } else {
407 0 $is_positional = 1;
408 }
409 0 ▶ if ($is_positional) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_positional) 0 0 0
410 0 $params{topic} = shift @p;
411 0 $params{partition} = shift @p;
412 0 $params{messages} = shift @p;
413 0 $params{keys} = shift @p;
414 0 $params{compression_codec} = shift @p;
415 0 $params{timestamps} = shift @p;
416 0 $params{key_schema} = shift @p;
417 0 $params{value_schema} = shift @p;
418 }
419 0 my $avro_schemas = {
420 key => {
421 id => undef,
422 schema => undef
423 },
424 value => {
425 id => undef,
426 schema => undef
427 }
428 };
429 0 my ($keys, $messages);
430  
431 0 $self->_clear_error();
432
433 $self->_set_error('Missing topic param')
434 and return undef
435 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
436 $self->_set_error('Missing partition param')
437 and return undef
438 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
439 $self->_set_error('Missing messages')
440 and return undef
441 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
442
443 # Get Avro schema for keys and values
444 0 foreach my $type (qw/key value/) {
445 0 ($avro_schemas->{$type}->{id}, $avro_schemas->{$type}->{schema}) = $self->_get_avro_schema($params{topic}, $type, $params{"${type}_schema"});
446 return undef
447 0 ▶ unless $avro_schemas->{$type}->{id};
Branch: 1 branch
BranchTrueFalseTotal
unless $avro_schemas->{$type}{"id"} 0 0 0
448 }
449  
450 0 ▶ if ($params{keys}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"keys"}) 0 0 0
451
452 # Return if key Avro schema was not found
453 $self->_set_error('No key Avro schema found')
454 and return undef
455 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
456
457 # Avro encoding of keys
458 0 ▶ if (ref($params{keys}) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params{'keys'} eq 'ARRAY') { } 0 0 0
459 $keys = [
460 map {
461 0 _encode($avro_schemas->{key}, $_);
462 0 } @{$params{keys}}
463 ];
464 } else {
465 0 $keys = _encode($avro_schemas->{key}, $params{keys});
466 }
467
468 }
469  
470 # Return if value Avro schema was not found
471 $self->_set_error('No value Avro schema found')
472 and return undef
473 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
474
475 # Avro encoding of messages
476 0 ▶ if (ref($params{messages}) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params{'messages'} eq 'ARRAY') { } 0 0 0
477 $messages = [
478 map {
479 0 _encode($avro_schemas->{value}, $_);
480 0 } @{$params{messages}}
481 ];
482 } else {
483 0 $messages = _encode($avro_schemas->{value}, $params{messages});
484 }
485
486 # Return in case of mismatch between keys and messages
487 0 ▶ if ($avro_schemas->{key}->{id}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($avro_schemas->{"key"}{"id"}) 0 0 0
488 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
489 and return undef
490 if ref($keys) eq 'ARRAY' && ref($messages) ne 'ARRAY';
491 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
492 and return undef
493 if ref($keys) eq 'ARRAY' && ref($messages) eq 'ARRAY' && $#$keys != $#$messages;
494 }
495
496 # Send messages through Kafka::Producer parent class
497 return $self->SUPER::send(
498 $params{topic},
499 $params{partition},
500 $messages,
501 $keys,
502 $params{compression_codec},
503 $params{timestamps}
504 0 );
505
506 }
507  
508  
509 =head3 C<bulk_send( %params )>
510  
511 Similar to C<send> but uses bulks to avoid memory leaking.
512  
513 Extra named parameters are expected:
514  
515 =over 3
516  
517 =item C<size =E<gt> $size>
518  
519 The size of the bulk
520  
521 =item C<on_before_send_bulk =E<gt> sub {...} > (optional)
522  
523 A code block that will be executed before the sending of each bulk.
524  
525 The block will receive the following positional parameters:
526  
527 =over 3
528  
529 =item C<$bulk_num> the number of the bulk
530  
531 =item C<$bulk_messages> the number of messages in the bulk
532  
533 =item C<$bulk_keys> the number of keys in the bulk
534  
535 =item C<$index_from> the absolute index of the first message in the bulk
536  
537 =item C<$index_to> the absolute index of the last message in the bulk
538  
539 =back
540  
541 =item C<on_after_send_bulk =E<gt> sub {...} > (optional)
542  
543 A code block that will be executed after the sending of each bulk.
544  
545 The block will receive the following positional parameters:
546  
547 =over 3
548  
549 =item C<$sent> the number of sent messages in the bulk
550  
551 =item C<$total_sent> the total number of messages sent
552  
553 =back
554  
555 =item C<on_init =E<gt> sub {...} > (optional)
556  
557 A code block that will be executed only once before at the beginning of the cycle.
558  
559 The block will receive the following positional parameters:
560  
561 =over 3
562  
563 =item C<$to_send> the total number of messages to send
564  
565 =item C<$bulk_size> the size of the bulk
566  
567 =back
568  
569 =item C<on_complete =E<gt> sub {...} > (optional)
570  
571 A code block that will be executed only once after the end of the cycle.
572  
573 The block will receive the following positional parameters:
574  
575 =over 3
576  
577 =item C<$to_send> the total number of messages to send
578  
579 =item C<$total_sent> the total number of messages sent
580  
581 =item C<$errors> the number bulks sent with errors
582  
583 =back
584  
585 =item C<on_send_error =E<gt> sub {...} > (optional)
586  
587 A code block that will be executed when a bulk registers an error.
588  
589 =back
590  
591 =cut
592  
593 sub bulk_send {
594 0 ▶ my $self = shift;
Subroutine bulk_send: not called CC=24 SCAR=64.0
Pod documented
595 0 my %params = @_;
596
597 0 $self->_clear_error();
598
599 # If there is no an array of messages use send()
600 return $self->send(%params)
601 0 ▶ unless ref($params{messages}) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $params{"messages"} eq "ARRAY" 0 0 0
602
603 $self->_set_error('Missing bulk size')
604 and return undef
605 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
606 $self->_set_error('Bad bulk size')
607 and return undef
608 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
609  
610 0 my $messages = $params{messages};
611 0 my $keys = $params{keys};
612 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
613 and return undef
614 if ref($keys) eq 'ARRAY' && ref($messages) ne 'ARRAY';
615 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
616 and return undef
617 if ref($keys) eq 'ARRAY' && ref($messages) eq 'ARRAY' && $#$keys != $#$messages;
618
619 0 my @messages = @$messages; # duplicate messages array to preserve external array
620 # Check for key(s) format (ARRAY vs. SCALAR)
621 0 my (@keys, $key);
622 0 ▶ if (ref($params{keys}) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params{'keys'} eq 'ARRAY') { } 0 0 0
623 0 @keys = @$keys;
624 } else {
625 0 $key = $keys;
626 }
627 0 my $message_count = scalar(@messages);
628 0 my $bulk_size = $params{size};
629 0 my $sent = 0;
630 0 my $errors = 0;
631 0 my $bulk_num = 0;
632
633 0 ▶ if (ref($params{on_init}) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params{"on_init"} eq "CODE") 0 0 0
634 0 $params{on_init}->($message_count, $bulk_size);
635 }
636 0 ▶ for (my $i=1; $i<=$message_count; $i+=$bulk_size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $message_count) 0 0 0
637 0 $bulk_num++;
638 0 my @bulk = splice @messages, 0, $bulk_size;
639 0 my $bulk_keys = $key;
640 0 ▶ unless ($bulk_keys) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($bulk_keys) 0 0 0
641 0 $bulk_keys = [ splice(@keys, 0, $bulk_size) ];
642 }
643 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
644 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
645 }
646 my $res = $self->send(
647 'topic' => $params{topic},
648 'partition' => 0,
649 'messages' => [ @bulk ],
650 'keys' => $bulk_keys,
651 'compressione_codec' => undef,
652 'key_schema' => $params{key_schema},
653 'value_schema' => $params{value_schema},
654 'timestamps' => $params{timestamps}
655 0 );
656 0 ▶ if (defined $res) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $res) { } 0 0 0
657 0 $sent += scalar(@bulk);
658 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
659 0 $params{on_after_send_bulk}->(scalar(@bulk), $sent);
660 }
661 } else {
662 0 $errors++;
663 0 ▶ if (ref($params{on_send_error}) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params{"on_send_error"} eq "CODE") 0 0 0
664 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
665 }
666 }
667 }
668 0 ▶ if (ref($params{on_complete}) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params{"on_complete"} eq "CODE") 0 0 0
669 0 $params{on_complete}->($message_count, $sent, $errors);
670 }
671 0 return $sent;
672
673 }
674  
675 =head1 AUTHOR
676  
677 Alvaro Livraghi, E<lt>alvarol@cpan.orgE<gt>
678  
679 =head1 CONTRIBUTE
680  
681 L<https://github.com/alivraghi/Kafka-Producer-Avro>
682  
683 =head1 BUGS
684  
685 Please use GitHub project link above to report problems or contact authors.
686  
687 =head1 COPYRIGHT AND LICENSE
688  
689 Copyright 2018 by Alvaro Livraghi
690  
691 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
692  
693 =cut
694  
695 1 1;
↑ Summary