diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-17 23:28:13 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-17 23:42:48 -0300 |
| commit | 9b20d00ca5ffbab640b87249d65e4165414d22c1 (patch) | |
| tree | e62644195d40b5a41ed92a5e19548fa84cff4821 /src/replica.zig | |
| parent | 6c61169832d6f396ee7a6e9e0b09d9c055d08f8d (diff) | |
| download | pardes-9b20d00ca5ffbab640b87249d65e4165414d22c1.tar.gz pardes-9b20d00ca5ffbab640b87249d65e4165414d22c1.zip | |
replica: a record of a MiB or more crosses to a client by its hash, beside its unit (tape.zig by_hash_min, Big; wire blob): a file over the wire's 16 MiB bound opened while a client was attached was one unit, past the bound, so a Debug session panicked and a release one dropped it, its client diverging at the next unit and joining again to be sent the whole file in the dump; now the read's answer is recorded as the file's BLAKE3, a client on the session's machine reads the path itself and checks it, a far one is sent it in 1 MiB pieces before the unit and keeps it in its cache.xitdb (XDG_CACHE_HOME/pardes), so a far client that joins after is sent none of it; any other record that long (a reload's text, a paste) goes to every client so; test/daemon.py by_hash: a 17 MiB file under a near and a far client, both agree, neither joins again, the near one reads it itself, the far one is sent 8848 KB of zstd and a far client after it none of its 2 blobs; the slot's own BLAKE3 the brief asked for was built and taken out (a hash a kind's open, 7 instructions a byte on the perf build's CPU, for no reader until a client renders kinds); perf --only medium --reps 10 instructions:u 2,459.25M -> 2,459.36M, --terminal-mib 8 3,928.50M -> 3,928.51M; tui core-test 811/950 (139 skipped), shell-test passes, replicas pass (diverged 0, checked/wrong 0), behaviour-test 269/273 (the four addon ABI tests the base fails), --replay 20 seeds 0 failures, process-test 2 of 144 failed (lspcomplete and lspcompletemoved, the base's; headersweep and headersweep-restored failed once at load 14 and pass alone), test/daemon.py passes
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'src/replica.zig')
| -rw-r--r-- | src/replica.zig | 86 |
1 files changed, 84 insertions, 2 deletions
diff --git a/src/replica.zig b/src/replica.zig index 09746377..fa888021 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -689,6 +689,14 @@ pub const Replica = struct { screen_out: std.ArrayListUnmanaged(u8) = .empty, /// Its Shadows' rasters, from the session's strips (17.3). blobs: pardes.Kind.Blobs = .{}, + /// A record's bytes by hash coming in pieces (`blobPiece`), and how + /// many of them came. + incoming: ?tape.Big = null, + incoming_got: usize = 0, + /// A far client's blobs on disk (its Have's), where a file it is sent + /// goes for its next join. + cache: ?Cache = null, + io: std.Io = undefined, /// Its predicted echo (src/term/echo.zig), drawn over its core's terminals: /// fed by its keys, its units and its deltas here, and by `echoFrame`. echo: Echo = .{}, @@ -777,6 +785,7 @@ pub const Replica = struct { was.end(); } r.blobs.deinit(r.gpa); + if (r.incoming) |b| r.gpa.free(b.bytes); r.copy.deinit(); for (r.pending.items) |p| r.freePending(p); r.pending.deinit(r.gpa); @@ -1261,6 +1270,33 @@ pub const Replica = struct { return r.screen_out.items; } + /// A piece of a record's bytes by hash (Peer.blob): whole and checked + /// against its hash, kept for the unit that names it (tape.keepFile), + /// and a file's, a far client's, on disk (`cache`) for its next join. + pub fn blobPiece(r: *Replica, msg: []const u8) void { + if (msg.len < 49) return r.fail("a short piece of a record by hash"); + const total = std.math.cast(usize, std.mem.readInt(u64, msg[32..40], .little)) orelse return r.fail("a record by hash too long"); + const at = std.mem.readInt(u64, msg[40..48], .little); + if (at == 0) { + if (r.incoming) |b| r.gpa.free(b.bytes); + r.incoming = .{ .hash = msg[0..32].*, .bytes = r.gpa.alloc(u8, total) catch return r.fail("out of memory for a record by hash"), .path = msg[48] != 0 }; + r.incoming_got = 0; + } + const b = r.incoming orelse return r.fail("a piece of a record by hash out of order"); + const piece = msg[49..]; + if (at != r.incoming_got or b.bytes.len != total or piece.len > total - at) return r.fail("a piece of a record by hash out of order"); + @memcpy(b.bytes[at..][0..piece.len], piece); + r.incoming_got += piece.len; + if (r.incoming_got < total) return; + r.incoming = null; + if ((b.path and b.bytes.len < 9) or !std.mem.eql(u8, &tape.Big.hashOf(b.bytes, b.path), &b.hash)) { + r.gpa.free(b.bytes); + return r.fail("a record by hash came other than its hash"); + } + if (b.path) if (r.cache) |c| if (b.bytes.len - 9 <= c.cap) c.put(r.io, &.{.{ .hash = b.hash, .bytes = b.bytes[9..] }}, &.{}); + tape.keepFile(&r.t, b); + } + pub fn digest(r: *Replica) u64 { const was = Stepping.begin(&r.t, &r.epoch); defer was.end(); @@ -1697,6 +1733,33 @@ pub const Peer = struct { peer.cond.signal(io); } + /// A record's bytes by hash (tape.Big) queued for it in pieces, before + /// the unit that names them: for a far client, and for one on this + /// machine unless they are a file it reads itself (tape.zig `byHash`). + /// Never cut off for them: a big file is no lag. + fn blob(peer: *Peer, b: tape.Big) void { + const io = peer.peers.io; + peer.lock.lockUncancelable(io); + defer peer.lock.unlock(io); + if (peer.cut or peer.closing or (b.path and peer.far == false)) return; + var at: usize = 0; + while (at < b.bytes.len) : (at += dump_piece) { + const piece = b.bytes[at..@min(b.bytes.len, at + dump_piece)]; + var meta: [49]u8 = undefined; + meta[0..32].* = b.hash; + std.mem.writeInt(u64, meta[32..40], b.bytes.len, .little); + std.mem.writeInt(u64, meta[40..48], at, .little); + meta[48] = @intFromBool(b.path); + const head = wire.header(@backingInt(wire.ServerTag.blob), meta.len + piece.len); + for ([_][]const u8{ &head, &meta, piece }) |part| peer.out.appendSlice(peer.peers.gpa, part) catch { + peer.cut = true; + _ = libc.shutdown(peer.fd, 2); + return; + }; + } + peer.cond.signal(io); + } + fn writeLoop(peer: *Peer) void { const io = peer.peers.io; defer peer.done.store(true, .release); @@ -1912,6 +1975,13 @@ pub const Peers = struct { const bytes = ps.t.buf.items; unitHeader(&ps.head, origin, seq); trace(ps.gpa, "session: unit {d}/{d}, {d} bytes, to {d} clients:\n", .{ origin, seq, bytes.len, ps.count() }, &.{bytes}); + // Its records by hash first (tape.by_hash_min: a file read), each + // whole before the unit that names it. + for (ps.t.big.items) |b| { + for (ps.list) |slot| if (slot) |peer| peer.blob(b); + ps.gpa.free(b.bytes); + } + ps.t.big.clearRetainingCapacity(); const head = wire.header(@backingInt(wire.ServerTag.unit), unit_head + bytes.len); for (ps.list) |slot| if (slot) |peer| peer.send(&.{ &head, &ps.head, bytes }); ps.t.buf.clearRetainingCapacity(); @@ -2692,7 +2762,7 @@ pub fn bye(fd: c_int, again: bool) void { /// What the session streams a client after its dump, as its frontend's loop /// takes it: a unit's and a terminal message's bytes and a refusal's words /// on the C heap, the loop's to free. -pub const Said = union(enum) { unit: []u8, screen: []u8, checked: u64, refused: []u8 }; +pub const Said = union(enum) { unit: []u8, screen: []u8, checked: u64, refused: []u8, blob: []u8 }; /// The session's stream off `fd`, each message handed to `post` until the /// link ends (a refusal ends it too); `post` says whether it took it (one @@ -2704,12 +2774,13 @@ pub fn readLink(gpa: Allocator, fd: c_int, rest: *Framer, ctx: anytype, comptime const msg: Said = switch (t) { .unit => .{ .unit = std.heap.c_allocator.dupe(u8, payload) catch return false }, .screen => .{ .screen = std.heap.c_allocator.dupe(u8, payload) catch return false }, + .blob => .{ .blob = std.heap.c_allocator.dupe(u8, payload) catch return false }, .check => .{ .checked = (wire.decodeServer(tag, payload) catch return false).check }, .refused => .{ .refused = std.heap.c_allocator.dupe(u8, (wire.decodeServer(tag, payload) catch return false).refused) catch return false }, .pong, .welcome, .dump, .blobs, .zstd => return true, }; if (!post(x, msg)) switch (msg) { - .unit, .screen, .refused => |b| std.heap.c_allocator.free(b), + .unit, .screen, .refused, .blob => |b| std.heap.c_allocator.free(b), .checked => {}, }; return msg != .refused; @@ -2766,6 +2837,13 @@ pub fn join(gpa: Allocator, j: *Joined, path: []const u8, session: []const u8, o return null; } r.gmtoff = j.gmtoff; + // On the session's machine: a file read sent by hash is read here; + // far, it goes to the disk for the next join. + r.t.local_files = have == null; + if (have) |h| { + r.cache = h.cache; + r.io = h.io; + } j.ms = (nowNs() - t0) / std.time.ns_per_ms; return r; } @@ -2923,6 +3001,10 @@ pub const Link = struct { if (c.r.screen(bytes)) |reply| if (!sendScreen(c.fd, reply)) c.cut(); c.dirty = true; }, + .blob => |bytes| { + defer std.heap.c_allocator.free(bytes); + c.r.blobPiece(bytes); + }, .checked => |d| c.checked(d), .refused => |words| { if (c.refused) |old| std.heap.c_allocator.free(old); |
