blib/lib/MapReduce/Framework/Simple.pm

filter: Statementstmt 34.5% 78 / 226 Branchbran 19.7% 13 / 66 Conditioncond 15.3% 2 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 57.1% 12 / 21 Podpod 100% 4 / 4 totaltotal 32.2% 109 / 338 SCAR 66.7
CC
47
Cov
30%
create_assigned_data
156.0
map_reduce
116.5
worker
30.0
CRAP 788.8
↑ Summary Build »
1 package MapReduce::Framework::Simple;
2 6 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use B::Deparse;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use Data::MessagePack;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Parallel::ForkManager;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use Plack::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use WWW::Mechanize;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use List::Util qw(shuffle);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 6 our $VERSION = "0.07";
14  
15 6 has 'verify_hostname' => (is => 'rw', isa => 'Int', default => 1);
16 6 has 'skip_undef_result' => (is => 'rw', isa => 'Int', default => 1);
17 6 has 'warn_discarded_data' => (is => 'rw', isa => 'Int', default => 1);
18 6 has 'die_discarded_data' => (is => 'rw', isa => 'Int', default => 0);
19 6 has 'worker_log' => (is => 'rw', isa => 'Int', default => 0);
20 6 has 'force_plackup' => (is => 'rw', isa => 'Int', default => 0);
21  
22 # To make load balanced data.
23 sub create_assigned_data {
24 0 ▶ my $self = shift;
Subroutine create_assigned_data: not called CC=12 SCAR=50.5
Pod documented
25 0 my $data = shift;
26 0 my $servers = shift;
27 0 my $options = shift;
28 0 my $chunk_num = 10;
29 0 my $method = 'volume_uniform';
30 0 ▶ if(defined($options)){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options) 0 0 0
31 0 ▶ if(defined($options->{chunk_num})){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options->{"chunk_num"}) 0 0 0
32 0 $chunk_num = $options->{chunk_num};
33 }
34 0 ▶ if(defined($options->{method})){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options->{"method"}) 0 0 0
35 0 $method = $options->{method};
36 }
37 }
38 0 my $output;
39 0 ▶ if($method eq 'element_shuffle'){
Branch: 2 branches
BranchTrueFalseTotal
if ($method eq 'element_shuffle') { } 0 0 0
elsif ($method eq 'element_sequential') { } 0 0 0
40  
41 0 @$data = shuffle(@$data);
42 0 for(0 .. $#$data){
43 0 push(@{$output->[$_ % $chunk_num]->[0]},$data->[$_]);
44 }
45 0 for(0 .. $#$output){
46 0 $output->[$_]->[1] = $servers->[$_ % scalar(@$servers)];
47 }
48 }elsif($method eq 'element_sequential'){
49 0 for(0 .. $#$data){
50 0 push(@{$output->[$_ % $chunk_num]->[0]},$data->[$_]);
51 }
52 0 for(0 .. $#$output){
53 0 $output->[$_]->[1] = $servers->[$_ % scalar(@$servers)];
54 }
55 }else{
56 0 my $mp = Data::MessagePack->new();
57 @$data = map $_->[0],
58 0 sort {$a->[1] <=> $b->[1]} map [$_, bytes::length $mp->pack($_)],
59 @$data;
60 0 for(0 .. $#$data){
61 0 push(@{$output->[$_ % $chunk_num]->[0]},$data->[$_]);
62 }
63 0 for(0 .. $#$output){
64 0 $output->[$_]->[1] = $servers->[$_ % scalar(@$servers)];
65 }
66 }
67 0 return($output);
68 }
69  
70 # MapReduce client(Master)
71 sub map_reduce {
72 5 ▶ my $self = shift;
Subroutine map_reduce: called CC=22 SCAR=47.6
Pod documented
73 5 my $data = shift;
74 5 my $mapper_ref = shift;
75 5 my $reducer_ref = shift;
76 5 my $max_proc = shift;
77 5 my $options = shift;
78 5 my $remote_flg = 1;
79 5 ▶ if(defined($options) and defined($options->{remote})){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options and defined $options->{"remote"}) 5 0 5
Condition33%1/3
defined $options and defined $options->{"remote"}
!ll&&!rl&&r
005
MC/DC0%0/2
defined $options and defined $options->{"remote"}
defined $options defined $options->{"remote"}
Truth table33%1/3
A and B
A= defined $optionsB= defined $options->{"remote"}
ABresult
0X0
100
111
80 5 $remote_flg = $options->{remote};
81 }
82 5 my $stringified_code = B::Deparse->new->coderef2text($mapper_ref);
83 5 my $result;
84 my $succeeded_remotes;
85 5 my $failed_remotes;
86 5 my $failed_data;
87 5 my $discarded_data;
88 5 my $pm = Parallel::ForkManager->new($max_proc);
89 $pm->run_on_finish(
90 sub {
91 4 ▶ my ($pid, $exit_code, $ident, $exit_signal, $core_dump, $data_structure) = @_;
Subroutine __ANON__: called CC=3 SCAR=13.0
92 4 ▶ if (defined $data_structure) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data_structure) 4 0 4
93 4 ▶ if($data_structure->{is_success} == 1){
Branch: 1 branch
BranchTrueFalseTotal
if ($data_structure->{'is_success'} == 1) { } 4 0 4
94 4 $succeeded_remotes->{$data_structure->{remote}} = 1;
95 4 $result->[$data_structure->{id}] = $data_structure->{result};
96 }else{
97 0 $failed_remotes->{$data_structure->{remote}} = 1;
98 0 push(@$failed_data,$data_structure->{failed_data});
99 0 $result->[$data_structure->{id}] = undef;
100 }
101 }
102 }
103 5 );
104 5 ▶ if($remote_flg == 1){
Branch: 1 branch
BranchTrueFalseTotal
if ($remote_flg == 1) { } 0 5 5
105 0 ▶ for(my $k=0; $k <= $#$data; $k++){
Branch: 1 branch
BranchTrueFalseTotal
if ($k <= $data->$#*) 0 0 0
106 0 ▶ $pm->start and next;
Branch: 1 branch
BranchTrueFalseTotal
$pm->start and next 0 0 0
107 0 my $payload = _perl_to_msgpack(
108 {
109 data => $data->[$k]->[0],
110 code => $stringified_code
111 }
112 );
113 0 my $result_chil_from_remote = _post_content(
114 $data->[$k]->[1],
115 'application/x-msgpack; charset=x-user-defined',
116 $payload,
117 $self->verify_hostname
118 );
119 0 my $result_with_id;
120 0 ▶ if($result_chil_from_remote->{is_success}){
Branch: 1 branch
BranchTrueFalseTotal
if ($result_chil_from_remote->{'is_success'}) { } 0 0 0
121 0 my $result_chil = _msgpack_to_perl($result_chil_from_remote->{res});
122 0 $result_with_id = {id => $k, result => $result_chil->{result}, remote => $data->[$k]->[1], is_success => 1};
123 }else{
124 0 $result_with_id = {id => $k, remote => $data->[$k]->[0], is_success => 0, failed_data => $data->[$k]};
125 }
126 0 $pm->finish(0,$result_with_id);
127 }
128 }else{
129 5 ▶ for(my $k=0; $k <= $#$data; $k++){
Branch: 1 branch
BranchTrueFalseTotal
if ($k <= $data->$#*) 14 1 15
130 14 ▶ $pm->start and next;
Branch: 1 branch
BranchTrueFalseTotal
$pm->start and next 10 4 14
131 4 my $result_chil = $mapper_ref->($data->[$k]);
132 4 my $result_with_id = {id => $k, result => $result_chil, is_success => 1, remote => 'LOCAL'};
133 4 $pm->finish(0,$result_with_id);
134 }
135 }
136 1 $pm->wait_all_children;
137 1 my $result_failover;
138 1 ▶ if($remote_flg == 1 and $#$failed_data >= 0){
Branch: 1 branch
BranchTrueFalseTotal
if ($remote_flg == 1 and $failed_data->$#* >= 0) 0 1 1
Condition33%1/3
$remote_flg == 1 and $failed_data->$#* >= 0
!ll&&!rl&&r
100
MC/DC0%0/2
$remote_flg == 1 and $failed_data->$#* >= 0
$remote_flg == 1 $failed_data->$#* >= 0
Truth table33%1/3
A and B
A= $remote_flg == 1B= $failed_data->$#* >= 0
ABresult
0X0
100
111
139 0 my @succeeded_remotes_list;
140 0 foreach my $key (keys %$succeeded_remotes){
141 0 push(@succeeded_remotes_list,$key);
142 }
143 0 my $pm2 = Parallel::ForkManager->new($max_proc);
144 $pm2->run_on_finish(
145 sub {
146 0 ▶ my ($pid, $exit_code, $ident, $exit_signal, $core_dump, $data_structure) = @_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
147 0 ▶ if (defined $data_structure) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data_structure) 0 0 0
148 0 ▶ if($data_structure->{is_success} == 1){
Branch: 1 branch
BranchTrueFalseTotal
if ($data_structure->{'is_success'} == 1) { } 0 0 0
149 0 $succeeded_remotes->{$data_structure->{remote}} = 1;
150 0 $result_failover->[$data_structure->{id}] = $data_structure->{result};
151 }else{
152 0 $failed_remotes->{$data_structure->{remote}} = 1;
153 0 push(@$discarded_data,$data_structure->{failed_data});
154 0 $result_failover->[$data_structure->{id}] = undef;
155 }
156 }
157 }
158 0 );
159  
160 0 ▶ for(my $k=0; $k <= $#$failed_data; $k++){
Branch: 1 branch
BranchTrueFalseTotal
if ($k <= $failed_data->$#*) 0 0 0
161 0 ▶ $pm2->start and next;
Branch: 1 branch
BranchTrueFalseTotal
$pm2->start and next 0 0 0
162 0 my $payload = _perl_to_msgpack(
163 {
164 data => $failed_data->[$k]->[0],
165 code => $stringified_code
166 }
167 );
168 0 my $rand_remote = $succeeded_remotes_list[int(rand($#succeeded_remotes_list))];
169 0 my $result_chil_from_remote = _post_content(
170 $rand_remote,
171 'application/x-msgpack; charset=x-user-defined',
172 $payload,
173 $self->verify_hostname
174 );
175 0 my $result_with_id;
176 0 ▶ if($result_chil_from_remote->{is_success}){
Branch: 1 branch
BranchTrueFalseTotal
if ($result_chil_from_remote->{'is_success'}) { } 0 0 0
177 0 my $result_chil = _msgpack_to_perl($result_chil_from_remote->{res});
178 0 $result_with_id = {id => $#$data + $k, result => $result_chil->{result}, remote => $rand_remote, is_success => 1};
179 }else{
180 0 $result_with_id = {id => $#$data + $k, remote => $rand_remote, is_success => 0, failed_data => $failed_data->[$k]};
181 }
182 0 $pm2->finish(0,$result_with_id);
183 }
184 0 $pm2->wait_all_children;
185 }
186 1 my $result_merged;
187 1 push(@$result_merged,@$result);
188 1 ▶ if($#$result_failover >= 0){
Branch: 1 branch
BranchTrueFalseTotal
if ($result_failover->$#* >= 0) 0 1 1
189 0 push(@$result_merged,@$result_failover);
190 }
191 1 ▶ if($#$discarded_data >= 0){
Branch: 1 branch
BranchTrueFalseTotal
if ($discarded_data->$#* >= 0) 0 1 1
192 0 ▶ if($self->die_discarded_data == 1){
Branch: 2 branches
BranchTrueFalseTotal
if ($self->die_discarded_data == 1) { } 0 0 0
elsif ($self->warn_discarded_data == 1) { } 0 0 0
193 0 die "Fatal: Discarded data exist due to remote server couldn't process requested data.\n";
194 }elsif($self->warn_discarded_data == 1){
195 0 warn "Warning: Discarded data exist.\n";
196 }
197 }
198 1 ▶ if($self->skip_undef_result == 1){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->skip_undef_result == 1) { } 1 0 1
199 1 my $result_skip;
200 1 for(0 .. $#$result_merged){
201 4 ▶ if(defined($result_merged->[$_])){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $result_merged->[$_]) 4 0 4
202 4 push(@$result_skip,$result_merged->[$_]);
203 }
204 }
205 1 return($reducer_ref->($result_skip));
206 }else{
207 0 return($reducer_ref->($result_merged));
208 }
209 }
210  
211 sub worker {
212 0 ▶ my $self = shift;
Subroutine worker: not called CC=5 SCAR=34.0
Pod documented
213 0 my $path = shift;
214 0 my $worker = shift;
215 0 my $port = shift;
216 0 ▶ unless(defined($worker)){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $worker) 0 0 0
217 0 $worker = 4;
218 }
219 0 ▶ unless(defined($port)){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $port) 0 0 0
220 0 $port = 5000;
221 }
222 0 my $rc = eval{
223 0 require Plack::Handler::Starlet;
224 0 1;
225 };
226 0 ▶ if($rc and $self->force_plackup == 0){
Branch: 1 branch
BranchTrueFalseTotal
if ($rc and $self->force_plackup == 0) { } 0 0 0
Condition0%0/3
$rc and $self->force_plackup == 0
!ll&&!rl&&r
000
MC/DC0%0/2
$rc and $self->force_plackup == 0
$rc $self->force_plackup == 0
Truth table0%0/3
A and B
A= $rcB= $self->force_plackup == 0
ABresult
0X0
100
111
227 0 print "Starting MapReduce Framework Worker by Starlet\n";
228 0 print "Path: $path\nPort: $port\n";
229 0 my $app = $self->load_worker_plack_app($path);
230 0 my $handler = Plack::Handler::Starlet->new(
231 max_workers => $worker,
232 port => $port
233 );
234 0 $handler->run($app);
235 }else{
236 0 require Plack::Runner;
237 0 my $runner = Plack::Runner->new;
238 0 print "Starting MapReduce Framework Worker by plackup. The number of workers will be ignored.\n";
239 0 print "Path: $path\nPort: $port\n";
240 0 my $app = $self->load_worker_plack_app($path);
241 0 $runner->parse_options('--port',$port);
242 0 $runner->run($app);
243 }
244 }
245  
246 sub load_worker_plack_app {
247 0 ▶ my $self = shift;
Subroutine load_worker_plack_app: not called CC=1 SCAR=6.9
Pod documented
248 0 my $path = shift;
249 my $app = sub {
250 0 ▶ my $env = shift;
Subroutine __ANON__: not called CC=4 SCAR=30.0
251 0 my $req = Plack::Request->new($env);
252 0 ▶ if($self->worker_log == 1){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->worker_log == 1) 0 0 0
253 0 print "REQ,$$,".$req->address.',';
254 0 my @tar = localtime(time());
255 0 printf(
256 "%04d-%02d-%02d %02d:%02d:%02d",
257 $tar[5]+1900,$tar[4]+1,$tar[3],$tar[2],$tar[1],$tar[0]
258 );
259 0 print "\n";
260 }
261 my $response = {
262 $path => sub {
263 0 ▶ my $msg_req = $req->content //
Subroutine __ANON__: not called CC=3 SCAR=24.8
Condition0%0/2
$req->content // (return [400, ["Content-Type", "text/plain"], ["Content body required."]])
l!l
00
MC/DC0%0/1
$req->content // (return [400, ["Content-Type", "text/plain"], ["Content body required."]])
$req->content
Truth table0%0/2
A
A= $req->content
Aresult
11
00
264 return [400,['Content-Type' => 'text/plain'],['Content body required.']];
265 0 ▶ my $perl_req = _msgpack_to_perl($msg_req) //
Condition0%0/2
_msgpack_to_perl($msg_req) // (return [400, ["Content-Type", "text/plain"], ["Valid MessagePack required"]])
l!l
00
MC/DC0%0/1
_msgpack_to_perl($msg_req) // (return [400, ["Content-Type", "text/plain"], ["Valid MessagePack required"]])
_msgpack_to_perl($msg_req)
Truth table0%0/2
A
A= _msgpack_to_perl($msg_req)
Aresult
11
00
266 return [400,['Content-Type' => 'text/plain'],['Valid MessagePack required']];
267 0 my $data = $perl_req->{data};
268 0 my $code_text = $perl_req->{code};
269 0 my $code_ref;
270 0 eval('$code_ref = sub '.$code_text.';');
271 0 my $result = $code_ref->($data);
272 0 return [200,['Content-Type' => 'application/x-msgpack; charset=x-user-defined'],[_perl_to_msgpack({result => $result})]];
273 }
274 0 };
275 0 ▶ if($self->worker_log == 1){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->worker_log == 1) 0 0 0
276 0 print "END,$$,".$req->address.',';
277 0 my @tar = localtime(time());
278 0 printf(
279 "%04d-%02d-%02d %02d:%02d:%02d",
280 $tar[5]+1900,$tar[4]+1,$tar[3],$tar[2],$tar[1],$tar[0]
281 );
282 0 print "\n";
283 }
284 0 ▶ if(defined($response->{$env->{PATH_INFO}})){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$response{$env->{'PATH_INFO'}}) { } 0 0 0
285 0 return $response->{$env->{PATH_INFO}}->();
286 }else{
287 0 return [404,['Content-Type' => 'text/plain'],['Not Found']];
288 }
289  
290 0 };
291 0 return($app);
292 }
293  
294  
295 sub _post_content {
296 0 ▶ my $url = shift;
Subroutine _post_content: not called CC=2 SCAR=17.9
297 0 my $content_type = shift;
298 0 my $data = shift;
299 0 my $ssl_opt = shift;
300 0 my $ua = WWW::Mechanize->new(
301 ssl_opts => {
302 verify_hostname => $ssl_opt
303 }
304 );
305 0 my $is_success = 1;
306 0 eval{
307 0 $ua->post($url,'Content-Type' => $content_type, Content => $data);
308 };
309 0 ▶ if($@){
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
310 0 $is_success = 0;
311 }
312 0 my $res = $ua->content();
313 0 return {res => $res, is_success => $is_success};
314 }
315  
316 sub _perl_to_msgpack {
317 0 ▶ my $data = shift;
Subroutine _perl_to_msgpack: not called CC=1 SCAR=6.9
318 0 my $msgpack = Data::MessagePack->new();
319 0 my $packed = $msgpack->pack($data);
320 0 return($packed);
321 }
322  
323 sub _msgpack_to_perl {
324 0 ▶ my $msg_text = shift;
Subroutine _msgpack_to_perl: not called CC=1 SCAR=6.9
325 0 my $msgpack = Data::MessagePack->new();
326 0 my $unpacked = $msgpack->unpack($msg_text);
327 0 return($unpacked);
328 }
329  
330  
331  
332 6 __PACKAGE__->meta->make_immutable();
333  
334 6 1;
335 __END__
↑ Summary Build »