AnalysisStats.pm 9.9 KB
Newer Older
1 2 3 4 5 6
#
# You may distribute this module under the same terms as perl itself

=pod 

=head1 NAME
7

8
  Bio::EnsEMBL::Hive::AnalysisStats
9 10 11 12 13 14

=head1 SYNOPSIS

=head1 DESCRIPTION

=head1 CONTACT
15 16

  Please contact ehive-users@ebi.ac.uk mailing list with questions/suggestions.
17 18

=head1 APPENDIX
19

20 21
  The rest of the documentation details each of the object methods.
  Internal methods are usually preceded with a _
22

23 24
=cut

25

26 27 28 29 30 31 32 33
package Bio::EnsEMBL::Hive::AnalysisStats;

use strict;

use Bio::EnsEMBL::Analysis;
use Bio::EnsEMBL::DBSQL::DBAdaptor;
use Bio::EnsEMBL::Hive::Worker;

34 35 36 37 38
sub new {
  my ($class,@args) = @_;
  my $self = bless {}, $class;
  return $self;
}
39 40 41 42 43 44 45

sub adaptor {
  my $self = shift;
  $self->{'_adaptor'} = shift if(@_);
  return $self->{'_adaptor'};
}

46 47 48 49 50 51
sub refresh {
  my $self = shift;
  return unless($self->adaptor);
  $self->adaptor->refresh($self);
}

52 53 54 55 56 57
sub update {
  my $self = shift;
  return unless($self->adaptor);
  $self->adaptor->update($self);
}

58 59 60 61
sub update_status {
  my ($self, $status ) = @_;
  return unless($self->adaptor);
  $self->adaptor->update_status($self->analysis_id, $status);
62
  $self->status($status);
63 64
}

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
sub decrease_hive_capacity {
  my ($self) = @_;
  return unless ($self->adaptor);
  $self->adaptor->decrease_hive_capacity($self->analysis_id);
}

sub increase_hive_capacity {
  my ($self) = @_;
  return unless ($self->adaptor);
  $self->adaptor->increase_hive_capacity($self->analysis_id);
}

sub get_running_worker_count {
  my $self = shift;
  return unless ($self->adaptor);
  return $self->adaptor->get_running_worker_count($self);
}

83
sub analysis_id {
84
  my $self = shift;
85 86
  $self->{'_analysis_id'} = shift if(@_);
  return $self->{'_analysis_id'};
87 88
}

89 90 91 92 93 94 95 96
sub get_analysis {
  my $self = shift;
  unless($self->{'_analysis'}) {
    $self->{'_analysis'} = $self->adaptor->db->get_AnalysisAdaptor->fetch_by_dbID($self->analysis_id);
  }
  return $self->{'_analysis'};
}

97 98 99 100 101 102 103 104 105 106 107 108
sub status {
  my ($self, $value ) = @_;

  if(defined $value) {
    $self->{'_status'} = $value;
  }
  return $self->{'_status'};
}

sub batch_size {
  my $self = shift;
  $self->{'_batch_size'} = shift if(@_);
109
  $self->{'_batch_size'}=1 unless(defined($self->{'_batch_size'}));
110 111 112
  return $self->{'_batch_size'};
}

113 114 115 116 117 118 119
sub avg_msec_per_job {
  my $self = shift;
  $self->{'_avg_msec_per_job'} = shift if(@_);
  $self->{'_avg_msec_per_job'}=0 unless($self->{'_avg_msec_per_job'});
  return $self->{'_avg_msec_per_job'};
}

120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
sub avg_input_msec_per_job {
  my $self = shift;
  $self->{'_avg_input_msec_per_job'} = shift if(@_);
  $self->{'_avg_input_msec_per_job'}=0 unless($self->{'_avg_input_msec_per_job'});
  return $self->{'_avg_input_msec_per_job'};
}

sub avg_run_msec_per_job {
  my $self = shift;
  $self->{'_avg_run_msec_per_job'} = shift if(@_);
  $self->{'_avg_run_msec_per_job'}=0 unless($self->{'_avg_run_msec_per_job'});
  return $self->{'_avg_run_msec_per_job'};
}

sub avg_output_msec_per_job {
  my $self = shift;
  $self->{'_avg_output_msec_per_job'} = shift if(@_);
  $self->{'_avg_output_msec_per_job'}=0 unless($self->{'_avg_output_msec_per_job'});
  return $self->{'_avg_output_msec_per_job'};
}

141 142 143 144 145
sub cpu_minutes_remaining {
  my $self = shift;
  return ($self->avg_msec_per_job * $self->unclaimed_job_count / 60000);
}

146 147 148 149 150 151
sub hive_capacity {
  my $self = shift;
  $self->{'_hive_capacity'} = shift if(@_);
  return $self->{'_hive_capacity'};
}

152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
sub behaviour {
  my $self = shift;
  $self->{'_behaviour'} = shift if(@_);
  return $self->{'_behaviour'};
}

sub input_capacity {
  my $self = shift;
  $self->{'_input_capacity'} = shift if(@_);
  return $self->{'_input_capacity'};
}

sub output_capacity {
  my $self = shift;
  $self->{'_output_capacity'} = shift if(@_);
  return $self->{'_output_capacity'};
}

170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
sub total_job_count {
  my $self = shift;
  $self->{'_total_job_count'} = shift if(@_);
  return $self->{'_total_job_count'};
}

sub unclaimed_job_count {
  my $self = shift;
  $self->{'_unclaimed_job_count'} = shift if(@_);
  return $self->{'_unclaimed_job_count'};
}

sub done_job_count {
  my $self = shift;
  $self->{'_done_job_count'} = shift if(@_);
  return $self->{'_done_job_count'};
}

188 189 190 191 192 193 194
sub failed_job_count {
  my $self = shift;
  $self->{'_failed_job_count'} = shift if(@_);
  $self->{'_failed_job_count'} = 0 unless(defined($self->{'_failed_job_count'}));
  return $self->{'_failed_job_count'};
}

195 196 197 198 199 200 201 202 203 204 205 206 207 208
sub max_retry_count {
  my $self = shift;
  $self->{'_max_retry_count'} = shift if(@_);
  $self->{'_max_retry_count'} = 3 unless(defined($self->{'_max_retry_count'}));
  return $self->{'_max_retry_count'};
}

sub failed_job_tolerance {
  my $self = shift;
  $self->{'_failed_job_tolerance'} = shift if(@_);
  $self->{'_failed_job_tolerance'} = 0 unless(defined($self->{'_failed_job_tolerance'}));
  return $self->{'_failed_job_tolerance'};
}

209 210 211 212 213 214 215 216
sub running_job_count {
  my $self = shift;
  return $self->total_job_count
         - $self->done_job_count
         - $self->unclaimed_job_count
         - $self->failed_job_count;
}

217 218 219 220 221 222 223
sub remaining_job_count {
  my $self = shift;
  return $self->total_job_count
         - $self->done_job_count
         - $self->failed_job_count;
}

224 225 226 227 228 229
sub num_running_workers {
  my $self = shift;
  $self->{'_num_running_workers'} = shift if(@_);
  return $self->{'_num_running_workers'};
}

230 231 232 233 234 235 236 237
sub num_required_workers {
  my $self = shift;
  $self->{'_num_required_workers'} = shift if(@_);
  return $self->{'_num_required_workers'};
}

sub seconds_since_last_update {
  my( $self, $value ) = @_;
238
  $self->{'_last_update'} = time() - $value if(defined($value));
239 240 241
  return time() - $self->{'_last_update'};
}

242 243 244 245 246 247
sub sync_lock {
  my $self = shift;
  $self->{'_sync_lock'} = shift if(@_);
  return $self->{'_sync_lock'};
}

Leo Gordon's avatar
Leo Gordon committed
248 249 250 251 252 253 254
sub rc_id {
    my $self = shift;

    $self->{'_rc_id'} = shift if(@_);
    return $self->{'_rc_id'};
}

255 256 257 258 259
sub can_be_empty {
    my $self = shift;

    $self->{'_can_be_empty'} = shift if(@_);
    return $self->{'_can_be_empty'};
260
}
261

262
  
263 264
sub print_stats {
  my $self = shift;
265
  my $mode = shift;
266

267
  return unless($self->get_analysis);
268 269 270 271

  $mode=1 unless($mode);

  if($mode == 1) {
Leo Gordon's avatar
Leo Gordon committed
272 273 274
    printf("%-27s(%2d) %11s %d:cpum job(%d/%d run:%d fail:%d %dms) worker[%d/%d] (sync'd %d sec ago)\n",
        $self->get_analysis->logic_name,
        $self->analysis_id,
275
        $self->status,
Leo Gordon's avatar
Leo Gordon committed
276
        $self->cpu_minutes_remaining,
277 278 279 280
        $self->remaining_job_count,
        $self->total_job_count,
        $self->running_job_count,
        $self->failed_job_count,
281
        $self->avg_msec_per_job,
282 283 284
        $self->num_required_workers, $self->hive_capacity,
        $self->seconds_since_last_update,
        );
285
  } elsif ($mode == 2) {
Leo Gordon's avatar
Leo Gordon committed
286 287 288
    printf("%-27s(%2d) %11s [%d/%d workers] (sync'd %d sec ago)\n",
        $self->get_analysis->logic_name,
        $self->analysis_id,
289 290 291 292 293 294 295 296 297 298 299 300 301 302
        $self->status,
        $self->num_required_workers, $self->hive_capacity,
        $self->seconds_since_last_update);

    printf("   msec_per_job   : %d\n", $self->avg_msec_per_job);
    printf("   cpu_min_total  : %d\n", $self->cpu_minutes_remaining);
    printf("   batch_size     : %d\n", $self->batch_size);
    printf("   total_jobs     : %d\n", $self->total_job_count);
    printf("   unclaimed jobs : %d\n", $self->unclaimed_job_count);
    printf("   running jobs   : %d\n", $self->running_job_count);
    printf("   done jobs      : %d\n", $self->done_job_count);
    printf("   failed jobs    : %d\n", $self->failed_job_count);
  }

303 304
}

305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379

sub determine_status {
  my $self = shift;
  
  if($self->status ne 'BLOCKED') {
    if ($self->unclaimed_job_count == 0 and
        $self->total_job_count == $self->done_job_count + $self->failed_job_count) {
      my $failure_percentage = 0;
      if ($self->total_job_count) {
        $failure_percentage = $self->failed_job_count * 100 / $self->total_job_count;
      }
      if ($failure_percentage > $self->failed_job_tolerance) {
        $self->status('FAILED');
        print
            "\n",
            "##################################################\n",
        printf
            "##   ERROR: %-35s ##\n", $self->get_analysis->logic_name." failed!";
        printf
            "##          %4.1f%% jobs failed (tolerance: %3d%%) ##\n", $failure_percentage, $self->failed_job_tolerance;
        print
            "##################################################\n\n";
      } else {
        $self->status('DONE');
      }
    }
    if($self->total_job_count == $self->unclaimed_job_count) {
      $self->status('READY');
    }
    if($self->unclaimed_job_count>0 and
       $self->total_job_count > $self->unclaimed_job_count) {
      $self->status('WORKING');
    }
  }
  return $self;
}


sub check_blocking_control_rules {
    my $self = shift;
  
    my $ctrl_rules = $self->adaptor->db->get_AnalysisCtrlRuleAdaptor->fetch_by_ctrled_analysis_id($self->analysis_id);

    if(scalar @$ctrl_rules) {    # there are blocking ctrl_rules to check

        my $all_ctrl_rules_done = 1;

        foreach my $ctrl_rule (@$ctrl_rules) {
                #use this method because the condition_analysis objects can be
                #network distributed to a different database so use it's adaptor to get
                #the AnalysisStats object
            my $condition_analysis              = $ctrl_rule->condition_analysis;
            my $condition_analysis_stats        = $condition_analysis && $condition_analysis->stats;
            my $condition_analysis_stats_status = $condition_analysis_stats && $condition_analysis_stats->status;
            my $condition_analysis_stats_cbe    = $condition_analysis_stats && $condition_analysis_stats->can_be_empty;

            unless( ($condition_analysis_stats_status eq 'DONE')
            or ($condition_analysis_stats_cbe and ($condition_analysis_stats_status eq 'READY'))
            ) {
                $all_ctrl_rules_done = 0;
                last;
            }
        }

        if($all_ctrl_rules_done) {
            if($self->status eq 'BLOCKED') {    # unblock, since all conditions are met
                $self->update_status('LOADING'); # trigger sync
            }
        } else {    # (re)block
            $self->update_status('BLOCKED');
        }
    }
}


380
1;