Public: anyone can read this; the crew can change it.
A replace waits for the old machine to let go of the volume before making the new one #16346
Check · Review · Land · Live done — Landed and live in production.
sold/replace-waits-for-volume → main 1 commit
7 files
changed +75
−5
src/cloud/droplet.ts +4
| @@ -46,6 +46,10 @@ export const digitalocean: Driver = { | ||
| 46 | 46 | async deleteVolume(call, volume) { |
| 47 | 47 | await call("DELETE", `/volumes/${volume}`); |
| 48 | 48 | }, |
| 49 | async volumeFree(call, volume) { | |
| 50 | const v = (await call("GET", `/volumes/${volume}`)).volume; | |
| 51 | return Array.isArray(v?.droplet_ids) ? v.droplet_ids.length === 0 : true; | |
| 52 | }, | |
| 49 | 53 | async readMachine(call, remote) { |
| 50 | 54 | const d = (await call("GET", `/droplets/${remote}`)).droplet; |
| 51 | 55 | const v4: unknown[] = Array.isArray(d?.networks?.v4) ? d.networks.v4 : []; |
src/cloud/hetzner.ts +4
| @@ -49,6 +49,10 @@ export const hetzner: Driver = { | ||
| 49 | 49 | async deleteVolume(call, volume) { |
| 50 | 50 | await call("DELETE", `/volumes/${volume}`); |
| 51 | 51 | }, |
| 52 | async volumeFree(call, volume) { | |
| 53 | const v = (await call("GET", `/volumes/${volume}`)).volume; | |
| 54 | return v?.server === null || v?.server === undefined; | |
| 55 | }, | |
| 52 | 56 | async readMachine(call, remote) { |
| 53 | 57 | const s = (await call("GET", `/servers/${remote}`)).server; |
| 54 | 58 | return { status: String(s?.status ?? ""), address: ipv4(s?.public_net?.ipv4?.ip) }; |
src/cloud/machine.ts +19
| @@ -157,6 +157,8 @@ export interface Driver { | ||
| 157 | 157 | deleteMachine(call: Call, remote: string): Promise<void>; |
| 158 | 158 | /** Delete the data volume: nothing of the forge's data is left once this answers. */ |
| 159 | 159 | deleteVolume(call: Call, volume: string): Promise<void>; |
| 160 | /** Whether the volume is attached to no machine: a cloud detaches it after a delete answers, and refuses a new machine with it until then. */ | |
| 161 | volumeFree(call: Call, volume: string): Promise<boolean>; | |
| 160 | 162 | readMachine(call: Call, remote: string): Promise<{ status: string; address: string }>; |
| 161 | 163 | } |
| 162 | 164 | |
| @@ -217,10 +219,27 @@ export async function replace( | ||
| 217 | 219 | if (!(e instanceof Error && e.message.endsWith("answered 404"))) throw e; |
| 218 | 220 | } |
| 219 | 221 | hooks.onDeleted(); |
| 222 | await untilFree(d, call, p.volume); | |
| 220 | 223 | } |
| 221 | 224 | return { remote: await d.createMachine(call, p, p.volume) }; |
| 222 | 225 | } |
| 223 | 226 | |
| 227 | /** How long a replace waits for the old machine to let go of the volume, and how often it asks. */ | |
| 228 | export const DETACH = { waitMs: 180_000, everyMs: 5_000 }; | |
| 229 | ||
| 230 | /** | |
| 231 | * Wait until the cloud says the volume is attached to nothing. DigitalOcean answered 422 to a | |
| 232 | * droplet made with a volume it was still detaching from the deleted one (test1, 2026-11-24), which | |
| 233 | * left a sold forge with no machine until someone asked again (#16342). | |
| 234 | */ | |
| 235 | async function untilFree(d: Driver, call: Call, volume: string): Promise<void> { | |
| 236 | const until = Date.now() + DETACH.waitMs; | |
| 237 | while (!(await d.volumeFree(call, volume))) { | |
| 238 | if (Date.now() >= until) throw new Error("the old machine still holds the data volume; nothing new was made: ask again in a minute"); | |
| 239 | await Bun.sleep(DETACH.everyMs); | |
| 240 | } | |
| 241 | } | |
| 242 | ||
| 224 | 243 | /** |
| 225 | 244 | * Ask whether the row's place may be ordered, and make nothing: the driver's `admit` alone, which |
| 226 | 245 | * is where a place that cannot be ordered, or a ceiling (`ceiling.ts`), refuses. Asked before a |
test/pinned-claim.test.ts +1 −1
| @@ -435,9 +435,9 @@ describe("a deployed forge is claimed with the link its deployer was shown, and | ||
| 435 | 435 | expect(calls.slice(0, 5)).toEqual([ |
| 436 | 436 | "GET /v2/droplets/" + first.value.remote, |
| 437 | 437 | "DELETE /v2/droplets/" + first.value.remote, |
| 438 | "GET /v2/volumes/vol-1", | |
| 438 | 439 | "POST /v2/account/keys", |
| 439 | 440 | "POST /v2/droplets", |
| 440 | "DELETE /v2/account/keys/4242", | |
| 441 | 441 | ]); |
| 442 | 442 | const next = LINK.exec(again.value.claim.link)![2]!; |
| 443 | 443 | expect(next).not.toBe(old); |
test/pinned-detach.test.ts added +42
| @@ -0,0 +1,42 @@ | ||
| 1 | import { expect, test } from "bun:test"; | |
| 2 | import { DETACH, replace, type Driver } from "../src/cloud/machine.ts"; | |
| 3 | ||
| 4 | // test1 on DigitalOcean, 2026-11-24: a replace deleted the droplet and made the new one with the | |
| 5 | // volume while the cloud was still detaching it; the create answered 422 and the forge had no | |
| 6 | // machine until someone asked again (#16342). A replace now waits for the volume to be free. | |
| 7 | function cloud(heldFor: number) { | |
| 8 | const seen: string[] = []; | |
| 9 | let asks = 0; | |
| 10 | const d: Driver = { | |
| 11 | api: "https://api.example.test", | |
| 12 | async createVolume() { return "vol"; }, | |
| 13 | async createMachine() { seen.push("create"); return "2"; }, | |
| 14 | async deleteMachine() { seen.push("delete"); }, | |
| 15 | async deleteVolume() {}, | |
| 16 | async volumeFree() { asks += 1; seen.push("free?"); return asks > heldFor; }, | |
| 17 | async readMachine() { return { status: "active", address: "" }; }, | |
| 18 | } as unknown as Driver; | |
| 19 | return { d, seen }; | |
| 20 | } | |
| 21 | const p = { remote: "1", volume: "vol", name: "acme" } as any; | |
| 22 | ||
| 23 | test("a replace waits until the old machine lets go of the volume, then makes one machine", async () => { | |
| 24 | const keep = { ...DETACH }; | |
| 25 | DETACH.everyMs = 1; | |
| 26 | try { | |
| 27 | const { d, seen } = cloud(2); | |
| 28 | const r = await replace(d, fetch, "t", p, { onDeleted() {} }); | |
| 29 | expect(r.remote).toBe("2"); | |
| 30 | expect(seen).toEqual(["delete", "free?", "free?", "free?", "create"]); | |
| 31 | } finally { Object.assign(DETACH, keep); } | |
| 32 | }); | |
| 33 | ||
| 34 | test("a volume that is never let go makes nothing and says so", async () => { | |
| 35 | const keep = { ...DETACH }; | |
| 36 | DETACH.everyMs = 1; DETACH.waitMs = 20; | |
| 37 | try { | |
| 38 | const { d, seen } = cloud(Number.POSITIVE_INFINITY); | |
| 39 | await expect(replace(d, fetch, "t", p, { onDeleted() {} })).rejects.toThrow("still holds the data volume"); | |
| 40 | expect(seen).not.toContain("create"); | |
| 41 | } finally { Object.assign(DETACH, keep); } | |
| 42 | }); |
test/pinned-forge.test.ts +3 −3
| @@ -138,10 +138,10 @@ describe("pinned forge", () => { | ||
| 138 | 138 | expect(calls.map((c) => `${c.method} ${new URL(c.url).pathname}`)).toEqual([ |
| 139 | 139 | // The machine's address is read first, to ask the forge there whether it was claimed. |
| 140 | 140 | expect.stringMatching(/^GET \/v2\/droplets\/\d+$/), |
| 141 | expect.stringMatching(/^DELETE \/v2\/droplets\/\d+$/), "POST /v2/account/keys", "POST /v2/droplets", "DELETE /v2/account/keys/4242", | |
| 141 | expect.stringMatching(/^DELETE \/v2\/droplets\/\d+$/), "GET /v2/volumes/vol-1", "POST /v2/account/keys", "POST /v2/droplets", "DELETE /v2/account/keys/4242", | |
| 142 | 142 | ]); |
| 143 | expect(calls[3]!.body.volumes).toEqual(["vol-1"]); | |
| 144 | expect(calls[3]!.body.user_data).toContain(`checkout --detach ${V2}`); | |
| 143 | expect(calls[4]!.body.volumes).toEqual(["vol-1"]); | |
| 144 | expect(calls[4]!.body.user_data).toContain(`checkout --detach ${V2}`); | |
| 145 | 145 | const [latest] = changesOf(app.db, getPinned(app.db, "acme")!.id); |
| 146 | 146 | expect(latest).toMatchObject({ kind: "update", fromVersion: V1, toVersion: V2, by: admin().id }); |
| 147 | 147 | expect(Date.parse(latest!.at)).toBeGreaterThan(0); |
test/pinned-hetzner.test.ts +2 −1
| @@ -59,6 +59,7 @@ describe("hetzner target", () => { | ||
| 59 | 59 | calls.push({ method, path: u.pathname, auth: new Headers(init?.headers).get("authorization"), body: init?.body ? JSON.parse(String(init.body)) : undefined }); |
| 60 | 60 | const json = (o: unknown, status = 200) => new Response(JSON.stringify(o), { status }); |
| 61 | 61 | if (u.pathname === "/v1/volumes") return json({ volume: { id: 77 } }, 201); |
| 62 | if (u.pathname === "/v1/volumes/77" && method === "GET") return json({ volume: { id: 77, server: null } }); | |
| 62 | 63 | if (u.pathname === "/v1/ssh_keys" && method === "POST") return json({ ssh_key: { id: 4242 } }, 201); |
| 63 | 64 | if (u.pathname === "/v1/servers" && method === "POST") { |
| 64 | 65 | if (failServer > 0) { failServer--; return json({}, 500); } |
| @@ -104,7 +105,7 @@ describe("hetzner target", () => { | ||
| 104 | 105 | expect(r.ok).toBe(true); |
| 105 | 106 | // The address is read to ask the forge whether it was claimed; whether the recorded place may |
| 106 | 107 | // be ordered is answered from the catalog, with no call; then the server is replaced. |
| 107 | expect(seen()).toEqual(["GET /v1/servers/501", "DELETE /v1/servers/501", "POST /v1/ssh_keys", "POST /v1/servers", "DELETE /v1/ssh_keys/4242"]); | |
| 108 | expect(seen()).toEqual(["GET /v1/servers/501", "DELETE /v1/servers/501", "GET /v1/volumes/77", "POST /v1/ssh_keys", "POST /v1/servers", "DELETE /v1/ssh_keys/4242"]); | |
| 108 | 109 | const made = calls.find((c) => c.method === "POST" && c.path === "/v1/servers")!; |
| 109 | 110 | expect(made.body).toMatchObject({ server_type: "cx23", location: "nbg1" }); |
| 110 | 111 | expect(made.body.volumes).toEqual([77]); |
new above
offline