diff options
Diffstat (limited to 'core/replica.zig')
| -rw-r--r-- | core/replica.zig | 132 |
1 files changed, 106 insertions, 26 deletions
diff --git a/core/replica.zig b/core/replica.zig index 75aa94e1..0d1802b5 100644 --- a/core/replica.zig +++ b/core/replica.zig @@ -2834,14 +2834,33 @@ fn fromDump(gpa: Allocator, j: *Joined, o: pardes.Options, have: ?*Have, why: [] return null; }; if (!r.loaded(j.digest)) { + var named: [400]u8 = undefined; + const fields = loadDiffers(r, j.dump, &named); r.deinit(); - _ = std.fmt.bufPrint(why, "the session's dump loaded as another session (a build's dump is not complete)\x00", .{}) catch {}; + log.warn("client {d}: the session's dump loaded as another session: {s}", .{ j.client, fields }); + _ = std.fmt.bufPrint(why, "the session's dump loaded as another session: {s}differs\x00", .{fields}) catch {}; return null; } takeJoined(r, j, have, t0); return r; } +/// The fields a replica loaded from the join's dump `bytes` has other than +/// the session as it dumped them (the digest a field at a time the dump +/// ends with: dump.joinParts), named in `out`. +fn loadDiffers(r: *Replica, bytes: []const u8, out: []u8) []const u8 { + const want = pardes.dump.joinParts(r.gpa, bytes) orelse return "(the dump names no field) "; + defer r.gpa.free(want); + var arena: std.heap.ArenaAllocator = .init(r.gpa); + defer arena.deinit(); + const got = arena.allocator().create(tape.Parts) catch return ""; + const was = Stepping.begin(&r.t, &r.epoch); + const parts = tape.digestParts(r.core(), got); + was.end(); + const fields = tape.partsDiffer(parts, want, arena.allocator()); + return std.fmt.bufPrint(out, "{s}", .{fields[0..@min(fields.len, out.len)]}) catch out; +} + /// What a replica joined by `j` takes of it, and of the environment. fn takeJoined(r: *Replica, j: *Joined, have: ?*Have, t0: u64) void { // PARDES_NO_ECHO: no predicted echo (the gates' baseline). @@ -7609,36 +7628,97 @@ const NinepJoin = struct { _ = try n.serve(.{ .tag = 2, .op = .write, .node = at, .handle = h, .data = data }, true); } - /// A client joined now and let go: its load is the session as dumped, - /// at the welcome's digest, the fields that differ named. fn loadsSame(n: *NinepJoin) !void { - const gpa = std.testing.allocator; - const joined = (try n.a.join(1, null)) orelse return error.NoView; - defer gpa.free(joined.dump); - const r = try Replica.initDump(gpa, 1, joined.view, joined.dump, .{ .cols = 80, .rows = 24 }); - defer r.deinit(); - if (!r.loaded(joined.digest)) { - var arena: std.heap.ArenaAllocator = .init(gpa); - defer arena.deinit(); - std.debug.print("the client's load is not the session: {s}\n", .{try loadDiffers(n.a, r, arena.allocator())}); - return error.LoadedAnother; - } - _ = try n.a.leave(1, false); + try loadsAsSession(n.a, 1); } }; -/// The fields a client's core has other than the session's, named -/// (tape.partsDiffer). -fn loadDiffers(a: *Authority, r: *Replica, arena: Allocator) ![]const u8 { - const want = try arena.create(tape.Parts); - const got = try arena.create(tape.Parts); - const w = tape.digestParts(a.core, want); - const g = parts: { - const was = Stepping.begin(&r.t, &r.epoch); - defer was.end(); - break :parts tape.digestParts(r.core(), got); +/// A client `client` of the session `a` joined now, as `Peers.join` joins +/// one, and let go: its load is the session as dumped, at the welcome's +/// digest; the fields that differ named (`loadDiffers`) when it is not. +/// A test's session ends with it to say its state crosses a join whole. +pub fn loadsAsSession(a: *Authority, client: u16) !void { + const gpa = std.testing.allocator; + const joined = (try a.join(client, null)) orelse return error.NoView; + defer gpa.free(joined.dump); + const r = try Replica.initDump(gpa, client, joined.view, joined.dump, .{ .cols = 80, .rows = 24 }); + defer r.deinit(); + if (!r.loaded(joined.digest)) { + var named: [400]u8 = undefined; + std.debug.print("the client's load is not the session: {s}\n", .{loadDiffers(r, joined.dump, &named)}); + return error.LoadedAnother; + } + _ = try a.leave(client, false); +} + +/// Every type the core's walk reaches that walks itself by hand +/// (walk.handWalked: a `digest` or a dump of its own, which can take what +/// the other does not), and the test that joins a client from a dump of a +/// session holding it and checks the load by the welcome's digest. The one +/// walk holds every other field: its digest, its dump and its rollback copy +/// are the same fields by one declaration (walk.zig `replica`). +const hand_walked = .{ + .{ pardes.filesystem.Namespace, "a client joined while 9P requests hold each kind of state the core keeps for them" }, + .{ pardes.ctlfs.Open.What, "a client joined while 9P requests hold each kind of state the core keeps for them" }, + .{ pardes.ctlfs.pane.State, "a client joined after a script set a pane's addr, its limit, or truncated its dot" }, + .{ Term.State, "bytes: a client joins from a dump with its terminals whole" }, + .{ pardes.Kind.Slot, "a client joined from a dump numbers a kind's next job as the session does" }, + .{ @import("locations.zig").Row, "a client joined from a dump makes a `/` query's rows as the session does" }, + .{ @import("jj.zig").Session, "a client replays Changes from what the session's jj answered and ends equal" }, + .{ pardes.panes.File.Layers, "a client replays Changes from what the session's jj answered and ends equal" }, +}; + +test "every type the core's walk reaches that hashes or dumps itself by hand has a test joining a client over it (hand_walked)" { + inline for (comptime walk.handWalked(&.{Pardes})) |T| { + const listed = comptime for (hand_walked) |entry| { + if (entry[0] == T) break true; + } else false; + if (!listed) { + std.debug.print("{s} walks itself by hand and no test joins a client over it: one, and its line in hand_walked\n", .{@typeName(T)}); + return error.NotListed; + } + } + // ...and every line is a type that does: one that walks itself by the + // one walk again leaves no line behind. + inline for (hand_walked) |entry| { + const reached = comptime for (walk.handWalked(&.{Pardes})) |T| { + if (entry[0] == T) break true; + } else false; + if (!reached) { + std.debug.print("{s} no longer walks itself by hand: its line in hand_walked goes\n", .{@typeName(entry[0])}); + return error.Stale; + } + } +} + +test "a client whose load is not the session says which field, in its words and its log" { + const gpa = std.testing.allocator; + var n = try NinepJoin.init("one\n", TestHost.host()); + defer n.deinit(); + const before = (try n.a.join(1, null)).?; + defer gpa.free(before.dump); + _ = try n.a.leave(1, false); + _ = try n.serve(.{ .tag = 1, .op = .open, .node = @backingInt(pardes.ctlfs.TopFile.screen) }, true); + const after = (try n.a.join(1, null)).?; + defer gpa.free(after.dump); + // The dump before the open, checked by the digest after it and named + // by its fields: what a dump made at one moment and a digest taken at + // another said. + const Tail = struct { + fn at(dump: []const u8) usize { + const parts = pardes.dump.joinParts(std.testing.allocator, dump).?; + defer std.testing.allocator.free(parts); + return dump.len - parts.len * 8 - 16; + } }; - return tape.partsDiffer(g, w, arena); + var spliced: std.ArrayListUnmanaged(u8) = .empty; + defer spliced.deinit(gpa); + try spliced.appendSlice(gpa, before.dump[0..Tail.at(before.dump)]); + try spliced.appendSlice(gpa, after.dump[Tail.at(after.dump)..]); + var j: Joined = .{ .fd = -1, .pid = 0, .client = 1, .view = before.view, .gmtoff = 0, .dump = spliced.items, .digest = after.digest, .semantics = wire.semantics }; + var why: [600]u8 = @splat(0); + try std.testing.expect(fromDump(gpa, &j, .{ .cols = 80, .rows = 24 }, null, &why, nowNs()) == null); + try std.testing.expectEqualStrings("the session's dump loaded as another session: fs differs", std.mem.sliceTo(&why, 0)); } /// Each kind of state 9P requests leave in the core between two requests |
