Storm/Component.pm

filter: Statementstmt 80.0% 112 / 140 Branchbran 40.0% 12 / 30 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 88.4% 23 / 26 Podpod 75.0% 6 / 8 totaltotal 71.5% 153 / 214 CC 18 SCAR 27.5
CC
18
Cov
70%
_setup_component
72.0
read_message
8.2
read_command
3.2
CRAP 15.6
↑ Summary Storm/Spout.pm »
1 # ABSTRACT: The base class for Bolts and Spouts.
2  
3 package IO::Storm::Component;
4 3 $IO::Storm::Component::VERSION = '0.15';
5 # Imports
6 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use IO::Handle qw(autoflush);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Log::Log4perl qw(:easy);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use IO::Storm::Tuple;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 # Setup Moo for object-oriented niceties
17 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 # Setup STDIN/STDOUT/STDERR to use UTF8
21 3 binmode STDERR, ':utf8';
22 3 ▶binmode STDIN, ':encoding(UTF-8)';
Subroutine BEGIN: called CC=1 SCAR=0.0
23 3 binmode STDOUT, ':utf8';
24  
25 has '_pending_commands' => (
26 is => 'rw',
27 5 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
28 3 );
29  
30 has '_pending_taskids' => (
31 is => 'rw',
32 5 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
33 3 );
34  
35 has '_stdin' => (
36 is => 'rw',
37 default => sub {
38 0 ▶ my $io = IO::Handle->new;
Subroutine __ANON__: not called CC=1 SCAR=6.9
39 0 $io->fdopen( fileno(STDIN), 'r' );
40 }
41 3 );
42  
43 3 has 'max_lines' => (
44 is => 'rw',
45 default => 100
46 );
47  
48 3 has 'max_blank_msgs' => (
49 is => 'rw',
50 default => 500
51 );
52  
53 has '_json' => (
54 is => 'rw',
55 3 ▶ default => sub { JSON::XS->new->allow_blessed->convert_blessed }
Subroutine __ANON__: called CC=1 SCAR=0.0
56 3 );
57  
58 3 has '_topology_name' => (
59 is => 'rw',
60 init_args => undef
61 );
62  
63 3 has '_task_id' => (
64 is => 'rw',
65 init_args => undef
66 );
67  
68 3 has '_component_name' => (
69 is => 'rw',
70 init_args => undef
71 );
72  
73 3 has '_debug' => (
74 is => 'rw',
75 init_args => undef
76 );
77  
78 3 has '_storm_conf' => (
79 is => 'rw',
80 init_args => undef
81 );
82  
83 3 has '_context' => (
84 is => 'rw',
85 init_args => undef
86 );
87  
88 3 my $logger = Log::Log4perl->get_logger('storm');
89  
90  
91 sub _setup_component {
92 0 ▶ my ( $self, $storm_conf, $context ) = @_;
Subroutine _setup_component: not called CC=8 SCAR=42.8
93 0 my $conf_is_hash = ref($storm_conf) eq ref {};
94 $self->_topology_name(
95 ( $conf_is_hash && exists( $storm_conf->{'topology.name'} ) )
96 0 ▶ ? $storm_conf->{'topology.name'}
Branch: 1 branch
BranchTrueFalseTotal
$conf_is_hash && exists $storm_conf->{'topology.name'} ? : 0 0 0
Condition0%0/3
$conf_is_hash and exists $storm_conf->{"topology.name"}
!ll&&!rl&&r
000
MC/DC0%0/2
$conf_is_hash and exists $storm_conf->{"topology.name"}
$conf_is_hash exists $storm_conf->{"topology.name"}
Truth table0%0/3
A and B
A= $conf_is_hashB= exists $storm_conf->{"topology.name"}
ABresult
0X0
100
111
97 : ''
98 );
99 0 ▶ $self->_task_id( exists( $context->{taskid} ) ? $context->{taskid} : '' );
Branch: 1 branch
BranchTrueFalseTotal
exists $context->{'taskid'} ? : 0 0 0
100 0 $self->_component_name('');
101 0 ▶ if ( exists( $context->{'task->component'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $context->{"task->component"}) 0 0 0
102 0 my $task_component = $context->{'task->component'};
103 0 ▶ if ( exists( $task_component->{ $self->_task_id } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$task_component{$self->_task_id}) 0 0 0
104 0 $self->_component_name( $task_component->{ $self->_task_id } );
105 }
106 }
107 $self->_debug(
108 ( $conf_is_hash && exists( $storm_conf->{'topology.debug'} ) )
109 0 ▶ ? $storm_conf->{'topology.debug'}
Branch: 1 branch
BranchTrueFalseTotal
$conf_is_hash && exists $storm_conf->{'topology.debug'} ? : 0 0 0
Condition0%0/3
$conf_is_hash and exists $storm_conf->{"topology.debug"}
!ll&&!rl&&r
000
MC/DC0%0/2
$conf_is_hash and exists $storm_conf->{"topology.debug"}
$conf_is_hash exists $storm_conf->{"topology.debug"}
Truth table0%0/3
A and B
A= $conf_is_hashB= exists $storm_conf->{"topology.debug"}
ABresult
0X0
100
111
110 : 0
111 );
112 0 $self->_storm_conf($storm_conf);
113 0 $self->_context($context);
114 }
115  
116  
117 sub read_message {
118 13 ▶ $logger->debug('start read_message');
Subroutine read_message: called CC=6 SCAR=21.0
Pod documented
119 13 my $self = shift;
120 13 my $blank_lines = 0;
121 13 my $message_size = 0;
122 13 my $line = '';
123  
124 13 my @messages = ();
125 13 while (1) {
126 26 $line = $self->_stdin->getline;
127 26 ▶ if ( defined($line) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $line) { } 26 0 26
128 26 $logger->debug("read_message: line=$line");
129 }
130 else {
131 0 $logger->error( "Received EOF while trying to read stdin from "
132 . "Storm, pipe appears to be broken, exiting." );
133 0 exit(1);
134 }
135 26 ▶ if ( $line eq "end\n" ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($line eq "end\n") { } 13 13 26
elsif ($line eq '') { } 0 13 13
elsif ($line eq "\n") { } 0 13 13
136 13 last;
137 }
138 elsif ( $line eq '' ) {
139 0 $logger->error( "Received EOF while trying to read stdin from "
140 . "Storm, pipe appears to be broken, exiting." );
141 0 exit(1);
142 }
143 elsif ( $line eq "\n" ) {
144 0 $blank_lines++;
145 0 ▶ if ( $blank_lines % 1000 == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($blank_lines % 1000 == 0) 0 0 0
146 0 $logger->warn( "While trying to read a command or pending "
147 . "task ID, Storm has instead sent $blank_lines "
148 . "'\\n' messages." );
149 0 next;
150 }
151 }
152 13 chomp($line);
153 13 push( @messages, $line );
154 }
155  
156 13 return $self->_json->decode( join( "\n", @messages ) );
157 }
158  
159 sub read_task_ids {
160 7 ▶ my $self = shift;
Subroutine read_task_ids: called CC=3 SCAR=11.2
Pod undocumented
161  
162 7 ▶ if ( scalar( @{ $self->_pending_taskids } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->_pending_taskids;}) { } 0 7 7
163 0 return shift( @{ $self->_pending_taskids } );
164 }
165 else {
166 7 my $msg = $self->read_message();
167 7 ▶ while ( ref($msg) ne 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $msg ne "ARRAY") 3 7 10
168 3 push( @{ $self->_pending_commands }, $msg );
169 3 $msg = $self->read_message();
170 }
171  
172 7 return $msg;
173 }
174 }
175  
176 sub read_command {
177 3 ▶ my $self = shift;
Subroutine read_command: called CC=3 SCAR=11.5
Pod undocumented
178  
179 3 ▶ if ( @{ $self->_pending_commands } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->_pending_commands;}) { } 2 1 3
180 2 return shift( @{ $self->_pending_commands } );
181 }
182 else {
183 1 my $msg = $self->read_message();
184 1 ▶ while ( ref($msg) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $msg eq "ARRAY") 0 1 1
185 0 push( @{ $self->_pending_taskids }, $msg );
186 0 $msg = $self->read_message();
187 }
188 1 return $msg;
189 }
190 }
191  
192  
193 sub read_tuple {
194 2 ▶ my $self = shift;
Subroutine read_tuple: called CC=1 SCAR=0.0
Pod documented
195 2 $logger->debug('read_tuple');
196  
197 2 my $tupmap = $self->read_command();
198  
199 return IO::Storm::Tuple->new(
200 id => $tupmap->{id},
201 component => $tupmap->{comp},
202 stream => $tupmap->{stream},
203 task => $tupmap->{task},
204 values => $tupmap->{tuple}
205 2 );
206 }
207  
208  
209 sub read_handshake {
210 1 ▶ my $self = shift;
Subroutine read_handshake: called CC=2 SCAR=7.0
Pod documented
211  
212 # TODO: Figure out how to redirect stdout to ensure that print
213 # statements/functions won't crash the Storm Java worker
214  
215 1 autoflush STDOUT 1;
216 1 autoflush STDERR 1;
217  
218 1 my $msg = $self->read_message();
219 $logger->debug(
220 1 ▶ sub { 'Received initial handshake from Storm: ' . Dumper($msg) } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
221  
222 # Write a blank PID file out to the pidDir
223 1 my $pid = $$;
224 1 my $pid_dir = $msg->{pidDir};
225 1 my $filename = $pid_dir . '/' . $pid;
226 1 ▶ open my $fh, '>', $filename
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $filename or die "Cant't write to '${filename}': $!\n" 0 1 1
227 or die "Cant't write to '$filename': $!\n";
228 1 $fh->close;
229 1 $logger->debug("Sending process ID $pid to Storm");
230 1 $self->send_message( { pid => int($pid) } );
231  
232 1 return [ $msg->{conf}, $msg->{context} ];
233 }
234  
235  
236 sub send_message {
237 11 ▶ my ( $self, $msg ) = @_;
Subroutine send_message: called CC=1 SCAR=0.0
Pod documented
238 11 say $self->_json->encode($msg);
239 11 say "end";
240 }
241  
242  
243 sub sync {
244 1 ▶ my $self = shift;
Subroutine sync: called CC=1 SCAR=0.0
Pod documented
245 1 $self->send_message( { command => 'sync' } );
246 }
247  
248  
249 sub log {
250 1 ▶ my ( $self, $message ) = @_;
Subroutine log: called CC=1 SCAR=0.0
Pod documented
251 1 $self->send_message( { command => 'log', msg => $message } );
252 }
253  
254 3 1;
255  
256 __END__
257  
258 =pod
259  
260 =head1 NAME
261  
262 IO::Storm::Component - The base class for Bolts and Spouts.
263  
264 =head1 VERSION
265  
266 version 0.15
267  
268 =head1 METHODS
269  
270 =head2 _setup_component
271  
272 Add helpful instance variables to component after initial handshake with Storm.
273  
274 =head2 read_message
275  
276 Read a message from the ShellBolt. Reads until it finds a "end" line.
277  
278 =head2 read_tuple
279  
280 Turn the incoming Tuple structure into an <IO::Storm::Tuple>.
281  
282 =head2 read_handshake
283  
284 Read and process an initial handshake message from Storm
285  
286 =head2 send_message
287  
288 Send a message to Storm, encoding it as JSON.
289  
290 =head2 sync
291  
292 Send a sync command to Storm.
293  
294 =head2 log
295  
296 Send a log command to Storm
297  
298 =head1 AUTHORS
299  
300 =over 4
301  
302 =item *
303  
304 Dan Blanchard <dblanchard@ets.org>
305  
306 =item *
307  
308 Cory G Watson <gphat@cpan.org>
309  
310 =back
311  
312 =head1 COPYRIGHT AND LICENSE
313  
314 This software is copyright (c) 2014 by Educational Testing Service.
315  
316 This is free software; you can redistribute it and/or modify it under
317 the same terms as the Perl 5 programming language system itself.
318  
319 =cut
↑ Summary Storm/Spout.pm »