Skip to content

Commit

Permalink
refactor(adaptive-core): improved readability recommendations
Browse files Browse the repository at this point in the history
  • Loading branch information
Vesyrak committed Dec 28, 2023
1 parent 81774d4 commit 6ca6212
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 19 deletions.
4 changes: 2 additions & 2 deletions distributed/deploy/adaptive.py
Expand Up @@ -8,7 +8,7 @@
import dask.config
from dask.utils import parse_timedelta

from distributed.deploy.adaptive_core import AdaptiveCore
from distributed.deploy.adaptive_core import AdaptiveCore, Recommendation

Check warning on line 11 in distributed/deploy/adaptive.py

View check run for this annotation

Codecov / codecov/patch

distributed/deploy/adaptive.py#L11

Added line #L11 was not covered by tests
from distributed.protocol import pickle
from distributed.utils import log_errors

Expand Down Expand Up @@ -152,7 +152,7 @@ async def target(self):
target_duration=self.target_duration
)

async def recommendations(self, target: int) -> dict:
async def recommendations(self, target: int) -> Recommendation:

Check warning on line 155 in distributed/deploy/adaptive.py

View check run for this annotation

Codecov / codecov/patch

distributed/deploy/adaptive.py#L155

Added line #L155 was not covered by tests
if len(self.plan) != len(self.requested):
# Ensure that the number of planned and requested workers
# are in sync before making recommendations.
Expand Down
52 changes: 35 additions & 17 deletions distributed/deploy/adaptive_core.py
Expand Up @@ -4,7 +4,9 @@
import math
from collections import defaultdict, deque
from collections.abc import Iterable
from dataclasses import asdict, dataclass, field

Check warning on line 7 in distributed/deploy/adaptive_core.py

View check run for this annotation

Codecov / codecov/patch

distributed/deploy/adaptive_core.py#L7

Added line #L7 was not covered by tests
from datetime import timedelta
from enum import Enum, auto

Check warning on line 9 in distributed/deploy/adaptive_core.py

View check run for this annotation

Codecov / codecov/patch

distributed/deploy/adaptive_core.py#L9

Added line #L9 was not covered by tests
from typing import TYPE_CHECKING, cast

import tlz as toolz
Expand All @@ -23,6 +25,22 @@
logger = logging.getLogger(__name__)


class RecommendationStatus(Enum):
Up = auto()
Down = auto()
Same = auto()

Check warning on line 31 in distributed/deploy/adaptive_core.py

View check run for this annotation

Codecov / codecov/patch

distributed/deploy/adaptive_core.py#L28-L31

Added lines #L28 - L31 were not covered by tests


@dataclass
class Recommendation:
status: RecommendationStatus
workers: list = field(default_factory=list)
n: int = 0

Check warning on line 38 in distributed/deploy/adaptive_core.py

View check run for this annotation

Codecov / codecov/patch

distributed/deploy/adaptive_core.py#L34-L38

Added lines #L34 - L38 were not covered by tests


RS = RecommendationStatus

Check warning on line 41 in distributed/deploy/adaptive_core.py

View check run for this annotation

Codecov / codecov/patch

distributed/deploy/adaptive_core.py#L41

Added line #L41 was not covered by tests


class AdaptiveCore:
"""
The core logic for adaptive deployments, with none of the cluster details
Expand Down Expand Up @@ -169,13 +187,13 @@ async def safe_target(self) -> int:

return n

async def scale_down(self, n: int) -> None:
async def scale_down(self, workers: Iterable) -> None:

Check warning on line 190 in distributed/deploy/adaptive_core.py

View check run for this annotation

Codecov / codecov/patch

distributed/deploy/adaptive_core.py#L190

Added line #L190 was not covered by tests
raise NotImplementedError()

async def scale_up(self, workers: Iterable) -> None:
async def scale_up(self, n: int) -> None:

Check warning on line 193 in distributed/deploy/adaptive_core.py

View check run for this annotation

Codecov / codecov/patch

distributed/deploy/adaptive_core.py#L193

Added line #L193 was not covered by tests
raise NotImplementedError()

async def recommendations(self, target: int) -> dict:
async def recommendations(self, target: int) -> Recommendation:

Check warning on line 196 in distributed/deploy/adaptive_core.py

View check run for this annotation

Codecov / codecov/patch

distributed/deploy/adaptive_core.py#L196

Added line #L196 was not covered by tests
"""
Make scale up/down recommendations based on current state and target
"""
Expand All @@ -185,11 +203,11 @@ async def recommendations(self, target: int) -> dict:

if target == len(plan):
self.close_counts.clear()
return {"status": "same"}
return Recommendation(status=RS.Same)

Check warning on line 206 in distributed/deploy/adaptive_core.py

View check run for this annotation

Codecov / codecov/patch

distributed/deploy/adaptive_core.py#L206

Added line #L206 was not covered by tests

if target > len(plan):
self.close_counts.clear()
return {"status": "up", "n": target}
return Recommendation(status=RS.Up, n=target)

Check warning on line 210 in distributed/deploy/adaptive_core.py

View check run for this annotation

Codecov / codecov/patch

distributed/deploy/adaptive_core.py#L210

Added line #L210 was not covered by tests

# target < len(plan)
not_yet_arrived = requested - observed
Expand All @@ -212,9 +230,11 @@ async def recommendations(self, target: int) -> dict:
del self.close_counts[k]

if firmly_close:
return {"status": "down", "workers": list(firmly_close)}
return Recommendation(

Check warning on line 233 in distributed/deploy/adaptive_core.py

View check run for this annotation

Codecov / codecov/patch

distributed/deploy/adaptive_core.py#L233

Added line #L233 was not covered by tests
status=RecommendationStatus.Down, workers=list(firmly_close)
)
else:
return {"status": "same"}
return Recommendation(status=RS.Same)

Check warning on line 237 in distributed/deploy/adaptive_core.py

View check run for this annotation

Codecov / codecov/patch

distributed/deploy/adaptive_core.py#L237

Added line #L237 was not covered by tests

async def adapt(self) -> None:
"""
Expand All @@ -229,18 +249,16 @@ async def adapt(self) -> None:

try:
target = await self.safe_target()
recommendations = await self.recommendations(target)
recommendation = await self.recommendations(target)

Check warning on line 252 in distributed/deploy/adaptive_core.py

View check run for this annotation

Codecov / codecov/patch

distributed/deploy/adaptive_core.py#L252

Added line #L252 was not covered by tests

if recommendations["status"] != "same":
self.log.append((time(), dict(recommendations)))

status = recommendations.pop("status")
if status == "same":
if recommendation.status == RS.Same:

Check warning on line 254 in distributed/deploy/adaptive_core.py

View check run for this annotation

Codecov / codecov/patch

distributed/deploy/adaptive_core.py#L254

Added line #L254 was not covered by tests
return
if status == "up":
await self.scale_up(**recommendations)
if status == "down":
await self.scale_down(**recommendations)
else:
self.log.append((time(), asdict(recommendation)))
if recommendation.status == RS.Up:
await self.scale_up(recommendation.n)
elif recommendation.status == RS.Down:
await self.scale_down(recommendation.workers)

Check warning on line 261 in distributed/deploy/adaptive_core.py

View check run for this annotation

Codecov / codecov/patch

distributed/deploy/adaptive_core.py#L257-L261

Added lines #L257 - L261 were not covered by tests
except OSError:
if status != "down":
logger.error("Adaptive stopping due to error", exc_info=True)
Expand Down

0 comments on commit 6ca6212

Please sign in to comment.