-
Notifications
You must be signed in to change notification settings - Fork 3
/
scheduling.py
executable file
·709 lines (465 loc) · 21.1 KB
/
scheduling.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
# Longbow is Copyright (C) of James T Gebbie-Rayet and Gareth B Shannon 2015.
#
# This file is part of the Longbow software which was developed as part of the
# HECBioSim project (http://www.hecbiosim.ac.uk/).
#
# HECBioSim facilitates and supports high-end computing within the UK
# biomolecular simulation community on resources such as ARCHER.
#
# Longbow is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 2 of the License, or (at your option) any later
# version.
#
# Longbow is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# Longbow. If not, see <http://www.gnu.org/licenses/>.
"""A module containing generic scheduling methods.
This module contains generic methods for preparing, submitting, deleting and
monitoring jobs. The methods contained within this module are all based on
generic job concepts. The specific functionality that comes from each scheduler
is accessed through the plug-in framework. To make use of these methods, the
plug-in framework must be present alongside the core library.
testenv(jobs, hostconf)
This method makes an attempt to test the environment and determine from
a pre-configured list what scheduler and job submission handler is present
on the machine.
delete(job)
A method containing the generic and boiler plate Longbow code for deleting
a job.
monitor(jobs)
A method containing the generic and boiler plate Longbow code for
monitoring a job, this method contains the entire structure of the loop
that deals with monitoring jobs.
prepare(jobs)
A method containing the generic and boiler plate Longbow code for
constructing the submit file.
submit(jobs)
A method containing the generic and boiler plate Longbow code for
submitting a job.
"""
import logging
import time
import os
import longbow.corelibs.configuration as configuration
import longbow.corelibs.exceptions as exceptions
import longbow.corelibs.shellwrappers as shellwrappers
import longbow.corelibs.staging as staging
import longbow.schedulers as schedulers
LOG = logging.getLogger("longbow.corelibs.scheduling")
QUEUEINFO = {}
def checkenv(jobs, hostconf):
"""A method for determining the sceduler and job handler on a machine.
This method makes an attempt to test the environment and determine from
a pre-configured list what scheduler and job submission handler is present
on the machine.
Required arguments are:
hostconf (string) - The path to the host configuration file, this should be
provided so that if any changes are made that they can
be saved.
jobs (dictionary) - The Longbow jobs data structure, see configuration.py
for more information about the format of this
structure.
"""
save = False
checked = []
saveparams = {}
# Take a look at each job.
for item in jobs:
job = jobs[item]
# If we have not checked this host already
if job["resource"] not in checked:
# Make sure we don't check the same thing again.
checked.extend([job["resource"]])
# If we don't have the resource defined then define it.
if job["resource"] not in saveparams:
saveparams[job["resource"]] = {}
# If we have no scheduler defined by the user then find it.
if job["scheduler"] is "":
_testscheduler(job)
saveparams[job["resource"]]["scheduler"] = job["scheduler"]
save = True
else:
LOG.info("The environment on host '%s' is '%s'",
job["resource"], job["scheduler"])
# If we have no job handler defined by the user then find it.
if job["handler"] is "":
_testhandler(job)
saveparams[job["resource"]]["handler"] = job["handler"]
save = True
else:
LOG.info("The handler on host '%s' is '%s'",
job["resource"], job["handler"])
# If resource has been checked.
else:
# Then we should have a look if the resource for this job has been
# altered.
if job["resource"] in saveparams:
# Then check if scheduler has been added.
if "scheduler" in saveparams[job["resource"]]:
job["scheduler"] = saveparams[job["resource"]]["scheduler"]
# Then check if handler has been added.
if "handler" in saveparams[job["resource"]]:
job["handler"] = saveparams[job["resource"]]["handler"]
# Do we have anything to change in the host file.
if save is True:
configuration.saveconfigs(hostconf, saveparams)
def delete(job):
"""A generic method for deleting jobs.
A method containing the generic and boiler plate Longbow code for deleting
a job.
Required arguments are:
job (dictionary) - A single job dictionary, this is often simply passed in
as a subset of the main jobs dictionary.
"""
scheduler = job["scheduler"]
try:
LOG.info("Deleting the job '%s'", job["jobname"])
getattr(schedulers, scheduler.lower()).delete(job)
except AttributeError:
raise exceptions.PluginattributeError(
"delete method cannot be found in plugin '{0}'"
.format(scheduler))
except exceptions.JobdeleteError:
LOG.info("Unable to delete job '%s'", job["jobname"])
LOG.info("Deletion successful")
def monitor(jobs):
"""A generic method for monitoring the status of jobs.
A method containing the generic and boiler plate Longbow code for
monitoring a job, this method contains the entire structure of the loop
that deals with monitoring jobs.
Required arguments are:
jobs (dictionary) - The Longbow jobs data structure, see configuration.py
for more information about the format of this
structure.
"""
LOG.info("Monitoring job/s. Depending on the chosen logging mode, Longbow "
"might appear to be doing nothing. Please be patient!")
stageinterval, pollinterval = _monitorinitialise(jobs)
allcomplete = False
allfinished = False
lastpolltime = 0
laststagetime = 0
recoveryfile = jobs[list(jobs.keys())[0]]["recoveryfile"]
saverecoveryfile = True
recoveryfileerror = False
# Loop until all jobs are done.
while allcomplete is False:
now = time.time()
# Check if we should be polling.
if int(now - lastpolltime) > int(pollinterval):
lastpolltime = int(now)
saverecoveryfile = _polljobs(jobs, saverecoveryfile)
saverecoveryfile = _checkwaitingjobs(jobs, saverecoveryfile)
# Check if we should be staging.
if ((int(now - laststagetime) > int(stageinterval) and
int(stageinterval) != 0) or allfinished is True):
laststagetime = int(now)
saverecoveryfile = _stagejobfiles(jobs, saverecoveryfile)
# Update the queue info settings to each job just in case something
# happens requiring user to use recovery.
for job in jobs:
jobs[job]["queue-slots"] = \
QUEUEINFO[jobs[job]["resource"]]["queue-slots"]
jobs[job]["queue-max"] = \
QUEUEINFO[jobs[job]["resource"]]["queue-max"]
# Save out the recovery files.
if (os.path.isdir(os.path.expanduser('~/.longbow')) and
saverecoveryfile is True and recoveryfileerror is False and
recoveryfile != ""):
saverecoveryfile = False
try:
configuration.saveini(recoveryfile, jobs)
except (OSError, IOError):
recoveryfileerror = True
LOG.warning("Could not write recovery file, possibly due to "
"permissions on the ~/.longbow directory.")
allcomplete, allfinished = _checkcomplete(jobs)
# Sane time interval (CPU core maxes out easily otherwise).
time.sleep(1.0)
complete = 0
error = 0
for job in jobs:
if jobs[job]["laststatus"] == "Submit Error":
error = error + 1
else:
complete = complete + 1
LOG.info("Session complete - %s jobs ran - %s jobs encountered submission "
"errors.", complete, error)
def prepare(jobs):
"""A generic method for creating job submit scripts.
A method containing the generic and boiler plate Longbow code for
constructing the submit file.
Required arguments are:
jobs (dictionary) - The Longbow jobs data structure, see configuration.py
for more information about the format of this
structure.
"""
LOG.info("Creating submit files for job/s.")
for item in jobs:
job = jobs[item]
scheduler = job["scheduler"]
try:
LOG.info("Creating submit file for job '%s'", item)
getattr(schedulers, scheduler.lower()).prepare(job)
LOG.info("Submit file created successfully")
except AttributeError:
raise exceptions.PluginattributeError(
"prepare method cannot be found in plugin '{0}'"
.format(scheduler))
LOG.info("Submit file/s created.")
def submit(jobs):
"""A generic method for submitting jobs.
A method containing the generic and boiler plate Longbow code for
submitting a job.
Required arguments are:
jobs (dictionary) - The Longbow jobs data structure, see configuration.py
for more information about the format of this
structure.
"""
# Initialise some counters.
submitted = 0
queued = 0
error = 0
LOG.info("Submitting job/s.")
for item in jobs:
job = jobs[item]
# Have we got this resource already?
if job["resource"] not in QUEUEINFO:
# no, well create it.
QUEUEINFO[job["resource"]] = {"queue-slots": str(0),
"queue-max": str(0)}
for item in jobs:
job = jobs[item]
scheduler = job["scheduler"]
# Try and submit.
try:
getattr(schedulers, scheduler.lower()).submit(job)
LOG.info("Job '%s' submitted with id '%s'", item, job["jobid"])
job["laststatus"] = "Queued"
# Increment the queue counter by one (used to count the slots).
QUEUEINFO[job["resource"]]["queue-slots"] = \
str(int(QUEUEINFO[job["resource"]]["queue-slots"]) + 1)
submitted += 1
# Submit method can't be found.
except AttributeError:
raise exceptions.PluginattributeError(
"submit method cannot be found in plugin '{0}'"
.format(scheduler))
# Some sort of error in submitting the job.
except exceptions.JobsubmitError as err:
LOG.error(err)
job["laststatus"] = "Submit Error"
error += 1
# Hit maximum slots on resource, Longbow will sub-schedule these.
except exceptions.QueuemaxError:
LOG.info("The job '%s' has been held back by Longbow due to "
"reaching queue slot limit, it will be submitted when a "
"slot opens up.", item)
# We will set a flag so that we can inform the user that it is
# handled.
job["laststatus"] = "Waiting Submission"
queued += 1
# We want to find out what the maximum number of slots we have are.
if int(QUEUEINFO[job["resource"]]["queue-slots"]) > \
int(QUEUEINFO[job["resource"]]["queue-max"]):
QUEUEINFO[job["resource"]]["queue-max"] = \
QUEUEINFO[job["resource"]]["queue-slots"]
# Store a copy of the queueinfo data in the jobs data structure in case
# recovery is needed.
for item in jobs:
job = jobs[item]
job["queue-slots"] = QUEUEINFO[job["resource"]]["queue-slots"]
job["queue-max"] = QUEUEINFO[job["resource"]]["queue-max"]
# Save out the recovery files.
if (os.path.isdir(os.path.expanduser('~/.longbow')) and
job["recoveryfile"] != ""):
try:
LOG.info("Recovery file will be placed at path '%s'",
job["recoveryfile"])
configuration.saveini(job["recoveryfile"], jobs)
except (OSError, IOError):
LOG.warning(
"Could not write recovery file, possibly due to permissions "
"on the ~/.longbow directory.")
LOG.info("%s Submitted, %s Held due to queue limits and %s Failed.",
submitted, queued, error)
def _testscheduler(job):
"""The test logic for finding out what scheduler is on the system."""
schedulerqueries = getattr(schedulers, "QUERY")
LOG.info("No environment for this host '%s' is specified - attempting to "
"determine it!", job["resource"])
# Go through the schedulers we are supporting.
for param in schedulerqueries:
try:
shellwrappers.sendtossh(job, schedulerqueries[param])
job["scheduler"] = param
LOG.info("The environment on this host is '%s'", param)
break
except exceptions.SSHError:
LOG.debug("Environment is not '%s'", param)
if job["scheduler"] is "":
raise exceptions.SchedulercheckError("Could not find the job "
"scheduling system.")
def _testhandler(job):
"""A method for finding out job handler is on the system."""
# Initialise variables.
handlers = {
"aprun": ["which aprun"],
"mpirun": ["which mpirun"]
}
LOG.info("No queue handler was specified for host '%s' - attempting to "
"find it", job["resource"])
modules = []
# Go through the handlers and find out which is there. Load modules first
# as this is necessary for some remote resources
for module in job["modules"].split(","):
module = module.replace(" ", "")
modules.extend(["module load " + module + "\n"])
for param in handlers:
try:
cmd = modules[:]
cmd.extend(handlers[param])
shellwrappers.sendtossh(job, cmd)
job["handler"] = param
LOG.info("The batch queue handler is '%s'", param)
break
except exceptions.SSHError:
LOG.debug("The batch queue handler is not '%s'", param)
if job["handler"] is "":
raise exceptions.HandlercheckError("Could not find the batch queue "
"handler.")
def _monitorinitialise(jobs):
"""Setup the conditions for monitoring jobs."""
# Initialise values.
pollinterval = 0
stageinterval = 0
# Sort out some defaults.
for job in jobs:
# If we came from recovery mode then rebuild the queueinfo structure.
if jobs[job]["resource"] not in QUEUEINFO:
QUEUEINFO[jobs[job]["resource"]] = {
"queue-slots": jobs[job]["queue-slots"],
"queue-max": jobs[job]["queue-max"]
}
# This should always be present.
if "laststatus" not in jobs[job]:
jobs[job]["laststatus"] = ""
# Set the file transfer interval.
if stageinterval < int(jobs[job]["staging-frequency"]):
stageinterval = int(jobs[job]["staging-frequency"])
# Attempt to grab a polling frequency that might have been set
if pollinterval < int(jobs[job]["polling-frequency"]):
pollinterval = int(jobs[job]["polling-frequency"])
# If somehow the polling interval parameter is still zero, reduce the
# polling to once every 5 minutes.
if pollinterval is 0:
pollinterval = 300
return stageinterval, pollinterval
def _polljobs(jobs, save):
"""A method to poll the status of all jobs.
Poll the status of all jobs that are not in error states, queued or
finihed.
"""
for job in jobs:
if (jobs[job]["laststatus"] != "Finished" and
jobs[job]["laststatus"] != "Complete" and
jobs[job]["laststatus"] != "Submit Error" and
jobs[job]["laststatus"] != "Waiting Submission"):
# Get the job status.
try:
status = getattr(
schedulers, jobs[job]["scheduler"].lower()).status(
jobs[job])
except AttributeError:
raise exceptions.PluginattributeError(
"Status method cannot be"
"found in plugin '{0}'".format(jobs[job]["scheduler"]))
# If the last status is different then change the flag (stops
# logfile getting flooded!)
if jobs[job]["laststatus"] != status:
jobs[job]["laststatus"] = status
save = True
if status == "Finished":
resource = jobs[job]["resource"]
QUEUEINFO[resource]["queue-slots"] = \
str(int(QUEUEINFO[resource]["queue-slots"]) - 1)
LOG.info("Status of job '%s' with id '%s' is '%s'", job,
jobs[job]["jobid"], status)
return save
def _stagejobfiles(jobs, save):
"""A method to stage all files for each running job.
Stage all files for each running job. For jobs that are finished, stage
and remove them from the QUEUEINFO data and then change their status to
complete. This will stop future staging.
"""
for job in jobs:
if (jobs[job]["laststatus"] == "Running" or
jobs[job]["laststatus"] == "Subjob(s) running" or
jobs[job]["laststatus"] == "Finished"):
staging.stage_downstream(jobs[job])
if jobs[job]["laststatus"] == "Finished":
jobs[job]["laststatus"] = "Complete"
save = True
return save
def _checkwaitingjobs(jobs, save):
"""Check if any jobs marked as "Waiting Submission" can be submitted."""
for job in jobs:
# Check if we can submit any further jobs.
if (jobs[job]["laststatus"] == "Waiting Submission" and
int(QUEUEINFO[jobs[job]["resource"]]["queue-slots"]) <
int(QUEUEINFO[jobs[job]["resource"]]["queue-max"])):
# Try and submit this job.
try:
getattr(schedulers,
jobs[job]["scheduler"].lower()).submit(jobs[job])
jobs[job]["laststatus"] = "Queued"
LOG.info("Job '%s' submitted with id '%s'", job,
jobs[job]["jobid"])
# Increment the queue counter by one (used to count the slots).
QUEUEINFO[jobs[job]["resource"]]["queue-slots"] = str(
int(QUEUEINFO[jobs[job]["resource"]]["queue-slots"]) + 1)
save = True
except AttributeError:
# Submit method can't be found.
raise exceptions.PluginattributeError(
"Submit method cannot be found in plugin '{0}'"
.format(jobs[job]["scheduler"]))
# Some sort of error in submitting the job.
except exceptions.JobsubmitError as err:
LOG.error(err)
jobs[job]["laststatus"] = "Submit Error"
# This time if a queue error is raised it might be due to other
# constraints such as resource limits on the queue.
except exceptions.QueuemaxError:
LOG.error("Job is still failing to submit, which could "
"indicate problems with resource limits for this "
"particular queue - marking this as in error state")
jobs[job]["laststatus"] = "Submit Error"
return save
def _checkcomplete(jobs):
"""Check if all the jobs are complete."""
# Initialise variables
allcomplete = False
allfinished = False
complete = []
error = []
finished = []
for job in jobs:
if jobs[job]["laststatus"] != "Submit Error":
complete.append(jobs[job]["laststatus"])
if (jobs[job]["laststatus"] != "Submit Error" and
jobs[job]["laststatus"] != "Complete"):
finished.append(jobs[job]["laststatus"])
if jobs[job]["laststatus"] == "Submit Error":
error.append(jobs[job]["laststatus"])
if all(state == "Complete" for state in complete) and len(complete) != 0:
allcomplete = True
if len(error) == len(jobs):
allcomplete = True
if all(state == "Finished" for state in finished) and len(finished) != 0:
allfinished = True
return allcomplete, allfinished