ensembl-hive  2.8.1
RoleAdaptor.pm
Go to the documentation of this file.
1 =pod
2 
3 =head1 NAME
4 
6 
7 =head1 SYNOPSIS
8 
9  $role_adaptor = $db_adaptor->get_RoleAdaptor;
10 
11  $role_adaptor = $role_object->adaptor;
12 
13 =head1 DESCRIPTION
14 
15  Module to encapsulate all db access for persistent class Role.
16  There should be just one per application and database connection.
17 
18 =head1 LICENSE
19 
20  See the NOTICE file distributed with this work for additional information
21  regarding copyright ownership.
22 
23  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
24  You may obtain a copy of the License at
25 
26  http://www.apache.org/licenses/LICENSE-2.0
27 
28  Unless required by applicable law or agreed to in writing, software distributed under the License
29  is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
30  See the License for the specific language governing permissions and limitations under the License.
31 
32 =head1 CONTACT
33 
34  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
35 
36 =cut
37 
38 
39 package Bio::EnsEMBL::Hive::DBSQL::RoleAdaptor;
40 
41 use strict;
42 use warnings;
43 
45 
46 use base ('Bio::EnsEMBL::Hive::DBSQL::ObjectAdaptor');
47 
48 
49 sub default_table_name {
50  return 'role';
51 }
52 
53 
54 sub object_class {
55  return 'Bio::EnsEMBL::Hive::Role';
56 }
57 
58 
59 sub finalize_role {
60  my ($self, $role, $release_undone_jobs) = @_;
61 
62  $role->when_finished( 'CURRENT_TIMESTAMP' );
63  $self->update_when_finished( $role );
64 
65  $self->db->get_AnalysisStatsAdaptor->increment_a_counter( 'num_running_workers', -1, $role->analysis_id );
66 
67  if( $release_undone_jobs ) {
68  $self->db->get_AnalysisJobAdaptor->release_undone_jobs_from_role( $role );
69  }
70 
71  # Re-sync the analysis_stats when a worker dies as part of dynamic sync system.
72  # It will also re-calculate num_running_workers (from active roles)
73  # so no further adjustment should be necessary.
74  $self->db->get_WorkerAdaptor->safe_synchronize_AnalysisStats( $role->analysis->stats );
75 }
76 
77 
78 sub fetch_last_unfinished_by_worker_id {
79  my ($self, $worker_id) = @_;
80 
81  return $self->fetch_all( "WHERE worker_id=$worker_id AND when_finished IS NULL ORDER BY role_id DESC LIMIT 1", 1 );
82 }
83 
84 
85 sub get_hive_current_load {
86  my $self = shift;
87  my $sql = qq{
88  SELECT sum(1/hive_capacity)
89  FROM role
90  JOIN analysis_base USING(analysis_id)
91  WHERE when_finished IS NULL
92  AND hive_capacity IS NOT NULL
93  AND hive_capacity>0
94  };
95  my $sth = $self->prepare($sql);
96  $sth->execute();
97  my ($current_load)=$sth->fetchrow_array();
98  $sth->finish;
99  return ($current_load || 0);
100 }
101 
102 
103 sub get_role_rank {
104  my ($self, $role) = @_;
105 
106  return $self->count_all( 'analysis_id=' . $role->analysis_id . ' AND when_finished IS NULL AND role_id<' . $role->dbID );
107 }
108 
109 
110 sub count_active_roles {
111  my ($self, $analysis_id) = @_;
112 
113  return $self->count_all( ($analysis_id ? "analysis_id=$analysis_id AND " : '') . 'when_finished IS NULL' );
114 }
115 
116 
117 sub print_active_role_counts {
118  my $self = shift;
119 
120  my $sql = qq{
121  SELECT logic_name, count(*)
122  FROM role
123  JOIN analysis_base a USING(analysis_id)
124  WHERE when_finished IS NULL
125  GROUP BY a.analysis_id
126  };
127 
128  my $total_roles = 0;
129  my $sth = $self->prepare($sql);
130  $sth->execute();
131 
132  print "\n===== Stats of active Roles as recorded in the pipeline database: ======\n";
133  while(my ($logic_name, $active_role_count) = $sth->fetchrow_array()) {
134  printf("%30s : %d active Roles\n", $logic_name, $active_role_count);
135  $total_roles += $active_role_count;
136  }
137  $sth->finish;
138  printf("%30s : %d active Roles\n\n", '======= TOTAL =======', $total_roles);
139 }
140 
141 
142 sub fetch_all_finished_roles_with_unfinished_jobs {
143  my $self = shift;
144 
145  return $self->fetch_all( "JOIN job USING(role_id) WHERE when_finished IS NOT NULL AND status IN ($Bio::EnsEMBL::Hive::DBSQL::AnalysisJobAdaptor::ALL_STATUSES_OF_TAKEN_JOBS) GROUP BY role_id" );
146 }
147 
148 
149 sub fetch_all_unfinished_roles_of_dead_workers {
150  my $self = shift;
151 
152  return $self->fetch_all( "JOIN worker USING(worker_id) WHERE when_finished IS NULL AND status='DEAD'" );
153 }
154 
155 
156 1;
157 
Bio::EnsEMBL::Hive::DBSQL::AnalysisJobAdaptor
Definition: AnalysisJobAdaptor.pm:22
Bio::EnsEMBL::Hive::DBSQL::RoleAdaptor
Definition: RoleAdaptor.pm:20