/
manager.py
381 lines (326 loc) · 15.6 KB
/
manager.py
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
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
380
381
'''
Name: manager.py
Purpose: Manage jobs submitted to the autonomics annotation system
Author: Mathew Citarella Revised By: Peter L. Williams
Created: 01/10/2012
Copyright: (c) Mathew Citarella 2012
'''
from sqlalchemy import exc
from sqlalchemy.sql import functions, select, and_
from autonomics import netutils, settings
from autonomics.jobs import BlastJob, BlastJobNR, BlastJobSwissprot, BlatJob
from autonomics.jobs import BlastAssociationJob, PfamJob, AssemblyJob
from autonomics.jobs import JobState, Locations, Resources, AdapterTrimJob
from autonomics.jobs import ReadNormJob, PantherJob, QuantificationJob
from autonomics.jobs import QualityTrimJob, UploadJob
from autonomics.queue import remove_from_queue
import argparse
import datetime
import gc
import time
import sys
def make_upload_job(q_r,args_r):
'''
q_r:
A sqlalchemy row from the queue table, representing an upload job.
args_r:
A sqlalchemy row from the args table for this job.
Returns an UploadJob object.
'''
return UploadJob(q_r.project_id, q_r.job_id, q_r.job_type)
def make_hpc_job(q_r,args_r):
'''
q_r:
An sqlalchemy row from the queue table for any of the HPCJob
subclasses.
args_r: An sqlalchemy row from the args table for this job.
Returns either a BlastJobNR, BlastJobSwissprot, BlastJob, PfamJob,
BlatJob, or QuantificationJob object.
'''
jobs = {"blast_nr": BlastJobNR, "blast_swissprot": BlastJobSwissprot,
"blast": BlastJob, "pfam": PfamJob, "blat": BlatJob,
"quantification": QuantificationJob}
return jobs[q_r.job_type](q_r.project_id, q_r.job_id, q_r.job_type,
args_r.executable, args_r.resources, args_r.pipeline_args,
args_r.process_args)
def make_blast_association_job(q_r,args_r):
'''
q_r: An sqlalchemy row from the queue table representing a
BlastAssociation job.
args_r: An sqlalchemy row from the args table for this job.
Returns a BlastAssocationJob object.
'''
return BlastAssociationJob(q_r.project_id, q_r.job_id, q_r.job_type,
args_r.pipeline_args, args_r.process_args)
def make_assembly_job(q_r,args_r):
'''
q_r:
An sqlalchemy row from the queue table representing an Assembly job.
args_r:
An sqlalchemy row from the args table for this job.
Returns an AssemblyJob object.
'''
return AssemblyJob(q_r.project_id, q_r.job_id, q_r.job_type,
args_r.executable, args_r.resources,
args_r.pipeline_args, args_r.process_args)
def make_preassembly_job(q_r,args_r):
'''
q_r: An sqlalchemy row from the queue table with data for one of the
three pre-assembly job types.
args_r:
An sqlalchemy row from the args table for this job.
Returns either a QualityTrimJob, AdapterTrimJob, or ReadNormJob object.
'''
jobs = {"quality_trim": QualityTrimJob, "adapter_trim": AdapterTrimJob,
"read_normalization": ReadNormJob}
return jobs[q_r.job_type](q_r.project_id, q_r.job_id, q_r.job_type,
args_r.executable, args_r.resources, args_r.pipeline_args,
args_r.process_args)
def make_panther_job(q_r,args_r):
'''
q_r:
An sqlalchemy row from the queue table with data for a Panther job.
args_r: An sqlalchemy row from the args table for this job.
Returns a PantherJob object.
'''
return PantherJob(q_r.project_id, q_r.job_id, q_r.job_type,
args_r.executable, args_r.resources,
args_r.pipeline_args, args_r.process_args)
job_constructors = {"blast": make_hpc_job, "blast_nr": make_hpc_job,
"blast_swissprot": make_hpc_job, "blat": make_hpc_job,
"go":make_blast_association_job, "kegg": make_blast_association_job,
"pfam": make_hpc_job, "assemble": make_assembly_job,
"quality_trim": make_preassembly_job, "panther": make_panther_job,
"adapter_trim": make_preassembly_job,
"read_normalization": make_preassembly_job, "upload": make_upload_job,
"quantification": make_hpc_job}
def mark_error(jid, session):
'''
jid (int):
The identifier to mark an error status for
session (netutils.DBSession):
A session object with an active connection to the Autonomics
database
Marks the provided job as having a status of 'E' in the jn_mapping table.
'''
jn_mapping = netutils.get_table_object('jn_mapping', session)
session.conn.execute(jn_mapping.update().where(
jn_mapping.c.job_id==jid).values(finished='E',
f_ts=functions.current_timestamp()))
def start_job(job, job_list, pipe_resources, mysql_session, queue):
'''
job:
The object to be started. Must be a subclass of Job.
job_list:
A list of jobs that this job should be added to, once started.
pipe_resources:
A Resources object that this job will be taking resources
from.
mysql_session:
A netutils.DBSession object with an active DB connection.
queue:
The queue in which this job was scheduled.
This method checks whether or not the job has any unfinished dependencies.
If it does not, the job takes its resources from the Resources object and
is added to the job_list.
'''
try:
dependency = None
if (queue==settings.normal_queue): # alternative is que_special which has no dependencies
#check if this job has unfinished dependencies in the queue
depends = netutils.get_table_object("jid_dependency", mysql_session)
jn = netutils.get_table_object("jn_mapping", mysql_session)
print "\nchecking dependency of job_id: ", job.jid
s = select([jn, depends], and_(depends.c.job_id==job.jid,
jn.c.job_id==depends.c.depends_on,
jn.c.finished=='N',))
dependency = s.execute().fetchone()
if(dependency is None):
print "dependency is None"
else:
print "depends on: ", dependency.depends_on
if(dependency is None):
mysql_session.conn.execute("UPDATE jn_mapping SET started='Y', \
s_ts=CURRENT_TIMESTAMP() WHERE job_id='" + str(job.jid) + "'")
remove_from_queue(job.jid, queue, mysql_session)
print " calling job.start() for job_id: ", job.jid, " job_type: ", job.job_type, " job_name: ", job.job_name
job.start()
pipe_resources.give_to(job)
job_list.append(job)
except Exception as e:
print "manager in exception after trying to start job"
sys.stderr.write(e.message + "\n")
sys.stderr.write("Error starting job: " + job.job_name + "\n")
class Unbuffered:
'''
This class is an unbuffered in/out stream which can be used to make
print unbuffered.
'''
def __init__(self, stream):
self.stream = stream
def write(self, data):
self.stream.write(data)
self.stream.flush()
def __getattr__(self, attr):
return getattr(self.stream, attr)
def main():
sys.stdout = Unbuffered(sys.stdout)
# ======================================
# Deal with command line args
# ======================================
parser = argparse.ArgumentParser()
parser.add_argument("--name", dest="my_name", default=None)
parser.add_argument("-u", "--user", dest="user", default=None)
parser.add_argument("-mu", "--mail-user", dest="mailUser", default=None)
parser.add_argument("-mh", "--mail-host", dest="mailHost", default="gmail.com")
parser.add_argument("--sleep-interval", dest="mainLoopSleepInterval",
default=settings.mainLoopSleepInterval,
help="How often this manager should sleep between checking job statuses.")
pargs = parser.parse_args()
if(pargs.my_name is None):
t = datetime.datetime.now()
my_name = "manager" + str(t.year) + str(t.month) + str(t.day) + \
str(t.hour) + str(t.minute) + str(t.second)
else:
my_name = pargs.my_name
SLEEP_INTERVAL = float(pargs.mainLoopSleepInterval)
print "\nSLEEP_INTERVAL between jobs checks: ", SLEEP_INTERVAL, " secs."
if(not pargs.user is None):
passwd = raw_input("Enter HPC password:")
settings.hpc_cred.update(pargs.user, passwd)
if(not pargs.mailUser is None):
mail_passwd = raw_input("Enter mail account password: ")
settings.mail_cred.update(pargs.user, mail_passwd)
# ======================================
# Set up resources
# ======================================
local_resources = Resources()
local_resources.add_resource("cpu", settings.MAX_NUM_LOCAL_CPUS)
local_resources.add_resource("local", settings.MAX_NUM_LOCAL_JOBS)
HPC_resources = Resources()
HPC_resources.add_resource("cpu", settings.MAX_NUM_HPC_CPUS)
HPC_resources.add_resource("blast_nr", settings.MAX_NUM_BLAST_NR_JOBS)
resources_at_location = {Locations.LOCAL: local_resources,
Locations.HPC: HPC_resources}
# ======================================
# Set up mysql db session
# ======================================
session = netutils.DBSession("localhost", settings.ZC_DB_NAME,
settings.db_cred.user, settings.db_cred.passwd)
# ======================================
# Loop forever in mainloop
# ======================================
job_list = []
finished = []
lloop_num = -1
print_res = 1
manager_queues = [settings.special_queue,settings.normal_queue]
while(True):
try:
# ======================================
# Start eleigible jobs that are in queue
# ======================================
for queue in manager_queues:
q = netutils.get_table_object(queue, session)
results = ""
if (queue == 'quenew'):
results=session.conn.execute("select * from quenew order by priority desc")
else:
s = q.select()
results = s.execute()
for q_row in results.fetchall():
# print "manager looking at queue job_id: ", q_row.job_id, " project_id: ", q_row.project_id, " job_type: ", q_row.job_type
if(q_row.job_type == 'upload'):
continue
args = netutils.get_table_object('args', session)
results = args.select(args.c.job_id==q_row.job_id).execute()
args_row = results.fetchone()
if(args_row is None):
default_args = netutils.get_table_object(
'default_args', session)
results = default_args.select(
default_args.c.job_type==q_row.job_type).execute()
args_row = results.fetchone()
if (args_row is None):
print "ERROR: can not find args for job_id: ", \
q_row.job_id, " job_type: ", q_row.job_type
sys.exit()
job = job_constructors[q_row.job_type](q_row, args_row)
if(resources_at_location[job.location].has_enough_free(
job.resources)):
print_res = 1
start_job(job, job_list,
resources_at_location[job.location], session, queue)
# code below here will not run till all eligible jobs in queue are
# started. some may take awhile to get started, e.g. splitting the
# reads for quantification
# ======================================
# Check state of all started jobs
# ======================================
finished = []
lloop_num = lloop_num + 1
pr_stats = 0
if ((lloop_num%10) == 0): pr_stats = 1
else: pr_stats = 0
pr_stats = 1 # eliminate lloop stuff and print every loop
local_job_types = ['quality_trim','adapter_trim', 'read_normalization', 'assemble']
for j in job_list:
state = j.check(pr_stats)
if(state == JobState.FINISHED):
finished.append(j)
res = j.complete()
if res == 0: print "\njob_state: ", j.job_name, " ERROR"
else: print "\njob_state: ", j.job_name, " FINISHED"
resources_at_location[j.location].take_from(j)
print_res = 1
elif(state == JobState.ERROR):
print("\njob_state: " + j.job_name + " ERROR")
finished.append(j)
mark_error(j.jid, session)
resources_at_location[j.location].take_from(j)
print_res = 1
elif(state == JobState.KILLED):
print("\njob_state: " + j.job_name + " KILLED by stop_job or stop_proj")
finished.append(j)
resources_at_location[j.location].take_from(j)
print_res = 1
elif(state == JobState.RETRIES_FAILED):
print("\njob_state: " + j.job_name + "RETRIES_FAILED")
finished.append(j)
mark_error(j.jid, session)
resources_at_location[j.location].take_from(j)
print_res = 1
elif(state == JobState.RUNNING):
if (j.job_type in local_job_types):
if ((lloop_num%10) == 0):
t = datetime.datetime.now()
print str(t.year) + "/" + str(t.month) + "/" + str(t.day) + " " + str(t.hour) + ":" + str(t.minute) + " ", j.job_name, " RUNNING"
else:
print "\njob_state: Unknown State ", j.job_name, " ", state
job_list = [j for j in job_list if not j in finished]
# ======================================
# Print Resources if a job has finished
# ======================================
if print_res == 1:
print_res = 0
print("\n--- Resources ---")
for loc, resources in resources_at_location.items():
print("free resources: ", resources.free)
print("total resources: ", resources.totals)
print('-----------------')
# ======================================
# Garbage collect & Sleep
# ======================================
gc.collect()
# sys.stdout.write('.')
time.sleep(SLEEP_INTERVAL)
# ======================================
# restart mySql server if necessary
# ======================================
except exc.OperationalError as e:
if("MySQL server has gone away" in e.message):
print("MySQL server has gone away, restarting session.\n")
session = netutils.DBSession("localhost", settings.ZC_DB_NAME,
settings.db_cred.user, settings.db_cred.passwd)
if __name__ == '__main__':
main()