diff options
Diffstat (limited to 'src/replica.zig')
| -rw-r--r-- | src/replica.zig | 205 |
1 files changed, 180 insertions, 25 deletions
diff --git a/src/replica.zig b/src/replica.zig index 8d8d47c5..2e14cb6f 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -1071,6 +1071,17 @@ fn writeAll(fd: c_int, bytes: []const u8) bool { return true; } +/// `bytes` as dump pieces (1 MiB at most each). +fn writePieces(fd: c_int, bytes: []const u8) bool { + var at: usize = 0; + while (at < bytes.len) : (at += dump_piece) { + const piece = bytes[at..@min(bytes.len, at + dump_piece)]; + const head = wire.header(@backingInt(wire.ServerTag.dump), piece.len); + if (!writeAll(fd, &head) or !writeAll(fd, piece)) return false; + } + return true; +} + fn readSome(fd: c_int, buf: []u8) usize { while (true) { const n = libc.read(fd, buf.ptr, buf.len); @@ -1144,6 +1155,24 @@ pub const Peer = struct { cut: bool = false, /// Its writer has ended (what it was sent, written). done: std.atomic.Value(bool) = .init(false), + /// Its join, which its writer writes before what it is sent: the + /// welcome, then the dump with the session's blobs put back where they + /// go, pinned until written (memory.zig `pin`). The core's allocator's. + welcome: [wire.hello_max + 64]u8 = undefined, + welcome_len: usize = 0, + dump: []u8 = &.{}, + blobs: std.ArrayListUnmanaged(walk.Deferred) = .empty, + dump_gpa: Allocator = undefined, + + fn writeJoin(peer: *Peer) bool { + if (!writeAll(peer.fd, peer.welcome[0..peer.welcome_len])) return false; + var pos: usize = 0; + for (peer.blobs.items) |b| { + if (!writePieces(peer.fd, peer.dump[pos..b.at]) or !writePieces(peer.fd, b.bytes)) return false; + pos = b.at; + } + return writePieces(peer.fd, peer.dump[pos..]); + } /// `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 @@ -1174,6 +1203,12 @@ pub const Peer = struct { fn writeLoop(peer: *Peer) void { const io = peer.peers.io; defer peer.done.store(true, .release); + if (!peer.writeJoin()) { + _ = libc.shutdown(peer.fd, 2); + return; + } + // Its pins let go at the next boundary. + peer.peers.post(.{ .joined = peer }); while (true) { peer.lock.lockUncancelable(io); while (peer.out.items.len == 0 and !peer.closing and !peer.cut) peer.cond.waitUncancelable(io, &peer.lock); @@ -1261,7 +1296,10 @@ pub const Peers = struct { alone_unattended: bool = false, /// The worst a join cost the session's step, and the last one's dump. join_ns: u64 = 0, + each_join: std.ArrayListUnmanaged(u64) = .empty, dump_bytes: usize = 0, + /// ...of it the state the step dumped (the rest, blobs, the writer's). + state_bytes: usize = 0, /// What each client input cost the session (its step, its unit sent), /// and each boundary (its pump's unit sent, what came taken in), ns: /// PARDES_REPLICA_STATS writes them at the session's end. @@ -1273,8 +1311,14 @@ pub const Peers = struct { /// it records: what each was sent. screens: Screens = undefined, 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, + 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 }; + pub const Note = union(enum) { join: c_int, input: Msg, screen: Msg, gone: *Peer, check: *Peer, joined: *Peer, hashed }; pub const Msg = struct { peer: *Peer, msg: []u8 }; pub fn count(ps: *const Peers) usize { @@ -1290,7 +1334,7 @@ pub const Peers = struct { switch (note) { .join => |fd| _ = libc.close(fd), .input, .screen => |in| ps.gpa.free(in.msg), - .gone, .check => {}, + .gone, .check, .joined, .hashed => {}, } return; }; @@ -1344,7 +1388,17 @@ pub const Peers = struct { ps.mail.store(false, .release); ps.lock.unlock(ps.io); for (ps.taken.items) |note| switch (note) { - .join => |fd| ps.join(core, fd), + .join => |fd| { + ps.held.append(ps.gpa, fd) catch { + refuse(fd, "out of memory"); + continue; + }; + ps.joins(core); + }, + .hashed => { + ps.unhashedDone(); + ps.joins(core); + }, .input => |in| { ps.input(core, in.peer, in.msg); ps.gpa.free(in.msg); @@ -1357,6 +1411,7 @@ pub const Peers = struct { in.peer.send(&.{ &head, reply }); }, .gone => |peer| ps.leave(core, peer), + .joined => |peer| released(peer), .check => |peer| { var out: [16]u8 = undefined; peer.send(&.{wire.encodeServer(&out, .{ .check = tape.digestOf(core) }) catch unreachable}); @@ -1410,9 +1465,48 @@ pub const Peers = struct { _ = libc.close(fd); } + /// The clients waiting to join, joined, once the texts the session + /// never digested have their words (`walk.unhashed`): those are hashed + /// on a thread of their own, pinned meanwhile, and the joins wait a + /// boundary or two for them, the session's step never. A second round + /// joins whatever is left. + fn joins(ps: *Peers, core: *Pardes) void { + if (ps.hasher != null or ps.held.items.len == 0) return; + if (ps.rounds < 2) { + walk.unhashed(Pardes, core, &ps.texts); + if (ps.texts.items.len > 0) { + for (ps.texts.items) |b| pardes.memory.pin(b); + if (std.Thread.spawn(.{}, hashTexts, .{ps})) |t| { + ps.hasher = t; + ps.rounds += 1; + return; + } else |_| ps.unhashedDone(); + } + } + ps.rounds = 0; + for (ps.held.items) |fd| ps.join(core, fd); + ps.held.clearRetainingCapacity(); + } + + fn hashTexts(ps: *Peers) void { + walk.makeWords(ps.texts.items); + ps.post(.hashed); + } + + /// The hashing thread joined, its texts unpinned. + fn unhashedDone(ps: *Peers) void { + if (ps.hasher) |t| t.join(); + ps.hasher = null; + for (ps.texts.items) |b| pardes.memory.unpin(b); + ps.texts.clearRetainingCapacity(); + } + fn join(ps: *Peers, core: *Pardes, fd: c_int) void { const t0 = nowNs(); - defer ps.join_ns = @max(ps.join_ns, nowNs() - t0); + defer { + ps.join_ns = @max(ps.join_ns, nowNs() - t0); + ps.each_join.append(ps.gpa, nowNs() - t0) catch {}; + } const slot = for (&ps.list) |*s| { if (s.* == null) break s; } else return refuse(fd, "that session has as many clients as it takes"); @@ -1440,7 +1534,12 @@ pub const Peers = struct { core.undrawn |= @as(u8, 1) << @intCast(k); ps.seq += 1; ps.unit(0, ps.seq); - const bytes = pardes.dump.dumpBytes(core) catch |err| { + // The session's mutable state, dumped now; its blobs (file texts, + // undo, registers) left out, pinned, and streamed by the client's + // writer thread: the step pays for the state, not for the text. + var blobs: std.ArrayListUnmanaged(walk.Deferred) = .empty; + const bytes = pardes.dump.dumpJoin(core, &blobs) catch |err| { + blobs.deinit(core.gpa); core.undrawn &= ~(@as(u8, 1) << @intCast(k)); core.closeView(k); ps.unit(0, ps.seq); @@ -1448,28 +1547,29 @@ pub const Peers = struct { var why: [96]u8 = undefined; return refuse(fd, std.fmt.bufPrint(&why, "that session could not be dumped: {t}", .{err}) catch "that session could not be dumped"); }; - // The core's, as a Dump's is. - defer core.gpa.free(bytes); - ps.dump_bytes = bytes.len; + var whole = bytes.len; + for (blobs.items) |b| { + pardes.memory.pin(b.bytes); + whole += b.bytes.len; + } + ps.dump_bytes = whole; + ps.state_bytes = bytes.len; // The joiner's core is the dump's: the host this tape wraps, and // what the host wrote in the core, are said again at the next input. tape.resync(&ps.t); - const peer = ps.gpa.create(Peer) catch return refuse(fd, "out of memory"); + const peer = ps.gpa.create(Peer) catch { + for (blobs.items) |b| pardes.memory.unpin(b.bytes); + blobs.deinit(core.gpa); + core.gpa.free(bytes); + return refuse(fd, "out of memory"); + }; if (std.c.getenv("PARDES_REPLICA_LAG_KB")) |kb| lag_max = (std.fmt.parseInt(usize, std.mem.span(kb), 10) catch 64 << 10) << 10; - peer.* = .{ .peers = ps, .fd = fd, .id = ps.next_id, .view = k, .limit = bytes.len + lag_max }; + peer.* = .{ .peers = ps, .fd = fd, .id = ps.next_id, .view = k, .limit = lag_max, .dump = bytes, .blobs = blobs, .dump_gpa = core.gpa }; ps.next_id +%= 1; if (ps.next_id == 0) ps.next_id = 1; - var out: [wire.hello_max + 64]u8 = undefined; // The session's digest as dumped, for the joiner to check its load by. const digest = tape.digestOf(core); - const welcome = wire.encodeServer(&out, .{ .welcome = .{ .pid = @intCast(libc.getpid()), .session = ps.name[0..@min(ps.name.len, wire.max_name)], .client = peer.id, .view = k, .dump_len = bytes.len, .gmtoff = gmtoff(), .digest = digest, .semantics = wire.semantics } }) catch unreachable; - peer.send(&.{welcome}); - var at: usize = 0; - while (at < bytes.len) : (at += dump_piece) { - const piece = bytes[at..@min(bytes.len, at + dump_piece)]; - const head = wire.header(@backingInt(wire.ServerTag.dump), piece.len); - peer.send(&.{ &head, piece }); - } + peer.welcome_len = (wire.encodeServer(&peer.welcome, .{ .welcome = .{ .pid = @intCast(libc.getpid()), .session = ps.name[0..@min(ps.name.len, wire.max_name)], .client = peer.id, .view = k, .dump_len = whole, .gmtoff = gmtoff(), .digest = digest, .semantics = wire.semantics } }) catch unreachable).len; peer.writer = std.Thread.spawn(.{}, Peer.writeLoop, .{peer}) catch null; peer.reader = std.Thread.spawn(.{}, Peer.readLoop, .{peer}) catch null; if (peer.writer == null or peer.reader == null) { @@ -1485,7 +1585,7 @@ pub const Peers = struct { peer.sid = sid; ps.screensTo(core, peer, .flush); } else |_| _ = libc.shutdown(fd, 2); - log.info("client {d} joined '{s}' as view {d}: a {d} KB dump in {d} us", .{ peer.id, ps.name, k, bytes.len >> 10, (nowNs() - t0) / 1000 }); + log.info("client {d} joined '{s}' as view {d}: a {d} KB dump ({d} KB of state) in {d} us", .{ peer.id, ps.name, k, whole >> 10, bytes.len >> 10, (nowNs() - t0) / 1000 }); if (std.c.getenv("PARDES_REPLICA_TRACE") != null) std.debug.print("session: client {d} joined after unit {d}\n", .{ peer.id, ps.seq }); } @@ -1514,6 +1614,7 @@ pub const Peers = struct { _ = libc.shutdown(peer.fd, 2); if (peer.writer) |w| w.join(); if (peer.reader) |r| r.join(); + released(peer); _ = libc.close(peer.fd); peer.out.deinit(ps.gpa); peer.sending.deinit(ps.gpa); @@ -1523,8 +1624,8 @@ pub const Peers = struct { while (i < ps.inbox.items.len) { const mine = switch (ps.inbox.items[i]) { .input, .screen => |in| in.peer == peer, - .gone, .check => |p| p == peer, - .join => false, + .gone, .check, .joined => |p| p == peer, + .join, .hashed => false, }; if (!mine) { i += 1; @@ -1567,6 +1668,15 @@ pub const Peers = struct { ps.dropAll(core); } + /// A join's dump and pins let go: written, or its client gone. + fn released(peer: *Peer) void { + for (peer.blobs.items) |b| pardes.memory.unpin(b.bytes); + peer.blobs.deinit(peer.dump_gpa); + peer.blobs = .empty; + if (peer.dump.len > 0) peer.dump_gpa.free(peer.dump); + peer.dump = &.{}; + } + /// Detach typed in client view `k`'s: its link ends (its reader says /// it is gone, and the next boundary lets it go). pub fn detach(ps: *Peers, k: u8) bool { @@ -1589,6 +1699,10 @@ 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.unhashedDone(); + for (ps.held.items) |fd| _ = libc.close(fd); + ps.held.clearRetainingCapacity(); + ps.rounds = 0; for (&ps.list) |*slot| if (slot.*) |peer| { slot.* = null; core.undrawn &= ~(@as(u8, 1) << @intCast(peer.view)); @@ -1601,7 +1715,7 @@ pub const Peers = struct { for (ps.inbox.items) |note| switch (note) { .join => |fd| _ = libc.close(fd), .input, .screen => |in| ps.gpa.free(in.msg), - .gone, .check => {}, + .gone, .check, .joined, .hashed => {}, }; ps.inbox.clearRetainingCapacity(); ps.lock.unlock(ps.io); @@ -1615,20 +1729,29 @@ pub const Peers = struct { ps.input_ns.deinit(ps.gpa); ps.boundary_ns.deinit(ps.gpa); ps.msgs.deinit(ps.gpa); + ps.held.deinit(ps.gpa); + ps.texts.deinit(std.heap.c_allocator); + ps.each_join.deinit(ps.gpa); } fn writeStats(ps: *Peers, path: []const u8) void { var buf: [400]u8 = undefined; - const line = std.fmt.bufPrint(&buf, "session inputs {d} input_us {d}/{d}/{d} boundaries {d} boundary_us {d}/{d}/{d} join_us {d} dump_kb {d} cut {d}\n", .{ + const line = std.fmt.bufPrint(&buf, "session inputs {d} input_us {d}/{d}/{d} boundaries {d} boundary_us {d}/{d}/{d} join_us {d} dump_kb {d} state_kb {d} cut {d}\n", .{ ps.input_ns.items.len, pctOf(ps.input_ns.items, 50) / 1000, pctOf(ps.input_ns.items, 99) / 1000, pctOf(ps.input_ns.items, 100) / 1000, ps.boundary_ns.items.len, pctOf(ps.boundary_ns.items, 50) / 1000, pctOf(ps.boundary_ns.items, 99) / 1000, pctOf(ps.boundary_ns.items, 100) / 1000, - ps.join_ns / 1000, ps.dump_bytes >> 10, ps.cut, + ps.join_ns / 1000, ps.dump_bytes >> 10, ps.state_bytes >> 10, ps.cut, }) catch return; var z: [4096]u8 = undefined; const pz = std.fmt.bufPrintSentinel(&z, "{s}", .{path}, 0) catch return; const fd = libc.open(pz, .{ .ACCMODE = .WRONLY, .CREAT = true, .APPEND = true, .CLOEXEC = true }, @as(libc.mode_t, 0o644)); if (fd < 0) return; _ = libc.write(fd, line.ptr, line.len); + var each: [400]u8 = undefined; + var w: std.Io.Writer = .fixed(&each); + w.writeAll("session joins_us") catch {}; + for (ps.each_join.items) |ns| w.print(" {d}", .{ns / 1000}) catch {}; + w.writeAll("\n") catch {}; + _ = libc.write(fd, w.buffered().ptr, w.buffered().len); _ = libc.close(fd); } }; @@ -2052,3 +2175,35 @@ test "a replica joined from the session's dump replays the units after it" { try std.testing.expect(r.quiet()); try std.testing.expectEqual(digestOf(a), r.digest()); } + +test "a join's dump, its blobs put back where they go, is the session's dump" { + 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 text = try gpa.alloc(u8, 20000); + defer gpa.free(text); + @memset(text, 'x'); + _ = try a.local(.{ .key = .{ .cp = 'i', .text = "i" } }); + _ = try a.local(.{ .paste = text }); + _ = try a.local(.{ .key = .{ .cp = pardes.Key.escape, .text = "" } }); + const was = Stepping.begin(&a.t, null); + defer was.end(); + const whole = try pardes.dump.dumpBytes(a.core); + defer a.core.gpa.free(whole); + var blobs: std.ArrayListUnmanaged(walk.Deferred) = .empty; + defer blobs.deinit(a.core.gpa); + const state = try pardes.dump.dumpJoin(a.core, &blobs); + defer a.core.gpa.free(state); + try std.testing.expect(blobs.items.len > 0); + try std.testing.expect(state.len + 20000 <= whole.len); + var joined: std.ArrayListUnmanaged(u8) = .empty; + defer joined.deinit(gpa); + var pos: usize = 0; + for (blobs.items) |b| { + try joined.appendSlice(gpa, state[pos..b.at]); + try joined.appendSlice(gpa, b.bytes); + pos = b.at; + } + try joined.appendSlice(gpa, state[pos..]); + try std.testing.expectEqualSlices(u8, whole, joined.items); +} |
