Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
46 changes: 19 additions & 27 deletions python/ray/autoscaler/aws/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,33 +156,25 @@ def _configure_subnet(config):
"and trying this again. Note that the subnet must map public IPs "
"on instance launch.")
if "availability_zone" in config["provider"]:
default_subnet = next((
s for s in subnets
if s.availability_zone == config["provider"]["availability_zone"]),
None)
if not default_subnet:
azs = config["provider"]["availability_zone"].split(',')
subnets = [s for s in subnets if s.availability_zone in azs]
if not subnets:
raise Exception(
"No usable subnets matching availability zone {} "
"found. Choose a different availability zone or try "
"manually creating an instance in your specified region "
"to populate the list of subnets and trying this again."
.format(config["provider"]["availability_zone"]))
else:
default_subnet = subnets[0]

if "SubnetId" not in config["head_node"]:
assert default_subnet.map_public_ip_on_launch, \
"The chosen subnet must map nodes with public IPs on launch"
config["head_node"]["SubnetId"] = default_subnet.id
print("SubnetId not specified for head node, using {} in {}".format(
default_subnet.id, default_subnet.availability_zone))

if "SubnetId" not in config["worker_nodes"]:
assert default_subnet.map_public_ip_on_launch, \
"The chosen subnet must map nodes with public IPs on launch"
config["worker_nodes"]["SubnetId"] = default_subnet.id
print("SubnetId not specified for workers, using {} in {}".format(
default_subnet.id, default_subnet.availability_zone))

subnet_ids = [s.subnet_id for s in subnets]
subnet_descr = [(s.subnet_id, s.availability_zone) for s in subnets]
if "SubnetIds" not in config["head_node"]:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to keep the map_public_ip_on_launch filter, at least for the head node? I know we don't require it for worker nodes any more.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check is already performed on line 149; the later checks I believe are redundant so I removed them.

config["head_node"]["SubnetIds"] = subnet_ids
print("SubnetIds not specified for head node, using ", subnet_descr)

if "SubnetIds" not in config["worker_nodes"]:
config["worker_nodes"]["SubnetIds"] = subnet_ids
print("SubnetId not specified for workers, using ", subnet_descr)

return config

Expand All @@ -193,17 +185,17 @@ def _configure_security_group(config):
return config # have user-defined groups

group_name = SECURITY_GROUP_TEMPLATE.format(config["cluster_name"])
subnet = _get_subnet_or_die(config, config["worker_nodes"]["SubnetId"])
security_group = _get_security_group(config, subnet.vpc_id, group_name)
vpc_id = _get_vpc_id_or_die(config, config["worker_nodes"]["SubnetIds"][0])
security_group = _get_security_group(config, vpc_id, group_name)

if security_group is None:
print("Creating new security group {}".format(group_name))
client = _client("ec2", config)
client.create_security_group(
Description="Auto-created security group for Ray workers",
GroupName=group_name,
VpcId=subnet.vpc_id)
security_group = _get_security_group(config, subnet.vpc_id, group_name)
VpcId=vpc_id)
security_group = _get_security_group(config, vpc_id, group_name)
assert security_group, "Failed to create security group"

if not security_group.ip_permissions:
Expand Down Expand Up @@ -236,7 +228,7 @@ def _configure_security_group(config):
return config


def _get_subnet_or_die(config, subnet_id):
def _get_vpc_id_or_die(config, subnet_id):
ec2 = _resource("ec2", config)
subnet = list(
ec2.subnets.filter(Filters=[{
Expand All @@ -245,7 +237,7 @@ def _get_subnet_or_die(config, subnet_id):
}]))
assert len(subnet) == 1, "Subnet not found"
subnet = subnet[0]
return subnet
return subnet.vpc_id


def _get_security_group(config, vpc_id, group_name):
Expand Down
9 changes: 9 additions & 0 deletions python/ray/autoscaler/aws/node_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from __future__ import division
from __future__ import print_function

import random

import boto3
from botocore.config import Config

Expand Down Expand Up @@ -35,6 +37,9 @@ def __init__(self, provider_config, cluster_name):
self.ec2 = boto3.resource(
"ec2", region_name=provider_config["region"], config=config)

# Try availability zones round-robin, starting from random offset
self.subnet_idx = random.randint(0, 100)

# Cache of node objects from the last nodes() call. This avoids
# excessive DescribeInstances requests.
self.cached_nodes = {}
Expand Down Expand Up @@ -121,9 +126,13 @@ def create_node(self, node_config, tags, count):
"Key": k,
"Value": v,
})
subnet_ids = conf.pop('SubnetIds')

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: double quotes

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment here that SubnetIds is not a real config key, and we need to resolve it before handing it to AWS

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed, comment added

subnet_id = subnet_ids[self.subnet_idx % len(subnet_ids)]
self.subnet_idx += 1
conf.update({
"MinCount": 1,
"MaxCount": count,
"SubnetId": subnet_id,
"TagSpecifications": conf.get("TagSpecifications", []) + [{
"ResourceType": "instance",
"Tags": tag_pairs,
Expand Down
2 changes: 1 addition & 1 deletion python/ray/ray_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

def env_integer(key, default):
if key in os.environ:
return int(os.environ(key))
return int(os.environ[key])
return default


Expand Down