-
Notifications
You must be signed in to change notification settings - Fork 49
/
constraints.py
79 lines (65 loc) · 2.57 KB
/
constraints.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
##############################################################
# Copyright 2022 Lawrence Livermore National Security, LLC
# (c.f. AUTHORS, NOTICE.LLNS, COPYING)
#
# This file is part of the Flux resource manager framework.
# For details, see https://github.com/flux-framework.
#
# SPDX-License-Identifier: LGPL-3.0
##############################################################
"""Apply constraints to incoming jobspec based on broker config.
"""
from flux.job.frobnicator import FrobnicatorPlugin
class QueueConfig:
"""Convenience class for handling jobspec queues configuration"""
def __init__(self, config={}):
self.queues = {}
try:
self.queues = config["queues"]
except KeyError:
pass
def queue_properties(self, name):
try:
return self.queues[name]["requires"]
except KeyError:
return None
def apply_constraints(self, jobspec):
"""Apply queue-specific constraints to jobspec"""
if jobspec.queue:
if jobspec.queue not in self.queues:
raise ValueError(f"Invalid queue '{jobspec.queue}' specified")
queue_properties = self.queue_properties(jobspec.queue)
if queue_properties is None:
return
# First try appending to existing constraints
try:
spec = jobspec.attributes["system"]["constraints"]["properties"]
for prop in queue_properties:
if prop not in spec:
spec.append(prop)
return
except KeyError:
# No "properties" operator at top level, try combining
# existing constraints with logical AND
pass
try:
jobspec.setattr(
"system.constraints",
{
"and": [
jobspec.attributes["system"]["constraints"],
{"properties": queue_properties},
]
},
)
except KeyError:
# No existing "constraints" - set constraints to queue
# constraints
jobspec.setattr("system.constraints", {"properties": queue_properties})
class Frobnicator(FrobnicatorPlugin):
def __init__(self, parser):
super().__init__(parser)
def configure(self, args, config):
self.config = QueueConfig(config)
def frob(self, jobspec, user, urgency, flags):
self.config.apply_constraints(jobspec)