|
14 | 14 | Field, |
15 | 15 | NonNegativeFloat, |
16 | 16 | NonNegativeInt, |
| 17 | + StrictFloat, |
| 18 | + StrictInt, |
17 | 19 | StringConstraints, |
18 | 20 | field_validator, |
19 | 21 | ) |
20 | 22 | from pydantic.config import JsonDict |
21 | 23 | from types_aiobotocore_ec2.literals import InstanceStateNameType, InstanceTypeType |
22 | 24 |
|
| 25 | +GenericResourceValue: TypeAlias = StrictInt | StrictFloat | str |
| 26 | + |
23 | 27 |
|
24 | 28 | class Resources(BaseModel, frozen=True): |
25 | 29 | cpus: NonNegativeFloat |
26 | 30 | ram: ByteSize |
| 31 | + generic_resources: Annotated[ |
| 32 | + dict[str, GenericResourceValue], |
| 33 | + Field( |
| 34 | + default_factory=dict, |
| 35 | + description=( |
| 36 | + "Arbitrary additional resources (e.g. {'threads': 8}). " |
| 37 | + "Numeric values are treated as quantities and participate in add/sub/compare." |
| 38 | + ), |
| 39 | + ), |
| 40 | + ] = DEFAULT_FACTORY |
27 | 41 |
|
28 | 42 | @classmethod |
29 | 43 | def create_as_empty(cls) -> "Resources": |
30 | 44 | return cls(cpus=0, ram=ByteSize(0)) |
31 | 45 |
|
32 | 46 | def __ge__(self, other: "Resources") -> bool: |
33 | | - return self.cpus >= other.cpus and self.ram >= other.ram |
| 47 | + if not (self.cpus >= other.cpus and self.ram >= other.ram): |
| 48 | + return False |
| 49 | + # ensure all numeric generic resources in `other` are satisfied by `self` |
| 50 | + for k, v in other.generic_resources.items(): |
| 51 | + if isinstance(v, int | float): |
| 52 | + lhs_val = self.generic_resources.get(k, 0) |
| 53 | + if not isinstance(lhs_val, int | float) or lhs_val < v: |
| 54 | + return False |
| 55 | + continue |
| 56 | + # non-numeric must be equal and present |
| 57 | + if k not in self.generic_resources or self.generic_resources[k] != v: |
| 58 | + return False |
| 59 | + return True |
34 | 60 |
|
35 | 61 | def __gt__(self, other: "Resources") -> bool: |
36 | | - return self.cpus > other.cpus or self.ram > other.ram |
| 62 | + if self.cpus > other.cpus or self.ram > other.ram: |
| 63 | + return True |
| 64 | + for k, v in other.generic_resources.items(): |
| 65 | + lhs_val = self.generic_resources.get(k) |
| 66 | + if ( |
| 67 | + isinstance(v, int | float) |
| 68 | + and isinstance(lhs_val, int | float) |
| 69 | + and lhs_val > v |
| 70 | + ): |
| 71 | + return True |
| 72 | + if not isinstance(v, int | float) and lhs_val is not None and lhs_val != v: |
| 73 | + return True |
| 74 | + return False |
37 | 75 |
|
38 | 76 | def __add__(self, other: "Resources") -> "Resources": |
| 77 | + """operator for adding two Resources |
| 78 | + Note that only numeric generic resources are added |
| 79 | + Non-numeric generic resources are ignored |
| 80 | + """ |
| 81 | + merged: dict[str, GenericResourceValue] = {} |
| 82 | + keys = set(self.generic_resources) | set(other.generic_resources) |
| 83 | + for k in keys: |
| 84 | + a = self.generic_resources.get(k) |
| 85 | + b = other.generic_resources.get(k) |
| 86 | + # adding non numeric values does not make sense, so we skip those for the resulting resource |
| 87 | + if isinstance(a, int | float) and isinstance(b, int | float): |
| 88 | + merged[k] = a + b |
| 89 | + elif a is None and isinstance(b, int | float): |
| 90 | + merged[k] = b |
| 91 | + elif b is None and isinstance(a, int | float): |
| 92 | + merged[k] = a |
| 93 | + |
39 | 94 | return Resources.model_construct( |
40 | | - **{ |
41 | | - key: a + b |
42 | | - for (key, a), b in zip( |
43 | | - self.model_dump().items(), other.model_dump().values(), strict=True |
44 | | - ) |
45 | | - } |
| 95 | + cpus=self.cpus + other.cpus, |
| 96 | + ram=self.ram + other.ram, |
| 97 | + generic_resources=merged, |
46 | 98 | ) |
47 | 99 |
|
48 | 100 | def __sub__(self, other: "Resources") -> "Resources": |
| 101 | + """operator for subtracting two Resources |
| 102 | + Note that only numeric generic resources are subtracted |
| 103 | + Non-numeric generic resources are ignored |
| 104 | + """ |
| 105 | + merged: dict[str, GenericResourceValue] = {} |
| 106 | + keys = set(self.generic_resources) | set(other.generic_resources) |
| 107 | + for k in keys: |
| 108 | + a = self.generic_resources.get(k) |
| 109 | + b = other.generic_resources.get(k) |
| 110 | + # subtracting non numeric values does not make sense, so we skip those for the resulting resource |
| 111 | + if isinstance(a, int | float) and isinstance(b, int | float): |
| 112 | + merged[k] = a - b |
| 113 | + elif a is None and isinstance(b, int | float): |
| 114 | + merged[k] = -b |
| 115 | + elif b is None and isinstance(a, int | float): |
| 116 | + merged[k] = a |
| 117 | + |
49 | 118 | return Resources.model_construct( |
50 | | - **{ |
51 | | - key: a - b |
52 | | - for (key, a), b in zip( |
53 | | - self.model_dump().items(), other.model_dump().values(), strict=True |
54 | | - ) |
55 | | - } |
| 119 | + cpus=self.cpus - other.cpus, |
| 120 | + ram=self.ram - other.ram, |
| 121 | + generic_resources=merged, |
| 122 | + ) |
| 123 | + |
| 124 | + def __hash__(self) -> int: |
| 125 | + """Deterministic hash including cpus, ram (in bytes) and generic_resources.""" |
| 126 | + # sort generic_resources items to ensure order-independent hashing |
| 127 | + generic_items: tuple[tuple[str, GenericResourceValue], ...] = tuple( |
| 128 | + sorted(self.generic_resources.items()) |
56 | 129 | ) |
| 130 | + return hash((self.cpus, self.ram, generic_items)) |
57 | 131 |
|
58 | 132 | @field_validator("cpus", mode="before") |
59 | 133 | @classmethod |
@@ -180,7 +254,9 @@ def validate_bash_calls(cls, v): |
180 | 254 | temp_file.writelines(v) |
181 | 255 | temp_file.flush() |
182 | 256 | # NOTE: this will not capture runtime errors, but at least some syntax errors such as invalid quotes |
183 | | - sh.bash("-n", temp_file.name) # pyright: ignore[reportCallIssue] # sh is untyped, but this call is safe for bash syntax checking |
| 257 | + sh.bash( |
| 258 | + "-n", temp_file.name |
| 259 | + ) # pyright: ignore[reportCallIssue] # sh is untyped, but this call is safe for bash syntax checking |
184 | 260 | except sh.ErrorReturnCode as exc: |
185 | 261 | msg = f"Invalid bash call in custom_boot_scripts: {v}, Error: {exc.stderr}" |
186 | 262 | raise ValueError(msg) from exc |
|
0 commit comments