Valley.pm 8.85 KB
Newer Older
1 2 3 4
=pod 

=head1 NAME

5
    Bio::EnsEMBL::Hive::Valley
6 7 8 9 10 11 12 13

=head1 DESCRIPTION

    A Valley represents a collection of available Meadows.

    Certain methods fit better with the concept of Valley -
    such as identifying all dead workers, or killing a particular one given worker_id.

14 15
=head1 LICENSE

16
    Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
Matthieu Muffato's avatar
Matthieu Muffato committed
17
    Copyright [2016-2017] EMBL-European Bioinformatics Institute
18 19 20 21 22 23 24 25 26 27

    Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

         http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software distributed under the License
    is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and limitations under the License.

28 29
=head1 CONTACT

30
    Please subscribe to the Hive mailing list:  http://listserver.ebi.ac.uk/mailman/listinfo/ehive-users  to discuss Hive-related questions or to be notified of our updates
31 32 33

=cut

34

35 36 37 38
package Bio::EnsEMBL::Hive::Valley;

use strict;
use warnings;
39
use List::Util ('sum');
40
use Sys::Hostname ('hostname');
41
use Bio::EnsEMBL::Hive::Utils ('find_submodules');
42
use Bio::EnsEMBL::Hive::Limiter;
43

44 45
use base ('Bio::EnsEMBL::Hive::Configurable');

46 47 48 49 50 51 52 53

sub meadow_class_path {

    return 'Bio::EnsEMBL::Hive::Meadow';
}


sub new {
54
    my ($class, $config, $default_meadow_type, $pipeline_name) = @_;
55 56 57

    my $self = bless {}, $class;

58 59 60
    $self->config( $config );
    $self->context( [ 'Valley' ] );

61
    my $amh = $self->available_meadow_hash( {} );
62 63

        # make sure modules are loaded and available ones are checked prior to setting the current one
64
    foreach my $meadow_class (@{ $self->get_implemented_meadow_list }) {
65
        eval "require $meadow_class";
66
        if( $meadow_class->name ) {
67
            my $meadow_object            = $meadow_class->new( $config );
68 69 70

            $meadow_object->pipeline_name( $pipeline_name ) if($pipeline_name);

71
            $amh->{$meadow_class->type} = $meadow_object;
72 73 74
        }
    }

75
    $self->set_default_meadow_type($default_meadow_type);     # run this method even if $default_meadow_type was not specified
76 77 78 79 80

    return $self;
}


81
sub available_meadow_hash {
82 83 84
    my $self = shift @_;

    if(@_) {
85
        $self->{_available_meadow_hash} = shift @_;
86
    }   
87
    return $self->{_available_meadow_hash};
88 89 90
}


91
sub get_available_meadow_list {     # this beautiful one-liner pushes $local to the bottom of the list
92 93 94 95
    my $self = shift @_;

    my $local = $self->meadow_class_path . '::LOCAL';

96
    return [ sort { (ref($a) eq $local) or -(ref($b) eq $local) } values %{ $self->available_meadow_hash } ];
97 98 99
}


100 101 102 103 104 105
sub get_implemented_meadow_list {
    my $self = shift @_;
    return find_submodules( $self->meadow_class_path );
}


106 107
sub set_default_meadow_type {
    my ($self, $default_meadow_type) = @_;
108

109 110 111
    if($default_meadow_type) {
        if( my $default_meadow = $self->available_meadow_hash->{$default_meadow_type} ) {   # store if available
            $self->{_default_meadow} = $default_meadow;
112
        } else {
113
            die "Meadow '$default_meadow_type' does not seem to be available on this machine, please investigate";
114
        }
115
    } else {
116
        $self->{_default_meadow} = $self->get_available_meadow_list->[0];     # take the first from preference list
117 118 119 120
    }
}


121
sub get_default_meadow {
122 123
    my $self = shift @_;

124
    return $self->{_default_meadow};
125 126 127 128 129 130
}


sub find_available_meadow_responsible_for_worker {
    my ($self, $worker) = @_;

131
    if( my $meadow = $self->available_meadow_hash->{$worker->meadow_type} ) {
132
        if($meadow->cached_name eq $worker->meadow_name) {
133 134
            return $meadow;
        }
135
    }
136
    return undef;
137 138 139
}


140
sub whereami {
141 142
    my $self = shift @_;

143 144 145
    my $meadow_host = hostname();
    my $meadow_user = $ENV{'USER'} || getpwuid($<);

146
    foreach my $meadow (@{ $self->get_available_meadow_list }) {
147
        my $pid;
148
        eval {
149 150 151 152
            # get_current_worker_process_id() is expected to die if the pid
            # cannot be determined. With the eval{} and the unless{} it will
            # skip the meadow and try the next one.
            $pid = $meadow->get_current_worker_process_id();
153 154
        };
        unless($@) {
155
            return ($meadow, $pid, $meadow_host, $meadow_user);
156 157
        }
    }
158
    die "Could not determine the Meadow, please investigate";
159 160 161
}


162
sub get_pending_worker_counts_by_meadow_type_rc_name {
163
    my ($self, $statuses) = @_;
164 165 166 167 168

    my %pending_counts = ();
    my $total_pending_all_meadows = 0;

    foreach my $meadow (@{ $self->get_available_meadow_list }) {
169 170 171 172 173 174 175 176
        my $pending_workers_per_rc_name = $statuses->{ $meadow->type }->{ 'PEND' } || {};

        $pending_counts{ $meadow->type } = {};
        while (my ($rc_name,$process_ids) = each %$pending_workers_per_rc_name) {
            my $n_pending = scalar(@$process_ids);
            $pending_counts{ $meadow->type }->{ $rc_name } = $n_pending;
            $total_pending_all_meadows += $n_pending;
        }
177 178 179 180 181 182
    }

    return (\%pending_counts, $total_pending_all_meadows);
}


183 184
sub generate_limiters {
    my ($self, $statuses) = @_;
185

186 187
    my $valley_running_worker_count             = 0;
    my %meadow_capacity_limiter_hashed_by_type  = ();
188 189

    foreach my $meadow (@{ $self->get_available_meadow_list }) {
190
        my $this_worker_count   = sum(0, (map {scalar(@$_)} values( %{ $statuses->{ $meadow->type }->{ 'RUN' } } )));
191 192

        $valley_running_worker_count                           += $this_worker_count;
193 194

        my $available_worker_slots = defined($meadow->config_get('TotalRunningWorkersMax'))
195
            ? $meadow->config_get('TotalRunningWorkersMax') - $this_worker_count
196 197
            : undef;

198
            # so the hash will contain limiters for every meadow_type, but not all of them active:
199
        $meadow_capacity_limiter_hashed_by_type{ $meadow->type } = Bio::EnsEMBL::Hive::Limiter->new( "Number of workers in '".$meadow->signature."' meadow", $available_worker_slots );
200 201
    }

202
    return ($valley_running_worker_count, \%meadow_capacity_limiter_hashed_by_type);
203 204
}

205 206 207 208 209 210 211 212 213 214 215
sub query_worker_statuses {
    my ($self, $all_registered_running_workers) = @_;

    my %statuses            = ();

    foreach my $meadow (@{ $self->get_available_meadow_list }) {
        my $process_ids_by_meadow_user      = $all_registered_running_workers->{$meadow->type}{$meadow->cached_name};
        my $this_status_list                = $meadow->status_of_all_our_workers( [keys %$process_ids_by_meadow_user] );
        $statuses{ $meadow->type }          = {};
        foreach my $ra (@$this_status_list) {
            my ($worker_pid, $meadow_user, $status, $rc_name) = @$ra;
216 217 218 219 220 221 222 223
            # Workers that are not properly named and are not in the
            # database are likely not ours. Let's skip them.
            if (($rc_name eq '__unknown_rc_name__') and !$process_ids_by_meadow_user->{$meadow_user}->{$worker_pid}) {
                next;
            }
            # Workers that are in RUN state but not yet in the database probably
            # have a hard time registering (db too busy ? registry too big ?).
            # Let's mark them as PENDing for the time being.
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
            if (($status eq 'RUN') and !$process_ids_by_meadow_user->{$meadow_user}->{$worker_pid}) {
                $status = 'PEND';
            }
            push @{ $statuses{ $meadow->type }->{ $status }->{ $rc_name } }, $worker_pid;
        }
    }
    return \%statuses;
}

sub status_of_all_our_workers_by_meadow_signature {
    my ($self, $statuses) = @_;

    my %worker_statuses = ();
    foreach my $meadow (@{ $self->get_available_meadow_list }) {
        my $meadow_signature = $meadow->type.'/'.$meadow->cached_name;
        my $statuses_rc_name = $statuses->{ $meadow->type };
240
        $worker_statuses{$meadow_signature} = {};
241 242 243 244 245 246 247 248 249
        foreach my $status (keys %$statuses_rc_name) {
            foreach my $pid_list (values %{ $statuses_rc_name->{$status} }) {
                $worker_statuses{$meadow_signature}{$_} = $status for @$pid_list;
            }
        }
    }
    return \%worker_statuses;
}

250 251 252 253 254 255 256 257
sub cleanup_left_temp_directory {
    my ($self, $worker) = @_;

    # cleanup_left_temp_directory is called when garbage-collecting dead-workers,
    # which is only possible for reachable meadows.
    # This guarantees that $meadow is defined.
    my $meadow = $self->available_meadow_hash->{$worker->meadow_type};

258 259 260 261 262
    if ($meadow->config_get('CleanupTempDirectoryKilledWorkers')) {
        warn "GarbageCollector:\tCleaning-up /tmp\n";
        my $rc = $meadow->run_on_host($worker->meadow_host, $worker->meadow_user, ['rm', '-rf', $worker->temp_directory_name]);
        $worker->worker_say(sprintf("Error: could not clean %s's temp directory '%s': %s\n", $worker->meadow_host, $worker->temp_directory_name, $@)) if $rc;
    }
263 264
}

265

266
1;