| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 88 | 25 | foreach my $free_host ( map {$_->{host} } @$free_hosts) { | |||||||||||||||||||||||||||
| 89 | 25 | ▶ | if (@{$free_host->queues}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | QUEUE_CHECK: | ||||||||||||||||||||||||||||
| 91 | { | ||||||||||||||||||||||||||||
| 92 | 1 | foreach my $queuehost (@{$free_host->queues}) { | |||||||||||||||||||||||||||
| 93 | 1 | ▶ | return 0 if $queue->id == $queue->id; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
$free_hosts and @$free_hosts
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 107 | my $queue = $self->algorithm->lookup_next_queue($queues); | |||||||||||||||||||||||||||
| 133 | 107 | ▶ | return () unless $queue; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 107 | ▶ | if ($job = $queue->get_first_fitting($free_hosts, $available_resources)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 27 | ▶ | if ($job->auto_rerun) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 9 | $job->testrun->rerun; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | 27 | ▶ | if ($job->testrun->scenario_element) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 143 | 1 | $prioqueue->add($peer_job); | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | 27 | ▶ | $self->algorithm->update_queue($job->queue) if $white_bandwith; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | 80 | ▶ | last QUEUE if not %$queues; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 46 | ▶ | if ($job and $job->testrun->scenario_element) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$job and $job->testrun->scenario_element
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
| |||||||||||||||||||||||||||||
| 157 | 2 | $self->mark_job_as_running($job); | |||||||||||||||||||||||||||
| 158 | 2 | ▶ | if ($job->testrun->scenario_element->peers_need_fitting > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
not $job and $args{"try_until_found"}
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"}
| |||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 45 | ▶ | return $job || () ; | ||||||||||||||||||||||||||
|
Condition66%2/3
$job || ()
MC/DC50%1/2
$job || ()
$job
()
Truth table66%2/3
A || B
A= $jobB= ()
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||