MCP/Scheduler/Controller.pm

filter: Statementstmt 98.2% 114 / 116 Branchbran 84.2% 32 / 38 Conditioncond 75.0% 9 / 12 MC/DCmcdc 50.0% 4 / 8 Subroutinesub 100% 17 / 17 Podpod 28.5% 2 / 7 totaltotal 89.9% 178 / 198 CC 28 SCAR 22.8
CC
28
Cov
93%
get_next_job
19.3
toggle_bandwith_color
6.0
official_queuelist
3.0
CRAP 9.8
« MCP/Config.pm ↑ Summary MCP/State/Plugin/OSRC.pm »
1 ## no critic (RequireUseStrict)
2 package Tapper::MCP::Scheduler::Controller;
3 15 our $AUTHORITY = 'cpan:TAPPER';
4 # ABSTRACT: Main class of the scheduler
5 15 $Tapper::MCP::Scheduler::Controller::VERSION = '5.0.9';
6 15 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 15 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 15 ▶use base "Tapper::Base";
Subroutine BEGIN: called CC=1 SCAR=0.0
9 15 ▶use Tapper::Model 'model';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 15 ▶use aliased 'Tapper::MCP::Scheduler::Algorithm';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 15 ▶use aliased 'Tapper::MCP::Scheduler::PrioQueue';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 15 ▶use Tapper::MCP::Net;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 15 ▶use Tapper::MCP::Scheduler::ObjectBuilder;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15  
16 15 has hostlist => (is => 'rw', isa => 'ArrayRef');
17 has algorithm => (is => 'rw',
18 isa => 'Tapper::MCP::Scheduler::Algorithm',
19 4 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
20 4 Algorithm->new_with_traits
21 (
22 traits => ['Tapper::MCP::Scheduler::Algorithm::WFQ']
23 );
24 }
25 15 );
26  
27  
28 15 has testrun => (is => 'rw');
29 11 ▶has cfg => (is => 'ro', default => sub {{}});
Subroutine __ANON__: called CC=1 SCAR=0.0
30  
31 15 with "Tapper::MCP::Net::TAP";
32  
33  
34  
35 sub free_hosts_with_features
36 47 ▶{
Subroutine free_hosts_with_features: called CC=2 SCAR=6.9
Pod undocumented
37 47 my $hosts = model('TestrunDB')->resultset("Host")->search({active => 1, free => 1});
38 47 $hosts->result_class('DBIx::Class::ResultClass::HashRefInflator');
39 47 my $obj_builder = Tapper::MCP::Scheduler::ObjectBuilder->instance;
40  
41 47 my @hosts_with_features;
42 47 ▶ while (my $host = $hosts->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $host = $hosts->next) 175 47 222
43 175 my $features = Tapper::Model::get_hardware_overview($host->{id});
44 175 $features->{hostname} = $host->{name};
45 175 my $host_obj = $obj_builder->new_host(%$host);
46 175 push @hosts_with_features, {host => $host_obj, features => $features};
47 }
48 47 return \@hosts_with_features;
49 }
50  
51 sub available_resources
52 46 ▶{
Subroutine available_resources: called CC=2 SCAR=7.4
Pod undocumented
53 46 my $resources = model('TestrunDB')->resultset("Resource")->search(
54 {active => 1, used_by_scheduling_id => undef});
55 46 my @_available_resources;
56 46 ▶ while (my $resource = $resources->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $resource = $resources->next) 0 46 46
57 0 push @_available_resources, $resource;
58 }
59 46 return \@_available_resources;
60 }
61  
62  
63 sub official_queuelist
64 46 ▶{
Subroutine official_queuelist: called CC=3 SCAR=11.0
Pod documented
65  
66 46 my $queue_rs = model('TestrunDB')->resultset('Queue')->search({active=> 1}
67 ,{result_class => 'DBIx::Class::ResultClass::HashRefInflator'});
68 46 my %queues;
69 46 ▶ while (my $q = $queue_rs->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $q = $queue_rs->next) 138 46 184
70 138 $queues{$q->{name}} = $q;
71 }
72  
73 46 my %queue_objects;
74 46 my $obj_builder = Tapper::MCP::Scheduler::ObjectBuilder->instance;
75 46 foreach my $name (keys %queues) {
76 138 $queue_objects{$name} = $obj_builder->new_queue(%{$queues{$name}});
77 }
78 46 return \%queue_objects;
79 }
80  
81  
82  
83  
84 80 ▶sub toggle_bandwith_color {
Subroutine toggle_bandwith_color: called CC=6 SCAR=18.0
Pod undocumented
85 80 my ($self, $free_hosts, $queue) = @_;
86  
87 80 ▶ return 0 if @{$queue->jobs} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @{$queue->jobs;} == 0 55 25 80
88 25 foreach my $free_host ( map {$_->{host} } @$free_hosts) {
89 25 ▶ if (@{$free_host->queues}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$free_host->queues;}) { } 1 24 25
90 QUEUE_CHECK:
91 {
92 1 foreach my $queuehost (@{$free_host->queues}) {
93 1 ▶ return 0 if $queue->id == $queue->id;
Branch: 1 branch
BranchTrueFalseTotal
if $queue->id == $queue->id 1 0 1
94 }
95 }
96 } else {
97 24 return 0;
98 }
99 }
100 0 return 1;
101 }
102  
103  
104  
105 47 ▶sub get_next_job {
Subroutine get_next_job: called CC=19 SCAR=29.6
Pod documented
106 47 my ($self, %args) = @_;
107  
108 47 my $obj_builder = Tapper::MCP::Scheduler::ObjectBuilder->instance;
109 47 $obj_builder->clear();
110  
111 47 my ($queue, $job);
112  
113 do {{
114  
115 47 my $free_hosts = $self->free_hosts_with_features();
116 47 ▶ return if not ($free_hosts and @$free_hosts);
Branch: 1 branch
BranchTrueFalseTotal
unless $free_hosts and @$free_hosts 1 46 47
Condition66%2/3
$free_hosts and @$free_hosts
!ll&&!rl&&r
0146
MC/DC50%1/2
$free_hosts and @$free_hosts
$free_hosts @$free_hosts
Truth table66%2/3
A and B
A= $free_hostsB= @$free_hosts
ABresult
0X0
100
111
117  
118 46 my $available_resources = $self->available_resources();
119  
120 46 my $queues = $self->official_queuelist();
121  
122 46 my $white_bandwith=1; # chosen queue was first choice
123  
124 # reset the list of associated jobs with this queue on every get_next_job
125 46 my $prioqueue = PrioQueue->new();
126 46 $job = $prioqueue->get_first_fitting($free_hosts, $available_resources);
127  
128  
129 QUEUE:
130 46 ▶ while (not $job) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($job) 107 1 108
131  
132 107 my $queue = $self->algorithm->lookup_next_queue($queues);
133 107 ▶ return () unless $queue;
Branch: 1 branch
BranchTrueFalseTotal
unless $queue 0 107 107
134 107 ▶ if ($job = $queue->get_first_fitting($free_hosts, $available_resources)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($job = $queue->get_first_fitting($free_hosts, $available_resources)) { } 27 80 107
135 27 ▶ if ($job->auto_rerun) {
Branch: 1 branch
BranchTrueFalseTotal
if ($job->auto_rerun) 9 18 27
136 9 $job->testrun->rerun;
137 }
138 27 ▶ if ($job->testrun->scenario_element) {
Branch: 1 branch
BranchTrueFalseTotal
if ($job->testrun->scenario_element) 1 26 27
139 ELEMENT:
140 1 foreach my $element ($job->testrun->scenario_element->peer_elements) {
141 2 my $peer_job = $element->testrun->testrun_scheduling;
142 2 ▶ next ELEMENT if $peer_job->id == $job->id;
Branch: 1 branch
BranchTrueFalseTotal
if $peer_job->id == $job->id 1 1 2
143 1 $prioqueue->add($peer_job);
144 }
145 }
146 27 ▶ $self->algorithm->update_queue($job->queue) if $white_bandwith;
Branch: 1 branch
BranchTrueFalseTotal
if $white_bandwith 27 0 27
147 27 last QUEUE;
148 } else {
149 80 delete $queues->{$queue->name};
150 80 ▶ $white_bandwith=0 if $self->toggle_bandwith_color($free_hosts, $queue);
Branch: 1 branch
BranchTrueFalseTotal
if $self->toggle_bandwith_color($free_hosts, $queue) 0 80 80
151  
152 }
153 80 ▶ last QUEUE if not %$queues;
Branch: 1 branch
BranchTrueFalseTotal
unless %$queues 18 62 80
154 }
155  
156 46 ▶ if ($job and $job->testrun->scenario_element) {
Branch: 1 branch
BranchTrueFalseTotal
if ($job and $job->testrun->scenario_element) 2 44 46
Condition100%3/3
$job and $job->testrun->scenario_element
!ll&&!rl&&r
18262
MC/DC100%2/2
$job and $job->testrun->scenario_element
$job $job->testrun->scenario_element
Truth table100%3/3
A and B
A= $jobB= $job->testrun->scenario_element
ABresult
0X0
100
111
157 2 $self->mark_job_as_running($job);
158 2 ▶ if ($job->testrun->scenario_element->peers_need_fitting > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($job->testrun->scenario_element->peers_need_fitting > 0) { } 1 1 2
159 # do not return this job already
160 1 $job = undef;
161 1 next;
162 } else {
163 1 return map{$_->testrun->testrun_scheduling} $job->testrun->scenario_element->peer_elements->all;
164 }
165 }
166 }
167 47 ▶ } while (not $job and $args{try_until_found});
Branch: 1 branch
BranchTrueFalseTotal
not $job and $args{"try_until_found"} and do { do { { my $free_hosts = $self->free_hosts_with_features; return unless $free_hosts and @$free_hosts; my $available_resources = $self->available_resources; my $queues = $self->official_queuelist; my $white_bandwith = 1; my $prioqueue = "Tapper::MCP::Scheduler::PrioQueue"->new; $job = $prioqueue->get_first_fitting($free_hosts, $available_resources); QUEUE: until ($job) { my $queue = $self->algorithm->lookup_next_queue($queues); return () unless $queue; if ($job = $queue->get_first_fitting($free_hosts, $available_resources)) { if ($job->auto_rerun) { $job->testrun->rerun; } ; if ($job->testrun->scenario_element) { ELEMENT: foreach my $element ($job->testrun->scenario_element->peer_elements) { my $peer_job = $element->testrun->testrun_scheduling; next ELEMENT if $peer_job->id == $job->id; $prioqueue->add($peer_job); } ; } ; $self->algorithm->update_queue($job->queue) if $white_bandwith; last QUEUE; } else { delete $$queues{$queue->name}; $white_bandwith = 0 if $self->toggle_bandwith_color($free_hosts, $queue); } ; last QUEUE unless %$queues; } ; if ($job and $job->testrun->scenario_element) { $self->mark_job_as_running($job); if ($job->testrun->scenario_element->peers_need_fitting > 0) { $job = undef; next; } else { return map({$_->testrun->testrun_scheduling;} $job->testrun->scenario_element->peer_elements->all); } ; } ; } } } 0 45 45
Condition66%2/3
not $job and $args{"try_until_found"}
!ll&&!rl&&r
26190
MC/DC0%0/2
not $job and $args{"try_until_found"}
not $job $args{"try_until_found"}
Truth table66%2/3
not(A) and B
A= not $jobB= $args{"try_until_found"}
ABresult
0X0
100
111
168  
169 45 ▶ return $job || () ;
Condition66%2/3
$job || ()
l!l&&r!l&&!r
26019
MC/DC50%1/2
$job || ()
$job ()
Truth table66%2/3
A || B
A= $jobB= ()
ABresult
1X1
011
000
170 }
171  
172  
173 25 ▶sub mark_job_as_running {
Subroutine mark_job_as_running: called CC=1 SCAR=0.0
Pod undocumented
174 25 my ($self, $job) = @_;
175  
176 25 $job->testrun->starttime_testrun(model('TestrunDB')->storage->datetime_parser->format_datetime(DateTime->now));
177 25 $job->testrun->update();
178 25 $job->mark_as_running;
179 }
180  
181 21 ▶sub mark_job_as_finished {
Subroutine mark_job_as_finished: called CC=1 SCAR=0.0
Pod undocumented
182 21 my ($self, $job) = @_;
183  
184 21 $job->testrun->endtime_test_program(model('TestrunDB')->storage->datetime_parser->format_datetime(DateTime->now));
185 21 $job->testrun->update();
186 21 $job->mark_as_finished;
187 }
188  
189 15 1; # End of Tapper::MCP::Scheduler::Controller
190  
191 __END__
« MCP/Config.pm ↑ Summary MCP/State/Plugin/OSRC.pm »