Foundational Compute

Many sandboxes

Tag sandboxes with metadata, then start, run, collect, pause and kill them as one group.

The group flow

# 1. Start a group
$ fdtn create base -d --count 10 -m run=eval-1 --timeout 3600 --name eval
$ fdtn ls -m run=eval-1
$ for i in $(seq 10); do fdtn cp ./eval.py @eval-$i:/app/eval.py; done   # each sandbox, by name
# 2. Run one command in each
$ fdtn each -m run=eval-1 -- python /app/eval.py
# 3. Download the results
$ fdtn collect -m run=eval-1 --path /app/results --destination ./results-1
# 4. Pause, resume or stop the group
$ fdtn pause -m run=eval-1
$ fdtn resume -m run=eval-1
$ fdtn kill --all -s running,paused -m run=eval-1

Cost: 10 base sandboxes for 20 minutes = 10 × $0.039/h × 1/3 h = $0.13. From CI, see Run CI.

Metadata

Metadata is key=value pairs set at create. A filter matches when every pair matches exactly.

Python · TypeScript · CLI

from fdtn import Sandbox, SandboxQuery

sbx = Sandbox.create("base", metadata={"project": "demo", "run": "eval-1"})

# find the group: running and paused sandboxes
pages = Sandbox.list(query=SandboxQuery(metadata={"run": "eval-1"}))
ids = []
while pages.has_next:
    ids += [s.sandbox_id for s in pages.next_items()]

Up to 64 pairs. Keys starting with fdtn_ are reserved. No secrets: anyone with your key can read it. Rules

Names

Use @name in place of an ID in any fdtn command. With --count, --name eval gives eval-1, eval-2, and so on. A duplicate name stops the command and lists both IDs.

Start a group with --count

Starts up to 100 sandboxes. Over your running limit (20 to start), the rest wait in the queue (Limits). Needs -d. All get the same -m, --timeout, -e and --public.

$ fdtn create base -d --count 3 --name shard -m run=tests-42 --json

A failed start does not stop the others; the exit code is then 1. New sandboxes can take seconds to show in fdtn ls. Scripts should read fdtn ls --json.

Run in each with fdtn each

Runs one command in every matching running or paused sandbox, 5 at a time (up to 20 with --concurrency).

$ fdtn each -m run=eval-1 --concurrency 20 --json -- python /app/eval.py
  • Each output line starts with the sandbox ID.
  • Exit code 1 means one or more commands failed.
  • Set a long enough --timeout at create.

The queue

Start as many as you need. When machines are busy, extra starts wait in a queue for up to 30 minutes. The queue

The SDK stops waiting after 60 seconds, and the queued create then stops too. For batches, set request_timeout=1800 / requestTimeoutMs: 1_800_000.

Start N sandboxes from Python

Runs one shard in each of 10 sandboxes, at most 20 open at once.

shards.py
import asyncio
from fdtn import AsyncSandbox, CommandExitException

N = 10
RUN = "tests-42"
limit = asyncio.Semaphore(20)   # sandboxes open at once

async def shard(i):
    async with limit:
        sbx = await AsyncSandbox.create(
            "base",
            timeout=1800,                         # sandbox lifetime: 30 minutes
            request_timeout=1800,                 # wait up to 30 minutes in the queue
            metadata={"run": RUN, "shard": str(i)},
        )
        try:
            r = await sbx.commands.run(f"echo shard {i} on $(hostname)", timeout=0)
            return i, r.exit_code, r.stdout.strip()
        except CommandExitException as e:
            return i, e.exit_code, e.stderr.strip()
        finally:
            await sbx.kill()

async def main():
    results = await asyncio.gather(*(shard(i) for i in range(N)), return_exceptions=True)
    for r in results:
        print(r)

asyncio.run(main())

Start N sandboxes from TypeScript

shards.mjs
import { Sandbox, CommandExitError } from '@fdtn/sdk'

const N = 10
const RUN = 'tests-42'
const LIMIT = 20   // sandboxes open at once

async function shard(i) {
  const sbx = await Sandbox.create('base', {
    timeoutMs: 1_800_000,          // sandbox lifetime: 30 minutes
    requestTimeoutMs: 1_800_000,   // wait up to 30 minutes in the queue
    metadata: { run: RUN, shard: String(i) },
  })
  try {
    const r = await sbx.commands.run(`echo shard ${i} on $(hostname)`, { timeoutMs: 0 })
    return [i, r.exitCode, r.stdout.trim()]
  } catch (e) {
    if (e instanceof CommandExitError) return [i, e.exitCode, e.stderr.trim()]
    throw e
  } finally {
    await sbx.kill()
  }
}

const queue = [...Array(N).keys()]
const results = []
await Promise.all(Array.from({ length: Math.min(LIMIT, N) }, async () => {
  while (queue.length) {
    const i = queue.shift()
    results.push(await shard(i).catch((err) => [i, 'error', String(err)]))
  }
}))
console.log(results)

If a script stops early, run fdtn kill --all -s running,paused -m run=tests-42.

Next