| 1 | # ABSTRACT: The base class for all IO::Storm Bolts. | ||||||||||||
| 2 | |||||||||||||
| 3 | package IO::Storm::Bolt; | ||||||||||||
| 4 | 1 | $IO::Storm::Bolt::VERSION = '0.15'; | |||||||||||
| 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 | |||||||||||||
| 16 | 1 | extends 'IO::Storm::Component'; | |||||||||||
| 17 | |||||||||||||
| 18 | # A boolean indicating whether or not the bolt should automatically | ||||||||||||
| 19 | # anchor emits to the incoming tuple ID. Tuple anchoring is how Storm | ||||||||||||
| 20 | # provides reliability, you can read more about tuple anchoring in Storm's | ||||||||||||
| 21 | # docs: | ||||||||||||
| 22 | # https://storm.incubator.apache.org/documentation/Guaranteeing-message-processing.html#what-is-storms-reliability-api | ||||||||||||
| 23 | 1 | has 'auto_anchor' => ( | |||||||||||
| 24 | is => 'rw', | ||||||||||||
| 25 | default => 1 | ||||||||||||
| 26 | ); | ||||||||||||
| 27 | |||||||||||||
| 28 | # A boolean indicating whether or not the bolt should automatically | ||||||||||||
| 29 | # acknowledge tuples after ``process()`` is called. | ||||||||||||
| 30 | 1 | has 'auto_ack' => ( | |||||||||||
| 31 | is => 'rw', | ||||||||||||
| 32 | default => 1 | ||||||||||||
| 33 | ); | ||||||||||||
| 34 | |||||||||||||
| 35 | # A boolean indicating whether or not the bolt should automatically fail | ||||||||||||
| 36 | # tuples when an exception occurs when the ``process()`` method is called. | ||||||||||||
| 37 | 1 | has 'auto_fail' => ( | |||||||||||
| 38 | is => 'rw', | ||||||||||||
| 39 | default => 1 | ||||||||||||
| 40 | ); | ||||||||||||
| 41 | |||||||||||||
| 42 | # Using a list so Bolt and subclasses can have more than one current_tup | ||||||||||||
| 43 | has '_current_tups' => ( | ||||||||||||
| 44 | is => 'rw', | ||||||||||||
| 45 | 1 | ▶ | default => sub { [] }, | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 46 | 1 | init_arg => undef | |||||||||||
| 47 | ); | ||||||||||||
| 48 | |||||||||||||
| 49 | |||||||||||||
| 50 | sub initialize { | ||||||||||||
| 51 | 0 | ▶ | my ( $self, $storm_conf, $context ) = @_; | ||||||||||
|
Subroutine
initialize: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 52 | } | ||||||||||||
| 53 | |||||||||||||
| 54 | |||||||||||||
| 55 | sub process { | ||||||||||||
| 56 | 0 | ▶ | my ( $self, $tuple ) = @_; | ||||||||||
|
Subroutine
process: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 57 | } | ||||||||||||
| 58 | |||||||||||||
| 59 | |||||||||||||
| 60 | sub emit ($$;$) { | ||||||||||||
| 61 | 3 | ▶ | my ( $self, $tuple, $args ) = @_; | ||||||||||
|
Subroutine
emit: called CC=10 SCAR=24.2
Pod
documented
| |||||||||||||
| 62 | |||||||||||||
| 63 | 3 | ▶ | $args = $args // {}; | ||||||||||
|
Condition50%1/2
$args // {}
MC/DC0%0/1
$args // {}
$args
Truth table50%1/2
A
A= $args
| |||||||||||||
| 64 | 3 | my $msg = { command => 'emit', tuple => $tuple }; | |||||||||||
| 65 | |||||||||||||
| 66 | 3 | my $anchors = []; | |||||||||||
| 67 | 3 | ▶ | if ( $self->auto_anchor ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 68 | 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
| |||||||||||||
| 69 | } | ||||||||||||
| 70 | 3 | ▶ | unless ( defined( $args->{anchors} ) ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 71 | 2 | $args->{anchors} = $anchors; | |||||||||||
| 72 | } | ||||||||||||
| 73 | |||||||||||||
| 74 | 3 | my $a; | |||||||||||
| 75 | 3 | for $a ( @{ $args->{anchors} } ) { | |||||||||||
| 76 | 2 | ▶ | if ( ref($a) eq "IO::Storm::Tuple" ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 77 | 0 | $a = $a->id; | |||||||||||
| 78 | } | ||||||||||||
| 79 | 2 | push( @$anchors, $a ); | |||||||||||
| 80 | } | ||||||||||||
| 81 | |||||||||||||
| 82 | 3 | ▶ | if ( defined( $args->{stream} ) ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 83 | 1 | $msg->{stream} = $args->{stream}; | |||||||||||
| 84 | } | ||||||||||||
| 85 | |||||||||||||
| 86 | 3 | ▶ | if ( defined( $args->{direct_task} ) ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 87 | 0 | $msg->{task} = $args->{direct_task}; | |||||||||||
| 88 | } | ||||||||||||
| 89 | |||||||||||||
| 90 | 3 | $msg->{anchors} = $anchors; | |||||||||||
| 91 | |||||||||||||
| 92 | 3 | $self->send_message($msg); | |||||||||||
| 93 | |||||||||||||
| 94 | 3 | ▶ | if ( defined $msg->{task} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 95 | 0 | return $msg->{task}; | |||||||||||
| 96 | } | ||||||||||||
| 97 | else { | ||||||||||||
| 98 | 3 | return $self->read_task_ids(); | |||||||||||
| 99 | } | ||||||||||||
| 100 | } | ||||||||||||
| 101 | |||||||||||||
| 102 | |||||||||||||
| 103 | sub ack { | ||||||||||||
| 104 | 1 | ▶ | my ( $self, $tuple ) = @_; | ||||||||||
|
Subroutine
ack: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||
| 105 | 1 | my $tup_id; | |||||||||||
| 106 | 1 | ▶ | if ( ref($tuple) eq "IO::Storm::Tuple" ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 107 | 1 | $tup_id = $tuple->id; | |||||||||||
| 108 | } | ||||||||||||
| 109 | else { | ||||||||||||
| 110 | 0 | $tup_id = $tuple; | |||||||||||
| 111 | } | ||||||||||||
| 112 | 1 | $self->send_message( { command => 'ack', id => $tup_id } ); | |||||||||||
| 113 | } | ||||||||||||
| 114 | |||||||||||||
| 115 | |||||||||||||
| 116 | sub fail { | ||||||||||||
| 117 | 1 | ▶ | my ( $self, $tuple ) = @_; | ||||||||||
|
Subroutine
fail: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||
| 118 | 1 | my $tup_id; | |||||||||||
| 119 | 1 | ▶ | if ( ref($tuple) eq "IO::Storm::Tuple" ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 120 | 1 | $tup_id = $tuple->id; | |||||||||||
| 121 | } | ||||||||||||
| 122 | else { | ||||||||||||
| 123 | 0 | $tup_id = $tuple; | |||||||||||
| 124 | } | ||||||||||||
| 125 | |||||||||||||
| 126 | 1 | $self->send_message( { command => 'fail', id => $tup_id } ); | |||||||||||
| 127 | } | ||||||||||||
| 128 | |||||||||||||
| 129 | |||||||||||||
| 130 | sub run { | ||||||||||||
| 131 | 0 | ▶ | my ($self) = @_; | ||||||||||
|
Subroutine
run: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 132 | 0 | my $tup; | |||||||||||
| 133 | |||||||||||||
| 134 | 0 | my ( $storm_conf, $context ) = $self->read_handshake(); | |||||||||||
| 135 | 0 | $self->_setup_component( $storm_conf, $context ); | |||||||||||
| 136 | 0 | $self->initialize( $storm_conf, $context ); | |||||||||||
| 137 | |||||||||||||
| 138 | try { | ||||||||||||
| 139 | 0 | ▶ | while (1) { | ||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||
| 140 | 0 | $tup = $self->read_tuple(); | |||||||||||
| 141 | 0 | $self->_current_tups( [$tup] ); | |||||||||||
| 142 | 0 | $self->process($tup); | |||||||||||
| 143 | 0 | ▶ | if ( $self->auto_ack ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 144 | 0 | $self->ack($tup); | |||||||||||
| 145 | } | ||||||||||||
| 146 | |||||||||||||
| 147 | # reset so that we don't accidentally fail the wrong tuples | ||||||||||||
| 148 | # if a successive call to read_tuple fails | ||||||||||||
| 149 | 0 | $self->_current_tups( [] ); | |||||||||||
| 150 | } | ||||||||||||
| 151 | } | ||||||||||||
| 152 | catch { | ||||||||||||
| 153 | 0 | ▶ | my $error = $_; | ||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||
| 154 | 0 | ▶ | if ( scalar( @{ $self->_current_tups } ) == 1 ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 155 | 0 | $tup = $self->_current_tups->[0]; | |||||||||||
| 156 | 0 | ▶ | if ( $self->auto_fail ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 157 | 0 | $self->fail($tup); | |||||||||||
| 158 | } | ||||||||||||
| 159 | } | ||||||||||||
| 160 | 0 | $self->log("Bolt encountered exception: $_"); | |||||||||||
| 161 | 0 | die("Encounter exception in Bolt: $_"); | |||||||||||
| 162 | 0 | }; | |||||||||||
| 163 | } | ||||||||||||
| 164 | |||||||||||||
| 165 | 1 | 1; | |||||||||||
| 166 | |||||||||||||
| 167 | __END__ | ||||||||||||
| 168 | |||||||||||||
| 169 | =pod | ||||||||||||
| 170 | |||||||||||||
| 171 | =head1 NAME | ||||||||||||
| 172 | |||||||||||||
| 173 | IO::Storm::Bolt - The base class for all IO::Storm Bolts. | ||||||||||||
| 174 | |||||||||||||
| 175 | =head1 VERSION | ||||||||||||
| 176 | |||||||||||||
| 177 | version 0.15 | ||||||||||||
| 178 | |||||||||||||
| 179 | =head1 NAME | ||||||||||||
| 180 | |||||||||||||
| 181 | IO::Storm::Bolt - The base class for all IO::Storm Bolts. | ||||||||||||
| 182 | |||||||||||||
| 183 | =head1 VERSION | ||||||||||||
| 184 | |||||||||||||
| 185 | version 0.06 | ||||||||||||
| 186 | |||||||||||||
| 187 | =head1 METHODS | ||||||||||||
| 188 | |||||||||||||
| 189 | =head2 initialize | ||||||||||||
| 190 | |||||||||||||
| 191 | Called immediately after the initial handshake with Storm and before the main | ||||||||||||
| 192 | run loop. A good place to initialize connections to data sources. | ||||||||||||
| 193 | |||||||||||||
| 194 | =head2 process | ||||||||||||
| 195 | |||||||||||||
| 196 | Process a single tuple of input. This should be overriden by subclasses | ||||||||||||
| 197 | |||||||||||||
| 198 | =head2 emit | ||||||||||||
| 199 | |||||||||||||
| 200 | Emit a tuple to a stream. | ||||||||||||
| 201 | |||||||||||||
| 202 | :param tuple: the Tuple payload to send to Storm, should contain only | ||||||||||||
| 203 | JSON-serializable data. | ||||||||||||
| 204 | :type tuple: arrayref | ||||||||||||
| 205 | :param stream: the ID of the stream to emit this tuple to. Specify | ||||||||||||
| 206 | ``undef`` to emit to default stream. | ||||||||||||
| 207 | :type stream: scalar | ||||||||||||
| 208 | :param anchors: IDs of the tuples (or the <IO::Storm::Tuple> instances) which | ||||||||||||
| 209 | the emitted tuples should be anchored to. If ``auto_anchor`` is | ||||||||||||
| 210 | set and you have not specified ``anchors``, ``anchors`` will be | ||||||||||||
| 211 | set to the incoming/most recent tuple ID(s). | ||||||||||||
| 212 | :type anchors: arrayref | ||||||||||||
| 213 | :param direct_task: the task to send the tuple to. | ||||||||||||
| 214 | :type direct_task: scalar | ||||||||||||
| 215 | |||||||||||||
| 216 | =head2 ack | ||||||||||||
| 217 | |||||||||||||
| 218 | Acknowledge a tuple. Argument can be either a Tuple or an ID. | ||||||||||||
| 219 | |||||||||||||
| 220 | =head2 fail | ||||||||||||
| 221 | |||||||||||||
| 222 | Fail a tuple. Argument can be either a Tuple or an ID. | ||||||||||||
| 223 | |||||||||||||
| 224 | =head2 run | ||||||||||||
| 225 | |||||||||||||
| 226 | Main run loop for all bolts. | ||||||||||||
| 227 | |||||||||||||
| 228 | Performs initial handshake with Storm and reads tuples handing them off | ||||||||||||
| 229 | to subclasses. Any exceptions are caught and logged back to Storm | ||||||||||||
| 230 | prior to the Perl process exiting. | ||||||||||||
| 231 | |||||||||||||
| 232 | Subclasses should **not** override this method. | ||||||||||||
| 233 | |||||||||||||
| 234 | =head1 AUTHORS | ||||||||||||
| 235 | |||||||||||||
| 236 | =over 4 | ||||||||||||
| 237 | |||||||||||||
| 238 | =item * | ||||||||||||
| 239 | |||||||||||||
| 240 | Cory G Watson <gphat@cpan.org> | ||||||||||||
| 241 | |||||||||||||
| 242 | =item * | ||||||||||||
| 243 | |||||||||||||
| 244 | Dan Blanchard <dblanchard@ets.org> | ||||||||||||
| 245 | |||||||||||||
| 246 | =back | ||||||||||||
| 247 | |||||||||||||
| 248 | =head1 COPYRIGHT AND LICENSE | ||||||||||||
| 249 | |||||||||||||
| 250 | This software is copyright (c) 2014 by Infinity Interactive, Inc. | ||||||||||||
| 251 | |||||||||||||
| 252 | This is free software; you can redistribute it and/or modify it under | ||||||||||||
| 253 | the same terms as the Perl 5 programming language system itself. | ||||||||||||
| 254 | |||||||||||||
| 255 | =head1 AUTHORS | ||||||||||||
| 256 | |||||||||||||
| 257 | =over 4 | ||||||||||||
| 258 | |||||||||||||
| 259 | =item * | ||||||||||||
| 260 | |||||||||||||
| 261 | Dan Blanchard <dblanchard@ets.org> | ||||||||||||
| 262 | |||||||||||||
| 263 | =item * | ||||||||||||
| 264 | |||||||||||||
| 265 | Cory G Watson <gphat@cpan.org> | ||||||||||||
| 266 | |||||||||||||
| 267 | =back | ||||||||||||
| 268 | |||||||||||||
| 269 | =head1 COPYRIGHT AND LICENSE | ||||||||||||
| 270 | |||||||||||||
| 271 | This software is copyright (c) 2014 by Educational Testing Service. | ||||||||||||
| 272 | |||||||||||||
| 273 | This is free software; you can redistribute it and/or modify it under | ||||||||||||
| 274 | the same terms as the Perl 5 programming language system itself. | ||||||||||||
| 275 | |||||||||||||
| 276 | =cut | ||||||||||||