Skip to content

Commit ff6f85a

Browse files
authored
✨Autoscaling: scale down while in use 🚨 (ITISFoundation#6898)
1 parent 9012c4d commit ff6f85a

File tree

9 files changed

+1286
-544
lines changed

9 files changed

+1286
-544
lines changed

packages/aws-library/src/aws_library/ec2/_client.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,8 @@ async def launch_instances(
181181
)
182182
instance_ids = [i["InstanceId"] for i in instances["Instances"]]
183183
_logger.info(
184-
"New instances launched: %s, waiting for them to start now...",
184+
"%s New instances launched: %s, waiting for them to start now...",
185+
len(instance_ids),
185186
instance_ids,
186187
)
187188

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
from collections.abc import Callable
2+
3+
import arrow
4+
from aws_library.ec2 import EC2InstanceData
5+
from models_library.generated_models.docker_rest_api import (
6+
Availability,
7+
Node,
8+
NodeState,
9+
)
10+
from pytest_mock import MockType
11+
from simcore_service_autoscaling.models import AssociatedInstance, Cluster
12+
from simcore_service_autoscaling.utils.utils_docker import (
13+
_OSPARC_NODE_TERMINATION_PROCESS_LABEL_KEY,
14+
_OSPARC_SERVICE_READY_LABEL_KEY,
15+
_OSPARC_SERVICES_READY_DATETIME_LABEL_KEY,
16+
)
17+
18+
19+
def assert_cluster_state(
20+
spied_cluster_analysis: MockType, *, expected_calls: int, expected_num_machines: int
21+
) -> Cluster:
22+
assert spied_cluster_analysis.call_count == expected_calls
23+
24+
assert isinstance(spied_cluster_analysis.spy_return, Cluster)
25+
assert (
26+
spied_cluster_analysis.spy_return.total_number_of_machines()
27+
== expected_num_machines
28+
)
29+
print("current cluster state:", spied_cluster_analysis.spy_return)
30+
cluster = spied_cluster_analysis.spy_return
31+
spied_cluster_analysis.reset_mock()
32+
return cluster
33+
34+
35+
def create_fake_association(
36+
create_fake_node: Callable[..., Node],
37+
drained_machine_id: str | None,
38+
terminating_machine_id: str | None,
39+
):
40+
fake_node_to_instance_map = {}
41+
42+
async def _fake_node_creator(
43+
_nodes: list[Node], ec2_instances: list[EC2InstanceData]
44+
) -> tuple[list[AssociatedInstance], list[EC2InstanceData]]:
45+
def _create_fake_node_with_labels(instance: EC2InstanceData) -> Node:
46+
if instance not in fake_node_to_instance_map:
47+
fake_node = create_fake_node()
48+
assert fake_node.spec
49+
fake_node.spec.availability = Availability.active
50+
assert fake_node.status
51+
fake_node.status.state = NodeState.ready
52+
assert fake_node.spec.labels
53+
fake_node.spec.labels |= {
54+
_OSPARC_SERVICES_READY_DATETIME_LABEL_KEY: arrow.utcnow().isoformat(),
55+
_OSPARC_SERVICE_READY_LABEL_KEY: (
56+
"true" if instance.id != drained_machine_id else "false"
57+
),
58+
}
59+
if instance.id == terminating_machine_id:
60+
fake_node.spec.labels |= {
61+
_OSPARC_NODE_TERMINATION_PROCESS_LABEL_KEY: arrow.utcnow().isoformat()
62+
}
63+
fake_node_to_instance_map[instance] = fake_node
64+
return fake_node_to_instance_map[instance]
65+
66+
associated_instances = [
67+
AssociatedInstance(node=_create_fake_node_with_labels(i), ec2_instance=i)
68+
for i in ec2_instances
69+
]
70+
71+
return associated_instances, []
72+
73+
return _fake_node_creator

scripts/maintenance/computational-clusters/autoscaled_monitor/core.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ def _print_dynamic_instances(
138138
f"{utils.color_encode_with_state(instance.name, instance.ec2_instance)}",
139139
f"ID: {instance.ec2_instance.instance_id}",
140140
f"AMI: {instance.ec2_instance.image_id}",
141-
f"AMI name: {instance.ec2_instance.image.name}",
142141
f"Type: {instance.ec2_instance.instance_type}",
143142
f"Up: {utils.timedelta_formatting(time_now - instance.ec2_instance.launch_time, color_code=True)}",
144143
f"ExtIP: {instance.ec2_instance.public_ip_address}",
@@ -183,7 +182,6 @@ def _print_computational_clusters(
183182
f"Name: {cluster.primary.name}",
184183
f"ID: {cluster.primary.ec2_instance.id}",
185184
f"AMI: {cluster.primary.ec2_instance.image_id}",
186-
f"AMI name: {cluster.primary.ec2_instance.image.name}",
187185
f"Type: {cluster.primary.ec2_instance.instance_type}",
188186
f"Up: {utils.timedelta_formatting(time_now - cluster.primary.ec2_instance.launch_time, color_code=True)}",
189187
f"ExtIP: {cluster.primary.ec2_instance.public_ip_address}",
@@ -229,7 +227,6 @@ def _print_computational_clusters(
229227
f"Name: {worker.name}",
230228
f"ID: {worker.ec2_instance.id}",
231229
f"AMI: {worker.ec2_instance.image_id}",
232-
f"AMI name: {worker.ec2_instance.image.name}",
233230
f"Type: {worker.ec2_instance.instance_type}",
234231
f"Up: {utils.timedelta_formatting(time_now - worker.ec2_instance.launch_time, color_code=True)}",
235232
f"ExtIP: {worker.ec2_instance.public_ip_address}",

0 commit comments

Comments
 (0)