summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-20 01:21:25 -0300
committerGabriel Schneider <[email protected]>2026-11-20 02:08:31 -0300
commitc9ac46755632d5cd1bdc34fe211112d730ffc77f (patch)
tree1db2a819b04acec388faaa50590c8ba7b837a4ed /core
parentdaee60fa42540864a89357a28b73033158158425 (diff)
downloadpardes-c9ac46755632d5cd1bdc34fe211112d730ffc77f.tar.gz
pardes-c9ac46755632d5cd1bdc34fe211112d730ffc77f.zip
replica: a client measures how far behind its session it is and past two seconds lets its link go and joins again from a dump, saying so once (P32 (b); step 3, item 2): the fault shown first (a client whose units came 2.1 s later than its quickest took them on: Link.quit false) and in the soak (the far client never let go: one join, its final screen 17.7 s after the flood's end); Link.behind, each unit's head's session clock against the client's own (Link.time), less the smallest difference seen; one constant, behind_max_ms = 2,000, near and far; past it the link let go (the bye's `again`, its view kept), the notice "joined again: N.N s behind" (Link.rejoins), the first unit after a join past it taken as the link now (a session's clock stood still while its machine slept; a dump that took longer than the bound: never a let-go at once, never a loop); the clock is the client's machine's, read in Link alone (no new line on root.zig's machine_lines; Link is no part of Pardes, so no digest or dump reaches it); a test that fed a simulated client clock with units of the real clock made them on its own clock; the soak counts the let-gos and how far behind each client fell; tui core-test 859/1009 (150 skipped), shell-test 1045/1048, term-test 17/17; the soak, screen state (--clients 5, 4 seeds): diverged 0, unequal 0, deltas 7093/0 7518/0 7603/0, quiet 590/0 472/0 541/0, unlisted 0, corpus 22 of 22, the far client 9/9/7 let-gos, others 0; bytes (--seeds 8): the far client shows its final screen in every run, 6.5-7.9 s after the flood's end, refreshed every 7.0-7.3 s (p50), others 0 let-gos and 2-10 ms behind at most, quiet 1077/0 1083/0 1457/0, unequal 0, BUT 5/3/1 near clients diverged at joins (reports/pardes-term-lag.md: open); ReleaseFast (2 seeds, 80 ms): 0 diverged, a join's unit on a client already there 9.6 ms p50, 17.7 ms at worst, the session's join step 1.4/14.3 ms
Co-Authored-By: Claude Opus <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'core')
-rw-r--r--core/replica.zig102
1 files changed, 96 insertions, 6 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);