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 /test | |
| 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 'test')
| -rw-r--r-- | test/behaviour/root_ctl.zig | 123 | ||||
| -rw-r--r-- | test/daemon.py | 6 | ||||
| -rw-r--r-- | test/monkey9p.zig | 10 | ||||
| -rw-r--r-- | test/process/tree.zig | 2 | ||||
| -rw-r--r-- | test/session.zig | 20 |
5 files changed, 155 insertions, 6 deletions
diff --git a/test/behaviour/root_ctl.zig b/test/behaviour/root_ctl.zig index b9909eef..7cf3025e 100644 --- a/test/behaviour/root_ctl.zig +++ b/test/behaviour/root_ctl.zig @@ -140,3 +140,126 @@ test "root-ctl: Joincol with no column to the right and Theme with no such theme try s.write("/ctl", "Theme DRACULA\n"); try std.testing.expect(has(try s.read("/ctl"), "\nTheme dracula\n")); } + +/// The labels /dumps lists, newest first. +fn labels(s: *Session) ![]const []const u8 { + var out: std.ArrayList([]const u8) = .empty; + var lines = std.mem.tokenizeScalar(u8, try s.read("/dumps"), '\n'); + while (lines.next()) |line| try out.append(s.arena.allocator(), line[0..std.mem.indexOfScalar(u8, line, ' ').?]); + return out.items; +} + +fn stateSize(dir: []const u8) !u64 { + var buf: [4200]u8 = undefined; + return (try std.Io.Dir.cwd().statFile(std.testing.io, try std.fmt.bufPrint(&buf, "{s}/pardes.xitdb", .{dir}), .{})).size; +} + +test "root-ctl: Dump keeps the session in the state file under a label /dumps lists and the workspace tag names, and Restore of it brings it back" { + var s = try Session.initFile(std.testing.allocator, "dumped text\n"); + defer s.deinit(); + const pane = try s.focused(); + try s.write("/ctl", "Dump\n"); + const listed = try labels(&s); + try std.testing.expectEqual(@as(usize, 1), listed.len); + try std.testing.expect(std.mem.startsWith(u8, listed[0], "pardes-")); + // `<label> <date> <size> <panes>`: one pane, its text in its size. + var words = std.mem.tokenizeScalar(u8, std.mem.trimEnd(u8, try s.read("/dumps"), "\n"), ' '); + _ = words.next(); + try std.testing.expect(std.mem.endsWith(u8, words.next().?, "Z")); + try std.testing.expect(try std.fmt.parseInt(u64, words.next().?, 10) > "dumped text\n".len); + try std.testing.expectEqualStrings("1", words.next().?); + try std.testing.expect(has(try s.read("/tag"), s.at("Restore {s}", .{listed[0]}))); + try s.write(s.at("/pane/{d}/body", .{pane}), "changed\n"); + // Unsaved text is asked about once; the second Restore goes ahead. + if (try s.refused("/ctl", s.at("Restore {s}\n", .{listed[0]})) != 0) try s.write("/ctl", s.at("Restore {s}\n", .{listed[0]})); + try s.expect(s.at("/pane/{d}/body", .{pane}), "dumped text\n"); + try std.testing.expect(has(try s.read("/log"), s.at("restore {s}\n", .{listed[0]}))); + // Bare, the last one this session made. + try s.write("/ctl", "Restore\n"); + try s.expect(s.at("/pane/{d}/body", .{pane}), "dumped text\n"); +} + +test "root-ctl: a Restore of a label the state file keeps none of is refused in words, changing nothing" { + var s = try Session.initFile(std.testing.allocator, "kept\n"); + defer s.deinit(); + try std.testing.expect(try s.refused("/ctl", "Restore pardes-19990101-000000\n") != 0); + try std.testing.expect(has(s.said, "Restore: no dump labelled pardes-19990101-000000")); + try std.testing.expect(try s.refused("/ctl", "Restore\n") != 0); + try std.testing.expect(has(s.said, "Restore: no dump yet; name one")); + try s.expect(s.at("/pane/{d}/body", .{try s.focused()}), "kept\n"); +} + +test "root-ctl: the state file keeps the newest 32 dumps: the 33rd drops the first" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + try s.write("/ctl", "Dump\n"); + const first = (try labels(&s))[0]; + for (0..32) |_| try s.write("/ctl", "Dump\n"); + const kept = try labels(&s); + try std.testing.expectEqual(@as(usize, 32), kept.len); + for (kept) |l| try std.testing.expect(!std.mem.eql(u8, l, first)); + try std.testing.expect(try s.refused("/ctl", s.at("Restore {s}\n", .{first})) != 0); + try std.testing.expect(has(s.said, "no dump labelled")); +} + +test "root-ctl: a text unchanged since the last Dump is not written again: the state file grows by the session, not the text" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + var dir_buf: [std.fs.max_path_bytes]u8 = undefined; + const dir = dir_buf[0..try tmp.dir.realPath(std.testing.io, &dir_buf)]; + var s = try Session.initShared(std.testing.allocator, dir); + defer s.deinit(); + const pane = try s.newPane(); + const text = try s.arena.allocator().alloc(u8, 1 << 20); + for (text, 0..) |*b, i| b.* = if (i % 64 == 63) '\n' else 'a' + @as(u8, @intCast(i % 26)); + try s.write(s.at("/pane/{d}/body", .{pane}), text); + const before = try stateSize(dir); + try s.write("/ctl", "Dump\n"); + const once = try stateSize(dir); + try std.testing.expect(once - before > text.len); + try s.write("/ctl", "Dump\n"); + // The session's own maps, a few hundred KB: not the text again. + const twice = try stateSize(dir); + try std.testing.expect(twice - once < text.len / 2); + try std.testing.expectEqual(@as(usize, 2), (try labels(&s)).len); +} + +test "root-ctl: a Dump killed in the middle leaves the one before it whole, and the next one is made" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + var dir_buf: [std.fs.max_path_bytes]u8 = undefined; + const dir = dir_buf[0..try tmp.dir.realPath(std.testing.io, &dir_buf)]; + var label_buf: [64]u8 = undefined; + var label: []const u8 = ""; + { + var s = try Session.initShared(std.testing.allocator, dir); + defer s.deinit(); + try s.write(s.at("/pane/{d}/body", .{try s.newPane()}), "the dump before\n"); + try s.write("/ctl", "Dump\n"); + const listed = try labels(&s); + label = label_buf[0..listed[0].len]; + @memcpy(label_buf[0..listed[0].len], listed[0]); + } + // What a Dump killed before its commit leaves: its bytes past the end + // the file's header says is committed. + { + const file = try tmp.dir.openFile(std.testing.io, "pardes.xitdb", .{ .mode = .read_write }); + defer file.close(std.testing.io); + const end = try file.length(std.testing.io); + var junk: [70000]u8 = undefined; + for (&junk, 0..) |*b, i| b.* = @truncate(i *% 131 +% 7); + try file.writePositionalAll(std.testing.io, &junk, end); + } + var s = try Session.initShared(std.testing.allocator, dir); + defer s.deinit(); + try std.testing.expectEqualStrings(label, (try labels(&s))[0]); + try s.write("/ctl", s.at("Restore {s}\n", .{label})); + try std.testing.expect(has(try s.read("/index"), "+New")); + var rows = std.mem.tokenizeScalar(u8, try s.read("/index"), '\n'); + const scratch = while (rows.next()) |row| { + if (has(row, "+New")) break row[0..std.mem.indexOfScalar(u8, row, ' ').?]; + } else return error.NoScratch; + try s.expect(s.at("/pane/{s}/body", .{scratch}), "the dump before\n"); + try s.write("/ctl", "Dump\n"); + try std.testing.expectEqual(@as(usize, 2), (try labels(&s)).len); +} diff --git a/test/daemon.py b/test/daemon.py index 52e0bccd..0c4c2f3c 100644 --- a/test/daemon.py +++ b/test/daemon.py @@ -1589,8 +1589,10 @@ def upgrades(old, new): client.write(f'/pane/{shell}/exec', b'Upgrade\n') client.socket.close() client = saying(sock, 'brings the rest back') - kept = root / '.local' / 'share' / 'pardes' / f'pardes-upgrade-{session}.dump' - assert kept.exists(), list((root / '.local' / 'share' / 'pardes').iterdir()) + # The dump stays in the state file, under its label (a second + # upgrade of one pid is `upgrade-<pid>-2`). + kept = client.read('/dumps').decode() + assert any(line.split()[0].startswith(f'upgrade-{session}') for line in kept.splitlines()), kept assert alive(shell_pid) and ppid(shell_pid) == session pids = {p: run(client, p, b'echo $$\n') for p in terminals(client)} adopted = [p for p, out in pids.items() if out.split(b'\n')[1:2] == [str(shell_pid).encode()]] diff --git a/test/monkey9p.zig b/test/monkey9p.zig index d6568d1e..f33c58f8 100644 --- a/test/monkey9p.zig +++ b/test/monkey9p.zig @@ -15,8 +15,9 @@ //! directories and working directory, so a panic or a hang is a failure //! the parent reports and shrinks like any other. //! -//! What T1 leaves out: Session performs no effects (no shell runs, nothing -//! is saved or dumped, a Restore hangs nothing up), and no 9P engine stands +//! What T1 leaves out: Session performs few effects (no shell runs, nothing +//! is saved; a Dump goes to its state in memory, and a Restore puts a new +//! core in the old one's place, its fids gone as a hang-up's), and no 9P engine stands //! in front of the core, so the engine's own refusals (a write on an open //! for reading, a directory opened to write, a mode bit missing) and //! OTRUNC's truncation first are made here, as cloud9's fs.zig makes them. @@ -78,7 +79,7 @@ const builtins = [_][]const u8{ "Sort", "Font", "Local", }; const pane_files = [_][]const u8{ "name", "body", "tag", "ctl", "addr", "dot", "limit", "data", "xdata", "sel", "dirty", "mark", "scroll", "errors", "event", "look", "exec", "tagexec", "pty", "pty/run", "pty/data", "pty/ctl", "pty/status" }; -const root_files = [_][]const u8{ "README", "index", "status", "focus", "ctl", "commands", "recent", "look", "exec", "log", "screen", "listeners", "layout", "tag", "tagexec", "pane", "pane/new", "col", "" }; +const root_files = [_][]const u8{ "README", "index", "status", "focus", "ctl", "commands", "recent", "dumps", "look", "exec", "log", "screen", "listeners", "layout", "tag", "tagexec", "pane", "pane/new", "col", "" }; const long_x: [300]u8 = @splat('x'); const col_files = [_][]const u8{ "tag", "ctl", "exec" }; const writable = [_][]const u8{ "ctl", "exec", "look", "tag", "tagexec", "focus", "body", "data", "xdata", "addr", "dot", "limit", "name", "sel", "dirty", "mark", "scroll", "errors", "event", "pty/run", "pty/data", "pty/ctl", "log" }; @@ -931,6 +932,9 @@ fn exec(c: *Ctx, a: Allocator, res: *Res, op: Op) anyerror!void { for (c.handles.items) |*h| _ = c.s().ask(&h.fid.w, .clunk, .{}); c.handles.clearRetainingCapacity(); c.marker = null; + // The log's follower with them: the new core's log is its own. + c.follower = null; + c.lines.clearRetainingCapacity(); }, .host => |o| if (o.data) |data| try putFile(a, c.root, o.name, data) else removeFile(a, c.root, o.name), .seq => |ops| for (ops) |sub| try exec(c, a, res, sub), diff --git a/test/process/tree.zig b/test/process/tree.zig index 70b2a89c..0136cc80 100644 --- a/test/process/tree.zig +++ b/test/process/tree.zig @@ -105,7 +105,7 @@ fn discovery(gpa: std.mem.Allocator, exe: [:0]const u8) !void { const top = try c.list(a, "/"); // A set: which files the root holds, not the order a listing gives. - const want = [_][]const u8{ "README", "index", "status", "focus", "ctl", "commands", "recent", "look", "exec", "pager", "log", "screen", "listeners", "sessions", "layout", "tag", "tagexec", "kbdin", "mousein", "pane", "col", "os" }; + const want = [_][]const u8{ "README", "index", "status", "focus", "ctl", "commands", "recent", "dumps", "look", "exec", "pager", "log", "screen", "listeners", "sessions", "layout", "tag", "tagexec", "kbdin", "mousein", "pane", "col", "os" }; var others: usize = 0; for (top) |n| { if (contains(&.{ "src", "shaders" }, n)) continue; diff --git a/test/session.zig b/test/session.zig index 71d723a4..71627dad 100644 --- a/test/session.zig +++ b/test/session.zig @@ -196,12 +196,17 @@ pub const Session = struct { // As the server does (src/9p_io.zig): a clunk's held lines run then. if (q.op == .release) pardes.ctlfs.runClosedLines(pardesOf(s)); s.drain(); + // A Restore asked for is performed once its write is answered (the + // write's reply holds nothing of the core it replaces). + s.restoreIfAsked(); return r; } fn drain(s: *Session) void { while (pardesOf(s).nextEffect()) |e| switch (e) { .save_file, .save_text => s.saved += 1, + // Into the session's state (a database in memory, or its file). + .write_dump => pardes.host_io.writeDump(pardesOf(s), pardesOf(s).dump_out.?), .lsp => |q| if (s.languages) |io| s.askServer(io, .{ .id = q.id, .kind = q.kind, .pane = q.pane, .offset = q.offset, .arg = q.arg.slice() }), else => {}, }; @@ -224,6 +229,21 @@ pub const Session = struct { p.update(.{ .lsp_resp = .{ .id = req.id, .rows = rows } }); } + /// A Restore the write asked for, performed as a host performs it after + /// the write is answered (host_io.restoreCore): the dump read, and the + /// new core in the old one's place. + fn restoreIfAsked(s: *Session) void { + const p = pardesOf(s); + const gpa = p.gpa; + const name = p.takeRestore() orelse return; + const bytes = pardes.dump.restoreBytes(p, gpa, name) catch |err| return p.reportError(p.active, "Restore", err); + defer gpa.free(bytes); + const nc = pardes.dump.restore(p, bytes, name) catch |err| return p.reportError(p.active, "Restore", err); + p.deinit(); + s.core = nc; + s.drain(); + } + fn pardesOf(s: *Session) *Pardes { return @ptrCast(@alignCast(s.core)); } |
