Valley.pm 6.11 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
17
    Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
    Copyright [2016] 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 Sys::Hostname ('hostname');
40
use Bio::EnsEMBL::Hive::Utils ('find_submodules');
41
use Bio::EnsEMBL::Hive::Limiter;
42

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

45
46
47
48
49
50
51
52

sub meadow_class_path {

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


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

    my $self = bless {}, $class;

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

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

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

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

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

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

    return $self;
}


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

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


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

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

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


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


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

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


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

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


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

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


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

142
    my ($meadow_type, $meadow_name, $pid);
143
    foreach my $meadow (@{ $self->get_available_meadow_list }) {
144
        eval {
145
146
            $pid         = $meadow->get_current_worker_process_id();
            $meadow_type = $meadow->type();
147
            $meadow_name = $meadow->cached_name();
148
149
150
151
152
153
154
155
156
        };
        unless($@) {
            last;
        }
    }
    unless($pid) {
        die "Could not determine the Meadow, please investigate";
    }

157
158
    my $meadow_host = hostname();
    my $meadow_user = $ENV{'USER'} || getpwuid($<);
159

160
    return ($meadow_type, $meadow_name, $pid, $meadow_host, $meadow_user);
161
162
163
}


164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
sub get_pending_worker_counts_by_meadow_type_rc_name {
    my $self = shift @_;

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

    foreach my $meadow (@{ $self->get_available_meadow_list }) {
        my ($pending_this_meadow_by_rc_name, $total_pending_this_meadow) = ($meadow->count_pending_workers_by_rc_name());
        $pending_counts{ $meadow->type } = $pending_this_meadow_by_rc_name;
        $total_pending_all_meadows += $total_pending_this_meadow;
    }

    return (\%pending_counts, $total_pending_all_meadows);
}


180
sub count_running_workers_and_generate_limiters {
181
    my ($self, $meadow_type_2_name_2_users) = @_;
182

183
184
    my $valley_running_worker_count             = 0;
    my %meadow_capacity_limiter_hashed_by_type  = ();
185
186

    foreach my $meadow (@{ $self->get_available_meadow_list }) {
187
        my $meadow_users_of_interest = [keys %{ $meadow_type_2_name_2_users->{$meadow->type}{$meadow->cached_name} || {} }];
188
189
190
        my $this_worker_count   = $meadow->count_running_workers( $meadow_users_of_interest );

        $valley_running_worker_count                           += $this_worker_count;
191
192

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

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

200
    return ($valley_running_worker_count, \%meadow_capacity_limiter_hashed_by_type);
201
202
}

203
1;