Spaces:
Running
Running
File size: 8,845 Bytes
9f72def 94e680d 9f72def | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 | import logging
import threading
import time
from concurrent.futures import ThreadPoolExecutor
from datetime import datetime, timedelta, timezone
import boto3
from botocore.config import Config
from botocore.exceptions import ClientError
from . import config
log = logging.getLogger(__name__)
BOTO_CONFIG = Config(retries={"mode": "adaptive", "max_attempts": 5})
# For probe creates only: botocore treats InsufficientInstanceCapacity as
# retryable, but here that error IS the answer — retrying turns a 1s probe
# into 20s of backoff. Throttling is retried manually in probe_az.
PROBE_BOTO_CONFIG = Config(retries={"mode": "standard", "max_attempts": 1})
# CreateCapacityReservation error code -> probe status
STATUS_BY_CODE = {
"InsufficientInstanceCapacity": "unavailable",
"InstanceLimitExceeded": "quota",
"ReservationCapacityExceeded": "quota",
"MaxConfigLimitExceededException": "quota",
"Unsupported": "unsupported",
"UnsupportedOperation": "unsupported",
"UnauthorizedOperation": "forbidden",
}
_clients: dict[str, object] = {}
_clients_lock = threading.Lock()
# (region, instance_type) -> (azs, fetched_at); describes are free, cached 24h
_offerings: dict[tuple[str, str], tuple[list[str], float]] = {}
_offerings_lock = threading.Lock()
OFFERINGS_TTL = 24 * 3600
def ec2(region: str):
with _clients_lock:
if region not in _clients:
_clients[region] = boto3.client("ec2", region_name=region, config=BOTO_CONFIG)
return _clients[region]
def probe_ec2(region: str):
with _clients_lock:
key = f"probe:{region}"
if key not in _clients:
_clients[key] = boto3.client("ec2", region_name=region, config=PROBE_BOTO_CONFIG)
return _clients[key]
def offered_azs(region: str, instance_type: str) -> list[str]:
"""AZs of `region` where `instance_type` is offered (cached 24h).
Works for any instance type, not just configured ones — an unknown or
unoffered type simply matches nothing and yields [].
"""
key = (region, instance_type)
with _offerings_lock:
cached = _offerings.get(key)
if cached and time.time() - cached[1] < OFFERINGS_TTL:
return cached[0]
resp = ec2(region).describe_instance_type_offerings(
LocationType="availability-zone",
Filters=[{"Name": "instance-type", "Values": [instance_type]}],
)
azs = sorted(off["Location"] for off in resp["InstanceTypeOfferings"])
with _offerings_lock:
_offerings[key] = (azs, time.time())
return azs
def _create_with_throttle_retry(region: str, **kwargs):
for attempt in range(3):
try:
return probe_ec2(region).create_capacity_reservation(**kwargs)
except ClientError as e:
if e.response["Error"]["Code"] == "RequestLimitExceeded" and attempt < 2:
time.sleep(1 + attempt)
continue
raise
# instance_type -> {"name", "count", "memory_gb"} or None; resolved once per process
_gpu_info: dict[str, dict | None] = {}
_gpu_lock = threading.Lock()
def gpu_info(instance_types: list[str]) -> dict[str, dict | None]:
"""GPU spec per type via DescribeInstanceTypes, from the first region that
knows the type (newer families only exist in a few regions)."""
with _gpu_lock:
missing = [t for t in instance_types if t not in _gpu_info]
for region in config.REGIONS:
if not missing:
break
try:
# Filters are applied per page, so matches must be collected across
# all pages (a bare call would return first-page matches only).
found = []
paginator = ec2(region).get_paginator("describe_instance_types")
for page in paginator.paginate(
Filters=[{"Name": "instance-type", "Values": missing}]
):
found.extend(page["InstanceTypes"])
except ClientError as e:
log.warning("describe_instance_types in %s failed: %s", region, e)
continue
with _gpu_lock:
for it in found:
gpus = (it.get("GpuInfo") or {}).get("Gpus") or []
_gpu_info[it["InstanceType"]] = {
"name": f"{gpus[0]['Manufacturer']} {gpus[0]['Name']}",
"count": gpus[0]["Count"],
"memory_gb": round(gpus[0]["MemoryInfo"]["SizeInMiB"] / 1024),
} if gpus else None
missing = [t for t in instance_types if t not in _gpu_info]
with _gpu_lock:
for t in missing: # unknown everywhere: cache None to avoid re-querying
_gpu_info[t] = None
return {t: _gpu_info[t] for t in instance_types}
def probe_az(region: str, instance_type: str, az: str) -> dict:
"""Create a 1-instance targeted capacity reservation in `az`, then cancel it.
Success means real on-demand capacity exists right now. The reservation
carries an EndDate dead-man switch so a crash between create and cancel
can't leak an open-ended reservation.
"""
client = ec2(region)
result = {"az": az, "status": "error", "error": None}
cr_id = None
try:
resp = _create_with_throttle_retry(
region,
InstanceType=instance_type,
InstancePlatform="Linux/UNIX",
AvailabilityZone=az,
InstanceCount=1,
InstanceMatchCriteria="targeted",
EndDateType="limited",
EndDate=datetime.now(timezone.utc)
+ timedelta(minutes=config.PROBE_END_DATE_MINUTES),
TagSpecifications=[
{
"ResourceType": "capacity-reservation",
"Tags": [
{"Key": config.PROBE_TAG_KEY, "Value": config.PROBE_TAG_VALUE}
],
}
],
)
cr_id = resp["CapacityReservation"]["CapacityReservationId"]
result["status"] = "available"
except ClientError as e:
code = e.response["Error"]["Code"]
result["status"] = STATUS_BY_CODE.get(code, "error")
result["error"] = code
if result["status"] == "error":
log.warning("probe %s %s/%s: %s", instance_type, region, az, e)
finally:
if cr_id:
_cancel(client, cr_id, region)
return result
def _cancel(client, cr_id: str, region: str):
for attempt in range(4):
try:
client.cancel_capacity_reservation(CapacityReservationId=cr_id)
return
except ClientError as e:
log.warning("cancel %s in %s failed (attempt %d): %s", cr_id, region, attempt + 1, e)
time.sleep(2**attempt)
log.error(
"LEAKED reservation %s in %s — will auto-expire in %d min",
cr_id, region, config.PROBE_END_DATE_MINUTES,
)
def probe_region(region: str, instance_type: str) -> dict:
"""Probe every offered AZ of a region. Region is 'available' if any AZ is."""
started = time.time()
try:
azs = offered_azs(region, instance_type)
except ClientError as e:
code = e.response["Error"]["Code"]
return {"status": "error", "error": code, "azs": {}, "checked_at": time.time()}
if not azs:
return {"status": "not_offered", "azs": {}, "checked_at": time.time()}
with ThreadPoolExecutor(max_workers=min(len(azs), 6)) as pool:
az_results = list(pool.map(lambda az: probe_az(region, instance_type, az), azs))
statuses = [r["status"] for r in az_results]
if "available" in statuses:
status = "available"
elif "unavailable" in statuses:
status = "unavailable"
else:
status = statuses[0] # quota / unsupported / forbidden / error
return {
"status": status,
"azs": {r["az"]: {"status": r["status"], "error": r["error"]} for r in az_results},
"checked_at": time.time(),
"duration_ms": int((time.time() - started) * 1000),
}
def cleanup_leaked(regions: list[str] | None = None):
"""Cancel any active probe-tagged reservation left over from a crash."""
for region in regions or config.REGIONS:
try:
resp = ec2(region).describe_capacity_reservations(
Filters=[
{"Name": f"tag:{config.PROBE_TAG_KEY}", "Values": [config.PROBE_TAG_VALUE]},
{"Name": "state", "Values": ["active"]},
]
)
for cr in resp["CapacityReservations"]:
log.warning("cleaning leaked reservation %s in %s", cr["CapacityReservationId"], region)
_cancel(ec2(region), cr["CapacityReservationId"], region)
except Exception as e: # ClientError, but also expired-SSO credential errors
log.warning("cleanup in %s failed: %s", region, e)
|