| 1 | # ABSTRACT: The base class for all IO::Storm Bolts. | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | package IO::Storm::Bolt; | ||||||||||||||||||||||||||||
| 4 | 1 | $IO::Storm::Bolt::VERSION = '0.17'; | |||||||||||||||||||||||||||
| 5 | # Imports | ||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use v5.10; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | # Setup Moo for object-oriented niceties | ||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 1 | extends 'IO::Storm::Component'; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | # A boolean indicating whether or not the bolt should automatically | ||||||||||||||||||||||||||||
| 18 | # anchor emits to the incoming tuple ID. Tuple anchoring is how Storm | ||||||||||||||||||||||||||||
| 19 | # provides reliability, you can read more about tuple anchoring in Storm's | ||||||||||||||||||||||||||||
| 20 | # docs: | ||||||||||||||||||||||||||||
| 21 | # https://storm.incubator.apache.org/documentation/Guaranteeing-message-processing.html#what-is-storms-reliability-api | ||||||||||||||||||||||||||||
| 22 | 1 | has 'auto_anchor' => ( | |||||||||||||||||||||||||||
| 23 | is => 'rw', | ||||||||||||||||||||||||||||
| 24 | default => 1 | ||||||||||||||||||||||||||||
| 25 | ); | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | # A boolean indicating whether or not the bolt should automatically | ||||||||||||||||||||||||||||
| 28 | # acknowledge tuples after ``process()`` is called. | ||||||||||||||||||||||||||||
| 29 | 1 | has 'auto_ack' => ( | |||||||||||||||||||||||||||
| 30 | is => 'rw', | ||||||||||||||||||||||||||||
| 31 | default => 1 | ||||||||||||||||||||||||||||
| 32 | ); | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | # A boolean indicating whether or not the bolt should automatically fail | ||||||||||||||||||||||||||||
| 35 | # tuples when an exception occurs when the ``process()`` method is called. | ||||||||||||||||||||||||||||
| 36 | 1 | has 'auto_fail' => ( | |||||||||||||||||||||||||||
| 37 | is => 'rw', | ||||||||||||||||||||||||||||
| 38 | default => 1 | ||||||||||||||||||||||||||||
| 39 | ); | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | # Using a list so Bolt and subclasses can have more than one current_tup | ||||||||||||||||||||||||||||
| 42 | has '_current_tups' => ( | ||||||||||||||||||||||||||||
| 43 | is => 'rw', | ||||||||||||||||||||||||||||
| 44 | 1 | ▶ | default => sub { [] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 45 | 1 | init_arg => undef | |||||||||||||||||||||||||||
| 46 | ); | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 0 | ▶ | sub initialize { | ||||||||||||||||||||||||||
|
Subroutine
initialize: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 49 | 0 | my ( $self, $storm_conf, $context ) = @_; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 0 | ▶ | sub process { | ||||||||||||||||||||||||||
|
Subroutine
process: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 53 | 0 | my ( $self, $tuple ) = @_; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 3 | ▶ | sub emit ($$;$) { | ||||||||||||||||||||||||||
|
Subroutine
emit: called CC=10 SCAR=24.3
Pod
documented
| |||||||||||||||||||||||||||||
| 57 | 3 | my ( $self, $tuple, $args ) = @_; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 3 | ▶ | $args = $args // {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args // {}
MC/DC0%0/1
$args // {}
$args
Truth table50%1/2
A
A= $args
| |||||||||||||||||||||||||||||
| 60 | 3 | my $msg = { command => 'emit', tuple => $tuple }; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 3 | my $anchors = []; | |||||||||||||||||||||||||||
| 63 | 3 | ▶ | if ( $self->auto_anchor ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 3 | ▶ | $anchors = $self->_current_tups // []; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->_current_tups // []
MC/DC0%0/1
$self->_current_tups // []
$self->_current_tups
Truth table50%1/2
A
A= $self->_current_tups
| |||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | 3 | ▶ | unless ( defined( $args->{anchors} ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 2 | $args->{anchors} = $anchors; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 3 | for my $a ( @{ $args->{anchors} } ) { | |||||||||||||||||||||||||||
| 71 | 2 | ▶ | if ( ref($a) eq "IO::Storm::Tuple" ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 0 | $a = $a->id; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | 2 | push( @$anchors, $a ); | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 3 | ▶ | if ( defined( $args->{stream} ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 1 | $msg->{stream} = $args->{stream}; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 3 | ▶ | if ( defined( $args->{direct_task} ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 0 | $msg->{task} = $args->{direct_task}; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 3 | $msg->{anchors} = $anchors; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 3 | $self->send_message($msg); | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 3 | ▶ | if ( defined $msg->{task} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 0 | return $msg->{task}; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | else { | ||||||||||||||||||||||||||||
| 93 | 3 | return $self->read_task_ids(); | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 1 | ▶ | sub ack { | ||||||||||||||||||||||||||
|
Subroutine
ack: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||
| 98 | 1 | my ( $self, $tuple ) = @_; | |||||||||||||||||||||||||||
| 99 | 1 | my $tup_id; | |||||||||||||||||||||||||||
| 100 | 1 | ▶ | if ( ref($tuple) eq "IO::Storm::Tuple" ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 1 | $tup_id = $tuple->id; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | else { | ||||||||||||||||||||||||||||
| 104 | 0 | $tup_id = $tuple; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | 1 | $self->send_message( { command => 'ack', id => $tup_id } ); | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 1 | ▶ | sub fail { | ||||||||||||||||||||||||||
|
Subroutine
fail: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||
| 110 | 1 | my ( $self, $tuple ) = @_; | |||||||||||||||||||||||||||
| 111 | 1 | my $tup_id; | |||||||||||||||||||||||||||
| 112 | 1 | ▶ | if ( ref($tuple) eq "IO::Storm::Tuple" ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 1 | $tup_id = $tuple->id; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | else { | ||||||||||||||||||||||||||||
| 116 | 0 | $tup_id = $tuple; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 1 | $self->send_message( { command => 'fail', id => $tup_id } ); | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 0 | ▶ | sub run { | ||||||||||||||||||||||||||
|
Subroutine
run: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 123 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 124 | 0 | my $tup; | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 0 | my ( $storm_conf, $context ) = $self->read_handshake(); | |||||||||||||||||||||||||||
| 127 | 0 | $self->_setup_component( $storm_conf, $context ); | |||||||||||||||||||||||||||
| 128 | 0 | $self->initialize( $storm_conf, $context ); | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 0 | ▶ | try { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 131 | 0 | while (1) { | |||||||||||||||||||||||||||
| 132 | 0 | $tup = $self->read_tuple(); | |||||||||||||||||||||||||||
| 133 | 0 | $self->_current_tups( [$tup] ); | |||||||||||||||||||||||||||
| 134 | 0 | ▶ | if ( $tup->{task} == -1 && $tup->{stream} eq '__heartbeat' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$tup->{"task"} == -1 and $tup->{"stream"} eq "__heartbeat"
MC/DC0%0/2
$tup->{"task"} == -1 and $tup->{"stream"} eq "__heartbeat"
$tup->{"task"} == -1
$tup->{"stream"} eq "__heartbeat"
Truth table0%0/3
A and B
A= $tup->{"task"} == -1B= $tup->{"stream"} eq "__heartbeat"
| |||||||||||||||||||||||||||||
| 135 | 0 | $self->send_message( { command => 'sync' } ); | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | else { | ||||||||||||||||||||||||||||
| 138 | 0 | $self->process($tup); | |||||||||||||||||||||||||||
| 139 | 0 | ▶ | if ( $self->auto_ack ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | 0 | $self->ack($tup); | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | # reset so that we don't accidentally fail the wrong tuples | ||||||||||||||||||||||||||||
| 146 | # if a successive call to read_tuple fails | ||||||||||||||||||||||||||||
| 147 | 0 | $self->_current_tups( [] ); | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | 0 | ▶ | catch { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 151 | 0 | my $error = $_; | |||||||||||||||||||||||||||
| 152 | 0 | ▶ | if ( scalar( @{ $self->_current_tups } ) == 1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | 0 | $tup = $self->_current_tups->[0]; | |||||||||||||||||||||||||||
| 154 | 0 | ▶ | if ( $self->auto_fail ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | 0 | $self->fail($tup); | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | 0 | $self->log("Bolt encountered exception: $_"); | |||||||||||||||||||||||||||
| 159 | 0 | die("Encounter exception in Bolt: $_"); | |||||||||||||||||||||||||||
| 160 | 0 | }; | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 1 | 1; | |||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | __END__ | ||||||||||||||||||||||||||||