diff options
Diffstat (limited to 'core/replica.zig')
| -rw-r--r-- | core/replica.zig | 199 |
1 files changed, 185 insertions, 14 deletions
diff --git a/core/replica.zig b/core/replica.zig index a52061f4..b6ac6ef9 100644 --- a/core/replica.zig +++ b/core/replica.zig @@ -1686,8 +1686,11 @@ pub const Peer = struct { /// What it is sent, not written yet, and what the writer writes now. out: std.ArrayListUnmanaged(u8) = .empty, sending: std.ArrayListUnmanaged(u8) = .empty, - limit: usize = 64 << 20, + /// What may wait in `out` before it is cut off (a test's less). + limit: usize = lag_max, closing: bool = false, + /// Cut off (`send`): the words why are the last it is sent, and its + /// view is kept for it (views.away), as for a bye's `again`. cut: bool = false, /// Its writer has ended (what it was sent, written). done: std.atomic.Value(bool) = .init(false), @@ -1714,7 +1717,7 @@ pub const Peer = struct { if (!writeAll(peer.fd, peer.welcome[0..peer.welcome_len])) return false; const io = peer.peers.io; peer.lock.lockUncancelable(io); - while (peer.far == null and !peer.closing and !peer.cut) peer.cond.waitUncancelable(io, &peer.lock); + while (peer.far == null and !peer.closing) peer.cond.waitUncancelable(io, &peer.lock); const far = peer.far; peer.lock.unlock(io); if (far orelse return false) return peer.farJoin(); @@ -1756,7 +1759,7 @@ pub const Peer = struct { if (peer.blobs.items.len > 0) { const io = peer.peers.io; peer.lock.lockUncancelable(io); - while (peer.want == null and !peer.closing and !peer.cut) peer.cond.waitUncancelable(io, &peer.lock); + while (peer.want == null and !peer.closing) peer.cond.waitUncancelable(io, &peer.lock); const want = peer.want; peer.lock.unlock(io); const bits = want orelse return false; @@ -1771,12 +1774,13 @@ pub const Peer = struct { return peer.z != null; } - /// Whether it is being let go (or was cut off). + /// Whether it is being let go. One cut off is still sent its join + /// whole, then the words why (`send`): it joins again after them. fn letting(peer: *Peer) bool { const io = peer.peers.io; peer.lock.lockUncancelable(io); defer peer.lock.unlock(io); - return peer.closing or peer.cut; + return peer.closing; } /// One message into its stream's compressor. @@ -1827,8 +1831,9 @@ pub const Peer = struct { } /// `parts` queued for it, in order; never waits. A client whose queue - /// passes its limit is cut off: its link is shut down, and its reader - /// says it is gone. + /// passes its limit is cut off: what waits for it is let go and the + /// words why queued in its place, after which its writer ends its link + /// (`writeLoop`); it joins again from a dump, for its view (`leave`). fn send(peer: *Peer, parts: []const []const u8) void { const io = peer.peers.io; peer.lock.lockUncancelable(io); @@ -1848,7 +1853,13 @@ pub const Peer = struct { log.info("client {d} is {d} MB behind: cut off", .{ peer.id, n >> 20 }); peer.peers.cut += 1; peer.cut = true; - _ = libc.shutdown(peer.fd, 2); + peer.out.clearRetainingCapacity(); + var words: [48]u8 = undefined; + var msg: [64]u8 = undefined; + const word = wire.encodeServer(&msg, .{ .refused = std.fmt.bufPrint(&words, "{d} MB behind the session", .{n >> 20}) catch unreachable }) catch unreachable; + peer.out.appendSlice(peer.peers.gpa, word) catch { + _ = libc.shutdown(peer.fd, 2); + }; peer.cond.signal(io); return; } @@ -1899,17 +1910,17 @@ pub const Peer = struct { while (true) { peer.lock.lockUncancelable(io); while (peer.out.items.len == 0 and !peer.closing and !peer.cut) peer.cond.waitUncancelable(io, &peer.lock); - if (peer.out.items.len == 0 or peer.cut) { + if (peer.out.items.len == 0) { peer.lock.unlock(io); return; } std.mem.swap(std.ArrayListUnmanaged(u8), &peer.out, &peer.sending); - // The limit was for the dump and what is queued behind it. - peer.limit = lag_max; + // Cut off: the words why came last, and its link ends after them. + const last = peer.cut; peer.lock.unlock(io); const ok = if (peer.z != null) peer.zWrite(peer.sending.items, .flush) else writeAll(peer.fd, peer.sending.items); peer.sending.clearRetainingCapacity(); - if (!ok) { + if (!ok or last) { _ = libc.shutdown(peer.fd, 2); return; } @@ -2303,7 +2314,7 @@ pub const Peers = struct { core.gpa.free(bytes); return daemon.refuseReplica(fd, "out of memory"); }; - peer.* = .{ .peers = ps, .fd = fd, .id = ps.next_id, .view = k, .limit = lag_max, .dump = bytes, .blobs = blobs, .dump_gpa = core.gpa }; + peer.* = .{ .peers = ps, .fd = fd, .id = ps.next_id, .view = k, .dump = bytes, .blobs = blobs, .dump_gpa = core.gpa }; ps.next_id +%= 1; if (ps.next_id == 0) ps.next_id = 1; // The session's digest as dumped, for the joiner to check its load by. @@ -2338,7 +2349,9 @@ pub const Peers = struct { ps.tellClients(); ps.screens.dropPeer(peer.sid); core.undrawn &= ~(@as(u8, 1) << @intCast(peer.view)); - ps.closeViewOf(core, peer, peer.again); + // Cut off, it joins again after the words why (`send`); its reader + // has ended and no other thread sets `cut` now. + ps.closeViewOf(core, peer, peer.again or peer.cut); if (ps.count() == 0) core.unattended = ps.detached; ps.seq += 1; ps.unit(0, ps.seq); @@ -4925,6 +4938,164 @@ test "a client that diverged joins again for its view: kept for it meanwhile, it try agrees(a, back); } +test "a client cut off for what it left unread is told why and joins again for its view, kept for it meanwhile: it loads the session's dump and agrees" { + // A session's side of its clients' links in memory (`Peers.threads` + // false, the soak's): a connection handed to it and joined at its next + // boundary, the replica loaded from what its writer would write (the + // welcome, the dump with its blobs in place); and what it queued for one, + // down its link in order, the link's end after it if it was cut off. + const MemoryLinks = struct { + fn join(ps: *Peers, core: *Pardes, view: ?u8, fd: *c_int) !struct { r: *Replica, peer: *Peer } { + const gpa = std.testing.allocator; + var fds: [2]c_int = undefined; + if (libc.socketpair(libc.AF.UNIX, libc.SOCK.STREAM | libc.SOCK.CLOEXEC, 0, &fds) != 0) return error.NoSocket; + fd.* = fds[1]; + ps.handed(fds[0], view); + ps.boundary(core); + const peer = for (ps.list) |slot| { + if (slot) |p| if (p.fd == fds[0]) break p; + } else return error.Refused; + const f = (try wire.framed(peer.welcome[0..peer.welcome_len])) orelse return error.BadWelcome; + const w = (try wire.decodeServer(f.tag, f.payload)).welcome; + var whole: std.ArrayListUnmanaged(u8) = .empty; + defer whole.deinit(gpa); + var pos: usize = 0; + for (peer.blobs.items) |b| { + try whole.appendSlice(gpa, peer.dump[pos..b.at]); + try whole.appendSlice(gpa, b.bytes); + pos = b.at; + } + try whole.appendSlice(gpa, peer.dump[pos..]); + var word: [16]u8 = undefined; + const far = try wire.encodeClient(&word, .{ .far = false }); + _ = peer.take(far[0], far[wire.header_len..]); + ps.post(.{ .joined = peer }); + const r = try Replica.initDump(gpa, w.client, w.view.index(), whole.items, .{ .cols = 80, .rows = 24 }); + if (!r.loaded(w.digest)) return error.LoadedAnother; + return .{ .r = r, .peer = peer }; + } + + fn down(peer: *Peer, link: *Link) !void { + var at: usize = 0; + while (try wire.framed(peer.out.items[at..])) |f| : (at += f.total) { + link.take(switch (std.enums.fromInt(wire.ServerTag, f.tag) orelse return error.BadFrame) { + .unit => .{ .unit = try std.heap.c_allocator.dupe(u8, f.payload) }, + .refused => .{ .refused = try std.heap.c_allocator.dupe(u8, (try wire.decodeServer(f.tag, f.payload)).refused) }, + else => return error.UnexpectedFrame, + }); + } + peer.out.clearRetainingCapacity(); + if (!peer.cut) return; + link.quit = true; + link.gone = true; + } + }; + const gpa = std.testing.allocator; + const core = try Pardes.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }); + defer core.deinit(); + core.host = TestHost.host(); + try core.pump(core.host); + var ps: Peers = .{ .gpa = gpa, .io = std.testing.io, .threads = false }; + defer ps.deinit(); + defer ps.dropAll(core); + var fd: c_int = -1; + const first = try MemoryLinks.join(&ps, core, null, &fd); + defer _ = libc.close(fd); + var link: Link = .{ .r = first.r, .fd = fd }; + defer link.deinit(gpa); + const view = first.peer.view; + // The session's own input, taken by the client. + core.update(.{ .key = .{ .cp = 'i', .text = "i" } }); + try core.pump(core.host); + ps.boundary(core); + try MemoryLinks.down(first.peer, &link); + try std.testing.expect(!link.quit and first.r.diverged == null); + // Its link stalls; the session goes on, and what waits for it passes + // the bound: it is cut off. + first.peer.limit = 16 << 10; + const text: [6000]u8 = @splat('t'); + for (0..3) |_| { + core.update(.{ .paste = &text }); + try core.pump(core.host); + ps.boundary(core); + } + try std.testing.expect(first.peer.cut); + try std.testing.expectEqual(@as(u32, 1), ps.cut); + try MemoryLinks.down(first.peer, &link); + try std.testing.expect(link.quit); + // It was told why, and joins again (as a divergence's client does)... + try std.testing.expect(link.rejoins()); + try std.testing.expect(std.mem.indexOf(u8, link.notice(), "behind") != null); + // ...once its old link's end is heard: its view kept for it meanwhile. + ps.post(.{ .gone = first.peer }); + ps.boundary(core); + try std.testing.expect(pardes.views.isAway(core, view)); + first.r.deinit(); + _ = libc.close(fd); + const back = try MemoryLinks.join(&ps, core, view, &fd); + defer back.r.deinit(); + try std.testing.expectEqual(view, back.peer.view); + link.reset(back.r, &.{ .fd = fd, .pid = 0, .client = back.r.id, .view = view, .gmtoff = 0, .dump = &.{} }); + core.update(.{ .paste = "back" }); + try core.pump(core.host); + ps.boundary(core); + try MemoryLinks.down(back.peer, &link); + try std.testing.expect(!link.quit and back.r.diverged == null and back.r.quiet()); + try std.testing.expectEqual(tape.digestOf(core), back.r.digest()); +} + +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 }; + defer ps.deinit(); + 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 _ = libc.close(fds[1]); + const peer = try gpa.create(Peer); + peer.* = .{ .peers = &ps, .fd = fds[0], .id = 1, .view = 0, .dump_gpa = gpa, .far = false, .limit = 256 << 10 }; + peer.welcome_len = (try wire.encodeServer(&peer.welcome, .{ .welcome = .{ .pid = 1, .session = "t" } })).len; + peer.writer = try std.Thread.spawn(.{}, Peer.writeLoop, .{peer}); + defer ps.letGo(peer); + // Units its client does not read: the socket fills, the writer waits + // on it with a batch in hand, and the rest queues up to the bound. + const unit: [16 << 10]u8 = @splat('u'); + const head = wire.header(@backingInt(wire.ServerTag.unit), unit.len); + // The first taken in hand. + peer.send(&.{ &head, &unit }); + while (true) { + peer.lock.lockUncancelable(std.testing.io); + const taken = peer.out.items.len == 0; + peer.lock.unlock(std.testing.io); + if (taken) break; + std.Thread.yield() catch {}; + } + var sent: u32 = 1; + while (!peer.cut) : (sent += 1) peer.send(&.{ &head, &unit }); + const Heard = struct { + units: u32 = 0, + last: ?[]u8 = null, + fn post(h: *@This(), msg: Said) bool { + switch (msg) { + .unit => |b| { + std.heap.c_allocator.free(b); + h.units += 1; + }, + .refused => |words| h.last = words, + else => return false, + } + return true; + } + }; + var heard: Heard = .{}; + var rest: Framer = .{}; + defer rest.deinit(gpa); + readLink(gpa, fds[1], &rest, &heard, Heard.post); + const words = heard.last orelse return error.NotTold; + defer std.heap.c_allocator.free(words); + try std.testing.expectEqualStrings("0 MB behind the session", words); + try std.testing.expect(heard.units > 0 and heard.units < sent); +} + test "a client joined from a dump moves through another view's pane as the session does, its lines not wrapped" { const gpa = std.testing.allocator; const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "build.zig" }, TestHost.host(), false); |
