summaryrefslogtreecommitdiff
path: root/src/replica.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/replica.zig')
-rw-r--r--src/replica.zig161
1 files changed, 92 insertions, 69 deletions
diff --git a/src/replica.zig b/src/replica.zig
index ba5ca55f..e567587c 100644
--- a/src/replica.zig
+++ b/src/replica.zig
@@ -2262,6 +2262,8 @@ pub const Joined = struct {
from_memory: u32 = 0,
from_disk: u32 = 0,
sent_bytes: u64 = 0,
+ /// How long its dial and load took, ms (`join`).
+ ms: u64 = 0,
};
/// A blob a far join was sent: where it is in the dump, and its hash.
@@ -2765,6 +2767,90 @@ pub fn readLink(gpa: Allocator, fd: c_int, rest: *Framer, ctx: anytype, comptime
rest.run(gpa, fd, ctx, Take.take);
}
+/// The words a client goes with when its session runs another build now.
+const upgrade_words = "session upgraded to a newer pardes: upgrade this client too";
+
+/// A replica client's join, the terminal's and the window's alike (their
+/// `remote`): the session at `path` dialled, its build checked to be this
+/// one's, and its dump loaded into a replica checked against the session
+/// as it was dumped. Or, `old` given, joined again once old's link has
+/// ended, for old's view (`redial`): the core it leaves held meanwhile so
+/// that its blobs are not sent again (`Have.leaving`), and let go before
+/// the new one loads. The dump is let go, a far join's to the thread that
+/// keeps the blobs it was sent (`Have.store`), and how long it all took is
+/// `j.ms`. Null, and why in `why`.
+pub fn join(gpa: Allocator, j: *Joined, path: []const u8, session: []const u8, o: pardes.Options, have: ?*Have, why: *[600]u8, old: ?*Replica) ?*Replica {
+ if (old) |prev| if (have) |h| h.leaving(prev.core());
+ const t0 = nowNs();
+ if (old) |prev| {
+ j.* = redial(gpa, path, session, why, have, prev.diverged == null, j.view) orelse {
+ var words: [600]u8 = undefined;
+ const said_ = std.mem.sliceTo(why, 0);
+ @memcpy(words[0..said_.len], said_);
+ _ = std.fmt.bufPrint(why, "could not join again: {s}\x00", .{words[0..said_.len]}) catch {};
+ return null;
+ };
+ if (have) |h| h.left();
+ prev.deinit();
+ } else j.* = sameBuild(gpa, dial(gpa, path, session, why, have, null) orelse return null, why) orelse return null;
+ defer {
+ if (have) |h| h.store(gpa, j) else gpa.free(j.dump);
+ j.dump = &.{};
+ }
+ const r = Replica.initDump(gpa, j.client, j.view, j.dump, o) catch |err| {
+ _ = std.fmt.bufPrint(why, "the session's dump would not load: {t}\x00", .{err}) catch {};
+ return null;
+ };
+ // PARDES_NO_ECHO: no predicted echo (the gates' baseline).
+ if (std.c.getenv("PARDES_NO_ECHO") != null) r.echo.on = false;
+ // PARDES_REPLICA_STATS: its timing samples kept, for its line.
+ r.stats.samples = std.c.getenv("PARDES_REPLICA_STATS") != null;
+ if (!r.loaded(j.digest)) {
+ r.deinit();
+ _ = std.fmt.bufPrint(why, "the session's dump loaded as another session (a build's dump is not complete)\x00", .{}) catch {};
+ return null;
+ }
+ r.gmtoff = j.gmtoff;
+ j.ms = (nowNs() - t0) / std.time.ns_per_ms;
+ return r;
+}
+
+/// `j`, when its session runs this build; else its link let go, and why in
+/// `why`: the daemon judged the hello by what the session last said, which
+/// an upgrade under way may not have said yet.
+fn sameBuild(gpa: Allocator, j: Joined, why: []u8) ?Joined {
+ if (j.semantics == wire.semantics) return j;
+ bye(j.fd, false);
+ _ = libc.close(j.fd);
+ var jj = j;
+ jj.rest.deinit(gpa);
+ gpa.free(j.dump);
+ _ = std.fmt.bufPrint(why, "{s}\x00", .{upgrade_words}) catch {};
+ return null;
+}
+
+/// The session again, after a divergence or an upgrade: an upgrade's
+/// takes the time its exec and load take, and its daemon's, so it is
+/// dialled every 100 ms for 20 s. Null, and why in `why`.
+/// `patient`: after an Upgrade's `refused` alone, while the session is
+/// away a moment; after a divergence a session that will not answer is
+/// gone, and the client says so at once (it froze 20 s, 2026-10-30).
+/// `view`: the one it had, which the session kept for it (views.away).
+fn redial(gpa: Allocator, path: []const u8, session: []const u8, why: *[600]u8, have: ?*Have, patient: bool, view: u8) ?Joined {
+ var tries: u32 = 0;
+ while (true) : (tries += 1) {
+ why.* = @splat(0);
+ if (dial(gpa, path, session, why, have, view)) |j| return sameBuild(gpa, j, why);
+ if (std.mem.indexOf(u8, std.mem.sliceTo(why, 0), "runs another build") != null) {
+ _ = std.fmt.bufPrint(why, "{s}\x00", .{upgrade_words}) catch {};
+ return null;
+ }
+ if (!patient or tries > 200) return null;
+ const ts: libc.timespec = .{ .sec = 0, .nsec = 100 * std.time.ns_per_ms };
+ _ = libc.nanosleep(&ts, null);
+ }
+}
+
/// A replica client's link to its session, the terminal's (tui.zig
/// `remote`) and the window's (gui.zig `remote`): its replica, its socket,
/// what the session said, and what it checks. Drawing is the frontend's.
@@ -2812,32 +2898,6 @@ pub const Link = struct {
join: struct { blobs: u32 = 0, from_memory: u32 = 0, from_disk: u32 = 0, sent_bytes: u64 = 0 } = .{},
join_ms: u64 = 0,
- pub const upgrade_words = "session upgraded to a newer pardes: upgrade this client too";
-
- /// The replica the dial's dump makes, checked against the session as
- /// it was dumped; null, and why in `why`. The dump is let go, a far
- /// join's to the thread that keeps the blobs it was sent (`Have.store`).
- pub fn load(gpa: Allocator, joined: *Joined, o: pardes.Options, why: []u8, have: ?*Have) ?*Replica {
- defer {
- if (have) |h| h.store(gpa, joined) else gpa.free(joined.dump);
- joined.dump = &.{};
- }
- const r = Replica.initDump(gpa, joined.client, joined.view, joined.dump, o) catch |err| {
- _ = std.fmt.bufPrint(why, "the session's dump would not load: {t}\x00", .{err}) catch {};
- return null;
- };
- // PARDES_NO_ECHO: no predicted echo (the gates' baseline).
- if (std.c.getenv("PARDES_NO_ECHO") != null) r.echo.on = false;
- // PARDES_REPLICA_STATS: its timing samples kept, for its line.
- r.stats.samples = std.c.getenv("PARDES_REPLICA_STATS") != null;
- if (!r.loaded(joined.digest)) {
- r.deinit();
- _ = std.fmt.bufPrint(why, "the session's dump loaded as another session (a build's dump is not complete)\x00", .{}) catch {};
- return null;
- }
- return r;
- }
-
/// The test knobs the environment sets (PARDES_REPLICA_CHECK,
/// PARDES_TEST_DIVERGE).
pub fn fromEnv(c: *Link, gpa: Allocator, env: *const std.process.Environ.Map) !void {
@@ -2850,16 +2910,12 @@ pub const Link = struct {
if (c.refused) |words| std.heap.c_allocator.free(words);
}
- /// How its join went, its dial and load `ms`.
- pub fn joinedAs(c: *Link, j: *const Joined, ms: u64) void {
- c.join = .{ .blobs = j.blobs, .from_memory = j.from_memory, .from_disk = j.from_disk, .sent_bytes = j.sent_bytes };
- c.join_ms = ms;
- }
-
- /// A join's link, from its start.
- pub fn reset(c: *Link, r: *Replica, fd: c_int) void {
+ /// Join `j`'s link, from its start, and how that join went.
+ pub fn reset(c: *Link, r: *Replica, j: *const Joined) void {
c.r = r;
- c.fd = fd;
+ c.fd = j.fd;
+ c.join = .{ .blobs = j.blobs, .from_memory = j.from_memory, .from_disk = j.from_disk, .sent_bytes = j.sent_bytes };
+ c.join_ms = j.ms;
c.quit = false;
c.gone = false;
c.dirty = true;
@@ -3011,45 +3067,12 @@ pub const Link = struct {
}
return false;
}
-
- /// The session again, after a divergence or an upgrade: an upgrade's
- /// takes the time its exec and load take, and its daemon's, so it is
- /// dialled every 100 ms for 20 s. A far client's (`have`) holds the
- /// core it leaves meanwhile (`Have.leaving`): its blobs are not sent
- /// again. Null, and why in `why`.
- /// `patient`: after an Upgrade's `refused` alone, while the session is
- /// away a moment; after a divergence a session that will not answer is
- /// gone, and the client says so at once (it froze 20 s, 2026-10-30).
- /// `view`: the one it had, which the session kept for it (views.away).
- pub fn redial(gpa: Allocator, path: []const u8, session: []const u8, why: *[600]u8, have: ?*Have, patient: bool, view: u8) ?Joined {
- var tries: u32 = 0;
- while (true) : (tries += 1) {
- why.* = @splat(0);
- if (dial(gpa, path, session, why, have, view)) |j| {
- if (j.semantics == wire.semantics) return j;
- bye(j.fd, false);
- _ = libc.close(j.fd);
- var jj = j;
- jj.rest.deinit(gpa);
- gpa.free(j.dump);
- _ = std.fmt.bufPrint(why, "{s}\x00", .{upgrade_words}) catch {};
- return null;
- }
- if (std.mem.indexOf(u8, std.mem.sliceTo(why, 0), "runs another build") != null) {
- _ = std.fmt.bufPrint(why, "{s}\x00", .{upgrade_words}) catch {};
- return null;
- }
- if (!patient or tries > 200) return null;
- const ts: libc.timespec = .{ .sec = 0, .nsec = 100 * std.time.ns_per_ms };
- _ = libc.nanosleep(&ts, null);
- }
- }
};
test "a client joining again after a divergence is told at once that its session is gone" {
var why: [600]u8 = undefined;
const t0 = nowNs();
- try std.testing.expect(Link.redial(std.testing.allocator, "/nonexistent/pardes-replica.sock", "gone", &why, null, false, 1) == null);
+ try std.testing.expect(redial(std.testing.allocator, "/nonexistent/pardes-replica.sock", "gone", &why, null, false, 1) == null);
try std.testing.expect(nowNs() - t0 < 100 * std.time.ns_per_ms);
}