summaryrefslogtreecommitdiff
path: root/src/replica.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/replica.zig')
-rw-r--r--src/replica.zig272
1 files changed, 94 insertions, 178 deletions
diff --git a/src/replica.zig b/src/replica.zig
index 5bcc0a53..b98d792b 100644
--- a/src/replica.zig
+++ b/src/replica.zig
@@ -2421,6 +2421,8 @@ fn joinOn(gpa: Allocator, fd: c_int, session: []const u8, why: []u8, have: ?*Hav
pub const Have = struct {
io: std.Io,
cache: ?Cache = null,
+ /// The blobs a join found on disk: the most recently used once stored.
+ hits: std.ArrayListUnmanaged([32]u8) = .empty,
/// The core it leaves (`leaving`), each blob hashed once when one of its
/// length is looked for. ponytail: a scan a lookup, fine to some
/// thousands of blobs; a map by length would take over past that.
@@ -2438,7 +2440,7 @@ pub const Have = struct {
h.left();
h.old.deinit(std.heap.c_allocator);
h.old_hash.deinit(std.heap.c_allocator);
- if (h.cache) |c| c.dir.close(h.io);
+ h.hits.deinit(std.heap.c_allocator);
}
fn wait(h: *Have) void {
@@ -2475,189 +2477,83 @@ pub const Have = struct {
@memcpy(out, b);
return .memory;
}
- if (h.cache) |c| if (c.get(h.io, hash, out)) return .disk;
+ if (h.cache) |*c| if (c.get(h.io, hash, out)) {
+ h.hits.append(std.heap.c_allocator, hash.*) catch {};
+ return .disk;
+ };
return null;
}
/// The blobs a join was sent, written to the cache on a thread of their
- /// own, which frees the dump (`gpa`'s) and the list after: the first
- /// frame waits for none of it.
+ /// own (with those it found there, made the most recently used), which
+ /// frees the dump (`gpa`'s) and the lists after: the first frame waits
+ /// for none of it.
pub fn store(h: *Have, gpa: Allocator, j: *Joined) void {
const dump = j.dump;
const got = j.got;
j.dump = &.{};
j.got = &.{};
h.wait();
+ var hits = h.hits;
+ h.hits = .empty;
const Store = struct {
- fn run(c: Cache, io: std.Io, a: Allocator, bytes: []u8, list: []Got) void {
+ fn run(c: Cache, io: std.Io, a: Allocator, bytes: []u8, list: []Got, found: std.ArrayListUnmanaged([32]u8)) void {
+ var seen = found;
defer {
a.free(bytes);
std.heap.c_allocator.free(list);
+ seen.deinit(std.heap.c_allocator);
}
- var incoming: u64 = 0;
- for (list) |g| incoming += g.len;
- c.trim(io, @min(incoming, c.cap));
+ var blobs: std.ArrayListUnmanaged(walk.Blob) = .empty;
+ defer blobs.deinit(std.heap.c_allocator);
var put: u64 = 0;
for (list) |g| {
if (put + g.len > c.cap) continue;
put += g.len;
- c.put(io, &g.hash, bytes[g.pos..][0..g.len]);
+ blobs.append(std.heap.c_allocator, .{ .hash = g.hash, .bytes = bytes[g.pos..][0..g.len] }) catch return;
}
+ c.put(io, blobs.items, seen.items);
}
};
const c = h.cache orelse {
gpa.free(dump);
std.heap.c_allocator.free(got);
+ hits.deinit(std.heap.c_allocator);
return;
};
- h.storing = std.Thread.spawn(.{}, Store.run, .{ c, h.io, gpa, dump, got }) catch {
- Store.run(c, h.io, gpa, dump, got);
+ h.storing = std.Thread.spawn(.{}, Store.run, .{ c, h.io, gpa, dump, got, hits }) catch {
+ Store.run(c, h.io, gpa, dump, got, hits);
return;
};
}
/// `core`'s blobs the cache lacks, written there: what this client made
- /// since it joined (an edit's text, its undo), for its next start.
+ /// since it joined (an edit's text, its undo), for its next start; then
+ /// the cache trimmed under its cap, at this client's end.
pub fn keep(h: *Have, core: *const Pardes) void {
h.wait();
const c = h.cache orelse return;
var all: std.ArrayListUnmanaged([]const u8) = .empty;
defer all.deinit(std.heap.c_allocator);
walk.blobs(Pardes, core, &all);
- var missing: std.ArrayListUnmanaged(struct { bytes: []const u8, hash: [32]u8 }) = .empty;
+ var missing: std.ArrayListUnmanaged(walk.Blob) = .empty;
defer missing.deinit(std.heap.c_allocator);
- var incoming: u64 = 0;
+ var put: u64 = 0;
for (all.items) |b| {
var d: [32]u8 = undefined;
std.crypto.hash.Blake3.hash(b, &d, .{});
- if (c.has(h.io, &d, b.len)) continue;
+ if (put + b.len > c.cap or c.has(h.io, &d, b.len)) continue;
+ put += b.len;
missing.append(std.heap.c_allocator, .{ .bytes = b, .hash = d }) catch return;
- incoming += b.len;
- }
- if (missing.items.len == 0) return;
- c.trim(h.io, @min(incoming, c.cap));
- var put: u64 = 0;
- for (missing.items) |m| {
- if (put + m.bytes.len > c.cap) continue;
- put += m.bytes.len;
- c.put(h.io, &m.hash, m.bytes);
}
+ c.put(h.io, missing.items, h.hits.items);
+ h.hits.clearRetainingCapacity();
+ c.trim(h.io);
}
};
-/// A far client's blobs on disk, a file each named by its BLAKE3 hash in
-/// hex, in $XDG_CACHE_HOME/pardes/blobs (else ~/.cache/pardes/blobs): what
-/// a client started again, or a second one on this machine, is not sent
-/// again. The cache directory and not the runtime one: the blobs are worth
-/// keeping across a logout or a reboot and safe to lose, and they can be
-/// hundreds of megabytes, which the runtime directory (a tmpfs, often
-/// capped at a tenth of memory and emptied at logout) is not for. A file
-/// read back is hashed again: one cut short, corrupted or planted is a
-/// miss and goes, never wrong content.
-pub const Cache = struct {
- dir: std.Io.Dir,
- /// Bytes it keeps at most: PARDES_BLOB_CACHE_MB, else 1 GiB.
- cap: u64,
-
- pub fn open(io: std.Io) ?Cache {
- var buf: [4096]u8 = undefined;
- const base = if (std.c.getenv("XDG_CACHE_HOME")) |x| std.mem.span(x) else "";
- const path = if (base.len > 0 and base[0] == '/')
- std.fmt.bufPrint(&buf, "{s}/pardes/blobs", .{base}) catch return null
- else if (std.c.getenv("HOME")) |home|
- std.fmt.bufPrint(&buf, "{s}/.cache/pardes/blobs", .{std.mem.span(home)}) catch return null
- else
- return null;
- const cwd = std.Io.Dir.cwd();
- cwd.createDirPath(io, std.fs.path.dirname(path).?) catch return null;
- cwd.createDir(io, path, .fromMode(0o700)) catch |err| if (err != error.PathAlreadyExists) return null;
- const mb = if (std.c.getenv("PARDES_BLOB_CACHE_MB")) |v| std.fmt.parseInt(u64, std.mem.span(v), 10) catch 1024 else 1024;
- return .{ .dir = cwd.openDir(io, path, .{ .iterate = true }) catch return null, .cap = mb << 20 };
- }
-
- fn name(hash: *const [32]u8) [64]u8 {
- return std.fmt.bytesToHex(hash.*, .lower);
- }
-
- /// The blob of `hash` read into `out` (its length) and hashed again.
- /// A file of another length or content is a miss, and goes; a hit's
- /// time is made now, so the least recently used go first (`trim`).
- pub fn get(c: Cache, io: std.Io, hash: *const [32]u8, out: []u8) bool {
- const file = name(hash);
- var f = c.dir.openFile(io, &file, .{}) catch return false;
- const ok = ok: {
- defer f.close(io);
- const st = f.stat(io) catch break :ok false;
- if (st.size != out.len) break :ok false;
- var reader = f.reader(io, &.{});
- const n = reader.interface.readSliceShort(out) catch break :ok false;
- if (n != out.len) break :ok false;
- var d: [32]u8 = undefined;
- std.crypto.hash.Blake3.hash(out, &d, .{});
- break :ok std.mem.eql(u8, &d, hash);
- };
- if (!ok) {
- c.dir.deleteFile(io, &file) catch {};
- return false;
- }
- c.dir.setTimestampsNow(io, &file, .{}) catch {};
- return true;
- }
-
- /// Whether a file of `len` bytes is there under `hash` (not read).
- pub fn has(c: Cache, io: std.Io, hash: *const [32]u8, len: usize) bool {
- const file = name(hash);
- const st = c.dir.statFile(io, &file, .{}) catch return false;
- return st.size == len;
- }
-
- var tmp_serial: std.atomic.Value(u32) = .init(0);
-
- /// `bytes` kept under `hash`: written aside and renamed into place, so
- /// that a reader never meets half a file (and would hash it wrong).
- pub fn put(c: Cache, io: std.Io, hash: *const [32]u8, bytes: []const u8) void {
- const file = name(hash);
- if (c.has(io, hash, bytes.len)) return;
- var buf: [96]u8 = undefined;
- const tmp = std.fmt.bufPrint(&buf, ".tmp-{d}-{d}-{s}", .{ std.c.getpid(), tmp_serial.fetchAdd(1, .monotonic), file[0..16] }) catch return;
- c.dir.writeFile(io, .{ .sub_path = tmp, .data = bytes }) catch {
- c.dir.deleteFile(io, tmp) catch {};
- return;
- };
- c.dir.rename(tmp, c.dir, &file, io) catch c.dir.deleteFile(io, tmp) catch {};
- }
-
- /// Room for `incoming` more bytes under the cap, the least recently
- /// used files deleted first (a hit makes its file's time now).
- /// ponytail: the directory scanned and sorted at each join's store;
- /// fine to tens of thousands of blobs, where an index would take over.
- pub fn trim(c: Cache, io: std.Io, incoming: u64) void {
- const File = struct { name: [80]u8, len: u8, size: u64, mtime: i96 };
- var files: std.ArrayListUnmanaged(File) = .empty;
- defer files.deinit(std.heap.c_allocator);
- var total: u64 = 0;
- var it = c.dir.iterate();
- while (it.next(io) catch null) |entry| {
- if (entry.kind != .file or entry.name.len > 80) continue;
- const st = c.dir.statFile(io, entry.name, .{}) catch continue;
- var f: File = .{ .name = undefined, .len = @intCast(entry.name.len), .size = st.size, .mtime = st.mtime.nanoseconds };
- @memcpy(f.name[0..f.len], entry.name);
- files.append(std.heap.c_allocator, f) catch return;
- total += st.size;
- }
- if (total + incoming <= c.cap) return;
- std.mem.sort(File, files.items, {}, struct {
- fn older(_: void, a: File, b: File) bool {
- return a.mtime < b.mtime;
- }
- }.older);
- for (files.items) |f| {
- if (total + incoming <= c.cap) break;
- c.dir.deleteFile(io, f.name[0..f.len]) catch continue;
- total -= f.size;
- }
- }
-};
+/// A far client's blobs on disk (Db.zig): cache.xitdb, by BLAKE3 hash.
+pub const Cache = pardes.Db.Cache;
fn said(why: []u8, comptime fmt: []const u8, args: anytype) ?Joined {
_ = std.fmt.bufPrint(why, fmt ++ "\x00", args) catch {};
@@ -3619,75 +3515,93 @@ test "a join's dump, its blobs put back where they go, is the session's dump" {
try std.testing.expectEqualSlices(u8, whole, joined.items);
}
-test "a cached blob is hashed again as it is read: one cut short, corrupted, planted or longer is a miss, and goes" {
+/// The blob cache of a test, in its own directory (never the user's).
+fn testCache(tmp: *std.testing.TmpDir, buf: *[4200]u8, cap: u64) !Cache {
+ var dir_buf: [std.fs.max_path_bytes]u8 = undefined;
+ const dir = dir_buf[0..try tmp.dir.realPath(std.testing.io, &dir_buf)];
+ return Cache.of(try std.fmt.bufPrint(buf, "{s}/" ++ Cache.name, .{dir}), cap);
+}
+
+test "a cached blob is hashed again as it is read: one kept cut short, corrupted, planted or longer is a miss, and goes" {
const io = std.testing.io;
- var tmp = std.testing.tmpDir(.{ .iterate = true });
+ var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
- const c: Cache = .{ .dir = tmp.dir, .cap = 1 << 20 };
+ var path: [4200]u8 = undefined;
+ const c = try testCache(&tmp, &path, 1 << 20);
var text: [6000]u8 = undefined;
for (&text, 0..) |*b, i| b.* = @truncate(i *% 31 +% i / 7);
var hash: [32]u8 = undefined;
std.crypto.hash.Blake3.hash(&text, &hash, .{});
- const file = Cache.name(&hash);
var out: [6000]u8 = undefined;
try std.testing.expect(!c.get(io, &hash, &out));
- c.put(io, &hash, &text);
- try std.testing.expect(c.has(io, &hash, text.len));
- try std.testing.expect(c.get(io, &hash, &out));
- try std.testing.expectEqualSlices(u8, &text, &out);
var corrupted = text;
corrupted[4000] ^= 0x20;
const planted: [6000]u8 = @splat('x');
var longer: [6001]u8 = undefined;
@memcpy(longer[0..6000], &text);
longer[6000] = '\n';
+ // Each kept under the text's hash, as a torn write or another hand
+ // would leave it: read, it is a miss, and gone.
for ([_][]const u8{ text[0..3000], &corrupted, &planted, &longer }) |bad| {
- try tmp.dir.writeFile(io, .{ .sub_path = &file, .data = bad });
+ c.put(io, &.{.{ .hash = hash, .bytes = bad }}, &.{});
+ try std.testing.expect(c.has(io, &hash, bad.len));
@memset(&out, 0);
try std.testing.expect(!c.get(io, &hash, &out));
- try std.testing.expectError(error.FileNotFound, tmp.dir.statFile(io, &file, .{}));
+ try std.testing.expect(!c.has(io, &hash, bad.len));
}
- // ...and put back, it is read again.
- c.put(io, &hash, &text);
+ // ...and put right, it is read again.
+ c.put(io, &.{.{ .hash = hash, .bytes = &text }}, &.{});
try std.testing.expect(c.get(io, &hash, &out));
try std.testing.expectEqualSlices(u8, &text, &out);
}
-test "the blob cache stays under its cap, the least recently used going first" {
+test "the blob cache stays under its cap, the least recently used going first, the file compacted" {
const io = std.testing.io;
- var tmp = std.testing.tmpDir(.{ .iterate = true });
+ var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
- const c: Cache = .{ .dir = tmp.dir, .cap = 3 * 5000 };
- var blobs: [3][5000]u8 = undefined;
- var hashes: [3][32]u8 = undefined;
+ var path: [4200]u8 = undefined;
+ const c = try testCache(&tmp, &path, 3 * 5000);
+ var blobs: [4][5000]u8 = undefined;
+ var hashes: [4][32]u8 = undefined;
for (&blobs, &hashes, 0..) |*b, *h, k| {
@memset(b, @intCast('a' + k));
std.crypto.hash.Blake3.hash(b, h, .{});
- c.put(io, h, b);
- // Each older than the next, a thousand seconds apart.
- const name = Cache.name(h);
- try tmp.dir.setTimestamps(io, &name, .{ .modify_timestamp = .{ .new = .{ .nanoseconds = @as(i96, @intCast(k + 1)) * 1000 * std.time.ns_per_s } } });
}
+ for (0..3) |k| c.put(io, &.{.{ .hash = hashes[k], .bytes = &blobs[k] }}, &.{});
// The oldest read: it is the newest now.
var out: [5000]u8 = undefined;
try std.testing.expect(c.get(io, &hashes[0], &out));
- c.trim(io, 5000);
+ c.put(io, &.{}, &.{hashes[0]});
+ // Under the cap: nothing goes.
+ c.trim(io);
+ for (hashes[0..3]) |*h| try std.testing.expect(c.has(io, h, 5000));
+ // One more is past it: the least recently used goes.
+ c.put(io, &.{.{ .hash = hashes[3], .bytes = &blobs[3] }}, &.{});
+ const before = (try std.Io.Dir.cwd().statFile(io, c.path(), .{})).size;
+ c.trim(io);
try std.testing.expect(c.has(io, &hashes[0], 5000));
try std.testing.expect(!c.has(io, &hashes[1], 5000));
- try std.testing.expect(c.has(io, &hashes[2], 5000));
- // A file not a blob's counts, and goes by its time as well.
- const junk: [9000]u8 = @splat(0);
- try tmp.dir.writeFile(io, .{ .sub_path = ".tmp-1-1-0123456789abcdef", .data = &junk });
- try tmp.dir.setTimestamps(io, ".tmp-1-1-0123456789abcdef", .{ .modify_timestamp = .{ .new = .{ .nanoseconds = 500 * std.time.ns_per_s } } });
- c.trim(io, 5000);
- try std.testing.expectError(error.FileNotFound, tmp.dir.statFile(io, ".tmp-1-1-0123456789abcdef", .{}));
- try std.testing.expect(c.has(io, &hashes[0], 5000) and c.has(io, &hashes[2], 5000));
- // Under the cap with room to spare: nothing goes.
- c.trim(io, 0);
- try std.testing.expect(c.has(io, &hashes[0], 5000) and c.has(io, &hashes[2], 5000));
- // Room for the whole cap: everything goes, the older first.
- c.trim(io, c.cap);
- try std.testing.expect(!c.has(io, &hashes[0], 5000) and !c.has(io, &hashes[2], 5000));
+ try std.testing.expect(c.has(io, &hashes[2], 5000) and c.has(io, &hashes[3], 5000));
+ try std.testing.expect((try std.Io.Dir.cwd().statFile(io, c.path(), .{})).size < before);
+ // Room for nothing: everything goes.
+ Cache.of(c.path(), 0).trim(io);
+ for (&hashes) |*h| try std.testing.expect(!c.has(io, h, 5000));
+}
+
+test "a blob cache that is no database is started afresh" {
+ const io = std.testing.io;
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ var path: [4200]u8 = undefined;
+ const c = try testCache(&tmp, &path, 1 << 20);
+ try std.Io.Dir.cwd().writeFile(io, .{ .sub_path = c.path(), .data = "xit but not a database, cut short" });
+ const text: [5000]u8 = @splat('t');
+ var hash: [32]u8 = undefined;
+ std.crypto.hash.Blake3.hash(&text, &hash, .{});
+ var out: [5000]u8 = undefined;
+ try std.testing.expect(!c.get(io, &hash, &out));
+ c.put(io, &.{.{ .hash = hash, .bytes = &text }}, &.{});
+ try std.testing.expect(c.get(io, &hash, &out));
}
/// A far join over a socket pair, the session's writer and reader threads
@@ -3785,9 +3699,10 @@ test "a far join is sent each blob once: joined again with them on disk or in th
}
try std.testing.expect(distinct.count() >= 3);
- var tmp = std.testing.tmpDir(.{ .iterate = true });
+ var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
- var have: Have = .{ .io = io, .cache = .{ .dir = tmp.dir, .cap = 64 << 20 } };
+ var path: [4200]u8 = undefined;
+ var have: Have = .{ .io = io, .cache = try testCache(&tmp, &path, 64 << 20) };
defer {
have.cache = null;
have.deinit();
@@ -3813,12 +3728,13 @@ test "a far join is sent each blob once: joined again with them on disk or in th
try std.testing.expectEqual(distinct.count(), j.from_disk);
try std.testing.expectEqual(@as(u64, 0), j.sent_bytes);
endJoin(&j, &have);
- // The biggest's file corrupted: it alone is sent again.
- const name = Cache.name(&biggest);
+ // The biggest kept wrong (read back once as another length, it goes;
+ // then junk under its hash): it alone is sent again.
const junk = try gpa.alloc(u8, biggest_len);
defer gpa.free(junk);
@memset(junk, 'z');
- try tmp.dir.writeFile(io, .{ .sub_path = &name, .data = junk });
+ try std.testing.expect(!have.cache.?.get(io, &biggest, junk[0 .. biggest_len - 1]));
+ have.cache.?.put(io, &.{.{ .hash = biggest, .bytes = junk }}, &.{});
j = try farJoinOnce(&ps, a.core, &have);
try std.testing.expectEqualSlices(u8, whole, j.dump);
try std.testing.expectEqual(distinct.count() - 1, j.from_disk);