summaryrefslogtreecommitdiff
path: root/src/replica.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/replica.zig')
-rw-r--r--src/replica.zig138
1 files changed, 101 insertions, 37 deletions
diff --git a/src/replica.zig b/src/replica.zig
index da0c3e64..4f84712f 100644
--- a/src/replica.zig
+++ b/src/replica.zig
@@ -1414,6 +1414,9 @@ pub const Peer = struct {
fd: c_int,
id: u16,
view: u8,
+ /// Its bye said it joins again for its view (a divergence's rejoin):
+ /// kept for it (views.away), not closed. Its reader's, read after its end.
+ again: bool = false,
/// Its number in the session's `Screens`.
sid: u16 = 0,
/// Inputs read (each one's seq, as the client numbered them).
@@ -1626,7 +1629,11 @@ pub const Peer = struct {
fn take(peer: *Peer, tag: u8, payload: []const u8) bool {
const t = std.enums.fromInt(wire.ClientTag, tag) orelse return false;
switch (t) {
- .hello, .bye => return false,
+ .hello => return false,
+ .bye => {
+ peer.again = (wire.decodeClient(tag, payload) catch return false).bye.again;
+ return false;
+ },
.ping => {
var out: [16]u8 = undefined;
const v = (wire.decodeClient(tag, payload) catch return false).ping;
@@ -1747,12 +1754,14 @@ pub const Peers = struct {
msgs: std.ArrayListUnmanaged([]u8) = .empty,
/// Clients waiting to join while the session's texts are hashed
/// (`joins`), the texts and the thread hashing them.
- held: std.ArrayListUnmanaged(c_int) = .empty,
+ held: std.ArrayListUnmanaged(Joining) = .empty,
texts: std.ArrayListUnmanaged([]const u8) = .empty,
hasher: ?std.Thread = null,
rounds: u8 = 0,
- pub const Note = union(enum) { join: c_int, input: Msg, screen: Msg, gone: *Peer, check: *Peer, joined: *Peer, hashed };
+ pub const Note = union(enum) { join: Joining, input: Msg, screen: Msg, gone: *Peer, check: *Peer, joined: *Peer, hashed };
+ /// A connection to join, and the view it joins again for, if it does.
+ pub const Joining = struct { fd: c_int, view: ?u8 = null };
pub const Msg = struct { peer: *Peer, msg: []u8 };
pub fn count(ps: *const Peers) usize {
@@ -1766,7 +1775,7 @@ pub const Peers = struct {
ps.inbox.append(ps.gpa, note) catch {
ps.lock.unlock(ps.io);
switch (note) {
- .join => |fd| _ = libc.close(fd),
+ .join => |j| _ = libc.close(j.fd),
.input, .screen => |in| ps.gpa.free(in.msg),
.gone, .check, .joined, .hashed => {},
}
@@ -1778,9 +1787,10 @@ pub const Peers = struct {
}
/// A replica's connection the daemon handed over (its hello taken
- /// there): joined at the next step boundary. Any thread.
- pub fn handed(ps: *Peers, fd: c_int) void {
- ps.post(.{ .join = fd });
+ /// there), and the view its hello says it had: joined at the next step
+ /// boundary. Any thread.
+ pub fn handed(ps: *Peers, fd: c_int, view: ?u8) void {
+ ps.post(.{ .join = .{ .fd = fd, .view = view } });
}
/// What was recorded since the last unit, to every client as one, its
@@ -1824,9 +1834,9 @@ pub const Peers = struct {
ps.mail.store(false, .release);
ps.lock.unlock(ps.io);
for (ps.taken.items) |note| switch (note) {
- .join => |fd| {
- ps.held.append(ps.gpa, fd) catch {
- refuse(fd, "out of memory");
+ .join => |j| {
+ ps.held.append(ps.gpa, j) catch {
+ refuse(j.fd, "out of memory");
continue;
};
ps.joins(core);
@@ -1922,7 +1932,7 @@ pub const Peers = struct {
}
}
ps.rounds = 0;
- for (ps.held.items) |fd| ps.join(core, fd);
+ for (ps.held.items) |j| ps.join(core, j);
ps.held.clearRetainingCapacity();
}
@@ -1939,7 +1949,8 @@ pub const Peers = struct {
ps.texts.clearRetainingCapacity();
}
- fn join(ps: *Peers, core: *Pardes, fd: c_int) void {
+ fn join(ps: *Peers, core: *Pardes, joining: Joining) void {
+ const fd = joining.fd;
const t0 = nowNs();
defer {
ps.join_ns = @max(ps.join_ns, nowNs() - t0);
@@ -1964,7 +1975,13 @@ pub const Peers = struct {
}
// Someone looks at it now: its messages go by keys, not the clock.
core.unattended = false;
- const k = core.openView() orelse {
+ // Its own view again, kept for it since it left (views.away), or one
+ // new. 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.
+ const back = if (joining.view) |v| core.rejoinView(v) else false;
+ const k = (if (back) joining.view else core.openView()) orelse {
core.unattended = ps.count() == 0 and ps.alone_unattended;
if (ps.count() == 0) ps.stop(core);
return refuse(fd, "that session has as many views as it takes");
@@ -2035,7 +2052,7 @@ pub const Peers = struct {
log.info("client {d} left '{s}'", .{ peer.id, ps.name });
ps.screens.dropPeer(peer.sid);
core.undrawn &= ~(@as(u8, 1) << @intCast(peer.view));
- ps.closeViewOf(core, peer);
+ ps.closeViewOf(core, peer, peer.again);
if (ps.count() == 0) core.unattended = ps.alone_unattended;
ps.seq += 1;
ps.unit(0, ps.seq);
@@ -2043,11 +2060,12 @@ pub const Peers = struct {
if (ps.count() == 0) ps.stop(core);
}
- /// A gone client's view closed (its slot let go already): the last one
- /// of a session no one else watches leaves the session its layout, for
- /// its rejoin (an Upgrade's, a diverged one's) to find.
- fn closeViewOf(ps: *Peers, core: *Pardes, peer: *Peer) void {
- if (ps.count() == 0 and ps.alone_unattended) core.leaveView(peer.view) else core.closeView(peer.view);
+ /// A gone client's view closed (its slot let go already), or kept for
+ /// it (`again`: it joins again for it, views.away): the last one of a
+ /// session no one else watches leaves the session its layout, for a
+ /// client that comes after to find.
+ fn closeViewOf(ps: *Peers, core: *Pardes, peer: *Peer, again: bool) void {
+ if (again) core.awayView(peer.view) else if (ps.count() == 0 and ps.alone_unattended) core.leaveView(peer.view) else core.closeView(peer.view);
}
/// Its threads ended and joined, its link closed.
@@ -2114,7 +2132,8 @@ pub const Peers = struct {
const ts: libc.timespec = .{ .sec = 0, .nsec = 2 * std.time.ns_per_ms };
_ = libc.nanosleep(&ts, null);
}
- ps.dropAll(core);
+ // Each joins again for its view, kept in the dump (views.away).
+ ps.drop(core, true);
}
/// A join's dump and pins let go: written, or its client gone.
@@ -2149,21 +2168,26 @@ pub const Peers = struct {
/// Every client let go (the session's end, a Restore): each sees its
/// link end.
pub fn dropAll(ps: *Peers, core: *Pardes) void {
+ ps.drop(core, false);
+ }
+
+ /// ...each view kept for its client if `again` (an Upgrade's).
+ fn drop(ps: *Peers, core: *Pardes, again: bool) void {
ps.unhashedDone();
- for (ps.held.items) |fd| _ = libc.close(fd);
+ for (ps.held.items) |j| _ = libc.close(j.fd);
ps.held.clearRetainingCapacity();
ps.rounds = 0;
for (&ps.list) |*slot| if (slot.*) |peer| {
slot.* = null;
core.undrawn &= ~(@as(u8, 1) << @intCast(peer.view));
- ps.closeViewOf(core, peer);
+ ps.closeViewOf(core, peer, again);
ps.screens.dropPeer(peer.sid);
ps.letGo(peer);
};
ps.stop(core);
ps.lock.lockUncancelable(ps.io);
for (ps.inbox.items) |note| switch (note) {
- .join => |fd| _ = libc.close(fd),
+ .join => |j| _ = libc.close(j.fd),
.input, .screen => |in| ps.gpa.free(in.msg),
.gone, .check, .joined, .hashed => {},
};
@@ -2238,8 +2262,9 @@ pub const Got = struct { pos: usize, len: usize, hash: [32]u8 };
/// Connected to the replica socket at `path`, its hello said for
/// `session`, welcomed, and the dump read whole: what a client joins with.
/// A far client (`have`) is sent its dump's blobs by hash first, and asks
-/// for those it holds nowhere. Errors are said in `why`.
-pub fn dial(gpa: Allocator, path: []const u8, session: []const u8, why: []u8, have: ?*Have) ?Joined {
+/// for those it holds nowhere. Errors are said in `why`. `view`: the one
+/// it had, joining again for it.
+pub fn dial(gpa: Allocator, path: []const u8, session: []const u8, why: []u8, have: ?*Have, view: ?u8) ?Joined {
const fd = libc.socket(libc.AF.UNIX, libc.SOCK.STREAM | libc.SOCK.CLOEXEC, 0);
if (fd < 0) return said(why, "no socket", .{});
var addr: libc.sockaddr.un = .{ .path = @splat(0) };
@@ -2252,14 +2277,14 @@ pub fn dial(gpa: Allocator, path: []const u8, session: []const u8, why: []u8, ha
_ = libc.close(fd);
return said(why, "nothing answers at {s}", .{path});
}
- return joinOn(gpa, fd, session, why, have);
+ return joinOn(gpa, fd, session, why, have, view);
}
/// `dial`'s, on a connection made: the hello, then the join. The
/// connection is closed if the join fails.
-fn joinOn(gpa: Allocator, fd: c_int, session: []const u8, why: []u8, have: ?*Have) ?Joined {
+fn joinOn(gpa: Allocator, fd: c_int, session: []const u8, why: []u8, have: ?*Have, view: ?u8) ?Joined {
var out: [wire.hello_max + 16]u8 = undefined;
- const hello = wire.encodeClient(&out, .{ .hello = .{ .session = session } }) catch {
+ const hello = wire.encodeClient(&out, .{ .hello = .{ .session = session, .view = view } }) catch {
_ = libc.close(fd);
return said(why, "that session's name is too long", .{});
};
@@ -2690,10 +2715,10 @@ pub fn askCheck(fd: c_int) bool {
return writeAll(fd, wire.encodeClient(&out, .check) catch unreachable);
}
-/// The client's last word.
-pub fn bye(fd: c_int) void {
+/// The client's last word: `again`, it joins again for its view at once.
+pub fn bye(fd: c_int, again: bool) void {
var out: [16]u8 = undefined;
- if (wire.encodeClient(&out, .bye)) |b| _ = writeAll(fd, b) else |_| {}
+ if (wire.encodeClient(&out, .{ .bye = .{ .again = again } })) |b| _ = writeAll(fd, b) else |_| {}
}
/// What follows the dump on the client's connection, each message handed
@@ -2985,13 +3010,14 @@ pub const Link = struct {
/// `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).
- pub fn redial(gpa: Allocator, path: []const u8, session: []const u8, why: *[600]u8, have: ?*Have, patient: bool) ?Joined {
+ /// `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)) |j| {
+ if (dial(gpa, path, session, why, have, view)) |j| {
if (j.semantics == wire.semantics) return j;
- bye(j.fd);
+ bye(j.fd, false);
_ = libc.close(j.fd);
var jj = j;
jj.rest.deinit(gpa);
@@ -3013,7 +3039,7 @@ pub const Link = struct {
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) == null);
+ try std.testing.expect(Link.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);
}
@@ -3777,7 +3803,7 @@ fn farJoinOnce(ps: *Peers, core: *Pardes, have: ?*Have) !Joined {
peer.welcome_len = (wire.encodeServer(&peer.welcome, .{ .welcome = .{ .pid = 1, .session = "t", .dump_len = whole, .blobs = @intCast(blobs.items.len) } }) catch unreachable).len;
const Serve = struct {
fn run(p: *Peer) void {
- var hello: [wire.header_len + 13]u8 = undefined;
+ var hello: [wire.header_len + 14]u8 = undefined;
var got: usize = 0;
while (got < hello.len) {
const n = libc.read(p.fd, hello[got..].ptr, hello.len - got);
@@ -3791,7 +3817,7 @@ fn farJoinOnce(ps: *Peers, core: *Pardes, have: ?*Have) !Joined {
peer.reader = try std.Thread.spawn(.{}, Serve.run, .{peer});
defer ps.letGo(peer);
var why: [300]u8 = @splat(0);
- return joinOn(std.testing.allocator, fds[1], "t", &why, have) orelse {
+ return joinOn(std.testing.allocator, fds[1], "t", &why, have, null) orelse {
std.debug.print("the join failed: {s}\n", .{std.mem.sliceTo(&why, 0)});
return error.JoinFailed;
};
@@ -4059,6 +4085,44 @@ test "a client leaves, and one joins: what the leave queued (a terminal's resize
try agrees(a, joiner);
}
+test "a client that diverged joins again for its view: kept for it meanwhile, its pane in it again; the other client agrees" {
+ 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 old = try Replica.init(gpa, 1, first, false);
+ defer old.deinit();
+ const opened = try a.openView(2);
+ old.confirm(opened.unit);
+ var buf: [256]u8 = undefined;
+ old.confirm(try a.take(try inputOf(&buf, 2, 1, .{ .command = "New" })));
+ const made = a.core.active;
+ // Its bye says it joins again: its view is kept, as every replica keeps it.
+ var was = Stepping.begin(&a.t, null);
+ a.core.awayView(opened.view);
+ was.end();
+ old.confirm(try a.unit(0, 100));
+ try agrees(a, old);
+ // Back for it: its own, its pane in it, in the dump it loads.
+ was = Stepping.begin(&a.t, null);
+ try std.testing.expect(a.core.rejoinView(opened.view));
+ was.end();
+ old.confirm(try a.unit(0, 101));
+ const back = try joinedNow(a, 3, opened.view);
+ defer back.deinit();
+ try std.testing.expectEqual(digestOf(a), back.digest());
+ a.view_of[3] = opened.view;
+ const u = try gpa.dupe(u8, try a.take(try back.input(.{ .key = .{ .cp = 'l', .text = "l" } })));
+ defer gpa.free(u);
+ old.confirm(u);
+ back.confirm(u);
+ try std.testing.expectEqual(made, a.core.active);
+ try std.testing.expect(pardes.layout.findPane(a.core, made) != null);
+ try agrees(a, old);
+ try agrees(a, back);
+}
+
/// An input of a replica's in the session's order, as `Peers.input` takes
/// it: the terminals flushed to every peer, the step, the rows it read that
/// each peer holds as holes (`fill`), then its unit to every replica.