Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a configurable spot strategy #40

Merged
merged 3 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/yaml.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,6 @@ Each forge command certain parameters. A yaml file with all the parameters can b
- Use the `--all` flag to run the script on all the instances in a cluster.
- E.g. `run_cmd: scripts/run.sh {env} {date} {ip}`
- **service** - `cluster` or `single`
- **spot_strategy** - Select the [spot allocation strategy](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2/client/create_fleet.html).
- **user_data** - Custom script passed to instance. Will be run only once when the instance starts up.
- **valid_time** - How many hours the fleet will stay up. After this time, all EC2s will be destroyed. The default is 8.
3 changes: 2 additions & 1 deletion src/forge/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
'destroy_after_failure': True,
'default_ratio': [8, 8],
'valid_time': 8,
'ec2_max': 768
'ec2_max': 768,
'spot_strategy': 'price-capacity-optimized'
}

# Required arguments for each Forge job
Expand Down
12 changes: 10 additions & 2 deletions src/forge/configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import sys

import yaml
from schema import Schema, And, Optional, SchemaError
from schema import Schema, And, Optional, Or, SchemaError

from .common import set_config_dir

Expand Down Expand Up @@ -62,7 +62,15 @@ def check_env_yaml(env_yaml):
Optional('tags'): And(list, len, error="Invalid AWS tags"),
Optional('excluded_ec2s'): And(list),
Optional('additional_config'): And(list),
Optional('ec2_max'): And(int)
Optional('ec2_max'): And(int),
Optional('spot_strategy'): And(str, len,
Or(
'lowest-price',
'diversified',
'capacity-optimized',
'capacity-optimized-prioritized',
'price-capacity-optimized'),
error='Invalid spot allocation strategy'),
})
try:
validated = schema.validate(env_yaml)
Expand Down
3 changes: 2 additions & 1 deletion src/forge/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,7 @@ def _check(x, i):
gpu = config.get('gpu_flag', False)
market = config.get('market', DEFAULT_ARG_VALS['market'])
subnet = config.get('aws_subnet')
strategy = config.get('spot_strategy')

market = market[-1] if 'cluster-worker' in n else market[0]

Expand All @@ -545,7 +546,7 @@ def _check(x, i):
'AllocationStrategy': 'lowest-price'
},
'SpotOptions': {
'AllocationStrategy': 'capacity-optimized',
'AllocationStrategy': strategy,
'InstanceInterruptionBehavior': 'terminate',
'MaintenanceStrategies': {
'CapacityRebalance': {
Expand Down
15 changes: 8 additions & 7 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def _load_admin_cfg(env):
'home_dir': os.path.dirname(FORGE_DIR), 'yaml_dir': os.path.join(TEST_DIR, 'data'), 'user': 'test_user',
'ami': 'single_ami', 'forge_version': False, 'config_dir': os.path.join(TEST_CFG_DIR, 'dev'),
'region': 'us-east-1', 'destroy_after_success': True, 'destroy_after_failure': True, 'default_ratio': [8, 8],
'valid_time': 8, 'ec2_max': 768}),
'valid_time': 8, 'ec2_max': 768, 'spot_strategy': 'price-capacity-optimized'}),
# Destroy job; passing the relative path to a yaml
(['forge', 'destroy', '--yaml', os.path.join(TEST_DIR_REL, 'data', 'single_intermediate.yaml')],
{'name': 'test-single-intermediate', 'log_level': 'INFO',
Expand All @@ -53,7 +53,7 @@ def _load_admin_cfg(env):
'yaml_dir': os.path.join(TEST_DIR, 'data'), 'user': 'test_user', 'ami': 'single_ami',
'forge_version': False, 'config_dir': os.path.join(TEST_CFG_DIR, 'dev'), 'region': 'us-east-1',
'destroy_after_success': True, 'destroy_after_failure': True, 'default_ratio': [8, 8], 'valid_time': 8,
'ec2_max': 768}),
'ec2_max': 768, 'spot_strategy': 'price-capacity-optimized'}),
# Destroy job; overriding log_level
(['forge', 'destroy', '--yaml', os.path.join(TEST_DIR, 'data', 'single_intermediate.yaml'), '--log_level', 'debug'],
{'name': 'test-single-intermediate', 'log_level': 'DEBUG',
Expand All @@ -63,7 +63,7 @@ def _load_admin_cfg(env):
'home_dir': os.path.dirname(FORGE_DIR), 'yaml_dir': os.path.join(TEST_DIR, 'data'), 'user': 'test_user',
'ami': 'single_ami', 'forge_version': False, 'config_dir': os.path.join(TEST_CFG_DIR, 'dev'),
'region': 'us-east-1', 'destroy_after_success': True, 'destroy_after_failure': True, 'default_ratio': [8, 8],
'valid_time': 8, 'ec2_max': 768}),
'valid_time': 8, 'ec2_max': 768, 'spot_strategy': 'price-capacity-optimized'}),
# Destroy job; overriding market
(['forge', 'destroy', '--yaml', os.path.join(TEST_DIR, 'data', 'single_intermediate.yaml'),
'--market', 'on-demand'],
Expand All @@ -73,7 +73,8 @@ def _load_admin_cfg(env):
'gpu_flag': False, 'app_dir': TEST_DIR, 'src_dir': FORGE_DIR, 'home_dir': os.path.dirname(FORGE_DIR),
'yaml_dir': os.path.join(TEST_DIR, 'data'), 'user': 'test_user', 'ami': 'single_ami', 'forge_version': False,
'config_dir': os.path.join(TEST_CFG_DIR, 'dev'), 'region': 'us-east-1', 'destroy_after_success': True,
'destroy_after_failure': True, 'default_ratio': [8, 8], 'valid_time': 8, 'ec2_max': 768}),
'destroy_after_failure': True, 'default_ratio': [8, 8], 'valid_time': 8, 'ec2_max': 768,
'spot_strategy': 'price-capacity-optimized'}),
# Destroy job; no market
(['forge', 'destroy', '--yaml', os.path.join(TEST_DIR, 'data', 'single_basic.yaml'), '--forge_env', 'dev'],
{'name': 'test-single-basic', 'log_level': 'INFO', 'yaml': os.path.join(TEST_DIR, 'data', 'single_basic.yaml'),
Expand All @@ -82,7 +83,7 @@ def _load_admin_cfg(env):
'home_dir': os.path.dirname(FORGE_DIR), 'yaml_dir': os.path.join(TEST_DIR, 'data'), 'user': 'test_user',
'ami': 'single_ami', 'forge_version': False, 'config_dir': os.path.join(TEST_CFG_DIR, 'dev'),
'region': 'us-east-1', 'destroy_after_success': True, 'destroy_after_failure': True, 'default_ratio': [8, 8],
'valid_time': 8, 'ec2_max': 768}),
'valid_time': 8, 'ec2_max': 768, 'spot_strategy': 'price-capacity-optimized'}),
# Configure job
(['forge', 'configure'],
{'forge_version': False, 'job': 'configure', 'log_level': 'INFO'}),
Expand All @@ -95,7 +96,7 @@ def _load_admin_cfg(env):
'home_dir': os.path.dirname(FORGE_DIR), 'yaml_dir': os.path.join(TEST_DIR, 'data'), 'user': 'test_user',
'ami': 'single_ami', 'forge_version': False, 'config_dir': os.path.join(TEST_CFG_DIR, 'dev'),
'region': 'us-east-1', 'destroy_after_success': True, 'destroy_after_failure': True, 'default_ratio': [8, 8],
'valid_time': 8, 'ec2_max': 768}),
'valid_time': 8, 'ec2_max': 768, 'spot_strategy': 'price-capacity-optimized'}),
# Create job; setting gpu
(['forge', 'create', '--yaml', os.path.join(TEST_DIR, 'data', 'single_basic.yaml'), '--forge_env', 'dev', '--gpu'],
{'name': 'test-single-basic', 'log_level': 'INFO', 'yaml': os.path.join(TEST_DIR, 'data', 'single_basic.yaml'),
Expand All @@ -104,7 +105,7 @@ def _load_admin_cfg(env):
'home_dir': os.path.dirname(FORGE_DIR), 'yaml_dir': os.path.join(TEST_DIR, 'data'), 'user': 'test_user',
'ami': 'single_ami', 'forge_version': False, 'config_dir': os.path.join(TEST_CFG_DIR, 'dev'),
'region': 'us-east-1', 'destroy_after_success': True, 'destroy_after_failure': True, 'default_ratio': [8, 8],
'valid_time': 8, 'ec2_max': 768}),
'valid_time': 8, 'ec2_max': 768, 'spot_strategy': 'price-capacity-optimized'}),
])
def test_forge_main(mock_pass, mock_execute, mock_keys, mock_config_dir, cli_call, exp_config, load_admin_cfg):
"""Test the config after calling forge via the command line."""
Expand Down