Skip to content

Commit e5b8153

Browse files
committed
replaced ABC by a protocol
1 parent 21e5648 commit e5b8153

File tree

3 files changed

+53
-96
lines changed

3 files changed

+53
-96
lines changed
Lines changed: 14 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
from abc import ABC, abstractmethod
2-
from dataclasses import dataclass
1+
from typing import Protocol
32

43
from aws_library.ec2 import EC2InstanceData, EC2Tags, Resources
54
from fastapi import FastAPI
@@ -10,66 +9,41 @@
109
from ..models import AssociatedInstance
1110

1211

13-
@dataclass
14-
class BaseAutoscaling(ABC): # pragma: no cover
15-
@staticmethod
16-
@abstractmethod
17-
async def get_monitored_nodes(app: FastAPI) -> list[DockerNode]: ...
12+
class BaseAutoscaling(Protocol):
13+
async def get_monitored_nodes(self, app: FastAPI) -> list[DockerNode]: ...
1814

19-
@staticmethod
20-
@abstractmethod
21-
def get_ec2_tags(app: FastAPI) -> EC2Tags: ...
15+
def get_ec2_tags(self, app: FastAPI) -> EC2Tags: ...
2216

23-
@staticmethod
24-
@abstractmethod
2517
def get_new_node_docker_tags(
26-
app: FastAPI, ec2_instance_data: EC2InstanceData
18+
self, app: FastAPI, ec2_instance_data: EC2InstanceData
2719
) -> dict[DockerLabelKey, str]: ...
2820

29-
@staticmethod
30-
@abstractmethod
31-
async def list_unrunnable_tasks(app: FastAPI) -> list: ...
21+
async def list_unrunnable_tasks(self, app: FastAPI) -> list: ...
3222

33-
@staticmethod
34-
@abstractmethod
35-
def get_task_required_resources(task) -> Resources: ...
23+
def get_task_required_resources(self, task) -> Resources: ...
3624

37-
@staticmethod
38-
@abstractmethod
3925
async def get_task_defined_instance(
40-
app: FastAPI, task
26+
self, app: FastAPI, task
4127
) -> InstanceTypeType | None: ...
4228

43-
@staticmethod
44-
@abstractmethod
4529
async def compute_node_used_resources(
46-
app: FastAPI, instance: AssociatedInstance
30+
self, app: FastAPI, instance: AssociatedInstance
4731
) -> Resources: ...
4832

49-
@staticmethod
50-
@abstractmethod
5133
async def compute_cluster_used_resources(
52-
app: FastAPI, instances: list[AssociatedInstance]
34+
self, app: FastAPI, instances: list[AssociatedInstance]
5335
) -> Resources: ...
5436

55-
@staticmethod
56-
@abstractmethod
5737
async def compute_cluster_total_resources(
58-
app: FastAPI, instances: list[AssociatedInstance]
38+
self, app: FastAPI, instances: list[AssociatedInstance]
5939
) -> Resources: ...
6040

61-
@staticmethod
62-
@abstractmethod
6341
async def is_instance_active(
64-
app: FastAPI, instance: AssociatedInstance
42+
self, app: FastAPI, instance: AssociatedInstance
6543
) -> bool: ...
6644

67-
@staticmethod
68-
@abstractmethod
6945
async def is_instance_retired(
70-
app: FastAPI, instance: AssociatedInstance
46+
self, app: FastAPI, instance: AssociatedInstance
7147
) -> bool: ...
7248

73-
@staticmethod
74-
@abstractmethod
75-
async def try_retire_nodes(app: FastAPI) -> None: ...
49+
async def try_retire_nodes(self, app: FastAPI) -> None: ...

services/autoscaling/src/simcore_service_autoscaling/modules/auto_scaling_mode_computational.py

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
from ..utils import computational_scaling as utils
2525
from ..utils import utils_docker, utils_ec2
2626
from . import dask
27-
from .auto_scaling_mode_base import BaseAutoscaling
2827
from .docker import get_docker_client
2928

3029
_logger = logging.getLogger(__name__)
@@ -42,27 +41,23 @@ def _scheduler_auth(app: FastAPI) -> ClusterAuthentication:
4241
return app_settings.AUTOSCALING_DASK.DASK_SCHEDULER_AUTH
4342

4443

45-
class ComputationalAutoscaling(BaseAutoscaling):
46-
@staticmethod
47-
async def get_monitored_nodes(app: FastAPI) -> list[Node]:
44+
class ComputationalAutoscaling:
45+
async def get_monitored_nodes(self, app: FastAPI) -> list[Node]:
4846
return await utils_docker.get_worker_nodes(get_docker_client(app))
4947

50-
@staticmethod
51-
def get_ec2_tags(app: FastAPI) -> EC2Tags:
48+
def get_ec2_tags(self, app: FastAPI) -> EC2Tags:
5249
app_settings = get_application_settings(app)
5350
return utils_ec2.get_ec2_tags_computational(app_settings)
5451

55-
@staticmethod
5652
def get_new_node_docker_tags(
57-
app: FastAPI, ec2_instance_data: EC2InstanceData
53+
self, app: FastAPI, ec2_instance_data: EC2InstanceData
5854
) -> dict[DockerLabelKey, str]:
5955
assert app # nosec
6056
return {
6157
DOCKER_TASK_EC2_INSTANCE_TYPE_PLACEMENT_CONSTRAINT_KEY: ec2_instance_data.type
6258
}
6359

64-
@staticmethod
65-
async def list_unrunnable_tasks(app: FastAPI) -> list[DaskTask]:
60+
async def list_unrunnable_tasks(self, app: FastAPI) -> list[DaskTask]:
6661
try:
6762
unrunnable_tasks = await dask.list_unrunnable_tasks(
6863
_scheduler_url(app), _scheduler_auth(app)
@@ -87,18 +82,17 @@ async def list_unrunnable_tasks(app: FastAPI) -> list[DaskTask]:
8782
)
8883
return []
8984

90-
@staticmethod
91-
def get_task_required_resources(task) -> Resources:
85+
def get_task_required_resources(self, task) -> Resources:
9286
return utils.resources_from_dask_task(task)
9387

94-
@staticmethod
95-
async def get_task_defined_instance(app: FastAPI, task) -> InstanceTypeType | None:
88+
async def get_task_defined_instance(
89+
self, app: FastAPI, task
90+
) -> InstanceTypeType | None:
9691
assert app # nosec
9792
return cast(InstanceTypeType | None, utils.get_task_instance_restriction(task))
9893

99-
@staticmethod
10094
async def compute_node_used_resources(
101-
app: FastAPI, instance: AssociatedInstance
95+
self, app: FastAPI, instance: AssociatedInstance
10296
) -> Resources:
10397
try:
10498
resource = await dask.get_worker_used_resources(
@@ -127,24 +121,19 @@ async def compute_node_used_resources(
127121
_logger.debug("no resource found for %s", f"{instance.ec2_instance.id}")
128122
return Resources.create_as_empty()
129123

130-
@staticmethod
131124
async def compute_cluster_used_resources(
132-
app: FastAPI, instances: list[AssociatedInstance]
125+
self, app: FastAPI, instances: list[AssociatedInstance]
133126
) -> Resources:
134127
list_of_used_resources: list[Resources] = await logged_gather(
135-
*(
136-
ComputationalAutoscaling.compute_node_used_resources(app, i)
137-
for i in instances
138-
)
128+
*(self.compute_node_used_resources(app, i) for i in instances)
139129
)
140130
counter = collections.Counter({k: 0 for k in Resources.model_fields})
141131
for result in list_of_used_resources:
142132
counter.update(result.model_dump())
143133
return Resources.model_validate(dict(counter))
144134

145-
@staticmethod
146135
async def compute_cluster_total_resources(
147-
app: FastAPI, instances: list[AssociatedInstance]
136+
self, app: FastAPI, instances: list[AssociatedInstance]
148137
) -> Resources:
149138
try:
150139
return await dask.compute_cluster_total_resources(
@@ -153,8 +142,9 @@ async def compute_cluster_total_resources(
153142
except DaskNoWorkersError:
154143
return Resources.create_as_empty()
155144

156-
@staticmethod
157-
async def is_instance_active(app: FastAPI, instance: AssociatedInstance) -> bool:
145+
async def is_instance_active(
146+
self, app: FastAPI, instance: AssociatedInstance
147+
) -> bool:
158148
if not utils_docker.is_node_osparc_ready(instance.node):
159149
return False
160150

@@ -163,14 +153,14 @@ async def is_instance_active(app: FastAPI, instance: AssociatedInstance) -> bool
163153
_scheduler_url(app), _scheduler_auth(app), instance.ec2_instance
164154
)
165155

166-
@staticmethod
167-
async def is_instance_retired(app: FastAPI, instance: AssociatedInstance) -> bool:
156+
async def is_instance_retired(
157+
self, app: FastAPI, instance: AssociatedInstance
158+
) -> bool:
168159
if not utils_docker.is_node_osparc_ready(instance.node):
169160
return False
170161
return await dask.is_worker_retired(
171162
_scheduler_url(app), _scheduler_auth(app), instance.ec2_instance
172163
)
173164

174-
@staticmethod
175-
async def try_retire_nodes(app: FastAPI) -> None:
165+
async def try_retire_nodes(self, app: FastAPI) -> None:
176166
await dask.try_retire_nodes(_scheduler_url(app), _scheduler_auth(app))

services/autoscaling/src/simcore_service_autoscaling/modules/auto_scaling_mode_dynamic.py

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,54 +7,48 @@
77
from ..core.settings import get_application_settings
88
from ..models import AssociatedInstance
99
from ..utils import utils_docker, utils_ec2
10-
from .auto_scaling_mode_base import BaseAutoscaling
1110
from .docker import get_docker_client
1211

1312

14-
class DynamicAutoscaling(BaseAutoscaling):
15-
@staticmethod
16-
async def get_monitored_nodes(app: FastAPI) -> list[Node]:
13+
class DynamicAutoscaling:
14+
async def get_monitored_nodes(self, app: FastAPI) -> list[Node]:
1715
app_settings = get_application_settings(app)
1816
assert app_settings.AUTOSCALING_NODES_MONITORING # nosec
1917
return await utils_docker.get_monitored_nodes(
2018
get_docker_client(app),
2119
node_labels=app_settings.AUTOSCALING_NODES_MONITORING.NODES_MONITORING_NODE_LABELS,
2220
)
2321

24-
@staticmethod
25-
def get_ec2_tags(app: FastAPI) -> EC2Tags:
22+
def get_ec2_tags(self, app: FastAPI) -> EC2Tags:
2623
app_settings = get_application_settings(app)
2724
return utils_ec2.get_ec2_tags_dynamic(app_settings)
2825

29-
@staticmethod
3026
def get_new_node_docker_tags(
31-
app: FastAPI, ec2_instance_data: EC2InstanceData
27+
self, app: FastAPI, ec2_instance_data: EC2InstanceData
3228
) -> dict[DockerLabelKey, str]:
3329
app_settings = get_application_settings(app)
3430
return utils_docker.get_new_node_docker_tags(app_settings, ec2_instance_data)
3531

36-
@staticmethod
37-
async def list_unrunnable_tasks(app: FastAPI) -> list[Task]:
32+
async def list_unrunnable_tasks(self, app: FastAPI) -> list[Task]:
3833
app_settings = get_application_settings(app)
3934
assert app_settings.AUTOSCALING_NODES_MONITORING # nosec
4035
return await utils_docker.pending_service_tasks_with_insufficient_resources(
4136
get_docker_client(app),
4237
service_labels=app_settings.AUTOSCALING_NODES_MONITORING.NODES_MONITORING_SERVICE_LABELS,
4338
)
4439

45-
@staticmethod
46-
def get_task_required_resources(task) -> Resources:
40+
def get_task_required_resources(self, task) -> Resources:
4741
return utils_docker.get_max_resources_from_docker_task(task)
4842

49-
@staticmethod
50-
async def get_task_defined_instance(app: FastAPI, task) -> InstanceTypeType | None:
43+
async def get_task_defined_instance(
44+
self, app: FastAPI, task
45+
) -> InstanceTypeType | None:
5146
return await utils_docker.get_task_instance_restriction(
5247
get_docker_client(app), task
5348
)
5449

55-
@staticmethod
5650
async def compute_node_used_resources(
57-
app: FastAPI, instance: AssociatedInstance
51+
self, app: FastAPI, instance: AssociatedInstance
5852
) -> Resources:
5953
docker_client = get_docker_client(app)
6054
app_settings = get_application_settings(app)
@@ -65,37 +59,36 @@ async def compute_node_used_resources(
6559
service_labels=app_settings.AUTOSCALING_NODES_MONITORING.NODES_MONITORING_SERVICE_LABELS,
6660
)
6761

68-
@staticmethod
6962
async def compute_cluster_used_resources(
70-
app: FastAPI, instances: list[AssociatedInstance]
63+
self, app: FastAPI, instances: list[AssociatedInstance]
7164
) -> Resources:
7265
docker_client = get_docker_client(app)
7366
return await utils_docker.compute_cluster_used_resources(
7467
docker_client, [i.node for i in instances]
7568
)
7669

77-
@staticmethod
7870
async def compute_cluster_total_resources(
79-
app: FastAPI, instances: list[AssociatedInstance]
71+
self, app: FastAPI, instances: list[AssociatedInstance]
8072
) -> Resources:
8173
assert app # nosec
8274
return await utils_docker.compute_cluster_total_resources(
8375
[i.node for i in instances]
8476
)
8577

86-
@staticmethod
87-
async def is_instance_active(app: FastAPI, instance: AssociatedInstance) -> bool:
78+
async def is_instance_active(
79+
self, app: FastAPI, instance: AssociatedInstance
80+
) -> bool:
8881
assert app # nosec
8982
return utils_docker.is_node_osparc_ready(instance.node)
9083

91-
@staticmethod
92-
async def is_instance_retired(app: FastAPI, instance: AssociatedInstance) -> bool:
84+
async def is_instance_retired(
85+
self, app: FastAPI, instance: AssociatedInstance
86+
) -> bool:
9387
assert app # nosec
9488
assert instance # nosec
9589
# nothing to do here
9690
return False
9791

98-
@staticmethod
99-
async def try_retire_nodes(app: FastAPI) -> None:
92+
async def try_retire_nodes(self, app: FastAPI) -> None:
10093
assert app # nosec
10194
# nothing to do here

0 commit comments

Comments
 (0)