2222import pytest
2323from aws_library .ec2 import Resources
2424from dask_task_models_library .resource_constraints import (
25+ DASK_WORKER_THREAD_RESOURCE_NAME ,
2526 create_ec2_resource_constraint_key ,
27+ estimate_dask_worker_resources_from_ec2_instance ,
2628)
2729from faker import Faker
2830from fastapi import FastAPI
@@ -259,16 +261,25 @@ async def _create_task_with_resources(
259261 instance_types = await ec2_client .describe_instance_types (
260262 InstanceTypes = [dask_task_imposed_ec2_type ]
261263 )
264+
262265 assert instance_types
263266 assert "InstanceTypes" in instance_types
264267 assert instance_types ["InstanceTypes" ]
265268 assert "MemoryInfo" in instance_types ["InstanceTypes" ][0 ]
266269 assert "SizeInMiB" in instance_types ["InstanceTypes" ][0 ]["MemoryInfo" ]
270+ ec2_ram = TypeAdapter (ByteSize ).validate_python (
271+ f"{ instance_types ['InstanceTypes' ][0 ]['MemoryInfo' ]['SizeInMiB' ]} MiB" ,
272+ )
273+ assert "VCpuInfo" in instance_types ["InstanceTypes" ][0 ]
274+ assert "DefaultVCpus" in instance_types ["InstanceTypes" ][0 ]["VCpuInfo" ]
275+ ec2_cpus = instance_types ["InstanceTypes" ][0 ]["VCpuInfo" ]["DefaultVCpus" ]
276+ required_cpus , required_ram = estimate_dask_worker_resources_from_ec2_instance (
277+ ec2_cpus , ec2_ram
278+ )
267279 task_resources = Resources (
268- cpus = 1 ,
269- ram = TypeAdapter (ByteSize ).validate_python (
270- f"{ instance_types ['InstanceTypes' ][0 ]['MemoryInfo' ]['SizeInMiB' ]} MiB" ,
271- ),
280+ cpus = required_cpus ,
281+ ram = ByteSize (required_ram ),
282+ generic_resources = {DASK_WORKER_THREAD_RESOURCE_NAME : 1 },
272283 )
273284
274285 assert task_resources
@@ -443,7 +454,7 @@ async def test_cluster_scaling_with_task_with_too_much_resources_starts_nothing(
443454 _ScaleUpParams (
444455 imposed_instance_type = None ,
445456 task_resources = Resources (
446- cpus = 1 , ram = TypeAdapter (ByteSize ).validate_python ("128Gib " )
457+ cpus = 1 , ram = TypeAdapter (ByteSize ).validate_python ("115Gib " )
447458 ),
448459 num_tasks = 1 ,
449460 expected_instance_type = "r5n.4xlarge" ,
@@ -465,7 +476,7 @@ async def test_cluster_scaling_with_task_with_too_much_resources_starts_nothing(
465476 _ScaleUpParams (
466477 imposed_instance_type = "r5n.8xlarge" ,
467478 task_resources = Resources (
468- cpus = 1 , ram = TypeAdapter (ByteSize ).validate_python ("116Gib " )
479+ cpus = 1 , ram = TypeAdapter (ByteSize ).validate_python ("115Gib " )
469480 ),
470481 num_tasks = 1 ,
471482 expected_instance_type = "r5n.8xlarge" ,
@@ -1281,7 +1292,7 @@ async def test_cluster_scaling_up_more_than_allowed_with_multiple_types_max_star
12811292 _ScaleUpParams (
12821293 imposed_instance_type = None ,
12831294 task_resources = Resources (
1284- cpus = 1 , ram = TypeAdapter (ByteSize ).validate_python ("128Gib " )
1295+ cpus = 1 , ram = TypeAdapter (ByteSize ).validate_python ("115Gib " )
12851296 ),
12861297 num_tasks = 1 ,
12871298 expected_instance_type = "r5n.4xlarge" ,
@@ -1456,7 +1467,7 @@ async def test_long_pending_ec2_is_detected_as_broken_terminated_and_restarted(
14561467 _ScaleUpParams (
14571468 imposed_instance_type = "g4dn.2xlarge" , # 1 GPU, 8 CPUs, 32GiB
14581469 task_resources = Resources (
1459- cpus = 8 , ram = TypeAdapter (ByteSize ).validate_python ("15Gib" )
1470+ cpus = 7.9 , ram = TypeAdapter (ByteSize ).validate_python ("15Gib" )
14601471 ),
14611472 num_tasks = 12 ,
14621473 expected_instance_type = "g4dn.2xlarge" , # 1 GPU, 8 CPUs, 32GiB
@@ -1465,7 +1476,7 @@ async def test_long_pending_ec2_is_detected_as_broken_terminated_and_restarted(
14651476 _ScaleUpParams (
14661477 imposed_instance_type = "g4dn.8xlarge" , # 32CPUs, 128GiB
14671478 task_resources = Resources (
1468- cpus = 32 , ram = TypeAdapter (ByteSize ).validate_python ("20480MB" )
1479+ cpus = 31.9 , ram = TypeAdapter (ByteSize ).validate_python ("20480MB" )
14691480 ),
14701481 num_tasks = 7 ,
14711482 expected_instance_type = "g4dn.8xlarge" , # 32CPUs, 128GiB
0 commit comments