diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-05 14:42:45 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-05 19:16:37 -0300 |
| commit | 15a1b94eaa73747afaa299262a2d6e4fd6af42fd (patch) | |
| tree | 46bff4f35b642521eead62bc3b5657902e5fee0b /src/upgrade.zig | |
| parent | 4e096f6dc6db043caa630514fa75bd18438cb15b (diff) | |
| download | pardes-15a1b94eaa73747afaa299262a2d6e4fd6af42fd.tar.gz pardes-15a1b94eaa73747afaa299262a2d6e4fd6af42fd.zip | |
dumps: a Dump is a labelled entry of the state file (src/Db.zig, architecture §14.3), one fsynced transaction: `dumps/<label>` holds the session as a database of its own, which the walk writes by field name into xitdb's maps (walk.Put: a struct a map keyed by its fields' names, a union a map of its one field, records a list, plain numbers one bytes value tagged with their kind and width, an enum its name, a number a uint or an int, an optional an absent key, a type with dumpWrite its bytes, a .blob text of 4 KiB and up its BLAKE3 hash) and another build reads by name (walk.Get: defaults, notes, a pane it cannot read left out, as the byte loader did), with the hashes of the texts it keeps once under `blobs/`, so an unchanged text is written once across dumps; labels are `pardes-<date>-<time>` (`-2` and on in one second), `last` names the newest, the newest 32 are kept and the texts no kept one names are dropped in the same transaction, and at a session's end the file is compacted into `pardes.xitdb.new` and renamed over once it is past twice what it holds plus 64 MiB (another session notices the new file by its inode); a dump travels as an image, a database in memory holding it alone, deterministic (std.Io.failing's zero ids, so the replay's fixpoint holds): what Dump hands its host, what `Dump export <path>` and $PARDES_DUMP write whole (the snapshot goldens' `Restore <path>` unchanged), what a Restore of a label is read into, what a handover carries (`handover-<seq>`), and what `-l` and `Restore <path>` read; Restore takes a label or a path (bare: this session's last dump, else the newest), a label the file keeps none of refused in words (`Restore: no dump labelled X`, asked of the authority as the closed panes are); `/dumps` lists them, `<label> <date> <size> <panes>`, the newest first; Upgrade commits `upgrade-<pid>` before the exec, its record naming the state file and the label (a record without one, an older build's, names its `.dump` file, read as before), and the label stays; the byte dump stays for the join, the closed panes and the old `.dump` files (dump.byteDump), and dump.outPath, uniqueIn and upgradePath go for dump.label; measured (Debug, a session with a 3.2 MB file and three scratch panes, 20 dumps): the image on the loop 64-66 ms (59 ms of it BLAKE3 of the file, 0.65 ms in ReleaseFast) where the byte dump was 0.7 ms, the commit 9-16 ms with its fsyncs where the file was written in 2, a Restore 1.8 ms to read and 11.4 to load, and the file 3.55 MB after the first dump and 379 KB a dump after it with the big text unchanged (3.58 MB a dump with it edited each time, as twenty `.dump` files would be); tests: dump.zig's 'two dumps in one second get names of their own' and 'a dump makes every directory missing on the way to its DumpDir' become Db.zig's 'two dumps with one label get labels of their own' and 'a Dump makes every directory missing on the way to its DumpDir, and the file there', 'a session read by name, field by field as another build reads it, is the same session' becomes 'a session's byte dump read by name...' beside the new 'a session's image, read by name as every load reads it, is the same session, and dumps to the same bytes again', two replica tests compare the join's stream with dump.byteDump, the Handover tests read the carried image, upgrade's record test carries a label, the root listing tests and the monkey list `dumps`, the behaviour suite gains five root-ctl tests (a Dump and Restore round trip through 9P with `/dumps` and the tag's word, a label kept none of refused in words, the 33rd dump dropping the first, an unchanged text written once across two dumps by the file's size, a killed Dump leaving the one before it whole), the test host performs a Dump's write and a Restore as a host does, the monkey lets go of its log follower at a Restore, and daemon.py's upgrade case finds the kept dump in `/dumps`
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'src/upgrade.zig')
| -rw-r--r-- | src/upgrade.zig | 77 |
1 files changed, 47 insertions, 30 deletions
diff --git a/src/upgrade.zig b/src/upgrade.zig index d8f8f3c3..212a8176 100644 --- a/src/upgrade.zig +++ b/src/upgrade.zig @@ -2,15 +2,17 @@ //! session, and the daemon, moved onto the pardes installed since they //! started, in place. //! -//! At a step boundary a session writes its dump (phase 2) and execs the -//! path it was started from. Its pty masters, terminals, stubs and daemon +//! At a step boundary a session commits its dump to the state file +//! (Db.zig, as `upgrade-<pid>`) and execs the path it was started from. Its pty masters, terminals, stubs and daemon //! link stay open across the exec, named in a record on the new command //! line: `--upgraded=<record>` ahead of the arguments it was started with. //! The pid stays, so its shells stay its children; the new process loads //! the dump and takes those fds back (each host's `resume`). A new build //! that cannot load the dump execs the old one again, from the copy this //! process left open (Linux), and with none it starts afresh with the -//! shells adopted, the dump left for a Restore by hand. +//! shells adopted, the dump left for a Restore by hand. A record of an older +//! build names a `.dump` file instead (0.24 reads one, and deletes it once +//! loaded). const std = @import("std"); const builtin = @import("builtin"); const libc = std.c; @@ -114,8 +116,11 @@ pub const Record = struct { nttys: usize = 0, /// The path exec'd, the one a later Upgrade execs again. exe: []const u8 = "", - /// The session's dump. + /// The state file the session's dump is in (an older build's: the + /// `.dump` file it is). dump: []const u8 = "", + /// The dump's label there; none, `dump` is a file of its own. + label: []const u8 = "", /// Why the new build could not load it: a start that went back to the /// old binary says so. refused: []const u8 = "", @@ -158,6 +163,7 @@ pub const Record = struct { for (rec.ptys[0..rec.nptys]) |p| try w.print("pty {d} {d} {d}\n", .{ p.pane, p.pid, p.fd }); for (rec.ttys[0..rec.nttys]) |t| try w.print("tty {d} {d} {d} {d} {x}\n", .{ t.view, t.fd, t.stub, @intFromBool(t.primary), std.mem.asBytes(&t.termios) }); if (rec.refused.len > 0) try w.print("refused {s}\n", .{rec.refused}); + if (rec.label.len > 0) try w.print("label {s}\n", .{rec.label}); try w.print("exe {s}\ndump {s}\n", .{ rec.exe, rec.dump }); } @@ -206,6 +212,8 @@ pub const Record = struct { rec.nttys += 1; } else if (std.mem.eql(u8, word, "refused")) { rec.refused = rest; + } else if (std.mem.eql(u8, word, "label")) { + rec.label = rest; } else if (std.mem.eql(u8, word, "exe")) { rec.exe = rest; } else if (std.mem.eql(u8, word, "dump")) { @@ -333,44 +341,44 @@ pub fn exec(gpa: std.mem.Allocator, path: ?[:0]const u8, old: c_int, rec: *const }; } -/// The dump of a session to `path` (0600, replaced): false when it could -/// not be written whole. -fn writeDump(path: [:0]const u8, bytes: []const u8) bool { - const fd = libc.open(path, .{ .ACCMODE = .WRONLY, .CREAT = true, .TRUNC = true, .CLOEXEC = true }, @as(libc.mode_t, 0o600)); - if (fd < 0) return false; - defer _ = libc.close(fd); - if (@import("host_io.zig").writeFd(fd, bytes)) return true; - _ = libc.unlink(path); - return false; -} +/// The state file's path an upgrade's record names (`dumpInto`'s). +var state_path: [4200]u8 = undefined; -/// An Upgrade's dump, the terminal's and the window's alike: written -/// where a Restore finds it (dump.upgradePath, into `at`) and named in -/// `rec`, the binary it replaces held open for a new build that cannot -/// load it. Null when it is, else why in words (`why`'s). +/// An Upgrade's dump, the terminal's and the window's alike: committed to +/// the state file as `upgrade-<pid>` (its label kept in `at`), where a +/// Restore finds it, and named in `rec`, the binary it replaces held open +/// for a new build that cannot load it. Null when it is, else why in words +/// (`why`'s). pub fn dumpInto(core: *pardes.Pardes, rec: *Record, at: *[1024:0]u8, why: []u8) ?[]const u8 { - const path = pardes.dump.upgradePath(at, core.settings.dump_dir.get()) orelse return "Upgrade: no directory for the session's dump; this session stays as it is"; - const bytes = pardes.dump.dumpBytes(core) catch |err| { + const path = pardes.Db.statePath(&state_path, core.settings.dump_dir.get()) orelse return "Upgrade: no directory for the session's dump; this session stays as it is"; + var name: [64]u8 = undefined; + const label = std.fmt.bufPrint(&name, "upgrade-{d}", .{std.c.getpid()}) catch unreachable; + const bytes = pardes.dump.image(core, label) catch |err| { var words: [128]u8 = undefined; return std.fmt.bufPrint(why, "Upgrade: the session would not dump: {s}; it stays as it is", .{pardes.Messages.errorWords(err, &words)}) catch "Upgrade: the session would not dump"; }; defer core.gpa.free(bytes); - if (!writeDump(path, bytes)) return std.fmt.bufPrint(why, "Upgrade: the session's dump could not be written to {s}; it stays as it is", .{path[0..@min(path.len, 600)]}) catch "Upgrade: its dump could not be written"; + const put = core.db.putDump(core, bytes, label, at[0 .. at.len - 1]) catch |err| { + var words: [128]u8 = undefined; + return std.fmt.bufPrint(why, "Upgrade: the session's dump could not be written to {s} ({s}); it stays as it is", .{ path[0..@min(path.len, 600)], pardes.Messages.errorWords(err, &words) }) catch "Upgrade: its dump could not be written"; + }; + at[put.len] = 0; rec.dump = path; + rec.label = at[0..put.len]; rec.old = openSelf(); return null; } /// The daemon told, 9P's last answers sent (`fs`), and this process -/// exec'd as `path` with `rec`, whose dump `dumpInto` wrote at `at`. -/// Returns only when the exec failed, that dump removed and the binary it -/// replaces let go: why, in words (`why`'s). +/// exec'd as `path` with `rec`, whose dump `dumpInto` labelled `at`. +/// Returns only when the exec failed, the binary it replaces let go (the +/// dump stays, a dump among the state file's): why, in words (`why`'s). pub fn execAs(gpa: std.mem.Allocator, path: [:0]const u8, rec: *const Record, at: [*:0]const u8, fs: ?*@import("9p_io.zig").Listener, why: []u8) []const u8 { + _ = at; @import("daemon.zig").upgraded(); if (fs) |f| f.flushOut(); const err_words = exec(gpa, path, -1, rec); // Still here: the exec failed. - _ = libc.unlink(at); if (rec.old >= 0) _ = libc.close(rec.old); return std.fmt.bufPrint(why, "Upgrade: {s} would not start: {s}; this session stays as it is", .{ path[0..@min(path.len, 600)], err_words }) catch "Upgrade: the new pardes would not start"; } @@ -391,13 +399,17 @@ pub fn load(gpa: std.mem.Allocator, core_gpa: std.mem.Allocator, opts: pardes.Op const loaded = loaded: { if (libc.getenv("PARDES_TEST_UPGRADE_REFUSE")) |how| if (rec.refused.len == 0 or std.mem.eql(u8, std.mem.span(how), "all")) break :loaded error.RefusedByTest; - const bytes = pardes.filesystem.readFile(gpa, rec.dump) catch |err| break :loaded err; + const bytes = (if (rec.label.len > 0) pardes.Db.readDumpAt(gpa, rec.dump, rec.label) else pardes.filesystem.readFile(gpa, rec.dump)) catch |err| break :loaded err; defer gpa.free(bytes); break :loaded pardes.dump.resumeFromDump(core_gpa, opts, bytes, &kept); }; var buf: [700]u8 = undefined; + // Restore's word for the dump left behind: its label, or its file. + const again_name = if (rec.label.len > 0) rec.label else rec.dump; if (loaded) |p| { - _ = libc.unlink(path); + // An older build's dump, a file of its own, goes once loaded; a + // label stays among the state file's dumps. + if (rec.label.len == 0) _ = libc.unlink(path); // The binary it replaced, let go: held, its file (deleted) would // keep its blocks for as long as this session runs. if (rec.old >= 0) _ = libc.close(rec.old); @@ -424,13 +436,13 @@ pub fn load(gpa: std.mem.Allocator, core_gpa: std.mem.Allocator, opts: pardes.Op var o = opts; o.load_path = null; const p = try pardes.Pardes.init(core_gpa, o); - p.setMessage(p.active, std.fmt.bufPrint(&buf, "Upgrade: no build could load the session ({s}); its shells are here, and Restore {s} brings the rest back", .{ why, rec.dump[0..@min(rec.dump.len, 400)] }) catch "Upgrade: no build could load the session; its dump is kept for a Restore"); + p.setMessage(p.active, std.fmt.bufPrint(&buf, "Upgrade: no build could load the session ({s}); its shells are here, and Restore {s} brings the rest back", .{ why, again_name[0..@min(again_name.len, 400)] }) catch "Upgrade: no build could load the session; its dump is kept for a Restore"); return p; } } test "an upgrade's record reads back as it was written, and refuses one cut short" { - var rec: Record = .{ .link = 5, .stub = 7, .old = 9, .window = true, .exe = "/a b/pardes", .dump = "/run/u/pardes-upgrade-1.dump" }; + var rec: Record = .{ .link = 5, .stub = 7, .old = 9, .window = true, .exe = "/a b/pardes", .dump = "/home/u/.local/share/pardes/pardes.xitdb", .label = "upgrade-1" }; rec.addPty(3, 1234, 11); rec.addPty(0, 99, 12); var tio: posix.termios = std.mem.zeroes(posix.termios); @@ -449,7 +461,12 @@ test "an upgrade's record reads back as it was written, and refuses one cut shor try std.testing.expect(back.ttys[0].primary and back.ttys[0].view == 2 and back.ttys[0].stub == 14); try std.testing.expectEqual(@as(u8, 0x5a), std.mem.asBytes(&back.ttys[0].termios)[0]); try std.testing.expectEqualStrings("/a b/pardes", back.exe); - try std.testing.expectEqualStrings("/run/u/pardes-upgrade-1.dump", back.dump); + try std.testing.expectEqualStrings("/home/u/.local/share/pardes/pardes.xitdb", back.dump); + try std.testing.expectEqualStrings("upgrade-1", back.label); + // An older build's: its dump a file, no label. + const older = try Record.decode("link 5\nexe /a\ndump /run/u/pardes-upgrade-1.dump\n"); + try std.testing.expectEqualStrings("/run/u/pardes-upgrade-1.dump", older.dump); + try std.testing.expectEqualStrings("", older.label); try std.testing.expectError(error.BadRecord, Record.decode("link 5\n")); try std.testing.expectError(error.BadRecord, Record.decode("tty 0 1 2 1 abc\ndump x\n")); } |
