diff options
Diffstat (limited to 'src/replica.zig')
| -rw-r--r-- | src/replica.zig | 149 |
1 files changed, 148 insertions, 1 deletions
diff --git a/src/replica.zig b/src/replica.zig index e5d5ad16..3af1caac 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -218,6 +218,16 @@ pub const Authority = struct { return a.unit(0, a.seq); } + /// An input of its own frontend's, in the view that frontend shows + /// (the first: tui.zig `Shell.handle` goes into `s.own`), as its unit. + pub fn own(a: *Authority, ev: pardes.Event) ![]const u8 { + std.debug.assert(a.batched == 0); + const was = Stepping.begin(&a.t, null); + a.core.toView(0); + was.end(); + return a.local(ev); + } + /// A step of its own (a pty's output) into the open batch: under a /// flood a replica gets a unit a frame, not one a read (each unit of the /// authority's rolls back a replica's pending inputs), and its digest @@ -256,6 +266,51 @@ pub const Authority = struct { return .{ .view = k, .unit = try a.unit(0, a.seq) }; } + /// A client joining as `Peers.join` takes one: its view kept for it + /// (`back`, after a divergence) or a new one, null when every view is + /// taken (refused); the unit that says so to the others (valid until the + /// next call), and the dump it loads, its blobs in place (`gpa`'s), with + /// the digest it checks its load by. + pub fn join(a: *Authority, client: u16, back: ?u8) !?struct { view: u8, unit: []const u8, dump: []u8, digest: u64 } { + std.debug.assert(a.batched == 0); + const was = Stepping.begin(&a.t, null); + defer was.end(); + const again = if (back) |v| a.core.rejoinView(v) else false; + const k = (if (again) back else a.core.openView()) orelse return null; + a.core.undrawn |= @as(u8, 1) << @intCast(k); + a.view_of[client] = k; + a.seq += 1; + const u = try a.unit(0, a.seq); + var blobs: std.ArrayListUnmanaged(walk.Deferred) = .empty; + defer blobs.deinit(a.core.gpa); + const state = try pardes.dump.dumpJoin(a.core, &blobs); + defer a.core.gpa.free(state); + tape.resync(&a.t); + var whole: std.ArrayListUnmanaged(u8) = .empty; + errdefer whole.deinit(a.gpa); + var pos: usize = 0; + for (blobs.items) |b| { + try whole.appendSlice(a.gpa, state[pos..b.at]); + try whole.appendSlice(a.gpa, b.bytes); + pos = b.at; + } + try whole.appendSlice(a.gpa, state[pos..]); + return .{ .view = k, .unit = u, .dump = try whole.toOwnedSlice(a.gpa), .digest = tape.digestOf(a.core) }; + } + + /// Client `client` gone as `Peers.leave` lets one go: its view kept for + /// it (`again`: it joins again for it) or closed; the unit. + pub fn leave(a: *Authority, client: u16, again: bool) ![]const u8 { + std.debug.assert(a.batched == 0); + const was = Stepping.begin(&a.t, null); + const k = a.view_of[client]; + a.core.undrawn &= ~(@as(u8, 1) << @intCast(k)); + if (again) a.core.awayView(k) else a.core.closeView(k); + was.end(); + a.seq += 1; + return a.unit(0, a.seq); + } + /// Client `client`'s view gone (its link ended), as its unit. pub fn closeView(a: *Authority, client: u16) ![]const u8 { std.debug.assert(a.batched == 0); @@ -686,6 +741,10 @@ pub const Replica = struct { /// as the last delta since set it: the copy holds no emulator, and a /// speculation may move a view, so a rollback puts these back. views: [pardes.MAX_PANES]?usize = @splat(null), + /// Its panes' serials as the copy was taken, and whether one was being + /// torn down then (`panesMoved`). + serials: [pardes.MAX_PANES]u32 = @splat(0), + retiring: bool = false, screen_out: std.ArrayListUnmanaged(u8) = .empty, /// Its Shadows' rasters, from the session's strips (17.3). blobs: pardes.Kind.Blobs = .{}, @@ -787,7 +846,13 @@ pub const Replica = struct { for (&r.screens) |*slot| if (slot.*) |*sc| Term.replicaFree(sc.rep, pardes.panes.emulator_gpa); if (r.t.core) |c| { const was = Stepping.begin(&r.t, &r.epoch); - if (r.copy.held) _ = r.copy.restore(); + // The state it ends on is whole as it is: the copy let go, its + // put-off frees done. Put back, it would bring back what was + // freed between steps meanwhile (the shell's rows a draw read + // again after a delta), which `toConfirmed` drops and this + // freed again (a held copy across an input of its own, a + // rejoin's old replica). + if (r.copy.held) r.copy.release(); c.deinit(); was.end(); } @@ -887,6 +952,7 @@ pub const Replica = struct { r.dirty = true; return; } + const read_terminal = r.pending.items[0].terminal; r.freePending(r.pending.orderedRemove(0)); r.applied -= 1; if (r.applied == 0) { @@ -902,6 +968,15 @@ pub const Replica = struct { return r.fail(why); } } + // The copy stays the point a rollback goes back to, this + // input kept to replay over it as a unit of the session's + // that comes while idle is: a step writes some bytes of the + // megabyte the copy is, walked once in `rebase_after` units, + // not once a key. Not over one that read a terminal (replayed + // later it would read the deltas since), nor once a pane + // opened, closed or was torn down since the copy (what it + // holds outside the copy was made or freed: `ahead`'s rule). + if (!read_terminal and !r.panesMoved()) return r.keepSince(u.tape); r.copy.release(); rollback.Epoch.select(null); r.clearSince(); @@ -1013,6 +1088,9 @@ pub const Replica = struct { // was there before is of a grid the deltas since moved on // from (a rollback never brings a terminal's past back). r.core().shell_rows = .{}; + // A tab's width is the process's (config.tab_now), which a + // step that set it (Tab) wrote and the copy does not hold. + pardes.config.tab_now = r.core().settings.tab; } r.applied = 0; // Its views as the copy was taken, or as the last delta set them @@ -1150,10 +1228,25 @@ pub const Replica = struct { } fn keepViews(r: *Replica) void { + const c = r.core(); + for (c.panes, &r.serials) |slot, *n| n.* = if (slot) |pane| pane.serial else 0; + r.retiring = for (c.retired_panes.current, c.retired_panes.previous) |x, y| { + if (x != null or y != null) break true; + } else false; if (!r.t.screen_state) return; for (r.core().panes, &r.views) |slot, *v| v.* = if (slot) |pane| if (pane.terminal) |state| pardes.Term.grid(state).view else null else null; } + /// Whether a pane opened, closed or is torn down since the copy was + /// taken (`keepViews`). + fn panesMoved(r: *const Replica) bool { + const c = r.core(); + if (r.retiring) return true; + for (c.retired_panes.current, c.retired_panes.previous) |x, y| if (x != null or y != null) return true; + for (c.panes, r.serials) |slot, n| if ((if (slot) |pane| pane.serial else 0) != n) return true; + return false; + } + fn putViews(r: *Replica) void { if (!r.t.screen_state) return; for (r.core().panes, r.views) |slot, at| if (slot) |pane| if (pane.terminal) |state| pardes.Term.viewTo(state, at); @@ -3558,6 +3651,60 @@ test "another client's input first: a replica rolls back, replays it and applies try std.testing.expectEqual(@as(u32, 4), r2.stats.fast); } +test "the copy taken for an input of its own stays once the input is confirmed: the next takes none, and a rollback replays the first over it" { + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); + defer a.deinit(); + const first = try gpa.dupe(u8, try a.unit(0, 0)); + defer gpa.free(first); + const r = try Replica.init(gpa, 1, first, false); + defer r.deinit(); + var sent: std.ArrayListUnmanaged([]u8) = .empty; + defer { + for (sent.items) |m| gpa.free(m); + sent.deinit(gpa); + } + freshSecond(); + try key(r, 'j', &sent); + const copies = r.stats.copies; + r.confirm(try a.take(sent.items[0])); + try std.testing.expect(r.copy.held); + try std.testing.expectEqual(@as(usize, 1), r.since.items.len); + try key(r, 'j', &sent); + try std.testing.expectEqual(copies, r.stats.copies); + // The session's own key first: back to the copy, the first replayed. + r.confirm(try a.local(.{ .key = .{ .cp = 'l', .text = "l" } })); + r.settle(); + r.confirm(try a.take(sent.items[1])); + r.settle(); + try agrees(a, r); + try std.testing.expectEqual(@as(u32, 1), r.stats.foreign); +} + +test "a rollback over a Tab it applied puts the process's tab width back as its core has it" { + // config.tab_now is the process's, written by the step that set the + // width (the soak, five clients: a Tab undone left it, and the next + // replayed step measured the files' tabs otherwise). + const gpa = std.testing.allocator; + defer pardes.config.tab_now = pardes.config.tab_width; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); + defer a.deinit(); + const first = try gpa.dupe(u8, try a.unit(0, 0)); + defer gpa.free(first); + const r = try Replica.init(gpa, 1, first, false); + defer r.deinit(); + pardes.config.tab_now = r.core().settings.tab; + const was = r.core().settings.tab; + const msg = try gpa.dupe(u8, try r.input(.{ .command = "Tab 2" })); + defer gpa.free(msg); + try std.testing.expectEqual(@as(usize, 1), r.applied); + try std.testing.expectEqual(@as(u16, 2), pardes.config.tab_now); + // The session's own key first: back to the confirmed position. + r.confirm(try a.local(.{ .key = .{ .cp = 'l', .text = "l" } })); + try std.testing.expectEqual(was, r.core().settings.tab); + try std.testing.expectEqual(was, pardes.config.tab_now); +} + test "a delta applied under a speculation outlives its rollback: the terminal stays the authority's newest" { if (pardes.Term.ops == null) return error.SkipZigTest; // a shell const gpa = std.testing.allocator; |
