Valley.pm 4.42 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
=pod 

=head1 NAME

  Bio::EnsEMBL::Hive::Valley

=head1 SYNOPSIS

=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.

=head1 CONTACT

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

=cut

package Bio::EnsEMBL::Hive::Valley;

use strict;
use warnings;
26
use Sys::Hostname;
27
28
use Bio::EnsEMBL::Hive::Utils ('find_submodules');

29
30
use base ('Bio::EnsEMBL::Hive::Configurable');

31
32
33
34
35
36
37
38

sub meadow_class_path {

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


sub new {
39
    my ($class, $config, $default_meadow_type, $pipeline_name) = @_;
40
41
42

    my $self = bless {}, $class;

43
44
45
    $self->config( $config );
    $self->context( [ 'Valley' ] );

46
    my $amh = $self->available_meadow_hash( {} );
47
48
49
50

        # make sure modules are loaded and available ones are checked prior to setting the current one
    foreach my $meadow_class (@{ find_submodules( $self->meadow_class_path ) }) {
        eval "require $meadow_class";
51
        if($meadow_class->name) {
52
            my $meadow_object            = $meadow_class->new( $config );
53
54
55

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

56
            $amh->{$meadow_class->type} = $meadow_object;
57
58
59
        }
    }

60
    $self->set_default_meadow_type($default_meadow_type);     # run this method even if $default_meadow_type was not specified
61
62
63
64
65

    return $self;
}


66
sub available_meadow_hash {
67
68
69
    my $self = shift @_;

    if(@_) {
70
        $self->{_available_meadow_hash} = shift @_;
71
    }   
72
    return $self->{_available_meadow_hash};
73
74
75
}


76
sub get_available_meadow_list {     # this beautiful one-liner pushes $local to the bottom of the list
77
78
79
80
    my $self = shift @_;

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

81
    return [ sort { (ref($a) eq $local) or -(ref($b) eq $local) } values %{ $self->available_meadow_hash } ];
82
83
84
}


85
86
sub set_default_meadow_type {
    my ($self, $default_meadow_type) = @_;
87

88
89
90
    if($default_meadow_type) {
        if( my $default_meadow = $self->available_meadow_hash->{$default_meadow_type} ) {   # store if available
            $self->{_default_meadow} = $default_meadow;
91
        } else {
92
            die "Meadow '$default_meadow_type' does not seem to be available on this machine, please investigate";
93
        }
94
    } else {
95
        $self->{_default_meadow} = $self->get_available_meadow_list->[0];     # take the first from preference list
96
97
98
99
    }
}


100
sub get_default_meadow {
101
102
    my $self = shift @_;

103
    return $self->{_default_meadow};
104
105
106
107
108
109
}


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

110
111
    if( my $meadow = $self->available_meadow_hash->{$worker->meadow_type} ) {
        if($meadow->name eq $worker->meadow_name) {
112
113
            return $meadow;
        }
114
    }
115
    return undef;
116
117
118
}


119
sub whereami {
120
121
    my $self = shift @_;

122
    my ($meadow_type, $meadow_name, $pid);
123
    foreach my $meadow (@{ $self->get_available_meadow_list }) {
124
        eval {
125
126
127
            $pid         = $meadow->get_current_worker_process_id();
            $meadow_type = $meadow->type();
            $meadow_name = $meadow->name();
128
129
130
131
132
133
134
135
136
137
138
        };
        unless($@) {
            last;
        }
    }
    unless($pid) {
        die "Could not determine the Meadow, please investigate";
    }

    my $exechost = hostname();

139
    return ($meadow_type, $meadow_name, $pid, $exechost);
140
141
142
}


143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
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);
}


sub get_available_worker_slots_by_meadow_type {
    my $self = shift @_;

    my %available_worker_slots = ();

    foreach my $meadow (@{ $self->get_available_meadow_list }) {
        if( $meadow->can('count_running_workers') and defined($meadow->config_get('TotalRunningWorkersMax'))) {
            $available_worker_slots{ $meadow->type } = $meadow->config_get('TotalRunningWorkersMax') - $meadow->count_running_workers;
        }
    }

    return \%available_worker_slots;
}


174
175
1;