MCP/Scheduler/Job.pm

filter: Statementstmt 75.8% 138 / 182 Branchbran 57.1% 32 / 56 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 82.6% 19 / 23 Podpod 0.0% 0 / 10 totaltotal 69.7% 189 / 271 CC 41 SCAR 20.5
CC
41
Cov
71%
fits
16.9
claim_resources
15.4
match_feature
10.1
CRAP 7.7
« MCP/State/Plugin/OSRC.pm ↑ Summary MCP/State.pm »
1 package Tapper::MCP::Scheduler::Job;
2 21 our $AUTHORITY = 'cpan:TAPPER';
3 21 $Tapper::MCP::Scheduler::Job::VERSION = '5.0.9';
4 21 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 21 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 21 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 21 ▶use Safe;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 21 ▶use Tapper::Model;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 21 ▶use Tapper::MCP::Scheduler::ObjectBuilder;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12  
13 21 has id => (is => 'ro');
14 21 has testrun_id => (is => 'ro');
15 21 has queue_id => (is => 'ro');
16 21 has host_id => (is => 'rw'); # needs to be rw because we set the host we actually use
17 21 has prioqueue_seq => (is => 'ro');
18 21 has status => (is => 'ro');
19 21 has auto_rerun => (is => 'ro');
20 21 has created_at => (is => 'ro');
21 21 has updated_at => (is => 'ro');
22  
23 has testrun => (is => 'ro',
24 lazy => 1,
25 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
26 0 my $self = shift;
27 0 return Tapper::Model::model('TestrunDB')->resultset('Testrun')->search({id => $self->testrun_id}, {rows => 1})->first;
28 21 });
29  
30 has requested_features => (is => "ro",
31 lazy => 1,
32 62 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
33 62 my ($self) = shift;
34 62 my @return_feat;
35 62 my $feats = Tapper::Model::model('TestrunDB')->resultset('TestrunRequestedFeature')->search({testrun_id => $self->testrun_id});
36 62 $feats->result_class('DBIx::Class::ResultClass::HashRefInflator');
37 62 ▶ while (my $this_feat = $feats->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $this_feat = $feats->next) 14 62 76
38 14 push @return_feat, $this_feat;
39 }
40 62 return \@return_feat;
41 21 });
42 has requested_hosts => (is => "ro",
43 lazy => 1,
44 81 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
45 81 my ($self) = shift;
46 81 my @return_hosts;
47 81 my $hosts = Tapper::Model::model('TestrunDB')->resultset('TestrunRequestedHost')->search({testrun_id => $self->testrun_id});
48 81 my $obj_builder = Tapper::MCP::Scheduler::ObjectBuilder->instance;
49 81 ▶ while (my $this_host = $hosts->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $this_host = $hosts->next) 58 81 139
50 58 my $host_rs = Tapper::Model::model->resultset('Host')->search({id => $this_host->host->id},{result_class => 'DBIx::Class::ResultClass::HashRefInflator'});
51 58 push @return_hosts, $obj_builder->new_host(%{$host_rs->search({}, {rows => 1})->first});
52 }
53 81 return \@return_hosts;
54 21 });
55 has requested_resources => (is => "ro",
56 lazy => 1,
57 28 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
58 28 my ($self) = shift;
59 28 my $resource_reqs = Tapper::Model::model('TestrunDB')->resultset('TestrunRequestedResource')->search(
60 {testrun_id => $self->testrun_id},{ prefetch => 'alternatives' });
61  
62 28 my @requested_resources;
63 28 ▶ while (my $resource_req = $resource_reqs->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $resource_req = $resource_reqs->next) 0 28 28
64 0 push @requested_resources, $resource_req;
65 }
66  
67 28 return \@requested_resources;
68 21 });
69 has queue => (is => 'ro',
70 lazy => 1,
71 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
72 0 my ($self) = shift;
73 0 my @return_obj;
74 0 my $queue_host = Tapper::Model::model('TestrunDB')->resultset('QueueHost')->search({queue_id => $self->id});
75 0 my $queue = Tapper::Model::model->resultset('Queue')->search({id => $queue_host->queue->id},{result_class => 'DBIx::Class::ResultClass::HashRefInflator'});
76 0 my $obj_builder = Tapper::MCP::Scheduler::ObjectBuilder->instance;
77 0 return $obj_builder->new_queue(%{$queue->search({}, {rows => 1})->first});
78 21 });
79  
80 # ----- scheduler related methods -----
81  
82 58 ▶sub match_host {
Subroutine match_host: called CC=7 SCAR=19.5
Pod undocumented
83 58 my ($self, $free_hosts) = @_;
84  
85 58 foreach my $req_host (@{$self->requested_hosts})
86 {
87 21 ▶ no strict 'refs'; ## no critic (ProhibitNoStrict)
Subroutine BEGIN: called CC=1 SCAR=0.0
88 FREE_HOST:
89 58 foreach my $free_host( map {$_->{host} } @$free_hosts) {
90 141 ▶ if (@{$free_host->queues}){
Branch: 1 branch
BranchTrueFalseTotal
if (@{$free_host->queues;}) 6 135 141
91 QUEUE_CHECK:
92 {
93 6 foreach my $queue(@{$free_host->queues}) {
94 6 ▶ last QUEUE_CHECK if $queue->id == $self->queue_id;
Branch: 1 branch
BranchTrueFalseTotal
if $queue->id == $self->queue_id 4 2 6
95 }
96 2 next FREE_HOST;
97 }
98 }
99 139 ▶ return $free_host if $free_host->name eq $req_host->name;
Branch: 1 branch
BranchTrueFalseTotal
if $free_host->name eq $req_host->name 19 120 139
100 }
101 }
102 39 return;
103 }
104  
105  
106 21 our @functions;
107 21 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=13.0
108 21 my $features = Tapper::Model::model->resultset('HostFeature')->search(
109 {
110 },
111 {
112 columns => [ qw/entry/ ],
113 distinct => 1,
114 });
115  
116 21 ▶ while ( my $feature = $features->next ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $feature = $features->next) 17 21 38
117 17 my $entry = $feature->entry;
118 17 push @functions, "&".$entry;
119 17 my $eval_string = "sub $entry (;\$)";
120 17 $eval_string .= "{
121 my (\$given) = \@_;
122  
123 if (\$given) {
124 # available
125 return \$given eq \$_->{features}->{$entry};
126 } else {
127 return \$_->{features}->{$entry} };
128 }";
129 0 ▶ eval $eval_string; ## no critic
Subroutine cores: called CC=2 SCAR=11.1
Subroutine keyword: not called CC=2 SCAR=11.1
Subroutine mem: called CC=2 SCAR=11.1
Subroutine vendor: not called CC=2 SCAR=11.1
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Branch: 4 branches
BranchTrueFalseTotal
if ($given) { } 0 3 3
if ($given) { } 0 0 0
if ($given) { } 0 33 33
if ($given) { } 0 0 0
130 }
131 21 ▶ if ( not grep {$_ =~ /hostname/} @functions ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not grep({$_ =~ /hostname/;} @Tapper::MCP::Scheduler::Job::functions)) 21 0 21
132 0 ▶ eval '
Subroutine hostname: called CC=2 SCAR=7.4
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if ($given) { } 0 3 3
133 sub hostname (;$) ## no critic (ProhibitSubroutinePrototypes)
134 {
135 my ($given) = @_;
136  
137 if ($given) {
138 # available
139 return $given eq $_->{features}->{hostname};
140 } else {
141 return $_->{features}->{hostname};
142 }
143 }';
144 21 push @functions, "&hostname";
145 }
146 }
147  
148 14 ▶sub match_feature {
Subroutine match_feature: called CC=8 SCAR=23.2
Pod undocumented
149 14 my ($self, $free_hosts) = @_;
150 HOST:
151 14 foreach my $host( @$free_hosts )
152 {
153 # filter out queuebound hosts
154 39 ▶ if (@{$host->{host}->queues}){
Branch: 1 branch
BranchTrueFalseTotal
if (@{$host->{"host"}->queues;}) 0 39 39
155 QUEUE_CHECK:
156 {
157 0 foreach my $queuehost(@{$host->{host}->queues}) {
158 0 ▶ last QUEUE_CHECK if $queuehost->queue->id == $self->queue->id;
Branch: 1 branch
BranchTrueFalseTotal
if $queuehost->queue->id == $self->queue->id 0 0 0
159 }
160 0 next HOST;
161 }
162 }
163  
164 39 $_ = $host;
165 39 my $compartment = Safe->new();
166 39 $compartment->permit(qw(:base_core));
167 39 $compartment->share(@functions);
168  
169 39 foreach my $this_feature( @{$self->requested_features} )
170 {
171 39 my $success = $compartment->reval($this_feature->{feature});
172 39 ▶ print STDERR "Error in TestRequest.fits: ", $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 39 39
173 39 ▶ next HOST if not $success;
Branch: 1 branch
BranchTrueFalseTotal
unless $success 36 3 39
174 }
175 3 return $host->{host};
176 }
177 11 return;
178 }
179  
180 # Tries to acquire resources for job
181 # Returns 2 values
182 # - If all requested resources are available
183 # - Which resources were acquired (array ref) or undef if not possible
184 28 ▶sub claim_resources {
Subroutine claim_resources: called CC=6 SCAR=27.4
Pod undocumented
185 28 my ($self, $free_resources) = @_;
186  
187 28 my %res_lookup;
188 28 $res_lookup{$_->id} = $_ foreach (@$free_resources);
189  
190 28 my @acquire_resources;
191  
192 # Transaction, rollback unless commit is called on this
193 28 my $guard = Tapper::Model->model('TestrunDB')->txn_scope_guard;
194  
195 28 foreach my $res_request (@{$self->requested_resources})
196 {
197 0 my $best_alternative;
198 0 foreach my $res_alternative ($res_request->alternatives)
199 {
200 0 ▶ if (my $resource = $res_lookup{$res_alternative->resource_id})
Branch: 1 branch
BranchTrueFalseTotal
if (my $resource = $res_lookup{$res_alternative->resource_id}) 0 0 0
201 {
202 0 $best_alternative = $resource;
203 0 last;
204 }
205 }
206  
207 0 ▶ return (0,undef) unless defined $best_alternative;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $best_alternative 0 0 0
208  
209 # Remove from lookup so it won't be chose twice.
210 0 delete $res_lookup{$best_alternative->id};
211  
212 # Remember choice for frontends
213 0 $res_request->selected_resource($best_alternative);
214 0 $res_request->update;
215  
216 # Mark as in use
217 0 $best_alternative->used_by_scheduling_id($self->{id});
218 0 $best_alternative->update;
219  
220 0 push @acquire_resources, $best_alternative;
221 }
222  
223 28 $guard->commit;
224  
225 28 return (1, \@acquire_resources);
226 }
227  
228 # Checks whether all testruns that our job depends on have finished.
229 28 ▶sub dependencies_finished {
Subroutine dependencies_finished: called CC=1 SCAR=0.0
Pod undocumented
230 28 my ($self) = @_;
231 28 my $unfinished_dependency = Tapper::Model::model('TestrunDB')->resultset('TestrunDependency')->search({
232 'depender_testrun_id' => $self->testrun_id,
233 'testrun_scheduling.status' => { '!=', 'finished' },
234 },{
235 'join' => { dependee => 'testrun_scheduling' },
236 })->first;
237  
238 28 return !defined($unfinished_dependency);
239 }
240  
241 # Checks a TestrunScheduling against a list of available hosts
242 # returns the matching host
243 81 ▶sub fits {
Subroutine fits: called CC=12 SCAR=28.3
Pod undocumented
244 81 my ($self, $free_hosts) = @_;
245  
246 81 ▶ if (not $free_hosts)
Branch: 3 branches
BranchTrueFalseTotal
if (not $free_hosts) { } 0 81 81
elsif (@{$self->requested_hosts;}) { } 58 23 81
elsif (@{$self->requested_features;}) { } 14 9 23
247 {
248 0 return;
249 }
250 81 elsif (@{$self->requested_hosts})
251 {
252 58 my $host = $self->match_host($free_hosts);
253 58 ▶ if ($host)
Branch: 2 branches
BranchTrueFalseTotal
if ($host) { } 19 39 58
elsif (@{$self->requested_features;}) { } 0 39 39
254 {
255 19 return $host;
256 }
257 39 elsif (@{$self->requested_features})
258 {
259 0 $host = $self->match_feature($free_hosts);
260 0 ▶ return $host if $host;
Branch: 1 branch
BranchTrueFalseTotal
if $host 0 0 0
261 }
262 }
263 23 elsif (@{$self->requested_features}) # but no wanted hostnames
264 {
265 14 my $host = $self->match_feature($free_hosts);
266 14 ▶ return $host if $host;
Branch: 1 branch
BranchTrueFalseTotal
if $host 3 11 14
267 }
268 else # free_hosts but no wanted hostnames and no requested_features
269 {
270 9 foreach my $host (map {$_->{host} } @$free_hosts) {
271 6 ▶ if (@{$host->queues}){
Branch: 1 branch
BranchTrueFalseTotal
if (@{$host->queues;}) { } 0 6 6
272 0 foreach my $queue(@{$host->queues}) {
273 0 ▶ return $host if $queue->id == $self->queue_id;
Branch: 1 branch
BranchTrueFalseTotal
if $queue->id == $self->queue_id 0 0 0
274 }
275 } else {
276 6 return $host;
277 }
278  
279 }
280 }
281 53 return;
282 }
283 21 __PACKAGE__->meta->make_immutable;
284  
285 21 1;
286  
287 __END__
« MCP/State/Plugin/OSRC.pm ↑ Summary MCP/State.pm »