|
23 | 23 | validator, |
24 | 24 | ) |
25 | 25 | from pydantic.networks import AnyUrl |
26 | | -from pydantic.types import ByteSize, PositiveFloat |
| 26 | +from pydantic.types import ByteSize, NonNegativeInt, PositiveFloat |
27 | 27 |
|
28 | 28 |
|
29 | 29 | class WorkerMetrics(BaseModel): |
30 | | - cpu: float = Field(..., description="consumed # of cpus") |
| 30 | + cpu: float = Field(..., description="consumed % of cpus") |
31 | 31 | memory: ByteSize = Field(..., description="consumed memory") |
32 | 32 | num_fds: int = Field(..., description="consumed file descriptors") |
33 | | - ready: int = Field(..., description="# tasks ready to run") |
34 | | - executing: int = Field(..., description="# tasks currently executing") |
35 | | - in_flight: int = Field(..., description="# tasks currenntly waiting for data") |
36 | | - in_memory: ByteSize = Field(..., description="result data still in memory") |
| 33 | + ready: NonNegativeInt = Field(..., description="# tasks ready to run") |
| 34 | + executing: NonNegativeInt = Field(..., description="# tasks currently executing") |
| 35 | + in_flight: NonNegativeInt = Field(..., description="# tasks waiting for data") |
| 36 | + in_memory: NonNegativeInt = Field(..., description="# tasks in worker memory") |
| 37 | + |
| 38 | + |
| 39 | +AvailableResources = DictModel[str, PositiveFloat] |
| 40 | + |
| 41 | + |
| 42 | +class UsedResources(DictModel[str, NonNegativeFloat]): |
| 43 | + @root_validator(pre=True) |
| 44 | + @classmethod |
| 45 | + def ensure_negative_value_is_zero(cls, values): |
| 46 | + # dasks adds/remove resource values and sometimes |
| 47 | + # they end up being negative instead of 0 |
| 48 | + if v := values.get("__root__", {}): |
| 49 | + for res_key, res_value in v.items(): |
| 50 | + if res_value < 0: |
| 51 | + v[res_key] = 0 |
| 52 | + return values |
37 | 53 |
|
38 | 54 |
|
39 | 55 | class Worker(BaseModel): |
40 | 56 | id: str |
41 | 57 | name: str |
42 | | - resources: DictModel[str, PositiveFloat] |
43 | | - used_resources: DictModel[str, NonNegativeFloat] |
| 58 | + resources: AvailableResources |
| 59 | + used_resources: UsedResources |
44 | 60 | memory_limit: ByteSize |
45 | 61 | metrics: WorkerMetrics |
46 | 62 |
|
|
0 commit comments