2026-05-11 16:48:52 +01:00
|
|
|
import argparse
|
|
|
|
|
import asyncio
|
|
|
|
|
import logging
|
|
|
|
|
import os
|
|
|
|
|
import sys
|
2026-05-11 17:08:44 +01:00
|
|
|
import time
|
2026-05-11 16:48:52 +01:00
|
|
|
|
|
|
|
|
from vastai import Serverless
|
|
|
|
|
|
|
|
|
|
logging.basicConfig(
|
2026-05-11 17:08:44 +01:00
|
|
|
level=logging.INFO,
|
2026-05-11 16:48:52 +01:00
|
|
|
format="%(asctime)s[%(levelname)-5s] %(message)s",
|
|
|
|
|
datefmt="%Y-%m-%d %H:%M:%S",
|
|
|
|
|
)
|
|
|
|
|
log = logging.getLogger(__file__)
|
|
|
|
|
|
|
|
|
|
ENDPOINT_NAME = "null-prod"
|
2026-05-12 11:50:03 +01:00
|
|
|
DEFAULT_SESSION_COST = 100 # matches the worker's max_perf
|
2026-05-11 16:48:52 +01:00
|
|
|
|
|
|
|
|
|
2026-05-11 17:08:44 +01:00
|
|
|
async def reserve(
|
|
|
|
|
client: Serverless,
|
|
|
|
|
*,
|
|
|
|
|
endpoint_name: str,
|
2026-05-12 10:51:24 +01:00
|
|
|
hold_for: float,
|
2026-05-12 11:31:26 +01:00
|
|
|
session_cost: int,
|
2026-05-12 10:51:24 +01:00
|
|
|
label: str = "session",
|
|
|
|
|
) -> None:
|
2026-05-11 16:48:52 +01:00
|
|
|
endpoint = await client.get_endpoint(name=endpoint_name)
|
2026-05-12 11:50:03 +01:00
|
|
|
lifetime = hold_for + 60 # outlast the hold; no keepalives sent
|
2026-05-11 17:08:44 +01:00
|
|
|
start = time.monotonic()
|
2026-05-12 11:50:03 +01:00
|
|
|
log.info("[%s] creating session (cost=%d, hold=%.0fs)", label, session_cost, hold_for)
|
2026-05-12 11:31:26 +01:00
|
|
|
async with await endpoint.session(cost=session_cost, lifetime=lifetime) as s:
|
2026-05-12 10:51:24 +01:00
|
|
|
log.info("[%s] session %s open", label, s.session_id)
|
|
|
|
|
try:
|
|
|
|
|
await asyncio.sleep(hold_for)
|
|
|
|
|
except asyncio.CancelledError:
|
2026-05-12 11:50:03 +01:00
|
|
|
log.info("[%s] cancelled after %.1fs", label, time.monotonic() - start)
|
2026-05-12 10:51:24 +01:00
|
|
|
raise
|
2026-05-12 11:50:03 +01:00
|
|
|
log.info("[%s] closed cleanly after %.1fs", label, time.monotonic() - start)
|
2026-05-11 17:08:44 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
async def run_demo(
|
|
|
|
|
client: Serverless,
|
|
|
|
|
*,
|
|
|
|
|
endpoint_name: str,
|
|
|
|
|
interval: float,
|
2026-05-11 18:26:31 +01:00
|
|
|
plateau: float,
|
2026-05-12 11:31:26 +01:00
|
|
|
session_cost: int,
|
2026-05-11 17:08:44 +01:00
|
|
|
) -> None:
|
2026-05-11 18:26:31 +01:00
|
|
|
n = 3
|
|
|
|
|
hold = (n - 1) * interval + plateau
|
2026-05-11 17:08:44 +01:00
|
|
|
tasks: list[asyncio.Task] = []
|
2026-05-11 18:26:31 +01:00
|
|
|
for i in range(1, n + 1):
|
2026-05-11 17:08:44 +01:00
|
|
|
label = f"res-{i}"
|
2026-05-12 11:50:03 +01:00
|
|
|
tasks.append(asyncio.create_task(
|
|
|
|
|
reserve(client, endpoint_name=endpoint_name, hold_for=hold,
|
|
|
|
|
session_cost=session_cost, label=label),
|
2026-05-11 17:08:44 +01:00
|
|
|
name=label,
|
2026-05-12 11:50:03 +01:00
|
|
|
))
|
2026-05-11 18:26:31 +01:00
|
|
|
if i < n:
|
2026-05-11 17:08:44 +01:00
|
|
|
await asyncio.sleep(interval)
|
|
|
|
|
log.info(
|
2026-05-12 11:50:03 +01:00
|
|
|
"All %d sessions in flight; plateau %.0fs, scale-down %.0fs apart",
|
|
|
|
|
n, plateau, interval,
|
2026-05-11 17:08:44 +01:00
|
|
|
)
|
2026-05-12 11:50:03 +01:00
|
|
|
await asyncio.gather(*tasks, return_exceptions=True)
|
2026-05-11 16:48:52 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def build_arg_parser() -> argparse.ArgumentParser:
|
|
|
|
|
p = argparse.ArgumentParser(description="Vast Null PyWorker demo client")
|
2026-05-12 11:50:03 +01:00
|
|
|
p.add_argument("--endpoint", default=os.environ.get("VAST_ENDPOINT", ENDPOINT_NAME),
|
|
|
|
|
help=f"endpoint name (default: {ENDPOINT_NAME})")
|
|
|
|
|
p.add_argument("--instance", choices=("prod", "alpha", "candidate", "local"),
|
|
|
|
|
default=os.environ.get("VAST_INSTANCE", "prod"),
|
|
|
|
|
help="serverless instance (default: prod)")
|
|
|
|
|
p.add_argument("--duration", type=float, default=180.0,
|
|
|
|
|
help="single-reserve mode: seconds to hold (default: 180)")
|
2026-05-11 17:08:44 +01:00
|
|
|
|
|
|
|
|
modes = p.add_mutually_exclusive_group(required=False)
|
2026-05-12 11:50:03 +01:00
|
|
|
modes.add_argument("--reserve", action="store_true",
|
|
|
|
|
help="single session (default if no mode given)")
|
|
|
|
|
modes.add_argument("--demo", action="store_true",
|
|
|
|
|
help="staggered 3-session trapezoid")
|
|
|
|
|
|
|
|
|
|
p.add_argument("--interval", type=float, default=30.0,
|
|
|
|
|
help="demo: seconds between sessions (default: 30)")
|
|
|
|
|
p.add_argument("--plateau", type=float, default=300.0,
|
|
|
|
|
help="demo: seconds to hold all 3 active (default: 300)")
|
|
|
|
|
p.add_argument("--session-cost", type=int, default=DEFAULT_SESSION_COST,
|
|
|
|
|
help=f"cost reported at session-create (default: {DEFAULT_SESSION_COST})")
|
2026-05-11 16:48:52 +01:00
|
|
|
return p
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def main_async():
|
|
|
|
|
args = build_arg_parser().parse_args()
|
|
|
|
|
print("=" * 60)
|
2026-05-12 11:40:51 +01:00
|
|
|
print(f"Endpoint: {args.endpoint} (instance: {args.instance})")
|
2026-05-11 16:48:52 +01:00
|
|
|
print("=" * 60)
|
|
|
|
|
|
|
|
|
|
try:
|
2026-05-12 11:40:51 +01:00
|
|
|
async with Serverless(instance=args.instance) as client:
|
2026-05-11 17:08:44 +01:00
|
|
|
if args.demo:
|
2026-05-12 11:50:03 +01:00
|
|
|
await run_demo(client, endpoint_name=args.endpoint,
|
|
|
|
|
interval=args.interval, plateau=args.plateau,
|
|
|
|
|
session_cost=args.session_cost)
|
2026-05-11 17:08:44 +01:00
|
|
|
else:
|
2026-05-12 11:50:03 +01:00
|
|
|
await reserve(client, endpoint_name=args.endpoint,
|
|
|
|
|
hold_for=args.duration,
|
|
|
|
|
session_cost=args.session_cost,
|
|
|
|
|
label="reservation")
|
2026-05-11 17:08:44 +01:00
|
|
|
except KeyboardInterrupt:
|
2026-05-12 11:50:03 +01:00
|
|
|
log.info("Interrupted; dropping in-flight sessions")
|
2026-05-11 16:48:52 +01:00
|
|
|
except Exception as e:
|
2026-05-11 17:08:44 +01:00
|
|
|
log.error("Error: %s", e, exc_info=True)
|
2026-05-11 16:48:52 +01:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
asyncio.run(main_async())
|