diff options
Diffstat (limited to 'core/replica.zig')
| -rw-r--r-- | core/replica.zig | 185 |
1 files changed, 168 insertions, 17 deletions
diff --git a/core/replica.zig b/core/replica.zig index dd3f13d1..10eba502 100644 --- a/core/replica.zig +++ b/core/replica.zig @@ -806,7 +806,7 @@ pub const Replica = struct { tape.adoptCore(&r.t, c); const was = Stepping.begin(&r.t, &r.epoch); defer was.end(); - r.view = Peers.viewFor(c, null) orelse return error.NoView; + r.view = Peers.viewFor(c, null, true) orelse return error.NoView; break :boot c; }; // The chrome's colours are local, never dumped: the restored theme's. @@ -2117,6 +2117,9 @@ pub const Peers = struct { /// No frontend of its own (the host says): with no client either, the /// session is unattended. detached: bool = false, + /// A Restore let every client go (`restoring`): the restored core's one + /// view (views.only) is the first to join again's. + restored: bool = false, /// Its last client left with a close (its window closed, its terminal /// hung up) and the session is a pair's not kept (daemon.sessionState): it /// ends there (runtime/session.zig). @@ -2246,7 +2249,14 @@ pub const Peers = struct { }, .screen => |in| { defer ps.gpa.free(in.msg); - const reply = (ps.screens.take(core, in.peer.sid, in.msg) catch null) orelse continue; + // An acknowledgement (or a reset): room again in its flight + // for what its terminals came to since. Its flush at this + // boundary came before it, and at the bound sent nothing: + // with no step after it, that client kept an old screen. + const reply = (ps.screens.take(core, in.peer.sid, in.msg) catch null) orelse { + ps.screensTo(core, in.peer, .flush); + continue; + }; defer ps.gpa.free(reply); const head = wire.header(@backingInt(wire.ServerTag.screen), reply.len); in.peer.send(&.{ &head, reply }); @@ -2346,10 +2356,26 @@ pub const Peers = struct { ps.recording = true; ps.samples = std.c.getenv("PARDES_REPLICA_STATS") != null; } - const k = viewFor(core, joining.view) orelse { + // A join with no room lets go a view kept past its bound by the + // core's clock (Core.openView), which a wake by mail alone, as this + // join's, does not move (tui.zig `clockNow`): moved now, on the tape. + if (@popCount(core.views_open) == pardes.views.max) if (core.host.call(.now, .{})) |now| core.advance(now); + // A pair's first client, or the first back after a Restore with no + // client and no view kept for one: the view the session has is the + // joiner's. Any other attach (a detached session's too) opens one. + const own = joining.own or (ps.restored and ps.count() == 0 and core.views_away == 0); + ps.restored = false; + const k = viewFor(core, joining.view, own) orelse { core.unattended = ps.count() == 0 and ps.detached; if (ps.count() == 0) ps.stop(core); - return daemon.refuseReplica(fd, "that session has as many views as it takes"); + const full = "that session has as many views as it takes"; + var left: u64 = 0; + for (0..pardes.views.max) |i| if (pardes.views.isAway(core, @intCast(i))) { + const n = (core.views[i].?.away_ns + pardes.views.away_kept_ns) -| core.clock.now_ns; + left = if (left == 0) n else @min(left, n); + }; + var why: [128]u8 = undefined; + return daemon.refuseReplica(fd, if (left == 0) full else std.fmt.bufPrint(&why, full ++ " (one is kept {d} s more for a client joining again)", .{(left + std.time.ns_per_s - 1) / std.time.ns_per_s}) catch full); }; ps.seq += 1; ps.unit(0, ps.seq); @@ -2420,18 +2446,22 @@ pub const Peers = struct { /// boot shell's spawn among them) is performed after it, on the tape /// its client is sent, not by the view's settle, as a client that made /// its core itself performs none of them. + /// `own`: the live view is the joiner's, in a session nobody looks at + /// (a pair's first client, Options.pair; the first back after a + /// Restore): not one beside it nobody looks at, which would size its + /// terminals (Pardes.sizes). /// ponytail: a divergence's redial that came before its old link's end /// (posted by that link's reader, microseconds after the bye; the /// redial is a dial and the daemon's hand-over later) finds its view /// not kept yet, and takes a new one. - fn viewFor(core: *Pardes, back: ?u8) ?u8 { + fn viewFor(core: *Pardes, back: ?u8, own: bool) ?u8 { const held = core.gpa.alloc(pardes.Effect, core.effects_len) catch return null; defer core.gpa.free(held); for (held, 0..) |*e, i| e.* = core.effects[(core.effects_head + i) % core.effects.len]; core.effects_len = 0; defer queueFirst(core, held); core.unattended = false; - const k = (if (back) |v| (if (core.rejoinView(.at(v))) v else null) else null) orelse (core.openView() orelse return null).index(); + const k = if (own) core.live else (if (back) |v| (if (core.rejoinView(.at(v))) v else null) else null) orelse (core.openView() orelse return null).index(); core.undrawn |= @as(u8, 1) << @intCast(k); return k; } @@ -2536,6 +2566,36 @@ pub const Peers = struct { ps.drop(core, true); } + /// Every client told the session is restoring a dump and let go: each + /// joins again at once (host_io.restoreCore), as at an Upgrade, not + /// asking the daemon whether a link cut with no word was its + /// session's end (`cutListed`). + pub fn restoring(ps: *Peers, core: *Pardes) void { + var out: [600]u8 = undefined; + const words = wire.encodeServer(&out, .{ .refused = restore_words }) catch unreachable; // the words fit + for (ps.list) |slot| if (slot) |peer| peer.last(words); + ps.flushed200(); + // One dialling again already (handed over meanwhile) joins the + // restored core at the next boundary, not hung up on: its redial + // would wait 100 ms for the next dial. + var again: std.ArrayListUnmanaged(Joining) = .empty; + defer again.deinit(ps.gpa); + ps.lock.lockUncancelable(ps.io); + var i: usize = 0; + while (i < ps.inbox.items.len) { + if (ps.inbox.items[i] != .join) { + i += 1; + continue; + } + again.append(ps.gpa, ps.inbox.items[i].join) catch break; + _ = ps.inbox.orderedRemove(i); + } + ps.lock.unlock(ps.io); + ps.drop(core, false); + ps.restored = true; + for (again.items) |j| ps.post(.{ .join = j }); + } + /// What each client was sent goes first, for up to 200 ms. fn flushed200(ps: *Peers) void { const until = nowNs() + 200 * std.time.ns_per_ms; @@ -3112,6 +3172,10 @@ pub fn ended(why: []const u8) bool { /// What an Upgrade tells its clients (`Peers.refuseAll`): each dials again. pub const upgrading = "the session is upgrading to another build of pardes"; +/// What a Restore tells its clients (`Peers.restoring`): each joins again, +/// saying nothing (the own frontend's Restore shows nothing either). +pub const restore_words = "the session is restoring a dump"; + /// A link cut with no last word (`why` its hang-up) whose session its /// daemon lists still: an Upgrade's exec cut it, a dump on the way past the /// clients' flush (`Peers.refuseAll`), whichever build the session is, not @@ -3639,7 +3703,7 @@ pub const Link = struct { return true; } if (c.refused) |words| { - c.notify("joined again: {s}", .{words}); + if (!std.mem.eql(u8, words, restore_words)) c.notify("joined again: {s}", .{words}); std.heap.c_allocator.free(words); c.refused = null; return true; @@ -4772,7 +4836,7 @@ test "a session's first client on the socket it starts with (Options.pair), join try firstOnPair(false); } -test "bytes: a session's first client on its pair, taken before its first effects, has the boot shell's first bytes with no reload of them (the session's one at the join is of its emulator before any byte, the client's none); a client after them makes every side reload" { +test "bytes: a session's first client on its pair, taken before its first effects, has the boot shell's first bytes with no reload at all (its view is the session's, none opened for it); a client after them makes every side reload" { try firstOnPair(true); } @@ -4784,6 +4848,79 @@ test "a pair's client's own core, the file edited between the session's read and try ownBootAgainst(true); } +test "a Restore tells its clients so before it lets them go (each joins again, saying nothing), and one handed over meanwhile is joined to the restored core at the next boundary, not hung up on" { + const gpa = std.testing.allocator; + var fds: [2]c_int = undefined; + if (libc.socketpair(libc.AF.UNIX, libc.SOCK.STREAM | libc.SOCK.CLOEXEC, 0, &fds) != 0) return error.NoSocket; + defer _ = libc.close(fds[1]); + var late: [2]c_int = undefined; + if (libc.socketpair(libc.AF.UNIX, libc.SOCK.STREAM | libc.SOCK.CLOEXEC, 0, &late) != 0) return error.NoSocket; + defer _ = libc.close(late[1]); + const core = try Pardes.init(gpa, .{ .cols = 80, .rows = 24 }); + defer core.deinit(); + core.host = TestHost.host(); + var ps: Peers = .{ .gpa = gpa, .io = std.testing.io, .name = "restore", .detached = true }; + defer ps.deinit(); + defer live_peers = null; + defer ps.dropAll(core); + ps.join(core, .{ .fd = fds[0] }); + var why: [600]u8 = @splat(0); + var j = joinOn(gpa, fds[1], null, &why, null, null, null) orelse return error.JoinFailed; + gpa.free(j.dump); + defer j.rest.deinit(gpa); + // A client dialling again, handed over before the Restore lets the first go. + ps.handed(late[0], null); + ps.restoring(core); + const Last = struct { + words: ?[]u8 = null, + fn post(l: *@This(), msg: Said) bool { + switch (msg) { + .refused => |w| l.words = w, + .unit, .screen, .blob => |b| std.heap.c_allocator.free(b), + .checked, .ended, .left => {}, + } + return true; + } + }; + var last: Last = .{}; + readLink(gpa, fds[1], &j.rest, &last, Last.post); + const words = last.words orelse return error.NoLastWord; + defer std.heap.c_allocator.free(words); + try std.testing.expectEqualStrings(restore_words, words); + try std.testing.expectEqual(1, ps.inbox.items.len); +} + +test "a detached session's first attach opens a view of its own beside the session's; the first back after a Restore takes the session's" { + const gpa = std.testing.allocator; + const core = try Pardes.init(gpa, .{ .cols = 80, .rows = 24 }); + defer core.deinit(); + core.host = TestHost.host(); + var ps: Peers = .{ .gpa = gpa, .io = std.testing.io, .name = "attach", .detached = true }; + defer ps.deinit(); + defer live_peers = null; + defer ps.dropAll(core); + const session_view = core.live; + var fds: [2]c_int = undefined; + if (libc.socketpair(libc.AF.UNIX, libc.SOCK.STREAM | libc.SOCK.CLOEXEC, 0, &fds) != 0) return error.NoSocket; + defer _ = libc.close(fds[1]); + ps.join(core, .{ .fd = fds[0] }); + const first = for (ps.list) |slot| { + if (slot) |peer| break peer; + } else return error.NotJoined; + try std.testing.expect(first.view.index() != session_view); + try std.testing.expect(pardes.views.isOpen(core, session_view)); + ps.restoring(core); + var again: [2]c_int = undefined; + if (libc.socketpair(libc.AF.UNIX, libc.SOCK.STREAM | libc.SOCK.CLOEXEC, 0, &again) != 0) return error.NoSocket; + defer _ = libc.close(again[1]); + ps.join(core, .{ .fd = again[0] }); + const back = for (ps.list) |slot| { + if (slot) |peer| break peer; + } else return error.NotJoined; + try std.testing.expectEqual(core.live, back.view.index()); + try std.testing.expectEqual(1, pardes.views.count(core)); +} + /// A session's first client on its pair making its core itself /// (`ownBoot`), the file it opens `edited` after the session read it. fn ownBootAgainst(edited: bool) !void { @@ -4805,7 +4942,7 @@ fn ownBootAgainst(edited: bool) !void { defer ps.deinit(); defer live_peers = null; defer ps.dropAll(core); - ps.join(core, .{ .fd = fds[0] }); + ps.join(core, .{ .fd = fds[0], .own = true }); // Edited after the session read it, before its client does. if (edited) try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "a.txt", .data = "edited between\n" }); var o = opts; @@ -4824,6 +4961,10 @@ fn ownBootAgainst(edited: bool) !void { } try std.testing.expectEqual(!edited, r == booted); try std.testing.expectEqual(tape.digestOf(core), r.digest()); + // The session's one view is its client's: none beside it, nobody's, + // sizes its terminals. + try std.testing.expectEqual(@as(u8, 1), core.views_open); + try std.testing.expectEqual(@as(u8, 0), r.view); // What the client shows is the session's file as the session read it. try std.testing.expectEqualStrings("the session's start\n", r.core().panes[0].?.file.?.content); } @@ -4855,7 +4996,7 @@ fn firstOnPair(bytes: bool) !void { defer live_peers = null; defer ps.dropAll(core); const reloads = pardes.Term.reloads.n; - ps.join(core, .{ .fd = fds[0] }); + ps.join(core, .{ .fd = fds[0], .own = true }); var j: Joined = undefined; var why: [600]u8 = @splat(0); const r = if (bytes) r: { @@ -4926,10 +5067,10 @@ fn firstOnPair(bytes: bool) !void { if (r.diverged) |w| std.debug.print("diverged: {s}\n", .{w}); try std.testing.expect(r.diverged == null); try std.testing.expectEqual(take.digest.?, r.digest()); - // Bytes: the session's reload at the join, of an emulator that held - // nothing yet (Pardes.init made it, the spawn is a first effect); the - // client loaded it from the dump and replayed every byte since. - try std.testing.expectEqual(reloads + @intFromBool(bytes), pardes.Term.reloads.n); + // No reload at the join: the session's view is its first client's, none + // opened for it (Peers.viewFor); the client loaded the terminal from the + // dump and replayed every byte since. + try std.testing.expectEqual(reloads, pardes.Term.reloads.n); var arena: std.heap.ArenaAllocator = .init(gpa); defer arena.deinit(); const rows = try pardes.Term.viewport(r.core().panes[shell].?.terminal.?, arena.allocator(), false); @@ -4940,7 +5081,7 @@ fn firstOnPair(bytes: bool) !void { if (libc.socketpair(libc.AF.UNIX, libc.SOCK.STREAM | libc.SOCK.CLOEXEC, 0, &late) != 0) return error.NoSocket; defer _ = libc.close(late[1]); ps.join(core, .{ .fd = late[0] }); - try std.testing.expectEqual(reloads + 2, pardes.Term.reloads.n); + try std.testing.expectEqual(reloads + 1, pardes.Term.reloads.n); } test "bytes: a join refused for want of a view reloads nothing on any side: a client with a key on top stays the session's" { @@ -6561,7 +6702,7 @@ test "a client's n in a kind's pane is a motion: it steps to the next place a re /// `addon`: a kind whose state is its own on the session (a PDF's), which /// the walk does not stand in (its Still has no search, place or word): /// the soak's (test/replicas.zig) PDF and pictures are, judged by name. -const On = enum { any, file, kind, shell, shell_normal, addon }; +const On = enum { any, file, kind, shell, shell_normal, addon, dir }; /// What a far client may wait a round trip for (the user's line, P14: a /// side effect on the system may be slow; motions, edits and layout are @@ -6582,6 +6723,12 @@ const may_wait = [_]Wait{ .{ .what = "LookPrev", .on = .any, .stop = "resolve", .why = "a Look's walk: each word asked of the disk" }, .{ .what = "Mini *", .on = .any, .stop = "resolve", .why = "a file opened by name, from the session's disk" }, .{ .what = "Get", .on = .any, .stop = "read_file", .why = "the file read again from the session's disk" }, + .{ .what = "Get", .on = .dir, .stop = "dir_listing", .why = "a directory listed again from the session's disk" }, + // A directory's pane (Dir.zig): its entries are names on the session's disk. + .{ .what = "Enter", .on = .dir, .stop = "resolve", .why = "the entry under the cursor opened, from the session's disk" }, + .{ .what = "* Enter", .on = .dir, .stop = "resolve", .why = "the entry opened (Enter after a prefix still opens it)" }, + .{ .what = "Tab", .on = .dir, .stop = "kind", .why = "what the entry under the cursor is, asked of the session's disk" }, + .{ .what = "* Tab", .on = .dir, .stop = "kind", .why = "what the entry is (Tab after a prefix still asks)" }, .{ .what = "Save", .on = .any, .stop = "write_file", .why = "a save" }, .{ .what = "Changes", .on = .any, .stop = "jj", .why = "what jj says of the pinned changes, the session's (P14: a word typed may take a moment once)" }, // A program run, a shell opened or closed: the session's processes. @@ -6858,12 +7005,16 @@ test "a far client's every word and key, on a file's pane, a kind's and a shell' _ = try a.local(.{ .command = "Still" }); const kind = a.core.view().active; try std.testing.expect(file != kind and a.core.panes[file].?.file != null and a.core.panes[kind].?.addon != null); + // A directory's pane (Dir.zig), listed from the disk. + _ = try a.local(.{ .command = try std.fmt.bufPrint(&line, "Look {s}/", .{dir}) }); + const listing = a.core.view().active; + try std.testing.expect(a.core.panes[listing].?.file.?.listing != null); var seen: [may_wait.len]bool = @splat(false); var hit: [may_wait.len]bool = @splat(false); var unlisted: u32 = 0; var tried: u32 = 0; - const stands = [_]struct { On, ?usize }{ .{ .file, file }, .{ .kind, kind }, .{ .shell, shell }, .{ .shell_normal, shell } }; + const stands = [_]struct { On, ?usize }{ .{ .file, file }, .{ .kind, kind }, .{ .shell, shell }, .{ .shell_normal, shell }, .{ .dir, listing } }; for (stands) |stand| { const on, const at = stand; const id = at orelse continue; |
