| 1 | # ABSTRACT: The base class for all IO::Storm Bolts. | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | package IO::Storm::Bolt; | ||||||||||||||||||||||||||||
| 4 | 1 | $IO::Storm::Bolt::VERSION = '0.16'; | |||||||||||||||||||||||||||
| 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 | sub initialize { | ||||||||||||||||||||||||||||
| 49 | 0 | ▶ | my ( $self, $storm_conf, $context ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
initialize: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | sub process { | ||||||||||||||||||||||||||||
| 53 | 0 | ▶ | my ( $self, $tuple ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
process: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | sub emit ($$;$) { | ||||||||||||||||||||||||||||
| 57 | 3 | ▶ | my ( $self, $tuple, $args ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
emit: called CC=10 SCAR=24.3
Pod
documented
| |||||||||||||||||||||||||||||
| 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 | sub ack { | ||||||||||||||||||||||||||||
| 98 | 1 | ▶ | my ( $self, $tuple ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
ack: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||
| 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 | sub fail { | ||||||||||||||||||||||||||||
| 110 | 1 | ▶ | my ( $self, $tuple ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
fail: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||
| 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 | sub run { | ||||||||||||||||||||||||||||
| 123 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
run: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 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 | try { | ||||||||||||||||||||||||||||
| 131 | 0 | ▶ | while (1) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 132 | 0 | $tup = $self->read_tuple(); | |||||||||||||||||||||||||||
| 133 | 0 | $self->_current_tups( [$tup] ); | |||||||||||||||||||||||||||
| 134 | 0 | ▶ | if ( $tup->{task} == -1 && $tup->{stream} == '__heartbeat' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$tup->{"task"} == -1 and $tup->{"stream"} == "__heartbeat"
MC/DC0%0/2
$tup->{"task"} == -1 and $tup->{"stream"} == "__heartbeat"
$tup->{"task"} == -1
$tup->{"stream"} == "__heartbeat"
Truth table0%0/3
A and B
A= $tup->{"task"} == -1B= $tup->{"stream"} == "__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 | catch { | ||||||||||||||||||||||||||||
| 151 | 0 | ▶ | my $error = $_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | =pod | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | =head1 NAME | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | IO::Storm::Bolt - The base class for all IO::Storm Bolts. | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | =head1 VERSION | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | version 0.16 | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | =head1 NAME | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | IO::Storm::Bolt - The base class for all IO::Storm Bolts. | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | =head1 VERSION | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | version 0.06 | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | =head1 METHODS | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | =head2 initialize | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | Called immediately after the initial handshake with Storm and before the main | ||||||||||||||||||||||||||||
| 190 | run loop. A good place to initialize connections to data sources. | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | =head2 process | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | Process a single tuple of input. This should be overriden by subclasses | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | =head2 emit | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | Emit a tuple to a stream. | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | :param tuple: the Tuple payload to send to Storm, should contain only | ||||||||||||||||||||||||||||
| 201 | JSON-serializable data. | ||||||||||||||||||||||||||||
| 202 | :type tuple: arrayref | ||||||||||||||||||||||||||||
| 203 | :param stream: the ID of the stream to emit this tuple to. Specify | ||||||||||||||||||||||||||||
| 204 | ``undef`` to emit to default stream. | ||||||||||||||||||||||||||||
| 205 | :type stream: scalar | ||||||||||||||||||||||||||||
| 206 | :param anchors: IDs of the tuples (or the <IO::Storm::Tuple> instances) which | ||||||||||||||||||||||||||||
| 207 | the emitted tuples should be anchored to. If ``auto_anchor`` is | ||||||||||||||||||||||||||||
| 208 | set and you have not specified ``anchors``, ``anchors`` will be | ||||||||||||||||||||||||||||
| 209 | set to the incoming/most recent tuple ID(s). | ||||||||||||||||||||||||||||
| 210 | :type anchors: arrayref | ||||||||||||||||||||||||||||
| 211 | :param direct_task: the task to send the tuple to. | ||||||||||||||||||||||||||||
| 212 | :type direct_task: scalar | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | =head2 ack | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | Acknowledge a tuple. Argument can be either a Tuple or an ID. | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | =head2 fail | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | Fail a tuple. Argument can be either a Tuple or an ID. | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | =head2 run | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | Main run loop for all bolts. | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | Performs initial handshake with Storm and reads tuples handing them off | ||||||||||||||||||||||||||||
| 227 | to subclasses. Any exceptions are caught and logged back to Storm | ||||||||||||||||||||||||||||
| 228 | prior to the Perl process exiting. | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | Subclasses should **not** override this method. | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | =head1 AUTHORS | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | =over 4 | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | =item * | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | Cory G Watson <gphat@cpan.org> | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | =item * | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | Dan Blanchard <dblanchard@ets.org> | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | =back | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | This software is copyright (c) 2014 by Infinity Interactive, Inc. | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 251 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | =head1 AUTHORS | ||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | =over 4 | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | =item * | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | Dan Blanchard <dblanchard@ets.org> | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | =item * | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | Cory G Watson <gphat@cpan.org> | ||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | =back | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | This software is copyright (c) 2014 by Educational Testing Service. | ||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 272 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | =cut | ||||||||||||||||||||||||||||