-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
don't create scancel commands for users without jobs #137
Open
wdpypere
wants to merge
10
commits into
hpcugent:master
Choose a base branch
from
wdpypere:slurm_acct
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
2a276d5
make limit configurable
wdpypere e054019
add function to get active jobs from clusters
wdpypere 238b92f
don't fail silently
wdpypere 5df7002
only add scancel jobs if a user actually has jobs
wdpypere dacb031
add limits
wdpypere 7b0a145
fix line too long
wdpypere 7c89257
add mock test
wdpypere ffbdca6
reshuffle
wdpypere 8e85fef
remove unused global
wdpypere da1e118
fix context manager
wdpypere File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
# | ||
# Copyright 2013-2022 Ghent University | ||
# | ||
# This file is part of vsc-administration, | ||
# originally created by the HPC team of Ghent University (http://ugent.be/hpc/en), | ||
# with support of Ghent University (http://ugent.be/hpc), | ||
# the Flemish Supercomputer Centre (VSC) (https://www.vscentrum.be), | ||
# the Flemish Research Foundation (FWO) (http://www.fwo.be/en) | ||
# and the Department of Economy, Science and Innovation (EWI) (http://www.ewi-vlaanderen.be/en). | ||
# | ||
# https://github.com/hpcugent/vsc-administration | ||
# | ||
# All rights reserved. | ||
# | ||
""" | ||
sacct commands | ||
""" | ||
import logging | ||
import re | ||
from enum import Enum | ||
|
||
from vsc.accountpage.wrappers import mkNamedTupleInstance | ||
from vsc.config.base import ANTWERPEN, BRUSSEL, GENT, LEUVEN | ||
from vsc.utils.missing import namedtuple_with_defaults | ||
from vsc.utils.run import asyncloop | ||
|
||
SLURM_SACCT = "/usr/bin/sacct" | ||
|
||
SLURM_ORGANISATIONS = { | ||
ANTWERPEN: 'uantwerpen', | ||
BRUSSEL: 'vub', | ||
GENT: 'ugent', | ||
LEUVEN: 'kuleuven', | ||
} | ||
|
||
|
||
class SacctParseException(Exception): | ||
pass | ||
|
||
class SacctException(Exception): | ||
pass | ||
|
||
|
||
class SacctTypes(Enum): | ||
jobs = "jobs" | ||
|
||
|
||
# Fields for Slurm 20.11. | ||
# FIXME: at some point this should be versioned | ||
|
||
SacctJobsFields = [ | ||
"JobID", "JobName", "Partition", "Account", "AllocCPUS", "State", "ExitCode", | ||
] | ||
|
||
SlurmJobs = namedtuple_with_defaults('SlurmJobs', SacctJobsFields) | ||
|
||
def mkSlurmJobs(fields): | ||
"""Make a named tuple from the given fields""" | ||
activejobs = mkNamedTupleInstance(fields, SlurmJobs) | ||
return activejobs | ||
|
||
def parse_slurm_sacct_line(header, line, info_type): | ||
"""Parse the line into the correct data type.""" | ||
fields = line.split("|") | ||
|
||
if info_type == SacctTypes.jobs: | ||
creator = mkSlurmJobs | ||
else: | ||
raise SacctParseException("info_type %s does not exist.", info_type) | ||
|
||
return creator(dict(zip(header, fields))) | ||
|
||
|
||
def parse_slurm_sacct_dump(lines, info_type): | ||
"""Parse the sacctmgr dump from the listing.""" | ||
acct_info = set() | ||
|
||
header = [w.replace(' ', '_').replace('%', 'PCT_') for w in lines[0].rstrip().split("|")] | ||
|
||
for line in lines[1:]: | ||
logging.debug("line %s", line) | ||
line = line.rstrip() | ||
try: | ||
info = parse_slurm_sacct_line(header, line, info_type) | ||
except Exception as err: | ||
logging.exception("Slurm sacct parse dump: could not process line %s [%s]", line, err) | ||
raise | ||
# This fails when we get e.g., the users and look at the account lines. | ||
# We should them just skip that line instead of raising an exception | ||
if info: | ||
acct_info.add(info) | ||
|
||
return acct_info | ||
|
||
|
||
def get_slurm_sacct_active_jobs_for_user(user): | ||
""" | ||
Get running and queued jobs for user. | ||
""" | ||
(exitcode, contents) = asyncloop([ | ||
SLURM_SACCT, "--allclusters", "--parsable2", "--state", "RUNNING,PENDING", "--user", user]) | ||
if exitcode != 0: | ||
if re.search("sacct: error: Invalid user id: %s" % user, contents): | ||
logging.warning("User %s does not exist, assuming no active jobs.", user) | ||
return None | ||
else: | ||
raise SacctException("Cannot run sacct") | ||
|
||
info = parse_slurm_sacct_dump(contents.splitlines(), SacctJobsFields) | ||
return info |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# | ||
# Copyright 2015-2022 Ghent University | ||
# | ||
# This file is part of vsc-administration, | ||
# originally created by the HPC team of Ghent University (http://ugent.be/hpc/en), | ||
# with support of Ghent University (http://ugent.be/hpc), | ||
# the Flemish Supercomputer Centre (VSC) (https://www.vscentrum.be), | ||
# the Flemish Research Foundation (FWO) (http://www.fwo.be/en) | ||
# and the Department of Economy, Science and Innovation (EWI) (http://www.ewi-vlaanderen.be/en). | ||
# | ||
# https://github.com/hpcugent/vsc-administration | ||
# | ||
# All rights reserved. | ||
# | ||
""" | ||
Tests for vsc.administration.slurm.* | ||
|
||
@author: Andy Georges (Ghent University) | ||
""" | ||
|
||
from vsc.install.testing import TestCase | ||
|
||
from vsc.administration.slurm.sacct import parse_slurm_sacct_dump, SlurmJobs, SacctTypes, SacctParseException | ||
|
||
|
||
class SlurmSacctmgrTest(TestCase): | ||
def test_parse_slurmm_sacct_dump(self): | ||
"""Test that the sacct output is correctly processed.""" | ||
|
||
sacct_active_jobs_output = [ | ||
"JobID|JobName|Partition|Account|AllocCPUS|State|ExitCode", | ||
"14367800|normal|part1|acc1|1|RUNNING|0:0", | ||
"14367800.batch|batch||acc1|1|RUNNING|0:0", | ||
"14367800.extern|extern||acc1|1|RUNNING|0:0", | ||
] | ||
info = parse_slurm_sacct_dump(sacct_active_jobs_output, SacctTypes.jobs) | ||
self.assertEqual(set(info), set([ | ||
SlurmJobs(JobID='14367800', JobName='normal', Partition='part1', Account='acc1', AllocCPUS='1', State='RUNNING', ExitCode='0:0'), | ||
SlurmJobs(JobID='14367800.batch', JobName='batch', Partition='', Account='acc1', AllocCPUS='1', State='RUNNING', ExitCode='0:0'), | ||
SlurmJobs(JobID='14367800.extern', JobName='extern', Partition='', Account='acc1', AllocCPUS='1', State='RUNNING', ExitCode='0:0') | ||
])) | ||
|
||
|
||
with self.assertRaises(SacctParseException): | ||
parse_slurm_sacct_dump("sacct_active_jobs_output", "doesnotexist") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a lot of boilerplate, but this is the main change.