Repository navigation
Expand file tree
/
Copy pathcheckpoints.ts
More file actions
142 lines (132 loc) · 6.76 KB
/
Copy pathcheckpoints.ts
File metadata and controls
142 lines (132 loc) · 6.76 KB
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
// A machine's checkpoints: saving its disks under a name, going back to one, and deleting one.
import { boot, bootEffect, diskCopy, restoreEffects, restoring, samePlan } from "./machines.ts";
import { closedWithIt, shellsClosed } from "./sessions.ts";
import { later, patch, put, step } from "./step.ts";
import { ACCEPTS, claimPorts, isPlanError, nicPortsOf, onAttempt, plugsOf, savingOf } from "./topology.ts";
import type { CheckpointName, LabState, Machine, MachineName, PlanError, RestorePlan, Saving, Step } from "./types.ts";
import { attachEffects, linksDown, plugInto } from "./wiring.ts";
/** What going back to the checkpoint of that number would take, or null when the machine cannot now. */
export function planRestore(state: LabState, name: MachineName, set: number): RestorePlan | PlanError | null {
const machine = state.machines[name];
if (!machine?.sets.checkpoints.some((checkpoint) => checkpoint.set === set)) return null;
const { life, spec } = machine;
if (!ACCEPTS[life.phase].restore || savingOf(life)) return null;
if (life.phase !== "failed") return { machine: name, set, ports: nicPortsOf(life) };
const ports = claimPorts(
state,
spec.nics.map(({ net }) => net),
);
return isPlanError(ports) ? ports : { machine: name, set, ports };
}
/** A person's name for the machine's disks as they are now. Ignored unless it is free. */
export function saveCheckpoint(state: LabState, name: MachineName, checkpoint: CheckpointName): Step {
const machine = state.machines[name];
if (machine?.life.phase !== "ready" && machine?.life.phase !== "off") return step(state);
const { life, sets } = machine;
if (life.saving || sets.checkpoints.some((other) => other.name === checkpoint)) return step(state);
const set = sets.next;
const saving: Machine = {
...machine,
sets: { ...sets, next: set + 1 },
unsaved: null,
life: { ...life, saving: { name: checkpoint, set, percent: 0 } },
};
return step(put(state, saving), [
{ type: "checkpoint/save", copy: diskCopy(machine, checkpoint, sets.live, set) },
{ type: "announce", text: `saving checkpoint ${checkpoint} of ${name}` },
]);
}
/** How much of the set that a machine is writing has been written. */
export function checkpointProgress(
state: LabState,
name: MachineName,
attempt: number,
set: number,
percent: number,
): Step {
const machine = onAttempt(state, name, attempt);
if (!machine) return step(state);
const { life } = machine;
if (life.phase === "restoring" && life.set === set)
return step(put(state, { ...machine, life: patch(life, { percent }) }));
const saving = savingOf(life);
if ((life.phase !== "ready" && life.phase !== "off") || saving?.set !== set) return step(state);
return step(put(state, { ...machine, life: { ...life, saving: patch(saving, { percent }) } }));
}
/** The machine, no longer saving the set, or null when it was not. */
function savingOver(state: LabState, name: MachineName, attempt: number, set: number): [Machine, Saving] | null {
const machine = onAttempt(state, name, attempt);
if (machine?.life.phase !== "ready" && machine?.life.phase !== "off") return null;
const { saving, ...life } = machine.life;
return saving?.set === set ? [{ ...machine, life }, saving] : null;
}
export function checkpointSaved(state: LabState, name: MachineName, attempt: number, set: number, bytes: number): Step {
const over = savingOver(state, name, attempt, set);
if (!over) return step(state);
const [machine, saving] = over;
const checkpoints = [...machine.sets.checkpoints, { name: saving.name, set, bytes }];
return step(put(state, { ...machine, sets: { ...machine.sets, checkpoints } }), [
{ type: "announce", text: `checkpoint ${saving.name} of ${name} saved` },
]);
}
export function checkpointUnsaved(
state: LabState,
name: MachineName,
attempt: number,
set: number,
reason: string,
): Step {
const over = savingOver(state, name, attempt, set);
if (!over) return step(state);
return step(put(state, { ...over[0], unsaved: { set, reason } }), [{ type: "announce", text: reason }]);
}
/** The guest is dropped, as its disks are, and the machine keeps its cables in. Ignored unless that is still the plan. */
export function restore(state: LabState, plan: RestorePlan): Step {
const fresh = planRestore(state, plan.machine, plan.set);
if (fresh === null || isPlanError(fresh) || !samePlan(fresh, plan)) return step(state);
const machine = state.machines[fresh.machine];
const from = machine?.sets.checkpoints.find(({ set }) => set === fresh.set);
if (!machine || !from) return step(state);
const { name, life } = machine;
const alone = shellsClosed(state, machine);
const [, ...closed] = machine.sessions;
const held = life.phase === "failed" ? null : plugsOf(life);
const back = restoring(alone.machine, from, life.attempt + 1, held ?? plugInto(fresh.ports), closed);
const doing = `${name} going back to checkpoint ${from.name}`;
const said = closed.length === 0 ? doing : [`${doing}.`, ...closedWithIt(closed)].join(" ");
return step(put(alone.state, { ...back, lastStop: held ? "clean" : null }), [
...linksDown(machine, "power"),
{ type: "vm/stop", machine: name, attempt: back.life.attempt, how: "drop" },
...(held ? [] : attachEffects(back)),
...alone.effects,
{ type: "announce", text: said },
...restoreEffects(back),
]);
}
/** The copy is whole, so it is what the machine has from here on. */
export function restored(state: LabState, name: MachineName, attempt: number, set: number, at: number): Step {
const machine = onAttempt(state, name, attempt);
if (machine?.life.phase !== "restoring" || machine.life.set !== set) return step(state);
const { plugs, from, closed } = machine.life;
const replaced = machine.sets.live;
const booting = boot({ ...machine, sets: { ...machine.sets, live: set } }, plugs, at);
const after = put({ ...state, now: later(state.now, at) }, booting);
const note = [`${name} is back at checkpoint ${from.name}.`, ...closedWithIt(closed)].join(" ");
return step(after, [
{ type: "screen/note", session: machine.sessions[0], text: note },
{ type: "disks/delete", n: machine.n, sets: [replaced] },
bootEffect(booting, after),
]);
}
/** A checkpoint that the machine is going back to stays until it has. */
export function forget(state: LabState, name: MachineName, set: number): Step {
const machine = state.machines[name];
if (!machine || machine.life.phase === "removing") return step(state);
if (machine.life.phase === "restoring" && machine.life.from.set === set) return step(state);
const checkpoints = machine.sets.checkpoints.filter((checkpoint) => checkpoint.set !== set);
if (checkpoints.length === machine.sets.checkpoints.length) return step(state);
return step(put(state, { ...machine, sets: { ...machine.sets, checkpoints } }), [
{ type: "disks/delete", n: machine.n, sets: [set] },
{ type: "announce", text: `checkpoint of ${name} deleted` },
]);
}