YARN/ResourceManager/Scheduler/UserApps.pm

filter: Statementstmt 28.3% 32 / 113 Branchbran 0.0% 0 / 58 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 66.6% 10 / 15 Podpod 100% 2 / 2 totaltotal 20.0% 44 / 219 CC 45 SCAR 70.2
CC
45
Cov
17%
collect
1640.0
__ANON__
20.0
__ANON__
6.0
CRAP 1113.8
↑ Summary YARN/Roles/Common.pm »
1 package Net::Hadoop::YARN::ResourceManager::Scheduler::UserApps;
2 2 $Net::Hadoop::YARN::ResourceManager::Scheduler::UserApps::VERSION = '0.203';
3 2 ▶use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use Data::Dumper ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Ref::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Time::Duration ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Net::Hadoop::YARN::ResourceManager;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 has rm_object => (
16 is => 'rw',
17 0 ▶ isa => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
18 0 my $thing = shift;
19 0 my $type = 'Net::Hadoop::YARN::ResourceManager';
20 0 ▶ if ( ! $thing
Branch: 1 branch
BranchTrueFalseTotal
if (not $thing && builtin::blessed($thing) && $thing->isa($type)) 0 0 0
Condition0%0/6
$thing && builtin::blessed($thing) && $thing->isa($type)
!ll&&!rl&&r
000
$thing and builtin::blessed($thing)
!ll&&!rl&&r
000
MC/DC0%0/3
$thing && builtin::blessed($thing) && $thing->isa($type)
$thing builtin::blessed($thing) $thing->isa($type)
Truth table0%0/4
A and B && C
A= $thingB= builtin::blessed($thing)C= $thing->isa($type)
ABCresult
0XX0
10X0
1100
1111
21 || ! Scalar::Util::blessed $thing
22 || ! $thing->isa( $type )
23 ) {
24 0 die "rm_object is not a $type";
25 }
26 },
27 0 ▶ default => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
28 Net::Hadoop::YARN::ResourceManager->new(
29 ( $ENV{YARN_RESOURCE_MANAGER} ? (
30 0 ▶ servers => [ split /,/, $ENV{YARN_RESOURCE_MANAGER} ]
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'YARN_RESOURCE_MANAGER'} ? : 0 0 0
31 ) : () )
32 );
33 },
34 2 );
35  
36 0 ▶sub collect {
Subroutine collect: not called CC=40 SCAR=74.0
Pod documented
37 0 my $self = shift;
38 0 ▶ my $user = shift || die "No user name was specified";
Condition0%0/2
shift() || die('No user name was specified')
l!l
00
MC/DC0%0/1
shift() || die('No user name was specified')
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
39  
40 0 my $apps = $self->rm_object->apps( user => $user );
41  
42 0 ▶ if ( ! Ref::Util::is_arrayref $apps ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (Ref::Util::XS::is_arrayref($apps)) 0 0 0
43 0 ▶ if ( Ref::Util::is_hashref $apps ) {
Branch: 1 branch
BranchTrueFalseTotal
if (Ref::Util::XS::is_hashref($apps)) { } 0 0 0
44 0 ▶ if ( my $check = $apps->{apps} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $check = $apps->{"apps"}) 0 0 0
45 0 ▶ if ( ! keys %{ $check }) {
Branch: 1 branch
BranchTrueFalseTotal
if (not keys %{$check;}) { } 0 0 0
46 0 $apps = [];
47 }
48 else {
49 0 die sprintf "[TODO-1] Don't know what to do with %s",
50 Data::Dumper::Dumper [ $user => $apps ],
51 ;
52 }
53 }
54 }
55 else {
56 0 die sprintf "[TODO-2] Don't know what to do with %s",
57 Data::Dumper::Dumper [ $user => $apps ],
58 ;
59 }
60 }
61  
62 0 ▶ my $format_epoch = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
63 0 ▶ my $epoch = shift || die "No epoch specified!";
Condition0%0/2
shift() || die('No epoch specified!')
l!l
00
MC/DC0%0/1
shift() || die('No epoch specified!')
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
64 0 return POSIX::strftime "%a %b %d %Y %H:%M:%S %Z", localtime $epoch;
65 0 };
66  
67 0 my %apps_by_state;
68  
69 0 foreach my $app ( @{ $apps } ) {
70 0 foreach my $resource ( qw(
71 allocatedMB
72 allocatedVCores
73 ) ) {
74 0 ▶ $app->{ $resource } = 0 if $app->{ $resource } eq '-1';
Branch: 1 branch
BranchTrueFalseTotal
if $app->{$resource} eq -1 0 0 0
75 }
76  
77 0 ▶ if ( $app->{allocatedMB} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($app->{"allocatedMB"}) 0 0 0
78 0 $app->{allocatedMB_fmt} = $self->format_bytes( $app->{allocatedMB} * 1024**2 );
79 }
80  
81 0 ▶ if ( $app->{allocatedVCores} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($app->{"allocatedVCores"}) 0 0 0
82 $app->{allocatedVCores_fmt} = sprintf '%s vCore%s',
83 $app->{allocatedVCores},
84 0 ▶ $app->{allocatedVCores} > 1 ? 's' : '',
Branch: 1 branch
BranchTrueFalseTotal
$app->{'allocatedVCores'} > 1 ? : 0 0 0
85 ;
86 }
87  
88 # TODO
89 # [STRING]"applicationTags"
90 # the value is something like "oozie-59a27f107d250c9822fd45e87fd40db8"
91 # which is not the job id.
92  
93 0 foreach my $hash_or_string ( qw(
94 diagnostics
95 applicationTags
96 )) {
97 0 ▶ next if ! exists $app->{ $hash_or_string };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $app->{$hash_or_string} 0 0 0
98 # This is a bug in the REST layer
99 0 ▶ if ( Ref::Util::is_hashref $app->{ $hash_or_string }
Branch: 1 branch
BranchTrueFalseTotal
if (Ref::Util::XS::is_hashref($app->{$hash_or_string}) and not keys %{$$app{$hash_or_string};}) 0 0 0
Condition0%0/3
Ref::Util::XS::is_hashref($app->{$hash_or_string}) and not keys %{$$app{$hash_or_string};}
!ll&&!rl&&r
000
MC/DC0%0/2
Ref::Util::XS::is_hashref($app->{$hash_or_string}) and not keys %{$$app{$hash_or_string};}
Ref::Util::XS::is_hashref($app->{$hash_or_string}) not keys %{$$app{$hash_or_string};}
Truth table0%0/3
A and not(B)
A= Ref::Util::XS::is_hashref($app->{$hash_or_string})B= not keys %{$$app{$hash_or_string};}
ABresult
0X0
100
111
100 0 && ! keys %{ $app->{ $hash_or_string } }
101 ) {
102 0 $app->{ $hash_or_string } = '';
103 }
104 }
105  
106 # https://www.cloudera.com/documentation/enterprise/latest/topics/cm_dg_yarn_applications.html
107 0 foreach my $duration_field ( qw(
108 vcoreSeconds
109 elapsedTime
110 memorySeconds
111 ) ) {
112 0 ▶ next if ! exists $app->{ $duration_field };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $app->{$duration_field} 0 0 0
113 0 ▶ if ( $app->{ $duration_field } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($app->{$duration_field}) 0 0 0
114 $app->{ $duration_field . '_fmt' } = Time::Duration::duration(
115 $duration_field eq 'elapsedTime'
116 ? $app->{ $duration_field } / 1000
117 0 ▶ : $app->{ $duration_field }
Branch: 1 branch
BranchTrueFalseTotal
$duration_field eq 'elapsedTime' ? : 0 0 0
118 );
119 }
120 }
121  
122 0 foreach my $time_field ( qw(
123 finishedTime
124 startedTime
125 ) ) {
126 0 ▶ next if ! exists $app->{ $time_field };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $app->{$time_field} 0 0 0
127 0 ▶ if ( $app->{ $time_field } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($app->{$time_field}) 0 0 0
128 0 $app->{ $time_field . '_fmt' } = $format_epoch->( $app->{ $time_field } / 1000);
129 }
130 }
131  
132 0 ▶ if ( $app->{name} =~ m{ \Q-oozie-oozi-W\E \z }xms ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($app->{"name"} =~ / \-oozie\-oozi\-W \z /msx) 0 0 0
133 0 ▶ my %name = map { @{ $_ } > 1 ? @{ $_ } : ( $_->[0] => 1 ) }
Branch: 1 branch
BranchTrueFalseTotal
@{$_;} > 1 ? : 0 0 0
134 0 map { [ split m{ [=] }xms, $_, 2 ] }
135 0 split m{ [:] }xms, $app->{name};
136 0 ▶ $name{workflow_name} = delete $name{W} if $name{W};
Branch: 1 branch
BranchTrueFalseTotal
if $name{"W"} 0 0 0
137 0 ▶ $name{action_name} = delete $name{A} if $name{A};
Branch: 1 branch
BranchTrueFalseTotal
if $name{"A"} 0 0 0
138 0 ▶ $name{action_type} = delete $name{T} if $name{T};
Branch: 1 branch
BranchTrueFalseTotal
if $name{"T"} 0 0 0
139 0 ▶ $name{id} = delete $name{ID} if $name{ID};
Branch: 1 branch
BranchTrueFalseTotal
if $name{"ID"} 0 0 0
140 0 $app->{oozie_meta} = \%name;
141 0 ▶ $app->{oozie_id} = $name{id} if $name{id};
Branch: 1 branch
BranchTrueFalseTotal
if $name{"id"} 0 0 0
142 }
143  
144 0 ▶ push @{ $apps_by_state{ $app->{state} } ||= [] }, $app;
Condition0%0/2
$apps_by_state{$app->{"state"}} ||= []
l!l
00
MC/DC0%0/1
$apps_by_state{$app->{"state"}} ||= []
$apps_by_state{$app->{"state"}}
Truth table0%0/2
A
A= $apps_by_state{$app->{"state"}}
Aresult
11
00
145 }
146  
147 0 my %total_res;
148 0 foreach my $app ( @{ $apps_by_state{RUNNING} }) {
149 0 $total_res{allocatedMB} += $app->{allocatedMB};
150 0 $total_res{allocatedVCores} += $app->{allocatedVCores};
151 }
152  
153 0 ▶ if ( $total_res{allocatedMB} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($total_res{"allocatedMB"}) 0 0 0
154 0 $total_res{allocatedMB_fmt} = $self->format_bytes( $total_res{allocatedMB} * 1024**2 );
155 }
156  
157 0 ▶ if ( $total_res{allocatedVCores} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($total_res{"allocatedVCores"}) 0 0 0
158 $total_res{allocatedVCores_fmt} = sprintf '%s vCore%s',
159 $total_res{allocatedVCores},
160 0 ▶ $total_res{allocatedVCores} > 1 ? 's' : '',
Branch: 1 branch
BranchTrueFalseTotal
$total_res{'allocatedVCores'} > 1 ? : 0 0 0
161 ;
162 }
163  
164 0 my @grouped;
165 0 foreach my $ordered_state (qw(
166 RUNNING
167 ACCEPTED
168 FINISHED
169 KILLED
170 FAILED
171 )) {
172 push @grouped, {
173 state => lc( $ordered_state ),
174 state_fmt => ucfirst( lc $ordered_state ),
175 0 ▶ apps => delete( $apps_by_state{$ordered_state} ) || [],
Condition0%0/2
delete $apps_by_state{$ordered_state} || []
l!l
00
MC/DC0%0/1
delete $apps_by_state{$ordered_state} || []
delete $apps_by_state{$ordered_state}
Truth table0%0/2
A
A= delete $apps_by_state{$ordered_state}
Aresult
11
00
176 },
177 }
178  
179 # TODO: possibly needs to be removed if we are sure that the code above
180 # is handling all of the possible states. So, this is a "just in case" part
181 #
182 push @grouped, {
183 state => 'rest',
184 0 apps => [ map { @{ $_ } } values %apps_by_state ],
185 };
186  
187 # Spark jobs are returned like this for whatever reason.
188 0 ▶ if ( my $apps = $grouped[-1]->{apps} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $apps = $grouped[-1]{"apps"}) 0 0 0
189 0 ▶ if ( Ref::Util::is_arrayref $apps && Ref::Util::is_arrayref $apps->[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (Ref::Util::XS::is_arrayref($apps) and Ref::Util::XS::is_arrayref($apps->[0])) 0 0 0
Condition0%0/3
Ref::Util::XS::is_arrayref($apps) and Ref::Util::XS::is_arrayref($apps->[0])
!ll&&!rl&&r
000
MC/DC0%0/2
Ref::Util::XS::is_arrayref($apps) and Ref::Util::XS::is_arrayref($apps->[0])
Ref::Util::XS::is_arrayref($apps) Ref::Util::XS::is_arrayref($apps->[0])
Truth table0%0/3
A and B
A= Ref::Util::XS::is_arrayref($apps)B= Ref::Util::XS::is_arrayref($apps->[0])
ABresult
0X0
100
111
190 0 $grouped[-1]->{apps} = [ @{ $apps->[0] } ];
191 }
192 }
193  
194 return {
195 0 grouped_apps => [ grep { @{ $_->{apps} } > 0 } @grouped ],
196 0 total_apps => scalar @{ $apps },
197 resources => \%total_res,
198 user => $user,
199 };
200 }
201  
202 0 ▶sub format_bytes {
Subroutine format_bytes: not called CC=1 SCAR=6.9
Pod documented
203 0 my $self = shift;
204 0 my $bytes = shift;
205 0 return sprintf '%.2f GB', $bytes / 1024**3;
206 }
207  
208 2 1;
209  
210 __END__
↑ Summary YARN/Roles/Common.pm »