diff options
| -rw-r--r-- | core/replica.zig | 102 | ||||
| -rw-r--r-- | runtime/client.zig | 7 | ||||
| -rw-r--r-- | test/replicas.zig | 27 |
3 files changed, 121 insertions, 15 deletions
diff --git a/core/replica.zig b/core/replica.zig index b6ac6ef9..8fd0a251 100644 --- a/core/replica.zig +++ b/core/replica.zig @@ -3050,6 +3050,10 @@ fn redial(gpa: Allocator, path: []const u8, session: []const u8, why: *[600]u8, } } +/// How far behind its session a client may fall before it lets its link go +/// and joins again from a dump (P32 (b)): near and far alike. +pub const behind_max_ms = 2_000; + /// A replica client's link to its session, the terminal's (tui.zig /// `runClient`) and the window's (gui.zig `runClient`): its replica, its socket, /// what the session said, and what it checks. Drawing is the frontend's. @@ -3110,6 +3114,17 @@ pub const Link = struct { /// moved on (its own motion), for PARDES_REPLICA_STATS. draws: u32 = 0, again: u32 = 0, + /// How far behind the session it is, ns: each unit's head carries the + /// session's monotonic clock as it was made; against this client's own, + /// less the smallest difference seen (the clocks' offset and the link's + /// quickest crossing). This machine's clock, the link's alone: no step + /// reads it and no digest holds it (`lags`). + behind: u64 = 0, + least: ?i64 = null, + /// No unit taken since it joined (`lags`). + fresh: bool = true, + /// How far behind it was as it let its link go (`rejoins` says so). + lagged: u64 = 0, fn time(c: *const Link) u64 { return c.now orelse nowNs(); @@ -3141,6 +3156,8 @@ pub const Link = struct { c.asked = false; c.mispredicted = 0; c.put_off = false; + c.behind = 0; + c.fresh = true; if (r.core().views[r.view]) |v| { c.yanks = v.yanks; c.pastes = v.pastes; @@ -3177,6 +3194,7 @@ pub const Link = struct { switch (msg) { .unit => |bytes| { defer std.heap.c_allocator.free(bytes); + if (c.quit or c.lags(bytes)) return; c.r.confirm(bytes); if (c.test_diverge != 0 and c.r.stats.units >= c.test_diverge and c.r.quiet()) { c.test_diverge = 0; @@ -3228,6 +3246,25 @@ pub const Link = struct { }; } + /// The session's unit `bytes`, taken now: how far behind it is. Past + /// `behind_max_ms` it lets its link go (true), to join again from a dump. + /// The first unit after a join past it is taken as the link now (a + /// session's clock stood still while its machine slept; a dump that took + /// longer than the bound to come), never a let-go again at once. + fn lags(c: *Link, bytes: []const u8) bool { + const u = parseUnit(bytes) orelse return false; + const d = @as(i64, @intCast(c.time())) - u.mono_ns; + const max = behind_max_ms * std.time.ns_per_ms; + if (c.fresh and d - (c.least orelse d) > max) c.least = d; + c.fresh = false; + c.least = @min(c.least orelse d, d); + c.behind = @intCast(d - c.least.?); + if (c.behind <= max) return false; + c.lagged = c.behind; + c.quit = true; + return true; + } + /// A write to the session failed: it hung up (it cut this client off, /// lagging), as its reader will say. fn cut(c: *Link) void { @@ -3301,12 +3338,18 @@ pub const Link = struct { } /// The link ended: whether the client joins again, and why in its - /// notice (it diverged, or the session let it go to upgrade). + /// notice (it diverged, it fell too far behind, or the session let it + /// go: to upgrade, or cut off). pub fn rejoins(c: *Link) bool { if (c.r.diverged) |reason| { c.notify("joined again: this client diverged from the session ({s})", .{reason}); return true; } + if (c.lagged != 0) { + c.notify("joined again: {d}.{d} s behind", .{ c.lagged / std.time.ns_per_s, c.lagged / (std.time.ns_per_s / 10) % 10 }); + c.lagged = 0; + return true; + } if (c.refused) |words| { c.notify("joined again: {s}", .{words}); std.heap.c_allocator.free(words); @@ -5044,6 +5087,50 @@ test "a client cut off for what it left unread is told why and joins again for i try std.testing.expectEqual(tape.digestOf(core), back.r.digest()); } +test "a client whose session's units come more than two seconds later than its quickest lets its link go, says so once, and joins again; its first unit after a join learns the clocks again" { + 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 r = try Replica.init(gpa, 1, try a.unit(0, 0), false); + 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 for (fds) |fd| { + _ = libc.close(fd); + }; + var link: Link = .{ .r = r, .fd = fds[1] }; + defer link.deinit(gpa); + const Late = struct { + // A unit the session made at `made` ms on its clock, taken at `at` + // ms on the client's. + fn take(l: *Link, from: *Authority, made: i64, at: u64) !void { + const u = try std.heap.c_allocator.dupe(u8, try from.local(.tick)); + std.mem.writeInt(i64, u[14..22], made * std.time.ns_per_ms, .little); + l.now = at * std.time.ns_per_ms; + l.take(.{ .unit = u }); + } + }; + // The client's clock 5 s on from the session's, the link 100 ms: the + // second unit 1.9 s later than the first, the third 2.1 s. + try Late.take(&link, a, 0, 5_100); + try Late.take(&link, a, 1_000, 8_000); + try std.testing.expect(!link.quit); + try Late.take(&link, a, 1_100, 8_300); + try std.testing.expect(link.quit); + try std.testing.expect(link.rejoins()); + try std.testing.expectEqualStrings("joined again: 2.1 s behind", link.notice()); + try std.testing.expect(!link.rejoins()); + r.deinit(); + // Joined again, its first unit 3 s later than any before (the session's + // clock stood still, its machine asleep; or the dump took that long): + // taken as the link now, not let go of again. + const back = try joinedNow(a, 1, 0); + defer back.deinit(); + link.reset(back, &.{ .fd = fds[1], .pid = 0, .client = 1, .view = 0, .gmtoff = 0, .dump = &.{} }); + try Late.take(&link, a, 2_000, 10_100); + try Late.take(&link, a, 2_100, 10_250); + try std.testing.expect(!link.quit and back.diverged == null); +} + test "a client's writer, the client cut off while it waits on the socket, writes what it has in hand, then the words why, and ends the link" { const gpa = std.testing.allocator; var ps: Peers = .{ .gpa = gpa, .io = std.testing.io }; @@ -6311,25 +6398,28 @@ test "an idle client's check answered covers the units that came while it was as _ = libc.close(fd); }; var c: Link = .{ .r = r, .fd = fds[0], .now = std.time.ns_per_s }; + // Each unit made as the client takes it, on its clock (Link.behind). const unit = struct { - fn of(au: *Authority) !Said { - return .{ .unit = try std.heap.c_allocator.dupe(u8, try au.local(.tick)) }; + fn of(au: *Authority, l: *const Link) !Said { + const u = try std.heap.c_allocator.dupe(u8, try au.local(.tick)); + std.mem.writeInt(i64, u[14..22], @intCast(l.now.?), .little); + return .{ .unit = u }; } }.of; // Something changed, 300 ms quiet since its last input: asked. - c.take(try unit(a)); + c.take(try unit(a, &c)); try std.testing.expectEqual(@as(?u32, null), c.quietCheck()); try std.testing.expect(c.asked); // The session's pump for it (a unit), then the answer: agreed, and // nothing to ask after it however long it stays quiet. - c.take(try unit(a)); + c.take(try unit(a, &c)); c.take(.{ .checked = digestOf(a) }); try std.testing.expect(!c.asked and c.r.diverged == null); c.now.? += 10 * std.time.ns_per_s; try std.testing.expectEqual(@as(?u32, null), c.quietCheck()); try std.testing.expect(!c.asked); // A unit after it is a change again. - c.take(try unit(a)); + c.take(try unit(a, &c)); c.now.? += std.time.ns_per_s; _ = c.quietCheck(); try std.testing.expect(c.asked); diff --git a/runtime/client.zig b/runtime/client.zig index 544be588..a2d89ab9 100644 --- a/runtime/client.zig +++ b/runtime/client.zig @@ -137,8 +137,9 @@ pub fn Loop(comptime Frontend: type) type { l.link.take(msg); } - /// A join a pass: the first, and one after the replica diverged or - /// the session let it go to upgrade; the frontend stays as it is. + /// A join a pass: the first, and one after the replica diverged, fell + /// too far behind (replica.behind_max_ms) or the session let it go + /// (to upgrade, or cut off); the frontend stays as it is. pub fn run(l: *L, s: *Start) !Ended { const link = &l.link; while (true) { @@ -151,7 +152,7 @@ pub fn Loop(comptime Frontend: type) type { l.after(); } if (s.env.get("PARDES_REPLICA_STATS")) |file| link.writeStats(file); - if (!link.gone) replica.bye(s.joined.fd, s.r.diverged != null); + if (!link.gone) replica.bye(s.joined.fd, s.r.diverged != null or link.lagged != 0); // The link's reader ends with the link, posting as it goes: // what it posted is let go until it has (a terminal's queue // is bounded: a reader blocked on it would never end). diff --git a/test/replicas.zig b/test/replicas.zig index 637af0b3..5cddf6c8 100644 --- a/test/replicas.zig +++ b/test/replicas.zig @@ -284,6 +284,12 @@ const Totals = struct { far_final: std.ArrayListUnmanaged(u64) = .empty, queue_max: u64 = 0, cut: u64 = 0, + /// How far behind the session each fell at most (Link.behind, ns), and + /// how often it let its link go for it: the far client, and the others. + far_behind: u64 = 0, + far_lags: u64 = 0, + near_behind: u64 = 0, + near_lags: u64 = 0, /// What a join costs the replicas already there, ns of this process's /// clock: the session's join step (its reloads and its dump), and each /// client's unit that reloaded its terminals. @@ -760,9 +766,12 @@ fn ended(cl: *Client, c: usize, net: *Net, now: u64, totals: *Totals, run_: Run, const r = cl.r.?; const link = &cl.loop.link; // Its link ended by the session (a Detach), it says nothing more. - if (!link.gone) replica.bye(link.fd, r.diverged != null); + if (!link.gone) replica.bye(link.fd, r.diverged != null or link.lagged != 0); try drain(cl, c, net, now); cl.live = 0; + if (link.lagged != 0) { + if (net.throttled == @as(?usize, c)) totals.far_lags += 1 else totals.near_lags += 1; + } if (r.diverged) |why| { if (link.put_off) totals.rejoined += 1 else { totals.divergences += 1; @@ -1515,6 +1524,8 @@ const Soak = struct { const t0 = monoNs(); cl.loop.take(.{ .unit = try std.heap.c_allocator.dupe(u8, m.bytes) }); if (pardes.Term.reloads.n != reloads) try s.totals.join_client_ns.append(gpa, monoNs() - t0); + const behind = if (net.throttled == @as(?usize, c)) &s.totals.far_behind else &s.totals.near_behind; + behind.* = @max(behind.*, cl.loop.link.behind); }, .word => cl.loop.take(.{ .refused = try std.heap.c_allocator.dupe(u8, m.bytes) }), .blob => cl.loop.take(.{ .blob = try std.heap.c_allocator.dupe(u8, m.bytes) }), @@ -3082,16 +3093,20 @@ pub fn main(init: std.process.Init) !void { for (t.stops) |st| if (st.n > 0) try stop_lines.print(gpa, "{s}:{d} ", .{ st.name, st.n }); try stop_lines.append(gpa, '\n'); try join_lines.print(gpa, "{d:>4} {d:>5} {d:>7} {d:>8} {d:>8} {d:>6} {d:>5}/{d:<5} {d:>5}/{d:<6} {d:>7} {d:>5}/{d}/{d}\n", .{ rtt, t.joins, t.refused, t.rejoined, t.detached, t.detach_words, t.agree, t.differ, t.checks, t.units, t.resizes, t.kind_panes[0], t.kind_panes[1], t.kind_panes[2] }); - try far_lines.print(gpa, "{d:>4} {d:>5} {d:>6.1}/{d:<6.1} {d:>6.1}/{d:<6.1} {d:>8} {d:>3} {d:>6.1}/{d:<6.1} {d:>6.1}/{d:<6.1} ({d})\n", .{ - rtt, t.far_joins, + try far_lines.print(gpa, "{d:>4} {d:>5} {d:>4} {d:>6.1}/{d:<6.1} {d:>6.1}/{d:<6.1} {d:>8} {d:>3} {d:>6.1}/{d:<6.1} {d:>6.1}/{d:<6.1} ({d}) {d:>6.0}/{d:<6.0} {d}\n", .{ + rtt, t.far_joins, t.far_lags, secs(pct(t.far_gaps.items, 50)), secs(pct(t.far_gaps.items, 100)), secs(pct(t.far_final.items, 50)), secs(pct(t.far_final.items, 100)), t.queue_max >> 10, t.cut, msOf(pct(t.join_session_ns.items, 50)), msOf(pct(t.join_session_ns.items, 100)), msOf(pct(t.join_client_ns.items, 50)), msOf(pct(t.join_client_ns.items, 100)), t.join_client_ns.items.len, + msOf(t.far_behind), msOf(t.near_behind), + t.near_lags, }); - if (t.far_final.items.len < seeds and clients >= 5) failed = true; + // The far client shows the final screen in every run, and the near + // ones never fall behind with it. + if ((t.far_final.items.len < seeds and clients >= 3) or t.near_lags > 0) failed = true; if (t.divergences > 0 or t.unequal > 0 or t.screens_wrong > 0 or t.differ > 0 or t.unlisted > 0) failed = true; } std.debug.print("terminals: deltas a replica applied a second under the flood; a peer's bytes a second under the flood (deltas, units); encode a frame of the flood for every peer, apply a delta (us); rows fetched, KB of rows sent before the step that read them; resets; deltas checked/wrong\n", .{}); @@ -3100,8 +3115,8 @@ pub fn main(init: std.process.Init) !void { std.debug.print(" rtt joins refused rejoined detached detach agree/differ checks/units resizes kinds\n{s}", .{join_lines.items}); std.debug.print("stops: the wakes the clients' loops asked, the frames they drew, the yanks, pastes and links their frontends did (y/p/l), and what stopped their inputs (a stop counted again as its input is applied again), by name; those at a name replica.may_wait does not list fail the run\n", .{}); std.debug.print(" rtt unlisted wakes draws y/p/l stops\n{s}", .{stop_lines.items}); - std.debug.print("far: the third client's link 1 Mbit/s and 200 ms each way (zstd's bytes): its joins from a dump, s between two from the flood's start (p50/max), s from the flood's end to its final screen (p50/max: a run that never shows it fails), the session's queue for it at most, clients cut off; a join's cost to the replicas there before it, ms on this process's clock (p50/max): the session's step (reloads and dump), a client's unit (reloads), how many\n", .{}); - std.debug.print(" rtt joins refresh s final s queue KB cut session ms client ms\n{s}", .{far_lines.items}); + std.debug.print("far: the third client's link 1 Mbit/s and 200 ms each way (zstd's bytes): its joins from a dump, of them for its lag (Link.behind past replica.behind_max_ms), s between two from the flood's start (p50/max), s from the flood's end to its final screen (p50/max: a run that never shows it fails), the session's queue for it at most, clients cut off; a join's cost to the replicas there before it, ms on this process's clock (p50/max): the session's step (reloads and dump), a client's unit (reloads), how many; ms behind the session at most (Link.behind), the far client and the others, and the others' joins for it (0, or the run fails)\n", .{}); + std.debug.print(" rtt joins lags refresh s final s queue KB cut session ms client ms behind far/near ms\n{s}", .{far_lines.items}); if (failed) std.process.exit(1); } |
